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