-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzdf.c
1738 lines (1430 loc) · 51.6 KB
/
zdf.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file zdf.c
* @author Ricardo
* @brief ZDF file library
* @version 1.0
* @date 2022-02-04
*
* @copyright Copyright (c) 2022
*
* This ZDF version is totally self contained. It does not depend on XDR or any other
* external libraries. Current implementation should also work on big endian systems (untested)
* This version is not compatible is version 0 (files have the opposite endianess)
*/
/**
* @brief Use 2008 edition of the POSIX standard (IEEE Standard 1003.1-2008)
*
*/
#define _POSIX_C_SOURCE 200809L
/**
* @brief Use 64 bit file interface
*
*/
#define _FILE_OFFSET_BITS 64
#include "zdf.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include <inttypes.h>
/**
* On Windows we cannot use the POSIX.1 mkdir command, so use _mkdir instead
*/
#if defined(_MSC_VER) || defined(_WIN32) || defined(_WIN64)
#include <direct.h>
#define mkdir(path,mode) _mkdir(path)
// Windows also lacks the 'fseeko', 'ftello' functions so replace with Windows
// equivalent.
#define fseeko _fseeki64
#define ftello _ftelli64
#endif
/**
* Number of bytes required for writing data to ZDF file
* (round up to multiple of BYTES_PER_ZDF_UNIT)
*/
#define RNDUP(x) ((((x) + BYTES_PER_ZDF_UNIT - 1) / BYTES_PER_ZDF_UNIT) \
* BYTES_PER_ZDF_UNIT)
/**
* Buffer size for converting big endian to little endian data
*/
#define ENDIAN_CONV_BUF_SIZE 1024
/**
* Sizes of datatypes
*/
const unsigned size_zdf_int32 = 4; ///< size of zdf_int32
const unsigned size_zdf_uint32 = 4; ///< size of zdf_uint32
const unsigned size_zdf_int64 = 8; ///< size of zdf_int64
const unsigned size_zdf_uint64 = 8; ///< size of zdf_uint64
const unsigned size_zdf_double = 8; ///< size of zdf_double
const unsigned size_zdf_float = 4; ///< size of zdf_float
/**
* IDs of ZDF records
*/
#define ZDF_INT32_ID 0x00010000 ///< Int32 record ID
#define ZDF_DOUBLE_ID 0x00020000 ///< Double record ID
#define ZDF_STRING_ID 0x00030000 ///< String record ID
#define ZDF_DATASET_ID 0x00100002 ///< Dataset record ID
#define ZDF_CDSET_START_ID 0x00110000 ///< Chunked dataset start record ID
#define ZDF_CDSET_CHUNK_ID 0x00120000 ///< Chunked dataset data chunk record ID
#define ZDF_CDSET_END_ID 0x00130000 ///< Chunked dataset end record ID
#define ZDF_ITERATION_ID 0x00200001 ///< Iteration record ID
#define ZDF_GRID_INFO_ID 0x00210001 ///< Grid information record ID
#define ZDF_PART_INFO_ID 0x00220002 ///< Particle set information record ID
#define ZDF_TRACK_INFO_ID 0x00230001 ///< Particle tracks information record ID
/* -----------------------------------------------------------------------------------------------
recursively create path if required
-------------------------------------------------------------------------------------------------- */
/**
* Recursively creates a path if required
* @param path path to create
* @return returns 0 on success, otherwise returns the value returned by mkdir
*/
int create_path( const char path[] )
{
char uppath[256], *p;
int ierr = 0;
if (mkdir(path,S_IRWXU | (S_IRGRP | S_IXGRP ) | (S_IROTH | S_IXOTH) )) {
switch (errno) {
case ENOENT : // A component of the path does not exist
// get upper path
strncpy(uppath, path, 255);
p = uppath + strlen(uppath);
while(*p!='/') p--;
*p=0;
//recursively build the path
if ( !create_path( uppath ) ) ierr = create_path( path );
break;
case EEXIST : /* if directory already exists ignore the error */
ierr = 0;
break;
default: ierr = errno;
}
}
return ierr;
}
/**
* Returns size of ZDF datatype
* @param data_type Data type id
* @return Returns type size in bytes or 0 for an invalid data type
*/
size_t zdf_sizeof( enum zdf_data_type data_type ) {
switch( data_type ) {
case zdf_null:
return(0);
case zdf_int8:
case zdf_uint8:
return(1);
case zdf_int16:
case zdf_uint16:
return(2);
case zdf_int32:
case zdf_uint32:
case zdf_float32:
return(4);
case zdf_int64:
case zdf_uint64:
case zdf_float64:
return(8);
}
return(0);
}
/* -----------------------------------------------------------------------------------------------
Open / Close ZDF file
-------------------------------------------------------------------------------------------------- */
/**
* Closes ZDF file
* @param zdf ZDF file to close
* @return Returns 1 on success, 0 on error
*/
int zdf_close_file( t_zdf_file* zdf ) {
if ( fclose( zdf->fp ) ) {
perror("(*error*) Unable to close ZDF file");
return(0);
}
zdf -> fp = NULL;
return(1);
}
/**
* Opens ZDF file
* @param zdf ZDF file to open
* @param filename Filename of the ZDF file to open, including path
* @param mode Can be one of ZDF_CREATE, ZDF_READ, or ZDF_UPDATE
* @return Returns 1 on success, 0 on error
*/
int zdf_open_file( t_zdf_file* zdf, const char* filename, enum zdf_file_access_mode mode ){
char test_magic[4];
zdf -> mode = mode;
switch( mode ) {
case ZDF_CREATE :
// Open file for writing
// The "wb" mode must be used for compatibility with Windows
if (!(zdf->fp = fopen( filename, "w+b"))) {
perror("(*error*) Unable to open ZDF file for writing");
return(0);
}
// Write magic number
if ( fwrite( (void *) zdf_magic, sizeof(char), ZDF_MAGIC_LENGTH, zdf->fp )
!= ZDF_MAGIC_LENGTH ) {
fprintf(stderr, "(*error*) Unable to write magic number to ZDF file.\n");
zdf_close_file( zdf );
return(0);
}
break;
case ZDF_READ :
// Open file for reading
if (!(zdf->fp = fopen( filename, "r"))) {
perror("(*error*) Unable to open ZDF file for reading");
return(0);
}
// Read magic number
if ( fread( (void *) test_magic, sizeof(char), ZDF_MAGIC_LENGTH, zdf->fp )
!= ZDF_MAGIC_LENGTH) {
fprintf(stderr, "(*error*) Unable to read magic number from ZDF file.\n");
zdf_close_file( zdf );
return(0);
}
// Check magic number
for( int i = 0; i < ZDF_MAGIC_LENGTH; i++) {
if ( test_magic[i] != zdf_magic[i] ) {
fprintf(stderr, "(*error*) Invalid magic number, file is not a proper ZDF file.\n");
zdf_close_file( zdf );
return(0);
}
}
break;
case ZDF_UPDATE :
// Open file for reading and writing
if (!(zdf->fp = fopen( filename, "r+"))) {
perror("(*error*) Unable to open ZDF file for reading / writing.\n");
return(0);
}
// Read magic number
if (fread( (void *) test_magic, sizeof(char), ZDF_MAGIC_LENGTH, zdf->fp )
!= ZDF_MAGIC_LENGTH) {
fprintf(stderr, "(*error*) Unable to read magic number from ZDF file.\n");
zdf_close_file( zdf );
return(0);
}
// Check magic number
for( int i = 0; i < ZDF_MAGIC_LENGTH; i++) {
if ( test_magic[i] != zdf_magic[i] ) {
fprintf(stderr, "(*error*) Invalid magic number, file is not a proper ZDF file.\n");
zdf_close_file( zdf );
return(0);
}
}
// Position the file pointer at the end of the file
fseeko( zdf->fp, 0, SEEK_END );
break;
default:
fprintf(stderr, "(*error*) zdf_open_file: unsupported mode.\n");
return(0);
}
zdf -> ndatasets = 0;
return(1);
}
/* -----------------------------------------------------------------------------------------------
Elemental types
-------------------------------------------------------------------------------------------------- */
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
/**
* Implementation for little endian systems (e.g. x86)
*
* For these systems just writing the data to disk is sufficient
*/
/**
* Writes scalar int32 value to file
* @param zdf ZDF file descriptor
* @param i int32_t value to write
* @return Returns number of bytes written on success, 0 on error
*/
size_t zdf_int32_write( t_zdf_file* zdf, const int32_t i ){
if ( fwrite( (void *) &i, sizeof(int32_t), 1, zdf -> fp ) != 1 )
return(0);
return ( sizeof(int32_t) );
}
/**
* Reads scalar int32 value from file
* @param zdf ZDF file descriptor
* @param i int32_t value read
* @return Returns number of bytes written on success, 0 on error
*/
size_t zdf_int32_read( t_zdf_file* zdf, int32_t* i ){
if ( fread( (void *) i, sizeof(int32_t), 1, zdf -> fp ) != 1 )
return(0);
return ( sizeof(int32_t) );
}
/**
* Writes scalar uint32 value to file
* @param zdf ZDF file descriptor
* @param u uint32_t value to write
* @return Returns number of bytes written on success, 0 on error
*/
size_t zdf_uint32_write( t_zdf_file* zdf, const uint32_t u ){
if ( fwrite( (void *) &u, sizeof(uint32_t), 1, zdf -> fp ) != 1 )
return(0);
return(sizeof(uint32_t));
}
/**
* Reads scalar uint32 value from file
* @param zdf ZDF file descriptor
* @param u uint32_t value read
* @return Returns number of bytes written on success, 0 on error
*/
size_t zdf_uint32_read( t_zdf_file* zdf, uint32_t* u ){
if ( fread( (void *) u, sizeof(uint32_t), 1, zdf -> fp ) != 1 )
return(0);
return(sizeof(uint32_t));
}
/**
* Writes scalar int64 value to file
* @param zdf ZDF file descriptor
* @param i int64_t value to write
* @return Returns number of bytes written on success, 0 on error
*/
size_t zdf_int64_write( t_zdf_file* zdf, const int64_t i ){
if ( fwrite( (void *) &i, sizeof(int64_t), 1, zdf -> fp ) != 1 )
return(0);
return(sizeof(int64_t));
}
/**
* Reads scalar int64 value from file
* @param zdf ZDF file descriptor
* @param i int64_t value read
* @return Returns number of bytes written on success, 0 on error
*/
size_t zdf_int64_read( t_zdf_file* zdf, int64_t *i ){
if ( fread( (void *) i, sizeof(int64_t), 1, zdf -> fp ) != 1 )
return(0);
return(sizeof(int64_t));
}
/**
* Writes scalar uint64_t value to file
* @param zdf ZDF file descriptor
* @param u uint64_t value to write
* @return Returns number of bytes written on success, 0 on error
*/
size_t zdf_uint64_write( t_zdf_file* zdf, const uint64_t u ){
if ( fwrite( (void *) &u, sizeof(uint64_t), 1, zdf -> fp ) != 1 )
return(0);
return(sizeof(uint64_t));
}
/**
* Reads scalar int64 value from file
* @param zdf ZDF file descriptor
* @param u uint64_t value read
* @return Returns number of bytes written on success, 0 on error
*/
size_t zdf_uint64_read( t_zdf_file* zdf, uint64_t *u ){
if ( fread( (void *) u, sizeof(int64_t), 1, zdf -> fp ) != 1 )
return(0);
return(sizeof(uint64_t));
}
/**
* Writes scalar double (float64) value to file
* @param zdf ZDF file descriptor
* @param d double value to write
* @return Returns number of bytes written on success, 0 on error
*/
size_t zdf_double_write( t_zdf_file* zdf, const double d ){
if ( fwrite( (void *) &d, sizeof(double), 1, zdf -> fp ) != 1 )
return(0);
return(sizeof(double));
}
/**
* Write arbitraty 16bit type vector to file
* @param zdf ZDF file descriptor
* @param data Pointer to data to write
* @param len Number of vector elements
* @return Returns number of bytes written on success, 0 on error
*/
size_t zdf_vector16_write( t_zdf_file* zdf, void const * const data, size_t len ) {
if ( fwrite( data, sizeof(int16_t), len, zdf -> fp ) != len )
return(0);
return( len * sizeof(int16_t) );
}
/**
* Write arbitraty 32bit type vector to file
* @param zdf ZDF file descriptor
* @param data Pointer to data to write
* @param len Number of vector elements
* @return Returns number of bytes written on success, 0 on error
*/
size_t zdf_vector32_write( t_zdf_file* zdf, void const * const data, size_t len ) {
if ( fwrite( data, sizeof(int32_t), len, zdf -> fp ) != len )
return(0);
return( len * sizeof(int32_t) );
}
/**
* Write arbitraty 64bit type vector to file
* @param zdf ZDF file descriptor
* @param data Pointer to data to write
* @param len Number of vector elements
* @return Returns number of bytes written on success, 0 on error
*/
size_t zdf_vector64_write( t_zdf_file* zdf, void const * const data, size_t len ) {
if( fwrite( data, sizeof(int64_t), len, zdf -> fp ) != len )
return(0);
return( len * sizeof(int64_t) );
}
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
/**
* Implementation for big endian systems (e.g. PowerPC)
*
* For these systems we need to swap the bytes before writing. For vectors, conversion
* is done in chunks of ENDIAN_CONV_BUF_SIZE values.
*
* The system may provide hardware optimized bswap_* routines, these are usually found in
* the <byteswap.h> header, but they are missing on many platforms.
*/
/**
* Swap bytes for 16 bit number
* @param x 16 bit number to convert
* @return byte-swapped version of x
*/
#define bswap_16(x) \
({ \
uint16_t __x = (x); \
((uint16_t)( \
(((uint16_t)(__x) & (uint16_t)0x00ffU) << 8) | \
(((uint16_t)(__x) & (uint16_t)0xff00U) >> 8) )); \
})
/**
* Swap bytes for 32 bit number
* @param x 32 bit number to convert
* @return byte-swapped version of x
*/
#define bswap_32(x) \
({ \
uint32_t __x = (x); \
((uint32_t)( \
(((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
(((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
(((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
(((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) )); \
})
/**
* Swap bytes for 64 bit number
* @param x 64 bit number to convert
* @return byte-swapped version of x
*/
#define bswap_64(x) \
({ \
uint64_t __x = (x); \
((uint64_t)( \
(uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000000000ffULL) << 56) | \
(uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
(uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
(uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000ff000000ULL) << 8) | \
(uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \
(uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
(uint64_t)(((uint64_t)(__x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
(uint64_t)(((uint64_t)(__x) & (uint64_t)0xff00000000000000ULL) >> 56) )); \
})
/**
* Writes int32 value to file
* @param zdf ZDF file descriptor
* @param i int32_t value to write
* @return Returns number of bytes written on success, 0 on error
*/
size_t zdf_int32_write( t_zdf_file* zdf, const int32_t i ){
uint32_t tmp = bswap_32((uint32_t)i);
if ( fwrite( (void *) &tmp, sizeof(uint32_t), 1, zdf -> fp ) != 1 )
return(0);
return( sizeof(uint32_t));
}
/**
* Reads scalar int32 value from file
* @param zdf ZDF file descriptor
* @param i int32_t value read
* @return Returns number of bytes read on success, 0 on error
*/
size_t zdf_int32_read( t_zdf_file* zdf, int32_t* i ){
uint32_t tmp;
if ( fread( (void *) &tmp, sizeof(uint32_t), 1, zdf -> fp ) != 1 )
return(0);
*i = (int32_t) bswap_32(tmp);
return( sizeof(uint32_t));
}
/**
* Writes uint32 value to file
* @param zdf ZDF file descriptor
* @param u uint32_t value to write
* @return Returns number of bytes written on success, 0 on error
*/
size_t zdf_uint32_write( t_zdf_file* zdf, const uint32_t u ){
uint32_t tmp = bswap_32(u);
if ( fwrite( (void *) &tmp, sizeof(uint32_t), 1, zdf -> fp ) != 1 )
return(0);
return(sizeof(uint32_t));
}
/**
* Reads scalar uint32 value from file
* @param zdf ZDF file descriptor
* @param i uint32_t value read
* @return Returns number of bytes read on success, 0 on error
*/
size_t zdf_uint32_read( t_zdf_file* zdf, uint32_t* u ){
uint32_t tmp;
if ( fread( (void *) &tmp, sizeof(uint32_t), 1, zdf -> fp ) != 1 )
return(0);
*u = bswap_32(tmp);
return(sizeof(uint32_t));
}
/**
* Writes int64 value to file
* @param zdf ZDF file descriptor
* @param i int64_t value to write
* @return Returns number of bytes written on success, 0 on error
*/
size_t zdf_int64_write( t_zdf_file* zdf, const int64_t i ){
uint64_t tmp = bswap_64((uint64_t)i);
if ( fwrite( (void *) &tmp, sizeof(uint64_t), 1, zdf -> fp ) != 1 )
return(0);
return(sizeof(uint64_t));
}
/**
* Reads scalar int64 value from file
* @param zdf ZDF file descriptor
* @param i int64_t value read
* @return Returns number of bytes read on success, 0 on error
*/
size_t zdf_int64_read( t_zdf_file* zdf, int64_t* i ){
uint64_t tmp;
if ( fread( (void *) &tmp, sizeof(uint64_t), 1, zdf -> fp ) != 1 )
return(0);
*i = (int64_t) bswap_64(tmp);
return(sizeof(uint64_t));
}
/**
* Writes uint64_t value to file
* @param zdf ZDF file descriptor
* @param u uint64_t value to write
* @return Returns number of bytes read on success, 0 on error
*/
size_t zdf_uint64_write( t_zdf_file* zdf, const uint64_t u ){
uint64_t tmp = bswap_64(u);
if ( fwrite( (void *) &tmp, sizeof(uint64_t), 1, zdf -> fp ) != 1 )
return(0);
return(sizeof(uint64_t));
}
/**
* Reads scalar uint64 value from file
* @param zdf ZDF file descriptor
* @param i uint64_t value read
* @return Returns number of bytes read on success, 0 on error
*/
size_t zdf_uint64_read( t_zdf_file* zdf, uint64_t* i ){
uint64_t tmp;
if ( fread( (void *) &tmp, sizeof(uint64_t), 1, zdf -> fp ) != 1 )
return(0);
*i = bswap_64(tmp);
return(sizeof(uint64_t));
}
/**
* Writes double (float64) value to file
* @param zdf ZDF file descriptor
* @param d double value to write
* @return Returns number of bytes written on success, 0 on error
*/
size_t zdf_double_write( t_zdf_file* zdf, const double d ){
uint64_t tmp = bswap_64((uint64_t)d);
if ( fwrite( (void *) &tmp, sizeof(uint64_t), 1, zdf -> fp ) != 1 )
return(0);
return(sizeof(uint64_t));
}
/**
* Write arbitraty 16bit type vector to file
* @param zdf ZDF file descriptor
* @param data Pointer to 16 bit data to write
* @param len Number of vector elements
* @return Returns number of bytes written on success, 0 on error
*/
size_t zdf_vector16_write( t_zdf_file* zdf, void const * const data, size_t len ) {
uint16_t buffer[ENDIAN_CONV_BUF_SIZE];
for( size_t offset = 0; offset < len; offset += ENDIAN_CONV_BUF_SIZE ) {
// Number of values in chunk
size_t chunk_len = (offset + ENDIAN_CONV_BUF_SIZE < len ) ? ENDIAN_CONV_BUF_SIZE : len - offset;
// Convert chunk to little endian
for( size_t i = 0; i < chunk_len; i++) buffer[i] = bswap_16( data[offset+i]);
// Write chunk
if ( fwrite( (void *) buffer, sizeof(uint16_t), chunk_len, zdf -> fp) != chunk_len )
return(0);
}
return( len * sizeof(uint16_t) );
}
/**
* Write arbitraty 32bit type vector to file
* @param zdf ZDF file descriptor
* @param data Pointer to 32 bit data to write
* @param len Number of vector elements
* @return Returns number of bytes written on success, 0 on error
*/
size_t zdf_vector32_write( t_zdf_file* zdf, void const * const data, size_t len ) {
uint32_t buffer[ENDIAN_CONV_BUF_SIZE];
for( size_t offset = 0; offset < len; offset += ENDIAN_CONV_BUF_SIZE ) {
// Number of values in chunk
size_t chunk_len = (offset + ENDIAN_CONV_BUF_SIZE < len ) ? ENDIAN_CONV_BUF_SIZE : len - offset;
// Convert chunk to little endian
for( size_t i = 0; i < chunk_len; i++) buffer[i] = bswap_32( data[offset+i]);
// Write chunk
if ( fwrite( (void *) buffer, sizeof(uint32_t), chunk_len, zdf -> fp) != chunk_len )
return(0);
}
return( len * sizeof(uint32_t) );
}
/**
* Write arbitraty 64bit type vector to file
* @param zdf ZDF file descriptor
* @param data Pointer to 64 bit data to write
* @param len Number of vector elements
* @return Returns number of bytes written on success, 0 on error
*/
size_t zdf_vector64_write( t_zdf_file* zdf, void const * const data, size_t len ) {
uint64_t buffer[ENDIAN_CONV_BUF_SIZE];
for( size_t offset = 0; offset < len; offset += ENDIAN_CONV_BUF_SIZE ) {
// Number of values in chunk
size_t chunk_len = (offset + ENDIAN_CONV_BUF_SIZE < len ) ? ENDIAN_CONV_BUF_SIZE : len - offset;
// Convert chunk to little endian
for( size_t i = 0; i < chunk_len; i++) buffer[i] = bswap_64( data[offset+i] );
// Write chunk
if ( fwrite( (void *) buffer, sizeof(uint64_t), chunk_len, zdf -> fp) != chunk_len )
return(0);
}
return( len * sizeof(uint64_t) );
}
#else
#error "System is neither little endian nor big endian, aborting."
#endif
/**
* Write arbitraty 8bit type vector to file. Adds padding at the end of the vector if
* necessary to maitain alignment.
* @param zdf ZDF file descriptor
* @param u Pointer to 8 bit data
* @param len Number of vector elements
* @return Returns number of bytes written (including padding), 0 on error
*/
size_t zdf_vector8_write( t_zdf_file* zdf, void const * const u, size_t len ){
if ( fwrite( u, sizeof(uint8_t), len, zdf -> fp ) != len ) {
return(0);
}
size_t npad = RNDUP(len) - len;
if ( npad > 0 ) {
const uint8_t pad[BYTES_PER_ZDF_UNIT] = {0};
if ( fwrite( (void *) pad, sizeof(uint8_t), npad, zdf -> fp ) != npad ) {
return(0);
}
}
return( RNDUP(len) );
}
/**
* Writes a vector of the specified datatype to file
* @param zdf ZDF file descriptor
* @param data Pointer to data
* @param data_type Data type descriptor
* @param len Number of elements in vector
* @return Returns number of bytes written on success, 0 on error
*/
size_t zdf_vector_write( t_zdf_file* zdf, const void * data, enum zdf_data_type data_type, size_t len ){
switch ( data_type ) {
case zdf_int8:
case zdf_uint8:
return ( zdf_vector8_write( zdf, data, len ));
case zdf_int16:
case zdf_uint16:
return ( zdf_vector16_write( zdf, data, len ) );
case zdf_int32:
case zdf_uint32:
case zdf_float32:
return ( zdf_vector32_write( zdf, data, len ) );
case zdf_int64:
case zdf_uint64:
case zdf_float64:
return ( zdf_vector64_write( zdf, data, len ) );
default:
fprintf(stderr,"(*error*) zdf_vector_write: Unsupported datatype.\n");
}
return(0);
}
/* -----------------------------------------------------------------------------------------------
zdf_string
-------------------------------------------------------------------------------------------------- */
/**
* Write a string to file
* @param zdf ZDF file descriptor
* @param str C string (char *) to write
* @return Returns number of bytes written on success, 0 on error
*/
size_t zdf_string_write( t_zdf_file* zdf, const char * str ){
uint32_t len;
int count;
len = ( str ) ? strlen( str ) : 0;
if ( ! zdf_uint32_write( zdf, len ) ) return(0);
if ( len > 0 ) {
if ( !(count = zdf_vector8_write( zdf, (void *) str, len ) ) )
return(0);
} else {
count = 0;
}
return( sizeof(uint32_t) + count );
}
/**
* Read a string from file
* @param zdf ZDF file descriptor
* @param str C string (char *) to read. The routine will allocate memory using
* malloc() for storing the string data.
* @return Returns number of bytes read on success, 0 on error
*/
size_t zdf_string_read( t_zdf_file* zdf, char * * str ){
uint32_t len;
if ( ! zdf_uint32_read( zdf, &len) ) return(0);
uint32_t flen = RNDUP(len);
char * buffer = (char *) malloc( (flen+1) * sizeof(uint8_t) );
if ( len > 0 ) {
if ( fread( (void *) buffer, sizeof(uint8_t), flen, zdf -> fp ) != flen ) return(0);
}
buffer[len] = 0;
*str = buffer;
return( sizeof(uint32_t) + flen );
}
/**
* Calculate size of string record
* @param s C string (char *) to write
* @return The number of bytes required for writing the string data
*/
size_t size_zdf_string( const char *s )
{
unsigned len = ( s ) ? strlen( s ) : 0;
return size_zdf_uint32 + ((len > 0) ? RNDUP(len) : 0);
}
/* -----------------------------------------------------------------------------------------------
zdf records
-------------------------------------------------------------------------------------------------- */
/**
* @brief ZDF Record
*
*/
typedef struct ZDF_Record{
uint32_t id_version; ///< id & version
char* name; ///< Name
uint64_t length; ///< Record length
} t_zdf_record;
/**
* Adds ZDF record header to file
* @param zdf ZDF file descriptor
* @param rec ZDF record
* @return Returns number of bytes written on success, 0 on error
*/
size_t zdf_record_write( t_zdf_file* zdf, const t_zdf_record* rec ){
if ( ! zdf_uint32_write( zdf, rec -> id_version ) ) return(0);
size_t len; if ( ! (len=zdf_string_write( zdf, rec -> name )) ) return(0);
if ( ! zdf_uint64_write( zdf, rec -> length ) ) return(0);
return( sizeof(uint32_t) + len + sizeof(uint64_t) );
}
/**
* Reads ZDF record header from file
* @param zdf ZDF file descriptor
* @param rec ZDF record
* @return Returns number of bytes read on success, 0 on error
*/
size_t zdf_record_read( t_zdf_file* zdf, t_zdf_record* rec ){
if ( ! zdf_uint32_read( zdf, & rec -> id_version ) ) return(0);
size_t len; if ( ! (len=zdf_string_read( zdf, & rec -> name )) ) return(0);
if ( ! zdf_uint64_read( zdf, & rec -> length ) ) return(0);
return( sizeof(uint32_t) + len + sizeof(uint64_t) );
}
/**
* Skips to the end of ZDF record. File pointer is assumed to be after the record header.
* @param zdf ZDF file descriptor
* @param rec ZDF record
* @return Returns 1 on success, 0 on error
*/
int zdf_record_skip( t_zdf_file* zdf, const t_zdf_record* rec ){
off_t offset = rec -> length;
return ( ! fseeko( zdf -> fp, offset, SEEK_CUR ) ? 1 : 0 );
}
/* -----------------------------------------------------------------------------------------------
zdf basic data tags
-------------------------------------------------------------------------------------------------- */
/**
* Adds string element to ZDF file
* @param zdf ZDF File handle
* @param name Element name
* @param str String value
* @return Returns number of bytes written on success, 0 on error
*/
size_t zdf_add_string( t_zdf_file* zdf, const char* name, const char* str ){
t_zdf_record rec = {
.id_version = ZDF_STRING_ID,
.name = (char *)name,
.length = size_zdf_string( str )
};
size_t ok, len;
len = ( ok = zdf_record_write( zdf, &rec ) );
if ( !ok ) return(0);
len += ( ok = zdf_string_write( zdf, str) );
if ( !ok ) return(0);
return(len);
}
/**
* Adds int32 element to ZDF file
* @param zdf ZDF File handle
* @param name Element name
* @param value int32 value
* @return Returns number of bytes written on success, 0 on error
*/
size_t zdf_add_int32( t_zdf_file* zdf, const char* name, const int32_t value ){
t_zdf_record rec = {
.id_version = ZDF_INT32_ID,
.name = (char *)name,
.length = size_zdf_int32
};
size_t ok, len;
len = ( ok = zdf_record_write(zdf, &rec) );
if (!ok) return(0);
len += ( ok = zdf_int32_write(zdf, value) );
if (!ok) return(0);
return( len );
}
/**
* Adds float64 element to ZDF file
* @param zdf ZDF file handle
* @param name Element name
* @param value float64 value
* @return Returns number of bytes written on success, 0 on error
*/
size_t zdf_add_double( t_zdf_file* zdf, const char* name, const double value )
{
t_zdf_record rec = {
.id_version = ZDF_DOUBLE_ID,
.name = (char *)name,
.length = size_zdf_double
};
size_t ok, len;
len = (ok = zdf_record_write(zdf, &rec) ); if ( !ok ) return(0);
len += ( ok = zdf_double_write(zdf, value) ); if ( !ok ) return(0);
return(len);
}
/* -----------------------------------------------------------------------------------------------
zdf compound metadata tags
-------------------------------------------------------------------------------------------------- */
/**
* Adds iteration metadata group to ZDF file
* @param zdf ZDF file handle
* @param iter Iteration info
* @return Number of bytes written on success, 0 on error
*/
size_t zdf_add_iteration( t_zdf_file* zdf, const t_zdf_iteration* iter ){
t_zdf_record rec = {
.id_version = ZDF_ITERATION_ID,
.name = (char *) iter -> name,
.length = size_zdf_uint32 +
size_zdf_double +
size_zdf_string( iter -> time_units )
};
size_t ok, len;
len = ( ok = zdf_record_write( zdf, &rec) ); if ( !ok ) return(0);
len += ( ok = zdf_int32_write( zdf, iter ->n ) ); if ( !ok ) return(0);
len += ( ok = zdf_double_write( zdf, iter->t ) ); if ( !ok ) return(0);
len += ( ok = zdf_string_write( zdf, iter->time_units ) ); if ( !ok ) return(0);
return(len);
}
/**
* Returns size of grid information metadata element
* @param grid Grid info
* @return Metadata group size in bytes
*/
size_t size_zdf_grid_info(const t_zdf_grid_info* grid) {
size_t size;
size = size_zdf_uint32 + grid -> ndims * size_zdf_uint64 +
size_zdf_string(grid->label) + size_zdf_string(grid->units);
// Includes axis information