Author: Bleep <tomh@inxpress.net>
[ircu2.10.12-pk.git] / include / common.h
1 /*
2  * IRC - Internet Relay Chat, include/common.h
3  * Copyright (C) 1998 Andrea Cocito
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 1, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20  /*
21     All the code in common.h/common.c is taken from the NTL
22     (Nemesi's Tools Library), adapted for ircu's character set 
23     and thereafter released under GNU GPL, from there comes the
24     NTL_ prefix of all macro and object names.
25     Removed isXdigit() to leave space to other char sets in the
26     bitmap, should give the same results as isxdigit() on any
27     implementation and isn't used in IRC anyway.
28   */
29
30 #ifndef COMMON_H
31 #define COMMON_H
32
33 /*=============================================================================
34  * System's headers needed in this header file
35  */
36
37 #include "sys.h"
38 #include <limits.h>
39
40 /*=============================================================================
41  * Macros and constants for internal use, 
42  * WARNING: match.c depends on these macros, don't change them
43  *          without looking at that part of the code too !
44  */
45
46 #define NTL_ALNUM 0x0001        /*    (NTL_ALPHA|NTL_DIGIT)               */
47 #define NTL_ALPHA 0x0002        /*    (NTL_LOWER|NTL_UPPER)               */
48 #define NTL_CNTRL 0x0004        /*    \000 - \037 == 0x00 - 0x1F          */
49 #define NTL_DIGIT 0x0008        /*    0123456789                          */
50 #define NTL_GRAPH 0x0010        /*    (NTL_ALNUM|NTL_PUNCT)               */
51 #define NTL_LOWER 0x0020        /*    abcdefghijklmnopqrstuvwxyz{|}~      */
52 #define NTL_PRINT 0x0040        /*    (NTL_GRAPH|' ')                     */
53 #define NTL_PUNCT 0x0080        /*    !"#$%&'()*+,-./:;<=>?@_`            */
54 #define NTL_SPACE 0x0100        /*    \011\012\013\014\015\040            */
55 #define NTL_UPPER 0x0200        /*    ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^      */
56 #define NTL_IRCCH 0x0400        /*    Channel's names charset             */
57 #define NTL_IRCCL 0x0800        /*    Force toLower() in ch-name          */
58 #define NTL_IRCNK 0x1000        /*    Nick names charset, aka isvalid()   */
59 #define NTL_IRCUI 0x2000        /*    UserIDs charset, IRCHN plus tilde   */
60 #define NTL_IRCHN 0x4000        /*    Hostnames charset (weak, RFC 1033)  */
61 #define NTL_IRCIP 0x8000        /*    Numeric IPs charset (DIGIT and .)   */
62 #define NTL_EOL  0x10000        /*    \r\n                                */
63
64 /*=============================================================================
65  * Structures
66  */
67
68 /*=============================================================================
69  * Externally visible function-like macros
70  */
71
72 #define DupString(x, y)   (strcpy((x = (char *)RunMalloc(strlen(y) + 1)), y))
73
74 #define toLower(c)        (NTL_tolower_tab[(c)-CHAR_MIN])
75 #define toUpper(c)        (NTL_toupper_tab[(c)-CHAR_MIN])
76
77 /* Char classification pseudo-functions, when others are needed add them */
78 #define isAlnum(c)        (NTL_char_attrib[(c)-CHAR_MIN] & NTL_ALNUM)
79 #define isAlpha(c)        (NTL_char_attrib[(c)-CHAR_MIN] & NTL_ALPHA)
80 #define isDigit(c)        (NTL_char_attrib[(c)-CHAR_MIN] & NTL_DIGIT)
81 #define isLower(c)        (NTL_char_attrib[(c)-CHAR_MIN] & NTL_LOWER)
82 #define isSpace(c)        (NTL_char_attrib[(c)-CHAR_MIN] & NTL_SPACE)
83 #define isUpper(c)        (NTL_char_attrib[(c)-CHAR_MIN] & NTL_UPPER)
84 #define isCntrl(c)        (NTL_char_attrib[(c)-CHAR_MIN] & NTL_CNTRL)
85
86 #define isIrcCh(c)        (NTL_char_attrib[(c)-CHAR_MIN] & NTL_IRCCH)
87 #define isIrcCl(c)        (NTL_char_attrib[(c)-CHAR_MIN] & NTL_IRCCL)
88 #define isIrcNk(c)        (NTL_char_attrib[(c)-CHAR_MIN] & NTL_IRCNK)
89 #define isIrcUi(c)        (NTL_char_attrib[(c)-CHAR_MIN] & NTL_IRCUI)
90 #define isIrcHn(c)        (NTL_char_attrib[(c)-CHAR_MIN] & NTL_IRCHN)
91 #define isIrcIp(c)        (NTL_char_attrib[(c)-CHAR_MIN] & NTL_IRCIP)
92 #define isEol(c)          (NTL_char_attrib[(c)-CHAR_MIN] & NTL_EOL)
93
94 /* String classification pseudo-functions, when others are needed add them,
95    strIsXxxxx(s) is true when IsXxxxx(c) is true for every char in s */
96
97 #define strIsAlnum(s)     (strChattr(s) & NTL_ALNUM)
98 #define strIsAlpha(s)     (strChattr(s) & NTL_ALPHA)
99 #define strIsDigit(s)     (strChattr(s) & NTL_DIGIT)
100 #define strIsLower(s)     (strChattr(s) & NTL_LOWER)
101 #define strIsSpace(s)     (strChattr(s) & NTL_SPACE)
102 #define strIsUpper(s)     (strChattr(s) & NTL_UPPER)
103
104 #define strIsIrcCh(s)     (strChattr(s) & NTL_IRCCH)
105 #define strIsIrcCl(s)     (strChattr(s) & NTL_IRCCL)
106 #define strIsIrcNk(s)     (strChattr(s) & NTL_IRCNK)
107 #define strIsIrcUi(s)     (strChattr(s) & NTL_IRCUI)
108 #define strIsIrcHn(s)     (strChattr(s) & NTL_IRCHN)
109 #define strIsIrcIp(s)     (strChattr(s) & NTL_IRCIP)
110
111 /*=============================================================================
112  * Externally visible static memory stuff
113  */
114 #ifdef MAKETABLES
115 extern char NTL_tolower_tab[];  /* 256 bytes */
116 extern char NTL_toupper_tab[];  /* 256 bytes */
117 extern unsigned int NTL_char_attrib[];  /* 256 ints = 0.5 to 2 kilobytes */
118 #else
119 extern const char NTL_tolower_tab[];    /* 256 bytes */
120 extern const char NTL_toupper_tab[];    /* 256 bytes */
121 extern const unsigned int NTL_char_attrib[];    /* 256 ints = 0.5 to 2 kilobytes */
122 #endif
123
124 /*=============================================================================
125  * Critical small functions to inline even in separate compilation
126  * when FORCEINLINE is defined (provided you have a compiler that supports
127  * `inline').
128  */
129
130 #define NTL_HDR_strChattr   int strChattr(const char *s)
131
132 #define NTL_SRC_strChattr   register const char *rs = s; \
133                             register int x = ~0; \
134                             while(*rs) \
135                               x &= NTL_char_attrib[*rs++ - CHAR_MIN]; \
136                             return x;
137
138 #define NTL_HDR_strCasediff int strCasediff(const char *a, const char *b)
139
140 #define NTL_SRC_strCasediff register const char *ra = a; \
141                             register const char *rb = b; \
142                             while(toLower(*ra) == toLower(*rb++)) \
143                               if(!*ra++) \
144                                 return 0; \
145                             return 1;
146
147 #ifndef FORCEINLINE
148 extern NTL_HDR_strChattr;
149 extern NTL_HDR_strCasediff;
150 #else /* FORCEINLINE */
151 /* *INDENT-OFF* */
152 #ifdef __cplusplus
153 inline NTL_HDR_strChattr { NTL_SRC_strChattr }
154 inline NTL_HDR_strCasediff { NTL_SRC_strCasediff }
155 #else
156 static __inline__ NTL_HDR_strChattr { NTL_SRC_strChattr }
157 static __inline__ NTL_HDR_strCasediff { NTL_SRC_strCasediff }
158 #endif
159 /* *INDENT-ON* */
160 #endif /* FORCEINLINE */
161
162 /*=============================================================================
163  * Proto types of other externally visible functions
164  */
165
166 extern int strnChattr(const char *s, const size_t n);
167 extern int strCasecmp(const char *a, const char *b);
168 extern int strnCasecmp(const char *a, const char *b, const size_t n);
169
170 #endif /* COMMON_H */