/***************************************************************************
	convert.c - converts the ASCII dealer database from
		    dbase III from AmigaWorld to mff format.
		    This program was just a quick hack to read
		    an ASCII file and output the mff's record format
		    into a file which was then appended by hand to
		    the actual mff dealer database.
		    This should give you an idea of how to output
		    records in mff format.  You can certainly get
		    fancier by appending directly to the mff file, etc.
		    You might want to use this or similar code to
		    import more complex file formats than what the
		    MFFImport utility can handle.

	Author - Gary Samad

	Revisions:
	  26-May-87	Original version.
***************************************************************************/

#include <stdio.h>

#define EOS		'\0'

#define INFILE		"ram:awdealer.txt"
#define OUTFILE		"ram:dealers.mff.data"
#define MAXLINE		256
#define MAXFILENAME	30

main(argc,argv)
int argc;
char *argv[];
{
	char oneline[MAXLINE];
	char company[MAXLINE];
	char street[MAXLINE];
	char city[MAXLINE];
	char state[MAXLINE];
	char zip[MAXLINE];
	char phone[MAXLINE];

	FILE *fp;
	FILE *outfp;
	char *curfield;

	if (!(fp=fopen(INFILE,"r")))
	{
	  printf("cannot open '%s'\n",INFILE);
	  exit(1);
	}

	if (!(outfp=fopen(OUTFILE,"w")))
	{
	  printf("cannot open '%s'\n",OUTFILE);
	  exit(1);
	}

	while (fgets(oneline,MAXLINE,fp))
	{
	  if (strlen(oneline) != 149)
	    printf("error, len=%d line='%s'\n",strlen(oneline),oneline);
	  else
	  {
	    curfield = &oneline[134];
	    remove_junk(&curfield);
	    strcpy(phone,curfield);
	    *curfield = EOS;
	    curfield = &oneline[124];
	    remove_junk(&curfield);
	    strcpy(zip,curfield);
	    *curfield = EOS;
	    curfield = &oneline[116];
	    remove_junk(&curfield);
	    strcpy(state,curfield);
	    *curfield = EOS;
	    curfield = &oneline[96];
	    remove_junk(&curfield);
	    strcpy(city,curfield);
	    *curfield = EOS;
	    curfield = &oneline[64];
	    remove_junk(&curfield);
	    strcpy(street,curfield);
	    *curfield = EOS;
	    curfield = &oneline[32];
	    remove_junk(&curfield);
	    strcpy(company,curfield);

	    fprintf(outfp," RECORD\n%d\n%s\n%d\n%s\n%d\n%s\n%d\n%s\n%d\n%s\n%d\n%s\n",
		strlen(company),company,
		strlen(street),street,
		strlen(city),city,
		strlen(state),state,
		strlen(zip),zip,
		strlen(phone),phone);
	  }
	}

	fclose(fp);
	fclose(outfp);
}

remove_junk(strptr)
register char **strptr;
{
	remove_trailing_junk(*strptr);

	if (**strptr != EOS);
	  remove_leading_junk(strptr);
}

remove_leading_junk(strptr)
char **strptr;
{
	while ((**strptr == ' ') && (**strptr != EOS))
	  (*strptr)++;
}

remove_trailing_junk(str)
char *str;
{
	char *strend;

	strend = str + strlen(str) - 1;

	while ((strend >= str) &&
	       ((*strend == ' ') || (*strend == '\n') ||
		(*strend == '\r') || (*strend == ',')))
	  strend--;

	*(strend+1) = EOS;
}
