2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include <doldaconnect/utils.h>
35 extern int vswprintf (wchar_t *__restrict __s, size_t __n,
36 __const wchar_t *__restrict __format,
37 __gnuc_va_list __arg);
39 char *vsprintf2(char *format, va_list al)
44 ret = vsnprintf(NULL, 0, format, al);
45 if((buf = malloc(ret + 1)) == NULL)
49 vsnprintf(buf, ret + 1, format, al);
53 char *sprintf2(char *format, ...)
58 va_start(args, format);
59 buf = vsprintf2(format, args);
64 wchar_t *vswprintf2(wchar_t *format, va_list al)
70 buf = smalloc(sizeof(wchar_t) * (bufsize = 1024));
71 while((ret = vswprintf(buf, bufsize, format, al)) < 0)
72 buf = srealloc(buf, sizeof(wchar_t) * (bufsize *= 2));
74 buf = srealloc(buf, sizeof(wchar_t) * (ret + 1));
78 wchar_t *swprintf2(wchar_t *format, ...)
83 va_start(args, format);
84 buf = vswprintf2(format, args);
89 wchar_t *icmbstowcs(char *mbs, char *charset)
94 size_t len1, len2, bufsize, data;
97 len1 = strlen(mbs) + 1;
98 bufsize = len2 = len1 * sizeof(wchar_t);
99 if((buf = malloc(bufsize)) == NULL)
104 charset = nl_langinfo(CODESET);
105 if((cd = iconv_open("wchar_t", charset)) == (iconv_t)-1)
113 ret = iconv(cd, &mbs, &len1, &p, &len2);
121 if((p2 = realloc(buf, bufsize)) == NULL)
135 buf = realloc(buf, p - buf);
137 return((wchar_t *)buf);
140 wchar_t *icsmbstowcs(char *mbs, char *charset, wchar_t *def)
142 static wchar_t *buf = NULL;
146 if((buf = icmbstowcs(mbs, charset)) == NULL)
151 char *icwcstombs(wchar_t *wcs, char *charset)
156 size_t len1, len2, bufsize, data;
159 len1 = sizeof(wchar_t) * (wcslen(wcs) + 1);
160 bufsize = len2 = len1;
161 if((buf = malloc(bufsize)) == NULL)
166 charset = nl_langinfo(CODESET);
167 if((cd = iconv_open(charset, "wchar_t")) == (iconv_t)-1)
175 ret = iconv(cd, (char **)&wcs, &len1, &p, &len2);
183 if((p2 = realloc(buf, bufsize)) == NULL)
197 buf = realloc(buf, p - buf);
202 char *icswcstombs(wchar_t *wcs, char *charset, char *def)
204 static char *buf = NULL;
208 if((buf = icwcstombs(wcs, charset)) == NULL)
213 wchar_t *wcstolower(wchar_t *wcs)
217 for(p = wcs; *p != L'\0'; p++)
222 void _sizebuf(void **buf, size_t *bufsize, size_t reqsize, size_t elsize, int algo)
224 if(*bufsize >= reqsize)
229 *buf = srealloc(*buf, elsize * ((*bufsize) = reqsize));
234 while(*bufsize < reqsize)
236 *buf = srealloc(*buf, elsize * (*bufsize));