strlib.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #ifndef _STRLIB_H_
  4. #define _STRLIB_H_
  5. #ifndef _CBASETYPES_H_
  6. #include "../common/cbasetypes.h"
  7. #endif
  8. #include <stdarg.h>
  9. char* jstrescape (char* pt);
  10. char* jstrescapecpy (char* pt, const char* spt);
  11. int jmemescapecpy (char* pt, const char* spt, int size);
  12. int remove_control_chars(char* str);
  13. char* trim(char* str);
  14. char* normalize_name(char* str,const char* delims);
  15. const char *stristr(const char *haystack, const char *needle);
  16. #ifdef WIN32
  17. #define HAVE_STRTOK_R
  18. #define strtok_r(s,delim,save_ptr) _strtok_r((s),(delim),(save_ptr))
  19. char* _strtok_r(char* s1, const char* s2, char** lasts);
  20. #endif
  21. #if !(defined(WIN32) && defined(_MSC_VER) && _MSC_VER >= 1400) && !defined(CYGWIN)
  22. size_t strnlen (const char* string, size_t maxlen);
  23. #endif
  24. int e_mail_check(char* email);
  25. int config_switch(const char* str);
  26. /// always nul-terminates the string
  27. char* safestrncpy(char* dst, const char* src, size_t n);
  28. /// doesn't crash on null pointer
  29. size_t safestrnlen(const char* string, size_t maxlen);
  30. /// Works like snprintf, but always nul-terminates the buffer.
  31. /// Returns the size of the string (without nul-terminator)
  32. /// or -1 if the buffer is too small.
  33. int safesnprintf(char* buf, size_t sz, const char* fmt, ...);
  34. /// Returns the line of the target position in the string.
  35. /// Lines start at 1.
  36. int strline(const char* str, size_t pos);
  37. /// Bitfield determining the behaviour of sv_parse and sv_split.
  38. typedef enum e_svopt
  39. {
  40. // default: no escapes and no line terminator
  41. SV_NOESCAPE_NOTERMINATE = 0,
  42. // Escapes according to the C compiler.
  43. SV_ESCAPE_C = 1,
  44. // Line terminators
  45. SV_TERMINATE_LF = 2,
  46. SV_TERMINATE_CRLF = 4,
  47. SV_TERMINATE_CR = 8,
  48. // If sv_split keeps the end of line terminator, instead of replacing with '\0'
  49. SV_KEEP_TERMINATOR = 16
  50. } e_svopt;
  51. /// Other escape sequences supported by the C compiler.
  52. #define SV_ESCAPE_C_SUPPORTED "abtnvfr\?\"'\\"
  53. /// Parses a delim-separated string.
  54. /// Starts parsing at startoff and fills the pos array with position pairs.
  55. /// out_pos[0] and out_pos[1] are the start and end of line.
  56. /// Other position pairs are the start and end of fields.
  57. /// Returns the number of fields found or -1 if an error occurs.
  58. int sv_parse(const char* str, int len, int startoff, char delim, int* out_pos, int npos, enum e_svopt opt);
  59. /// Splits a delim-separated string.
  60. /// WARNING: this function modifies the input string
  61. /// Starts splitting at startoff and fills the out_fields array.
  62. /// out_fields[0] is the start of the next line.
  63. /// Other entries are the start of fields (nul-teminated).
  64. /// Returns the number of fields found or -1 if an error occurs.
  65. int sv_split(char* str, int len, int startoff, char delim, char** out_fields, int nfields, enum e_svopt opt);
  66. /// Escapes src to out_dest according to the format of the C compiler.
  67. /// Returns the length of the escaped string.
  68. /// out_dest should be len*4+1 in size.
  69. size_t sv_escape_c(char* out_dest, const char* src, size_t len, const char* escapes);
  70. /// Unescapes src to out_dest according to the format of the C compiler.
  71. /// Returns the length of the unescaped string.
  72. /// out_dest should be len+1 in size and can be the same buffer as src.
  73. size_t sv_unescape_c(char* out_dest, const char* src, size_t len);
  74. /// Skips a C escape sequence (starting with '\\').
  75. const char* skip_escaped_c(const char* p);
  76. /// Opens and parses a file containing delim-separated columns, feeding them to the specified callback function row by row.
  77. /// Tracks the progress of the operation (current line number, number of successfully processed rows).
  78. /// Returns 'true' if it was able to process the specified file, or 'false' if it could not be read.
  79. bool sv_readdb(const char* directory, const char* filename, char delim, int mincols, int maxcols, int maxrows, bool (*parseproc)(char* fields[], int columns, int current));
  80. /// StringBuf - dynamic string
  81. struct StringBuf
  82. {
  83. char *buf_;
  84. char *ptr_;
  85. unsigned int max_;
  86. };
  87. typedef struct StringBuf StringBuf;
  88. StringBuf* StringBuf_Malloc(void);
  89. void StringBuf_Init(StringBuf* self);
  90. int StringBuf_Printf(StringBuf* self, const char* fmt, ...);
  91. int StringBuf_Vprintf(StringBuf* self, const char* fmt, va_list args);
  92. int StringBuf_Append(StringBuf* self, const StringBuf *sbuf);
  93. int StringBuf_AppendStr(StringBuf* self, const char* str);
  94. int StringBuf_Length(StringBuf* self);
  95. char* StringBuf_Value(StringBuf* self);
  96. void StringBuf_Clear(StringBuf* self);
  97. void StringBuf_Destroy(StringBuf* self);
  98. void StringBuf_Free(StringBuf* self);
  99. #endif /* _STRLIB_H_ */