/************************************************************************
* retroLP solves linear programs using the simplex method.             *
* Copyright (C) 2001, 2003, 2005 Gavriel Yarmish & Richard Van Slyke.  *
* For further information, and gpl licensing, see the end of this file.*
************************************************************************/


//--------------------- FILE: READMPS.CPP ---------------------
// This routine reads in a problem description in .mps format
// For the time being, we continue using C style files for mps input
// and corresponding binary files. New files, such as parameter files
// are C++ style.

//
// CHANGES
// 4/13/05 Integrated with retroLP.
// 8/31/04 Corrected line count to prevent comments
//         being counted twice.
// 8/8/01  Save variable names for out put
// 6/28/00 Put in missing handling for PL types in bounds in
//         fparse
//
// The MPS format is somewhat ambiguous. We follow
// the MINOS conventions (Bruce A. Murtagh and Michael A.
// Saunders, MINOS 5.5 USER'S GUIDE, Technical Report SOL 83-20R
// Revised July, 1998. We make the following additional
// assumptions
// 1.   There is exactly one objective function
// 2.   It corresponds to the FIRST row with code "N"
// 3.   There is exactly one RHS
// 4.   The right hand side may be empty (all zero), but "RHS"
//      must appear; We assume only one RHS; if there is more
//      than one, we only look at the first,
// 5.   If there are any non-zero elements of the RHS the column
//      of elements must be given a name.
// 6.   The bounds section is optional
// 7.   Lines in the bounds section for free ("FR") and lower
//      bound to -infinity ("MI") should not have numeric values
// 8.   For now we assume no ranges
// 9.   The BOUNDS section is optional, but if there is one, we
//      assume there is only one RHS; if there is more than one,
//      we only look at the first.

#pragma warning(disable: 4786)

#define BUFFSIZE 80

#include <stdlib.h>
#include <stdio.h>
#include "simplex.h"
#include "params.h"

int to_file(FILE *, LP_state*);
void problem_size(FILE *, long *, long *, long *);
LP_state * fparse( FILE *, long, long);
FILE * cget_file(const char *, int);

LP_state * // Pointer to LP data structure
readmps(
	const char * ipath, // path to mps file
	FILE * bfile  // binary file for reinversion, etc.
	)
{


  FILE * ifile;
  LP_state * lp;

  long  upto=0;

  long lookahead = 0;
  long m=0, n=0, e=0;

  //Open mps file
  ifile = cget_file(ipath, 12);
  if(ifile == NULL)return(NULL);
 







// Make preliminary pass through data
// to determine m and n.
  problem_size(ifile, &m, &n, &e);
  printf(" %ld rows; %ld columns; %ld non-zeros (except for RHS).\n", m, n, e);
// Close and reopen input file, and parse
  fclose(ifile);
  ifile = cget_file(ipath, 13);
  if(ifile == NULL)
  {
    printf("\nError 13 reopening input file\n");
    return(NULL);
  }
  lp = fparse(ifile, m, n);
  if(lp==NULL)
    {
      printf("Error in input, terminated\n");
      return NULL;
    }
  fclose(ifile);
  // SEND RESULTS TO BINARY FILE
  // Make binary file for refresh, etc.
  int error_code = to_file(bfile,lp);
  if(error_code)
    {
      printf("Unable to write intermediate binary file (%d)\n",error_code);
      return NULL;
    }
  return lp;
}


/************************************************************************
* retroLP solves linear programs using the simplex method.             *
* Copyright (C) 2001, 2003, 2005 Gavriel Yarmish & Richard Van Slyke.  *
*                                                                      *
* 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., 51 Franklin Street, Fifth Floor, Boston,           *
*                                                                      *
************************************************************************/
