[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); |
---|
[cfc8d58] | 71 | if(nelem <= 0) { |
---|
[b7d3cc34] | 72 | char * s; |
---|
| 73 | nelem=0; |
---|
[cfc8d58] | 74 | if (*str) { |
---|
| 75 | for(s=strstr(str, sep); s; s=strstr(s+l, sep),nelem++) |
---|
| 76 | ; |
---|
| 77 | if(strcmp(str+strlen(str)-l, sep)) |
---|
| 78 | nelem++; |
---|
| 79 | } |
---|
[b7d3cc34] | 80 | } |
---|
| 81 | |
---|
| 82 | vector = y_new(char *, nelem + 1); |
---|
| 83 | |
---|
| 84 | for(p=str, s=strstr(p,sep); i<nelem && s; p=s+l, s=strstr(p,sep), i++) { |
---|
| 85 | int len = s-p; |
---|
| 86 | vector[i] = y_new(char, len+1); |
---|
| 87 | strncpy(vector[i], p, len); |
---|
| 88 | vector[i][len] = '\0'; |
---|
| 89 | } |
---|
| 90 | |
---|
[cfc8d58] | 91 | if(i<nelem && *str) /* str didn't end with sep, and str isn't empty */ |
---|
[b7d3cc34] | 92 | vector[i++] = strdup(p); |
---|
| 93 | |
---|
| 94 | vector[i] = NULL; |
---|
| 95 | |
---|
| 96 | return vector; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | void * y_memdup(const void * addr, int n) |
---|
| 100 | { |
---|
| 101 | void * new_chunk = malloc(n); |
---|
| 102 | if(new_chunk) |
---|
| 103 | memcpy(new_chunk, addr, n); |
---|
| 104 | return new_chunk; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | #endif |
---|