ini file reader/writer

This is a discussion on ini file reader/writer within the Game Programming forums, part of the General Programming Boards category; does anybody know of a class or set of functions to read/write ini files? If not are there tutorials on ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    ini file reader/writer

    does anybody know of a class or set of functions to read/write ini files? If not are there tutorials on this subject, i was starting then i realized my way wasn't very efficient.

    (sorry if this had already been asked, the search wouldn't let me search for "ini")

    [my bad, i meant to post this in c++, i didn't realize i was here]

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    look at the GetPrivateProfileString(), WritePrivateProfileString(), GetPrivateProfileInt(), WritePrivateProfileInt() functions. then write your own class/object to wrap them up.

    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    403
    thanks, but i should have specified that i was looking for a pure C++ method to do this, not WinAPI.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    In that case, i can't help ya. Unless you can find a class on the web that someone else has already written it think you'll find that you'll have to code one yourself.

    BTW..You can still use the Windows API calls through your console application if you want.

    Good luck.
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    yeah, i've used the WinAPI way before in console

    I'm using SDL and I wanted to make it pure C/C++ so it could go to any system that SDL can go on. I found a solution (wrote my own config file format) though, but thanks for the help.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    There is nothing special about ini files. Just open up the file stream like normal and start writing. Read it in a similar way. Any questions, ask.

  7. #7
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    I wrote a Parser module that reads files that are something like .ini for my game (not strictly, however, as I found the following syntax easier to parse, and easier to modify). The following is a bit simplified, and you are welcome to get the source for the game, but hopefully this will help you, or at least provide some insight into an implementation that worked rather nicely for me.

    My parser has a few key classes..

    The SimpleParser. This drives the parsing. It maintains a map of strings and actions. It reads strings until a certain token is hit (in this example, both { and = tell the parser to call an action, a } tells the parser to stop).

    There is also a ParseAction base class, which is simple and looks like this

    Code:
    class ParseAction {
    public:
       virtual void getValue(istream& input)=0;
       virtual ~ParseAction() { }
    };
    The Simple parser maintains a map<string, ParseAction*>. When hits a { or =, it looks into the map for the corresponding ParseAction matching the string, if it finds a ParseAction, it calls getValue with the stream it has read from.. so the ParseAction will read the value and then exit.

    SimpleParser looks like this.

    Code:
    class SimpleParser {
    public:
       void read();  // starts parsing
       SimpleParser(std::istream& inputStream); // make parser read from input stream
    // action must be dynmically allocated via new
       void addAction(const std::string& indentifer, ParseAction* action);  
    // deletes the actions add with addAction
       ~SimpleParser(); 
    private:
        map<string, ParseAction*> actionMap;
    };
    You can then subclass ParseAction for specific reading behavior. The simplest and easiest to understand ParseAction implementation is a templated StdRead class, which simply wraps operator >> into a ParseAction.

    Code:
    template <class T>
    class StdRead: public ParseAction {
    public:
        StdRead(T& object) :
            obj(object)
        { }
        virtual void getValue(istream& input) {
             input >> obj;
         }
    protected:
        T& obj;
    };
    Now, let's say you want to parse, say, a file for player configuration. Say the file looks like this... the lines can be rearranged in any order.

    Code:
    left=a
    right=d
    up=w
    down=s
    
    hp=100
    mp=15
    speed=12
    The corresponding player structure might look like this.

    Code:
    struct player {
        char left, right, down up;
        string name;
        int hp, mp, speed;
    };
    And the corresponding parser would be like this.

    Code:
    player p;
    SimpleParser parser("knight.cfg");
    
    parser.addAction("left", new StdRead<char>(player.left));
    parser.addAction("up", new StdRead<char>(player.up));
    parser.addAction("right", new StdRead<char>(player.right));
    parser.addAction("down", new StdRead<char>(player.down));
    parser.addAction("name", new StdRead<string>(player.name));
    parser.addAction("hp", new StdRead<int>(player.hp));
    parser.addAction("mp", new StdRead<int>(player.mp));
    parser.addAction("speed", new StdRead<int>(player.speed));
    
    parser.read();
    By subclassing ParseAction you can extend the behavior in a modular manner, and reuse it. Perhaps you want to be able to nest things.. in that case, you could make a ParseAction that calls a Parser .. or perhaps you might want to know if an action was called.. you could write a subclass CheckedRead, which is like StdRead, except it takes a bool that it sets to true if it's getValue function was called.

    I don't know if this has helped you at all (I hope it has though.. ), I didn't handle writing the files, but that is the easier half of the deal.

    Here is source documentation from the Parser module of my game, that perhaps provides a bigger picture.

    http://bomberlan.sourceforge.net/html/a00219.html
    Last edited by SilentStrike; 06-11-2002 at 04:31 AM.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    wow, thanks

    that was an excellent explanation SilentStrike.. It is also an ingenious technique thanks so much for sharing it , i'm sure that will help me out tons

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21