summaryrefslogtreecommitdiff
path: root/desktop
diff options
context:
space:
mode:
authorVincent Sanders <vince@kyllikki.org>2014-02-01 22:17:36 +0000
committerVincent Sanders <vince@kyllikki.org>2014-02-01 22:17:36 +0000
commit886a3106db6d6a60d2d706d2caad3e883dd70b14 (patch)
tree0fcd0fb9c8821f06d47e7e20e8c796e085e8aa2b /desktop
parent427f127fa9d42cdf9005d06813c580d18d5e0d6a (diff)
downloadnetsurf-886a3106db6d6a60d2d706d2caad3e883dd70b14.tar.gz
netsurf-886a3106db6d6a60d2d706d2caad3e883dd70b14.tar.bz2
move utf8 local conversion operations to table
Diffstat (limited to 'desktop')
-rw-r--r--desktop/gui_factory.c83
-rw-r--r--desktop/save_text.c12
2 files changed, 77 insertions, 18 deletions
diff --git a/desktop/gui_factory.c b/desktop/gui_factory.c
index b61fdf0be..8996ab5d6 100644
--- a/desktop/gui_factory.c
+++ b/desktop/gui_factory.c
@@ -272,6 +272,11 @@ static void gui_default_set_clipboard(const char *buffer, size_t length,
{
}
+static struct gui_clipboard_table default_clipboard_table = {
+ .get = gui_default_get_clipboard,
+ .set = gui_default_set_clipboard,
+};
+
/** verify clipboard table is valid */
static nserror verify_clipboard_register(struct gui_clipboard_table *gct)
{
@@ -290,6 +295,54 @@ static nserror verify_clipboard_register(struct gui_clipboard_table *gct)
return NSERROR_OK;
}
+/**
+ * The default utf8 conversion implementation.
+ *
+ * The default implementation assumes the local encoding is utf8
+ * allowing the conversion to be a simple copy.
+ *
+ * @param [in] string The source string.
+ * @param [in] len The \a string length or 0 to compute it.
+ * @param [out] result A pointer to the converted string.
+ * @result NSERROR_OK or NSERROR_NOMEM if memory could not be allocated.
+ */
+static nserror gui_default_utf8(const char *string, size_t len, char **result)
+{
+ assert(string && result);
+
+ if (len == 0)
+ len = strlen(string);
+
+ *result = strndup(string, len);
+ if (!(*result))
+ return NSERROR_NOMEM;
+
+ return NSERROR_OK;
+}
+
+static struct gui_utf8_table default_utf8_table = {
+ .utf8_to_local = gui_default_utf8,
+ .local_to_utf8 = gui_default_utf8,
+};
+
+/** verify clipboard table is valid */
+static nserror verify_utf8_register(struct gui_utf8_table *gut)
+{
+ /* check table is present */
+ if (gut == NULL) {
+ return NSERROR_BAD_PARAMETER;
+ }
+
+ /* mandantory operations */
+ if (gut->utf8_to_local == NULL) {
+ return NSERROR_BAD_PARAMETER;
+ }
+ if (gut->local_to_utf8 == NULL) {
+ return NSERROR_BAD_PARAMETER;
+ }
+ return NSERROR_OK;
+}
+
static nsurl *gui_default_get_resource_url(const char *path)
{
return NULL;
@@ -370,6 +423,13 @@ static void gui_default_401login_open(nsurl *url, const char *realm,
cb(false, cbpw);
}
+static struct gui_download_table default_download_table = {
+ .create = gui_default_download_create,
+ .data = gui_default_download_data,
+ .error = gui_default_download_error,
+ .done = gui_default_download_done,
+};
+
/** verify browser table is valid */
static nserror verify_browser_register(struct gui_browser_table *gbt)
{
@@ -406,19 +466,6 @@ static nserror verify_browser_register(struct gui_browser_table *gbt)
}
-
-static struct gui_download_table default_download_table = {
- .create = gui_default_download_create,
- .data = gui_default_download_data,
- .error = gui_default_download_error,
- .done = gui_default_download_done,
-};
-
-static struct gui_clipboard_table default_clipboard_table = {
- .get = gui_default_get_clipboard,
- .set = gui_default_set_clipboard,
-};
-
/* exported interface documented in desktop/gui_factory.h */
nserror gui_factory_register(struct gui_table *gt)
{
@@ -472,6 +519,16 @@ nserror gui_factory_register(struct gui_table *gt)
return err;
}
+ /* utf8 table */
+ if (gt->utf8 == NULL) {
+ /* set default clipboard table */
+ gt->utf8 = &default_utf8_table;
+ }
+ err = verify_utf8_register(gt->utf8);
+ if (err != NSERROR_OK) {
+ return err;
+ }
+
guit = gt;
return NSERROR_OK;
diff --git a/desktop/save_text.c b/desktop/save_text.c
index 38f8c5334..bc4d4c57a 100644
--- a/desktop/save_text.c
+++ b/desktop/save_text.c
@@ -28,14 +28,16 @@
#include <dom/dom.h>
#include "utils/config.h"
+#include "utils/log.h"
+#include "utils/utf8.h"
+#include "utils/utils.h"
#include "content/content.h"
#include "content/hlcache.h"
-#include "desktop/save_text.h"
#include "render/box.h"
#include "render/html.h"
-#include "utils/log.h"
-#include "utils/utf8.h"
-#include "utils/utils.h"
+
+#include "desktop/gui_factory.h"
+#include "desktop/save_text.h"
static void extract_text(struct box *box, bool *first,
save_text_whitespace *before, struct save_text_state *save);
@@ -69,7 +71,7 @@ void save_as_text(hlcache_handle *c, char *path)
if (!save.block)
return;
- ret = utf8_to_local_encoding(save.block, save.length, &result);
+ ret = guit->utf8->utf8_to_local(save.block, save.length, &result);
free(save.block);
if (ret != NSERROR_OK) {