FrontISTR 5.2.0
Large-scale structural analysis program with finit element method
Loading...
Searching...
No Matches
hecmw_bin_io.c
Go to the documentation of this file.
1/*****************************************************************************
2 * Copyright (c) 2019 FrontISTR Commons
3 * This software is released under the MIT License, see LICENSE.txt
4 *****************************************************************************/
5
6#include <stdio.h>
7#include <stdlib.h>
8#include <stdarg.h>
9#include <ctype.h>
10#include <assert.h>
11#include <string.h>
12
13#include "hecmw_bin_io.h"
14
15/*
16 format of fmt
17 'I' : int
18 'F' : double
19 'I<n>' : int[n]
20 'F<n>' : double[n]
21 'S' : char[]
22*/
23
24#ifndef SUCCESS
25#define SUCCESS 0
26#define FAIL -1
27#endif
28
29#ifndef TRUE
30#define TRUE 1
31#define FALSE 0
32#endif
33
34static int fg_little_endian = TRUE;
35
36#define RES_BIN_HEADER "HECMW_BINARY_RESULT"
37
38/*---------------------------------------------------------------------------*/
39
41 char c[2];
42 short s;
43};
44
45static void check_endian(void) {
46 union endian_check_u u;
47 u.c[0] = 0;
48 u.c[1] = 0;
49 u.s = 1;
50 fg_little_endian = (u.c[0] == 1);
51}
52
53void hecmw_set_endian_info(void) { check_endian(); }
54
55/*---------------------------------------------------------------------------*/
56
57int hecmw_write_bin_value(unsigned char *x, int size, FILE *fp) {
58 unsigned char *c;
59 int i;
60
61 if (fg_little_endian) {
62 c = x;
63
64 for (i = 0; i < size; i++) {
65 if (putc(*c, fp) == EOF) return FAIL;
66
67 c++;
68 }
69
70 } else {
71 c = x;
72 c += size - 1;
73
74 for (i = 0; i < size; i++) {
75 if (putc(*c, fp) == EOF) return FAIL;
76
77 c--;
78 }
79 }
80
81 return SUCCESS;
82}
83
84int hecmw_write_bin_int(int x, FILE *fp) {
85 long xx = x;
86 return hecmw_write_bin_value((unsigned char *)&xx, sizeof(long), fp);
87}
88
89int hecmw_write_bin_int_arr(int *x, int n, FILE *fp) {
90 long xx;
91 int i;
92
93 for (i = 0; i < n; i++, x++) {
94 xx = *x;
95
96 if (hecmw_write_bin_value((unsigned char *)&xx, sizeof(long), fp) !=
97 SUCCESS)
98 return FAIL;
99 }
100
101 return SUCCESS;
102}
103
104int hecmw_write_bin_double(double x, FILE *fp) {
105 return hecmw_write_bin_value((unsigned char *)&x, sizeof(double), fp);
106}
107
108int hecmw_write_bin_double_arr(double *x, int n, FILE *fp) {
109 int i;
110
111 for (i = 0; i < n; i++, x++) {
112 if (hecmw_write_bin_value((unsigned char *)&x, sizeof(double), fp) !=
113 SUCCESS)
114 return FAIL;
115 }
116
117 return SUCCESS;
118}
119
120/*---------------------------------------------------------------------------*/
121
122int hecmw_read_bin_value(unsigned char *x, int size, FILE *fp) {
123 unsigned char *c;
124 int data;
125 int i;
126
127 if (fg_little_endian) {
128 c = x;
129
130 for (i = 0; i < size; i++) {
131 data = getc(fp);
132
133 if (data == EOF) return FAIL;
134
135 *c = (unsigned char)data;
136 c++;
137 }
138
139 } else {
140 c = x;
141 c += size - 1;
142
143 for (i = 0; i < size; i++) {
144 data = getc(fp);
145
146 if (data == EOF) return FAIL;
147
148 *c = (unsigned char)data;
149 c--;
150 }
151 }
152
153 return SUCCESS;
154}
155
156int hecmw_read_bin_int(int *x, FILE *fp) {
157 long xx;
158
159 if (hecmw_read_bin_value((unsigned char *)&xx, sizeof(long), fp) != SUCCESS) {
160 return FAIL;
161 }
162
163 *x = xx;
164 return SUCCESS;
165}
166
167int hecmw_read_bin_int_arr(int *x, int n, FILE *fp) {
168 long xx;
169 int i;
170
171 for (i = 0; i < n; i++, x++) {
172 if (hecmw_read_bin_value((unsigned char *)&xx, sizeof(long), fp) !=
173 SUCCESS) {
174 return FAIL;
175 }
176
177 *x = xx;
178 }
179
180 return SUCCESS;
181}
182
183int hecmw_read_bin_double(double *x, FILE *fp) {
184 if (hecmw_read_bin_value((unsigned char *)x, sizeof(double), fp) != SUCCESS) {
185 return FAIL;
186 }
187
188 return SUCCESS;
189}
190
191int hecmw_read_bin_double_arr(double *x, int n, FILE *fp) {
192 int i;
193
194 for (i = 0; i < n; i++, x++) {
195 if (hecmw_read_bin_value((unsigned char *)x, sizeof(double), fp) != SUCCESS)
196 return FAIL;
197 }
198
199 return SUCCESS;
200}
201
202/*---------------------------------------------------------------------------*/
203/*---------------------------------------------------------------------------*/
204
205static int get_fmt_type_size(char **fmt_p, char *type, int *size) {
206 int i;
207 char s[256];
208 char *sp = s;
209 char *p = *fmt_p;
210
211 if (!*p) return 0; /* FAIL */
212
213 *type = *p;
214 p++;
215 i = 0;
216
217 while (*p && '0' <= *p && *p <= '9') {
218 *sp = *p;
219 p++;
220 sp++;
221 i++;
222 }
223
224 *sp = 0;
225 p = *fmt_p;
226
227 if (i > 0) {
228 sscanf(s, "%d", size);
229 p += i + 1;
230
231 } else {
232 *size = 0;
233 p++;
234 }
235
236 *fmt_p = p;
237 return 1; /* SUCCESS */
238}
239
240int hecmw_write_bin(FILE *fp, const char *fmt, ...) {
241 int i, n;
242 char type;
243 int size;
244 char *fmt_p;
245 int i_data;
246 double d_data;
247 int *i_ptr;
248 double *d_ptr;
249 char *s_ptr;
250 va_list va;
251 va_start(va, fmt);
252 n = strlen((char *)fmt);
253 i = 0;
254 fmt_p = (char *)fmt;
255
256 while (get_fmt_type_size(&fmt_p, &type, &size)) {
257 switch (type) {
258 case 'I':
259 case 'i': /* int */
260 if (size > 0) {
261 i_ptr = va_arg(va, int *);
262
263 if (hecmw_write_bin_int_arr(i_ptr, size, fp)) goto ERROR_EXIT;
264
265 } else {
266 i_data = va_arg(va, int);
267
268 if (hecmw_write_bin_int(i_data, fp)) goto ERROR_EXIT;
269 }
270
271 break;
272
273 case 'F':
274 case 'f': /* double */
275 if (size > 0) {
276 d_ptr = va_arg(va, double *);
277
278 if (hecmw_write_bin_double_arr(d_ptr, size, fp)) goto ERROR_EXIT;
279
280 } else {
281 d_data = va_arg(va, double);
282
283 if (hecmw_write_bin_double(d_data, fp)) goto ERROR_EXIT;
284 }
285
286 break;
287
288 case 'S':
289 case 's': /* string */
290 if (size > 0) {
291 s_ptr = va_arg(va, char *);
292
293 if (fwrite(s_ptr, sizeof(char), size, fp) != size) goto ERROR_EXIT;
294
295 } else {
296 s_ptr = va_arg(va, char *);
297
298 while (*s_ptr) {
299 if (fwrite(s_ptr, sizeof(char), 1, fp) != 1) goto ERROR_EXIT;
300
301 s_ptr++;
302 }
303
304 if (fwrite(s_ptr, sizeof(char), 1, fp) != 1) goto ERROR_EXIT;
305 }
306
307 break;
308
309 default:
310 fprintf(stderr, "Illeagal type : %c (0x%x)\n", type, type);
311 assert(0);
312 }
313 }
314
315 va_end(va);
316 return SUCCESS;
317ERROR_EXIT:
318 va_end(va);
319 return FAIL;
320}
321
322int hecmw_read_bin(FILE *fp, const char *fmt, ...) {
323 int i, n;
324 char type;
325 int size;
326 char *fmt_p;
327 int *i_ptr;
328 double *d_ptr;
329 char *s_ptr;
330 char c;
331 va_list va;
332 va_start(va, fmt);
333 n = strlen((char *)fmt);
334 i = 0;
335 fmt_p = (char *)fmt;
336
337 while (get_fmt_type_size(&fmt_p, &type, &size)) {
338 switch (type) {
339 case 'I':
340 case 'i': /* int */
341 if (size == 0) size = 1;
342
343 i_ptr = va_arg(va, int *);
344
345 if (hecmw_read_bin_int_arr(i_ptr, size, fp)) goto ERROR_EXIT;
346
347 break;
348
349 case 'F':
350 case 'f': /* double */
351 if (size == 0) size = 1;
352
353 d_ptr = va_arg(va, double *);
354
355 if (hecmw_read_bin_double_arr(d_ptr, size, fp)) goto ERROR_EXIT;
356
357 break;
358
359 case 'S':
360 case 's': /* string */
361 if (size > 0) {
362 s_ptr = va_arg(va, char *);
363
364 if (fread(s_ptr, sizeof(char), size, fp) != size) goto ERROR_EXIT;
365
366 } else {
367 s_ptr = va_arg(va, char *);
368
369 for (;;) {
370 if (fread(&c, sizeof(char), 1, fp) != 1) break;
371
372 *s_ptr = c;
373
374 if (c == 0) break;
375
376 s_ptr++;
377 }
378 }
379
380 break;
381
382 default:
383 assert(0);
384 }
385 }
386
387 va_end(va);
388 return SUCCESS;
389ERROR_EXIT:
390 va_end(va);
391 return FAIL;
392}
int hecmw_write_bin_int(int x, FILE *fp)
Definition: hecmw_bin_io.c:84
int hecmw_write_bin_value(unsigned char *x, int size, FILE *fp)
Definition: hecmw_bin_io.c:57
int hecmw_read_bin_value(unsigned char *x, int size, FILE *fp)
Definition: hecmw_bin_io.c:122
int hecmw_read_bin_double_arr(double *x, int n, FILE *fp)
Definition: hecmw_bin_io.c:191
int hecmw_read_bin_int_arr(int *x, int n, FILE *fp)
Definition: hecmw_bin_io.c:167
int hecmw_read_bin_double(double *x, FILE *fp)
Definition: hecmw_bin_io.c:183
int hecmw_write_bin_double(double x, FILE *fp)
Definition: hecmw_bin_io.c:104
int hecmw_read_bin_int(int *x, FILE *fp)
Definition: hecmw_bin_io.c:156
#define TRUE
Definition: hecmw_bin_io.c:30
#define SUCCESS
Definition: hecmw_bin_io.c:25
int hecmw_write_bin_int_arr(int *x, int n, FILE *fp)
Definition: hecmw_bin_io.c:89
void hecmw_set_endian_info(void)
Definition: hecmw_bin_io.c:53
#define FAIL
Definition: hecmw_bin_io.c:26
int hecmw_write_bin_double_arr(double *x, int n, FILE *fp)
Definition: hecmw_bin_io.c:108
int hecmw_write_bin(FILE *fp, const char *fmt,...)
Definition: hecmw_bin_io.c:240
int hecmw_read_bin(FILE *fp, const char *fmt,...)
Definition: hecmw_bin_io.c:322
CNFData data