added basic ssl support to ircu
[ircu2.10.12-pk.git] / include / ircd_string.h
1 /** @file ircd_string.h
2  * @brief Public declarations and APIs for string operations.
3  * @version $Id$
4  */
5 #ifndef INCLUDED_ircd_string_h
6 #define INCLUDED_ircd_string_h
7
8 #include <string.h> /* for DupString()'s strcpy, strlen */
9
10 #ifndef INCLUDED_ircd_chattr_h
11 #include "ircd_chattr.h"
12 #endif
13
14 struct irc_in_addr;
15
16 /*
17  * Macros
18  */
19 /** Check whether \a x is a NULL or empty string. */
20 #define EmptyString(x) (!(x) || !(*x))
21
22 extern int string_has_wildcards(const char* str);
23
24 extern char*       ircd_strncpy(char* dest, const char* src, size_t len);
25 extern int         ircd_strcmp(const char *a, const char *b);
26 extern int         ircd_strncmp(const char *a, const char *b, size_t n);
27 extern int         unique_name_vector(char* names, char token,
28                                       char** vector, int size);
29 extern int         token_vector(char* names, char token,
30                                 char** vector, int size);
31 extern const char* ircd_ntoa(const struct irc_in_addr* addr);
32 extern const char* ircd_ntoa_r(char* buf, const struct irc_in_addr* addr);
33 #define ircd_aton(ADDR, STR) ipmask_parse((STR), (ADDR), NULL)
34 extern int ipmask_parse(const char *in, struct irc_in_addr *mask, unsigned char *bits_ptr);
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*       canonize(char* buf);
39
40 /** Make \a y a duplicate \a x, a la strdup(). */
41 #define DupString(x, y)  (strcpy((x = (char*) MyMalloc(strlen(y) + 1)), y))
42
43
44 /* String classification pseudo-functions, when others are needed add them,
45    strIsXxxxx(s) is true when IsXxxxx(c) is true for every char in s */
46
47 /** Test whether all characters in \a s are alphanumeric. */
48 #define strIsAlnum(s)     (strChattr(s) & NTL_ALNUM)
49 /** Test whether all characters in \a s are alphabetic. */
50 #define strIsAlpha(s)     (strChattr(s) & NTL_ALPHA)
51 /** Test whether all characters in \a s are digits. */
52 #define strIsDigit(s)     (strChattr(s) & NTL_DIGIT)
53 /** Test whether all characters in \a s are lower case. */
54 #define strIsLower(s)     (strChattr(s) & NTL_LOWER)
55 /** Test whether all characters in \a s are whitespace. */
56 #define strIsSpace(s)     (strChattr(s) & NTL_SPACE)
57 /** Test whether all characters in \a s are upper case. */
58 #define strIsUpper(s)     (strChattr(s) & NTL_UPPER)
59
60 /** Test whether all characters in \a s are channel name characters. */
61 #define strIsIrcCh(s)     (strChattr(s) & NTL_IRCCH)
62 /** Test whether all characters in \a s are forced to lower-case in channel names. */
63 #define strIsIrcCl(s)     (strChattr(s) & NTL_IRCCL)
64 /** Test whether all characters in \a s are valid in nicknames. */
65 #define strIsIrcNk(s)     (strChattr(s) & NTL_IRCNK)
66 /** Test whether all characters in \a s are valid in a user field. */
67 #define strIsIrcUi(s)     (strChattr(s) & NTL_IRCUI)
68 /** Test whether all characters in \a s are valid in host names. */
69 #define strIsIrcHn(s)     (strChattr(s) & NTL_IRCHN)
70 /** Test whether all characters in \a s are valid in IP addresses. */
71 #define strIsIrcIp(s)     (strChattr(s) & NTL_IRCIP)
72
73 /*
74  * Critical small functions to inline even in separate compilation
75  * when FORCEINLINE is defined (provided you have a compiler that supports
76  * `inline').
77  */
78
79 /** Declaration for strChattr(). */
80 #define NTL_HDR_strChattr   unsigned int strChattr(const char *s)
81
82 /** Body for strChattr(). */
83 #define NTL_SRC_strChattr   const char *rs = s; \
84                             unsigned int x = ~0; \
85                             while(*rs) \
86                               x &= IRCD_CharAttrTab[*rs++ - CHAR_MIN]; \
87                             return x;
88
89 /*
90  * XXX - bleah should return 1 if different 0 if the same
91  */
92 /** Declaration for strCasediff(). */
93 #define NTL_HDR_strCasediff int strCasediff(const char *a, const char *b)
94
95 /** Body for strCasediff(). */
96 #define NTL_SRC_strCasediff const char *ra = a; \
97                             const char *rb = b; \
98                             while(ToLower(*ra) == ToLower(*rb++)) \
99                               if(!*ra++) \
100                                 return 0; \
101                             return 1;
102
103 #ifndef FORCEINLINE
104 extern NTL_HDR_strChattr;
105 extern NTL_HDR_strCasediff;
106
107 #else /* FORCEINLINE */
108 #ifdef __cplusplus
109 inline NTL_HDR_strChattr { NTL_SRC_strChattr }
110 inline NTL_HDR_strCasediff { NTL_SRC_strCasediff }
111 #else
112 static __inline__ NTL_HDR_strChattr { NTL_SRC_strChattr }
113 static __inline__ NTL_HDR_strCasediff { NTL_SRC_strCasediff }
114 #endif
115 #endif /* FORCEINLINE */
116
117 #endif /* INCLUDED_ircd_string_h */
118