[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 | char * y_str_to_utf8(const char *in) |
---|
| 55 | { |
---|
| 56 | unsigned int n, i = 0; |
---|
| 57 | char *result = NULL; |
---|
| 58 | |
---|
| 59 | if(in == NULL || *in == '\0') |
---|
| 60 | return ""; |
---|
| 61 | |
---|
| 62 | result = y_new(char, strlen(in) * 2 + 1); |
---|
| 63 | |
---|
| 64 | /* convert a string to UTF-8 Format */ |
---|
| 65 | for (n = 0; n < strlen(in); n++) { |
---|
| 66 | unsigned char c = (unsigned char)in[n]; |
---|
| 67 | |
---|
| 68 | if (c < 128) { |
---|
| 69 | result[i++] = (char) c; |
---|
| 70 | } else { |
---|
| 71 | result[i++] = (char) ((c >> 6) | 192); |
---|
| 72 | result[i++] = (char) ((c & 63) | 128); |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | result[i] = '\0'; |
---|
| 76 | return result; |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | char * y_utf8_to_str(const char *in) |
---|
| 80 | { |
---|
| 81 | int i = 0; |
---|
| 82 | unsigned int n; |
---|
| 83 | char *result = NULL; |
---|
| 84 | |
---|
| 85 | if(in == NULL || *in == '\0') |
---|
| 86 | return ""; |
---|
| 87 | |
---|
| 88 | result = y_new(char, strlen(in) + 1); |
---|
| 89 | |
---|
| 90 | /* convert a string from UTF-8 Format */ |
---|
| 91 | for (n = 0; n < strlen(in); n++) { |
---|
| 92 | unsigned char c = in[n]; |
---|
| 93 | |
---|
| 94 | if (c < 128) { |
---|
| 95 | result[i++] = (char) c; |
---|
| 96 | } else { |
---|
| 97 | result[i++] = (c << 6) | (in[++n] & 63); |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | result[i] = '\0'; |
---|
| 101 | return result; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | #if !HAVE_GLIB |
---|
| 105 | |
---|
| 106 | void y_strfreev(char ** vector) |
---|
| 107 | { |
---|
| 108 | char **v; |
---|
| 109 | for(v = vector; *v; v++) { |
---|
| 110 | FREE(*v); |
---|
| 111 | } |
---|
| 112 | FREE(vector); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | char ** y_strsplit(char * str, char * sep, int nelem) |
---|
| 116 | { |
---|
| 117 | char ** vector; |
---|
| 118 | char *s, *p; |
---|
| 119 | int i=0; |
---|
| 120 | int l = strlen(sep); |
---|
| 121 | if(nelem < 0) { |
---|
| 122 | char * s; |
---|
| 123 | nelem=0; |
---|
| 124 | for(s=strstr(str, sep); s; s=strstr(s+l, sep),nelem++) |
---|
| 125 | ; |
---|
| 126 | if(strcmp(str+strlen(str)-l, sep)) |
---|
| 127 | nelem++; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | vector = y_new(char *, nelem + 1); |
---|
| 131 | |
---|
| 132 | for(p=str, s=strstr(p,sep); i<nelem && s; p=s+l, s=strstr(p,sep), i++) { |
---|
| 133 | int len = s-p; |
---|
| 134 | vector[i] = y_new(char, len+1); |
---|
| 135 | strncpy(vector[i], p, len); |
---|
| 136 | vector[i][len] = '\0'; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | if(i<nelem) /* str didn't end with sep */ |
---|
| 140 | vector[i++] = strdup(p); |
---|
| 141 | |
---|
| 142 | vector[i] = NULL; |
---|
| 143 | |
---|
| 144 | return vector; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | void * y_memdup(const void * addr, int n) |
---|
| 148 | { |
---|
| 149 | void * new_chunk = malloc(n); |
---|
| 150 | if(new_chunk) |
---|
| 151 | memcpy(new_chunk, addr, n); |
---|
| 152 | return new_chunk; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | #endif |
---|