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