reader.c

Go to the documentation of this file.
00001 /****************************************************************************/
00002 /*                                                                          */
00003 /*  This file is part of QSopt_ex.                                          */
00004 /*                                                                          */
00005 /*  (c) Copyright 2006 by David Applegate, William Cook, Sanjeeb Dash,      */
00006 /*  and Daniel Espinoza                                                     */
00007 /*                                                                          */
00008 /*  Sanjeeb Dash ownership of copyright in QSopt_ex is derived from his     */
00009 /*  copyright in QSopt.                                                     */
00010 /*                                                                          */
00011 /*  This code may be used under the terms of the GNU General Public License */
00012 /*  (Version 2.1 or later) as published by the Free Software Foundation.    */
00013 /*                                                                          */
00014 /*  Alternatively, use is granted for research purposes only.               */
00015 /*                                                                          */
00016 /*  It is your choice of which of these two licenses you are operating      */
00017 /*  under.                                                                  */
00018 /*                                                                          */
00019 /*  We make no guarantees about the correctness or usefulness of this code. */
00020 /*                                                                          */
00021 /****************************************************************************/
00022 
00023 /* RCS_INFO = "$RCSfile: reader.c,v $ $Revision: 1.2 $ $Date: 2003/11/05 16:49:52 $"; */
00024 
00025 #include "reader.h"
00026 #include "qstruct.h"
00027 #include "iqsutil.h"
00028 #include "qsopt.h"
00029 
00030 static int TRACE = 0;
00031 static int TEST_ERROR_COLLECTOR = 0;
00032 static int TEST_ERROR_MEMORY = 0;
00033 
00034 static char *fname = (char *) NULL;
00035 static char *out_lp = (char *) NULL;
00036 static char *out_mps = (char *) NULL;
00037 static int lpfile = 0;
00038 static int stats = 0;
00039 
00040 static void usage (
00041   char *s);
00042 
00043 static int parseargs (
00044   int ac,
00045   char **av);
00046 
00047 static int add_error (
00048   void *dest,
00049   QSformat_error error)
00050 {
00051   const char *type = "Error";
00052   const char *line;
00053   int tp, i, at;
00054   FILE *out = (FILE *) dest;
00055 
00056   at = QSerror_get_pos (error);
00057   tp = QSerror_get_type (error);
00058   type = QSformat_error_type_string (tp);
00059 
00060   fprintf (out, "ADD: ");
00061   fprintf (out, "%s  line %d pos %d\n",
00062            type, QSerror_get_line_number (error), at);
00063   line = QSerror_get_line (error);
00064   if (line != NULL)
00065   {
00066     fprintf (out, "LINE %s", line);
00067     if (at >= 0)
00068     {
00069       fprintf (out, ".....");
00070       for (i = 0; i <= (at - 1); i++)
00071       {
00072         if (line[i] == '\t')
00073         {
00074           fputc ('\t', out);
00075         }
00076         else
00077         {
00078           fputc ('.', out);
00079         }
00080       }
00081       fprintf (out, "^\n");
00082     }
00083   }
00084   else
00085   {
00086     fprintf (out, "NO LINE\n");
00087   }
00088 
00089   fprintf (out, "MSG: %s\n", QSerror_get_desc (error));
00090   return 0;
00091 }
00092 
00093 QSLIB_INTERFACE int reader_main (
00094   int ac,
00095   char **av)
00096 {
00097   int rval = 0;
00098   int rvalmps = 0;
00099   int rvallp = 0;
00100   QSdata *p = NULL;
00101   ILLutil_timer timer_read;
00102   ILLutil_timer timer_write;
00103   FILE *fin = NULL;
00104   QSline_reader reader = NULL;
00105   QSerror_collector collector = NULL;
00106   QSerror_memory error_mem = NULL;
00107   QSformat_error e = NULL;
00108 
00109   if (parseargs (ac, av))
00110     goto CLEANUP;
00111 
00112   ILLutil_init_timer (&timer_read, "READER_READ");
00113   ILLutil_start_timer (&timer_read);
00114   if (TEST_ERROR_COLLECTOR || TEST_ERROR_MEMORY)
00115   {
00116     fin = fopen (fname, "r");
00117     reader = QSline_reader_new ((void *) fgets, fin);
00118     if (TEST_ERROR_COLLECTOR)
00119     {
00120       collector = QSerror_collector_new ((void *) add_error, stderr);
00121     }
00122     if (TEST_ERROR_MEMORY)
00123     {
00124       error_mem = QSerror_memory_create (0);
00125       ILL_CHECKnull (error_mem, "Could not make error memory");
00126       collector = QSerror_memory_collector_new (error_mem);
00127     }
00128     if (fin == NULL)
00129     {
00130       fprintf (stderr, "Can't open \"%s\" for reading.\n", fname);
00131     }
00132     rval = (fin == NULL) || (reader == NULL) || (collector == NULL);
00133     ILL_CLEANUP_IF (rval);
00134 
00135     QSline_reader_set_error_collector (reader, collector);
00136     p = QSget_prob (reader, fname, (lpfile == 0) ? "MPS" : "LP");
00137 
00138     if (TEST_ERROR_MEMORY)
00139     {
00140       int n = QSerror_memory_get_nerrors (error_mem);
00141 
00142       fprintf (stderr, "#error %d\n", n);
00143       for (e = QSerror_memory_get_last_error (error_mem);
00144            e != NULL; e = QSerror_memory_get_prev_error (e))
00145       {
00146         QSerror_print (stderr, e);
00147       }
00148     }
00149   }
00150   else
00151   {
00152     if (lpfile == 0)
00153     {
00154       p = QSread_prob (fname, "MPS");
00155     }
00156     else
00157     {
00158       p = QSread_prob (fname, "LP");
00159     }
00160   }
00161   ILL_IFTRACE ("QSread_prob %s\n", fname);
00162   ILLutil_stop_timer (&timer_read, 1);
00163   rval = (p == NULL);
00164   fprintf (stdout,
00165            "read \"%s\" %s.\n", fname, (rval == 0) ? "succeeded" : "failed");
00166   ILL_CLEANUP_IF (rval);
00167 
00168   if (stats)
00169   {
00170     fprintf (stdout, "\n");
00171     fprintf (stdout, "The problem \"%s\" has %d rows and %d cols.\n",
00172              QSget_probname (p), QSget_rowcount (p), QSget_colcount (p));
00173   }
00174   ILLutil_init_timer (&timer_write, "READER_WRITE");
00175   ILLutil_start_timer (&timer_write);
00176   if (out_mps)
00177   {
00178     fprintf (stdout, "\n");
00179     rvalmps = QSwrite_prob (p, out_mps, "MPS");
00180     fprintf (stdout, "write \"%s\" %s.\n", out_mps,
00181              (rvalmps == 0) ? "succeeded" : "failed");
00182   }
00183   if (out_lp)
00184   {
00185     fprintf (stdout, "\n");
00186     rvallp = QSwrite_prob (p, out_lp, "LP");
00187     fprintf (stdout, "write \"%s\" %s.\n", out_lp,
00188              (rvallp == 0) ? "succeeded" : "failed");
00189   }
00190   ILLutil_stop_timer (&timer_write, 1);
00191 
00192   rval = rvalmps || rvallp;
00193   ILL_CLEANUP_IF (rval);
00194 
00195 CLEANUP:
00196 
00197   if (p != NULL)
00198     QSfree_prob (p);
00199   if (fin != NULL)
00200     fclose (fin);
00201   if (reader != NULL)
00202     QSline_reader_free (reader);
00203   if (collector != NULL)
00204     QSerror_collector_free (collector);
00205   if (error_mem != NULL)
00206     QSerror_memory_free (error_mem);
00207 
00208   return rval;                  /* main return */
00209 }
00210 
00211 static void usage (
00212   char *s)
00213 {
00214   fprintf (stderr, "Usage: %s [- below -] prob_file\n", s);
00215   fprintf (stderr, "   -L    input file is in lp format (default: mps)\n");
00216   fprintf (stderr, "   -s    print information about problem to stdout\n");
00217   fprintf (stderr, "   -l f  write lp in LP format to file f\n");
00218   fprintf (stderr, "   -m f  write lp in MPS format to file f\n");
00219 #if 0
00220   fprintf (stderr, "   -E    test error_memory\n");
00221   fprintf (stderr, "   -e    test error_collector\n");
00222 #endif
00223 }
00224 
00225 static int parseargs (
00226   int ac,
00227   char **av)
00228 {
00229   int c;
00230   int boptind = 1;
00231   char *boptarg = (char *) NULL;
00232 
00233   while ((c =
00234           ILLutil_bix_getopt (ac, av, "Ll:m:tseE", &boptind, &boptarg)) != EOF)
00235     switch (c)
00236     {
00237     case 'L':
00238       lpfile = 1;
00239       break;
00240     case 'l':
00241       out_lp = boptarg;
00242       break;
00243     case 'm':
00244       out_mps = boptarg;
00245       break;
00246     case 's':
00247       stats = 1;
00248       break;
00249     case 't':
00250       TRACE++;
00251       break;
00252 #if 0
00253     case 'E':
00254       TEST_ERROR_MEMORY++;
00255       break;
00256     case 'e':
00257       TEST_ERROR_COLLECTOR++;
00258       break;
00259 #endif
00260     case '?':
00261     default:
00262       usage (av[0]);
00263       return 1;
00264     }
00265 
00266   if (boptind != (ac - 1))
00267   {
00268     usage (av[0]);
00269     return 1;
00270   }
00271 
00272   fname = av[boptind++];
00273   return 0;
00274 }
00275 
00276 
00277 #ifndef WIN32
00278 int main (
00279   int ac,
00280   char **av)
00281 {
00282   return reader_main (ac, av);
00283 }
00284 #endif

Generated on Thu Mar 29 09:32:33 2012 for QSopt_ex by  doxygen 1.4.7