Skip to content

Commit

Permalink
- Read csv-file using C API instead of C++
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@14737 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Jan 10, 2013
1 parent adc44b0 commit df64af6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 38 deletions.
15 changes: 9 additions & 6 deletions Compiler/runtime/SimulationResults.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,16 @@ static void* SimulationResultsImpl__readVars(const char *filename, SimulationRes
case CSV: {
char **variables = read_csv_variables(filename);
char **toFree = variables;
while (*variables)
{
res = mk_cons(mk_scon(*variables),res);
free(*variables);
variables++;
if (variables) {
variables++ /* Skip the first element: It's the malloc buffer. */;
while (*variables) {
res = mk_cons(mk_scon(*variables),res);
variables++;
}
/* All strings are allocated in a single malloc for efficiency */
free(*toFree);
free(toFree);
}
free(toFree);
break;
}
default:
Expand Down
74 changes: 42 additions & 32 deletions SimulationRuntime/c/simulation/results/read_csv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,41 +67,51 @@ int read_csv_dataset_size(const char* filename)

char** read_csv_variables(const char* filename)
{
string header;
string variable;
vector<string> variablesList;
bool startReading = false;
int length;

ifstream stream(filename);

getline(stream,header);
length = strlen(header.c_str());

for(int i = 0 ; i < length ; i++)
{
if(startReading)
variable.append(1,header[i]);

if(header[i] == '"') {
if(startReading) {
if(header[i+1] == ',') {
startReading = false;
variablesList.push_back(variable.erase((strlen(variable.c_str()) - 1), 1));
variable.clear();
}
} else {
startReading = true;
}
}
FILE *fin;
int length = 0, numVar = 0, p;
char *buf,**res,**tmp;

fin = fopen(filename,"r");
if (!fin) {
/* c_add_message(-1, ErrorType_scripting, ErrorLevel_error, gettext("read_csv_variables: Could not open file: %s"), &filename, 1); */
return NULL;
}

char **res = (char**)malloc((1+variablesList.size())*sizeof(char));
for(unsigned int k = 0 ; k < variablesList.size(); k++)
{
res[k] = strdup(variablesList.at(k).c_str());
do {
p = (char)fgetc(fin);
if (p == ',') numVar++;
length++;
} while(p != EOF && p != '\n');
fseek(fin,0,SEEK_SET);
buf = (char*) malloc((length+1)*sizeof(char));
res = (char**)malloc((2+numVar)*sizeof(char*));
if (!fgets(buf,length,fin)) {
free(buf);
free(res);
return NULL;
}
res[variablesList.size()] = NULL;
res[numVar] = 0;
res[numVar+1] = 0;
tmp = res;
tmp[0] = buf;
tmp = tmp+1;
if (*buf == '\"')
buf++;
tmp[0] = buf;
do {
if (*buf == ',') {
*buf = '\0';
tmp++;
if (buf[-1] == '"')
buf[-1] = '\0';
if (buf[1] == '"') buf++;
if (buf[1] != '\0' && buf[1] != '\r')
*tmp = ++buf;
} else {
++buf;
}
} while(*buf != '\0');

return res;
}

Expand Down

0 comments on commit df64af6

Please sign in to comment.