[gnumeric] solver: move reading code to xml-sax-read.



commit 3d4e53072712abc320021aa8448c7e56e932759e
Author: Morten Welinder <terra gnome org>
Date:   Thu Nov 12 15:56:06 2009 -0500

    solver: move reading code to xml-sax-read.

 src/solver.h              |    3 -
 src/tools/solver/solver.c |  182 ---------------------------------------------
 src/xml-sax-read.c        |  121 +++++++++++++++++++++++++++++-
 3 files changed, 120 insertions(+), 186 deletions(-)
---
diff --git a/src/solver.h b/src/solver.h
index 38b2c85..530cd5b 100644
--- a/src/solver.h
+++ b/src/solver.h
@@ -82,9 +82,6 @@ gchar *          solver_reports        (WorkbookControl *wbc, Sheet *sheet,
 					gboolean limits, gboolean performance,
 					gboolean program, gboolean dual);
 
-/* Frees the data structure allocated for the results. */
-void             solver_results_free   (SolverResults *res);
-
 /* Returns a pointer to a input variable cell. */
 GnmCell		*solver_get_input_var (SolverResults *res, int n);
 
diff --git a/src/tools/solver/solver.c b/src/tools/solver/solver.c
index 30bd130..f148af8 100644
--- a/src/tools/solver/solver.c
+++ b/src/tools/solver/solver.c
@@ -51,188 +51,6 @@
 #include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
-#ifdef HAVE_TIMES
-#include <sys/times.h>
-#endif
-
-#define GNM 100
-#define CXML2C(s) ((char const *)(s))
-
-static inline gboolean
-attr_eq (const xmlChar *a, const char *s)
-{
-	return !strcmp (CXML2C (a), s);
-}
-
-/* ------------------------------------------------------------------------- */
-
-static void
-solver_constr_start (GsfXMLIn *xin, xmlChar const **attrs)
-{
-	int type = 0;
-	GnmSolverConstraint *c;
-	Sheet *sheet = gnm_xml_in_cur_sheet (xin);
-	GnmSolverParameters *sp = sheet->solver_parameters;
-	int lhs_col = 0, lhs_row = 0, rhs_col = 0, rhs_row = 0;
-	int cols = 1, rows = 1;
-	gboolean old = FALSE;
-	GnmParsePos pp;
-	GnmExprParseFlags flags = GNM_EXPR_PARSE_DEFAULT;
-
-	c = gnm_solver_constraint_new (sheet);
-
-	parse_pos_init_sheet (&pp, sheet);
-
-	for (; attrs && attrs[0] && attrs[1] ; attrs += 2) {
-		if (gnm_xml_attr_int (attrs, "Lcol", &lhs_col) ||
-		    gnm_xml_attr_int (attrs, "Lrow", &lhs_row) ||
-		    gnm_xml_attr_int (attrs, "Rcol", &rhs_col) ||
-		    gnm_xml_attr_int (attrs, "Rrow", &rhs_row) ||
-		    gnm_xml_attr_int (attrs, "Cols", &cols) ||
-		    gnm_xml_attr_int (attrs, "Rows", &rows))
-			old = TRUE;
-		else if (gnm_xml_attr_int (attrs, "Type", &type))
-			; /* Nothing */
-		else if (attr_eq (attrs[0], "lhs")) {
-			GnmValue *v = value_new_cellrange_parsepos_str
-				(&pp, CXML2C (attrs[1]), flags);
-			gnm_solver_constraint_set_lhs (c, v);
-		} else if (attr_eq (attrs[0], "rhs")) {
-			GnmValue *v = value_new_cellrange_parsepos_str
-				(&pp, CXML2C (attrs[1]), flags);
-			gnm_solver_constraint_set_rhs (c, v);
-		}
-	}
-
-	switch (type) {
-	case 1: c->type = GNM_SOLVER_LE; break;
-	case 2: c->type = GNM_SOLVER_GE; break;
-	case 4: c->type = GNM_SOLVER_EQ; break;
-	case 8: c->type = GNM_SOLVER_INTEGER; break;
-	case 16: c->type = GNM_SOLVER_BOOLEAN; break;
-	default: c->type = GNM_SOLVER_LE; break;
-	}
-
-	if (old)
-		gnm_solver_constraint_set_old (c, c->type,
-					       lhs_col, lhs_row,
-					       rhs_col, rhs_row,
-					       cols, rows);
-
-	sp->constraints = g_slist_append (sp->constraints, c);
-}
-
-void
-solver_param_read_sax (GsfXMLIn *xin, xmlChar const **attrs)
-{
-	Sheet *sheet = gnm_xml_in_cur_sheet (xin);
-	GnmSolverParameters *sp = sheet->solver_parameters;
-	int col = -1, row = -1;
-	int ptype;
-	GnmParsePos pp;
-	gboolean old = FALSE;
-
-	static GsfXMLInNode const dtd[] = {
-	  GSF_XML_IN_NODE (SHEET_SOLVER_CONSTR, SHEET_SOLVER_CONSTR, GNM, "Constr", GSF_XML_NO_CONTENT, &solver_constr_start, NULL),
-	  GSF_XML_IN_NODE_END
-	};
-	static GsfXMLInDoc *doc;
-
-	parse_pos_init_sheet (&pp, sheet);
-
-	for (; attrs && attrs[0] && attrs[1] ; attrs += 2) {
-		if (gnm_xml_attr_int (attrs, "ProblemType", &ptype)) {
-			sp->problem_type = (GnmSolverProblemType)ptype;
-		} else if (attr_eq (attrs[0], "Inputs")) {
-			GnmValue *v = value_new_cellrange_parsepos_str
-				(&pp,
-				 CXML2C (attrs[1]),
-				 GNM_EXPR_PARSE_DEFAULT);
-			gnm_solver_param_set_input (sp, v);
-		} else if (gnm_xml_attr_int (attrs, "TargetCol", &col) ||
-			   gnm_xml_attr_int (attrs, "TargetRow", &row)) {
-			old = TRUE;
-		} else if (attr_eq (attrs[0], "Target")) {
-			GnmValue *v = value_new_cellrange_parsepos_str
-				(&pp,
-				 CXML2C (attrs[1]),
-				 GNM_EXPR_PARSE_DEFAULT);
-			GnmSheetRange sr;
-			GnmCellRef cr;
-
-			if (!v ||
-			    (gnm_sheet_range_from_value (&sr, v), !range_is_singleton (&sr.range)))
-				continue;
-			gnm_cellref_init (&cr, sr.sheet,
-					  sr.range.start.col,
-					  sr.range.start.row,
-					  TRUE);
-			gnm_solver_param_set_target (sp, &cr);
-		} else if (gnm_xml_attr_int (attrs, "MaxTime", &(sp->options.max_time_sec)) ||
-			   gnm_xml_attr_int (attrs, "MaxIter", &(sp->options.max_iter)) ||
-			   gnm_xml_attr_bool (attrs, "NonNeg", &(sp->options.assume_non_negative)) ||
-			   gnm_xml_attr_bool (attrs, "Discr", &(sp->options.assume_discrete)) ||
-			   gnm_xml_attr_bool (attrs, "AutoScale", &(sp->options.automatic_scaling)) ||
-			   gnm_xml_attr_bool (attrs, "ShowIter", &(sp->options.show_iter_results)) ||
-			   gnm_xml_attr_bool (attrs, "AnswerR", &(sp->options.answer_report)) ||
-			   gnm_xml_attr_bool (attrs, "SensitivityR", &(sp->options.sensitivity_report)) ||
-			   gnm_xml_attr_bool (attrs, "LimitsR", &(sp->options.limits_report)) ||
-			   gnm_xml_attr_bool (attrs, "PerformR", &(sp->options.performance_report)) ||
-			   gnm_xml_attr_bool (attrs, "ProgramR", &(sp->options.program_report)))
-			; /* Nothing */
-	}
-
-	if (old &&
-	    col >= 0 && col < gnm_sheet_get_max_cols (sheet) &&
-	    row >= 0 && row < gnm_sheet_get_max_rows (sheet)) {
-		GnmCellRef cr;
-		gnm_cellref_init (&cr, NULL, col, row, TRUE);
-		gnm_solver_param_set_target (sp, &cr);
-	}
-
-	if (!doc)
-		doc = gsf_xml_in_doc_new (dtd, NULL);
-	gsf_xml_in_push_state (xin, doc, NULL, NULL, attrs);
-}
-
-
-
-void
-solver_results_free (SolverResults *res)
-{
-        int i;
-
-	for (i = 0; i < res->n_variables; i++)
-	        g_free (res->variable_names[i]);
-	for (i = 0; i < res->n_constraints; i++)
-	        g_free (res->constraint_names[i]);
-
-        g_free (res->optimal_values);
-	g_free (res->original_values);
-	g_free (res->target_name);
-	g_free (res->variable_names);
-	g_free (res->constraint_names);
-	g_free (res->shadow_prizes);
-	g_free (res->input_cells_array);
-	if (res->constraints_array)
-	        for (i = 0; i < res->n_constraints; i++)
-		        gnm_solver_constraint_free (res->constraints_array[i]);
-	g_free (res->constraints_array);
-	g_free (res->obj_coeff);
-	if (res->constr_coeff != NULL)
-	        for (i = 0; i < res->n_constraints; i++)
-		        g_free (res->constr_coeff[i]);
-	g_free (res->constr_coeff);
-	g_free (res->limits);
-	g_free (res->constr_allowable_increase);
-	g_free (res->constr_allowable_decrease);
-	g_free (res->slack);
-	g_free (res->lhs);
-	g_free (res->rhs);
-
-	memset (res, 0xff, sizeof (*res));
-	g_free (res);
-}
 
 /* ------------------------------------------------------------------------- */
 
diff --git a/src/xml-sax-read.c b/src/xml-sax-read.c
index cc0c276..12a591b 100644
--- a/src/xml-sax-read.c
+++ b/src/xml-sax-read.c
@@ -2273,9 +2273,127 @@ xml_sax_object_end (GsfXMLIn *xin, G_GNUC_UNUSED GsfXMLBlob *blob)
 }
 
 static void
+xml_sax_solver_constr_start (GsfXMLIn *xin, xmlChar const **attrs)
+{
+	int type = 0;
+	GnmSolverConstraint *c;
+	Sheet *sheet = gnm_xml_in_cur_sheet (xin);
+	GnmSolverParameters *sp = sheet->solver_parameters;
+	int lhs_col = 0, lhs_row = 0, rhs_col = 0, rhs_row = 0;
+	int cols = 1, rows = 1;
+	gboolean old = FALSE;
+	GnmParsePos pp;
+	GnmExprParseFlags flags = GNM_EXPR_PARSE_DEFAULT;
+
+	c = gnm_solver_constraint_new (sheet);
+
+	parse_pos_init_sheet (&pp, sheet);
+
+	for (; attrs && attrs[0] && attrs[1] ; attrs += 2) {
+		if (gnm_xml_attr_int (attrs, "Lcol", &lhs_col) ||
+		    gnm_xml_attr_int (attrs, "Lrow", &lhs_row) ||
+		    gnm_xml_attr_int (attrs, "Rcol", &rhs_col) ||
+		    gnm_xml_attr_int (attrs, "Rrow", &rhs_row) ||
+		    gnm_xml_attr_int (attrs, "Cols", &cols) ||
+		    gnm_xml_attr_int (attrs, "Rows", &rows))
+			old = TRUE;
+		else if (gnm_xml_attr_int (attrs, "Type", &type))
+			; /* Nothing */
+		else if (attr_eq (attrs[0], "lhs")) {
+			GnmValue *v = value_new_cellrange_parsepos_str
+				(&pp, CXML2C (attrs[1]), flags);
+			gnm_solver_constraint_set_lhs (c, v);
+		} else if (attr_eq (attrs[0], "rhs")) {
+			GnmValue *v = value_new_cellrange_parsepos_str
+				(&pp, CXML2C (attrs[1]), flags);
+			gnm_solver_constraint_set_rhs (c, v);
+		}
+	}
+
+	switch (type) {
+	default:
+	case 1: c->type = GNM_SOLVER_LE; break;
+	case 2: c->type = GNM_SOLVER_GE; break;
+	case 4: c->type = GNM_SOLVER_EQ; break;
+	case 8: c->type = GNM_SOLVER_INTEGER; break;
+	case 16: c->type = GNM_SOLVER_BOOLEAN; break;
+	}
+
+	if (old)
+		gnm_solver_constraint_set_old (c, c->type,
+					       lhs_col, lhs_row,
+					       rhs_col, rhs_row,
+					       cols, rows);
+
+	sp->constraints = g_slist_append (sp->constraints, c);
+}
+
+static void
 xml_sax_solver_start (GsfXMLIn *xin, xmlChar const **attrs)
 {
-	solver_param_read_sax (xin, attrs);
+	Sheet *sheet = gnm_xml_in_cur_sheet (xin);
+	GnmSolverParameters *sp = sheet->solver_parameters;
+	int col = -1, row = -1;
+	int ptype;
+	GnmParsePos pp;
+	gboolean old = FALSE;
+
+	parse_pos_init_sheet (&pp, sheet);
+
+	for (; attrs && attrs[0] && attrs[1] ; attrs += 2) {
+		if (gnm_xml_attr_int (attrs, "ProblemType", &ptype)) {
+			sp->problem_type = (GnmSolverProblemType)ptype;
+		} else if (attr_eq (attrs[0], "Inputs")) {
+			GnmValue *v = value_new_cellrange_parsepos_str
+				(&pp,
+				 CXML2C (attrs[1]),
+				 GNM_EXPR_PARSE_DEFAULT);
+			gnm_solver_param_set_input (sp, v);
+		} else if (gnm_xml_attr_int (attrs, "TargetCol", &col) ||
+			   gnm_xml_attr_int (attrs, "TargetRow", &row)) {
+			old = TRUE;
+		} else if (attr_eq (attrs[0], "Target")) {
+			GnmValue *v = value_new_cellrange_parsepos_str
+				(&pp,
+				 CXML2C (attrs[1]),
+				 GNM_EXPR_PARSE_DEFAULT);
+			GnmSheetRange sr;
+			GnmCellRef cr;
+			gboolean  bad;
+
+			bad = (!v ||
+			       (gnm_sheet_range_from_value (&sr, v), !range_is_singleton (&sr.range)));
+			value_release (v);
+			if (bad) {
+				continue;
+			}
+
+			gnm_cellref_init (&cr, sr.sheet,
+					  sr.range.start.col,
+					  sr.range.start.row,
+					  TRUE);
+			gnm_solver_param_set_target (sp, &cr);
+		} else if (gnm_xml_attr_int (attrs, "MaxTime", &(sp->options.max_time_sec)) ||
+			   gnm_xml_attr_int (attrs, "MaxIter", &(sp->options.max_iter)) ||
+			   gnm_xml_attr_bool (attrs, "NonNeg", &(sp->options.assume_non_negative)) ||
+			   gnm_xml_attr_bool (attrs, "Discr", &(sp->options.assume_discrete)) ||
+			   gnm_xml_attr_bool (attrs, "AutoScale", &(sp->options.automatic_scaling)) ||
+			   gnm_xml_attr_bool (attrs, "ShowIter", &(sp->options.show_iter_results)) ||
+			   gnm_xml_attr_bool (attrs, "AnswerR", &(sp->options.answer_report)) ||
+			   gnm_xml_attr_bool (attrs, "SensitivityR", &(sp->options.sensitivity_report)) ||
+			   gnm_xml_attr_bool (attrs, "LimitsR", &(sp->options.limits_report)) ||
+			   gnm_xml_attr_bool (attrs, "PerformR", &(sp->options.performance_report)) ||
+			   gnm_xml_attr_bool (attrs, "ProgramR", &(sp->options.program_report)))
+			; /* Nothing */
+	}
+
+	if (old &&
+	    col >= 0 && col < gnm_sheet_get_max_cols (sheet) &&
+	    row >= 0 && row < gnm_sheet_get_max_rows (sheet)) {
+		GnmCellRef cr;
+		gnm_cellref_init (&cr, NULL, col, row, TRUE);
+		gnm_solver_param_set_target (sp, &cr);
+	}
 }
 
 static void
@@ -2603,6 +2721,7 @@ GSF_XML_IN_NODE_FULL (START, WB, GNM, "Workbook", GSF_XML_NO_CONTENT, TRUE, TRUE
 	GSF_XML_IN_NODE (SHEET_LAYOUT, SHEET_FREEZEPANES, GNM, "FreezePanes", GSF_XML_NO_CONTENT, &xml_sax_sheet_freezepanes, NULL),
 
       GSF_XML_IN_NODE (SHEET, SHEET_SOLVER, GNM, "Solver", GSF_XML_NO_CONTENT, xml_sax_solver_start, NULL),
+	GSF_XML_IN_NODE (SHEET_SOLVER, SOLVER_CONSTR, GNM, "Constr", GSF_XML_NO_CONTENT, xml_sax_solver_constr_start, NULL),
       GSF_XML_IN_NODE (SHEET, SHEET_SCENARIOS, GNM, "Scenarios", GSF_XML_NO_CONTENT, NULL, NULL),
         GSF_XML_IN_NODE (SHEET_SCENARIOS, SHEET_SCENARIO, GNM, "Scenario", GSF_XML_NO_CONTENT, NULL, NULL),
 



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