# include # include # include # include # include "ppc_fetch_line.h" /******************************************************************************/ char *trim_line ( char *s ) /******************************************************************************/ { char *t; /* Increase S to location of first nonwhitespace character. */ while ( isspace ( *s ) ) { s++; } /* T begins at S, but advances until it reaches a null or #. */ t = s; while ( *t != '\0' && *t != '#' ) { t++; } if ( *t == '#' ) { *t = '\0'; } /* Replace each trailing whitespace character by null. */ if ( s < t ) { while ( 1 ) { t--; if ( ! isspace ( *t ) ) { break; } *t = '\0'; } } return s; } /******************************************************************************/ char *fetch_line ( char *buf, int buflen, FILE *stream, int *lineno ) /******************************************************************************/ /* Purpose: fetch_line() returns a line from a text file. Licensing: This code is distributed under the MIT license. Modified: 06 September 2023 Author: Original C version by Rouben Rostamian. This version by John Burkardt. Reference: Rouben Rostamian, Programming Projects in C for Students of Engineering, Science, and Mathematics, SIAM, 2014, ISBN: 978-1-611973-49-5 Input: Output: */ { char *s; if ( fgets ( buf, buflen, stream ) == NULL ) { return NULL; } (*lineno)++; if ( buf[ strlen(buf) - 1 ] != '\n' ) { fprintf ( stderr, "\n" ); fprintf ( stderr, "ppc_fetch_line(): Fatal error!\n" ); fprintf ( stderr, " Input line %d too long\n", *lineno ); fprintf ( stderr, " for buffer size %d\n", buflen ); exit ( EXIT_FAILURE ); } s = trim_line ( buf ); if ( *s != '\0' ) { return s; } else { return fetch_line ( buf, buflen, stream, lineno ); } }