forked from LLNL/cardioid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathioUtils.c
322 lines (277 loc) · 6.62 KB
/
ioUtils.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// $Id$
#include "ioUtils.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include "ddcMalloc.h"
#include "utilities.h"
static int needsSwap=0;
int filetest(const char* filename, int type)
{
struct stat buf;
int rc = stat(filename, &buf);
if (rc == -1 && errno == ENOENT)
{
printf("FILE: %s does not exist\n", filename);
rc = 1;
}
if (rc == 0 && !(buf.st_mode & type))
{
printf("FILE: %s is not the specified type\n", filename);
rc = 2;
}
return rc;
}
int DirTestCreate(const char *dirname)
{
int mode = 0775;
struct stat statbuf;
char line[1024];
int rc;
rc = stat(dirname, &statbuf);
if (rc == -1 && errno == ENOENT)
{
sprintf(line,"Creating Directory: %s\n", dirname);
timestamp(line);
rc = mkdir(dirname, mode);
rc = stat(dirname, &statbuf);
}
if (rc != 0 || !(statbuf.st_mode & S_IFDIR))
{
printf("Can't Stat the Directory %s\n", dirname);
printf("%d %x %x %x\n", rc, statbuf.st_mode, S_IFDIR, statbuf.st_mode & S_IFDIR);
}
return rc;
}
void
ioUtils_setSwap(unsigned endianKey)
{
unsigned keylocal;
memcpy(&keylocal,"1234",4);
needsSwap = (endianKey != keylocal);
}
unsigned long long
mkInt(const unsigned char* data, const char* fieldType)
{
unsigned long long result=0, i8;
unsigned i4;
int invalid = 1;
if (fieldType[0] == 'u')
{
invalid = 0;
long fieldLength = strtol(fieldType+1, NULL, 10);
switch (fieldLength)
{
case 4:
copyBytes(&i4, data, 4);
if (needsSwap) endianSwap(&i4, 4);
result = i4;
break;
case 8:
copyBytes(&i8, data, 8);
if (needsSwap) endianSwap(&i8, 8);
result = i8;
break;
default:
invalid = 1;
}
}
if (fieldType[0] == 'b')
{
invalid = 0;
result = bFieldUnpack(data, fieldType);
}
if (invalid)
{
printf("ERROR: Invalid field type (%s) in mkInt\n", fieldType);
exit(1);
}
return result;
}
double
mkDouble(const unsigned char* data, const char* fieldType)
{
double result, f8;
float f4;
int invalid = 1;
if (fieldType[0] == 'f')
{
invalid = 0;
long fieldLength = strtol(fieldType+1, NULL, 10);
switch (fieldLength)
{
case 4:
copyBytes(&f4, data, 4);
if (needsSwap) endianSwap(&f4, 4);
result = f4;
break;
case 8:
copyBytes(&f8, data, 8);
if (needsSwap) endianSwap(&f8, 8);
result = f8;
break;
default:
invalid = 1;
}
}
if (invalid)
{
printf("ERROR: Invalid field type (%s) in mkDouble\n", fieldType);
exit(1);
}
return result;
}
double
mkValue(const unsigned char* data, const char* fieldType)
{
if (fieldType[0] == 'f') return mkDouble(data, fieldType);
if (fieldType[0] == 'b' || fieldType[0] == 'u') return mkInt(data, fieldType);
assert(1==0);
return 0.0;
}
void
copyBytes(void* to, const void* from, unsigned size)
{
for (unsigned ii=0; ii<size; ++ii)
*(((char*) to)+ii) = *(((char*) from)+ii);
}
unsigned long long
bFieldUnpack(const unsigned char* buf, const char* fieldType)
{
assert(fieldType[0] == 'b');
long fieldLength = strtol(fieldType+1, NULL, 10);
unsigned long long result = 0;
for (long ii=0; ii<fieldLength; ++ii)
{
result *= 256;
result += *(buf+ii);
}
return result;
}
void
bFieldPack(unsigned char* buf, unsigned size, unsigned long long in)
{
for (unsigned ii=0; ii<size; ++ii)
{
buf[(size-1) - ii] = in%256;
in /= 256;
}
}
unsigned bFieldSize(unsigned long long i)
{
unsigned result=0;
do
{
i /= 256;
++result;
}
while (i>0);
return result;
}
unsigned* makeOffsets(char** fieldType, unsigned nFields)
{
unsigned* offset = (unsigned *) ddcMalloc((nFields+1)*sizeof(unsigned));
offset[0] = 0;
for (unsigned ii=0; ii<nFields; ++ii)
{
unsigned fieldSize = strtoul( (fieldType[ii]+1), NULL, 10);
if (fieldSize == 0)
{
printf("ERROR");
exit(1);
}
offset[ii+1] = offset[ii] + (unsigned) fieldSize;
}
return offset;
}
enum IO_FIELDS* makeFieldMap(char** fieldNames, unsigned nFields)
{
enum IO_FIELDS* map = (enum IO_FIELDS*) ddcMalloc(nFields*sizeof(enum IO_FIELDS));
for (unsigned ii=0; ii<nFields; ++ii)
{
map[ii] = IOF_NONE;
if (strcasecmp(fieldNames[ii], "checksum") == 0) map[ii] = IOF_CHECKSUM;
else if (strcasecmp(fieldNames[ii], "id") == 0) map[ii] = IOF_ID;
else if (strcasecmp(fieldNames[ii], "pinfo") == 0) map[ii] = IOF_PINFO;
else if (strcasecmp(fieldNames[ii], "rx") == 0) map[ii] = IOF_RX;
else if (strcasecmp(fieldNames[ii], "ry") == 0) map[ii] = IOF_RY;
else if (strcasecmp(fieldNames[ii], "rz") == 0) map[ii] = IOF_RZ;
else if (strcasecmp(fieldNames[ii], "vx") == 0) map[ii] = IOF_VX;
else if (strcasecmp(fieldNames[ii], "vy") == 0) map[ii] = IOF_VY;
else if (strcasecmp(fieldNames[ii], "vz") == 0) map[ii] = IOF_VZ;
}
return map;
}
/**
* WARNING!! The offset array must have size nFields+1 where nFields is
* the number of fields in the field_types string.
*/
unsigned parseFieldSizes(unsigned* offset, const char* field_types)
{
unsigned count = 0;
char* string = strdup(field_types);
char* word = strtok(string, " ");
assert(word);
offset[0] = 0;
while (word != NULL)
{
assert(strlen(word) == 2);
offset[count+1] = offset[count] + atoi(word+1);
word = strtok(NULL, " ");
++count;
}
ddcFree(string);
return count;
}
int makeOffsetsAndTypes(unsigned nInput, char** inputNames,
char** inputTypes, const char* requestedNames,
unsigned* offset, char** type)
{
int nRequest = 0;
int nWords = 0;
unsigned* inputOffsets = makeOffsets(inputTypes, nInput);
char* string = strdup(requestedNames);
char* word = strtok(string, " ");
if (!word)
return -1;
while (word != NULL)
{
++nWords;
for (unsigned ii=0; ii<nInput; ++ii)
{
if (strcmp(word, inputNames[ii]) == 0)
{
offset[nRequest] = inputOffsets[ii];
strcpy(type[nRequest], inputTypes[ii]);
++nRequest;
continue;
}
}
if (nWords != nRequest)
return -1;
word = strtok(NULL, " ");
}
ddcFree(string);
ddcFree(inputOffsets);
return nRequest;
}
void
endianSwap(void* data, int size)
{
int i, j;
char* b, save;
b = (char*) (data) ;
j = size;
for (i=0; i<size/2; i++)
{
--j;
save = b[i] ;
b[i] = b[j];
b[j] = save;
}
}