From f8e789be8a673d567aed9076df33b378b5021caa Mon Sep 17 00:00:00 2001 From: Genoil Date: Fri, 3 Apr 2015 07:45:11 +0200 Subject: [PATCH] added function to read from disk into (OpenCL) buffer --- libdevcore/CommonIO.cpp | 17 +++++++++++++++++ libdevcore/CommonIO.h | 4 ++++ 2 files changed, 21 insertions(+) diff --git a/libdevcore/CommonIO.cpp b/libdevcore/CommonIO.cpp index 46d6e3a6b..12a35c6cb 100644 --- a/libdevcore/CommonIO.cpp +++ b/libdevcore/CommonIO.cpp @@ -75,6 +75,23 @@ bytesRef dev::contentsNew(std::string const& _file) return ret; } +// Don't forget to delete[] later. +uint64_t dev::contentsToBuffer(std::string const& _file, void * buf) +{ + std::ifstream is(_file, std::ifstream::binary); + if (!is) + return 0; + // get length of file: + is.seekg(0, is.end); + streamoff length = is.tellg(); + if (length == 0) // return early, MSVC does not like reading 0 bytes + return 0; + is.seekg(0, is.beg); + is.read((char*)buf, length); + is.close(); + return (uint64_t)length; +} + bytes dev::contents(std::string const& _file) { std::ifstream is(_file, std::ifstream::binary); diff --git a/libdevcore/CommonIO.h b/libdevcore/CommonIO.h index f42f449bb..d3f92d3d2 100644 --- a/libdevcore/CommonIO.h +++ b/libdevcore/CommonIO.h @@ -48,6 +48,10 @@ std::string contentsString(std::string const& _file); /// Retrieve and returns the allocated contents of the given file. If the file doesn't exist or isn't readable, returns nullptr. Don't forget to delete [] when finished. bytesRef contentsNew(std::string const& _file); +/// Retrieve and returns the allocated contents of the given file and load into buffer. Used to fill mapped OpenCL buffer. +uint64_t contentsToBuffer(std::string const& _file, void * buf); + + /// Write the given binary data into the given file, replacing the file if it pre-exists. void writeFile(std::string const& _file, bytesConstRef _data); /// Write the given binary data into the given file, replacing the file if it pre-exists.