-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcsv.c
154 lines (148 loc) · 3.34 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "fundamental_config.h"
#include "fundamental.h"
#include "csv.h"
#include "utils.h"
#ifdef REAL_HUNTER
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
off_t getfilelen(int fd)
{
struct stat buf;
if(fstat(fd,&buf))
exit(errno);
return buf.st_size;
}
void parselines(char *buff,off_t len,char ***lines,int *num_csv_lines)
{
off_t curr,lastline;
int currline;
buff[len]=0;
*num_csv_lines=0;
for(curr=0;curr<len;curr++)
{
if(buff[curr]==0xa)
(*num_csv_lines)++;
}
if(buff[len-1]!=0xa)
(*num_csv_lines)++;
if((*lines=(char **)malloc(((*num_csv_lines)+1)*sizeof(char*)))==NULL)
{
fprintf(stderr,"parselines: failed to allocate memory\n");
exit(errno);
}
lastline=0;
currline=0;
for(curr=0;curr<len;curr++)
{
if(buff[curr]==0xa)
{
buff[curr]=0;
(*lines)[currline++]=&buff[lastline];
lastline=curr+1;
}
}
(*lines)[(*num_csv_lines)-1]=&buff[lastline];
buff[len]=0;
(*lines)[*num_csv_lines]=&buff[len];
}
void readreals(char *filename)
{
int num_csv_lines=0;
int i,j,k,num_commas0,num_commas,readlen,currlen;
char **lines,*line,*endptr;
int *strlens;
off_t filelen;
int fd;
char *buff;
if((fd=open(filename,O_RDONLY))<0)
{
fprintf(stderr,"Couldn't open %s exiting\n",filename);
exit(errno);
}
filelen=getfilelen(fd);
if((buff=malloc(filelen+1))==NULL)
exit(errno);
/* if read doesn't read the whole file in one go this will break */
currlen=0;
while(readlen=read(fd,buff,filelen)!=0)
{
if(readlen==0)
exit_error("Bad file read\n");
currlen+=readlen;
if(currlen>=filelen)
break;
}
close(fd);
parselines(buff,filelen,&lines,&num_csv_lines);
num_commas0=0;
for(i=0;i<num_csv_lines;i++)
{
line=lines[i];
num_commas=0;
while(*line!=0)
{
if(*line==',')
{
if(i==0)
num_commas0++;
else
num_commas++;
if(*(line+1)==',')
exit_error("Too many commas in csv");
}
line++;
}
if(i==0)
{
if(num_commas0<1)
exit_error("bad num_commas0 in cvs file\n");
}
else
if(num_commas0!=num_commas)
exit_error("bad num_commas in line %d\n",i);
}
if(num_commas!=NUM_HUNTER_DIMENSIONS)
exit_error("num_commas!=NUM_HUNTER_DIMENSIONS");
strlens=(int *)calloc(num_csv_lines,sizeof(int));
if(strlens==NULL)
exit_error("calloc strlens failed");
for(i=0;i<num_csv_lines;i++)
strlens[i]=strlen(lines[i]);
for(i=0;i<num_csv_lines;i++)
{
line=lines[i];
k=strlens[i];
for(j=0;j<k;j++)
{
if(line[j]==',')
line[j]=0;
}
}
coord_and_result_array=calloc((NUM_HUNTER_DIMENSIONS+1)*num_csv_lines,sizeof(number_t));
if(coord_and_result_array==NULL)
exit_error("failed to allocate coord_and_result_array");
for(i=0;i<num_csv_lines;i++)
{
line=lines[i];
for(j=0;j<=NUM_HUNTER_DIMENSIONS;j++)
{
while(isspace(*line))
line++;
coord_and_result_array[((NUM_HUNTER_DIMENSIONS+1)*i)+j]=strtod(line, &endptr);
if(endptr==NULL)
exit_error("bad_number");
while(isspace(*endptr))
endptr++;
line=endptr+1;
}
}
num_real_coord_idx=num_csv_lines-1;
free(buff);
free(lines);
}
#endif