[gnote] Add file_write_all_text
- From: Aurimas Černius <aurimasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnote] Add file_write_all_text
- Date: Thu, 16 Feb 2017 11:11:49 +0000 (UTC)
commit 49b97d4a4739695314bd8ae636d8f58aea0070a3
Author: Aurimas Černius <aurisc4 gmail com>
Date: Thu Feb 16 12:50:02 2017 +0200
Add file_write_all_text
src/sharp/files.cpp | 13 +++++++++++++
src/sharp/files.hpp | 1 +
src/test/unit/filesutests.cpp | 22 ++++++++++++++++++++++
3 files changed, 36 insertions(+), 0 deletions(-)
---
diff --git a/src/sharp/files.cpp b/src/sharp/files.cpp
index ec3500c..f716ba4 100644
--- a/src/sharp/files.cpp
+++ b/src/sharp/files.cpp
@@ -115,5 +115,18 @@ namespace sharp {
return text;
}
+
+ void file_write_all_text(const Glib::ustring & path, const Glib::ustring & content)
+ {
+ std::ofstream fout(path);
+ if(!fout.is_open()) {
+ throw sharp::Exception("Failed to open file: " + path);
+ }
+ fout << content;
+ if(!fout.good()) {
+ throw sharp::Exception("Failed to write to file");
+ }
+ fout.close();
+ }
}
diff --git a/src/sharp/files.hpp b/src/sharp/files.hpp
index 00d416c..32822ce 100644
--- a/src/sharp/files.hpp
+++ b/src/sharp/files.hpp
@@ -45,6 +45,7 @@ namespace sharp {
std::vector<Glib::ustring> file_read_all_lines(const Glib::ustring & path);
Glib::ustring file_read_all_text(const Glib::ustring & path);
+ void file_write_all_text(const Glib::ustring & path, const Glib::ustring & content);
}
diff --git a/src/test/unit/filesutests.cpp b/src/test/unit/filesutests.cpp
index 8ec25eb..80ab3da 100644
--- a/src/test/unit/filesutests.cpp
+++ b/src/test/unit/filesutests.cpp
@@ -108,5 +108,27 @@ SUITE(files)
file_content = sharp::file_read_all_text(temp_file_name);
CHECK_EQUAL("line1\nline2\nline3", file_content);
}
+
+ TEST(write_all_text)
+ {
+ Glib::ustring file_content = "line1\nline2\nline3";
+ char temp_file_name[] = "/tmp/gnotetestXXXXXX";
+ int fd = mkstemp(temp_file_name);
+ close(fd);
+
+ sharp::file_write_all_text(temp_file_name, file_content);
+ std::ifstream fin(temp_file_name);
+ std::string line1, line2, line3;
+ std::getline(fin, line1);
+ std::getline(fin, line2);
+ std::getline(fin, line3);
+ fin.close();
+
+ CHECK_EQUAL("line1", line1);
+ CHECK_EQUAL("line2", line2);
+ CHECK_EQUAL("line3", line3);
+
+ CHECK_THROW(sharp::file_write_all_text("/usr/gnotetest", file_content), sharp::Exception);
+ }
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]