Sunday, 16 March 2008

XML based script for use in tools

Some months ago, I was asked to find a way to store user profiles and other internal application data so it could be saved for later use and shared between the application users, not a big amount of information, but enough to avoid standard .ini files.

I had been using TinyXml in the past, but I do not really like the interface to access or store information (not really a problem of TinyXml but the xml itself), so I decided to use it but also to implement a small layer over it, to have a better interface from the application and at the same time hide as much as possible the standard xml usage.

Some small issues appeared though, as I had to encode regional characters into the xml files, so UTF-8 was selected as the xml encoding. I created a couple of functions to convert from ansi to utf8 and from utf8 to ansi, so save standard local text was possible (this is not intended to be a unicode universal conversion tool, but with few changes it can be used with any language and codepage).

Once the encoding problem was solved, I started to work on the intermediate layer, to enable the applications to store and retrieve data without any xml knowledge and doing it with the less possible amount of effort (the objective was to allow users to store and retrieve data, not to force then to learn the xml internals).

The result is a small class called DataScript (the name clearly indicates the intended use, it is not a script for decision making, it is just for data storage).

As a small example of use, look at this code to store and load some bits of information:


****** SAVE ******

DataScript root;
    DataScript usersDataSection = root.addSection("USERS_DATA");
    for (int i=0; i<mNumUsers; i++)
        DataScript userSection = usersDataSection.addSection("USER");

        userSection.setString("NAME", mUserData[i].mName.c_str());
        userSection.setUInt("ID", mUserData[i].mId);


******* LOAD *******

DataScript root;
if (root.loadFile(""))
    DataScript usersSection = root["USERS_DATA"];
    int numUsers = usersSection.count();
    for (int i=0; i<numUsers; i++)
        DataScript userData = usersSection[i];

        sUserData * user = mUserData.addUser();

        user->mName = userData.getString("NAME", "");
        user->mId = userData.getUInt("ID", 0);

****** XML File generated *******

<?xml version="1.0" encoding="UTF-8" ?>
            <NAME Str="Jaico" />
            <ID UInt="1001" />
            <NAME Str="Cherno" />
            <ID UInt="2002" />


Feel free to contact me if you need something like this, I will be happy to help in any way my limited free time allows me. I still have to search a place to store it and post a link to it, but in the meantime I can send it by email to anyone interested, just send me an email to cherno.olivares at the google mail system.

And this is all for this weekend, I have a really busy months in the horizon, but I will do my best to continue writing some articles.

Have a great day!


No comments: