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