LSL: Read INI Notecard

list group_labels;
list buttons;
list labels;
list button_labels;
list colors;
 
// Internal vars
string DEFAULT_NOTECARD = "!CONFIG";
string notecard_name;
key notecard_qid;
integer line;
integer current_group;
string current_section;
 
integer current_buttons;
string current_label;
string current_button_label;
vector current_color;
 
 
// See if the notecard is present in object inventory
integer can_haz_notecard(string name) {
    integer count = llGetInventoryNumber(INVENTORY_NOTECARD);
    while (count--) {
        if (llGetInventoryName(INVENTORY_NOTECARD, count) == name) {
            log("ap: Found notecard: " + name);
            return TRUE;
        }
    }
    log("ap: Notecard " + name + " not found");
    return FALSE;
}
 
load_notecard(string name) {
    notecard_name = name;
    if (notecard_name == "") {
        notecard_name = DEFAULT_NOTECARD;
    }
    if (can_haz_notecard(notecard_name)) {
        log("ap: Reading notecard: " + notecard_name);
        line = 0;
        skin_config = [];
        button_names = [];
        thumbnails = [];
        reading_notecard = TRUE;
        notecard_qid = llGetNotecardLine(notecard_name, line);
    }
}
 
init() {
    reading_notecard = FALSE;
    load_notecard(notecard_name);
}
 
save_section() {
    // Save what we have
    log(" " + current_section + " " + (string)current_buttons + " " + (string)current_color);
    group_labels += current_section;
    buttons += current_buttons;
    labels += current_label;
    button_labels += current_button_label;
    colors += current_color;
 
    // Clean up for next line
    current_section = "";
    current_buttons = 0;
    current_label = "";
    current_button_label = "";
    current_color = ZERO_VECTOR;
}
 
read_config(string data) {
    if (data == EOF) {
        // All done
        save_section();
        return;
    }
 
    data = llStringTrim(data, STRING_TRIM_HEAD);
    if (data != "" && llSubStringIndex(data, "#") != 0) {
        if (llSubStringIndex(data, "[") == 0) {
            // Save previous section if valid
            save_section();
            // Section header
            integer end = llSubStringIndex(data, "]");
            if (end != 0) {
                // Well-formed section header
                current_section = llGetSubString(data, 1, end-1);
                log("Reading section " + current_section);
                // Reset section globals
                attr1 = 0;
                attr2 = ZERO_VECTOR;
            }
        } else {
            integer i = llSubStringIndex(data, "=");
            if (i != -1) {
                string attr = llToLower(llStringTrim(llGetSubString(data, 0, i-1), STRING_TRIM));
                string value = llStringTrim(llGetSubString(data, i+1, -1), STRING_TRIM);
 
                if (attr == "attr1") {
                    // Save attr somewhere for later
                    attr1 = (integer)value;
                }
                else if (attr == "attr2") {
                    // Save attr somewhere for later
                    attr2 = value;
                }
                else {
//                    llOwnerSay("Unknown configuration value: " + name + " on line " + (string)line);
                }
            } else {
            // not an assignment line
//                llOwnerSay("Configuration could not be read on line " + (string)line);
            }
        }
    }
 
    notecard_qid = llGetNotecardLine(notecard_name, ++line);
}
 
default {
    on_rez(integer start_param) {
        init();
    }
 
    changed(integer change) {
        if(change & (CHANGED_OWNER | CHANGED_INVENTORY))
            init();
    }
 
    state_entry() {
        init();
    }
 
    dataserver(key request_id, string data) {
        if(request_id == notecard_qid) {
            read_config(data);
            if (data == EOF) {
                // Do whatever happens after the notecard is read
            }
        }
    }
}