Factor out allocating conversion between wcs and mbs

weechat-hashes
Curtis McEnroe 2018-08-07 16:24:14 -04:00
parent 2fe8b4e614
commit a6ede6f91f
No known key found for this signature in database
GPG Key ID: CEA2F97ADCFCD77C
3 changed files with 41 additions and 14 deletions

2
chat.h
View File

@ -64,4 +64,6 @@ void tabTouch(const char *word);
void tabRemove(const char *word);
void tabReplace(const char *prev, const char *next);
wchar_t *ambstowcs(const char *src);
char *awcstombs(const wchar_t *src);
int vaswprintf(wchar_t **ret, const wchar_t *format, va_list ap);

32
pls.c
View File

@ -20,6 +20,38 @@
#include <stdlib.h>
#include <wchar.h>
wchar_t *ambstowcs(const char *src) {
size_t len = mbsrtowcs(NULL, &src, 0, NULL);
if (len == (size_t)-1) return NULL;
wchar_t *dst = malloc(sizeof(*dst) * (1 + len));
if (!dst) return NULL;
len = mbsrtowcs(dst, &src, 1 + len, NULL);
if (len == (size_t)-1) {
free(dst);
return NULL;
}
return dst;
}
char *awcstombs(const wchar_t *src) {
size_t len = wcsrtombs(NULL, &src, 0, NULL);
if (len == (size_t)-1) return NULL;
char *dst = malloc(sizeof(*dst) * (1 + len));
if (!dst) return NULL;
len = wcsrtombs(dst, &src, 1 + len, NULL);
if (len == (size_t)-1) {
free(dst);
return NULL;
}
return dst;
}
// From <https://en.cppreference.com/w/c/io/fwprintf#Notes>:
//
// While narrow strings provide snprintf, which makes it possible to determine

21
ui.c
View File

@ -267,11 +267,10 @@ void uiTopic(const wchar_t *topic) {
}
void uiTopicStr(const char *topic) {
size_t len = strlen(topic);
wchar_t wcs[1 + len];
len = mbstowcs(wcs, topic, 1 + len);
if (len == (size_t)-1) err(EX_DATAERR, "mbstowcs");
wchar_t *wcs = ambstowcs(topic);
if (!wcs) err(EX_DATAERR, "ambstowcs");
uiTopic(wcs);
free(wcs);
}
void uiLog(const wchar_t *line) {
@ -350,16 +349,10 @@ static void delete(void) {
static void enter(void) {
if (line.end == line.buf) return;
*line.end = L'\0';
const wchar_t *src = line.buf;
size_t len = wcsrtombs(NULL, &src, 0, NULL);
if (len == (size_t)-1) err(EX_DATAERR, "wcsrtombs");
char buf[1 + len];
len = wcsrtombs(buf, &src, sizeof(buf), NULL);
if (len == (size_t)-1) err(EX_DATAERR, "wcsrtombs");
input(buf);
char *str = awcstombs(line.buf);
if (!str) err(EX_DATAERR, "awcstombs");
input(str);
free(str);
line.ptr = line.buf;
line.end = line.buf;
}