root/src/common/strlib.h @ 21

Revision 1, 4.6 kB (checked in by jinshiro, 17 years ago)
Line 
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
12char* jstrescape (char* pt);
13char* jstrescapecpy (char* pt, const char* spt);
14int jmemescapecpy (char* pt, const char* spt, int size);
15
16int remove_control_chars(char* str);
17char* trim(char* str);
18char* normalize_name(char* str,const char* delims);
19const 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))
24char* _strtok_r(char* s1, const char* s2, char** lasts);
25#endif
26
27#if !(defined(WIN32) && defined(_MSC_VER) && _MSC_VER >= 1400) && !defined(CYGWIN)
28size_t strnlen (const char* string, size_t maxlen);
29#endif
30
31int e_mail_check(char* email);
32int config_switch(const char* str);
33
34/// always nul-terminates the string
35char* safestrncpy(char* dst, const char* src, size_t n);
36
37/// doesn't crash on null pointer
38size_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.
43int 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.
47int strline(const char* str, size_t pos);
48
49
50
51/// Bitfield determining the behaviour of sv_parse and sv_split.
52typedef 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.
74int 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.
82int 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.
87size_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.
92size_t sv_unescape_c(char* out_dest, const char* src, size_t len);
93
94/// Skips a C escape sequence (starting with '\\').
95const 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.
100bool 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
104struct StringBuf
105{
106        char *buf_;
107        char *ptr_;
108        unsigned int max_;
109};
110typedef struct StringBuf StringBuf;
111
112StringBuf* StringBuf_Malloc(void);
113void StringBuf_Init(StringBuf* self);
114int StringBuf_Printf(StringBuf* self, const char* fmt, ...);
115int StringBuf_Vprintf(StringBuf* self, const char* fmt, va_list args);
116int StringBuf_Append(StringBuf* self, const StringBuf *sbuf);
117int StringBuf_AppendStr(StringBuf* self, const char* str);
118int StringBuf_Length(StringBuf* self);
119char* StringBuf_Value(StringBuf* self);
120void StringBuf_Clear(StringBuf* self);
121void StringBuf_Destroy(StringBuf* self);
122void StringBuf_Free(StringBuf* self);
123
124#endif /* _STRLIB_H_ */
Note: See TracBrowser for help on using the browser.