/*-----------------------------------------------------------------------------

   FUNCTION:  getline_nc

      It reads a line (of specified maximum length) from a file.
      Comment lines, indicated by the '%' character in the first
      column, are ignored.  Blank lines, containing only spaces,
      tabs, and newlines, are also ignored.  The newline character
      is included in the string if there is room for it.

   PARAMETERS:

      fp = pointer to input file

      line_buffer = pointer to character array where
                    line will be stored

      n = max number of characters to read, including terminating null.

   RETURNS:  the number of characters read into the array if successful;
               this count EXCLUDES the terminating nul.
             EOF if end-of-file is reached

   CALLED FROM:  C

   (EF; Sat  09-03-1988)

   Tue  03-14-1989  Modified to accept a % as the first
   non-blank character instead of requiring it as the first
   character in the line.

   95/06/21 YX and EF: Fixed bug, so that now getline_nc always
   discards characters beyond n-1 in the input line.  Previously
   it would choke on comment lines exceeding this length.

   Wed  97/05/15 EF: modified to allow # as a comment character.

*/
#include <stdio.h>
#include <string.h>  /* strlen() */
#include "ioserv.h"  /* non_blank() */
#define NUL '\0'

#if PROTOTYPE_ALLOWED
char *non_blank(char *line)
#else
char *non_blank(line)
char *line;
#endif
{
   while (*line != NUL)   /* search up to terminating null */
   {
      if (*line != ' ' && *line != '\t' && *line != '\n' && *line != '\r')
         return(line);   /* non-white character was found */
      line++;
   }
   return(NULL);    /* no non-white characters in the line */
}

#if PROTOTYPE_ALLOWED
int getline_nc(FILE *fp, char *line_buffer, int n)
#else
int getline_nc(fp, line_buffer, n)
   FILE *fp;
   char *line_buffer;
   int n;                 /* length of line buffer */
#endif
{
   int l, c;
   char *ch_ptr;

   line_buffer[0] = NUL;
   while (fgets(line_buffer, n, fp) != NULL)
   {

      l = strlen(line_buffer);
      if (line_buffer[l-1] != '\n')   /* go to end of this line */
      {
         while ((c = getc(fp)) != '\n' && c != EOF);
      }

      if ( (ch_ptr = non_blank(line_buffer)) != NULL &&  /* not a blank line */
                    *ch_ptr != '%' && *ch_ptr != '#')

      {
         return(l);   /* normal exit, when valid non-comment line is found */
      }
   }
   return(EOF);
}

/* above, to change the function to reject any line not starting
with a number, one could use:
                    *ch_ptr > '*' && *ch_ptr < ':' &&
                    *ch_ptr != '/' && *ch_ptr != ',' ) 
This is not compatible with programs like nmea_gps, however, so
we stick with specific comment characters: % and #.
*/


#ifdef EXE

#include "ioserv.h"   /* check_fopen() */
static char line[256];

#if PROTOTYPE_ALLOWED
void main(int argc, char *argv[])
#else
void main(argc, argv)
int argc;
char *argv[];
#endif
{
   FILE *fp_in, *fp_out;
   int n;

   if (argc != 3)
   {
      printf("\nThis program filters a text file to remove lines");
      printf("\nthat start with '%'.");
      printf("\nUsage: NO_PC infile outfile\n");
      exit (0);
   }


   fp_in  = check_fopen(argv[1], "r");
   fp_out = check_fopen(argv[2], "w");

   while ((n = getline_nc(fp_in, line, 256)) != EOF)
   {
      fputs(line, fp_out);
      if (line[n-1] != '\n')
         fputc('\n', fp_out);
   }

   fclose(fp_in);
   fclose(fp_out);
}

#endif
