/************************************************************************
* 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.*
************************************************************************/


#include "simplex.h"
#include "params.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int to_file(FILE *file, LP_state *lp)
{
   size_t size;
   int k;
   long j;

   k=0;
   printf("PROBLEM: %60.59s",lp->name);
   printf("\nWRITING FILE\n");
   // Make sure we write to beginning of file
   rewind(file);
   size = strlen(lp->name);
   fwrite(&size,sizeof(size_t),1,file);
   if(feof(file)+ferror(file)>0)goto error;
   fwrite(lp->name,sizeof(char),size + 1,file);
   if(feof(file)+ferror(file)>0)goto error;
   k=1;
   fwrite(&(lp->m),sizeof(long),1,file);
   if(feof(file)+ferror(file)>0)goto error;
   k=2;
   fwrite(&(lp->n),sizeof(long),1,file);
   if(feof(file)+ferror(file)>0)goto error;
   k=3;
   fwrite(&(lp->mm),sizeof(long),1,file);
   if(feof(file)+ferror(file)>0)goto error;
   k=4;
   // Write out each column separately since allocated separately.
   for (j=0;j<=lp->n; j++)
   {
      fwrite(lp->a[j], sizeof(double), lp->m + 2,file);
      if(feof(file)+ferror(file)>0)goto error;
   }
   k=5 ;
   fwrite(lp->jnonbasic ,sizeof(long), lp->n+1,file);
   if(feof(file)+ferror(file)>0)goto error;
   k=6;
   fwrite(lp->ntype, sizeof(var_type), lp->n+1, file);
   if(feof(file)+ferror(file)>0)goto error;
   k=7;
   fwrite(lp->nrange, sizeof(var_range), lp->n+1, file);
   if(feof(file)+ferror(file)>0)goto error;
   k=8;
   fwrite(lp->x,sizeof(double),lp->n+1,file);
   if(feof(file)+ferror(file)>0)goto error;
   k=9;
   fwrite(lp->nl,sizeof(double),lp->n+1,file);
   if(feof(file)+ferror(file)>0)goto error;
   k=10;
   fwrite(lp->nu,sizeof(double),lp->n+1,file);
   if(feof(file)+ferror(file)>0)goto error;
   k=11 ;
   fwrite(lp->ibasic ,sizeof(long),lp->m+1, file);
   if(feof(file)+ferror(file)>0)goto error;
   k=12;
   fwrite(lp->btype, sizeof(var_type), lp->m+1, file);
   if(feof(file)+ferror(file)>0)goto error;
   k=13;
   fwrite(lp->brange, sizeof(var_range), lp->m+1, file);
   if(feof(file)+ferror(file)>0)goto error;
   k=14;
   fwrite(lp->b,sizeof(double),lp->m+2,file);
   if(feof(file)+ferror(file)>0)goto error;
   k=15;
   fwrite(lp->bl,sizeof(double),lp->m+1,file);
   if(feof(file)+ferror(file)>0)goto error;
   k=16;
   fwrite(lp->bu,sizeof(double),lp->m+1,file);
   if(feof(file)+ferror(file)>0)goto error;
// Extension of file format starts here (lpbV1)
   k=17;
   fwrite(lp->lpb_version,sizeof(char),10,file);
   if(feof(file)+ferror(file)>0)goto error;
   k=18;
   fwrite(lp->names,sizeof(char),9*(lp->m+lp->n+1),file);
   if(feof(file)+ferror(file)>0)goto error;

   return 0;
   error:
     printf("Error in to_file (%d) -- feof, ferror: %d %d \n" , k, feof(file), ferror(file));
     clearerr(file);
     exit(100+k);
}

/************************************************************************
* 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,           *
*                                                                      *
************************************************************************/
