Advertisement
Apjjm

ATDD Scripting: StringList (Doc)

Jun 6th, 2012
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.93 KB | None | 0 0
  1. //Begin String List
  2. /* String List Implementation V1.0 */
  3. /* Written by: Apjjm               */
  4.  
  5. //+ Constants
  6. //Change the delimiter to suit, it is currently the null character.
  7. const string _STRLPFX = "_!strli!_"; //Prefixed to all stringList var names
  8. const string _STRLDLM = "\0";    //SINGLE CHARACTER Delimiter for items
  9. //-
  10.  
  11. //Begin List Management
  12. //+ bool stringListCreate(string &in asName)
  13. ////////////////////////////////////////////////////////////////
  14. //stringListCreate:                                           //
  15. //      initiates a list with the given name                  //
  16. //asName:                                                     //
  17. //      name for the list                                     //
  18. //returns:                                                    //
  19. //      if the creation was sucessful (fails if list exists)  //
  20. ////////////////////////////////////////////////////////////////
  21. bool stringListCreate(string &in asName) {
  22.   string name = _STRLPFX + asName;
  23.   //If Exists alread, don't overwrite
  24.   if(GetLocalVarInt(name + "_exist")==1) return false;
  25.   //Else continue as normal
  26.   SetLocalVarInt(name + "_exist",1); //State list exists
  27.   SetLocalVarInt(name + "_count",0);  //Put a count on the list
  28.   SetLocalVarString(name,"");         //Define list w/ empty string
  29.   return true;
  30. }
  31. //-
  32. //+ bool stringListDestroy(string &in asName)
  33. ////////////////////////////////////////////////////////////////
  34. //stringListDestroy:                                          //
  35. //      Clears & destroys a list with the given name          //
  36. //asName:                                                     //
  37. //      name of the list                                      //
  38. //returns:                                                    //
  39. //      destruction was sucessful? (false if invalid list)    //
  40. ////////////////////////////////////////////////////////////////
  41. bool stringListDestroy(string &in asName) {
  42.   string name = _STRLPFX + asName;
  43.   //If it doesn't exist - we can't destroy it
  44.   if(GetLocalVarInt(name + "_exist")!=1) return false;
  45.   //Else reduce all vars to min size - mark as not existing
  46.   SetLocalVarInt(name + "_exist",0);  //State list doesn't exist
  47.   SetLocalVarInt(name + "_count",0);  //Change the list count
  48.   SetLocalVarString(name,"");         //Define list w/ empty string
  49.   return true;
  50. }  
  51. //-
  52. //+ bool stringListExists(string &in asName)
  53. ////////////////////////////////////////////////////////////////
  54. //stringListExists:                                           //
  55. //      Determines if list with a given name exists           //
  56. //asName:                                                     //
  57. //      name of the list                                      //
  58. //returns:                                                    //
  59. //      if the list exists                                    //
  60. ////////////////////////////////////////////////////////////////
  61. bool stringListExists(string &in asName) {
  62.   return (GetLocalVarInt(_STRLPFX + asName + "_exist") == 1);
  63. }
  64. //-
  65. //+ int stringListSize(string &in asName)
  66. ////////////////////////////////////////////////////////////////
  67. //stringListSize:                                             //
  68. //      returns the size of a list                            //
  69. //asName:                                                     //
  70. //      name of the list                                      //
  71. //returns:                                                    //
  72. //      list size (0 if empty or non-existant)                //
  73. ////////////////////////////////////////////////////////////////
  74. int stringListSize(string &in asName) {
  75.   return (GetLocalVarInt(_STRLPFX + asName + "_count"));
  76. }
  77. //-
  78. //+ void stringListClear(string &in asName)
  79. ////////////////////////////////////////////////////////////////
  80. //stringListClear:                                            //
  81. //      initiates a list with the given name                  //
  82. //asName:                                                     //
  83. //      name of the list                                      //
  84. ////////////////////////////////////////////////////////////////
  85. void stringListClear(string &in asName) {
  86.   string name = _STRLPFX + asName;
  87.   //Don't do anything if list doesn't exist
  88.   if(GetLocalVarInt(name + "_exist")!=1) return;
  89.   //Else reset list to defaults
  90.   SetLocalVarInt(name + "_count",0);  //Put a count on the list to 0
  91.   SetLocalVarString(name,"");         //Define list w/ empty string
  92. }
  93. //-
  94. //End List Management
  95.  
  96. //Begin Item Management
  97. //+ string[] stringListItems(string &in asName)
  98. ////////////////////////////////////////////////////////////////
  99. //stringListItems                                             //
  100. //                                                            //
  101. //asName:                                                     //
  102. //      name of the list                                      //
  103. //returns:                                                    //
  104. //      Array of all the items in the list                    //
  105. ////////////////////////////////////////////////////////////////
  106. string[] stringListItems(string &in asName) {
  107.   string str = GetLocalVarString(_STRLPFX + asName);
  108.   //Output array vars
  109.   string[] output = {};
  110.   int outputIndex = 0;
  111.   //Early-out if string is faulty
  112.   if(!StringContains(str,_STRLDLM)) return output;
  113.   //String matching vars
  114.   uint8 dlm = _STRLDLM[0];
  115.   int last = 0;
  116.   //Loop and add each substring
  117.   for(int i =0; i<str.length(); i++) //For each char
  118.    {
  119.     if(str[i] == dlm) //If char matches delimiter
  120.      {      
  121.       int len = i - last; //Determine length of token to read
  122.       if(len>= 0)
  123.        {
  124.         output.resize(outputIndex+1); //Increase array size
  125.         output[outputIndex] = StringSub(str,last,len); //Get token into array
  126.         outputIndex++; //Location of next token
  127.        }
  128.       last = i+1; //Say token begins at the next char
  129.      }    
  130.    }
  131.   //Return array of substrings
  132.   return output;
  133. }
  134. //-
  135. //+ int stringListAddItem(string &in asName, string &in asItem)
  136. ////////////////////////////////////////////////////////////////
  137. //stringListAddItem                                           //
  138. //      Adds specified item to the list                       //
  139. //asName:                                                     //
  140. //      name of the list                                      //
  141. //asItem:                                                     //
  142. //      Item to be added                                      //
  143. //returns:                                                    //
  144. //      Index of the item added (-1 if adding failed)         //
  145. ////////////////////////////////////////////////////////////////
  146. int stringListAddItem(string &in asName, string &in asItem) {
  147.   string name = _STRLPFX + asName;
  148.   //Return error value if list doesn't exist
  149.   if(GetLocalVarInt(name + "_exist")!=1) return -1;
  150.   //Add the item with the delimiter, increment count
  151.   int index = GetLocalVarInt(name + "_count");
  152.   AddLocalVarInt(name + "_count",1);
  153.   AddLocalVarString(name,asItem + _STRLDLM);
  154.   return index;
  155. }
  156. //-
  157. //+ void stringListAddItems(string &in asName, string[] asItem)
  158. ////////////////////////////////////////////////////////////////
  159. //stringListAddItems                                          //
  160. //      Adds an array of items to the list                    //
  161. //asName:                                                     //
  162. //      name of the list                                      //
  163. //asItem:                                                     //
  164. //      Array of Items to be added                            //
  165. ////////////////////////////////////////////////////////////////
  166. void stringListAddItems(string &in asName, string[] asItem) {
  167.   //Doesnt exist / Bad parameter? return.
  168.   string name = _STRLPFX + asName;
  169.   if(GetLocalVarInt(name + "_exist")!=1  || asItem.length()==0) return;
  170.   //Append all items together w/ the delimiter
  171.   string item = "";
  172.   for(int i =0; i<asItem.length(); i++) item += asItem[i] + _STRLDLM;
  173.   //Add this new item string, Increment count
  174.   AddLocalVarString(name,item);
  175.   AddLocalVarInt(name + "_count",asItem.length());
  176. }
  177. //-
  178. //+ string stringListGetItem(string &in asName, int alIndex)
  179. ////////////////////////////////////////////////////////////////
  180. //stringListGetItem                                           //
  181. //      Gets item from the list at the specified index        //
  182. //asName:                                                     //
  183. //      name of the list                                      //
  184. //alIndex:                                                    //
  185. //      Index of  the item to retrieve                        //
  186. //returns:                                                    //
  187. //      Item at specified index                               //
  188. ////////////////////////////////////////////////////////////////
  189. string stringListGetItem(string &in asName, int alIndex) {
  190.   //This is just an array fetch + grab at index
  191.   string name = _STRLPFX + asName;
  192.   string output = "";
  193.   if(alIndex>= 0 && GetLocalVarInt(name + "_exist") == 1) //Parameters valid enough to progress?
  194.    {
  195.     string[] names = stringListItems(asName); //Get list of items
  196.     if(alIndex <names.length())
  197.      {
  198.       output = names[alIndex]; //Index in bounds: set ouput to item
  199.      }
  200.    }
  201.   return output;
  202. }
  203. //-
  204. //+ int stringListGetLastIndexof(string &in asName, string &in asItem)
  205. ////////////////////////////////////////////////////////////////
  206. //stringListLastIndexof                                       //
  207. //      Finds the last index of the specified item in the list//
  208. //asName:                                                     //
  209. //      name of the list                                      //
  210. //asItem:                                                     //
  211. //      Item to be found                                      //
  212. //returns:                                                    //
  213. //      Index of last occurance of asItem in list (-1 if none)//
  214. ////////////////////////////////////////////////////////////////
  215. int stringListGetLastIndexof(string &in asName, string &in asItem) {
  216.   //This is a linear search of the list of items
  217.   string name = _STRLPFX + asName;
  218.   int index = -1;
  219.   if(GetLocalVarInt(name + "_exist") == 1)
  220.    {
  221.     string[] items = stringListItems(asName); //Get list of items
  222.     for(int i=0; i<items.length(); i++)
  223.      { if(asItem == items[i]) index = i; } //Search for match. If match set index to index of match.
  224.    }
  225.   return index;
  226. }
  227. //-
  228. //+ int stringListGetFirstIndexof(string &in asName, string &in asItem)
  229. ////////////////////////////////////////////////////////////////
  230. //stringListGetFirstIndexof                                   //
  231. //      gets the first index of asItem in the target list     //
  232. //asName:                                                     //
  233. //      name of the list                                      //
  234. //asItem:                                                     //
  235. //      Item to be located                                    //
  236. //returns:                                                    //
  237. //      Index of first occurance of asItem in list(-1 if none)//
  238. ////////////////////////////////////////////////////////////////
  239. int stringListGetFirstIndexof(string &in asName, string &in asItem) {
  240.   //This is a linear search of the list of items
  241.   string name = _STRLPFX + asName;
  242.   int index = -1;
  243.   if(GetLocalVarInt(name + "_exist") == 1)
  244.    {
  245.     string[] items = stringListItems(asName); //Get list of items
  246.     for(int i=0; i<items.length(); i++)
  247.      {
  248.       if(asItem == items[i])
  249.         {
  250.          index = i;
  251.          i = items.length();
  252.         }
  253.      }
  254.    }
  255.   return index;
  256. }
  257. //-
  258. //+ bool stringListContains(string &in asName, string &in asItem)
  259. ////////////////////////////////////////////////////////////////
  260. //stringListContains                                          //
  261. //      Gets if the list contains asItem                      //
  262. //asName:                                                     //
  263. //      name of the list                                      //
  264. //asItem:                                                     //
  265. //      Item to be found                                      //
  266. //returns:                                                    //
  267. //      If asItem is in the list                              //
  268. ////////////////////////////////////////////////////////////////
  269. bool stringListContains(string &in asName, string &in asItem) {
  270.   return stringListGetLastIndexof(asName, asItem)>=0;
  271. }
  272. //-
  273. //+ bool stringListRemoveItemAt(string &in asName, int alIndex)
  274. ////////////////////////////////////////////////////////////////
  275. //stringListRemoveItemAt                                      //
  276. //      Removes item at specified index                       //
  277. //asName:                                                     //
  278. //      name of the list                                      //
  279. //alIndex:                                                    //
  280. //      Index to be removed                                   //
  281. //returns:                                                    //
  282. //      True if the item was found and removed                //
  283. ////////////////////////////////////////////////////////////////
  284. bool stringListRemoveItemAt(string &in asName, int alIndex) {
  285.     string name = _STRLPFX + asName;
  286.     //List exists?
  287.     if(GetLocalVarInt(name + "_exist")!=1) return false;
  288.     //Get list
  289.     string[] list = stringListItems(asName);
  290.     //Re-add all but deleted item
  291.     string item = "";
  292.     int count = 0;
  293.     bool op = false;
  294.     for(int i =0; i<list.length(); i++)
  295.     {
  296.      if(i != alIndex)
  297.       {
  298.        item += list[i] + _STRLDLM;
  299.        count++;
  300.       }
  301.      else op=true;
  302.     }
  303.     //Set back to the array
  304.     SetLocalVarInt(name + "_count",count);  //Change count
  305.     SetLocalVarString(name,item);           //Redefine list      
  306.     //Return if anything was deleted
  307.     return op;
  308. }
  309. //-
  310. //+ int stringListRemoveItems(string &in asName, string asItem)
  311. ////////////////////////////////////////////////////////////////
  312. //stringListRemoveItems                                       //
  313. //      Removes any items matching asItem                     //
  314. //asName:                                                     //
  315. //      name of the list                                      //
  316. //asItem:                                                     //
  317. //      Item to be removed (duplicate items are removed too)  //
  318. //returns:                                                    //
  319. //      Number of occurances of the item removed              //
  320. ////////////////////////////////////////////////////////////////
  321. int stringListRemoveItems(string &in asName, string asItem) {
  322.     string name = _STRLPFX + asName;
  323.     //List exists?
  324.     if(GetLocalVarInt(name + "_exist")!=1) return 0;
  325.     //Get list
  326.     string[] list = stringListItems(asName);
  327.     //Re-add all but deleted items
  328.     string item = "";
  329.     int count = 0;
  330.     int delamt = 0;
  331.     for(int i =0; i<list.length(); i++)
  332.     {
  333.      if(asItem != list[i])
  334.       {
  335.        item += list[i] + _STRLDLM;
  336.        count++;
  337.       }
  338.      else delamt++;
  339.     }
  340.     //Set back to the array
  341.     SetLocalVarInt(name + "_count",count);  //Change count
  342.     SetLocalVarString(name,item);           //Redefine list      
  343.     //Return if anything was deleted
  344.     return delamt;
  345. }
  346. //-
  347. //End Item Management
  348.  
  349. //Begin List Debug
  350. //Draws each item in list specified by asName
  351. void stringListDraw(string &in asName) {
  352.     string[] sli = stringListItems(asName); string o = "";
  353.     for(int i =0; i<sli.length(); i++) o += " | " + i + ": " + sli[i] + " |";
  354.     AddDebugMessage(o,false);
  355. }
  356. //Draws the string of the specified list straight to the screen
  357. //Warning: Will not display correctly - if delimiter isn't defined in the font file!
  358. void stringListDrawRaw(string & in asName) {
  359.   AddDebugMessage(GetLocalVarString(_STRLPFX + asName),false);
  360. }
  361. //End
  362. //End String List
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement