SND@LHC Software
Loading...
Searching...
No Matches
readc.c File Reference
#include <stdio.h>
#include "cfortran.h"
Include dependency graph for readc.c:

Go to the source code of this file.

Functions

void initC (int *nFiles)
 Initialises the 'global' variables used for file handling.
 
void resetC (int *nFileIn)
 Rewind file.
 
void closeC (int *nFileIn)
 Close file.
 
void openC (const char *fileName, int *nFileIn, int *errorFlag)
 Open file.
 
void readC (double *bufferDouble, float *bufferFloat, int *bufferInt, int *lengthBuffers, int *nFileIn, int *errorFlag)
 Read record from file.
 

Variables

FILE ** files
 pointer to list of pointers to opened binary files
 
unsigned int maxNumFiles
 max number of files
 
unsigned int numAllFiles
 number of opened files
 

Detailed Description

Read from C/C++ binary files.

Author
Gero Flucke, University Hamburg, 2006
Claus Kleinwort, DESY (maintenance and developement)

C-methods to handle input of C/C++ binary files as input for the fortran pede program (see peread). This includes macros utilising cfortran.h to allow direct callability from fortran.

initC() has to be called once in the beginning, followed by one or several calls to openC() to open one or several files. readC() is then called to read the records sequentially. resetC() allows to rewind files.

If compiled with preprocessor macro USE_SHIFT_RFIO, uses libRFIO, i.e. includes shift.h instead of stdio.h

If compiled with preprocessor macro USE_ZLIB, uses libz, enables direct reading of gzipped files.

Written by Gero Flucke (gero..nosp@m.fluc.nosp@m.ke@ce.nosp@m.rn.c.nosp@m.h) in 2006/7

  • update on July 14th, 2008
  • update on October 29th, 2008: return for file number in readC()

Major updates on April 24th, 2012 by C.Kleinwort:

  • skip records larger than buffer size (to determine max record length)
  • dynamic allocation of file pointer list (no hard-coded max number of files)

Major update on February 26th, 2014 by C.Kleinwort:

  • implement reading of records containing doubles (instead of floats) indicated by negative record length.

Last major update on April 10th, 2019 by C.Kleinwort:

  • Option to close and reopen files

Definition in file readc.c.

Function Documentation

◆ closeC()

void closeC ( int *  nFileIn)

Close file.

Parameters
[in]nFileInFile number (1 .. maxNumFiles)

Definition at line 140 of file readc.c.

140 {
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}
FILE ** files
pointer to list of pointers to opened binary files
Definition readc.c:75

◆ initC()

void initC ( int *  nFiles)

Initialises the 'global' variables used for file handling.

Parameters
[in]nFilesMaximal number of C binary files to use.

Definition at line 86 of file readc.c.

86 {
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}
int i
Definition ShipAna.py:86
unsigned int maxNumFiles
max number of files
Definition readc.c:78
unsigned int numAllFiles
number of opened files
Definition readc.c:79

◆ openC()

void openC ( const char *  fileName,
int *  nFileIn,
int *  errorFlag 
)

Open file.

Parameters
[in]fileNameFile name
[in]nFileInFile number (1 .. maxNumFiles) or <=0 for next one
[out]errorFlagerror flag:
  • 0: if file opened and OK,
  • 1: if too many files open,
  • 2: if file could not be opened
  • 3: if file opened, but with error (can that happen?)

Definition at line 155 of file readc.c.

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}

◆ readC()

void readC ( double *  bufferDouble,
float *  bufferFloat,
int *  bufferInt,
int *  lengthBuffers,
int *  nFileIn,
int *  errorFlag 
)

Read record from file.

Parameters
[out]bufferDoubleread buffer for doubles
[out]bufferFloatread buffer for floats
[out]bufferIntread buffer for integers
[in,out]lengthBuffersin: buffer length, out: number of floats/ints in records (> buffer size: record skipped)
[in]nFileInFile number (1 .. maxNumFiles)
[out]errorFlagerror flag:
  • -1: pointer to a buffer or lengthBuffers are null
  • -2: problem reading record length
  • -4: given buffers too short for record
  • -8: problem with stream or EOF reading floats
  • -16: problem with stream or EOF reading ints
  • -32: problem with stream or EOF reading doubles
  • =0: reached end of file (or read empty record?!)
  • =4: found floats
  • =8: found doubles

Definition at line 220 of file readc.c.

221 {
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}

◆ resetC()

void resetC ( int *  nFileIn)

Rewind file.

Parameters
[in]nFileInFile number (1 .. maxNumFiles)

Definition at line 121 of file readc.c.

121 {
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}

Variable Documentation

◆ files

FILE** files

pointer to list of pointers to opened binary files

Definition at line 75 of file readc.c.

◆ maxNumFiles

unsigned int maxNumFiles

max number of files

Definition at line 78 of file readc.c.

◆ numAllFiles

unsigned int numAllFiles

number of opened files

Definition at line 79 of file readc.c.