2 Copyright (c) 2000 Koji Arai
4 Permission is hereby granted, free of charge, to any person
5 obtaining a copy of this software and associated documentation files
6 (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge,
8 publish, distribute, sublicense, and/or sell copies of the Software,
9 and to permit persons to whom the Software is furnished to do so,
10 subject to the following conditions:
12 The above copyright notice and this permission notice shall be
13 included in all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 string_equal(char *str1, char *str2)
32 return strcmp(str1, str2) == 0;
35 /* This function is similar to strncpy() but `dst' is always terminated '\0'.
36 Return the copied string length. */
38 string_copy(char *dst, char *src, int dstsz)
42 if (dstsz < 1) return 0;
44 for (i = 0; i < dstsz; i++) {
45 if ((dst[i] = src[i]) == '\0')
49 /* here is i == dstsz */
50 dst[--i] = '\0'; /* if eliminate this line and return dst,
51 this function is same as strncpy(). */
56 /* This function is identical to stpncpy()
57 in GNU gcc (and in MS-DOG compiler?) */
59 string_pcopy(char *dst, char *src, int dstsz)
63 for (i = 0; i < dstsz; i++) {
64 if ((dst[i] = src[i]) == '\0')
68 /* if here is `return dst', this function is same as strncpy() */
72 /* string concatenation.
73 1st arg. `dst' is always terminated '\0'.
74 Return the string length */
76 string_cat(char *dst, int dstsz, ...)
79 char *dp, *src, *tail;
82 if (dstsz < 1) return 0;
87 while ((src = va_arg(ap, char *)) != NULL) {
88 tail = string_pcopy(dp, src, leftsz);
89 if (tail == dst + dstsz) {
102 /* Return a point of tail letter.
103 If it point '\0', `str' is null string from the beginning. */
105 string_tail(char *str)
111 while (str != '\0') {
118 /* Eliminate a newline.
119 Return the string length. */
126 if (len == 0) return 0;
128 if (len > 0 && s[len-1] == '\n') {
132 if (len > 0 && s[len-1] == '\r') {
139 /* Return the address points non-space letter */
140 /* the `space' means bellow. */
141 #define IS_SPACE(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
151 /* Return the address points space letter */
153 skip_to_space(char *s)
155 while (!IS_SPACE(*s))
164 return skip_space(skip_to_space(s));
167 /* Strip spaces on both sides. */
196 puts("--- test string_copy()");
199 char src[80] = "abcdefg", dst[5];
201 strncpy(dst, src, sizeof dst); dst[sizeof dst - 1] = '\0';
203 printf("%d:%s\n", i, dst);
206 i = string_copy(dst, src, sizeof dst);
207 printf("%d:%s\n", i, dst);
210 puts("--- test string_pcopy()");
212 char buf[10] = "", *p1, *p2;
213 int size, len, length = 0;
217 p1 = string_pcopy(buf, "abc", size);
222 p2 = string_pcopy(p1, "def", size);
227 printf("%d:%s\n", length, buf);
230 puts("--- test string_cat()");
235 len = string_cat(buf, 9, "012", "345", "678", "9ab", 0);
236 printf("%d:%s\n", len, buf);
239 puts("--- test for strip_space()");
244 strcpy(buf, " a b c ");
245 ptr = strip_space(buf);
246 if (ptr != NULL) puts(ptr);
249 strcpy(buf, "a b c");
250 printf("\x22%s\x22\n", strip_space(buf));
254 printf("\x22%s\x22\n", strip_space(buf));
258 printf("\x22%s\x22\n", strip_space(buf));