|
| 1 | +/* |
| 2 | + * This file is part of OpenModelica. |
| 3 | + * |
| 4 | + * Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC), |
| 5 | + * c/o Linköpings universitet, Department of Computer and Information Science, |
| 6 | + * SE-58183 Linköping, Sweden. |
| 7 | + * |
| 8 | + * All rights reserved. |
| 9 | + * |
| 10 | + * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR |
| 11 | + * THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2. |
| 12 | + * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES |
| 13 | + * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3, |
| 14 | + * ACCORDING TO RECIPIENTS CHOICE. |
| 15 | + * |
| 16 | + * The OpenModelica software and the Open Source Modelica |
| 17 | + * Consortium (OSMC) Public License (OSMC-PL) are obtained |
| 18 | + * from OSMC, either from the above address, |
| 19 | + * from the URLs: http://www.ida.liu.se/projects/OpenModelica or |
| 20 | + * http://www.openmodelica.org, and in the OpenModelica distribution. |
| 21 | + * GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html. |
| 22 | + * |
| 23 | + * This program is distributed WITHOUT ANY WARRANTY; without |
| 24 | + * even the implied warranty of MERCHANTABILITY or FITNESS |
| 25 | + * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH |
| 26 | + * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL. |
| 27 | + * |
| 28 | + * See the full OSMC Public License conditions for more details. |
| 29 | + * |
| 30 | + */ |
| 31 | + |
| 32 | +#include "MatVer4.h" |
| 33 | + |
| 34 | +#include <stddef.h> |
| 35 | +#include <stdint.h> |
| 36 | +#include <stdio.h> |
| 37 | +#include <stdlib.h> |
| 38 | +#include <string.h> |
| 39 | + |
| 40 | +#ifdef __cplusplus |
| 41 | +extern "C" { |
| 42 | +#endif |
| 43 | + |
| 44 | +const char isBigEndian() |
| 45 | +{ |
| 46 | + union |
| 47 | + { |
| 48 | + uint32_t i32; |
| 49 | + uint8_t i8[4]; |
| 50 | + } test = { 0x01020304 }; |
| 51 | + return (1 == test.i8[0]); |
| 52 | +} |
| 53 | + |
| 54 | +int writeMatVer4Matrix_4(FILE* file, const char* name, size_t rows, size_t cols, const void* matrixData, MatVer4Type_t type) |
| 55 | +{ |
| 56 | + struct MatVer4Header |
| 57 | + { |
| 58 | + unsigned int type; |
| 59 | + unsigned int mrows; |
| 60 | + unsigned int ncols; |
| 61 | + unsigned int imagf; |
| 62 | + unsigned int namelen; |
| 63 | + } header; |
| 64 | + |
| 65 | + size_t size; |
| 66 | + switch (type) |
| 67 | + { |
| 68 | + case MatVer4Type_DOUBLE: |
| 69 | + size = sizeof(double); |
| 70 | + break; |
| 71 | + case MatVer4Type_SINGLE: |
| 72 | + size = sizeof(float); |
| 73 | + break; |
| 74 | + case MatVer4Type_INT32: |
| 75 | + size = sizeof(int32_t); |
| 76 | + break; |
| 77 | + case MatVer4Type_CHAR: |
| 78 | + size = sizeof(uint8_t); |
| 79 | + break; |
| 80 | + default: |
| 81 | + return -1; |
| 82 | + } |
| 83 | + |
| 84 | + header.type = (isBigEndian() ? 1000 : 0) + type; |
| 85 | + header.mrows = (unsigned int) rows; |
| 86 | + header.ncols = (unsigned int) cols; |
| 87 | + header.imagf = 0; |
| 88 | + header.namelen = (unsigned int) strlen(name) + 1; |
| 89 | + |
| 90 | + fwrite(&header, sizeof(MatVer4Header), 1, file); |
| 91 | + fwrite(name, sizeof(char), header.namelen, file); |
| 92 | + fwrite(matrixData, size, rows * cols, file); |
| 93 | + |
| 94 | + return 0; |
| 95 | +} |
| 96 | + |
| 97 | +int appendMatVer4Matrix_4(FILE* file, long position, const char* name, size_t rows, size_t cols, const void* matrixData, MatVer4Type_t type) |
| 98 | +{ |
| 99 | + struct MatVer4Header |
| 100 | + { |
| 101 | + unsigned int type; |
| 102 | + unsigned int mrows; |
| 103 | + unsigned int ncols; |
| 104 | + unsigned int imagf; |
| 105 | + unsigned int namelen; |
| 106 | + } header; |
| 107 | + |
| 108 | + size_t size; |
| 109 | + switch (type) |
| 110 | + { |
| 111 | + case MatVer4Type_DOUBLE: |
| 112 | + size = sizeof(double); |
| 113 | + break; |
| 114 | + case MatVer4Type_SINGLE: |
| 115 | + size = sizeof(float); |
| 116 | + break; |
| 117 | + case MatVer4Type_INT32: |
| 118 | + size = sizeof(int32_t); |
| 119 | + break; |
| 120 | + case MatVer4Type_CHAR: |
| 121 | + size = sizeof(uint8_t); |
| 122 | + break; |
| 123 | + default: |
| 124 | + return -1; |
| 125 | + } |
| 126 | + |
| 127 | + long eof = ftell(file); |
| 128 | + fseek(file, position, SEEK_SET); |
| 129 | + fread(&header, sizeof(MatVer4Header), 1, file); |
| 130 | + if (header.type != (isBigEndian() ? 1000 : 0) + type) |
| 131 | + return -1; |
| 132 | + if (header.mrows != rows) |
| 133 | + return -1; |
| 134 | + header.ncols += (unsigned int) cols; |
| 135 | + if (header.imagf != 0) |
| 136 | + return -1; |
| 137 | + if (header.namelen != strlen(name) + 1) |
| 138 | + return -1; |
| 139 | + |
| 140 | + fseek(file, position, SEEK_SET); |
| 141 | + fwrite(&header, sizeof(MatVer4Header), 1, file); |
| 142 | + fseek(file, eof, SEEK_SET); |
| 143 | + fwrite(matrixData, size, rows * cols, file); |
| 144 | + return 0; |
| 145 | +} |
| 146 | + |
| 147 | +int writeMatVer4Header_4(FILE* file, long position, const char* name, size_t rows, size_t cols, MatVer4Type_t type) |
| 148 | +{ |
| 149 | + struct MatVer4Header |
| 150 | + { |
| 151 | + unsigned int type; |
| 152 | + unsigned int mrows; |
| 153 | + unsigned int ncols; |
| 154 | + unsigned int imagf; |
| 155 | + unsigned int namelen; |
| 156 | + } header; |
| 157 | + |
| 158 | + size_t size; |
| 159 | + switch (type) |
| 160 | + { |
| 161 | + case MatVer4Type_DOUBLE: |
| 162 | + size = sizeof(double); |
| 163 | + break; |
| 164 | + case MatVer4Type_SINGLE: |
| 165 | + size = sizeof(float); |
| 166 | + break; |
| 167 | + case MatVer4Type_INT32: |
| 168 | + size = sizeof(int32_t); |
| 169 | + break; |
| 170 | + case MatVer4Type_CHAR: |
| 171 | + size = sizeof(uint8_t); |
| 172 | + break; |
| 173 | + default: |
| 174 | + return -1; |
| 175 | + } |
| 176 | + |
| 177 | + long eof = ftell(file); |
| 178 | + fseek(file, position, SEEK_SET); |
| 179 | + fread(&header, sizeof(MatVer4Header), 1, file); |
| 180 | + if (header.type != (isBigEndian() ? 1000 : 0) + type) |
| 181 | + return -1; |
| 182 | + if (header.mrows != rows) |
| 183 | + return -1; |
| 184 | + header.ncols = (unsigned int) cols; |
| 185 | + if (header.imagf != 0) |
| 186 | + return -1; |
| 187 | + if (header.namelen != strlen(name) + 1) |
| 188 | + return -1; |
| 189 | + |
| 190 | + fseek(file, position, SEEK_SET); |
| 191 | + fwrite(&header, sizeof(MatVer4Header), 1, file); |
| 192 | + fseek(file, eof, SEEK_SET); |
| 193 | + return 0; |
| 194 | +} |
| 195 | + |
| 196 | +MatVer4Matrix* readMatVer4Matrix_4(FILE* file) |
| 197 | +{ |
| 198 | + MatVer4Matrix *matrix = (MatVer4Matrix*) malloc(sizeof(MatVer4Matrix)); |
| 199 | + if (!matrix) |
| 200 | + return NULL; |
| 201 | + |
| 202 | + fread(&matrix->header, sizeof(MatVer4Header), 1, file); |
| 203 | + |
| 204 | + // skip name |
| 205 | + fseek(file, matrix->header.namelen, SEEK_CUR); |
| 206 | + |
| 207 | + size_t size; |
| 208 | + switch (matrix->header.type % 100) |
| 209 | + { |
| 210 | + case MatVer4Type_DOUBLE: |
| 211 | + size = sizeof(double); |
| 212 | + break; |
| 213 | + case MatVer4Type_SINGLE: |
| 214 | + size = sizeof(float); |
| 215 | + break; |
| 216 | + case MatVer4Type_INT32: |
| 217 | + size = sizeof(int32_t); |
| 218 | + break; |
| 219 | + case MatVer4Type_CHAR: |
| 220 | + size = sizeof(uint8_t); |
| 221 | + break; |
| 222 | + default: |
| 223 | + free(matrix); |
| 224 | + return NULL; |
| 225 | + } |
| 226 | + |
| 227 | + matrix->data = malloc(matrix->header.mrows * matrix->header.ncols * size); |
| 228 | + fread(matrix->data, size, matrix->header.mrows*matrix->header.ncols, file); |
| 229 | + |
| 230 | + return matrix; |
| 231 | +} |
| 232 | + |
| 233 | +void freeMatVer4Matrix_4(MatVer4Matrix** matrix) |
| 234 | +{ |
| 235 | + if (*matrix) |
| 236 | + { |
| 237 | + if ((*matrix)->data) |
| 238 | + free((*matrix)->data); |
| 239 | + free(*matrix); |
| 240 | + *matrix = NULL; |
| 241 | + } |
| 242 | +} |
| 243 | + |
| 244 | +int skipMatVer4Matrix_4(FILE* file) |
| 245 | +{ |
| 246 | + MatVer4Header header; |
| 247 | + fread(&header, sizeof(MatVer4Header), 1, file); |
| 248 | + |
| 249 | + // skip name |
| 250 | + fseek(file, header.namelen, SEEK_CUR); |
| 251 | + |
| 252 | + size_t size; |
| 253 | + switch (header.type % 100) |
| 254 | + { |
| 255 | + case MatVer4Type_DOUBLE: |
| 256 | + size = sizeof(double); |
| 257 | + break; |
| 258 | + case MatVer4Type_SINGLE: |
| 259 | + size = sizeof(float); |
| 260 | + break; |
| 261 | + case MatVer4Type_INT32: |
| 262 | + size = sizeof(int32_t); |
| 263 | + break; |
| 264 | + case MatVer4Type_CHAR: |
| 265 | + size = sizeof(uint8_t); |
| 266 | + break; |
| 267 | + default: |
| 268 | + return -1; |
| 269 | + } |
| 270 | + |
| 271 | + // skip data |
| 272 | + fseek(file, header.mrows*header.ncols*size, SEEK_CUR); |
| 273 | + return 0; |
| 274 | +} |
| 275 | + |
| 276 | +#ifdef __cplusplus |
| 277 | +} |
| 278 | +#endif |
0 commit comments