OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
FileStream.cpp
Go to the documentation of this file.
1 // ------------------------------------------------------------------------
2 // $RCSfile: FileStream.cpp,v $
3 // ------------------------------------------------------------------------
4 // $Revision: 1.2 $
5 // ------------------------------------------------------------------------
6 // Copyright: see Copyright.readme
7 // ------------------------------------------------------------------------
8 //
9 // Class: FileStream
10 // Implements an input buffer for reading from a file.
11 //
12 // ------------------------------------------------------------------------
13 // Class category: Parser
14 // ------------------------------------------------------------------------
15 //
16 // $Date: 2001/08/24 19:33:11 $
17 // $Author: jsberg $
18 //
19 // ------------------------------------------------------------------------
20 
21 #include "Parser/FileStream.h"
22 #include "Utilities/ParseError.h"
23 #include "Utility/IpplInfo.h"
24 #include <iomanip>
25 #include <iostream>
26 
27 // Class FileStream
28 // ------------------------------------------------------------------------
29 
30 bool FileStream::echoFlag = false;
31 
32 
33 FileStream::FileStream(const std::string &name):
35  is(name.c_str()) {
36  if(is.fail()) {
37  throw ParseError("FileStream::FileStream()",
38  "Cannot open file \"" + name + "\".");
39  }
40 }
41 
42 
44 {}
45 
46 
47 void FileStream::setEcho(bool flag) {
48  echoFlag = flag;
49 }
50 
51 
53  return echoFlag;
54 }
55 
56 
58  if(is.eof()) {
59  // End of file.
60  return false;
61  } else if(is.bad()) {
62  // Read error.
63  throw ParseError("FileStream::fillLine()",
64  "Read error on file \"" + stream_name + "\".");
65  } else {
66  // Stream OK, read next line.
67  line = "";
68  std::getline(is, line, '\n');
69  line += "\n";
70  curr_line++;
71  if(echoFlag && Ippl::myNode() == 0) {
72  std::cerr.width(5);
73  std::cerr << curr_line << " " << line;
74  }
75  curr_char = 0;
76  return true;
77  }
78 }
const std::string name
A stream of input tokens.
Definition: AbsFileStream.h:31
std::string line
Definition: AbsFileStream.h:63
static bool getEcho()
Return echo flag.
Definition: FileStream.cpp:52
std::ifstream is
Definition: FileStream.h:63
virtual ~FileStream()
Destructor.
Definition: FileStream.cpp:43
static void setEcho(bool flag)
Set echo flag.
Definition: FileStream.cpp:47
static bool echoFlag
Definition: FileStream.h:66
virtual bool fillLine()
Read next input line.
Definition: FileStream.cpp:57
std::string stream_name
Definition: TokenStream.h:59
Parse exception.
Definition: ParseError.h:32
static int myNode()
Definition: IpplInfo.cpp:691