Advertisement
Madi_Perth

llinlksetlists

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