All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
scanutils.cpp File Reference
#include <ctype.h>
#include <math.h>
#include <stdarg.h>
#include <stddef.h>
#include <string.h>
#include <limits.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "scanutils.h"
#include "tprintf.h"

Go to the source code of this file.

Typedefs

typedef long off_t
 

Enumerations

enum  Flags { FL_SPLAT = 0x01, FL_INV = 0x02, FL_WIDTH = 0x04, FL_MINUS = 0x08 }
 
enum  Ranks {
  RANK_CHAR = -2, RANK_SHORT = -1, RANK_INT = 0, RANK_LONG = 1,
  RANK_LONGLONG = 2, RANK_PTR = INT_MAX
}
 
enum  Bail { BAIL_NONE = 0, BAIL_EOF, BAIL_ERR }
 

Functions

size_t LongBit ()
 
uintmax_t streamtoumax (FILE *s, int base)
 
double streamtofloat (FILE *s)
 
double strtofloat (const char *s)
 
int tfscanf (FILE *stream, const char *format,...)
 

Variables

enum Ranks kMinRank = RANK_CHAR
 
enum Ranks kMaxRank = RANK_LONGLONG
 
enum Ranks kIntMaxRank = RANK_LONGLONG
 
enum Ranks kSizeTRank = RANK_LONG
 
enum Ranks kPtrDiffRank = RANK_LONG
 

Typedef Documentation

typedef long off_t

Definition at line 38 of file scanutils.cpp.

Enumeration Type Documentation

enum Bail
Enumerator
BAIL_NONE 
BAIL_EOF 
BAIL_ERR 

Definition at line 64 of file scanutils.cpp.

64  {
65  BAIL_NONE = 0, // No error condition
66  BAIL_EOF, // Hit EOF
67  BAIL_ERR // Conversion mismatch
68 };
enum Flags
Enumerator
FL_SPLAT 
FL_INV 
FL_WIDTH 
FL_MINUS 

Definition at line 41 of file scanutils.cpp.

41  {
42  FL_SPLAT = 0x01, // Drop the value, do not assign
43  FL_INV = 0x02, // Character-set with inverse
44  FL_WIDTH = 0x04, // Field width specified
45  FL_MINUS = 0x08, // Negative number
46 };
enum Ranks
Enumerator
RANK_CHAR 
RANK_SHORT 
RANK_INT 
RANK_LONG 
RANK_LONGLONG 
RANK_PTR 

Definition at line 48 of file scanutils.cpp.

48  {
49  RANK_CHAR = -2,
50  RANK_SHORT = -1,
51  RANK_INT = 0,
52  RANK_LONG = 1,
53  RANK_LONGLONG = 2,
54  RANK_PTR = INT_MAX // Special value used for pointers
55 };

Function Documentation

size_t LongBit ( )
inline

Definition at line 71 of file scanutils.cpp.

71  {
72  return CHAR_BIT * sizeof(long);
73 }
double streamtofloat ( FILE *  s)

Definition at line 147 of file scanutils.cpp.

147  {
148  int minus = 0;
149  int v = 0;
150  int d, c = 0;
151  int k = 1;
152  int w = 0;
153 
154  for (c = fgetc(s);
155  isspace(static_cast<unsigned char>(c)) && (c != EOF);
156  c = fgetc(s));
157 
158  // Single optional + or -
159  if (c == '-' || c == '+') {
160  minus = (c == '-');
161  c = fgetc(s);
162  }
163 
164  // Actual number parsing
165  for (; c != EOF && (d = DigitValue(c, 10)) >= 0; c = fgetc(s))
166  v = v*10 + d;
167  if (c == '.') {
168  for (c = fgetc(s); c != EOF && (d = DigitValue(c, 10)) >= 0; c = fgetc(s)) {
169  w = w*10 + d;
170  k *= 10;
171  }
172  }
173  double f = static_cast<double>(v)
174  + static_cast<double>(w) / static_cast<double>(k);
175  if (c == 'e' || c == 'E') {
176  c = fgetc(s);
177  int expsign = 1;
178  if (c == '-' || c == '+') {
179  expsign = (c == '-') ? -1 : 1;
180  c = fgetc(s);
181  }
182  int exponent = 0;
183  for (; (c != EOF) && (d = DigitValue(c, 10)) >= 0; c = fgetc(s)) {
184  exponent = exponent * 10 + d;
185  }
186  exponent *= expsign;
187  f *= pow(10.0, static_cast<double>(exponent));
188  }
189  ungetc(c, s);
190 
191  return minus ? -f : f;
192 }
uintmax_t streamtoumax ( FILE *  s,
int  base 
)

Definition at line 106 of file scanutils.cpp.

106  {
107  int minus = 0;
108  uintmax_t v = 0;
109  int d, c = 0;
110 
111  for (c = fgetc(s);
112  isspace(static_cast<unsigned char>(c)) && (c != EOF);
113  c = fgetc(s)) {}
114 
115  // Single optional + or -
116  if (c == '-' || c == '+') {
117  minus = (c == '-');
118  c = fgetc(s);
119  }
120 
121  // Assign correct base
122  if (base == 0) {
123  if (c == '0') {
124  c = fgetc(s);
125  if (c == 'x' || c == 'X') {
126  base = 16;
127  c = fgetc(s);
128  } else {
129  base = 8;
130  }
131  }
132  } else if (base == 16) {
133  if (c == '0') {
134  c = fgetc(s);
135  if (c == 'x' || c == 'X') c = fgetc(s);
136  }
137  }
138 
139  // Actual number parsing
140  for (; (c != EOF) && (d = DigitValue(c, base)) >= 0; c = fgetc(s))
141  v = v*base + d;
142 
143  ungetc(c, s);
144  return minus ? -v : v;
145 }
double strtofloat ( const char *  s)

Definition at line 194 of file scanutils.cpp.

194  {
195  int minus = 0;
196  int v = 0;
197  int d;
198  int k = 1;
199  int w = 0;
200 
201  while(*s && isspace(static_cast<unsigned char>(*s))) s++;
202 
203  // Single optional + or -
204  if (*s == '-' || *s == '+') {
205  minus = (*s == '-');
206  s++;
207  }
208 
209  // Actual number parsing
210  for (; *s && (d = DigitValue(*s, 10)) >= 0; s++)
211  v = v*10 + d;
212  if (*s == '.') {
213  for (++s; *s && (d = DigitValue(*s, 10)) >= 0; s++) {
214  w = w*10 + d;
215  k *= 10;
216  }
217  }
218  if (*s == 'e' || *s == 'E')
219  tprintf("WARNING: Scientific Notation not supported!");
220 
221  double f = static_cast<double>(v)
222  + static_cast<double>(w) / static_cast<double>(k);
223 
224  return minus ? -f : f;
225 }
#define tprintf(...)
Definition: tprintf.h:31
int tfscanf ( FILE *  stream,
const char *  format,
  ... 
)

fscanf variant to ensure correct reading regardless of locale.

tfscanf parse a file stream according to the given format. See the fscanf manpage for more information, as this function attempts to mimic its behavior.

Note
Note that scientific floating-point notation is not supported.

Definition at line 229 of file scanutils.cpp.

229  {
230  va_list ap;
231  int rv;
232 
233  va_start(ap, format);
234  rv = tvfscanf(stream, format, ap);
235  va_end(ap);
236 
237  return rv;
238 }

Variable Documentation

enum Ranks kIntMaxRank = RANK_LONGLONG

Definition at line 60 of file scanutils.cpp.

enum Ranks kMaxRank = RANK_LONGLONG

Definition at line 58 of file scanutils.cpp.

enum Ranks kMinRank = RANK_CHAR

Definition at line 57 of file scanutils.cpp.

enum Ranks kPtrDiffRank = RANK_LONG

Definition at line 62 of file scanutils.cpp.

enum Ranks kSizeTRank = RANK_LONG

Definition at line 61 of file scanutils.cpp.