Skip to content

Commit

Permalink
- Added fix for ptolemyio.cpp using atof (which has no exception hand…
Browse files Browse the repository at this point in the history
…ling for reading things that are not numbers)

  - We now use strtod instead, and return NaN for invalid strings (we also handle Inf for the Windows platform).


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@5465 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed May 6, 2010
1 parent 2cfc58b commit ca6f3d9
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion Compiler/runtime/ptolemyio.cpp
Expand Up @@ -49,6 +49,7 @@ extern "C"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
void print_error_buf_impl(const char* str);


Expand Down Expand Up @@ -109,6 +110,9 @@ void * read_ptolemy_dataset(char*filename, int size,char**vars,int datasize)
lst = mk_nil();
int j=0;
while(j<datasize) {
const char* buf1;
char* buf2;

stream.getline(buf,255);

if (string(buf).find("DataSet:") == 1) {
Expand All @@ -117,7 +121,19 @@ void * read_ptolemy_dataset(char*filename, int size,char**vars,int datasize)
}
string values(buf);
int commapos=values.find(",");
val = atof(values.substr(commapos+1).c_str()); // Second value after comma

buf1 = values.substr(commapos+1).c_str();
val = strtod(buf1,&buf2); // Second value after comma
if (buf1 == buf2) {
// We may be trying to parse Infinity on a Windows platform.
// Don't we feel stupid expecting this to work?
// Let's do these extra tests on Linux as well to check for NaN
if (0 == strncmp(buf1,"Inf",3)) val = INFINITY;
else if (0 == strncmp(buf1,"-Inf",4)) val = -INFINITY;
else if (0 == strncmp(buf1,"inf",3)) val = INFINITY;
else if (0 == strncmp(buf1,"-inf",4)) val = -INFINITY;
else val = NAN;
}

lst = (void*)mk_cons(Values__REAL(mk_rcon(val)),lst);
j++;
Expand Down

0 comments on commit ca6f3d9

Please sign in to comment.