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.cc
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_CC
30 #define LSST_AP_UTILS_CSV_CC
31 
32 #include "Csv.h"
33 
34 #include <limits>
35 
36 #include "boost/mpl/or.hpp"
37 #include "boost/static_assert.hpp"
38 #include "boost/type_traits/is_integral.hpp"
39 #include "boost/type_traits/is_floating_point.hpp"
40 #include "boost/type_traits/is_same.hpp"
41 
42 
43 namespace lsst { namespace ap { namespace utils {
44 
45 // -- CsvReader inline/template members ----
46 
49 inline CsvControl const & CsvReader::getControl() const {
50  return _control;
51 }
52 
57 inline size_t CsvReader::getNumLines() const {
58  return _numLines;
59 }
60 
64 inline size_t CsvReader::getNumRecords() const {
65  return _numRecords;
66 }
67 
71 inline std::vector<std::string> const & CsvReader::getFieldNames() const {
72  return _names;
73 }
74 
76 
79 inline int CsvReader::getIndexOf(std::string const &name) const {
80  FieldIndexes::const_iterator i = _indexes.find(name);
81  return i == _indexes.end() ? -1 : static_cast<int>(i->second);
82 }
83 inline int CsvReader::getIndexOf(char const *name) const {
84  return getIndexOf(std::string(name));
85 }
87 
90 inline bool CsvReader::isDone() const {
91  return _done;
92 }
93 
110 inline void CsvReader::nextRecord() {
111  if (!_done) {
112  _readRecord();
113  }
114 }
115 
119 inline int CsvReader::getNumFields() const {
120  return static_cast<int>(_fields.size());
121 }
122 
125 inline bool CsvReader::isNull(int i) const {
126  if (_done) {
127  _ioError("no current record (all records have been read)");
128  }
129  if (i < 0 || static_cast<size_t>(i) >= _fields.size()) {
130  _ioError("attempt to access field with invalid field name or index");
131  }
132  return _fields[i] < 0;
133 }
136 inline bool CsvReader::isNull(std::string const &name) const {
137  return isNull(getIndexOf(name));
138 }
141 inline bool CsvReader::isNull(char const *name) const {
142  return isNull(getIndexOf(name));
143 }
144 
147 template <typename T> inline T const CsvReader::get(int i) const {
148  // force a compile time error for unsupported types
149  static int const typeAllowed = boost::mpl::or_<
150  boost::is_same<T, char const *>,
151  boost::is_same<T, std::string>,
152  boost::is_integral<T>,
153  boost::is_floating_point<T>
154  >::value;
155  BOOST_STATIC_ASSERT(typeAllowed);
156  if (isNull(i)) {
157  return _null<T>();
158  }
159  // _get specializations live in the implementation file
160  return _get<T>(_record.get() + _fields[i]);
161 }
164 template <typename T> inline T const CsvReader::get(std::string const &name) const {
165  return get<T>(getIndexOf(name));
166 }
169 template <typename T> inline T const CsvReader::get(char const *name) const {
170  return get<T>(getIndexOf(name));
171 }
172 
175 inline std::string const CsvReader::get(int i) const {
176  return get<std::string>(i);
177 }
180 inline std::string const CsvReader::get(std::string const &name) const {
181  return get<std::string>(getIndexOf(name));
182 }
185 inline std::string const CsvReader::get(char const *name) const {
186  return get<std::string>(getIndexOf(name));
187 }
188 
189 // NULL values for various types
190 template <typename T> inline T CsvReader::_null() {
191  BOOST_STATIC_ASSERT(sizeof(T) == 0);
192 }
193 template <> inline std::string CsvReader::_null<std::string>() {
194  return std::string();
195 }
196 template <> inline char const * CsvReader::_null<char const *>() {
197  return 0;
198 }
199 template <> inline bool CsvReader::_null<bool>() {
200  return false;
201 }
202 template <> inline char CsvReader::_null<char>() {
203  return '\0';
204 }
205 
206 #define LSST_SPECIALIZE_NULL(T) \
207  template <> inline T CsvReader::_null<T>() { \
208  return std::numeric_limits<T>::min(); \
209  }
215 #undef LSST_SPECIALIZE_NULL
216 
217 #define LSST_SPECIALIZE_NULL(T) \
218  template <> inline T CsvReader::_null<T>() { \
219  return std::numeric_limits<T>::max(); \
220  }
221 LSST_SPECIALIZE_NULL(unsigned char)
222 LSST_SPECIALIZE_NULL(unsigned short)
223 LSST_SPECIALIZE_NULL(unsigned int)
224 LSST_SPECIALIZE_NULL(unsigned long)
225 LSST_SPECIALIZE_NULL(unsigned long long)
226 #undef LSST_SPECIALIZE_NULL
227 
228 #define LSST_SPECIALIZE_NULL(T) \
229  template <> inline T CsvReader::_null<T>() { \
230  return std::numeric_limits<T>::quiet_NaN(); \
231  }
235 #undef LSST_SPECIALIZE_NULL
236 
237 // Trivial _get implementations
238 template <>
239 inline char const * CsvReader::_get<char const *>(char const *field) const {
240  return field;
241 }
242 template <>
243 inline std::string CsvReader::_get<std::string>(char const *field) const {
244  return std::string(field);
245 }
246 
247 
248 // -- CsvWriter inlines ----
249 
252 inline CsvControl const & CsvWriter::getControl() const {
253  return _control;
254 }
255 
259 inline void CsvWriter::flush() {
260  _out->flush();
261 }
262 
265 inline size_t CsvWriter::getNumLines() const {
266  return _numLines;
267 }
268 
271 inline size_t CsvWriter::getNumRecords() const {
272  return _numRecords;
273 }
274 
277 inline size_t CsvWriter::getNumFields() const {
278  return _numFields;
279 }
280 
281 inline void CsvWriter::appendField(std::string const &v) {
282  _write(v.c_str());
283 }
284 
285 inline void CsvWriter::appendField(char const *v) {
286  if (v == 0) {
287  appendNull();
288  } else {
289  _write(v);
290  }
291 }
292 
293 inline void CsvWriter::appendField(bool v) {
294  _write(v ? "1" : "0");
295 }
296 
297 inline void CsvWriter::appendField(char v) {
298  char s[2];
299  s[0] = v;
300  s[1] = '\0';
301  _write(s);
302 }
303 
309 inline void CsvWriter::write(std::string const &s) {
310  _out->write(s.c_str(), s.size());
311 }
312 inline void CsvWriter::write(char c) {
313  _out->put(c);
314 }
316 
317 
318 // -- STL-style output for CsvWriter ----
319 
322 template <typename T>
323 inline CsvWriter & operator<<(CsvWriter &w, T const &v) {
324  w.appendField(v);
325  return w;
326 }
327 
330 inline CsvWriter & endr(CsvWriter &w) {
331  w.endRecord();
332  return w;
333 }
334 
338  w.flush();
339  return w;
340 }
341 
345  w.appendNull();
346  return w;
347 }
348 
351 template <typename T>
353  return manip(w);
354 }
355 
356 }}} // namespace lsst::ap::utils
357 
358 #endif // LSST_AP_UTILS_CSV_CC
void _write(char const *s)
Definition: Csv.cc:915
bool _done
Finished reading file?
Definition: Csv.h:216
size_t getNumLines() const
Definition: Csv.cc:57
void appendField(std::string const &v)
Definition: Csv.cc:281
size_t getNumLines() const
Definition: Csv.cc:265
Parameters that define a Character-Separated-Value dialect.
Definition: CsvControl.h:48
CsvWriter & endr(CsvWriter &w)
Definition: Csv.cc:330
size_t getNumRecords() const
Definition: Csv.cc:64
std::ostream * _out
Output stream.
Definition: Csv.h:310
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
boost::scoped_array< char > _record
Data for a single record.
Definition: Csv.h:214
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
std::string const get(int i) const
Definition: Csv.cc:147
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
size_t _numLines
Number of lines written.
Definition: Csv.h:313
double w
Definition: CoaddPsf.cc:57
bool isDone() const
Definition: Csv.cc:90
#define LSST_SPECIALIZE_NULL(T)
Definition: Csv.cc:228
CsvControl _control
File format.
Definition: Csv.h:311
CsvWriter & flush(CsvWriter &w)
Definition: Csv.cc:337
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
Classes for CSV I/O.
CsvControl const & getControl() const
Definition: Csv.cc:49
CsvControl const & getControl() const
Definition: Csv.cc:252