wdlcstring.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. WDL - wdlcstring.h
  3. Copyright (C) 2005 and later, Cockos Incorporated
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /*
  19. C string manipulation utilities -- [v]snprintf for Win32, also snprintf_append, lstrcatn, etc
  20. */
  21. #ifndef _WDL_CSTRING_H_
  22. #define _WDL_CSTRING_H_
  23. #include <stdarg.h>
  24. #include <stdio.h>
  25. #include <ctype.h>
  26. #include "wdltypes.h"
  27. #ifdef _WDL_CSTRING_IMPL_ONLY_
  28. #ifdef _WDL_CSTRING_IF_ONLY_
  29. #undef _WDL_CSTRING_IF_ONLY_
  30. #endif
  31. #define _WDL_CSTRING_PREFIX
  32. #else
  33. #define _WDL_CSTRING_PREFIX static WDL_STATICFUNC_UNUSED
  34. #endif
  35. #if defined(_WIN32) && defined(_MSC_VER)
  36. // provide snprintf()/vsnprintf() for win32 -- note that these have no way of knowing
  37. // what the amount written was, code should(must) be written to not depend on this.
  38. #ifdef snprintf
  39. #undef snprintf
  40. #endif
  41. #define snprintf WDL_snprintf
  42. #ifdef vsnprintf
  43. #undef vsnprintf
  44. #endif
  45. #define vsnprintf WDL_vsnprintf
  46. #endif // win32 snprintf/vsnprintf
  47. // use wdlcstring.h's lstrcpyn_safe rather than the real lstrcpyn.
  48. #ifdef _WIN32
  49. #ifdef lstrcpyn
  50. #undef lstrcpyn
  51. #endif
  52. #define lstrcpyn lstrcpyn_safe
  53. #endif
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. #ifdef _WDL_CSTRING_IF_ONLY_
  58. void lstrcpyn_safe(char *o, const char *in, INT_PTR count);
  59. void lstrcatn(char *o, const char *in, INT_PTR count);
  60. void WDL_VARARG_WARN(printf,3,4) snprintf_append(char *o, INT_PTR count, const char *format, ...);
  61. void vsnprintf_append(char *o, INT_PTR count, const char *format, va_list va);
  62. const char *WDL_get_filepart(const char *str); // returns whole string if no dir chars
  63. const char *WDL_get_fileext(const char *str); // returns ".ext" or end of string "" if no extension
  64. char *WDL_remove_fileext(char *str); // returns pointer to "ext" if ".ext" was removed (zero-d dot), or NULL
  65. char WDL_remove_filepart(char *str); // returns dir character that was zeroed, or 0 if new string is empty
  66. int WDL_remove_trailing_dirchars(char *str); // returns trailing dirchar count removed, will not convert "/" into ""
  67. size_t WDL_remove_trailing_crlf(char *str); // returns new length
  68. #if defined(_WIN32) && defined(_MSC_VER)
  69. void WDL_vsnprintf(char *o, size_t count, const char *format, va_list args);
  70. void WDL_VARARG_WARN(printf,3,4) WDL_snprintf(char *o, size_t count, const char *format, ...);
  71. #endif
  72. int WDL_strcmp_logical(const char *s1, const char *s2, int case_sensitive);
  73. #else
  74. #if defined(_WIN32) && defined(_MSC_VER)
  75. _WDL_CSTRING_PREFIX void WDL_vsnprintf(char *o, size_t count, const char *format, va_list args)
  76. {
  77. if (count>0)
  78. {
  79. int rv;
  80. o[0]=0;
  81. rv=_vsnprintf(o,count,format,args); // returns -1 if over, and does not null terminate, ugh
  82. if (rv < 0 || rv>=(int)count-1) o[count-1]=0;
  83. }
  84. }
  85. _WDL_CSTRING_PREFIX void WDL_VARARG_WARN(printf,3,4) WDL_snprintf(char *o, size_t count, const char *format, ...)
  86. {
  87. if (count>0)
  88. {
  89. int rv;
  90. va_list va;
  91. va_start(va,format);
  92. o[0]=0;
  93. rv=_vsnprintf(o,count,format,va); // returns -1 if over, and does not null terminate, ugh
  94. va_end(va);
  95. if (rv < 0 || rv>=(int)count-1) o[count-1]=0;
  96. }
  97. }
  98. #endif
  99. _WDL_CSTRING_PREFIX void lstrcpyn_safe(char *o, const char *in, INT_PTR count)
  100. {
  101. if (count>0)
  102. {
  103. while (--count>0 && *in) *o++ = *in++;
  104. *o=0;
  105. }
  106. }
  107. _WDL_CSTRING_PREFIX void lstrcatn(char *o, const char *in, INT_PTR count)
  108. {
  109. if (count>0)
  110. {
  111. while (*o) { if (--count < 1) return; o++; }
  112. while (--count>0 && *in) *o++ = *in++;
  113. *o=0;
  114. }
  115. }
  116. _WDL_CSTRING_PREFIX const char *WDL_get_filepart(const char *str) // returns whole string if no dir chars
  117. {
  118. const char *p = str;
  119. while (*p) p++;
  120. while (p >= str && !WDL_IS_DIRCHAR(*p)) --p;
  121. return p + 1;
  122. }
  123. _WDL_CSTRING_PREFIX const char *WDL_get_fileext(const char *str) // returns ".ext" or end of string "" if no extension
  124. {
  125. const char *p=str, *ep;
  126. while (*p) p++;
  127. ep = p;
  128. while (p >= str && !WDL_IS_DIRCHAR(*p))
  129. {
  130. if (*p == '.') return p;
  131. --p;
  132. }
  133. return ep;
  134. }
  135. _WDL_CSTRING_PREFIX char *WDL_remove_fileext(char *str) // returns pointer to "ext" if ".ext" was removed (zero-d dot), or NULL
  136. {
  137. char *p=str;
  138. while (*p) p++;
  139. while (p >= str && !WDL_IS_DIRCHAR(*p))
  140. {
  141. if (*p == '.')
  142. {
  143. *p = 0;
  144. return p+1;
  145. }
  146. --p;
  147. }
  148. return NULL;
  149. }
  150. _WDL_CSTRING_PREFIX char WDL_remove_filepart(char *str) // returns dir character that was zeroed, or 0 if new string is empty
  151. {
  152. char *p=str;
  153. while (*p) p++;
  154. while (p >= str)
  155. {
  156. char c = *p;
  157. if (WDL_IS_DIRCHAR(c))
  158. {
  159. *p = 0;
  160. return c;
  161. }
  162. --p;
  163. }
  164. str[0] = 0;
  165. return 0;
  166. }
  167. _WDL_CSTRING_PREFIX int WDL_remove_trailing_dirchars(char *str) // returns trailing dirchar count removed
  168. {
  169. int cnt = 0;
  170. char *p=str;
  171. while (*p) p++;
  172. while (p > str+1 && WDL_IS_DIRCHAR(p[-1]))
  173. {
  174. cnt++;
  175. p--;
  176. }
  177. *p = 0;
  178. return cnt;
  179. }
  180. _WDL_CSTRING_PREFIX size_t WDL_remove_trailing_crlf(char *str) // returns new length
  181. {
  182. char *p=str;
  183. while (*p) p++;
  184. while (p > str && (p[-1] == '\r' || p[-1] == '\n')) p--;
  185. *p = 0;
  186. return p-str;
  187. }
  188. _WDL_CSTRING_PREFIX void WDL_VARARG_WARN(printf,3,4) snprintf_append(char *o, INT_PTR count, const char *format, ...)
  189. {
  190. if (count>0)
  191. {
  192. va_list va;
  193. while (*o) { if (--count < 1) return; o++; }
  194. va_start(va,format);
  195. vsnprintf(o,count,format,va);
  196. va_end(va);
  197. }
  198. }
  199. _WDL_CSTRING_PREFIX void vsnprintf_append(char *o, INT_PTR count, const char *format, va_list va)
  200. {
  201. if (count>0)
  202. {
  203. while (*o) { if (--count < 1) return; o++; }
  204. vsnprintf(o,count,format,va);
  205. }
  206. }
  207. _WDL_CSTRING_PREFIX int WDL_strcmp_logical(const char *s1, const char *s2, int case_sensitive)
  208. {
  209. // also exists as WDL_LogicalSortStringKeyedArray::_cmpstr()
  210. char lastNonZeroChar=0;
  211. // last matching character, updated if not 0. this allows us to track whether
  212. // we are inside of a number with the same leading digits
  213. for (;;)
  214. {
  215. char c1=*s1++, c2=*s2++;
  216. if (!c1) return c1-c2;
  217. if (c1!=c2)
  218. {
  219. if (c1 >= '0' && c1 <= '9' && c2 >= '0' && c2 <= '9')
  220. {
  221. int lzdiff=0, cnt=0;
  222. if (lastNonZeroChar < '1' || lastNonZeroChar > '9')
  223. {
  224. while (c1 == '0') { c1=*s1++; lzdiff--; }
  225. while (c2 == '0') { c2=*s2++; lzdiff++; } // lzdiff = lz2-lz1, more leading 0s = earlier in list
  226. }
  227. for (;;)
  228. {
  229. if (c1 >= '0' && c1 <= '9')
  230. {
  231. if (c2 < '0' || c2 > '9') return 1;
  232. c1=s1[cnt];
  233. c2=s2[cnt++];
  234. }
  235. else
  236. {
  237. if (c2 >= '0' && c2 <= '9') return -1;
  238. break;
  239. }
  240. }
  241. s1--;
  242. s2--;
  243. while (cnt--)
  244. {
  245. const int d = *s1++ - *s2++;
  246. if (d) return d;
  247. }
  248. if (lzdiff) return lzdiff;
  249. }
  250. else
  251. {
  252. if (!case_sensitive)
  253. {
  254. if (c1>='a' && c1<='z') c1+='A'-'a';
  255. if (c2>='a' && c2<='z') c2+='A'-'a';
  256. }
  257. if (c1 != c2) return c1-c2;
  258. }
  259. }
  260. else if (c1 != '0') lastNonZeroChar=c1;
  261. }
  262. }
  263. #endif
  264. #ifdef __cplusplus
  265. };
  266. #endif
  267. #undef _WDL_CSTRING_PREFIX
  268. #endif