Linear Methods for Image Interpolation
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
strutil.c
Go to the documentation of this file.
1 
16 #include <ctype.h>
17 #include <math.h>
18 #include "strutil.h"
19 
31 int EatWhitespace(const char **StrPtr)
32 {
33  const char *Str = *StrPtr;
34 
35  while(isspace(*Str))
36  Str++;
37 
38  *StrPtr = Str;
39  return (*Str != '\0');
40 }
41 
42 
55 int ParseNumber(double *Number, const char **StrPtr, int FloatAllowed)
56 {
57  const char *Str = *StrPtr;
58  double Accum = 0, Div = 1, Exponent = 0;
59  int Sign = 1, ExponentSign = 1;
60  char c;
61 
62 
63  /* Eat leading whitespace */
64  if(!EatWhitespace(&Str))
65  return 0;
66 
67  if(*Str == '-') /* Read sign */
68  {
69  Sign = -1;
70  Str++;
71  }
72  else if(*Str == '+')
73  Str++;
74 
75  /* Read one or more digits appearing left of the decimal point */
76  if(isdigit(c = *Str))
77  Accum = c - '0';
78  else
79  return 0; /* First character is not a digit */
80 
81  while(isdigit(c = *(++Str)))
82  Accum = 10*Accum + (c - '0');
83 
84  if(c == '.') /* There is a decimal point */
85  {
86  if(!FloatAllowed)
87  return 0;
88 
89  /* Read zero or more digits appearing right of the decimal point */
90  while(isdigit(c = *(++Str)))
91  {
92  Div *= 10;
93  Accum += (c - '0')/Div;
94  }
95  }
96 
97  if(c == 'e' || c == 'E') /* There is an exponent */
98  {
99  if(!FloatAllowed)
100  return 0;
101 
102  Str++;
103 
104  if(*Str == '-') /* Read exponent sign */
105  {
106  ExponentSign = -1;
107  Str++;
108  }
109  else if(*Str == '+')
110  Str++;
111 
112  /* Read digits in the exponent */
113  if(isdigit(c = *Str))
114  {
115  Exponent = c - '0';
116 
117  while(isdigit(c = *(++Str)))
118  Exponent = 10*Exponent + (c - '0');
119 
120  Exponent *= ExponentSign;
121  Accum = Accum * pow(10, Exponent);
122  }
123  else
124  return 0;
125  }
126 
127  *Number = Sign*Accum;
128  *StrPtr = Str;
129  return 1;
130 }