SND@LHC Software
Loading...
Searching...
No Matches
readc.c
Go to the documentation of this file.
1
56#ifdef USE_SHIFT_RFIO
57#include <shift.h>
58// or this??
59// // needed?#define _LARGEFILE64_SOURCE
60//#include <sys/types.h>
61//#include "rfio_api.h"
62#else
63#include <stdio.h>
64#endif
65#include "cfortran.h"
66#ifdef USE_ZLIB
67#include <zlib.h>
68#endif
69
70/* ________ global variables used for file handling __________ */
71
72#ifdef USE_ZLIB
73gzFile **files;
74#else
75FILE **files;
76#endif
77
78unsigned int maxNumFiles;
79unsigned int numAllFiles;
80
81/*______________________________________________________________*/
83
86void initC(int *nFiles) {
87 maxNumFiles = *nFiles;
88#ifdef USE_ZLIB
89 printf(" initC: using zlib version %s\n",ZLIB_VERSION);
90 files = (gzFile **) malloc(sizeof(gzFile *)*maxNumFiles);
91#else
92 files = (FILE **) malloc(sizeof(FILE *) * maxNumFiles);
93#endif
94 {
95 int i = 0;
96 for (; i < maxNumFiles; ++i) {
97 files[i] = 0;
98 }
99 }
100 numAllFiles = 0;
101}
102FCALLSCSUB1( initC, INITC, initc, PINT)
103
104/*______________________________________________________________*/
105
106/* void rewinC() */
107/* { */
108/* /\* rewind all open files and start again with first file *\/ */
109
110/* unsigned int i = numAllFiles; */
111/* while (i--) rewind(files[i]); /\* postfix decrement! *\/ */
112/* fileIndex = 0; */
113/* } */
114/* FCALLSCSUB0(rewinC,REWINC,rewinc) */
115
116/*______________________________________________________________*/
118
121void resetC(int *nFileIn) {
122 int fileIndex = *nFileIn - 1; /* index of current file */
123 if (fileIndex < 0)
124 return; /* no file opened at all... */
125#ifdef USE_ZLIB
126 gzrewind(files[fileIndex]);
127#else
128 /* rewind(files[fileIndex]); Does not work with rfio, so call: */
129 fseek(files[fileIndex], 0L, SEEK_SET);
130 clearerr(files[fileIndex]); /* These two should be the same as rewind... */
131#endif
132}
133FCALLSCSUB1( resetC, RESETC, resetc, PINT)
134
135/*______________________________________________________________*/
137
140void closeC(int *nFileIn) {
141 int fileIndex = *nFileIn - 1; /* index of current file */
142 if (fileIndex < 0)
143 return; /* no file opened at all... */
144#ifdef USE_ZLIB
145 gzclose(files[fileIndex]);
146#else
147 fclose(files[fileIndex]);
148#endif
149 files[fileIndex] = 0;
150}
151FCALLSCSUB1( closeC, CLOSEC, closec, PINT)
152
153/*______________________________________________________________*/
155void openC(const char *fileName, int *nFileIn, int *errorFlag)
165{
166 /* No return value since to be called as subroutine from fortran */
167
168 if (!errorFlag)
169 return; /* 'printout' error? */
170
171 int fileIndex = *nFileIn - 1; /* index of specific file */
172 if (fileIndex < 0) fileIndex = numAllFiles; /* next one */
173
174 if (fileIndex >= maxNumFiles) {
175 *errorFlag = 1;
176 } else {
177#ifdef USE_ZLIB
178 files[fileIndex] = gzopen(fileName, "rb");
179 if (!files[fileIndex]) {
180 *errorFlag = 2;
181 } else
182#else
183 files[fileIndex] = fopen(fileName, "rb");
184 if (!files[fileIndex]) {
185 *errorFlag = 2;
186 } else if (ferror(files[fileIndex])) {
187 fclose(files[fileIndex]);
188 files[fileIndex] = 0;
189 *errorFlag = 3;
190 } else
191#endif
192 {
193 ++numAllFiles; /* We have one more opened file! */
194 *errorFlag = 0;
195 }
196 }
197}
198FCALLSCSUB3( openC, OPENC, openc, STRING, PINT, PINT)
199
200/*______________________________________________________________*/
202
220void readC(double *bufferDouble, float *bufferFloat, int *bufferInt,
221 int *lengthBuffers, int *nFileIn, int *errorFlag) {
222 /* No return value since to be called as subroutine from fortran,
223 negative *errorFlag are errors, otherwise fine.
224
225 *nFileIn: number of the file the record is read from,
226 starting from 1 (not 0)
227 */
228 int doublePrec = 0;
229
230 if (!errorFlag)
231 return;
232 *errorFlag = 0;
233 int fileIndex = *nFileIn - 1; /* index of current file */
234 if (fileIndex < 0)
235 return; /* no file opened at all... */
236 if (!bufferFloat || !bufferInt || !lengthBuffers) {
237 *errorFlag = -1;
238 return;
239 }
240
241 /* read length of 'record' */
242 int recordLength = 0; /* becomes number of words following in file */
243#ifdef USE_ZLIB
244 int nCheckR = gzread(files[fileIndex], &recordLength, sizeof(recordLength));
245 if (gzeof(files[fileIndex])) {
246 /* gzrewind(files[fileIndex]); CHK: moved to binrwd */
247 *errorFlag = 0; /* Means EOF of file. */
248 return;
249 }
250 if (recordLength<0) {
251 doublePrec = 1;
252 recordLength = -recordLength;
253 }
254 if (sizeof(recordLength) != nCheckR) {
255 printf("readC: problem reading length of record file %d\n", fileIndex);
256 *errorFlag = -2;
257 return;
258 }
259
260 if (recordLength/2 > *lengthBuffers) {
261 /* printf("readC: given buffers too short (%d, need > %d)\n", *lengthBuffers,
262 recordLength/2); */
263 /* skip floats */
264 int i=0;
265 if (doublePrec) {
266 for (; i< recordLength/2; ++i)
267 {
268 int nCheckD = gzread(files[fileIndex], bufferDouble, sizeof(bufferDouble[0]));
269 if (nCheckD != sizeof(bufferDouble[0])) {
270 printf("readC: problem with stream or EOF skipping doubles\n");
271 *errorFlag = -32;
272 return;
273 }
274 }
275 } else {
276 for (; i< recordLength/2; ++i)
277 {
278 int nCheckF = gzread(files[fileIndex], bufferFloat, sizeof(bufferFloat[0]));
279 if (nCheckF != sizeof(bufferFloat[0])) {
280 printf("readC: problem with stream or EOF skipping floats\n");
281 *errorFlag = -8;
282 return;
283 }
284 }
285 }
286 i=0;
287 /* skip ints */
288 for (; i< recordLength/2; ++i)
289 {
290 int nCheckI = gzread(files[fileIndex], bufferInt, sizeof(bufferInt[0]));
291 if (nCheckI != sizeof(bufferInt[0])) {
292 printf("readC: problem with stream or EOF skipping ints\n");
293 *errorFlag = -16;
294 return;
295 }
296 }
297
298 *errorFlag = -4;
299 *lengthBuffers = recordLength/2;
300 return;
301 } else {
302 *lengthBuffers = recordLength/2;
303 }
304
305 /* read floats (i.e. derivatives + value + sigma) */
306 if (doublePrec) {
307 int nCheckD = gzread(files[fileIndex], bufferDouble, *lengthBuffers*8);
308 if (nCheckD != *lengthBuffers*8) {
309 printf("readC: problem with stream or EOF reading doubles\n");
310 *errorFlag = -32;
311 return;
312 }
313 } else {
314 int nCheckF = gzread(files[fileIndex], bufferFloat, *lengthBuffers*4);
315 if (nCheckF != *lengthBuffers*4) {
316 printf("readC: problem with stream or EOF reading floats\n");
317 *errorFlag = -8;
318 return;
319 }
320 int i=0;
321 for (; i< recordLength/2; ++i) bufferDouble[i] = (double) bufferFloat[i];
322 }
323
324 /* read ints (i.e. parameter labels) */
325 int nCheckI = gzread(files[fileIndex], bufferInt, *lengthBuffers*4);
326 if (nCheckI != *lengthBuffers*4) {
327 printf("readC: problem with stream or EOF reading ints\n");
328 *errorFlag = -16;
329 return;
330 }
331#else
332 size_t nCheckR = fread(&recordLength, sizeof(recordLength), 1,
333 files[fileIndex]);
334 if (feof(files[fileIndex])) {
335 /* rewind(files[fileIndex]); Does not work with rfio, so call: */
336 /* fseek(files[fileIndex], 0L, SEEK_SET); CHK: moved to binrwd
337 clearerr(files[fileIndex]); These two should be the same as rewind... */
338 *errorFlag = 0; /* Means EOF of file. */
339 return;
340 }
341
342 if (1 != nCheckR || ferror(files[fileIndex])) {
343 printf("readC: problem reading length of record, file %d\n", fileIndex);
344 *errorFlag = -2;
345 return;
346 }
347
348 if (recordLength < 0) {
349 doublePrec = 1;
350 recordLength = -recordLength;
351 }
352 if (recordLength / 2 > *lengthBuffers) {
353 /* printf("readC: given buffers too short (%d, need > %d)\n", *lengthBuffers,
354 recordLength/2); */
355 /* skip floats */
356 int i = 0;
357 if (doublePrec) {
358 for (; i < recordLength / 2; ++i) {
359 size_t nCheckD = fread(bufferDouble, sizeof(bufferDouble[0]), 1,
360 files[fileIndex]);
361 if (ferror(files[fileIndex]) || feof(files[fileIndex])
362 || nCheckD != *lengthBuffers) {
363 printf(
364 "readC: problem with stream or EOF skipping doubles\n");
365 *errorFlag = -32;
366 return;
367 }
368 }
369 } else {
370 for (; i < recordLength / 2; ++i) {
371 size_t nCheckF = fread(bufferFloat, sizeof(bufferFloat[0]), 1,
372 files[fileIndex]);
373 if (ferror(files[fileIndex]) || feof(files[fileIndex])
374 || nCheckF != *lengthBuffers) {
375 printf(
376 "readC: problem with stream or EOF skipping floats\n");
377 *errorFlag = -8;
378 return;
379 }
380 }
381 }
382 i = 0;
383 /* skip ints */
384 for (; i < recordLength / 2; ++i) {
385 size_t nCheckI = fread(bufferInt, sizeof(bufferInt[0]), 1,
386 files[fileIndex]);
387 if (ferror(files[fileIndex]) || feof(files[fileIndex])
388 || nCheckI != *lengthBuffers) {
389 printf("readC: problem with stream or EOF skiping ints\n");
390 *errorFlag = -16;
391 return;
392 }
393 }
394
395 *errorFlag = -4;
396 *lengthBuffers = recordLength / 2;
397 return;
398 } else {
399 *lengthBuffers = recordLength / 2;
400 }
401
402 /* read floats (i.e. derivatives + value + sigma) */
403 if (doublePrec) {
404 size_t nCheckD = fread(bufferDouble, sizeof(bufferDouble[0]),
405 *lengthBuffers, files[fileIndex]);
406 if (ferror(files[fileIndex]) || feof(files[fileIndex])
407 || nCheckD != *lengthBuffers) {
408 printf("readC: problem with stream or EOF reading doubles\n");
409 *errorFlag = -32;
410 return;
411 }
412 } else {
413 size_t nCheckF = fread(bufferFloat, sizeof(bufferFloat[0]),
414 *lengthBuffers, files[fileIndex]);
415 if (ferror(files[fileIndex]) || feof(files[fileIndex])
416 || nCheckF != *lengthBuffers) {
417 printf("readC: problem with stream or EOF reading floats\n");
418 *errorFlag = -8;
419 return;
420 }
421 int i = 0;
422 for (; i < recordLength / 2; ++i)
423 bufferDouble[i] = (double) bufferFloat[i];
424 }
425 /* read ints (i.e. parameter labels) */
426 size_t nCheckI = fread(bufferInt, sizeof(bufferInt[0]), *lengthBuffers,
427 files[fileIndex]);
428 if (ferror(files[fileIndex]) || feof(files[fileIndex])
429 || nCheckI != *lengthBuffers) {
430 printf("readC: problem with stream or EOF reading ints\n");
431 *errorFlag = -16;
432 return;
433 }
434#endif
435
436 *errorFlag = 4 * (doublePrec + 1);
437}
438FCALLSCSUB6(readC,READC,readc,PDOUBLE,PFLOAT,PINT,PINT,PINT,PINT)
#define FCALLSCSUB1(CN, UN, LN, T1)
Definition cfortran.h:2199
#define FCALLSCSUB6(CN, UN, LN, T1, T2, T3, T4, T5, T6)
Definition cfortran.h:2206
#define FCALLSCSUB3(CN, UN, LN, T1, T2, T3)
Definition cfortran.h:2201
void resetC(int *nFileIn)
Rewind file.
Definition readc.c:121
void closeC(int *nFileIn)
Close file.
Definition readc.c:140
void readC(double *bufferDouble, float *bufferFloat, int *bufferInt, int *lengthBuffers, int *nFileIn, int *errorFlag)
Read record from file.
Definition readc.c:220
void initC(int *nFiles)
Initialises the 'global' variables used for file handling.
Definition readc.c:86
void openC(const char *fileName, int *nFileIn, int *errorFlag)
Open file.
Definition readc.c:155
unsigned int maxNumFiles
max number of files
Definition readc.c:78
unsigned int numAllFiles
number of opened files
Definition readc.c:79
FILE ** files
pointer to list of pointers to opened binary files
Definition readc.c:75