blob: 544c8bcff5bda7f0f4fa2e558612013c50dfde82 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
/*
* Copyright (C) 2007-2013 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;
}
|