lemon-project-template-glpk

annotate deps/glpk/src/glpsdf.c @ 9:33de93886c88

Import GLPK 4.47
author Alpar Juttner <alpar@cs.elte.hu>
date Sun, 06 Nov 2011 20:59:10 +0100
parents
children
rev   line source
alpar@9 1 /* glpsdf.c (plain data file reading routines) */
alpar@9 2
alpar@9 3 /***********************************************************************
alpar@9 4 * This code is part of GLPK (GNU Linear Programming Kit).
alpar@9 5 *
alpar@9 6 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
alpar@9 7 * 2009, 2010, 2011 Andrew Makhorin, Department for Applied Informatics,
alpar@9 8 * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
alpar@9 9 * E-mail: <mao@gnu.org>.
alpar@9 10 *
alpar@9 11 * GLPK is free software: you can redistribute it and/or modify it
alpar@9 12 * under the terms of the GNU General Public License as published by
alpar@9 13 * the Free Software Foundation, either version 3 of the License, or
alpar@9 14 * (at your option) any later version.
alpar@9 15 *
alpar@9 16 * GLPK is distributed in the hope that it will be useful, but WITHOUT
alpar@9 17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
alpar@9 18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
alpar@9 19 * License for more details.
alpar@9 20 *
alpar@9 21 * You should have received a copy of the GNU General Public License
alpar@9 22 * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
alpar@9 23 ***********************************************************************/
alpar@9 24
alpar@9 25 #define GLPSDF_H
alpar@9 26
alpar@9 27 #define GLP_DATA_DEFINED
alpar@9 28 typedef struct glp_data glp_data;
alpar@9 29
alpar@9 30 #include "glpapi.h"
alpar@9 31
alpar@9 32 struct glp_data
alpar@9 33 { /* plain data file */
alpar@9 34 char *fname;
alpar@9 35 /* name of data file */
alpar@9 36 XFILE *fp;
alpar@9 37 /* stream assigned to data file */
alpar@9 38 void *jump; /* jmp_buf jump; */
alpar@9 39 /* label for go to in case of error */
alpar@9 40 int count;
alpar@9 41 /* line count */
alpar@9 42 int c;
alpar@9 43 /* current character of XEOF */
alpar@9 44 char item[255+1];
alpar@9 45 /* current data item */
alpar@9 46 };
alpar@9 47
alpar@9 48 static void next_char(glp_data *data);
alpar@9 49
alpar@9 50 glp_data *glp_sdf_open_file(const char *fname)
alpar@9 51 { /* open plain data file */
alpar@9 52 glp_data *data = NULL;
alpar@9 53 XFILE *fp;
alpar@9 54 jmp_buf jump;
alpar@9 55 fp = xfopen(fname, "r");
alpar@9 56 if (fp == NULL)
alpar@9 57 { xprintf("Unable to open `%s' - %s\n", fname, xerrmsg());
alpar@9 58 goto done;
alpar@9 59 }
alpar@9 60 data = xmalloc(sizeof(glp_data));
alpar@9 61 data->fname = xmalloc(strlen(fname)+1);
alpar@9 62 strcpy(data->fname, fname);
alpar@9 63 data->fp = fp;
alpar@9 64 data->jump = NULL;
alpar@9 65 data->count = 0;
alpar@9 66 data->c = '\n';
alpar@9 67 data->item[0] = '\0';
alpar@9 68 /* read the very first character */
alpar@9 69 if (setjmp(jump))
alpar@9 70 { glp_sdf_close_file(data);
alpar@9 71 data = NULL;
alpar@9 72 goto done;
alpar@9 73 }
alpar@9 74 data->jump = jump;
alpar@9 75 next_char(data);
alpar@9 76 data->jump = NULL;
alpar@9 77 done: return data;
alpar@9 78 }
alpar@9 79
alpar@9 80 void glp_sdf_set_jump(glp_data *data, void *jump)
alpar@9 81 { /* set up error handling */
alpar@9 82 data->jump = jump;
alpar@9 83 return;
alpar@9 84 }
alpar@9 85
alpar@9 86 void glp_sdf_error(glp_data *data, const char *fmt, ...)
alpar@9 87 { /* print error message */
alpar@9 88 va_list arg;
alpar@9 89 xprintf("%s:%d: ", data->fname, data->count);
alpar@9 90 va_start(arg, fmt);
alpar@9 91 xvprintf(fmt, arg);
alpar@9 92 va_end(arg);
alpar@9 93 if (data->jump == NULL)
alpar@9 94 xerror("");
alpar@9 95 else
alpar@9 96 longjmp(data->jump, 1);
alpar@9 97 /* no return */
alpar@9 98 }
alpar@9 99
alpar@9 100 void glp_sdf_warning(glp_data *data, const char *fmt, ...)
alpar@9 101 { /* print warning message */
alpar@9 102 va_list arg;
alpar@9 103 xprintf("%s:%d: warning: ", data->fname, data->count);
alpar@9 104 va_start(arg, fmt);
alpar@9 105 xvprintf(fmt, arg);
alpar@9 106 va_end(arg);
alpar@9 107 return;
alpar@9 108 }
alpar@9 109
alpar@9 110 static void next_char(glp_data *data)
alpar@9 111 { /* read next character */
alpar@9 112 int c;
alpar@9 113 if (data->c == XEOF)
alpar@9 114 glp_sdf_error(data, "unexpected end of file\n");
alpar@9 115 else if (data->c == '\n')
alpar@9 116 data->count++;
alpar@9 117 c = xfgetc(data->fp);
alpar@9 118 if (c < 0)
alpar@9 119 { if (xferror(data->fp))
alpar@9 120 glp_sdf_error(data, "read error - %s\n", xerrmsg());
alpar@9 121 else if (data->c == '\n')
alpar@9 122 c = XEOF;
alpar@9 123 else
alpar@9 124 { glp_sdf_warning(data, "missing final end of line\n");
alpar@9 125 c = '\n';
alpar@9 126 }
alpar@9 127 }
alpar@9 128 else if (c == '\n')
alpar@9 129 ;
alpar@9 130 else if (isspace(c))
alpar@9 131 c = ' ';
alpar@9 132 else if (iscntrl(c))
alpar@9 133 glp_sdf_error(data, "invalid control character 0x%02X\n", c);
alpar@9 134 data->c = c;
alpar@9 135 return;
alpar@9 136 }
alpar@9 137
alpar@9 138 static void skip_pad(glp_data *data)
alpar@9 139 { /* skip uninteresting characters and comments */
alpar@9 140 loop: while (data->c == ' ' || data->c == '\n')
alpar@9 141 next_char(data);
alpar@9 142 if (data->c == '/')
alpar@9 143 { next_char(data);
alpar@9 144 if (data->c != '*')
alpar@9 145 glp_sdf_error(data, "invalid use of slash\n");
alpar@9 146 next_char(data);
alpar@9 147 for (;;)
alpar@9 148 { if (data->c == '*')
alpar@9 149 { next_char(data);
alpar@9 150 if (data->c == '/')
alpar@9 151 { next_char(data);
alpar@9 152 break;
alpar@9 153 }
alpar@9 154 }
alpar@9 155 next_char(data);
alpar@9 156 }
alpar@9 157 goto loop;
alpar@9 158 }
alpar@9 159 return;
alpar@9 160 }
alpar@9 161
alpar@9 162 static void next_item(glp_data *data)
alpar@9 163 { /* read next item */
alpar@9 164 int len;
alpar@9 165 skip_pad(data);
alpar@9 166 len = 0;
alpar@9 167 while (!(data->c == ' ' || data->c == '\n'))
alpar@9 168 { data->item[len++] = (char)data->c;
alpar@9 169 if (len == sizeof(data->item))
alpar@9 170 glp_sdf_error(data, "data item `%.31s...' too long\n",
alpar@9 171 data->item);
alpar@9 172 next_char(data);
alpar@9 173 }
alpar@9 174 data->item[len] = '\0';
alpar@9 175 return;
alpar@9 176 }
alpar@9 177
alpar@9 178 int glp_sdf_read_int(glp_data *data)
alpar@9 179 { /* read integer number */
alpar@9 180 int x;
alpar@9 181 next_item(data);
alpar@9 182 switch (str2int(data->item, &x))
alpar@9 183 { case 0:
alpar@9 184 break;
alpar@9 185 case 1:
alpar@9 186 glp_sdf_error(data, "integer `%s' out of range\n",
alpar@9 187 data->item);
alpar@9 188 case 2:
alpar@9 189 glp_sdf_error(data, "cannot convert `%s' to integer\n",
alpar@9 190 data->item);
alpar@9 191 default:
alpar@9 192 xassert(data != data);
alpar@9 193 }
alpar@9 194 return x;
alpar@9 195 }
alpar@9 196
alpar@9 197 double glp_sdf_read_num(glp_data *data)
alpar@9 198 { /* read floating-point number */
alpar@9 199 double x;
alpar@9 200 next_item(data);
alpar@9 201 switch (str2num(data->item, &x))
alpar@9 202 { case 0:
alpar@9 203 break;
alpar@9 204 case 1:
alpar@9 205 glp_sdf_error(data, "number `%s' out of range\n",
alpar@9 206 data->item);
alpar@9 207 case 2:
alpar@9 208 glp_sdf_error(data, "cannot convert `%s' to number\n",
alpar@9 209 data->item);
alpar@9 210 default:
alpar@9 211 xassert(data != data);
alpar@9 212 }
alpar@9 213 return x;
alpar@9 214 }
alpar@9 215
alpar@9 216 const char *glp_sdf_read_item(glp_data *data)
alpar@9 217 { /* read data item */
alpar@9 218 next_item(data);
alpar@9 219 return data->item;
alpar@9 220 }
alpar@9 221
alpar@9 222 const char *glp_sdf_read_text(glp_data *data)
alpar@9 223 { /* read text until end of line */
alpar@9 224 int c, len = 0;
alpar@9 225 for (;;)
alpar@9 226 { c = data->c;
alpar@9 227 next_char(data);
alpar@9 228 if (c == ' ')
alpar@9 229 { /* ignore initial spaces */
alpar@9 230 if (len == 0) continue;
alpar@9 231 /* and multiple ones */
alpar@9 232 if (data->item[len-1] == ' ') continue;
alpar@9 233 }
alpar@9 234 else if (c == '\n')
alpar@9 235 { /* remove trailing space */
alpar@9 236 if (len > 0 && data->item[len-1] == ' ') len--;
alpar@9 237 /* and stop reading */
alpar@9 238 break;
alpar@9 239 }
alpar@9 240 /* add current character to the buffer */
alpar@9 241 data->item[len++] = (char)c;
alpar@9 242 if (len == sizeof(data->item))
alpar@9 243 glp_sdf_error(data, "line too long\n", data->item);
alpar@9 244 }
alpar@9 245 data->item[len] = '\0';
alpar@9 246 return data->item;
alpar@9 247 }
alpar@9 248
alpar@9 249 int glp_sdf_line(glp_data *data)
alpar@9 250 { /* determine current line number */
alpar@9 251 return data->count;
alpar@9 252 }
alpar@9 253
alpar@9 254 void glp_sdf_close_file(glp_data *data)
alpar@9 255 { /* close plain data file */
alpar@9 256 xfclose(data->fp);
alpar@9 257 xfree(data->fname);
alpar@9 258 xfree(data);
alpar@9 259 return;
alpar@9 260 }
alpar@9 261
alpar@9 262 /* eof */