gnumeric r16300 - in trunk: . src src/tools/solver



Author: mortenw
Date: Mon Jan 28 19:04:01 2008
New Revision: 16300
URL: http://svn.gnome.org/viewvc/gnumeric?rev=16300&view=rev

Log:

2008-01-28  Morten Welinder  <terra gnome org>

	* src/xml-sax-read.c (xml_sax_solver_start): New function.
	(gnm_xml_attr_bool): Renamed from xml_sax_attr_bool and made
	public.



Modified:
   trunk/ChangeLog
   trunk/NEWS
   trunk/src/solver.h
   trunk/src/tools/solver/ChangeLog
   trunk/src/tools/solver/solver.c
   trunk/src/xml-io.h
   trunk/src/xml-sax-read.c

Modified: trunk/NEWS
==============================================================================
--- trunk/NEWS	(original)
+++ trunk/NEWS	Mon Jan 28 19:04:01 2008
@@ -14,6 +14,7 @@
 	* Fix xls read crash.  [#505330]
 	* Plug csv leak.
 	* Fix inter-sheet cut problem.  [#511706]
+	* Fix loading of solver constraints.
 
 --------------------------------------------------------------------------
 Gnumeric 1.8.0

Modified: trunk/src/solver.h
==============================================================================
--- trunk/src/solver.h	(original)
+++ trunk/src/solver.h	Mon Jan 28 19:04:01 2008
@@ -4,6 +4,7 @@
 
 #include "gnumeric.h"
 #include "numbers.h"
+#include <gsf/gsf-libxml.h>
 
 
 G_BEGIN_DECLS
@@ -246,6 +247,8 @@
 void              solver_delete_rows    (Sheet *sheet, int row, int count);
 void              solver_delete_cols    (Sheet *sheet, int col, int count);
 
+void              solver_param_read_sax (GsfXMLIn *xin, xmlChar const **attrs);
+
 #else /* !GNM_ENABLE_SOLVER */
 
 #define solver_param_new() NULL
@@ -256,6 +259,7 @@
 #define solver_delete_cols(sheet, col, count)	do {} while(0)
 #define solver_delete_rows(sheet, row, count)	do {} while(0)
 #define solver_constraint_destroy(c) do {} while(0)
+#define solver_param_read_sax (void)
 
 #endif
 

Modified: trunk/src/tools/solver/solver.c
==============================================================================
--- trunk/src/tools/solver/solver.c	(original)
+++ trunk/src/tools/solver/solver.c	Mon Jan 28 19:04:01 2008
@@ -42,14 +42,19 @@
 #include "api.h"
 #include "gutils.h"
 #include <goffice/utils/go-glib-extras.h>
+#include "xml-io.h"
 
 #include <math.h>
+#include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
 #ifdef HAVE_TIMES
 #include <sys/times.h>
 #endif
 
+#define GNM 0
+#define CXML2C(s) ((char const *)(s))
+
 /* ------------------------------------------------------------------------- */
 
 
@@ -83,6 +88,93 @@
 	g_free (sp);
 }
 
+static void
+solver_constr_start (GsfXMLIn *xin, xmlChar const **attrs)
+{
+	int type;
+	SolverConstraint *c;
+	int i;
+	Sheet *sheet = gnm_xml_in_cur_sheet (xin);
+	SolverParameters *sp = sheet->solver_parameters;
+
+	c = g_new0 (SolverConstraint, 1);
+
+	for (i = 0; attrs != NULL && attrs[i] && attrs[i + 1] ; i += 2) {
+		if (gnm_xml_attr_int (attrs+i, "Lcol", &c->lhs.col) ||
+		    gnm_xml_attr_int (attrs+i, "Lrow", &c->lhs.row) ||
+		    gnm_xml_attr_int (attrs+i, "Rcol", &c->rhs.col) ||
+		    gnm_xml_attr_int (attrs+i, "Rrow", &c->rhs.row) ||
+		    gnm_xml_attr_int (attrs+i, "Cols", &c->cols) ||
+		    gnm_xml_attr_int (attrs+i, "Rows", &c->rows) ||
+		    gnm_xml_attr_int (attrs+i, "Type", &type))
+			; /* Nothing */
+	}
+
+	switch (type) {
+	case 1: c->type = SolverLE; break;
+	case 2: c->type = SolverGE; break;
+	case 4: c->type = SolverEQ; break;
+	case 8: c->type = SolverINT; break;
+	case 16: c->type = SolverBOOL; break;
+	default: c->type = SolverLE; break;
+	}
+
+#ifdef GNM_ENABLE_SOLVER
+	c->str = write_constraint_str (c->lhs.col, c->lhs.row,
+				       c->rhs.col, c->rhs.row,
+				       c->type, c->cols, c->rows);
+#endif
+	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);
+	SolverParameters *sp = sheet->solver_parameters;
+	int i;
+	int col = -1, row = -1;
+	int ptype;
+
+	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;
+
+	for (i = 0; attrs != NULL && attrs[i] && attrs[i + 1] ; i += 2) {
+		if (gnm_xml_attr_int (attrs+i, "ProblemType", &ptype))
+			sp->problem_type = (SolverProblemType)ptype;
+		else 		if (strcmp (CXML2C (attrs[i]), "Inputs") == 0) {
+			g_free (sp->input_entry_str);
+			sp->input_entry_str = g_strdup (CXML2C (attrs[i+1]));
+		} else if (gnm_xml_attr_int (attrs+i, "TargetCol", &col) ||
+			   gnm_xml_attr_int (attrs+i, "TargetRow", &row) ||
+			   gnm_xml_attr_int (attrs+i, "MaxTime", &(sp->options.max_time_sec)) ||
+			   gnm_xml_attr_int (attrs+i, "MaxIter", &(sp->options.max_iter)) ||
+			   gnm_xml_attr_bool (attrs+i, "NonNeg", &(sp->options.assume_non_negative)) ||
+			   gnm_xml_attr_bool (attrs+i, "Discr", &(sp->options.assume_discrete)) ||
+			   gnm_xml_attr_bool (attrs+i, "AutoScale", &(sp->options.automatic_scaling)) ||
+			   gnm_xml_attr_bool (attrs+i, "ShowIter", &(sp->options.show_iter_results)) ||
+			   gnm_xml_attr_bool (attrs+i, "AnswerR", &(sp->options.answer_report)) ||
+			   gnm_xml_attr_bool (attrs+i, "SensitivityR", &(sp->options.sensitivity_report)) ||
+			   gnm_xml_attr_bool (attrs+i, "LimitsR", &(sp->options.limits_report)) ||
+			   gnm_xml_attr_bool (attrs+i, "PerformR", &(sp->options.performance_report)) ||
+			   gnm_xml_attr_bool (attrs+i, "ProgramR", &(sp->options.program_report)))
+			; /* Nothing */
+	}
+
+	if (col >= 0 && col < SHEET_MAX_COLS &&
+	    row >= 0 && row < SHEET_MAX_ROWS)
+		sp->target_cell = sheet_cell_fetch (sheet, col, row);
+
+	if (!doc)
+		doc = gsf_xml_in_doc_new (dtd, NULL);
+	gsf_xml_in_push_state (xin, doc, NULL, NULL, attrs);
+}
+
+
+
 static SolverResults *
 solver_results_init (const SolverParameters *sp)
 {

Modified: trunk/src/xml-io.h
==============================================================================
--- trunk/src/xml-io.h	(original)
+++ trunk/src/xml-io.h	Mon Jan 28 19:04:01 2008
@@ -60,6 +60,8 @@
 			       char const *name, int * res);
 gboolean gnm_xml_attr_double  (xmlChar const * const *attrs,
 			       char const *name, double * res);
+gboolean gnm_xml_attr_bool    (xmlChar const * const *attrs,
+			       char const *name, gboolean *res);
 
 SheetObject *gnm_xml_in_cur_obj   (GsfXMLIn const *xin);
 Sheet	    *gnm_xml_in_cur_sheet (GsfXMLIn const *xin);

Modified: trunk/src/xml-sax-read.c
==============================================================================
--- trunk/src/xml-sax-read.c	(original)
+++ trunk/src/xml-sax-read.c	Mon Jan 28 19:04:01 2008
@@ -31,6 +31,7 @@
 #include "sheet-filter.h"
 #include "sheet.h"
 #include "ranges.h"
+#include "solver.h"
 #include "style.h"
 #include "style-border.h"
 #include "style-color.h"
@@ -118,8 +119,8 @@
 	return *end == '\0';
 }
 
-static gboolean
-xml_sax_attr_bool (xmlChar const * const *attrs, char const *name, gboolean *res)
+gboolean
+gnm_xml_attr_bool (xmlChar const * const *attrs, char const *name, gboolean *res)
 {
 	g_return_val_if_fail (attrs != NULL, FALSE);
 	g_return_val_if_fail (attrs[0] != NULL, FALSE);
@@ -487,9 +488,9 @@
 	double	 d;
 
 	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2)
-		if (xml_sax_attr_bool (attrs, "ManualRecalc", &b))
+		if (gnm_xml_attr_bool (attrs, "ManualRecalc", &b))
 			workbook_set_recalcmode (state->wb, !b);
-		else if (xml_sax_attr_bool (attrs, "EnableIteration", &b))
+		else if (gnm_xml_attr_bool (attrs, "EnableIteration", &b))
 			workbook_iteration_enabled (state->wb, b);
 		else if (gnm_xml_attr_int  (attrs, "MaxIterations", &i))
 			workbook_iteration_max_number (state->wb, i);
@@ -570,27 +571,27 @@
 	state->sheet_zoom = 1.; /* default */
 
 	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2)
-		if (xml_sax_attr_bool (attrs, "DisplayFormulas", &tmp))
+		if (gnm_xml_attr_bool (attrs, "DisplayFormulas", &tmp))
 			state->display_formulas = tmp;
-		else if (xml_sax_attr_bool (attrs, "HideZero", &tmp))
+		else if (gnm_xml_attr_bool (attrs, "HideZero", &tmp))
 			state->hide_zero = tmp;
-		else if (xml_sax_attr_bool (attrs, "HideGrid", &tmp))
+		else if (gnm_xml_attr_bool (attrs, "HideGrid", &tmp))
 			state->hide_grid = tmp;
-		else if (xml_sax_attr_bool (attrs, "HideColHeader", &tmp))
+		else if (gnm_xml_attr_bool (attrs, "HideColHeader", &tmp))
 			state->hide_col_header = tmp;
-		else if (xml_sax_attr_bool (attrs, "HideRowHeader", &tmp))
+		else if (gnm_xml_attr_bool (attrs, "HideRowHeader", &tmp))
 			state->hide_row_header = tmp;
-		else if (xml_sax_attr_bool (attrs, "DisplayOutlines", &tmp))
+		else if (gnm_xml_attr_bool (attrs, "DisplayOutlines", &tmp))
 			state->display_outlines = tmp;
-		else if (xml_sax_attr_bool (attrs, "OutlineSymbolsBelow", &tmp))
+		else if (gnm_xml_attr_bool (attrs, "OutlineSymbolsBelow", &tmp))
 			state->outline_symbols_below = tmp;
-		else if (xml_sax_attr_bool (attrs, "OutlineSymbolsRight", &tmp))
+		else if (gnm_xml_attr_bool (attrs, "OutlineSymbolsRight", &tmp))
 			state->outline_symbols_right = tmp;
 		else if (xml_sax_attr_enum (attrs, "Visibility", GNM_SHEET_VISIBILITY_TYPE, &tmpi))
 			state->visibility = tmpi;
-		else if (xml_sax_attr_bool (attrs, "RTL_Layout", &tmp))
+		else if (gnm_xml_attr_bool (attrs, "RTL_Layout", &tmp))
 			state->text_is_rtl = tmp;
-		else if (xml_sax_attr_bool (attrs, "Protected", &tmp))
+		else if (gnm_xml_attr_bool (attrs, "Protected", &tmp))
 			state->is_protected = tmp;
 		else if (strcmp (CXML2C (attrs[0]), "ExprConvention") == 0)
 			state->expr_conv_name = g_strdup (attrs[1]);
@@ -1255,7 +1256,7 @@
 
 		else if (gnm_xml_attr_int (attrs, "WrapText", &val))
 			gnm_style_set_wrap_text (state->style, val);
-		else if (xml_sax_attr_bool (attrs, "ShrinkToFit", &val))
+		else if (gnm_xml_attr_bool (attrs, "ShrinkToFit", &val))
 			gnm_style_set_shrink_to_fit (state->style, val);
 		else if (gnm_xml_attr_int (attrs, "Rotation", &val)) {
 			/* Work around a bug pre 1.5.1 that would allow
@@ -1410,9 +1411,9 @@
 			state->validation.title = g_strdup (CXML2C (attrs[1]));
 		} else if (attr_eq (attrs[0], "Message")) {
 			state->validation.msg = g_strdup (CXML2C (attrs[1]));
-		} else if (xml_sax_attr_bool (attrs, "AllowBlank", &b_dummy)) {
+		} else if (gnm_xml_attr_bool (attrs, "AllowBlank", &b_dummy)) {
 			state->validation.allow_blank = b_dummy;
-		} else if (xml_sax_attr_bool (attrs, "UseDropdown", &b_dummy)) {
+		} else if (gnm_xml_attr_bool (attrs, "UseDropdown", &b_dummy)) {
 			state->validation.use_dropdown = b_dummy;
 		} else
 			unknown_attr (xin, attrs);
@@ -1905,10 +1906,10 @@
 	for (i = 0; attrs != NULL && attrs[i] && attrs[i + 1] ; i += 2)
 		if (attr_eq (attrs[i], "Type"))   type = CXML2C (attrs[i + 1]);
 		else if (gnm_xml_attr_int (attrs+i, "Index", &cond_num)) ;
-		else if (xml_sax_attr_bool (attrs, "Top", &top)) ;
-		else if (xml_sax_attr_bool (attrs, "Items", &items)) ;
+		else if (gnm_xml_attr_bool (attrs, "Top", &top)) ;
+		else if (gnm_xml_attr_bool (attrs, "Items", &items)) ;
 		else if (gnm_xml_attr_double  (attrs, "Count", &bucket_count)) ;
-		else if (xml_sax_attr_bool (attrs, "IsAnd", &is_and)) ;
+		else if (gnm_xml_attr_bool (attrs, "IsAnd", &is_and)) ;
 		else if (attr_eq (attrs[i], "Op0")) xml_sax_filter_operator (state, &op0, attrs[i + 1]);
 		else if (attr_eq (attrs[i], "Op1")) xml_sax_filter_operator (state, &op1, attrs[i + 1]);
 		/*
@@ -2105,6 +2106,12 @@
 }
 
 static void
+xml_sax_solver_start (GsfXMLIn *xin, xmlChar const **attrs)
+{
+	solver_param_read_sax (xin, attrs);
+}
+
+static void
 xml_sax_named_expr_end (GsfXMLIn *xin, G_GNUC_UNUSED GsfXMLBlob *blob)
 {
 	XMLSaxParseState *state = (XMLSaxParseState *)xin->user_state;
@@ -2403,7 +2410,7 @@
       GSF_XML_IN_NODE (SHEET, SHEET_LAYOUT, GNM, "SheetLayout", GSF_XML_NO_CONTENT, &xml_sax_sheet_layout, NULL),
 	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, NULL, NULL),
+      GSF_XML_IN_NODE (SHEET, SHEET_SOLVER, GNM, "Solver", GSF_XML_NO_CONTENT, xml_sax_solver_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]