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