blob: a31eae477de6e008b411d16e649b01ed8cd8bf60 [file] [log] [blame]
Daniel Veillard92ad2102001-03-27 12:47:33 +00001/*************************************************************************
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
18#ifndef H_STRIO
19#define H_STRIO
20
21#include <stdlib.h>
22#include <string.h>
23#include <time.h>
24#include "assert.h"
25
26/*
27 * StrAppend(target, source)
28 * StrAppendMax(target, maxsize, source)
29 *
30 * Append 'source' to 'target'
31 *
32 * target = StrAlloc(size)
33 *
34 * Allocate a new string
35 *
36 * StrContains(target, substring)
37 *
38 * Find out if the string 'substring' is
39 * contained in the string 'target'
40 *
41 * StrCopy(target, source)
42 * StrCopyMax(target, maxsize, source)
43 *
44 * Copy 'source' to 'target'
45 *
46 * target = StrDuplicate(source)
47 * target = StrDuplicateMax(source, maxsize)
48 *
49 * Allocate and copy 'source' to 'target'
50 *
51 * StrEqual(first, second)
52 * StrEqualMax(first, maxsize, second)
53 *
54 * Compare if 'first' is equal to 'second'.
55 * Case-independent.
56 *
57 * StrEqualCase(first, second)
58 * StrEqualCaseMax(first, maxsize, second)
59 *
60 * Compare if 'first' is equal to 'second'
61 * Case-dependent. Please note that the use of the
62 * word 'case' has the opposite meaning as that of
63 * strcasecmp().
64 *
65 * StrFormat(target, format, ...)
66 * StrFormatMax(target, maxsize, format, ...)
67 *
68 * Build 'target' according to 'format' and succesive
69 * arguments. This is equal to the sprintf() and
70 * snprintf() functions.
71 *
72 * StrFormatDate(target, format, ...)
73 *
74 * StrFree(target)
75 *
76 * De-allocates a string
77 *
78 * StrHash(string, type)
79 *
80 * Calculates the hash value of 'string' based on the
81 * 'type'.
82 *
83 * StrIndex(target, character)
84 * StrIndexLast(target, character)
85 *
86 * Find the first/last occurrence of 'character' in
87 * 'target'
88 *
89 * StrLength(target)
90 *
91 * Return the length of 'target'
92 *
93 * StrMatch(string, pattern)
94 * StrMatchCase(string, pattern)
95 *
96 * Find 'pattern' within 'string'. 'pattern' may contain
97 * wildcards such as * (asterics) and ? (question mark)
98 * which matches zero or more characters and exactly
99 * on character respectively
100 *
101 * StrScan(source, format, ...)
102 *
103 * Equal to sscanf()
104 *
105 * StrSubstring(target, substring)
106 *
107 * Find the first occurrence of the string 'substring'
108 * within the string 'target'
109 *
110 * StrTokenize(target, list)
111 *
112 * Split 'target' into the first token delimited by
113 * one of the characters in 'list'. If 'target' is
114 * NULL then next token will be returned.
115 *
116 * StrToUpper(target)
117 *
118 * Convert all lower case characters in 'target' into
119 * upper case characters.
120 */
121
122enum {
123 STRIO_HASH_NONE = 0,
124 STRIO_HASH_PLAIN,
125 STRIO_HASH_TWOSIGNED
126};
127
128#if !defined(DEBUG) || defined(__DECC)
129#define StrAppend(x,y) strcat((x), (y))
130#define StrAlloc(n) ((char *)calloc(1, (n)))
131#define StrContains(x,y) (0 != strstr((x), (y)))
132#define StrCopy(x,y) strcpy((x), (y))
133#define StrFree(x) free((x))
134#define StrIndex(x,y) strchr((x), (y))
135#define StrIndexLast(x,y) strrchr((x), (y))
136#define StrLength(x) strlen((x))
137#define StrSubstring(x,y) strstr((x), (y))
138#define StrTokenize(x,y) strtok((x), (y))
139#define StrToLong(x,y,n) strtol((x), (y), (n))
140#define StrToUnsignedLong(x,y,n) strtoul((x), (y), (n))
141#else /* DEBUG */
142 /*
143 * To be able to use these macros everywhere, including in
144 * if() sentences, the assertions are put first in a comma
145 * seperated list.
146 *
147 * Unfortunately the DECC compiler does not seem to like this
148 * so it will use the un-asserted functions above for the
149 * debugging case too.
150 */
151#define StrAppend(x,y) \
152 (assert((x) != NULL),\
153 assert((y) != NULL),\
154 strcat((x), (y)))
155#define StrAlloc(n) \
156 (assert((n) > 0),\
157 ((char *)calloc(1, (n))))
158#define StrContains(x,y) \
159 (assert((x) != NULL),\
160 assert((y) != NULL),\
161 (0 != strstr((x), (y))))
162#define StrCopy(x,y) \
163 (assert((x) != NULL),\
164 assert((y) != NULL),\
165 strcpy((x), (y)))
166#define StrIndex(x,c) \
167 (assert((x) != NULL),\
168 strchr((x), (c)))
169#define StrIndexLast(x,c) \
170 (assert((x) != NULL),\
171 strrchr((x), (c)))
172#define StrFree(x) \
173 (assert((x) != NULL),\
174 free((x)))
175#define StrLength(x) \
176 (assert((x) != NULL),\
177 strlen((x)))
178#define StrSubstring(x,y) \
179 (assert((x) != NULL),\
180 assert((y) != NULL),\
181 strstr((x), (y)))
182#define StrTokenize(x,y) \
183 (assert((y) != NULL),\
184 strtok((x), (y)))
185#define StrToLong(x,y,n) \
186 (assert((x) != NULL),\
187 assert((y) != NULL),\
188 assert((n) >= 2 && (n) <= 36),\
189 strtol((x), (y), (n)))
190#define StrToUnsignedLong(x,y,n) \
191 (assert((x) != NULL),\
192 assert((y) != NULL),\
193 assert((n) >= 2 && (n) <= 36),\
194 strtoul((x), (y), (n)))
195#endif /* DEBUG */
196
197char *StrAppendMax(char *target, size_t max, const char *source);
198char *StrCopyMax(char *target, size_t max, const char *source);
199char *StrDuplicate(const char *source);
200char *StrDuplicateMax(const char *source, size_t max);
201int StrEqual(const char *first, const char *second);
202int StrEqualCase(const char *first, const char *second);
203int StrEqualCaseMax(const char *first, size_t max, const char *second);
204int StrEqualMax(const char *first, size_t max, const char *second);
205const char *StrError(int);
206size_t StrFormatDateMax(char *target, size_t max, const char *format, const struct tm *datetime);
207unsigned long StrHash(const char *string, int type);
208int StrMatch(char *string, char *pattern);
209int StrMatchCase(char *string, char *pattern);
210size_t StrSpanFunction(char *source, int (*Function)(int));
211char *StrSubstringMax(const char *string, size_t max, const char *find);
212float StrToFloat(const char *source, const char **target);
213double StrToDouble(const char *source, const char **target);
214int StrToUpper(char *target);
215
216#endif /* H_STRIO */