forked from namelessjon/tip_trace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_file.c
182 lines (146 loc) · 4.38 KB
/
read_file.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* read_file.c
* Jonathan D. Stott <[email protected]>
*
*/
#include <zlib.h>
#include "helper.h"
#include "tip_trace_binary.h"
#define BUFSIZE 1048576
static int read_binary_float_sheet(int x, int y, float ** E, const char *filename);
static int read_binary_double_sheet(int x, int y, float ** E, const char *filename);
static int read_text_sheet(int x, int y, float ** E, const char *filename);
int read_file(file_type_t file_type, int x, int y, float **sheet, const char *filename) {
// reads in the given file, assigning the values to sheet.
//
// file_type: Type of files in the list
// x: x dimension of the sheet
// y: y dimension of the sheet
// sheet[y][x]: 2D sheet of floats
// filename: filename to open
//
// returns:
// 0: success
// <0: error
switch(file_type) {
case BINARY_FLOAT:
return read_binary_float_sheet(x, y, sheet, filename);
case BINARY_DOUBLE:
return read_binary_double_sheet(x, y, sheet, filename);
case TEXT:
return read_text_sheet(x, y, sheet, filename);
default:
fprintf(stderr, "Unknown sheet type\n");
return -1;
}
}
static int read_binary_float_sheet(int x, int y, float ** E, const char * filename) {
gzFile sheet_file;
int rw;
sheet_file = gzopen(filename, "r");
if (!sheet_file) {
perror(filename);
return -1;
}
rw = gzread(sheet_file, E[0], sizeof(float)*x*y);
if (rw != x*y*sizeof(float)) {
fprintf(stderr, "Problem reading %s\n", filename);
fprintf(stderr, "%d/%d floats read\n", rw, x*y);
gzclose(sheet_file);
return -1;
}
gzclose(sheet_file);
return 0;
}
static int read_binary_double_sheet(int x, int y, float ** E, const char * filename) {
gzFile sheet_file;
int rw, j, i;
double ** sheet;
sheet_file = gzopen(filename, "r");
if (!sheet_file) {
perror(filename);
return -1;
}
D_ARRAY_2D(sheet, y, x);
rw = gzread(sheet_file, sheet[0], sizeof(double)*x*y);
if (rw != x*y*sizeof(double)) {
fprintf(stderr, "Problem reading %s\n", filename);
fprintf(stderr, "%d/%d doubles read\n", rw, x*y);
free(sheet[0]);
free(sheet);
gzclose(sheet_file);
return -1;
}
for (j = 0; j < y; ++j) {
for (i = 0; i < x; ++i) {
E[j][i] = (float) sheet[j][i];
}
}
free(sheet[0]);
free(sheet);
gzclose(sheet_file);
return 0;
}
static int read_text_sheet(int x, int y, float ** E, const char * filename) {
gzFile sheet_file;
char *buffer, *c, *pos, *prev_pos;
size_t i, j;
sheet_file = gzopen(filename, "r");
if (!sheet_file) {
perror(filename);
return -1;
}
// allocate a 1mb buffer
buffer = calloc(BUFSIZE, sizeof(char));
if (!buffer) {
gzclose(sheet_file);
return -1;
}
// zero our counters
i = j = 0;
while (j < y) {
// read in a line
c = gzgets(sheet_file, buffer, BUFSIZE);
// check we were successful.
if (Z_NULL == c) {
fprintf(stderr, "Error reading from '%s' on line %lu\n", filename, j);
free(buffer);
gzclose(sheet_file);
return -1;
}
pos = buffer;
prev_pos = 0;
i = 0;
while ((i < x) && (prev_pos != pos)) {
// assign the previous position to prev_pos
// this will be used to tell when we have the last float in the
// line.
prev_pos = pos;
// read the float from the buffer line
E[j][i] = (float) strtod(pos, &pos);
++i;
}
// check we read in all we should.
if (i != x) {
fprintf(stderr, "Error reading from '%s' on line %lu. %lu/%d floats read\n", filename, j, i, x);
free(buffer);
gzclose(sheet_file);
return -1;
}
++j;
// if we've reached the end of the file, break.
if (gzeof(sheet_file))
break;
}
// check we read in all we should.
if (j != y) {
fprintf(stderr, "Error reading from '%s'. %lu/%d lines read\n", filename, j, y);
free(buffer);
gzclose(sheet_file);
return -1;
}
// cleanup, and return.
free(buffer);
gzclose(sheet_file);
return 0;
}