#include <stdio.h>
#include <string.h>

#define MAXLEN 128

static int memicmp (const char *s, const char *t, int n) {
	int r = 0;
	while (n-- > 0 && (r = toupper (*s++) - toupper (*t++)) == 0);
	return (r);
	}

main (int argc, char *argv[]) {
	int i,j,k,m,n;
	int nl,np;
	FILE *in;
	char input_file[MAXLEN];
	char string[MAXLEN],*s;
	double x,y;

	if (argc > 1) {
		strcpy (input_file,argv[1]);
		if (in = fopen (input_file,"r")) {
			k = 0;
			while (fgets (string,MAXLEN,in)) {
				if (s = strrchr (string,'\n')) *s = 0;
				if (memicmp (string,"REGION",6) == 0) {
					if (sscanf (string+6,"%d",&nl) == 1) {
						k++;
						fprintf (stderr,"Region %3d has %3d lines\n",k,nl);
						m = 0;
						for (i=0; i < nl; i++) {
							fgets (string,MAXLEN,in);
							if (sscanf (string,"%d",&np) == 1) {
								m++;
								fprintf (stderr,"  line %3d has %4d points\n",m,np);
								for (j=0; j < np; j++) {
									fgets (string,MAXLEN,in);
									if (sscanf (string,"%lf%lf",&x,&y) != 2) {
										fprintf (stderr,"Error: two doubles expected in \"%s\"\n",string);
										exit (1);
										}
									}
								}
							else {
								fprintf (stderr,"Error: no number of points found in \"%s\"\n",string);
								exit (1);
								}
							}
						}
					else {
						fprintf (stderr,"Error: no number of lines found in \"%s\"\n",string);
						exit (1);
						}
					}
				else
					if (memicmp (string,"PLINE",5) == 0) {
						if (sscanf (string+5,"%d",&np) == 1) {
							fprintf (stderr,"  Pline has %4d points\n",np);
							for (j=0; j < np; j++) {
								fgets (string,MAXLEN,in);
								if (sscanf (string,"%lf%lf",&x,&y) != 2) {
									fprintf (stderr,"Error: two doubles expected in \"%s\"\n",string);
									exit (1);
									}
								}
							}
						else {
							fprintf (stderr,"Error: no number of points found in \"%s\"\n",string);
							exit (1);
							}
						}
					else
						fprintf (stderr,"Warning: record not in region or pline: \"%s\"\n",string);
				}
			fclose (in);
			}
		else {
			fprintf (stderr,"Error: could not open input file %s\n",input_file);
			exit (1);
			}
		}
	else {
		fprintf (stderr,"Usage: %s infile\n",argv[0]);
		exit (1);
		}
	}