]> Savannah Git Hosting - gnulib.git/commitdiff
read-file: add flags to modify reading behavior
authorDaiki Ueno <ueno@gnu.org>
Wed, 27 May 2020 06:14:44 +0000 (08:14 +0200)
committerDaiki Ueno <ueno@gnu.org>
Wed, 27 May 2020 12:14:00 +0000 (14:14 +0200)
* lib/read-file.h (RF_BINARY): New define.
(fread_file, read_file): Take FLAGS argument.
(read_binary_file): Remove.
* lib/read-file.c (internal_read_file): Merge into ...
(read_file): ... here.
* modules/read-file-tests (Files): Add "tests/macros.h".
* tests/test-read-file.c (main): Refactor using ASSERT macro.
* NEWS: Mention this change.

ChangeLog
NEWS
lib/read-file.c
lib/read-file.h
modules/read-file-tests
tests/test-read-file.c

index aa7bc100da72489e2bb52bc002f05429d714532c..291fd0788b68b4e452ec05d341b52b393d3e070c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2020-05-27  Daiki Ueno  <ueno@gnu.org>
+
+       read-file: add flags to modify reading behavior
+       * lib/read-file.h (RF_BINARY): New define.
+       (fread_file, read_file): Take FLAGS argument.
+       (read_binary_file): Remove.
+       * lib/read-file.c (internal_read_file): Merge into ...
+       (read_file): ... here.
+       * modules/read-file-tests (Files): Add "tests/macros.h".
+       * tests/test-read-file.c (main): Refactor using ASSERT macro.
+       * NEWS: Mention this change.
+
 2020-05-26  Bernhard Voelker  <mail@bernhard-voelker.de>
 
        doc/gnulib-intro.texi: add missing "to" in sentence
diff --git a/NEWS b/NEWS
index 99973c5c39547f456f16d388df1a3a2d25ad0c1d..c8f78ea16135bd57cf6f8a3397d28a842ee2b754 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -58,6 +58,11 @@ User visible incompatible changes
 
 Date        Modules         Changes
 
+2020-05-27  read-file       The functions provided by this module now take an
+                            'int flags' argument to modify the file reading
+                            behavior.  The read_binary_file function has been
+                            removed as it is no longer necessary.
+
 2020-04-27  getdate         This deprecated module is removed. Use the module
                             'parse-datetime' instead. Instead of
                               #include "getdate.h"
index 293bc3e8a57f0644800817fcc3a155e6c8e6f0be..904f1c9018583c53ff82462616ab3edd6f92f0ff 100644 (file)
@@ -40,7 +40,7 @@
    *LENGTH.  On errors, *LENGTH is undefined, errno preserves the
    values set by system functions (if any), and NULL is returned.  */
 char *
-fread_file (FILE *stream, size_t *length)
+fread_file (FILE *stream, int flags _GL_UNUSED, size_t *length)
 {
   char *buf = NULL;
   size_t alloc = BUFSIZ;
@@ -134,9 +134,19 @@ fread_file (FILE *stream, size_t *length)
   }
 }
 
-static char *
-internal_read_file (const char *filename, size_t *length, const char *mode)
+/* Open and read the contents of FILENAME, and return a newly
+   allocated string with the content, and set *LENGTH to the length of
+   the string.  The string is zero-terminated, but the terminating
+   zero byte is not counted in *LENGTH.  On errors, *LENGTH is
+   undefined, errno preserves the values set by system functions (if
+   any), and NULL is returned.
+
+   If the RF_BINARY flag is set in FLAGS, the file is opened in binary
+   mode.  */
+char *
+read_file (const char *filename, int flags, size_t *length)
 {
+  const char *mode = (flags & RF_BINARY) ? "rbe" : "re";
   FILE *stream = fopen (filename, mode);
   char *out;
   int save_errno;
@@ -144,7 +154,7 @@ internal_read_file (const char *filename, size_t *length, const char *mode)
   if (!stream)
     return NULL;
 
-  out = fread_file (stream, length);
+  out = fread_file (stream, flags, length);
 
   save_errno = errno;
 
@@ -161,28 +171,3 @@ internal_read_file (const char *filename, size_t *length, const char *mode)
 
   return out;
 }
-
-/* Open and read the contents of FILENAME, and return a newly
-   allocated string with the content, and set *LENGTH to the length of
-   the string.  The string is zero-terminated, but the terminating
-   zero byte is not counted in *LENGTH.  On errors, *LENGTH is
-   undefined, errno preserves the values set by system functions (if
-   any), and NULL is returned.  */
-char *
-read_file (const char *filename, size_t *length)
-{
-  return internal_read_file (filename, length, "re");
-}
-
-/* Open (on non-POSIX systems, in binary mode) and read the contents
-   of FILENAME, and return a newly allocated string with the content,
-   and set LENGTH to the length of the string.  The string is
-   zero-terminated, but the terminating zero byte is not counted in
-   the LENGTH variable.  On errors, *LENGTH is undefined, errno
-   preserves the values set by system functions (if any), and NULL is
-   returned.  */
-char *
-read_binary_file (const char *filename, size_t *length)
-{
-  return internal_read_file (filename, length, "rbe");
-}
index bb28abd65ea9c4171202b01010b766854f5a2019..7ff82ca77df6a993edcb2bc31a6d201983c8c26e 100644 (file)
 /* Get FILE.  */
 #include <stdio.h>
 
-extern char *fread_file (FILE * stream, size_t * length);
+/* Indicate that the file is treated as binary.  */
+#define RF_BINARY 0x1
 
-extern char *read_file (const char *filename, size_t * length);
+extern char *fread_file (FILE * stream, int flags, size_t * length);
 
-extern char *read_binary_file (const char *filename, size_t * length);
+extern char *read_file (const char *filename, int flags, size_t * length);
 
 #endif /* READ_FILE_H */
index 361bca8065e9ac050a70190e2a88c9db7713d770..29963164446eb35d1872ad7629820b9c7dd56de7 100644 (file)
@@ -1,5 +1,6 @@
 Files:
 tests/test-read-file.c
+tests/macros.h
 
 Depends-on:
 
index 930cf4acb0d0b112aea52ad8c5043b4c5b4dd915..84b90499403e65f114a5deb2f1ee65261bea7ebd 100644 (file)
 #include <stdlib.h>
 #include <sys/stat.h>
 
+#include "macros.h"
+
 #define FILE1 "/etc/resolv.conf"
 #define FILE2 "/dev/null"
 
 int
-main (void)
+test_read_file (int flags)
 {
   struct stat statbuf;
   int err = 0;
@@ -37,7 +39,7 @@ main (void)
   if (stat (FILE1, &statbuf) >= 0)
     {
       size_t len;
-      char *out = read_file (FILE1, &len);
+      char *out = read_file (FILE1, flags, &len);
 
       if (!out)
         {
@@ -80,7 +82,7 @@ main (void)
   if (stat (FILE2, &statbuf) >= 0)
     {
       size_t len;
-      char *out = read_file (FILE2, &len);
+      char *out = read_file (FILE2, flags, &len);
 
       if (!out)
         {
@@ -109,3 +111,12 @@ main (void)
 
   return err;
 }
+
+int
+main (void)
+{
+  ASSERT (!test_read_file (0));
+  ASSERT (!test_read_file (RF_BINARY));
+
+  return 0;
+}