Commit 2a35ef5b authored by Kirill Smelkov's avatar Kirill Smelkov

os += ReadFile

Add convenient utility to read whole file and return its content
similarly to Go. The code is taken from wendelin.core:

https://lab.nexedi.com/nexedi/wendelin.core/blob/wendelin.core-2.0.alpha1-18-g38dde766/wcfs/client/wcfs_misc.cpp#L246-281
parent e18adbab
...@@ -49,6 +49,7 @@ namespace sys = golang::internal::syscall; ...@@ -49,6 +49,7 @@ namespace sys = golang::internal::syscall;
using std::tuple; using std::tuple;
using std::make_tuple; using std::make_tuple;
using std::tie; using std::tie;
using std::vector;
// golang::os:: // golang::os::
namespace golang { namespace golang {
...@@ -211,6 +212,38 @@ error _File::Stat(struct stat *st) { ...@@ -211,6 +212,38 @@ error _File::Stat(struct stat *st) {
} }
tuple<string, error> ReadFile(const string& path) {
// errctx is ok as returned by all calls.
File f;
error err;
tie(f, err) = Open(path);
if (err != nil)
return make_tuple("", err);
string data;
vector<char> buf(4096);
while (1) {
int n;
tie(n, err) = f->Read(&buf[0], buf.size());
data.append(&buf[0], n);
if (err != nil) {
if (err == io::EOF_)
err = nil;
break;
}
}
error err2 = f->Close();
if (err == nil)
err = err2;
if (err != nil)
data = "";
return make_tuple(data, err);
}
// pipe // pipe
tuple<File, File, error> Pipe() { tuple<File, File, error> Pipe() {
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
// - `Open` opens file @path. // - `Open` opens file @path.
// - `Pipe` creates new pipe. // - `Pipe` creates new pipe.
// - `NewFile` wraps OS-level file-descriptor into File. // - `NewFile` wraps OS-level file-descriptor into File.
// - `ReadFile` returns content of file @path.
// - `Signal` represents OS-level signal. // - `Signal` represents OS-level signal.
// //
// See also https://golang.org/pkg/os for Go os package documentation. // See also https://golang.org/pkg/os for Go os package documentation.
...@@ -105,6 +106,9 @@ LIBGOLANG_API std::tuple<File, error> NewFile(int sysfd, const string& name); ...@@ -105,6 +106,9 @@ LIBGOLANG_API std::tuple<File, error> NewFile(int sysfd, const string& name);
// Pipe creates connected pair of files. // Pipe creates connected pair of files.
LIBGOLANG_API std::tuple</*r*/File, /*w*/File, error> Pipe(); LIBGOLANG_API std::tuple</*r*/File, /*w*/File, error> Pipe();
// ReadFile returns content of file @path.
LIBGOLANG_API std::tuple<string, error> ReadFile(const string& path);
// Signal represents an OS signal. // Signal represents an OS signal.
// //
......
...@@ -87,6 +87,12 @@ void __test_os_fileio_cpp(const string& tmpd) { ...@@ -87,6 +87,12 @@ void __test_os_fileio_cpp(const string& tmpd) {
err = f->Close(); err = f->Close();
ASSERT(err == nil); ASSERT(err == nil);
// readfile
string data;
tie(data, err) = os::ReadFile(tpath);
ASSERT(err == nil);
ASSERT_EQ(data, "hello world\n");
} }
void _test_os_pipe_cpp() { void _test_os_pipe_cpp() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment