Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "./IniHandler.h"
- /*-----------------------------------------------------------
- Function: ctor
- Parameters:
- [in] fileName - INI file name
- ctor
- -------------------------------------------------------------*/
- IniHandler::IniHandler(const MyStringAnsi & fileName)
- {
- FILE * file = NULL;
- my_fopen(&file, fileName.GetConstString(), "rb");
- if (file == NULL)
- {
- printf("Failed to open file %s.", fileName.GetConstString());
- return;
- }
- fseek(file, 0, SEEK_END);
- uint32 dataSize = ftell(file);
- fseek(file, 0, SEEK_SET);
- char * memory = new char[dataSize + 1];
- fread(memory, sizeof(char), dataSize, file);
- memory[dataSize] = 0;
- fclose(file);
- this->Load(memory);
- delete[] memory;
- }
- IniHandler::IniHandler(const char * memory, int bufferSize)
- {
- char * tmp = new char[bufferSize + 1];
- memcpy(tmp, memory, bufferSize);
- tmp[bufferSize] = 0;
- this->Load(tmp);
- delete[] tmp;
- }
- /*-----------------------------------------------------------
- Function: dtor
- dtor
- -------------------------------------------------------------*/
- IniHandler::~IniHandler()
- {
- }
- /*-----------------------------------------------------------
- Function: Load
- Parametrs:
- [in] memory - content of ini file loaded in memory
- Parse memory and fill hash-tables with ini file structure
- -------------------------------------------------------------*/
- void IniHandler::Load(char * memory)
- {
- MyStringAnsi tmp = memory;
- MyStringAnsi section;
- std::vector<MyStringAnsi> lines = tmp.Split("\n");
- for (uint32 i = 0; i < lines.size(); i++)
- {
- if (lines[i].GetLength() == 0)
- {
- continue;
- }
- MyStringAnsi line = lines[i];
- line.Trim();
- if (line.GetLength() == 0)
- {
- continue;
- }
- if (line[0] == '#')
- {
- continue;
- }
- if (line[0] == '[')
- {
- line.Replace("[", "");
- line.Replace("]", "");
- section = line;
- continue;
- }
- if (line.Find("#"))
- {
- std::vector<MyStringAnsi> tmp = line.Split("#");
- line = tmp[0];
- }
- std::vector<MyStringAnsi> entry = line.Split("=");
- if (entry.size() != 2)
- {
- continue;
- }
- MyStringAnsi key = entry[0];
- MyStringAnsi value = entry[1];
- key.Trim();
- value.Trim();
- this->values[section][key] = value;
- }
- }
- bool IniHandler::KeyExist(const MyStringAnsi & section, const MyStringAnsi & key)
- {
- if (this->values[section].find(key) == this->values[section].end())
- {
- return false;
- }
- return true;
- }
- /*-----------------------------------------------------------
- Function: ReadInt
- Parameters:
- [in] section - section
- [in] key - key within section
- Returns:
- int value
- Read int value from INI file
- -------------------------------------------------------------*/
- int IniHandler::ReadInt(const MyStringAnsi & section, const MyStringAnsi & key)
- {
- if (!this->KeyExist(section, key))
- {
- printf("Key %s not found in section %s.", key.GetConstString(), section.GetConstString());
- return 0;
- }
- //int value = MyUtils::Utils::StringToInt(this->values[section][key]);
- int value = atoi(this->values[section][key].GetConstString());
- return value;
- }
- /*-----------------------------------------------------------
- Function: ReadFloat
- Parameters:
- [in] section - section
- [in] key - key within section
- Returns:
- float value
- Read float value from INI file
- -------------------------------------------------------------*/
- float IniHandler::ReadFloat(const MyStringAnsi & section, const MyStringAnsi & key)
- {
- if (!this->KeyExist(section, key))
- {
- printf("Key %s not found in section %s.", key.GetConstString(), section.GetConstString());
- return 0;
- }
- float value = static_cast<float>(atof(this->values[section][key].GetConstString()));
- return value;
- }
- std::vector<float> IniHandler::ReadFloatArray(const MyStringAnsi & section, const MyStringAnsi & key)
- {
- std::vector<float> arr;
- if (!this->KeyExist(section, key))
- {
- printf("Key %s not found in section %s.", key.GetConstString(), section.GetConstString());
- return arr;
- }
- MyStringAnsi data = this->values[section][key];
- data.Replace("f", "");
- char * delimeters = ",{}";
- std::vector<MyStringAnsi> splitted = data.Split(delimeters);
- for (uint32 i = 0; i < splitted.size(); i++)
- {
- splitted[i].Replace(",", ".");
- splitted[i].Trim();
- float value = static_cast<float>(atof(splitted[i].GetConstString()));
- arr.push_back(value);
- }
- return arr;
- }
- std::vector<uint32> IniHandler::ReadUInt32Array(const MyStringAnsi & section, const MyStringAnsi & key)
- {
- std::vector<uint32> arr;
- if (!this->KeyExist(section, key))
- {
- printf("Key %s not found in section %s.", key.GetConstString(), section.GetConstString());
- return arr;
- }
- MyStringAnsi data = this->values[section][key];
- char * delimeters = ",";
- std::vector<MyStringAnsi> splitted = data.Split(delimeters);
- for (uint32 i = 0; i < splitted.size(); i++)
- {
- splitted[i].Trim();
- arr.push_back((uint32)atoi(splitted[i].GetConstString()));
- }
- return arr;
- }
- /*-----------------------------------------------------------
- Function: ReadBool
- Parameters:
- [in] section - section
- [in] key - key within section
- Returns:
- bool value
- Read bool value from INI file. Value is saved in INI
- as "true" / "false"
- -------------------------------------------------------------*/
- bool IniHandler::ReadBool(const MyStringAnsi & section, const MyStringAnsi & key)
- {
- if (!this->KeyExist(section, key))
- {
- printf("Key %s not found in section %s.", key.GetConstString(), section.GetConstString());
- return false;
- }
- MyStringAnsi tmp = this->ReadString(section, key);
- if (tmp.Find("true")) return false;
- return true;
- }
- /*-----------------------------------------------------------
- Function: ReadString
- Parameters:
- [in] section - section
- [in] key - key within section
- Returns:
- string value
- Read string value from INI file
- -------------------------------------------------------------*/
- MyStringAnsi IniHandler::ReadString(const MyStringAnsi & section, const MyStringAnsi & key)
- {
- return this->values[section][key];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement