mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
csv format: determine type of value (double or int) in received sample by checking for a dot in the value string
This commit is contained in:
parent
89f4e9b768
commit
f7a61e3f97
1 changed files with 23 additions and 8 deletions
|
@ -106,16 +106,31 @@ static size_t csv_sscan_single(struct io *io, const char *buf, size_t len, struc
|
|||
if (*end == io->delimiter)
|
||||
goto out;
|
||||
|
||||
switch (s->format & (1 << s->length)) {
|
||||
case SAMPLE_DATA_FORMAT_FLOAT:
|
||||
s->data[s->length].f = strtod(ptr, &end);
|
||||
break;
|
||||
case SAMPLE_DATA_FORMAT_INT:
|
||||
s->data[s->length].i = strtol(ptr, &end, 10);
|
||||
break;
|
||||
//determine format (int or double) of current number starting at ptr
|
||||
char * next_seperator = strchr(ptr, io->separator);
|
||||
if(next_seperator == NULL){
|
||||
//the last element of a row
|
||||
next_seperator = strchr(ptr, io->delimiter);
|
||||
}
|
||||
|
||||
/* There are no valid FP values anymore. */
|
||||
char *number = malloc(next_seperator - ptr);
|
||||
strncpy(number, ptr, next_seperator-ptr);
|
||||
char * contains_dot = strstr(number, ".");
|
||||
if(contains_dot == NULL){
|
||||
//no dot in string number --> number is an integer
|
||||
s->data[s->length].i = strtol(ptr, &end, 10);
|
||||
sample_set_data_format(s, s->length, SAMPLE_DATA_FORMAT_INT);
|
||||
}
|
||||
|
||||
else{
|
||||
//dot in string number --> number is a floating point value
|
||||
s->data[s->length].f = strtod(ptr, &end);
|
||||
sample_set_data_format(s, s->length, SAMPLE_DATA_FORMAT_FLOAT);
|
||||
}
|
||||
free(number);
|
||||
|
||||
|
||||
/* There are no valid values anymore. */
|
||||
if (end == ptr)
|
||||
goto out;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue