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