Browse Source

added function to read from disk into (OpenCL) buffer

cl-refactor
Genoil 10 years ago
parent
commit
f8e789be8a
  1. 17
      libdevcore/CommonIO.cpp
  2. 4
      libdevcore/CommonIO.h

17
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);

4
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.

Loading…
Cancel
Save