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