-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
56 lines (42 loc) · 1.13 KB
/
parser.c
File metadata and controls
56 lines (42 loc) · 1.13 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parser.h"
int parseJaggedFrequenciesFromFile(const char* filename, DoubleMatrix* mat, bool isVerbose){
FILE* file = fopen(filename, "r");
if(!file){
fprintf(stderr, "Error: impossible to open file %s\n", filename);
return 0;
}
char buffer[1024];
size_t row_idx = 0;
if(isVerbose) printf("--- Parsing Frequencies ---\n");
while(fgets(buffer, sizeof(buffer), file)){
buffer[strcspn(buffer, "\r\n")] = 0;
if(strlen(buffer) == 0) continue;
char* ptr = buffer;
char* endptr;
int foundInRow = 0;
while(1){
double freq = strtod(ptr, &endptr);
if(ptr == endptr) break;
if(freq >= 0.0){
DoubleMatrix_push_at(mat, row_idx, freq);
foundInRow = 1;
}
ptr = endptr;
}
if(foundInRow){
if(isVerbose){
printf("Row %zu (%zu items): ", row_idx, mat->data[row_idx].len);
for(size_t i = 0; i < mat->data[row_idx].len; i++){
printf("%f ", mat->data[row_idx].data[i]);
}
printf("\n");
}
row_idx++;
}
}
fclose(file);
return 1;
}