KrisLibrary  1.0.0
SimpleFile.h
1 #ifndef SIMPLE_FILE_H
2 #define SIMPLE_FILE_H
3 
4 #include <map>
5 #include <vector>
6 #include <string>
7 #include <iosfwd>
8 #include "PrimitiveValue.h"
9 
19 {
20  public:
21  SimpleFile(const char* fn);
22  SimpleFile(istream& in);
23  SimpleFile();
24  bool Load(const char* fn);
25  bool Load(istream& in);
26  bool Save(const char* fn);
27  bool Save(ostream& out);
28  operator bool () const { return loaded; }
29  void AllowItem(const std::string& str,bool caseSensitive=false);
30 
31  //accessors
32  inline bool empty() { return entries.empty(); }
33  inline size_t count(const string& name) { return entries.count(name); }
34  inline void erase(const string& name) { entries.erase(entries.find(name)); }
35  inline std::vector<PrimitiveValue>& operator [] (const string& name) { return entries[name]; }
36  inline std::vector<PrimitiveValue>& operator [] (const char* name) { return entries[string(name)]; }
37 
38  //size and type-checking utilities
39  bool CheckSize(const string& name,int size,const char* errorString=NULL);
40  bool CheckType(const string& name,int type,const char* errorString=NULL);
41  std::vector<int> AsInteger(const string& name);
42  std::vector<double> AsDouble(const string& name);
43  std::vector<std::string> AsString(const string& name);
44 
45  bool loaded;
46  std::map<std::string,std::vector<PrimitiveValue> > entries;
47  std::map<std::string,bool> validItems;
48 };
49 
50 #endif
A simple file format that has named items and whitespace-delimited values.
Definition: SimpleFile.h:18