Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [test]
- host = localhost
- user = root
- pass = xxxx
- db = age_of_warriors_1_0_0
- charset = utf8
- /*
- Name: IniParser.h
- Copyright: GNU
- Author: Jiri Dubansky <Mr.S1lent.cz@gmail.com>
- Version: 1.0.0 beta
- Date: 2.6.2010
- Description: C++ ini file parser
- */
- #ifndef INIPARSER_H_
- #define INIPARSER_H_
- #include <map>
- #define BUFFER_MAXSIZE 256
- struct ltstr
- {
- bool operator()(const char* s1, const char* s2) const
- {
- return strcmp(s1, s2) < 0;
- }
- };
- typedef struct {
- std::map<char*, char*, ltstr> key;
- std::map<char*, std::map<char*, char*, ltstr>, ltstr > option;
- const char *value;
- } ini_file;
- class IniParser {
- public:
- int loadFile( const char *file_path, int scanner_mode = 0 );
- std::map<char*, std::map<char*, char*, ltstr>, ltstr > * getAllOptions();
- std::map<char*, char*, ltstr> * getAllKeys();
- const char * getValue( char *p_key, char *p_option = 0 );
- private:
- ini_file inifile;
- std::ifstream ini;
- int mode;
- int fc;
- };
- #endif
- /*
- Name: IniParser.cpp
- Copyright: GNU
- Author: Jiri Dubansky <Mr.S1lent.cz@gmail.com>
- Version: 1.0.0 beta
- Date: 2.6.2010
- Description: C++ ini file parser
- */
- #include <stdlib.h>
- #include <iostream>
- #include <fstream>
- #include <map>
- #include <string>
- #include <string.h>
- #include "IniParser.h"
- using namespace std;
- int IniParser::loadFile( const char *file_path, int scanner_mode ) {
- printf("load\n");
- ini.open( file_path, ifstream::out );
- mode = scanner_mode;
- printf("load2\n");
- if ( ini.is_open() ) {
- char line[BUFFER_MAXSIZE], *line_buffer, *option_buffer, *key_buffer;
- char *value, *var;
- option_buffer = 0;
- printf("load3\n");
- while( ini.good() ) {
- ini.getline( line, BUFFER_MAXSIZE );
- line_buffer = strtok( line, " " );
- int i = 0;
- key_buffer = 0;
- printf("load4\n");
- while( line_buffer != 0 ) {
- printf("Line = %s\n", line_buffer);
- if ( i == 0 && line_buffer[0] == '[' && scanner_mode != 0 ) {
- if ( option_buffer ) {
- printf("uy");
- inifile.option.insert( make_pair( option_buffer, inifile.key ) );
- }
- option_buffer = (char * )malloc( strlen( line_buffer ) +1 );
- strcpy( option_buffer, line_buffer );
- i++;
- continue;
- }
- if ( i == 0 && line_buffer[0] != '[' ) {
- key_buffer = (char*) malloc(strlen(line_buffer)+0);
- strcpy( key_buffer, line_buffer );
- }
- if ( i == 2 && sizeof( &key_buffer ) > 0 ) {
- value = (char * )malloc( strlen( line_buffer ) +1 );
- strcpy(value, line_buffer);
- var = (char * )malloc( strlen( key_buffer ) +1 );
- strcpy(var, key_buffer);
- //cout << "key: " << key_buffer << " value: " << line << "\n";
- printf("Make pair %s => %s\n", var, value);
- inifile.key.insert( make_pair( var, value ) );
- printf("Mapsize = %d\n", inifile.key.size());
- //inifile.key.insert( make_pair( key_buffer, line_buffer ) );
- }
- i++;
- line_buffer = strtok( 0, " " );
- }
- }
- if ( option_buffer ) {
- printf("!!insert option %s\n", option_buffer);
- inifile.option.insert( make_pair( option_buffer, inifile.key ) );
- printf("option size = %d\n", inifile.option.size());
- }
- fc = 0;
- } else {
- fc = -1;
- }
- return fc;
- }
- map<char *, map<char*, char*, ltstr>, ltstr> * IniParser::getAllOptions() {
- return &(inifile.option);
- }
- map<char *, char *, ltstr> * IniParser::getAllKeys() {
- return &(inifile.key);
- }
- const char * IniParser::getValue( char *p_key, char *p_option ) {
- if ( fc == 0 ) {
- // char *key, *option;
- // strcpy( key, p_key );
- if ( mode != 0 && p_option) {
- // strcpy( option, p_option );
- // map<char *, char *> map_option;
- map<char *, map<char *, char *, ltstr>, ltstr>::iterator it_option;
- printf("f1 - %s\n", p_option);
- it_option = inifile.option.find(p_option );
- // it_option = inifile.option.find("test");
- printf("f2\n");
- if ( it_option != inifile.option.end() ) {
- printf("Option found\n");
- // map_option = it_option->second;
- map<char *, char *, ltstr>::iterator it_key;
- it_key = it_option->second.find( (char*)p_key );
- if ( it_key != inifile.key.end() ) {
- return (*it_key).second;
- }
- }
- } else {
- printf("a\n");
- map<char *, char *, ltstr>::iterator it;
- printf("b\n");
- it = inifile.key.find( p_key );
- printf("find = %s\n", p_key);
- printf("c\n");
- if ( it != inifile.key.end() ) {
- printf("d\n");
- //cout << "key: " << (*it).first << " value: " << (*it).second << "\n";
- return (*it).second;
- }
- printf("e\n");
- }
- }
- return "";
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <iostream>
- #include <fstream>
- #include <string.h>
- #include <map>
- #include <vector>
- #include <string>
- #include "IniParser.h"
- using namespace std;
- int main( int argc, char *argv[] ) {
- IniParser ini;
- if( ini.loadFile("database.ini") == 0 ) {
- cout << "user je: " << ini.getValue( "user") << "\n";
- cout << "host je: " << ini.getValue( "host") << "\n";
- } else {
- cout << "Nepodarilo se otevrit soubor\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement