forked from modelica/ModelicaStandardLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModelicaIO.c
1351 lines (1224 loc) · 45.2 KB
/
ModelicaIO.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
/* ModelicaIO.c - Array I/O functions
Copyright (C) 2016-2020, Modelica Association and contributors
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Definition of interface to external functions for array I/O
in the Modelica Standard Library:
Modelica.Utilities.Streams.readMatrixSize
Modelica.Utilities.Streams.readRealMatrix
Modelica.Utilities.Streams.writeRealMatrix
Changelog:
Nov. 27, 2021: by Thomas Beutlich
Fixed error handling for invalid format specifier in
text file (ticket #3903)
Dec. 22, 2020: by Thomas Beutlich
Added reading of CSV files (ticket #1153)
July 08, 2020: by Thomas Beutlich
Improved error message if reading text file with zero bytes
(ticket #3603)
Jan. 15, 2018: by Thomas Beutlich, ESI ITI GmbH
Added support to ignore UTF-8 BOM if reading text file
(ticket #2404)
Apr. 12, 2017: by Thomas Beutlich, ESI ITI GmbH
Improved error messages if reading struct arrays from
MATLAB MAT-file fails (ticket #2105)
Mar. 08, 2017: by Thomas Beutlich, ESI ITI GmbH
Added ModelicaIO_readRealTable from ModelicaStandardTables
(ticket #2192)
Feb. 07, 2017: by Thomas Beutlich, ESI ITI GmbH
Added support for reading integer and single-precision
variable classes of MATLAB MAT-files (ticket #2106)
Jan. 31, 2017: by Thomas Beutlich, ESI ITI GmbH
Added diagnostic message for (supported) partial read of table
from a text file (ticket #2151)
Jan. 07, 2017: by Thomas Beutlich, ESI ITI GmbH
Replaced strtok by re-entrant string tokenize function
(ticket #1153)
Nov. 23, 2016: by Martin Sjoelund, SICS East Swedish ICT AB
Added NO_LOCALE define flag, in case the OS does
not have this (for example when using GCC compiler,
but not libc). Also added autoconf detection for
this flag, NO_PID, NO_TIME, and NO_FILE_SYSTEM
Nov. 21, 2016: by Thomas Beutlich, ESI ITI GmbH
Fixed error handling if a variable cannot be found in a
MATLAB MAT-file (ticket #2119)
Mar. 03, 2016: by Thomas Beutlich, ITI GmbH and Martin Otter, DLR
Implemented a first version (ticket #1856)
*/
#if defined(__gnu_linux__) && !defined(NO_FILE_SYSTEM)
#define _GNU_SOURCE 1
#endif
#include "ModelicaIO.h"
#include <string.h>
#include "ModelicaUtilities.h"
#ifdef NO_FILE_SYSTEM
MODELICA_NORETURN static void ModelicaNotExistError(const char* name) MODELICA_NORETURNATTR;
static void ModelicaNotExistError(const char* name) {
/* Print error message if a function is not implemented */
ModelicaFormatError("C-Function \"%s\" is called "
"but is not implemented for the actual environment "
"(e.g., because there is no file system available on the machine "
"as for dSPACE or xPC systems)\n", name);
}
void ModelicaIO_readMatrixSizes(_In_z_ const char* fileName,
_In_z_ const char* matrixName, _Out_ int* dim) {
ModelicaNotExistError("ModelicaIO_readMatrixSizes"); }
void ModelicaIO_readRealMatrix(_In_z_ const char* fileName,
_In_z_ const char* matrixName, _Out_ double* matrix, size_t m, size_t n,
int verbose) {
ModelicaNotExistError("ModelicaIO_readRealMatrix"); }
int ModelicaIO_writeRealMatrix(_In_z_ const char* fileName,
_In_z_ const char* matrixName, _In_ const double* matrix, size_t m, size_t n,
int append, _In_z_ const char* version) {
ModelicaNotExistError("ModelicaIO_writeRealMatrix"); return 0; }
double* ModelicaIO_readRealTable(_In_z_ const char* fileName,
_In_z_ const char* matrixName, _Out_ size_t* m, _Out_ size_t* n,
int verbose) {
ModelicaNotExistError("ModelicaIO_readRealTable"); return NULL; }
#else
#include <stdio.h>
#if !defined(NO_LOCALE)
#include <locale.h>
#endif
#include "ModelicaMatIO.h"
/* The standard way to detect POSIX is to check _POSIX_VERSION,
* which is defined in <unistd.h>
*/
#if defined(__unix__) || defined(__linux__) || defined(__APPLE_CC__)
#include <unistd.h>
#endif
#if !defined(_POSIX_) && defined(_POSIX_VERSION)
#define _POSIX_ 1
#endif
/* Use re-entrant string tokenize function if available */
#if defined(_POSIX_)
#elif defined(_MSC_VER) && _MSC_VER >= 1400
#define strtok_r(str, delim, saveptr) strtok_s((str), (delim), (saveptr))
#else
#define strtok_r(str, delim, saveptr) strtok((str), (delim))
#endif
#if !defined(LINE_BUFFER_LENGTH)
#define LINE_BUFFER_LENGTH (64)
#endif
#if !defined(MATLAB_NAME_LENGTH_MAX)
#define MATLAB_NAME_LENGTH_MAX (64)
#endif
typedef struct MatIO {
mat_t* mat; /* Pointer to MAT-file */
matvar_t* matvar; /* Pointer to MAT-file variable for data */
matvar_t* matvarRoot; /* Pointer to MAT-file variable for free */
} MatIO;
static double* readMatTable(_In_z_ const char* fileName, _In_z_ const char* tableName,
_Out_ size_t* m, _Out_ size_t* n) MODELICA_NONNULLATTR;
/* Read a table from a MATLAB MAT-file using MatIO functions
<- RETURN: Pointer to array (row-wise storage) of table values
*/
static void readMatIO(_In_z_ const char* fileName, _In_z_ const char* matrixName,
_Inout_ MatIO* matio);
/* Read a variable from a MATLAB MAT-file using MatIO functions */
static void readRealMatIO(_In_z_ const char* fileName, _In_z_ const char* matrixName,
_Inout_ MatIO* matio);
/* Read a real variable from a MATLAB MAT-file using MatIO functions */
static double* readCsvTable(_In_z_ const char* fileName, _In_z_ const char* tableName,
_Out_ size_t* m, _Out_ size_t* n, _In_z_ const char* delimiter,
int nHeaderLines) MODELICA_NONNULLATTR;
/* Read a table from a CSV file
<- RETURN: Pointer to array (row-wise storage) of table values
*/
static double* readTxtTable(_In_z_ const char* fileName, _In_z_ const char* tableName,
_Out_ size_t* m, _Out_ size_t* n) MODELICA_NONNULLATTR;
/* Read a table from a text file
<- RETURN: Pointer to array (row-wise storage) of table values
*/
static int readLine(_In_ char** buf, _In_ int* bufLen, _In_ FILE* fp) MODELICA_NONNULLATTR;
/* Read line (of unknown and arbitrary length) from a text file */
static int IsNumber(char* token);
/* Check, whether a token represents a floating-point number */
static void transpose(_Inout_ double* table, size_t nRow, size_t nCol) MODELICA_NONNULLATTR;
/* Cycle-based in-place array transposition */
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wtautological-compare"
#endif
void ModelicaIO_readMatrixSizes(_In_z_ const char* fileName,
_In_z_ const char* matrixName,
_Out_ int* dim) {
MatIO matio = {NULL, NULL, NULL};
dim[0] = 0;
dim[1] = 0;
readRealMatIO(fileName, matrixName, &matio);
if (NULL != matio.matvar) {
matvar_t* matvar = matio.matvar;
dim[0] = (int)matvar->dims[0];
dim[1] = (int)matvar->dims[1];
}
Mat_VarFree(matio.matvarRoot);
(void)Mat_Close(matio.mat);
}
void ModelicaIO_readRealMatrix(_In_z_ const char* fileName,
_In_z_ const char* matrixName,
_Inout_ double* matrix, size_t m, size_t n,
int verbose) {
MatIO matio = {NULL, NULL, NULL};
int readError = 0;
if (verbose == 1) {
/* Print info message, that matrix / file is loading */
ModelicaFormatMessage("... loading \"%s\" from \"%s\"\n",
matrixName, fileName);
}
readRealMatIO(fileName, matrixName, &matio);
if (NULL != matio.matvar) {
matvar_t* matvar = matio.matvar;
/* Check if number of rows matches */
if (m != matvar->dims[0]) {
Mat_VarFree(matio.matvarRoot);
(void)Mat_Close(matio.mat);
ModelicaFormatError(
"Cannot read %lu rows of array \"%s(%lu,%lu)\" "
"from file \"%s\"\n", (unsigned long)m, matrixName,
(unsigned long)matvar->dims[0], (unsigned long)matvar->dims[1],
fileName);
return;
}
/* Check if number of columns matches */
if (n != matvar->dims[1]) {
Mat_VarFree(matio.matvarRoot);
(void)Mat_Close(matio.mat);
ModelicaFormatError(
"Cannot read %lu columns of array \"%s(%lu,%lu)\" "
"from file \"%s\"\n", (unsigned long)n, matrixName,
(unsigned long)matvar->dims[0], (unsigned long)matvar->dims[1],
fileName);
return;
}
{
int start[2] = {0, 0};
int stride[2] = {1, 1};
int edge[2];
edge[0] = (int)matvar->dims[0];
edge[1] = (int)matvar->dims[1];
readError = Mat_VarReadData(matio.mat, matvar, matrix, start, stride, edge);
}
}
Mat_VarFree(matio.matvarRoot);
(void)Mat_Close(matio.mat);
if (readError == 0 && NULL != matrix) {
/* Array is stored column-wise -> need to transpose */
transpose(matrix, m, n);
}
else {
ModelicaFormatError(
"Error when reading numeric data of matrix \"%s(%lu,%lu)\" "
"from file \"%s\"\n", matrixName, (unsigned long)m,
(unsigned long)n, fileName);
}
}
int ModelicaIO_writeRealMatrix(_In_z_ const char* fileName,
_In_z_ const char* matrixName,
_In_ const double* matrix, size_t m, size_t n,
int append,
_In_z_ const char* version) {
int status;
mat_t* mat;
matvar_t* matvar;
size_t dims[2];
double* aT;
enum mat_ft matv;
enum matio_compression matc;
if ((0 != strcmp(version, "4")) && (0 != strcmp(version, "6")) && (0 != strcmp(version, "7")) && (0 != strcmp(version, "7.3"))) {
ModelicaFormatError("Invalid version %s for file \"%s\"\n", version, fileName);
return 0;
}
if (0 == strcmp(version, "4")) {
matv = MAT_FT_MAT4;
matc = MAT_COMPRESSION_NONE;
}
else if (0 == strcmp(version, "7.3")) {
matv = MAT_FT_MAT73;
matc = MAT_COMPRESSION_ZLIB;
}
else if (0 == strcmp(version, "7")) {
matv = MAT_FT_MAT5;
matc = MAT_COMPRESSION_ZLIB;
}
else {
matv = MAT_FT_MAT5;
matc = MAT_COMPRESSION_NONE;
}
if (append == 0) {
mat = Mat_CreateVer(fileName, NULL, matv);
if (NULL == mat) {
ModelicaFormatError("Not possible to newly create file \"%s\"\n(maybe version 7.3 not supported)\n", fileName);
return 0;
}
} else {
mat = Mat_Open(fileName, (int)MAT_ACC_RDWR | matv);
if (NULL == mat) {
ModelicaFormatError("Not possible to open file \"%s\"\n", fileName);
return 0;
}
}
/* MAT file array is stored column-wise -> need to transpose */
aT = (double*)malloc(m*n*sizeof(double));
if (NULL == aT) {
(void)Mat_Close(mat);
ModelicaError("Memory allocation error\n");
return 0;
}
memcpy(aT, matrix, m*n*sizeof(double));
transpose(aT, n, m);
if (append != 0) {
(void)Mat_VarDelete(mat, matrixName);
}
dims[0] = m;
dims[1] = n;
matvar = Mat_VarCreate(matrixName, MAT_C_DOUBLE, MAT_T_DOUBLE, 2, dims, aT, MAT_F_DONT_COPY_DATA);
status = Mat_VarWrite(mat, matvar, matc);
Mat_VarFree(matvar);
(void)Mat_Close(mat);
free(aT);
if (status != 0) {
ModelicaFormatError("Cannot write variable \"%s\" to \"%s\"\n", matrixName, fileName);
return 0;
}
return 1;
}
double* ModelicaIO_readRealTable(_In_z_ const char* fileName,
_In_z_ const char* tableName,
_Out_ size_t* m, _Out_ size_t* n,
int verbose) {
return ModelicaIO_readRealTable2(fileName, tableName, m, n, verbose, ",", 0);
}
double* ModelicaIO_readRealTable2(_In_z_ const char* fileName,
_In_z_ const char* tableName,
_Out_ size_t* m, _Out_ size_t* n,
int verbose, _In_z_ const char* delimiter,
int nHeaderLines) {
double* table;
const char* ext;
int isMatExt = 0;
int isCsvExt = 0;
/* Table file can be either text or binary MATLAB MAT-file */
ext = strrchr(fileName, '.');
if (NULL != ext) {
if (0 == strncmp(ext, ".mat", 4) ||
0 == strncmp(ext, ".MAT", 4)) {
isMatExt = 1;
}
else if (0 == strncmp(ext, ".csv", 4) ||
0 == strncmp(ext, ".CSV", 4)) {
isCsvExt = 1;
if (strlen(delimiter) != 1) {
ModelicaFormatError("Invalid column delimiter \"%s\", must be a single character.\n", delimiter);
return NULL;
}
}
}
if (verbose == 1) {
/* Print info message, that table / file is loading */
ModelicaFormatMessage("... loading \"%s\" from \"%s\"\n",
tableName, fileName);
}
if (isMatExt == 1) {
table = readMatTable(fileName, tableName, m, n);
}
else if (isCsvExt == 1) {
table = readCsvTable(fileName, tableName, m, n, delimiter, nHeaderLines);
}
else {
table = readTxtTable(fileName, tableName, m, n);
}
return table;
}
void ModelicaIO_freeRealTable(double* table) {
free(table);
}
static double* readMatTable(_In_z_ const char* fileName, _In_z_ const char* tableName,
_Out_ size_t* m, _Out_ size_t* n) {
double* table = NULL;
MatIO matio = {NULL, NULL, NULL};
int readError = 0;
*m = 0;
*n = 0;
readRealMatIO(fileName, tableName, &matio);
if (NULL != matio.matvar) {
matvar_t* matvar = matio.matvar;
table = (double*)malloc(matvar->dims[0]*matvar->dims[1]*sizeof(double));
if (NULL == table) {
Mat_VarFree(matio.matvarRoot);
(void)Mat_Close(matio.mat);
ModelicaError("Memory allocation error\n");
return NULL;
}
{
int start[2] = {0, 0};
int stride[2] = {1, 1};
int edge[2];
edge[0] = (int)matvar->dims[0];
edge[1] = (int)matvar->dims[1];
readError = Mat_VarReadData(matio.mat, matvar, table, start, stride, edge);
*m = matvar->dims[0];
*n = matvar->dims[1];
}
}
Mat_VarFree(matio.matvarRoot);
(void)Mat_Close(matio.mat);
if (readError == 0 && NULL != table) {
/* Array is stored column-wise -> need to transpose */
transpose(table, *m, *n);
}
else {
size_t dim[2];
dim[0] = *m;
dim[1] = *n;
*m = 0;
*n = 0;
free(table);
table = NULL;
ModelicaFormatError(
"Error when reading numeric data of matrix \"%s(%lu,%lu)\" "
"from file \"%s\"\n", tableName, (unsigned long)dim[0],
(unsigned long)dim[1], fileName);
}
return table;
}
static void readMatIO(_In_z_ const char* fileName,
_In_z_ const char* matrixName, _Inout_ MatIO* matio) {
mat_t* mat;
matvar_t* matvar;
matvar_t* matvarRoot;
char* matrixNameCopy;
char* token;
#if defined(_POSIX_) || (defined(_MSC_VER) && _MSC_VER >= 1400)
char* nextToken = NULL;
#endif
char* prevToken;
int err = 0;
mat = Mat_Open(fileName, (int)MAT_ACC_RDONLY);
if (NULL == mat) {
ModelicaFormatError("Not possible to open file \"%s\": "
"No such file or directory\n", fileName);
return;
}
matrixNameCopy = (char*)malloc((strlen(matrixName) + 1) * sizeof(char));
if (NULL != matrixNameCopy) {
strcpy(matrixNameCopy, matrixName);
}
else {
(void)Mat_Close(mat);
ModelicaError("Memory allocation error\n");
return;
}
token = strtok_r(matrixNameCopy, ".", &nextToken);
matvarRoot = Mat_VarReadInfo(mat, NULL == token ? matrixName : token);
if (NULL == matvarRoot) {
(void)Mat_Close(mat);
if (NULL == token) {
free(matrixNameCopy);
ModelicaFormatError(
"Variable \"%s\" not found in file \"%s\".\n",
matrixName, fileName);
}
else {
char matrixNameBuf[MATLAB_NAME_LENGTH_MAX];
char dots[4];
if (strlen(token) > MATLAB_NAME_LENGTH_MAX - 1) {
strncpy(matrixNameBuf, token, MATLAB_NAME_LENGTH_MAX - 1);
matrixNameBuf[MATLAB_NAME_LENGTH_MAX - 1] = '\0';
strcpy(dots, "...");
}
else {
strcpy(matrixNameBuf, token);
dots[0] = '\0';
}
free(matrixNameCopy);
ModelicaFormatError(
"Variable \"%s%s\" not found in file \"%s\".\n",
matrixNameBuf, dots, fileName);
}
return;
}
matvar = matvarRoot;
prevToken = token;
token = strtok_r(NULL, ".", &nextToken);
/* Get field while matvar is of struct class and of 1x1 size */
while (NULL != token && NULL != matvar) {
if (matvar->class_type == MAT_C_STRUCT && matvar->rank == 2 &&
matvar->dims[0] == 1 && matvar->dims[1] == 1) {
matvar = Mat_VarGetStructField(matvar, (void*)token, MAT_BY_NAME, 0);
token = strtok_r(NULL, ".", &nextToken);
}
else if (matvar->class_type != MAT_C_STRUCT) {
err = 1;
matvar = NULL;
break;
}
else if (matvar->rank != 2) {
err = 2;
matvar = NULL;
break;
}
else if (matvar->dims[0] != 1 || matvar->dims[2] != 1) {
err = 3;
matvar = NULL;
break;
}
}
if (NULL == matvar) {
Mat_VarFree(matvarRoot);
(void)Mat_Close(mat);
if (NULL != token) {
char matrixNameBuf[MATLAB_NAME_LENGTH_MAX];
char dots[4];
if (strlen(prevToken) > MATLAB_NAME_LENGTH_MAX - 1) {
strncpy(matrixNameBuf, prevToken, MATLAB_NAME_LENGTH_MAX - 1);
matrixNameBuf[MATLAB_NAME_LENGTH_MAX - 1] = '\0';
strcpy(dots, "...");
}
else {
strcpy(matrixNameBuf, prevToken);
dots[0] = '\0';
}
free(matrixNameCopy);
if (1 == err) {
ModelicaFormatError(
"Variable \"%s%s\" of \"%s\" is not a struct array.\n",
matrixNameBuf, dots, matrixName);
}
else if (2 == err) {
ModelicaFormatError(
"Variable \"%s%s\" of \"%s\" is not a struct array "
"of rank 2.\n", matrixNameBuf, dots, matrixName);
}
else if (3 == err) {
ModelicaFormatError(
"Variable \"%s%s\" of \"%s\" is not a 1x1 struct array.\n",
matrixNameBuf, dots, matrixName);
}
}
else {
free(matrixNameCopy);
ModelicaFormatError(
"Variable \"%s\" not found in file \"%s\".\n", matrixName, fileName);
}
return;
}
free(matrixNameCopy);
/* Check if matvar is a matrix */
if (matvar->rank != 2) {
Mat_VarFree(matvarRoot);
(void)Mat_Close(mat);
ModelicaFormatError(
"Variable \"%s\" is not of rank 2.\n", matrixName);
return;
}
/* Set output fields for MatIO structure */
matio->mat = mat;
matio->matvar = matvar;
matio->matvarRoot = matvarRoot;
}
static void readRealMatIO(_In_z_ const char* fileName,
_In_z_ const char* matrixName, _Inout_ MatIO* matio) {
readMatIO(fileName, matrixName, matio);
if (NULL != matio->matvar) {
matvar_t* matvar = matio->matvar;
/* Check if variable class of matvar is numeric (and thus non-sparse) */
if (matvar->class_type != MAT_C_DOUBLE && matvar->class_type != MAT_C_SINGLE &&
matvar->class_type != MAT_C_INT8 && matvar->class_type != MAT_C_UINT8 &&
matvar->class_type != MAT_C_INT16 && matvar->class_type != MAT_C_UINT16 &&
matvar->class_type != MAT_C_INT32 && matvar->class_type != MAT_C_UINT32 &&
matvar->class_type != MAT_C_INT64 && matvar->class_type != MAT_C_UINT64) {
Mat_VarFree(matio->matvarRoot);
(void)Mat_Close(matio->mat);
ModelicaFormatError("Matrix \"%s\" is not a "
"numeric array.\n", matrixName);
return;
}
matvar->class_type = MAT_C_DOUBLE;
/* Check if matvar is purely real-valued */
if (matvar->isComplex) {
Mat_VarFree(matio->matvarRoot);
(void)Mat_Close(matio->mat);
ModelicaFormatError("Matrix \"%s\" must not be complex.\n",
matrixName);
return;
}
}
}
static int IsNumber(char* token) {
int foundExponentSign = 0;
int foundExponent = 0;
int foundDec = 0;
int foundDigit = 0;
int isNumber = 1;
int k;
if (token[0] == '-' || token[0] == '+') {
k = 1;
}
else {
k = 0;
}
while (token[k] != '\0') {
if (token[k] >= '0' && token[k] <= '9') {
k++;
foundDigit++;
}
else if (token[k] == '.' && foundDec == 0 &&
foundExponent == 0 && foundExponentSign == 0) {
foundDec = 1;
k++;
}
else if ((token[k] == 'e' || token[k] == 'E') &&
foundExponent == 0 && foundDigit > 0) {
foundExponent = 1;
foundDigit = 0;
k++;
}
else if ((token[k] == '-' || token[k] == '+') &&
foundExponent == 1 && foundExponentSign == 0) {
foundExponentSign = 1;
k++;
}
else {
isNumber = 0;
break;
}
}
return isNumber && foundDigit > 0;
}
static double* readCsvTable(_In_z_ const char* fileName, _In_z_ const char* tableName,
_Out_ size_t* m, _Out_ size_t* n, _In_z_ const char* delimiter,
int nHeaderLines) {
double* table = NULL;
char* buf;
int bufLen = LINE_BUFFER_LENGTH;
FILE* fp;
int readError;
unsigned long nRow = 0;
unsigned long nCol = 0;
unsigned long lineNo = 1;
#if defined(NO_LOCALE)
const char * const dec = ".";
#elif defined(_MSC_VER) && _MSC_VER >= 1400
_locale_t loc;
#elif defined(__GLIBC__) && defined(__GLIBC_MINOR__) && ((__GLIBC__ << 16) + __GLIBC_MINOR__ >= (2 << 16) + 3)
locale_t loc;
#else
char* dec;
#endif
char delimTable[5] = " \t\r";
if (delimiter[0] != ' ' && delimiter[0] != '\t' && delimiter[0] != '\r') {
strncat(delimTable, delimiter, 1);
}
fp = fopen(fileName, "r");
if (NULL == fp) {
ModelicaFormatError("Not possible to open file \"%s\": "
"No such file or directory\n", fileName);
return NULL;
}
buf = (char*)malloc(LINE_BUFFER_LENGTH*sizeof(char));
if (NULL == buf) {
fclose(fp);
ModelicaError("Memory allocation error\n");
return NULL;
}
/* Ignore file header */
while (lineNo <= (unsigned long)nHeaderLines) {
if ((readError = readLine(&buf, &bufLen, fp)) != 0) {
free(buf);
fclose(fp);
if (readError < 0) {
ModelicaFormatError(
"Error reading line %lu from file \"%s\": "
"End-Of-File reached.\n", lineNo, fileName);
}
return NULL;
}
lineNo++;
}
#if defined(NO_LOCALE)
#elif defined(_MSC_VER) && _MSC_VER >= 1400
loc = _create_locale(LC_NUMERIC, "C");
#elif defined(__GLIBC__) && defined(__GLIBC_MINOR__) && ((__GLIBC__ << 16) + __GLIBC_MINOR__ >= (2 << 16) + 3)
loc = newlocale(LC_NUMERIC, "C", NULL);
#else
dec = localeconv()->decimal_point;
#endif
/* First pass: Loop over lines of file and determine dimensions */
while (readLine(&buf, &bufLen, fp) == 0) {
nRow++;
if (nRow == 1) {
#if defined(_POSIX_) || (defined(_MSC_VER) && _MSC_VER >= 1400)
char* nextToken = NULL;
#endif
char* token = strtok_r(buf, delimTable, &nextToken);
while (NULL != token) {
token = strtok_r(NULL, delimTable, &nextToken);
nCol++;
}
}
}
/* Reset for second pass */
fseek(fp, 0, SEEK_SET);
lineNo = 1;
/* Ignore file header */
while (lineNo <= (unsigned long)nHeaderLines) {
readLine(&buf, &bufLen, fp);
lineNo++;
}
lineNo--;
{
size_t i = 0;
table = (double*)malloc(nRow*nCol*sizeof(double));
if (NULL == table) {
*m = 0;
*n = 0;
free(buf);
fclose(fp);
#if defined(NO_LOCALE)
#elif defined(_MSC_VER) && _MSC_VER >= 1400
_free_locale(loc);
#elif defined(__GLIBC__) && defined(__GLIBC_MINOR__) && ((__GLIBC__ << 16) + __GLIBC_MINOR__ >= (2 << 16) + 3)
freelocale(loc);
#endif
ModelicaError("Memory allocation error\n");
return table;
}
readError = 0;
/* Loop over rows and store table row-wise */
for (i = 0; i < nRow; i++) {
size_t j = 0;
char* token;
char* endptr;
#if defined(_POSIX_) || (defined(_MSC_VER) && _MSC_VER >= 1400)
char* nextToken = NULL;
#endif
if (readError != 0) {
break;
}
lineNo++;
readError = readLine(&buf, &bufLen, fp) != 0;
#if defined(_POSIX_) || (defined(_MSC_VER) && _MSC_VER >= 1400)
nextToken = NULL;
#endif
token = strtok_r(buf, delimTable, &nextToken);
for (j = 0; j < nCol; j++) {
if (token == NULL) {
readError = 1;
break;
}
#if !defined(NO_LOCALE) && (defined(_MSC_VER) && _MSC_VER >= 1400)
table[i*nCol + j] = _strtod_l(token, &endptr, loc);
if (*endptr != 0) {
readError = 1;
}
#elif !defined(NO_LOCALE) && (defined(__GLIBC__) && defined(__GLIBC_MINOR__) && ((__GLIBC__ << 16) + __GLIBC_MINOR__ >= (2 << 16) + 3))
table[i*nCol + j] = strtod_l(token, &endptr, loc);
if (*endptr != 0) {
readError = 1;
}
#else
if (*dec == '.') {
table[i*nCol + j] = strtod(token, &endptr);
}
else if (NULL == strchr(token, '.')) {
table[i*nCol + j] = strtod(token, &endptr);
}
else {
char* token2 = (char*)malloc(
(strlen(token) + 1)*sizeof(char));
if (NULL != token2) {
char* p;
strcpy(token2, token);
p = strchr(token2, '.');
*p = *dec;
table[i*nCol + j] = strtod(token2, &endptr);
if (*endptr != 0) {
readError = 1;
}
free(token2);
}
else {
*m = 0;
*n = 0;
free(buf);
fclose(fp);
readError = 1;
ModelicaError("Memory allocation error\n");
break;
}
}
#endif
if (readError == 0) {
token = strtok_r(NULL, delimTable, &nextToken);
}
else {
break;
}
}
}
}
free(buf);
fclose(fp);
#if defined(NO_LOCALE)
#elif defined(_MSC_VER) && _MSC_VER >= 1400
_free_locale(loc);
#elif defined(__GLIBC__) && defined(__GLIBC_MINOR__) && ((__GLIBC__ << 16) + __GLIBC_MINOR__ >= (2 << 16) + 3)
freelocale(loc);
#endif
if (readError == 0) {
*m = (size_t)nRow;
*n = (size_t)nCol;
}
else {
free(table);
table = NULL;
*m = 0;
*n = 0;
ModelicaFormatError(
"Error in line %lu when reading numeric data of matrix "
"\"%s(%lu,%lu)\" from file \"%s\"\n", lineNo,
tableName, nRow, nCol, fileName);
}
return table;
}
static double* readTxtTable(_In_z_ const char* fileName, _In_z_ const char* tableName,
_Out_ size_t* m, _Out_ size_t* n) {
#define DELIM_TABLE_HEADER " \t(,)\r"
#define DELIM_TABLE_NUMBER " \t,;\r"
double* table = NULL;
char* buf;
char* header;
int bufLen = LINE_BUFFER_LENGTH;
FILE* fp;
int foundTable = 0;
int foundDims = 0;
int readError;
unsigned long nRow = 0;
unsigned long nCol = 0;
unsigned long lineNo = 1;
const unsigned char txtHeader[2] = { 0x23,0x31 };
const unsigned char utf8BOM[3] = { 0xef,0xbb,0xbf };
#if defined(NO_LOCALE)
const char * const dec = ".";
#elif defined(_MSC_VER) && _MSC_VER >= 1400
_locale_t loc;
#elif defined(__GLIBC__) && defined(__GLIBC_MINOR__) && ((__GLIBC__ << 16) + __GLIBC_MINOR__ >= (2 << 16) + 3)
locale_t loc;
#else
char* dec;
#endif
fp = fopen(fileName, "r");
if (NULL == fp) {
ModelicaFormatError("Not possible to open file \"%s\": "
"No such file or directory\n", fileName);
return NULL;
}
buf = (char*)calloc(LINE_BUFFER_LENGTH, sizeof(char));
if (NULL == buf) {
fclose(fp);
ModelicaError("Memory allocation error\n");
return NULL;
}
/* Read file header */
if ((readError = readLine(&buf, &bufLen, fp)) == EOF) {
free(buf);
fclose(fp);
if (readError < 0) {
ModelicaFormatError(
"Error reading first line from file \"%s\": "
"End-Of-File reached.\n", fileName);
}
return NULL;
}
header = buf;
/* Ignore optional UTF-8 BOM */
if (0 == memcmp(buf, utf8BOM, sizeof(utf8BOM)))
{
header += sizeof(utf8BOM);
}
/* Expected file header format: "#1" */
if (0 != memcmp(header, txtHeader, sizeof(txtHeader))) {
size_t len = strlen(header);
fclose(fp);
if (len == 0) {
free(buf);
ModelicaFormatError(
"Error reading format and version information in first "
"line of file \"%s\": \"#1\" expected.\n", fileName);
}
else if (len == 1) {
char c0 = header[0];
free(buf);
ModelicaFormatError(
"Error reading format and version information in first "
"line of file \"%s\": \"#1\" expected, but \"0x%02x\" found.\n",
fileName, (int)(c0 & 0xff));
}
else {
char c0 = header[0];
char c1 = header[1];
free(buf);
ModelicaFormatError(
"Error reading format and version information in first "
"line of file \"%s\": \"#1\" expected, but \"0x%02x0x%02x\" "
"found.\n", fileName, (int)(c0 & 0xff), (int)(c1 & 0xff));
}
return NULL;
}
#if defined(NO_LOCALE)
#elif defined(_MSC_VER) && _MSC_VER >= 1400
loc = _create_locale(LC_NUMERIC, "C");
#elif defined(__GLIBC__) && defined(__GLIBC_MINOR__) && ((__GLIBC__ << 16) + __GLIBC_MINOR__ >= (2 << 16) + 3)
loc = newlocale(LC_NUMERIC, "C", NULL);