ATLAS Offline Software
Public Member Functions | Static Public Member Functions | Private Attributes | Static Private Attributes | List of all members
AsciiInput Class Reference

A class to provide read access to an ASCII input file. More...

#include <AsciiInput.h>

Collaboration diagram for AsciiInput:

Public Member Functions

 AsciiInput (const std::string &fileName)
 
 ~AsciiInput ()
 
int open ()
 Open the input file. More...
 
int close ()
 Close the input file. More...
 
std::vector< std::string > readRow ()
 Read one line of the input text file into a vector of strings. More...
 

Static Public Member Functions

static std::vector< std::string > strToStrVec (const std::string &inputString)
 A helper function to convert a string into a string vector, by using any number of space or tab characters as separators. More...
 
static long strToLong (const std::string &inputString)
 A helper function to convert a string to a long value. More...
 
static double strToDouble (const std::string &inputString)
 A helper function to convert a string to a double value. More...
 

Private Attributes

std::string m_fileName
 Input file name. More...
 
std::ifstream m_file
 Input file stream. More...
 
char m_lineBuffer [MAX_LINE_LENGTH]
 Character buffers used while parsing the input file. More...
 

Static Private Attributes

static const int MAX_LINE_LENGTH = 500
 Size of the character buffers. More...
 

Detailed Description

A class to provide read access to an ASCII input file.

Author
W. H. Bell W.Bel.nosp@m.l@ce.nosp@m.rn.ch

Definition at line 18 of file AsciiInput.h.

Constructor & Destructor Documentation

◆ AsciiInput()

AsciiInput::AsciiInput ( const std::string &  fileName)

Definition at line 9 of file AsciiInput.cxx.

10  m_file() {
11  for(int i=0; i< MAX_LINE_LENGTH;i++ )
12  m_lineBuffer[i]='\0';
13 }

◆ ~AsciiInput()

AsciiInput::~AsciiInput ( )

Definition at line 17 of file AsciiInput.cxx.

17  {
18 }

Member Function Documentation

◆ close()

int AsciiInput::close ( )

Close the input file.

A non-zero value is returned if this is not successful.

Definition at line 32 of file AsciiInput.cxx.

32  {
33  if(m_file.is_open()) {
34  m_file.close();
35  }
36 
37  return 0;
38 }

◆ open()

int AsciiInput::open ( )

Open the input file.

A non-zero value is returned if this is not successful.

Definition at line 22 of file AsciiInput.cxx.

22  {
23  m_file.open(m_fileName.c_str());
24  if(!m_file) {
25  std::cerr << "Error: could not open " << m_fileName << std::endl;
26  return 1;
27  }
28 
29  return 0;
30 }

◆ readRow()

std::vector< std::string > AsciiInput::readRow ( )

Read one line of the input text file into a vector of strings.

Any number of space or tab characters are used as separators. If the row contains only separators or the end of file has been reached the returned vector will be empty.

Definition at line 42 of file AsciiInput.cxx.

42  {
43  std::vector<std::string> row;
44 
45  // Check if the end of file has been reached.
46  if(m_file.eof()) {
47  std::cout << "End of file reached." << std::endl;
48  return row;
49  }
50 
51  // Read one line from the input file. (Need one additional character
52  // for string termination of a word.)
54  if(m_file.eof()) { // True when normally reaching the end of the file.
55  return row;
56  }
57 
58  // Convert the cstring to a string.
59  std::string tmpString(m_lineBuffer);
60 
61  // Split the string into substrings.
62  return strToStrVec(tmpString);
63 }

◆ strToDouble()

double AsciiInput::strToDouble ( const std::string &  inputString)
static

A helper function to convert a string to a double value.

Definition at line 122 of file AsciiInput.cxx.

122  {
123  double doubleValue;
124  std::istringstream inStr(inputString);
125  inStr >> doubleValue;
126  return doubleValue;
127 }

◆ strToLong()

long AsciiInput::strToLong ( const std::string &  inputString)
static

A helper function to convert a string to a long value.

Definition at line 113 of file AsciiInput.cxx.

113  {
114  long longValue;
115  std::istringstream inStr(inputString);
116  inStr >> longValue;
117  return longValue;
118 }

◆ strToStrVec()

std::vector< std::string > AsciiInput::strToStrVec ( const std::string &  inputString)
static

A helper function to convert a string into a string vector, by using any number of space or tab characters as separators.

Definition at line 67 of file AsciiInput.cxx.

67  {
68  std::vector<std::string> strVec;
69  std::string tmpString;
70  size_t stringLength, i;
71 
72  stringLength = inputString.length();
73 
74  // Loop over all characters in the string.
75  i=0;
76  while(i<stringLength) {
77 
78  // Skip over any white spaces at the start of the string.
79  while(inputString[i] == ' ' ||
80  inputString[i] == '\t') {
81  i++;
82  if(i >= stringLength) break;
83  }
84 
85  if(i>=stringLength) continue;
86 
87  // Copy all non-white space characters until a white space, end of string or comment character.
88  tmpString.clear();
89  while(inputString[i] != ' ' &&
90  inputString[i] != '\t' &&
91  inputString[i] != '#') {
92  tmpString.push_back(inputString[i]);
93  i++;
94  if(i >= stringLength) break;
95  }
96 
97  // Push the string back if its length is greater than zero.
98  if(tmpString.length() > 0) {
99  strVec.push_back(tmpString);
100  }
101 
102  // Check if a comment was found.
103  if(i < stringLength) {
104  if(inputString[i] == '#') break;
105  }
106  }
107 
108  return strVec;
109 }

Member Data Documentation

◆ m_file

std::ifstream AsciiInput::m_file
private

Input file stream.

Definition at line 53 of file AsciiInput.h.

◆ m_fileName

std::string AsciiInput::m_fileName
private

Input file name.

Definition at line 50 of file AsciiInput.h.

◆ m_lineBuffer

char AsciiInput::m_lineBuffer[MAX_LINE_LENGTH]
private

Character buffers used while parsing the input file.

Definition at line 59 of file AsciiInput.h.

◆ MAX_LINE_LENGTH

const int AsciiInput::MAX_LINE_LENGTH = 500
staticprivate

Size of the character buffers.

Definition at line 56 of file AsciiInput.h.


The documentation for this class was generated from the following files:
query_example.row
row
Definition: query_example.py:24
FortranAlgorithmOptions.fileName
fileName
Definition: FortranAlgorithmOptions.py:13
lumiFormat.i
int i
Definition: lumiFormat.py:92
AsciiInput::m_lineBuffer
char m_lineBuffer[MAX_LINE_LENGTH]
Character buffers used while parsing the input file.
Definition: AsciiInput.h:59
AsciiInput::MAX_LINE_LENGTH
static const int MAX_LINE_LENGTH
Size of the character buffers.
Definition: AsciiInput.h:56
AsciiInput::strToStrVec
static std::vector< std::string > strToStrVec(const std::string &inputString)
A helper function to convert a string into a string vector, by using any number of space or tab chara...
Definition: AsciiInput.cxx:67
AsciiInput::m_file
std::ifstream m_file
Input file stream.
Definition: AsciiInput.h:53
AsciiInput::m_fileName
std::string m_fileName
Input file name.
Definition: AsciiInput.h:50