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

Decimal point functions. More...

#include "tpcclibConfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <math.h>
#include "tpcextensions.h"

Go to the source code of this file.

Functions

int strIsValidNumber (const char *s)
 
double atofVerified (const char *s)
 
int atofCheck (const char *s, double *v)
 
int strHaveDecimalComma (const char *s)
 
int strHaveDecimalSeparator (const char *s)
 
int atofList (const char *s1, const char *s2, double *x, int maxn)
 

Detailed Description

Decimal point functions.

Decimal point in data files and user-specified arguments can be either dot (UK/USA) or comma (EU), independent on what is the defined locale on the current platform. Therefore we usually need to try to correctly read decimal numbers whether they are stored in either format, and to convert decimal dots to commas and vice versa. This file contains functions used for that purpose.

Author
Vesa Oikonen

Definition in file decpoint.c.

Function Documentation

◆ atofCheck()

int atofCheck ( const char * s,
double * v )

Verifies that argument string at least seems like a valid number.

Both decimal point and comma are accepted. Optional result double value is set to NaN if string was not valid value.

See also
atofVerified, atoiCheck, doubleGetWithUnit, atofList, strCleanSpaces, strIsValidNumber
Returns
0 if successful, and 1 in case of an error.
Parameters
sString which is converted to a double; string must not contain any space characters.
vPointer to the double float; enter NULL, if not needed.

Definition at line 94 of file decpoint.c.

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}
int strIsValidNumber(const char *s)
Definition decpoint.c:33
char * strdup(const char *s)
Definition stringext.c:185

Referenced by doubleGetWithUnit(), floatGetWithUnit(), iftGetDoubleValue(), tacReadCarimasTxt(), and tacReadInveonCSV().

◆ atofList()

int atofList ( const char * s1,
const char * s2,
double * x,
int maxn )

Read a list of double values from given string with given delimiters.

Returns
The number of double values, or <0 in case of an error.
Author
Vesa Oikonen
See also
strTokenNr, strTokenNCpy, intlistAddFromString, atofCheck, tpcYesNo, doubleCopy, doubleSpanPositives
Parameters
s1Pointer to string from which double values are read.
s2String containing character delimiters
xPointer to a pre-allocated array of doubles.
maxnSize of double list; obligatory. If string contains more values, only maxn values will be read.

Definition at line 150 of file decpoint.c.

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}
double atofVerified(const char *s)
Definition decpoint.c:75
int strTokenNr(const char *s1, const char *s2)
Definition stringext.c:25
int strTokenNCpy(const char *s1, const char *s2, int i, char *s3, int count)
Definition stringext.c:53

◆ atofVerified()

double atofVerified ( const char * s)

Version of atof() which verifies that argument string at least seems like a valid number.

Both decimal point and comma are accepted. Result value is set to NaN if string was not valid value.

See also
atofCheck, doubleGetWithUnit, atoiCheck, strIsValidNumber, tpcYesNo
Returns
converted double value, or NaN in case of an error.
Parameters
sString which is converted to a double; string must not contain any space characters.

Definition at line 75 of file decpoint.c.

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}

Referenced by atofList(), dcmImgIsotope(), iftGetDoubleWithUnit(), parReadCSV(), parReadFIT(), parReadRES(), tacRead4DM(), tacReadCSV(), tacReadDFT(), tacReadHRPLUSHC(), tacReadHRRTHC(), tacReadMat(), tacReadPMOD(), tacReadQView(), tacReadSIF(), and tacReadSimple().

◆ strHaveDecimalComma()

int strHaveDecimalComma ( const char * s)

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

Author
Vesa Oikonen
Returns
1 if decimal comma is found and 0 if not.
See also
strHaveDecimalSeparator, atofVerified, strIsValidNumber
Parameters
sPointer to string.

Definition at line 115 of file decpoint.c.

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}

Referenced by tacReadCSV().

◆ strHaveDecimalSeparator()

int strHaveDecimalSeparator ( const char * s)

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

Author
Vesa Oikonen
Returns
0, if neither is found, 1 if dot, and 2 if comma is found.
See also
strHaveDecimalComma, atofVerified, strIsValidNumber
Parameters
sPointer to string.

Definition at line 132 of file decpoint.c.

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}

◆ strIsValidNumber()

int strIsValidNumber ( const char * s)

Verifies that given string seems like a valid representation of integer or floating point number in decimal or exponential format.

Returns
1 if string is valid number, and 0 if not.
Author
Vesa Oikonen
See also
atofVerified, atoiCheck, tpcYesNo
Parameters
sPointer to the string to be verified; it must not contain any leading or trailing space characters etc, or it will never verify.

Definition at line 33 of file decpoint.c.

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}
size_t strnlen(const char *s, size_t n)
Definition stringext.c:566

Referenced by atofCheck(), and atofVerified().