strlib.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 <ctype.h>
  7. #include "strlib.h"
  8. #include "../common/cbasetypes.h"
  9. #include "../common/utils.h"
  10. #include "../common/malloc.h"
  11. //-----------------------------------------------
  12. // string lib.
  13. char* jstrescape (char* pt) {
  14. //copy from here
  15. char *ptr;
  16. int i =0, j=0;
  17. //copy string to temporary
  18. CREATE(ptr, char, J_MAX_MALLOC_SIZE);
  19. strcpy(ptr,pt);
  20. while (ptr[i] != '\0') {
  21. switch (ptr[i]) {
  22. case '\'':
  23. pt[j++] = '\\';
  24. pt[j++] = ptr[i++];
  25. break;
  26. case '\\':
  27. pt[j++] = '\\';
  28. pt[j++] = ptr[i++];
  29. break;
  30. case '%':
  31. pt[j++] = '_'; i++;
  32. break;
  33. default:
  34. pt[j++] = ptr[i++];
  35. }
  36. }
  37. pt[j++] = '\0';
  38. aFree(ptr);
  39. return &pt[0];
  40. }
  41. char* jstrescapecpy (char* pt, const char* spt) {
  42. //copy from here
  43. //WARNING: Target string pt should be able to hold strlen(spt)*2, as each time
  44. //a escape character is found, the target's final length increases! [Skotlex]
  45. int i =0, j=0;
  46. if (!spt) { //Return an empty string [Skotlex]
  47. pt[0] = '\0';
  48. return &pt[0];
  49. }
  50. while (spt[i] != '\0') {
  51. switch (spt[i]) {
  52. case '\'':
  53. pt[j++] = '\\';
  54. pt[j++] = spt[i++];
  55. break;
  56. case '\\':
  57. pt[j++] = '\\';
  58. pt[j++] = spt[i++];
  59. break;
  60. case '%':
  61. pt[j++] = '_'; i++;
  62. break;
  63. default:
  64. pt[j++] = spt[i++];
  65. }
  66. }
  67. pt[j++] = '\0';
  68. return &pt[0];
  69. }
  70. int jmemescapecpy (char* pt,char* spt, int size) {
  71. //copy from here
  72. int i =0, j=0;
  73. while (i < size) {
  74. switch (spt[i]) {
  75. case '\'':
  76. pt[j++] = '\\';
  77. pt[j++] = spt[i++];
  78. break;
  79. case '\\':
  80. pt[j++] = '\\';
  81. pt[j++] = spt[i++];
  82. break;
  83. case '%':
  84. pt[j++] = '_'; i++;
  85. break;
  86. default:
  87. pt[j++] = spt[i++];
  88. }
  89. }
  90. // copy size is 0 ~ (j-1)
  91. return j;
  92. }
  93. //-----------------------------------------------------
  94. // Function to suppress control characters in a string.
  95. //-----------------------------------------------------
  96. //int remove_control_chars(char *str) {
  97. int remove_control_chars(unsigned char *str) {
  98. int i;
  99. int change = 0;
  100. for(i = 0; str[i]; i++) {
  101. if (str[i] < 32) {
  102. str[i] = '_';
  103. change = 1;
  104. }
  105. }
  106. return change;
  107. }
  108. //Trims a string, also removes illegal characters such as \t and reduces continous spaces to a single one. by [Foruken]
  109. char *trim(char *str, const char *delim)
  110. {
  111. char *strp = strtok(str,delim);
  112. char buf[1024];
  113. char *bufp = buf;
  114. memset(buf,0,sizeof buf);
  115. while(strp) {
  116. strcpy(bufp, strp);
  117. bufp = bufp + strlen(strp);
  118. strp = strtok(NULL, delim);
  119. if (strp) {
  120. strcpy(bufp," ");
  121. bufp++;
  122. }
  123. }
  124. strcpy(str,buf);
  125. return str;
  126. }
  127. //stristr: Case insensitive version of strstr, code taken from
  128. //http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  129. //
  130. const char *stristr(const char *haystack, const char *needle)
  131. {
  132. if ( !*needle )
  133. {
  134. return haystack;
  135. }
  136. for ( ; *haystack; ++haystack )
  137. {
  138. if ( TOUPPER(*haystack) == TOUPPER(*needle) )
  139. {
  140. /*
  141. * Matched starting char -- loop through remaining chars.
  142. */
  143. const char *h, *n;
  144. for ( h = haystack, n = needle; *h && *n; ++h, ++n )
  145. {
  146. if ( TOUPPER(*h) != TOUPPER(*n) )
  147. {
  148. break;
  149. }
  150. }
  151. if ( !*n ) /* matched all of 'needle' to null termination */
  152. {
  153. return haystack; /* return the start of the match */
  154. }
  155. }
  156. }
  157. return 0;
  158. }
  159. #ifdef __WIN32
  160. char *_strtok_r(char *s1, const char *s2, char **lasts)
  161. {
  162. char *ret;
  163. if (s1 == NULL)
  164. s1 = *lasts;
  165. while(*s1 && strchr(s2, *s1))
  166. ++s1;
  167. if(*s1 == '\0')
  168. return NULL;
  169. ret = s1;
  170. while(*s1 && !strchr(s2, *s1))
  171. ++s1;
  172. if(*s1)
  173. *s1++ = '\0';
  174. *lasts = s1;
  175. return ret;
  176. }
  177. #endif
  178. #if !defined(WIN32) || (defined(_MSC_VER) && _MSC_VER < 1400)
  179. /* Find the length of STRING, but scan at most MAXLEN characters.
  180. If no '\0' terminator is found in that many characters, return MAXLEN. */
  181. size_t strnlen (const char* string, size_t maxlen)
  182. {
  183. const char* end = memchr (string, '\0', maxlen);
  184. return end ? (size_t) (end - string) : maxlen;
  185. }
  186. #endif