Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "zcommon.acs"
- global int 0:strings[];
- global int 9:stringIndex[];
- function int addString(int string)
- {
- int strSize = StrLen(string) + 1; // gotta remember the null byte at the start
- int index = 0; int ret; int i = 0; int j; int c;
- int wIndex = findFreeIndex(strSize);
- for (i = 0; i < strSize; i++)
- {
- j = i + wIndex;
- strings[j] = GetChar(string, i);
- }
- strings[j + i] = 0;
- addIndex(wIndex);
- return wIndex;
- }
- function int addCleanString(int string)
- {
- int strSize = StrLen(string) + 1;
- int index = 0; int i = 0; int j = 0; int k = 0; int c; int ignoreNext;
- int wIndex = findFreeIndex(strSize);
- for (i = 0; i < strSize; i++)
- {
- c = GetChar(string, i);
- if ( ( ((c > 8) && (c < 14)) || ((c > 31) && (c < 127)) || ((c > 160) && (c < 173)) ) && !ignoreNext)
- {
- j = (k++) + wIndex;
- strings[j] = c;
- }
- else if (c == 28 && !ignoreNext)
- {
- ignoreNext = 1;
- }
- else
- {
- ignoreNext = 0;
- }
- }
- strings[j + 1] = 0;
- addIndex(wIndex);
- return wIndex;
- }
- function int addLowerString(int string)
- {
- int strSize = StrLen(string) + 1;
- int index = 0; int i = 0; int j = 0; int k = 0; int c; int ignoreNext;
- int wIndex = findFreeIndex(strSize);
- for (i = 0; i < strSize; i++)
- {
- c = GetChar(string, i);
- if ( ( ((c > 8) && (c < 14)) || ((c > 31) && (c < 127)) || ((c > 160) && (c < 173)) ) && !ignoreNext)
- {
- j = (k++) + wIndex;
- if ((c > 64) && (c < 91))
- {
- strings[j] = c + 32;
- }
- else
- {
- strings[j] = c;
- }
- }
- else if (c == 28 && !ignoreNext)
- {
- ignoreNext = 1;
- }
- else
- {
- ignoreNext = 0;
- }
- }
- strings[j + 1] = 0;
- addIndex(wIndex);
- return wIndex;
- }
- function int getString(int index)
- {
- int ret = "";
- int i = 0; int j; int c;
- if (getIndex(index) == -1)
- {
- return StrParam(s:"ERR");
- }
- while (1)
- {
- j = i + index;
- i += 1;
- c = strings[j];
- if (c == 0)
- {
- break;
- }
- ret = StrParam(s:ret, c:c);
- }
- return ret;
- }
- function void freeString(int index)
- {
- int i = 0; int j; int c;
- if (getIndex(index) == -1)
- {
- return;
- }
- while (1)
- {
- j = i + index;
- c = strings[j];
- if (c == 0)
- {
- break;
- }
- strings[j] = 0;
- i += 1;
- }
- freeIndex(index);
- }
- function int stringLength(int index)
- {
- int i = 0; int j; int c;
- if (getIndex(index) == -1)
- {
- return -1;
- }
- while (1)
- {
- j = i + index;
- c = strings[j];
- if (c == 0)
- {
- break;
- }
- i += 1;
- }
- return i;
- }
- function int reallocString(int index, int string)
- {
- freeString(index);
- return addString(string);
- }
- function int reallocCleanString(int index, int string)
- {
- freeString(index);
- return addCleanString(string);
- }
- function int reallocLowerString(int index, int string)
- {
- freeString(index);
- return addLowerString(string);
- }
- function int findFreeIndex(int size)
- {
- int c; int i = 0; int index = 0; int ret;
- while (1)
- {
- c = strings[index];
- if (c == 0)
- {
- if (i == 0)
- {
- ret = index;
- }
- i += 1;
- }
- else
- {
- i = -1;
- }
- if (i >= (size - 1))
- {
- break;
- }
- index += 1;
- }
- return ret;
- }
- function int addIndex(int index)
- {
- int i = 0;
- while (1)
- {
- if (stringIndex[i] == (index + 1))
- {
- break;
- }
- else if (stringIndex[i] == 0)
- {
- stringIndex[i] = (index + 1);
- break;
- }
- i += 1;
- }
- return i;
- }
- function int getIndex(int index)
- {
- int i = 0;
- while (1)
- {
- if (stringIndex[i] == (index + 1))
- {
- break;
- }
- else if (stringIndex[i] == 0)
- {
- return -1;
- }
- i += 1;
- }
- return i;
- }
- function void freeIndex(int index)
- {
- int curIndex = getIndex(index);
- if (curIndex == -1)
- {
- return;
- }
- int i = curIndex + 1;
- int j = i;
- stringIndex[curIndex] = 0;
- while (1)
- {
- if (stringIndex[i] == 0)
- {
- break;
- }
- j = i;
- i += 1;
- }
- stringIndex[curIndex] = stringIndex[j];
- stringIndex[j] = 0;
- }
- function int dumpStrings(int stop)
- {
- int printStr = "";
- int i = 0; int strsFound = 0; int stopAtZero = 0;
- if (stop == 0)
- {
- stop = 0x7FFFFFFF;
- stopAtZero = 1;
- }
- while (i < stop)
- {
- if (stringIndex[i] != 0)
- {
- strsFound++;
- printStr = StrParam(s:printStr, s:"(", d:i, s:", ", d:stringIndex[i], s:"): ", s:getString(stringIndex[i] - 1), s:"\n");
- }
- else if (stopAtZero)
- {
- break;
- }
- i += 1;
- }
- if (strsFound == 0)
- {
- printStr = StrParam(s:"<NONE>"); // For consistency reasons, we use StrParam here
- }
- return printStr;
- }
- script 193 (void)
- {
- int s1; int s2; int s3;
- s1 = addString(StrParam(n:0));
- s2 = addCleanString(StrParam(n:0));
- s3 = addLowerString(StrParam(n:0));
- Print(s:dumpStrings(0));
- Print(s:"(", d:getIndex(s1), s:", ", d:s1, s:"): ", s:getString(s1));
- Print(s:"(", d:getIndex(s2), s:", ", d:s2, s:"): ", s:getString(s2));
- Print(s:"(", d:getIndex(s3), s:", ", d:s3, s:"): ", s:getString(s3));
- Delay(1);
- s1 = reallocLowerString(s1, StrParam(n:0));
- s2 = reallocString(s2, StrParam(n:0));
- s3 = reallocCleanString(s3, StrParam(n:0));
- Print(s:dumpStrings(0));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement