/*----------------------------------------------------------------------*\
 | Convert  output from  Arc/INFO ungenerate LINE command to MapInfo	|
 | Interchange Format.													|
 |																		|
 | Peter N. Schweitzer (U.S. Geological Survey, Reston, VA 22092)		|
\*----------------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAXLEN	128
#define MAXPTS	8192

static int n = 0;
static double xx [MAXPTS], yy [MAXPTS];

static void add_point (double x, double y) {
	if (n < MAXPTS) {
		xx[n] = x;
		yy[n] = y;
		n++;
		}
	else {
		puts ("Error: too many points; enlarge MAXPTS and recompile");
		exit (1);
		}
	}

static void write_mif_header (FILE *out) {
	fprintf (out,"VERSION 1\n");
	fprintf (out,"COLUMNS 1\n");
	fprintf (out,"    ID integer\n");
	fprintf (out,"DATA\n");
	}

static void output_line (FILE *out) {
	int i;

	if (n > 1) {
		fprintf (out,"PLINE %d\n",n);
		for (i=0; i < n; i++)
			fprintf (out,"%.6lf\t%.6lf\n",xx[i],yy[i]);
		}
	}

main (int argc, char *argv[]) {
	int i,k;
	char input_file [FILENAME_MAX];
	char mif_file [FILENAME_MAX];
	char mid_file [FILENAME_MAX];
	FILE *in, *mif, *mid;
	char string [MAXLEN];
	char *s;
	double x,y;
	int flipx = 0;
	int flipy = 0;
	int verbose = 0;

	if (argc < 2) {
		fprintf (stderr,"Usage: %s [-v] [-flip x|y] input_file\n",argv[0]);
		exit (0);
		}

	*input_file = 0;

	for (i=1; i < argc; i++)
		if (memcmp (argv[i],"-v",2) == 0) {
			verbose = 1;
			}
		else
			if (memcmp (argv[i],"-flip",5) == 0) {
				i++;
				if (toupper (*argv[i]) == 'X') flipx = 1;
				else
					if (toupper (*argv[i]) == 'Y') flipy = 1;
					else {
						fprintf (stderr,"Error: expected 'x' or 'y' after -flip in command line\n");
						exit (1);
						}
				}
			else
				strcpy (input_file,argv[i]);


	if (*input_file) {
		if (in = fopen (input_file,"r")) {
			strcpy (mif_file,input_file);
			strcat (mif_file,".mif");
			if (mif = fopen (mif_file,"w")) {
				strcpy (mid_file,input_file);
				strcat (mid_file,".mid");
				if (mid = fopen (mid_file,"w")) {
					write_mif_header (mif);
					k = 1;
					while (fgets (string,MAXLEN,in)) {
						if (strstr (string,"END")) {
							output_line (mif);
							fprintf (mid,"LINE%04d\n",k);
							k++;
							n = 0;
							}
						else {

							/*------------------------------------------*\
							 | Replace D exponents with E.				|
							\*------------------------------------------*/

							s = string;
							while (*s) {
								if (toupper(*s) == 'D') *s = 'E';
								s++;
								}

							if (sscanf (string,"%lf%lf",&x,&y) == 2) {
								if (flipx) x = -x;
								if (flipy) y = -y;
								add_point (x,y);
								}

							}
						}
					if (verbose) printf ("%d PLINEs written to %s\n",k,mif_file);
					fclose (mid);
					}
				else
					printf ("Error: could not create mid file %s\n",mid_file);
				fclose (mif);
				}
			else
				printf ("Error: could not create mif file %s\n",mif_file);
			fclose (in);
			}
		else {
			printf ("Error: could not open input file %s\n",input_file);
			exit (1);
			}
		}
	}

/*----------------------------------------------------------------------*\
\*----------------------------------------------------------------------*/
