std::string GetString(std::string key)
std::string pet = GetString("dog");
const char * newpet = pet.c_str();
std::string GetString(std::string key)
{
//Check to see if anything got parsed?
if (i == 0)
{
return "";
}
for (int x = 0; x <= i; x++)
{
if (key == iniItem[x]->key)
{
return iniItem[x]->value;
}
}
return "";
}
std::string str = "This is a string"; char* ptr_str = (char*)str.data(); or you can: std::string str = "This is another string": char* ptr_str = new char[ str.size() + 1]; memcpy( (void*)ptr_str, (void*)str.data(), str.size() );
std::string pet = GetString("dog");
char* new_pet = (char*)pet.data();
std::string pet = GetString("dog");
const char * newpet = pet.c_str();
std::cout << "New Pet: " << GetString("dog") << "\n"; //Return string
std::string pet = GetString("dog");
std::string newpert = std::string(pet.c_str());

std::string str = GetString("dog");
char* Buffer = NULL;
if( str.size() )
{
Buffer = new char[ str.size() + 1 ];
memcpy( (void*)Buffer, (void*)str.data(), str.size() );
} else std::cout << "No string was returned by GetString()!" << std::endl;
...
...
// string goes out of scope or is destroyed somewhere..
..
..
std::cout << "New pet: " << Buffer << std::endl;
ifndef CONFIG_H #define CONFIG_H #include <string> #include <fstream> #include <algorithm> #include <iostream> #include <functional> #include <locale> #include <cctype> #include <cstdlib> #include <winsock2.h> #include <mysql.h> /** * Parse a configuration file * * @param fileName The name of the file to parse * @return none */ void parseIniFile(char *fileName); /** * If you are finished with the config item, use this function to cleanup the results * * @return none */ void cleanupIniReader(); /** * Return the value of the requested key in with the string type * * @param key The option key * @return string The value of the requested key */ std::string GetString(std::string key); /** * Return the value of the requested key in with the int type * * @param key The option key * @return int The value of the requested key * @note If item is not an integer (or does not exist) then 0 will be returned. */ int GetInt(std::string key); /** * Return the value of the requested key in with the char type * * @param key The option key * @return char The value of the requested key */ const char *GetChar(std::string key); std::string parseName(std::string value); std::string parseValue(std::string value); std::string trim(std::string s); std::string rtrim(std::string s); std::string ltrim(std::string s); #endif /* CONFIG_H */
#include "config.h"
struct ConfigItems
{
std::string key;
std::string value;
};
ConfigItems* iniItem[1024];
int i = 0;
void parseIniFile(char *fileName)
{
std::string optionValue;
std::ifstream infile;
infile.open(fileName);
//Does the file exist?
if (infile.is_open() != true)
{
return;
}
std::string key;
while (!infile.eof()) // To get you all the lines.
{
getline(infile, optionValue); // Saves the line in STRING.
//Is the option a comment
if (optionValue.substr(0, 1) == "#")
{
continue;
}
key = parseName(optionValue);
if (key.length() > 0)
{
iniItem[i] = new ConfigItems;
iniItem[i]->key = key;
iniItem[i]->value = parseValue(optionValue);
i++;
}
}
i--;
infile.close();
}
void cleanupIniReader()
{
for (int x = 0; x <= i; x++)
{
delete iniItem[x];
}
i = 0;
}
std::string GetString(std::string key)
{
//Check to see if anything got parsed?
if (i == 0)
{
return "";
}
for (int x = 0; x <= i; x++)
{
if (key == iniItem[x]->key)
{
return iniItem[x]->value;
}
}
return "";
}
const char *GetChar(std::string key)
{
//Check to see if anything got parsed?
if (i == 0)
{
return "";
}
for (int x = 0; x <= i; x++)
{
if (key == iniItem[x]->key)
{
return iniItem[x]->value.c_str();
}
}
return "";
}
int GetInt(std::string key)
{
//Check to see if anything got parsed?
if (i == 0)
{
return 0;
}
for (int x = 0; x <= i; x++)
{
if (key == iniItem[x]->key)
{
return atoi(iniItem[x]->value.c_str());
}
}
return 0;
}
std::string parseName(std::string value)
{
size_t found;
found = value.find('=');
if (found > 100)
{
return "";
}
std::string key = value.substr(0, (found-1));
key = trim(key);
return key;
}
std::string parseValue(std::string value)
{
size_t found;
found = value.find('=');
if (found > 100)
{
return "";
}
std::string keyValue = value.substr((found+1));
keyValue = trim(keyValue);
return keyValue;
}
std::string trim(std::string s)
{
return ltrim(rtrim(s));
}
// trim from start
std::string ltrim(std::string s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
// trim from end
std::string rtrim(std::string s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s;
}
#include "config.h"
int main( int argc, char* argv[] )
{
parseIniFile("config.ini");
std::string pet = GetString("pet");
char* new_pet = (char*)pet.data();
cleanupIniReader();
return 0;
}
