TPCCLIB
Toggle main menu visibility
Loading...
Searching...
No Matches
libtpcmisc
decpoint.c
Go to the documentation of this file.
1
6
/*****************************************************************************/
7
#include "
libtpcmisc.h
"
8
/*****************************************************************************/
9
10
/*****************************************************************************/
14
int
dec_comma_is
(
16
char
*str
17
) {
18
if
(strchr(str,
'.'
)!=NULL)
return
(0);
19
if
(strchr(str,
','
)!=NULL)
return
(1);
20
return
(0);
21
}
22
/*****************************************************************************/
23
24
/*****************************************************************************/
28
int
dec_separator
(
30
char
*str
31
) {
32
if
(strchr(str,
'.'
)!=NULL)
return
(1);
33
if
(strchr(str,
','
)!=NULL)
return
(2);
34
return
(0);
35
}
36
/*****************************************************************************/
37
38
/*****************************************************************************/
40
void
dec_separator_change
(
42
char
*str,
44
int
decsep
45
) {
46
char
*cptr;
47
cptr=strchr(str,
'.'
);
if
(cptr!=NULL && decsep==1) {*cptr=
','
;
return
;}
48
cptr=strchr(str,
','
);
if
(cptr!=NULL && decsep==0) {*cptr=
'.'
;
return
;}
49
return
;
50
}
51
/*****************************************************************************/
52
53
/*****************************************************************************/
59
double
atof_dpi
(
61
char
*str
62
) {
63
char
*cptr;
64
double
f;
65
66
if
(str==NULL)
return
(nan(
""
));
67
/* If string contains a dot, then use atof directly */
68
if
(strchr(str,
'.'
)!=NULL)
return
(atof(str));
69
/* Otherwise, convert all commas to dots */
70
//while((cptr=strchr(str, ','))!=NULL) *cptr='.';
71
cptr=strchr(str,
','
);
if
(cptr!=NULL) *cptr=
'.'
;
72
f=atof(str);
if
(cptr!=NULL) *cptr=
','
;
73
return
(f);
74
}
75
/*****************************************************************************/
76
77
/*****************************************************************************/
81
int
dec_nr
(
char
*str)
82
{
83
char
*cptr;
84
int
n;
85
86
if
(str==NULL)
return
0;
87
/* Find the first dot or comma, but stop with E */
88
cptr=str;
89
while
(*cptr!=
'.'
&& *cptr!=
','
) {
90
if
(*cptr==(
char
)0 || *cptr==
'E'
|| *cptr==
'e'
)
return
0;
91
cptr++;
92
}
93
if
(*cptr==(
char
)0 || (*cptr!=
'.'
&& *cptr!=
','
))
return
0;
94
/* Calculate the number of digits that follows */
95
cptr++; n=0;
while
(cptr!=NULL && isdigit(*cptr)) {n++; cptr++;}
96
return
n;
97
}
98
/*****************************************************************************/
99
100
/*****************************************************************************/
107
int
atof_with_check
(
109
char
*double_as_string,
111
double
*result_value
112
) {
113
char
* cptr;
114
double
f;
115
116
if
(result_value!=NULL) *result_value=nan(
""
);
117
if
(double_as_string==NULL)
return
(1);
118
f=
atof_dpi
(double_as_string);
119
if
(f!=0.0) {
if
(result_value!=NULL) *result_value=f;
return
(0);}
120
cptr=double_as_string;
121
while
(*cptr!=0 && (*cptr==
'+'
|| *cptr==
'-'
|| *cptr==
' '
)) cptr++;
122
if
(*cptr==
'0'
) {
if
(result_value!=NULL) *result_value=f;
return
(0);}
123
return
(1);
124
}
125
/*****************************************************************************/
126
127
/*****************************************************************************/
134
char
*
strPtrToNextValue
(
136
char
*str,
139
char
**nxtp
140
) {
141
char
*cptr, *strt;
142
unsigned
int
i, j, n=0;
143
144
// search the start
145
if
(str==NULL || strlen(str)<1) {*nxtp=NULL;
return
NULL;}
146
// find the index where number might start
147
cptr=str;
148
do
{
149
i=strcspn(cptr,
"+-0123456789"
);
if
(i==strlen(cptr)) {*nxtp=NULL;
return
NULL;}
150
strt=cptr+i;
151
// any previous character must be a separator
152
if
(i>0) {cptr=strt-1; i=strcspn(cptr,
" \t,;"
);
if
(i>0) cptr+=i;}
153
}
while
(i>0);
154
// get past + and - ; if more than one, that is an error
155
cptr=strt;
if
(*cptr==
'-'
|| *cptr==
'+'
) {n++; cptr++;}
156
// get past first set of numbers
157
i=strspn(cptr,
"0123456789"
); n+=i;
158
if
(i<1) {*nxtp=cptr;
return
NULL;}
159
cptr+=i; n+=i;
160
// get past decimal separator
161
i=strspn(cptr,
",."
);
162
if
(i==0) {
// there was none, thus we're done
163
*nxtp=cptr;
return
strt;
164
}
else
if
(i>1) {
// can't be more than one decimal separator
165
*nxtp=cptr;
return
strt;
166
}
167
cptr+=1; n+=1;
168
// after decimal separator we must have numbers again
169
i=strspn(cptr,
"0123456789"
);
170
if
(i<1) {
// what was thought to be decimal separator was not that
171
*nxtp=cptr-1;
return
strt;
172
}
173
cptr+=i; n+=i;
174
// check if we have exponent part
175
if
(*cptr==
'E'
|| *cptr==
'e'
) i=1;
else
i=0;
176
// if not, then we're done
177
if
(i==0) {*nxtp=cptr;
return
strt;}
178
cptr+=1;
179
// we may have signed exponent
180
if
(*cptr==
'-'
|| *cptr==
'+'
) {cptr+=1; i++;}
181
// after exponent we must have numbers again
182
j=strspn(cptr,
"0123456789"
);
183
if
(j<1) {
// what was thought to be exponent was not that
184
*nxtp=cptr-i;
return
strt;
185
}
186
cptr+=j; n+=j;
187
// now this must be the end
188
*nxtp=cptr;
189
return
strt;
190
}
191
#if(0)
// Tests
192
int
test_strPtrToNextValue()
193
{
194
char
*cptr, *cptr2=NULL, tmp[256];
195
int
i=0, j=0, n;
196
197
static
char
*tests[] = {
198
""
,
199
" "
,
200
" abc d ef"
,
201
" 1 "
,
202
"M2 s123456 7s"
,
203
"65s, 44.3min m44m b7,7 c1,2,3 43.2"
,
204
"6 s"
,
205
"1 23 4,5 6, 7"
,
206
"-1.2345E-009 0.23 434.1 +65 +2.3E2"
,
207
"-1.2345E-009,0.23,434.1,+65,+2.3E2"
,
208
"-1.2345E-009, 0.23, 434.1, +65, +2.3E2"
,
209
"-1,2345E-009;0,23;434,1;+65;+2,3E2"
,
210
"-1.2345E-009 0.23 434.1 +65 +2.3E2"
,
// tabs
211
0};
212
213
while
(tests[i]!=0) {
214
j=0; printf(
"Test %d:\n"
, 1+i); printf(
" test_string := '%s'\n"
, tests[i]);
215
cptr=
strPtrToNextValue
(tests[i], &cptr2);
216
while
(cptr!=NULL) {
217
n=strlen(cptr);
if
(cptr2!=NULL) n-=strlen(cptr2);
218
strncpy(tmp, cptr, n); tmp[n]=(char)0;
219
printf(
" valstring[%d] := '%s'\n"
, 1+j, tmp);
220
if
(cptr2==NULL)
break
;
221
cptr=cptr2; cptr=
strPtrToNextValue
(cptr, &cptr2);
222
j++;
223
}
224
i++;
225
}
226
return
0;
227
}
228
#endif
229
/*****************************************************************************/
230
231
/*****************************************************************************/
238
int
atoi_with_check
(
240
const
char
*int_as_string,
242
int
*result_value
243
) {
244
int
i, len;
245
246
if
(result_value!=NULL) *result_value=0;
247
if
(int_as_string==NULL)
return
(1);
248
len=strlen(int_as_string);
if
(len<1)
return
(1);
249
/* First character can be + or - */
250
i=strspn(int_as_string,
"+-"
);
251
if
(i>1)
return
(1);
252
if
(i==1 && len==1)
return
(1);
253
/* Check that (rest of) characters are digits */
254
for
( ; i<len; i++)
if
(!isdigit(int_as_string[i]))
return
(1);
255
/* Convert to int */
256
if
(result_value!=NULL) *result_value=atoi(int_as_string);
257
return
(0);
258
}
259
#if(0)
// Tests
260
int
test_atoi_with_check()
261
{
262
char
temp[128];
263
int
v, ret;
264
265
strcpy(temp,
""
);
266
ret=
atoi_with_check
(temp, &v);
267
printf(
"atoi_with_check('%s', %d) := %d\n\n"
, temp, v, ret);
268
if
(ret==0) {printf(
"Wrong result.\n"
);
return
(1);}
269
270
strcpy(temp,
"a"
);
271
ret=
atoi_with_check
(temp, &v);
272
printf(
"atoi_with_check('%s', %d) := %d\n\n"
, temp, v, ret);
273
if
(ret==0) {printf(
"Wrong result.\n"
);
return
(1);}
274
275
strcpy(temp,
"a123"
);
276
ret=
atoi_with_check
(temp, &v);
277
printf(
"atoi_with_check('%s', %d) := %d\n\n"
, temp, v, ret);
278
if
(ret==0) {printf(
"Wrong result.\n"
);
return
(1);}
279
280
strcpy(temp,
"1"
);
281
ret=
atoi_with_check
(temp, &v);
282
printf(
"atoi_with_check('%s', %d) := %d\n\n"
, temp, v, ret);
283
if
(ret!=0 || v!=1) {printf(
"Wrong result.\n"
);
return
(1);}
284
285
strcpy(temp,
"0"
);
286
ret=
atoi_with_check
(temp, &v);
287
printf(
"atoi_with_check('%s', %d) := %d\n\n"
, temp, v, ret);
288
if
(ret!=0 || v!=0) {printf(
"Wrong result.\n"
);
return
(1);}
289
290
strcpy(temp,
"123a"
);
291
ret=
atoi_with_check
(temp, &v);
292
printf(
"atoi_with_check('%s', %d) := %d\n\n"
, temp, v, ret);
293
if
(ret==0) {printf(
"Wrong result.\n"
);
return
(1);}
294
295
strcpy(temp,
"+123"
);
296
ret=
atoi_with_check
(temp, &v);
297
printf(
"atoi_with_check('%s', %d) := %d\n\n"
, temp, v, ret);
298
if
(ret!=0 || v!=123) {printf(
"Wrong result.\n"
);
return
(1);}
299
300
strcpy(temp,
"-2"
);
301
ret=
atoi_with_check
(temp, &v);
302
printf(
"atoi_with_check('%s', %d) := %d\n\n"
, temp, v, ret);
303
if
(ret!=0 || v!=-2) {printf(
"Wrong result.\n"
);
return
(1);}
304
305
strcpy(temp,
"+-2"
);
306
ret=
atoi_with_check
(temp, &v);
307
printf(
"atoi_with_check('%s', %d) := %d\n\n"
, temp, v, ret);
308
if
(ret==0) {printf(
"Wrong result.\n"
);
return
(1);}
309
310
strcpy(temp,
"-2.3"
);
311
ret=
atoi_with_check
(temp, &v);
312
printf(
"atoi_with_check('%s', %d) := %d\n\n"
, temp, v, ret);
313
if
(ret==0) {printf(
"Wrong result.\n"
);
return
(1);}
314
315
strcpy(temp,
"3,14"
);
316
ret=
atoi_with_check
(temp, &v);
317
printf(
"atoi_with_check('%s', %d) := %d\n\n"
, temp, v, ret);
318
if
(ret==0) {printf(
"Wrong result.\n"
);
return
(1);}
319
320
strcpy(temp,
"-2E2"
);
321
ret=
atoi_with_check
(temp, &v);
322
printf(
"atoi_with_check('%s', %d) := %d\n\n"
, temp, v, ret);
323
if
(ret==0) {printf(
"Wrong result.\n"
);
return
(1);}
324
325
return
(0);
326
}
327
#endif
328
/*****************************************************************************/
329
330
/*****************************************************************************/
atof_with_check
int atof_with_check(char *double_as_string, double *result_value)
Definition
decpoint.c:107
dec_separator_change
void dec_separator_change(char *str, int decsep)
Definition
decpoint.c:40
atof_dpi
double atof_dpi(char *str)
Definition
decpoint.c:59
atoi_with_check
int atoi_with_check(const char *int_as_string, int *result_value)
Definition
decpoint.c:238
dec_nr
int dec_nr(char *str)
Definition
decpoint.c:81
strPtrToNextValue
char * strPtrToNextValue(char *str, char **nxtp)
Definition
decpoint.c:134
dec_comma_is
int dec_comma_is(char *str)
Definition
decpoint.c:14
dec_separator
int dec_separator(char *str)
Definition
decpoint.c:28
libtpcmisc.h
Header file for libtpcmisc.
Generated on
for TPCCLIB by
1.17.0