[gnumeric] Works: codepage support.



commit 33d2b1c01794e13801500c3c27297004426f14da
Author: Morten Welinder <terra gnome org>
Date:   Wed May 4 16:39:48 2011 -0400

    Works: codepage support.

 plugins/lotus-123/boot.c  |   11 +++---
 plugins/lotus-123/lotus.c |   76 ++++++++++++++++++++++++++++++++-------------
 plugins/lotus-123/lotus.h |    1 +
 3 files changed, 60 insertions(+), 28 deletions(-)
---
diff --git a/plugins/lotus-123/boot.c b/plugins/lotus-123/boot.c
index 7a8e14b..c272c8b 100644
--- a/plugins/lotus-123/boot.c
+++ b/plugins/lotus-123/boot.c
@@ -80,19 +80,18 @@ lotus_file_open (GOFileOpener const *fo, GOIOContext *io_context,
 	state.sheet_area_error = FALSE;
 	state.style_pool = NULL;
 	state.fonts      = NULL;
+	state.works_conv = (GIConv)(-1);
 
 	if (!lotus_read (&state))
 		go_io_error_string (io_context,
 			_("Error while reading lotus workbook."));
 
-	if (state.style_pool) {
+	if (state.style_pool)
 		g_hash_table_destroy (state.style_pool);
-		state.style_pool = NULL;
-	}
-	if (state.fonts) {
+	if (state.fonts)
 		g_hash_table_destroy (state.fonts);
-		state.fonts = NULL;
-	}
+	if (state.works_conv != (GIConv)(-1))
+		gsf_iconv_close (state.works_conv);
 }
 
 
diff --git a/plugins/lotus-123/lotus.c b/plugins/lotus-123/lotus.c
index 8891224..3a251fc 100644
--- a/plugins/lotus-123/lotus.c
+++ b/plugins/lotus-123/lotus.c
@@ -1547,10 +1547,10 @@ lotus_read_old (LotusState *state, record_t *r)
 		case LOTUS_INTEGER: CHECK_RECORD_SIZE (>= 7) {
 			GnmValue *v = value_new_int (GSF_LE_GET_GINT16 (r->data + 5));
 			guint8 fmt = GSF_LE_GET_GUINT8 (r->data);
-			int i = GSF_LE_GET_GUINT16 (r->data + 1);
-			int j = GSF_LE_GET_GUINT16 (r->data + 3);
+			int col = GSF_LE_GET_GUINT16 (r->data + 1);
+			int row = GSF_LE_GET_GUINT16 (r->data + 3);
 
-			cell = insert_value (state, state->sheet, i, j, v);
+			cell = insert_value (state, state->sheet, col, row, v);
 			if (cell)
 				cell_set_format_from_lotus_format (cell, fmt);
 			break;
@@ -1558,10 +1558,10 @@ lotus_read_old (LotusState *state, record_t *r)
 		case LOTUS_NUMBER: CHECK_RECORD_SIZE (>= 13) {
 			GnmValue *v = lotus_value (gsf_le_get_double (r->data + 5));
 			guint8 fmt = GSF_LE_GET_GUINT8 (r->data);
-			int i = GSF_LE_GET_GUINT16 (r->data + 1);
-			int j = GSF_LE_GET_GUINT16 (r->data + 3);
+			int col = GSF_LE_GET_GUINT16 (r->data + 1);
+			int row = GSF_LE_GET_GUINT16 (r->data + 3);
 
-			cell = insert_value (state, state->sheet, i, j, v);
+			cell = insert_value (state, state->sheet, col, row, v);
 			if (cell)
 				cell_set_format_from_lotus_format (cell, fmt);
 			break;
@@ -1571,9 +1571,9 @@ lotus_read_old (LotusState *state, record_t *r)
 			/* gchar format_prefix = *(r->data + 1 + 4);*/
 			GnmValue *v = lotus_get_strval (r, 6, state->lmbcs_group);
 			guint8 fmt = GSF_LE_GET_GUINT8 (r->data);
-			int i = GSF_LE_GET_GUINT16 (r->data + 1);
-			int j = GSF_LE_GET_GUINT16 (r->data + 3);
-			cell = insert_value (state, state->sheet, i, j, v);
+			int col = GSF_LE_GET_GUINT16 (r->data + 1);
+			int row = GSF_LE_GET_GUINT16 (r->data + 3);
+			cell = insert_value (state, state->sheet, col, row, v);
 			if (cell)
 				cell_set_format_from_lotus_format (cell, fmt);
 			break;
@@ -2593,6 +2593,29 @@ works_format_string (guint8 arg)
 	return g_string_free(str, FALSE);
 }
 
+static GnmValue *
+works_get_strval (const record_t *r, int ofs, LotusState *state)
+{
+	GString *str = g_string_new (NULL);
+	char *strutf8;
+
+	while (ofs < r->len && r->data[ofs] != 0) {
+		g_string_append_c (str, r->data[ofs]);
+		ofs++;
+	}
+
+	strutf8 = g_convert_with_iconv (str->str, str->len,
+				    state->works_conv,
+				    NULL, NULL,
+				    NULL);
+	g_string_free (str, TRUE);
+
+	if (strutf8)
+		return value_new_string_nocopy (strutf8);
+	else
+		return value_new_empty ();
+}
+
 static gboolean
 lotus_read_works (LotusState *state, record_t *r)
 {
@@ -2616,6 +2639,9 @@ lotus_read_works (LotusState *state, record_t *r)
 
 	state->lmbcs_group = 1;
 
+	/* FIXME: Where do we get the codepage from?  */
+	state->works_conv = gsf_msole_iconv_open_for_import (1252);
+
 	do {
 		switch (r->type) {
 		case WORKS_BOF:
@@ -2648,7 +2674,7 @@ lotus_read_works (LotusState *state, record_t *r)
 			int i = GSF_LE_GET_GUINT16 (r->data + 1);
 			int j = GSF_LE_GET_GUINT16 (r->data + 3);
 
-			cell = insert_value (state, state->sheet, i, j, v);
+			cell = insert_value (state, state->sheet, col, row, v);
 			if (cell)
 				cell_set_format_from_lotus_format (cell, fmt);
 			break;
@@ -2657,31 +2683,36 @@ lotus_read_works (LotusState *state, record_t *r)
 
 		case LOTUS_NUMBER: CHECK_RECORD_SIZE (>= 14) {
 			GnmValue *v = lotus_value (gsf_le_get_double (r->data + 6));
-			int i = GSF_LE_GET_GUINT16 (r->data + 0);
-			int j = GSF_LE_GET_GUINT16 (r->data + 2);
+			int col = GSF_LE_GET_GUINT16 (r->data + 0);
+			int row = GSF_LE_GET_GUINT16 (r->data + 2);
 			int fmt = GSF_LE_GET_GUINT16 (r->data + 4);
 
-			cell = insert_value (state, state->sheet, i, j, v);
+			cell = insert_value (state, state->sheet, col, row, v);
 			if (cell)
 				cell_set_fmt (state, cell, fmt);
 			break;
 		}
 		case LOTUS_LABEL: CHECK_RECORD_SIZE (>= 8) {
-			GnmValue *v = lotus_get_strval (r, 6, state->lmbcs_group);
+			GnmValue *v = works_get_strval (r, 6, state);
+			int col = GSF_LE_GET_GUINT16 (r->data + 0);
+			int row = GSF_LE_GET_GUINT16 (r->data + 2);
 			int fmt = GSF_LE_GET_GUINT16 (r->data + 4);
-			int i = GSF_LE_GET_GUINT16 (r->data + 0);
-			int j = GSF_LE_GET_GUINT16 (r->data + 2);
-			cell = insert_value (state, state->sheet, i, j, v);
+			cell = insert_value (state, state->sheet, col, row, v);
 			if (cell)
 				cell_set_fmt (state, cell, fmt);
+#if 0
+			g_printerr ("String at %s:\n",
+				    cellpos_as_string (&cell->pos));
+#endif
+			gsf_mem_dump (r->data, r->len);
 			break;
 		}
 		case LOTUS_BLANK: CHECK_RECORD_SIZE (>= 6) {
 			GnmValue *v = value_new_empty();
-			int i = GSF_LE_GET_GUINT16 (r->data + 0);
-			int j = GSF_LE_GET_GUINT16 (r->data + 2);
+			int col = GSF_LE_GET_GUINT16 (r->data + 0);
+			int row = GSF_LE_GET_GUINT16 (r->data + 2);
 			int fmt = GSF_LE_GET_GUINT16 (r->data + 4);
-			cell = insert_value (state, state->sheet, i, j, v);
+			cell = insert_value (state, state->sheet, col, row, v);
 			if (cell)
 				cell_set_fmt (state, cell, fmt);
 			break;
@@ -2728,16 +2759,17 @@ lotus_read_works (LotusState *state, record_t *r)
 			int fid = fontidx++;
 			int l;
 			font->variant = GSF_LE_GET_GUINT16(r->data + 0);
-			l=strlen(r->data + 2);
+			l = strlen (r->data + 2);
 			if (l > 34) l = 34;
 			font->typeface = g_malloc(l + 1);
 			/* verify UTF-8? */
 			memcpy(font->typeface, r->data + 2, l);
-			((char*)font->typeface)[l] = 0;
+			font->typeface[l] = 0;
 			font->size = r->data[36];
 			g_hash_table_insert (state->fonts,
 			     GUINT_TO_POINTER ((guint)fid),
 			     font);
+
 			break;
 		}
 
diff --git a/plugins/lotus-123/lotus.h b/plugins/lotus-123/lotus.h
index 7967d31..2338dc5 100644
--- a/plugins/lotus-123/lotus.h
+++ b/plugins/lotus-123/lotus.h
@@ -26,6 +26,7 @@ typedef struct {
         GHashTable      *style_pool;
 	gboolean        sheet_area_error;
 	GHashTable      *fonts;
+	GIConv          works_conv;
 } LotusState;
 
 Sheet	 *lotus_get_sheet  (Workbook *wb, int i);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]