
/*
 *  photom: a package to perform photometric calibration
 *  Copyright (C) 2001  Michael William Richmond
 *
 *  Contact: Michael William Richmond
 *           Physics Department
 *           Rochester Institute of Technology
 *           85 Lomb Memorial Drive
 *           Rochester, NY  14623-5603
 *           E-mail: mwrsps@rit.edu
 *
 *  
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */



   /*
    * little support functions for the matching code 
    *
    */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#include <string.h>
#include "misc.h"
#include "photom.h"



   /*********************************************************************
    * ROUTINE: shMalloc
    *
    * Attempt to allocate the given number of bytes.  Return the 
    * memory, if we succeeded, or print an error message and
    * exit with error code if we failed.
    * 
    * RETURNS:
    *      void *             to new memory, if we got it
    */

void *
shMalloc
   (
   int nbytes                /* I: allocate a chunk of this many bytes */
   )
{
   void *vptr;

   if ((vptr = (void *) malloc(nbytes)) == NULL) {
      shError("shMalloc: failed to allocate for %d bytes", nbytes);
      exit(1);
   }
   return(vptr);
}


   /*********************************************************************
    * ROUTINE: shFree
    *
    * Attempt to free the given piece of memory.  
    * 
    * RETURNS:
    *      nothing
    */

void 
shFree
   (
   void *vptr                /* I: free this chunk of memory */
   )
{
   free(vptr);
}



   /*********************************************************************
    * ROUTINE: shError
    *
    * Print the given error message to stderr, but continue to execute.
    * 
    * RETURNS:
    *      nothing
    */

void 
shError
   (
   char *format,             /* I: format part of printf statement */
   ...                       /* I: optional arguments to printf */
   )
{
   va_list ap;

   va_start(ap, format);
   (void) vfprintf(stderr, (const char *)format, ap);
   fputc('\n', stderr);
   fflush(stdout);
   fflush(stderr);
   va_end(ap);
}


   /*********************************************************************
    * ROUTINE: shFatal
    *
    * Print the given error message to stderr, and halt program execution.
    * 
    * RETURNS:
    *      nothing
    */

void 
shFatal
   (
   char *format,             /* I: format part of printf statement */
   ...                       /* I: optional arguments to printf */
   )
{
   va_list ap;

   va_start(ap, format);
   (void) vfprintf(stderr, (const char *)format, ap);
   fputc('\n', stderr);
   fflush(stdout);
   fflush(stderr);
   va_end(ap);
   exit(1);
}


   /*********************************************************************
    * ROUTINE: shDebugSet, shDebug
    *
    * shDebugSet: sets the static variable 'debug_level' to the
    * given integer; it starts at 0, but the user may set it to
    * higher levels.  The higher the level, the more messages
    * may be printed during execution.
    *
    * shDebug: If the current 'debug level' is >= the passed 'level', 
    * then print the given message to stdout, and continue execution.
    * Otherwise, just continue.
    * 
    * RETURNS:
    *      nothing
    */

static int debug_level = 0;

void
shDebugSet
   (
   int level                 /* I: set debug level to this value */
   )
{
   debug_level = level;
}

void 
shDebug
   (
   int level,                /* I: debug level at which we print this */
   char *format,             /* I: format part of printf statement */
   ...                       /* I: optional arguments to printf */
   )
{
   va_list ap;

   if (level > debug_level) {
      return;
   }

   va_start(ap, format);
   (void) vfprintf(stdout, (const char *)format, ap);
   fputc('\n', stdout);
   fflush(stdout);
   va_end(ap);
}


/**********************************************************************
 * ROUTINE: lines_in_file
 *
 * Given a FILE pointer, run through the file and count the number
 * of lines.  Move the FILE pointer back to the start of the file
 * when finished.
 *
 * RETURNS:
 *    number of lines in file
 */

int 
lines_in_file
   (
   FILE *fp                  /* I/O: pointer to file */
   )
{
   char line[LINELEN];
   int nlines = 0;

   shAssert(fp != NULL);

   rewind(fp);
   while (fgets(line, LINELEN, fp) != NULL) {
      nlines++;
   }
   rewind(fp);
   
   return(nlines);
}
