Advertisement
Guest User

C++ IniReader

a guest
Aug 13th, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.08 KB | None | 0 0
  1. #include "./IniHandler.h"
  2.  
  3.  
  4.  
  5. /*-----------------------------------------------------------
  6. Function:   ctor
  7. Parameters:
  8.     [in] fileName - INI file name
  9.  
  10.     ctor
  11. -------------------------------------------------------------*/
  12. IniHandler::IniHandler(const MyStringAnsi & fileName)
  13. {
  14.     FILE * file = NULL;
  15.     my_fopen(&file, fileName.GetConstString(), "rb");
  16.  
  17.     if (file == NULL)
  18.     {
  19.         printf("Failed to open file %s.", fileName.GetConstString());
  20.         return;
  21.     }
  22.  
  23.     fseek(file, 0, SEEK_END);
  24.     uint32 dataSize = ftell(file);
  25.     fseek(file, 0, SEEK_SET);
  26.  
  27.     char * memory = new char[dataSize + 1];
  28.  
  29.     fread(memory, sizeof(char), dataSize, file);
  30.  
  31.     memory[dataSize] = 0;
  32.  
  33.     fclose(file);
  34.  
  35.     this->Load(memory);
  36.  
  37.     delete[] memory;
  38. }
  39.  
  40. IniHandler::IniHandler(const char * memory, int bufferSize)
  41. {
  42.     char * tmp = new char[bufferSize + 1];
  43.  
  44.     memcpy(tmp, memory, bufferSize);
  45.     tmp[bufferSize] = 0;
  46.  
  47.     this->Load(tmp);
  48.  
  49.     delete[] tmp;
  50. }
  51.  
  52. /*-----------------------------------------------------------
  53. Function:   dtor
  54.  
  55. dtor
  56. -------------------------------------------------------------*/
  57. IniHandler::~IniHandler()
  58. {  
  59. }
  60.  
  61.  
  62. /*-----------------------------------------------------------
  63. Function:   Load
  64. Parametrs:
  65.     [in] memory - content of ini file loaded in memory
  66.  
  67. Parse memory and fill hash-tables with ini file structure
  68. -------------------------------------------------------------*/
  69. void IniHandler::Load(char * memory)
  70. {
  71.     MyStringAnsi tmp = memory;
  72.  
  73.     MyStringAnsi section;
  74.     std::vector<MyStringAnsi> lines = tmp.Split("\n");
  75.  
  76.     for (uint32 i = 0; i < lines.size(); i++)
  77.     {
  78.         if (lines[i].GetLength() == 0)
  79.         {
  80.             continue;
  81.         }
  82.  
  83.         MyStringAnsi line = lines[i];
  84.         line.Trim();
  85.  
  86.         if (line.GetLength() == 0)
  87.         {
  88.             continue;
  89.         }
  90.  
  91.         if (line[0] == '#')
  92.         {
  93.             continue;
  94.         }
  95.  
  96.         if (line[0] == '[')
  97.         {          
  98.             line.Replace("[", "");
  99.             line.Replace("]", "");
  100.             section = line;
  101.  
  102.             continue;
  103.         }
  104.  
  105.         if (line.Find("#"))
  106.         {
  107.             std::vector<MyStringAnsi> tmp = line.Split("#");
  108.             line = tmp[0];
  109.         }
  110.  
  111.         std::vector<MyStringAnsi> entry = line.Split("=");
  112.         if (entry.size() != 2)
  113.         {
  114.             continue;
  115.         }
  116.         MyStringAnsi key = entry[0];
  117.         MyStringAnsi value = entry[1];
  118.  
  119.         key.Trim();
  120.         value.Trim();
  121.  
  122.         this->values[section][key] = value;
  123.  
  124.  
  125.     }
  126.  
  127. }
  128.  
  129. bool IniHandler::KeyExist(const MyStringAnsi & section, const MyStringAnsi & key)
  130. {
  131.     if (this->values[section].find(key) == this->values[section].end())
  132.     {
  133.         return false;
  134.     }
  135.  
  136.     return true;
  137. }
  138.  
  139. /*-----------------------------------------------------------
  140. Function:   ReadInt
  141. Parameters:
  142.     [in] section - section
  143.     [in] key - key within section
  144. Returns:
  145.     int value
  146.  
  147. Read int value from INI file
  148. -------------------------------------------------------------*/
  149. int IniHandler::ReadInt(const MyStringAnsi & section, const MyStringAnsi & key)
  150. {
  151.     if (!this->KeyExist(section, key))
  152.     {
  153.         printf("Key %s not found in section %s.", key.GetConstString(), section.GetConstString());
  154.         return 0;
  155.     }
  156.  
  157.     //int value = MyUtils::Utils::StringToInt(this->values[section][key]);
  158.     int value = atoi(this->values[section][key].GetConstString());
  159.     return value;
  160. }
  161.  
  162. /*-----------------------------------------------------------
  163. Function:   ReadFloat
  164. Parameters:
  165.     [in] section - section
  166.     [in] key - key within section
  167. Returns:
  168.     float value
  169.  
  170. Read float value from INI file
  171. -------------------------------------------------------------*/
  172. float IniHandler::ReadFloat(const MyStringAnsi & section, const MyStringAnsi & key)
  173. {  
  174.  
  175.     if (!this->KeyExist(section, key))
  176.     {
  177.         printf("Key %s not found in section %s.", key.GetConstString(), section.GetConstString());
  178.         return 0;
  179.     }
  180.     float value = static_cast<float>(atof(this->values[section][key].GetConstString()));
  181.     return value;
  182. }
  183.  
  184. std::vector<float> IniHandler::ReadFloatArray(const MyStringAnsi & section, const MyStringAnsi & key)
  185. {
  186.     std::vector<float> arr;
  187.  
  188.     if (!this->KeyExist(section, key))
  189.     {
  190.         printf("Key %s not found in section %s.", key.GetConstString(), section.GetConstString());
  191.         return arr;
  192.     }
  193.  
  194.     MyStringAnsi data = this->values[section][key];
  195.    
  196.     data.Replace("f", ""); 
  197.     char * delimeters = ",{}";
  198.     std::vector<MyStringAnsi> splitted = data.Split(delimeters);
  199.    
  200.     for (uint32 i = 0; i < splitted.size(); i++)
  201.     {
  202.         splitted[i].Replace(",", ".");
  203.         splitted[i].Trim();
  204.         float value = static_cast<float>(atof(splitted[i].GetConstString()));
  205.         arr.push_back(value);
  206.     }
  207.  
  208.     return arr;
  209. }
  210.  
  211.  
  212. std::vector<uint32> IniHandler::ReadUInt32Array(const MyStringAnsi & section, const MyStringAnsi & key)
  213. {
  214.     std::vector<uint32> arr;
  215.  
  216.     if (!this->KeyExist(section, key))
  217.     {
  218.         printf("Key %s not found in section %s.", key.GetConstString(), section.GetConstString());
  219.         return arr;
  220.     }
  221.  
  222.     MyStringAnsi data = this->values[section][key];
  223.    
  224.    
  225.     char * delimeters = ",";
  226.     std::vector<MyStringAnsi> splitted = data.Split(delimeters);
  227.    
  228.     for (uint32 i = 0; i < splitted.size(); i++)
  229.     {      
  230.         splitted[i].Trim();
  231.         arr.push_back((uint32)atoi(splitted[i].GetConstString()));
  232.     }
  233.  
  234.     return arr;
  235. }
  236.  
  237. /*-----------------------------------------------------------
  238. Function:   ReadBool
  239. Parameters:
  240.     [in] section - section
  241.     [in] key - key within section
  242. Returns:
  243.     bool value
  244.  
  245. Read bool value from INI file. Value is saved in INI
  246. as "true" / "false"
  247. -------------------------------------------------------------*/
  248. bool IniHandler::ReadBool(const MyStringAnsi & section, const MyStringAnsi & key)
  249. {
  250.     if (!this->KeyExist(section, key))
  251.     {
  252.         printf("Key %s not found in section %s.", key.GetConstString(), section.GetConstString());
  253.         return false;
  254.     }
  255.  
  256.     MyStringAnsi tmp = this->ReadString(section, key);
  257.    
  258.     if (tmp.Find("true")) return false;
  259.     return true;
  260. }
  261.  
  262. /*-----------------------------------------------------------
  263. Function:   ReadString
  264. Parameters:
  265.     [in] section - section
  266.     [in] key - key within section
  267. Returns:
  268.     string value
  269.  
  270. Read string value from INI file
  271. -------------------------------------------------------------*/
  272. MyStringAnsi IniHandler::ReadString(const MyStringAnsi & section, const MyStringAnsi & key)
  273. {
  274.     return this->values[section][key]; 
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement