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;
83 if (dstsz < 1) return 0;
88 while ((src = va_arg(ap, char *)) != NULL) {
89 tail = string_pcopy(dp, src, leftsz);
90 if (tail == dst + dstsz) {
103 /* Return a point of tail letter.
104 If it point '\0', `str' is null string from the beginning. */
106 string_tail(char *str)
112 while (str != '\0') {
119 /* Eliminate a newline.
120 Return the string length. */
127 if (len == 0) return 0;
129 if (len > 0 && s[len-1] == '\n') {
133 if (len > 0 && s[len-1] == '\r') {
140 /* Return the address points non-space letter */
141 /* the `space' means bellow. */
142 #define IS_SPACE(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
152 /* Return the address points space letter */
154 skip_to_space(char *s)
156 while (!IS_SPACE(*s))
165 return skip_space(skip_to_space(s));
168 /* Strip spaces on both sides. */
197 puts("--- test string_copy()");
200 char src[80] = "abcdefg", dst[5];
202 strncpy(dst, src, sizeof dst); dst[sizeof dst - 1] = '\0';
204 printf("%d:%s\n", i, dst);
207 i = string_copy(dst, src, sizeof dst);
208 printf("%d:%s\n", i, dst);
211 puts("--- test string_pcopy()");
213 char buf[10] = "", *p1, *p2;
214 int size, len, length = 0;
218 p1 = string_pcopy(buf, "abc", size);
223 p2 = string_pcopy(p1, "def", size);
228 printf("%d:%s\n", length, buf);
231 puts("--- test string_cat()");
236 len = string_cat(buf, 9, "012", "345", "678", "9ab", 0);
237 printf("%d:%s\n", len, buf);
240 puts("--- test for strip_space()");
245 strcpy(buf, " a b c ");
246 ptr = strip_space(buf);
247 if (ptr != NULL) puts(ptr);
250 strcpy(buf, "a b c");
251 printf("\x22%s\x22\n", strip_space(buf));
255 printf("\x22%s\x22\n", strip_space(buf));
259 printf("\x22%s\x22\n", strip_space(buf));