1 | /** |
---|
2 | * @file rw.c |
---|
3 | * @author Sheng Di |
---|
4 | * @date April, 2015 |
---|
5 | * @brief io interface for fortrance |
---|
6 | * (C) 2015 by Mathematics and Computer Science (MCS), Argonne National Laboratory. |
---|
7 | * See COPYRIGHT in top-level directory. |
---|
8 | */ |
---|
9 | |
---|
10 | #include <stdio.h> |
---|
11 | #include <stdlib.h> |
---|
12 | #include <string.h> |
---|
13 | #include <stdint.h> |
---|
14 | #include "rw.h" |
---|
15 | #include "sz.h" |
---|
16 | |
---|
17 | int checkFileExistance(char* filePath) |
---|
18 | { |
---|
19 | if( access( filePath, F_OK ) != -1 ) { |
---|
20 | // file exists |
---|
21 | return 1; |
---|
22 | } else { |
---|
23 | // file doesn't exist |
---|
24 | return 0; |
---|
25 | } |
---|
26 | } |
---|
27 | |
---|
28 | float** create2DArray_float(size_t m, size_t n) |
---|
29 | { |
---|
30 | size_t i=0; |
---|
31 | float **data = (float**)malloc(sizeof(float*)*m); |
---|
32 | for(i=0;i<m;i++) |
---|
33 | data[i] = (float*)malloc(sizeof(float)*n); |
---|
34 | return data; |
---|
35 | } |
---|
36 | |
---|
37 | void free2DArray_float(float** data, size_t m) |
---|
38 | { |
---|
39 | size_t i = 0; |
---|
40 | for(i=0;i<m;i++) |
---|
41 | free(data[i]); |
---|
42 | free(data); |
---|
43 | } |
---|
44 | |
---|
45 | float*** create3DArray_float(size_t p, size_t m, size_t n) |
---|
46 | { |
---|
47 | size_t i = 0, j = 0; |
---|
48 | float ***data = (float***)malloc(sizeof(float**)*m); |
---|
49 | for(i=0;i<p;i++) |
---|
50 | { |
---|
51 | data[i] = (float**)malloc(sizeof(float*)*n); |
---|
52 | for(j=0;j<m;j++) |
---|
53 | data[i][j] = (float*)malloc(sizeof(float)*n); |
---|
54 | } |
---|
55 | return data; |
---|
56 | } |
---|
57 | |
---|
58 | void free3DArray_float(float*** data, size_t p, size_t m) |
---|
59 | { |
---|
60 | size_t i,j; |
---|
61 | for(i=0;i<p;i++) |
---|
62 | { |
---|
63 | for(j=0;j<m;j++) |
---|
64 | free(data[i][j]); |
---|
65 | free(data[i]); |
---|
66 | } |
---|
67 | free(data); |
---|
68 | } |
---|
69 | |
---|
70 | double** create2DArray_double(size_t m, size_t n) |
---|
71 | { |
---|
72 | size_t i=0; |
---|
73 | double **data = (double**)malloc(sizeof(double*)*m); |
---|
74 | for(i=0;i<m;i++) |
---|
75 | data[i] = (double*)malloc(sizeof(double)*n); |
---|
76 | |
---|
77 | return data; |
---|
78 | } |
---|
79 | |
---|
80 | void free2DArray_double(double** data, size_t m) |
---|
81 | { |
---|
82 | size_t i; |
---|
83 | for(i=0;i<m;i++) |
---|
84 | free(data[i]); |
---|
85 | free(data); |
---|
86 | } |
---|
87 | |
---|
88 | double*** create3DArray_double(size_t p, size_t m, size_t n) |
---|
89 | { |
---|
90 | size_t i = 0, j = 0; |
---|
91 | double ***data = (double***)malloc(sizeof(double**)*m); |
---|
92 | for(i=0;i<p;i++) |
---|
93 | { |
---|
94 | data[i] = (double**)malloc(sizeof(double*)*n); |
---|
95 | for(j=0;j<m;j++) |
---|
96 | data[i][j] = (double*)malloc(sizeof(double)*n); |
---|
97 | } |
---|
98 | return data; |
---|
99 | } |
---|
100 | |
---|
101 | void free3DArray_double(double*** data, size_t p, size_t m) |
---|
102 | { |
---|
103 | size_t i,j; |
---|
104 | for(i=0;i<p;i++) |
---|
105 | { |
---|
106 | for(j=0;j<m;j++) |
---|
107 | free(data[i][j]); |
---|
108 | free(data[i]); |
---|
109 | } |
---|
110 | free(data); |
---|
111 | } |
---|
112 | |
---|
113 | size_t checkFileSize(char *srcFilePath, int *status) |
---|
114 | { |
---|
115 | size_t filesize; |
---|
116 | FILE *pFile = fopen(srcFilePath, "rb"); |
---|
117 | if (pFile == NULL) |
---|
118 | { |
---|
119 | printf("Failed to open input file. 1\n"); |
---|
120 | *status = SZ_FERR; |
---|
121 | return -1; |
---|
122 | } |
---|
123 | fseek(pFile, 0, SEEK_END); |
---|
124 | filesize = ftell(pFile); |
---|
125 | fclose(pFile); |
---|
126 | *status = SZ_SCES; |
---|
127 | return filesize; |
---|
128 | } |
---|
129 | |
---|
130 | unsigned char *readByteData(char *srcFilePath, size_t *byteLength, int *status) |
---|
131 | { |
---|
132 | FILE *pFile = fopen(srcFilePath, "rb"); |
---|
133 | if (pFile == NULL) |
---|
134 | { |
---|
135 | printf("Failed to open input file. 1\n"); |
---|
136 | *status = SZ_FERR; |
---|
137 | return 0; |
---|
138 | } |
---|
139 | fseek(pFile, 0, SEEK_END); |
---|
140 | *byteLength = ftell(pFile); |
---|
141 | fclose(pFile); |
---|
142 | |
---|
143 | unsigned char *byteBuf = ( unsigned char *)malloc((*byteLength)*sizeof(unsigned char)); //sizeof(char)==1 |
---|
144 | |
---|
145 | pFile = fopen(srcFilePath, "rb"); |
---|
146 | if (pFile == NULL) |
---|
147 | { |
---|
148 | printf("Failed to open input file. 2\n"); |
---|
149 | *status = SZ_FERR; |
---|
150 | return 0; |
---|
151 | } |
---|
152 | fread(byteBuf, 1, *byteLength, pFile); |
---|
153 | fclose(pFile); |
---|
154 | *status = SZ_SCES; |
---|
155 | return byteBuf; |
---|
156 | } |
---|
157 | |
---|
158 | double *readDoubleData(char *srcFilePath, size_t *nbEle, int *status) |
---|
159 | { |
---|
160 | int state = SZ_SCES; |
---|
161 | if(dataEndianType==sysEndianType) |
---|
162 | { |
---|
163 | double *daBuf = readDoubleData_systemEndian(srcFilePath, nbEle,&state); |
---|
164 | *status = state; |
---|
165 | return daBuf; |
---|
166 | } |
---|
167 | else |
---|
168 | { |
---|
169 | size_t i,j; |
---|
170 | |
---|
171 | size_t byteLength; |
---|
172 | unsigned char* bytes = readByteData(srcFilePath, &byteLength, &state); |
---|
173 | if(state==SZ_FERR) |
---|
174 | { |
---|
175 | *status = SZ_FERR; |
---|
176 | return NULL; |
---|
177 | } |
---|
178 | double *daBuf = (double *)malloc(byteLength); |
---|
179 | *nbEle = byteLength/8; |
---|
180 | |
---|
181 | ldouble buf; |
---|
182 | for(i = 0;i<*nbEle;i++) |
---|
183 | { |
---|
184 | j = i*8; |
---|
185 | memcpy(buf.byte, bytes+j, 8); |
---|
186 | symTransform_8bytes(buf.byte); |
---|
187 | daBuf[i] = buf.value; |
---|
188 | } |
---|
189 | free(bytes); |
---|
190 | return daBuf; |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | |
---|
195 | int8_t *readInt8Data(char *srcFilePath, size_t *nbEle, int *status) |
---|
196 | { |
---|
197 | int state = SZ_SCES; |
---|
198 | int8_t *daBuf = readInt8Data_systemEndian(srcFilePath, nbEle, &state); |
---|
199 | *status = state; |
---|
200 | return daBuf; |
---|
201 | } |
---|
202 | |
---|
203 | int16_t *readInt16Data(char *srcFilePath, size_t *nbEle, int *status) |
---|
204 | { |
---|
205 | int state = SZ_SCES; |
---|
206 | if(dataEndianType==sysEndianType) |
---|
207 | { |
---|
208 | int16_t *daBuf = readInt16Data_systemEndian(srcFilePath, nbEle, &state); |
---|
209 | *status = state; |
---|
210 | return daBuf; |
---|
211 | } |
---|
212 | else |
---|
213 | { |
---|
214 | size_t i,j; |
---|
215 | |
---|
216 | size_t byteLength; |
---|
217 | unsigned char* bytes = readByteData(srcFilePath, &byteLength, &state); |
---|
218 | if(state == SZ_FERR) |
---|
219 | { |
---|
220 | *status = SZ_FERR; |
---|
221 | return NULL; |
---|
222 | } |
---|
223 | int16_t *daBuf = (int16_t *)malloc(byteLength); |
---|
224 | *nbEle = byteLength/2; |
---|
225 | |
---|
226 | lint16 buf; |
---|
227 | for(i = 0;i<*nbEle;i++) |
---|
228 | { |
---|
229 | j = i << 1;//*2 |
---|
230 | memcpy(buf.byte, bytes+j, 2); |
---|
231 | symTransform_2bytes(buf.byte); |
---|
232 | daBuf[i] = buf.svalue; |
---|
233 | } |
---|
234 | free(bytes); |
---|
235 | return daBuf; |
---|
236 | } |
---|
237 | } |
---|
238 | |
---|
239 | uint16_t *readUInt16Data(char *srcFilePath, size_t *nbEle, int *status) |
---|
240 | { |
---|
241 | int state = SZ_SCES; |
---|
242 | if(dataEndianType==sysEndianType) |
---|
243 | { |
---|
244 | uint16_t *daBuf = readUInt16Data_systemEndian(srcFilePath, nbEle, &state); |
---|
245 | *status = state; |
---|
246 | return daBuf; |
---|
247 | } |
---|
248 | else |
---|
249 | { |
---|
250 | size_t i,j; |
---|
251 | |
---|
252 | size_t byteLength; |
---|
253 | unsigned char* bytes = readByteData(srcFilePath, &byteLength, &state); |
---|
254 | if(state == SZ_FERR) |
---|
255 | { |
---|
256 | *status = SZ_FERR; |
---|
257 | return NULL; |
---|
258 | } |
---|
259 | uint16_t *daBuf = (uint16_t *)malloc(byteLength); |
---|
260 | *nbEle = byteLength/2; |
---|
261 | |
---|
262 | lint16 buf; |
---|
263 | for(i = 0;i<*nbEle;i++) |
---|
264 | { |
---|
265 | j = i << 1;//*2 |
---|
266 | memcpy(buf.byte, bytes+j, 2); |
---|
267 | symTransform_2bytes(buf.byte); |
---|
268 | daBuf[i] = buf.usvalue; |
---|
269 | } |
---|
270 | free(bytes); |
---|
271 | return daBuf; |
---|
272 | } |
---|
273 | } |
---|
274 | |
---|
275 | int32_t *readInt32Data(char *srcFilePath, size_t *nbEle, int *status) |
---|
276 | { |
---|
277 | int state = SZ_SCES; |
---|
278 | if(dataEndianType==sysEndianType) |
---|
279 | { |
---|
280 | int32_t *daBuf = readInt32Data_systemEndian(srcFilePath, nbEle, &state); |
---|
281 | *status = state; |
---|
282 | return daBuf; |
---|
283 | } |
---|
284 | else |
---|
285 | { |
---|
286 | size_t i,j; |
---|
287 | |
---|
288 | size_t byteLength; |
---|
289 | unsigned char* bytes = readByteData(srcFilePath, &byteLength, &state); |
---|
290 | if(state == SZ_FERR) |
---|
291 | { |
---|
292 | *status = SZ_FERR; |
---|
293 | return NULL; |
---|
294 | } |
---|
295 | int32_t *daBuf = (int32_t *)malloc(byteLength); |
---|
296 | *nbEle = byteLength/4; |
---|
297 | |
---|
298 | lint32 buf; |
---|
299 | for(i = 0;i<*nbEle;i++) |
---|
300 | { |
---|
301 | j = i*4; |
---|
302 | memcpy(buf.byte, bytes+j, 4); |
---|
303 | symTransform_4bytes(buf.byte); |
---|
304 | daBuf[i] = buf.ivalue; |
---|
305 | } |
---|
306 | free(bytes); |
---|
307 | return daBuf; |
---|
308 | } |
---|
309 | } |
---|
310 | |
---|
311 | uint32_t *readUInt32Data(char *srcFilePath, size_t *nbEle, int *status) |
---|
312 | { |
---|
313 | int state = SZ_SCES; |
---|
314 | if(dataEndianType==sysEndianType) |
---|
315 | { |
---|
316 | uint32_t *daBuf = readUInt32Data_systemEndian(srcFilePath, nbEle, &state); |
---|
317 | *status = state; |
---|
318 | return daBuf; |
---|
319 | } |
---|
320 | else |
---|
321 | { |
---|
322 | size_t i,j; |
---|
323 | |
---|
324 | size_t byteLength; |
---|
325 | unsigned char* bytes = readByteData(srcFilePath, &byteLength, &state); |
---|
326 | if(state == SZ_FERR) |
---|
327 | { |
---|
328 | *status = SZ_FERR; |
---|
329 | return NULL; |
---|
330 | } |
---|
331 | uint32_t *daBuf = (uint32_t *)malloc(byteLength); |
---|
332 | *nbEle = byteLength/4; |
---|
333 | |
---|
334 | lint32 buf; |
---|
335 | for(i = 0;i<*nbEle;i++) |
---|
336 | { |
---|
337 | j = i << 2; //*4 |
---|
338 | memcpy(buf.byte, bytes+j, 4); |
---|
339 | symTransform_4bytes(buf.byte); |
---|
340 | daBuf[i] = buf.uivalue; |
---|
341 | } |
---|
342 | free(bytes); |
---|
343 | return daBuf; |
---|
344 | } |
---|
345 | } |
---|
346 | |
---|
347 | int64_t *readInt64Data(char *srcFilePath, size_t *nbEle, int *status) |
---|
348 | { |
---|
349 | int state = SZ_SCES; |
---|
350 | if(dataEndianType==sysEndianType) |
---|
351 | { |
---|
352 | int64_t *daBuf = readInt64Data_systemEndian(srcFilePath, nbEle, &state); |
---|
353 | *status = state; |
---|
354 | return daBuf; |
---|
355 | } |
---|
356 | else |
---|
357 | { |
---|
358 | size_t i,j; |
---|
359 | |
---|
360 | size_t byteLength; |
---|
361 | unsigned char* bytes = readByteData(srcFilePath, &byteLength, &state); |
---|
362 | if(state == SZ_FERR) |
---|
363 | { |
---|
364 | *status = SZ_FERR; |
---|
365 | return NULL; |
---|
366 | } |
---|
367 | int64_t *daBuf = (int64_t *)malloc(byteLength); |
---|
368 | *nbEle = byteLength/8; |
---|
369 | |
---|
370 | lint64 buf; |
---|
371 | for(i = 0;i<*nbEle;i++) |
---|
372 | { |
---|
373 | j = i << 3; //*8 |
---|
374 | memcpy(buf.byte, bytes+j, 8); |
---|
375 | symTransform_8bytes(buf.byte); |
---|
376 | daBuf[i] = buf.lvalue; |
---|
377 | } |
---|
378 | free(bytes); |
---|
379 | return daBuf; |
---|
380 | } |
---|
381 | } |
---|
382 | |
---|
383 | uint64_t *readUInt64Data(char *srcFilePath, size_t *nbEle, int *status) |
---|
384 | { |
---|
385 | int state = SZ_SCES; |
---|
386 | if(dataEndianType==sysEndianType) |
---|
387 | { |
---|
388 | uint64_t *daBuf = readUInt64Data_systemEndian(srcFilePath, nbEle, &state); |
---|
389 | *status = state; |
---|
390 | return daBuf; |
---|
391 | } |
---|
392 | else |
---|
393 | { |
---|
394 | size_t i,j; |
---|
395 | |
---|
396 | size_t byteLength; |
---|
397 | unsigned char* bytes = readByteData(srcFilePath, &byteLength, &state); |
---|
398 | if(state == SZ_FERR) |
---|
399 | { |
---|
400 | *status = SZ_FERR; |
---|
401 | return NULL; |
---|
402 | } |
---|
403 | uint64_t *daBuf = (uint64_t *)malloc(byteLength); |
---|
404 | *nbEle = byteLength/8; |
---|
405 | |
---|
406 | lint64 buf; |
---|
407 | for(i = 0;i<*nbEle;i++) |
---|
408 | { |
---|
409 | j = i << 3; //*8 |
---|
410 | memcpy(buf.byte, bytes+j, 8); |
---|
411 | symTransform_8bytes(buf.byte); |
---|
412 | daBuf[i] = buf.ulvalue; |
---|
413 | } |
---|
414 | free(bytes); |
---|
415 | return daBuf; |
---|
416 | } |
---|
417 | } |
---|
418 | |
---|
419 | |
---|
420 | float *readFloatData(char *srcFilePath, size_t *nbEle, int *status) |
---|
421 | { |
---|
422 | int state = SZ_SCES; |
---|
423 | if(dataEndianType==sysEndianType) |
---|
424 | { |
---|
425 | float *daBuf = readFloatData_systemEndian(srcFilePath, nbEle, &state); |
---|
426 | *status = state; |
---|
427 | return daBuf; |
---|
428 | } |
---|
429 | else |
---|
430 | { |
---|
431 | size_t i,j; |
---|
432 | |
---|
433 | size_t byteLength; |
---|
434 | unsigned char* bytes = readByteData(srcFilePath, &byteLength, &state); |
---|
435 | if(state == SZ_FERR) |
---|
436 | { |
---|
437 | *status = SZ_FERR; |
---|
438 | return NULL; |
---|
439 | } |
---|
440 | float *daBuf = (float *)malloc(byteLength); |
---|
441 | *nbEle = byteLength/4; |
---|
442 | |
---|
443 | lfloat buf; |
---|
444 | for(i = 0;i<*nbEle;i++) |
---|
445 | { |
---|
446 | j = i*4; |
---|
447 | memcpy(buf.byte, bytes+j, 4); |
---|
448 | symTransform_4bytes(buf.byte); |
---|
449 | daBuf[i] = buf.value; |
---|
450 | } |
---|
451 | free(bytes); |
---|
452 | return daBuf; |
---|
453 | } |
---|
454 | } |
---|
455 | |
---|
456 | double *readDoubleData_systemEndian(char *srcFilePath, size_t *nbEle, int *status) |
---|
457 | { |
---|
458 | size_t inSize; |
---|
459 | FILE *pFile = fopen(srcFilePath, "rb"); |
---|
460 | if (pFile == NULL) |
---|
461 | { |
---|
462 | printf("Failed to open input file. 1\n"); |
---|
463 | *status = SZ_FERR; |
---|
464 | return NULL; |
---|
465 | } |
---|
466 | fseek(pFile, 0, SEEK_END); |
---|
467 | inSize = ftell(pFile); |
---|
468 | *nbEle = inSize/8; //only support double in this version |
---|
469 | fclose(pFile); |
---|
470 | |
---|
471 | double *daBuf = (double *)malloc(inSize); |
---|
472 | |
---|
473 | pFile = fopen(srcFilePath, "rb"); |
---|
474 | if (pFile == NULL) |
---|
475 | { |
---|
476 | printf("Failed to open input file. 2\n"); |
---|
477 | *status = SZ_FERR; |
---|
478 | return NULL; |
---|
479 | } |
---|
480 | fread(daBuf, 8, *nbEle, pFile); |
---|
481 | fclose(pFile); |
---|
482 | *status = SZ_SCES; |
---|
483 | return daBuf; |
---|
484 | } |
---|
485 | |
---|
486 | |
---|
487 | int8_t *readInt8Data_systemEndian(char *srcFilePath, size_t *nbEle, int *status) |
---|
488 | { |
---|
489 | size_t inSize; |
---|
490 | FILE *pFile = fopen(srcFilePath, "rb"); |
---|
491 | if (pFile == NULL) |
---|
492 | { |
---|
493 | printf("Failed to open input file. 1\n"); |
---|
494 | *status = SZ_FERR; |
---|
495 | return NULL; |
---|
496 | } |
---|
497 | fseek(pFile, 0, SEEK_END); |
---|
498 | inSize = ftell(pFile); |
---|
499 | *nbEle = inSize; |
---|
500 | fclose(pFile); |
---|
501 | |
---|
502 | if(inSize<=0) |
---|
503 | { |
---|
504 | printf("Error: input file is wrong!\n"); |
---|
505 | *status = SZ_FERR; |
---|
506 | } |
---|
507 | |
---|
508 | int8_t *daBuf = (int8_t *)malloc(inSize); |
---|
509 | |
---|
510 | pFile = fopen(srcFilePath, "rb"); |
---|
511 | if (pFile == NULL) |
---|
512 | { |
---|
513 | printf("Failed to open input file. 2\n"); |
---|
514 | *status = SZ_FERR; |
---|
515 | return NULL; |
---|
516 | } |
---|
517 | fread(daBuf, 1, *nbEle, pFile); |
---|
518 | fclose(pFile); |
---|
519 | *status = SZ_SCES; |
---|
520 | return daBuf; |
---|
521 | } |
---|
522 | |
---|
523 | |
---|
524 | int16_t *readInt16Data_systemEndian(char *srcFilePath, size_t *nbEle, int *status) |
---|
525 | { |
---|
526 | size_t inSize; |
---|
527 | FILE *pFile = fopen(srcFilePath, "rb"); |
---|
528 | if (pFile == NULL) |
---|
529 | { |
---|
530 | printf("Failed to open input file. 1\n"); |
---|
531 | *status = SZ_FERR; |
---|
532 | return NULL; |
---|
533 | } |
---|
534 | fseek(pFile, 0, SEEK_END); |
---|
535 | inSize = ftell(pFile); |
---|
536 | *nbEle = inSize/2; |
---|
537 | fclose(pFile); |
---|
538 | |
---|
539 | if(inSize<=0) |
---|
540 | { |
---|
541 | printf("Error: input file is wrong!\n"); |
---|
542 | *status = SZ_FERR; |
---|
543 | } |
---|
544 | |
---|
545 | int16_t *daBuf = (int16_t *)malloc(inSize); |
---|
546 | |
---|
547 | pFile = fopen(srcFilePath, "rb"); |
---|
548 | if (pFile == NULL) |
---|
549 | { |
---|
550 | printf("Failed to open input file. 2\n"); |
---|
551 | *status = SZ_FERR; |
---|
552 | return NULL; |
---|
553 | } |
---|
554 | fread(daBuf, 2, *nbEle, pFile); |
---|
555 | fclose(pFile); |
---|
556 | *status = SZ_SCES; |
---|
557 | return daBuf; |
---|
558 | } |
---|
559 | |
---|
560 | uint16_t *readUInt16Data_systemEndian(char *srcFilePath, size_t *nbEle, int *status) |
---|
561 | { |
---|
562 | size_t inSize; |
---|
563 | FILE *pFile = fopen(srcFilePath, "rb"); |
---|
564 | if (pFile == NULL) |
---|
565 | { |
---|
566 | printf("Failed to open input file. 1\n"); |
---|
567 | *status = SZ_FERR; |
---|
568 | return NULL; |
---|
569 | } |
---|
570 | fseek(pFile, 0, SEEK_END); |
---|
571 | inSize = ftell(pFile); |
---|
572 | *nbEle = inSize/2; |
---|
573 | fclose(pFile); |
---|
574 | |
---|
575 | if(inSize<=0) |
---|
576 | { |
---|
577 | printf("Error: input file is wrong!\n"); |
---|
578 | *status = SZ_FERR; |
---|
579 | } |
---|
580 | |
---|
581 | uint16_t *daBuf = (uint16_t *)malloc(inSize); |
---|
582 | |
---|
583 | pFile = fopen(srcFilePath, "rb"); |
---|
584 | if (pFile == NULL) |
---|
585 | { |
---|
586 | printf("Failed to open input file. 2\n"); |
---|
587 | *status = SZ_FERR; |
---|
588 | return NULL; |
---|
589 | } |
---|
590 | fread(daBuf, 2, *nbEle, pFile); |
---|
591 | fclose(pFile); |
---|
592 | *status = SZ_SCES; |
---|
593 | return daBuf; |
---|
594 | } |
---|
595 | |
---|
596 | int32_t *readInt32Data_systemEndian(char *srcFilePath, size_t *nbEle, int *status) |
---|
597 | { |
---|
598 | size_t inSize; |
---|
599 | FILE *pFile = fopen(srcFilePath, "rb"); |
---|
600 | if (pFile == NULL) |
---|
601 | { |
---|
602 | printf("Failed to open input file. 1\n"); |
---|
603 | *status = SZ_FERR; |
---|
604 | return NULL; |
---|
605 | } |
---|
606 | fseek(pFile, 0, SEEK_END); |
---|
607 | inSize = ftell(pFile); |
---|
608 | *nbEle = inSize/4; |
---|
609 | fclose(pFile); |
---|
610 | |
---|
611 | if(inSize<=0) |
---|
612 | { |
---|
613 | printf("Error: input file is wrong!\n"); |
---|
614 | *status = SZ_FERR; |
---|
615 | } |
---|
616 | |
---|
617 | int32_t *daBuf = (int32_t *)malloc(inSize); |
---|
618 | |
---|
619 | pFile = fopen(srcFilePath, "rb"); |
---|
620 | if (pFile == NULL) |
---|
621 | { |
---|
622 | printf("Failed to open input file. 2\n"); |
---|
623 | *status = SZ_FERR; |
---|
624 | return NULL; |
---|
625 | } |
---|
626 | fread(daBuf, 4, *nbEle, pFile); |
---|
627 | fclose(pFile); |
---|
628 | *status = SZ_SCES; |
---|
629 | return daBuf; |
---|
630 | } |
---|
631 | |
---|
632 | uint32_t *readUInt32Data_systemEndian(char *srcFilePath, size_t *nbEle, int *status) |
---|
633 | { |
---|
634 | size_t inSize; |
---|
635 | FILE *pFile = fopen(srcFilePath, "rb"); |
---|
636 | if (pFile == NULL) |
---|
637 | { |
---|
638 | printf("Failed to open input file. 1\n"); |
---|
639 | *status = SZ_FERR; |
---|
640 | return NULL; |
---|
641 | } |
---|
642 | fseek(pFile, 0, SEEK_END); |
---|
643 | inSize = ftell(pFile); |
---|
644 | *nbEle = inSize/4; |
---|
645 | fclose(pFile); |
---|
646 | |
---|
647 | if(inSize<=0) |
---|
648 | { |
---|
649 | printf("Error: input file is wrong!\n"); |
---|
650 | *status = SZ_FERR; |
---|
651 | } |
---|
652 | |
---|
653 | uint32_t *daBuf = (uint32_t *)malloc(inSize); |
---|
654 | |
---|
655 | pFile = fopen(srcFilePath, "rb"); |
---|
656 | if (pFile == NULL) |
---|
657 | { |
---|
658 | printf("Failed to open input file. 2\n"); |
---|
659 | *status = SZ_FERR; |
---|
660 | return NULL; |
---|
661 | } |
---|
662 | fread(daBuf, 4, *nbEle, pFile); |
---|
663 | fclose(pFile); |
---|
664 | *status = SZ_SCES; |
---|
665 | return daBuf; |
---|
666 | } |
---|
667 | |
---|
668 | int64_t *readInt64Data_systemEndian(char *srcFilePath, size_t *nbEle, int *status) |
---|
669 | { |
---|
670 | size_t inSize; |
---|
671 | FILE *pFile = fopen(srcFilePath, "rb"); |
---|
672 | if (pFile == NULL) |
---|
673 | { |
---|
674 | printf("Failed to open input file. 1\n"); |
---|
675 | *status = SZ_FERR; |
---|
676 | return NULL; |
---|
677 | } |
---|
678 | fseek(pFile, 0, SEEK_END); |
---|
679 | inSize = ftell(pFile); |
---|
680 | *nbEle = inSize/8; |
---|
681 | fclose(pFile); |
---|
682 | |
---|
683 | if(inSize<=0) |
---|
684 | { |
---|
685 | printf("Error: input file is wrong!\n"); |
---|
686 | *status = SZ_FERR; |
---|
687 | } |
---|
688 | |
---|
689 | int64_t *daBuf = (int64_t *)malloc(inSize); |
---|
690 | |
---|
691 | pFile = fopen(srcFilePath, "rb"); |
---|
692 | if (pFile == NULL) |
---|
693 | { |
---|
694 | printf("Failed to open input file. 2\n"); |
---|
695 | *status = SZ_FERR; |
---|
696 | return NULL; |
---|
697 | } |
---|
698 | fread(daBuf, 8, *nbEle, pFile); |
---|
699 | fclose(pFile); |
---|
700 | *status = SZ_SCES; |
---|
701 | return daBuf; |
---|
702 | } |
---|
703 | |
---|
704 | uint64_t *readUInt64Data_systemEndian(char *srcFilePath, size_t *nbEle, int *status) |
---|
705 | { |
---|
706 | size_t inSize; |
---|
707 | FILE *pFile = fopen(srcFilePath, "rb"); |
---|
708 | if (pFile == NULL) |
---|
709 | { |
---|
710 | printf("Failed to open input file. 1\n"); |
---|
711 | *status = SZ_FERR; |
---|
712 | return NULL; |
---|
713 | } |
---|
714 | fseek(pFile, 0, SEEK_END); |
---|
715 | inSize = ftell(pFile); |
---|
716 | *nbEle = inSize/8; |
---|
717 | fclose(pFile); |
---|
718 | |
---|
719 | if(inSize<=0) |
---|
720 | { |
---|
721 | printf("Error: input file is wrong!\n"); |
---|
722 | *status = SZ_FERR; |
---|
723 | } |
---|
724 | |
---|
725 | uint64_t *daBuf = (uint64_t *)malloc(inSize); |
---|
726 | |
---|
727 | pFile = fopen(srcFilePath, "rb"); |
---|
728 | if (pFile == NULL) |
---|
729 | { |
---|
730 | printf("Failed to open input file. 2\n"); |
---|
731 | *status = SZ_FERR; |
---|
732 | return NULL; |
---|
733 | } |
---|
734 | fread(daBuf, 8, *nbEle, pFile); |
---|
735 | fclose(pFile); |
---|
736 | *status = SZ_SCES; |
---|
737 | return daBuf; |
---|
738 | } |
---|
739 | |
---|
740 | float *readFloatData_systemEndian(char *srcFilePath, size_t *nbEle, int *status) |
---|
741 | { |
---|
742 | size_t inSize; |
---|
743 | FILE *pFile = fopen(srcFilePath, "rb"); |
---|
744 | if (pFile == NULL) |
---|
745 | { |
---|
746 | printf("Failed to open input file. 1\n"); |
---|
747 | *status = SZ_FERR; |
---|
748 | return NULL; |
---|
749 | } |
---|
750 | fseek(pFile, 0, SEEK_END); |
---|
751 | inSize = ftell(pFile); |
---|
752 | *nbEle = inSize/4; |
---|
753 | fclose(pFile); |
---|
754 | |
---|
755 | if(inSize<=0) |
---|
756 | { |
---|
757 | printf("Error: input file is wrong!\n"); |
---|
758 | *status = SZ_FERR; |
---|
759 | } |
---|
760 | |
---|
761 | float *daBuf = (float *)malloc(inSize); |
---|
762 | |
---|
763 | pFile = fopen(srcFilePath, "rb"); |
---|
764 | if (pFile == NULL) |
---|
765 | { |
---|
766 | printf("Failed to open input file. 2\n"); |
---|
767 | *status = SZ_FERR; |
---|
768 | return NULL; |
---|
769 | } |
---|
770 | fread(daBuf, 4, *nbEle, pFile); |
---|
771 | fclose(pFile); |
---|
772 | *status = SZ_SCES; |
---|
773 | return daBuf; |
---|
774 | } |
---|
775 | |
---|
776 | void writeByteData(unsigned char *bytes, size_t byteLength, char *tgtFilePath, int *status) |
---|
777 | { |
---|
778 | FILE *pFile = fopen(tgtFilePath, "wb"); |
---|
779 | if (pFile == NULL) |
---|
780 | { |
---|
781 | printf("Failed to open input file. 3\n"); |
---|
782 | *status = SZ_FERR; |
---|
783 | return; |
---|
784 | } |
---|
785 | |
---|
786 | fwrite(bytes, 1, byteLength, pFile); //write outSize bytes |
---|
787 | fclose(pFile); |
---|
788 | *status = SZ_SCES; |
---|
789 | } |
---|
790 | |
---|
791 | void writeDoubleData(double *data, size_t nbEle, char *tgtFilePath, int *status) |
---|
792 | { |
---|
793 | size_t i = 0; |
---|
794 | char s[64]; |
---|
795 | FILE *pFile = fopen(tgtFilePath, "wb"); |
---|
796 | if (pFile == NULL) |
---|
797 | { |
---|
798 | printf("Failed to open input file. 3\n"); |
---|
799 | *status = SZ_FERR; |
---|
800 | return; |
---|
801 | } |
---|
802 | |
---|
803 | for(i = 0;i<nbEle;i++) |
---|
804 | { |
---|
805 | sprintf(s,"%.20G\n",data[i]); |
---|
806 | fputs(s, pFile); |
---|
807 | } |
---|
808 | |
---|
809 | fclose(pFile); |
---|
810 | *status = SZ_SCES; |
---|
811 | } |
---|
812 | |
---|
813 | void writeFloatData(float *data, size_t nbEle, char *tgtFilePath, int *status) |
---|
814 | { |
---|
815 | size_t i = 0; |
---|
816 | char s[64]; |
---|
817 | FILE *pFile = fopen(tgtFilePath, "wb"); |
---|
818 | if (pFile == NULL) |
---|
819 | { |
---|
820 | printf("Failed to open input file. 3\n"); |
---|
821 | *status = SZ_FERR; |
---|
822 | return; |
---|
823 | } |
---|
824 | |
---|
825 | for(i = 0;i<nbEle;i++) |
---|
826 | { |
---|
827 | //printf("i=%d\n",i); |
---|
828 | //printf("data[i]=%f\n",data[i]); |
---|
829 | sprintf(s,"%.30G\n",data[i]); |
---|
830 | fputs(s, pFile); |
---|
831 | } |
---|
832 | |
---|
833 | fclose(pFile); |
---|
834 | *status = SZ_SCES; |
---|
835 | } |
---|
836 | |
---|
837 | void writeData(void *data, int dataType, size_t nbEle, char *tgtFilePath, int *status) |
---|
838 | { |
---|
839 | int state = SZ_SCES; |
---|
840 | if(dataType == SZ_FLOAT) |
---|
841 | { |
---|
842 | float* dataArray = (float *)data; |
---|
843 | writeFloatData(dataArray, nbEle, tgtFilePath, &state); |
---|
844 | } |
---|
845 | else if(dataType == SZ_DOUBLE) |
---|
846 | { |
---|
847 | double* dataArray = (double *)data; |
---|
848 | writeDoubleData(dataArray, nbEle, tgtFilePath, &state); |
---|
849 | } |
---|
850 | else |
---|
851 | { |
---|
852 | printf("Error: data type cannot be the types other than SZ_FLOAT or SZ_DOUBLE\n"); |
---|
853 | *status = SZ_TERR; //wrong type |
---|
854 | return; |
---|
855 | } |
---|
856 | *status = state; |
---|
857 | } |
---|
858 | |
---|
859 | void writeFloatData_inBytes(float *data, size_t nbEle, char* tgtFilePath, int *status) |
---|
860 | { |
---|
861 | size_t i = 0; |
---|
862 | int state = SZ_SCES; |
---|
863 | lfloat buf; |
---|
864 | unsigned char* bytes = (unsigned char*)malloc(nbEle*sizeof(float)); |
---|
865 | for(i=0;i<nbEle;i++) |
---|
866 | { |
---|
867 | buf.value = data[i]; |
---|
868 | bytes[i*4+0] = buf.byte[0]; |
---|
869 | bytes[i*4+1] = buf.byte[1]; |
---|
870 | bytes[i*4+2] = buf.byte[2]; |
---|
871 | bytes[i*4+3] = buf.byte[3]; |
---|
872 | } |
---|
873 | |
---|
874 | size_t byteLength = nbEle*sizeof(float); |
---|
875 | writeByteData(bytes, byteLength, tgtFilePath, &state); |
---|
876 | free(bytes); |
---|
877 | *status = state; |
---|
878 | } |
---|
879 | |
---|
880 | void writeDoubleData_inBytes(double *data, size_t nbEle, char* tgtFilePath, int *status) |
---|
881 | { |
---|
882 | size_t i = 0, index = 0; |
---|
883 | int state = SZ_SCES; |
---|
884 | ldouble buf; |
---|
885 | unsigned char* bytes = (unsigned char*)malloc(nbEle*sizeof(double)); |
---|
886 | for(i=0;i<nbEle;i++) |
---|
887 | { |
---|
888 | index = i*8; |
---|
889 | buf.value = data[i]; |
---|
890 | bytes[index+0] = buf.byte[0]; |
---|
891 | bytes[index+1] = buf.byte[1]; |
---|
892 | bytes[index+2] = buf.byte[2]; |
---|
893 | bytes[index+3] = buf.byte[3]; |
---|
894 | bytes[index+4] = buf.byte[4]; |
---|
895 | bytes[index+5] = buf.byte[5]; |
---|
896 | bytes[index+6] = buf.byte[6]; |
---|
897 | bytes[index+7] = buf.byte[7]; |
---|
898 | } |
---|
899 | |
---|
900 | size_t byteLength = nbEle*sizeof(double); |
---|
901 | writeByteData(bytes, byteLength, tgtFilePath, &state); |
---|
902 | free(bytes); |
---|
903 | *status = state; |
---|
904 | } |
---|
905 | |
---|
906 | void writeShortData_inBytes(short *states, size_t stateLength, char *tgtFilePath, int *status) |
---|
907 | { |
---|
908 | int state = SZ_SCES; |
---|
909 | size_t byteLength = stateLength*2; |
---|
910 | unsigned char* bytes = (unsigned char*)malloc(byteLength*sizeof(char)); |
---|
911 | convertShortArrayToBytes(states, stateLength, bytes); |
---|
912 | writeByteData(bytes, byteLength, tgtFilePath, &state); |
---|
913 | free(bytes); |
---|
914 | *status = state; |
---|
915 | } |
---|
916 | |
---|
917 | void writeUShortData_inBytes(unsigned short *states, size_t stateLength, char *tgtFilePath, int *status) |
---|
918 | { |
---|
919 | int state = SZ_SCES; |
---|
920 | size_t byteLength = stateLength*2; |
---|
921 | unsigned char* bytes = (unsigned char*)malloc(byteLength*sizeof(char)); |
---|
922 | convertUShortArrayToBytes(states, stateLength, bytes); |
---|
923 | writeByteData(bytes, byteLength, tgtFilePath, &state); |
---|
924 | free(bytes); |
---|
925 | *status = state; |
---|
926 | } |
---|
927 | |
---|
928 | void writeIntData_inBytes(int *states, size_t stateLength, char *tgtFilePath, int *status) |
---|
929 | { |
---|
930 | int state = SZ_SCES; |
---|
931 | size_t byteLength = stateLength*4; |
---|
932 | unsigned char* bytes = (unsigned char*)malloc(byteLength*sizeof(char)); |
---|
933 | convertIntArrayToBytes(states, stateLength, bytes); |
---|
934 | writeByteData(bytes, byteLength, tgtFilePath, &state); |
---|
935 | free(bytes); |
---|
936 | *status = state; |
---|
937 | } |
---|
938 | |
---|
939 | void writeUIntData_inBytes(unsigned int *states, size_t stateLength, char *tgtFilePath, int *status) |
---|
940 | { |
---|
941 | int state = SZ_SCES; |
---|
942 | size_t byteLength = stateLength*4; |
---|
943 | unsigned char* bytes = (unsigned char*)malloc(byteLength*sizeof(char)); |
---|
944 | convertUIntArrayToBytes(states, stateLength, bytes); |
---|
945 | writeByteData(bytes, byteLength, tgtFilePath, &state); |
---|
946 | free(bytes); |
---|
947 | *status = state; |
---|
948 | } |
---|
949 | |
---|
950 | void writeLongData_inBytes(int64_t *states, size_t stateLength, char *tgtFilePath, int *status) |
---|
951 | { |
---|
952 | int state = SZ_SCES; |
---|
953 | size_t byteLength = stateLength*8; |
---|
954 | unsigned char* bytes = (unsigned char*)malloc(byteLength*sizeof(char)); |
---|
955 | convertLongArrayToBytes(states, stateLength, bytes); |
---|
956 | writeByteData(bytes, byteLength, tgtFilePath, &state); |
---|
957 | free(bytes); |
---|
958 | *status = state; |
---|
959 | } |
---|
960 | |
---|
961 | void writeULongData_inBytes(uint64_t *states, size_t stateLength, char *tgtFilePath, int *status) |
---|
962 | { |
---|
963 | int state = SZ_SCES; |
---|
964 | size_t byteLength = stateLength*8; |
---|
965 | unsigned char* bytes = (unsigned char*)malloc(byteLength*sizeof(char)); |
---|
966 | convertULongArrayToBytes(states, stateLength, bytes); |
---|
967 | writeByteData(bytes, byteLength, tgtFilePath, &state); |
---|
968 | free(bytes); |
---|
969 | *status = state; |
---|
970 | } |
---|
971 | |
---|
972 | unsigned short* readShortData(char *srcFilePath, size_t *dataLength, int *status) |
---|
973 | { |
---|
974 | size_t byteLength = 0; |
---|
975 | int state = SZ_SCES; |
---|
976 | unsigned char * bytes = readByteData(srcFilePath, &byteLength, &state); |
---|
977 | *dataLength = byteLength/2; |
---|
978 | unsigned short* states = convertByteDataToUShortArray(bytes, byteLength); |
---|
979 | free(bytes); |
---|
980 | *status = state; |
---|
981 | return states; |
---|
982 | } |
---|
983 | |
---|
984 | void writeStrings(int nbStr, char *str[], char *tgtFilePath, int *status) |
---|
985 | { |
---|
986 | size_t i = 0; |
---|
987 | char s[256]; |
---|
988 | FILE *pFile = fopen(tgtFilePath, "wb"); |
---|
989 | if (pFile == NULL) |
---|
990 | { |
---|
991 | printf("Failed to open input file. 3\n"); |
---|
992 | *status = SZ_FERR; |
---|
993 | return; |
---|
994 | } |
---|
995 | |
---|
996 | for(i = 0;i<nbStr;i++) |
---|
997 | { |
---|
998 | sprintf(s,"%s\n",str[i]); |
---|
999 | fputs(s, pFile); |
---|
1000 | } |
---|
1001 | |
---|
1002 | fclose(pFile); |
---|
1003 | *status = SZ_SCES; |
---|
1004 | } |
---|
1005 | |
---|
1006 | /* |
---|
1007 | //@deprecated |
---|
1008 | //binToPFM_float is to convert the floating-point data to PFM supported by Jpeg XT |
---|
1009 | //But wrong version! |
---|
1010 | //In order to do the conversion, we need to use https://github.com/thorfdbg/difftest_ng according to Thomas Richter. |
---|
1011 | |
---|
1012 | |
---|
1013 | void convertToPFM_float(float *data, size_t r5, size_t r4, size_t r3, size_t r2, size_t r1, int endianType, char *tgtFilePath, int *status) |
---|
1014 | { |
---|
1015 | size_t i, nbEle = computeDataLength(r5, r4, r3, r2, r1); |
---|
1016 | int dim = computeDimension(r5, r4, r3, r2, r1); |
---|
1017 | |
---|
1018 | FILE *pFile = fopen(tgtFilePath, "wb"); |
---|
1019 | if (pFile == NULL) |
---|
1020 | { |
---|
1021 | printf("Failed to open input file. 3\n"); |
---|
1022 | *status = SZ_NSCS; |
---|
1023 | return; |
---|
1024 | } |
---|
1025 | fputs("PF\n", pFile); |
---|
1026 | char strBuf[256]; |
---|
1027 | switch(dim) |
---|
1028 | { |
---|
1029 | case 1: |
---|
1030 | sprintf(strBuf, "%zu\n", r1); |
---|
1031 | break; |
---|
1032 | case 2: |
---|
1033 | sprintf(strBuf, "%zu %zu\n", r1, r2); |
---|
1034 | break; |
---|
1035 | case 3: |
---|
1036 | sprintf(strBuf, "%zu %zu %zu\n", r1, r2, r3); |
---|
1037 | break; |
---|
1038 | case 4: |
---|
1039 | sprintf(strBuf, "%zu %zu %zu %zu\n", r1, r2, r3, r4); |
---|
1040 | break; |
---|
1041 | case 5: |
---|
1042 | sprintf(strBuf, "%zu %zu %zu %zu %zu\n", r1, r2, r3, r4, r5); |
---|
1043 | break; |
---|
1044 | } |
---|
1045 | fputs(strBuf, pFile); |
---|
1046 | if(endianType==LITTLE_ENDIAN) |
---|
1047 | fputs("-1.0\n", pFile); |
---|
1048 | else |
---|
1049 | fputs("1.0\n", pFile); |
---|
1050 | |
---|
1051 | size_t byteLength = nbEle*sizeof(float); |
---|
1052 | lfloat buf; |
---|
1053 | unsigned char* bytes = (unsigned char*)malloc(byteLength); |
---|
1054 | for(i=0;i<nbEle;i++) |
---|
1055 | { |
---|
1056 | buf.value = data[i]; |
---|
1057 | bytes[i*4+0] = buf.byte[0]; |
---|
1058 | bytes[i*4+1] = buf.byte[1]; |
---|
1059 | bytes[i*4+2] = buf.byte[2]; |
---|
1060 | bytes[i*4+3] = buf.byte[3]; |
---|
1061 | } |
---|
1062 | |
---|
1063 | fwrite(bytes, 1, byteLength, pFile); //write outSize bytes |
---|
1064 | fclose(pFile); |
---|
1065 | |
---|
1066 | free(bytes); |
---|
1067 | *status = SZ_SCES; |
---|
1068 | }*/ |
---|