[gnote] Add read_all_text for Gio::File
- From: Aurimas Černius <aurimasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnote] Add read_all_text for Gio::File
- Date: Sat, 13 Apr 2019 12:56:34 +0000 (UTC)
commit 9b167a2bea60604303982fa6249591faec3d6d3e
Author: Aurimas Černius <aurisc4 gmail com>
Date: Sat Apr 13 14:39:12 2019 +0300
Add read_all_text for Gio::File
src/sharp/files.cpp | 18 +++++++++++++++++-
src/sharp/files.hpp | 3 ++-
src/test/unit/filesutests.cpp | 22 +++++++++++++++++++++-
3 files changed, 40 insertions(+), 3 deletions(-)
---
diff --git a/src/sharp/files.cpp b/src/sharp/files.cpp
index 1e40fdcf..3d9a4cba 100644
--- a/src/sharp/files.cpp
+++ b/src/sharp/files.cpp
@@ -1,7 +1,7 @@
/*
* gnote
*
- * Copyright (C) 2011,2017-2018 Aurimas Cernius
+ * Copyright (C) 2011,2017-2019 Aurimas Cernius
* Copyright (C) 2009 Hubert Figuiere
*
* Permission is hereby granted, free of charge, to any person obtaining a
@@ -124,6 +124,22 @@ namespace sharp {
return text;
}
+ Glib::ustring file_read_all_text(const Glib::RefPtr<Gio::File> & path)
+ {
+ auto stream = path->read();
+ std::ostringstream os;
+ int buf_size = 4 * 1024;
+ char buffer[buf_size];
+ gssize read = 0;
+ do {
+ read = stream->read(buffer, buf_size);
+ os.write(buffer, read);
+ }
+ while(read == buf_size);
+ stream->close();
+ return os.str();
+ }
+
void file_write_all_text(const Glib::ustring & path, const Glib::ustring & content)
{
std::ofstream fout(path);
diff --git a/src/sharp/files.hpp b/src/sharp/files.hpp
index 85fc0fa3..41ffa2df 100644
--- a/src/sharp/files.hpp
+++ b/src/sharp/files.hpp
@@ -1,7 +1,7 @@
/*
* gnote
*
- * Copyright (C) 2017-2018 Aurimas Cernius
+ * Copyright (C) 2017-2019 Aurimas Cernius
* Copyright (C) 2009 Hubert Figuiere
*
* Permission is hereby granted, free of charge, to any person obtaining a
@@ -47,6 +47,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);
+ Glib::ustring file_read_all_text(const Glib::RefPtr<Gio::File> & 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 e5a59ec5..fe76c6d1 100644
--- a/src/test/unit/filesutests.cpp
+++ b/src/test/unit/filesutests.cpp
@@ -1,7 +1,7 @@
/*
* gnote
*
- * Copyright (C) 2017-2018 Aurimas Cernius
+ * Copyright (C) 2017-2019 Aurimas Cernius
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -117,6 +117,26 @@ SUITE(files)
CHECK_EQUAL("line1\nline2\nline3", file_content);
}
+ TEST(read_all_text_gio)
+ {
+ Glib::ustring file_content;
+
+ char temp_file_name[] = "/tmp/gnotetestXXXXXX";
+ int fd = mkstemp(temp_file_name);
+ close(fd);
+
+ auto file = Gio::File::create_for_path(temp_file_name);
+ file_content = sharp::file_read_all_text(file);
+ CHECK_EQUAL("", file_content);
+
+ FILE *f = fopen(temp_file_name, "w");
+ fputs("line1\nline2\nline3", f);
+ fclose(f);
+
+ file_content = sharp::file_read_all_text(file);
+ CHECK_EQUAL("line1\nline2\nline3", file_content);
+ }
+
TEST(write_all_text)
{
Glib::ustring file_content = "line1\nline2\nline3";
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]