]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Use std::ios_base::openmode as the third Util::write_file parameter
authorJoel Rosdahl <joel@rosdahl.net>
Mon, 18 May 2020 18:03:24 +0000 (20:03 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 20 May 2020 18:07:12 +0000 (20:07 +0200)
src/Util.cpp
src/Util.hpp
unittest/test_Util.cpp

index 104abb00762d3c3a949eed48a1c565d7b697f77c..d951be2d06ae11ff35db32cb79cfc0a751e6e8a1 100644 (file)
@@ -762,10 +762,12 @@ wipe_path(const std::string& path)
 }
 
 void
-write_file(const std::string& path, const std::string& data, bool binary)
+write_file(const std::string& path,
+           const std::string& data,
+           std::ios_base::openmode open_mode)
 {
-  std::ofstream file(path,
-                     binary ? std::ios::out | std::ios::binary : std::ios::out);
+  open_mode |= std::ios::out;
+  std::ofstream file(path, open_mode);
   if (!file) {
     throw Error(strerror(errno));
   }
index 44c2306b9e681052323b1f1f5e046ebfb457a0d3..38c49861ef0839b47750a7432d9e0a59d2f8198d 100644 (file)
@@ -311,12 +311,14 @@ bool unlink_tmp(const std::string& path,
 // Throws Error on error.
 void wipe_path(const std::string& path);
 
-// Write file data from a string.
+// Write file data from a string. The file will be opened according to
+// `open_mode`, which always will include `std::ios::out` even if not specified
+// at the call site.
 //
 // Throws `Error` on error. The description contains the error message without
 // the path.
 void write_file(const std::string& path,
                 const std::string& data,
-                bool binary = false);
+                std::ios_base::openmode open_mode = std::ios::out);
 
 } // namespace Util
index ade6875bce7fa47cb493494dc79e2f369749de94..d68f009fe97bbeb1da9f56d0cf703c792067ba3f 100644 (file)
@@ -482,6 +482,14 @@ TEST_CASE("Util::read_file and Util::write_file")
   std::string data = Util::read_file("test");
   CHECK(data == "foo\nbar\n");
 
+  Util::write_file("test", "car");
+  data = Util::read_file("test");
+  CHECK(data == "car");
+
+  Util::write_file("test", "pet", std::ios::app);
+  data = Util::read_file("test");
+  CHECK(data == "carpet");
+
   CHECK_THROWS_WITH(Util::read_file("does/not/exist"),
                     Equals("No such file or directory"));