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