-
Parsing a INI file
I am attempting to parse a ini file but I can't seem to find the commands I can use.
Code:
section main { //the start
/////////////////////////////////////////////
// MAP SETTINGS
//
/////////////////////////////////////////////
ShowThingsOnMap="TRUE"; //show stuff
ThingsAttributes="TRUE"; //prints stuff
FlyingThings="TRUE"; //prints small squares for all missiles
ShowBigThingsNames="TRUE"; //prints boss stats
} //the end
That is a sample part of the INI (with values/variables changed). My problem is finding the right functions and how to pass the right variables through them. I searched the net, and couldn't find anything on parsing text files, just writing to them and reading from them. And how to move the pointer in the file ahead a certain number of places.
-
win32 api has a few functions that maintain .ini files, but if you can't use those, then you will have to write your own program.
All .ini files I have seen are in this form
Code:
[ header1-tag ]
ShowThingsOnMap="TRUE"; //show stuff
ThingsAttributes="TRUE"; //prints stuff
FlyingThings="TRUE"; //prints small squares for all missiles
ShowBigThingsNames="TRUE"; //prints boss stats
[ header2-tag ]
ShowThingsOnMap="TRUE"; //show stuff
ThingsAttributes="TRUE"; //prints stuff
FlyingThings="TRUE"; //prints small squares for all missiles
ShowBigThingsNames="TRUE"; //prints boss stats
[ header3-tag]
Your program needs to first find the header-tag text. After that is found the progam can parse each of the "value=" lines. use getline() to read each line into a std::string object, then use normal std::string functions to search for the text you want.
Code:
#include <fstream>
#include <string>
int main()
{
std:: fstream in("filename");
std::string line;
while( getline(in,line))
{
// put your code here
}
-
So I would just gather up the whole line, then use string functions to search? That seems so simple, I don't know why I didn't think of that.
-
Wraithan,
take a look in GetPrivateProfileString() API... it parse .ini file pretty similar to your ones
jmgk
-
Thanks for the help so far!
Ok, I found a way for me to parse the variable from the value, and display them. The problem is when I use 2 ints as the arguements for .substr the second one not used... Also, my next step is going to be to make it stop displaying the lines of //////////////// so any tips on that are welcome.
Code:
#include <iostream>
#include <fstream>
#include <string>
int main ( int argc , char ** argv )
{
std::string filename = "C:\\Program Files\\EasyPlayer\\easyplay.ini";
if (argc >= 2) filename = argv[1];
std::ifstream file( filename.c_str() , std::ios::in );
std::cout << "Reading from: " << filename << std::endl;
std::string line;
while (getline( file , line ))
{
if (line.empty() || (line == "\r")) continue;
int equal_pos = line.find('=');
int comment_pos = line.find('/' && '/');
std::string key = line.substr(0,equal_pos);
std::string value = line.substr(equal_pos+1,comment_pos);
std::string comment = line.substr(comment_pos+2,line.size()-comment_pos-1);
std::cout << "Key: " << key << " " << "Value: " << value << " " << "Comments: " << comment << std::endl;
}
std::cin.get();
}
~Wraith
-
Nvm, changing my stratigy, going to parse one character at a time, seems like it would be easier.
-
All c++ line comments begin with double slash, not single slash. Stripping comments should be done first, before any other checks. If, after that, the line is empty then just continue reading the next line.
And parsing the file one character at a time will not make it any easier.
Code:
int comment_pos = line.find("//");
if(comment_pos >= 0)
line = line.substr(0,comment_pos);
// do the rest of the checking here