Linear Methods for Image Interpolation
strutil.c
Go to the documentation of this file.
00001 
00016 #include <ctype.h>
00017 #include <math.h>
00018 #include "strutil.h"
00019 
00031 int EatWhitespace(const char **StrPtr)
00032 {
00033     const char *Str = *StrPtr;
00034 
00035     while(isspace(*Str))
00036         Str++;
00037 
00038     *StrPtr = Str;
00039     return (*Str != '\0');
00040 }
00041 
00042 
00055 int ParseNumber(double *Number, const char **StrPtr, int FloatAllowed)
00056 {
00057     const char *Str = *StrPtr;
00058     double Accum = 0, Div = 1, Exponent = 0;
00059     int Sign = 1, ExponentSign = 1;
00060     char c;
00061 
00062 
00063     /* Eat leading whitespace */
00064     if(!EatWhitespace(&Str))
00065         return 0;
00066 
00067     if(*Str == '-')        /* Read sign */
00068     {
00069         Sign = -1;
00070         Str++;
00071     }
00072     else if(*Str == '+')
00073         Str++;
00074 
00075     /* Read one or more digits appearing left of the decimal point */
00076     if(isdigit(c = *Str))
00077         Accum = c - '0';
00078     else
00079         return 0;               /* First character is not a digit */
00080 
00081     while(isdigit(c = *(++Str)))
00082         Accum = 10*Accum + (c - '0');
00083 
00084     if(c == '.')                /* There is a decimal point */
00085     {
00086         if(!FloatAllowed)
00087             return 0;
00088 
00089         /* Read zero or more digits appearing right of the decimal point */
00090         while(isdigit(c = *(++Str)))
00091         {
00092             Div *= 10;
00093             Accum += (c - '0')/Div;
00094         }
00095     }
00096 
00097     if(c == 'e' || c == 'E')    /* There is an exponent */
00098     {
00099         if(!FloatAllowed)
00100             return 0;
00101 
00102         Str++;
00103 
00104         if(*Str == '-')      /* Read exponent sign */
00105         {
00106             ExponentSign = -1;
00107             Str++;
00108         }
00109         else if(*Str == '+')
00110             Str++;
00111 
00112         /* Read digits in the exponent */
00113         if(isdigit(c = *Str))
00114         {
00115             Exponent = c - '0';
00116 
00117             while(isdigit(c = *(++Str)))
00118                 Exponent = 10*Exponent + (c - '0');
00119 
00120             Exponent *= ExponentSign;
00121             Accum = Accum * pow(10, Exponent);
00122         }
00123         else
00124             return 0;
00125     }
00126 
00127     *Number = Sign*Accum;
00128     *StrPtr = Str;
00129     return 1;
00130 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines