aimstil  5.0.5
io.h
Go to the documentation of this file.
1 #ifndef TIL_IO_H_
2 #define TIL_IO_H_
3 
4 // includes from STL
5 #include <exception>
6 #include <fstream>
7 #include <sstream> // std::istringstream
8 #include <utility> // std::pair
9 
10 
11 // includes from TIL
12 #include "progress_indicator.h"
13 
14 namespace til { namespace io
15 {
16 
17  //---------------------------------------------------------------------------
18 
19  //---------------------//
20  // GetMatrixFileSize //
21  //---------------------//
22 
25  {
26  public: // exceptions
27 
28  class CannotOpenFile : public std::exception {};
29 
30  public: // constructors
31 
32  explicit GetArraySize(std::size_t maxlinelength);
33 
34  public: // set & get
35 
36  void setMaxLineLength(std::size_t mll) { m_buffer.resize(mll); }
38  std::size_t nlines() const { return m_linecount; }
40  std::size_t ncolumns() const { return m_columncount; }
41 
42  public: // operators
43 
46  void operator()(const char * filename);
47 
49  void operator()(const std::string & filename) { (*this)(filename.c_str()); }
50 
51  private: // data, output
52  std::size_t m_linecount, m_columncount;
53 
54  private: // data, internal
55  std::string m_buffer;
56  };
57 
58  //---------------------------------------------------------------------------
59 
61  std::pair<std::size_t, std::size_t>
62  get_array_size(const char * filename, std::size_t maxLineLength = 65536);
63 
64  //---------------------------------------------------------------------------
65 
66  //----------------//
67  // SimpleLoader //
68  //----------------//
69 
70  template < typename TIterator, typename TProgressIndicator = NoIndicator >
72  {
73  public: // exceptions
74 
75  class CannotOpenFile : public std::exception {};
76 
77  public: // constructors
78 
80  // TODO: somehow we should check when this max number is reached...
81  explicit SimpleLoader(std::size_t maxlinelength)
82  {
83  m_buffer.resize(maxlinelength);
84  }
85 
86  public: // set & get
87 
88  TProgressIndicator & progress_indicator() { return m_indicator; }
89 
90  public: // operators
91 
94  void operator()(const char * filename, TIterator begin);
95 
98  void operator()(const std::string & filename, TIterator begin);
99 
100  private: // data, input
101 
102  TProgressIndicator m_indicator;
103 
104  private: // data, internal
105 
106  std::string m_buffer;
107  };
108 
109 
110  template < typename TIterator >
111  void simple_load(const char * filename, TIterator begin, std::size_t maxlinelength = 65536)
112  {
113  SimpleLoader<TIterator> loader(maxlinelength);
114  loader(filename, begin);
115  }
116 
117 
118  //----------------------------------------------------------------------------------------
119 
120 
121  //---------------//
122  // ArrayWriter //
123  //---------------//
124 
125  /*
126  class ArrayWriter
127  {
128  public: // operators
129 
130  void operator()(const char * filename)
131  };
132 
133  {
134  // convert from list to vector
135  std::vector<std::size_t> res(linecount);
136  std::copy(sets.front().begin(), sets.front().end(), res.begin());
137 
138  std::ofstream f(matoutname.c_str());
139  if (!f) std::cerr << "Cannot open file " << matoutname << std::endl;
140  else
141  {
142  til::DelayedProgressIndicator indicator(10, linecount);
143  for (std::size_t i = 0; i < linecount; ++i)
144  {
145  indicator(i);
146  for (std::size_t j = 0; j < linecount; ++j)
147  {
148  f << mat[res[i] * linecount + res[j]] << " ";
149  }
150  f << std::endl;
151  }
152  }
153  }
154  */
155 
156 
157 }} // namespace til::io
158 
159 // package include
160 #include "io.tpp"
161 
162 #endif /*IO_H_*/
GetArraySize(std::size_t maxlinelength)
SimpleLoader(std::size_t maxlinelength)
Construct object, giving maximum length, in characters, of a line on file.
Definition: io.h:81
std::pair< std::size_t, std::size_t > get_array_size(const char *filename, std::size_t maxLineLength=65536)
Returns the number of row and columns in an array file.
TProgressIndicator & progress_indicator()
Definition: io.h:88
void operator()(const char *filename)
Scan file to extract size information.
void setMaxLineLength(std::size_t mll)
Definition: io.h:36
Belongs to package Box Do not include directly, include til/Box.h instead.
Definition: Accumulator.h:10
std::size_t nlines() const
Returns the number of lines.
Definition: io.h:38
Return the number of row and columns of an array in a text file.
Definition: io.h:24
std::size_t ncolumns() const
Returns the number of columns.
Definition: io.h:40
void operator()(const std::string &filename)
Scan file to extract size information.
Definition: io.h:49
void simple_load(const char *filename, TIterator begin, std::size_t maxlinelength=65536)
Definition: io.h:111