Advertisement
Guest User

Untitled

a guest
May 28th, 2017
562
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.72 KB | None | 0 0
  1. [test]
  2. host = localhost
  3. user = root
  4. pass = xxxx
  5. db = age_of_warriors_1_0_0
  6. charset = utf8
  7. /*
  8. Name: IniParser.h
  9. Copyright: GNU
  10. Author: Jiri Dubansky <Mr.S1lent.cz@gmail.com>
  11. Version: 1.0.0 beta
  12. Date: 2.6.2010
  13. Description: C++ ini file parser
  14. */
  15.  
  16. #ifndef INIPARSER_H_
  17. #define INIPARSER_H_
  18. #include <map>
  19.  
  20. #define BUFFER_MAXSIZE 256
  21.  
  22. struct ltstr
  23. {
  24.   bool operator()(const char* s1, const char* s2) const
  25.   {
  26.     return strcmp(s1, s2) < 0;
  27.   }
  28. };
  29.  
  30. typedef struct {
  31.     std::map<char*, char*, ltstr> key;
  32.     std::map<char*, std::map<char*, char*, ltstr>, ltstr > option;
  33.     const char *value;
  34. } ini_file;
  35.  
  36. class IniParser {
  37.  
  38.     public:
  39.  
  40.     int loadFile( const char *file_path, int scanner_mode = 0 );
  41.     std::map<char*, std::map<char*, char*, ltstr>, ltstr > * getAllOptions();
  42.     std::map<char*, char*, ltstr> * getAllKeys();
  43.     const char * getValue( char *p_key,  char *p_option = 0 );
  44.  
  45.     private:
  46.  
  47.     ini_file inifile;
  48.     std::ifstream ini;
  49.     int mode;
  50.     int fc;
  51.  
  52. };
  53. #endif
  54. /*
  55. Name: IniParser.cpp
  56. Copyright: GNU
  57. Author: Jiri Dubansky <Mr.S1lent.cz@gmail.com>
  58. Version: 1.0.0 beta
  59. Date: 2.6.2010
  60. Description: C++ ini file parser
  61. */
  62.  
  63. #include <stdlib.h>
  64. #include <iostream>
  65. #include <fstream>
  66. #include <map>
  67. #include <string>
  68. #include <string.h>
  69. #include "IniParser.h"
  70. using namespace std;
  71.  
  72.  
  73.  
  74.  
  75. int IniParser::loadFile( const char *file_path, int scanner_mode ) {
  76. printf("load\n");
  77.     ini.open( file_path, ifstream::out );
  78.     mode = scanner_mode;
  79. printf("load2\n");
  80.     if ( ini.is_open() ) {
  81.         char line[BUFFER_MAXSIZE], *line_buffer, *option_buffer, *key_buffer;
  82.         char *value, *var;
  83.         option_buffer = 0;
  84. printf("load3\n");
  85.         while( ini.good() ) {
  86.             ini.getline( line, BUFFER_MAXSIZE );
  87.             line_buffer = strtok( line, " " );
  88.              int i = 0;
  89.             key_buffer = 0;
  90. printf("load4\n");
  91.             while( line_buffer != 0 ) {
  92. printf("Line = %s\n", line_buffer);
  93.                 if ( i == 0 && line_buffer[0] == '[' && scanner_mode != 0 ) {
  94.                     if ( option_buffer ) {
  95.                 printf("uy");
  96.                         inifile.option.insert( make_pair( option_buffer, inifile.key ) );
  97.                     }
  98.                         option_buffer = (char * )malloc( strlen( line_buffer ) +1 );
  99.                         strcpy( option_buffer, line_buffer );
  100.                     i++;
  101.                     continue;
  102.                 }
  103.                 if ( i == 0 && line_buffer[0] != '[' ) {
  104.                         key_buffer = (char*) malloc(strlen(line_buffer)+0);
  105.  
  106.                     strcpy( key_buffer, line_buffer );
  107.                 }
  108.                 if ( i == 2 && sizeof( &key_buffer ) > 0 ) {
  109.                     value = (char * )malloc( strlen( line_buffer ) +1 );
  110.                     strcpy(value, line_buffer);
  111.                     var = (char * )malloc( strlen( key_buffer ) +1 );
  112.                     strcpy(var, key_buffer);
  113.                     //cout << "key: " << key_buffer << " value: " << line << "\n";
  114. printf("Make pair %s => %s\n", var, value);
  115.                     inifile.key.insert( make_pair( var, value ) );
  116. printf("Mapsize = %d\n", inifile.key.size());
  117.                     //inifile.key.insert( make_pair( key_buffer, line_buffer ) );
  118.                 }
  119.                 i++;
  120.                 line_buffer = strtok( 0, " " );
  121.             }
  122.         }
  123.     if ( option_buffer ) {
  124. printf("!!insert option %s\n", option_buffer);
  125.         inifile.option.insert( make_pair( option_buffer, inifile.key ) );
  126. printf("option size = %d\n", inifile.option.size());
  127.     }
  128.         fc = 0;
  129.     } else {
  130.         fc = -1;
  131.     }
  132.     return fc;
  133. }
  134.  
  135. map<char *, map<char*, char*, ltstr>, ltstr> * IniParser::getAllOptions() {
  136.     return &(inifile.option);
  137. }
  138.  
  139. map<char *, char *, ltstr> * IniParser::getAllKeys() {
  140.     return &(inifile.key);
  141. }
  142.  
  143. const char * IniParser::getValue( char *p_key,  char *p_option ) {
  144.     if ( fc == 0 ) {
  145. //        char *key, *option;
  146. //        strcpy( key, p_key );
  147.         if ( mode != 0 && p_option) {
  148. //            strcpy( option, p_option );
  149. //            map<char *, char *> map_option;
  150.             map<char *, map<char *, char *, ltstr>, ltstr>::iterator it_option;
  151. printf("f1 - %s\n", p_option);
  152.             it_option = inifile.option.find(p_option );
  153. //            it_option = inifile.option.find("test");
  154. printf("f2\n");
  155.             if ( it_option != inifile.option.end() ) {
  156. printf("Option found\n");
  157. //                map_option = it_option->second;
  158.                 map<char *, char *, ltstr>::iterator it_key;
  159.                 it_key = it_option->second.find( (char*)p_key );
  160.                 if ( it_key != inifile.key.end() ) {
  161.                     return (*it_key).second;
  162.                 }
  163.             }
  164.         } else {
  165. printf("a\n");
  166.             map<char *, char *, ltstr>::iterator it;
  167. printf("b\n");
  168.             it = inifile.key.find( p_key );
  169. printf("find = %s\n", p_key);
  170. printf("c\n");
  171.             if ( it != inifile.key.end() ) {
  172. printf("d\n");
  173.                 //cout << "key: " << (*it).first << " value: " << (*it).second << "\n";
  174.                 return (*it).second;
  175.             }
  176. printf("e\n");
  177.         }
  178.     }
  179.     return "";
  180. }
  181. #include <stdio.h>
  182. #include <stdlib.h>
  183. #include <iostream>
  184. #include <fstream>
  185. #include <string.h>
  186. #include <map>
  187. #include <vector>
  188. #include <string>
  189. #include "IniParser.h"
  190. using namespace std;
  191.  
  192. int main( int argc, char *argv[] ) {
  193.     IniParser ini;
  194.     if( ini.loadFile("database.ini") == 0 ) {
  195.         cout << "user je: " << ini.getValue( "user") << "\n";
  196.         cout << "host je: " << ini.getValue( "host") << "\n";
  197.     } else {
  198.         cout << "Nepodarilo se otevrit soubor\n";
  199.     }
  200.     return 0;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement