PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/commoncpp2-1.8.1/src/strchar.cpp

#
C++ | 281 lines | 184 code | 59 blank | 38 comment | 40 complexity | 4c29bf466379442f65e28edd523801fc MD5 | raw file
Possible License(s): GPL-2.0
  1. // Copyright (C) 2001-2005 Open Source Telecom Corporation.
  2. // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; either version 2 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. //
  18. // As a special exception, you may use this file as part of a free software
  19. // library without restriction. Specifically, if other files instantiate
  20. // templates or use macros or inline functions from this file, or you compile
  21. // this file and link it with other files to produce an executable, this
  22. // file does not by itself cause the resulting executable to be covered by
  23. // the GNU General Public License. This exception does not however
  24. // invalidate any other reasons why the executable file might be covered by
  25. // the GNU General Public License.
  26. //
  27. // This exception applies only to the code released under the name GNU
  28. // Common C++. If you copy code from other releases into a copy of GNU
  29. // Common C++, as the General Public License permits, the exception does
  30. // not apply to the code that you add in this way. To avoid misleading
  31. // anyone as to the status of such modified files, you must delete
  32. // this exception notice from them.
  33. //
  34. // If you write modifications of your own for GNU Common C++, it is your choice
  35. // whether to permit this exception to apply to your modifications.
  36. // If you do not wish that, delete this exception notice.
  37. //
  38. #include <cc++/config.h>
  39. #include <cc++/export.h>
  40. #include <cc++/missing.h>
  41. #include <cc++/strchar.h>
  42. #include <cstdio>
  43. #include <cstdlib>
  44. #include <cstring>
  45. #ifdef WIN32
  46. #include <malloc.h>
  47. //#define alloca(x) _alloca(x)
  48. #endif
  49. #ifdef CCXX_NAMESPACES
  50. namespace ost {
  51. using namespace std;
  52. #endif
  53. char *find(const char *cs, char *str, size_t len)
  54. {
  55. unsigned pos = 0;
  56. if(!len)
  57. len = strlen(str);
  58. while(pos < len) {
  59. if(strchr(cs, str[pos]))
  60. return str + pos;
  61. ++pos;
  62. }
  63. if(!str[pos])
  64. return str + pos;
  65. return NULL;
  66. }
  67. char *rfind(const char *cs, char *str, size_t len)
  68. {
  69. if(!len)
  70. len = strlen(str);
  71. while(len--) {
  72. if(strchr(cs, str[len]))
  73. return str + len;
  74. }
  75. return str;
  76. }
  77. char *ifind(const char *cs, char *str, size_t len)
  78. {
  79. unsigned pos = 0;
  80. if(!len)
  81. len = strlen(str);
  82. while(pos < len) {
  83. if(!strchr(cs, str[pos]))
  84. return str + pos;
  85. ++pos;
  86. }
  87. if(!str[pos])
  88. return str + pos;
  89. return NULL;
  90. }
  91. char *strip(const char *chars, char *str, size_t len)
  92. {
  93. len = strtrim(chars, str, len);
  94. if(!len)
  95. return str;
  96. return ifind(chars, str, len);
  97. }
  98. size_t strtrim(const char *cs, char *str, size_t len)
  99. {
  100. if(!str)
  101. return 0;
  102. if(!len)
  103. len = strlen(str);
  104. if(!len)
  105. return 0;
  106. while(len--) {
  107. if(!strchr(cs, str[len]))
  108. return ++len;
  109. str[len] = 0;
  110. }
  111. return 0;
  112. }
  113. size_t strchop(const char *cs, char *str, size_t len)
  114. {
  115. unsigned pos = 0;
  116. if(!str)
  117. return 0;
  118. if(!len)
  119. len = strlen(str);
  120. if(!len)
  121. return 0;
  122. while(pos < len) {
  123. if(!strchr(cs, str[pos]))
  124. break;
  125. ++pos;
  126. }
  127. if(pos == len) {
  128. *str = 0;
  129. return 0;
  130. }
  131. memmove(str, str + pos, len - pos + 1);
  132. return len - pos;
  133. }
  134. char *rsetField(char *dest, size_t size, const char *src, const char fill)
  135. {
  136. size_t len = 0;
  137. if(src)
  138. len = strlen(src);
  139. if(len > size)
  140. len = size;
  141. if(len)
  142. memmove(dest + size - len, (void *)src, len);
  143. if(len < size && fill)
  144. memset(dest, fill, size - len);
  145. return dest;
  146. }
  147. char *lsetField(char *dest, size_t size, const char *src, const char fill)
  148. {
  149. size_t len = 0;
  150. if(src)
  151. len = strlen(src);
  152. if(len > size)
  153. len = size;
  154. if(len)
  155. memmove(dest, src, len);
  156. if(len < size && fill)
  157. memset(dest + len, fill, size - len);
  158. return dest;
  159. }
  160. char *setUpper(char *string, size_t size)
  161. {
  162. char *ret = string;
  163. if(!size)
  164. size = strlen(string);
  165. while(size && *string) {
  166. *string = toupper(*string);
  167. ++string;
  168. --size;
  169. }
  170. return ret;
  171. }
  172. char *setLower(char *string, size_t size)
  173. {
  174. char *ret = string;
  175. if(!size)
  176. size = strlen(string);
  177. while(size && *string) {
  178. *string = tolower(*string);
  179. ++string;
  180. --size;
  181. }
  182. return ret;
  183. }
  184. char *setString(char *dest, size_t size, const char *src)
  185. {
  186. size_t len = strlen(src);
  187. if(size == 1)
  188. *dest = 0;
  189. if(size < 2)
  190. return dest;
  191. if(len >= size)
  192. len = size - 1;
  193. if(!len) {
  194. dest[0] = 0;
  195. return dest;
  196. }
  197. memcpy(dest, src, len);
  198. dest[len] = 0;
  199. return dest;
  200. }
  201. char *addString(char *dest, size_t size, const char *src)
  202. {
  203. size_t len = strlen(dest);
  204. if(len < size)
  205. setString(dest + len, size - len, src);
  206. return dest;
  207. }
  208. char *newString(const char *src, size_t size)
  209. {
  210. char *dest;
  211. if(!size)
  212. size = strlen(src) + 1;
  213. dest = new char[size];
  214. return setString(dest, size, src);
  215. }
  216. void delString(char *str)
  217. {
  218. delete[] str;
  219. }
  220. #ifdef CCXX_NAMESPACES
  221. }
  222. #endif