strutil.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #ifndef STRUTIL_H
  2. #define STRUTIL_H
  3. #pragma once
  4. #include "config.h"
  5. #include <stddef.h>
  6. #include <stdarg.h>
  7. #include <stdio.h>
  8. #include <limits.h>
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. /* c-string extension funtions */
  13. /**
  14. * search string in buffer,
  15. * memstr : buffer search, case sensitive
  16. * memistr : buffer search, case insensitive
  17. */
  18. TOOLKIT_API const char *memstr(const char *buf, int n, const char *str);
  19. TOOLKIT_API char *memstr1(char *buf, int n, const char *str);
  20. TOOLKIT_API const char *memrstr(const char *buf, int n, const char *str);
  21. TOOLKIT_API const char *memistr(const char *buf, int n, const char *str);
  22. TOOLKIT_API const char *memristr(const char *buf, int n, const char *str);
  23. #ifdef _WIN32
  24. TOOLKIT_API void* memmem(const void* haystack, size_t haystacklen,
  25. const void* needle, size_t needlelen);
  26. TOOLKIT_API char* strcasestr(const char* haystack, const char* needle);
  27. #endif //_WIN32
  28. /**
  29. * trim string inplace
  30. * strltrim : left trim
  31. * strrtrim : right trim
  32. * strtrim : both left and right trim
  33. * strnormws: removes white-space surrounding the string, and
  34. * converts all remaining groups of white-space to a single space.
  35. */
  36. TOOLKIT_API char *strtrim(char *s, const char *trims);
  37. TOOLKIT_API char *strltrim(char *s, const char *trims);
  38. TOOLKIT_API char *strrtrim(char *s, const char *trims);
  39. TOOLKIT_API char *strnormws(char *s);
  40. #ifdef _WIN32
  41. /**
  42. * copy string and return pointer to the end null-terminated char
  43. */
  44. TOOLKIT_API char* stpcpy(char* dst, const char* src);
  45. /**
  46. * string tokenize
  47. */
  48. TOOLKIT_API char* strsep(char** stringp, const char* delim);
  49. TOOLKIT_API char* strtok_r(char* str, const char* delim, char** tracker);
  50. #endif //_WIN32
  51. /**
  52. * replace occurrences of substring in string, return value should be freed by free
  53. */
  54. TOOLKIT_API char *strrepleace(const char *str, const char *oldstr, const char *newstr);
  55. /**
  56. * split string to string list, use strarrayfree to free memory
  57. */
  58. TOOLKIT_API char **strsplit(const char *s, const char *delim);
  59. TOOLKIT_API void strfreev(char **strings);
  60. /**
  61. * substring
  62. */
  63. TOOLKIT_API char *strsub(char *dest,char *src, size_t offset, size_t len);
  64. TOOLKIT_API char *strleft(char *dest,char *src, size_t len);
  65. TOOLKIT_API char *strright(char *dest,char *src, size_t len);
  66. /**
  67. * allocate a concatenation of strings, Use (char*)0, not NULL, to end the list of parameters.
  68. */
  69. TOOLKIT_API char * TOOLKIT_CC stralloc(const char *arg1, ...);
  70. TOOLKIT_API char * TOOLKIT_CC stralloca(const char *arg1, ...);
  71. /**
  72. * duplicate a memory buffer
  73. */
  74. TOOLKIT_API void *memdup(const void *buf, int len);
  75. TOOLKIT_API void *memdupa(const void *buf, int len);
  76. #ifdef _WIN32
  77. TOOLKIT_API char* strdupa(const char* str);
  78. /**
  79. * Duplicates the first @n bytes of a string, returning a newly-allocated
  80. * conflict with /usr/include/string.h
  81. */
  82. TOOLKIT_API char *strndup(const char *str, int n);
  83. #else
  84. #define _strnicmp strncasecmp
  85. #endif //_WIN32
  86. /**
  87. * Creates a new string @length bytes long filled with @fill_char.
  88. * Creates a new string @length bytes long filled with @fill_char.
  89. */
  90. TOOLKIT_API char* strnfill(int length, int fill_char);
  91. /**
  92. * Compares str1 and str2 like strcmp(). Handles NULL strings gracefully.
  93. */
  94. TOOLKIT_API int strcmp0 (const char *str1,const char *str2);
  95. /**
  96. * create a string from format list
  97. */
  98. TOOLKIT_API char* TOOLKIT_CC strdup_printf(const char *format, ...);
  99. TOOLKIT_API char* TOOLKIT_CC strdup_vprintf(const char *format, va_list args);
  100. /**
  101. * Copies src to dest;
  102. * dest is guaranteed to be nul-terminated;
  103. * src must be nul-terminated;
  104. * dest_size is the buffer size, not the number of chars to copy.
  105. * Caveat: strlcpy() is supposedly more secure than strcpy() or strncpy(),
  106. * but if you really want to avoid screwups, g_strdup() is an even better idea.
  107. */
  108. TOOLKIT_API size_t strlcpy(char *dest, const char *src, size_t size);
  109. TOOLKIT_API size_t strlcat(char *dest, const char *src, size_t size);
  110. TOOLKIT_API ssize_t strscpy(char* dest, const char* src, size_t size);
  111. /**
  112. * Copies NULL-terminated array of strings. The copy is a deep copy;
  113. * the new array should be freed by first freeing each string,
  114. * then the array itself. strfreev() does this for you.
  115. * If called on a NULL value, g_strdupv() simply returns NULL.
  116. */
  117. TOOLKIT_API char** strdupv (char **str_array);
  118. /**
  119. * Joins a number of strings together to form one long string,
  120. * with the optional separator inserted between each of them.
  121. * @return a newly-allocated string containing all of the strings joined together, with separator between them.
  122. */
  123. TOOLKIT_API char* strjoinv (const char *separator,char **str_array);
  124. TOOLKIT_API char* TOOLKIT_CC strjoin (const char *separator, ...);
  125. /**
  126. * Looks whether the string str ends with suffix.
  127. */
  128. TOOLKIT_API int str_has_suffix (const char *str, const char *suffix);
  129. /**
  130. * Looks whether the string str starts with prefix.
  131. */
  132. TOOLKIT_API int str_has_prefix (const char *str,const char *prefix);
  133. /**
  134. * Returns the length of the given NULL-terminated string array str_array.
  135. */
  136. TOOLKIT_API unsigned int strv_length (char **str_array);
  137. /** xml escape */
  138. TOOLKIT_API char *str_xml_escape(const char *src);
  139. /** parse cmd line */
  140. TOOLKIT_API void str_parse_cmdline (char *cmdstart,char **argv,char *args,int *numargs,int *numchars);
  141. TOOLKIT_API void str_get_format_uuid(char* strbuffer, size_t ulen);
  142. TOOLKIT_API int str_reset_buffer(void* pbuf, int idata, size_t ulen);
  143. TOOLKIT_API int str_rend_string(char* pSrc, int iflag);
  144. TOOLKIT_API int str_fifter_string(char* pbuf, size_t usize, const char* psrc, const char cflag);
  145. TOOLKIT_API int str_get_interger_netaddr(char* strbuf, size_t ubufszie, const char* szip);
  146. TOOLKIT_API int str_convert_interaddr_strip(char* strbuf, size_t ubufszie, const char* szinter_ip);
  147. TOOLKIT_API int str_connect_strings(char* strbuf, size_t ulen, const char* psrca, const char* psrcb, const char* pstr);
  148. #define proxy_copy(pDst, pSrc) if(pSrc) { memcpy(pDst, pSrc, strlen(pSrc)); }
  149. #ifdef __cplusplus
  150. } // extern "C" {
  151. #endif
  152. #endif // STRUTIL_H