diff options
Diffstat (limited to 'src/file-list.c')
-rw-r--r-- | src/file-list.c | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/src/file-list.c b/src/file-list.c new file mode 100644 index 0000000..1cffa56 --- /dev/null +++ b/src/file-list.c @@ -0,0 +1,93 @@ +/* Reading file lists. + Copyright (C) 1995-1998, 2000, 2001 Free Software Foundation, Inc. + + 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, 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. */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "file-list.h" +#include "str-list.h" +#include "error.h" +#include "getline.h" +#include "libgettext.h" + +/* A convenience macro. I don't like writing gettext() every time. */ +#define _(str) gettext (str) + + +/* Read list of filenames from a file. */ +string_list_ty * +read_names_from_file (file_name) + const char *file_name; +{ + size_t line_len = 0; + char *line_buf = NULL; + FILE *fp; + string_list_ty *result; + + if (strcmp (file_name, "-") == 0) + fp = stdin; + else + { + fp = fopen (file_name, "r"); + if (fp == NULL) + error (EXIT_FAILURE, errno, + _("error while opening \"%s\" for reading"), file_name); + } + + result = string_list_alloc (); + + while (!feof (fp)) + { + /* Read next line from file. */ + int len = getline (&line_buf, &line_len, fp); + + /* In case of an error leave loop. */ + if (len < 0) + break; + + /* Remove trailing '\n' and trailing whitespace. */ + if (len > 0 && line_buf[len - 1] == '\n') + line_buf[--len] = '\0'; + while (len > 0 + && (line_buf[len - 1] == ' ' + || line_buf[len - 1] == '\t' + || line_buf[len - 1] == '\r')) + line_buf[--len] = '\0'; + + /* Test if we have to ignore the line. */ + if (*line_buf == '\0' || *line_buf == '#') + continue; + + string_list_append_unique (result, line_buf); + } + + /* Free buffer allocated through getline. */ + if (line_buf != NULL) + free (line_buf); + + /* Close input stream. */ + if (fp != stdin) + fclose (fp); + + return result; +} |