forked from jkriege2/TinyMAT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinymatwriter.cpp
2095 lines (1737 loc) · 70.2 KB
/
tinymatwriter.cpp
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
/*
Copyright (c) 2008-2015 Jan W. Krieger (<[email protected]>, <[email protected]>), German Cancer Research Center (DKFZ) & IWR, University of Heidelberg
last modification: $LastChangedDate: 2017-11-03 14:13:22 +0100 (Fr, 03 Nov 2017) $ (revision $Rev: 105888 $)
This software is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License (LGPL) as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <math.h>
#include <float.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <list>
#include <algorithm>
//#include <iostream>
#include "tinymatwriter.h"
/** \brief if defined, files are beeing created in a memory buffer and are only written to disk at the end.
* if undefined, the files are written directly to disk, including move operations on disk, which can be a factor 2-3 slower. */
#define TINYMAT_WRITE_VIA_MEMORY
#ifndef __WINDOWS__
# if defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32)
# define __WINDOWS__
# endif
#endif
#ifndef __LINUX__
# if defined(linux)
# define __LINUX__
# endif
#endif
#ifdef TINYMAT_USES_QVARIANT
//# include <QDebug>
# include <QPoint>
# include <QSize>
#endif
#define TINYMAT_ORDER_UNKNOWN 0
#define TINYMAT_ORDER_BIGENDIAN 1
#define TINYMAT_ORDER_LITTLEENDIAN 2
#if defined(__GNUC__) || defined(__GNUG__)
#define TINYMAT_inlineattrib inline
// use this for no-inline:
//#define TINYMAT_inlineattrib __attribute__((noinline))
#else
#define TINYMAT_inlineattrib inline
#endif
#define TINYMAT_mxCHAR_CLASS_CLASS_arrayflags 0x00000004
#define TINYMAT_mxDOUBLE_CLASS_arrayflags 0x00000006
#define TINYMAT_mxSINGLE_CLASS_arrayflags 0x00000007
#define TINYMAT_mxINT8_CLASS_arrayflags 0x00000008
#define TINYMAT_mxUINT8_CLASS_arrayflags 0x00000009
#define TINYMAT_mxINT16_CLASS_arrayflags 0x0000000A
#define TINYMAT_mxUINT16_CLASS_arrayflags 0x0000000B
#define TINYMAT_mxINT32_CLASS_arrayflags 0x0000000C
#define TINYMAT_mxUINT32_CLASS_arrayflags 0x0000000D
#define TINYMAT_mxINT64_CLASS_arrayflags 0x0000000E
#define TINYMAT_mxUINT64_CLASS_arrayflags 0x0000000F
#define TINYMAT_mxUINT8_LOGICAL_CLASS_arrayflags (TINYMAT_mxUINT8_CLASS_arrayflags+(0x0002<<8))
#define TINYMAT_miINT8 1
#define TINYMAT_miUINT8 2
#define TINYMAT_miINT16 3
#define TINYMAT_miUINT16 4
#define TINYMAT_miINT32 5
#define TINYMAT_miUINT32 6
#define TINYMAT_miSINGLE 7
#define TINYMAT_miDOUBLE 9
#define TINYMAT_miINT64 12
#define TINYMAT_miUINT64 13
#define TINYMAT_miMATRIX 14
#define TINYMAT_miCOMPRESSED 15
#define TINYMAT_miUTF8 16
#define TINYMAT_miUTF16 17
#define TINYMAT_miUTF32 18
struct TinyMATWriterStruct {
inline TinyMATWriterStruct() :
sizepos(-1),
data_start(-1)
{
}
/** \brief position of the size-data field */
long sizepos;
long data_start;
std::vector<std::string> itemnames;
};
struct TinyMATWriterCell {
inline TinyMATWriterCell() :
sizepos(-1),
data_start(-1)
{
}
/** \brief position of the size-data field */
long sizepos;
long data_start;
};
enum class TinyMATWriterStackItem {
Cell,
Struct
};
/*! \brief this struct represents a mat file
\ingroup TinyMATwriter
\internal
*/
struct TinyMATWriterFile {
TinyMATWriterFile() :
file(NULL),
filedata(NULL),
filedata_size(0),
filedata_current(0),
filedata_count(0),
byteorder(TINYMAT_ORDER_UNKNOWN)
{
}
/** \brief the libc file handle */
FILE* file;
/** \brief Zwischenspeicher-Array beim Schreiben von Matlab-Daten */
uint8_t* filedata;
/** \brief Größe von filedata */
size_t filedata_size;
/** \brief aktuelle Position in filedata */
size_t filedata_current;
/** \brief tatsächliche Datenbytes in filedata */
size_t filedata_count;
/** \brief specifies the byte order of the system (and the written file!) */
uint8_t byteorder;
std::vector<TinyMATWriterStruct> structures;
std::vector<TinyMATWriterCell> cells;
std::vector<TinyMATWriterStackItem> stack;
inline void startStruct() {
structures.push_back(TinyMATWriterStruct());
stack.push_back(TinyMATWriterStackItem::Struct);
}
void endStruct() {
structures.pop_back();
stack.pop_back();
}
inline TinyMATWriterStruct& lastStruct() {
return structures[structures.size()-1];
}
inline void addStructItemName(const std::string& name) {
if (structures.size()>0 && stack.size()>0 && stack[stack.size()-1]==TinyMATWriterStackItem::Struct) {
lastStruct().itemnames.push_back(name);
}
}
inline void startCell() {
cells.push_back(TinyMATWriterCell());
stack.push_back(TinyMATWriterStackItem::Cell);
}
void endCell() {
cells.pop_back();
stack.pop_back();
}
inline TinyMATWriterCell& lastCell() {
return cells[cells.size()-1];
}
};
int TinyMATWriter_fOK(const TinyMATWriterFile* mat) {
return (mat && (mat->file!=NULL));
}
/*! \brief determines the byte order of the system
\ingroup tinymatwriter
\internal
\return TINYMAT_ORDER_BIGENDIAN or TINYMAT_ORDER_LITTLEENDIAN, or TINYMAT_ORDER_UNKNOWN if the byte order cannot be determined
*/
TINYMAT_inlineattrib static int TinyMAT_get_byteorder()
{
union {
long l;
char c[4];
} test;
test.l = 1;
if( test.c[3] && !test.c[2] && !test.c[1] && !test.c[0] )
return TINYMAT_ORDER_BIGENDIAN;
if( !test.c[3] && !test.c[2] && !test.c[1] && test.c[0] )
return TINYMAT_ORDER_LITTLEENDIAN;
return TINYMAT_ORDER_UNKNOWN;
}
TINYMAT_inlineattrib static int TinyMAT_fclose(TinyMATWriterFile* file) {
//std::cout<<"TinyMAT_fclose()\n";
//std::cout.flush();
if (!file) return 0;
if (!file->file) {
delete file;
return 0;
}
#ifdef TINYMAT_WRITE_VIA_MEMORY
if (file->filedata_count>0 && file->filedata) {
fseek(file->file, 0, SEEK_SET);
fwrite(file->filedata, 1, file->filedata_count, file->file);
file->filedata_size = 0;
file->filedata_current = 0;
file->filedata_count = 0;
free(file->filedata);
}
#endif
int ret= fclose(file->file);
delete file;
return ret;
}
TINYMAT_inlineattrib static TinyMATWriterFile* TinyMAT_fopen(const char* filename, size_t bufSize=1024*100) {
//std::cout<<"TinyMAT_fopen()\n";
//std::cout.flush();
TinyMATWriterFile* mat=new TinyMATWriterFile;
if (fopen_s(&(mat->file), filename, "wb+") == 0) {
if (mat->file) {
if (bufSize > 0) {
setvbuf(mat->file, NULL, _IOFBF, bufSize);
}
else {
setvbuf(mat->file, NULL, _IOFBF, BUFSIZ);
}
}
mat->byteorder = (uint8_t)TinyMAT_get_byteorder();
}
#ifdef TINYMAT_WRITE_VIA_MEMORY
mat->filedata_size = std::max<size_t>(bufSize, BUFSIZ);
mat->filedata_current = 0;
mat->filedata_count = 0;
mat->filedata = (uint8_t*)malloc(mat->filedata_size);
if (!mat->filedata) {
delete mat;
return NULL;
}
#endif
return mat;
}
TINYMAT_inlineattrib static long TinyMAT_ftell(TinyMATWriterFile* file) {
//std::cout<<"TinyMAT_ftell()\n";
//std::cout.flush();
if (!file || !file->file) return 0;
#ifdef TINYMAT_WRITE_VIA_MEMORY
return static_cast<long>(file->filedata_current);
#else
return ftell(file->file);
#endif
}
TINYMAT_inlineattrib static int TinyMAT_fseek(TinyMATWriterFile* file, long offset) {
//std::cout<<"TinyMAT_fseek()\n";
//std::cout.flush();
if (!file || !file->file) return 0;
#ifdef TINYMAT_WRITE_VIA_MEMORY
long start = 0;
int res = 0;
if (start + offset < 0) {
throw std::runtime_error("seek before start of file");
res=-1;
} else if (static_cast<size_t>(start + offset) > file->filedata_count) {
throw std::runtime_error("seek after end of file");
res=-1;
} else {
file->filedata_current = start + offset;
res=0;
}
return res;
#else
return fseek(file->file, offset, SEEK_SET);
#endif
}
/** \brief grows the internal memory array for file writing by \a size_increment bytes */
TINYMAT_inlineattrib static void TinyMAT_growMem(uint32_t size_increment, TinyMATWriterFile* file) {
#ifdef TINYMAT_WRITE_VIA_MEMORY
if (file->filedata_current + size_increment + 100 >= file->filedata_size) {
size_t newsize = file->filedata_size;
while (file->filedata_current + size_increment + 100 >= newsize) {
if (newsize < 100 * 1024 * 1024) newsize = newsize * 2;
else if (newsize < 1000 * 1024 * 1024) newsize = newsize * 3 / 2;
else newsize = newsize * 6 / 5;
}
file->filedata = (uint8_t*)realloc(file->filedata, newsize);
file->filedata_size = newsize;
}
#endif
}
TINYMAT_inlineattrib static int TinyMAT_fwrite(const void* data, uint32_t size, uint32_t count, TinyMATWriterFile* file)
{
//std::cout<<"TinyMAT_fwrite()\n";
if (!file || !file->file || !data || size*count<=0) return 0;
int res = 0;
#ifdef TINYMAT_WRITE_VIA_MEMORY
if (file->filedata_current + size*count + 100 >= file->filedata_size) {
TinyMAT_growMem(size*count, file);
}
memcpy_s(&(file->filedata[file->filedata_current]), file->filedata_size- file->filedata_current, data, size*count);
file->filedata_current = file->filedata_current + size*count;
file->filedata_count = std::max(file->filedata_count, file->filedata_current);
res=size*count;
#else
res = (int)fwrite(data, 1, size*count, file->file);
#endif
return res;
}
template<typename T>
TINYMAT_inlineattrib static int TinyMAT_fwritesmall(T data, TinyMATWriterFile* file)
{
if (!file || !file->file) return 0;
int res = 0;
#ifdef TINYMAT_WRITE_VIA_MEMORY
if (file->filedata_current + sizeof(T) + 100 >= file->filedata_size) {
TinyMAT_growMem(sizeof(T), file);
}
T* datap = reinterpret_cast<T*>(&(file->filedata[file->filedata_current]));
*datap = data;
file->filedata_current = file->filedata_current + sizeof(T);
file->filedata_count = std::max(file->filedata_count, file->filedata_current);
res=sizeof(T);
#else
res = (int)fwrite(&data, 1, sizeof(T), file->file);
#endif
return res;
}
TINYMAT_inlineattrib static int TinyMAT_fread(void* data, uint32_t size, uint32_t count, TinyMATWriterFile* file)
{
//std::cout<<"TinyMAT_fwrite()\n";
if (!file || !file->file || !data || size*count<=0) return 0;
int res = 0;
#ifdef TINYMAT_WRITE_VIA_MEMORY
int cnt = std::min<int>(size*count, static_cast<int>(file->filedata_size - file->filedata_current));
if (cnt != size*count) {
throw std::runtime_error("read after end of file");
}
memcpy_s(data, size*count, &(file->filedata[file->filedata_current]), cnt);
file->filedata_current = file->filedata_current + cnt;
res = cnt;
#else
res = (int)fread(data, 1, size*count, file->file);
#endif
return res;
}
TINYMAT_inlineattrib static void TinyMAT_write8(TinyMATWriterFile* filen, uint8_t data) {
TinyMAT_fwritesmall(data, filen);
}
TINYMAT_inlineattrib static void TinyMAT_write8(TinyMATWriterFile* filen, int8_t data) {
TinyMAT_fwritesmall(data, filen);
}
TINYMAT_inlineattrib static void TinyMAT_write16(TinyMATWriterFile* filen, uint16_t data) {
TinyMAT_fwritesmall(data, filen);
}
TINYMAT_inlineattrib static void TinyMAT_write16(TinyMATWriterFile* filen, int16_t data) {
TinyMAT_fwritesmall(data, filen);
}
TINYMAT_inlineattrib static void TinyMAT_write32(TinyMATWriterFile* filen, uint32_t data) {
TinyMAT_fwritesmall(data, filen);
}
TINYMAT_inlineattrib static void TinyMAT_write32(TinyMATWriterFile* filen, int32_t data) {
TinyMAT_fwritesmall(data, filen);
}
TINYMAT_inlineattrib static void TinyMAT_write64(TinyMATWriterFile* filen, uint64_t data) {
TinyMAT_fwritesmall(data, filen);
}
TINYMAT_inlineattrib static void TinyMAT_write64(TinyMATWriterFile* filen, int64_t data) {
TinyMAT_fwritesmall(data, filen);
}
TINYMAT_inlineattrib static void TinyMAT_write64d(TinyMATWriterFile* filen, double data) {
TinyMAT_fwritesmall(data, filen);
}
TINYMAT_inlineattrib static void TinyMAT_write32f(TinyMATWriterFile* filen, float data) {
TinyMAT_fwritesmall(data, filen);
}
TINYMAT_inlineattrib static void TinyMAT_writeDatElement_u64(TinyMATWriterFile* mat, uint64_t data) {
TinyMAT_write32(mat, (uint32_t)TINYMAT_miUINT64);
TinyMAT_write32(mat, (uint32_t)8);
TinyMAT_write64(mat, data);
}
TINYMAT_inlineattrib static void TinyMAT_writeDatElement_i64(TinyMATWriterFile* mat, int64_t data) {
TinyMAT_write32(mat, (uint32_t)TINYMAT_miINT64);
TinyMAT_write32(mat, (uint32_t)8);
TinyMAT_write64(mat, data);
}
TINYMAT_inlineattrib static void TinyMAT_writeDatElement_u32(TinyMATWriterFile* mat, uint32_t data) {
TinyMAT_write32(mat, (uint32_t)TINYMAT_miUINT32);
TinyMAT_write32(mat, (uint32_t)4);
TinyMAT_write32(mat, data);
TinyMAT_write32(mat, (uint32_t)0); // write padding
}
TINYMAT_inlineattrib static void TinyMAT_writeDatElement_i32(TinyMATWriterFile* mat, int32_t data) {
TinyMAT_write32(mat, (uint32_t)TINYMAT_miINT32);
TinyMAT_write32(mat, (uint32_t)4);
TinyMAT_write32(mat, data);
TinyMAT_write32(mat, (uint32_t)0); // write padding
}
TINYMAT_inlineattrib static void TinyMAT_writeDatElementS_i32(TinyMATWriterFile* mat, int32_t data) {
TinyMAT_write16(mat, (uint16_t)TINYMAT_miINT32);
TinyMAT_write16(mat, (uint16_t)4);
TinyMAT_write32(mat, data);
}
TINYMAT_inlineattrib static void TinyMAT_writeDatElement_u16(TinyMATWriterFile* mat, uint16_t data) {
TinyMAT_write32(mat, (uint32_t)TINYMAT_miUINT16);
TinyMAT_write32(mat, (uint32_t)2);
TinyMAT_write16(mat, data);
TinyMAT_write16(mat, (uint16_t)0); // write padding
TinyMAT_write32(mat, (uint32_t)0);
}
TINYMAT_inlineattrib static void TinyMAT_writeDatElement_i16(TinyMATWriterFile* mat, int16_t data) {
TinyMAT_write32(mat, (uint32_t)TINYMAT_miINT16);
TinyMAT_write32(mat, (uint32_t)2);
TinyMAT_write16(mat, data);
TinyMAT_write16(mat, (uint16_t)0); // write padding
TinyMAT_write32(mat, (uint32_t)0);
}
TINYMAT_inlineattrib static void TinyMAT_writeDatElement_dbl(TinyMATWriterFile* mat, double data) {
TinyMAT_write32(mat, (uint32_t)TINYMAT_miDOUBLE);
TinyMAT_write32(mat, (uint32_t)8);
TinyMAT_write64d(mat, data);
// no padding required
}
TINYMAT_inlineattrib static void TinyMAT_writeDatElement_dbla(TinyMATWriterFile* mat, const double* data, uint32_t items) {
TinyMAT_write32(mat, (uint32_t)TINYMAT_miDOUBLE);
if (!data) items=0;
TinyMAT_write32(mat, items*8);
if (items>0 && data){
TinyMAT_fwrite(data, 8, items, mat);
}
// no padding required
}
TINYMAT_inlineattrib static void TinyMAT_writeDatElement_flta(TinyMATWriterFile* mat, const float* data, uint32_t items) {
TinyMAT_write32(mat, (uint32_t)TINYMAT_miSINGLE);
if (!data) items=0;
TinyMAT_write32(mat, items*4);
if (items>0 && data){
TinyMAT_fwrite(data, 4, items, mat);
// write padding
if (items%2==1) TinyMAT_write32(mat, (int32_t)0);
}
}
TINYMAT_inlineattrib static void TinyMAT_writeDatElement_u32a(TinyMATWriterFile* mat, const uint32_t* data, size_t items) {
TinyMAT_write32(mat, (uint32_t)TINYMAT_miUINT32);
TinyMAT_write32(mat, (uint32_t)(items*4));
if (items>0) {
TinyMAT_fwrite(data, 4, (uint32_t)items, mat);
// write padding
if (items%2==1) TinyMAT_write32(mat, (uint32_t)0);
}
}
TINYMAT_inlineattrib static void TinyMAT_writeDatElement_i32a(TinyMATWriterFile* mat, const int32_t* data, size_t items) {
TinyMAT_write32(mat, (uint32_t)TINYMAT_miINT32);
TinyMAT_write32(mat, (uint32_t)(items*4));
if (items>0) {
TinyMAT_fwrite(data, 4, (uint32_t)items, mat);
// write padding
if (items%2==1) TinyMAT_write32(mat, (int32_t)0);
}
}
TINYMAT_inlineattrib static void TinyMAT_writeDatElement_u16a(TinyMATWriterFile* mat, const uint16_t* data, size_t items) {
TinyMAT_write32(mat, (uint32_t)TINYMAT_miUINT16);
TinyMAT_write32(mat, (uint32_t)(items*2));
if (items>0) {
TinyMAT_fwrite(data, 4, (uint32_t)items, mat);
// write padding
if (items%4==1) {
TinyMAT_write32(mat, (uint32_t)0);
TinyMAT_write16(mat, (uint16_t)0);
} else if (items%4==2) {
TinyMAT_write32(mat, (uint32_t)0);
} else if (items%4==3) {
TinyMAT_write16(mat, (uint16_t)0);
}
}
}
TINYMAT_inlineattrib static void TinyMAT_writeDatElement_i16a(TinyMATWriterFile* mat, const int16_t* data, size_t items) {
TinyMAT_write32(mat, (uint32_t)TINYMAT_miINT16);
TinyMAT_write32(mat, (uint32_t)(items*2));
if (items>0) {
TinyMAT_fwrite(data, 4, (uint32_t)items, mat);
// write padding
if (items%4==1) {
TinyMAT_write32(mat, (uint32_t)0);
TinyMAT_write16(mat, (uint16_t)0);
} else if (items%4==2) {
TinyMAT_write32(mat, (uint32_t)0);
} else if (items%4==3) {
TinyMAT_write16(mat, (uint16_t)0);
}
}
}
TINYMAT_inlineattrib static void TinyMAT_writeDatElement_u64a(TinyMATWriterFile* mat, const uint64_t* data, size_t items) {
TinyMAT_write32(mat, (uint32_t)TINYMAT_miUINT64);
TinyMAT_write32(mat, (uint32_t)(items*8));
if (items>0) {
TinyMAT_fwrite(data, 8, (uint32_t)items, mat);
// no padding required
}
}
TINYMAT_inlineattrib static void TinyMAT_writeDatElement_i64a(TinyMATWriterFile* mat, const int64_t* data, size_t items) {
TinyMAT_write32(mat, (uint32_t)TINYMAT_miINT64);
TinyMAT_write32(mat, (uint32_t)(items*8));
if (items>0) {
TinyMAT_fwrite(data, 8, (uint32_t)items, mat);
// no padding required
}
}
TINYMAT_inlineattrib static void TinyMAT_writeDatElement_i8a(TinyMATWriterFile* mat, const int8_t* data, uint32_t slen) {
size_t pad=(slen)%8;
uint32_t cla=TINYMAT_miINT8;
TinyMAT_write32(mat, cla);
TinyMAT_write32(mat, slen);
if (slen>0 && data) {
TinyMAT_fwrite(data, 1, slen, mat);
if (pad>0) {
static const uint8_t paddata[8] = { 0,0,0,0,0,0,0,0 };
TinyMAT_fwrite(paddata, static_cast<uint32_t>(8 - pad), 1, mat);
}
}
}
TINYMAT_inlineattrib static void TinyMAT_writeDatElement_u8a(TinyMATWriterFile* mat, const uint8_t* data, uint32_t slen) {
size_t pad=(slen)%8;
uint32_t cla=TINYMAT_miUINT8;
TinyMAT_write32(mat, cla);
TinyMAT_write32(mat, slen);
if (slen>0 && data) {
TinyMAT_fwrite(data, 1, slen, mat);
if (pad>0) {
static const uint8_t paddata[8] = { 0,0,0,0,0,0,0,0 };
TinyMAT_fwrite(paddata, static_cast<uint32_t>(8 - pad), 1, mat);
}
}
}
TINYMAT_inlineattrib static void TinyMAT_writeDatElement_stringas8bit(TinyMATWriterFile* mat, const char* data, uint32_t slen) {
TinyMAT_writeDatElement_i8a(mat, (const int8_t*)data, slen);
}
TINYMAT_inlineattrib static void TinyMAT_writeDatElement_string(TinyMATWriterFile* mat, const char* data, uint32_t slen) {
size_t pad=(2*slen)%8;
int16_t* tmp=NULL;
if (slen>0 && data) {
tmp=(int16_t*)malloc(slen*2);
for (uint32_t i=0; i<slen; i++) {
tmp[i]=data[i];
}
}
uint32_t cla=TINYMAT_miUINT16;
TinyMAT_write32(mat, cla);
TinyMAT_write32(mat, slen*2);
if (slen>0 && tmp) {
TinyMAT_fwrite(tmp, 2, slen, mat);
// write padding
if (pad>0) {
static const uint8_t paddata[8] = { 0,0,0,0,0,0,0,0 };
TinyMAT_fwrite(paddata, static_cast<uint32_t>(8 - pad), 1, mat);
}
} else {
}
if (tmp) free(tmp);
}
TINYMAT_inlineattrib static void TinyMAT_writeDatElement_stringas8bit(TinyMATWriterFile* mat, const char* data) {
TinyMAT_writeDatElement_stringas8bit(mat, data, (uint32_t)strlen(data));
}
TINYMAT_inlineattrib static void TinyMAT_writeDatElement_string(TinyMATWriterFile* mat, const char* data) {
TinyMAT_writeDatElement_string(mat, data, (uint32_t)strlen(data));
}
TINYMAT_inlineattrib static size_t TinyMAT_DatElement_realstringlen8bit(const char* data) {
if (!data) return 0;
size_t slen=strlen(data);
if (slen==0) return 0;
size_t pad=slen%8;
if (pad>0) {
slen=slen+(8-pad);
}
return slen;
}
TINYMAT_inlineattrib static size_t TinyMAT_DatElement_realstringlen16bit(const char* data) {
if (!data) return 0;
size_t slen=strlen(data)*2;
if (slen==0) return 0;
size_t pad=slen%8;
if (pad>0) {
slen=slen+(8-pad);
}
return slen;
}
void TinyMATWriter_writeMatrixND_colmajor(TinyMATWriterFile *mat, const char *name, const double *data_real, const int32_t *sizes, uint32_t ndims)
{
if (!data_real || !sizes || ndims<=0) {
TinyMATWriter_writeEmptyMatrix(mat, name);
} else {
mat->addStructItemName(name);
uint32_t nentries=0;
for (uint32_t i=0; i<ndims; i++) {
if (i==0) {
nentries=sizes[0];
} else {
nentries=nentries*sizes[i];
}
}
uint32_t size_bytes=0;
uint32_t arrayflags[2]={TINYMAT_mxDOUBLE_CLASS_arrayflags, 0};
// write tag header
TinyMAT_write32(mat, (uint32_t)TINYMAT_miMATRIX);
long sizepos=TinyMAT_ftell(mat);
TinyMAT_write32(mat, size_bytes);
// write arrayflags
TinyMAT_writeDatElement_u32a(mat, arrayflags, 2);
// write field dimensions
TinyMAT_writeDatElement_i32a(mat, sizes, ndims);
// write field name
TinyMAT_writeDatElement_stringas8bit(mat, name);
// write data type
TinyMAT_writeDatElement_dbla(mat, data_real, nentries);
long endpos;
endpos=TinyMAT_ftell(mat);
TinyMAT_fseek(mat, sizepos);
size_bytes=endpos-sizepos-4;
TinyMAT_write32(mat, size_bytes);
TinyMAT_fseek(mat, endpos);
}
}
void TinyMATWriter_writeMatrixND_colmajor(TinyMATWriterFile *mat, const char *name, const float *data_real, const int32_t *sizes, uint32_t ndims)
{
if (!data_real || !sizes || ndims<=0) {
TinyMATWriter_writeEmptyMatrix(mat, name);
} else {
mat->addStructItemName(name);
uint32_t nentries=0;
for (uint32_t i=0; i<ndims; i++) {
if (i==0) {
nentries=sizes[0];
} else {
nentries=nentries*sizes[i];
}
}
uint32_t size_bytes=0;
uint32_t arrayflags[2]={TINYMAT_mxSINGLE_CLASS_arrayflags, 0};
// write tag header
TinyMAT_write32(mat, (uint32_t)TINYMAT_miMATRIX);
long sizepos=TinyMAT_ftell(mat);
TinyMAT_write32(mat, size_bytes);
// write arrayflags
TinyMAT_writeDatElement_u32a(mat, arrayflags, 2);
// write field dimensions
TinyMAT_writeDatElement_i32a(mat, sizes, ndims);
// write field name
TinyMAT_writeDatElement_stringas8bit(mat, name);
// write data type
TinyMAT_writeDatElement_flta(mat, data_real, nentries);
long endpos;
endpos=TinyMAT_ftell(mat);
TinyMAT_fseek(mat, sizepos);
size_bytes=endpos-sizepos-4;
TinyMAT_write32(mat, size_bytes);
TinyMAT_fseek(mat, endpos);
}
}
void TinyMATWriter_writeMatrixND_colmajor(TinyMATWriterFile *mat, const char *name, const uint64_t *data_real, const int32_t *sizes, uint32_t ndims)
{
if (!data_real || !sizes || ndims<=0) {
TinyMATWriter_writeEmptyMatrix(mat, name);
} else {
mat->addStructItemName(name);
uint32_t nentries=0;
for (uint32_t i=0; i<ndims; i++) {
if (i==0) {
nentries=sizes[0];
} else {
nentries=nentries*sizes[i];
}
}
uint32_t size_bytes=0;
uint32_t arrayflags[2]={TINYMAT_mxUINT64_CLASS_arrayflags, 0};
// write tag header
TinyMAT_write32(mat, (uint32_t)TINYMAT_miMATRIX);
long sizepos=TinyMAT_ftell(mat);
TinyMAT_write32(mat, size_bytes);
// write arrayflags
TinyMAT_writeDatElement_u32a(mat, arrayflags, 2);
// write field dimensions
TinyMAT_writeDatElement_i32a(mat, sizes, ndims);
// write field name
TinyMAT_writeDatElement_stringas8bit(mat, name);
// write data type
TinyMAT_writeDatElement_u64a(mat, data_real, nentries);
long endpos;
endpos=TinyMAT_ftell(mat);
TinyMAT_fseek(mat, sizepos);
size_bytes=endpos-sizepos-4;
TinyMAT_write32(mat, size_bytes);
TinyMAT_fseek(mat, endpos);
}
}
void TinyMATWriter_writeMatrixND_colmajor(TinyMATWriterFile *mat, const char *name, const int64_t *data_real, const int32_t *sizes, uint32_t ndims)
{
if (!data_real || !sizes || ndims<=0) {
TinyMATWriter_writeEmptyMatrix(mat, name);
} else {
mat->addStructItemName(name);
uint32_t nentries=0;
for (uint32_t i=0; i<ndims; i++) {
if (i==0) {
nentries=sizes[0];
} else {
nentries=nentries*sizes[i];
}
}
uint32_t size_bytes=0;
uint32_t arrayflags[2]={TINYMAT_mxINT64_CLASS_arrayflags, 0};
// write tag header
TinyMAT_write32(mat, (uint32_t)TINYMAT_miMATRIX);
long sizepos=TinyMAT_ftell(mat);
TinyMAT_write32(mat, size_bytes);
// write arrayflags
TinyMAT_writeDatElement_u32a(mat, arrayflags, 2);
// write field dimensions
TinyMAT_writeDatElement_i32a(mat, sizes, ndims);
// write field name
TinyMAT_writeDatElement_stringas8bit(mat, name);
// write data type
TinyMAT_writeDatElement_i64a(mat, data_real, nentries);
long endpos;
endpos=TinyMAT_ftell(mat);
TinyMAT_fseek(mat, sizepos);
size_bytes=endpos-sizepos-4;
TinyMAT_write32(mat, size_bytes);
TinyMAT_fseek(mat, endpos);
}
}
void TinyMATWriter_writeMatrixND_colmajor(TinyMATWriterFile *mat, const char *name, const uint32_t *data_real, const int32_t *sizes, uint32_t ndims)
{
if (!data_real || !sizes || ndims<=0) {
TinyMATWriter_writeEmptyMatrix(mat, name);
} else {
mat->addStructItemName(name);
uint32_t nentries=0;
for (uint32_t i=0; i<ndims; i++) {
if (i==0) {
nentries=sizes[0];
} else {
nentries=nentries*sizes[i];
}
}
uint32_t size_bytes=0;
uint32_t arrayflags[2]={TINYMAT_mxUINT32_CLASS_arrayflags, 0};
// write tag header
TinyMAT_write32(mat, (uint32_t)TINYMAT_miMATRIX);
long sizepos=TinyMAT_ftell(mat);
TinyMAT_write32(mat, size_bytes);
// write arrayflags
TinyMAT_writeDatElement_u32a(mat, arrayflags, 2);
// write field dimensions
TinyMAT_writeDatElement_i32a(mat, sizes, ndims);
// write field name
TinyMAT_writeDatElement_stringas8bit(mat, name);
// write data type
TinyMAT_writeDatElement_u32a(mat, data_real, nentries);
long endpos;
endpos=TinyMAT_ftell(mat);
TinyMAT_fseek(mat, sizepos);
size_bytes=endpos-sizepos-4;
TinyMAT_write32(mat, size_bytes);
TinyMAT_fseek(mat, endpos);
}
}
void TinyMATWriter_writeMatrixND_colmajor(TinyMATWriterFile *mat, const char *name, const int32_t *data_real, const int32_t *sizes, uint32_t ndims)
{
if (!data_real || !sizes || ndims<=0) {
TinyMATWriter_writeEmptyMatrix(mat, name);
} else {
mat->addStructItemName(name);
uint32_t nentries=0;
for (uint32_t i=0; i<ndims; i++) {
if (i==0) {
nentries=sizes[0];
} else {
nentries=nentries*sizes[i];
}
}
uint32_t size_bytes=0;
uint32_t arrayflags[2]={TINYMAT_mxINT32_CLASS_arrayflags, 0};
// write tag header
TinyMAT_write32(mat, (uint32_t)TINYMAT_miMATRIX);
long sizepos=TinyMAT_ftell(mat);
TinyMAT_write32(mat, size_bytes);
// write arrayflags
TinyMAT_writeDatElement_u32a(mat, arrayflags, 2);
// write field dimensions
TinyMAT_writeDatElement_i32a(mat, sizes, ndims);
// write field name
TinyMAT_writeDatElement_stringas8bit(mat, name);
// write data type
TinyMAT_writeDatElement_i32a(mat, data_real, nentries);
long endpos;
endpos=TinyMAT_ftell(mat);
TinyMAT_fseek(mat, sizepos);
size_bytes=endpos-sizepos-4;
TinyMAT_write32(mat, size_bytes);
TinyMAT_fseek(mat, endpos);
}
}
void TinyMATWriter_writeMatrixND_colmajor(TinyMATWriterFile *mat, const char *name, const uint16_t *data_real, const int32_t *sizes, uint32_t ndims)
{
if (!data_real || !sizes || ndims<=0) {
TinyMATWriter_writeEmptyMatrix(mat, name);
} else {
mat->addStructItemName(name);
uint32_t nentries=0;
for (uint32_t i=0; i<ndims; i++) {
if (i==0) {
nentries=sizes[0];
} else {
nentries=nentries*sizes[i];
}
}
uint32_t size_bytes=0;
uint32_t arrayflags[2]={TINYMAT_mxUINT16_CLASS_arrayflags, 0};
// write tag header
TinyMAT_write32(mat, (uint32_t)TINYMAT_miMATRIX);
long sizepos=TinyMAT_ftell(mat);
TinyMAT_write32(mat, size_bytes);
// write arrayflags
TinyMAT_writeDatElement_u32a(mat, arrayflags, 2);
// write field dimensions
TinyMAT_writeDatElement_i32a(mat, sizes, ndims);
// write field name
TinyMAT_writeDatElement_stringas8bit(mat, name);
// write data type
TinyMAT_writeDatElement_u16a(mat, data_real, nentries);
long endpos;
endpos=TinyMAT_ftell(mat);
TinyMAT_fseek(mat, sizepos);
size_bytes=endpos-sizepos-4;
TinyMAT_write32(mat, size_bytes);
TinyMAT_fseek(mat, endpos);
}
}
void TinyMATWriter_writeMatrixND_colmajor(TinyMATWriterFile *mat, const char *name, const int16_t *data_real, const int32_t *sizes, uint32_t ndims)
{
if (!data_real || !sizes || ndims<=0) {
TinyMATWriter_writeEmptyMatrix(mat, name);
} else {
mat->addStructItemName(name);
uint32_t nentries=0;
for (uint32_t i=0; i<ndims; i++) {
if (i==0) {
nentries=sizes[0];
} else {
nentries=nentries*sizes[i];
}
}
uint32_t size_bytes=0;
uint32_t arrayflags[2]={TINYMAT_mxINT16_CLASS_arrayflags, 0};
// write tag header
TinyMAT_write32(mat, (uint32_t)TINYMAT_miMATRIX);
long sizepos=TinyMAT_ftell(mat);
TinyMAT_write32(mat, size_bytes);
// write arrayflags
TinyMAT_writeDatElement_u32a(mat, arrayflags, 2);
// write field dimensions
TinyMAT_writeDatElement_i32a(mat, sizes, ndims);
// write field name