roadrunner  2.6.0
Fast simulator for SBML models
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<std::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  std::string mCommentIndicators;
45  const std::string mEqualIndicator;
46  const std::string mWhiteSpace;
47 
48  // General Purpose Utility Functions
49  std::string GetNextWord(std::string& CommandLine);
50  std::string Trim(std::string& Str);
51  int WriteLine(std::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 std::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  std::string GetFilePath() { return mIniFileName.GetPath(); }
72  std::string GetFileName() { return mIniFileName; }
73  std::string GetFullFileName() { return mIniFileName.GetPathAndFileName(); }
74  bool SetFilePath(const std::string& path);
75  bool Load(const std::string& fName = "");
76  IniSection* LoadSection(const std::string& theSection);
77  bool Save(ios_base::openmode openMode = std::ios::out | std::ios::trunc);
78  bool UnLoad() { return Save(); }
79  bool WasItFound() { return mWasFound; }
80  bool SectionExists(const std::string& section);
81 
82  //Reading
83  // ReadValue: Default access method. Returns the raw std::string value
84  std::string ReadValue(const std::string& Key, const std::string& Section = "");
85  std::string ReadString(const std::string& Key, const std::string& Section = "", const std::string& def_val = "");
86  double ReadDouble(const std::string& Key, const std::string& Section = "", double def_value = 0);
87  float ReadFloat(const std::string& Key, const std::string& Section = "", double def_value = 0) { return (float)ReadDouble(Key, Section, def_value); }
88  int ReadInteger(const std::string& Key, const std::string& Section = "", int def_value = 0);
89  bool ReadBool(const std::string& Key, const std::string& Section = "", bool def_value = false);
90 
91  //Writing
92  bool WriteValue(const std::string& Key, const std::string& Value, const std::string& Comment = "", const std::string& Section = "");
93  bool WriteFloat(const std::string& Key, double value, const std::string& Comment = "", const std::string& Section = "");
94  bool WriteInteger(const std::string& Key, int nValue, const std::string& Comment = "", const std::string& Section = "");
95  bool WriteBool(const std::string& Key, bool bValue, const std::string& Comment = "", const std::string& Section = "");
96  bool WriteString(const std::string& Key, const std::string& Value, const std::string& Comment = "", const std::string& Section = "") { return WriteValue(Key, Value, Comment, Section); }
97  bool WriteNonKey(const std::string& nonKey, const std::string& Section = "");
98 
99  //KeyHandling
100  bool SetKeyComment(const std::string& Key, const std::string& Comment, const std::string& Section = "");
101  bool SetSectionComment(const std::string& Section, const std::string& Comment);
102  bool DeleteKey(const std::string& Key, const std::string& FromSection = "");
103  bool DeleteSection(const std::string& Section);
104  bool DeleteSectionsWithKeyValue(const std::string& key, const std::string& value);
105  bool CreateKey(const std::string& Key, const std::string& Value, const std::string& Comment = "", const std::string& Section = "");
106 
107  //Section stuff
108  bool CreateSection(const std::string& Section, const std::string& Comment = "");
109  bool CreateSection(const std::string& Section, const std::string& Comment, KeyList Keys);
110 
111  // Utility Methods
112  size_t SectionCount();
113  size_t KeyCount();
114  size_t KeyCount(const std::string& section);
115  void Clear();
116  void SetFileName(const std::string& fName);
117  std::string CommentStr(std::string& Comment);
118  IniKey* GetKey(const std::string& Key, const std::string& Section);
119  IniSection* GetSection(const std::string& Section, bool create = false);
120  IniSection* GetSection(const unsigned int secNr);
121  bool ClearSection(const std::string& section);
122  bool IsModified() { return mIsDirty; }
123  };
126 }
127 #endif