INI Notecards with Vanilla LSL

This page contains code from a script that enables a scripter to read and manipulate notecards that contain data in the INI File format. Another version of these functions that uses OSSL functions is available, and has to ability to write data to a notecard. I want to provide a full description of the functions available, but if you’re in a hurry to simply grab them, scroll down to the bottom of this page for the full code.

Contents

Notes

These functions have some limited support for INI style comments within the notecard. Comments must start with a semicolon and occupy a notecard line of their own. They cannot be appended to the end a key/value pair. These functions simply ignore comments. They do not store them, and comments will not be preserved when the notecard is written.

Within a script, the INI data is stored in a single list. Each list element is composed of a section, key, and value delimited by a null character. This is important because a notecard line is limited to 255 characters, so you must keep in mind that any values you store share those 255 characters with the name of the section, the key, and two null characters. Unless you go nuts, this should still be sufficient for most uses.

These functions were written and tested with OpenSimulator’s XEngine in mind. These may work over in SL, but have not been tested there. In addition, SL imposes limits on lists that XEngine does not. As written, these functions also will not function properly on Aurora-Sim. I have used modified versions there, so they’re usable on Aurora-Sim if you’re up to tracking down the different ways scripts function there.

Function Documentation

The following functions are provided: GetINIKey, SetINIKey, GetINISectionList, GetINIKeyList, DeleteINISection,  DeleteINIKey, and KeyExists. A state for reading the notecard is also provided. A description of this state and each function follows.

state ReadINI

This state simply takes a notecard with INI formatted data, reads it, and returns a list that contains the INI data. You’ll probably want this list to be assigned to a global variable, as many of the other functions require this list to be passed to it, and return it. The notecard must, of course, reside in the contents of the same prim as the script using this function.

string GetINIKey(list ini, string inisection, string inikey)

GetINIKey returns the value of a section’s key. It expects a list containing INI data, the name of a section, and the name of the key. If no value is found, it simply returns an empty string. Any leading or trailing spaces are trimmed from the key.

list SetINIKey(list ini, string inisection, string inikey, string inivalue)

This function is passed a list containing INI data, the name of a section, name of a key, and a value assigned to the key. You can use SetINIKey in a few different ways. If the section or key do not yet exist, they will be created. If it does exist, the value will be updated with the one you provide. This function returns a list that contains the newly updated INI data in its entirety. Any leading or trailing spaces are trimmed from the key.

list GetINISectionList(list ini)

GetINISectionList simply returns a list which contains all sections within the INI data passed to it.

list GetINIKeyList(list ini, string inisection)

GetINIKeyList likewise returns a list of all the keys within a specified section from the INI date passed to it.

list DeleteINISection(list ini, string inisection)

This function will remove an entire section from the INI data passed to it, and return a list containing the resulting INI data in its entirety. It removes all key/value pairs associated with the section as well.

list DeleteINIKey(list ini, string inisection, string inikey)

DeleteINIKey deletes a key from a section. It expects a list containing INI data, the name of the section that contains the key, and the name of the key to be removed. A list containing the updated INI data is returned in its entirety.

integer KeyExists(list ini, string inisection, string inikey)

This function can be used simply to check whether or not a key from a given section exists. It returns the LSL constant TRUE if it does, or FALSE if it does not.

Source Code

// Copyright 2012 Michael Bailey (Marcus Llewellyn). All rights reserved.

// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:

// 1. Redistributions of source code must retain the above copyright notice, this list of
//  conditions and the following disclaimer.

// 2. Redistributions in binary form must reproduce the above copyright notice, this list
//  of conditions and the following disclaimer in the documentation and/or other materials
//  provided with the distribution.

// THIS SOFTWARE IS PROVIDED BY MICHAEL BAILEY ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MICHAEL BAILEY OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of Michael Bailey.

string inicardname = "TestCard.ini";
string tempsection = "";
key iniquery = NULL_KEY;
integer inilinenum = 0;
list inidata = [];

string GetINIKey(list ini, string inisection, string inikey) {
    // Retrieve the value for a given section and key. Spaces are trimmed from
    // the result.
    integer inilen = llGetListLength(ini);
    integer i = 0;
    for (i; i<inilen; i++) {
        list temp = llParseString2List(llList2String(ini, i), [llUnescapeURL("%00")], []);
        if (llList2String(temp, 0) == inisection) {
            // Section found, now look for key.
            integer j = 0;
            for (j; j<2; j++) {
                if (llList2String(temp, 1) == inikey) {
                    // Key found, so return value.
                    return llStringTrim(llList2String(temp, 2), STRING_TRIM);
                }
            }
        }
    }
    // No results found.
    return "";
}

list SetINIKey(list ini, string inisection, string inikey, string inivalue) {
    // Store a value for a given section and key. Create new INI entries
    // if they don't already exist. Spaces are trimmed from parameters.
    inisection = llStringTrim(inisection, STRING_TRIM);
    inikey = llStringTrim(inikey, STRING_TRIM);
    inivalue = llStringTrim(inivalue, STRING_TRIM);
    integer inilen = llGetListLength(ini);
    integer sectionpos = -1;
    integer i = 0;
    for (i; i<inilen; i++) {
        list iniline = llParseString2List(llList2String(ini, i), [llUnescapeURL("%00")], []);
        // If the section was found, keep track of it's last known position
        // in the list in case the key isn't found.
        if (llList2String(iniline, 0) == inisection) { sectionpos = i; }
        if ((llList2String(iniline, 0) == inisection) && (llList2String(iniline, 1) == inikey)) {
            // Found the section and key, so replace value.
            string newline = inisection + llUnescapeURL("%00") + inikey + llUnescapeURL("%00") + inivalue;
            return llListReplaceList(ini, [newline], i, i);
        }
    }
    // We didn't find it, so we'll have to create and add a new line to the INI.
    if (sectionpos == -1) {
        // No matching section was found. So create and add it with the new
        // key-value pair.
        ini += [inisection + llUnescapeURL("%00") + inikey + llUnescapeURL("%00") + inivalue];
        return ini;
    } else {
        // There was a matching section, but no matching key. So insert a new
        // INI line at the end of the requested section.
        ini = llListInsertList(ini, [inisection + llUnescapeURL("%00") + inikey + llUnescapeURL("%00") + inivalue], sectionpos + 1);
        return ini;
    }
    return ini;
}

list GetINISectionList(list ini) {
    list sectionlist = [];
    string section = "";
    integer i = 0;
    integer inilength = llGetListLength(ini);
    for (i; i<inilength; i++) {
        list iniline = llParseString2List(llList2String(ini, i), [llUnescapeURL("%00")], []);
        string temp = llList2String(iniline,0);
        if (temp != section) {
            sectionlist += [temp];
            section = temp;
        }
    }
    return sectionlist;
}

list GetINIKeyList(list ini, string inisection) {
    list keylist = [];
    integer i = 0;
    integer inilength = llGetListLength(ini);
    for (i; i<inilength; i++) {
        list iniline = llParseString2List(llList2String(ini, i), [llUnescapeURL("%00")], []);
        if (llList2String(iniline,0) == inisection) {
            keylist += [llList2String(iniline,1)];
        }
    }
    return keylist;
}

list DeleteINISection(list ini, string inisection) {
    integer i = 0;
    integer inilength = llGetListLength(ini);
    for (i; i<inilength; i++) {
        list iniline = llParseString2List(llList2String(ini, i), [llUnescapeURL("%00")], []);
        if (llList2String(iniline,0) == inisection) {
            ini = llDeleteSubList(ini, i, i);
            i--;
            inilength--;
        }
    }
    return ini;
}

list DeleteINIKey(list ini, string inisection, string inikey) {
    integer i = 0;
    integer inilength = llGetListLength(ini);
    for (i; i<inilength; i++) {
        list iniline = llParseString2List(llList2String(ini, i), [llUnescapeURL("%00")], []);
        if (llList2String(iniline,0) == inisection && llList2String(iniline,1) == inikey) {
            ini = llDeleteSubList(ini, i, i);
            i--;
            inilength--;
        }
    }
    return ini;
}

integer KeyExists(list ini, string inisection, string inikey) {
    integer i = 0;
    integer inilength = llGetListLength(ini);
    for (i; i<inilength; i++) {
        list iniline = llParseString2List(llList2String(ini, i), [llUnescapeURL("%00")], []);
        if (llList2String(iniline,0) == inisection && llList2String(iniline,1) == inikey) return TRUE;
    }
    return FALSE;
}

default {

    state_entry() {
        state ReadINI;
    }
}

state ReadINI {

// Read INI file in as a list, each element of which is a null delimited line
// containing the section name, key name, and value.

    state_entry() {
        // Initialize variables and start reading.
        iniquery = NULL_KEY;
        inilinenum = 0;
        inidata = [];
        tempsection = "";
        iniquery = llGetNotecardLine(inicardname, inilinenum);
    }

    dataserver(key query_id, string data) {
        if (query_id == iniquery) {
            if (data == EOF) {
                // Finished reading notecard.
                state contest_init;
            } else if ((llStringTrim(data, STRING_TRIM) == "") || (llGetSubString(data, 0, 0) == ";")) {
                // Skip blank likes and comments.
                inilinenum++;
                iniquery = llGetNotecardLine(inicardname, inilinenum);
            } else {
                // The real work begins.
                string firstchar = llGetSubString(data, 0, 0);
                string lastchar = llGetSubString(data, llStringLength(data) -1, llStringLength(data) -1);
                if ((firstchar == "[") && (lastchar == "]")) {
                    // This is a new section.
                    tempsection = llGetSubString(data, 1, llStringLength(data) - 2);
                } else if (~(llSubStringIndex(data, "="))) {
                    // This is a key=value line.
                    string newkey = llStringTrim(llDeleteSubString(data, llSubStringIndex(data, "="), -1), STRING_TRIM);
                    string newval = llStringTrim(llDeleteSubString(data, 0, llSubStringIndex(data, "=")), STRING_TRIM);
                    if (KeyExists(inidata, tempsection, newkey)) {
                        inidata = SetINIKey(inidata, tempsection, newkey, newval);
                    } else {
                        inidata += [tempsection + llUnescapeURL("%00") + newkey + llUnescapeURL("%00") + newval];
                    }
                }
                inilinenum++;
                iniquery = llGetNotecardLine(inicardname, inilinenum);
            }
        }
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s