-
Notifications
You must be signed in to change notification settings - Fork 1
/
csv.c
88 lines (70 loc) · 1.59 KB
/
csv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "csv.h"
#define MAX_LINE_LEN ((2<<24)-1)
/*
Load a CSV file to an array of floats
*/
DENSEFILE* load_csv( const char *filename ) {
char *line = NULL;
char item[512];
int num_lines = 0;
int num_commas = 0;
line = (char*) malloc( MAX_LINE_LEN * sizeof(char) );
DENSEFILE* dfile = (DENSEFILE*) malloc( sizeof(DENSEFILE) );
dfile->M=0;
// open the file
FILE *fp = fopen( filename, "r" );
if( fp == NULL ) {
printf( "ERROR cannot open %s\n", filename );
exit( 0 );
}
// get dataset statistics
while( fgets( line, MAX_LINE_LEN, fp ) != NULL ) {
// count the number of points (for every line)
num_lines++;
// count the number of dimensions (once)
if( dfile->M == 0 ) {
int i = 0;
while( line[i] != '\0' ) {
if( line[i] == ',' ) {
num_commas++;
}
i++;
}
dfile->M = num_commas+1;
}
}
fclose( fp );
// allocate our data buffer
dfile->N = num_lines;
dfile->data = (float*) malloc( sizeof(float) * (dfile->N) * (dfile->M) );
// read the data into the buffer
fp = fopen(filename, "r");
int k = 0;
while( fgets( line, MAX_LINE_LEN, fp ) != NULL ) {
int done = 0;
int i = 0;
int j = 0;
while( !done ) {
// parse character data
if( line[i] == ',' ) {
item[j] = '\0';
dfile->data[k++] = (float) atof( item );
j = 0;
}
else if( line[i] == '\n' || line[i] == '\0' ) {
item[j] = '\0';
dfile->data[k++] = (float) atof( item );
done++;
}
else if( line[i] != ' ' ) {
item[j++] = line[i];
}
i++;
}
}
free(line);
return dfile;
}