npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
vectorAsText.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_VECTORASTEXT_HH_
2 #define NPSTAT_VECTORASTEXT_HH_
3 
4 /*!
5 // \file vectorAsText.hh
6 //
7 // \brief Utilities for reading/writing std::vector objects from/to text files
8 //
9 // Author: I. Volobouev
10 //
11 // January 2023
12 */
13 
14 #include <iostream>
15 #include <sstream>
16 #include <string>
17 #include <vector>
18 #include <climits>
19 
20 namespace npstat {
21  /**
22  // Function for dumping vectors into text files, one element per line.
23  //
24  // Note that, while this function will work with T objects that do
25  // not have default constructors, it will not be possible to read
26  // such objects back.
27  //
28  // "true" is returned on success, "false" on failure.
29  // Dumping less than "nElementsToDump" elements is considered
30  // a success.
31  */
32  template <typename T>
33  bool dumpVectorAsText(const std::vector<T>& v,
34  std::ostream& asciiStream,
35  const unsigned long firstElementToDump=0,
36  const unsigned long nElementsToDump=ULONG_MAX)
37  {
38  if (nElementsToDump)
39  {
40  const unsigned long sz = v.size();
41  if (firstElementToDump < sz)
42  {
43  unsigned long ndumped = 0;
44  for (unsigned long i=firstElementToDump;
45  i<sz && ndumped<nElementsToDump; ++i, ++ndumped)
46  asciiStream << v[i] << '\n';
47  if (asciiStream.fail())
48  return false;
49  }
50  }
51  return true;
52  }
53 
54  /**
55  // Function for filling vectors from text files, one element per line.
56  // Will only work with T objects that have default constructors.
57  // "true" is returned on success, "false" on failure.
58  //
59  // Empty lines, lines which consist of pure white space, and lines
60  // which start with an arbitrary amount of white space (including
61  // none) followed by '#' are ignored (considered comments).
62  */
63  template <typename T>
64  bool fillVectorFromText(std::istream& asciiStream,
65  std::vector<T>* v,
66  const unsigned long maxElementsToFill=ULONG_MAX)
67  {
68  bool status = true;
69  if (maxElementsToFill && asciiStream)
70  {
71  assert(v);
72  std::string linebuf;
73  std::istringstream is;
74  unsigned long nfilled = 0;
75  T buffer;
76 
77  while (asciiStream && status && nfilled<maxElementsToFill)
78  {
79  std::getline(asciiStream, linebuf);
80  const unsigned long len = linebuf.size();
81  if (len == 0UL)
82  continue;
83 
84  // Ignore lines which are pure white space
85  // or which start with an arbitrary number
86  // of white space characters followed by #.
87  bool isComment = false;
88  bool allSpace = true;
89  char* line = &linebuf[0];
90 
91  for (unsigned long i=0; i<len && allSpace; ++i)
92  {
93  if (isspace(line[i]))
94  continue;
95  if (line[i] == '#')
96  isComment = true;
97  allSpace = false;
98  }
99  if (isComment || allSpace)
100  continue;
101 
102  is.str(linebuf);
103  is.clear();
104  is >> buffer;
105  if (is.fail())
106  status = false;
107  else
108  v->push_back(buffer);
109 
110  if ((asciiStream.fail() && !asciiStream.eof()) ||
111  asciiStream.bad())
112  status = false;
113  }
114  }
115  return status;
116  }
117 }
118 
119 #endif // NPSTAT_VECTORASTEXT_HH_
Definition: AbsArrayProjector.hh:14
bool fillVectorFromText(std::istream &asciiStream, std::vector< T > *v, const unsigned long maxElementsToFill=ULONG_MAX)
Definition: vectorAsText.hh:64
bool dumpVectorAsText(const std::vector< T > &v, std::ostream &asciiStream, const unsigned long firstElementToDump=0, const unsigned long nElementsToDump=ULONG_MAX)
Definition: vectorAsText.hh:33