diff options
Diffstat (limited to 'src/stable/csvreader.cpp')
-rw-r--r-- | src/stable/csvreader.cpp | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/src/stable/csvreader.cpp b/src/stable/csvreader.cpp new file mode 100644 index 0000000..4da7883 --- /dev/null +++ b/src/stable/csvreader.cpp | |||
@@ -0,0 +1,130 @@ | |||
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/csvreader.h" | ||
9 | #include "bu/stream.h" | ||
10 | |||
11 | #include "bu/sio.h" | ||
12 | using namespace Bu; | ||
13 | |||
14 | Bu::CsvReader::CsvReader( Bu::Stream &sIn, Bu::CsvReader::Style eStyle ) : | ||
15 | sIn( sIn ) | ||
16 | { | ||
17 | switch( eStyle ) | ||
18 | { | ||
19 | case styleExcel: | ||
20 | sDecode = Bu::slot( &decodeExcel ); | ||
21 | break; | ||
22 | |||
23 | case styleC: | ||
24 | sDecode = Bu::slot( &decodeC ); | ||
25 | break; | ||
26 | } | ||
27 | } | ||
28 | |||
29 | Bu::CsvReader::CsvReader( Bu::Stream &sIn, | ||
30 | Bu::CsvReader::DecodeSignal sDecode ) : | ||
31 | sIn( sIn ), | ||
32 | sDecode( sDecode ) | ||
33 | { | ||
34 | } | ||
35 | |||
36 | Bu::CsvReader::~CsvReader() | ||
37 | { | ||
38 | } | ||
39 | |||
40 | Bu::StrArray Bu::CsvReader::readLine() | ||
41 | { | ||
42 | Bu::StrArray aVals; | ||
43 | |||
44 | Bu::String sLine = sIn.readLine(); | ||
45 | |||
46 | if( !sLine.isSet() ) | ||
47 | return Bu::StrArray(); | ||
48 | |||
49 | Bu::String::iterator i = sLine.begin(); | ||
50 | |||
51 | aVals.append( sDecode( i ) ); | ||
52 | |||
53 | while( i ) | ||
54 | { | ||
55 | if( *i == ',' ) | ||
56 | { | ||
57 | i++; | ||
58 | if( !i ) | ||
59 | { | ||
60 | aVals.append(""); | ||
61 | break; | ||
62 | } | ||
63 | aVals.append( sDecode( i ) ); | ||
64 | } | ||
65 | else | ||
66 | { | ||
67 | // Blanks and stuff? | ||
68 | sio << "Out of bound: '" << *i << "'" << sio.nl; | ||
69 | i++; | ||
70 | } | ||
71 | } | ||
72 | |||
73 | return aVals; | ||
74 | } | ||
75 | |||
76 | Bu::String Bu::CsvReader::decodeExcel( Bu::String::iterator &i ) | ||
77 | { | ||
78 | Bu::String sRet; | ||
79 | |||
80 | for(; i && (*i == ' ' || *i == '\t'); i++ ) { } | ||
81 | |||
82 | if( !i ) | ||
83 | return sRet; | ||
84 | |||
85 | if( *i == '\"' ) | ||
86 | { | ||
87 | for( i++ ; i; i++ ) | ||
88 | { | ||
89 | if( *i == '\"' ) | ||
90 | { | ||
91 | i++; | ||
92 | if( !i ) | ||
93 | { | ||
94 | return sRet; | ||
95 | } | ||
96 | else if( *i == '\"' ) | ||
97 | { | ||
98 | sRet += *i; | ||
99 | } | ||
100 | else | ||
101 | { | ||
102 | return sRet; | ||
103 | } | ||
104 | } | ||
105 | else | ||
106 | { | ||
107 | sRet += *i; | ||
108 | } | ||
109 | } | ||
110 | } | ||
111 | else | ||
112 | { | ||
113 | for( ; i; i++ ) | ||
114 | { | ||
115 | if( *i == ',' ) | ||
116 | { | ||
117 | return sRet; | ||
118 | } | ||
119 | sRet += *i; | ||
120 | } | ||
121 | } | ||
122 | |||
123 | return sRet; | ||
124 | } | ||
125 | |||
126 | Bu::String Bu::CsvReader::decodeC( Bu::String::iterator & ) | ||
127 | { | ||
128 | return ""; | ||
129 | } | ||
130 | |||