[b7d3cc34] | 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 |
---|
[6f10697] | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
---|
[b7d3cc34] | 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 |
---|
[5ebff60] | 29 | char *strchr(), *strrchr(); |
---|
[b7d3cc34] | 30 | # if !HAVE_MEMCPY |
---|
[5ebff60] | 31 | # define memcpy(d, s, n) bcopy((s), (d), (n)) |
---|
| 32 | # define memmove(d, s, n) bcopy((s), (d), (n)) |
---|
[b7d3cc34] | 33 | # endif |
---|
| 34 | #endif |
---|
| 35 | |
---|
| 36 | #include "yahoo_util.h" |
---|
| 37 | |
---|
[c36f73b] | 38 | char *y_string_append(char *string, char *append) |
---|
[b7d3cc34] | 39 | { |
---|
| 40 | int size = strlen(string) + strlen(append) + 1; |
---|
[c36f73b] | 41 | char *new_string = y_renew(char, string, size); |
---|
[b7d3cc34] | 42 | |
---|
[c36f73b] | 43 | if (new_string == NULL) { |
---|
[b7d3cc34] | 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; |
---|
[5ebff60] | 59 | |
---|
| 60 | for (v = vector; *v; v++) { |
---|
[b7d3cc34] | 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; |
---|
[5ebff60] | 70 | int i = 0; |
---|
[b7d3cc34] | 71 | int l = strlen(sep); |
---|
[5ebff60] | 72 | |
---|
| 73 | if (nelem <= 0) { |
---|
[b7d3cc34] | 74 | char * s; |
---|
[5ebff60] | 75 | nelem = 0; |
---|
[cfc8d58] | 76 | if (*str) { |
---|
[5ebff60] | 77 | for (s = strstr(str, sep); s; s = strstr(s + l, sep), nelem++) { |
---|
[cfc8d58] | 78 | ; |
---|
[5ebff60] | 79 | } |
---|
| 80 | if (strcmp(str + strlen(str) - l, sep)) { |
---|
[cfc8d58] | 81 | nelem++; |
---|
[5ebff60] | 82 | } |
---|
[cfc8d58] | 83 | } |
---|
[b7d3cc34] | 84 | } |
---|
| 85 | |
---|
| 86 | vector = y_new(char *, nelem + 1); |
---|
| 87 | |
---|
[5ebff60] | 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); |
---|
[b7d3cc34] | 91 | strncpy(vector[i], p, len); |
---|
| 92 | vector[i][len] = '\0'; |
---|
| 93 | } |
---|
| 94 | |
---|
[5ebff60] | 95 | if (i < nelem && *str) { /* str didn't end with sep, and str isn't empty */ |
---|
[b7d3cc34] | 96 | vector[i++] = strdup(p); |
---|
[5ebff60] | 97 | } |
---|
| 98 | |
---|
[b7d3cc34] | 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); |
---|
[5ebff60] | 107 | |
---|
| 108 | if (new_chunk) { |
---|
[b7d3cc34] | 109 | memcpy(new_chunk, addr, n); |
---|
[5ebff60] | 110 | } |
---|
[b7d3cc34] | 111 | return new_chunk; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | #endif |
---|