File: string.c

package info (click to toggle)
libapache-csacek 2.1.9-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,500 kB
  • ctags: 1,773
  • sloc: ansic: 11,833; makefile: 454; yacc: 199; sh: 164; php: 51; sed: 5
file content (271 lines) | stat: -rw-r--r-- 7,893 bytes parent folder | download | duplicates (3)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#line 2 "string.c"
/*-
 * C-SaCzech
 * Copyright (c) 1996-2002 Jaromir Dolecek <dolecek@ics.muni.cz>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Jaromir Dolecek
 *	for the CSacek project.
 * 4. The name of Jaromir Dolecek may not be used to endorse or promote
 *    products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY JAROMIR DOLECEK ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL JAROMIR DOLECEK BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/* $Id: string.c,v 1.55 2002/02/03 11:13:41 dolecek Exp $ */

/* CSacek functions which work with strings */

#include "csacek.h"

/* 
 * tries to find ``search'' in ``str''; the comparison is case insensitive
 * note this routine works for ASCII only
 */                   
const char *
csa_strcasestr( str, search )
  const char *str, *search;
{
	const char *i,*j;

	if (!*search) return str;

   	for(; *str; str++) {
	   if (CSA_UPPER(*str) == CSA_UPPER(*search)) {
		i = str+1;
		j = search+1;
		for(; *i && *j && CSA_UPPER(*i) == CSA_UPPER(*j); i++, j++);
		if (*j == '\0') return str;
	   }
	}

	return NULL;
}

/*
 * fills csa_String with values given; if ``len'' is lower than 0,
 * strlen(val) is supstutited instead of it; if ``maxlen'' is lower
 * than zero, ``len'' is subtituted instead of it
 */
void
csa_fillstring(str, val, len, maxlen)
  csa_String *str;
  const char   *val;
  int len, maxlen;
{
	str->value = val;
	str->len = (len >= 0) ? len : strlen(val);
	str->maxlen = (maxlen >= 0) ? maxlen : str->len;
}

/* creates & fills in new csa_String struct */
csa_String *
csa_createstring(wpool, val)
  struct pool *wpool;
  const char *val;
{
	csa_String *newstr;
	newstr = (csa_String *) ap_palloc(wpool, sizeof(csa_String));
	csa_fillstring(newstr, val, -1, -1);
	return newstr;
}

/*
 * finds out, if ``str'' ends with one of items in ``list'';
 * items in ``list'' are separated by ``sep''
 * returns pointer at suffix found in  string ``str'' or NULL if no suffix
 * was found 
 */
const char * 
csa_has_suffix(str, list, sep_int)
  const char *str, *list;
  int sep_int;
{
   const char *b,  *suffix_pos=NULL;
   char sep=(char)sep_int;
   int str_len = 0, len = 0;

   str_len = strlen(str);
   while( *list )
   {
      b = strchr(list, sep);
      if (b == NULL) b = strchr(list,0); /* last item */
      len = b - list;
      if ( len <= str_len && len > 0 
		/* LINTED */
		&& strncmp(list, &str[str_len-len], len) == 0 )
      {
	suffix_pos = &str[str_len-len];
	break;
      }
      if (*b) list = ++b; /* shift list behind separator */
      else list = b;
   }

   return suffix_pos;
}                                             

/*
 * this finds first __FOO__ string and returns position where it was found,
 * position of text behind it and csa_String structure with what it
 * should be substituted by
 * return length of found item, 0 if nothing has been found
 */
size_t
csa_find_subs(p, str, len, idxp, subs)
  csa_params_t *p;
  const char *str;
  size_t len, *idxp;
  const csa_String **subs;
{
	const char *temp, *newtemp;
	size_t add_len=0, remains=len;

	temp = str;
	for(; remains > 0;) {
		newtemp = memchr(temp, '_', remains);
		if (!newtemp) break; /* stop if '_' not found */

		remains -= (newtemp - temp);
		/* if there is no chance anything would match (the remaining
		 * part is too short), just stop right away */
		if (remains < 8) break;
			
		temp = newtemp;
		if (temp[1] != '_') {
			temp++, remains--;
			continue;
		}
	
		if (remains>=11 && strncasecmp(temp, "__CHARSET__", 11) == 0) {
			add_len = 11;
			*subs	= &p->charset;
		}
		else if (remains>=8 && strncasecmp(temp, "__PART__", 8) == 0) {
			add_len = 8;
			*subs	= &p->part;
		}
		else if (remains>=16
			  && strncasecmp(temp, "__LAMPACHARSET__", 16) == 0) {
			add_len = 16;
			*subs	= &p->lampacharset;
		} else {
			temp++, remains--;
			continue; /* nothing supported */
		}

		/*
		 * Go berserk if add_len is longer than current value of
		 * CSA_FINDSUBS_MAX - cause a SIGSEGV in that case.
		 */
		if (add_len > CSA_FINDSUBS_MAX) {
			char *foo = NULL;
			foo[0] = 0;
		}

		*idxp = temp - str;
		return add_len;
	}

	return 0; /* nothing supported found */
}

/*
 * parses string and substitutes all occurencies of supported __FOO__
 * strings by appropriate values; returns new strings with all
 * __FOO__ strings substituted or original string if no __FOO__ string
 * has been found in it
 * space for new string is allocated in temporary pool, so it will be
 * reused as soon as we reach main loop in loop.c again
 */
char *
csa_subs_string(p, str)
  csa_params_t *p;
  char *str;
{
	char *buff=NULL, *past=NULL;
	const csa_String *subs;
	size_t buff_len=0, need_len, skipped, idx, idx_rest, rest_len;
	size_t str_len = strlen(str), past_len = 0;
	int use_past;

	/*
	 * XXX This migh be slighly optimized by keeping point of where
	 * we stopped searching last time and starting csa_find_subs()
	 * from there. However, csa_subs_string() is typically called
	 * for short strings, so implementing this optimalization is
	 * not worth the extra complexity of code it would introduce.
	 */
	while((skipped = csa_find_subs(p, str, str_len, &idx, &subs)) != 0) {
		idx_rest = idx + skipped;
		rest_len = str_len - idx_rest;
		if (skipped >= subs->len) {
			memcpy(&str[idx], subs->value, subs->len);
			memmove(&str[idx + subs->len], &str[idx_rest],rest_len);
			str_len = str_len - (skipped - subs->len);
		} else {
			need_len = str_len + (subs->len - skipped);
			if (buff_len < need_len) {
				buff = (char *)csa_alloca(need_len + 1,
							p->pool_tmp);
				buff_len = need_len;
				if (idx) memcpy(buff, str, idx);
				use_past = 0;
			} else {
				/*
				 * Temp buffer is long enough to hold the entire
				 * string, but subs string is longer than
				 * original - have to keep the rest of string
				 * so that it can be correctly appended after
				 * the substitution takes place.
				 */
				if (past_len < rest_len)  {
					past = (char *)csa_alloca(rest_len,
							p->pool_tmp);
					past_len = rest_len;
				}
				memcpy(past, &str[idx_rest], rest_len);
				use_past = 1;
			}
			memcpy(&buff[idx], subs->value, subs->len);
			memcpy(&buff[idx + subs->len],
				(use_past) ? past : &str[idx_rest], rest_len);
			str = buff;
			str_len = need_len;
		}
	}
	str[str_len] = '\0';

	return (buff) ? ap_pstrndup(p->pool_tmp, str, (int)str_len) : str;
}

/*
 * It checks whether strings are non-null before comparing them. 
 * Returns: 0 - same, 1 - different or any is null
 */
int
csa_n_strcmp(s1, s2)
  const char *s1, *s2;
{
    return !(s1 && s2 && strcmp(s1, s2) == 0);
}