134 lines
3.1 KiB
C++
134 lines
3.1 KiB
C++
#ifndef headerfileiniparser
|
|
#define headerfileiniparser
|
|
|
|
/*!
|
|
\file iniparser.h
|
|
\brief A Parser witch we use for parsing 'ini' files
|
|
*/
|
|
|
|
#include <string>
|
|
#include <fstream>
|
|
#include <map>
|
|
|
|
|
|
/*!
|
|
\brief it implements the parser for reading 'ini' files
|
|
|
|
how it works?
|
|
first you have to create an object of this class
|
|
IniParser ini_parser;
|
|
then you must associate a pattern and its value which'll be set after parsing
|
|
ini_parser.Associate( "sec|pattern", &value );
|
|
where 'value' is std::string type
|
|
|
|
when the parser meet in the file following sequences:
|
|
|
|
---- file.ini -------
|
|
# some commentary
|
|
[sec]
|
|
pattern = some_values with space itd
|
|
---------------------
|
|
|
|
then the value 'some_values with space etc.' will be set into the 'value' variable,
|
|
you can use '#' as first character in a line which means the line is only a commentary,
|
|
a pattern and its value have to be in one line.
|
|
|
|
|
|
you can also read the whole section, for example:
|
|
[test]
|
|
item1 = 10
|
|
item2 = 20
|
|
item3 = 30
|
|
etc.
|
|
by using ...
|
|
*/
|
|
class IniParser
|
|
{
|
|
public:
|
|
|
|
typedef std::map<std::string, std::string> Section;
|
|
|
|
enum Error
|
|
{
|
|
err_ok = 0,
|
|
err_file_end,
|
|
err_is_section,
|
|
|
|
err_cant_open_file,
|
|
err_incorrect_character
|
|
};
|
|
|
|
IniParser();
|
|
|
|
/*!
|
|
these methods associate pattern and its value
|
|
(look at a description of this class)
|
|
*/
|
|
void Associate(const char * pattern, std::string * result);
|
|
void Associate(const std::string & pattern, std::string * result);
|
|
|
|
/*!
|
|
these methods associate pattern to the whole its section
|
|
*/
|
|
void Associate(const char * pattern, std::map<std::string, std::string> * result);
|
|
void Associate(const std::string & pattern, std::map<std::string, std::string> * result);
|
|
|
|
/*!
|
|
this method clears patterns and their values
|
|
*/
|
|
void Clear();
|
|
|
|
|
|
/*!
|
|
when the parser could not read correctly a ini file
|
|
this method returns a number of a bad line
|
|
*/
|
|
int GetBadLine();
|
|
|
|
|
|
/*!
|
|
this method attempts to read a file
|
|
|
|
if the reading was good it returns 'err_ok'
|
|
else it returns either 'err_cant_open_file' or 'err_incorrect_character'
|
|
|
|
*/
|
|
Error ReadFromFile(const char * path);
|
|
|
|
|
|
private:
|
|
|
|
typedef std::map<std::string, std::string *> Table;
|
|
typedef std::map<std::string, Section *> TableWholeSection;
|
|
|
|
Table table;
|
|
TableWholeSection table_whole_section;
|
|
std::ifstream file;
|
|
int line;
|
|
bool strip_white_characters_from_value;
|
|
bool convert_value_to_small_letters;
|
|
|
|
Error Read();
|
|
Error ReadSection(std::string & section);
|
|
int SkipCommentaryAndEmptyLines();
|
|
//Error CheckEndOfLine();
|
|
Error ReadExpression(std::string & pattern, std::string & value);
|
|
void CheckAndSet(const std::string & section, const std::string & pattern, const std::string & value);
|
|
bool IsWhiteCharacter(int c);
|
|
void SkipWhiteCharacters();
|
|
int ReadCharacter();
|
|
void ReturnCharacter(int c);
|
|
bool IsSectionCharacter(int c);
|
|
bool IsPatternCharacter(int c);
|
|
bool IsValueCharacter(int c);
|
|
void SkipLine();
|
|
int LowerCase(int c);
|
|
|
|
public:
|
|
static void CheckWhiteCharacters(const char * string, const char ** start, const char ** end);
|
|
static void StripWhiteCharacters(std::string & string);
|
|
};
|
|
|
|
|
|
#endif
|