[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 |
---|
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 | for(v = vector; *v; v++) { |
---|
| 60 | FREE(*v); |
---|
| 61 | } |
---|
| 62 | FREE(vector); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | char ** y_strsplit(char * str, char * sep, int nelem) |
---|
| 66 | { |
---|
| 67 | char ** vector; |
---|
| 68 | char *s, *p; |
---|
| 69 | int i=0; |
---|
| 70 | int l = strlen(sep); |
---|
| 71 | if(nelem < 0) { |
---|
| 72 | char * s; |
---|
| 73 | nelem=0; |
---|
| 74 | for(s=strstr(str, sep); s; s=strstr(s+l, sep),nelem++) |
---|
| 75 | ; |
---|
| 76 | if(strcmp(str+strlen(str)-l, sep)) |
---|
| 77 | nelem++; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | vector = y_new(char *, nelem + 1); |
---|
| 81 | |
---|
| 82 | for(p=str, s=strstr(p,sep); i<nelem && s; p=s+l, s=strstr(p,sep), i++) { |
---|
| 83 | int len = s-p; |
---|
| 84 | vector[i] = y_new(char, len+1); |
---|
| 85 | strncpy(vector[i], p, len); |
---|
| 86 | vector[i][len] = '\0'; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | if(i<nelem) /* str didn't end with sep */ |
---|
| 90 | vector[i++] = strdup(p); |
---|
| 91 | |
---|
| 92 | vector[i] = NULL; |
---|
| 93 | |
---|
| 94 | return vector; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | void * y_memdup(const void * addr, int n) |
---|
| 98 | { |
---|
| 99 | void * new_chunk = malloc(n); |
---|
| 100 | if(new_chunk) |
---|
| 101 | memcpy(new_chunk, addr, n); |
---|
| 102 | return new_chunk; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | #endif |
---|