INI Notecards with OSSL

This page contains code from a script that enables a scripter to read, write, and manipulate notecards that contain data in the INI File format. It requires that the simulator allows the use of the OSSL functions osMakeNotecardosGetNotecardLine, and osGetNumberOfNotecardLines. Another version of these functions that uses only vanilla LSL functions is available, but does not have 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. Some of them won’t work at all over in SL, as they make use of OSSL functions. 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: ReadINI, WriteINI, GetINIKey, SetINIKey, GetINISectionList, GetINIKeyList, DeleteINISection,  DeleteINIKey, and KeyExists. A description of each function follows.

list ReadINI(string cardname)

This function simply takes the name of the notecard that contains the 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. This function requires that the simulator allows the OSSL functions osGetNotecardLine, and osGetNumberOfNotecardLines.

WriteINI(string cardname, list ini)

This function simply writes the INI data stored in our list to the specified notecard. If the notecard already exists, it is replaced. This function requires that the simulator allows the use of osMakeNotecard.

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 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 ReadINI(string cardname) {
    // Read an INI formatted notecard into a list. Each line of the list
    // will be a null char delimted string of the section, key, and value.
    integer i = 0;
    list inilist = [];
    string iniline = "";
    string section = "";
    integer numlines = osGetNumberOfNotecardLines(cardname);
    for (i; i<numlines; i++) {
        iniline = osGetNotecardLine(cardname, i);
        string firstchar = llGetSubString(iniline, 0, 0);
        string lastchar = llGetSubString(iniline, llStringLength(iniline) -1, llStringLength(iniline) -1);
        if ((llStringTrim(iniline, STRING_TRIM) == "") || (llGetSubString(iniline, 0, 0) == ";")) {
            // Skip blank likes and comments.
        } else if ((firstchar == "[") && (lastchar == "]")) {
            // This is a new section, take note of it.
            section = llGetSubString(iniline, 1, llStringLength(iniline) - 2);
        } else if (~(llSubStringIndex(iniline, "="))) {
            // This is a key=value line.
            string newkey = llStringTrim(llDeleteSubString(iniline, llSubStringIndex(iniline, "="), -1), STRING_TRIM);
            string newval = llStringTrim(llDeleteSubString(iniline, 0, llSubStringIndex(iniline, "=")), STRING_TRIM);
            // Check for existing copy of key, and replace or add it.
            if (KeyExists(inilist, section, newkey)) {
                inilist = SetINIKey(inilist, section, newkey, newval);
            } else {
                inilist += [section + llUnescapeURL("%00") + newkey + llUnescapeURL("%00") + newval];
            }
        }
    }
    return inilist;
}

WriteINI(string cardname, list ini) {
    // Write out our list containing INI data to a notecard.
    list newini = [];
    string inisection = "";
    integer inilength = llGetListLength(ini);
    integer i = 0;
    for (i; i<inilength; i++) {
        list iniline = llParseString2List(llList2String(ini, i), [llUnescapeURL("%00")], []);
        string temp = llList2String(iniline, 0);
        if (temp != inisection) {
            inisection = temp;
            newini += ["\n[" + inisection + "]"];
            newini += [llList2String(iniline, 1) + "=" + llList2String(iniline, 2)];
        } else {
            newini += [llList2String(iniline, 1) + "=" + llList2String(iniline, 2)];
        }
    }
    llRemoveInventory(cardname);
    osMakeNotecard(cardname, newini);
}

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;
}

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