Advertisement
Apjjm

ATDD Scripting: StringList (Bare)

Jun 6th, 2012
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.74 KB | None | 0 0
  1. ///////////////////////////////
  2. //Begin String List LIBRARY
  3. ///////////////////////////////
  4. /* String List Implementation V1.0 *\
  5. \* Written by: Apjjm               */
  6.  
  7. //+ Constants
  8. //Change the delimiter to suit, it is currently the null character.
  9. const string _STRLPFX = "_!strli!_"; //Prefixed to all stringList var names
  10. const string _STRLDLM = "\0";    //SINGLE CHARACTER Delimiter for items
  11. //-
  12.  
  13. //+ List Management
  14. bool stringListCreate(string &in asName) {
  15.   string name = _STRLPFX + asName;
  16.   //If Exists alread, don't overwrite
  17.   if(GetLocalVarInt(name + "_exist")==1) return false;
  18.   //Else continue as normal
  19.   SetLocalVarInt(name + "_exist",1); //State list exists
  20.   SetLocalVarInt(name + "_count",0);  //Put a count on the list
  21.   SetLocalVarString(name,"");         //Define list w/ empty string
  22.   return true;
  23. }
  24.  
  25. bool stringListDestroy(string &in asName) {
  26.   string name = _STRLPFX + asName;
  27.   //If it doesn't exist - we can't destroy it
  28.   if(GetLocalVarInt(name + "_exist")!=1) return false;
  29.   //Else reduce all vars to min size - mark as not existing
  30.   SetLocalVarInt(name + "_exist",0);  //State list doesn't exist
  31.   SetLocalVarInt(name + "_count",0);  //Change the list count
  32.   SetLocalVarString(name,"");         //Define list w/ empty string
  33.   return true;
  34. }  
  35.  
  36. bool stringListExists(string &in asName) {
  37.   return (GetLocalVarInt(_STRLPFX + asName + "_exist") == 1);
  38. }
  39.  
  40. int stringListSize(string &in asName) {
  41.   return (GetLocalVarInt(_STRLPFX + asName + "_count"));
  42. }
  43.  
  44. void stringListClear(string &in asName) {
  45.   string name = _STRLPFX + asName;
  46.   //Don't do anything if list doesn't exist
  47.   if(GetLocalVarInt(name + "_exist")!=1) return;
  48.   //Else reset list to defaults
  49.   SetLocalVarInt(name + "_count",0);  //Put a count on the list to 0
  50.   SetLocalVarString(name,"");         //Define list w/ empty string
  51. }
  52.  
  53. //- List Management
  54.  
  55. //+ Item Management
  56. string[] stringListItems(string &in asName) {
  57.   string str = GetLocalVarString(_STRLPFX + asName);
  58.   //Output array vars
  59.   string[] output = {};
  60.   int outputIndex = 0;
  61.   //Early-out if string is faulty
  62.   if(!StringContains(str,_STRLDLM)) return output;
  63.   //String matching vars
  64.   uint8 dlm = _STRLDLM[0];
  65.   int last = 0;
  66.   //Loop and add each substring
  67.   for(uint i =0; i<str.length(); i++) //For each char
  68.    {
  69.     if(str[i] == dlm) //If char matches delimiter
  70.      {      
  71.       int len = int(i) - last; //Determine length of token to read
  72.       if(len >= 0)
  73.        {
  74.         output.resize(outputIndex+1); //Increase array size
  75.         output[outputIndex] = StringSub(str,last,len); //Get token into array
  76.         outputIndex++; //Location of next token
  77.        }
  78.       last = i+1; //Say token begins at the next char
  79.      }    
  80.    }
  81.   //Return array of substrings
  82.   return output;
  83. }
  84.  
  85. int stringListAddItem(string &in asName, string &in asItem) {
  86.   string name = _STRLPFX + asName;
  87.   //Return error value if list doesn't exist
  88.   if(GetLocalVarInt(name + "_exist")!=1) return -1;
  89.   //Add the item with the delimiter, increment count
  90.   int index = GetLocalVarInt(name + "_count");
  91.   AddLocalVarInt(name + "_count",1);
  92.   AddLocalVarString(name,asItem + _STRLDLM);
  93.   return index;
  94. }
  95.  
  96. void stringListAddItems(string &in asName, string[] asItem) {
  97.   //Doesnt exist / Bad parameter? return.
  98.   string name = _STRLPFX + asName;
  99.   if(GetLocalVarInt(name + "_exist")!=1  || asItem.length()==0) return;
  100.   //Append all items together w/ the delimiter
  101.   string item = "";
  102.   for(uint i =0; i<asItem.length(); i++) item += asItem[i] + _STRLDLM;
  103.   //Add this new item string, Increment count
  104.   AddLocalVarString(name,item);
  105.   AddLocalVarInt(name + "_count",asItem.length());
  106. }
  107.  
  108. string stringListGetItem(string &in asName, int alIndex) {
  109.   //This is just an array fetch + grab at index
  110.   string name = _STRLPFX + asName;
  111.   string output = "";
  112.   if(alIndex >= 0 && GetLocalVarInt(name + "_exist") == 1) //Parameters valid enough to progress?
  113.    {
  114.     string[] names = stringListItems(asName); //Get list of items
  115.     if(alIndex < int(names.length()))
  116.      {
  117.       output = names[alIndex]; //Index in bounds: set ouput to item
  118.      }
  119.    }
  120.   return output;
  121. }
  122.  
  123. int stringListGetLastIndexof(string &in asName, string &in asItem) {
  124.   //This is a linear search of the list of items
  125.   string name = _STRLPFX + asName;
  126.   int index = -1;
  127.   if(GetLocalVarInt(name + "_exist") == 1)
  128.    {
  129.     string[] items = stringListItems(asName); //Get list of items
  130.     for(int i=0; i<int(items.length()); i++)
  131.      { if(asItem == items[i]) index = i; } //Search for match. If match set index to index of match.
  132.    }
  133.   return index;
  134. }
  135.  
  136. int stringListGetFirstIndexof(string &in asName, string &in asItem) {
  137.   //This is a linear search of the list of items
  138.   string name = _STRLPFX + asName;
  139.   int index = -1;
  140.   if(GetLocalVarInt(name + "_exist") == 1)
  141.    {
  142.     string[] items = stringListItems(asName); //Get list of items
  143.     for(uint i=0; i<items.length(); i++)
  144.      {
  145.       if(asItem == items[i])
  146.         {
  147.          index = int(i);
  148.          i = items.length();
  149.         }
  150.      }
  151.    }
  152.   return index;
  153. }
  154.  
  155. bool stringListContains(string &in asName, string &in asItem) {
  156.   return stringListGetLastIndexof(asName, asItem)>=0;
  157. }
  158.  
  159. bool stringListRemoveItemAt(string &in asName, uint alIndex) {
  160.     string name = _STRLPFX + asName;
  161.     //List exists?
  162.     if(GetLocalVarInt(name + "_exist")!=1) return false;
  163.     //Get list
  164.     string[] list = stringListItems(asName);
  165.     //Re-add all but deleted item
  166.     string item = "";
  167.     int count = 0;
  168.     bool op = false;
  169.     for(uint i =0; i<list.length(); i++)
  170.     {
  171.      if(i != alIndex)
  172.       {
  173.        item += list[i] + _STRLDLM;
  174.        count++;
  175.       }
  176.      else op=true;
  177.     }
  178.     //Set back to the array
  179.     SetLocalVarInt(name + "_count",count);  //Change count
  180.     SetLocalVarString(name,item);           //Redefine list      
  181.     //Return if anything was deleted
  182.     return op;
  183. }
  184.  
  185. int stringListRemoveItems(string &in asName, string asItem) {
  186.     string name = _STRLPFX + asName;
  187.     //List exists?
  188.     if(GetLocalVarInt(name + "_exist")!=1) return 0;
  189.     //Get list
  190.     string[] list = stringListItems(asName);
  191.     //Re-add all but deleted items
  192.     string item = "";
  193.     int count = 0;
  194.     int delamt = 0;
  195.     for(uint i =0; i<list.length(); i++)
  196.     {
  197.      if(asItem != list[i])
  198.       {
  199.        item += list[i] + _STRLDLM;
  200.        count++;
  201.       }
  202.      else delamt++;
  203.     }
  204.     //Set back to the array
  205.     SetLocalVarInt(name + "_count",count);  //Change count
  206.     SetLocalVarString(name,item);           //Redefine list      
  207.     //Return if anything was deleted
  208.     return delamt;
  209. }
  210. //- Item Management
  211.  
  212. ///////////////////////////////
  213. //End String List
  214. ///////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement