Advertisement
ijontichy

stralloc.c

Apr 18th, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.00 KB | None | 0 0
  1. #include "zcommon.acs"
  2.  
  3. global int 0:strings[];
  4. global int 9:stringIndex[];
  5.  
  6. function int addString(int string)
  7. {
  8.     int strSize = StrLen(string) + 1;  // gotta remember the null byte at the start
  9.  
  10.     int index = 0; int ret; int i = 0; int j; int c;
  11.    
  12.     int wIndex = findFreeIndex(strSize);
  13.  
  14.     for (i = 0; i < strSize; i++)
  15.     {
  16.         j = i + wIndex;
  17.  
  18.         strings[j] = GetChar(string, i);
  19.     }
  20.  
  21.     strings[j + i] = 0;
  22.    
  23.     addIndex(wIndex);
  24.     return wIndex;
  25. }
  26.  
  27. function int addCleanString(int string)
  28. {
  29.     int strSize = StrLen(string) + 1;
  30.  
  31.     int index = 0; int i = 0; int j = 0; int k = 0; int c; int ignoreNext;
  32.    
  33.     int wIndex = findFreeIndex(strSize);
  34.  
  35.     for (i = 0; i < strSize; i++)
  36.     {
  37.         c = GetChar(string, i);
  38.  
  39.         if ( ( ((c > 8) && (c < 14)) || ((c > 31) && (c < 127)) || ((c > 160) && (c < 173)) ) && !ignoreNext)
  40.         {
  41.             j = (k++) + wIndex;
  42.             strings[j] = c;
  43.         }
  44.         else if (c == 28 && !ignoreNext)
  45.         {
  46.             ignoreNext = 1;
  47.         }
  48.         else
  49.         {
  50.             ignoreNext = 0;
  51.         }
  52.     }
  53.  
  54.     strings[j + 1] = 0;
  55.    
  56.     addIndex(wIndex);
  57.     return wIndex;
  58. }
  59.  
  60. function int addLowerString(int string)
  61. {
  62.     int strSize = StrLen(string) + 1;
  63.  
  64.     int index = 0; int i = 0; int j = 0; int k = 0; int c; int ignoreNext;
  65.    
  66.     int wIndex = findFreeIndex(strSize);
  67.  
  68.     for (i = 0; i < strSize; i++)
  69.     {
  70.         c = GetChar(string, i);
  71.  
  72.         if ( ( ((c > 8) && (c < 14)) || ((c > 31) && (c < 127)) || ((c > 160) && (c < 173)) ) && !ignoreNext)
  73.         {
  74.             j = (k++) + wIndex;
  75.  
  76.             if ((c > 64) && (c < 91))
  77.             {
  78.                 strings[j] = c + 32;
  79.             }
  80.             else
  81.             {
  82.                 strings[j] = c;
  83.             }
  84.         }
  85.         else if (c == 28 && !ignoreNext)
  86.         {
  87.             ignoreNext = 1;
  88.         }
  89.         else
  90.         {
  91.             ignoreNext = 0;
  92.         }
  93.     }
  94.  
  95.     strings[j + 1] = 0;
  96.    
  97.     addIndex(wIndex);
  98.     return wIndex;
  99. }
  100.  
  101. function int getString(int index)
  102. {
  103.     int ret = "";
  104.     int i = 0; int j; int c;
  105.    
  106.     if (getIndex(index) == -1)
  107.     {
  108.         return StrParam(s:"ERR");
  109.     }
  110.  
  111.     while (1)
  112.     {
  113.         j = i + index;
  114.         i += 1;
  115.  
  116.         c = strings[j];
  117.  
  118.         if (c == 0)
  119.         {
  120.             break;
  121.         }
  122.  
  123.         ret = StrParam(s:ret, c:c);
  124.     }
  125.    
  126.     return ret;
  127. }
  128.  
  129. function void freeString(int index)
  130. {
  131.     int i = 0; int j; int c;
  132.    
  133.     if (getIndex(index) == -1)
  134.     {
  135.         return;
  136.     }
  137.  
  138.     while (1)
  139.     {
  140.         j = i + index;
  141.  
  142.         c = strings[j];
  143.  
  144.         if (c == 0)
  145.         {
  146.             break;
  147.         }
  148.  
  149.         strings[j] = 0;
  150.         i += 1;
  151.     }
  152.  
  153.     freeIndex(index);
  154. }
  155.  
  156. function int stringLength(int index)
  157. {
  158.     int i = 0; int j; int c;
  159.    
  160.     if (getIndex(index) == -1)
  161.     {
  162.         return -1;
  163.     }
  164.  
  165.     while (1)
  166.     {
  167.         j = i + index;
  168.  
  169.         c = strings[j];
  170.  
  171.         if (c == 0)
  172.         {
  173.             break;
  174.         }
  175.        
  176.         i += 1;
  177.     }
  178.    
  179.     return i;
  180. }
  181.  
  182. function int reallocString(int index, int string)
  183. {
  184.     freeString(index);
  185.     return addString(string);
  186. }
  187.  
  188. function int reallocCleanString(int index, int string)
  189. {
  190.     freeString(index);
  191.     return addCleanString(string);
  192. }
  193.  
  194. function int reallocLowerString(int index, int string)
  195. {
  196.     freeString(index);
  197.     return addLowerString(string);
  198. }
  199.  
  200.  
  201.  
  202. function int findFreeIndex(int size)
  203. {
  204.     int c; int i = 0; int index = 0; int ret;
  205.  
  206.     while (1)
  207.     {
  208.         c = strings[index];
  209.        
  210.         if (c == 0)
  211.         {
  212.             if (i == 0)
  213.             {
  214.                 ret = index;
  215.             }
  216.  
  217.             i += 1;
  218.         }
  219.         else
  220.         {
  221.             i = -1;
  222.         }
  223.  
  224.         if (i >= (size - 1))
  225.         {
  226.             break;
  227.         }
  228.  
  229.         index += 1;
  230.     }
  231.  
  232.     return ret;
  233. }
  234.  
  235. function int addIndex(int index)
  236. {
  237.     int i = 0;
  238.     while (1)
  239.     {
  240.         if (stringIndex[i] == (index + 1))
  241.         {
  242.             break;
  243.         }
  244.         else if (stringIndex[i] == 0)
  245.         {
  246.             stringIndex[i] = (index + 1);
  247.             break;
  248.         }
  249.         i += 1;
  250.     }
  251.  
  252.     return i;
  253. }
  254.  
  255. function int getIndex(int index)
  256. {
  257.     int i = 0;
  258.  
  259.     while (1)
  260.     {
  261.         if (stringIndex[i] == (index + 1))
  262.         {
  263.             break;
  264.         }
  265.         else if (stringIndex[i] == 0)
  266.         {
  267.             return -1;
  268.         }
  269.  
  270.         i += 1;
  271.     }
  272.  
  273.     return i;
  274. }
  275.  
  276. function void freeIndex(int index)
  277. {
  278.     int curIndex = getIndex(index);
  279.    
  280.     if (curIndex == -1)
  281.     {
  282.         return;
  283.     }
  284.  
  285.     int i = curIndex + 1;
  286.     int j = i;
  287.  
  288.     stringIndex[curIndex] = 0;
  289.  
  290.     while (1)
  291.     {
  292.         if (stringIndex[i] == 0)
  293.         {
  294.             break;
  295.         }
  296.         j = i;
  297.         i += 1;
  298.     }
  299.    
  300.     stringIndex[curIndex] = stringIndex[j];
  301.     stringIndex[j] = 0;
  302. }
  303.  
  304.  
  305. function int dumpStrings(int stop)
  306. {
  307.     int printStr = "";
  308.     int i = 0; int strsFound = 0; int stopAtZero = 0;
  309.    
  310.     if (stop == 0)
  311.     {
  312.         stop = 0x7FFFFFFF;
  313.         stopAtZero = 1;
  314.     }
  315.  
  316.     while (i < stop)
  317.     {
  318.         if (stringIndex[i] != 0)
  319.         {
  320.             strsFound++;
  321.             printStr = StrParam(s:printStr, s:"(", d:i, s:", ", d:stringIndex[i], s:"): ", s:getString(stringIndex[i] - 1), s:"\n");
  322.         }
  323.         else if (stopAtZero)
  324.         {
  325.             break;
  326.         }
  327.         i += 1;
  328.     }
  329.    
  330.     if (strsFound == 0)
  331.     {
  332.         printStr = StrParam(s:"<NONE>"); // For consistency reasons, we use StrParam here
  333.     }
  334.  
  335.     return printStr;
  336. }
  337.  
  338. script 193 (void)
  339. {
  340.     int s1; int s2; int s3;
  341.  
  342.     s1 = addString(StrParam(n:0));
  343.     s2 = addCleanString(StrParam(n:0));
  344.     s3 = addLowerString(StrParam(n:0));
  345.  
  346.     Print(s:dumpStrings(0));
  347.  
  348.     Print(s:"(", d:getIndex(s1), s:", ", d:s1, s:"): ", s:getString(s1));
  349.     Print(s:"(", d:getIndex(s2), s:", ", d:s2, s:"): ", s:getString(s2));
  350.     Print(s:"(", d:getIndex(s3), s:", ", d:s3, s:"): ", s:getString(s3));
  351.  
  352.     Delay(1);
  353.  
  354.     s1 = reallocLowerString(s1, StrParam(n:0));
  355.     s2 = reallocString(s2, StrParam(n:0));
  356.     s3 = reallocCleanString(s3, StrParam(n:0));
  357.  
  358.     Print(s:dumpStrings(0));
  359. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement