[gnumeric] From ODF also import named expressions with illegal name. [#650125]



commit 302bd0b8062731fe388bfccff50e7d1472e6fc49
Author: Andreas J Guelzow <aguelzow pyrshep ca>
Date:   Thu May 19 12:44:58 2011 -0600

    From ODF also import named expressions with illegal name. [#650125]
    
    2011-05-19  Andreas J. Guelzow <aguelzow pyrshep ca>
    
    	* openoffice-read.c (odf_fix_expr_names): implement
    	(odf_fix_expr_names_t_new): new
    	(odf_fix_expr_names_t_free): new
    	(odf_fix_expr_names_t_add): new
    	(odf_fix_en_validate): new
    	(odf_fix_en_collect): new
    	(odf_fix_en_apply): new
    
    2011-05-19  Andreas J. Guelzow <aguelzow pyrshep ca>
    
    	* src/expr-name.h (gnm_named_expr_collection_rename): new
    	* src/expr-name.c (gnm_named_expr_collection_rename): new

 ChangeLog                            |    5 ++
 plugins/openoffice/ChangeLog         |   10 ++++
 plugins/openoffice/openoffice-read.c |   99 +++++++++++++++++++++++++++++++++-
 src/expr-name.c                      |   26 +++++++++
 src/expr-name.h                      |    3 +
 5 files changed, 142 insertions(+), 1 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index efcbc0d..aa0895d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2011-05-19  Andreas J. Guelzow <aguelzow pyrshep ca>
+
+	* src/expr-name.h (gnm_named_expr_collection_rename): new
+	* src/expr-name.c (gnm_named_expr_collection_rename): new
+
 2011-05-18  Andreas J. Guelzow <aguelzow pyrshep ca>
 
 	* src/print.c (gnm_print_sheet): wbc may be NULL
diff --git a/plugins/openoffice/ChangeLog b/plugins/openoffice/ChangeLog
index 847dca6..111373b 100644
--- a/plugins/openoffice/ChangeLog
+++ b/plugins/openoffice/ChangeLog
@@ -1,5 +1,15 @@
 2011-05-19  Andreas J. Guelzow <aguelzow pyrshep ca>
 
+	* openoffice-read.c (odf_fix_expr_names): implement
+	(odf_fix_expr_names_t_new): new
+	(odf_fix_expr_names_t_free): new
+	(odf_fix_expr_names_t_add): new
+	(odf_fix_en_validate): new
+	(odf_fix_en_collect): new
+	(odf_fix_en_apply): new
+	
+2011-05-19  Andreas J. Guelzow <aguelzow pyrshep ca>
+
 	* openoffice-write.c (odf_write_sheet): use sheet_get_cells_extent
 
 2011-05-17  Andreas J. Guelzow <aguelzow pyrshep ca>
diff --git a/plugins/openoffice/openoffice-read.c b/plugins/openoffice/openoffice-read.c
index 0d3a94b..20a4156 100644
--- a/plugins/openoffice/openoffice-read.c
+++ b/plugins/openoffice/openoffice-read.c
@@ -29,6 +29,7 @@
 #include <gnm-plugin.h>
 #include <workbook-view.h>
 #include <workbook.h>
+#include <workbook-priv.h>
 #include <sheet.h>
 #include <sheet-merge.h>
 #include <sheet-filter.h>
@@ -1210,6 +1211,87 @@ odf_strunescape (char const *string, GString *target,
 	return NULL;	
 }
 
+typedef struct {
+	GHashTable *orig2fixed;
+	GHashTable *fixed2orig;
+	OOParseState *state;
+} odf_fix_expr_names_t;
+
+static odf_fix_expr_names_t *
+odf_fix_expr_names_t_new (OOParseState *state)
+{
+	odf_fix_expr_names_t *fen = g_new (odf_fix_expr_names_t, 1);
+	
+	fen->fixed2orig = g_hash_table_new (g_str_hash, g_str_equal);
+	fen->orig2fixed = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
+	fen->state = state;
+
+	return fen;
+}
+
+static void
+odf_fix_expr_names_t_free (odf_fix_expr_names_t *fen)
+{
+	g_hash_table_unref (fen->fixed2orig);
+	g_hash_table_unref (fen->orig2fixed);
+	g_free (fen);
+}
+
+static void
+odf_fix_expr_names_t_add (odf_fix_expr_names_t *fen, char const *orig, char *fixed)
+{
+	char *orig_c = g_strdup (orig);
+	g_hash_table_insert (fen->orig2fixed, orig_c, fixed);
+	g_hash_table_insert (fen->fixed2orig, fixed, orig_c);
+}
+
+static gboolean
+odf_fix_en_validate (char const *name, odf_fix_expr_names_t *fen)
+{
+	GSList *sheets;
+
+	if (!expr_name_validate (name))
+		return FALSE;
+	if (NULL != g_hash_table_lookup (fen->fixed2orig, name))
+		return FALSE;
+	if (NULL != gnm_named_expr_collection_lookup (fen->state->pos.wb->names, name))
+		return FALSE;
+	for (sheets = workbook_sheets (fen->state->pos.wb); sheets != NULL; sheets = sheets->next)
+		if (NULL != gnm_named_expr_collection_lookup 
+		    (((Sheet *)(sheets->data))->names, name))
+			return FALSE;
+	return TRUE;
+}
+
+static void 
+odf_fix_en_collect (gchar const *key, GnmNamedExpr *nexpr, odf_fix_expr_names_t *fen)
+{
+	GString *str;
+	gchar *here;
+
+	if (expr_name_validate (key))
+		return;
+	if (NULL != g_hash_table_lookup (fen->orig2fixed, key))
+		return;
+	str = g_string_new (key);
+	while ((here = strchr (str->str, '.')) != NULL)
+		*here = '_';
+	while (!odf_fix_en_validate (str->str, fen))
+		g_string_append_c (str, '_');
+	odf_fix_expr_names_t_add (fen, key, g_string_free (str, FALSE));
+}
+
+static void 
+odf_fix_en_apply (gchar const *orig, gchar const *fixed, OOParseState *state)
+{
+	GSList *sheets = workbook_sheets (state->pos.wb), *l;
+	gnm_named_expr_collection_rename (state->pos.wb->names, orig, fixed);
+	for (l = sheets; l != NULL; l = l->next) {
+		Sheet *sheet = l->data;
+		gnm_named_expr_collection_rename (sheet->names, orig, fixed);
+	}
+}
+
 /**
  * When we initialy validate names we have to accept every ODF name
  * in odf_fix_expr_names we fix them.
@@ -1220,7 +1302,22 @@ odf_strunescape (char const *string, GString *target,
 static void
 odf_fix_expr_names (OOParseState *state)
 {
-#warning We need to implement this
+	odf_fix_expr_names_t *fen = odf_fix_expr_names_t_new (state);
+	GSList *sheets = workbook_sheets (state->pos.wb), *l;
+
+	g_hash_table_foreach (state->pos.wb->names->names, 
+			      (GHFunc)odf_fix_en_collect, fen);
+	g_hash_table_foreach (state->pos.wb->names->placeholders, 
+			      (GHFunc)odf_fix_en_collect, fen);
+	for (l = sheets; l != NULL; l = l->next) {
+		Sheet *sheet = l->data;
+		g_hash_table_foreach (sheet->names->names, 
+				      (GHFunc) odf_fix_en_collect, fen);
+		g_hash_table_foreach (sheet->names->placeholders, 
+				      (GHFunc) odf_fix_en_collect, fen);
+	}
+	g_hash_table_foreach (fen->orig2fixed, (GHFunc) odf_fix_en_apply, state);
+	odf_fix_expr_names_t_free (fen);
 }
 
 /**
diff --git a/src/expr-name.c b/src/expr-name.c
index 8de925c..9a787cf 100644
--- a/src/expr-name.c
+++ b/src/expr-name.c
@@ -372,6 +372,32 @@ gnm_named_expr_collection_foreach (GnmNamedExprCollection *names,
 	g_hash_table_foreach (names->placeholders, func, data);
 }
 
+static void
+gnm_named_expr_collection_rename_in_hash (GHashTable *hash, 
+					  gchar const *old_name, 
+					  gchar const *new_name)
+{
+	GnmNamedExpr *nexpr;
+
+	if ((nexpr = g_hash_table_lookup (hash, old_name)) != NULL) {
+		g_hash_table_steal (hash, old_name);
+		go_string_unref (nexpr->name);
+		nexpr->name = go_string_new (new_name);
+		g_hash_table_insert (hash, (gpointer)nexpr->name->str, nexpr);
+	}
+}
+
+/* rename old_name to new_name (if such a name exists) */
+void gnm_named_expr_collection_rename (GnmNamedExprCollection *names,
+				       gchar const *old_name,
+				       gchar const *new_name)
+{
+	gnm_named_expr_collection_rename_in_hash 
+		(names->names, old_name, new_name);
+	gnm_named_expr_collection_rename_in_hash 
+		(names->placeholders, old_name, new_name);
+}
+
 /******************************************************************************/
 
 /**
diff --git a/src/expr-name.h b/src/expr-name.h
index 3adcffe..51e0906 100644
--- a/src/expr-name.h
+++ b/src/expr-name.h
@@ -80,6 +80,9 @@ void gnm_named_expr_collection_foreach (GnmNamedExprCollection *names,
 
 GnmNamedExpr *gnm_named_expr_collection_lookup (GnmNamedExprCollection const *scope,
 						char const *name);
+void gnm_named_expr_collection_rename (GnmNamedExprCollection *names,
+				       gchar const *old_name,
+				       gchar const *new_name);
 
 G_END_DECLS
 



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