lemon-project-template-glpk

diff 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
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/deps/glpk/src/glpsdf.c	Sun Nov 06 20:59:10 2011 +0100
     1.3 @@ -0,0 +1,262 @@
     1.4 +/* glpsdf.c (plain data file reading routines) */
     1.5 +
     1.6 +/***********************************************************************
     1.7 +*  This code is part of GLPK (GNU Linear Programming Kit).
     1.8 +*
     1.9 +*  Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
    1.10 +*  2009, 2010, 2011 Andrew Makhorin, Department for Applied Informatics,
    1.11 +*  Moscow Aviation Institute, Moscow, Russia. All rights reserved.
    1.12 +*  E-mail: <mao@gnu.org>.
    1.13 +*
    1.14 +*  GLPK is free software: you can redistribute it and/or modify it
    1.15 +*  under the terms of the GNU General Public License as published by
    1.16 +*  the Free Software Foundation, either version 3 of the License, or
    1.17 +*  (at your option) any later version.
    1.18 +*
    1.19 +*  GLPK is distributed in the hope that it will be useful, but WITHOUT
    1.20 +*  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    1.21 +*  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
    1.22 +*  License for more details.
    1.23 +*
    1.24 +*  You should have received a copy of the GNU General Public License
    1.25 +*  along with GLPK. If not, see <http://www.gnu.org/licenses/>.
    1.26 +***********************************************************************/
    1.27 +
    1.28 +#define GLPSDF_H
    1.29 +
    1.30 +#define GLP_DATA_DEFINED
    1.31 +typedef struct glp_data glp_data;
    1.32 +
    1.33 +#include "glpapi.h"
    1.34 +
    1.35 +struct glp_data
    1.36 +{     /* plain data file */
    1.37 +      char *fname;
    1.38 +      /* name of data file */
    1.39 +      XFILE *fp;
    1.40 +      /* stream assigned to data file */
    1.41 +      void *jump; /* jmp_buf jump; */
    1.42 +      /* label for go to in case of error */
    1.43 +      int count;
    1.44 +      /* line count */
    1.45 +      int c;
    1.46 +      /* current character of XEOF */
    1.47 +      char item[255+1];
    1.48 +      /* current data item */
    1.49 +};
    1.50 +
    1.51 +static void next_char(glp_data *data);
    1.52 +
    1.53 +glp_data *glp_sdf_open_file(const char *fname)
    1.54 +{     /* open plain data file */
    1.55 +      glp_data *data = NULL;
    1.56 +      XFILE *fp;
    1.57 +      jmp_buf jump;
    1.58 +      fp = xfopen(fname, "r");
    1.59 +      if (fp == NULL)
    1.60 +      {  xprintf("Unable to open `%s' - %s\n", fname, xerrmsg());
    1.61 +         goto done;
    1.62 +      }
    1.63 +      data = xmalloc(sizeof(glp_data));
    1.64 +      data->fname = xmalloc(strlen(fname)+1);
    1.65 +      strcpy(data->fname, fname);
    1.66 +      data->fp = fp;
    1.67 +      data->jump = NULL;
    1.68 +      data->count = 0;
    1.69 +      data->c = '\n';
    1.70 +      data->item[0] = '\0';
    1.71 +      /* read the very first character */
    1.72 +      if (setjmp(jump))
    1.73 +      {  glp_sdf_close_file(data);
    1.74 +         data = NULL;
    1.75 +         goto done;
    1.76 +      }
    1.77 +      data->jump = jump;
    1.78 +      next_char(data);
    1.79 +      data->jump = NULL;
    1.80 +done: return data;
    1.81 +}
    1.82 +
    1.83 +void glp_sdf_set_jump(glp_data *data, void *jump)
    1.84 +{     /* set up error handling */
    1.85 +      data->jump = jump;
    1.86 +      return;
    1.87 +}
    1.88 +
    1.89 +void glp_sdf_error(glp_data *data, const char *fmt, ...)
    1.90 +{     /* print error message */
    1.91 +      va_list arg;
    1.92 +      xprintf("%s:%d: ", data->fname, data->count);
    1.93 +      va_start(arg, fmt);
    1.94 +      xvprintf(fmt, arg);
    1.95 +      va_end(arg);
    1.96 +      if (data->jump == NULL)
    1.97 +         xerror("");
    1.98 +      else
    1.99 +         longjmp(data->jump, 1);
   1.100 +      /* no return */
   1.101 +}
   1.102 +
   1.103 +void glp_sdf_warning(glp_data *data, const char *fmt, ...)
   1.104 +{     /* print warning message */
   1.105 +      va_list arg;
   1.106 +      xprintf("%s:%d: warning: ", data->fname, data->count);
   1.107 +      va_start(arg, fmt);
   1.108 +      xvprintf(fmt, arg);
   1.109 +      va_end(arg);
   1.110 +      return;
   1.111 +}
   1.112 +
   1.113 +static void next_char(glp_data *data)
   1.114 +{     /* read next character */
   1.115 +      int c;
   1.116 +      if (data->c == XEOF)
   1.117 +         glp_sdf_error(data, "unexpected end of file\n");
   1.118 +      else if (data->c == '\n')
   1.119 +         data->count++;
   1.120 +      c = xfgetc(data->fp);
   1.121 +      if (c < 0)
   1.122 +      {  if (xferror(data->fp))
   1.123 +            glp_sdf_error(data, "read error - %s\n", xerrmsg());
   1.124 +         else if (data->c == '\n')
   1.125 +            c = XEOF;
   1.126 +         else
   1.127 +         {  glp_sdf_warning(data, "missing final end of line\n");
   1.128 +            c = '\n';
   1.129 +         }
   1.130 +      }
   1.131 +      else if (c == '\n')
   1.132 +         ;
   1.133 +      else if (isspace(c))
   1.134 +         c = ' ';
   1.135 +      else if (iscntrl(c))
   1.136 +         glp_sdf_error(data, "invalid control character 0x%02X\n", c);
   1.137 +      data->c = c;
   1.138 +      return;
   1.139 +}
   1.140 +
   1.141 +static void skip_pad(glp_data *data)
   1.142 +{     /* skip uninteresting characters and comments */
   1.143 +loop: while (data->c == ' ' || data->c == '\n')
   1.144 +         next_char(data);
   1.145 +      if (data->c == '/')
   1.146 +      {  next_char(data);
   1.147 +         if (data->c != '*')
   1.148 +            glp_sdf_error(data, "invalid use of slash\n");
   1.149 +         next_char(data);
   1.150 +         for (;;)
   1.151 +         {  if (data->c == '*')
   1.152 +            {  next_char(data);
   1.153 +               if (data->c == '/')
   1.154 +               {  next_char(data);
   1.155 +                  break;
   1.156 +               }
   1.157 +            }
   1.158 +            next_char(data);
   1.159 +         }
   1.160 +         goto loop;
   1.161 +      }
   1.162 +      return;
   1.163 +}
   1.164 +
   1.165 +static void next_item(glp_data *data)
   1.166 +{     /* read next item */
   1.167 +      int len;
   1.168 +      skip_pad(data);
   1.169 +      len = 0;
   1.170 +      while (!(data->c == ' ' || data->c == '\n'))
   1.171 +      {  data->item[len++] = (char)data->c;
   1.172 +         if (len == sizeof(data->item))
   1.173 +            glp_sdf_error(data, "data item `%.31s...' too long\n",
   1.174 +               data->item);
   1.175 +         next_char(data);
   1.176 +      }
   1.177 +      data->item[len] = '\0';
   1.178 +      return;
   1.179 +}
   1.180 +
   1.181 +int glp_sdf_read_int(glp_data *data)
   1.182 +{     /* read integer number */
   1.183 +      int x;
   1.184 +      next_item(data);
   1.185 +      switch (str2int(data->item, &x))
   1.186 +      {  case 0:
   1.187 +            break;
   1.188 +         case 1:
   1.189 +            glp_sdf_error(data, "integer `%s' out of range\n",
   1.190 +               data->item);
   1.191 +         case 2:
   1.192 +            glp_sdf_error(data, "cannot convert `%s' to integer\n",
   1.193 +               data->item);
   1.194 +         default:
   1.195 +            xassert(data != data);
   1.196 +      }
   1.197 +      return x;
   1.198 +}
   1.199 +
   1.200 +double glp_sdf_read_num(glp_data *data)
   1.201 +{     /* read floating-point number */
   1.202 +      double x;
   1.203 +      next_item(data);
   1.204 +      switch (str2num(data->item, &x))
   1.205 +      {  case 0:
   1.206 +            break;
   1.207 +         case 1:
   1.208 +            glp_sdf_error(data, "number `%s' out of range\n",
   1.209 +               data->item);
   1.210 +         case 2:
   1.211 +            glp_sdf_error(data, "cannot convert `%s' to number\n",
   1.212 +               data->item);
   1.213 +         default:
   1.214 +            xassert(data != data);
   1.215 +      }
   1.216 +      return x;
   1.217 +}
   1.218 +
   1.219 +const char *glp_sdf_read_item(glp_data *data)
   1.220 +{     /* read data item */
   1.221 +      next_item(data);
   1.222 +      return data->item;
   1.223 +}
   1.224 +
   1.225 +const char *glp_sdf_read_text(glp_data *data)
   1.226 +{     /* read text until end of line */
   1.227 +      int c, len = 0;
   1.228 +      for (;;)
   1.229 +      {  c = data->c;
   1.230 +         next_char(data);
   1.231 +         if (c == ' ')
   1.232 +         {  /* ignore initial spaces */
   1.233 +            if (len == 0) continue;
   1.234 +            /* and multiple ones */
   1.235 +            if (data->item[len-1] == ' ') continue;
   1.236 +         }
   1.237 +         else if (c == '\n')
   1.238 +         {  /* remove trailing space */
   1.239 +            if (len > 0 && data->item[len-1] == ' ') len--;
   1.240 +            /* and stop reading */
   1.241 +            break;
   1.242 +         }
   1.243 +         /* add current character to the buffer */
   1.244 +         data->item[len++] = (char)c;
   1.245 +         if (len == sizeof(data->item))
   1.246 +            glp_sdf_error(data, "line too long\n", data->item);
   1.247 +      }
   1.248 +      data->item[len] = '\0';
   1.249 +      return data->item;
   1.250 +}
   1.251 +
   1.252 +int glp_sdf_line(glp_data *data)
   1.253 +{     /* determine current line number */
   1.254 +      return data->count;
   1.255 +}
   1.256 +
   1.257 +void glp_sdf_close_file(glp_data *data)
   1.258 +{     /* close plain data file */
   1.259 +      xfclose(data->fp);
   1.260 +      xfree(data->fname);
   1.261 +      xfree(data);
   1.262 +      return;
   1.263 +}
   1.264 +
   1.265 +/* eof */