Config Notecard Reader - SLUniverse Forums
Old 07-22-2017, 08:06 PM   #1 (permalink)
Junior Member
 
Join Date: Jul 2017
Posts: 1
Post Config Notecard Reader

This is a LSL Script to read configuration Notecards. It recognizes section headings and can be customized to read multiple different sections of the notecard, processing each section differently. It skips comments and blank lines.

If you specify a section that doesn't exist, it will display an error and continue to process the next sections you specify.

You will need to customize the script to fit your purpose:

The notecard example "Config":
Code:
; Lines starting with semicolon and blank lines are ignored.
; Note that some section names have a space, and some do not.

[SectionOne]
value1=section one value one
value2=section one value two

; This will be the 3rd line of section one
section one, line three

section one, line four
[Section Two]
value1=section two value one
;
section two, line three

section two, line four

[section 3]
[Section 4]
this is the 4th section

; end
The Script "Config Notecard Reader (script)":
Code:
// "Config Notecard Reader" (Version 1) Created July 19, 2017 by Jacob81 Yuhara
// http://www.sluniverse.com/php/vb/script-library/127226-config-notecard-reader.html
// I grant permission for all to freely use, copy, modify for free or for money.

// Based on "Read Notecard Example" by Jacob81 Yuhara
// Which is based on example at:
// http://wiki.secondlife.com/wiki/LlGetNotecardLine
// On July 22, 2017 I checked the code over one last time. Looks perfect. :-)

// Note: After changing this scrip and saving it, be sure to reset it then resave the config notecard for this to work corectly.

string notecardName = "Config"; // Put name of notecard to process in quotes.

// Put all the headings for your config notecard here:
list listNotecardHeadings=[
    "[Section 4]",
    "[section 3]",
    "[noneExisting]",
    "[SectionOne]"
];

// Leave the rest of these glabal varibles alone.

// script-wise, the first notecard line is line 0, the second line is line 1, etc.
integer notecardLine=0;

key dataQueryId;
key headingQueryId;

list listNotecardData=[];
string strNotecardHeading="";
integer bDataserverReady=TRUE;
string strTimerReason; // The reason the timer is being used.

integer iNotecardHeadingNo=0;

// bool fnCheckNotecard()
integer fnCheckNotecard() // You can leave this function alone.
{
        // Check the notecard exists, and has been saved
        if (llGetInventoryKey(notecardName) == NULL_KEY)
        {
            llOwnerSay( "Notecard '" + notecardName + "' missing or unwritten");
            return FALSE;
        }
        else
            return TRUE;
}

// this is called from dataserver
fnProcessData() // Put your own code in this function to process the notecard code.
{
    
    llSay(0, "Processing lines from " + notecardName + " " + "section " + strNotecardHeading);
    
    // use if else statments to seperate code for processing diffrent sections of the notecard:
    if (strNotecardHeading=="[SectionOne]")
    {
        llSay(0, "Processing SectionOne!");
    }
    else
    {
        llSay(0, "Not processing SectionOne.");
    }
    
    // while loops can be used to go through all the lines of data in your section.
    integer i = 0;
    integer length = llGetListLength(listNotecardData);
    while (length > i)
    {
        llOwnerSay( "Line: " + (string)i + " " + llList2String(listNotecardData, i) );
        ++i;
    }
}

// This is the function that starts the reading and processing of the notecard.
fnInitReadData()
{
    // Cleanup:
    notecardLine=0;
    listNotecardData=[];
    strNotecardHeading="";
    
    integer length = llGetListLength(listNotecardHeadings);
    if (length > iNotecardHeadingNo)
    {
        strNotecardHeading = llList2String(listNotecardHeadings, iNotecardHeadingNo);
        ++iNotecardHeadingNo;
        
        headingQueryId = llGetNotecardLine(notecardName, notecardLine); // this line runs asynchronously
    }
    else
    {
                
        iNotecardHeadingNo=0;
        
        bDataserverReady=TRUE;
        
        // for testing, you can comment out.
        llSay(0, "Dataserver done");
    }
}

default
{
    state_entry()
    {
        llSay(0, "state_entry");
    }

    changed(integer change)
    {
        llSay(0, "Changed.");
        
        if (change & CHANGED_INVENTORY)
        {
            if ( fnCheckNotecard() )
            {
                llSay(0, "reading notecard named '" + notecardName + "'.");
                
                if (bDataserverReady==FALSE)
                {
                    llSay(0, "to bad. Dataserver not ready. Try again.");
                }
                else
                {
                    fnInitReadData();
                }
                
            }
            else
            {
                llOwnerSay( "Notecard '" + notecardName + "' missing or unwritten");
            }
        }
    }
    
    touch_start(integer total_number)
    {
        llSay(0, "Touched.");
    }
    
    timer()
    {
        llSay(0, "timer");
        
        //  stop timer
        llSetTimerEvent(0);
    }
    
    // you should leave this code alone, (except you can comment out the testing lines).
    dataserver(key query_id, string data)
    {
        bDataserverReady=FALSE;
        
        if (query_id == headingQueryId)
        {
            if ( data == strNotecardHeading )
            {
                // we found it
                ++notecardLine; // goto first line in section
                
                // Get the data from the section
                dataQueryId = llGetNotecardLine(notecardName, notecardLine);
            }
            else if ( data == EOF )
            {
                // it doesn't exist
                
                llSay(0, "Section " + strNotecardHeading + " not found.");
                
                fnInitReadData();
            }
            else
            {
                // keep looking
                ++notecardLine;
                headingQueryId = llGetNotecardLine(notecardName, notecardLine);
            }
        }
        else if (query_id == dataQueryId)
        {
            if ( data == EOF || llGetSubString(data, 0, 0) == "[" )
            {
                // End of section
                
                fnProcessData();
                
                fnInitReadData();
                
                // bDataserverReady=TRUE;
            }
            else if ( data == "" || llGetSubString(data, 0, 0) == ";" )
            {
                // skip line
                ++notecardLine;
                dataQueryId = llGetNotecardLine(notecardName, notecardLine);
            }
            else
            {
                // for testing (you can comment this line out)
                //llOwnerSay( "Line: " + (string) notecardLine + " " + data);
                
                listNotecardData += data;
                ++notecardLine;
                dataQueryId = llGetNotecardLine(notecardName, notecardLine);
            }
        }
    }

}

Last edited by Jacob81 Yuhara; 07-22-2017 at 08:09 PM. Reason: minor, added url, spell correct
Jacob81 Yuhara is offline   Reply With Quote
1 User Said Thanks:
Reply

Tags
config , notecard , read , reader , section

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On




SEO by vBSEO