Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
csv.h
Go to the documentation of this file.
1 #include "config.h"
2 #include <string>
3 #include <vector>
4 #include <gnuhash.h>
5 
6 #include "hashtable.h"
7 namespace VSFileSystem
8 {
9 class VSFile;
10 }
11 
12 // delim should be read as separator and not to be confused with text delimiter see http://creativyst.com/Doc/Articles/CSV/CSV01.htm
13 // separator values , and ; while delimiter is listed as quote or "
14 std::vector< std::string >readCSV( std::string line, std::string delim = ",;" );
15 std::string writeCSV( const std::vector< std::string > &key, const std::vector< std::string > &table, std::string delim = ",;" );
16 class CSVTable
17 {
18 private:
19  void Init( std::string data );
20 public:
21  std::string rootdir;
22  vsUMap< std::string, int >columns;
23  vsUMap< std::string, int >rows;
24  std::vector< std::string >key;
25  std::vector< std::string >table;
26 
27  CSVTable( std::string name, std::string saveroot );
28  CSVTable( VSFileSystem::VSFile &f, std::string saveroot );
29 
30  bool RowExists( std::string name, unsigned int &where );
31  bool ColumnExists( std::string name, unsigned int &where );
32 
33 public:
34 //Optimizer toolbox
36  void SetupOptimizer( std::vector< std::string >keys, unsigned int type );
37 
38 //Opaque Optimizers - use the optimizer toolbox to set them up
40  unsigned int optimizer_type;
41  std::vector< std::string >optimizer_keys;
42  std::vector< unsigned int >optimizer_indexes;
43 };
44 
45 class CSVRow
46 {
47  std::string::size_type iter;
48  CSVTable *parent;
49 public:
50  std::string getRoot();
51  size_t size()
52  {
53  return parent->key.size();
54  }
55  CSVRow( CSVTable *parent, std::string key );
56  CSVRow( CSVTable *parent, unsigned int which );
58  {
59  parent = NULL;
60  iter = std::string::npos;
61  }
62  const std::string&operator[]( const std::string& ) const;
63  const std::string&operator[]( unsigned int ) const;
64  const std::string& getKey( unsigned int which ) const;
65  std::vector< std::string >::iterator begin();
66  std::vector< std::string >::iterator end();
67  bool success() const
68  {
69  return parent != NULL;
70  }
72  {
73  return parent;
74  }
75 };
76 
77 extern std::vector< CSVTable* >unitTables;
78