forked from vonj/snippets.org
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbastrngs.c
209 lines (172 loc) · 4.41 KB
/
bastrngs.c
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
/*
** BASIC-like string operations
**
** public domain by Bob Stout
*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <limits.h>
#include <assert.h>
#include "sniptype.h"
#include "bastrngs.h"
static int stralloc_ptr;
static char *strings[8];
static int str_tag[8];
/*
** stralloc() is the key function in this package, maintaining a pool of
** reusable strings.
*/
char *stralloc(size_t length)
{
register int i;
if (UINT_MAX == length) /* Assume size_t == unsigned int */
return NULL;
i = stralloc_ptr++;
++length; /* Allow for terminating NUL */
if ((!strings[i]) || (length > strlen(strings[i])))
{
strings[i] = (char *)realloc(strings[i], length);
assert(NULL != strings[i]);
str_tag[i] = -1;
}
else str_tag[i] = 0;
stralloc_ptr &= 7;
return (strings[i]);
/* Maintains 8 strings in a circular buffer */
}
/*
** free the string pool.
*/
void str_free(char *string)
{
register int i;
for (i = 0; i < 8; ++i)
{
if (strings[i] == string)
{
if (str_tag[i])
free(strings[i]);
return;
}
}
}
/*
** return the leftmost N characters from a string, equivalent to LEFT$
*/
char *left(char *string, size_t N)
{
char *buf;
size_t strlength = strlen(string);
if (N > strlength)
N = strlength;
buf = stralloc(N);
memcpy(buf, string, N);
buf[N] = NUL;
return buf;
}
/*
** return the rightmost N characters from a string, equivalent to RIGHT$
*/
char *right(char *string, size_t N)
{
char *buf;
size_t strlength = strlen(string);
if (N > strlength)
N = strlength;
buf = stralloc(N);
strcpy(buf, &string[strlength-N]);
return buf;
}
/*
** return a substring, N characters long beginning at position M,
** equivalent to MID$
*/
char *mid(char *string, size_t M, size_t N)
{
char *buf;
size_t strlength = strlen(string);
if (M > strlength)
return NULL;
if (N > (strlength - M))
N = strlength - M;
buf = stralloc(N);
memcpy(buf, &string[M-1], N);
buf[N] = NUL;
return buf;
}
/*
** string concatenation function, equivalent to A$=B$+C$+...
*/
char *string_add(char *string, ...)
{
va_list arg_ptr;
char *temp1, *temp2, *buf;
va_start(arg_ptr, string);
temp1 = string;
do
{
if(NULL == (temp2 = va_arg(arg_ptr, char *)))
break;
buf = stralloc(strlen(temp1) + strlen(temp2));
temp1 = strcat(strcpy(buf, temp1), temp2);
} while (NULL != temp2);
return temp1;
}
/*
** create a string of repeated characters, equivalent to STRING$()
*/
char *string(int ch, size_t N)
{
char *buf;
if (N)
{
buf = stralloc(N);
memset(buf, ch, N);
}
buf[N] = NUL;
return buf;
}
/*
** BASIC INSTR$() work alike - returns position (1-based) of findstr in
** string, starting search at position start_pos (also 1-based).
**
** Function suggested by Tika Carr
*/
unsigned int instr(const unsigned int start_pos,
const char *string,
const char *findstr)
{
char *p;
unsigned int pos;
if (start_pos > strlen(string))
return 0;
else pos = start_pos - 1; /* BASIC offsets are one-based, not
zero-based as in C */
if (NULL != (p = strstr(string + pos, findstr)))
return (int)(p - (char *)string) + 1; /* Position 0 = position 1 */
else return 0;
}
#ifdef TEST
/*
** Demo main()
*/
main()
{
char *x = "European", *y = "Hardware", *z = "Skaters", *q;
char *a = "This is a test", *b = "is" ,
*c = "\nSearching for \"%s\" in \"%s\" starting at position %d\n";
unsigned int i, pos;
z = string_add(left(x, 2), right(y, 2), mid(z, 2, 2), "!", NULL);
q = string('!', 4);
printf("%s%s\n", z, q);
for (i = 2; i < strlen(a); i+= 2)
{
printf(c, b, a, i);
if (0 != (pos = instr(i, a, b)))
printf("Found at position %d\n", pos);
else puts("Not found");
}
return 0;
}
#endif /* TEST */