Browse Source

Extended FileOutputStream class

- added constructor for taking "append" flag to
provide functionality to append content to existing
 file content
- extended tests for FileOutputStream class
Patrick 4 years ago
parent
commit
09376be620

+ 22 - 6
source/io/FileOutputStream.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-20
- * Changed:         2020-08-20
+ * Changed:         2020-08-21
  *
  * */
 
@@ -14,11 +14,14 @@
 ls_std::FileOutputStream::FileOutputStream(File &_file) : Class("FileOutputStream"),
 file(_file)
 {
-  if(!_file.exists()) {
-    throw ls_std::FileNotFoundException {};
-  } else {
-    this->outputStream.open(_file.getAbsoluteFilePath());
-  }
+  this->_init();
+}
+
+ls_std::FileOutputStream::FileOutputStream(File &_file, bool _append) : Class("FileOutputStream"),
+append(_append),
+file(_file)
+{
+  this->_init();
 }
 
 ls_std::FileOutputStream::~FileOutputStream()
@@ -52,3 +55,16 @@ void ls_std::FileOutputStream::_close()
     this->outputStream.close();
   }
 }
+
+void ls_std::FileOutputStream::_init()
+{
+  if(!this->file.exists()) {
+    throw ls_std::FileNotFoundException {};
+  } else {
+    if(this->append) {
+      this->outputStream.open(this->file.getAbsoluteFilePath(), std::ios::out | std::ios::app);
+    } else {
+      this->outputStream.open(this->file.getAbsoluteFilePath());
+    }
+  }
+}

+ 4 - 1
source/io/FileOutputStream.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-20
- * Changed:         2020-08-20
+ * Changed:         2020-08-21
  *
  * */
 
@@ -20,6 +20,7 @@ namespace ls_std {
     public:
 
       explicit FileOutputStream(File& _file);
+      explicit FileOutputStream(File& _file, bool _append);
       ~FileOutputStream();
 
       void close();
@@ -27,10 +28,12 @@ namespace ls_std {
 
     private:
 
+      bool append {};
       File file;
       std::ofstream outputStream {};
 
       void _close();
+      void _init();
   };
 }
 

+ 31 - 1
test/cases/io/FileOutputStreamTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-20
- * Changed:         2020-08-20
+ * Changed:         2020-08-21
  *
  * */
 
@@ -11,6 +11,8 @@
 #include "../../../source/io/File.hpp"
 #include "../../TestHelper.hpp"
 #include "../../../source/io/FileOutputStream.hpp"
+#include "../../../source/boxing/String.hpp"
+#include "../../../source/io/FileReader.hpp"
 
 namespace {
   class FileOutputStreamTest : public ::testing::Test {
@@ -38,4 +40,32 @@ namespace {
     file.remove();
     ASSERT_FALSE(file.exists());
   }
+
+  TEST_F(FileOutputStreamTest, append)
+  {
+    std::string path = TestHelper::getResourcesFolderLocation() + "tmp_output_stream.txt";
+    ls_std::File file {path};
+    file.createNewFile();
+    ASSERT_TRUE(file.exists());
+
+    ls_std::FileOutputStream outputStream {file};
+    ASSERT_TRUE(outputStream.write("Hello! "));
+    ASSERT_TRUE(outputStream.write("How are you?"));
+    outputStream.close();
+
+    ls_std::FileOutputStream newOutputStream {file, true};
+    ASSERT_TRUE(newOutputStream.write(" I'm fine! "));
+    ASSERT_TRUE(newOutputStream.write("Thank you!"));
+    newOutputStream.close();
+
+    // validation
+
+    ls_std::FileReader reader {file};
+    ls_std::String content {reader.read()};
+
+    ASSERT_TRUE(content.contains("Hello! How are you? I'm fine! Thank you!"));
+
+    file.remove();
+    ASSERT_FALSE(file.exists());
+  }
 }