Advertisement
Madi_Perth

linkSetList

Apr 3rd, 2024
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Linden Scripting 8.87 KB | Source Code | 0 0
  1. /*
  2. This file contains a series of defines and functions that will
  3. allow you to use linkSetData memory for lists.  These lists will
  4. not have all the versatility of normal LSL lists.
  5.  
  6. The format in LinksetData memory are as follows.
  7.  
  8. The first entry in a list is the master header file.  It is
  9. defined as the following.
  10.  
  11. linkset listName is the list listName and the data is the number of
  12. entries the list.
  13.  
  14. <listName> -> <list entries number>
  15.  
  16. the format of each list entry is as follows.
  17.  
  18. <listName>|<entrynumber>:<data>
  19.  
  20. */
  21.  
  22. // next version will use the firestorm preprocessor
  23. //#define GetlinkListLength getlinkListLength
  24.  
  25. // The first step is the define the linkSetList entry
  26. integer linkSetDefine(string listName)
  27. {
  28.     // test to see if entry already exists, if so return error
  29.     // of -1
  30.     if(llLinksetDataRead(listName) != "")
  31.         return -1;
  32.     else
  33.         llLinksetDataWrite(listName, "0");
  34.     return 1; // linkSetList Created.
  35. }
  36.  
  37. // add a entry to the linkset entry and increment the
  38. // the count by one when adding.   create the linkSet
  39. // if it does not exist. any data other than a string
  40. // must be typecast to a string.
  41. integer addLinkData(string listName, string data)
  42. {
  43.     integer tcList;
  44.     // if listName exists, add link
  45.     // and increment count.
  46.     if(linkSetDefine(listName) == 1)
  47.         tcList = incrmLinkCount(listName, 0);
  48.     else
  49.         tcList = incrmLinkCount(listName, 1);
  50.     llLinksetDataWrite(listName +"|"+ (string) tcList +":"+data, "0");    
  51.  
  52.     return tcList;
  53. }
  54.  
  55. // get count total from base and increase it by 'count'
  56. integer incrmLinkCount(string listName, integer count)
  57. {
  58.     integer currentNum = (integer) llLinksetDataRead(listName);
  59.    
  60.     currentNum += count;
  61.     llLinksetDataWrite(listName, (string) currentNum);
  62.  
  63.     return currentNum;
  64. }
  65.  
  66. // Return the number of entries in a linkSetList
  67. integer getlinkListLength(string listName)
  68. {
  69.     // return error if linkSetList does not exits of -1
  70.     integer listLength = (integer) llLinksetDataRead(listName);
  71.     if(listLength == 0)
  72.         return -1;
  73.     else
  74.         return (listLength + 1);
  75. }
  76.  
  77. /*
  78. The following fuctions return entries in a linkList.
  79. None of them currently support negative indexes.
  80. */
  81.  
  82. // returns a string that is at index in src
  83. string linkList2String(string src, integer index)
  84. {
  85.     string found;
  86.     string searchFor = src+"\\|"+(string) index +":";
  87.  
  88.     found = (string) llLinksetDataFindKeys("^" + searchFor, 0, 1);
  89.     return llGetSubString(found, (llSubStringIndex(found, ":") + 1), -1);
  90. }
  91.  
  92. // returns a integer that is at index in src
  93. integer linkList2Integer(string src, integer index)
  94. {
  95.     string found;
  96.     string searchFor = src+"\\|"+(string) index +":";
  97.  
  98.     found = (string) llLinksetDataFindKeys("^" + searchFor, 0, 1);
  99.     return (integer) llGetSubString(found, (llSubStringIndex(found, ":") + 1), -1);
  100. }
  101.  
  102. // returns a float that is at index in src
  103. float linkList2Float(string src, integer index)
  104. {
  105.     string found;
  106.     string searchFor = src+"\\|"+(string) index +":";
  107.  
  108.     found = (string) llLinksetDataFindKeys("^" + searchFor, 0, 1);
  109.     return (float) llGetSubString(found, (llSubStringIndex(found, ":") + 1), -1);
  110. }
  111.  
  112. // returns a key that is at index in src
  113. key linkList2Key(string src, integer index)
  114. {
  115.     string found;
  116.     string searchFor = src+"\\|"+(string) index +":";
  117.  
  118.     found = (string) llLinksetDataFindKeys("^" + searchFor, 0, 1);
  119.     return (key) llGetSubString(found, (llSubStringIndex(found, ":") + 1), -1);
  120. }
  121.  
  122. // returns a rotation that is at index in src
  123. rotation linkList2Rot(string src, integer index)
  124. {
  125.     string found;
  126.     string searchFor = src+"\\|"+(string) index +":";
  127.  
  128.     found = (string) llLinksetDataFindKeys("^" + searchFor, 0, 1);
  129.     return (rotation) llGetSubString(found, (llSubStringIndex(found, ":") + 1), -1);
  130. }
  131.  
  132. // returns a vector that is at index in src
  133. vector linkList2Vector(string src, integer index)
  134. {
  135.     string found;
  136.     string searchFor = src+"\\|"+(string) index +":";
  137.  
  138.     found = (string) llLinksetDataFindKeys("^" + searchFor, 0, 1);
  139.     return (vector) llGetSubString(found, (llSubStringIndex(found, ":") + 1), -1);
  140. }
  141.  
  142. // Returns a list that is a subset of a linkList.
  143. // be aware of memory limtations since linkLists can
  144. // be significnally larger than script memory.
  145. //
  146. // if the end parameter is a -1 the function will return
  147. // everything from start to the end of the linkList.  
  148. // linkList2List(listName, 0, -1) will return the entire linkList
  149. // as a list.  
  150. list linkList2List(string listName, integer start, integer end)
  151. {
  152.     // check see if linkList exists, return emply list if does not.
  153.     if(llLinksetDataRead(listName) == "")
  154.         return [""];
  155.     // linkList exist, but start is out of bounds,
  156.     // return a empty list.
  157.     integer listLength = getlinkListLength(listName);
  158.     if(start > listLength)
  159.         return [""];
  160.     // if end of linkList is longer than linkList, set end to
  161.     // listLength
  162.     if(end > listLength || end == -1)
  163.         end = listLength;
  164.  
  165.     integer i;
  166.     list    tl;
  167.     for(i = start; i < (end + 1); i++)
  168.     {
  169.         // DS("i : " + (string)i);
  170.         tl += linkList2String(listName, i);
  171.     }
  172.     return tl;
  173. }
  174.  
  175. // Returns a string with the linklist converted to string
  176. // with seperator between entries.
  177. string linkListDump2String(string listName, string seperator)
  178. {
  179.     integer i;  
  180.     integer endofList = getlinkListLength(listName);
  181.     string rc;
  182.  
  183.     for (i = 0; i < endofList; i++)
  184.         rc += linkList2String(listName, i) + seperator;
  185.     return rc;
  186. }
  187.  
  188. // returns a string with the linkList converted to string
  189. // with commas(,) between entries
  190. string linkList2CSV(string linkName)
  191. {
  192.     return linkListDump2String(linkName, ",");
  193. }
  194.  
  195. // removes entries from start to end of linkName. returning the new size of
  196. // linkName;
  197. // if a -1 is used for the end parameter then the linkList will be deleted
  198. // from start to end of the linkLIst
  199. integer linkListDeleteSubList(string linkName, integer start, integer end)
  200. {
  201.     integer i;
  202.     list tl;
  203.     integer tmpEnd = (integer) llLinksetDataRead(linkName);
  204.     string  tmpData;
  205.  
  206.     if(end == -1)
  207.         end = tmpEnd;
  208.     // remove entries from linkName, start to end
  209.     for(i = start; i < (end + 1); ++i)
  210.         tl = llLinksetDataDeleteFound("^" + linkName+"\\|"+(string) i +":", "");
  211.  
  212.     llLinksetDataWrite(linkName, (string) (start-1));
  213.     for(i = (end+1); i < (tmpEnd+1); i++)
  214.     {
  215.         tmpData = linkList2String(linkName, i);
  216.         addLinkData(linkName, tmpData);
  217.         tl = llLinksetDataDeleteFound("^" + linkName+"\\|"+(string) i +":", "");
  218.     }
  219.     return (integer) llLinksetDataRead(linkName);
  220. }
  221.  
  222. // The following takes list (src) and puts it in linkList (dest) starting at
  223. // position start.  If start is a -1, then the list is appended to the end
  224. // of the linkLinks.  The function returns the new length of the linkList.
  225. integer listInsertLinkList (string linkName, list src, integer start)
  226. {
  227.     integer i;
  228.     string tmplink = (string) llGenerateKey();
  229.  
  230.     // if start is longer than linkName, then just append
  231.     // the output to linkName.
  232.     if(start == -1 || start > getlinkListLength(linkName))
  233.     {
  234.         for(i = 0; i < llGetListLength(src); i++)
  235.             addLinkData(linkName, llList2String(src, i));
  236.     }
  237.     else // insert src into linkList at position indicated by start
  238.     {
  239.         // move linkName from 0 to start to tmplink
  240.         for(i = 0; i < start; ++i)
  241.             addLinkData(tmplink, linkList2String(linkName, i));
  242.         // add list src to the end of tmplink
  243.         for(i = 0; i < llGetListLength(src); i++)
  244.             addLinkData(tmplink, llList2String(src, i));
  245.         for(i = start; i < getlinkListLength(linkName); i++)
  246.             addLinkData(tmplink, linkList2String(linkName, i));
  247.  
  248.         // delete old linkList, then move data from tmplink to
  249.         // listLink.
  250.         linkListRemove(linkName);
  251.         for (i = 0; i< getlinkListLength(tmplink); i++)
  252.             addLinkData(linkName, linkList2String(tmplink, i));
  253.         linkListRemove(tmplink);
  254.     }
  255.  
  256.     return getlinkListLength(linkName);
  257. }
  258.  
  259. // function to remove a linkList
  260. // returns -1 if not successful
  261. // returns 1 if successful
  262. integer linkListRemove(string linkName)
  263. {
  264.     llLinksetDataDeleteFound("^" + linkName+"\\|", "");
  265.     llLinksetDataDeleteFound("^" + linkName, "");
  266.  
  267.     return 1;
  268. }
  269.  
  270. // returns the index of the first instance of needle
  271. // in linkList of linkName
  272. integer linkListFind(string linkName, string needle)
  273. {
  274.     string findTmp = (string) llLinksetDataFindKeys("^" + linkName + "\\|" + ".*:.*" + needle +".*", 0, 1);
  275.     list tmpList = llParseString2List(findTmp, [":", "|"], []);
  276.     if(llGetListLength(tmpList) < 1)
  277.         return -1;
  278.     else
  279.         return llList2Integer(tmpList, 1);
  280. }
  281.  
  282.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement