summaryrefslogtreecommitdiff
path: root/strio.h
blob: 68845a38dd3a54ac68a240b71641ce3504d4c304 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*************************************************************************
 *
 * $Id$
 *
 * Copyright (C) 1998 Bjorn Reese and Daniel Stenberg.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
 * CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
 *
 ************************************************************************/

#ifndef TRIO_STRIO_H
#define TRIO_STRIO_H

#if !(defined(DEBUG) || defined(NDEBUG))
# define NDEBUG
#endif
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#ifndef STRIO_MALLOC
# define STRIO_MALLOC(n) malloc(n)
#endif
#ifndef STRIO_FREE
# define STRIO_FREE(x) free(x)
#endif

/*
 * StrAppend(target, source)
 * StrAppendMax(target, maxsize, source)
 *
 *   Append 'source' to 'target'
 *
 * target = StrAlloc(size)
 *
 *   Allocate a new string
 *
 * StrContains(target, substring)
 *
 *   Find out if the string 'substring' is
 *   contained in the string 'target'
 *
 * StrCopy(target, source)
 * StrCopyMax(target, maxsize, source)
 *
 *   Copy 'source' to 'target'
 *
 * target = StrDuplicate(source)
 * target = StrDuplicateMax(source, maxsize)
 *
 *   Allocate and copy 'source' to 'target'
 *
 * StrEqual(first, second)
 * StrEqualMax(first, maxsize, second)
 *
 *   Compare if 'first' is equal to 'second'.
 *   Case-independent.
 *
 * StrEqualCase(first, second)
 * StrEqualCaseMax(first, maxsize, second)
 *
 *   Compare if 'first' is equal to 'second'
 *   Case-dependent. Please note that the use of the
 *   word 'case' has the opposite meaning as that of
 *   strcasecmp().
 *
 * StrFormat(target, format, ...)
 * StrFormatMax(target, maxsize, format, ...)
 *
 *   Build 'target' according to 'format' and succesive
 *   arguments. This is equal to the sprintf() and
 *   snprintf() functions.
 *
 * StrFormatDate(target, format, ...)
 *
 * StrFree(target)
 *
 *   De-allocates a string
 *
 * StrHash(string, type)
 *
 *   Calculates the hash value of 'string' based on the
 *   'type'.
 *
 * StrIndex(target, character)
 * StrIndexLast(target, character)
 *
 *   Find the first/last occurrence of 'character' in
 *   'target'
 *
 * StrLength(target)
 *
 *   Return the length of 'target'
 *
 * StrMatch(string, pattern)
 * StrMatchCase(string, pattern)
 *
 *   Find 'pattern' within 'string'. 'pattern' may contain
 *   wildcards such as * (asterics) and ? (question mark)
 *   which matches zero or more characters and exactly
 *   on character respectively
 *
 * StrScan(source, format, ...)
 *
 *   Equal to sscanf()
 *
 * StrSubstring(target, substring)
 *
 *   Find the first occurrence of the string 'substring'
 *   within the string 'target'
 *
 * StrTokenize(target, list)
 *
 *   Split 'target' into the first token delimited by
 *   one of the characters in 'list'. If 'target' is
 *   NULL then next token will be returned.
 *
 * StrToUpper(target)
 *
 *   Convert all lower case characters in 'target' into
 *   upper case characters.
 */

enum {
  STRIO_HASH_NONE = 0,
  STRIO_HASH_PLAIN,
  STRIO_HASH_TWOSIGNED
};

#if !defined(DEBUG) || defined(__DECC)
#define StrAlloc(n) (char *)STRIO_MALLOC(n)
#define StrAppend(x,y) strcat((x), (y))
#define StrContains(x,y) (0 != strstr((x), (y)))
#define StrCopy(x,y) strcpy((x), (y))
#define StrIndex(x,y) strchr((x), (y))
#define StrIndexLast(x,y) strrchr((x), (y))
#define StrFree(x) STRIO_FREE(x)
#define StrLength(x) strlen((x))
#define StrSubstring(x,y) strstr((x), (y))
#define StrTokenize(x,y) strtok((x), (y))
#define StrToLong(x,y,n) strtol((x), (y), (n))
#define StrToUnsignedLong(x,y,n) strtoul((x), (y), (n))
#else /* DEBUG */
 /*
  * To be able to use these macros everywhere, including in
  * if() sentences, the assertions are put first in a comma
  * seperated list.
  *
  * Unfortunately the DECC compiler does not seem to like this
  * so it will use the un-asserted functions above for the
  * debugging case too.
  */
#define StrAlloc(n) \
     (assert((n) > 0),\
      (char *)STRIO_MALLOC(n))
#define StrAppend(x,y) \
     (assert((x) != NULL),\
      assert((y) != NULL),\
      strcat((x), (y)))
#define StrContains(x,y) \
     (assert((x) != NULL),\
      assert((y) != NULL),\
      (0 != strstr((x), (y))))
#define StrCopy(x,y) \
     (assert((x) != NULL),\
      assert((y) != NULL),\
      strcpy((x), (y)))
#define StrIndex(x,c) \
     (assert((x) != NULL),\
      strchr((x), (c)))
#define StrIndexLast(x,c) \
     (assert((x) != NULL),\
      strrchr((x), (c)))
#define StrFree(x) \
     (assert((x) != NULL),\
      STRIO_FREE(x))
#define StrLength(x) \
     (assert((x) != NULL),\
      strlen((x)))
#define StrSubstring(x,y) \
     (assert((x) != NULL),\
      assert((y) != NULL),\
      strstr((x), (y)))
#define StrTokenize(x,y) \
     (assert((y) != NULL),\
      strtok((x), (y)))
#define StrToLong(x,y,n) \
     (assert((x) != NULL),\
      assert((y) != NULL),\
      assert((n) >= 2 && (n) <= 36),\
      strtol((x), (y), (n)))
#define StrToUnsignedLong(x,y,n) \
     (assert((x) != NULL),\
      assert((y) != NULL),\
      assert((n) >= 2 && (n) <= 36),\
      strtoul((x), (y), (n)))
#endif /* DEBUG */

char *StrAppendMax(char *target, size_t max, const char *source);
char *StrCopyMax(char *target, size_t max, const char *source);
char *StrDuplicate(const char *source);
char *StrDuplicateMax(const char *source, size_t max);
int StrEqual(const char *first, const char *second);
int StrEqualCase(const char *first, const char *second);
int StrEqualCaseMax(const char *first, size_t max, const char *second);
int StrEqualLocale(const char *first, const char *second);
int StrEqualMax(const char *first, size_t max, const char *second);
const char *StrError(int);
size_t StrFormatDateMax(char *target, size_t max, const char *format, const struct tm *datetime);
unsigned long StrHash(const char *string, int type);
int StrMatch(char *string, char *pattern);
int StrMatchCase(char *string, char *pattern);
size_t StrSpanFunction(char *source, int (*Function)(int));
char *StrSubstringMax(const char *string, size_t max, const char *find);
float StrToFloat(const char *source, const char **target);
double StrToDouble(const char *source, const char **target);
int StrToUpper(char *target);

#endif /* TRIO_STRIO_H */