C API Documentation
rrIniFile.h
1 #ifndef IniFileH
2 #define IniFileH
3 #include <vector>
4 #include <fstream>
5 #include <string>
6 #include "rrExporter.h"
7 #include "rrStringUtils.h"
8 #include "rrIniSection.h"
9 #include "rrIniKey.h"
10 #include "rrFileName.h"
11 
12 namespace rr
13 {
14 
15 using std::string;
16 using std::fstream;
17 using std::ios;
18 using std::ios_base;
19 
20 const int MAX_LINE_BUFFER_SIZE = 2048;
21 class IniKey;
22 class IniSection;
23 
24 // IniFile typedefs
25 typedef std::vector<IniKey*> KeyList;
26 typedef std::vector<string> NonKeyList;
27 typedef std::vector<IniSection*> SectionList;
28 typedef SectionList::iterator SectionItor;
29 
33 class RR_DECLSPEC IniFile
34 {
35  private:
36  // When set, this define will cause SetValue() to create a new section, if
37  // the requested section does not allready exist.
38  const int mAutoCreateSections;
39 
40  // When set, this define causes SetValue() to create a new key, if the
41  // requested key does not allready exist.
42  const int mAutoCreateKeys;
43 
44  string mCommentIndicators;
45  const string mEqualIndicator;
46  const string mWhiteSpace;
47 
48  // General Purpose Utility Functions
49  string GetNextWord(string& CommandLine);
50  string Trim(string& Str);
51  int WriteLine(fstream& stream, const char* fmt, ...);
52 
53  protected:
54  SectionList mSections; // List of sections
55  FileName mIniFileName; // The filename to write to
56  bool mIsDirty; // Tracks whether or not data has changed.
57  bool mWasFound;
58  bool mAutoSave; //Save ini file automatically on destruction
59 
60 
61  public:
62  int mFlags; // Our settings flags.
63  IniFile(const string& fName = "", bool autoLoad = false, bool autoSave = false);
64  virtual ~IniFile();
65 
66  size_t GetNumberOfSections(){return mSections.size();}
67  IniSection* GetSection(int i){return mSections[i];}
68  IniSection* GetSection(size_t i) { return mSections[i]; }
69 
70  // File handling methods
71  string GetFilePath(){return mIniFileName.GetPath();}
72  string GetFileName(){return mIniFileName;}
73  string GetFullFileName(){return mIniFileName.GetPathAndFileName();}
74  bool SetFilePath(const string& path);
75  bool Load(const string& fName = "");
76  IniSection* LoadSection(const string& theSection);
77  bool Save(ios_base::openmode openMode = ios::out|ios::trunc);
78  bool UnLoad(){return Save();}
79  bool WasItFound(){return mWasFound;}
80  bool SectionExists(const string& section);
81 
82  //Reading
83  // ReadValue: Default access method. Returns the raw string value
84  string ReadValue(const string& Key, const string& Section = "");
85  string ReadString(const string& Key, const string& Section = "", const string& def_val="");
86  double ReadDouble(const string& Key, const string& Section = "", double def_value = 0);
87  float ReadFloat(const string& Key, const string& Section = "", double def_value = 0){return (float) ReadDouble(Key, Section, def_value);}
88  int ReadInteger(const string& Key, const string& Section = "", int def_value = 0);
89  bool ReadBool(const string& Key, const string& Section = "", bool def_value = false);
90 
91  //Writing
92  bool WriteValue(const string& Key, const string& Value, const string& Comment = "", const string& Section = "");
93  bool WriteFloat(const string& Key, double value, const string& Comment = "", const string& Section = "");
94  bool WriteInteger(const string& Key, int nValue, const string& Comment = "", const string& Section = "");
95  bool WriteBool(const string& Key, bool bValue, const string& Comment = "", const string& Section = "");
96  bool WriteString(const string& Key, const string& Value, const string& Comment = "", const string& Section = ""){return WriteValue(Key, Value, Comment, Section);}
97  bool WriteNonKey(const string& nonKey, const string& Section = "");
98 
99  //KeyHandling
100  bool SetKeyComment(const string& Key, const string& Comment, const string& Section = "");
101  bool SetSectionComment(const string& Section, const string& Comment);
102  bool DeleteKey(const string& Key, const string& FromSection = "");
103  bool DeleteSection(const string& Section);
104  bool DeleteSectionsWithKeyValue(const string& key, const string& value);
105  bool CreateKey(const string& Key, const string& Value, const string& Comment = "", const string& Section = "");
106 
107  //Section stuff
108  bool CreateSection(const string& Section, const string& Comment = "");
109  bool CreateSection(const string& Section, const string& Comment, KeyList Keys);
110 
111  // Utility Methods
112  size_t SectionCount();
113  size_t KeyCount();
114  size_t KeyCount(const string& section);
115  void Clear();
116  void SetFileName(const string& fName);
117  string CommentStr(string& Comment);
118  IniKey* GetKey(const string& Key, const string& Section);
119  IniSection* GetSection(const string& Section, bool create = false);
120  IniSection* GetSection(const unsigned int secNr);
121  bool ClearSection(const string& section);
122  bool IsModified(){return mIsDirty;}
123 };
124 
125 }
126 #endif
Definition: rrFileName.h:17
Definition: rrIniFile.h:34
bool SectionExists(const string &section)
Boolean indicating if the last key was found in the ini file.
Definition: rrIniSection.h:24