/*****************************************************************************
******************************************************************************
*****                                                                    *****
*****                           RSFUNCS.H v1.1                           *****
*****    Copyright (c) 1990 by Joyce C. Miller.  All rights reserved.    *****
*****                                                                    *****
***** This  header file  contains some functions  used frequently by the *****
***** programs in the RESTSITE package,  which analyzes restriction site *****
***** and  fragment data.  This file needs the error  messages  found in *****
***** file RSERRORS.C.  It also needs "MATH.H".                          *****
*****                                                                    *****
******************************************************************************
*****************************************************************************/
#include <math.h>
#include <string.h>
/*****************************************************************************
**                           FUNCTION PROTOTYPES                            **
*****************************************************************************/
FILE *opntrdfl(FILE *fpr,char rdfl[]);        /* open text file for reading */
FILE *opntwtfl(FILE *fpw,char wtfl[]);        /* open text file for writing */
FILE *opnbrdfl(FILE *fpr,char rdfl[]);      /* open binary file for reading */
FILE *opnbwtfl(FILE *fpw,char wtfl[]);      /* open binary file for writing */
FILE *opnbrwfl(FILE *fpw, char wtfl[]);       /* open binary file for R & W */
FILE *opnbapfl(FILE *fpw,char wtfl[],char bakfl[]); /* open & append binary */
FILE *opntapfl(FILE *fpw, char wtfl[]);          /* open & append text file */
char *inttoalph(int i, char str[]);         /* essentially an ITOA function */
float roundnum(float number, float dec);             /* rounds off a number */
/*****************************************************************************
**                                                                          **
**                             FUNCTION OPNTRDFL                            **
**                                                                          **
** This function opens a text file for reading.                             **
**                                                                          **
** Functions called:                                                        **
** rserror310     --  error message if error opening file for reading.      **
*****************************************************************************/
FILE *opntrdfl(FILE *fpr, char rdfl[])
{
  if ((fpr = fopen(rdfl,"rt")) == NULL)
    rserror310(rdfl);                                         /* open error */
  rewind(fpr);
  return(fpr);                                   /* return pointer position */
}                                               /* END OF FUNCTION OPNTRDFL */
/*****************************************************************************
**                                                                          **
**                             FUNCTION OPNTWTFL                            **
**                                                                          **
** This function opens a text file for writing.                             **
**                                                                          **
** Functions called:                                                        **
** rserror320     --  error message if error opening file for writing.      **
*****************************************************************************/
FILE *opntwtfl(FILE *fpw, char wtfl[])
{
  if ((fpw = fopen(wtfl,"wt")) == NULL)
    rserror320(wtfl);                                         /* open error */
  rewind(fpw);
  return(fpw);                                   /* return pointer position */
}                                               /* END OF FUNCTION OPNTWTFL */
/*****************************************************************************
**                                                                          **
**                            FUNCTION OPNBRDFL                             **
**                                                                          **
** This function opens a binary file for reading.                           **
**                                                                          **
** Functions called:                                                        **
** rserror310     --  error message if error opening file for reading.      **
*****************************************************************************/
FILE *opnbrdfl(FILE *fpr, char rdfl[])
{
  if ((fpr = fopen(rdfl,"rb")) == NULL)
    rserror310(rdfl);                                         /* open error */
  rewind(fpr);
  return(fpr);                                   /* return pointer position */
}                                               /* END OF FUNCTION OPNBRDFL */
/*****************************************************************************
**                                                                          **
**                            FUNCTION OPNBWTFL                             **
**                                                                          **
** This function opens a binary file for writing.                           **
**                                                                          **
** Functions called:                                                        **
** rserror320     --  error message if error opening file for writing.      **
*****************************************************************************/
FILE *opnbwtfl(FILE *fpw, char wtfl[])
{
  if ((fpw = fopen(wtfl,"wb")) == NULL)
    rserror320(wtfl);                                         /* open error */
  rewind(fpw);
  return(fpw);                                   /* return pointer position */
}                                               /* END OF FUNCTION OPNBWTFL */
/*****************************************************************************
**                                                                          **
**                            FUNCTION OPNBRWFL                             **
**                                                                          **
** This function opens a binary file for reading and writing.               **
**                                                                          **
** Functions called:                                                        **
** rserror320     --  error message if error opening file for writing.      **
*****************************************************************************/
FILE *opnbrwfl(FILE *fpw, char wtfl[])
{
  if ((fpw = fopen(wtfl,"rb+")) == NULL)
    rserror320(wtfl);                                         /* open error */
  rewind(fpw);
  return(fpw);                                   /* return pointer position */
}                                               /* END OF FUNCTION OPNBRWFL */
/*****************************************************************************
**                                                                          **
**                            FUNCTION OPNBAPFL                             **
**                                                                          **
** This function opens a binary  file for appending, after  making a backup **
** copy of it.                                                              **
**                                                                          **
** Functions called:                                                        **
** rserror320     --  error message if error opening file for writing.      **
*****************************************************************************/
FILE *opnbapfl(FILE *fpw, char wtfl[], char bakfl[])
{
  char *comm = "COPY ";

  printf("\r\n Copying %s to %s\r\n",wtfl,bakfl);        /* message to user */
  comm = strcat(comm,wtfl);                                /* = "COPY WTFL" */
  comm = strcat(comm," ");                                /* = "COPY WTFL " */
  comm = strcat(comm,bakfl);                         /* = "COPY WTFL BAKFL" */
  printf("\n\n");
  system(comm);                              /* carry out "COPY WTFL BAKFL" */

  if ((fpw = fopen(wtfl,"ab")) == NULL)
    rserror330(wtfl);                                         /* open error */
  rewind(fpw);
  return(fpw);                                   /* return pointer position */
}                                               /* END OF FUNCTION OPNBAPFL */
/*****************************************************************************
**                                                                          **
**                            FUNCTION OPNTAPFL                             **
**                                                                          **
** This function opens a text file for appending.                           **
**                                                                          **
** Functions called:                                                        **
** rserror320     --  error message if error opening file for writing.      **
*****************************************************************************/
FILE *opntapfl(FILE *fpw, char wtfl[])
{
  if ((fpw = fopen(wtfl,"at")) == NULL)
    rserror330(wtfl);                                         /* open error */
  rewind(fpw);
  return(fpw);                                   /* return pointer position */
}                                               /* END OF FUNCTION OPNTAPFL */
/*****************************************************************************
**                                                                          **
**                            FUNCTION INTTOALPH                            **
**                                                                          **
** This  function is  invoked by  several other  functions.  It receives an **
** integer and  an empty  string, and turns  the integer  into  a character **
** string, and returns a pointer to the character string.  It is my version **
** of the very common (but non-ANSI) function ITOA.                         **
**                                                                          **
** Functions called:  none                                                  **
*****************************************************************************/
char *inttoalph(int i, char str[])
{
  int j,k,l;                                      /* loop control variables */
  double n = 0;                            /* number of digits in integer I */
  double z = 0;                             /* temp. variable used to get n */
  char *f;

    str[0] = '\0';                                     /* initialize string */
    if ((i) > 0) z = modf((log10(i)),&n);      /* get number of digits in I */
    l = n;                                  /* convert double to an integer */
    for (j=0; j<n; ++j) {                        /* examine each digit of I */
      for (k=0; (i>=pow(10,l)); ++k) i = i - pow(10,l);
      if (k == 0) f = strcat(str,"0");       /* convert digit to character, */
      if (k == 1) f = strcat(str,"1");        /* then tack character onto f */
      if (k == 2) f = strcat(str,"2");
      if (k == 3) f = strcat(str,"3");
      if (k == 4) f = strcat(str,"4");
      if (k == 5) f = strcat(str,"5");
      if (k == 6) f = strcat(str,"6");
      if (k == 7) f = strcat(str,"7");
      if (k == 8) f = strcat(str,"8");
      if (k == 9) f = strcat(str,"9");
      l--;
    }
    if (i == 0) f = strcat(str,"0");
    if (i == 1) f = strcat(str,"1");
    if (i == 2) f = strcat(str,"2");
    if (i == 3) f = strcat(str,"3");
    if (i == 4) f = strcat(str,"4");
    if (i == 5) f = strcat(str,"5");
    if (i == 6) f = strcat(str,"6");
    if (i == 7) f = strcat(str,"7");
    if (i == 8) f = strcat(str,"8");
    if (i == 9) f = strcat(str,"9");
    return(f);                                             /* return string */
}                                              /* END OF FUNCTION INTTOALPH */
/*****************************************************************************
**                                                                          **
**                             FUNCTION ROUNDNUM                            **
**                                                                          **
** This  function takes a  floating-point  number and  rounds  it  off.  It **
** receives the number to be  rounded, and the  number of decimal places it **
** is to be rounded to.  For example:                                       **
**                        3.5 , 0   rounds to : 4                           **
**                        3.5 , 0.1 rounds to : 3.5                         **
**                        3.49, 0.1 rounds to : 3.5                         **
**                        3.51, 0.1 rounds to : 3.5                         **
**                        35  , 10  rounds to : 40                          **
**                        350 , 100 rounds to : 400                         **
**                                                                          **
** Functions called:  none                                                  **
*****************************************************************************/
float roundnum(float number, float dec)
{
  float i;                                   /* number to be rounded off TO */

  if (dec != 0) i = number/dec;          /* divide number by decimal places */
  else i = number;

  if ((i - floor(i)) >= 0.5) i = floor(i) + 1;       /* round to an integer */
  else i = floor(i);

  if (dec != 0) i = i * dec;                  /* multiply by decimal places */
  return(i);                                                /* send it back */
}                                               /* END OF FUNCTION ROUNDNUM */
/****************************************************************************/
