ZoriaRPG

string_functions.zh for ZC 2.55

Nov 25th, 2016
98
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //string.zh
  2. //13-JUNE-2016
  3. //ZScript string handling functions, akin to those found in C
  4.  
  5. //Returns the number of chracters of a given type in a specified string.
  6. int NumCharsOf(int str, int chr){
  7.     int chars;
  8.     for ( int q = 0; q < SizeOfArray(str)-1; q++ ) {
  9.         if ( str[q] == chr ) chars++;
  10.     }
  11.     return chars;
  12. }
  13.  
  14. //Single character functions
  15. //Returns true if 'chr' is in the control code range of ascii characters
  16. bool isControlCode(int chr)
  17. {
  18.     return (chr >= 0 && chr < ' ');
  19. }
  20.  
  21. //Returns true if 'chr' is in the range of ascii characters '0' to '9'
  22. bool isNumber(int chr)
  23. {
  24.     return (chr >= '0' && chr <= '9');
  25. }
  26.  
  27. //Returns true if 'chr' is an alphabetic character
  28. bool isAlphabetic(int chr)
  29. {
  30.     return (chr >= 'a' && chr <= 'z') || (chr >= 'A' && chr <= 'Z');
  31. }
  32.  
  33. //Returns true if 'chr' is an alphanumeric character
  34. bool isAlphaNumeric(int chr)
  35. {
  36.     return isNumber(chr) || isAlphabetic(chr);
  37. }
  38.  
  39. //Returns true if 'chr' is in the set { '0'-'9', 'A'-'F' , 'a'-'f' }
  40. bool isHex(int chr)
  41. {
  42.     return isNumber(chr) || (chr >= 'A' && chr <= 'F') || (chr >= 'a' && chr <= 'f');
  43. }
  44.  
  45. //Returns true if 'chr' is an upper-case character
  46. bool isUpperCase(int chr)
  47. {
  48.     return (chr >= 'A' && chr <= 'Z');
  49. }
  50.  
  51. //Returns true if 'chr' is a lower-case character
  52. bool isLowerCase(int chr)
  53. {
  54.     return (chr >= 'a' && chr <= 'z');
  55. }
  56.  
  57. //Retruns true if a 'chr' is a blank space.
  58. bool isSpace(int chr){
  59.     return (chr == ' ');
  60. }
  61.  
  62. //Returns true if 'chr' is a vowel.
  63. bool isVowel(int chr){
  64.     return (chr == 'A' || chr == 'E' || chr == 'I'|| chr == 'O' || chr == 'U' || chr == 'a' || chr == 'e' || chr == 'i' || chr == 'o' || chr == 'u');
  65. }
  66.  
  67. //Returns true if the string contains a specific character.
  68. bool ContainsChar(int chr, int buffer){
  69.     for ( int q = 0; q <= SizeOfArray(buffer); q++ ){
  70.         if ( buffer[q] == chr ) return true;
  71.     }
  72.     return false;
  73. }
  74.  
  75. //Returns first character position if the string contains a specific character, else -1.
  76. bool ContainsCharPos(int chr, int buffer){
  77.     for ( int q = 0; q <= SizeOfArray(buffer); q++ ){
  78.         if ( buffer[q] == chr ) return q;
  79.     }
  80.     return -1;
  81. }
  82.  
  83.  
  84. //Converts all upper case characters to lower case, leaving non-alphabetic
  85. //characters unchanged
  86. int UpperToLower(int chr)
  87. {
  88.     if(!isAlphabetic(chr))
  89.         return chr;
  90.     return Cond(isLowerCase(chr), chr, chr + ('a' - 'A'));
  91. }
  92.  
  93. //Converts all lower case characters to upper case, leaving non-alphabetic
  94. //characters unchanged
  95. int LowerToUpper(int chr)
  96. {
  97.     if(!isAlphabetic(chr))
  98.         return chr;
  99.     return Cond(isLowerCase(chr), chr - ('a' - 'A'), chr);
  100. }
  101.  
  102. //Converts lower case to upper case and upper case to lower case
  103. int ConvertCase(int chr)
  104. {
  105.     if(!isAlphabetic(chr))
  106.         return chr;
  107.     return chr + Cond(isLowerCase(chr), 'A' - 'a', 'a' - 'A');
  108. }
  109.  
  110.  
  111. //Memory Manipulation
  112. //Memory Set
  113. //Sets block of memory of size 'n' pointed by 'ptr' to 'value'
  114. void memset(int ptr, int pos, int value, int n)
  115. {
  116.     for(int i = 0; i < n; i++)
  117.         ptr[pos + i] = value;
  118. }
  119. void memset(int ptr, int value, int n)
  120. {
  121.     memset(ptr, 0, value, n);
  122. }
  123.  
  124. //Memory Copy
  125. //Copys block of memory pointed by 'src' of size 'n' to 'dest'
  126. void memcpy(int dest, int dpos, int src, int spos, int n)
  127. {
  128.     for(int i = 0; i < n; i++)
  129.         dest[dpos + i] = src[spos + i];
  130. }
  131. void memcpy(int dest, int src, int n)
  132. {
  133.     memcpy(dest, 0, src, 0, n);
  134. }
  135.  
  136. //Memory Move
  137. //As memcpy, but uses a buffer so memory space can overlap
  138. int memmove(int dest, int dpos, int src, int spos, int n)
  139. {
  140.     int buffer[0x100];
  141.     for(int i = 0; i < n; i++)
  142.         buffer[i] = src[spos + i];
  143.     for(int i = 0; i < n; i++)
  144.         dest[dpos + i] = buffer[i];
  145.     return dest;
  146. }
  147.  
  148.  
  149. //Array Set
  150. //Assign all elements of array. Overloaded.
  151. void arrayset(int a,int a0,int a1,int a2,int a3,int a4,int a5,int a6,int a7,
  152.                     int a8,int a9,int aa,int ab,int ac,int ad,int ae,int af){
  153.     a[0x0] = a0; a[0x1] = a1; a[0x2] = a2; a[0x3] = a3; a[0x4] = a4; a[0x5] = a5; a[0x6] = a6; a[0x7] = a7;
  154.     a[0x8] = a8; a[0x9] = a9; a[0xa] = aa; a[0xb] = ab; a[0xc] = ac; a[0xd] = ad; a[0xe] = ae; a[0xf] = af;
  155. }
  156.  
  157.  
  158. //String Manipulation
  159. //String Copy
  160. //Copys string 'src' into string 'dest' without checking for overflow in 'dest'
  161. void strcpy(int dest, int dpos, int src, int spos)
  162. {
  163.     for(int i = 0; src[spos + i] != 0; i++)
  164.         dest[dpos + i] = src[spos + i];
  165. }
  166. void strcpy(int dest, int src)
  167. {
  168.     strcpy(dest, 0, src, 0);
  169. }
  170.  
  171. //As strcpy, but only takes the first 'n' characters from src
  172. void strncpy(int dest, int dpos, int src, int spos, int n)
  173. {
  174.     for(int i = 0; src[spos + i] != 0 && i < n; i++)
  175.         dest[dpos + i] = src[spos + i];
  176. }
  177. void strncpy(int dest, int src, int n)
  178. {
  179.     strncpy(dest, 0, src, 0, n);
  180. }
  181.  
  182. //Remove Characters
  183. //Remove all characters starting from pointer 'string'
  184. void remchr(int string, int pos)
  185. {
  186.     for(int i = 0; string[pos + i] != 0; i++)
  187.         string[pos + i] = 0;
  188. }
  189.  
  190. //Remove 'n' characters and shift string back to pointer 'string'
  191. void remnchr(int string, int pos, int n)
  192. {
  193.     int l = strlen(string);
  194.     int temp = n + pos;
  195.     for(int i = 0; string[pos + i] != 0; i++)
  196.         string[pos + i] = Cond(temp + i < l, string[temp + i] ,0);
  197. }
  198.  
  199. //String Length
  200. //Returns the length of string 'string'
  201. int strlen(int string)
  202. {
  203.     int l;
  204.     for(l = 0; string[l] != 0; l++);
  205.     return l;
  206. }
  207.  
  208. //String Concatenate
  209. //Appends string 'src' onto string 'dest' (assuming dest has enough extra memory
  210. //allocated to allow the operation)
  211. void strcat(int dest, int src, int spos)
  212. {
  213.     int i;
  214.     int l = strlen(dest);
  215.     for(i = 0; src[spos + i] != 0; i++)
  216.         dest[l + i] = src[spos + i];
  217.     dest[l + i] = 0;
  218. }
  219. void strcat(int dest, int src)
  220. {
  221.     strcat(dest, src, 0);
  222. }
  223.  
  224. //strcat up to 'n' characters in src
  225. void strncat(int dest, int src, int spos, int n)
  226. {
  227.     int i;
  228.     int l = strlen(dest);
  229.     for(i = 0; src[spos + i] != 0 && i < n; i++)
  230.         dest[l + i] = src[spos + i];
  231.     dest[l + i] = 0;
  232. }
  233. void strncat(int dest, int src, int n)
  234. {
  235.     strncat(dest, src, 0, n);
  236. }
  237.  
  238.  
  239. //String Searching
  240. //String Character
  241. //Returns the position of the first occurence of 'character' in 'string',
  242. //or -1 if none are found
  243. int strchr(int string, int pos, int chr)
  244. {
  245.     for(int i = 0; string[pos + i] != 0; i++)
  246.         if(string[pos + i] == chr)
  247.             return pos + i;
  248.     return -1;
  249. }
  250. int strchr(int string, int chr)
  251. {
  252.     return strchr(string, 0, chr);
  253. }
  254.  
  255. //String Reverse Character
  256. //Returns the position of the last occurence of 'character' in 'string'
  257. //starting from the end, or -1 if none are found
  258. int strrchr(int string, int pos, int chr)
  259. {
  260.     for(int i = strlen(string) - pos; i > 0; i--)
  261.         if(string[i] == chr)
  262.             return i;
  263.     return -1;
  264. }
  265. int strrchr(int string, int chr)
  266. {
  267.     return strrchr(string, 0, chr);
  268. }
  269.  
  270.  
  271. //String Sub-String
  272. //Returns the position of the first occurence of sub-string 'sub' in 'string',
  273. //or -1 if sub is not found
  274. int strstr(int string, int pos, int sub)
  275. {
  276.     int l = strlen(sub) - 1;
  277.     for(int i = 0; string[pos + i] != 0; i++)
  278.         for(int j = 0; sub[j] != 0 && string[pos + i + j] != 0 && string[pos + i + j] == sub[j]; j++)
  279.             if(j == l)
  280.                 return pos + i;
  281.     return -1;
  282. }
  283. int strstr(int string, int sub)
  284. {
  285.     return strstr(string, 0, sub);
  286. }
  287.  
  288. //String Span
  289. //Returns the length of characters in 'str' before a character not contained in
  290. //'keys' is found
  291. int strspn(int str, int pos, int keys)
  292. {
  293.     int i;
  294.     bool found;
  295.     for(i = 0; str[pos + i] != 0; i++)
  296.     {
  297.         found = false;
  298.         for(int j = 0; keys[j] != 0;j ++)
  299.             if(str[pos + i] == keys[j])
  300.                 found = true;
  301.         if(!found)
  302.             return pos + i;
  303.     }
  304.     return pos + i;
  305. }
  306. int strspn(int str, int keys)
  307. {
  308.     return strspn(str, 0, keys);
  309. }
  310.  
  311. //String Complement Span
  312. //Returns the length of characters in 'str' before a character contained in
  313. //'keys' is found
  314. int strcspn(int str, int pos, int keys)
  315. {
  316.     int i;
  317.     for(i = 0; str[pos + i] != 0; i++)
  318.         for(int j = 0; keys[j] != 0; j++)
  319.             if(str[pos + i] == keys[j])
  320.                 return pos + i;
  321.     return pos + i;
  322. }
  323. int strcspn(int str, int keys)
  324. {
  325.     return strcspn(str, 0, keys);
  326. }
  327.  
  328. //String Comparison
  329. //String Compare
  330. //Iterates through str1 and str2 until a character is found which is not the same in
  331. //both strings, and then returns > 0 if the character is larger in str1, and < 0 if it is
  332. //larger in str2. Returns 0 if the strings are equal
  333. int strcmp(int str1, int pos1, int str2, int pos2)
  334. {
  335.     int i;
  336.     for(i = 0; str1[pos1 + i] != 0 && str2[pos2 + i] != 0 && str1[pos1 + i] == str2[pos2 + i]; i++);
  337.     return str1[pos1 + i] - str2[pos2 + i];
  338. }
  339. int strcmp(int str1, int str2)
  340. {
  341.     return strcmp(str1, 0, str2, 0);
  342. }
  343.  
  344. //strcmp up to 'n' characters
  345. int strncmp(int str1, int pos1, int str2, int pos2, int n)
  346. {
  347.     int i;
  348.     for(i = 0; str1[pos1 + i] != 0 && str2[pos2 + i] != 0 && str1[pos1 + i] == str2[pos2 + i] && i < n; i++);
  349.     if(i == n)
  350.         i--;
  351.     return str1[pos1 + i] - str2[pos2 + i];
  352. }
  353. int strncmp(int str1, int str2, int n)
  354. {
  355.     return strncmp(str1, 0, str2, 0 , n);
  356. }
  357.  
  358.  
  359. //Converting between variables and strings
  360. //ASCII to Integer
  361. //Returns the decimal integer pointed by 'string'
  362. int atoi(int string, int pos)
  363. {
  364.     int i=0;
  365.     bool neg = false;
  366.     if(string[pos + i] == '-'){
  367.         i++;
  368.         neg = true;
  369.     }
  370.     int ret;
  371.     for(ret = 0; isNumber(string[pos + i]); i++)
  372.         ret = ret*10 + (string[pos + i] - '0');
  373.     return ret*Cond(neg, -1, 1);
  374. }
  375. int atoi(int string)
  376. {
  377.     return atoi(string, 0);
  378. }
  379.  
  380. //Integer Length
  381. //Returns the length of characters of the decimal integer pointed by 'string'
  382. int ilen(int string, int pos)
  383. {
  384.     int ret = 0;
  385.     if(string[pos] == '-')
  386.         ret++;
  387.     for(; isNumber(string[pos + ret]); ret++);
  388.     return ret;
  389. }
  390. int ilen(int string)
  391. {
  392.     return ilen(string, 0);
  393. }
  394.  
  395. //Hexadecimal ASCII to Integer
  396. //Returns the (positive) hexadecimal integer pointed by 'string'
  397. int xtoi(int string, int pos)
  398. {
  399.     int ret = 0;
  400.     for(int i = 0; isHex(string[pos + i]); i++)
  401.         ret = ret*0x10 + Cond(isNumber(string[pos + i]), string[pos + i] - '0', LowerToUpper(string[pos + i]) - ('A' + 0xA));
  402.     return ret;
  403. }
  404. int xtoi(int string)
  405. {
  406.     return xtoi(string, 0);
  407. }
  408.  
  409. //Hexadecimal Length
  410. //Returns the length of characters of the (positive) hexadecimal integer pointed by 'string'
  411. int xlen(int string, int pos)
  412. {
  413.     int ret = 0;
  414.     for(; isHex(string[pos + ret]); ret++);
  415.     return ret;
  416. }
  417. int xlen(int string)
  418. {
  419.     return xlen(string, 0);
  420. }
  421.  
  422. //ASCII to Float
  423. //Returns the floating point number pointed by 'string'
  424. float atof(int string, int pos)
  425. {
  426.     int i = 0;
  427.     bool neg = false;
  428.     if(string[pos + i] == '-')
  429.     {
  430.         i++;
  431.         neg = true;
  432.     }
  433.  
  434.     int ret = 0;
  435.     for(; isNumber(string[pos + i]); i++)
  436.         ret = ret*10 + (string[pos + i]-'0');
  437.  
  438.     if(string[pos + i] != '.')
  439.         return ret;
  440.     i++;
  441.  
  442.     int decimal = 0;
  443.     for(int j = 0; j < 4; j++)
  444.         decimal = decimal*10 + Cond(isNumber(string[pos + i + j]), (string[pos + i + j] - '0'), 0);
  445.  
  446.     return (ret + decimal / 10000) * Cond(neg, -1, 1);
  447. }
  448. float atof(int string)
  449. {
  450.     return atof(string, 0);
  451. }
  452.  
  453. //Float Length
  454. //Returns the length of characters of the floating point number pointed by 'string'
  455. int flen(int string, int pos)
  456. {
  457.     int ret = ilen(string, pos);
  458.  
  459.     if(string[pos + ret] != '.')
  460.         return ret;
  461.     ret++;
  462.  
  463.     return ret + ilen(string, pos + ret);
  464. }
  465. int flen(int string)
  466. {
  467.     return flen(string, 0);
  468. }
  469.  
  470. //ASCII to Number
  471. //Calls either atoi or atof depending on context
  472. int aton(int string, int pos)
  473. {
  474.     int i = 0;
  475.     if(string[pos + i] == '-')
  476.         i++;
  477.     for(; isNumber(string[pos + i]); i++);
  478.  
  479.     if(string[pos + i] == '.')
  480.         return atof(string, pos);
  481.     else
  482.         return atoi(string, pos);
  483. }
  484. int aton(int string)
  485. {
  486.     return aton(string, 0);
  487. }
  488.  
  489. //Number Length
  490. //Calls either flen or ilen depending on context
  491. int nlen(int string, int pos)
  492. {
  493.     int i = 0;
  494.     if(string[pos + i] == '-')
  495.         i++;
  496.     for(; isNumber(string[pos + i]); i++);
  497.  
  498.     if(string[i] == '.')
  499.         return flen(string, pos);
  500.     else
  501.         return ilen(string, pos);
  502. }
  503. int nlen(int string)
  504. {
  505.     return nlen(string, 0);
  506. }
  507.  
  508. //Integer to ASCII
  509. //Places integer 'num' into string 'string' without checking for overflow,
  510. //and returns the number of characters used
  511. int itoa(int string, int pos, int num)
  512. {
  513.     int ret = 0;
  514.     if(num < 0)
  515.     {
  516.         string[pos] = '-';
  517.         ret++;
  518.         num = -num;
  519.     }
  520.     else if(num == 0)
  521.     {
  522.         string[pos] = '0';
  523.         return 1;
  524.     }
  525.  
  526.     int digits = Floor(Log10(num) + 1);
  527.     for(int i = 0; i < digits; i++)
  528.         string[pos + ret + i] = (Floor(num / Pow(10, digits - i - 1)) % 10) + '0';
  529.     return ret + digits;
  530. }
  531. int itoa(int string, int num)
  532. {
  533.     return itoa(string, 0, num);
  534. }
  535.  
  536. //Hexadecimal Integer to ASCII
  537. //Places integer 'num' into string 'string' in base 16 without checking for overflow,
  538. //and returns the number of characters used
  539. int xtoa(int string, int pos, int num, bool upper)
  540. {
  541.     num = Floor(Abs(num));
  542.     string[pos] = '0';
  543.     string[pos+1] = 'x';
  544.     int ret = 2;
  545.  
  546.     if(num == 0)
  547.     {
  548.         string[pos+2] = '0';
  549.         return 3;
  550.     }
  551.  
  552.     int digits = Floor(LogToBase(num, 16) + 1);
  553.     int alphaoffset = Cond(upper, 'A' - 0xA, 'a' - 0xa);
  554.     for(int i = 0; i < digits; i++)
  555.     {
  556.         int coeff = (Floor(num / Pow(0x10, digits - i - 1)) % 0x10);
  557.         string[pos + ret + i] = Cond(coeff < 0xA, coeff + '0', coeff + alphaoffset);
  558.     }
  559.     return ret + digits;
  560. }
  561. int xtoa(int string, int num, bool upper)
  562. {
  563.     return xtoa(string, 0, num, upper);
  564. }
  565. int xtoa(int string, int num)
  566. {
  567.     return xtoa(string, 0, num, true);
  568. }
  569.  
  570. //Float to ASCII
  571. //Places float 'num' into string 'string' without checking for overflow,
  572. //and returns the number of characters used. If 'printall' is true, it will add 4 decimal places
  573. //regardless of the most significant digit
  574. int ftoa(int string, int pos, float num, bool printall)
  575. {
  576.     int oldPos=pos;
  577.     int place=100000;
  578.     int digit;
  579.     bool printZero=false;
  580.     int storedZeroes=0;
  581.    
  582.     if(num<0)
  583.     {
  584.         string[pos]='-';
  585.         pos++;
  586.         num=-num;
  587.     }
  588.        
  589.    
  590.     for(int i=0; i<10; i++)
  591.     {
  592.         digit=((num/place)<<0)%10;
  593.        
  594.         // If the fractional part hasn't been reached yet, or
  595.         // if all four of its digits are to be printed, this is easy.
  596.         if(place>=1 || printall)
  597.         {
  598.             if(digit>0 || printZero)
  599.             {
  600.                 string[pos]=digit+'0';
  601.                 pos++;
  602.                
  603.                 // Start printing 0 at the first non-zero digit.
  604.                 printZero=true;
  605.             }
  606.         }
  607.        
  608.         // Otherwise, it's trickier.
  609.         else
  610.         {
  611.             // A zero isn't printed unless there's something else after it.
  612.             // Don't print it, just keep count.
  613.             if(digit==0)
  614.                 storedZeroes++;
  615.            
  616.             // Any other digit flushes the zeroes and then is printed.
  617.             else
  618.             {
  619.                 for(; storedZeroes>0; storedZeroes--)
  620.                 {
  621.                     string[pos]='0';
  622.                     pos++;
  623.                 }
  624.                
  625.                 string[pos]=digit+'0';
  626.                 pos++;
  627.             }
  628.         }
  629.        
  630.         num%=place; // To make sure num/place doesn't overflow when place<1
  631.         place/=10;
  632.        
  633.         if(place==1) // Last digit before the decimal point
  634.             printZero=true;
  635.        
  636.         else if(place==0.1) // Reached the fractional part
  637.         {
  638.             string[pos]='.';
  639.             pos++;
  640.         }
  641.     }
  642.    
  643.     if(storedZeroes==4)
  644.     {
  645.         // printall is false and the number is an integer;
  646.         // just add one zero to the end.
  647.         string[pos]='0';
  648.         pos++;
  649.     }
  650.    
  651.     return pos-oldPos;
  652. }
  653. int ftoa(int string, float num, bool printall)
  654. {
  655.     return ftoa(string, 0, num, printall);
  656. }
  657. int ftoa(int string, int pos, float num)
  658. {
  659.     return ftoa(string, pos, num, false);
  660. }
  661. int ftoa(int string, float num)
  662. {
  663.     return ftoa(string, 0, num, false);
  664. }
  665.  
  666. //Number to ASCII
  667. //Checks whether 'num' is an integer or not, and calls the appropriate function
  668. int ntoa(int string, int pos, float num)
  669. {
  670.     if(num == Floor(num))
  671.         return itoa(string, pos, num);
  672.     else
  673.         return ftoa(string, pos, num, false);
  674. }
  675. int ntoa(int string, float num)
  676. {
  677.     return ntoa(string, 0, num);
  678. }
  679.  
  680.  
  681. //String Formating
  682. //String Concatenate Format
  683. //Appends 'arg' onto 'dest' as the MF_ constant passed into 'format'
  684. void strcatf(int dest, int arg, int format)
  685. {
  686.     if(format == MF_INT)
  687.     {
  688.         int buffer[0x20];
  689.         itoa(buffer, arg);
  690.         strcat(dest, buffer);
  691.     }
  692.     else if(format == MF_HEXLOWER)
  693.     {
  694.         int buffer[0x20];
  695.         xtoa(buffer, arg, false);
  696.         strcat(dest, buffer);
  697.     }
  698.     else if(format == MF_HEXUPPER)
  699.     {
  700.         int buffer[0x20];
  701.         xtoa(buffer, arg, true);
  702.         strcat(dest, buffer);
  703.     }
  704.     else if(format == MF_FLOAT)
  705.     {
  706.         int buffer[0x20];
  707.         ftoa(buffer, arg, false);
  708.         strcat(dest, buffer);
  709.     }
  710.     else if(format == MF_FLOAT)
  711.     {
  712.         int buffer[0x20];
  713.         ftoa(buffer, arg, false);
  714.         strcat(dest, buffer);
  715.     }
  716.     else if(format == MF_NUM)
  717.     {
  718.         int buffer[0x20];
  719.         ntoa(buffer, arg);
  720.         strcat(dest, buffer);
  721.     }
  722.     else if(format == MF_PTR)
  723.     {
  724.         int buffer[0x20];
  725.         itoa(buffer, arg);
  726.         strcat(dest, buffer);
  727.     }
  728.     else if(format == MF_CHAR)
  729.     {
  730.         int buffer[0x2];
  731.         arrayset(buffer, arg, 0);
  732.         strcat(dest, buffer);
  733.     }
  734.     else if(format == MF_STRING && arg != 0)
  735.         strcat(dest, arg);
  736.  
  737. }
  738.  
  739. //As strcatf, using only 'n' characters of 'arg'
  740. void strncatf(int dest, int arg, int format, int n)
  741. {
  742.     if(format == MF_INT)
  743.     {
  744.         int buffer[0x20];
  745.         itoa(buffer, arg);
  746.         strncat(dest, buffer, n);
  747.     }
  748.     else if(format == MF_HEXLOWER)
  749.     {
  750.         int buffer[0x20];
  751.         xtoa(buffer, arg, false);
  752.         strncat(dest, buffer, n);
  753.     }
  754.     else if(format == MF_HEXUPPER)
  755.     {
  756.         int buffer[0x20];
  757.         xtoa(buffer, arg, true);
  758.         strncat(dest, buffer, n);
  759.     }
  760.     else if(format == MF_FLOAT)
  761.     {
  762.         int buffer[0x20];
  763.         ftoa(buffer, arg, false);
  764.         strncat(dest, buffer, n);
  765.     }
  766.     else if(format == MF_NUM)
  767.     {
  768.         int buffer[0x20];
  769.         ntoa(buffer, arg);
  770.         strncat(dest, buffer, n);
  771.     }
  772.     else if(format == MF_PTR)
  773.     {
  774.         int buffer[0x20];
  775.         itoa(buffer, arg);
  776.         strncat(dest, buffer, n);
  777.     }
  778.     else if(format == MF_CHAR)
  779.     {
  780.         int buffer[0x2];
  781.         arrayset(buffer, arg, 0);
  782.         strncat(dest, buffer, n);
  783.     }
  784.     else if(format == MF_STRING && arg != 0)
  785.         strncat(dest, arg, n);
  786.  
  787. }
  788.  
  789. //String Print Format
  790. //Prints string 'formatstr' into 'ret' according to the arguments inputted (see C function for reference)
  791. //Maximum 16 arguments
  792. int sprintf(int ret,int formatstr,int a0,int a1,int a2,int a3,int a4,int a5,int a6,int a7,
  793.                                   int a8,int a9,int aa,int ab,int ac,int ad,int ae,int af)
  794. {
  795.     int pos = 0;
  796.     int currentarg = 0;
  797.     int a[0x10];
  798.     arrayset(a,a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,aa,ab,ac,ad,ae,af);
  799.  
  800.     for(int i = 0; formatstr[i] != 0; i++)
  801.     {
  802.         int chr = formatstr[i];
  803.         if(chr == '\') //Control code
  804.         {
  805.             int nextchr = formatstr[i + 1];
  806.             if(nextchr == 'n')
  807.             {
  808.                 ret[pos] = MSGC_LINEFEED;
  809.                 pos++;
  810.                 i++;
  811.                 continue;
  812.             }
  813.         }
  814.         else if(chr == '%') //Format argument
  815.         {
  816.             int nextchr = formatstr[i + 1];
  817.             if(sprintf_isMFCode(nextchr))
  818.             {
  819.                 strcatf(ret, a[currentarg], sprintf_MFCodeToInt(nextchr));
  820.                 for(; ret[pos] != 0; pos++);
  821.                 currentarg++;
  822.                 i++;
  823.                 continue;
  824.             }
  825.         }
  826.  
  827.         ret[pos] = chr;
  828.         pos++;
  829.     }
  830.  
  831.     return pos;
  832. }
  833.  
  834. //Print Format
  835. //Uses a buffer to print the results of sprintf(formatstr,...) straight to allegro.log
  836. void printf(int formatstr,int a0,int a1,int a2,int a3,int a4,int a5,int a6,int a7,
  837.                           int a8,int a9,int aa,int ab,int ac,int ad,int ae,int af)
  838. {
  839.     int buffer[0x200]; //Max TraceS length is 512
  840.     sprintf(buffer,formatstr,a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,aa,ab,ac,ad,ae,af);
  841.     TraceS(buffer);
  842. }
  843.  
  844. //Used by sprintf
  845. bool sprintf_isMFCode(int chr)
  846. {
  847.     return (chr == 'n' || chr == 's' || chr == 'i' || chr == 'f' || chr == 'd' ||
  848.            chr == 'p' || chr == 'c' || chr == 'x' || chr == 'X');
  849. }
  850. int sprintf_MFCodeToInt(int chr)
  851. {
  852.     if(chr == 'n') return MF_NONE;
  853.     else if(chr == 'i') return MF_INT;
  854.     else if(chr == 'f') return MF_FLOAT;
  855.     else if(chr == 'd') return MF_NUM;
  856.     else if(chr == 's') return MF_STRING;
  857.     else if(chr == 'p') return MF_PTR;
  858.     else if(chr == 'c') return MF_CHAR;
  859.     else if(chr == 'x') return MF_HEXLOWER;
  860.     else if(chr == 'X') return MF_HEXUPPER;
  861.     return -1;
  862. }
  863.  
  864.  
  865. //Function overloads
  866. int sprintf(int ret,int formatstr,int a0,int a1,int a2,int a3,int a4,int a5,int a6,int a7,
  867.                                    int a8,int a9,int aa,int ab,int ac,int ad,int ae){
  868.     return sprintf(ret,formatstr,a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,aa,ab,ac,ad,ae,0);
  869. }
  870. int sprintf(int ret,int formatstr,int a0,int a1,int a2,int a3,int a4,int a5,int a6,int a7,
  871.                                    int a8,int a9,int aa,int ab,int ac,int ad){
  872.     return sprintf(ret,formatstr,a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,aa,ab,ac,ad,0,0);
  873. }
  874. int sprintf(int ret,int formatstr,int a0,int a1,int a2,int a3,int a4,int a5,int a6,int a7,
  875.                                    int a8,int a9,int aa,int ab){
  876.     return sprintf(ret,formatstr,a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,aa,ab,0,0,0,0);
  877. }
  878. int sprintf(int ret,int formatstr,int a0,int a1,int a2,int a3,int a4,int a5,int a6,int a7,
  879.                                    int a8,int a9,int aa){
  880.     return sprintf(ret,formatstr,a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,aa,0,0,0,0,0);
  881. }
  882. int sprintf(int ret,int formatstr,int a0,int a1,int a2,int a3,int a4,int a5,int a6,int a7,
  883.                                    int a8,int a9){
  884.     return sprintf(ret,formatstr,a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,0,0,0,0,0,0);
  885. }
  886. int sprintf(int ret,int formatstr,int a0,int a1,int a2,int a3,int a4,int a5,int a6,int a7,
  887.                                    int a8){
  888.     return sprintf(ret,formatstr,a0,a1,a2,a3,a4,a5,a6,a7,a8,0,0,0,0,0,0,0);
  889. }
  890. int sprintf(int ret,int formatstr,int a0,int a1,int a2,int a3,int a4,int a5,int a6,int a7){
  891.     return sprintf(ret,formatstr,a0,a1,a2,a3,a4,a5,a6,a7,0,0,0,0,0,0,0,0);
  892. }
  893. int sprintf(int ret,int formatstr,int a0,int a1,int a2,int a3,int a4,int a5,int a6){
  894.     return sprintf(ret,formatstr,a0,a1,a2,a3,a4,a5,a6,0,0,0,0,0,0,0,0,0);
  895. }
  896. int sprintf(int ret,int formatstr,int a0,int a1,int a2,int a3,int a4,int a5){
  897.     return sprintf(ret,formatstr,a0,a1,a2,a3,a4,a5,0,0,0,0,0,0,0,0,0,0);
  898. }
  899. int sprintf(int ret,int formatstr,int a0,int a1,int a2,int a3,int a4){
  900.     return sprintf(ret,formatstr,a0,a1,a2,a3,a4,0,0,0,0,0,0,0,0,0,0,0);
  901. }
  902. int sprintf(int ret,int formatstr,int a0,int a1,int a2,int a3){
  903.     return sprintf(ret,formatstr,a0,a1,a2,a3,0,0,0,0,0,0,0,0,0,0,0,0);
  904. }
  905. int sprintf(int ret,int formatstr,int a0,int a1,int a2){
  906.     return sprintf(ret,formatstr,a0,a1,a2,0,0,0,0,0,0,0,0,0,0,0,0,0);
  907. }
  908. int sprintf(int ret,int formatstr,int a0,int a1){
  909.     return sprintf(ret,formatstr,a0,a1,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
  910. }
  911. int sprintf(int ret,int formatstr,int a0){
  912.     return sprintf(ret,formatstr,a0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
  913. }
  914. int sprintf(int ret,int formatstr){
  915.     return sprintf(ret,formatstr,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
  916. }
  917.  
  918. void printf(int formatstr,int a0,int a1,int a2,int a3,int a4,int a5,int a6,int a7,
  919.                           int a8,int a9,int aa,int ab,int ac,int ad,int ae){
  920.     printf(formatstr,a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,aa,ab,ac,ad,ae,0);
  921. }
  922. void printf(int formatstr,int a0,int a1,int a2,int a3,int a4,int a5,int a6,int a7,
  923.                           int a8,int a9,int aa,int ab,int ac,int ad){
  924.     printf(formatstr,a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,aa,ab,ac,ad,0,0);
  925. }
  926. void printf(int formatstr,int a0,int a1,int a2,int a3,int a4,int a5,int a6,int a7,
  927.                           int a8,int a9,int aa,int ab,int ac){
  928.     printf(formatstr,a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,aa,ab,ac,0,0,0);
  929. }
  930. void printf(int formatstr,int a0,int a1,int a2,int a3,int a4,int a5,int a6,int a7,
  931.                           int a8,int a9,int aa,int ab){
  932.     printf(formatstr,a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,aa,ab,0,0,0,0);
  933. }
  934. void printf(int formatstr,int a0,int a1,int a2,int a3,int a4,int a5,int a6,int a7,
  935.                           int a8,int a9,int aa){
  936.     printf(formatstr,a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,aa,0,0,0,0,0);
  937. }
  938. void printf(int formatstr,int a0,int a1,int a2,int a3,int a4,int a5,int a6,int a7,
  939.                           int a8,int a9){
  940.     printf(formatstr,a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,0,0,0,0,0,0);
  941. }
  942. void printf(int formatstr,int a0,int a1,int a2,int a3,int a4,int a5,int a6,int a7,
  943.                           int a8){
  944.     printf(formatstr,a0,a1,a2,a3,a4,a5,a6,a7,a8,0,0,0,0,0,0,0);
  945. }
  946. void printf(int formatstr,int a0,int a1,int a2,int a3,int a4,int a5,int a6,int a7){
  947.     printf(formatstr,a0,a1,a2,a3,a4,a5,a6,a7,0,0,0,0,0,0,0,0);
  948. }
  949. void printf(int formatstr,int a0,int a1,int a2,int a3,int a4,int a5,int a6){
  950.     printf(formatstr,a0,a1,a2,a3,a4,a5,a6,0,0,0,0,0,0,0,0,0);
  951. }
  952. void printf(int formatstr,int a0,int a1,int a2,int a3,int a4,int a5){
  953.     printf(formatstr,a0,a1,a2,a3,a4,a5,0,0,0,0,0,0,0,0,0,0);
  954. }
  955. void printf(int formatstr,int a0,int a1,int a2,int a3,int a4){
  956.     printf(formatstr,a0,a1,a2,a3,a4,0,0,0,0,0,0,0,0,0,0,0);
  957. }
  958. void printf(int formatstr,int a0,int a1,int a2,int a3){
  959.     printf(formatstr,a0,a1,a2,a3,0,0,0,0,0,0,0,0,0,0,0,0);
  960. }
  961. void printf(int formatstr,int a0,int a1,int a2){
  962.     printf(formatstr,a0,a1,a2,0,0,0,0,0,0,0,0,0,0,0,0,0);
  963. }
  964. void printf(int formatstr,int a0,int a1){
  965.     printf(formatstr,a0,a1,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
  966. }
  967. void printf(int formatstr,int a0){
  968.     printf(formatstr,a0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
  969. }
  970. void printf(int formatstr){
  971.     printf(formatstr,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
  972. }
  973.  
  974. void arrayset(int a,int a0,int a1,int a2,int a3,int a4,int a5,int a6,int a7,
  975.                 int a8,int a9,int aa,int ab, int ac, int ad, int ae){
  976.     a[0x0] = a0; a[0x1] = a1; a[0x2] = a2; a[0x3] = a3; a[0x4] = a4; a[0x5] = a5; a[0x6] = a6; a[0x7] = a7;
  977.     a[0x8] = a8; a[0x9] = a9; a[0xa] = aa; a[0xb] = ab; a[0xc] = ac; a[0xd] = ad; a[0xe] = ae;
  978. }
  979. void arrayset(int a,int a0,int a1,int a2,int a3,int a4,int a5,int a6,int a7,
  980.                 int a8,int a9,int aa,int ab, int ac, int ad){
  981.     a[0x0] = a0; a[0x1] = a1; a[0x2] = a2; a[0x3] = a3; a[0x4] = a4; a[0x5] = a5; a[0x6] = a6; a[0x7] = a7;
  982.     a[0x8] = a8; a[0x9] = a9; a[0xa] = aa; a[0xb] = ab; a[0xc] = ac; a[0xd] = ad;
  983. }
  984. void arrayset(int a,int a0,int a1,int a2,int a3,int a4,int a5,int a6,int a7,
  985.                 int a8,int a9,int aa,int ab, int ac){
  986.     a[0x0] = a0; a[0x1] = a1; a[0x2] = a2; a[0x3] = a3; a[0x4] = a4; a[0x5] = a5; a[0x6] = a6; a[0x7] = a7;
  987.     a[0x8] = a8; a[0x9] = a9; a[0xa] = aa; a[0xb] = ab; a[0xc] = ac;
  988. }
  989. void arrayset(int a,int a0,int a1,int a2,int a3,int a4,int a5,int a6,int a7,
  990.                 int a8,int a9,int aa,int ab){
  991.     a[0x0] = a0; a[0x1] = a1; a[0x2] = a2; a[0x3] = a3; a[0x4] = a4; a[0x5] = a5; a[0x6] = a6; a[0x7] = a7;
  992.     a[0x8] = a8; a[0x9] = a9; a[0xa] = aa; a[0xb] = ab;
  993. }
  994. void arrayset(int a,int a0,int a1,int a2,int a3,int a4,int a5,int a6,int a7,
  995.                 int a8,int a9,int aa){
  996.     a[0x0] = a0; a[0x1] = a1; a[0x2] = a2; a[0x3] = a3; a[0x4] = a4; a[0x5] = a5; a[0x6] = a6; a[0x7] = a7;
  997.     a[0x8] = a8; a[0x9] = a9; a[0xa] = aa;
  998. }
  999. void arrayset(int a,int a0,int a1,int a2,int a3,int a4,int a5,int a6,int a7,
  1000.                 int a8,int a9){
  1001.     a[0x0] = a0; a[0x1] = a1; a[0x2] = a2; a[0x3] = a3; a[0x4] = a4; a[0x5] = a5; a[0x6] = a6; a[0x7] = a7;
  1002.     a[0x8] = a8; a[0x9] = a9;
  1003. }
  1004. void arrayset(int a,int a0,int a1,int a2,int a3,int a4,int a5,int a6,int a7,
  1005.                 int a8){
  1006.     a[0x0] = a0; a[0x1] = a1; a[0x2] = a2; a[0x3] = a3; a[0x4] = a4; a[0x5] = a5; a[0x6] = a6; a[0x7] = a7;
  1007.     a[0x8] = a8;
  1008. }
  1009. void arrayset(int a,int a0,int a1,int a2,int a3,int a4,int a5,int a6,int a7){
  1010.     a[0x0] = a0; a[0x1] = a1; a[0x2] = a2; a[0x3] = a3; a[0x4] = a4; a[0x5] = a5; a[0x6] = a6; a[0x7] = a7;
  1011. }
  1012. void arrayset(int a,int a0,int a1,int a2,int a3,int a4,int a5,int a6){
  1013.     a[0x0] = a0; a[0x1] = a1; a[0x2] = a2; a[0x3] = a3; a[0x4] = a4; a[0x5] = a5; a[0x6] = a6;
  1014. }
  1015. void arrayset(int a,int a0,int a1,int a2,int a3,int a4,int a5){
  1016.     a[0x0] = a0; a[0x1] = a1; a[0x2] = a2; a[0x3] = a3; a[0x4] = a4; a[0x5] = a5;
  1017. }
  1018. void arrayset(int a,int a0,int a1,int a2,int a3,int a4){
  1019.     a[0x0] = a0; a[0x1] = a1; a[0x2] = a2; a[0x3] = a3; a[0x4] = a4;
  1020. }
  1021. void arrayset(int a,int a0,int a1,int a2,int a3){
  1022.     a[0x0] = a0; a[0x1] = a1; a[0x2] = a2; a[0x3] = a3;
  1023. }
  1024. void arrayset(int a,int a0,int a1,int a2){
  1025.     a[0x0] = a0; a[0x1] = a1; a[0x2] = a2;
  1026. }
  1027. void arrayset(int a,int a0,int a1){
  1028.     a[0x0] = a0; a[0x1] = a1;
  1029. }
  1030. void arrayset(int a,int a0){
  1031.     a[0x0] = a0;
  1032. }
  1033.  
  1034.  
  1035. int ReturnStringCharPos(int str, int chr){
  1036.     for ( int q = 0; q <= strlen(str-1); q ++ ) {
  1037.         int a = str[q];
  1038.         if ( a == chr ) return q;
  1039.     }
  1040. }
  1041.  
  1042. int IsChar(int chr) { return chr; }
  1043. bool IsChar(int chr, int comp) { return (chr == comp); }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×