Skip to content

Commit

Permalink
Read csv file delim from header
Browse files Browse the repository at this point in the history
  • Loading branch information
lochel authored and OpenModelica-Hudson committed May 15, 2017
1 parent ef8d929 commit a7b3828
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 12 deletions.
4 changes: 2 additions & 2 deletions SimulationRuntime/c/util/libcsv.c
Expand Up @@ -113,7 +113,7 @@ csv_set_opts(struct csv_parser *p, unsigned char options)
}

int
csv_init(struct csv_parser *p, unsigned char options)
csv_init(struct csv_parser *p, unsigned char options, unsigned char delim)
{
/* Initialize a csv_parser object returns 0 on success, -1 on error */
if (p == NULL)
Expand All @@ -128,7 +128,7 @@ csv_init(struct csv_parser *p, unsigned char options)
p->status = 0;
p->options = options;
p->quote_char = CSV_QUOTE;
p->delim_char = CSV_COMMA;
p->delim_char = delim;
p->is_space = NULL;
p->is_term = NULL;
p->blk_size = MEM_BLK_SIZE;
Expand Down
2 changes: 1 addition & 1 deletion SimulationRuntime/c/util/libcsv.h
Expand Up @@ -58,7 +58,7 @@ struct csv_parser {
};

/* Function Prototypes */
int csv_init(struct csv_parser *p, unsigned char options);
int csv_init(struct csv_parser *p, unsigned char options, unsigned char delim);
int csv_fini(struct csv_parser *p, void (*cb1)(void *, size_t, void *), void (*cb2)(int, void *), void *data);
void csv_free(struct csv_parser *p);
int csv_error(struct csv_parser *p);
Expand Down
51 changes: 43 additions & 8 deletions SimulationRuntime/c/util/read_csv.c
Expand Up @@ -100,11 +100,23 @@ int read_csv_dataset_size(const char* filename)
FILE *f;
struct csv_parser p;
struct cell_row_count count = {0};
size_t offset=0;
unsigned char delim = CSV_COMMA;
f = fopen(filename,"r");
if (f == NULL) {
return -1;
}
csv_init(&p, CSV_STRICT | CSV_REPALL_NL | CSV_STRICT_FINI | CSV_APPEND_NULL | CSV_EMPTY_IS_NULL);

/* determine delim */
fread(buf, 1, 5, f);
if (0 == strcmp(buf, "\"sep="))
{
fread(&delim, 1, 1, f);
offset = 8;
}
fseek(f, offset, SEEK_SET);

csv_init(&p, CSV_STRICT | CSV_REPALL_NL | CSV_STRICT_FINI | CSV_APPEND_NULL | CSV_EMPTY_IS_NULL, delim);
csv_set_realloc_func(&p, realloc);
csv_set_free_func(&p, free);
do {
Expand All @@ -122,15 +134,14 @@ int read_csv_dataset_size(const char* filename)
return count.row_count - 1; /* The header is excluded */
}

char** read_csv_variables(FILE *fin, int *length)
char** read_csv_variables(FILE *fin, int *length, unsigned char delim)
{
const int buf_size = 4096;
char buf[4096];
char **res;
struct csv_parser p;
struct csv_head head = {0};
fseek(fin,0,SEEK_SET);
csv_init(&p, CSV_STRICT | CSV_REPALL_NL | CSV_STRICT_FINI | CSV_APPEND_NULL | CSV_EMPTY_IS_NULL);
csv_init(&p, CSV_STRICT | CSV_REPALL_NL | CSV_STRICT_FINI | CSV_APPEND_NULL | CSV_EMPTY_IS_NULL, delim);
csv_set_realloc_func(&p, realloc);
csv_set_free_func(&p, free);
do {
Expand Down Expand Up @@ -205,10 +216,22 @@ double* read_csv_dataset_var(const char *filename, const char *var, int dimsize)
struct csv_parser p;
struct csv_body body = {0};
FILE *fin = fopen(filename, "r");
size_t offset = 0;
unsigned char delim = CSV_COMMA;
if (!fin) {
return NULL;
}
csv_init(&p, CSV_STRICT | CSV_REPALL_NL | CSV_STRICT_FINI | CSV_APPEND_NULL | CSV_EMPTY_IS_NULL);

/* determine delim */
fread(buf, 1, 5, fin);
if (0 == strcmp(buf, "\"sep="))
{
fread(&delim, 1, 1, fin);
offset = 8;
}
fseek(fin, offset, SEEK_SET);

csv_init(&p, CSV_STRICT | CSV_REPALL_NL | CSV_STRICT_FINI | CSV_APPEND_NULL | CSV_EMPTY_IS_NULL, delim);
csv_set_realloc_func(&p, realloc);
csv_set_free_func(&p, free);
do {
Expand Down Expand Up @@ -238,18 +261,30 @@ struct csv_data* read_csv(const char *filename)
struct csv_parser p;
struct csv_body body = {0};
struct csv_data *res;
size_t offset = 0;
unsigned char delim = CSV_COMMA;
FILE *fin = fopen(filename, "r");
if (!fin) {
return NULL;
}
variables = read_csv_variables(fin,&dummy);

/* determine delim */
fread(buf, 1, 5, fin);
if (0 == strcmp(buf, "\"sep="))
{
fread(&delim, 1, 1, fin);
offset = 8;
}
fseek(fin, offset, SEEK_SET);

variables = read_csv_variables(fin, &dummy, delim);
if (!variables) {
fclose(fin);
return NULL;
}
fseek(fin,0,SEEK_SET);
fseek(fin,offset,SEEK_SET);

csv_init(&p, CSV_STRICT | CSV_REPALL_NL | CSV_STRICT_FINI | CSV_APPEND_NULL | CSV_EMPTY_IS_NULL);
csv_init(&p, CSV_STRICT | CSV_REPALL_NL | CSV_STRICT_FINI | CSV_APPEND_NULL | CSV_EMPTY_IS_NULL, delim);
csv_set_realloc_func(&p, realloc);
csv_set_free_func(&p, free);
do {
Expand Down
2 changes: 1 addition & 1 deletion SimulationRuntime/c/util/read_csv.h
Expand Up @@ -44,7 +44,7 @@ extern "C" {

int read_csv_dataset_size(const char* filename);

char** read_csv_variables(FILE *fin, int *length);
char** read_csv_variables(FILE *fin, int *length, unsigned char delim);

struct csv_data* read_csv(const char *filename);
double* read_csv_dataset(struct csv_data *data, const char *var);
Expand Down

0 comments on commit a7b3828

Please sign in to comment.