strutil.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. #endif //_WIN32
  84. /**
  85. * Creates a new string @length bytes long filled with @fill_char.
  86. * Creates a new string @length bytes long filled with @fill_char.
  87. */
  88. TOOLKIT_API char* strnfill(int length, int fill_char);
  89. /**
  90. * Compares str1 and str2 like strcmp(). Handles NULL strings gracefully.
  91. */
  92. TOOLKIT_API int strcmp0 (const char *str1,const char *str2);
  93. /**
  94. * create a string from format list
  95. */
  96. TOOLKIT_API char* TOOLKIT_CC strdup_printf(const char *format, ...);
  97. TOOLKIT_API char* TOOLKIT_CC strdup_vprintf(const char *format, va_list args);
  98. /**
  99. * Copies src to dest;
  100. * dest is guaranteed to be nul-terminated;
  101. * src must be nul-terminated;
  102. * dest_size is the buffer size, not the number of chars to copy.
  103. * Caveat: strlcpy() is supposedly more secure than strcpy() or strncpy(),
  104. * but if you really want to avoid screwups, g_strdup() is an even better idea.
  105. */
  106. TOOLKIT_API size_t strlcpy(char *dest, const char *src, size_t size);
  107. TOOLKIT_API size_t strlcat(char *dest, const char *src, size_t size);
  108. TOOLKIT_API ssize_t strscpy(char* dest, const char* src, size_t size);
  109. /**
  110. * Copies NULL-terminated array of strings. The copy is a deep copy;
  111. * the new array should be freed by first freeing each string,
  112. * then the array itself. strfreev() does this for you.
  113. * If called on a NULL value, g_strdupv() simply returns NULL.
  114. */
  115. TOOLKIT_API char** strdupv (char **str_array);
  116. /**
  117. * Joins a number of strings together to form one long string,
  118. * with the optional separator inserted between each of them.
  119. * @return a newly-allocated string containing all of the strings joined together, with separator between them.
  120. */
  121. TOOLKIT_API char* strjoinv (const char *separator,char **str_array);
  122. TOOLKIT_API char* TOOLKIT_CC strjoin (const char *separator, ...);
  123. /**
  124. * Looks whether the string str ends with suffix.
  125. */
  126. TOOLKIT_API int str_has_suffix (const char *str, const char *suffix);
  127. /**
  128. * Looks whether the string str starts with prefix.
  129. */
  130. TOOLKIT_API int str_has_prefix (const char *str,const char *prefix);
  131. /**
  132. * Returns the length of the given NULL-terminated string array str_array.
  133. */
  134. TOOLKIT_API unsigned int strv_length (char **str_array);
  135. /** xml escape */
  136. TOOLKIT_API char *str_xml_escape(const char *src);
  137. /** parse cmd line */
  138. TOOLKIT_API void str_parse_cmdline (char *cmdstart,char **argv,char *args,int *numargs,int *numchars);
  139. TOOLKIT_API void str_get_format_uuid(char* strbuffer, size_t ulen);
  140. TOOLKIT_API int str_reset_buffer(void* pbuf, int idata, size_t ulen);
  141. TOOLKIT_API int str_rend_string(char* pSrc, int iflag);
  142. TOOLKIT_API int str_fifter_string(char* pbuf, size_t usize, const char* psrc, const char cflag);
  143. TOOLKIT_API int str_get_interger_netaddr(char* strbuf, size_t ubufszie, const char* szip);
  144. TOOLKIT_API int str_convert_interaddr_strip(char* strbuf, size_t ubufszie, const char* szinter_ip);
  145. TOOLKIT_API int str_connect_strings(char* strbuf, size_t ulen, const char* psrca, const char* psrcb, const char* pstr);
  146. #ifdef __cplusplus
  147. } // extern "C" {
  148. #endif
  149. #endif // STRUTIL_H