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