1 /* glpsdf.c (plain data file reading routines) */
3 /***********************************************************************
4 * This code is part of GLPK (GNU Linear Programming Kit).
6 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
7 * 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
8 * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
9 * E-mail: <mao@gnu.org>.
11 * GLPK is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * GLPK is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
19 * License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
23 ***********************************************************************/
27 #define GLP_DATA_DEFINED
28 typedef struct glp_data glp_data;
33 { /* plain data file */
35 /* name of data file */
37 /* stream assigned to data file */
38 void *jump; /* jmp_buf jump; */
39 /* label for go to in case of error */
43 /* current character of XEOF */
45 /* current data item */
48 static void next_char(glp_data *data);
50 glp_data *glp_sdf_open_file(const char *fname)
51 { /* open plain data file */
52 glp_data *data = NULL;
55 fp = xfopen(fname, "r");
57 { xprintf("Unable to open `%s' - %s\n", fname, xerrmsg());
60 data = xmalloc(sizeof(glp_data));
61 data->fname = xmalloc(strlen(fname)+1);
62 strcpy(data->fname, fname);
68 /* read the very first character */
70 { glp_sdf_close_file(data);
80 void glp_sdf_set_jump(glp_data *data, void *jump)
81 { /* set up error handling */
86 void glp_sdf_error(glp_data *data, const char *fmt, ...)
87 { /* print error message */
89 xprintf("%s:%d: ", data->fname, data->count);
93 if (data->jump == NULL)
96 longjmp(data->jump, 1);
100 void glp_sdf_warning(glp_data *data, const char *fmt, ...)
101 { /* print warning message */
103 xprintf("%s:%d: warning: ", data->fname, data->count);
110 static void next_char(glp_data *data)
111 { /* read next character */
114 glp_sdf_error(data, "unexpected end of file\n");
115 else if (data->c == '\n')
117 c = xfgetc(data->fp);
119 { if (xferror(data->fp))
120 glp_sdf_error(data, "read error - %s\n", xerrmsg());
121 else if (data->c == '\n')
124 { glp_sdf_warning(data, "missing final end of line\n");
133 glp_sdf_error(data, "invalid control character 0x%02X\n", c);
138 static void skip_pad(glp_data *data)
139 { /* skip uninteresting characters and comments */
140 loop: while (data->c == ' ' || data->c == '\n')
145 glp_sdf_error(data, "invalid use of slash\n");
148 { if (data->c == '*')
162 static void next_item(glp_data *data)
163 { /* read next item */
167 while (!(data->c == ' ' || data->c == '\n'))
168 { data->item[len++] = (char)data->c;
169 if (len == sizeof(data->item))
170 glp_sdf_error(data, "data item `%.31s...' too long\n",
174 data->item[len] = '\0';
178 int glp_sdf_read_int(glp_data *data)
179 { /* read integer number */
182 switch (str2int(data->item, &x))
186 glp_sdf_error(data, "integer `%s' out of range\n",
189 glp_sdf_error(data, "cannot convert `%s' to integer\n",
192 xassert(data != data);
197 double glp_sdf_read_num(glp_data *data)
198 { /* read floating-point number */
201 switch (str2num(data->item, &x))
205 glp_sdf_error(data, "number `%s' out of range\n",
208 glp_sdf_error(data, "cannot convert `%s' to number\n",
211 xassert(data != data);
216 const char *glp_sdf_read_item(glp_data *data)
217 { /* read data item */
222 const char *glp_sdf_read_text(glp_data *data)
223 { /* read text until end of line */
229 { /* ignore initial spaces */
230 if (len == 0) continue;
231 /* and multiple ones */
232 if (data->item[len-1] == ' ') continue;
235 { /* remove trailing space */
236 if (len > 0 && data->item[len-1] == ' ') len--;
237 /* and stop reading */
240 /* add current character to the buffer */
241 data->item[len++] = (char)c;
242 if (len == sizeof(data->item))
243 glp_sdf_error(data, "line too long\n", data->item);
245 data->item[len] = '\0';
249 int glp_sdf_line(glp_data *data)
250 { /* determine current line number */
254 void glp_sdf_close_file(glp_data *data)
255 { /* close plain data file */