[gnumeric] scenarios: implement saving.



commit d6338c2fd54b5fc7c2b30f361dcb3fe0d67d12a8
Author: Morten Welinder <terra gnome org>
Date:   Tue Nov 24 16:00:53 2009 -0500

    scenarios: implement saving.

 src/tools/scenarios.c |    8 ++++
 src/tools/scenarios.h |    1 +
 src/xml-sax-write.c   |   86 ++++++++++++++++++++++++++++++++----------------
 3 files changed, 66 insertions(+), 29 deletions(-)
---
diff --git a/src/tools/scenarios.c b/src/tools/scenarios.c
index 601da80..2e99ca5 100644
--- a/src/tools/scenarios.c
+++ b/src/tools/scenarios.c
@@ -77,6 +77,14 @@ gnm_scenario_item_set_value (GnmScenarioItem *sci, const GnmValue *v)
 	sci->value = value_dup (v);
 }
 
+gboolean
+gnm_scenario_item_valid (const GnmScenarioItem *sci)
+{
+	GnmExprTop const *texpr = sci ? sci->dep.texpr : NULL;
+	GnmValue const *vr = texpr ? gnm_expr_top_get_constant (texpr) : NULL;
+	return vr && vr->type == VALUE_CELLRANGE;
+}
+
 /* ------------------------------------------------------------------------- */
 
 typedef GnmValue * (*ScenarioValueCB) (int col, int row, GnmValue *v, gpointer data);
diff --git a/src/tools/scenarios.h b/src/tools/scenarios.h
index cb31293..3326df8 100644
--- a/src/tools/scenarios.h
+++ b/src/tools/scenarios.h
@@ -17,6 +17,7 @@ void gnm_scenario_item_set_range (GnmScenarioItem *sci,
 				  const GnmSheetRange *sr);
 void gnm_scenario_item_set_value (GnmScenarioItem *sci,
 				  const GnmValue *v);
+gboolean gnm_scenario_item_valid (const GnmScenarioItem *sci);
 
 /* ------------------------------------------------------------------------- */
 
diff --git a/src/xml-sax-write.c b/src/xml-sax-write.c
index bbab272..571f0c9 100644
--- a/src/xml-sax-write.c
+++ b/src/xml-sax-write.c
@@ -1043,9 +1043,63 @@ xml_write_solver (GnmOutputXML *state)
 }
 
 static void
+xml_write_scenario (GnmOutputXML *state, GnmScenario const *sc)
+{
+	GSList *l;
+	GnmParsePos pp;
+
+	parse_pos_init_sheet (&pp, sc->sheet);
+
+	gsf_xml_out_start_element (state->output, GNM "Scenario");
+
+	gsf_xml_out_add_cstr (state->output, "Name", sc->name);
+	if (sc->comment)
+		gsf_xml_out_add_cstr (state->output, "Comment", sc->comment);
+
+	for (l = sc->items; l; l = l->next) {
+		GnmScenarioItem const *sci = l->data;
+		GnmValue const *val = sci->value;
+		GString *str;
+		GnmConventionsOut out;
+
+		if (!gnm_scenario_item_valid (sci))
+			continue;
+
+		str = g_string_new (NULL);
+		gsf_xml_out_start_element (state->output, GNM "Item");
+
+		out.accum = str;
+		out.pp    = &pp;
+		out.convs = state->convs;
+
+		gnm_expr_top_as_gstring (sci->dep.texpr, &out);
+		gsf_xml_out_add_cstr (state->output, "Range", str->str);
+
+		if (val) {
+			gsf_xml_out_add_int (state->output,
+					     "ValueType",
+					     val->type);
+			if (VALUE_FMT (val) != NULL) {
+				const char *fmt = go_format_as_XL (VALUE_FMT (val));
+				gsf_xml_out_add_cstr (state->output, "ValueFormat", fmt);
+			}
+			g_string_truncate (str, 0);
+			value_get_as_gstring (val, str, state->convs);
+			gsf_xml_out_add_cstr (state->output, NULL, str->str);
+		}
+
+		gsf_xml_out_end_element (state->output); /* </gnm:Item> */
+		g_string_free (str, TRUE);
+	}
+
+	gsf_xml_out_end_element (state->output); /* </gnm:Scenario> */
+}
+
+
+static void
 xml_write_scenarios (GnmOutputXML *state)
 {
-	GList   *ptr;
+	GList *ptr;
 
 	if (state->sheet->scenarios == NULL)
 		return;
@@ -1053,34 +1107,8 @@ xml_write_scenarios (GnmOutputXML *state)
 	gsf_xml_out_start_element (state->output, GNM "Scenarios");
 
 	for (ptr = state->sheet->scenarios ; ptr != NULL ; ptr = ptr->next) {
-		GnmScenario const *s = (GnmScenario const *)ptr->data;
-#if 0
-		int       i, cols, rows;
-#endif
-
-		gsf_xml_out_start_element (state->output, GNM "Scenario");
-		gsf_xml_out_add_cstr (state->output, "Name", s->name);
-		gsf_xml_out_add_cstr (state->output, "Comment", s->comment);
-
-		/* Scenario: changing cells in a string form.  In a string
-		 * form so that we can in the future allow it to contain
-		 * multiple ranges without modifing the file format.*/
-		gsf_xml_out_add_cstr (state->output, "CellsStr", s->cell_sel_str);
-
-#if 0 /* CRACK CRACK CRACK need something cleaner */
-		/* Scenario: values. */
-		rows = range_height (&s->range);
-		cols = range_width (&s->range);
-		for (i = 0; i < cols * rows; i++) {
-			GString  *name = g_string_new (NULL);
-			g_string_append_printf (name, "V%d", i);
-			xml_node_set_value (scen, name->str,
-					    s->changing_cells [i]);
-			g_string_free (name, TRUE);
-		}
-#endif
-
-		gsf_xml_out_end_element (state->output); /* </gnm:Scenario> */
+		GnmScenario const *sc = ptr->data;
+		xml_write_scenario (state, sc);
 	}
 
 	gsf_xml_out_end_element (state->output); /* </gnm:Scenarios> */



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