[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 HAVE_CONFIG_H |
---|
| 23 | # include <config.h> |
---|
| 24 | #endif |
---|
| 25 | |
---|
| 26 | #if STDC_HEADERS |
---|
| 27 | # include <string.h> |
---|
| 28 | #else |
---|
| 29 | # if !HAVE_STRCHR |
---|
| 30 | # define strchr index |
---|
| 31 | # define strrchr rindex |
---|
| 32 | # endif |
---|
| 33 | char *strchr (), *strrchr (); |
---|
| 34 | # if !HAVE_MEMCPY |
---|
| 35 | # define memcpy(d, s, n) bcopy ((s), (d), (n)) |
---|
| 36 | # define memmove(d, s, n) bcopy ((s), (d), (n)) |
---|
| 37 | # endif |
---|
| 38 | #endif |
---|
| 39 | |
---|
| 40 | #include "yahoo_util.h" |
---|
| 41 | |
---|
| 42 | char * y_string_append(char * string, char * append) |
---|
| 43 | { |
---|
| 44 | int size = strlen(string) + strlen(append) + 1; |
---|
| 45 | char * new_string = y_renew(char, string, size); |
---|
| 46 | |
---|
| 47 | if(new_string == NULL) { |
---|
| 48 | new_string = y_new(char, size); |
---|
| 49 | strcpy(new_string, string); |
---|
| 50 | FREE(string); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | strcat(new_string, append); |
---|
| 54 | |
---|
| 55 | return new_string; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | char * y_str_to_utf8(const char *in) |
---|
| 59 | { |
---|
| 60 | unsigned int n, i = 0; |
---|
| 61 | char *result = NULL; |
---|
| 62 | |
---|
| 63 | if(in == NULL || *in == '\0') |
---|
| 64 | return ""; |
---|
| 65 | |
---|
| 66 | result = y_new(char, strlen(in) * 2 + 1); |
---|
| 67 | |
---|
| 68 | /* convert a string to UTF-8 Format */ |
---|
| 69 | for (n = 0; n < strlen(in); n++) { |
---|
| 70 | unsigned char c = (unsigned char)in[n]; |
---|
| 71 | |
---|
| 72 | if (c < 128) { |
---|
| 73 | result[i++] = (char) c; |
---|
| 74 | } else { |
---|
| 75 | result[i++] = (char) ((c >> 6) | 192); |
---|
| 76 | result[i++] = (char) ((c & 63) | 128); |
---|
| 77 | } |
---|
| 78 | } |
---|
| 79 | result[i] = '\0'; |
---|
| 80 | return result; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | char * y_utf8_to_str(const char *in) |
---|
| 84 | { |
---|
| 85 | int i = 0; |
---|
| 86 | unsigned int n; |
---|
| 87 | char *result = NULL; |
---|
| 88 | |
---|
| 89 | if(in == NULL || *in == '\0') |
---|
| 90 | return ""; |
---|
| 91 | |
---|
| 92 | result = y_new(char, strlen(in) + 1); |
---|
| 93 | |
---|
| 94 | /* convert a string from UTF-8 Format */ |
---|
| 95 | for (n = 0; n < strlen(in); n++) { |
---|
| 96 | unsigned char c = in[n]; |
---|
| 97 | |
---|
| 98 | if (c < 128) { |
---|
| 99 | result[i++] = (char) c; |
---|
| 100 | } else { |
---|
| 101 | result[i++] = (c << 6) | (in[++n] & 63); |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | result[i] = '\0'; |
---|
| 105 | return result; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | #if !HAVE_GLIB |
---|
| 109 | |
---|
| 110 | void y_strfreev(char ** vector) |
---|
| 111 | { |
---|
| 112 | char **v; |
---|
| 113 | for(v = vector; *v; v++) { |
---|
| 114 | FREE(*v); |
---|
| 115 | } |
---|
| 116 | FREE(vector); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | char ** y_strsplit(char * str, char * sep, int nelem) |
---|
| 120 | { |
---|
| 121 | char ** vector; |
---|
| 122 | char *s, *p; |
---|
| 123 | int i=0; |
---|
| 124 | int l = strlen(sep); |
---|
| 125 | if(nelem < 0) { |
---|
| 126 | char * s; |
---|
| 127 | nelem=0; |
---|
| 128 | for(s=strstr(str, sep); s; s=strstr(s+l, sep),nelem++) |
---|
| 129 | ; |
---|
| 130 | if(strcmp(str+strlen(str)-l, sep)) |
---|
| 131 | nelem++; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | vector = y_new(char *, nelem + 1); |
---|
| 135 | |
---|
| 136 | for(p=str, s=strstr(p,sep); i<nelem && s; p=s+l, s=strstr(p,sep), i++) { |
---|
| 137 | int len = s-p; |
---|
| 138 | vector[i] = y_new(char, len+1); |
---|
| 139 | strncpy(vector[i], p, len); |
---|
| 140 | vector[i][len] = '\0'; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | if(i<nelem) /* str didn't end with sep */ |
---|
| 144 | vector[i++] = strdup(p); |
---|
| 145 | |
---|
| 146 | vector[i] = NULL; |
---|
| 147 | |
---|
| 148 | return vector; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | void * y_memdup(const void * addr, int n) |
---|
| 152 | { |
---|
| 153 | void * new_chunk = malloc(n); |
---|
| 154 | if(new_chunk) |
---|
| 155 | memcpy(new_chunk, addr, n); |
---|
| 156 | return new_chunk; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | #endif |
---|