1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-11-04 08:17:17 -05:00

bug 153, 1066: Convert ~/.elinks/bookmarks to/from UTF-8.

The ~/.elinks/bookmarks file is in the system charset,
for compatibility with earlier ELinks releases,
but internally the strings are in UTF-8.
This commit is contained in:
Kalle Olavi Niemitalo 2008-10-20 01:08:20 +03:00 committed by Kalle Olavi Niemitalo
parent 1cb81679f4
commit 8c0ae2a215

View File

@ -4,6 +4,7 @@
#include "config.h" #include "config.h"
#endif #endif
#include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -33,6 +34,7 @@ read_bookmarks_default(FILE *f)
unsigned char in_buffer[INBUF_SIZE]; /* read buffer */ unsigned char in_buffer[INBUF_SIZE]; /* read buffer */
struct bookmark *last_bm = NULL; struct bookmark *last_bm = NULL;
int last_depth = 0; int last_depth = 0;
const int file_cp = get_cp_index("System");
/* TODO: Ignore lines with bad chars in title or url (?). -- Zas */ /* TODO: Ignore lines with bad chars in title or url (?). -- Zas */
while (fgets(in_buffer, INBUF_SIZE, f)) { while (fgets(in_buffer, INBUF_SIZE, f)) {
@ -101,9 +103,8 @@ read_bookmarks_default(FILE *f)
root = last_bm->root; root = last_bm->root;
} }
} }
/** @todo Bugs 153, 1066: add_bookmark() last_bm = add_bookmark_cp(root, 1, file_cp,
* expects UTF-8. */ title, url);
last_bm = add_bookmark(root, 1, title, url);
last_depth = depth; last_depth = depth;
if (!*url && title[0] == '-' && title[1] == '\0') { if (!*url && title[0] == '-' && title[1] == '\0') {
@ -127,29 +128,65 @@ read_bookmarks_default(FILE *f)
#undef INBUF_SIZE #undef INBUF_SIZE
} }
struct write_bookmarks_default
{
struct secure_save_info *ssi;
int save_folder_state;
int codepage;
struct conv_table *conv_table;
};
static void
write_bookmarks_default_inner(const struct write_bookmarks_default *out,
LIST_OF(struct bookmark) *bookmarks_list)
{
struct bookmark *bm;
foreach (bm, *bookmarks_list) {
unsigned char *title, *url;
title = convert_string(out->conv_table, bm->title,
strlen(bm->title), out->codepage,
CSM_NONE, NULL, NULL, NULL);
url = convert_string(out->conv_table, bm->url,
strlen(bm->url), out->codepage,
CSM_NONE, NULL, NULL, NULL);
secure_fprintf(out->ssi, "%s\t%s\t%d\t",
empty_string_or_(title), empty_string_or_(url),
bm->box_item->depth);
if (bm->box_item->type == BI_FOLDER) {
secure_fputc(out->ssi, 'F');
if (out->save_folder_state && bm->box_item->expanded)
secure_fputc(out->ssi, 'E');
}
secure_fputc(out->ssi, '\n');
if (!title || !url) {
secsave_errno = SS_ERR_OTHER;
out->ssi->err = ENOMEM;
}
mem_free_if(title);
mem_free_if(url);
if (out->ssi->err) break;
if (!list_empty(bm->child))
write_bookmarks_default_inner(out, &bm->child);
}
}
/* Saves the bookmarks to file */ /* Saves the bookmarks to file */
static void static void
write_bookmarks_default(struct secure_save_info *ssi, write_bookmarks_default(struct secure_save_info *ssi,
LIST_OF(struct bookmark) *bookmarks_list) LIST_OF(struct bookmark) *bookmarks_list)
{ {
int folder_state = get_opt_bool("bookmarks.folder_state"); struct write_bookmarks_default out;
struct bookmark *bm;
foreach (bm, *bookmarks_list) { out.ssi = ssi;
/** @todo Bug 153: bm->title should be UTF-8. out.save_folder_state = get_opt_bool("bookmarks.folder_state");
* @todo Bug 1066: bm->url should be UTF-8. */ out.codepage = get_cp_index("System");
secure_fprintf(ssi, "%s\t%s\t%d\t", bm->title, bm->url, bm->box_item->depth); out.conv_table = get_translation_table(get_cp_index("UTF-8"),
if (bm->box_item->type == BI_FOLDER) { out.codepage);
secure_fputc(ssi, 'F'); write_bookmarks_default_inner(&out, bookmarks_list);
if (folder_state && bm->box_item->expanded)
secure_fputc(ssi, 'E');
}
secure_fputc(ssi, '\n');
if (ssi->err) break;
if (!list_empty(bm->child))
write_bookmarks_default(ssi, &bm->child);
}
} }
static unsigned char * static unsigned char *