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)) {
WriteChunkBytes(iffh, buffer, length);
} else {
if(utf8_to_local_encoding(buffer, length, &text) == UTF8_CONVERT_OK) {
if(utf8_to_local_encoding(buffer, length, &text) == NSERROR_OK) {
char *p;
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);
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;
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;
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;
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(!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;
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;
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;
}
@ -48,7 +48,7 @@ char *ami_to_utf8_easy(const char *string)
{
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;
}
@ -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,
char **result)
nserror utf8_from_local_encoding(const char *string, size_t len, char **result)
{
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);
}
utf8_convert_ret utf8_to_local_encoding(const char *string, size_t len,
char **result)
nserror utf8_to_local_encoding(const char *string, size_t len, char **result)
{
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.
( 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,
char **result)
{
utf8_convert_ret r;
nserror r;
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);
assert( r == UTF8_CONVERT_OK );
assert( r == NSERROR_OK );
}
return r;
}
utf8_convert_ret utf8_from_local_encoding(const char *string,
size_t len,
char **result)
nserror utf8_from_local_encoding(const char *string, size_t len, char **result)
{
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;
char * conv;
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));
if(gw->title == NULL)
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:
char *utf8 = NULL;
utf8_convert_ret ret;
nserror ret;
clip_len = strlen(clip);
if (clip_len > 0) {
ret = utf8_to_local_encoding(clip, clip_len, &utf8);
if (ret == UTF8_CONVERT_OK && utf8 != NULL) {
if (ret == NSERROR_OK && utf8 != NULL) {
*buffer = utf8;
*length = strlen(utf8);
} 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:
utf8_convert_ret ret;
nserror ret;
char *clip = NULL;
ret = utf8_to_local_encoding(buffer,length, &clip);
if (ret == UTF8_CONVERT_OK) {
if (ret == NSERROR_OK) {
scrap_txt_write(clip);
} else {
assert(ret == UTF8_CONVERT_OK);
assert(ret == NSERROR_OK);
}
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,
size_t len,
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;
GRECT canvas;
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 );
int slen = strlen(lstr);

View File

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

View File

@ -740,11 +740,11 @@ bool toolbar_key_input(struct s_toolbar *tb, short nkc)
int clip_length = strlen( clip );
if ( clip_length > 0 ) {
char *utf8;
utf8_convert_ret res;
nserror res;
/* Clipboard is in local encoding so
* convert to UTF8 */
res = utf8_from_local_encoding( clip, clip_length, &utf8 );
if ( res == UTF8_CONVERT_OK ) {
if ( res == NSERROR_OK ) {
toolbar_set_url(tb, utf8);
free(utf8);
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,
char **result)
nserror utf8_to_local_encoding(const char *string, size_t len, char **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);
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,
char **result)
nserror utf8_from_local_encoding(const char *string, size_t len, char **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);
if (!(*result))
return UTF8_CONVERT_NOMEM;
return NSERROR_NOMEM;
return UTF8_CONVERT_OK;
return NSERROR_OK;
}
static char *path_to_url(const char *path)

View File

@ -20,21 +20,20 @@
#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)
{
NSCParameterAssert( NULL != result );
char *newString = malloc( len + 1 );
if (NULL == newString) return UTF8_CONVERT_NOMEM;
if (NULL == newString) return NSERROR_NOMEM;
memcpy( newString, string, len );
newString[len] = 0;
*result = newString;
return UTF8_CONVERT_OK;
return NSERROR_OK;
}
utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len,
char **result)
nserror utf8_from_local_encoding(const char *string, size_t len, char **result)
{
/* same function, local encoding = UTF-8 */
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)
{
struct treeview_export_walk_ctx *tw = ctx;
nserror ret;
if (type == TREE_NODE_ENTRY) {
struct global_history_entry *e = node_data;
utf8_convert_ret ret;
char *t_text;
char *u_text;
ret = utf8_to_html(e->data[GH_TITLE].value, "iso-8859-1",
e->data[GH_TITLE].value_len, &t_text);
if (ret != UTF8_CONVERT_OK)
if (ret != NSERROR_OK)
return NSERROR_SAVE_FAILED;
ret = utf8_to_html(e->data[GH_URL].value, "iso-8859-1",
e->data[GH_URL].value_len, &u_text);
if (ret != UTF8_CONVERT_OK) {
if (ret != NSERROR_OK) {
free(t_text);
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) {
struct global_history_folder *f = node_data;
utf8_convert_ret ret;
char *f_text;
ret = utf8_to_html(f->data.value, "iso-8859-1",
f->data.value_len, &f_text);
if (ret != UTF8_CONVERT_OK)
if (ret != NSERROR_OK)
return NSERROR_SAVE_FAILED;
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)
{
struct treeview_export_walk_ctx *tw = ctx;
nserror ret;
if (type == TREE_NODE_ENTRY) {
struct hotlist_entry *e = node_data;
utf8_convert_ret ret;
char *t_text;
char *u_text;
ret = utf8_to_html(e->data[HL_TITLE].value, "iso-8859-1",
e->data[HL_TITLE].value_len, &t_text);
if (ret != UTF8_CONVERT_OK)
if (ret != NSERROR_OK)
return NSERROR_SAVE_FAILED;
ret = utf8_to_html(e->data[HL_URL].value, "iso-8859-1",
e->data[HL_URL].value_len, &u_text);
if (ret != UTF8_CONVERT_OK) {
if (ret != NSERROR_OK) {
free(t_text);
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) {
struct hotlist_folder *f = node_data;
utf8_convert_ret ret;
char *f_text;
ret = utf8_to_html(f->data.value, "iso-8859-1",
f->data.value_len, &f_text);
if (ret != UTF8_CONVERT_OK)
if (ret != NSERROR_OK)
return NSERROR_SAVE_FAILED;
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;
char *escaped;
nserror error;
utf8_convert_ret ret;
error = nsurl_join(ctx->base, value, &url);
if (error == NSERROR_NOMEM)
@ -562,11 +561,11 @@ static bool save_complete_rewrite_url_value(save_complete_ctx *ctx,
fprintf(ctx->fp, "\"%p\"", content);
} else {
/* 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_unref(url);
if (ret != UTF8_CONVERT_OK)
if (error != NSERROR_OK)
return false;
fprintf(ctx->fp, "\"%s\"", escaped);
@ -574,8 +573,8 @@ static bool save_complete_rewrite_url_value(save_complete_ctx *ctx,
free(escaped);
}
} else {
ret = utf8_to_html(value, "UTF-8", value_len, &escaped);
if (ret != UTF8_CONVERT_OK)
error = utf8_to_html(value, "UTF-8", value_len, &escaped);
if (error != NSERROR_OK)
return false;
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)
{
char *escaped;
utf8_convert_ret ret;
nserror ret;
ret = utf8_to_html(value, "UTF-8", value_len, &escaped);
if (ret != UTF8_CONVERT_OK)
if (ret != NSERROR_OK)
return false;
fprintf(ctx->fp, "\"%s\"", escaped);
@ -919,7 +918,7 @@ static bool save_complete_node_handler(dom_node *node,
save_complete_ctx *ctx = ctxin;
dom_node_type type;
dom_exception error;
utf8_convert_ret ret;
nserror ret;
error = dom_node_get_node_type(node, &type);
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",
text_len, &escaped);
if (ret != UTF8_CONVERT_OK)
if (ret != NSERROR_OK)
return false;
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 };
save_text_whitespace before = WHITESPACE_NONE;
bool first = true;
utf8_convert_ret ret;
nserror ret;
char *result;
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);
free(save.block);
if (ret != UTF8_CONVERT_OK) {
if (ret != NSERROR_OK) {
LOG(("failed to convert to local encoding, return %d", ret));
return;
}

View File

@ -75,27 +75,27 @@ enum fb_face_e {
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,
char **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,
char **result)
{
*result = malloc(len + 1);
if (*result == NULL) {
return UTF8_CONVERT_NOMEM;
return NSERROR_NOMEM;
}
memcpy(*result, string, len);
(*result)[len] = '\0';
return UTF8_CONVERT_OK;
return NSERROR_OK;
}
/* 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,
size_t len,
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,
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,
char **result)
{
*result = malloc(len + 1);
if (*result == NULL) {
return UTF8_CONVERT_NOMEM;
return NSERROR_NOMEM;
}
memcpy(*result, string, len);
(*result)[len] = '\0';
return UTF8_CONVERT_OK;
return NSERROR_OK;
}
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 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,
size_t len,
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_size);
utf8_convert_ret r = utf8_from_enc(
nserror r = utf8_from_enc(
source_data,
html_get_encoding(bw->current_content),
source_size,
&data,
&data_len);
if (r == UTF8_CONVERT_NOMEM) {
if (r == NSERROR_NOMEM) {
warn_user("NoMemory",0);
return;
} else if (r == UTF8_CONVERT_BADENC) {
} else if (r == NSERROR_BAD_ENCODING) {
warn_user("EncNotRec",0);
return;
}
@ -270,7 +270,7 @@ void nsgtk_source_tab_init(GtkWindow *parent, struct browser_window *bw)
size_t ndata_len;
nsurl *url;
nserror error;
utf8_convert_ret r;
nserror r;
gchar *filename;
char *fileurl;
gint handle;
@ -283,10 +283,10 @@ void nsgtk_source_tab_init(GtkWindow *parent, struct browser_window *bw)
source_size,
&ndata,
&ndata_len);
if (r == UTF8_CONVERT_NOMEM) {
if (r == NSERROR_NOMEM) {
warn_user("NoMemory",0);
return;
} else if (r == UTF8_CONVERT_BADENC) {
} else if (r == NSERROR_BAD_ENCODING) {
warn_user("EncNotRec",0);
return;
}

View File

@ -788,8 +788,7 @@ gboolean nsgtk_ssl_delete_event(GtkWidget *w, GdkEvent *event, gpointer data)
return FALSE;
}
utf8_convert_ret utf8_to_local_encoding(const char *string, size_t len,
char **result)
nserror utf8_to_local_encoding(const char *string, size_t len, char **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);
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,
char **result)
nserror utf8_from_local_encoding(const char *string, size_t len, char **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);
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);
}
utf8_convert_ret
utf8_to_local_encoding(const char *string, size_t len,
char **result)
nserror utf8_to_local_encoding(const char *string, size_t len, char **result)
{
*result = strndup(string, len);
return (*result == NULL) ? UTF8_CONVERT_NOMEM : UTF8_CONVERT_OK;
return (*result == NULL) ? NSERROR_NOMEM : NSERROR_OK;
}
utf8_convert_ret
utf8_from_local_encoding(const char *string, size_t len,
char **result)
nserror utf8_from_local_encoding(const char *string, size_t len, char **result)
{
*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,
const char *fallback)
{
utf8_convert_ret err;
nserror err;
char *ret = NULL;
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);
err = utf8_to_enc(item, cset, 0, &ret);
if (err == UTF8_CONVERT_BADENC) {
if (err == NSERROR_BAD_ENCODING) {
/* charset not understood, try without transliteration */
snprintf(cset, sizeof cset, "%s", charset);
err = utf8_to_enc(item, cset, len, &ret);
if (err == UTF8_CONVERT_BADENC) {
if (err == NSERROR_BAD_ENCODING) {
/* nope, try fallback charset (if any) */
if (fallback) {
snprintf(cset, sizeof cset,
"%s//TRANSLIT", fallback);
err = utf8_to_enc(item, cset, 0, &ret);
if (err == UTF8_CONVERT_BADENC) {
if (err == NSERROR_BAD_ENCODING) {
/* and without transliteration */
snprintf(cset, sizeof cset,
"%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 */
err = utf8_to_enc(item, "ISO-8859-1//TRANSLIT",
0, &ret);
if (err == UTF8_CONVERT_BADENC) {
if (err == NSERROR_BAD_ENCODING) {
/* and without transliteration */
err = utf8_to_enc(item, "ISO-8859-1",
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;
}

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,
struct form_control *gadget, const char *fn)
{
utf8_convert_ret ret;
nserror ret;
char *utf8_fn, *oldfile = NULL;
html_content *html = (html_content *)c;
struct box *file_box = gadget->box;
ret = utf8_from_local_encoding(fn,0, &utf8_fn);
if (ret != UTF8_CONVERT_OK) {
assert(ret != UTF8_CONVERT_BADENC);
if (ret != NSERROR_OK) {
assert(ret != NSERROR_BAD_ENCODING);
LOG(("utf8_from_local_encoding failed"));
/* Load was for us - just no memory */
return;
@ -1874,7 +1874,7 @@ static bool html_drop_file_at_point(struct content *c, int x, int y, char *file)
FILE *fp = NULL;
char *buffer;
char *utf8_buff;
utf8_convert_ret ret;
nserror ret;
unsigned int size;
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 */
ret = utf8_from_local_encoding(buffer, file_len, &utf8_buff);
if (ret != UTF8_CONVERT_OK) {
if (ret != NSERROR_OK) {
/* bad encoding shouldn't happen */
assert(ret != UTF8_CONVERT_BADENC);
assert(ret != NSERROR_BAD_ENCODING);
LOG(("utf8_from_local_encoding failed"));
free(buffer);
warn_user("NoMemory", NULL);

View File

@ -228,7 +228,7 @@ static struct gui_download_window *gui_download_window_create(download_context *
os_error *error;
url_func_result res;
char *local_path;
utf8_convert_ret err;
nserror err;
size_t i, last_dot;
dw = malloc(sizeof *dw);
@ -376,9 +376,9 @@ static struct gui_download_window *gui_download_window_create(download_context *
filename);
err = utf8_to_local_encoding(dw->path, 0, &local_path);
if (err != UTF8_CONVERT_OK) {
if (err != NSERROR_OK) {
/* badenc should never happen */
assert(err != UTF8_CONVERT_BADENC);
assert(err !=NSERROR_BAD_ENCODING);
LOG(("utf8_to_local_encoding failed"));
warn_user("NoMemory", 0);
free(dw);
@ -588,7 +588,7 @@ void ro_gui_download_update_status(struct gui_download_window *dw)
os_error *error;
int width;
char *local_status;
utf8_convert_ret err;
nserror err;
gettimeofday(&t, 0);
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 */
err = utf8_to_local_encoding(
messages_get("Download"), 0, &local_status);
if (err != UTF8_CONVERT_OK) {
if (err != NSERROR_OK) {
/* badenc should never happen */
assert(err != UTF8_CONVERT_BADENC);
assert(err != NSERROR_BAD_ENCODING);
/* hide nomem error */
snprintf(dw->status, sizeof dw->status,
messages_get("Download"),
@ -645,9 +645,9 @@ void ro_gui_download_update_status(struct gui_download_window *dw)
err = utf8_to_local_encoding(
messages_get("DownloadU"), 0, &local_status);
if (err != UTF8_CONVERT_OK) {
if (err != NSERROR_OK) {
/* badenc should never happen */
assert(err != UTF8_CONVERT_BADENC);
assert(err != NSERROR_BAD_ENCODING);
/* hide nomem error */
snprintf(dw->status, sizeof dw->status,
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,
&local_status);
if (err != UTF8_CONVERT_OK) {
if (err != NSERROR_OK) {
/* badenc should never happen */
assert(err != UTF8_CONVERT_BADENC);
assert(err != NSERROR_BAD_ENCODING);
/* hide nomem error */
snprintf(dw->status, sizeof dw->status,
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) {
utf8_convert_ret err;
nserror err;
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 */
assert(err != UTF8_CONVERT_BADENC);
assert(err != NSERROR_BAD_ENCODING);
LOG(("utf8_to_enc failed"));
/* Paranoia */
local_text = NULL;

View File

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

View File

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

View File

@ -150,7 +150,7 @@ query_id query_user_xy(const char *query, const char *detail,
int len;
int tx;
char *local_text = NULL;
utf8_convert_ret err;
nserror err;
qw = malloc(sizeof(struct gui_query_window));
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 */
err = utf8_to_local_encoding(yes, 0, &local_text);
if (err != UTF8_CONVERT_OK) {
assert(err != UTF8_CONVERT_BADENC);
if (err != NSERROR_OK) {
assert(err != NSERROR_BAD_ENCODING);
LOG(("utf8_to_local_encoding_failed"));
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 */
err = utf8_to_local_encoding(no, 0, &local_text);
if (err != UTF8_CONVERT_OK) {
assert(err != UTF8_CONVERT_BADENC);
if (err != NSERROR_OK) {
assert(err != NSERROR_BAD_ENCODING);
LOG(("utf8_to_local_encoding_failed"));
local_text = NULL;
}

View File

@ -614,7 +614,7 @@ static void ro_gui_save_drag_end(wimp_dragged *drag, void *data)
os_error *error;
char *dp, *ep;
char *local_name = NULL;
utf8_convert_ret err;
nserror err;
if (dragbox_active)
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
* name based upon the URL */
err = utf8_to_local_encoding(save_leafname, 0, &local_name);
if (err != UTF8_CONVERT_OK) {
if (err != NSERROR_OK) {
/* badenc should never happen */
assert(err != UTF8_CONVERT_BADENC);
assert(err != NSERROR_BAD_ENCODING);
local_name = NULL;
}
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;
bool done = false;
char *nice = NULL;
utf8_convert_ret err;
nserror err;
char *local_name;
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;
err = utf8_to_local_encoding(name, 0, &local_name);
if (err != UTF8_CONVERT_OK) {
if (err != NSERROR_OK) {
/* badenc should never happen */
assert(err != UTF8_CONVERT_BADENC);
assert(err != NSERROR_BAD_ENCODING);
local_name = NULL;
}

View File

@ -391,7 +391,7 @@ bool ro_gui_selection_prepare_paste_dataload(
FILE *fp;
long size;
char *local_cb;
utf8_convert_ret ret;
nserror ret;
/* Ignore messages that aren't for us */
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,
&clipboard);
if (ret == UTF8_CONVERT_OK) {
if (ret == NSERROR_OK) {
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)
{
char *local_cb;
utf8_convert_ret ret;
nserror ret;
os_error *error;
assert(clip_length > 0 && clipboard);
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");
return false;
}

View File

@ -446,17 +446,16 @@ static const struct special {
* \param string The string to convert
* \param len The length (in bytes) of the string, or 0
* \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,
char **result)
nserror utf8_to_local_encoding(const char *string, size_t len, char **result)
{
os_error *error;
int alphabet, i;
size_t off, prev_off;
char *temp, *cur_pos;
const char *enc;
utf8_convert_ret err;
nserror err;
assert(string);
assert(result);
@ -473,7 +472,7 @@ utf8_convert_ret utf8_to_local_encoding(const char *string, size_t len,
/* UTF-8 -> simply copy string */
if (alphabet == 111 /* UTF-8 */) {
*result = strndup(string, len);
return UTF8_CONVERT_OK;
return NSERROR_OK;
}
/* get encoding name */
@ -485,7 +484,7 @@ utf8_convert_ret utf8_to_local_encoding(const char *string, size_t len,
/* create output buffer */
*(result) = malloc(len + 1);
if (!(*result))
return UTF8_CONVERT_NOMEM;
return NSERROR_NOMEM;
*(*result) = '\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) {
err = utf8_to_enc(string + prev_off, enc,
off - prev_off, &temp);
if (err != UTF8_CONVERT_OK) {
assert(err != UTF8_CONVERT_BADENC);
if (err != NSERROR_OK) {
assert(err != NSERROR_BAD_ENCODING);
free(*result);
return UTF8_CONVERT_NOMEM;
return NSERROR_NOMEM;
}
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) {
err = utf8_to_enc(string + prev_off, enc, len - prev_off,
&temp);
if (err != UTF8_CONVERT_OK) {
assert(err != UTF8_CONVERT_BADENC);
if (err != NSERROR_OK) {
assert(err != NSERROR_BAD_ENCODING);
free(*result);
return UTF8_CONVERT_NOMEM;
return NSERROR_NOMEM;
}
strcat(cur_pos, temp);
@ -544,7 +543,7 @@ utf8_convert_ret utf8_to_local_encoding(const char *string, size_t len,
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 len The length (in bytes) of the string, or 0
* \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,
char **result)
nserror utf8_from_local_encoding(const char *string, size_t len, char **result)
{
os_error *error;
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;
char *temp;
const char *enc;
utf8_convert_ret err;
nserror err;
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 */) {
temp = strndup(string, len);
if (!temp)
return UTF8_CONVERT_NOMEM;
return NSERROR_NOMEM;
*result = temp;
return UTF8_CONVERT_OK;
return NSERROR_OK;
}
/* get encoding name */
@ -598,7 +596,7 @@ utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len,
*(result) = malloc(result_alloc);
if (!(*result))
return UTF8_CONVERT_NOMEM;
return NSERROR_NOMEM;
*(*result) = '\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) {
err = utf8_from_enc(string + prev_off, enc,
off - prev_off, &temp, NULL);
if (err != UTF8_CONVERT_OK) {
assert(err != UTF8_CONVERT_BADENC);
if (err != NSERROR_OK) {
assert(err != NSERROR_BAD_ENCODING);
LOG(("utf8_from_enc failed"));
free(*result);
return UTF8_CONVERT_NOMEM;
return NSERROR_NOMEM;
}
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));
if (!temp) {
free(*result);
return UTF8_CONVERT_NOMEM;
return NSERRO_NOMEM;
}
*result = temp;
@ -661,11 +659,11 @@ utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len,
if (prev_off < len) {
err = utf8_from_enc(string + prev_off, enc, len - prev_off,
&temp, NULL);
if (err != UTF8_CONVERT_OK) {
assert(err != UTF8_CONVERT_BADENC);
if (err != NSERROR_OK) {
assert(err != NSERROR_BAD_ENCODING);
LOG(("utf8_from_enc failed"));
free(*result);
return UTF8_CONVERT_NOMEM;
return NSERROR_NOMEM;
}
strcat((*result) + cur_off, temp);
@ -680,9 +678,9 @@ utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len,
if (!temp) {
LOG(("realloc failed"));
free(*result);
return UTF8_CONVERT_NOMEM;
return NSERROR_NOMEM;
}
*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;
if (is_utf8) {
utf8_convert_ret err;
nserror err;
/* convert text to local encoding */
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 */
assert(err != UTF8_CONVERT_BADENC);
assert(err != NSERROR_BAD_ENCODING);
LOG(("utf8_to_enc failed"));
/* Paranoia */
local_text = NULL;
@ -677,7 +677,7 @@ void ro_gui_set_window_title(wimp_w w, const char *text)
wimp_window_info_base window;
os_error *error;
char *title_local_enc;
utf8_convert_ret err;
nserror err;
/* 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 */
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,
* so assert this */
assert(err != UTF8_CONVERT_BADENC);
assert(err != NSERROR_BAD_ENCODING);
LOG(("utf8_to_enc failed"));
return;
}

View File

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

View File

@ -33,23 +33,14 @@
#include "utils/log.h"
#include "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
*/
/* exported interface documented in utils/utf8.h */
uint32_t utf8_to_ucs4(const char *s_in, size_t l)
{
uint32_t ucs4;
size_t len;
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);
if (perror != PARSERUTILS_OK)
ucs4 = 0xfffd;
@ -57,16 +48,7 @@ uint32_t utf8_to_ucs4(const char *s_in, size_t l)
return ucs4;
}
/**
* 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
*/
/* exported interface documented in utils/utf8.h */
size_t utf8_from_ucs4(uint32_t c, char *s)
{
uint8_t *in = (uint8_t *) s;
@ -84,24 +66,13 @@ size_t utf8_from_ucs4(uint32_t c, char *s)
return 6 - len;
}
/**
* Calculate the length (in characters) of a NULL-terminated UTF-8 string
*
* \param s The string
* \return Length of string
*/
/* exported interface documented in utils/utf8.h */
size_t utf8_length(const char *s)
{
return utf8_bounded_length(s, strlen(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
*/
/* exported interface documented in utils/utf8.h */
size_t utf8_bounded_length(const char *s, size_t l)
{
size_t len;
@ -114,14 +85,7 @@ size_t utf8_bounded_length(const char *s, size_t l)
return len;
}
/**
* 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
*/
/* exported interface documented in utils/utf8.h */
size_t utf8_bounded_byte_length(const char *s, size_t l, size_t c)
{
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;
}
/**
* Calculate the length (in bytes) of a UTF-8 character
*
* \param s Pointer to start of character
* \return Length of character, in bytes
*/
/* exported interface documented in utils/utf8.h */
size_t utf8_char_byte_length(const char *s)
{
size_t len;
@ -150,13 +109,7 @@ size_t utf8_char_byte_length(const char *s)
return len;
}
/**
* 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
*/
/* exported interface documented in utils/utf8.h */
size_t utf8_prev(const char *s, size_t o)
{
uint32_t prev;
@ -168,20 +121,13 @@ size_t utf8_prev(const char *s, size_t o)
return prev;
}
/**
* 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
*/
/* exported interface documented in utils/utf8.h */
size_t utf8_next(const char *s, size_t l, size_t o)
{
uint32_t next;
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);
assert(perror == PARSERUTILS_OK);
@ -202,16 +148,16 @@ static inline void utf8_clear_cd_cache(void)
last_cd.cd = 0;
}
/**
* Finalise the UTF-8 library
*/
void utf8_finalise(void)
/* exported interface documented in utils/utf8.h */
nserror utf8_finalise(void)
{
if (last_cd.cd != 0)
iconv_close(last_cd.cd);
/* paranoia follows */
utf8_clear_cd_cache();
return NSERROR_OK;
}
@ -224,11 +170,16 @@ void utf8_finalise(void)
* \param to The encoding name to convert to
* \param result Pointer to location in which to store result.
* \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,
const char *from, const char *to,
char **result, size_t *result_len)
static nserror
utf8_convert(const char *string,
size_t len,
const char *from,
const char *to,
char **result,
size_t *result_len)
{
iconv_t cd;
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);
if (string[0] == '\0') {
/* On AmigaOS, iconv() returns an error if we pass an
* empty string. This prevents iconv() being called as
/* On AmigaOS, iconv() returns an error if we pass an
* empty string. This prevents iconv() being called as
* there is no conversion necessary anyway. */
*result = strdup("");
if (!(*result)) {
*result = NULL;
return UTF8_CONVERT_NOMEM;
return NSERROR_NOMEM;
}
return UTF8_CONVERT_OK;
return NSERROR_OK;
}
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);
if (!(*result)) {
*(result) = NULL;
return UTF8_CONVERT_NOMEM;
return NSERROR_NOMEM;
}
return UTF8_CONVERT_OK;
return NSERROR_OK;
}
in = (char *)string;
@ -274,9 +225,9 @@ static utf8_convert_ret utf8_convert(const char *string, size_t len,
cd = iconv_open(to, from);
if (cd == (iconv_t)-1) {
if (errno == EINVAL)
return UTF8_CONVERT_BADENC;
return NSERROR_BAD_ENCODING;
/* default to no memory */
return UTF8_CONVERT_NOMEM;
return NSERROR_NOMEM;
}
/* 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;
temp = out = malloc(rlen);
if (!out)
return UTF8_CONVERT_NOMEM;
if (!out) {
return NSERROR_NOMEM;
}
/* perform conversion */
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
* b) Invalid input byte sequence
* c) Incomplete input sequence */
return UTF8_CONVERT_NOMEM;
return NSERROR_NOMEM;
}
*(result) = realloc(temp, out - temp + 4);
if (!(*result)) {
free(temp);
*(result) = NULL; /* for sanity's sake */
return UTF8_CONVERT_NOMEM;
return NSERROR_NOMEM;
}
/* 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);
}
return UTF8_CONVERT_OK;
return NSERROR_OK;
}
/**
* 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 Appropriate utf8_convert_ret value
*/
utf8_convert_ret utf8_to_enc(const char *string, const char *encname,
/* exported interface documented in utils/utf8.h */
nserror utf8_to_enc(const char *string, const char *encname,
size_t len, char **result)
{
return utf8_convert(string, len, "UTF-8", encname, result, NULL);
}
/**
* 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 Appropriate utf8_convert_ret value
*/
utf8_convert_ret utf8_from_enc(const char *string, const char *encname,
/* exported interface documented in utils/utf8.h */
nserror utf8_from_enc(const char *string, const char *encname,
size_t len, char **result, size_t *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,
char **out, size_t *outlen)
/**
* convert a chunk of html data
*/
static nserror
utf8_convert_html_chunk(iconv_t cd,
const char *chunk,
size_t inlen,
char **out,
size_t *outlen)
{
size_t ret, esclen;
uint32_t ucs4;
@ -377,7 +319,7 @@ static utf8_convert_ret utf8_convert_html_chunk(iconv_t cd,
break;
if (errno != EILSEQ)
return UTF8_CONVERT_NOMEM;
return NSERROR_NOMEM;
ucs4 = utf8_to_ucs4(chunk, inlen);
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,
(void *) out, outlen);
if (ret == (size_t) -1)
return UTF8_CONVERT_NOMEM;
return NSERROR_NOMEM;
esclen = utf8_next(chunk, inlen, 0);
chunk += esclen;
inlen -= esclen;
}
return UTF8_CONVERT_OK;
return NSERROR_OK;
}
/**
* 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 Appropriate utf8_convert_ret value
*/
utf8_convert_ret utf8_to_html(const char *string, const char *encname,
size_t len, char **result)
/* exported interface documented in utils/utf8.h */
nserror
utf8_to_html(const char *string, const char *encname, size_t len, char **result)
{
iconv_t cd;
const char *in;
char *out, *origout;
size_t off, prev_off, inlen, outlen, origoutlen, esclen;
utf8_convert_ret ret;
nserror ret;
char *pescape, escape[11];
if (len == 0)
@ -425,15 +358,14 @@ utf8_convert_ret utf8_to_html(const char *string, const char *encname,
sizeof(last_cd.to)) == 0 &&
last_cd.cd != 0) {
cd = last_cd.cd;
}
else {
} else {
/* no match, so create a new cd */
cd = iconv_open(encname, "UTF-8");
if (cd == (iconv_t) -1) {
if (errno == EINVAL)
return UTF8_CONVERT_BADENC;
return NSERROR_BAD_ENCODING;
/* default to no memory */
return UTF8_CONVERT_NOMEM;
return NSERROR_NOMEM;
}
/* 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;
}
/* Worst case is ASCII -> UCS4, with all characters escaped:
* "&#xYYYYYY;", thus each input character may become a string
/* Worst case is ASCII -> UCS4, with all characters escaped:
* "&#xYYYYYY;", thus each input character may become a string
* of 10 UCS4 characters, each 4 bytes in length, plus four for
* terminating the string */
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) {
iconv_close(cd);
utf8_clear_cd_cache();
return UTF8_CONVERT_NOMEM;
return NSERROR_NOMEM;
}
/* 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;
ret = utf8_convert_html_chunk(cd, in, inlen,
&out, &outlen);
if (ret != UTF8_CONVERT_OK) {
if (ret != NSERROR_OK) {
free(origout);
iconv_close(cd);
utf8_clear_cd_cache();
@ -484,7 +416,7 @@ utf8_convert_ret utf8_to_html(const char *string, const char *encname,
pescape = escape;
ret = utf8_convert_html_chunk(cd, pescape, esclen,
&out, &outlen);
if (ret != UTF8_CONVERT_OK) {
if (ret != NSERROR_OK) {
free(origout);
iconv_close(cd);
utf8_clear_cd_cache();
@ -502,7 +434,7 @@ utf8_convert_ret utf8_to_html(const char *string, const char *encname,
in = string + prev_off;
inlen = len - prev_off;
ret = utf8_convert_html_chunk(cd, in, inlen, &out, &outlen);
if (ret != UTF8_CONVERT_OK) {
if (ret != NSERROR_OK) {
free(origout);
iconv_close(cd);
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);
if (*result == NULL) {
free(origout);
return UTF8_CONVERT_NOMEM;
return NSERROR_NOMEM;
}
return UTF8_CONVERT_OK;
return NSERROR_OK;
}
/**
* 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
*/
/* exported interface documented in utils/utf8.h */
bool utf8_save_text(const char *utf8_text, const char *path)
{
utf8_convert_ret ret;
nserror ret;
char *conv;
FILE *out;
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));
return false;
}
@ -562,5 +486,3 @@ bool utf8_save_text(const char *utf8_text, const char *path)
return false;
}

View File

@ -26,40 +26,144 @@
#include <stdbool.h>
#include <stdint.h>
typedef enum {
UTF8_CONVERT_OK,
UTF8_CONVERT_NOMEM,
UTF8_CONVERT_BADENC
} utf8_convert_ret;
#include "utils/errors.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, 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);
/**
* 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);
/**
* 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);
/**
* 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);
/**
* 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);
/**
* 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);
/**
* 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);
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);
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);
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);
/**
* 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);
/* 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

View File

@ -35,7 +35,7 @@
#include "windows/gui.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,
size_t len,
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);
}
utf8_convert_ret utf8_to_local_encoding(const char *string,
nserror utf8_to_local_encoding(const char *string,
size_t len,
char **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)
{
assert(string && result);
@ -60,9 +60,9 @@ utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len,
*result = strndup(string, len);
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)

View File

@ -29,7 +29,7 @@ struct font_desc {
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,
size_t len,
char **result);