strlib.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "strlib.h"
  7. #include "utils.h"
  8. #include "malloc.h"
  9. //-----------------------------------------------
  10. // string lib.
  11. char* jstrescape (char* pt) {
  12. //copy from here
  13. char *ptr;
  14. int i =0, j=0;
  15. //copy string to temporary
  16. CREATE_A(ptr, char, J_MAX_MALLOC_SIZE);
  17. strcpy(ptr,pt);
  18. while (ptr[i] != '\0') {
  19. switch (ptr[i]) {
  20. case '\'':
  21. pt[j++] = '\\';
  22. pt[j++] = ptr[i++];
  23. break;
  24. case '\\':
  25. pt[j++] = '\\';
  26. pt[j++] = ptr[i++];
  27. break;
  28. case '%':
  29. pt[j++] = '_'; i++;
  30. break;
  31. default:
  32. pt[j++] = ptr[i++];
  33. }
  34. }
  35. pt[j++] = '\0';
  36. aFree (ptr);
  37. return &pt[0];
  38. }
  39. char* jstrescapecpy (char* pt,char* spt) {
  40. //copy from here
  41. //WARNING: Target string pt should be able to hold strlen(spt)*2, as each time
  42. //a escape character is found, the target's final length increases! [Skotlex]
  43. int i =0, j=0;
  44. if (!spt) { //Return an empty string [Skotlex]
  45. pt[0] = '\0';
  46. return &pt[0];
  47. }
  48. while (spt[i] != '\0') {
  49. switch (spt[i]) {
  50. case '\'':
  51. pt[j++] = '\\';
  52. pt[j++] = spt[i++];
  53. break;
  54. case '\\':
  55. pt[j++] = '\\';
  56. pt[j++] = spt[i++];
  57. break;
  58. case '%':
  59. pt[j++] = '_'; i++;
  60. break;
  61. default:
  62. pt[j++] = spt[i++];
  63. }
  64. }
  65. pt[j++] = '\0';
  66. return &pt[0];
  67. }
  68. int jmemescapecpy (char* pt,char* spt, int size) {
  69. //copy from here
  70. int i =0, j=0;
  71. while (i < size) {
  72. switch (spt[i]) {
  73. case '\'':
  74. pt[j++] = '\\';
  75. pt[j++] = spt[i++];
  76. break;
  77. case '\\':
  78. pt[j++] = '\\';
  79. pt[j++] = spt[i++];
  80. break;
  81. case '%':
  82. pt[j++] = '_'; i++;
  83. break;
  84. default:
  85. pt[j++] = spt[i++];
  86. }
  87. }
  88. // copy size is 0 ~ (j-1)
  89. return j;
  90. }
  91. //-----------------------------------------------------
  92. // Function to suppress control characters in a string.
  93. //-----------------------------------------------------
  94. //int remove_control_chars(char *str) {
  95. int remove_control_chars(unsigned char *str) {
  96. int i;
  97. int change = 0;
  98. for(i = 0; str[i]; i++) {
  99. if (str[i] < 32) {
  100. str[i] = '_';
  101. change = 1;
  102. }
  103. }
  104. return change;
  105. }
  106. //Trims a string, also removes illegal characters such as \t and reduces continous spaces to a single one. by [Foruken]
  107. char *trim(char *str, const char *delim)
  108. {
  109. char *strp = strtok(str,delim);
  110. char buf[1024];
  111. char *bufp = buf;
  112. memset(buf,0,sizeof buf);
  113. while(strp) {
  114. strcpy(bufp, strp);
  115. bufp = bufp + strlen(strp);
  116. strp = strtok(NULL, delim);
  117. if (strp) {
  118. strcpy(bufp," ");
  119. bufp++;
  120. }
  121. }
  122. strcpy(str,buf);
  123. return str;
  124. }
  125. #ifdef _WIN32
  126. char *athena_strtok_r (char *s, const char *delim, char **save_ptr)
  127. {
  128. char *token;
  129. if (s == NULL)
  130. s = *save_ptr;
  131. /* Scan leading delimiters. */
  132. s += strspn (s, delim);
  133. if (*s == '\0')
  134. {
  135. *save_ptr = s;
  136. return NULL;
  137. }
  138. /* Find the end of the token. */
  139. token = s;
  140. s = strpbrk (token, delim);
  141. if (s == NULL)
  142. /* This token finishes the string. */
  143. /* *save_ptr = __rawmemchr (token, '\0'); */
  144. *save_ptr = token + strlen (token);
  145. else
  146. {
  147. /* Terminate the token and make *SAVE_PTR point past it. */
  148. *s = '\0';
  149. *save_ptr = s + 1;
  150. }
  151. return token;
  152. }
  153. #endif