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
31 # define strrchr rindex
33 char *strchr (), *strrchr ();
35 # define memcpy(d, s, n) bcopy ((s), (d), (n))
36 # define memmove(d, s, n) bcopy ((s), (d), (n))
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
);
53 strcat(new_string
, append
);
58 char * y_str_to_utf8(const char *in
)
60 unsigned int n
, i
= 0;
63 if(in
== NULL
|| *in
== '\0')
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
];
73 result
[i
++] = (char) c
;
75 result
[i
++] = (char) ((c
>> 6) | 192);
76 result
[i
++] = (char) ((c
& 63) | 128);
83 char * y_utf8_to_str(const char *in
)
89 if(in
== NULL
|| *in
== '\0')
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
];
99 result
[i
++] = (char) c
;
101 result
[i
++] = (c
<< 6) | (in
[++n
] & 63);
110 void y_strfreev(char ** vector
)
113 for(v
= vector
; *v
; v
++) {
119 char ** y_strsplit(char * str
, char * sep
, int nelem
)
129 for(s
=strstr(str
, sep
); s
; s
=strstr(s
+l
, sep
),nelem
++)
131 if(strcmp(str
+strlen(str
)-l
, sep
))
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
++) {
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
);
153 void * y_memdup(const void * addr
, int n
)
155 void * new_chunk
= malloc(n
);
157 memcpy(new_chunk
, addr
, n
);