Daniel Veillard | 92ad210 | 2001-03-27 12:47:33 +0000 | [diff] [blame] | 1 | /************************************************************************* |
| 2 | * |
| 3 | * $Id$ |
| 4 | * |
| 5 | * Copyright (C) 1998 Bjorn Reese and Daniel Stenberg. |
| 6 | * |
| 7 | * Permission to use, copy, modify, and distribute this software for any |
| 8 | * purpose with or without fee is hereby granted, provided that the above |
| 9 | * copyright notice and this permission notice appear in all copies. |
| 10 | * |
| 11 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED |
| 12 | * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF |
| 13 | * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND |
| 14 | * CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER. |
| 15 | * |
| 16 | ************************************************************************/ |
| 17 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 18 | #ifndef TRIO_STRIO_H |
| 19 | #define TRIO_STRIO_H |
Daniel Veillard | 92ad210 | 2001-03-27 12:47:33 +0000 | [diff] [blame] | 20 | |
Bjorn Reese | 906ec8a | 2001-06-05 12:46:33 +0000 | [diff] [blame] | 21 | #if !(defined(DEBUG) || defined(NDEBUG)) |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 22 | # define NDEBUG |
| 23 | #endif |
| 24 | #include <assert.h> |
Bjorn Reese | 906ec8a | 2001-06-05 12:46:33 +0000 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <time.h> |
| 28 | |
| 29 | #ifndef STRIO_MALLOC |
| 30 | # define STRIO_MALLOC(n) malloc(n) |
| 31 | #endif |
| 32 | #ifndef STRIO_FREE |
| 33 | # define STRIO_FREE(x) free(x) |
| 34 | #endif |
Daniel Veillard | 92ad210 | 2001-03-27 12:47:33 +0000 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * StrAppend(target, source) |
| 38 | * StrAppendMax(target, maxsize, source) |
| 39 | * |
| 40 | * Append 'source' to 'target' |
| 41 | * |
| 42 | * target = StrAlloc(size) |
| 43 | * |
| 44 | * Allocate a new string |
| 45 | * |
| 46 | * StrContains(target, substring) |
| 47 | * |
| 48 | * Find out if the string 'substring' is |
| 49 | * contained in the string 'target' |
| 50 | * |
| 51 | * StrCopy(target, source) |
| 52 | * StrCopyMax(target, maxsize, source) |
| 53 | * |
| 54 | * Copy 'source' to 'target' |
| 55 | * |
| 56 | * target = StrDuplicate(source) |
| 57 | * target = StrDuplicateMax(source, maxsize) |
| 58 | * |
| 59 | * Allocate and copy 'source' to 'target' |
| 60 | * |
| 61 | * StrEqual(first, second) |
| 62 | * StrEqualMax(first, maxsize, second) |
| 63 | * |
| 64 | * Compare if 'first' is equal to 'second'. |
| 65 | * Case-independent. |
| 66 | * |
| 67 | * StrEqualCase(first, second) |
| 68 | * StrEqualCaseMax(first, maxsize, second) |
| 69 | * |
| 70 | * Compare if 'first' is equal to 'second' |
| 71 | * Case-dependent. Please note that the use of the |
| 72 | * word 'case' has the opposite meaning as that of |
| 73 | * strcasecmp(). |
| 74 | * |
| 75 | * StrFormat(target, format, ...) |
| 76 | * StrFormatMax(target, maxsize, format, ...) |
| 77 | * |
| 78 | * Build 'target' according to 'format' and succesive |
| 79 | * arguments. This is equal to the sprintf() and |
| 80 | * snprintf() functions. |
| 81 | * |
| 82 | * StrFormatDate(target, format, ...) |
| 83 | * |
| 84 | * StrFree(target) |
| 85 | * |
| 86 | * De-allocates a string |
| 87 | * |
| 88 | * StrHash(string, type) |
| 89 | * |
| 90 | * Calculates the hash value of 'string' based on the |
| 91 | * 'type'. |
| 92 | * |
| 93 | * StrIndex(target, character) |
| 94 | * StrIndexLast(target, character) |
| 95 | * |
| 96 | * Find the first/last occurrence of 'character' in |
| 97 | * 'target' |
| 98 | * |
| 99 | * StrLength(target) |
| 100 | * |
| 101 | * Return the length of 'target' |
| 102 | * |
| 103 | * StrMatch(string, pattern) |
| 104 | * StrMatchCase(string, pattern) |
| 105 | * |
| 106 | * Find 'pattern' within 'string'. 'pattern' may contain |
| 107 | * wildcards such as * (asterics) and ? (question mark) |
| 108 | * which matches zero or more characters and exactly |
| 109 | * on character respectively |
| 110 | * |
| 111 | * StrScan(source, format, ...) |
| 112 | * |
| 113 | * Equal to sscanf() |
| 114 | * |
| 115 | * StrSubstring(target, substring) |
| 116 | * |
| 117 | * Find the first occurrence of the string 'substring' |
| 118 | * within the string 'target' |
| 119 | * |
| 120 | * StrTokenize(target, list) |
| 121 | * |
| 122 | * Split 'target' into the first token delimited by |
| 123 | * one of the characters in 'list'. If 'target' is |
| 124 | * NULL then next token will be returned. |
| 125 | * |
| 126 | * StrToUpper(target) |
| 127 | * |
| 128 | * Convert all lower case characters in 'target' into |
| 129 | * upper case characters. |
| 130 | */ |
| 131 | |
| 132 | enum { |
| 133 | STRIO_HASH_NONE = 0, |
| 134 | STRIO_HASH_PLAIN, |
Daniel Veillard | 5792e16 | 2001-04-30 17:44:45 +0000 | [diff] [blame] | 135 | STRIO_HASH_TWOSIGNED |
Daniel Veillard | 92ad210 | 2001-03-27 12:47:33 +0000 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | #if !defined(DEBUG) || defined(__DECC) |
Bjorn Reese | 906ec8a | 2001-06-05 12:46:33 +0000 | [diff] [blame] | 139 | #define StrAlloc(n) (char *)STRIO_MALLOC(n) |
Daniel Veillard | 92ad210 | 2001-03-27 12:47:33 +0000 | [diff] [blame] | 140 | #define StrAppend(x,y) strcat((x), (y)) |
Daniel Veillard | 92ad210 | 2001-03-27 12:47:33 +0000 | [diff] [blame] | 141 | #define StrContains(x,y) (0 != strstr((x), (y))) |
| 142 | #define StrCopy(x,y) strcpy((x), (y)) |
Daniel Veillard | 92ad210 | 2001-03-27 12:47:33 +0000 | [diff] [blame] | 143 | #define StrIndex(x,y) strchr((x), (y)) |
| 144 | #define StrIndexLast(x,y) strrchr((x), (y)) |
Bjorn Reese | 906ec8a | 2001-06-05 12:46:33 +0000 | [diff] [blame] | 145 | #define StrFree(x) STRIO_FREE(x) |
Daniel Veillard | 92ad210 | 2001-03-27 12:47:33 +0000 | [diff] [blame] | 146 | #define StrLength(x) strlen((x)) |
| 147 | #define StrSubstring(x,y) strstr((x), (y)) |
| 148 | #define StrTokenize(x,y) strtok((x), (y)) |
| 149 | #define StrToLong(x,y,n) strtol((x), (y), (n)) |
| 150 | #define StrToUnsignedLong(x,y,n) strtoul((x), (y), (n)) |
| 151 | #else /* DEBUG */ |
| 152 | /* |
| 153 | * To be able to use these macros everywhere, including in |
| 154 | * if() sentences, the assertions are put first in a comma |
| 155 | * seperated list. |
| 156 | * |
| 157 | * Unfortunately the DECC compiler does not seem to like this |
| 158 | * so it will use the un-asserted functions above for the |
| 159 | * debugging case too. |
| 160 | */ |
Bjorn Reese | 906ec8a | 2001-06-05 12:46:33 +0000 | [diff] [blame] | 161 | #define StrAlloc(n) \ |
| 162 | (assert((n) > 0),\ |
| 163 | (char *)STRIO_MALLOC(n)) |
Daniel Veillard | 92ad210 | 2001-03-27 12:47:33 +0000 | [diff] [blame] | 164 | #define StrAppend(x,y) \ |
| 165 | (assert((x) != NULL),\ |
| 166 | assert((y) != NULL),\ |
| 167 | strcat((x), (y))) |
Daniel Veillard | 92ad210 | 2001-03-27 12:47:33 +0000 | [diff] [blame] | 168 | #define StrContains(x,y) \ |
| 169 | (assert((x) != NULL),\ |
| 170 | assert((y) != NULL),\ |
| 171 | (0 != strstr((x), (y)))) |
| 172 | #define StrCopy(x,y) \ |
| 173 | (assert((x) != NULL),\ |
| 174 | assert((y) != NULL),\ |
| 175 | strcpy((x), (y))) |
| 176 | #define StrIndex(x,c) \ |
| 177 | (assert((x) != NULL),\ |
| 178 | strchr((x), (c))) |
| 179 | #define StrIndexLast(x,c) \ |
| 180 | (assert((x) != NULL),\ |
| 181 | strrchr((x), (c))) |
| 182 | #define StrFree(x) \ |
| 183 | (assert((x) != NULL),\ |
Bjorn Reese | 906ec8a | 2001-06-05 12:46:33 +0000 | [diff] [blame] | 184 | STRIO_FREE(x)) |
Daniel Veillard | 92ad210 | 2001-03-27 12:47:33 +0000 | [diff] [blame] | 185 | #define StrLength(x) \ |
| 186 | (assert((x) != NULL),\ |
| 187 | strlen((x))) |
| 188 | #define StrSubstring(x,y) \ |
| 189 | (assert((x) != NULL),\ |
| 190 | assert((y) != NULL),\ |
| 191 | strstr((x), (y))) |
| 192 | #define StrTokenize(x,y) \ |
| 193 | (assert((y) != NULL),\ |
| 194 | strtok((x), (y))) |
| 195 | #define StrToLong(x,y,n) \ |
| 196 | (assert((x) != NULL),\ |
| 197 | assert((y) != NULL),\ |
| 198 | assert((n) >= 2 && (n) <= 36),\ |
| 199 | strtol((x), (y), (n))) |
| 200 | #define StrToUnsignedLong(x,y,n) \ |
| 201 | (assert((x) != NULL),\ |
| 202 | assert((y) != NULL),\ |
| 203 | assert((n) >= 2 && (n) <= 36),\ |
| 204 | strtoul((x), (y), (n))) |
| 205 | #endif /* DEBUG */ |
| 206 | |
| 207 | char *StrAppendMax(char *target, size_t max, const char *source); |
| 208 | char *StrCopyMax(char *target, size_t max, const char *source); |
| 209 | char *StrDuplicate(const char *source); |
| 210 | char *StrDuplicateMax(const char *source, size_t max); |
| 211 | int StrEqual(const char *first, const char *second); |
| 212 | int StrEqualCase(const char *first, const char *second); |
| 213 | int StrEqualCaseMax(const char *first, size_t max, const char *second); |
Bjorn Reese | 906ec8a | 2001-06-05 12:46:33 +0000 | [diff] [blame] | 214 | int StrEqualLocale(const char *first, const char *second); |
Daniel Veillard | 92ad210 | 2001-03-27 12:47:33 +0000 | [diff] [blame] | 215 | int StrEqualMax(const char *first, size_t max, const char *second); |
| 216 | const char *StrError(int); |
| 217 | size_t StrFormatDateMax(char *target, size_t max, const char *format, const struct tm *datetime); |
| 218 | unsigned long StrHash(const char *string, int type); |
| 219 | int StrMatch(char *string, char *pattern); |
| 220 | int StrMatchCase(char *string, char *pattern); |
| 221 | size_t StrSpanFunction(char *source, int (*Function)(int)); |
| 222 | char *StrSubstringMax(const char *string, size_t max, const char *find); |
| 223 | float StrToFloat(const char *source, const char **target); |
| 224 | double StrToDouble(const char *source, const char **target); |
| 225 | int StrToUpper(char *target); |
| 226 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 227 | #endif /* TRIO_STRIO_H */ |