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
Public Member Functions | Private Member Functions | Private Attributes | List of all members
lsst::ap::utils::CsvWriter Class Reference

#include <Csv.h>

Public Member Functions

 CsvWriter (std::string const &path, CsvControl const &control, bool truncate=false, bool append=false)
 
 CsvWriter (std::ostream &out, CsvControl const &control)
 
 ~CsvWriter ()
 
CsvControl const & getControl () const
 
void endRecord ()
 
void flush ()
 
size_t getNumLines () const
 
size_t getNumRecords () const
 
size_t getNumFields () const
 
Append field(s) to the end of a record
void appendFields (std::vector< std::string > const &fields)
 
void appendField (std::string const &v)
 
void appendField (char const *v)
 
void appendField (bool v)
 
void appendField (char v)
 
void appendField (signed char v)
 
void appendField (short v)
 
void appendField (int v)
 
void appendField (long v)
 
void appendField (long long v)
 
void appendField (unsigned char v)
 
void appendField (unsigned short v)
 
void appendField (unsigned int v)
 
void appendField (unsigned long v)
 
void appendField (unsigned long long v)
 
void appendField (float v)
 
void appendField (double v)
 
void appendField (long double v)
 
void appendNull ()
 
void write (std::string const &v)
 
void write (char c)
 

Private Member Functions

 CsvWriter (CsvWriter const &)
 
CsvWriteroperator= (CsvWriter const &)
 
void _write (char const *s)
 
void _writeQuoted (char const *s)
 
void _writeUnquoted (char const *s)
 

Private Attributes

boost::scoped_ptr< std::ofstream > _stream
 Output file stream. More...
 
std::ostream * _out
 Output stream. More...
 
CsvControl _control
 File format. More...
 
size_t _numRecords
 Number of records written. More...
 
size_t _numLines
 Number of lines written. More...
 
size_t _numFields
 Number of fields written. More...
 

Detailed Description

A class for writing fields and records to Character-Separated-Value files in sequential order.

Field Output

Methods are provided to output strings, bool, signed/unsigned integers, floating point types, and database NULLs. Integers are always output in base 10, and booleans are output as "1" (true) or "0". The precision of the decimal output for floating point numbers is such that the original binary value can be recovered exactly if the systems float to decimal conversion routines are correctly rounded and the input/output rounding modes agree.

Exception Safety

This class provides the minimal exception safety guarantee - that is, exceptions will not cause crashes or leaks, and one can technically continue using the writer. However, the CSV files written under these conditions are not guaranteed to be legal. For example, partially written fields may be produced by a writer when raising an exception.

Limitations

Definition at line 249 of file Csv.h.

Constructor & Destructor Documentation

lsst::ap::utils::CsvWriter::CsvWriter ( std::string const &  path,
CsvControl const &  control,
bool  truncate = false,
bool  append = false 
)

Creates a new CsvWriter that outputs to the given file.

Parameters
pathName of file to write to.
controlCSV format of the output file.
truncateIf true, an existing file is truncated. Otherwise, an attempt to create a writer for an existing file raises an exception unless append is true.
appendIf true, append to an existing file. Has no effect if truncate is true.

Definition at line 649 of file Csv.cc.

658  :
659  _stream(),
660  _out(0),
661  _control(control),
662  _numRecords(0),
663  _numLines(0),
664  _numFields(0)
665 {
666  ios::openmode mode = ios::out | ios::binary;
667  if (!truncate && boost::filesystem::exists(path)) {
668  if (append) {
669  mode |= ios::app;
670  } else {
671  throw LSST_EXCEPT(pexExcept::IoError,
672  "file " + path + " already exists");
673  }
674  }
675  if (truncate) {
676  mode |= ios::trunc;
677  }
678  _stream.reset(new ofstream(path.c_str(), mode));
679  if (!_stream->good()) {
680  throw LSST_EXCEPT(pexExcept::IoError,
681  "failed to open file " + path + " for writing");
682  }
683  _out = _stream.get();
684  // throw on any kind of output error
685  _stream->exceptions(ios::eofbit | ios::failbit | ios::badbit);
686 }
Extent< int, N > truncate(Extent< double, N > const &input)
std::ostream * _out
Output stream.
Definition: Csv.h:310
size_t _numFields
Number of fields written.
Definition: Csv.h:314
size_t _numRecords
Number of records written.
Definition: Csv.h:312
size_t _numLines
Number of lines written.
Definition: Csv.h:313
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
boost::scoped_ptr< std::ofstream > _stream
Output file stream.
Definition: Csv.h:309
CsvControl _control
File format.
Definition: Csv.h:311
def exists
Definition: cuda.py:53
lsst::ap::utils::CsvWriter::CsvWriter ( std::ostream &  out,
CsvControl const &  control 
)

Creates a new CsvWriter that sends output to the given std::ostream.

This std::ostream must be kept alive for the life-time of the CsvWriter. If it is externally modified while the writer is alive (e.g. its exception mask is changed, or external writes are performed on it), then the behaviour and output of the writer is undefined.

Definition at line 695 of file Csv.cc.

695  :
696  _stream(),
697  _out(&out),
698  _control(control),
699  _numRecords(0),
700  _numLines(0),
701  _numFields(0)
702 {
703  if (!out.good()) {
704  throw LSST_EXCEPT(pexExcept::IoError,
705  "std::ostream not good() for writing");
706  }
707  out.exceptions(ios::eofbit | ios::failbit | ios::badbit);
708 }
std::ostream * _out
Output stream.
Definition: Csv.h:310
size_t _numFields
Number of fields written.
Definition: Csv.h:314
size_t _numRecords
Number of records written.
Definition: Csv.h:312
size_t _numLines
Number of lines written.
Definition: Csv.h:313
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
boost::scoped_ptr< std::ofstream > _stream
Output file stream.
Definition: Csv.h:309
CsvControl _control
File format.
Definition: Csv.h:311
lsst::ap::utils::CsvWriter::~CsvWriter ( )

Definition at line 710 of file Csv.cc.

710  {
711  _out = 0;
712 }
std::ostream * _out
Output stream.
Definition: Csv.h:310
lsst::ap::utils::CsvWriter::CsvWriter ( CsvWriter const &  )
private

Member Function Documentation

void lsst::ap::utils::CsvWriter::_write ( char const *  s)
private

Appends a field to the current record.

Definition at line 915 of file Csv.cc.

915  {
916  // output leading delimiter except for the first field in a record
917  if (_numFields > 0) {
918  _out->put(_control.getDelimiter());
919  }
920  ++_numFields;
921  if (_control.quoting == "QUOTE_NONE") {
922  if (_control.hasNull && _control.null == s) {
923  throw LSST_EXCEPT(pexExcept::RuntimeError,
924  "Field value coincides with NULL string "
925  "and quoting is disabled");
926  }
927  _writeUnquoted(s);
928  return;
929  } else if (_control.quoting == "QUOTE_ALL" ||
930  (_control.hasNull && _control.null == s)) {
931  _writeQuoted(s);
932  return;
933  }
934 
935  // minimal quoting mode - first determine whether to quote/escape
936  size_t n = 0;
937  bool wantEscape = false;
938  bool wantQuote = false;
939  for (char c = *s; c != '\0'; c = s[++n]) {
940  if (c == '\n' || c == '\r') {
942  wantEscape = true;
943  } else {
944  wantQuote = true;
945  }
946  } else if (c == _control.getDelimiter()) {
947  wantQuote = true;
948  } else if (c == _control.getEscapeChar()) {
949  wantQuote = true;
950  wantEscape = true;
951  } else if (c == _control.getQuoteChar()) {
952  if (_control.doubleQuote) {
953  wantQuote = true;
954  }
955  wantEscape = true;
956  }
957  }
958  // if no escapes are necessary, blast chars to output stream, otherwise
959  // delegate to _writeQuoted() and _writeUnquoted()
960  if (wantQuote) {
961  if (wantEscape) {
962  _writeQuoted(s);
963  } else {
964  _out->put(_control.getQuoteChar());
965  _out->write(s, n);
966  _out->put(_control.getQuoteChar());
967  }
968  } else if (wantEscape) {
969  _writeUnquoted(s);
970  } else {
971  _out->write(s, n);
972  }
973 }
char getDelimiter() const
Definition: CsvControl.h:155
char getEscapeChar() const
Definition: CsvControl.h:158
char getQuoteChar() const
Definition: CsvControl.h:161
std::ostream * _out
Output stream.
Definition: Csv.h:310
void _writeUnquoted(char const *s)
Definition: Csv.cc:1029
std::string null
&quot;String representation of NULL field values. Never quoted on output.\n&quot; &quot;If specified, the representation may not contain any delimiter,\n&quot; &quot;quote, escape or line terminator characters (&#39;\\n&#39;/&#39;\\r&#39;).\n&quot; ;
Definition: CsvControl.h:61
size_t _numFields
Number of fields written.
Definition: Csv.h:314
void _writeQuoted(char const *s)
Definition: Csv.cc:977
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
CsvControl _control
File format.
Definition: Csv.h:311
std::string quoting
&quot;Field quoting style for CSV input/output. Legal values are:\n&quot; &quot;\n&quot; &quot;&#39;QUOTE_MINIMAL&#39;: Only quot...
Definition: CsvControl.h:77
bool hasNull
&quot;Indicates whether the null string is valid. If set to false, the only\n&quot; &quot;way NULLs can be recogn...
Definition: CsvControl.h:67
bool standardEscapes
&quot;Flag indicating whether standard escape sequences should be handled.\n&quot; &quot;If false, then the character sequence &#39;\\C&#39;, where C is any character,\n&quot; &quot;is mapped to C (assuming &#39;\\&#39; is the escape character). If true,\n&quot; &quot;the following special cases are handled differently:\n&quot; &quot;\n&quot; &quot;- &#39;\\b&#39; is mapped to BS - backspace (ASCII 8)\n&quot; &quot;- &#39;\\f&#39; is mapped to FF - form feed (ASCII 12)\n&quot; &quot;- &#39;\\n&#39; is mapped to NL - newline (ASCII 10)\n&quot; &quot;- &#39;\\r&#39; is mapped to CR - carriage return (ASCII 13)\n&quot; &quot;- &#39;\\t&#39; is mapped to TAB - horizontal tab (ASCII 9)\n&quot; &quot;- &#39;\\v&#39; is mapped to VT - vertical tab (ASCII 11)\n&quot; &quot;- &#39;\\xD&#39; and &#39;\\xDD&#39;, where D is a hexadecimal digit, is mapped to\n&quot; &quot; the character with that numeric code.\n&quot; &quot;- A field value of exactly &#39;\\N&#39; (no quotes, whitespace, or other\n&quot; &quot; content) is treated as a NULL.\n&quot; ;
Definition: CsvControl.h:123
bool doubleQuote
&quot;If true, embedded quote characters are escaped with a leading quote\n&quot; &quot;character. Otherwise the escape character is used. If escaping and\n&quot; &quot;double-quoting are disabled, writing a field with embedded quote\n&quot; &quot;character will raise an exception.\n&quot; ;
Definition: CsvControl.h:106
void lsst::ap::utils::CsvWriter::_writeQuoted ( char const *  s)
private

Writes quoted field data, handling any required escaping.

Definition at line 977 of file Csv.cc.

977  {
978  _out->put(_control.getQuoteChar());
979  size_t n = 0;
980  while (true) {
981  char const c = s[n];
982  if (c == '\0') {
983  _out->write(s, n);
984  break;
985  }
986  if (c == _control.getQuoteChar()) {
987  _out->write(s, n);
988  s += n + 1;
989  n = 0;
990  if (_control.doubleQuote) {
991  _out->put(c);
992  _out->put(c);
993  } else if (_control.getEscapeChar() == '\0') {
994  _out->put(c);
995  throw LSST_EXCEPT(pexExcept::InvalidParameterError,
996  "Field value requires escaping, but "
997  "no escape character is set");
998  } else {
999  _out->put(_control.getEscapeChar());
1000  _out->put(c);
1001  }
1002  } else if (c == _control.getEscapeChar()) {
1003  _out->write(s, n);
1004  s += n + 1;
1005  n = 0;
1006  _out->put(c);
1007  _out->put(c);
1008  } else if (_control.standardEscapes && (c == '\n' || c == '\r')) {
1009  _out->write(s, n);
1010  s += n + 1;
1011  n = 0;
1012  if (_control.getEscapeChar() == '\0') {
1013  _out->put(_control.getQuoteChar());
1014  throw LSST_EXCEPT(pexExcept::InvalidParameterError,
1015  "Field value requires escaping, but "
1016  "no escape character is set");
1017  }
1018  _out->put(_control.getEscapeChar());
1019  _out->put(c == '\n' ? 'n' : 'r');
1020  } else {
1021  ++n;
1022  }
1023  }
1024  _out->put(_control.getQuoteChar());
1025 }
char getEscapeChar() const
Definition: CsvControl.h:158
char getQuoteChar() const
Definition: CsvControl.h:161
std::ostream * _out
Output stream.
Definition: Csv.h:310
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
CsvControl _control
File format.
Definition: Csv.h:311
bool standardEscapes
&quot;Flag indicating whether standard escape sequences should be handled.\n&quot; &quot;If false, then the character sequence &#39;\\C&#39;, where C is any character,\n&quot; &quot;is mapped to C (assuming &#39;\\&#39; is the escape character). If true,\n&quot; &quot;the following special cases are handled differently:\n&quot; &quot;\n&quot; &quot;- &#39;\\b&#39; is mapped to BS - backspace (ASCII 8)\n&quot; &quot;- &#39;\\f&#39; is mapped to FF - form feed (ASCII 12)\n&quot; &quot;- &#39;\\n&#39; is mapped to NL - newline (ASCII 10)\n&quot; &quot;- &#39;\\r&#39; is mapped to CR - carriage return (ASCII 13)\n&quot; &quot;- &#39;\\t&#39; is mapped to TAB - horizontal tab (ASCII 9)\n&quot; &quot;- &#39;\\v&#39; is mapped to VT - vertical tab (ASCII 11)\n&quot; &quot;- &#39;\\xD&#39; and &#39;\\xDD&#39;, where D is a hexadecimal digit, is mapped to\n&quot; &quot; the character with that numeric code.\n&quot; &quot;- A field value of exactly &#39;\\N&#39; (no quotes, whitespace, or other\n&quot; &quot; content) is treated as a NULL.\n&quot; ;
Definition: CsvControl.h:123
bool doubleQuote
&quot;If true, embedded quote characters are escaped with a leading quote\n&quot; &quot;character. Otherwise the escape character is used. If escaping and\n&quot; &quot;double-quoting are disabled, writing a field with embedded quote\n&quot; &quot;character will raise an exception.\n&quot; ;
Definition: CsvControl.h:106
void lsst::ap::utils::CsvWriter::_writeUnquoted ( char const *  s)
private

Writes unquoted field data, handling any required escaping.

Definition at line 1029 of file Csv.cc.

1029  {
1030  size_t n = 0;
1031  while (true) {
1032  char const c = s[n];
1033  if (c == '\0') {
1034  _out->write(s, n);
1035  break;
1036  }
1037  if (c == '\n' ||
1038  c == '\r' ||
1039  c == _control.getDelimiter() ||
1040  c == _control.getEscapeChar()) {
1041 
1042  _out->write(s, n);
1043  s += n + 1;
1044  n = 0;
1045  if (_control.getEscapeChar() == '\0') {
1046  throw LSST_EXCEPT(pexExcept::InvalidParameterError,
1047  "Field value requires escaping, but "
1048  "no escape character is set");
1049  }
1050  _out->put(_control.getEscapeChar());
1051  if (c == '\n') {
1052  _out->put(_control.standardEscapes ? 'n' : c);
1053  } else if (c == '\r') {
1054  _out->put(_control.standardEscapes ? 'r' : c);
1055  } else {
1056  _out->put(c);
1057  }
1058  continue;
1059  } else if (c == _control.getQuoteChar() &&
1060  _control.quoting != "QUOTE_NONE") {
1061 
1062  _out->write(s, n);
1063  s += n + 1;
1064  n = 0;
1065  if (_control.doubleQuote) {
1066  _out->put(c);
1067  _out->put(c);
1068  } else if (_control.getEscapeChar() == '\0') {
1069  throw LSST_EXCEPT(pexExcept::InvalidParameterError,
1070  "Field value requires escaping, but "
1071  "no escape character is set");
1072  } else {
1073  _out->put(_control.getEscapeChar());
1074  _out->put(c);
1075  }
1076  } else {
1077  ++n;
1078  }
1079  }
1080 }
char getDelimiter() const
Definition: CsvControl.h:155
char getEscapeChar() const
Definition: CsvControl.h:158
char getQuoteChar() const
Definition: CsvControl.h:161
std::ostream * _out
Output stream.
Definition: Csv.h:310
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
CsvControl _control
File format.
Definition: Csv.h:311
std::string quoting
&quot;Field quoting style for CSV input/output. Legal values are:\n&quot; &quot;\n&quot; &quot;&#39;QUOTE_MINIMAL&#39;: Only quot...
Definition: CsvControl.h:77
bool standardEscapes
&quot;Flag indicating whether standard escape sequences should be handled.\n&quot; &quot;If false, then the character sequence &#39;\\C&#39;, where C is any character,\n&quot; &quot;is mapped to C (assuming &#39;\\&#39; is the escape character). If true,\n&quot; &quot;the following special cases are handled differently:\n&quot; &quot;\n&quot; &quot;- &#39;\\b&#39; is mapped to BS - backspace (ASCII 8)\n&quot; &quot;- &#39;\\f&#39; is mapped to FF - form feed (ASCII 12)\n&quot; &quot;- &#39;\\n&#39; is mapped to NL - newline (ASCII 10)\n&quot; &quot;- &#39;\\r&#39; is mapped to CR - carriage return (ASCII 13)\n&quot; &quot;- &#39;\\t&#39; is mapped to TAB - horizontal tab (ASCII 9)\n&quot; &quot;- &#39;\\v&#39; is mapped to VT - vertical tab (ASCII 11)\n&quot; &quot;- &#39;\\xD&#39; and &#39;\\xDD&#39;, where D is a hexadecimal digit, is mapped to\n&quot; &quot; the character with that numeric code.\n&quot; &quot;- A field value of exactly &#39;\\N&#39; (no quotes, whitespace, or other\n&quot; &quot; content) is treated as a NULL.\n&quot; ;
Definition: CsvControl.h:123
bool doubleQuote
&quot;If true, embedded quote characters are escaped with a leading quote\n&quot; &quot;character. Otherwise the escape character is used. If escaping and\n&quot; &quot;double-quoting are disabled, writing a field with embedded quote\n&quot; &quot;character will raise an exception.\n&quot; ;
Definition: CsvControl.h:106
void lsst::ap::utils::CsvWriter::appendField ( std::string const &  v)
inline

Appends a field for each string in the given list.

Definition at line 281 of file Csv.cc.

281  {
282  _write(v.c_str());
283 }
void _write(char const *s)
Definition: Csv.cc:915
void lsst::ap::utils::CsvWriter::appendField ( char const *  v)
inline

Appends a field for each string in the given list.

Definition at line 285 of file Csv.cc.

285  {
286  if (v == 0) {
287  appendNull();
288  } else {
289  _write(v);
290  }
291 }
void _write(char const *s)
Definition: Csv.cc:915
void lsst::ap::utils::CsvWriter::appendField ( bool  v)
inline

Appends a field for each string in the given list.

Definition at line 293 of file Csv.cc.

293  {
294  _write(v ? "1" : "0");
295 }
void _write(char const *s)
Definition: Csv.cc:915
void lsst::ap::utils::CsvWriter::appendField ( char  v)
inline

Appends a field for each string in the given list.

Definition at line 297 of file Csv.cc.

297  {
298  char s[2];
299  s[0] = v;
300  s[1] = '\0';
301  _write(s);
302 }
void _write(char const *s)
Definition: Csv.cc:915
void lsst::ap::utils::CsvWriter::appendField ( signed char  v)

Appends a field for each string in the given list.

Definition at line 758 of file Csv.cc.

void lsst::ap::utils::CsvWriter::appendField ( short  v)

Appends a field for each string in the given list.

Definition at line 749 of file Csv.cc.

void lsst::ap::utils::CsvWriter::appendField ( int  v)

Appends a field for each string in the given list.

Definition at line 750 of file Csv.cc.

void lsst::ap::utils::CsvWriter::appendField ( long  v)

Appends a field for each string in the given list.

Definition at line 751 of file Csv.cc.

void lsst::ap::utils::CsvWriter::appendField ( long long  v)

Appends a field for each string in the given list.

Definition at line 752 of file Csv.cc.

void lsst::ap::utils::CsvWriter::appendField ( unsigned char  v)

Appends a field for each string in the given list.

Definition at line 759 of file Csv.cc.

void lsst::ap::utils::CsvWriter::appendField ( unsigned short  v)

Appends a field for each string in the given list.

Definition at line 753 of file Csv.cc.

void lsst::ap::utils::CsvWriter::appendField ( unsigned int  v)

Appends a field for each string in the given list.

Definition at line 754 of file Csv.cc.

void lsst::ap::utils::CsvWriter::appendField ( unsigned long  v)

Appends a field for each string in the given list.

Definition at line 755 of file Csv.cc.

void lsst::ap::utils::CsvWriter::appendField ( unsigned long long  v)

Appends a field for each string in the given list.

Definition at line 756 of file Csv.cc.

void lsst::ap::utils::CsvWriter::appendField ( float  v)

Appends a field for each string in the given list.

Definition at line 769 of file Csv.cc.

769  {
770  char buf[64];
771  char fmt[32];
772  int n = 0;
774  appendNull();
775  return;
776  }
777 #if FLT_RADIX == 2
778  if (FLT_MANT_DIG == 24) {
779  // 32 bit IEEE fast path
780  n = snprintf(buf, sizeof(buf), "%.9g", static_cast<double>(v));
781  } else {
782  // ceil(1 + FLT_MANT_DIG*log10(2))
783  static unsigned long const ndig = 2 + (FLT_MANT_DIG*30103UL)/100000UL;
784 #elif defined(FLT_MAXDIG10)
785  static unsigned long const ndig = FLT_MAXDIG10;
786 #elif defined(DECIMAL_DIG)
787  static unsigned long const ndig = DECIMAL_DIG;
788 #else
789 # error Unable to determine number of digits for lossless float->decimal->float conversion
790 #endif
791  n = snprintf(fmt, sizeof(fmt), "%%.%lug", ndig);
792  if (n <= 0 || n >= static_cast<int>(sizeof(fmt))) {
793  throw LSST_EXCEPT(pexExcept::LogicError, \
794  "internal buffer for string conversion too small"); \
795  }
796  n = snprintf(buf, sizeof(buf), fmt, static_cast<double>(v));
797 #if FLT_RADIX == 2
798  }
799 #endif
800  if (n <= 0 || n >= static_cast<int>(sizeof(buf))) {
801  throw LSST_EXCEPT(pexExcept::LogicError,
802  "snprintf() failed to convert float to string");
803  }
804  _write(buf);
805 }
void _write(char const *s)
Definition: Csv.cc:915
bool nonfiniteAsNull
&quot;If true, then non-finite (NaN, Inf, -Inf) floating point values are\n&quot; &quot;written out as NULL field va...
Definition: CsvControl.h:131
int isfinite(T t)
Definition: ieee.h:100
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
CsvControl _control
File format.
Definition: Csv.h:311
void lsst::ap::utils::CsvWriter::appendField ( double  v)

Appends a field for each string in the given list.

Definition at line 807 of file Csv.cc.

807  {
808  char buf[64];
809  char fmt[32];
810  int n = 0;
812  appendNull();
813  return;
814  }
815 #if FLT_RADIX == 2
816  if (DBL_MANT_DIG == 53) {
817  // 64 bit IEEE fast path
818  n = snprintf(buf, sizeof(buf), "%.17g", v);
819  } else {
820  // ceil(1 + DBL_MANT_DIG*log10(2))
821  static unsigned long const ndig = 2 + (DBL_MANT_DIG*30103UL)/100000UL;
822 #elif defined(DBL_MAXDIG10)
823  static unsigned long const ndig = DBL_MAXDIG10;
824 #elif defined(DECIMAL_DIG)
825  static unsigned long const ndig = DECIMAL_DIG;
826 #else
827 # error Unable to determine number of digits for lossless double->decimal->double conversion
828 #endif
829  n = snprintf(fmt, sizeof(fmt), "%%.%lug", ndig);
830  if (n <= 0 || n >= static_cast<int>(sizeof(fmt))) {
831  throw LSST_EXCEPT(pexExcept::LogicError,
832  "snprintf() failed to produce format string");
833  }
834  n = snprintf(buf, sizeof(buf), fmt, v);
835 #if FLT_RADIX == 2
836  }
837 #endif
838  if (n <= 0 || n >= static_cast<int>(sizeof(buf))) {
839  throw LSST_EXCEPT(pexExcept::LogicError,
840  "snprintf() failed to convert double to string");
841  }
842  _write(buf);
843 }
void _write(char const *s)
Definition: Csv.cc:915
bool nonfiniteAsNull
&quot;If true, then non-finite (NaN, Inf, -Inf) floating point values are\n&quot; &quot;written out as NULL field va...
Definition: CsvControl.h:131
int isfinite(T t)
Definition: ieee.h:100
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
CsvControl _control
File format.
Definition: Csv.h:311
void lsst::ap::utils::CsvWriter::appendField ( long double  v)

Appends a field for each string in the given list.

Definition at line 845 of file Csv.cc.

845  {
846  char buf[64];
847  char fmt[32];
848  int n = 0;
850  appendNull();
851  return;
852  }
853 #if FLT_RADIX == 2
854  if (LDBL_MANT_DIG == 64) {
855  // 80bit IEEE long double fast path
856  n = snprintf(buf, sizeof(buf), "%.21Lg", v);
857  } else if (LDBL_MANT_DIG == 113) {
858  // 128bit IEEE long double fast path
859  n = snprintf(buf, sizeof(buf), "%.36Lg", v);
860  } else {
861  // ceil(1 + LDBL_MANT_DIG*log10(2))
862  static unsigned long const ndig = 2 + (LDBL_MANT_DIG*30103UL)/100000UL;
863 #elif defined(DBL_MAXDIG10)
864  static unsigned long const ndig = LDBL_MAXDIG10;
865 #elif defined(DECIMAL_DIG)
866  static unsigned long const ndig = DECIMAL_DIG;
867 #else
868 # error Unable to determine number of digits for lossless long double->decimal->long double conversion
869 #endif
870  n = snprintf(fmt, sizeof(fmt), "%%.%luLg", ndig);
871  if (n <= 0 || n >= static_cast<int>(sizeof(fmt))) {
872  throw LSST_EXCEPT(pexExcept::LogicError,
873  "snprintf() failed to produce format string");
874  }
875  n = snprintf(buf, sizeof(buf), fmt, v);
876 #if FLT_RADIX == 2
877  }
878 #endif
879  if (n <= 0 || n >= static_cast<int>(sizeof(buf))) {
880  throw LSST_EXCEPT(pexExcept::LogicError,
881  "snprintf() failed to convert long double to string");
882  }
883  _write(buf);
884 }
void _write(char const *s)
Definition: Csv.cc:915
bool nonfiniteAsNull
&quot;If true, then non-finite (NaN, Inf, -Inf) floating point values are\n&quot; &quot;written out as NULL field va...
Definition: CsvControl.h:131
int isfinite(T t)
Definition: ieee.h:100
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
CsvControl _control
File format.
Definition: Csv.h:311
void lsst::ap::utils::CsvWriter::appendFields ( std::vector< std::string > const &  fields)

Appends a field for each string in the given list.

Definition at line 728 of file Csv.cc.

728  {
729  for (vector<string>::const_iterator i = fields.begin(), e = fields.end();
730  i != e; ++i) {
731  appendField(*i);
732  }
733 }
void appendField(std::string const &v)
Definition: Csv.cc:281
void lsst::ap::utils::CsvWriter::appendNull ( )

Appends a database NULL field to the current record. If there is no NULL representation, an empty field is written.

Definition at line 889 of file Csv.cc.

889  {
890  if (_control.hasNull) {
891  // output leading delimiter except for the first field in a record
892  if (_numFields > 0) {
893  _out->put(_control.getDelimiter());
894  }
895  ++_numFields;
896  // NULL is never quoted, and guaranteed not to require escaping
897  _out->write(_control.null.c_str(), _control.null.size());
898  } else if (_control.standardEscapes) {
899  // output leading delimiter except for the first field in a record
900  if (_numFields > 0) {
901  _out->put(_control.getDelimiter());
902  }
903  ++_numFields;
904  // write \N
905  _out->put(_control.getEscapeChar());
906  _out->put('N');
907  } else {
908  // write an empty field
909  _write("");
910  }
911 }
void _write(char const *s)
Definition: Csv.cc:915
char getDelimiter() const
Definition: CsvControl.h:155
char getEscapeChar() const
Definition: CsvControl.h:158
std::ostream * _out
Output stream.
Definition: Csv.h:310
std::string null
&quot;String representation of NULL field values. Never quoted on output.\n&quot; &quot;If specified, the representation may not contain any delimiter,\n&quot; &quot;quote, escape or line terminator characters (&#39;\\n&#39;/&#39;\\r&#39;).\n&quot; ;
Definition: CsvControl.h:61
size_t _numFields
Number of fields written.
Definition: Csv.h:314
CsvControl _control
File format.
Definition: Csv.h:311
bool hasNull
&quot;Indicates whether the null string is valid. If set to false, the only\n&quot; &quot;way NULLs can be recogn...
Definition: CsvControl.h:67
bool standardEscapes
&quot;Flag indicating whether standard escape sequences should be handled.\n&quot; &quot;If false, then the character sequence &#39;\\C&#39;, where C is any character,\n&quot; &quot;is mapped to C (assuming &#39;\\&#39; is the escape character). If true,\n&quot; &quot;the following special cases are handled differently:\n&quot; &quot;\n&quot; &quot;- &#39;\\b&#39; is mapped to BS - backspace (ASCII 8)\n&quot; &quot;- &#39;\\f&#39; is mapped to FF - form feed (ASCII 12)\n&quot; &quot;- &#39;\\n&#39; is mapped to NL - newline (ASCII 10)\n&quot; &quot;- &#39;\\r&#39; is mapped to CR - carriage return (ASCII 13)\n&quot; &quot;- &#39;\\t&#39; is mapped to TAB - horizontal tab (ASCII 9)\n&quot; &quot;- &#39;\\v&#39; is mapped to VT - vertical tab (ASCII 11)\n&quot; &quot;- &#39;\\xD&#39; and &#39;\\xDD&#39;, where D is a hexadecimal digit, is mapped to\n&quot; &quot; the character with that numeric code.\n&quot; &quot;- A field value of exactly &#39;\\N&#39; (no quotes, whitespace, or other\n&quot; &quot; content) is treated as a NULL.\n&quot; ;
Definition: CsvControl.h:123
void lsst::ap::utils::CsvWriter::endRecord ( )

Terminates a record - subsequent apppends will be to a new record.

Definition at line 716 of file Csv.cc.

716  {
718  _out->put(_control.getDelimiter());
719  }
720  _out->put('\n');
721  _numFields = 0;
722  ++_numLines;
723  ++_numRecords;
724 }
char getDelimiter() const
Definition: CsvControl.h:155
std::ostream * _out
Output stream.
Definition: Csv.h:310
size_t _numFields
Number of fields written.
Definition: Csv.h:314
size_t _numRecords
Number of records written.
Definition: Csv.h:312
size_t _numLines
Number of lines written.
Definition: Csv.h:313
CsvControl _control
File format.
Definition: Csv.h:311
bool trailingDelimiter
&quot;If true, then a trailing delimiter character is expected and written\n&quot; &quot;at end of every record...
Definition: CsvControl.h:127
void lsst::ap::utils::CsvWriter::flush ( )
inline

Forces a write of all user-space buffered data to the underlying stream. Does not terminate the current record.

Definition at line 259 of file Csv.cc.

259  {
260  _out->flush();
261 }
std::ostream * _out
Output stream.
Definition: Csv.h:310
CsvControl const & lsst::ap::utils::CsvWriter::getControl ( ) const
inline

Returns the format of this writer.

Definition at line 252 of file Csv.cc.

252  {
253  return _control;
254 }
CsvControl _control
File format.
Definition: Csv.h:311
size_t lsst::ap::utils::CsvWriter::getNumFields ( ) const
inline

Returns the number of fields written to the current record.

Definition at line 277 of file Csv.cc.

277  {
278  return _numFields;
279 }
size_t _numFields
Number of fields written.
Definition: Csv.h:314
size_t lsst::ap::utils::CsvWriter::getNumLines ( ) const
inline

Returns the number of lines written out.

Definition at line 265 of file Csv.cc.

265  {
266  return _numLines;
267 }
size_t _numLines
Number of lines written.
Definition: Csv.h:313
size_t lsst::ap::utils::CsvWriter::getNumRecords ( ) const
inline

Returns the number of records written out.

Definition at line 271 of file Csv.cc.

271  {
272  return _numRecords;
273 }
size_t _numRecords
Number of records written.
Definition: Csv.h:312
CsvWriter& lsst::ap::utils::CsvWriter::operator= ( CsvWriter const &  )
private
void lsst::ap::utils::CsvWriter::write ( std::string const &  s)
inline

Raw write: the given string/character is written directly to the output stream without being run through the quoting/escaping process. This can be useful when repeatedly outputting the same records/fields.

Definition at line 309 of file Csv.cc.

309  {
310  _out->write(s.c_str(), s.size());
311 }
std::ostream * _out
Output stream.
Definition: Csv.h:310
void lsst::ap::utils::CsvWriter::write ( char  c)
inline

Raw write: the given string/character is written directly to the output stream without being run through the quoting/escaping process. This can be useful when repeatedly outputting the same records/fields.

Definition at line 312 of file Csv.cc.

312  {
313  _out->put(c);
314 }
std::ostream * _out
Output stream.
Definition: Csv.h:310

Member Data Documentation

CsvControl lsst::ap::utils::CsvWriter::_control
private

File format.

Definition at line 311 of file Csv.h.

size_t lsst::ap::utils::CsvWriter::_numFields
private

Number of fields written.

Definition at line 314 of file Csv.h.

size_t lsst::ap::utils::CsvWriter::_numLines
private

Number of lines written.

Definition at line 313 of file Csv.h.

size_t lsst::ap::utils::CsvWriter::_numRecords
private

Number of records written.

Definition at line 312 of file Csv.h.

std::ostream* lsst::ap::utils::CsvWriter::_out
private

Output stream.

Definition at line 310 of file Csv.h.

boost::scoped_ptr<std::ofstream> lsst::ap::utils::CsvWriter::_stream
private

Output file stream.

Definition at line 309 of file Csv.h.


The documentation for this class was generated from the following files: