1 | /* |
---|
2 | * libyahoo2: yahoo_util.c |
---|
3 | * |
---|
4 | * Copyright (C) 2002-2004, Philip S Tellis <philip.tellis AT gmx.net> |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation; either version 2 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
---|
19 | * |
---|
20 | */ |
---|
21 | |
---|
22 | #if STDC_HEADERS |
---|
23 | # include <string.h> |
---|
24 | #else |
---|
25 | # if !HAVE_STRCHR |
---|
26 | # define strchr index |
---|
27 | # define strrchr rindex |
---|
28 | # endif |
---|
29 | char *strchr(), *strrchr(); |
---|
30 | # if !HAVE_MEMCPY |
---|
31 | # define memcpy(d, s, n) bcopy((s), (d), (n)) |
---|
32 | # define memmove(d, s, n) bcopy((s), (d), (n)) |
---|
33 | # endif |
---|
34 | #endif |
---|
35 | |
---|
36 | #include "yahoo_util.h" |
---|
37 | |
---|
38 | char *y_string_append(char *string, char *append) |
---|
39 | { |
---|
40 | int size = strlen(string) + strlen(append) + 1; |
---|
41 | char *new_string = y_renew(char, string, size); |
---|
42 | |
---|
43 | if (new_string == NULL) { |
---|
44 | new_string = y_new(char, size); |
---|
45 | strcpy(new_string, string); |
---|
46 | FREE(string); |
---|
47 | } |
---|
48 | |
---|
49 | strcat(new_string, append); |
---|
50 | |
---|
51 | return new_string; |
---|
52 | } |
---|
53 | |
---|
54 | #if !HAVE_GLIB |
---|
55 | |
---|
56 | void y_strfreev(char ** vector) |
---|
57 | { |
---|
58 | char **v; |
---|
59 | |
---|
60 | for (v = vector; *v; v++) { |
---|
61 | FREE(*v); |
---|
62 | } |
---|
63 | FREE(vector); |
---|
64 | } |
---|
65 | |
---|
66 | char ** y_strsplit(char * str, char * sep, int nelem) |
---|
67 | { |
---|
68 | char ** vector; |
---|
69 | char *s, *p; |
---|
70 | int i = 0; |
---|
71 | int l = strlen(sep); |
---|
72 | |
---|
73 | if (nelem <= 0) { |
---|
74 | char * s; |
---|
75 | nelem = 0; |
---|
76 | if (*str) { |
---|
77 | for (s = strstr(str, sep); s; s = strstr(s + l, sep), nelem++) { |
---|
78 | ; |
---|
79 | } |
---|
80 | if (strcmp(str + strlen(str) - l, sep)) { |
---|
81 | nelem++; |
---|
82 | } |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | vector = y_new(char *, nelem + 1); |
---|
87 | |
---|
88 | for (p = str, s = strstr(p, sep); i < nelem && s; p = s + l, s = strstr(p, sep), i++) { |
---|
89 | int len = s - p; |
---|
90 | vector[i] = y_new(char, len + 1); |
---|
91 | strncpy(vector[i], p, len); |
---|
92 | vector[i][len] = '\0'; |
---|
93 | } |
---|
94 | |
---|
95 | if (i < nelem && *str) { /* str didn't end with sep, and str isn't empty */ |
---|
96 | vector[i++] = strdup(p); |
---|
97 | } |
---|
98 | |
---|
99 | vector[i] = NULL; |
---|
100 | |
---|
101 | return vector; |
---|
102 | } |
---|
103 | |
---|
104 | void * y_memdup(const void * addr, int n) |
---|
105 | { |
---|
106 | void * new_chunk = malloc(n); |
---|
107 | |
---|
108 | if (new_chunk) { |
---|
109 | memcpy(new_chunk, addr, n); |
---|
110 | } |
---|
111 | return new_chunk; |
---|
112 | } |
---|
113 | |
---|
114 | #endif |
---|