TPCCLIB
Loading...
Searching...
No Matches
decpoint.c File Reference

Functions for reading real numbers from strings which may contain either decimal dots or commas. More...

#include "libtpcmisc.h"

Go to the source code of this file.

Functions

int dec_comma_is (char *str)
 
int dec_separator (char *str)
 
void dec_separator_change (char *str, int decsep)
 
double atof_dpi (char *str)
 
int dec_nr (char *str)
 
int atof_with_check (char *double_as_string, double *result_value)
 
char * strPtrToNextValue (char *str, char **nxtp)
 
int atoi_with_check (const char *int_as_string, int *result_value)
 

Detailed Description

Functions for reading real numbers from strings which may contain either decimal dots or commas.

Author
Vesa Oikonen

Definition in file decpoint.c.

Function Documentation

◆ atof_dpi()

double atof_dpi ( char * str)

Replacement of atof(), which works whether string contains decimal dots or decimal commas. Possible commas are replaced by dots in the argument string.

Returns
Returns the double float.
Parameters
strPointer to string (not modified).

Definition at line 59 of file decpoint.c.

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}

Referenced by atof_with_check(), csv2dft_a(), csv2dft_b(), dft_fill_hdr_from_IFT(), dftRead(), fitRead(), and resRead().

◆ atof_with_check()

int atof_with_check ( char * double_as_string,
double * result_value )

Converts a string to float using atof(), but if its return value is zero this function checks that argument string actually contains a number. Result value is set to NaN if string was not valid value. Both decimal point and comma are accepted.

Returns
Returns 0 if successful, and 1 in case of an error.
Parameters
double_as_stringString which is converted to a double; not modified
result_valuePointer to the double float; enter NULL, if not needed

Definition at line 107 of file decpoint.c.

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}
double atof_dpi(char *str)
Definition decpoint.c:59

Referenced by csv2dft_linkset(), csv2dft_mat(), dftRead(), and readEcat931Calibrationfile().

◆ atoi_with_check()

int atoi_with_check ( const char * int_as_string,
int * result_value )

Converts a string to integer (int) using atoi, but this function verifies that argument string actually contains an integer number. String must end in NULL character. Exponentials are not accepted. Result value is set to 0 if string was not valid value.

Returns
Returns 0 if successful, and 1 in case of an error.
Parameters
int_as_stringString which is converted to a int; not modified
result_valuePointer to the int; enter NULL, if not needed

Definition at line 238 of file decpoint.c.

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}

Referenced by integerListAddFromString(), and pxlRead().

◆ dec_comma_is()

int dec_comma_is ( char * str)

Checks whether argument string contains a decimal comma instead of dot.

Returns
Returns 1 if decimal comma is found and 0 if not found.
Parameters
strPointer to string (not modified).

Definition at line 14 of file decpoint.c.

17 {
18 if(strchr(str, '.')!=NULL) return(0);
19 if(strchr(str, ',')!=NULL) return(1);
20 return(0);
21}

◆ dec_nr()

int dec_nr ( char * str)

Returns the number of decimal places in the argument string, representing a floating point value. String can contain either decimal dots or commas.

Definition at line 81 of file decpoint.c.

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}

Referenced by dftRead().

◆ dec_separator()

int dec_separator ( char * str)

Checks whether argument string contains a decimal comma or dot, or neither.

Returns
Returns 0, if neither is found, 1 if dot, and 2 if comma is found.
Parameters
strPointer to string (not modified).

Definition at line 28 of file decpoint.c.

31 {
32 if(strchr(str, '.')!=NULL) return(1);
33 if(strchr(str, ',')!=NULL) return(2);
34 return(0);
35}

◆ dec_separator_change()

void dec_separator_change ( char * str,
int decsep )

Convert the first decimal separator to comma or dot, as required.

Parameters
strPointer to string (modified when necessary).
decsepRequested decimal separator: 0=dot, 1=comma.

Definition at line 40 of file decpoint.c.

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}

◆ strPtrToNextValue()

char * strPtrToNextValue ( char * str,
char ** nxtp )

This function searches the given string for a string representation of numerical value, possibly with decimal and exponent part. Returns also pointer to the string right after where the numerical value ended, and from where the next number can be searched. Return Returns pointer to start of the next string, or NULL if not found.

Parameters
strPointer to string where numerical values are searched; not changed.
nxtpObligatory pointer to string pointer, which will be set to point to the first character after value string, or to NULL if string ends.

Definition at line 134 of file decpoint.c.

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}