Memosa-FVM  0.2
Reader.cpp
Go to the documentation of this file.
1 // This file os part of FVM
2 // Copyright (c) 2012 FVM Authors
3 // See LICENSE file for terms.
4 
5 #include "Reader.h"
6 #include <errno.h>
7 #include <stdlib.h>
8 
9 #ifdef FVM_PARALLEL
10 #include <mpi.h>
11 #endif
12 
13 
14 Reader::Reader(const string& fileName) :
15  _fileName(fileName),
16  _fp(0)
17 {
18  resetFilePtr();
19 }
20 
22 {}
23 
24 
25 
26 void
28 {
29  if (_fp)
30  fclose(_fp);
31 
32  _fp = NULL;
33  int numOfAttempts = 10;
34  while( _fp == NULL && numOfAttempts > 0){
35  _fp = fopen(_fileName.c_str(),"rb");
36  if (_fp == NULL)
37  printf("Error opening file %s: errno=%d %m", _fileName.c_str(), errno);
38  numOfAttempts--;
39  }
40 
41  if ( _fp == NULL){
42  //parallel
43  #ifdef FVM_PARALLEL
44  const int mpi_err=99;
45  MPI::COMM_WORLD.Abort(mpi_err);
46  #endif
47  //serial
48  #ifndef FVM_PARALLEL
49  abort();
50  #endif
51  }
52 }
53 
54 string
56 {
57  char buf[256];
58  fgets(buf,256,_fp);
59  string s(buf);
60  return s;
61 }
62 
63 void
65 {
66  if (_fp)
67  fclose(_fp);
68 }
void resetFilePtr()
Definition: Reader.cpp:27
Reader(const string &fileName)
Definition: Reader.cpp:14
FILE * _fp
Definition: Reader.h:26
const string _fileName
Definition: Reader.h:25
string readLine()
Definition: Reader.cpp:55
virtual ~Reader()
Definition: Reader.cpp:21
void close()
Definition: Reader.cpp:64