tesseract  3.04.01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 42 of file scanutils.cpp.

Enumeration Type Documentation

enum Bail
Enumerator
BAIL_NONE 
BAIL_EOF 
BAIL_ERR 

Definition at line 68 of file scanutils.cpp.

68  {
69  BAIL_NONE = 0, // No error condition
70  BAIL_EOF, // Hit EOF
71  BAIL_ERR // Conversion mismatch
72 };
enum Flags
Enumerator
FL_SPLAT 
FL_INV 
FL_WIDTH 
FL_MINUS 

Definition at line 45 of file scanutils.cpp.

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

Definition at line 52 of file scanutils.cpp.

52  {
53  RANK_CHAR = -2,
54  RANK_SHORT = -1,
55  RANK_INT = 0,
56  RANK_LONG = 1,
57  RANK_LONGLONG = 2,
58  RANK_PTR = INT_MAX // Special value used for pointers
59 };

Function Documentation

size_t LongBit ( )
inline

Definition at line 75 of file scanutils.cpp.

75  {
76  return CHAR_BIT * sizeof(long);
77 }
double streamtofloat ( FILE *  s)

Definition at line 151 of file scanutils.cpp.

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

Definition at line 110 of file scanutils.cpp.

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

Definition at line 198 of file scanutils.cpp.

198  {
199  int minus = 0;
200  int v = 0;
201  int d;
202  int k = 1;
203  int w = 0;
204 
205  while(*s && isspace(static_cast<unsigned char>(*s))) s++;
206 
207  // Single optional + or -
208  if (*s == '-' || *s == '+') {
209  minus = (*s == '-');
210  s++;
211  }
212 
213  // Actual number parsing
214  for (; *s && (d = DigitValue(*s, 10)) >= 0; s++)
215  v = v*10 + d;
216  if (*s == '.') {
217  for (++s; *s && (d = DigitValue(*s, 10)) >= 0; s++) {
218  w = w*10 + d;
219  k *= 10;
220  }
221  }
222  if (*s == 'e' || *s == 'E')
223  tprintf("WARNING: Scientific Notation not supported!");
224 
225  double f = static_cast<double>(v)
226  + static_cast<double>(w) / static_cast<double>(k);
227 
228  return minus ? -f : f;
229 }
#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 233 of file scanutils.cpp.

233  {
234  va_list ap;
235  int rv;
236 
237  va_start(ap, format);
238  rv = tvfscanf(stream, format, ap);
239  va_end(ap);
240 
241  return rv;
242 }

Variable Documentation

enum Ranks kIntMaxRank = RANK_LONGLONG

Definition at line 64 of file scanutils.cpp.

enum Ranks kMaxRank = RANK_LONGLONG

Definition at line 62 of file scanutils.cpp.

enum Ranks kMinRank = RANK_CHAR

Definition at line 61 of file scanutils.cpp.

enum Ranks kPtrDiffRank = RANK_LONG

Definition at line 66 of file scanutils.cpp.

enum Ranks kSizeTRank = RANK_LONG

Definition at line 65 of file scanutils.cpp.