aboutsummaryrefslogtreecommitdiff
path: root/src/tools/viewcsv.cpp
blob: 69bc6a15698c59bc396c1289bc7fd341a76e826a (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <bu/sio.h>
#include <bu/optparser.h>
#include <bu/csvreader.h>
#include <bu/file.h>

using namespace Bu;

class Options : public Bu::OptParser
{
public:
	Options( int argc, char *argv[] )
	{
		setNonOption( slot( this, &Options::onNonOption ) );
		addHelpOption();
		parse( argc, argv );
	}

	virtual ~Options()
	{
	}

	int onNonOption( StrArray aParams )
	{
		//sio << aParams << sio.nl;
		sFileIn = aParams[0];

		return 0;
	}

	Bu::FString sFileIn;
};

typedef Bu::Array<StrArray> StrGrid;
typedef Bu::Array<int> IntArray;
class CsvDoc
{
public:
	CsvDoc() :
		iMaxCols( 0 )
	{
	}

	virtual ~CsvDoc()
	{
	}

	void addRow( StrArray aStr )
	{
		sgData.append( aStr );
		if( iMaxCols < aStr.getSize() )
			iMaxCols = aStr.getSize();
		while( aWidths.getSize() < iMaxCols )
		{
			aWidths.append( 0 );
		}
		for( int j = 0; j < aStr.getSize(); j++ )
		{
			if( aWidths[j] < aStr[j].getSize() )
				aWidths[j] = aStr[j].getSize();
		}
	}

	int iMaxCols;
	StrGrid sgData;
	IntArray aWidths;
};

int main( int argc, char *argv[] )
{
	Options opt( argc, argv );

	if( !opt.sFileIn.isSet() )
	{
		sio << "No file specified." << sio.nl;
		return 1;
	}

	CsvDoc doc;
	File fIn( opt.sFileIn, File::Read );
	CsvReader cr( fIn );

	while( !fIn.isEos() )
	{
		StrArray sa = cr.readLine();
		if( fIn.isEos() )
			break;
		doc.addRow( sa );
	}

	sio << "Csv grid:  " << doc.iMaxCols << " x " << doc.sgData.getSize()
		<< sio.nl;

	return 0;
}