Config Card Reader

From Fire And Ice Grid
Jump to navigation Jump to search

OSSL configuration card reader written using OSSL. This script will work in Opensim but will not work in Second Life.

  • Change the name of the notecard to be read in the User Changeable Code
  • Blank lines are ignored
  • Lines with a # at the start are ignored for comments
  • Add your processing for each line read in the same place

Script On Github

LSL And OSSL Script Library On GitHub

Licence

 1 BSD 3-Clause License
 2 Copyright (c) 2020, Sara Payne
 3 All rights reserved.
 4 Redistribution and use in source and binary forms, with or without
 5 modification, are permitted provided that the following conditions are met:
 6 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 7 2. Redistributions in binary form must reproduce the above copyright notice,
 8    this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 9 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
11 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
12 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
13 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
14 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
15 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
17 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
18 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
19 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 1 /*
 2 Config Card Reader
 3 ==================
 4 Blank Lines are ignored
 5 Lines starting with  # are also ignored
 6 Only lines which contain an "=" sign will be read. 
 7 White Space Safe: ("InstructionType=DataType" is the same as "InstructionType = DataType", which is the same as " InstructionType  =  DataType  ")
 8 Capital Letters are all converted to lower case to make it less prone to user error ("InstructionType=DataType" is the same as "instructiontype=datatype").
 9 Example Of A Line Which would be read:
10 Instruction = Data
11 UserName    = Manwa Pastorelli
12 UserName    = Sara Payne 
13 The line is then processed in whatever way you wish taking the part before the equals sign as the instruction and the part after as the data. 
14 */
15 
16 
17 
18 //User Changeable Code
19 //======================
20 string configCardName = "My Notecard Name"; //change this to the name of the notecard you wish to read
21 
22 ProcessInstructionLine(string instruction, string data)
23 {
24     //add your code here for whatever you wish to do with the line you have just read. 
25 }
26 //========================
27 //End User Changeable Code
28 
29 string CleanUpString(string inputString)
30 { 
31     string cleanString = llStringTrim( llToLower(inputString), STRING_TRIM );
32     return cleanString;   
33 }
34 
35 ReadConfigCards(string notecardName)
36 {   //Reads the named config card if it exists
37     if (llGetInventoryType(notecardName) == INVENTORY_NOTECARD)
38     {   //only come here if the name notecard actually exists, otherwise give the user an error
39         integer notecardLength = osGetNumberOfNotecardLines(notecardName); //gets the length of the notecard
40         integer index; //defines the index for the next line
41         for (index = 0; index < notecardLength; ++index)
42         {    //loops through the notecard line by line  
43             string currentLine = osGetNotecardLine(notecardName,index); //contents of the current line exactly as it is in the notecard
44             string firstChar = llGetSubString(currentLine, 0,0); //gets the first character of this line
45             integer equalsIndex = llSubStringIndex(currentLine, "="); //gets the position of hte equals sign on this line if it exists
46             if (currentLine != "" && firstChar != "#" && equalsIndex != -1 )
47             {   //only come here if the line has content, it does not start with # and it contains an equal sign
48                 string instruction = llGetSubString (currentLine, 0, equalsIndex-1); //everything before the equals sign
49                 string data = llGetSubString(currentLine, equalsIndex+1, -1); //everything after the equals sign    
50                 instruction = CleanUpString (instruction); //sends the instruvtion to the cleanup method to remove white space and turn to lower case
51                 data = CleanUpString (data); //sends the data to the cleanup method to remove white space and turn to lower case
52                 ProcessInstructionLine(instruction, data); //sends the instruction and the data to the Process instruction method
53             }//close if the line is valid
54             else
55             {
56                 if ( (currentLine != "") && (firstChar != "#") && (equalsIndex == -1))
57                 {
58                     llOwnerSay("Line number: " + (string)index + " is malformed. It is not blank, and does not begin with a #, yet it contains no equals sign.");
59                 }
60             }
61         }
62     }//close if the notecard exists
63     else 
64     {   //the named notecard does not exist, send an error to the user. 
65         llOwnerSay ("The notecard called " + notecardName + " is missing, please address this");
66     }//close error the notecard does not exist
67 }//close read config card. 
68 
69 default
70 {
71     state_entry()
72     {   //main entry point of the script, this runs when the script starts
73         ReadConfigCards(configCardName); //calls the read config card method passing the name of the card defined in the global variables above
74     }//close state entry
75 }//close default

Syntax Highlighting

GeShi syntax highlighting is enabled on this wiki. <be>

When using the tabs described in the link, change the language part to "lsl". Eg.
syntaxhighlight lang="lsl" line
inside angled brackets <>