strlib.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #include "../common/cbasetypes.h"
  6. #include <stdarg.h>
  7. #define __USE_GNU // required to enable strnlen on some platforms
  8. #include <string.h>
  9. #undef __USE_GNU
  10. char* jstrescape (char* pt);
  11. char* jstrescapecpy (char* pt, const char* spt);
  12. int jmemescapecpy (char* pt, const char* spt, int size);
  13. int remove_control_chars(char* str);
  14. char* trim(char* str);
  15. char* normalize_name(char* str,const char* delims);
  16. const char *stristr(const char *haystack, const char *needle);
  17. #ifdef WIN32
  18. #define HAVE_STRTOK_R
  19. #define strtok_r(s,delim,save_ptr) _strtok_r((s),(delim),(save_ptr))
  20. char* _strtok_r(char* s1, const char* s2, char** lasts);
  21. #endif
  22. #if !(defined(WIN32) && defined(_MSC_VER) && _MSC_VER >= 1400) && !defined(HAVE_STRNLEN)
  23. size_t strnlen (const char* string, size_t maxlen);
  24. #endif
  25. #if defined(WIN32) && defined(_MSC_VER) && _MSC_VER <= 1200
  26. uint64 strtoull(const char* str, char** endptr, int base);
  27. #endif
  28. int e_mail_check(char* email);
  29. int config_switch(const char* str);
  30. /// strncpy that always nul-terminates the string
  31. char* safestrncpy(char* dst, const char* src, size_t n);
  32. /// doesn't crash on null pointer
  33. size_t safestrnlen(const char* string, size_t maxlen);
  34. /// Works like snprintf, but always nul-terminates the buffer.
  35. /// Returns the size of the string (without nul-terminator)
  36. /// or -1 if the buffer is too small.
  37. int safesnprintf(char* buf, size_t sz, const char* fmt, ...);
  38. /// Returns the line of the target position in the string.
  39. /// Lines start at 1.
  40. int strline(const char* str, size_t pos);
  41. /// Produces the hexadecimal representation of the given input.
  42. /// The output buffer must be at least count*2+1 in size.
  43. /// Returns true on success, false on failure.
  44. bool bin2hex(char* output, unsigned char* input, size_t count);
  45. /// Bitfield determining the behaviour of sv_parse and sv_split.
  46. typedef enum e_svopt
  47. {
  48. // default: no escapes and no line terminator
  49. SV_NOESCAPE_NOTERMINATE = 0,
  50. // Escapes according to the C compiler.
  51. SV_ESCAPE_C = 1,
  52. // Line terminators
  53. SV_TERMINATE_LF = 2,
  54. SV_TERMINATE_CRLF = 4,
  55. SV_TERMINATE_CR = 8,
  56. // If sv_split keeps the end of line terminator, instead of replacing with '\0'
  57. SV_KEEP_TERMINATOR = 16
  58. } e_svopt;
  59. /// Other escape sequences supported by the C compiler.
  60. #define SV_ESCAPE_C_SUPPORTED "abtnvfr\?\"'\\"
  61. /// Parse state.
  62. /// The field is [start,end[
  63. struct s_svstate
  64. {
  65. const char* str; //< string to parse
  66. int len; //< string length
  67. int off; //< current offset in the string
  68. int start; //< where the field starts
  69. int end; //< where the field ends
  70. enum e_svopt opt; //< parse options
  71. char delim; //< field delimiter
  72. bool done; //< if all the text has been parsed
  73. };
  74. /// Parses a single field in a delim-separated string.
  75. /// The delimiter after the field is skipped.
  76. ///
  77. /// @param sv Parse state
  78. /// @return 1 if a field was parsed, 0 if done, -1 on error.
  79. int sv_parse_next(struct s_svstate* sv);
  80. /// Parses a delim-separated string.
  81. /// Starts parsing at startoff and fills the pos array with position pairs.
  82. /// out_pos[0] and out_pos[1] are the start and end of line.
  83. /// Other position pairs are the start and end of fields.
  84. /// Returns the number of fields found or -1 if an error occurs.
  85. int sv_parse(const char* str, int len, int startoff, char delim, int* out_pos, int npos, enum e_svopt opt);
  86. /// Splits a delim-separated string.
  87. /// WARNING: this function modifies the input string
  88. /// Starts splitting at startoff and fills the out_fields array.
  89. /// out_fields[0] is the start of the next line.
  90. /// Other entries are the start of fields (nul-teminated).
  91. /// Returns the number of fields found or -1 if an error occurs.
  92. int sv_split(char* str, int len, int startoff, char delim, char** out_fields, int nfields, enum e_svopt opt);
  93. /// Escapes src to out_dest according to the format of the C compiler.
  94. /// Returns the length of the escaped string.
  95. /// out_dest should be len*4+1 in size.
  96. size_t sv_escape_c(char* out_dest, const char* src, size_t len, const char* escapes);
  97. /// Unescapes src to out_dest according to the format of the C compiler.
  98. /// Returns the length of the unescaped string.
  99. /// out_dest should be len+1 in size and can be the same buffer as src.
  100. size_t sv_unescape_c(char* out_dest, const char* src, size_t len);
  101. /// Skips a C escape sequence (starting with '\\').
  102. const char* skip_escaped_c(const char* p);
  103. /// Opens and parses a file containing delim-separated columns, feeding them to the specified callback function row by row.
  104. /// Tracks the progress of the operation (current line number, number of successfully processed rows).
  105. /// Returns 'true' if it was able to process the specified file, or 'false' if it could not be read.
  106. 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));
  107. /// StringBuf - dynamic string
  108. struct StringBuf
  109. {
  110. char *buf_;
  111. char *ptr_;
  112. unsigned int max_;
  113. };
  114. typedef struct StringBuf StringBuf;
  115. StringBuf* StringBuf_Malloc(void);
  116. void StringBuf_Init(StringBuf* self);
  117. int StringBuf_Printf(StringBuf* self, const char* fmt, ...);
  118. int StringBuf_Vprintf(StringBuf* self, const char* fmt, va_list args);
  119. int StringBuf_Append(StringBuf* self, const StringBuf *sbuf);
  120. int StringBuf_AppendStr(StringBuf* self, const char* str);
  121. int StringBuf_Length(StringBuf* self);
  122. char* StringBuf_Value(StringBuf* self);
  123. void StringBuf_Clear(StringBuf* self);
  124. void StringBuf_Destroy(StringBuf* self);
  125. void StringBuf_Free(StringBuf* self);
  126. #endif /* _STRLIB_H_ */