Author: Bleep <helveytw@home.com>
[ircu2.10.12-pk.git] / include / ircd_string.h
1 /*
2  * ircd_string.h
3  *
4  * $Id$
5  */
6 #ifndef INCLUDED_ircd_string_h
7 #define INCLUDED_ircd_string_h
8 #ifndef INCLUDED_config_h
9 #include "config.h"
10 #endif
11 #ifndef INCLUDED_ircd_chattr_h
12 #include "ircd_chattr.h"
13 #endif
14
15 /*
16  * Macros
17  */
18 #define EmptyString(x) (!(x) || !(*x))
19
20 /*
21  * initialize recognizers
22  */
23 extern int init_string(void);
24
25 extern int string_is_hostname(const char* str);
26 extern int string_is_address(const char* str);
27
28 extern char*       ircd_strncpy(char* dest, const char* src, size_t len);
29 extern int         ircd_strcmp(const char *a, const char *b);
30 extern int         ircd_strncmp(const char *a, const char *b, size_t n);
31 extern int         unique_name_vector(char* names, char token, char** vector, int size);
32 extern int         token_vector(char* names, char token, char** vector, int size);
33 extern const char* ircd_ntoa(const char* addr);
34 extern const char* ircd_ntoa_r(char* buf, const char* addr);
35 extern char*       host_from_uh(char* buf, const char* userhost, size_t len);
36 extern char*       ircd_strtok(char** save, char* str, char* fs);
37
38 extern char*       sprintf_irc(char* str, const char* format, ...); 
39
40 extern char*       canonize(char* buf);
41
42 #define DupString(x, y)  (strcpy((x = (char*) MyMalloc(strlen(y) + 1)), y))
43
44
45 /* String classification pseudo-functions, when others are needed add them,
46    strIsXxxxx(s) is true when IsXxxxx(c) is true for every char in s */
47
48 #define strIsAlnum(s)     (strChattr(s) & NTL_ALNUM)
49 #define strIsAlpha(s)     (strChattr(s) & NTL_ALPHA)
50 #define strIsDigit(s)     (strChattr(s) & NTL_DIGIT)
51 #define strIsLower(s)     (strChattr(s) & NTL_LOWER)
52 #define strIsSpace(s)     (strChattr(s) & NTL_SPACE)
53 #define strIsUpper(s)     (strChattr(s) & NTL_UPPER)
54
55 #define strIsIrcCh(s)     (strChattr(s) & NTL_IRCCH)
56 #define strIsIrcCl(s)     (strChattr(s) & NTL_IRCCL)
57 #define strIsIrcNk(s)     (strChattr(s) & NTL_IRCNK)
58 #define strIsIrcUi(s)     (strChattr(s) & NTL_IRCUI)
59 #define strIsIrcHn(s)     (strChattr(s) & NTL_IRCHN)
60 #define strIsIrcIp(s)     (strChattr(s) & NTL_IRCIP)
61
62 /*
63  * Critical small functions to inline even in separate compilation
64  * when FORCEINLINE is defined (provided you have a compiler that supports
65  * `inline').
66  */
67
68 #define NTL_HDR_strChattr   unsigned int strChattr(const char *s)
69
70 #define NTL_SRC_strChattr   const char *rs = s; \
71                             unsigned int x = ~0; \
72                             while(*rs) \
73                               x &= IRCD_CharAttrTab[*rs++ - CHAR_MIN]; \
74                             return x;
75
76 /*
77  * XXX - bleah should return 1 if different 0 if the same
78  */
79 #define NTL_HDR_strCasediff int strCasediff(const char *a, const char *b)
80
81 #define NTL_SRC_strCasediff const char *ra = a; \
82                             const char *rb = b; \
83                             while(ToLower(*ra) == ToLower(*rb++)) \
84                               if(!*ra++) \
85                                 return 0; \
86                             return 1;
87
88 #ifndef FORCEINLINE
89 extern NTL_HDR_strChattr;
90 extern NTL_HDR_strCasediff;
91
92 #else /* FORCEINLINE */
93 #ifdef __cplusplus
94 inline NTL_HDR_strChattr { NTL_SRC_strChattr }
95 inline NTL_HDR_strCasediff { NTL_SRC_strCasediff }
96 #else
97 static __inline__ NTL_HDR_strChattr { NTL_SRC_strChattr }
98 static __inline__ NTL_HDR_strCasediff { NTL_SRC_strCasediff }
99 #endif
100 #endif /* FORCEINLINE */
101
102 /*
103  * Proto types of other externally visible functions
104  */
105 extern int strnChattr(const char *s, const size_t n);
106
107 #endif /* INCLUDED_ircd_string_h */
108