ATLAS Offline Software
AsciiInput.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 #include <iostream>
7 #include <sstream>
8 
9 AsciiInput::AsciiInput(const std::string& fileName): m_fileName(fileName),
10  m_file() {
11  for(int i=0; i< MAX_LINE_LENGTH;i++ )
12  m_lineBuffer[i]='\0';
13 }
14 
15 //-------------------------------------------------------------------------
16 
18 }
19 
20 //-------------------------------------------------------------------------
21 
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 }
31 
33  if(m_file.is_open()) {
34  m_file.close();
35  }
36 
37  return 0;
38 }
39 
40 //-------------------------------------------------------------------------
41 
42 std::vector<std::string> AsciiInput::readRow() {
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 }
64 
65 //-------------------------------------------------------------------------
66 
67 std::vector<std::string> AsciiInput::strToStrVec(const std::string& inputString) {
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 }
110 
111 //-------------------------------------------------------------------------
112 
113 long AsciiInput::strToLong(const std::string& inputString) {
114  long longValue;
115  std::istringstream inStr(inputString);
116  inStr >> longValue;
117  return longValue;
118 }
119 
120 //-------------------------------------------------------------------------
121 
122 double AsciiInput::strToDouble(const std::string& inputString) {
123  double doubleValue;
124  std::istringstream inStr(inputString);
125  inStr >> doubleValue;
126  return doubleValue;
127 }
query_example.row
row
Definition: query_example.py:24
AsciiInput::AsciiInput
AsciiInput(const std::string &fileName)
Definition: AsciiInput.cxx:9
AsciiInput.h
AsciiInput::~AsciiInput
~AsciiInput()
Definition: AsciiInput.cxx:17
AsciiInput::readRow
std::vector< std::string > readRow()
Read one line of the input text file into a vector of strings.
Definition: AsciiInput.cxx:42
m_file
std::unique_ptr< TFile > m_file
description: this is a custom writer for the old-school drivers that don't use an actual writer
Definition: OutputStreamData.cxx:52
AsciiInput::close
int close()
Close the input file.
Definition: AsciiInput.cxx:32
FortranAlgorithmOptions.fileName
fileName
Definition: FortranAlgorithmOptions.py:13
lumiFormat.i
int i
Definition: lumiFormat.py:85
AsciiInput::strToLong
static long strToLong(const std::string &inputString)
A helper function to convert a string to a long value.
Definition: AsciiInput.cxx:113
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::open
int open()
Open the input file.
Definition: AsciiInput.cxx:22
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
AsciiInput::strToDouble
static double strToDouble(const std::string &inputString)
A helper function to convert a string to a double value.
Definition: AsciiInput.cxx:122