diff options
Diffstat (limited to 'src/stable/csvwriter.cpp')
-rw-r--r-- | src/stable/csvwriter.cpp | 81 |
1 files changed, 81 insertions, 0 deletions
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 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2011 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #include "bu/csvwriter.h" | ||
9 | #include "bu/stream.h" | ||
10 | |||
11 | Bu::CsvWriter::CsvWriter( Bu::Stream &sOut, Bu::CsvWriter::Style eStyle ) : | ||
12 | sOut( sOut ) | ||
13 | { | ||
14 | switch( eStyle ) | ||
15 | { | ||
16 | case styleExcel: | ||
17 | sEncode = Bu::slot( &encodeExcel ); | ||
18 | break; | ||
19 | |||
20 | case styleC: | ||
21 | sEncode = Bu::slot( &encodeExcel ); | ||
22 | break; | ||
23 | } | ||
24 | } | ||
25 | |||
26 | Bu::CsvWriter::CsvWriter( Bu::Stream &sOut, | ||
27 | Bu::CsvWriter::EncodeSignal sEncode ) : | ||
28 | sOut( sOut ), | ||
29 | sEncode( sEncode ) | ||
30 | { | ||
31 | } | ||
32 | |||
33 | Bu::CsvWriter::~CsvWriter() | ||
34 | { | ||
35 | } | ||
36 | |||
37 | void Bu::CsvWriter::writeLine( const StrArray &aStrs ) | ||
38 | { | ||
39 | Bu::String sBuf; | ||
40 | for( StrArray::const_iterator i = aStrs.begin(); i; i++ ) | ||
41 | { | ||
42 | if( i != aStrs.begin() ) | ||
43 | sBuf += ","; | ||
44 | sBuf += sEncode( *i ); | ||
45 | } | ||
46 | sBuf += "\n"; | ||
47 | |||
48 | sOut.write( sBuf ); | ||
49 | } | ||
50 | |||
51 | Bu::String Bu::CsvWriter::encodeExcel( const Bu::String &sIn ) | ||
52 | { | ||
53 | if( sIn.find('\"') || sIn.find(',') ) | ||
54 | { | ||
55 | Bu::String sOut = "\""; | ||
56 | for( Bu::String::const_iterator i = sIn.begin(); i; i++ ) | ||
57 | { | ||
58 | if( *i == '\"' ) | ||
59 | sOut += "\"\""; | ||
60 | else | ||
61 | sOut += *i; | ||
62 | } | ||
63 | sOut += '\"'; | ||
64 | return sOut; | ||
65 | } | ||
66 | return sIn; | ||
67 | } | ||
68 | |||
69 | Bu::String Bu::CsvWriter::encodeC( const Bu::String &sIn ) | ||
70 | { | ||
71 | Bu::String sOut = ""; | ||
72 | for( Bu::String::const_iterator i = sIn.begin(); i; i++ ) | ||
73 | { | ||
74 | if( *i == ',' ) | ||
75 | sOut += "\\,"; | ||
76 | else | ||
77 | sOut += *i; | ||
78 | } | ||
79 | return sOut; | ||
80 | } | ||
81 | |||