File: snstring.c

package info (click to toggle)
advancecomp 1.19-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,864 kB
  • ctags: 1,959
  • sloc: cpp: 11,148; ansic: 5,972; sh: 3,670; makefile: 308
file content (217 lines) | stat: -rw-r--r-- 5,030 bytes parent folder | download | duplicates (12)
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
/*
 * This file is part of the Advance project.
 *
 * Copyright (C) 1999, 2000, 2001, 2002, 2003 Andrea Mazzoleni
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details. 
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * In addition, as a special exception, Andrea Mazzoleni
 * gives permission to link the code of this program with
 * the MAME library (or with modified versions of MAME that use the
 * same license as MAME), and distribute linked combinations including
 * the two.  You must obey the GNU General Public License in all
 * respects for all of the code used other than MAME.  If you modify
 * this file, you may extend this exception to your version of the
 * file, but you are not obligated to do so.  If you do not wish to
 * do so, delete this exception statement from your version.
 */

#include "portable.h"

#include "snstring.h"

/**
 * Copy a string with a size limit.
 * The destination string always has the terminating 0.
 */
void sncpy(char* dst, size_t len, const char* src)
{
	if (len) {
		--len;
		while (len && *src) {
			*dst++ = *src++;
			--len;
		}
		*dst = 0;
#ifndef NDEBUG
		++dst;
		while (len) {
			*dst++ = 0x5A;
			--len;
		}
#endif
	}
}

/**
 * Copy a string with a size limit.
 * The destination string always has the terminating 0.
 */
void sncpyc(char* dst, size_t len, char src)
{
	sncpyn(dst, len, &src, 1);
}

/**
 * Copy a string with a size limit and no more than the specified number of chars.
 * The destination string always has the terminating 0.
 */
void sncpyn(char* dst, size_t len, const char* src, size_t src_len)
{
	if (len < src_len + 1)
		sncpy(dst, len, src);
	else
		sncpy(dst, src_len + 1, src);
}

/**
 * Cat a string with a size limit.
 * The destination string always has the terminating 0.
 */
void sncat(char* dst, size_t len, const char* src)
{
	while (len && *dst) {
		++dst;
		--len;
	}
	sncpy(dst, len, src);
}

/**
 * Cat a string with a size limit.
 * The destination string always has the terminating 0.
 */
void sncatc(char* dst, size_t len, char src)
{
	while (len && *dst) {
		++dst;
		--len;
	}
	sncpyc(dst, len, src);
}


/**
 * Print at the end of a string with a size limit.
 * The destination string always has the terminating 0.
 */
void sncatf(char* str, size_t count, const char* fmt, ...)
{
	unsigned l;
	va_list arg;
	va_start(arg, fmt);

	l = 0;
	while (l<count && str[l])
		++l;

	if (count > l)
		vsnprintf(str + l, count - l, fmt, arg);

	va_end(arg);
}

/**
 * Skip a subset of chars.
 * \param p Index on the string.
 * \param s String to scan.
 * \param sep Set of chars to skip.
 */
void sskip(int* p, const char* s, const char* sep)
{
	while (s[*p] && strchr(sep, s[*p])!=0)
		++*p;
}

/**
 * Extract a token from a string.
 * \param c Separator character. It's cleared in the string to ensure a 0 termination of the token. It's 0 if the token end the string.
 * \param p Index on the string. The index is increased at the end of the token or after the separator if present.
 * \param s String to scan. The string is modified to put the terminating 0 at the end of the token.
 * \param sep Set of chars to use as separators.
 * \param ignore Set of chars to ignore. They are removed at the start and at the end of the token. They are not removed after the separator.
 * \return The extratect token, always 0 terminated.
 */
const char* stoken(char* c, int* p, char* s, const char* sep, const char* ignore)
{
	unsigned begin;
	unsigned end;

	begin = *p;

	while (s[*p] && strchr(sep, s[*p])==0)
		++*p;

	end = *p;

	*c = s[*p];
	if (s[*p]) {
		s[*p] = 0;
		++*p;
	}

	while (begin < end && strchr(ignore, s[begin])!=0) {
		++begin;
	}

	while (begin < end && strchr(ignore, s[end-1])!=0) {
		--end;
		s[end] = 0;
	}

	return s + begin;
}

/**
 * Match a glob pattern to a string.
 * \param s String to compare.
 * \param glob Pattern to use. The glob chars * and ? are allowed. You can prefix
 * these char with a backslash to prevent globbing expansion.
 * \return If the pattern match.
 */
adv_bool sglob(const char* s, const char* glob)
{
	while (*s && *glob) {
		if (*glob == '*') {
			if (sglob(s, glob+1))
				return 1;
			++s;
			continue;
		}

		if (*glob == '?') {
			++glob;
			++s;
			continue;
		}

		if (glob[0] == '\\' && (glob[1] == '\\' || glob[1] == '*' || glob[1] == '?')) {
			++glob;
		}

		if (*glob != *s) {
			return 0;
		}

		++glob;
		++s;
	}

	while (*glob == '*')
		++glob;

	return !*s && !*glob;
}