From 469bbcf0701e1eb8a6670c23145b0da87357e178 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Sun, 25 Mar 2012 20:00:08 +0000 Subject: Code is all reorganized. We're about ready to release. I should write up a little explenation of the arrangement. --- src/stable/csvwriter.cpp | 81 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/stable/csvwriter.cpp (limited to 'src/stable/csvwriter.cpp') diff --git a/src/stable/csvwriter.cpp b/src/stable/csvwriter.cpp new file mode 100644 index 0000000..d8910aa --- /dev/null +++ b/src/stable/csvwriter.cpp @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2007-2011 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#include "bu/csvwriter.h" +#include "bu/stream.h" + +Bu::CsvWriter::CsvWriter( Bu::Stream &sOut, Bu::CsvWriter::Style eStyle ) : + sOut( sOut ) +{ + switch( eStyle ) + { + case styleExcel: + sEncode = Bu::slot( &encodeExcel ); + break; + + case styleC: + sEncode = Bu::slot( &encodeExcel ); + break; + } +} + +Bu::CsvWriter::CsvWriter( Bu::Stream &sOut, + Bu::CsvWriter::EncodeSignal sEncode ) : + sOut( sOut ), + sEncode( sEncode ) +{ +} + +Bu::CsvWriter::~CsvWriter() +{ +} + +void Bu::CsvWriter::writeLine( const StrArray &aStrs ) +{ + Bu::String sBuf; + for( StrArray::const_iterator i = aStrs.begin(); i; i++ ) + { + if( i != aStrs.begin() ) + sBuf += ","; + sBuf += sEncode( *i ); + } + sBuf += "\n"; + + sOut.write( sBuf ); +} + +Bu::String Bu::CsvWriter::encodeExcel( const Bu::String &sIn ) +{ + if( sIn.find('\"') || sIn.find(',') ) + { + Bu::String sOut = "\""; + for( Bu::String::const_iterator i = sIn.begin(); i; i++ ) + { + if( *i == '\"' ) + sOut += "\"\""; + else + sOut += *i; + } + sOut += '\"'; + return sOut; + } + return sIn; +} + +Bu::String Bu::CsvWriter::encodeC( const Bu::String &sIn ) +{ + Bu::String sOut = ""; + for( Bu::String::const_iterator i = sIn.begin(); i; i++ ) + { + if( *i == ',' ) + sOut += "\\,"; + else + sOut += *i; + } + return sOut; +} + -- cgit v1.2.3