[gnote] Add utilities for reading text files
- From: Aurimas ÄŒernius <aurimasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnote] Add utilities for reading text files
- Date: Sun, 12 Feb 2017 20:14:02 +0000 (UTC)
commit 22c6e5e77eefd3c35e6a1244f460215584c6487e
Author: Aurimas ÄŒernius <aurisc4 gmail com>
Date: Sun Feb 12 22:13:30 2017 +0200
Add utilities for reading text files
src/sharp/files.cpp | 34 +++++++++++++++++++++++++++
src/sharp/files.hpp | 3 ++
src/test/unit/filesutests.cpp | 51 +++++++++++++++++++++++++++++++++++++++++
3 files changed, 88 insertions(+), 0 deletions(-)
---
diff --git a/src/sharp/files.cpp b/src/sharp/files.cpp
index 52fbb73..f15ab01 100644
--- a/src/sharp/files.cpp
+++ b/src/sharp/files.cpp
@@ -23,6 +23,8 @@
* DEALINGS IN THE SOFTWARE.
*/
+#include <fstream>
+
#include <glib/gstdio.h>
#include <glibmm.h>
#include <giomm/file.h>
@@ -74,5 +76,37 @@ namespace sharp {
{
g_rename(from.c_str(), to.c_str());
}
+
+
+ std::vector<Glib::ustring> file_read_all_lines(const Glib::ustring & path)
+ {
+ std::vector<Glib::ustring> lines;
+ std::ifstream fin;
+ fin.open(path.c_str());
+ if(fin.is_open()) {
+ std::string line;
+ while(std::getline(fin, line)) {
+ lines.push_back(line);
+ }
+ fin.close();
+ }
+
+ return lines;
+ }
+
+ Glib::ustring file_read_all_text(const Glib::ustring & path)
+ {
+ auto lines = file_read_all_lines(path);
+ if(lines.size() == 0) {
+ return "";
+ }
+
+ Glib::ustring text = lines[0];
+ for(unsigned i = 1; i < lines.size(); ++i) {
+ text += "\n" + lines[i];
+ }
+
+ return text;
+ }
}
diff --git a/src/sharp/files.hpp b/src/sharp/files.hpp
index cf330b9..0f20ffa 100644
--- a/src/sharp/files.hpp
+++ b/src/sharp/files.hpp
@@ -40,6 +40,9 @@ namespace sharp {
/** return the filename from the file path */
Glib::ustring file_filename(const Glib::ustring & p);
void file_copy(const Glib::ustring & source, const Glib::ustring & dest);
+
+ std::vector<Glib::ustring> file_read_all_lines(const Glib::ustring & path);
+ Glib::ustring file_read_all_text(const Glib::ustring & path);
}
diff --git a/src/test/unit/filesutests.cpp b/src/test/unit/filesutests.cpp
index e0a39ad..0403e6a 100644
--- a/src/test/unit/filesutests.cpp
+++ b/src/test/unit/filesutests.cpp
@@ -17,6 +17,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+#include <fstream>
+#include <stdlib.h>
+#include <unistd.h>
+
#include <glibmm/miscutils.h>
#include <UnitTest++/UnitTest++.h>
@@ -58,5 +62,52 @@ SUITE(files)
CHECK(sharp::file_exists(__FILE__ __FILE__) == false);
CHECK(sharp::file_exists(__FILE__) == true);
}
+
+ TEST(read_all_lines)
+ {
+ std::vector<Glib::ustring> lines;
+ // very unlikely to exist
+ lines = sharp::file_read_all_lines(__FILE__ __FILE__);
+ CHECK_EQUAL(0, lines.size());
+
+ char temp_file_name[] = "/tmp/gnotetestXXXXXX";
+ int fd = mkstemp(temp_file_name);
+ close(fd);
+
+ lines = sharp::file_read_all_lines(temp_file_name);
+ CHECK_EQUAL(0, lines.size());
+
+ FILE *file = fopen(temp_file_name, "w");
+ fputs("line1\nline2\nline3", file);
+ fclose(file);
+
+ lines = sharp::file_read_all_lines(temp_file_name);
+ CHECK_EQUAL(3, lines.size());
+ CHECK_EQUAL("line1", lines[0]);
+ CHECK_EQUAL("line2", lines[1]);
+ CHECK_EQUAL("line3", lines[2]);
+ }
+
+ TEST(read_all_text)
+ {
+ Glib::ustring file_content;
+ // very unlikely to exist
+ file_content = sharp::file_read_all_text(__FILE__ __FILE__);
+ CHECK_EQUAL("", file_content);
+
+ char temp_file_name[] = "/tmp/gnotetestXXXXXX";
+ int fd = mkstemp(temp_file_name);
+ close(fd);
+
+ file_content = sharp::file_read_all_text(temp_file_name);
+ CHECK_EQUAL("", file_content);
+
+ FILE *file = fopen(temp_file_name, "w");
+ fputs("line1\nline2\nline3", file);
+ fclose(file);
+
+ file_content = sharp::file_read_all_text(temp_file_name);
+ CHECK_EQUAL("line1\nline2\nline3", file_content);
+ }
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]