Fixed bug #52
[centerim.git] / libyahoo2 / yahoo_util.c
blob04a6c345149caf4680349c22b74376904660ec55
1 /*
2 * libyahoo2: yahoo_util.c
4 * Copyright (C) 2002-2004, Philip S Tellis <philip.tellis AT gmx.net>
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.
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.
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
22 #if HAVE_CONFIG_H
23 # include <config.h>
24 #endif
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
40 #include "yahoo_util.h"
42 char * y_string_append(char * string, char * append)
44 int size = strlen(string) + strlen(append) + 1;
45 char * new_string = y_renew(char, string, size);
47 if(new_string == NULL) {
48 new_string = y_new(char, size);
49 strcpy(new_string, string);
50 FREE(string);
53 strcat(new_string, append);
55 return new_string;
58 char * y_str_to_utf8(const char *in)
60 unsigned int n, i = 0;
61 char *result = NULL;
63 if(in == NULL || *in == '\0')
64 return "";
66 result = y_new(char, strlen(in) * 2 + 1);
68 /* convert a string to UTF-8 Format */
69 for (n = 0; n < strlen(in); n++) {
70 unsigned char c = (unsigned char)in[n];
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);
79 result[i] = '\0';
80 return result;
83 char * y_utf8_to_str(const char *in)
85 int i = 0;
86 unsigned int n;
87 char *result = NULL;
89 if(in == NULL || *in == '\0')
90 return "";
92 result = y_new(char, strlen(in) + 1);
94 /* convert a string from UTF-8 Format */
95 for (n = 0; n < strlen(in); n++) {
96 unsigned char c = in[n];
98 if (c < 128) {
99 result[i++] = (char) c;
100 } else {
101 result[i++] = (c << 6) | (in[++n] & 63);
104 result[i] = '\0';
105 return result;
108 #if !HAVE_GLIB
110 void y_strfreev(char ** vector)
112 char **v;
113 for(v = vector; *v; v++) {
114 FREE(*v);
116 FREE(vector);
119 char ** y_strsplit(char * str, char * sep, int nelem)
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 if (*str) {
129 for(s=strstr(str, sep); s; s=strstr(s+l, sep),nelem++)
131 if(strcmp(str+strlen(str)-l, sep))
132 nelem++;
136 vector = y_new(char *, nelem + 1);
138 for(p=str, s=strstr(p,sep); i<nelem && s; p=s+l, s=strstr(p,sep), i++) {
139 int len = s-p;
140 vector[i] = y_new(char, len+1);
141 strncpy(vector[i], p, len);
142 vector[i][len] = '\0';
145 if(i<nelem && *str) /* str didn't end with sep, and str isn't empty */
146 vector[i++] = strdup(p);
148 vector[i] = NULL;
150 return vector;
153 void * y_memdup(const void * addr, int n)
155 void * new_chunk = malloc(n);
156 if(new_chunk)
157 memcpy(new_chunk, addr, n);
158 return new_chunk;
161 #endif