TPCCLIB
Toggle main menu visibility
Loading...
Searching...
No Matches
libtpcextensions
decpoint.c
Go to the documentation of this file.
1
11
/*****************************************************************************/
12
13
/*****************************************************************************/
14
#include "tpcclibConfig.h"
15
/*****************************************************************************/
16
#include <stdio.h>
17
#include <stdlib.h>
18
#include <time.h>
19
#include <string.h>
20
#include <ctype.h>
21
#include <unistd.h>
22
#include <math.h>
23
#include "
tpcextensions.h
"
24
/*****************************************************************************/
25
26
/*****************************************************************************/
33
int
strIsValidNumber
(
36
const
char
*s
37
) {
38
if
(s==NULL ||
strnlen
(s, 256)<1)
return
0;
39
40
char
*p=(
char
*)s;
41
int
i, nn=0;
42
/* It can start with + or minus */
43
if
(*p==
'+'
|| *p==
'-'
) p++;
44
/* Then jump over all digits, counting them */
45
i=strspn(p,
"0123456789"
); nn+=i; p+=i;
46
if
(!*p) {
if
(nn>0)
return
1;
else
return
0;}
// end of string
47
/* Then there can be a decimal point or comma, but only one per string */
48
if
(*p==
'.'
|| *p==
','
) p++;
49
if
(!*p) {
if
(nn>0)
return
1;
else
return
0;}
// end of string
50
/* Again jump over all digits, counting them */
51
i=strspn(p,
"0123456789"
); nn+=i; p+=i;
52
/* At this point we must have got at least one digit */
53
if
(nn<1)
return
0;
54
if
(!*p)
return
1;
// end of string
55
/* String continues, the next character must be E or e */
56
if
(*p==
'E'
|| *p==
'e'
) p++;
else
return
0;
57
/* next can be + or - but not necessarily */
58
if
(*p==
'+'
|| *p==
'-'
) p++;
59
/* then we must have at least one digit */
60
i=strspn(p,
"0123456789"
);
if
(i<1)
return
0;
61
nn+=i; p+=i;
62
/* now we must be at the end of string */
63
if
(!*p)
return
1;
else
return
0;
64
}
65
/*****************************************************************************/
66
67
/*****************************************************************************/
75
double
atofVerified
(
77
const
char
*s
78
) {
79
if
(s==NULL || !
strIsValidNumber
(s))
return
nan(
""
);
80
char
*p; p=strchr(s,
','
);
if
(p==NULL)
return
atof(s);
81
char
*s2=
strdup
(s); p=strchr(s2,
','
); *p=
'.'
;
82
double
t=atof(s2); free(s2);
return
t;
83
}
84
/*****************************************************************************/
85
86
/*****************************************************************************/
94
int
atofCheck
(
96
const
char
*s,
98
double
*v
99
) {
100
if
(v!=NULL) *v=nan(
""
);
101
if
(s==NULL)
return
1;
102
if
(!
strIsValidNumber
(s))
return
1;
103
char
*p; p=strchr(s,
','
);
if
(p==NULL) {*v=atof(s);
return
0;}
104
char
*s2=
strdup
(s); p=strchr(s2,
','
); *p=
'.'
;
105
*v=atof(s2); free(s2);
return
0;
106
}
107
/*****************************************************************************/
108
109
/*****************************************************************************/
115
int
strHaveDecimalComma
(
117
const
char
*s
118
) {
119
if
(s==NULL)
return
(0);
120
if
(strchr(s,
'.'
)!=NULL)
return
(0);
121
if
(strchr(s,
','
)!=NULL)
return
(1);
122
return
(0);
123
}
124
/*****************************************************************************/
125
126
/*****************************************************************************/
132
int
strHaveDecimalSeparator
(
134
const
char
*s
135
) {
136
if
(s==NULL)
return
(0);
137
if
(strchr(s,
'.'
)!=NULL)
return
(1);
138
if
(strchr(s,
','
)!=NULL)
return
(2);
139
return
(0);
140
}
141
/*****************************************************************************/
142
143
/*****************************************************************************/
150
int
atofList
(
152
const
char
*s1,
154
const
char
*s2,
156
double
*x,
159
int
maxn
160
) {
161
if
(x==NULL || maxn<=0)
return
(-1);
162
for
(
int
i=0; i<maxn; i++) x[i]=nan(
""
);
163
if
(s1==NULL || s2==NULL)
return
(0);
164
165
/* Get the nr of tokens */
166
int
i, n;
167
n=
strTokenNr
((
char
*)s1, s2);
if
(n<1)
return
(0);
168
/* Read the values */
169
char
tmp[256];
170
double
v;
171
for
(i=0; i<n && i<maxn; i++) {
172
if
(
strTokenNCpy
(s1, s2, 1+i, tmp, 256)<1)
return
(-2);
173
v=
atofVerified
(tmp);
if
(isnan(v))
return
(-3);
174
x[i]=v;
175
}
176
return
(i);
177
}
178
/*****************************************************************************/
179
180
/*****************************************************************************/
atofVerified
double atofVerified(const char *s)
Definition
decpoint.c:75
strHaveDecimalComma
int strHaveDecimalComma(const char *s)
Definition
decpoint.c:115
atofCheck
int atofCheck(const char *s, double *v)
Definition
decpoint.c:94
atofList
int atofList(const char *s1, const char *s2, double *x, int maxn)
Definition
decpoint.c:150
strHaveDecimalSeparator
int strHaveDecimalSeparator(const char *s)
Definition
decpoint.c:132
strIsValidNumber
int strIsValidNumber(const char *s)
Definition
decpoint.c:33
strTokenNr
int strTokenNr(const char *s1, const char *s2)
Definition
stringext.c:25
strdup
char * strdup(const char *s)
Definition
stringext.c:185
strnlen
size_t strnlen(const char *s, size_t n)
Definition
stringext.c:565
strTokenNCpy
int strTokenNCpy(const char *s1, const char *s2, int i, char *s3, int count)
Definition
stringext.c:53
tpcextensions.h
Header file for library libtpcextensions.
Generated on
for TPCCLIB by
1.17.0