getf.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2003 by egnite Software GmbH. All rights reserved.
00003  *
00004  * Copyright (c) 1990, 1993
00005  *      The Regents of the University of California.  All rights reserved.
00006  *
00007  * This code is partly derived from software contributed to Berkeley by
00008  * Chris Torek, but heavily rewritten for Nut/OS.
00009  *
00010  * Redistribution and use in source and binary forms, with or without
00011  * modification, are permitted provided that the following conditions
00012  * are met:
00013  *
00014  * 1. Redistributions of source code must retain the above copyright
00015  *    notice, this list of conditions and the following disclaimer.
00016  * 2. Redistributions in binary form must reproduce the above copyright
00017  *    notice, this list of conditions and the following disclaimer in the
00018  *    documentation and/or other materials provided with the distribution.
00019  * 3. Neither the name of the copyright holders nor the names of
00020  *    contributors may be used to endorse or promote products derived
00021  *    from this software without specific prior written permission.
00022  *
00023  * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
00024  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
00027  * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00030  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00031  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00033  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00034  * SUCH DAMAGE.
00035  *
00036  * For additional information see http://www.ethernut.de/
00037  *
00038  */
00039 
00040 /*
00041  * $Log: getf.c,v $
00042  * Revision 1.6  2008/07/09 14:20:25  haraldkipp
00043  * Added support for length modifier h. According to C99, F, G and X type
00044  * specifiers should work like f, g, and x.
00045  *
00046  * Revision 1.5  2006/05/15 15:31:11  freckle
00047  * Take care of first character after integer
00048  *
00049  * Revision 1.4  2006/05/05 15:43:07  freckle
00050  * Fixes for bugs #1477658 and #1477676
00051  *
00052  * Revision 1.3  2004/11/24 15:24:07  haraldkipp
00053  * Floating point configuration works again.
00054  *
00055  * Revision 1.2  2004/02/28 20:14:38  drsung
00056  * Merge from nut-3_4-release b/c of bugfixes.
00057  *
00058  * Revision 1.1.1.1.2.1  2004/02/28 18:47:34  drsung
00059  * Several bugfixes provided by Francois Rademeyer.
00060  * - "%%" didnt work
00061  * - integer parsing corrected
00062  * - support for "%u"
00063  *
00064  * Revision 1.1.1.1  2003/05/09 14:40:29  haraldkipp
00065  * Initial using 3.2.1
00066  *
00067  * Revision 1.1  2003/02/04 17:49:07  harald
00068  * *** empty log message ***
00069  *
00070  */
00071 
00072 #include <cfg/crt.h>
00073 
00074 #include "nut_io.h"
00075 
00076 #include <ctype.h>
00077 #include <limits.h>
00078 #include <stdlib.h>
00079 #include <string.h>
00080 
00085 
00086 #define CF_LONG     0x01    /* 1: long or double */
00087 #define CF_SUPPRESS 0x02    /* suppress assignment */
00088 #define CF_SIGNOK   0x04    /* +/- is (still) legal */
00089 #define CF_NDIGITS  0x08    /* no digits detected */
00090 #define CF_PFXOK    0x10    /* 0x prefix is (still) legal */
00091 #define CF_NZDIGITS 0x20    /* no zero digits detected */
00092 #define CF_DPTOK    0x10    /* (float) decimal point is still legal */
00093 #define CF_EXPOK    0x20    /* (float) exponent (e+3, etc) still legal */
00094 
00095 /*
00096  * Conversion types.
00097  */
00098 #define CT_CHAR     0       /* %c conversion */
00099 #define CT_STRING   2       /* %s conversion */
00100 #define CT_INT      3       /* integer, i.e., strtoq or strtouq */
00101 #define CT_FLOAT    4       /* floating, i.e., strtod */
00102 
00116 int _getf(int _getb(int, void *, size_t), int fd, CONST char *fmt, va_list ap)
00117 {
00118 
00119     u_char cf;                  /* Character from format. */
00120     u_char ch;                  /* Character from input. */
00121     size_t width;               /* Field width. */
00122     u_char flags;               /* CF_ flags. */
00123     u_char ct;                  /* CT_ conversion type. */
00124     u_char base;                /* Conversion base. */
00125     u_char ccnt = 0;            /* Number of conversions. */
00126     u_char acnt = 0;            /* Number of fields assigned. */
00127     u_char hcnt;                /* Number of 'half' specifiers. */
00128     char buf[16];               /* Temporary buffer. */
00129     char *cp;                   /* Temporary pointer. */
00130     u_char ch_ready = 0;        /* Character available from previous peek
00131                                    This is necessary as a hack to get around a missing ungetc */
00132     
00133     for (;;) {
00134         cf = *fmt++;
00135         if (cf == 0)
00136             return acnt;
00137 
00138         /*
00139          * Match whitespace.
00140          */
00141         if (isspace(cf)) {
00142             for (;;) {
00143                 if (_getb(fd, &ch, 1) != 1)
00144                     break;
00145                 if (!isspace(ch)) {
00146                     ch_ready = 1; /* character avail without read */
00147                     break;
00148                 }
00149             }
00150             continue;
00151         }
00152 
00153         /*
00154          * Match literals.
00155          */
00156         if (cf != '%') {
00157             if (!ch_ready && _getb(fd, &ch, 1) != 1)
00158                 return ccnt ? acnt : EOF;
00159             if (ch != cf)
00160                 return acnt;
00161             ch_ready = 0; /* character used now */
00162             continue;
00163         }
00164 
00165         cf = *fmt++;
00166         /*
00167          * Check for a '%' literal.
00168          */
00169         if (cf == '%') {
00170             if (!ch_ready && _getb(fd, &ch, 1) != 1)
00171                 return ccnt ? acnt : EOF;
00172             if (ch != cf)
00173                 return acnt;
00174             ch_ready = 0; /* character used now */
00175             continue;
00176         }
00177 
00178         /*
00179          * Collect modifiers.
00180          */
00181         width = 0;
00182         flags = 0;
00183         hcnt = 0;
00184         for (;;) {
00185             if (cf == '*')
00186                 flags |= CF_SUPPRESS;
00187             else if (cf == 'l')
00188                 flags |= CF_LONG;
00189             else if (cf == 'h')
00190                 hcnt++;
00191             else if (cf >= '0' && cf <= '9')
00192                 width = width * 10 + cf - '0';
00193             else
00194                 break;
00195 
00196             cf = *fmt++;
00197         }
00198 
00199         /*
00200          * Determine the types.
00201          */
00202         base = 10;
00203         ct = CT_INT;
00204         switch (cf) {
00205         case '\0':
00206             return EOF;
00207         case 's':
00208             ct = CT_STRING;
00209             break;
00210         case 'c':
00211             ct = CT_CHAR;
00212             break;
00213         case 'i':
00214             base = 0;
00215             break;
00216         case 'u':
00217             base = 10;
00218             break;
00219         case 'o':
00220             base = 8;
00221             break;
00222         case 'x':
00223         case 'X':
00224             flags |= CF_PFXOK;
00225             base = 16;
00226             break;
00227 #ifdef STDIO_FLOATING_POINT
00228         case 'e':
00229         case 'f':
00230         case 'F':
00231         case 'g':
00232         case 'G':
00233             ct = CT_FLOAT;
00234             break;
00235 #endif
00236         }
00237 
00238         /*
00239          * Process characters.
00240          */
00241         if (ct == CT_CHAR) {
00242             if (width == 0)
00243                 width = 1;
00244             if (flags & CF_SUPPRESS) {
00245                 while (width > sizeof(buf)) {
00246                     if (!ch_ready && _getb(fd, buf, sizeof(buf)) <= 0)
00247                         return ccnt ? acnt : EOF;
00248                     width -= sizeof(buf);
00249                 }
00250                 if (width)
00251                     if (!ch_ready && _getb(fd, &buf, width) <= 0)
00252                         return ccnt ? acnt : EOF;
00253             } else {
00254                 if (!ch_ready && _getb(fd, (void *) va_arg(ap, char *), width) <= 0)
00255                      return ccnt ? acnt : EOF;
00256                 acnt++;
00257             }
00258             ch_ready = 0; /* character used now */
00259             ccnt++;
00260             continue;
00261         }
00262 
00263         /*
00264          * Skip whitespaces.
00265          */
00266         if (!ch_ready && _getb(fd, &ch, 1) != 1)
00267             return ccnt ? acnt : EOF;
00268         ch_ready = 0; /* no character ready anymore */
00269         while (isspace(ch)) {
00270             if (_getb(fd, &ch, 1) != 1)
00271                 return ccnt ? acnt : EOF;
00272         }
00273 
00274         /*
00275          * Process string.
00276          */
00277         if (ct == CT_STRING) {
00278             if (width == 0)
00279                 width = 0xFFFF;
00280             if (flags & CF_SUPPRESS) {
00281                 while (!isspace(ch)) {
00282                     if (--width == 0)
00283                         break;
00284                     if (_getb(fd, &ch, 1) != 1)
00285                         break;
00286                 }
00287             } else {
00288                 cp = va_arg(ap, char *);
00289                 while (!isspace(ch)) {
00290                     *cp++ = ch;
00291                     if (--width == 0)
00292                         break;
00293                     if (_getb(fd, &ch, 1) != 1)
00294                         break;
00295                 }
00296                 *cp = 0;
00297                 acnt++;
00298             }
00299             ccnt++;
00300         }
00301 
00302         /*
00303          * Process integer.
00304          */
00305         else if (ct == CT_INT) {
00306             if (width == 0 || width > sizeof(buf) - 1)
00307                 width = sizeof(buf) - 1;
00308 
00309             flags |= CF_SIGNOK | CF_NDIGITS | CF_NZDIGITS;
00310 
00311             for (cp = buf; width; width--) {
00312                 if (ch == '0') {
00313                     if (base == 0) {
00314                         base = 8;
00315                         flags |= CF_PFXOK;
00316                     }
00317                     if (flags & CF_NZDIGITS)
00318                         flags &= ~(CF_SIGNOK | CF_NZDIGITS | CF_NDIGITS);
00319                     else
00320                         flags &= ~(CF_SIGNOK | CF_PFXOK | CF_NDIGITS);
00321                 } else if (ch >= '1' && ch <= '7') {
00322                     if (base == 0)
00323                         base = 10;
00324                     flags &= ~(CF_SIGNOK | CF_PFXOK | CF_NDIGITS);
00325                 } else if (ch == '8' || ch == '9') {
00326                     if (base == 0)
00327                         base = 10;
00328                     else if (base <= 8)
00329                         break;
00330                     flags &= ~(CF_SIGNOK | CF_PFXOK | CF_NDIGITS);
00331                 } else if ((ch >= 'A' && ch <= 'F')
00332                            || (ch >= 'a' && ch <= 'f')) {
00333                     if (base <= 10)
00334                         break;
00335                     flags &= ~(CF_SIGNOK | CF_PFXOK | CF_NDIGITS);
00336                 } else if (ch == '-' || ch == '+') {
00337                     if ((flags & CF_SIGNOK) == 0)
00338                         break;
00339                     flags &= ~CF_SIGNOK;
00340                 } else if (ch == 'x' || ch == 'X') {
00341                     if ((flags & CF_PFXOK) == 0)
00342                         break;
00343                     base = 16;
00344                     flags &= ~CF_PFXOK;
00345                 } else {
00346                     ch_ready = 1; /* character avail without read */
00347                     break;
00348                 }
00349                 *cp++ = ch;
00350                 if (width > 1) {
00351                     if (_getb(fd, &ch, 1) != 1)
00352                         break;
00353                 }
00354             }
00355 
00356             if (flags & CF_NDIGITS)
00357                 return acnt;
00358 
00359             if ((flags & CF_SUPPRESS) == 0) {
00360                 u_long res;
00361 
00362                 *cp = 0;
00363                 res = strtol(buf, 0, base);
00364                 if (flags & CF_LONG)
00365                     *va_arg(ap, long *) = res;
00366                 else if (hcnt == 1)
00367                     *va_arg(ap, short *) = res;
00368                 else if (hcnt)
00369                     *va_arg(ap, char *) = res;
00370                 else
00371                     *va_arg(ap, int *) = res;
00372                 acnt++;
00373             }
00374             ccnt++;
00375         }
00376 #ifdef STDIO_FLOATING_POINT
00377         else if (ct == CT_FLOAT) {
00378             if (width == 0 || width > sizeof(buf) - 1)
00379                 width = sizeof(buf) - 1;
00380             flags |= CF_SIGNOK | CF_NDIGITS | CF_DPTOK | CF_EXPOK;
00381             for (cp = buf; width; width--) {
00382                 if (ch >= '0' && ch <= '9')
00383                     flags &= ~(CF_SIGNOK | CF_NDIGITS);
00384                 else if (ch == '+' || ch == '-') {
00385                     if ((flags & CF_SIGNOK) == 0)
00386                         break;
00387                     flags &= ~CF_SIGNOK;
00388                 } else if (ch == '.') {
00389                     if ((flags & CF_DPTOK) == 0)
00390                         break;
00391                     flags &= ~(CF_SIGNOK | CF_DPTOK);
00392                 } else if (ch == 'e' || ch == 'E') {
00393                     if ((flags & (CF_NDIGITS | CF_EXPOK)) != CF_EXPOK)
00394                         break;
00395                     flags = (flags & ~(CF_EXPOK | CF_DPTOK)) | CF_SIGNOK | CF_NDIGITS;
00396                 } else {
00397                     ch_ready = 1; /* character avail without read */
00398                     break;
00399                 }
00400                 *cp++ = ch;
00401                 if (_getb(fd, &ch, 1) != 1)
00402                     break;
00403             }
00404             if (flags & CF_NDIGITS) {
00405                 if (flags & CF_EXPOK)
00406                     return acnt;
00407             }
00408             if ((flags & CF_SUPPRESS) == 0) {
00409                 double res;
00410 
00411                 *cp = 0;
00412                 res = strtod(buf, 0);
00413                 *va_arg(ap, double *) = res;
00414                 acnt++;
00415             }
00416             ccnt++;
00417         }
00418 #endif                          /* STDIO_FLOATING_POINT */
00419     }
00420 }
00421 

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/