Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////////////////////////
- //Begin String List LIBRARY
- ///////////////////////////////
- /* String List Implementation V1.0 *\
- \* Written by: Apjjm */
- //+ Constants
- //Change the delimiter to suit, it is currently the null character.
- const string _STRLPFX = "_!strli!_"; //Prefixed to all stringList var names
- const string _STRLDLM = "\0"; //SINGLE CHARACTER Delimiter for items
- //-
- //+ List Management
- bool stringListCreate(string &in asName) {
- string name = _STRLPFX + asName;
- //If Exists alread, don't overwrite
- if(GetLocalVarInt(name + "_exist")==1) return false;
- //Else continue as normal
- SetLocalVarInt(name + "_exist",1); //State list exists
- SetLocalVarInt(name + "_count",0); //Put a count on the list
- SetLocalVarString(name,""); //Define list w/ empty string
- return true;
- }
- bool stringListDestroy(string &in asName) {
- string name = _STRLPFX + asName;
- //If it doesn't exist - we can't destroy it
- if(GetLocalVarInt(name + "_exist")!=1) return false;
- //Else reduce all vars to min size - mark as not existing
- SetLocalVarInt(name + "_exist",0); //State list doesn't exist
- SetLocalVarInt(name + "_count",0); //Change the list count
- SetLocalVarString(name,""); //Define list w/ empty string
- return true;
- }
- bool stringListExists(string &in asName) {
- return (GetLocalVarInt(_STRLPFX + asName + "_exist") == 1);
- }
- int stringListSize(string &in asName) {
- return (GetLocalVarInt(_STRLPFX + asName + "_count"));
- }
- void stringListClear(string &in asName) {
- string name = _STRLPFX + asName;
- //Don't do anything if list doesn't exist
- if(GetLocalVarInt(name + "_exist")!=1) return;
- //Else reset list to defaults
- SetLocalVarInt(name + "_count",0); //Put a count on the list to 0
- SetLocalVarString(name,""); //Define list w/ empty string
- }
- //- List Management
- //+ Item Management
- string[] stringListItems(string &in asName) {
- string str = GetLocalVarString(_STRLPFX + asName);
- //Output array vars
- string[] output = {};
- int outputIndex = 0;
- //Early-out if string is faulty
- if(!StringContains(str,_STRLDLM)) return output;
- //String matching vars
- uint8 dlm = _STRLDLM[0];
- int last = 0;
- //Loop and add each substring
- for(uint i =0; i<str.length(); i++) //For each char
- {
- if(str[i] == dlm) //If char matches delimiter
- {
- int len = int(i) - last; //Determine length of token to read
- if(len >= 0)
- {
- output.resize(outputIndex+1); //Increase array size
- output[outputIndex] = StringSub(str,last,len); //Get token into array
- outputIndex++; //Location of next token
- }
- last = i+1; //Say token begins at the next char
- }
- }
- //Return array of substrings
- return output;
- }
- int stringListAddItem(string &in asName, string &in asItem) {
- string name = _STRLPFX + asName;
- //Return error value if list doesn't exist
- if(GetLocalVarInt(name + "_exist")!=1) return -1;
- //Add the item with the delimiter, increment count
- int index = GetLocalVarInt(name + "_count");
- AddLocalVarInt(name + "_count",1);
- AddLocalVarString(name,asItem + _STRLDLM);
- return index;
- }
- void stringListAddItems(string &in asName, string[] asItem) {
- //Doesnt exist / Bad parameter? return.
- string name = _STRLPFX + asName;
- if(GetLocalVarInt(name + "_exist")!=1 || asItem.length()==0) return;
- //Append all items together w/ the delimiter
- string item = "";
- for(uint i =0; i<asItem.length(); i++) item += asItem[i] + _STRLDLM;
- //Add this new item string, Increment count
- AddLocalVarString(name,item);
- AddLocalVarInt(name + "_count",asItem.length());
- }
- string stringListGetItem(string &in asName, int alIndex) {
- //This is just an array fetch + grab at index
- string name = _STRLPFX + asName;
- string output = "";
- if(alIndex >= 0 && GetLocalVarInt(name + "_exist") == 1) //Parameters valid enough to progress?
- {
- string[] names = stringListItems(asName); //Get list of items
- if(alIndex < int(names.length()))
- {
- output = names[alIndex]; //Index in bounds: set ouput to item
- }
- }
- return output;
- }
- int stringListGetLastIndexof(string &in asName, string &in asItem) {
- //This is a linear search of the list of items
- string name = _STRLPFX + asName;
- int index = -1;
- if(GetLocalVarInt(name + "_exist") == 1)
- {
- string[] items = stringListItems(asName); //Get list of items
- for(int i=0; i<int(items.length()); i++)
- { if(asItem == items[i]) index = i; } //Search for match. If match set index to index of match.
- }
- return index;
- }
- int stringListGetFirstIndexof(string &in asName, string &in asItem) {
- //This is a linear search of the list of items
- string name = _STRLPFX + asName;
- int index = -1;
- if(GetLocalVarInt(name + "_exist") == 1)
- {
- string[] items = stringListItems(asName); //Get list of items
- for(uint i=0; i<items.length(); i++)
- {
- if(asItem == items[i])
- {
- index = int(i);
- i = items.length();
- }
- }
- }
- return index;
- }
- bool stringListContains(string &in asName, string &in asItem) {
- return stringListGetLastIndexof(asName, asItem)>=0;
- }
- bool stringListRemoveItemAt(string &in asName, uint alIndex) {
- string name = _STRLPFX + asName;
- //List exists?
- if(GetLocalVarInt(name + "_exist")!=1) return false;
- //Get list
- string[] list = stringListItems(asName);
- //Re-add all but deleted item
- string item = "";
- int count = 0;
- bool op = false;
- for(uint i =0; i<list.length(); i++)
- {
- if(i != alIndex)
- {
- item += list[i] + _STRLDLM;
- count++;
- }
- else op=true;
- }
- //Set back to the array
- SetLocalVarInt(name + "_count",count); //Change count
- SetLocalVarString(name,item); //Redefine list
- //Return if anything was deleted
- return op;
- }
- int stringListRemoveItems(string &in asName, string asItem) {
- string name = _STRLPFX + asName;
- //List exists?
- if(GetLocalVarInt(name + "_exist")!=1) return 0;
- //Get list
- string[] list = stringListItems(asName);
- //Re-add all but deleted items
- string item = "";
- int count = 0;
- int delamt = 0;
- for(uint i =0; i<list.length(); i++)
- {
- if(asItem != list[i])
- {
- item += list[i] + _STRLDLM;
- count++;
- }
- else delamt++;
- }
- //Set back to the array
- SetLocalVarInt(name + "_count",count); //Change count
- SetLocalVarString(name,item); //Redefine list
- //Return if anything was deleted
- return delamt;
- }
- //- Item Management
- ///////////////////////////////
- //End String List
- ///////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement