-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathread_bkn.c
252 lines (202 loc) · 6.09 KB
/
read_bkn.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include <stdlib.h>
#include <string.h>
#include "read_bkn.h"
struct bkn_file {
uint8_t *buffer;
size_t size;
size_t offset;
};
/**
* Searches a BKN file for the first occurrence of a target sequence of bytes, starting
* from the current offset.
*
* bknFile -- BKN file structure.
* target -- The sequence of bytes locate in `buffer`.
* targetSize -- The size of `target`.
*
* Returns:
* The index immediately following `target` if `target` is found; -1 otherwise.
*/
static bool search_bkn_file(struct bkn_file* bknFile, uint8_t *target, size_t targetSize) {
size_t targetIndex = 0;
while (bknFile->offset < bknFile->size) {
if (bknFile->buffer[bknFile->offset] == target[targetIndex]) {
if (++targetIndex == targetSize) {
bknFile->offset++;
return true;
}
}
else {
if (targetIndex != 0) {
bknFile->offset -= targetIndex;
}
targetIndex = 0;
}
bknFile->offset++;
}
bknFile->offset = bknFile->size -1;
return false;
}
/**
* Reads a float from the current offset.
*
* bknFile -- BKN file structure.
*
* Returns:
* Float value.
*/
static float read_float(struct bkn_file* bknFile) {
float value;
memcpy(&value, bknFile->buffer + bknFile->offset, sizeof(float));
bknFile->offset += sizeof(float);
return value;
}
/*
* Reads the absorbance/time points from the current offset.
*
* bknFile -- BKN file structure.
* numPoints -- Number of points to read.
*
* Returns:
* Array of points.
*/
static struct bkn_point* read_bkn_points(struct bkn_file* bknFile, int numPoints) {
struct bkn_point* bknPoints = malloc(numPoints * sizeof(struct bkn_point));
if (bknPoints == NULL) {
out_of_memory();
}
uint32_t i;
for (i = 0; i < numPoints; i++) {
// A point is a pair of single-precision floating point values
bknPoints[i].absorbance = read_float(bknFile);
bknPoints[i].time = read_float(bknFile);
}
return bknPoints;
}
/*
* Reads a metadata field from the current offset.
*
* bknFile -- BKN file structure.
*
* Returns:
* Field value.
*/
static char* read_bkn_field(struct bkn_file* bknFile) {
// The field is preceded by a 32-bit integer indicating the number of
// bytes in the field
uint32_t fieldLength;
memcpy(&fieldLength, bknFile->buffer + bknFile->offset, 4);
bknFile->offset += 4;
// Read in the field value
char* value = malloc((fieldLength * sizeof(char)) + 1);
if (value == NULL) {
out_of_memory();
}
memcpy(value, bknFile->buffer + bknFile->offset, fieldLength);
bknFile->offset += fieldLength;
value[fieldLength] = '\0';
return value;
}
/*
* Reads method data from the current offset.
*
* bknFile -- BKN file structure.
* bknMethod -- Structure method data will be written into.
*
* Returns:
* BKN method.
*/
static struct bkn_method* read_bkn_method(struct bkn_file* bknFile, struct bkn_method* bknMethod) {
// Seek to the number of points in the method
bknFile->offset += 0x1C;
// The number of points is indicated with a 32-bit integer
memcpy(&bknMethod->numPoints, bknFile->buffer + bknFile->offset, 4);
// Seek to the start of the points
bknFile->offset += 0x3EC;
bknMethod->points = read_bkn_points(bknFile, bknMethod->numPoints);
// The metadata immediately follows the points
char* value;
char* endValue = "End Method";
while(true) {
value = read_bkn_field(bknFile);
bknMethod->metadata = realloc(bknMethod->metadata, (bknMethod->numMetadata + 1) * sizeof(char*));
if (bknMethod->metadata == NULL) {
out_of_memory();
}
bknMethod->metadata[bknMethod->numMetadata] = value;
bknMethod->numMetadata++;
if (strncmp(endValue, value, strlen(endValue)) == 0) {
break;
}
}
return bknMethod;
}
/*
* Reads a batch kinetics file (.bkn) into a file structure.
*
* bknFile -- BKN file structure.
* filePath -- Path to the file.
*
* Returns:
* true if file is read successfully; false otherwise.
*/
static bool load_bkn_file(struct bkn_file* bknFile, char* filePath) {
// Open the file
FILE* fileHandle = fopen(filePath, "rb");
if (fileHandle == NULL) {
fprintf(stderr, "Unable to open '%s'.\n", filePath);
return false;
}
// Get the file size in bytes
fseek(fileHandle, 0, SEEK_END);
long fileSize = ftell(fileHandle);
rewind(fileHandle);
// Allocate memory for the whole file
uint8_t* buffer = malloc(fileSize * sizeof(uint8_t));
if(buffer == NULL) {
out_of_memory();
}
// Read the file into memory
size_t bytesRead = fread(buffer, 1, fileSize, fileHandle);
fclose(fileHandle);
if (bytesRead != fileSize) {
fprintf(stderr, "Error reading %s.\n", filePath);
free(buffer);
return false;
}
bknFile->buffer = buffer;
bknFile->size = (size_t)fileSize;
bknFile->offset = 0;
return true;
}
/**
* Reads method data from a BKN file.
*
* filePath -- Path to .BKN file.
*
* Returns:
* true if read was successful; false otherwise.
*/
bool read_bkn(char* filePath, struct bkn_data* bknData) {
// Load the file into memory
struct bkn_file bknFile;
memset(&bknFile, 0, sizeof(struct bkn_file));
if (!load_bkn_file(&bknFile, filePath)) {
return false;
}
// Scan the file from top to bottom for method data
char* methodStartValue = "TContinuumStore";
while (1) {
if (!search_bkn_file(&bknFile, (uint8_t*)methodStartValue, strlen(methodStartValue))) {
// No more method data in file
break;
}
bknData->methods = realloc(bknData->methods, (bknData->numMethods + 1) * sizeof(struct bkn_method));
if (bknData->methods == NULL) {
out_of_memory();
}
read_bkn_method(&bknFile, &bknData->methods[bknData->numMethods]);
bknData->numMethods++;
}
return true;
}