/*****************************************************************************
******************************************************************************
******************************************************************************
*****                                                                    *****
*****                       PROGRAM WRITETXT v1.1                        *****
***** Program for writing restriction site or fragment data in text form *****
*****    Copyright (c) 1990 by Joyce C. Miller.  All Rights Reserved.    *****
*****                                                                    *****
***** This  program takes  a  datafile name and a textfile name from the *****
***** command line.  Each record from the datafile is turned into a text *****
***** line, which is written to the textfile.                            *****
*****                                                                    *****
*****                                                                    *****
***** List of C functions used in this program:                          *****
*****                                                                    *****
*****     FUNCTION         LIBRARY          FUNCTION         LIBRARY     *****
*****     fclose           stdio.h          feof             stdio.h     *****
*****     fprintf          stdio.h          fread            stdio.h     *****
*****     printf           stdio.h          strcat           string.h    *****
*****     strchr           string.h         strcpy           string.h    *****
*****     strlen           string.h         strncpy          string.h    *****
*****     toupper          ctype.h                                       *****
*****                                                                    *****
******************************************************************************
******************************************************************************
*****************************************************************************/

/*****************************************************************************
**                              INCLUDE FILES                               **
*****************************************************************************/
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <rstypes.h>       /* header file with type definitions & constants */
#include <rserrors.h>                           /* file with error messages */
#include <rsfuncs.h>          /* file with some functions (NEEDS RSTYPES.H) */
/*****************************************************************************
**                            SYMBOLIC CONSTANTS                            **
*****************************************************************************/
#define VNUM 1.1                         /* version number for this program */
#define YEAR 1990                             /* calendar year of copyright */
/*****************************************************************************
**                          FUNCTION PROTOTYPES                             **
*****************************************************************************/
void  rddat(char rdfl[], char wtfl[]);                     /* main function */
FILE *prntxtln(FILE *fpw, fragdat *tr);                 /* prints text line */
char *newfile(char wtfl[], int flnum);             /* creates new file name */
/*****************************************************************************
******************************************************************************
******************************************************************************
******                                                                  ******
******                           MAIN PROGRAM                           ******
******                                                                  ******
****** This  function is  the primary  organizational  function  of the ******
****** program.  It prints  out the  program name, version  number, and ******
****** copyright  notice.  It then  converts the  input and output file ******
****** names to uppercase, and feeds them to FUNCTION RDDAT.            ******
******                                                                  ******
****** Functions called:                                                ******
****** rddat          --  reads the  binary data  file, and  prints the ******
******                    data in it to a text file.                    ******
****** rserror101     --  error if no  input data  file or output  file ******
******                    specified.                                    ******
******************************************************************************
******************************************************************************
*****************************************************************************/
void main(int argc, char *argv[])
{
  char rdfl[FILELEN];
  char wtfl[FILELEN];
  int i;

  printf("\r\n\nPROGRAM WRITETXT v%3.1f\r\nCopyright ",VNUM);
  printf("(c) %4.0d by Joyce C. Miller.  All Rights Reserved.\r\n",YEAR);

  if (argc < 3) rserror101();                /* error if no files specified */

  for (i=0; i<strlen(argv[1]); ++i) /* convert input file name to uppercase */
    if (i<FILELEN) rdfl[i] = toupper(argv[1][i]);
    else break;
  rdfl[i] = '\0';

  for (i=0; i<strlen(argv[2]); ++i)    /* convert outfile name to uppercase */
    if (i<FILELEN) wtfl[i] = toupper(argv[2][i]);
    else break;
  wtfl[i] = '\0';

  rddat(rdfl,wtfl);                                   /* go on with program */
  printf("\r\nEnd of datafile %s.\r\n",rdfl);
}                                                   /* END OF FUNCTION MAIN */
/*****************************************************************************
******************************************************************************
*****                                                                    *****
*****                             FUNCTIONS                              *****
*****                                                                    *****
******************************************************************************
*****************************************************************************/

/*****************************************************************************
**                                                                          **
**                              FUNCTION RDDAT                              **
**                                                                          **
** This  function opens the datafile  (RDFL) and  the textfile  (WTFL).  It **
** then reads each record from RDFL, and writes the elements as a text line **
** to WTFL.                                                                 **
**                                                                          **
** Functions called:                                                        **
** opnbrdfl       --  opens a binary file for reading.                      **
** opntwtfl       --  opens a text file for writing.                        **
** newfile        --  produces a new file name (WTFL.TX#) for each 1000     **
**                    lines of text printed.                                **
** prntxtln       --  takes a record, prints the elements to WTFL.          **
** rserror311     --  error message if error reading.                       **
*****************************************************************************/
void rddat(char rdfl[], char wtfl[])
{
  FILE *fpr, *fpw;                        /* pointers to READ & WRITE files */
  fragdat t;                                            /* temporary record */
  fragdat *tr = 0;                           /* pointer to temporary record */
  char *w;                                          /* pointer to WTFL name */
  int flnum   = 1;                               /* present textfile number */
  int ln      = 0;                                           /* line number */

  fpr = opnbrdfl(fpr,rdfl);                  /* opens data file for reading */
  fpw = opntwtfl(fpw,wtfl);                  /* opens text file for writing */


  printf("\r\nReading binary datafile %s ...",rdfl);    /* message for user */
  printf("\r\nWriting to textfile %s...\r\n\n",wtfl);

  while (!feof(fpr)) {                             /* while not end of file */
    if (fread(&t,sizeof(fragdat),1,fpr) != NULL) {         /* read a record */
      if (++ln > FLLMT) {         /* if we've reached max # lines/text file */
       if (flnum == 1) {
         printf("%s is very long, and is being printed to ",rdfl);
         printf("several textfiles:\r\n");               /* message to user */
       }
       fclose(fpw);                                   /* close current file */
       printf("%d lines printed to %s\r\n",ln-1,wtfl);   /* give # of lines */
       w = newfile(wtfl,++flnum);                 /* make name for new file */
       strcpy(wtfl,w);
       fpw = opntwtfl(fpw,wtfl);               /* open new file for writing */
       ln = 1;                                      /* set line number to 1 */
      }
      fpw = prntxtln(fpw,&t);                  /* print record to text line */
    }
    else break;                                        /* read error or EOF */
  }                                                         /* end of while */

  if (!feof(fpr)) rserror311(rdfl);            /* if stop because of EOF... */
  if (ln > 0) printf("%d lines printed to %s\r\n",ln,wtfl); /* give # lines */
  fclose(fpr);                                           /* close data file */
  fclose(fpw);                                         /* close output file */
}                                                  /* END OF FUNCTION RDDAT */
/*****************************************************************************
**                                                                          **
**                              FUNCTION NEWFL                              **
**                                                                          **
** This function receives the current  WTFL name,  and the file number, and **
** creates the name of the next text file from them.  The first  FLLMT text **
** lines are printed to the user-named WTFL.  After that, NEWFILE creates a **
** file named  WTFL.TX2  for  the next  FLLMT lines,  then  WTFL.TX3,  etc. **
** Whenever the FLLMT-line limit is reached,  NEWFILE creates the next WTFL **
** name from the old one.                                                   **
**                                                                          **
** Functions called:                                                        **
** rserror210     --  error if too many data records.                       **
** Note :  This  function  also  relies on  "FLLMT",  which is  the maximum **
** number of lines  that can be printed to a text file, and is #DEFINEd  in **
** the header file "RSTYPES.H".                                             **
*****************************************************************************/
char *newfile(char wtfl[], int flnum)
{
  char wf[FILELEN];
  char vf[FILELEN];
  char s[10];                                           /* temporary string */
  char *fn;
  char *v;                                                /* string pointer */
  int i;

  vf[0] = '\0';     /* create text part of file extension and store in "vf" */
  if (flnum < 10)         strcpy(vf,"TX");             /* extension = "TX#" */
  else if (flnum < 100)   strcpy(vf,"T");              /* extension = "T##" */
  else if (flnum >= 1000) rserror210();       /* extension = "###" or error */

  s[0] = '\0';           /* create numbered part of extension, store in "s" */
  v = inttoalph(flnum,s);     /* convert flnum from int to character string */
  strcpy(s,v);                                              /* store in "s" */

  v = '\0';                                               /* initialize "v" */
  v = strcat(vf,s);           /* combine text and number parts of extension */
  s[0] = '\0';                                            /* initialize "s" */
  strcpy(s,v);                             /* store entire extension in "s" */
  strcpy(wf,wtfl);                         /* copy "wtfl" file name to "wf" */
  for (i=0; i<strlen(wf); ++i)             /* copy "wf" into "vf" up to dot */
    if (wf[i] != '.') vf[i] = wf[i];
    else vf[i] = '\0';
  v = strcat(vf,".");                                            /* add dot */
  v = strcat(vf,s);                                        /* add extension */
  return(v);                             /* return pointer to new file name */
}                                                /* END OF FUNCTION NEWFILE */
/*****************************************************************************
**                                                                          **
**                            FUNCTION PRNTXTLN                             **
**                                                                          **
** This function receives a record, and the file pointer fpw.  The elements **
** of the record are printed to WTFL, their lengths padded out with blanks, **
** and the text line is followed with a line feed.  The file pointer fpw is **
** returned.                                                                **
**                                                                          **
** Functions called:  none                                                  **
** Note: also relies on "IDLEN", "KEYLEN", "PRBLEN", "ENZLEN", AND "NUMFS", **
** which are #DEFINEd at beginning of program.  These are the maximum       **
** lengths of the fields within the record structure FRAGDAT.               **
*****************************************************************************/
FILE *prntxtln(FILE *fpw, fragdat *tr)
{
  register int i;
  fragdat t;

  t = *tr;                                       /* receive the data record */

  fprintf(fpw,"%s",t.id);                                    /* print IDNUM */
  i = strlen(t.id);
  while (i++ < IDLEN) fprintf(fpw," ");              /* pad out with blanks */
  
  if (strlen(t.key1) != 0) {
    fprintf(fpw," %s",t.key1);                                /* print KEY1 */
    i = strlen(t.key1);
    while (i++ < KEYLEN) fprintf(fpw," ");           /* pad out with blanks */
  }
  else {
    fprintf(fpw," X");
    i = strlen(t.key1)+1;
    while (i++ < KEYLEN) fprintf(fpw," ");
  }

  if (strlen(t.key2) != 0) {
    fprintf(fpw," %s",t.key2);                                /* print KEY2 */
    i = strlen(t.key2);
    while (i++ < KEYLEN) fprintf(fpw," ");           /* pad out with blanks */
  }
  else {
    fprintf(fpw," X");
    i = strlen(t.key2)+1;
    while (i++ < KEYLEN) fprintf(fpw," ");
  }

  if (strlen(t.key3) != 0) {
    fprintf(fpw," %s",t.key3);                                /* print KEY3 */
    i = strlen(t.key3);
    while (i++ < KEYLEN) fprintf(fpw," ");           /* pad out with blanks */
  }
  else {
    fprintf(fpw," X");
    i = strlen(t.key3)+1;
    while (i++ < KEYLEN) fprintf(fpw," ");
  }

  if (strlen(t.key4) != 0) {
    fprintf(fpw," %s",t.key4);                                /* print KEY4 */
    i = strlen(t.key4);
    while (i++ < KEYLEN) fprintf(fpw," ");           /* pad out with blanks */
  }
  else {
    fprintf(fpw," X");
    i = strlen(t.key4)+1;
    while (i++ < KEYLEN) fprintf(fpw," ");
  }

  fprintf(fpw," %s",t.prb);                                  /* print PROBE */
  i = strlen(t.prb);
  while (i++ < PRBLEN) fprintf(fpw," ");             /* pad out with blanks */

  fprintf(fpw," %s",t.enz);                                 /* print ENZYME */
  i = strlen(t.enz);
  while (i++ < ENZLEN) fprintf(fpw," ");             /* pad out with blanks */
  
  fprintf(fpw, " %.2f",t.rv);                              /* print R-VALUE */

  i = 0;
  while ((i<NUMFS) && (t.f[i] != -5))
    if (t.f[i] != -5) fprintf(fpw," %.2f",t.f[i++]);          /* print SITE */

  fprintf(fpw,"\n");                             /* print newline character */
  return(fpw);
}                                                /* END OF FUNCTIONPRNTXTLN */
/****************************************************************************/
