parser.c 13.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * mps.c: MPS file parser.
 *
 * Authors:
 *   Jukka-Pekka Iivonen <jiivonen@hutcs.cs.hut.fi>
 *
 *      Reads an MPS file and stores the data in it in data structures
 *      defined in mps.h.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#include <gnumeric-config.h>
#include <gnumeric.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "cell.h"
#include "sheet.h"
#include "value.h"
#include "file.h"
#include "io-context.h"
#include "workbook-view.h"
#include "workbook.h"
#include "plugin-util.h"
#include "module-plugin-defs.h"
#include "ranges.h"
#include "style.h"
#include "value.h"
#include "solver.h"
#include "sheet-style.h"
#include "parse-util.h"
#include "mps.h"

#include <libgnome/gnome-i18n.h>


/************************************************************************
 *
 * Parser's low level stuff.
 */

/*---------------------------------------------------------------------*/

/* Read a line from the file. */
static gboolean
mps_get_line (MpsInputContext *ctxt)
{
65
66
67
68
	do {
		ctxt->line = gsf_input_textline_ascii_gets (ctxt->input);
		if (ctxt->line == NULL)
			return FALSE;
69
70
		/* Check if a comment or empty line */
	} while (ctxt->line[0] == '*' || ctxt->line[0] == '\0');
71
72
73
74
75
76
77
78

	return TRUE;
}

static gboolean
mps_parse_data (gchar *str, gchar *type, gchar *name1, gchar *name2,
		gchar *value1, gchar *name3, gchar *value2)
{
79
        gint  i;
80
81
82
83
	gchar *n1 = name1;
	gchar *n2 = name2;
	gchar *n3 = name3;

84
	for (i = 0; i < 8; i++)
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
	        name1[i] = name2[i] = name3[i] = ' ';
	*value2 = *name3 = '\0';
        if (!(*str) || *str++ != ' ' || !(*str))
	        return FALSE;

	/* Type field is present */
	if (*str != ' ') {
	        *type++ = *str++;
		if (!(*str))
		        return FALSE;
		if (*str != ' ')
		        *type++ = *str++;
		else
		        str++;
		*type = '\0';
	} else
	        str += 2;

	/* Label 1 */
	if (!(*str) || *str++ != ' ')
	        return FALSE;
	for (i=5; i<=12; i++, str++) {
	        *name1++ = *str;
	        if (!(*str))
		        goto ok_out;
	}
	*name1 = '\0';

	/* Label 2 */
	if (*str == '\0')
	        goto ok_out;
	if (*str++ != ' ')
	        return FALSE;
	if (*str == '\0')
	        goto ok_out;
	if (*str++ != ' ')
	        return FALSE;
122
	for (i = 15; i <= 22; i++, str++) {
123
124
125
126
127
128
129
130
131
	        *name2++ = *str;
		if (!(*str))
		        return FALSE;
	}
	*name2 = '\0';

	/* Value 1 */
	if (!(*str) || *str++ != ' ' || !(*str) || *str++ != ' ')
	        return FALSE;
132
	for (i = 25; i <= 36; i++, str++) {
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
	        *value1++ = *str;
		if (!(*str))
		        goto ok_out;
	}
	*value1 = '\0';

	/* Label 3 */
	if (!(*str))
	        goto ok_out;
	if (*str++ != ' ')
	        return FALSE;
	if (!(*str))
	        goto ok_out;
	if (*str++ != ' ')
	        return FALSE;
	if (!(*str))
	        goto ok_out;
	if (*str++ != ' ')
	        return FALSE;
152
	for (i = 40; i <= 47; i++, str++) {
153
154
155
156
157
158
159
160
161
	        *name3++ = *str;
		if (!(*str))
		        return FALSE;
	}
	*name3 = '\0';

	/* Value 2 */
	if (!(*str) || *str++ != ' ' || !(*str) || *str++ != ' ')
	        return FALSE;
162
	for (i = 50; i <= 61; i++, str++) {
163
164
165
166
167
168
169
	        *value2++ = *str;
		if (!(*str))
		        goto ok_out;
	}
	*value2 = '\0';

 ok_out:
170
	for (i = 7; i >= 0; i--)
171
172
173
	        if (n1[i] != ' ')
		        break;
	n1[i+1] = '\0';
174
	for (i = 7; i >= 0; i--)
175
176
177
	        if (n2[i] != ' ')
		        break;
	n2[i+1] = '\0';
178
	for (i = 7; i >= 0; i--)
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
	        if (n3[i] != ' ')
		        break;
	n3[i+1] = '\0';

	return TRUE;
}

/************************************************************************
 *
 * Parser.
 */

/*
 * NAME section parsing.  Saves the program name into `ctxt->name'.
 *
 * Returns FALSE on error.
 */
static gboolean
mps_parse_name (MpsInputContext *ctxt)
{
        while (1) {
	        gchar *line;

		if (!mps_get_line (ctxt))
		        return FALSE;

		if (strncmp (ctxt->line, "NAME", 4) == 0
		    && isspace ((unsigned char)(ctxt->line[4]))) {
		        line = ctxt->line + 5;
			while (isspace ((unsigned char) *line))
			        line++;

211
			ctxt->name = g_strdup (ctxt->line);
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
			break;
		} else
		        return FALSE;
	}

	return TRUE;
}


/* Add one ROW definition. */
gboolean
mps_add_row (MpsInputContext *ctxt, MpsRowType type, gchar *txt)
{
        MpsRow *row;
	int    len;

        while (isspace ((unsigned char) *txt))
	          txt++;

	row = g_new (MpsRow, 1);
	len = strlen(txt);

	if (len == 0)
	          return FALSE;

237
	row->name = g_strdup (txt);
238
239
240
241
	row->type = type;

	if (type == ObjectiveRow)
	          ctxt->objective_row = row;
242
243
244
245
246
247
	else {
		row->index = ctxt->n_rows;
		ctxt->n_rows += 1;

		ctxt->rows = g_slist_prepend (ctxt->rows, row);
	}
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305

	return TRUE;
}

/*
 * ROWS section parsing.  Saves the number of rows into `ctxt->n_rows'.
 * The rows are saved into `ctxt->rows' which is a GSList containing
 * MpsRow elements.  These elements get their `name', `type', and
 * `index' fields set.
 *
 * Returns FALSE on error.
 */
static gboolean
mps_parse_rows (MpsInputContext *ctxt)
{
	gchar  type[3], n1[10], n2[10], n3[10], v1[20], v2[20];
	GSList *tmp;

	while (1) {
	        if (!mps_get_line (ctxt))
		        return FALSE;

		if (strncmp (ctxt->line, "ROWS", 4) == 0)
		        break;
		else
		        return FALSE;
	}

	while (1) {
	        if (!mps_get_line (ctxt))
		        return FALSE;

		if (!mps_parse_data (ctxt->line, type, n1, n2, v1, n3, v2)) {
		        if (ctxt->line[0] != ' ')
			        goto ok_out;
			else
			        return FALSE;
		}

		if (strcmp (type, "E") == 0) {
		        if (!mps_add_row (ctxt, EqualityRow, n1))
			        return FALSE;
		} else if (strcmp (type, "L") == 0) {
		        if (!mps_add_row (ctxt, LessOrEqualRow, n1))
			        return FALSE;
		} else if (strcmp (type, "G") == 0) {
		        if (!mps_add_row (ctxt, GreaterOrEqualRow, n1))
			        return FALSE;
		} else if (strcmp (type, "N") == 0) {
		        if (!mps_add_row (ctxt, ObjectiveRow, n1))
			        return FALSE;
		} else
		        return FALSE;
	}

 ok_out:
	for (tmp = ctxt->rows; tmp != NULL; tmp = tmp->next) {
	        MpsRow *row = (MpsRow *) tmp->data;
306
		g_hash_table_insert (ctxt->row_hash, row->name, (gpointer) row);
307
	}
308
309
310
311
	g_hash_table_insert (ctxt->row_hash, ctxt->objective_row->name,
			     (gpointer) ctxt->objective_row);
	ctxt->objective_row->index = ctxt->n_rows;
	ctxt->n_rows += 1;
312
313
314
315
316
317
318
319
320
321

	return TRUE;
}


/* Add one COLUMN definition. */
static gboolean
mps_add_column (MpsInputContext *ctxt, gchar *row_name, gchar *col_name,
		gchar *value_str)
{
322
323
        MpsCol     *col;
	MpsRow     *row;
324
325
326
327
328
	MpsColInfo *i;

	row = (MpsRow *) g_hash_table_lookup (ctxt->row_hash, row_name);
	if (row == NULL)
	          return FALSE;
329
330
331
332

	col        = g_new (MpsCol, 1);
	col->row   = row;
	col->name  = g_strdup (col_name);
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
	col->value = atof (value_str);
	ctxt->cols = g_slist_prepend (ctxt->cols, col);

	i = (MpsColInfo *) g_hash_table_lookup (ctxt->col_hash, col_name);
	if (i == NULL) {
	          i = g_new (MpsColInfo, 1);
		  i->index = ctxt->n_cols;
		  i->name = strcpy (g_malloc (strlen (col_name) + 1), col_name);
		  ctxt->n_cols += 1;
		  g_hash_table_insert (ctxt->col_hash, col->name, (gpointer) i);
	}

	return TRUE;
}

/*
 * COLUMNS section parsing.  Saves the number of columns into `ctxt->n_cols'.
 * The columns are saved into `ctxt->cols' which is a GSList containing
 * MpsCol elements.  Fields `row', `name' and `value' are set of each element.
 *
 * Keeps track of the column names using `ctxt->col_hash'.
 *
 * Returns FALSE on error.
 */
static gboolean
mps_parse_columns (MpsInputContext *ctxt)
{
	gchar type[3], n1[10], n2[10], n3[10], v1[20], v2[20];

	while (1) {
	        if (strncmp (ctxt->line, "COLUMNS", 7) == 0)
		        break;
		else
		        return FALSE;

		if (!mps_get_line (ctxt))
		        return FALSE;
	}

	while (1) {
	        if (!mps_get_line (ctxt))
		        return FALSE;

		if (!mps_parse_data (ctxt->line, type, n1, n2, v1, n3, v2)) {
		        if (ctxt->line[0] != ' ')
			        return TRUE;
			else
			        return FALSE;
		}

		if (!mps_add_column (ctxt, n2, n1, v1))
		        return FALSE;

		/* Optional second column definition */
		if (*v2)
		        if (!mps_add_column (ctxt, n3, n1, v2))
			        return FALSE;
	}

	return TRUE;
}


/* Add one RHS definition. */
static gboolean
mps_add_rhs (MpsInputContext *ctxt, gchar *rhs_name, gchar *row_name,
	     gchar *value_str)
{
        MpsRhs *rhs;

403
	rhs       = g_new (MpsRhs, 1);
404
	rhs->name = g_strdup (rhs_name);
405
	rhs->row  = (MpsRow *) g_hash_table_lookup (ctxt->row_hash, row_name);
406
407
408
	if (rhs->row == NULL)
	          return FALSE;
	rhs->value = atof (value_str);
409
	ctxt->rhs  = g_slist_prepend (ctxt->rhs, rhs);
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465

	return TRUE;
}

/*
 * RHS section parsing.  Saves the RHS entries into ctxt->rhs list (GSList).
 * MpsRhs is the type of the elements in the list.  Fields `name', `row',
 * and `value' are stored into each element.
 *
 * Returns FALSE on error.
 */
static gboolean
mps_parse_rhs (MpsInputContext *ctxt)
{
	gchar type[3], rhs_name[10], row_name[10], value[20], n2[10], v2[20];

	if (strncmp (ctxt->line, "RHS", 3) != 0 || ctxt->line[3] != '\0')
	        return FALSE;

	while (1) {
	        if (!mps_get_line (ctxt))
		        return FALSE;

		if (!mps_parse_data (ctxt->line, type, rhs_name, row_name,
				     value, n2, v2)) {
		        if (ctxt->line[0] != ' ')
			        return TRUE;
			else
			        return FALSE;
		}

		if (!mps_add_rhs (ctxt, rhs_name, row_name, value))
		        return FALSE;

		/* Optional second RHS definition */
		if (*v2)
			if (!mps_add_rhs (ctxt, rhs_name, n2, v2))
			        return FALSE;
	}

	return TRUE;
}


/* Add one BOUND definition. */
static gboolean
mps_add_bound (MpsInputContext *ctxt, MpsBoundType type, gchar *bound_name,
	       gchar *col_name, gchar *value_str)
{
        MpsBound   *bound;
	MpsColInfo *info;

	info = (MpsColInfo *) g_hash_table_lookup (ctxt->col_hash, col_name);
	if (info == NULL)
	        return FALSE;  /* Column is not defined */

466
467
	bound            = g_new (MpsBound, 1);
	bound->name      = g_new (gchar, strlen (bound_name) + 13);
468
469
	sprintf(bound->name, "Bound #%d: %s", ctxt->n_bounds + 1, bound_name);
	bound->col_index = info->index;
470
	bound->value     = atof (value_str);
Jukka Pekka's avatar
Jukka Pekka committed
471
	bound->type      = type;
472
	ctxt->bounds     = g_slist_prepend (ctxt->bounds, bound);
473
474
475
476
477
	(ctxt->n_bounds)++;

	return TRUE;
}

478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
 * RANGES section parsing.  Ranges are currently not supported.
 *
 * Returns FALSE on error.
 */
static gboolean
mps_parse_ranges (MpsInputContext *ctxt)
{
	gchar        type[3], n1[10], n2[10], v1[20], n3[10], v2[20];

	if (strncmp (ctxt->line, "ENDATA", 6) == 0)
	        return TRUE;

	if (strncmp (ctxt->line, "RANGES", 6) != 0 || ctxt->line[6] != '\0')
	        return TRUE;
}

495
496
497
498
499
500
501
502
503
504
/*
 * BOUNDS section parsing.  Saves the bounds into `ctxt->bounds' GSList.
 * Each list element is MpsBound, and their `name', `col_index', and `value'
 * fields are stored.
 *
 * Returns FALSE on error.
 */
static gboolean
mps_parse_bounds (MpsInputContext *ctxt)
{
505
506
	gchar        type[3], n1[10], n2[10], v1[20], n3[10], v2[20];
	MpsBoundType t;
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524

	if (strncmp (ctxt->line, "ENDATA", 6) == 0)
	        return TRUE;

	if (strncmp (ctxt->line, "BOUNDS", 6) != 0 || ctxt->line[6] != '\0')
	        return FALSE;

	while (1) {
	        if (!mps_get_line (ctxt))
		        return FALSE;

		if (!mps_parse_data (ctxt->line, type, n1, n2, v1, n3, v2)) {
		        if (ctxt->line[0] != ' ')
			        return TRUE;
			else
			        return FALSE;
		}

525
526
527
528
		if (strncmp (type, "UP", 2) == 0)
			t = UpperBound;
		else if (strncmp (type, "LO", 2) == 0)
			t = LowerBound;
Jukka Pekka's avatar
Jukka Pekka committed
529
530
		else if (strncmp (type, "FX", 2) == 0)
			t = FixedVariable;
531
532
533
534
535
		else
		        return FALSE; /* Not all bound types are implemented */

		if (!mps_add_bound (ctxt, t, n1, n2, v1))
			return FALSE;
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
	}
}

/*
 * MPS Parser.
 */
void
mps_parse_file (MpsInputContext *ctxt)
{
        if (!mps_parse_name (ctxt)) {
	        gnumeric_io_error_info_set
			(ctxt->io_context,
			 error_info_new_printf (_("Problem name was not "
						  "defined in the file.")));
	} else if (!mps_parse_rows (ctxt)) {
	        gnumeric_io_error_info_set
			(ctxt->io_context,
			 error_info_new_printf (_("Invalid ROWS section in "
						  "the file.")));
	} else if (!mps_parse_columns (ctxt)) {
	        gnumeric_io_error_info_set
			(ctxt->io_context,
			 error_info_new_printf (_("Invalid COLUMNS section "
						  "in the file.")));
	} else if (!mps_parse_rhs (ctxt)) {
	        gnumeric_io_error_info_set
			(ctxt->io_context,
			 error_info_new_printf (_("Invalid RHS section in the "
						  "file.")));
565
566
567
568
569
	} else if (!mps_parse_ranges (ctxt)) {
	        gnumeric_io_error_info_set
			(ctxt->io_context,
			 error_info_new_printf (_("Invalid RANGES section in "
						  "the file.")));
570
571
572
573
574
575
576
	} else if (!mps_parse_bounds (ctxt)) {
	        gnumeric_io_error_info_set
			(ctxt->io_context,
			 error_info_new_printf (_("Invalid BOUNDS section in "
						  "the file.")));
	}
}