Debian Codegraph @

dune/uggrid/low/bio.cc

raw
1 // SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2 // SPDX-License-Identifier: LGPL-2.1-or-later
3 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 // vi: set et ts=4 sw=2 sts=2:
5 /*! \file bio.c
6 * \ingroup low
7 */
8
9 /** \addtogroup low
10 *
11 * @{
12 */
13
14 /****************************************************************************/
15 /* */
16 /* File: bio.c */
17 /* */
18 /* Purpose: basic input/output */
19 /* */
20 /* Author: Klaus Johannsen */
21 /* Institut fuer Computeranwendungen III */
22 /* Universitaet Stuttgart */
23 /* Pfaffenwaldring 27 */
24 /* 70550 Stuttgart */
25 /* email: [email protected] */
26 /* */
27 /* History: 09.12.96 begin, */
28 /* */
29 /* Remarks: */
30 /* */
31 /****************************************************************************/
32
33 /****************************************************************************/
34 /* */
35 /* include files */
36 /* system include files */
37 /* application include files */
38 /* */
39 /****************************************************************************/
40
41 #include <config.h>
42 #include <cstdio>
43 #include <cstring>
44
45 #include "bio.h"
46
47 USING_UG_NAMESPACE
48
49 /****************************************************************************/
50 /* */
51 /* defines in the following order */
52 /* */
53 /* compile time constants defining static data size (i.e. arrays) */
54 /* other constants */
55 /* macros */
56 /* */
57 /****************************************************************************/
58
59 /****************************************************************************/
60 /* */
61 /* data structures used in this source file (exported data structures are */
62 /* in the corresponding include file!) */
63 /* */
64 /****************************************************************************/
65
66 typedef int (*R_mint_proc)(int n, int *intList);
67 typedef int (*W_mint_proc)(int n, const int *intList);
68 typedef int (*R_mdouble_proc)(int n, double *doubleList);
69 typedef int (*W_mdouble_proc)(int n, const double *doubleList);
70 typedef int (*R_string_proc)(char *string);
71 typedef int (*W_string_proc)(const char *string);
72
73 /****************************************************************************/
74 /* */
75 /* definition of exported global variables */
76 /* */
77 /****************************************************************************/
78
79 /****************************************************************************/
80 /* */
81 /* definition of variables global to this source file only (static!) */
82 /* */
83 /****************************************************************************/
84
85 /* file */
86 static FILE *stream;
87 static int n_byte;
88 static fpos_t pos;
89
90 /* low level read/write functions */
91 static R_mint_proc Read_mint;
92 static W_mint_proc Write_mint;
93 static R_mdouble_proc Read_mdouble;
94 static W_mdouble_proc Write_mdouble;
95 static R_string_proc Read_string;
96 static W_string_proc Write_string;
97
98
99 /****************************************************************************/
100 /* */
101 /* forward declarations of functions used before they are defined */
102 /* */
103 /****************************************************************************/
104
105
106 /****************************************************************************/
107 /* */
108 /* ascii i/o */
109 /* */
110 /****************************************************************************/
111
112
113 static int ASCII_Read_mint (int n, int *intList)
114 {
115 int i;
116
117 for (i=0; i<n; i++)
118 if (fscanf(stream,"%d\n",intList+i)!=1) return (1);
119 return (0);
120 }
121
122 static int ASCII_Write_mint (int n, const int *intList)
123 {
124 int i,m;
125
126 for (i=0; i<n; i++)
127 {
128 m = fprintf(stream,"%d\n",intList[i]);
129 if (m<0) return (1);
130 n_byte += m;
131 }
132 return (0);
133 }
134
135 static int ASCII_Read_mdouble (int n, double *doubleList)
136 {
137 int i;
138
139 for (i=0; i<n; i++)
140 if (fscanf(stream,"%lg\n",doubleList+i)!=1) return (1);
141 return (0);
142 }
143
144 static int ASCII_Write_mdouble (int n, const double *doubleList)
145 {
146 int i,m;
147
148 for (i=0; i<n; i++)
149 {
150 m = fprintf(stream,"%g\n",doubleList[i]);
151 if (m<0) return (1);
152 n_byte += m;
153 }
154 return (0);
155 }
156
157 static int ASCII_Read_string (char *string)
158 {
159 int i,len;
160
161 if (fscanf(stream,"%d\n",&len)!=1) return (1);
162 for (i=0; i<len; i++)
163 {
164 string[i] = fgetc(stream);
165 if (string[i]==EOF)
166 return (1);
167 }
168 string[i] = fgetc(stream);
169 if (string[i]!=' ') return (1);
170 string[i] = '\0';
171
172 return (0);
173 }
174
175
176 static int ASCII_Write_string (const char *string)
177 {
178 int i,m,len;
179
180 len = strlen(string);
181 m = fprintf(stream,"%d\n",len);
182 if (m<0) return (1);
183 n_byte += m;
184 for (i=0; i<len; i++)
185 if (fputc(string[i],stream)==EOF)
186 return (1);
187 m = fprintf(stream," ");
188 if (m<0) return (1);
189 n_byte += len+m;
190
191 return (0);
192 }
193
194 /****************************************************************************/
195 /* */
196 /* binary i/o */
197 /* */
198 /****************************************************************************/
199
200
201 static int BIN_Read_mint (int n, int *intList)
202 {
203 if (fread((void*)intList,sizeof(int)*n,1,stream)!=1) return (1);
204 return (0);
205 }
206
207 static int BIN_Write_mint (int n, const int *intList)
208 {
209 if (fwrite((void*)intList,sizeof(int)*n,1,stream)!=1) return (1);
210 n_byte += n*sizeof(int);
211 return (0);
212 }
213
214 static int BIN_Read_mdouble (int n, double *doubleList)
215 {
216 if (fread((void*)doubleList,sizeof(double)*n,1,stream)!=1) return (1);
217 return (0);
218 }
219
220 static int BIN_Write_mdouble (int n, const double *doubleList)
221 {
222 if (fwrite((void*)doubleList,sizeof(double)*n,1,stream)!=1) return (1);
223 n_byte += n*sizeof(double);
224 return (0);
225 }
226
227 static int BIN_Read_string (char *string)
228 {
229 int i,len;
230
231 if (fscanf(stream,"%d ",&len)!=1) return (1);
232 for (i=0; i<len; i++)
233 {
234 string[i] = fgetc(stream);
235 if (string[i]==EOF)
236 return (1);
237 }
238 string[i] = fgetc(stream);
239 if (string[i]!=' ') return (1);
240 string[i] = '\0';
241
242 return (0);
243 }
244
245
246 static int BIN_Write_string (const char *string)
247 {
248 int i,m,len;
249
250 len = strlen(string);
251 m = fprintf(stream,"%d ",len);
252 if (m<0) return (1);
253 n_byte += m;
254 for (i=0; i<len; i++)
255 if (fputc(string[i],stream)==EOF)
256 return (1);
257 m = fprintf(stream," ");
258 if (m<0) return (1);
259 n_byte += len+m;
260
261 return (0);
262 }
263 /****************************************************************************/
264 /* */
265 /* exported i/o */
266 /* */
267 /****************************************************************************/
268
269 int NS_PREFIX Bio_Initialize (FILE *file, int mode, char rw)
270 {
271 stream = file;
272
273 switch (mode)
274 {
275 case BIO_ASCII :
276 Read_mint = ASCII_Read_mint;
277 Read_mdouble = ASCII_Read_mdouble;
278 Read_string = ASCII_Read_string;
279 Write_mint = ASCII_Write_mint;
280 Write_mdouble = ASCII_Write_mdouble;
281 Write_string = ASCII_Write_string;
282 break;
283 case BIO_BIN :
284 Read_mint = BIN_Read_mint;
285 Read_mdouble = BIN_Read_mdouble;
286 Read_string = BIN_Read_string;
287 Write_mint = BIN_Write_mint;
288 Write_mdouble = BIN_Write_mdouble;
289 Write_string = BIN_Write_string;
290 break;
291 default :
292 return (1);
293 }
294
295 return (0);
296 }
297
298 int NS_PREFIX Bio_Read_mint (int n, int *intList)
299 {
300 return ((*Read_mint)(n,intList));
301 }
302
303 int NS_PREFIX Bio_Write_mint (int n, int *intList)
304 {
305 return ((*Write_mint)(n,intList));
306 }
307
308 int NS_PREFIX Bio_Read_mdouble (int n, double *doubleList)
309 {
310 return ((*Read_mdouble)(n,doubleList));
311 }
312
313 int NS_PREFIX Bio_Write_mdouble (int n, double *doubleList)
314 {
315 return ((*Write_mdouble)(n,doubleList));
316 }
317
318 int NS_PREFIX Bio_Read_string (char *string)
319 {
320 return ((*Read_string)(string));
321 }
322
323 int NS_PREFIX Bio_Write_string (const char *string)
324 {
325 return ((*Write_string)(string));
326 }
327
328 int NS_PREFIX Bio_Jump_From (void)
329 {
330 n_byte = 0;
331 if (fgetpos(stream,&pos)) return (1);
332 if (fprintf(stream," %20d ",n_byte)<0) return (1);
333
334 return (0);
335 }
336
337 int NS_PREFIX Bio_Jump_To (void)
338 {
339 fpos_t act;
340
341 if (fgetpos(stream,&act)) return (1);
342 if (fsetpos(stream,&pos)) return (1);
343 if (fprintf(stream," %20d ",n_byte)<0) return (1);
344 if (fsetpos(stream,&act)) return (1);
345
346 return (0);
347 }
348
349 int NS_PREFIX Bio_Jump (int dojump)
350 {
351 int jump;
352
353 if (fscanf(stream," %20d ",&jump)!=1) return (1);
354 if (dojump==0) return (0);
355 while(jump>0)
356 {
357 if (fgetc(stream)==EOF) return (1);
358 jump--;
359 }
360
361 return (0);
362 }
363
364 /** @} */