/* readfile.c -- Copyright 1992, 1994, 1995 Liam R. E. Quin. * All Rights Reserved. * This code is NOT in the public domain. * See the file COPYRIGHT for full details. */ /* $Id: readfile.c,v 1.11 2001/05/31 03:50:13 liam Exp $ * readfile - read files or lines into memory. */ #include "error.h" #include #include #include "globals.h" #include #include /* isspace() etc */ #ifdef HAVE_STDLIB_H # include #else # include #endif #ifdef HAVE_UNISTD_H # include #endif #include "emalloc.h" #include "lqutil.h" #include "lqtrace.h" /* * LQU_ReadFile * Utilities/Files * *

Reads the file named by the Name argument, and returns a * pointer to an array of pointers to the start of * each line in the file.

*

The Flags argument is any combination of flags from * lqutil.h combined with bitwise or; in practice, * however, LQUF_NORMAL is the most frequently used flag, which is * a bitwise or of all of the flags described below. *

The flags are as follows: *

  • LQUF_IGNBLANKS to throw away blank lines;
  • *
  • LQUF_IGNSPACES to discard leading and trailing spaces;
  • *
  • LQUF_IGNHASH to discard leading comments (# with a hash-sign);
  • *
  • LQUF_IGNALLHASH to discard comments (# with a hash-sign);
  • *
  • LQUF_ESCAPEOK to accept \# and \\ as # and \.
  • *

    This is the file descriptor version of LQU_fReadFile.

    *

    In the event of an error, the given Severity argument is passed * to Error, along with the given What argument, which should be a * brief English description, perhaps of the order of three words long, * of the file. * * the number of lines read, if any. The char ** pointed to by * the Lines argument is set to point to an array of strings, each * containing one line of text, NUL-terminated with trailing * newlines removed. * If E_FATAL was given, LQU_fReadFile does not return after an error. * * int numberOfLines; * char **theLines; * int i; * * numberOfLines = LQU_fReadFile(E_FATAL, * "julian.txt", * "Book of Meditations", * &theLines, * LQUF_NORMAL * ); * * for (i = 0; i < numberOfLines; i++) { * printf("Line %d was: %s\n", i, Lines[i]); * efree(Lines[i]); * } * efree((char *) Lines); * * * Generates a Warning or Error of the given Severity if the file * can't be opened, and attempts to diagnose the cause. * * LQU_fReadFile * */ API long LQU_ReadFile(Severity, Name, What, Lines, Flags) int Severity; CONST char *Name; CONST char *What; char ***Lines; int Flags; { FILE *f; long NLines; f = LQU_fEopen(Severity, Name, What, "r"); if (f == (FILE *) 0) { if (Lines) { *Lines = (char **) 0; } return -1L; } NLines = LQU_fReadFile(Severity, f, Name, What, Lines, Flags); LQU_fEclose(Severity, f, Name, What); return NLines; }