inih/ini.h
00001 #ifndef INI_H
00002 #define INI_H
00003 /*
00004  * Copyright (C) Jefferson 2010 <web@jeffersongta.de> / <remis80@gmx.net>
00005  *
00006  http://creativecommons.org/licenses/by/3.0/de/deed.en_US
00007 
00008   You are free:
00009 
00010     * to Share — to copy, distribute and transmit the work
00011     * to Remix — to adapt the work
00012     *
00013 
00014 Under the following conditions:
00015 
00016     *
00017 
00018       Attribution — You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work).
00019 
00020       Attribute this work:
00021       Information
00022       What does "Attribute this work" mean?
00023       The page you came from contained embedded licensing metadata, including how the creator wishes to be attributed for re-use. You can use the HTML here to cite the work. Doing so will also include metadata on your page so that others can find the original work as well.
00024 
00025 With the understanding that:
00026 
00027     * Waiver — Any of the above conditions can be waived if you get permission from the copyright holder.
00028     * Public Domain — Where the work or any of its elements is in the public domain under applicable law, that status is in no way affected by the license.
00029     * Other Rights — In no way are any of the following rights affected by the license:
00030           o Your fair dealing or fair use rights, or other applicable copyright exceptions and limitations;
00031           o The author's moral rights;
00032           o Rights other persons may have either in the work itself or in how the work is used, such as publicity or privacy rights.
00033     * Notice — For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to this web page.
00034 
00035  * Version: r9
00036  * Date (last edit): 11.09.2010
00037  * Function: This C++ include offers functions to read from a file in the
00038  *           INI format and write to a file in the INI format
00039  *           It is easy to use, stable and secure
00040 */
00041 
00042 #include <string>
00043 #include <vector>
00044 #include <fstream>
00045 #include <sstream>
00046 #include <iostream> // DEBUG only
00047 using namespace std;
00048 
00049 #define INI_DEFAULT_SECTION "main"
00050 
00051 #define INI_MAX_LENGTH 300
00052 #define INI_MAX_LINES_PER_SEC 50 // lines every section can have
00053 #define INI_MAX_SECTIONS 20 // maximum amount of sections
00054 
00055 #define INI_LINE_DONTSAVE "ini_dont_save123" // if a line contains this, it won't save
00056 
00057 
00058 class INIParser {
00059  public:
00060   INIParser(const char _filename[]);
00061   ~INIParser();
00062 
00063   int GetInteger(const char section[], const char key[], int defaultValue);
00064   double GetDouble(const char section[], const char key[], double defaultValue);
00065   long GetLong(const char section[], const char key[], long defaultValue);
00066   bool GetBoolean(const char section[], const char key[], bool defaultValue);
00067   string GetString(const char section[], const char key[], string defaultValue);
00068   string Get(const char section[], const char key[], string defaultValue);
00069 
00070   void SetInt(const char section[],const char key[], int newval);
00071   void SetLong(const char section[], const char key[], long newval);
00072   void SetBool(const char section[], const char key[], bool newval);
00073   void SetString(const char section[], const char key[], string newval);
00074   void Set(const char section[], const char key[], const char newval[]);
00075 
00076   void CreateSection(const char section[]);
00077   void RemoveSection(const char section[]);
00078   void RemoveEntry(const char section[], const char key[]);
00079   bool ValidSection(const char section[]);
00080   bool ValidEntry(const char section[], const char key[]);
00081   int CountSections();
00082   int CountEntries();
00083   void Clear();
00084   void Close();
00085   bool Good();
00086 
00087 
00088  private:
00089   // internal functions
00090   string getKeyFromString(string mystring);
00091   string getValueFromString(string mystring);
00092   template <class T>
00093   string numtostring(T num);
00094   template <class T>
00095   T stringtonum(string mystring);
00096 
00097 
00098   // Variables
00099   string filename;
00100   vector<string> sectionnames;
00101   vector< vector<string> > buffer;
00102   bool InitGood; // no errors in constructor (could open the file ?)
00103   bool Closed;
00104   bool withoutSections; // mode without sections
00105 
00106   // cache for some statistics
00107   int sections;
00108   int entries;
00109 
00110 };
00111 #endif // INI_H
 All Classes Functions