tesseract  4.00.00dev
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.

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
 

Enumeration Type Documentation

◆ Bail

enum Bail
Enumerator
BAIL_NONE 
BAIL_EOF 
BAIL_ERR 

Definition at line 63 of file scanutils.cpp.

63  {
64  BAIL_NONE = 0, // No error condition
65  BAIL_EOF, // Hit EOF
66  BAIL_ERR // Conversion mismatch
67 };

◆ Flags

enum Flags
Enumerator
FL_SPLAT 
FL_INV 
FL_WIDTH 
FL_MINUS 

Definition at line 40 of file scanutils.cpp.

40  {
41  FL_SPLAT = 0x01, // Drop the value, do not assign
42  FL_INV = 0x02, // Character-set with inverse
43  FL_WIDTH = 0x04, // Field width specified
44  FL_MINUS = 0x08, // Negative number
45 };

◆ Ranks

enum Ranks
Enumerator
RANK_CHAR 
RANK_SHORT 
RANK_INT 
RANK_LONG 
RANK_LONGLONG 
RANK_PTR 

Definition at line 47 of file scanutils.cpp.

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

Function Documentation

◆ LongBit()

size_t LongBit ( )
inline

Definition at line 70 of file scanutils.cpp.

70  {
71  return CHAR_BIT * sizeof(long);
72 }
typedef long(ZCALLBACK *tell_file_func) OF((voidpf opaque

◆ streamtofloat()

double streamtofloat ( FILE *  s)

Definition at line 146 of file scanutils.cpp.

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

◆ streamtoumax()

uintmax_t streamtoumax ( FILE *  s,
int  base 
)

Definition at line 105 of file scanutils.cpp.

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

◆ strtofloat()

double strtofloat ( const char *  s)

Definition at line 193 of file scanutils.cpp.

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

◆ tfscanf()

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 228 of file scanutils.cpp.

228  {
229  va_list ap;
230  int rv;
231 
232  va_start(ap, format);
233  rv = tvfscanf(stream, format, ap);
234  va_end(ap);
235 
236  return rv;
237 }
voidpf stream
Definition: ioapi.h:39

Variable Documentation

◆ kIntMaxRank

enum Ranks kIntMaxRank = RANK_LONGLONG

Definition at line 59 of file scanutils.cpp.

◆ kMaxRank

enum Ranks kMaxRank = RANK_LONGLONG

Definition at line 57 of file scanutils.cpp.

◆ kMinRank

enum Ranks kMinRank = RANK_CHAR

Definition at line 56 of file scanutils.cpp.

◆ kPtrDiffRank

enum Ranks kPtrDiffRank = RANK_LONG

Definition at line 61 of file scanutils.cpp.

◆ kSizeTRank

enum Ranks kSizeTRank = RANK_LONG

Definition at line 60 of file scanutils.cpp.