LSSTApplications  1.1.2+25,10.0+13,10.0+132,10.0+133,10.0+224,10.0+41,10.0+8,10.0-1-g0f53050+14,10.0-1-g4b7b172+19,10.0-1-g61a5bae+98,10.0-1-g7408a83+3,10.0-1-gc1e0f5a+19,10.0-1-gdb4482e+14,10.0-11-g3947115+2,10.0-12-g8719d8b+2,10.0-15-ga3f480f+1,10.0-2-g4f67435,10.0-2-gcb4bc6c+26,10.0-28-gf7f57a9+1,10.0-3-g1bbe32c+14,10.0-3-g5b46d21,10.0-4-g027f45f+5,10.0-4-g86f66b5+2,10.0-4-gc4fccf3+24,10.0-40-g4349866+2,10.0-5-g766159b,10.0-5-gca2295e+25,10.0-6-g462a451+1
LSSTDataManagementBasePackage
Csv.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
29 #ifndef LSST_AP_UTILS_CSV_H
30 #define LSST_AP_UTILS_CSV_H
31 
32 #include <fstream>
33 #include <string>
34 #include <vector>
35 
36 #include "lsst/tr1/unordered_map.h"
37 
38 #include "boost/scoped_ptr.hpp"
39 #include "boost/scoped_array.hpp"
40 
41 #include "lsst/pex/config.h"
42 
43 #include "CsvControl.h"
44 
45 
46 namespace lsst { namespace ap { namespace utils {
47 
122 class CsvReader {
123 public:
124  CsvReader(std::string const &path,
125  CsvControl const &control,
126  bool namesInFirstRecord=false);
127  CsvReader(std::istream &in,
128  CsvControl const &control,
129  bool namesInFirstRecord=false);
130  ~CsvReader();
131 
132  inline CsvControl const & getControl() const;
133 
134  // Return the number of lines/records read
135  inline size_t getNumLines() const;
136  inline size_t getNumRecords() const;
137 
138  // Get/set field names
139  inline std::vector<std::string> const & getFieldNames() const;
140  void setFieldNames(std::vector<std::string> const & names);
141  void setFieldNames(std::string const & names,
142  std::string const & regex,
143  bool stripWhitespace=true);
144 
145  // Map field names to field indexes
146  inline int getIndexOf(std::string const &name) const;
147  inline int getIndexOf(char const *name) const;
148 
149  // Have all records been read?
150  inline bool isDone() const;
151 
152  // Advance to the next record
153  inline void nextRecord();
154 
156 
157  inline int getNumFields() const;
158 
159  inline bool isNull(int i) const;
160  inline bool isNull(std::string const &name) const;
161  inline bool isNull(char const *name) const;
162 
163  inline std::string const get(int i) const;
164  inline std::string const get(std::string const &name) const;
165  inline std::string const get(char const *name) const;
166 
167  // Get and convert a field value. Allowed types are std::string, bool,
168  // built-in integral and floating point types, and char const *. The
169  // pointers returned by get<char const *>() are invalidated by a call
170  // to nextRecord().
171  template <typename T> inline T const get(int i) const;
172  template <typename T> inline T const get(std::string const &name) const;
173  template <typename T> inline T const get(char const *name) const;
175 
176 private:
177  typedef std::tr1::unordered_map<std::string, int> FieldIndexes;
178 
179  static std::string const WHITESPACE;
180  static int const MAX_RECORD_LENGTH;
181  static int const DEFAULT_CAPACITY;
182 
183  enum State {
191  };
192 
193  // disable copy construction/assignment
194  CsvReader(CsvReader const &);
195  CsvReader & operator=(CsvReader const &);
196 
197  void _ioError(char const *msg) const;
198  void _runtimeError(char const *msg) const;
199  bool _readLine(int offset);
200  void _readRecord();
201  void _checkWhitespace(char const *s, char const *msg) const;
202  template <typename T> static inline T _null();
203  template <typename T> T _get(char const *value) const;
204 
205  std::string _path;
207  std::vector<std::string> _names;
209 
210  boost::scoped_ptr<std::ifstream> _stream;
211  std::istream *_in;
212  size_t _numLines;
213  size_t _numRecords;
214  boost::scoped_array<char> _record;
215  int _capacity;
216  bool _done;
217  std::vector<int> _fields;
218 };
220 
249 class CsvWriter {
250 public:
251  CsvWriter(std::string const &path,
252  CsvControl const &control,
253  bool truncate=false,
254  bool append=false);
255  CsvWriter(std::ostream &out,
256  CsvControl const &control);
257  ~CsvWriter();
258 
259  inline CsvControl const & getControl() const;
260 
261  // End the current record
262  void endRecord();
263 
264  // Flush the underlying output stream without ending the current record
265  inline void flush();
266 
267  // Return the number of lines/records/fields written
268  inline size_t getNumLines() const;
269  inline size_t getNumRecords() const;
270  inline size_t getNumFields() const;
271 
273 
274  void appendFields(std::vector<std::string> const &fields);
275  inline void appendField(std::string const &v);
276  inline void appendField(char const *v);
277  inline void appendField(bool v);
278  inline void appendField(char v);
279  void appendField(signed char v);
280  void appendField(short v);
281  void appendField(int v);
282  void appendField(long v);
283  void appendField(long long v);
284  void appendField(unsigned char v);
285  void appendField(unsigned short v);
286  void appendField(unsigned int v);
287  void appendField(unsigned long v);
288  void appendField(unsigned long long v);
289  void appendField(float v);
290  void appendField(double v);
291  void appendField(long double v);
292 
293  void appendNull();
295 
296  // Raw writes. Beware: these bypass all output formatting!
297  inline void write(std::string const &v);
298  inline void write(char c);
299 
300 private:
301  // disable copy construction/assignment
302  CsvWriter(CsvWriter const &);
303  CsvWriter & operator=(CsvWriter const &);
304 
305  void _write(char const *s);
306  void _writeQuoted(char const *s);
307  void _writeUnquoted(char const *s);
308 
309  boost::scoped_ptr<std::ofstream> _stream;
310  std::ostream *_out;
312  size_t _numRecords;
313  size_t _numLines;
314  size_t _numFields;
315 };
316 
317 // STL-style output for CsvWriter
318 inline CsvWriter & endr(CsvWriter &);
319 inline CsvWriter & flush(CsvWriter &);
320 inline CsvWriter & nullf(CsvWriter &);
321 template <typename T> inline CsvWriter & operator<<(CsvWriter &, T const &);
322 template <typename T> inline CsvWriter& operator<<(CsvWriter&, CsvWriter& (*)(CsvWriter&));
323 
324 }}} // namespace lsst::ap::utils
325 
326 #include "Csv.cc" // for inline/template members
327 
328 #endif // LSST_AP_UTILS_CSV_H
329 
void _write(char const *s)
Definition: Csv.cc:915
Extent< int, N > truncate(Extent< double, N > const &input)
bool _done
Finished reading file?
Definition: Csv.h:216
size_t getNumLines() const
Definition: Csv.cc:57
boost::scoped_ptr< std::ifstream > _stream
File stream.
Definition: Csv.h:210
void appendField(std::string const &v)
Definition: Csv.cc:281
size_t getNumLines() const
Definition: Csv.cc:265
static int const DEFAULT_CAPACITY
Definition: Csv.h:181
Parameters that define a Character-Separated-Value dialect.
Definition: CsvControl.h:48
CsvWriter & endr(CsvWriter &w)
Definition: Csv.cc:330
std::tr1::unordered_map< std::string, int > FieldIndexes
Definition: Csv.h:177
size_t getNumRecords() const
Definition: Csv.cc:64
static int const MAX_RECORD_LENGTH
Definition: Csv.h:180
std::ostream * _out
Output stream.
Definition: Csv.h:310
void setFieldNames(std::vector< std::string > const &names)
Definition: Csv.cc:167
std::istream * _in
Input stream.
Definition: Csv.h:211
void _ioError(char const *msg) const
Definition: Csv.cc:235
std::vector< std::string > const & getFieldNames() const
Definition: Csv.cc:71
CsvWriter & operator<<(CsvWriter &w, T const &v)
Definition: Csv.cc:323
size_t getNumRecords() const
Definition: Csv.cc:271
CsvWriter & operator=(CsvWriter const &)
void _writeUnquoted(char const *s)
Definition: Csv.cc:1029
boost::scoped_array< char > _record
Data for a single record.
Definition: Csv.h:214
int _capacity
Capacity of _record.
Definition: Csv.h:215
CsvWriter(std::string const &path, CsvControl const &control, bool truncate=false, bool append=false)
Definition: Csv.cc:649
size_t _numLines
1-based index of current line.
Definition: Csv.h:212
size_t _numFields
Number of fields written.
Definition: Csv.h:314
CsvControl _control
File format.
Definition: Csv.h:206
std::vector< int > _fields
Definition: Csv.h:217
size_t _numRecords
1-based index of current record.
Definition: Csv.h:213
size_t _numRecords
Number of records written.
Definition: Csv.h:312
bool isNull(int i) const
Definition: Csv.cc:125
Inlines for CSV I/O classes.
size_t _numLines
Number of lines written.
Definition: Csv.h:313
CSV format control.
void appendFields(std::vector< std::string > const &fields)
Definition: Csv.cc:728
void _writeQuoted(char const *s)
Definition: Csv.cc:977
CsvReader & operator=(CsvReader const &)
bool isDone() const
Definition: Csv.cc:90
void _checkWhitespace(char const *s, char const *msg) const
Definition: Csv.cc:533
T _get(char const *value) const
boost::scoped_ptr< std::ofstream > _stream
Output file stream.
Definition: Csv.h:309
CsvControl _control
File format.
Definition: Csv.h:311
CsvWriter & flush(CsvWriter &w)
Definition: Csv.cc:337
bool _readLine(int offset)
Definition: Csv.cc:262
static std::string const WHITESPACE
Definition: Csv.h:179
int getNumFields() const
Definition: Csv.cc:119
FieldIndexes _indexes
Field name to index map.
Definition: Csv.h:208
std::vector< std::string > _names
Field names in order of occurence.
Definition: Csv.h:207
size_t getNumFields() const
Definition: Csv.cc:277
CsvWriter & nullf(CsvWriter &w)
Definition: Csv.cc:344
void write(std::string const &v)
Definition: Csv.cc:309
int getIndexOf(std::string const &name) const
Definition: Csv.cc:79
void _runtimeError(char const *msg) const
Definition: Csv.cc:249
CsvReader(std::string const &path, CsvControl const &control, bool namesInFirstRecord=false)
Definition: Csv.cc:76
CsvControl const & getControl() const
Definition: Csv.cc:49
CsvControl const & getControl() const
Definition: Csv.cc:252
std::string _path
File name.
Definition: Csv.h:205