/* Program convstod -- converts sequential files to GSAS fixed-record
   length files by padding each line to 80 characters and removing
   line-end characters */
/* Program convdtos -- converts GSAS fixed-record length files to
   sequential files to by reading in blocks of 80 characters and
   adding line-end characters */

/* 10 Aug 98 Enhanced:
   File formats are checked so that if a file has already been converted to the 
   requested format, the file is copied and is not converted again. This means
   that it is safe to use convstod and convdtos repeatedly.

   Sequential files are now fixed record length and compatible with DOS GSAS */

#include <stdio.h>
#include <string.h>
#define MAXLINE 161

int main (int argc, char **argv) {
  char line[MAXLINE];

  if ( strcmp(argv[0]+strlen(argv[0])-8, "convstod") == 0) {

    /* convstod (sequential to direct access filter */

    int linnum,i;
    linnum = 0;
    /* get the first line */
    if (fgets(line, MAXLINE, stdin) == NULL) return 1;
    linnum++;
    /* were there any line-ends in the first 160 characters? */
    i = strlen(line);
    if (i==MAXLINE-1) {
      /* this is already a direct access file, just copy it as is */
      fputs (line, stdout);
      while (fgets(line, MAXLINE, stdin) != NULL) {
	fputs (line, stdout);
      }
      /* fprintf(stderr, "convstod NOTE: input file is already direct access\n"); */
    } else {
      char * flag = line;
      while (flag != NULL) {
	int j;
	/* remove control characters -- especially nulls */
	for (j=0; j < i; j++) {if (line[j] < ' ') line[j] = ' ';}
	/* pad the rest of the line with spaces */
	for (j=i-1; j < 80; j++) line[j] = ' ';
	line[80] = 0;
	fputs (line, stdout);
	linnum++;
	flag = fgets(line, MAXLINE, stdin);
	i = strlen(line);
      }
    }
    return 0;
  }
  else if ( strcmp(argv[0]+strlen(argv[0])-8, "convdtos") == 0) {

    /* convdtos ( GSAS direct access to sequential filter ) */

    char line1[82], line2[82];
    int i,j;
    int linnum = 0;
    /* get the first 2 lines */
    if (fgets(line, MAXLINE, stdin) == NULL) return 1;
    linnum++;
    /* were there any line-ends in the first 160 characters? */
    i = strlen(line);

    if (i==MAXLINE-1) {
      /* this is a direct access file, start copying */
      linnum++;
      /* remove control characters -- especially nulls */
      for (j=0; j < i; j++) {if (line[j] < ' ') line[j] = ' ';}
      /* break up and write the lines */
      for (j=0; j < 80; j++) {
	line1[j] = line[j];
	line2[j] = line[j+80];
      }
      /* add a line feed and terminate the string */
      line1[80]='\r';
      line2[80]='\r';
      line1[81]='\0';
      line2[81]='\0';
      puts (line1);
      puts (line2);
      while (fread(line, sizeof(char), 80, stdin) == 80) /* get 80 characters */
	{
	  /* remove control characters -- especially nulls */
	  for (j=0; j < 80; j++) {if (line[j] < ' ') line[j] = ' ';}
	  linnum++;	
	  /* add a line feed and terminate the string */
	  line[80]='\r';
	  line[81]='\0';
	  puts (line);
	}
    } else {
      /* this not a direct access file, but no matter pad each record and
	 write in a DOS compatible format */
      char * flag = line;
      while (flag != NULL) {
	i = strlen(line);
	/* remove control characters -- especially nulls */
	for (j=0; j < i; j++) {if (line[j] < ' ') line[j] = ' ';}
	/* pad the rest of the line with spaces */
	for (j=i-1; j < 80; j++) line[j] = ' ';
	/* add a line feed and terminate the string */
	line[80]='\r';
	line[81]='\0';
	puts (line);
	flag = fgets(line, MAXLINE, stdin);
	linnum++;	
      }
      /* fprintf(stderr, "convstod NOTE: input file is already sequential\n"); */
    }
    return 0;
  } else {
    fprintf (stderr, "Unknown program %s\n",argv[0]);
    return 2;
  }
  
}

