1 | // Copyright (c) Athena Dev Teams - Licensed under GNU GPL |
---|
2 | // For more information, see LICENCE in the main folder |
---|
3 | |
---|
4 | #ifndef _STRLIB_H_ |
---|
5 | #define _STRLIB_H_ |
---|
6 | |
---|
7 | #ifndef _CBASETYPES_H_ |
---|
8 | #include "../common/cbasetypes.h" |
---|
9 | #endif |
---|
10 | #include <stdarg.h> |
---|
11 | |
---|
12 | char* jstrescape (char* pt); |
---|
13 | char* jstrescapecpy (char* pt, const char* spt); |
---|
14 | int jmemescapecpy (char* pt, const char* spt, int size); |
---|
15 | |
---|
16 | int remove_control_chars(char* str); |
---|
17 | char* trim(char* str); |
---|
18 | char* normalize_name(char* str,const char* delims); |
---|
19 | const char *stristr(const char *haystack, const char *needle); |
---|
20 | |
---|
21 | #ifdef WIN32 |
---|
22 | #define HAVE_STRTOK_R |
---|
23 | #define strtok_r(s,delim,save_ptr) _strtok_r((s),(delim),(save_ptr)) |
---|
24 | char* _strtok_r(char* s1, const char* s2, char** lasts); |
---|
25 | #endif |
---|
26 | |
---|
27 | #if !(defined(WIN32) && defined(_MSC_VER) && _MSC_VER >= 1400) && !defined(CYGWIN) |
---|
28 | size_t strnlen (const char* string, size_t maxlen); |
---|
29 | #endif |
---|
30 | |
---|
31 | int e_mail_check(char* email); |
---|
32 | int config_switch(const char* str); |
---|
33 | |
---|
34 | /// always nul-terminates the string |
---|
35 | char* safestrncpy(char* dst, const char* src, size_t n); |
---|
36 | |
---|
37 | /// doesn't crash on null pointer |
---|
38 | size_t safestrnlen(const char* string, size_t maxlen); |
---|
39 | |
---|
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 | |
---|
45 | /// Returns the line of the target position in the string. |
---|
46 | /// Lines start at 1. |
---|
47 | int strline(const char* str, size_t pos); |
---|
48 | |
---|
49 | |
---|
50 | |
---|
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 | |
---|
66 | /// Other escape sequences supported by the C compiler. |
---|
67 | #define SV_ESCAPE_C_SUPPORTED "abtnvfr\?\"'\\" |
---|
68 | |
---|
69 | /// Parses a delim-separated string. |
---|
70 | /// Starts parsing at startoff and fills the pos array with position pairs. |
---|
71 | /// out_pos[0] and out_pos[1] are the start and end of line. |
---|
72 | /// Other position pairs are the start and end of fields. |
---|
73 | /// Returns the number of fields found or -1 if an error occurs. |
---|
74 | int sv_parse(const char* str, int len, int startoff, char delim, int* out_pos, int npos, enum e_svopt opt); |
---|
75 | |
---|
76 | /// Splits a delim-separated string. |
---|
77 | /// WARNING: this function modifies the input string |
---|
78 | /// Starts splitting at startoff and fills the out_fields array. |
---|
79 | /// out_fields[0] is the start of the next line. |
---|
80 | /// Other entries are the start of fields (nul-teminated). |
---|
81 | /// Returns the number of fields found or -1 if an error occurs. |
---|
82 | int sv_split(char* str, int len, int startoff, char delim, char** out_fields, int nfields, enum e_svopt opt); |
---|
83 | |
---|
84 | /// Escapes src to out_dest according to the format of the C compiler. |
---|
85 | /// Returns the length of the escaped string. |
---|
86 | /// out_dest should be len*4+1 in size. |
---|
87 | size_t sv_escape_c(char* out_dest, const char* src, size_t len, const char* escapes); |
---|
88 | |
---|
89 | /// Unescapes src to out_dest according to the format of the C compiler. |
---|
90 | /// Returns the length of the unescaped string. |
---|
91 | /// out_dest should be len+1 in size and can be the same buffer as src. |
---|
92 | size_t sv_unescape_c(char* out_dest, const char* src, size_t len); |
---|
93 | |
---|
94 | /// Skips a C escape sequence (starting with '\\'). |
---|
95 | const char* skip_escaped_c(const char* p); |
---|
96 | |
---|
97 | /// Opens and parses a file containing delim-separated columns, feeding them to the specified callback function row by row. |
---|
98 | /// Tracks the progress of the operation (current line number, number of successfully processed rows). |
---|
99 | /// Returns 'true' if it was able to process the specified file, or 'false' if it could not be read. |
---|
100 | 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)); |
---|
101 | |
---|
102 | |
---|
103 | /// StringBuf - dynamic string |
---|
104 | struct StringBuf |
---|
105 | { |
---|
106 | char *buf_; |
---|
107 | char *ptr_; |
---|
108 | unsigned int max_; |
---|
109 | }; |
---|
110 | typedef struct StringBuf StringBuf; |
---|
111 | |
---|
112 | StringBuf* StringBuf_Malloc(void); |
---|
113 | void StringBuf_Init(StringBuf* self); |
---|
114 | int StringBuf_Printf(StringBuf* self, const char* fmt, ...); |
---|
115 | int StringBuf_Vprintf(StringBuf* self, const char* fmt, va_list args); |
---|
116 | int StringBuf_Append(StringBuf* self, const StringBuf *sbuf); |
---|
117 | int StringBuf_AppendStr(StringBuf* self, const char* str); |
---|
118 | int StringBuf_Length(StringBuf* self); |
---|
119 | char* StringBuf_Value(StringBuf* self); |
---|
120 | void StringBuf_Clear(StringBuf* self); |
---|
121 | void StringBuf_Destroy(StringBuf* self); |
---|
122 | void StringBuf_Free(StringBuf* self); |
---|
123 | |
---|
124 | #endif /* _STRLIB_H_ */ |
---|