OMCompiler/SimulationRuntime/c/util/write_matlab4.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file belongs to the OpenModelica Run-Time System | ||
| 3 | * | ||
| 4 | * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC), c/o Linköpings | ||
| 5 | * universitet, Department of Computer and Information Science, SE-58183 Linköping, Sweden. All rights | ||
| 6 | * reserved. | ||
| 7 | * | ||
| 8 | * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE | ||
| 9 | * AGPL VERSION 3 LICENSE OR THE OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8. ANY | ||
| 10 | * USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S | ||
| 11 | * ACCEPTANCE OF THE BSD NEW LICENSE OR THE OSMC PUBLIC LICENSE OR THE AGPL | ||
| 12 | * VERSION 3, ACCORDING TO RECIPIENTS CHOICE. | ||
| 13 | * | ||
| 14 | * The OpenModelica software and the OSMC (Open Source Modelica Consortium) Public License | ||
| 15 | * (OSMC-PL) are obtained from OSMC, either from the above address, from the URLs: | ||
| 16 | * http://www.openmodelica.org or https://github.com/OpenModelica/ or | ||
| 17 | * http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica distribution. GNU | ||
| 18 | * AGPL version 3 is obtained from: https://www.gnu.org/licenses/licenses.html#GPL. The BSD NEW | ||
| 19 | * License is obtained from: http://www.opensource.org/licenses/BSD-3-Clause. | ||
| 20 | * | ||
| 21 | * This program is distributed WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY | ||
| 23 | * SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF | ||
| 24 | * OSMC-PL. | ||
| 25 | * | ||
| 26 | */ | ||
| 27 | |||
| 28 | #include <stdlib.h> | ||
| 29 | #include <stdint.h> | ||
| 30 | #include <errno.h> | ||
| 31 | #include <string.h> | ||
| 32 | #include <assert.h> | ||
| 33 | #include "read_matlab4.h" | ||
| 34 | #include "write_matlab4.h" | ||
| 35 | |||
| 36 | extern const char *omc_mat_Aclass; | ||
| 37 | |||
| 38 | typedef struct { | ||
| 39 | uint32_t type; | ||
| 40 | uint32_t mrows; | ||
| 41 | uint32_t ncols; | ||
| 42 | uint32_t imagf; | ||
| 43 | uint32_t namelen; | ||
| 44 | } MHeader_t; | ||
| 45 | |||
| 46 | /* Matrix list: "Aclass" "name" "description" "dataInfo" "data_1" "data_2" */ | ||
| 47 | |||
| 48 | /* Returns 0 on success */ | ||
| 49 | 1 | int writeMatVer4AclassNormal(FILE *fout) | |
| 50 | { | ||
| 51 | 1 | const char Aclass_normal[] = "A1 bt. ir1 na Nj oe rc mt ao lr y "; | |
| 52 | 1 | return writeMatVer4Matrix(fout, "Aclass", 4, 11, Aclass_normal, sizeof(int8_t)); | |
| 53 | } | ||
| 54 | |||
| 55 | // writes MAT-file matrix header to file | ||
| 56 | 6 | int writeMatVer4MatrixHeader(FILE *fout,const char *name, int rows, int cols, unsigned int size) | |
| 57 | { | ||
| 58 | typedef struct MHeader { | ||
| 59 | uint32_t type; | ||
| 60 | uint32_t mrows; | ||
| 61 | uint32_t ncols; | ||
| 62 | uint32_t imagf; | ||
| 63 | uint32_t namelen; | ||
| 64 | } MHeader_t; | ||
| 65 | const int endian_test = 1; | ||
| 66 | MHeader_t hdr; | ||
| 67 | |||
| 68 | int type = 0; | ||
| 69 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if(size == 1 /* char */) |
| 70 | type = 51; | ||
| 71 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 5 times.
|
6 | if(size == 4 /* int32 */) |
| 72 | type = 20; | ||
| 73 | |||
| 74 | /* create matrix header structure */ | ||
| 75 | 6 | hdr.type = 1000*((*(char*)&endian_test) == 0) + type; | |
| 76 | 6 | hdr.mrows = rows; | |
| 77 | 6 | hdr.ncols = cols; | |
| 78 | 6 | hdr.imagf = 0; | |
| 79 | 6 | hdr.namelen = strlen(name)+1; | |
| 80 | |||
| 81 | /* write header to file */ | ||
| 82 |
2/4✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 6 times.
|
6 | return !(1 == fwrite(&hdr, sizeof(MHeader_t), 1, fout) && 1 == fwrite(name, sizeof(char)*hdr.namelen, 1, fout)); |
| 83 | } | ||
| 84 | |||
| 85 | 1 | int writeMatVer4Matrix(FILE *fout, const char *name, int rows, int cols, const void *matrixData, unsigned int size) | |
| 86 | { | ||
| 87 | /* write data */ | ||
| 88 |
2/4✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
|
1 | return !(0==writeMatVer4MatrixHeader(fout, name, rows, cols, size) && 1 == fwrite(matrixData, (size)*rows*cols, 1, fout)); |
| 89 | } | ||
| 90 |