OSDN Git Service

First version
[st-ro/stro.git] / src / common / strlib.h
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 #include "cbasetypes.h"
8 #include <stdarg.h>
9
10 #if !defined(__USE_GNU)
11 #define __USE_GNU  // required to enable strnlen on some platforms
12 #define __USED_GNU
13 #endif
14 #include <string.h>
15 #if defined(__USED_GNU)
16 #undef __USE_GNU
17 #undef __USED_GNU
18 #endif
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 char* jstrescape (char* pt);
25 char* jstrescapecpy (char* pt, const char* spt);
26 int jmemescapecpy (char* pt, const char* spt, int size);
27
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);
32
33 #ifdef WIN32
34 #define HAVE_STRTOK_R
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);
37 #endif
38
39 #if !(defined(WIN32) && defined(_MSC_VER) && _MSC_VER >= 1400) && !defined(HAVE_STRNLEN)
40 size_t strnlen (const char* string, size_t maxlen);
41 #endif
42
43 #if defined(WIN32) && defined(_MSC_VER) && _MSC_VER <= 1200
44 uint64 strtoull(const char* str, char** endptr, int base);
45 #endif
46
47 int e_mail_check(char* email);
48 int config_switch(const char* str);
49
50 /// strncpy that always nul-terminates the string
51 char* safestrncpy(char* dst, const char* src, size_t n);
52
53 /// doesn't crash on null pointer
54 size_t safestrnlen(const char* string, size_t maxlen);
55
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, ...);
60
61 /// Returns the line of the target position in the string.
62 /// Lines start at 1.
63 int strline(const char* str, size_t pos);
64
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);
69
70
71 /// Bitfield determining the behaviour of sv_parse and sv_split.
72 typedef enum e_svopt
73 {
74         // default: no escapes and no line terminator
75         SV_NOESCAPE_NOTERMINATE = 0,
76         // Escapes according to the C compiler.
77         SV_ESCAPE_C = 1,
78         // Line terminators
79         SV_TERMINATE_LF = 2,
80         SV_TERMINATE_CRLF = 4,
81         SV_TERMINATE_CR = 8,
82         // If sv_split keeps the end of line terminator, instead of replacing with '\0'
83         SV_KEEP_TERMINATOR = 16
84 } e_svopt;
85
86 /// Other escape sequences supported by the C compiler.
87 #define SV_ESCAPE_C_SUPPORTED "abtnvfr\?\"'\\"
88
89 /// Parse state.
90 /// The field is [start,end[
91 struct s_svstate
92 {
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
101 };
102
103 /// Parses a single field in a delim-separated string.
104 /// The delimiter after the field is skipped.
105 ///
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);
109
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);
116
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);
124
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);
129
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);
134
135 /// Skips a C escape sequence (starting with '\\').
136 const char* skip_escaped_c(const char* p);
137
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);
142
143
144 /// StringBuf - dynamic string
145 struct StringBuf
146 {
147         char *buf_;
148         char *ptr_;
149         unsigned int max_;
150 };
151 typedef struct StringBuf StringBuf;
152
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);
164
165 #ifdef __cplusplus
166 }
167 #endif
168
169 #endif /* _STRLIB_H_ */