aboutsummaryrefslogtreecommitdiff
path: root/countrows.c
blob: 13448d7cb6bf2815b1632f6330b357de71632393 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * Reads a polyhedron file on stdin, counts the rows, and outputs 
 * an equivalent file with a corrected row count.
 * 
 * David Bremner. bremner@cs.unb.ca
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <float.h>
#include <errno.h>
#include <ctype.h>

#include "lrsmp.h"
#include "process_args.h"

typedef  lrs_mp integer_t;
#define MP_DIGITS 1000L


#include "countrows.h"


int main(argc,argv)
	 int argc;
	 char **argv;
{
  int i,j;
  long int m,n;

  char **lines;
  int line_count;
  char  buf[BUFSIZ];
  
  char *type;
  char *cursor;


  process_args(argc,argv,DOCSTRING);

  lrs_mp_init (MP_DIGITS,stdin,stdout);   

  while ( fgets(buf,BUFSIZ,stdin) !=NULL )    {
    fputs(buf,stdout);
    if (strncmp(buf,"begin",5)==0) break;
  }

  if (fgets(buf,BUFSIZ,stdin)==NULL) {
    fprintf(stderr,"No parameter line");
    exit(1);
  }
  
  cursor=buf;
  
  while(isspace(*cursor))
    cursor++;

  while (!isspace(*cursor))
    cursor++;

  n=strtol(cursor,&cursor,10);
  if (errno==ERANGE || n==0){
    fprintf(stderr,"Missing or absurd column count");
    exit(1);
  }    

  line_count=2*n;
  lines=calloc(line_count,sizeof(char *));

  while (isblank(*cursor)){
    cursor++;
  }

  type=strdup(cursor);

  m=0;
  do {
    if (fgets(buf,BUFSIZ,stdin)==NULL) {
      fprintf(stderr,"missing end");
      exit(1);
    }
    if (strncmp(buf,"end",3)!=0){
      if (m>=line_count){
	line_count *=2;
	lines=realloc(lines,line_count*sizeof(char *));
      }

      lines[m++]=strdup(buf);
    }
    
  } while (strncmp(buf,"end",3)!=0);

    

  printf("%ld %ld %s",m,n,type);

  for (i=0;i<m;i++)
    fputs(lines[i],stdout);

  fputs("end\n",stdout);
}