move utf8 conversion routines to use nserror instead of their own error enum

This commit is contained in:
Vincent Sanders 2014-01-28 21:40:13 +00:00
parent 4b760c7e49
commit 654da2ffb5
37 changed files with 366 additions and 359 deletions

View File

@ -246,7 +246,7 @@ static void gui_set_clipboard(const char *buffer, size_t length,
if(nsoption_bool(clipboard_write_utf8)) { if(nsoption_bool(clipboard_write_utf8)) {
WriteChunkBytes(iffh, buffer, length); WriteChunkBytes(iffh, buffer, length);
} else { } else {
if(utf8_to_local_encoding(buffer, length, &text) == UTF8_CONVERT_OK) { if(utf8_to_local_encoding(buffer, length, &text) == NSERROR_OK) {
char *p; char *p;
p = text; p = text;

View File

@ -212,7 +212,7 @@ bool nsfont_position_in_string(const plot_font_style_t *fstyle,
ULONG emwidth = (ULONG)NSA_FONT_EMWIDTH(fstyle->size); ULONG emwidth = (ULONG)NSA_FONT_EMWIDTH(fstyle->size);
int32 tempx; int32 tempx;
if(utf8_to_enc(string,"UTF-16",length,(char **)&utf16) != UTF8_CONVERT_OK) return false; if(utf8_to_enc(string,"UTF-16",length,(char **)&utf16) != NSERROR_OK) return false;
outf16 = utf16; outf16 = utf16;
if(!(ofont = ami_open_outline_font(fstyle, 0))) return false; if(!(ofont = ami_open_outline_font(fstyle, 0))) return false;
@ -301,7 +301,7 @@ bool nsfont_split(const plot_font_style_t *fstyle,
int32 tempx = 0; int32 tempx = 0;
ULONG emwidth = (ULONG)NSA_FONT_EMWIDTH(fstyle->size); ULONG emwidth = (ULONG)NSA_FONT_EMWIDTH(fstyle->size);
if(utf8_to_enc((char *)string,"UTF-16",length,(char **)&utf16) != UTF8_CONVERT_OK) return false; if(utf8_to_enc((char *)string,"UTF-16",length,(char **)&utf16) != NSERROR_OK) return false;
outf16 = utf16; outf16 = utf16;
if(!(ofont = ami_open_outline_font(fstyle, 0))) return false; if(!(ofont = ami_open_outline_font(fstyle, 0))) return false;
@ -732,7 +732,7 @@ ULONG ami_unicode_text(struct RastPort *rp, const char *string, ULONG length,
if(!string || string[0]=='\0') return 0; if(!string || string[0]=='\0') return 0;
if(!length) return 0; if(!length) return 0;
if(utf8_to_enc(string,"UTF-16",length,(char **)&utf16) != UTF8_CONVERT_OK) return 0; if(utf8_to_enc(string,"UTF-16",length,(char **)&utf16) != NSERROR_OK) return 0;
outf16 = utf16; outf16 = utf16;
if(!(ofont = ami_open_outline_font(fstyle, 0))) return 0; if(!(ofont = ami_open_outline_font(fstyle, 0))) return 0;

View File

@ -34,7 +34,7 @@ char *ami_utf8_easy(const char *string)
{ {
char *localtext; char *localtext;
if(utf8_to_local_encoding(string,strlen(string),&localtext) == UTF8_CONVERT_OK) if(utf8_to_local_encoding(string,strlen(string),&localtext) == NSERROR_OK)
{ {
return localtext; return localtext;
} }
@ -48,7 +48,7 @@ char *ami_to_utf8_easy(const char *string)
{ {
char *localtext; char *localtext;
if(utf8_from_local_encoding(string,strlen(string),&localtext) == UTF8_CONVERT_OK) if(utf8_from_local_encoding(string,strlen(string),&localtext) == NSERROR_OK)
{ {
return localtext; return localtext;
} }
@ -58,8 +58,7 @@ char *ami_to_utf8_easy(const char *string)
} }
} }
utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len, nserror utf8_from_local_encoding(const char *string, size_t len, char **result)
char **result)
{ {
const char *encname = "ISO-8859-1"; const char *encname = "ISO-8859-1";
@ -73,8 +72,7 @@ utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len,
return utf8_from_enc(string,encname,len,result,NULL); return utf8_from_enc(string,encname,len,result,NULL);
} }
utf8_convert_ret utf8_to_local_encoding(const char *string, size_t len, nserror utf8_to_local_encoding(const char *string, size_t len, char **result)
char **result)
{ {
const char *encname = "ISO-8859-1"; const char *encname = "ISO-8859-1";

View File

@ -21,23 +21,21 @@
/* TODO: this need a rework..., encoding to atari st doesn|t always work. /* TODO: this need a rework..., encoding to atari st doesn|t always work.
( gui_add_to_clipboard...) */ ( gui_add_to_clipboard...) */
utf8_convert_ret utf8_to_local_encoding(const char *string, nserror utf8_to_local_encoding(const char *string,
size_t len, size_t len,
char **result) char **result)
{ {
utf8_convert_ret r; nserror r;
r = utf8_to_enc(string, "ATARIST", len, result); r = utf8_to_enc(string, "ATARIST", len, result);
if(r != UTF8_CONVERT_OK) { if(r != NSERROR_OK) {
r = utf8_to_enc(string, "UTF-8", len, result); r = utf8_to_enc(string, "UTF-8", len, result);
assert( r == UTF8_CONVERT_OK ); assert( r == NSERROR_OK );
} }
return r; return r;
} }
utf8_convert_ret utf8_from_local_encoding(const char *string, nserror utf8_from_local_encoding(const char *string, size_t len, char **result)
size_t len,
char **result)
{ {
return utf8_from_enc(string, "ATARIST", len, result, NULL); return utf8_from_enc(string, "ATARIST", len, result, NULL);
} }

View File

@ -326,7 +326,7 @@ static void gui_window_set_title(struct gui_window *gw, const char *title)
int l; int l;
char * conv; char * conv;
l = strlen(title)+1; l = strlen(title)+1;
if (utf8_to_local_encoding(title, l-1, &conv) == UTF8_CONVERT_OK ) { if (utf8_to_local_encoding(title, l-1, &conv) == NSERROR_OK ) {
l = MIN((uint32_t)atari_sysinfo.aes_max_win_title_len, strlen(conv)); l = MIN((uint32_t)atari_sysinfo.aes_max_win_title_len, strlen(conv));
if(gw->title == NULL) if(gw->title == NULL)
gw->title = malloc(l); gw->title = malloc(l);
@ -699,16 +699,16 @@ static void gui_get_clipboard(char **buffer, size_t *length)
// clipboard is in atari encoding, convert it to utf8: // clipboard is in atari encoding, convert it to utf8:
char *utf8 = NULL; char *utf8 = NULL;
utf8_convert_ret ret; nserror ret;
clip_len = strlen(clip); clip_len = strlen(clip);
if (clip_len > 0) { if (clip_len > 0) {
ret = utf8_to_local_encoding(clip, clip_len, &utf8); ret = utf8_to_local_encoding(clip, clip_len, &utf8);
if (ret == UTF8_CONVERT_OK && utf8 != NULL) { if (ret == NSERROR_OK && utf8 != NULL) {
*buffer = utf8; *buffer = utf8;
*length = strlen(utf8); *length = strlen(utf8);
} else { } else {
assert(ret == UTF8_CONVERT_OK && utf8 != NULL); assert(ret == NSERROR_OK && utf8 != NULL);
} }
} }
@ -731,14 +731,14 @@ static void gui_set_clipboard(const char *buffer, size_t length,
// convert utf8 input to atari encoding: // convert utf8 input to atari encoding:
utf8_convert_ret ret; nserror ret;
char *clip = NULL; char *clip = NULL;
ret = utf8_to_local_encoding(buffer,length, &clip); ret = utf8_to_local_encoding(buffer,length, &clip);
if (ret == UTF8_CONVERT_OK) { if (ret == NSERROR_OK) {
scrap_txt_write(clip); scrap_txt_write(clip);
} else { } else {
assert(ret == UTF8_CONVERT_OK); assert(ret == NSERROR_OK);
} }
free(clip); free(clip);
} }

View File

@ -72,7 +72,7 @@ fb_get_font(const plot_font_style_t *fstyle)
} }
} }
static utf8_convert_ret utf8_to_font_encoding(const struct fb_font_desc* font, static nserror utf8_to_font_encoding(const struct fb_font_desc* font,
const char *string, const char *string,
size_t len, size_t len,
char **result) char **result)

View File

@ -267,7 +267,7 @@ static int text( FONT_PLOTTER self, int x, int y, const char *text, size_t leng
short fx=0; short fx=0;
GRECT canvas; GRECT canvas;
char *lstr = NULL; char *lstr = NULL;
assert( utf8_to_local_encoding(text, length, &lstr) == UTF8_CONVERT_OK); assert( utf8_to_local_encoding(text, length, &lstr) == NSERROR_OK);
assert( lstr != NULL ); assert( lstr != NULL );
int slen = strlen(lstr); int slen = strlen(lstr);

View File

@ -1510,15 +1510,15 @@ static void on_file_dropped(ROOTWIN *rootwin, short msg[8])
mx+sx, my+sy, mx+sx, my+sy,
NULL); NULL);
if(processed == true) { if(processed == true) {
utf8_convert_ret ret; nserror ret;
char *utf8_fn; char *utf8_fn;
ret = utf8_from_local_encoding(buff, 0, &utf8_fn); ret = utf8_from_local_encoding(buff, 0, &utf8_fn);
if (ret != UTF8_CONVERT_OK) { if (ret != NSERROR_OK) {
free(buff); free(buff);
/* A bad encoding should never happen */ /* A bad encoding should never happen */
LOG(("utf8_from_local_encoding failed")); LOG(("utf8_from_local_encoding failed"));
assert(ret != UTF8_CONVERT_BADENC); assert(ret != NSERROR_BAD_ENCODING);
/* no memory */ /* no memory */
goto error; goto error;
} }

View File

@ -740,11 +740,11 @@ bool toolbar_key_input(struct s_toolbar *tb, short nkc)
int clip_length = strlen( clip ); int clip_length = strlen( clip );
if ( clip_length > 0 ) { if ( clip_length > 0 ) {
char *utf8; char *utf8;
utf8_convert_ret res; nserror res;
/* Clipboard is in local encoding so /* Clipboard is in local encoding so
* convert to UTF8 */ * convert to UTF8 */
res = utf8_from_local_encoding( clip, clip_length, &utf8 ); res = utf8_from_local_encoding( clip, clip_length, &utf8 );
if ( res == UTF8_CONVERT_OK ) { if ( res == NSERROR_OK ) {
toolbar_set_url(tb, utf8); toolbar_set_url(tb, utf8);
free(utf8); free(utf8);
ret = true; ret = true;

View File

@ -964,8 +964,7 @@ static void nsbeos_create_ssl_verify_window(struct browser_window *bw,
} }
utf8_convert_ret utf8_to_local_encoding(const char *string, size_t len, nserror utf8_to_local_encoding(const char *string, size_t len, char **result)
char **result)
{ {
assert(string && result); assert(string && result);
@ -974,13 +973,12 @@ utf8_convert_ret utf8_to_local_encoding(const char *string, size_t len,
*result = strndup(string, len); *result = strndup(string, len);
if (!(*result)) if (!(*result))
return UTF8_CONVERT_NOMEM; return NSERROR_NOMEM;
return UTF8_CONVERT_OK; return NSERROR_OK;
} }
utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len, nserror utf8_from_local_encoding(const char *string, size_t len, char **result)
char **result)
{ {
assert(string && result); assert(string && result);
@ -989,9 +987,9 @@ utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len,
*result = strndup(string, len); *result = strndup(string, len);
if (!(*result)) if (!(*result))
return UTF8_CONVERT_NOMEM; return NSERROR_NOMEM;
return UTF8_CONVERT_OK; return NSERROR_OK;
} }
static char *path_to_url(const char *path) static char *path_to_url(const char *path)

View File

@ -20,21 +20,20 @@
#import "utils/utf8.h" #import "utils/utf8.h"
utf8_convert_ret utf8_to_local_encoding(const char *string, size_t len, nserror utf8_to_local_encoding(const char *string, size_t len,
char **result) char **result)
{ {
NSCParameterAssert( NULL != result ); NSCParameterAssert( NULL != result );
char *newString = malloc( len + 1 ); char *newString = malloc( len + 1 );
if (NULL == newString) return UTF8_CONVERT_NOMEM; if (NULL == newString) return NSERROR_NOMEM;
memcpy( newString, string, len ); memcpy( newString, string, len );
newString[len] = 0; newString[len] = 0;
*result = newString; *result = newString;
return UTF8_CONVERT_OK; return NSERROR_OK;
} }
utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len, nserror utf8_from_local_encoding(const char *string, size_t len, char **result)
char **result)
{ {
/* same function, local encoding = UTF-8 */ /* same function, local encoding = UTF-8 */
return utf8_to_local_encoding( string, len, result ); return utf8_to_local_encoding( string, len, result );

View File

@ -828,21 +828,21 @@ static nserror global_history_export_enter_cb(void *ctx, void *node_data,
enum treeview_node_type type, bool *abort) enum treeview_node_type type, bool *abort)
{ {
struct treeview_export_walk_ctx *tw = ctx; struct treeview_export_walk_ctx *tw = ctx;
nserror ret;
if (type == TREE_NODE_ENTRY) { if (type == TREE_NODE_ENTRY) {
struct global_history_entry *e = node_data; struct global_history_entry *e = node_data;
utf8_convert_ret ret;
char *t_text; char *t_text;
char *u_text; char *u_text;
ret = utf8_to_html(e->data[GH_TITLE].value, "iso-8859-1", ret = utf8_to_html(e->data[GH_TITLE].value, "iso-8859-1",
e->data[GH_TITLE].value_len, &t_text); e->data[GH_TITLE].value_len, &t_text);
if (ret != UTF8_CONVERT_OK) if (ret != NSERROR_OK)
return NSERROR_SAVE_FAILED; return NSERROR_SAVE_FAILED;
ret = utf8_to_html(e->data[GH_URL].value, "iso-8859-1", ret = utf8_to_html(e->data[GH_URL].value, "iso-8859-1",
e->data[GH_URL].value_len, &u_text); e->data[GH_URL].value_len, &u_text);
if (ret != UTF8_CONVERT_OK) { if (ret != NSERROR_OK) {
free(t_text); free(t_text);
return NSERROR_SAVE_FAILED; return NSERROR_SAVE_FAILED;
} }
@ -855,12 +855,11 @@ static nserror global_history_export_enter_cb(void *ctx, void *node_data,
} else if (type == TREE_NODE_FOLDER) { } else if (type == TREE_NODE_FOLDER) {
struct global_history_folder *f = node_data; struct global_history_folder *f = node_data;
utf8_convert_ret ret;
char *f_text; char *f_text;
ret = utf8_to_html(f->data.value, "iso-8859-1", ret = utf8_to_html(f->data.value, "iso-8859-1",
f->data.value_len, &f_text); f->data.value_len, &f_text);
if (ret != UTF8_CONVERT_OK) if (ret != NSERROR_OK)
return NSERROR_SAVE_FAILED; return NSERROR_SAVE_FAILED;
fprintf(tw->fp, "<li><h4>%s</h4>\n<ul>\n", f_text); fprintf(tw->fp, "<li><h4>%s</h4>\n<ul>\n", f_text);

View File

@ -879,21 +879,21 @@ static nserror hotlist_export_enter_cb(void *ctx, void *node_data,
enum treeview_node_type type, bool *abort) enum treeview_node_type type, bool *abort)
{ {
struct treeview_export_walk_ctx *tw = ctx; struct treeview_export_walk_ctx *tw = ctx;
nserror ret;
if (type == TREE_NODE_ENTRY) { if (type == TREE_NODE_ENTRY) {
struct hotlist_entry *e = node_data; struct hotlist_entry *e = node_data;
utf8_convert_ret ret;
char *t_text; char *t_text;
char *u_text; char *u_text;
ret = utf8_to_html(e->data[HL_TITLE].value, "iso-8859-1", ret = utf8_to_html(e->data[HL_TITLE].value, "iso-8859-1",
e->data[HL_TITLE].value_len, &t_text); e->data[HL_TITLE].value_len, &t_text);
if (ret != UTF8_CONVERT_OK) if (ret != NSERROR_OK)
return NSERROR_SAVE_FAILED; return NSERROR_SAVE_FAILED;
ret = utf8_to_html(e->data[HL_URL].value, "iso-8859-1", ret = utf8_to_html(e->data[HL_URL].value, "iso-8859-1",
e->data[HL_URL].value_len, &u_text); e->data[HL_URL].value_len, &u_text);
if (ret != UTF8_CONVERT_OK) { if (ret != NSERROR_OK) {
free(t_text); free(t_text);
return NSERROR_SAVE_FAILED; return NSERROR_SAVE_FAILED;
} }
@ -906,12 +906,11 @@ static nserror hotlist_export_enter_cb(void *ctx, void *node_data,
} else if (type == TREE_NODE_FOLDER) { } else if (type == TREE_NODE_FOLDER) {
struct hotlist_folder *f = node_data; struct hotlist_folder *f = node_data;
utf8_convert_ret ret;
char *f_text; char *f_text;
ret = utf8_to_html(f->data.value, "iso-8859-1", ret = utf8_to_html(f->data.value, "iso-8859-1",
f->data.value_len, &f_text); f->data.value_len, &f_text);
if (ret != UTF8_CONVERT_OK) if (ret != NSERROR_OK)
return NSERROR_SAVE_FAILED; return NSERROR_SAVE_FAILED;
if (f == hl_ctx.default_folder) { if (f == hl_ctx.default_folder) {

View File

@ -547,7 +547,6 @@ static bool save_complete_rewrite_url_value(save_complete_ctx *ctx,
hlcache_handle *content; hlcache_handle *content;
char *escaped; char *escaped;
nserror error; nserror error;
utf8_convert_ret ret;
error = nsurl_join(ctx->base, value, &url); error = nsurl_join(ctx->base, value, &url);
if (error == NSERROR_NOMEM) if (error == NSERROR_NOMEM)
@ -562,11 +561,11 @@ static bool save_complete_rewrite_url_value(save_complete_ctx *ctx,
fprintf(ctx->fp, "\"%p\"", content); fprintf(ctx->fp, "\"%p\"", content);
} else { } else {
/* no match found */ /* no match found */
ret = utf8_to_html(nsurl_access(url), "UTF-8", error = utf8_to_html(nsurl_access(url), "UTF-8",
nsurl_length(url), &escaped); nsurl_length(url), &escaped);
nsurl_unref(url); nsurl_unref(url);
if (ret != UTF8_CONVERT_OK) if (error != NSERROR_OK)
return false; return false;
fprintf(ctx->fp, "\"%s\"", escaped); fprintf(ctx->fp, "\"%s\"", escaped);
@ -574,8 +573,8 @@ static bool save_complete_rewrite_url_value(save_complete_ctx *ctx,
free(escaped); free(escaped);
} }
} else { } else {
ret = utf8_to_html(value, "UTF-8", value_len, &escaped); error = utf8_to_html(value, "UTF-8", value_len, &escaped);
if (ret != UTF8_CONVERT_OK) if (error != NSERROR_OK)
return false; return false;
fprintf(ctx->fp, "\"%s\"", escaped); fprintf(ctx->fp, "\"%s\"", escaped);
@ -590,10 +589,10 @@ static bool save_complete_write_value(save_complete_ctx *ctx,
const char *value, size_t value_len) const char *value, size_t value_len)
{ {
char *escaped; char *escaped;
utf8_convert_ret ret; nserror ret;
ret = utf8_to_html(value, "UTF-8", value_len, &escaped); ret = utf8_to_html(value, "UTF-8", value_len, &escaped);
if (ret != UTF8_CONVERT_OK) if (ret != NSERROR_OK)
return false; return false;
fprintf(ctx->fp, "\"%s\"", escaped); fprintf(ctx->fp, "\"%s\"", escaped);
@ -919,7 +918,7 @@ static bool save_complete_node_handler(dom_node *node,
save_complete_ctx *ctx = ctxin; save_complete_ctx *ctx = ctxin;
dom_node_type type; dom_node_type type;
dom_exception error; dom_exception error;
utf8_convert_ret ret; nserror ret;
error = dom_node_get_node_type(node, &type); error = dom_node_get_node_type(node, &type);
if (error != DOM_NO_ERR) if (error != DOM_NO_ERR)
@ -953,7 +952,7 @@ static bool save_complete_node_handler(dom_node *node,
ret = utf8_to_html(text_data, "UTF-8", ret = utf8_to_html(text_data, "UTF-8",
text_len, &escaped); text_len, &escaped);
if (ret != UTF8_CONVERT_OK) if (ret != NSERROR_OK)
return false; return false;
fwrite(escaped, sizeof(*escaped), fwrite(escaped, sizeof(*escaped),

View File

@ -58,7 +58,7 @@ void save_as_text(hlcache_handle *c, char *path)
struct save_text_state save = { NULL, 0, 0 }; struct save_text_state save = { NULL, 0, 0 };
save_text_whitespace before = WHITESPACE_NONE; save_text_whitespace before = WHITESPACE_NONE;
bool first = true; bool first = true;
utf8_convert_ret ret; nserror ret;
char *result; char *result;
if (!c || content_get_type(c) != CONTENT_HTML) { if (!c || content_get_type(c) != CONTENT_HTML) {
@ -72,7 +72,7 @@ void save_as_text(hlcache_handle *c, char *path)
ret = utf8_to_local_encoding(save.block, save.length, &result); ret = utf8_to_local_encoding(save.block, save.length, &result);
free(save.block); free(save.block);
if (ret != UTF8_CONVERT_OK) { if (ret != NSERROR_OK) {
LOG(("failed to convert to local encoding, return %d", ret)); LOG(("failed to convert to local encoding, return %d", ret));
return; return;
} }

View File

@ -75,27 +75,27 @@ enum fb_face_e {
static fb_faceid_t *fb_faces[FB_FACE_COUNT]; static fb_faceid_t *fb_faces[FB_FACE_COUNT];
utf8_convert_ret utf8_to_local_encoding(const char *string, nserror utf8_to_local_encoding(const char *string,
size_t len, size_t len,
char **result) char **result)
{ {
return utf8_to_enc(string, "UTF-8", len, result); return utf8_to_enc(string, "UTF-8", len, result);
} }
utf8_convert_ret utf8_from_local_encoding(const char *string, nserror utf8_from_local_encoding(const char *string,
size_t len, size_t len,
char **result) char **result)
{ {
*result = malloc(len + 1); *result = malloc(len + 1);
if (*result == NULL) { if (*result == NULL) {
return UTF8_CONVERT_NOMEM; return NSERROR_NOMEM;
} }
memcpy(*result, string, len); memcpy(*result, string, len);
(*result)[len] = '\0'; (*result)[len] = '\0';
return UTF8_CONVERT_OK; return NSERROR_OK;
} }
/* map cache manager handle to face id */ /* map cache manager handle to face id */

View File

@ -59,7 +59,7 @@ fb_get_font(const plot_font_style_t *fstyle)
} }
} }
utf8_convert_ret utf8_to_font_encoding(const struct fb_font_desc* font, nserror_ret utf8_to_font_encoding(const struct fb_font_desc* font,
const char *string, const char *string,
size_t len, size_t len,
char **result) char **result)
@ -68,7 +68,7 @@ utf8_convert_ret utf8_to_font_encoding(const struct fb_font_desc* font,
} }
utf8_convert_ret utf8_to_local_encoding(const char *string, nserror utf8_to_local_encoding(const char *string,
size_t len, size_t len,
char **result) char **result)
{ {
@ -76,20 +76,20 @@ utf8_convert_ret utf8_to_local_encoding(const char *string,
} }
utf8_convert_ret utf8_from_local_encoding(const char *string, nserror utf8_from_local_encoding(const char *string,
size_t len, size_t len,
char **result) char **result)
{ {
*result = malloc(len + 1); *result = malloc(len + 1);
if (*result == NULL) { if (*result == NULL) {
return UTF8_CONVERT_NOMEM; return NSERROR_NOMEM;
} }
memcpy(*result, string, len); memcpy(*result, string, len);
(*result)[len] = '\0'; (*result)[len] = '\0';
return UTF8_CONVERT_OK; return NSERROR_OK;
} }
static bool nsfont_width(const plot_font_style_t *fstyle, static bool nsfont_width(const plot_font_style_t *fstyle,

View File

@ -33,7 +33,7 @@ extern const struct fb_font_desc font_italic_bold;
extern const struct fb_font_desc* fb_get_font(const plot_font_style_t *fstyle); extern const struct fb_font_desc* fb_get_font(const plot_font_style_t *fstyle);
extern utf8_convert_ret utf8_to_font_encoding(const struct fb_font_desc* font, extern nserror utf8_to_font_encoding(const struct fb_font_desc* font,
const char *string, const char *string,
size_t len, size_t len,
char **result); char **result);

View File

@ -169,16 +169,16 @@ void nsgtk_source_dialog_init(GtkWindow *parent, struct browser_window *bw)
source_data = content_get_source_data(bw->current_content, source_data = content_get_source_data(bw->current_content,
&source_size); &source_size);
utf8_convert_ret r = utf8_from_enc( nserror r = utf8_from_enc(
source_data, source_data,
html_get_encoding(bw->current_content), html_get_encoding(bw->current_content),
source_size, source_size,
&data, &data,
&data_len); &data_len);
if (r == UTF8_CONVERT_NOMEM) { if (r == NSERROR_NOMEM) {
warn_user("NoMemory",0); warn_user("NoMemory",0);
return; return;
} else if (r == UTF8_CONVERT_BADENC) { } else if (r == NSERROR_BAD_ENCODING) {
warn_user("EncNotRec",0); warn_user("EncNotRec",0);
return; return;
} }
@ -270,7 +270,7 @@ void nsgtk_source_tab_init(GtkWindow *parent, struct browser_window *bw)
size_t ndata_len; size_t ndata_len;
nsurl *url; nsurl *url;
nserror error; nserror error;
utf8_convert_ret r; nserror r;
gchar *filename; gchar *filename;
char *fileurl; char *fileurl;
gint handle; gint handle;
@ -283,10 +283,10 @@ void nsgtk_source_tab_init(GtkWindow *parent, struct browser_window *bw)
source_size, source_size,
&ndata, &ndata,
&ndata_len); &ndata_len);
if (r == UTF8_CONVERT_NOMEM) { if (r == NSERROR_NOMEM) {
warn_user("NoMemory",0); warn_user("NoMemory",0);
return; return;
} else if (r == UTF8_CONVERT_BADENC) { } else if (r == NSERROR_BAD_ENCODING) {
warn_user("EncNotRec",0); warn_user("EncNotRec",0);
return; return;
} }

View File

@ -788,8 +788,7 @@ gboolean nsgtk_ssl_delete_event(GtkWidget *w, GdkEvent *event, gpointer data)
return FALSE; return FALSE;
} }
utf8_convert_ret utf8_to_local_encoding(const char *string, size_t len, nserror utf8_to_local_encoding(const char *string, size_t len, char **result)
char **result)
{ {
assert(string && result); assert(string && result);
@ -798,14 +797,13 @@ utf8_convert_ret utf8_to_local_encoding(const char *string, size_t len,
*result = strndup(string, len); *result = strndup(string, len);
if (!(*result)) if (!(*result))
return UTF8_CONVERT_NOMEM; return NSERROR_NOMEM;
return UTF8_CONVERT_OK; return NSERROR_OK;
} }
utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len, nserror utf8_from_local_encoding(const char *string, size_t len, char **result)
char **result)
{ {
assert(string && result); assert(string && result);
@ -814,9 +812,9 @@ utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len,
*result = strndup(string, len); *result = strndup(string, len);
if (!(*result)) if (!(*result))
return UTF8_CONVERT_NOMEM; return NSERROR_NOMEM;
return UTF8_CONVERT_OK; return NSERROR_OK;
} }

View File

@ -38,20 +38,16 @@ void die(const char * const error)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
utf8_convert_ret nserror utf8_to_local_encoding(const char *string, size_t len, char **result)
utf8_to_local_encoding(const char *string, size_t len,
char **result)
{ {
*result = strndup(string, len); *result = strndup(string, len);
return (*result == NULL) ? UTF8_CONVERT_NOMEM : UTF8_CONVERT_OK; return (*result == NULL) ? NSERROR_NOMEM : NSERROR_OK;
} }
utf8_convert_ret nserror utf8_from_local_encoding(const char *string, size_t len, char **result)
utf8_from_local_encoding(const char *string, size_t len,
char **result)
{ {
*result = strndup(string, len); *result = strndup(string, len);
return (*result == NULL) ? UTF8_CONVERT_NOMEM : UTF8_CONVERT_OK; return (*result == NULL) ? NSERROR_NOMEM : NSERROR_OK;
} }

View File

@ -992,7 +992,7 @@ char *form_acceptable_charset(struct form *form)
char *form_encode_item(const char *item, uint32_t len, const char *charset, char *form_encode_item(const char *item, uint32_t len, const char *charset,
const char *fallback) const char *fallback)
{ {
utf8_convert_ret err; nserror err;
char *ret = NULL; char *ret = NULL;
char cset[256]; char cset[256];
@ -1002,19 +1002,19 @@ char *form_encode_item(const char *item, uint32_t len, const char *charset,
snprintf(cset, sizeof cset, "%s//TRANSLIT", charset); snprintf(cset, sizeof cset, "%s//TRANSLIT", charset);
err = utf8_to_enc(item, cset, 0, &ret); err = utf8_to_enc(item, cset, 0, &ret);
if (err == UTF8_CONVERT_BADENC) { if (err == NSERROR_BAD_ENCODING) {
/* charset not understood, try without transliteration */ /* charset not understood, try without transliteration */
snprintf(cset, sizeof cset, "%s", charset); snprintf(cset, sizeof cset, "%s", charset);
err = utf8_to_enc(item, cset, len, &ret); err = utf8_to_enc(item, cset, len, &ret);
if (err == UTF8_CONVERT_BADENC) { if (err == NSERROR_BAD_ENCODING) {
/* nope, try fallback charset (if any) */ /* nope, try fallback charset (if any) */
if (fallback) { if (fallback) {
snprintf(cset, sizeof cset, snprintf(cset, sizeof cset,
"%s//TRANSLIT", fallback); "%s//TRANSLIT", fallback);
err = utf8_to_enc(item, cset, 0, &ret); err = utf8_to_enc(item, cset, 0, &ret);
if (err == UTF8_CONVERT_BADENC) { if (err == NSERROR_BAD_ENCODING) {
/* and without transliteration */ /* and without transliteration */
snprintf(cset, sizeof cset, snprintf(cset, sizeof cset,
"%s", fallback); "%s", fallback);
@ -1022,11 +1022,11 @@ char *form_encode_item(const char *item, uint32_t len, const char *charset,
} }
} }
if (err == UTF8_CONVERT_BADENC) { if (err == NSERROR_BAD_ENCODING) {
/* that also failed, use 8859-1 */ /* that also failed, use 8859-1 */
err = utf8_to_enc(item, "ISO-8859-1//TRANSLIT", err = utf8_to_enc(item, "ISO-8859-1//TRANSLIT",
0, &ret); 0, &ret);
if (err == UTF8_CONVERT_BADENC) { if (err == NSERROR_BAD_ENCODING) {
/* and without transliteration */ /* and without transliteration */
err = utf8_to_enc(item, "ISO-8859-1", err = utf8_to_enc(item, "ISO-8859-1",
0, &ret); 0, &ret);
@ -1034,7 +1034,7 @@ char *form_encode_item(const char *item, uint32_t len, const char *charset,
} }
} }
} }
if (err == UTF8_CONVERT_NOMEM) { if (err == NSERROR_NOMEM) {
return NULL; return NULL;
} }

View File

@ -1765,14 +1765,14 @@ static void html__dom_user_data_handler(dom_node_operation operation,
static void html__set_file_gadget_filename(struct content *c, static void html__set_file_gadget_filename(struct content *c,
struct form_control *gadget, const char *fn) struct form_control *gadget, const char *fn)
{ {
utf8_convert_ret ret; nserror ret;
char *utf8_fn, *oldfile = NULL; char *utf8_fn, *oldfile = NULL;
html_content *html = (html_content *)c; html_content *html = (html_content *)c;
struct box *file_box = gadget->box; struct box *file_box = gadget->box;
ret = utf8_from_local_encoding(fn,0, &utf8_fn); ret = utf8_from_local_encoding(fn,0, &utf8_fn);
if (ret != UTF8_CONVERT_OK) { if (ret != NSERROR_OK) {
assert(ret != UTF8_CONVERT_BADENC); assert(ret != NSERROR_BAD_ENCODING);
LOG(("utf8_from_local_encoding failed")); LOG(("utf8_from_local_encoding failed"));
/* Load was for us - just no memory */ /* Load was for us - just no memory */
return; return;
@ -1874,7 +1874,7 @@ static bool html_drop_file_at_point(struct content *c, int x, int y, char *file)
FILE *fp = NULL; FILE *fp = NULL;
char *buffer; char *buffer;
char *utf8_buff; char *utf8_buff;
utf8_convert_ret ret; nserror ret;
unsigned int size; unsigned int size;
int bx, by; int bx, by;
@ -1916,9 +1916,9 @@ static bool html_drop_file_at_point(struct content *c, int x, int y, char *file)
/* Convert to UTF-8 */ /* Convert to UTF-8 */
ret = utf8_from_local_encoding(buffer, file_len, &utf8_buff); ret = utf8_from_local_encoding(buffer, file_len, &utf8_buff);
if (ret != UTF8_CONVERT_OK) { if (ret != NSERROR_OK) {
/* bad encoding shouldn't happen */ /* bad encoding shouldn't happen */
assert(ret != UTF8_CONVERT_BADENC); assert(ret != NSERROR_BAD_ENCODING);
LOG(("utf8_from_local_encoding failed")); LOG(("utf8_from_local_encoding failed"));
free(buffer); free(buffer);
warn_user("NoMemory", NULL); warn_user("NoMemory", NULL);

View File

@ -228,7 +228,7 @@ static struct gui_download_window *gui_download_window_create(download_context *
os_error *error; os_error *error;
url_func_result res; url_func_result res;
char *local_path; char *local_path;
utf8_convert_ret err; nserror err;
size_t i, last_dot; size_t i, last_dot;
dw = malloc(sizeof *dw); dw = malloc(sizeof *dw);
@ -376,9 +376,9 @@ static struct gui_download_window *gui_download_window_create(download_context *
filename); filename);
err = utf8_to_local_encoding(dw->path, 0, &local_path); err = utf8_to_local_encoding(dw->path, 0, &local_path);
if (err != UTF8_CONVERT_OK) { if (err != NSERROR_OK) {
/* badenc should never happen */ /* badenc should never happen */
assert(err != UTF8_CONVERT_BADENC); assert(err !=NSERROR_BAD_ENCODING);
LOG(("utf8_to_local_encoding failed")); LOG(("utf8_to_local_encoding failed"));
warn_user("NoMemory", 0); warn_user("NoMemory", 0);
free(dw); free(dw);
@ -588,7 +588,7 @@ void ro_gui_download_update_status(struct gui_download_window *dw)
os_error *error; os_error *error;
int width; int width;
char *local_status; char *local_status;
utf8_convert_ret err; nserror err;
gettimeofday(&t, 0); gettimeofday(&t, 0);
dt = (t.tv_sec + 0.000001 * t.tv_usec) - (dw->last_time.tv_sec + dt = (t.tv_sec + 0.000001 * t.tv_usec) - (dw->last_time.tv_sec +
@ -622,9 +622,9 @@ void ro_gui_download_update_status(struct gui_download_window *dw)
/* convert to local encoding */ /* convert to local encoding */
err = utf8_to_local_encoding( err = utf8_to_local_encoding(
messages_get("Download"), 0, &local_status); messages_get("Download"), 0, &local_status);
if (err != UTF8_CONVERT_OK) { if (err != NSERROR_OK) {
/* badenc should never happen */ /* badenc should never happen */
assert(err != UTF8_CONVERT_BADENC); assert(err != NSERROR_BAD_ENCODING);
/* hide nomem error */ /* hide nomem error */
snprintf(dw->status, sizeof dw->status, snprintf(dw->status, sizeof dw->status,
messages_get("Download"), messages_get("Download"),
@ -645,9 +645,9 @@ void ro_gui_download_update_status(struct gui_download_window *dw)
err = utf8_to_local_encoding( err = utf8_to_local_encoding(
messages_get("DownloadU"), 0, &local_status); messages_get("DownloadU"), 0, &local_status);
if (err != UTF8_CONVERT_OK) { if (err != NSERROR_OK) {
/* badenc should never happen */ /* badenc should never happen */
assert(err != UTF8_CONVERT_BADENC); assert(err != NSERROR_BAD_ENCODING);
/* hide nomem error */ /* hide nomem error */
snprintf(dw->status, sizeof dw->status, snprintf(dw->status, sizeof dw->status,
messages_get("DownloadU"), messages_get("DownloadU"),
@ -673,9 +673,9 @@ void ro_gui_download_update_status(struct gui_download_window *dw)
err = utf8_to_local_encoding(messages_get("Downloaded"), 0, err = utf8_to_local_encoding(messages_get("Downloaded"), 0,
&local_status); &local_status);
if (err != UTF8_CONVERT_OK) { if (err != NSERROR_OK) {
/* badenc should never happen */ /* badenc should never happen */
assert(err != UTF8_CONVERT_BADENC); assert(err != NSERROR_BAD_ENCODING);
/* hide nomem error */ /* hide nomem error */
snprintf(dw->status, sizeof dw->status, snprintf(dw->status, sizeof dw->status,
messages_get("Downloaded"), messages_get("Downloaded"),

View File

@ -956,12 +956,12 @@ void ro_gui_url_bar_set_url(struct url_bar *url_bar, const char *url,
*/ */
if (is_utf8) { if (is_utf8) {
utf8_convert_ret err; nserror err;
err = utf8_to_local_encoding(url, 0, &local_text); err = utf8_to_local_encoding(url, 0, &local_text);
if (err != UTF8_CONVERT_OK) { if (err != NSERROR_OK) {
/* A bad encoding should never happen, so assert this */ /* A bad encoding should never happen, so assert this */
assert(err != UTF8_CONVERT_BADENC); assert(err != NSERROR_BAD_ENCODING);
LOG(("utf8_to_enc failed")); LOG(("utf8_to_enc failed"));
/* Paranoia */ /* Paranoia */
local_text = NULL; local_text = NULL;

View File

@ -225,7 +225,7 @@ static void ro_gui_interactive_help_broadcast(wimp_message *message,
char *base_token; char *base_token;
char *local_token; char *local_token;
os_error *error; os_error *error;
utf8_convert_ret err; nserror err;
/* start off with an empty reply */ /* start off with an empty reply */
reply = (help_full_message_reply *)message; reply = (help_full_message_reply *)message;
@ -255,9 +255,9 @@ static void ro_gui_interactive_help_broadcast(wimp_message *message,
/* convert to local encoding */ /* convert to local encoding */
err = utf8_to_local_encoding(translated_token, 0, err = utf8_to_local_encoding(translated_token, 0,
&local_token); &local_token);
if (err != UTF8_CONVERT_OK) { if (err != NSERROR_OK) {
/* badenc should never happen */ /* badenc should never happen */
assert(err != UTF8_CONVERT_BADENC); assert(err != NSERROR_BAD_ENCODING);
/* simply use UTF-8 string */ /* simply use UTF-8 string */
strncpy(reply->reply, translated_token, 235); strncpy(reply->reply, translated_token, 235);
} }

View File

@ -875,8 +875,7 @@ bool ro_gui_menu_translate(struct menu_definition *menu)
int alphabet; int alphabet;
struct menu_definition_entry *entry; struct menu_definition_entry *entry;
char *translated; char *translated;
utf8_convert_ret err; nserror err;
/* read current alphabet */ /* read current alphabet */
error = xosbyte1(osbyte_ALPHABET_NUMBER, 127, 0, &alphabet); error = xosbyte1(osbyte_ALPHABET_NUMBER, 127, 0, &alphabet);
@ -895,8 +894,8 @@ bool ro_gui_menu_translate(struct menu_definition *menu)
free(menu->menu->title_data.indirected_text.text); free(menu->menu->title_data.indirected_text.text);
err = utf8_to_local_encoding(messages_get(menu->title_key), err = utf8_to_local_encoding(messages_get(menu->title_key),
0, &translated); 0, &translated);
if (err != UTF8_CONVERT_OK) { if (err != NSERROR_OK) {
assert(err != UTF8_CONVERT_BADENC); assert(err != NSERROR_BAD_ENCODING);
LOG(("utf8_to_enc failed")); LOG(("utf8_to_enc failed"));
return false; return false;
} }
@ -912,8 +911,8 @@ bool ro_gui_menu_translate(struct menu_definition *menu)
free(entry->menu_entry->data.indirected_text.text); free(entry->menu_entry->data.indirected_text.text);
err = utf8_to_local_encoding(messages_get(entry->entry_key), err = utf8_to_local_encoding(messages_get(entry->entry_key),
0, &translated); 0, &translated);
if (err != UTF8_CONVERT_OK) { if (err != NSERROR_OK) {
assert(err != UTF8_CONVERT_BADENC); assert(err != NSERROR_BAD_ENCODING);
LOG(("utf8_to_enc failed")); LOG(("utf8_to_enc failed"));
return false; return false;
} }

View File

@ -150,7 +150,7 @@ query_id query_user_xy(const char *query, const char *detail,
int len; int len;
int tx; int tx;
char *local_text = NULL; char *local_text = NULL;
utf8_convert_ret err; nserror err;
qw = malloc(sizeof(struct gui_query_window)); qw = malloc(sizeof(struct gui_query_window));
if (!qw) { if (!qw) {
@ -171,8 +171,8 @@ query_id query_user_xy(const char *query, const char *detail,
/* set the text of the 'Yes' button and size accordingly */ /* set the text of the 'Yes' button and size accordingly */
err = utf8_to_local_encoding(yes, 0, &local_text); err = utf8_to_local_encoding(yes, 0, &local_text);
if (err != UTF8_CONVERT_OK) { if (err != NSERROR_OK) {
assert(err != UTF8_CONVERT_BADENC); assert(err != NSERROR_BAD_ENCODING);
LOG(("utf8_to_local_encoding_failed")); LOG(("utf8_to_local_encoding_failed"));
local_text = NULL; local_text = NULL;
} }
@ -201,8 +201,8 @@ query_id query_user_xy(const char *query, const char *detail,
/* set the text of the 'No' button and size accordingly */ /* set the text of the 'No' button and size accordingly */
err = utf8_to_local_encoding(no, 0, &local_text); err = utf8_to_local_encoding(no, 0, &local_text);
if (err != UTF8_CONVERT_OK) { if (err != NSERROR_OK) {
assert(err != UTF8_CONVERT_BADENC); assert(err != NSERROR_BAD_ENCODING);
LOG(("utf8_to_local_encoding_failed")); LOG(("utf8_to_local_encoding_failed"));
local_text = NULL; local_text = NULL;
} }

View File

@ -614,7 +614,7 @@ static void ro_gui_save_drag_end(wimp_dragged *drag, void *data)
os_error *error; os_error *error;
char *dp, *ep; char *dp, *ep;
char *local_name = NULL; char *local_name = NULL;
utf8_convert_ret err; nserror err;
if (dragbox_active) if (dragbox_active)
ro_gui_drag_box_cancel(); ro_gui_drag_box_cancel();
@ -651,9 +651,9 @@ static void ro_gui_save_drag_end(wimp_dragged *drag, void *data)
/* saving directly from browser window, choose a /* saving directly from browser window, choose a
* name based upon the URL */ * name based upon the URL */
err = utf8_to_local_encoding(save_leafname, 0, &local_name); err = utf8_to_local_encoding(save_leafname, 0, &local_name);
if (err != UTF8_CONVERT_OK) { if (err != NSERROR_OK) {
/* badenc should never happen */ /* badenc should never happen */
assert(err != UTF8_CONVERT_BADENC); assert(err != NSERROR_BAD_ENCODING);
local_name = NULL; local_name = NULL;
} }
name = local_name ? local_name : save_leafname; name = local_name ? local_name : save_leafname;
@ -1216,7 +1216,7 @@ void ro_gui_save_set_state(hlcache_handle *h, gui_save_type save_type,
const char *name = gui_save_table[save_type].name; const char *name = gui_save_table[save_type].name;
bool done = false; bool done = false;
char *nice = NULL; char *nice = NULL;
utf8_convert_ret err; nserror err;
char *local_name; char *local_name;
size_t i; size_t i;
@ -1270,9 +1270,9 @@ void ro_gui_save_set_state(hlcache_handle *h, gui_save_type save_type,
leaf_buf[leaf_len - 1] = 0; leaf_buf[leaf_len - 1] = 0;
err = utf8_to_local_encoding(name, 0, &local_name); err = utf8_to_local_encoding(name, 0, &local_name);
if (err != UTF8_CONVERT_OK) { if (err != NSERROR_OK) {
/* badenc should never happen */ /* badenc should never happen */
assert(err != UTF8_CONVERT_BADENC); assert(err != NSERROR_BAD_ENCODING);
local_name = NULL; local_name = NULL;
} }

View File

@ -391,7 +391,7 @@ bool ro_gui_selection_prepare_paste_dataload(
FILE *fp; FILE *fp;
long size; long size;
char *local_cb; char *local_cb;
utf8_convert_ret ret; nserror ret;
/* Ignore messages that aren't for us */ /* Ignore messages that aren't for us */
if (dataxfer->your_ref == 0 || dataxfer->your_ref != paste_prev_message) if (dataxfer->your_ref == 0 || dataxfer->your_ref != paste_prev_message)
@ -410,7 +410,7 @@ bool ro_gui_selection_prepare_paste_dataload(
ret = utf8_from_local_encoding(local_cb, size, ret = utf8_from_local_encoding(local_cb, size,
&clipboard); &clipboard);
if (ret == UTF8_CONVERT_OK) { if (ret == NSERROR_OK) {
clip_length = strlen(clipboard); clip_length = strlen(clipboard);
} }
@ -509,13 +509,13 @@ void ro_gui_selection_data_request(wimp_full_message_data_request *req)
bool ro_gui_save_clipboard(const char *path) bool ro_gui_save_clipboard(const char *path)
{ {
char *local_cb; char *local_cb;
utf8_convert_ret ret; nserror ret;
os_error *error; os_error *error;
assert(clip_length > 0 && clipboard); assert(clip_length > 0 && clipboard);
ret = utf8_to_local_encoding(clipboard, clip_length, &local_cb); ret = utf8_to_local_encoding(clipboard, clip_length, &local_cb);
if (ret != UTF8_CONVERT_OK) { if (ret != NSERROR_OK) {
warn_user("SaveError", "Could not convert"); warn_user("SaveError", "Could not convert");
return false; return false;
} }

View File

@ -446,17 +446,16 @@ static const struct special {
* \param string The string to convert * \param string The string to convert
* \param len The length (in bytes) of the string, or 0 * \param len The length (in bytes) of the string, or 0
* \param result Pointer to location in which to store result * \param result Pointer to location in which to store result
* \return The appropriate utf8_convert_ret value * \return An nserror code
*/ */
utf8_convert_ret utf8_to_local_encoding(const char *string, size_t len, nserror utf8_to_local_encoding(const char *string, size_t len, char **result)
char **result)
{ {
os_error *error; os_error *error;
int alphabet, i; int alphabet, i;
size_t off, prev_off; size_t off, prev_off;
char *temp, *cur_pos; char *temp, *cur_pos;
const char *enc; const char *enc;
utf8_convert_ret err; nserror err;
assert(string); assert(string);
assert(result); assert(result);
@ -473,7 +472,7 @@ utf8_convert_ret utf8_to_local_encoding(const char *string, size_t len,
/* UTF-8 -> simply copy string */ /* UTF-8 -> simply copy string */
if (alphabet == 111 /* UTF-8 */) { if (alphabet == 111 /* UTF-8 */) {
*result = strndup(string, len); *result = strndup(string, len);
return UTF8_CONVERT_OK; return NSERROR_OK;
} }
/* get encoding name */ /* get encoding name */
@ -485,7 +484,7 @@ utf8_convert_ret utf8_to_local_encoding(const char *string, size_t len,
/* create output buffer */ /* create output buffer */
*(result) = malloc(len + 1); *(result) = malloc(len + 1);
if (!(*result)) if (!(*result))
return UTF8_CONVERT_NOMEM; return NSERROR_NOMEM;
*(*result) = '\0'; *(*result) = '\0';
prev_off = 0; prev_off = 0;
@ -508,10 +507,10 @@ utf8_convert_ret utf8_to_local_encoding(const char *string, size_t len,
if (off - prev_off > 0) { if (off - prev_off > 0) {
err = utf8_to_enc(string + prev_off, enc, err = utf8_to_enc(string + prev_off, enc,
off - prev_off, &temp); off - prev_off, &temp);
if (err != UTF8_CONVERT_OK) { if (err != NSERROR_OK) {
assert(err != UTF8_CONVERT_BADENC); assert(err != NSERROR_BAD_ENCODING);
free(*result); free(*result);
return UTF8_CONVERT_NOMEM; return NSERROR_NOMEM;
} }
strcat(cur_pos, temp); strcat(cur_pos, temp);
@ -533,10 +532,10 @@ utf8_convert_ret utf8_to_local_encoding(const char *string, size_t len,
if (prev_off < len) { if (prev_off < len) {
err = utf8_to_enc(string + prev_off, enc, len - prev_off, err = utf8_to_enc(string + prev_off, enc, len - prev_off,
&temp); &temp);
if (err != UTF8_CONVERT_OK) { if (err != NSERROR_OK) {
assert(err != UTF8_CONVERT_BADENC); assert(err != NSERROR_BAD_ENCODING);
free(*result); free(*result);
return UTF8_CONVERT_NOMEM; return NSERROR_NOMEM;
} }
strcat(cur_pos, temp); strcat(cur_pos, temp);
@ -544,7 +543,7 @@ utf8_convert_ret utf8_to_local_encoding(const char *string, size_t len,
free(temp); free(temp);
} }
return UTF8_CONVERT_OK; return NSERROR_OK;
} }
/** /**
@ -553,10 +552,9 @@ utf8_convert_ret utf8_to_local_encoding(const char *string, size_t len,
* \param string The string to convert * \param string The string to convert
* \param len The length (in bytes) of the string, or 0 * \param len The length (in bytes) of the string, or 0
* \param result Pointer to location in which to store result * \param result Pointer to location in which to store result
* \return The appropriate utf8_convert_ret value * \return An nserror code
*/ */
utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len, nserror utf8_from_local_encoding(const char *string, size_t len, char **result)
char **result)
{ {
os_error *error; os_error *error;
int alphabet, i, num_specials = 0, result_alloc; int alphabet, i, num_specials = 0, result_alloc;
@ -564,7 +562,7 @@ utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len,
size_t off, prev_off, cur_off; size_t off, prev_off, cur_off;
char *temp; char *temp;
const char *enc; const char *enc;
utf8_convert_ret err; nserror err;
assert(string && result); assert(string && result);
@ -581,10 +579,10 @@ utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len,
if (alphabet == 111 /* UTF-8 */) { if (alphabet == 111 /* UTF-8 */) {
temp = strndup(string, len); temp = strndup(string, len);
if (!temp) if (!temp)
return UTF8_CONVERT_NOMEM; return NSERROR_NOMEM;
*result = temp; *result = temp;
return UTF8_CONVERT_OK; return NSERROR_OK;
} }
/* get encoding name */ /* get encoding name */
@ -598,7 +596,7 @@ utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len,
*(result) = malloc(result_alloc); *(result) = malloc(result_alloc);
if (!(*result)) if (!(*result))
return UTF8_CONVERT_NOMEM; return NSERROR_NOMEM;
*(*result) = '\0'; *(*result) = '\0';
prev_off = 0; prev_off = 0;
@ -619,11 +617,11 @@ utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len,
if (off - prev_off > 0) { if (off - prev_off > 0) {
err = utf8_from_enc(string + prev_off, enc, err = utf8_from_enc(string + prev_off, enc,
off - prev_off, &temp, NULL); off - prev_off, &temp, NULL);
if (err != UTF8_CONVERT_OK) { if (err != NSERROR_OK) {
assert(err != UTF8_CONVERT_BADENC); assert(err != NSERROR_BAD_ENCODING);
LOG(("utf8_from_enc failed")); LOG(("utf8_from_enc failed"));
free(*result); free(*result);
return UTF8_CONVERT_NOMEM; return NSERROR_NOMEM;
} }
strcat((*result) + cur_off, temp); strcat((*result) + cur_off, temp);
@ -647,7 +645,7 @@ utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len,
(3 * SPECIAL_CHUNK_SIZE)); (3 * SPECIAL_CHUNK_SIZE));
if (!temp) { if (!temp) {
free(*result); free(*result);
return UTF8_CONVERT_NOMEM; return NSERRO_NOMEM;
} }
*result = temp; *result = temp;
@ -661,11 +659,11 @@ utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len,
if (prev_off < len) { if (prev_off < len) {
err = utf8_from_enc(string + prev_off, enc, len - prev_off, err = utf8_from_enc(string + prev_off, enc, len - prev_off,
&temp, NULL); &temp, NULL);
if (err != UTF8_CONVERT_OK) { if (err != NSERROR_OK) {
assert(err != UTF8_CONVERT_BADENC); assert(err != NSERROR_BAD_ENCODING);
LOG(("utf8_from_enc failed")); LOG(("utf8_from_enc failed"));
free(*result); free(*result);
return UTF8_CONVERT_NOMEM; return NSERROR_NOMEM;
} }
strcat((*result) + cur_off, temp); strcat((*result) + cur_off, temp);
@ -680,9 +678,9 @@ utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len,
if (!temp) { if (!temp) {
LOG(("realloc failed")); LOG(("realloc failed"));
free(*result); free(*result);
return UTF8_CONVERT_NOMEM; return NSERROR_NOMEM;
} }
*result = temp; *result = temp;
return UTF8_CONVERT_OK; return NSERROR_OK;
} }

View File

@ -273,12 +273,12 @@ void ro_gui_set_icon_string(wimp_w w, wimp_i i, const char *text, bool is_utf8)
unsigned int button_type; unsigned int button_type;
if (is_utf8) { if (is_utf8) {
utf8_convert_ret err; nserror err;
/* convert text to local encoding */ /* convert text to local encoding */
err = utf8_to_local_encoding(text, 0, &local_text); err = utf8_to_local_encoding(text, 0, &local_text);
if (err != UTF8_CONVERT_OK) { if (err != NSERROR_OK) {
/* A bad encoding should never happen, so assert this */ /* A bad encoding should never happen, so assert this */
assert(err != UTF8_CONVERT_BADENC); assert(err != NSERROR_BAD_ENCODING);
LOG(("utf8_to_enc failed")); LOG(("utf8_to_enc failed"));
/* Paranoia */ /* Paranoia */
local_text = NULL; local_text = NULL;
@ -677,7 +677,7 @@ void ro_gui_set_window_title(wimp_w w, const char *text)
wimp_window_info_base window; wimp_window_info_base window;
os_error *error; os_error *error;
char *title_local_enc; char *title_local_enc;
utf8_convert_ret err; nserror err;
/* Get the window details /* Get the window details
*/ */
@ -692,10 +692,10 @@ void ro_gui_set_window_title(wimp_w w, const char *text)
/* convert text to local encoding */ /* convert text to local encoding */
err = utf8_to_local_encoding(text, 0, &title_local_enc); err = utf8_to_local_encoding(text, 0, &title_local_enc);
if (err != UTF8_CONVERT_OK) { if (err != NSERROR_OK) {
/* A bad encoding should never happen, /* A bad encoding should never happen,
* so assert this */ * so assert this */
assert(err != UTF8_CONVERT_BADENC); assert(err != NSERROR_BAD_ENCODING);
LOG(("utf8_to_enc failed")); LOG(("utf8_to_enc failed"));
return; return;
} }

View File

@ -4621,7 +4621,7 @@ bool ro_gui_window_import_text(struct gui_window *g, const char *filename)
os_error *error; os_error *error;
char *buf, *utf8_buf, *sp; char *buf, *utf8_buf, *sp;
int size; int size;
utf8_convert_ret ret; nserror ret;
const char *ep; const char *ep;
char *p; char *p;
@ -4656,9 +4656,9 @@ bool ro_gui_window_import_text(struct gui_window *g, const char *filename)
} }
ret = utf8_from_local_encoding(buf, size, &utf8_buf); ret = utf8_from_local_encoding(buf, size, &utf8_buf);
if (ret != UTF8_CONVERT_OK) { if (ret != NSERROR_OK) {
/* bad encoding shouldn't happen */ /* bad encoding shouldn't happen */
assert(ret != UTF8_CONVERT_BADENC); assert(ret != NSERROR_BAD_ENCODING);
LOG(("utf8_from_local_encoding failed")); LOG(("utf8_from_local_encoding failed"));
free(buf); free(buf);
warn_user("NoMemory", NULL); warn_user("NoMemory", NULL);
@ -4802,7 +4802,7 @@ bool ro_gui_window_prepare_form_select_menu(struct browser_window *bw,
char *text_convert, *temp; char *text_convert, *temp;
struct form_option *option; struct form_option *option;
bool reopen = true; bool reopen = true;
utf8_convert_ret err; nserror err;
assert(control); assert(control);
@ -4839,9 +4839,9 @@ bool ro_gui_window_prepare_form_select_menu(struct browser_window *bw,
} }
err = utf8_to_local_encoding(messages_get("SelectMenu"), 0, err = utf8_to_local_encoding(messages_get("SelectMenu"), 0,
&text_convert); &text_convert);
if (err != UTF8_CONVERT_OK) { if (err != NSERROR_OK) {
/* badenc should never happen */ /* badenc should never happen */
assert(err != UTF8_CONVERT_BADENC); assert(err != NSERROR_BAD_ENCODING);
LOG(("utf8_to_local_encoding failed")); LOG(("utf8_to_local_encoding failed"));
warn_user("NoMemory", 0); warn_user("NoMemory", 0);
ro_gui_menu_closed(); ro_gui_menu_closed();
@ -4875,10 +4875,10 @@ bool ro_gui_window_prepare_form_select_menu(struct browser_window *bw,
err = utf8_to_local_encoding(temp, err = utf8_to_local_encoding(temp,
0, &text_convert); 0, &text_convert);
if (err != UTF8_CONVERT_OK) { if (err != NSERROR_OK) {
/* A bad encoding should never happen, /* A bad encoding should never happen,
* so assert this */ * so assert this */
assert(err != UTF8_CONVERT_BADENC); assert(err != NSERROR_BAD_ENCODING);
LOG(("utf8_to_enc failed")); LOG(("utf8_to_enc failed"));
warn_user("NoMemory", 0); warn_user("NoMemory", 0);
ro_gui_menu_closed(); ro_gui_menu_closed();

View File

@ -33,23 +33,14 @@
#include "utils/log.h" #include "utils/log.h"
#include "utils/utf8.h" #include "utils/utf8.h"
/** /* exported interface documented in utils/utf8.h */
* Convert a UTF-8 multibyte sequence into a single UCS4 character
*
* Encoding of UCS values outside the UTF-16 plane has been removed from
* RFC3629. This function conforms to RFC2279, however.
*
* \param s_in The sequence to process
* \param l Length of sequence
* \return UCS4 character
*/
uint32_t utf8_to_ucs4(const char *s_in, size_t l) uint32_t utf8_to_ucs4(const char *s_in, size_t l)
{ {
uint32_t ucs4; uint32_t ucs4;
size_t len; size_t len;
parserutils_error perror; parserutils_error perror;
perror = parserutils_charset_utf8_to_ucs4((const uint8_t *) s_in, l, perror = parserutils_charset_utf8_to_ucs4((const uint8_t *) s_in, l,
&ucs4, &len); &ucs4, &len);
if (perror != PARSERUTILS_OK) if (perror != PARSERUTILS_OK)
ucs4 = 0xfffd; ucs4 = 0xfffd;
@ -57,16 +48,7 @@ uint32_t utf8_to_ucs4(const char *s_in, size_t l)
return ucs4; return ucs4;
} }
/** /* exported interface documented in utils/utf8.h */
* Convert a single UCS4 character into a UTF-8 multibyte sequence
*
* Encoding of UCS values outside the UTF-16 plane has been removed from
* RFC3629. This function conforms to RFC2279, however.
*
* \param c The character to process (0 <= c <= 0x7FFFFFFF)
* \param s Pointer to 6 byte long output buffer
* \return Length of multibyte sequence
*/
size_t utf8_from_ucs4(uint32_t c, char *s) size_t utf8_from_ucs4(uint32_t c, char *s)
{ {
uint8_t *in = (uint8_t *) s; uint8_t *in = (uint8_t *) s;
@ -84,24 +66,13 @@ size_t utf8_from_ucs4(uint32_t c, char *s)
return 6 - len; return 6 - len;
} }
/** /* exported interface documented in utils/utf8.h */
* Calculate the length (in characters) of a NULL-terminated UTF-8 string
*
* \param s The string
* \return Length of string
*/
size_t utf8_length(const char *s) size_t utf8_length(const char *s)
{ {
return utf8_bounded_length(s, strlen(s)); return utf8_bounded_length(s, strlen(s));
} }
/** /* exported interface documented in utils/utf8.h */
* Calculated the length (in characters) of a bounded UTF-8 string
*
* \param s The string
* \param l Maximum length of input (in bytes)
* \return Length of string, in characters
*/
size_t utf8_bounded_length(const char *s, size_t l) size_t utf8_bounded_length(const char *s, size_t l)
{ {
size_t len; size_t len;
@ -114,14 +85,7 @@ size_t utf8_bounded_length(const char *s, size_t l)
return len; return len;
} }
/** /* exported interface documented in utils/utf8.h */
* Calculate the length (in bytes) of a bounded UTF-8 string
*
* \param s The string
* \param l Maximum length of input (in bytes)
* \param c Maximum number of characters to measure
* \return Length of string, in bytes
*/
size_t utf8_bounded_byte_length(const char *s, size_t l, size_t c) size_t utf8_bounded_byte_length(const char *s, size_t l, size_t c)
{ {
size_t len = 0; size_t len = 0;
@ -132,12 +96,7 @@ size_t utf8_bounded_byte_length(const char *s, size_t l, size_t c)
return len; return len;
} }
/** /* exported interface documented in utils/utf8.h */
* Calculate the length (in bytes) of a UTF-8 character
*
* \param s Pointer to start of character
* \return Length of character, in bytes
*/
size_t utf8_char_byte_length(const char *s) size_t utf8_char_byte_length(const char *s)
{ {
size_t len; size_t len;
@ -150,13 +109,7 @@ size_t utf8_char_byte_length(const char *s)
return len; return len;
} }
/** /* exported interface documented in utils/utf8.h */
* Find previous legal UTF-8 char in string
*
* \param s The string
* \param o Offset in the string to start at
* \return Offset of first byte of previous legal character
*/
size_t utf8_prev(const char *s, size_t o) size_t utf8_prev(const char *s, size_t o)
{ {
uint32_t prev; uint32_t prev;
@ -168,20 +121,13 @@ size_t utf8_prev(const char *s, size_t o)
return prev; return prev;
} }
/** /* exported interface documented in utils/utf8.h */
* Find next legal UTF-8 char in string
*
* \param s The string
* \param l Maximum offset in string
* \param o Offset in the string to start at
* \return Offset of first byte of next legal character
*/
size_t utf8_next(const char *s, size_t l, size_t o) size_t utf8_next(const char *s, size_t l, size_t o)
{ {
uint32_t next; uint32_t next;
parserutils_error perror; parserutils_error perror;
perror = parserutils_charset_utf8_next((const uint8_t *) s, l, o, perror = parserutils_charset_utf8_next((const uint8_t *) s, l, o,
&next); &next);
assert(perror == PARSERUTILS_OK); assert(perror == PARSERUTILS_OK);
@ -202,16 +148,16 @@ static inline void utf8_clear_cd_cache(void)
last_cd.cd = 0; last_cd.cd = 0;
} }
/** /* exported interface documented in utils/utf8.h */
* Finalise the UTF-8 library nserror utf8_finalise(void)
*/
void utf8_finalise(void)
{ {
if (last_cd.cd != 0) if (last_cd.cd != 0)
iconv_close(last_cd.cd); iconv_close(last_cd.cd);
/* paranoia follows */ /* paranoia follows */
utf8_clear_cd_cache(); utf8_clear_cd_cache();
return NSERROR_OK;
} }
@ -224,11 +170,16 @@ void utf8_finalise(void)
* \param to The encoding name to convert to * \param to The encoding name to convert to
* \param result Pointer to location in which to store result. * \param result Pointer to location in which to store result.
* \param result_len Pointer to location in which to store result length. * \param result_len Pointer to location in which to store result length.
* \return Appropriate utf8_convert_ret value * \return NSERROR_OK for no error, NSERROR_NOMEM on allocation error,
* NSERROR_BAD_ENCODING for a bad character encoding
*/ */
static utf8_convert_ret utf8_convert(const char *string, size_t len, static nserror
const char *from, const char *to, utf8_convert(const char *string,
char **result, size_t *result_len) size_t len,
const char *from,
const char *to,
char **result,
size_t *result_len)
{ {
iconv_t cd; iconv_t cd;
char *temp, *out, *in; char *temp, *out, *in;
@ -237,16 +188,16 @@ static utf8_convert_ret utf8_convert(const char *string, size_t len,
assert(string && from && to && result); assert(string && from && to && result);
if (string[0] == '\0') { if (string[0] == '\0') {
/* On AmigaOS, iconv() returns an error if we pass an /* On AmigaOS, iconv() returns an error if we pass an
* empty string. This prevents iconv() being called as * empty string. This prevents iconv() being called as
* there is no conversion necessary anyway. */ * there is no conversion necessary anyway. */
*result = strdup(""); *result = strdup("");
if (!(*result)) { if (!(*result)) {
*result = NULL; *result = NULL;
return UTF8_CONVERT_NOMEM; return NSERROR_NOMEM;
} }
return UTF8_CONVERT_OK; return NSERROR_OK;
} }
if (strcasecmp(from, to) == 0) { if (strcasecmp(from, to) == 0) {
@ -255,10 +206,10 @@ static utf8_convert_ret utf8_convert(const char *string, size_t len,
*(result) = strndup(string, slen); *(result) = strndup(string, slen);
if (!(*result)) { if (!(*result)) {
*(result) = NULL; *(result) = NULL;
return UTF8_CONVERT_NOMEM; return NSERROR_NOMEM;
} }
return UTF8_CONVERT_OK; return NSERROR_OK;
} }
in = (char *)string; in = (char *)string;
@ -274,9 +225,9 @@ static utf8_convert_ret utf8_convert(const char *string, size_t len,
cd = iconv_open(to, from); cd = iconv_open(to, from);
if (cd == (iconv_t)-1) { if (cd == (iconv_t)-1) {
if (errno == EINVAL) if (errno == EINVAL)
return UTF8_CONVERT_BADENC; return NSERROR_BAD_ENCODING;
/* default to no memory */ /* default to no memory */
return UTF8_CONVERT_NOMEM; return NSERROR_NOMEM;
} }
/* close the last cd - we don't care if this fails */ /* close the last cd - we don't care if this fails */
@ -297,8 +248,9 @@ static utf8_convert_ret utf8_convert(const char *string, size_t len,
rlen = slen * 4 + 4; rlen = slen * 4 + 4;
temp = out = malloc(rlen); temp = out = malloc(rlen);
if (!out) if (!out) {
return UTF8_CONVERT_NOMEM; return NSERROR_NOMEM;
}
/* perform conversion */ /* perform conversion */
if (iconv(cd, (void *) &in, &slen, &out, &rlen) == (size_t)-1) { if (iconv(cd, (void *) &in, &slen, &out, &rlen) == (size_t)-1) {
@ -312,14 +264,14 @@ static utf8_convert_ret utf8_convert(const char *string, size_t len,
* a) Insufficiently large output buffer * a) Insufficiently large output buffer
* b) Invalid input byte sequence * b) Invalid input byte sequence
* c) Incomplete input sequence */ * c) Incomplete input sequence */
return UTF8_CONVERT_NOMEM; return NSERROR_NOMEM;
} }
*(result) = realloc(temp, out - temp + 4); *(result) = realloc(temp, out - temp + 4);
if (!(*result)) { if (!(*result)) {
free(temp); free(temp);
*(result) = NULL; /* for sanity's sake */ *(result) = NULL; /* for sanity's sake */
return UTF8_CONVERT_NOMEM; return NSERROR_NOMEM;
} }
/* NULL terminate - needs 4 characters as we may have /* NULL terminate - needs 4 characters as we may have
@ -330,42 +282,32 @@ static utf8_convert_ret utf8_convert(const char *string, size_t len,
*result_len = (out - temp); *result_len = (out - temp);
} }
return UTF8_CONVERT_OK; return NSERROR_OK;
} }
/** /* exported interface documented in utils/utf8.h */
* Convert a UTF8 string into the named encoding nserror utf8_to_enc(const char *string, const char *encname,
*
* \param string The NULL-terminated string to convert
* \param encname The encoding name (suitable for passing to iconv)
* \param len Length of input string to consider (in bytes), or 0
* \param result Pointer to location to store result (allocated on heap)
* \return Appropriate utf8_convert_ret value
*/
utf8_convert_ret utf8_to_enc(const char *string, const char *encname,
size_t len, char **result) size_t len, char **result)
{ {
return utf8_convert(string, len, "UTF-8", encname, result, NULL); return utf8_convert(string, len, "UTF-8", encname, result, NULL);
} }
/** /* exported interface documented in utils/utf8.h */
* Convert a string in the named encoding into a UTF-8 string nserror utf8_from_enc(const char *string, const char *encname,
*
* \param string The NULL-terminated string to convert
* \param encname The encoding name (suitable for passing to iconv)
* \param len Length of input string to consider (in bytes), or 0
* \param result Pointer to location to store result (allocated on heap)
* \return Appropriate utf8_convert_ret value
*/
utf8_convert_ret utf8_from_enc(const char *string, const char *encname,
size_t len, char **result, size_t *result_len) size_t len, char **result, size_t *result_len)
{ {
return utf8_convert(string, len, encname, "UTF-8", result, result_len); return utf8_convert(string, len, encname, "UTF-8", result, result_len);
} }
static utf8_convert_ret utf8_convert_html_chunk(iconv_t cd, /**
const char *chunk, size_t inlen, * convert a chunk of html data
char **out, size_t *outlen) */
static nserror
utf8_convert_html_chunk(iconv_t cd,
const char *chunk,
size_t inlen,
char **out,
size_t *outlen)
{ {
size_t ret, esclen; size_t ret, esclen;
uint32_t ucs4; uint32_t ucs4;
@ -377,7 +319,7 @@ static utf8_convert_ret utf8_convert_html_chunk(iconv_t cd,
break; break;
if (errno != EILSEQ) if (errno != EILSEQ)
return UTF8_CONVERT_NOMEM; return NSERROR_NOMEM;
ucs4 = utf8_to_ucs4(chunk, inlen); ucs4 = utf8_to_ucs4(chunk, inlen);
esclen = snprintf(escape, sizeof(escape), "&#x%06x;", ucs4); esclen = snprintf(escape, sizeof(escape), "&#x%06x;", ucs4);
@ -385,34 +327,25 @@ static utf8_convert_ret utf8_convert_html_chunk(iconv_t cd,
ret = iconv(cd, (void *) &pescape, &esclen, ret = iconv(cd, (void *) &pescape, &esclen,
(void *) out, outlen); (void *) out, outlen);
if (ret == (size_t) -1) if (ret == (size_t) -1)
return UTF8_CONVERT_NOMEM; return NSERROR_NOMEM;
esclen = utf8_next(chunk, inlen, 0); esclen = utf8_next(chunk, inlen, 0);
chunk += esclen; chunk += esclen;
inlen -= esclen; inlen -= esclen;
} }
return UTF8_CONVERT_OK; return NSERROR_OK;
} }
/** /* exported interface documented in utils/utf8.h */
* Convert a UTF-8 encoded string into a string of the given encoding, nserror
* applying HTML escape sequences where necessary. utf8_to_html(const char *string, const char *encname, size_t len, char **result)
*
* \param string String to convert (NUL-terminated)
* \param encname Name of encoding to convert to
* \param len Length, in bytes, of the input string, or 0
* \param result Pointer to location to receive result
* \return Appropriate utf8_convert_ret value
*/
utf8_convert_ret utf8_to_html(const char *string, const char *encname,
size_t len, char **result)
{ {
iconv_t cd; iconv_t cd;
const char *in; const char *in;
char *out, *origout; char *out, *origout;
size_t off, prev_off, inlen, outlen, origoutlen, esclen; size_t off, prev_off, inlen, outlen, origoutlen, esclen;
utf8_convert_ret ret; nserror ret;
char *pescape, escape[11]; char *pescape, escape[11];
if (len == 0) if (len == 0)
@ -425,15 +358,14 @@ utf8_convert_ret utf8_to_html(const char *string, const char *encname,
sizeof(last_cd.to)) == 0 && sizeof(last_cd.to)) == 0 &&
last_cd.cd != 0) { last_cd.cd != 0) {
cd = last_cd.cd; cd = last_cd.cd;
} } else {
else {
/* no match, so create a new cd */ /* no match, so create a new cd */
cd = iconv_open(encname, "UTF-8"); cd = iconv_open(encname, "UTF-8");
if (cd == (iconv_t) -1) { if (cd == (iconv_t) -1) {
if (errno == EINVAL) if (errno == EINVAL)
return UTF8_CONVERT_BADENC; return NSERROR_BAD_ENCODING;
/* default to no memory */ /* default to no memory */
return UTF8_CONVERT_NOMEM; return NSERROR_NOMEM;
} }
/* close the last cd - we don't care if this fails */ /* close the last cd - we don't care if this fails */
@ -446,8 +378,8 @@ utf8_convert_ret utf8_to_html(const char *string, const char *encname,
last_cd.cd = cd; last_cd.cd = cd;
} }
/* Worst case is ASCII -> UCS4, with all characters escaped: /* Worst case is ASCII -> UCS4, with all characters escaped:
* "&#xYYYYYY;", thus each input character may become a string * "&#xYYYYYY;", thus each input character may become a string
* of 10 UCS4 characters, each 4 bytes in length, plus four for * of 10 UCS4 characters, each 4 bytes in length, plus four for
* terminating the string */ * terminating the string */
origoutlen = outlen = len * 10 * 4 + 4; origoutlen = outlen = len * 10 * 4 + 4;
@ -455,7 +387,7 @@ utf8_convert_ret utf8_to_html(const char *string, const char *encname,
if (out == NULL) { if (out == NULL) {
iconv_close(cd); iconv_close(cd);
utf8_clear_cd_cache(); utf8_clear_cd_cache();
return UTF8_CONVERT_NOMEM; return NSERROR_NOMEM;
} }
/* Process input in chunks between characters we must escape */ /* Process input in chunks between characters we must escape */
@ -470,7 +402,7 @@ utf8_convert_ret utf8_to_html(const char *string, const char *encname,
inlen = off - prev_off; inlen = off - prev_off;
ret = utf8_convert_html_chunk(cd, in, inlen, ret = utf8_convert_html_chunk(cd, in, inlen,
&out, &outlen); &out, &outlen);
if (ret != UTF8_CONVERT_OK) { if (ret != NSERROR_OK) {
free(origout); free(origout);
iconv_close(cd); iconv_close(cd);
utf8_clear_cd_cache(); utf8_clear_cd_cache();
@ -484,7 +416,7 @@ utf8_convert_ret utf8_to_html(const char *string, const char *encname,
pescape = escape; pescape = escape;
ret = utf8_convert_html_chunk(cd, pescape, esclen, ret = utf8_convert_html_chunk(cd, pescape, esclen,
&out, &outlen); &out, &outlen);
if (ret != UTF8_CONVERT_OK) { if (ret != NSERROR_OK) {
free(origout); free(origout);
iconv_close(cd); iconv_close(cd);
utf8_clear_cd_cache(); utf8_clear_cd_cache();
@ -502,7 +434,7 @@ utf8_convert_ret utf8_to_html(const char *string, const char *encname,
in = string + prev_off; in = string + prev_off;
inlen = len - prev_off; inlen = len - prev_off;
ret = utf8_convert_html_chunk(cd, in, inlen, &out, &outlen); ret = utf8_convert_html_chunk(cd, in, inlen, &out, &outlen);
if (ret != UTF8_CONVERT_OK) { if (ret != NSERROR_OK) {
free(origout); free(origout);
iconv_close(cd); iconv_close(cd);
utf8_clear_cd_cache(); utf8_clear_cd_cache();
@ -518,30 +450,22 @@ utf8_convert_ret utf8_to_html(const char *string, const char *encname,
*result = realloc(origout, origoutlen - outlen); *result = realloc(origout, origoutlen - outlen);
if (*result == NULL) { if (*result == NULL) {
free(origout); free(origout);
return UTF8_CONVERT_NOMEM; return NSERROR_NOMEM;
} }
return UTF8_CONVERT_OK; return NSERROR_OK;
} }
/* exported interface documented in utils/utf8.h */
/**
* Save the given utf8 text to a file, converting to local encoding.
*
* \param utf8_text text to save to file
* \param path pathname to save to
* \return true iff the save succeeded
*/
bool utf8_save_text(const char *utf8_text, const char *path) bool utf8_save_text(const char *utf8_text, const char *path)
{ {
utf8_convert_ret ret; nserror ret;
char *conv; char *conv;
FILE *out; FILE *out;
ret = utf8_to_local_encoding(utf8_text, strlen(utf8_text), &conv); ret = utf8_to_local_encoding(utf8_text, strlen(utf8_text), &conv);
if (ret != UTF8_CONVERT_OK) { if (ret != NSERROR_OK) {
LOG(("failed to convert to local encoding, return %d", ret)); LOG(("failed to convert to local encoding, return %d", ret));
return false; return false;
} }
@ -562,5 +486,3 @@ bool utf8_save_text(const char *utf8_text, const char *path)
return false; return false;
} }

View File

@ -26,40 +26,144 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
typedef enum { #include "utils/errors.h"
UTF8_CONVERT_OK,
UTF8_CONVERT_NOMEM,
UTF8_CONVERT_BADENC
} utf8_convert_ret;
/**
* Convert a UTF-8 multibyte sequence into a single UCS4 character
*
* Encoding of UCS values outside the UTF-16 plane has been removed from
* RFC3629. This function conforms to RFC2279, however.
*
* \param s_in The sequence to process
* \param l Length of sequence
* \return UCS4 character
*/
uint32_t utf8_to_ucs4(const char *s, size_t l); uint32_t utf8_to_ucs4(const char *s, size_t l);
/**
* Convert a single UCS4 character into a UTF-8 multibyte sequence
*
* Encoding of UCS values outside the UTF-16 plane has been removed from
* RFC3629. This function conforms to RFC2279, however.
*
* \param c The character to process (0 <= c <= 0x7FFFFFFF)
* \param s Pointer to 6 byte long output buffer
* \return Length of multibyte sequence
*/
size_t utf8_from_ucs4(uint32_t c, char *s); size_t utf8_from_ucs4(uint32_t c, char *s);
/**
* Calculate the length (in characters) of a NULL-terminated UTF-8 string
*
* \param s The string
* \return Length of string
*/
size_t utf8_length(const char *s); size_t utf8_length(const char *s);
/**
* Calculated the length (in characters) of a bounded UTF-8 string
*
* \param s The string
* \param l Maximum length of input (in bytes)
* \return Length of string, in characters
*/
size_t utf8_bounded_length(const char *s, size_t l); size_t utf8_bounded_length(const char *s, size_t l);
/**
* Calculate the length (in bytes) of a bounded UTF-8 string
*
* \param s The string
* \param l Maximum length of input (in bytes)
* \param c Maximum number of characters to measure
* \return Length of string, in bytes
*/
size_t utf8_bounded_byte_length(const char *s, size_t l, size_t c); size_t utf8_bounded_byte_length(const char *s, size_t l, size_t c);
/**
* Calculate the length (in bytes) of a UTF-8 character
*
* \param s Pointer to start of character
* \return Length of character, in bytes
*/
size_t utf8_char_byte_length(const char *s); size_t utf8_char_byte_length(const char *s);
/**
* Find previous legal UTF-8 char in string
*
* \param s The string
* \param o Offset in the string to start at
* \return Offset of first byte of previous legal character
*/
size_t utf8_prev(const char *s, size_t o); size_t utf8_prev(const char *s, size_t o);
/**
* Find next legal UTF-8 char in string
*
* \param s The string
* \param l Maximum offset in string
* \param o Offset in the string to start at
* \return Offset of first byte of next legal character
*/
size_t utf8_next(const char *s, size_t l, size_t o); size_t utf8_next(const char *s, size_t l, size_t o);
utf8_convert_ret utf8_to_enc(const char *string, const char *encname,
/**
* Convert a UTF8 string into the named encoding
*
* \param string The NULL-terminated string to convert
* \param encname The encoding name (suitable for passing to iconv)
* \param len Length of input string to consider (in bytes), or 0
* \param result Pointer to location to store result (allocated on heap)
* \return standard nserror value
*/
nserror utf8_to_enc(const char *string, const char *encname,
size_t len, char **result); size_t len, char **result);
utf8_convert_ret utf8_from_enc(const char *string, const char *encname,
/**
* Convert a string in the named encoding into a UTF-8 string
*
* \param string The NULL-terminated string to convert
* \param encname The encoding name (suitable for passing to iconv)
* \param len Length of input string to consider (in bytes), or 0
* \param result Pointer to location to store result (allocated on heap)
* \return standard nserror value
*/
nserror utf8_from_enc(const char *string, const char *encname,
size_t len, char **result, size_t *result_len); size_t len, char **result, size_t *result_len);
utf8_convert_ret utf8_to_html(const char *string, const char *encname, /**
* Convert a UTF-8 encoded string into a string of the given encoding,
* applying HTML escape sequences where necessary.
*
* \param string String to convert (NUL-terminated)
* \param encname Name of encoding to convert to
* \param len Length, in bytes, of the input string, or 0
* \param result Pointer to location to receive result
* \return standard nserror code
*/
nserror utf8_to_html(const char *string, const char *encname,
size_t len, char **result); size_t len, char **result);
/**
* Save the given utf8 text to a file, converting to local encoding.
*
* \param utf8_text text to save to file
* \param path pathname to save to
* \return true iff the save succeeded
*/
bool utf8_save_text(const char *utf8_text, const char *path); bool utf8_save_text(const char *utf8_text, const char *path);
/* These two are platform specific */
utf8_convert_ret utf8_to_local_encoding(const char *string, size_t len,
char **result);
utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len,
char **result);
void utf8_finalise(void); /**
* Finalise the UTF-8 library
*/
nserror utf8_finalise(void);
/* These two are platform specific */
nserror utf8_to_local_encoding(const char *string, size_t len, char **result);
nserror utf8_from_local_encoding(const char *string, size_t len, char **result);
#endif #endif

View File

@ -35,7 +35,7 @@
#include "windows/gui.h" #include "windows/gui.h"
#include "windows/plot.h" #include "windows/plot.h"
utf8_convert_ret utf8_to_font_encoding(const struct font_desc* font, nserror utf8_to_font_encoding(const struct font_desc* font,
const char *string, const char *string,
size_t len, size_t len,
char **result) char **result)
@ -43,14 +43,14 @@ utf8_convert_ret utf8_to_font_encoding(const struct font_desc* font,
return utf8_to_enc(string, font->encoding, len, result); return utf8_to_enc(string, font->encoding, len, result);
} }
utf8_convert_ret utf8_to_local_encoding(const char *string, nserror utf8_to_local_encoding(const char *string,
size_t len, size_t len,
char **result) char **result)
{ {
return utf8_to_enc(string, "UCS-2", len, result); return utf8_to_enc(string, "UCS-2", len, result);
} }
utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len, nserror utf8_from_local_encoding(const char *string, size_t len,
char **result) char **result)
{ {
assert(string && result); assert(string && result);
@ -60,9 +60,9 @@ utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len,
*result = strndup(string, len); *result = strndup(string, len);
if (!(*result)) if (!(*result))
return UTF8_CONVERT_NOMEM; return NSERROR_NOMEM;
return UTF8_CONVERT_OK; return NSERROR_OK;
} }
HFONT get_font(const plot_font_style_t *style) HFONT get_font(const plot_font_style_t *style)

View File

@ -29,7 +29,7 @@ struct font_desc {
const char *encoding; const char *encoding;
}; };
extern utf8_convert_ret utf8_to_font_encoding(const struct font_desc* font, extern nserror utf8_to_font_encoding(const struct font_desc* font,
const char *string, const char *string,
size_t len, size_t len,
char **result); char **result);