strutil.h 5.1 KB

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