diff options
-rw-r--r-- | src/tools/viewcsv.cpp | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/src/tools/viewcsv.cpp b/src/tools/viewcsv.cpp new file mode 100644 index 0000000..69bc6a1 --- /dev/null +++ b/src/tools/viewcsv.cpp | |||
@@ -0,0 +1,95 @@ | |||
1 | #include <bu/sio.h> | ||
2 | #include <bu/optparser.h> | ||
3 | #include <bu/csvreader.h> | ||
4 | #include <bu/file.h> | ||
5 | |||
6 | using namespace Bu; | ||
7 | |||
8 | class Options : public Bu::OptParser | ||
9 | { | ||
10 | public: | ||
11 | Options( int argc, char *argv[] ) | ||
12 | { | ||
13 | setNonOption( slot( this, &Options::onNonOption ) ); | ||
14 | addHelpOption(); | ||
15 | parse( argc, argv ); | ||
16 | } | ||
17 | |||
18 | virtual ~Options() | ||
19 | { | ||
20 | } | ||
21 | |||
22 | int onNonOption( StrArray aParams ) | ||
23 | { | ||
24 | //sio << aParams << sio.nl; | ||
25 | sFileIn = aParams[0]; | ||
26 | |||
27 | return 0; | ||
28 | } | ||
29 | |||
30 | Bu::FString sFileIn; | ||
31 | }; | ||
32 | |||
33 | typedef Bu::Array<StrArray> StrGrid; | ||
34 | typedef Bu::Array<int> IntArray; | ||
35 | class CsvDoc | ||
36 | { | ||
37 | public: | ||
38 | CsvDoc() : | ||
39 | iMaxCols( 0 ) | ||
40 | { | ||
41 | } | ||
42 | |||
43 | virtual ~CsvDoc() | ||
44 | { | ||
45 | } | ||
46 | |||
47 | void addRow( StrArray aStr ) | ||
48 | { | ||
49 | sgData.append( aStr ); | ||
50 | if( iMaxCols < aStr.getSize() ) | ||
51 | iMaxCols = aStr.getSize(); | ||
52 | while( aWidths.getSize() < iMaxCols ) | ||
53 | { | ||
54 | aWidths.append( 0 ); | ||
55 | } | ||
56 | for( int j = 0; j < aStr.getSize(); j++ ) | ||
57 | { | ||
58 | if( aWidths[j] < aStr[j].getSize() ) | ||
59 | aWidths[j] = aStr[j].getSize(); | ||
60 | } | ||
61 | } | ||
62 | |||
63 | int iMaxCols; | ||
64 | StrGrid sgData; | ||
65 | IntArray aWidths; | ||
66 | }; | ||
67 | |||
68 | int main( int argc, char *argv[] ) | ||
69 | { | ||
70 | Options opt( argc, argv ); | ||
71 | |||
72 | if( !opt.sFileIn.isSet() ) | ||
73 | { | ||
74 | sio << "No file specified." << sio.nl; | ||
75 | return 1; | ||
76 | } | ||
77 | |||
78 | CsvDoc doc; | ||
79 | File fIn( opt.sFileIn, File::Read ); | ||
80 | CsvReader cr( fIn ); | ||
81 | |||
82 | while( !fIn.isEos() ) | ||
83 | { | ||
84 | StrArray sa = cr.readLine(); | ||
85 | if( fIn.isEos() ) | ||
86 | break; | ||
87 | doc.addRow( sa ); | ||
88 | } | ||
89 | |||
90 | sio << "Csv grid: " << doc.iMaxCols << " x " << doc.sgData.getSize() | ||
91 | << sio.nl; | ||
92 | |||
93 | return 0; | ||
94 | } | ||
95 | |||