/* lp_strrd.c
 * Neil Moore <neil@cs.uky.edu>
 *  1 Oct 1999
 *
 * Part of liblp.  See COPYRIGHT for copyright information.
 */
#include <stdio.h>
#include <string.h>
#include <lp_util.h>

static char *errmsg=0;
static int err_alloc=0;

/* Read a stratification.  *strata is allocated as an array of ints, with
 * (*strata)[i] indicating the number of rules in the ith strata.  The
 * input file is in DeReS stratification format, which makes things quite
 * simple.  This function returns an integer indicating the number of
 * strata, or -1 if there was an error.
 *
 */
int lp_read_strata(FILE *f, int **strata) 
{
	char buff[80];
	int nstrata=0;
	int i;

	while(!feof(f)) {
		fgets(buff,80,f);
		if(!feof(f))
			nstrata++;
	}
	
	*strata = lp_malloc(nstrata * sizeof(int));
	
	fseek(f,0,SEEK_SET);
	for(i=0;i<nstrata;i++) {
		if(((*strata)[i] = lp_getnumline(f)) < 0) {
			if(err_alloc)
				free(errmsg);
			err_alloc=1;
			errmsg = lp_malloc(strlen("Error on line 999999999")+1);
			sprintf(errmsg, "Error on line %d", i % 1000000000);
			return -1;
		}
	}

	return nstrata;
}


/* Return the error message generated by the last call to lp_read_strata.
 */
const char *lp_strrd_error(void) 
{
	return errmsg;
}

