Profile Functions Or Ini Functions In General


Pickle

Mega GP Mania
Joined
May 30, 2006
Messages
5,518
Location
Detroit, Michigan
Website
Visit site
Anyone know if there source for such functions? If you dont know what im refering to, im looking for functions that handle INI structure.

Example:

[section]
thisismystring = "blahblah"
thisismyint = 3
 
This is the code I use in gmenu2x to read from the /usr/gp2x/common.ini file.

Code:
ifstream inf("/usr/gp2x/common.ini", ios_base::in);
if (inf.is_open()) {
	string line;
	string section = "";
	while (getline(inf, line, '\n')) {
		line = trim(line);
		if (line[0]=='[' && line[line.length()-1]==']') {
			section = line.substr(1,line.length()-2);
		} else {
			string::size_type pos = line.find("=");
			string name = trim(line.substr(0,pos));
			string value = trim(line.substr(pos+1,line.length()));
			if (section=="usbnet") {
				if (name=="enable")
					usbnet = value=="true" ? true : false;
				else if (name=="ip")
					ip = value;
			} else if (section=="server") {
				if (name=="inet")
					inet = value=="true" ? true : false;
				else if (name=="samba")
					samba = value=="true" ? true : false;
				else if (name=="web")
					web = value=="true" ? true : false;
			}
		}
	}
	inf.close();
}

The trim function:
Code:
string trim(const string& s) {
  if(s.length() == 0)
	return s;
  int b = s.find_first_not_of(" \t\r");
  int e = s.find_last_not_of(" \t\r");
  if(b == -1) // No non-spaces
	return "";
  return string(s, b, e - b + 1);
}
 
A friend of mine used LUA for configuration files. While it doesn't parse real ini files it's not so hard to use LUA for config files.
 
Back
Top