strlib.hpp 5.7 KB

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