mirror of
https://github.com/matrix-construct/construct
synced 2024-11-29 10:12:39 +01:00
ircd::fs: Improve basic file read/write/append into buffers.
This commit is contained in:
parent
0eb10c0495
commit
4954c90e27
2 changed files with 44 additions and 0 deletions
|
@ -95,4 +95,8 @@ namespace ircd::fs
|
|||
bool mkdir(const std::string &path);
|
||||
|
||||
std::string read(const std::string &name);
|
||||
string_view read(const std::string &name, const mutable_raw_buffer &buf);
|
||||
bool write(const std::string &name, const const_raw_buffer &buf);
|
||||
bool append(const std::string &name, const const_raw_buffer &buf);
|
||||
bool overwrite(const std::string &name, const const_raw_buffer &buf);
|
||||
}
|
||||
|
|
40
ircd/fs.cc
40
ircd/fs.cc
|
@ -60,6 +60,34 @@ std::array<ent, num_of<index>()> paths
|
|||
} // namespace fs
|
||||
} // namespace ircd
|
||||
|
||||
bool
|
||||
ircd::fs::write(const std::string &path,
|
||||
const const_raw_buffer &buf)
|
||||
{
|
||||
if(fs::exists(path))
|
||||
return false;
|
||||
|
||||
return overwrite(path, buf);
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::fs::overwrite(const std::string &path,
|
||||
const const_raw_buffer &buf)
|
||||
{
|
||||
std::ofstream file{path, std::ios::trunc};
|
||||
file.write(reinterpret_cast<const char *>(data(buf)), size(buf));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::fs::append(const std::string &path,
|
||||
const const_raw_buffer &buf)
|
||||
{
|
||||
std::ofstream file{path, std::ios::app};
|
||||
file.write(reinterpret_cast<const char *>(data(buf)), size(buf));
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string
|
||||
ircd::fs::read(const std::string &path)
|
||||
{
|
||||
|
@ -70,6 +98,18 @@ ircd::fs::read(const std::string &path)
|
|||
return std::string{b, e};
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::fs::read(const std::string &path,
|
||||
const mutable_raw_buffer &buf)
|
||||
{
|
||||
std::ifstream file{path};
|
||||
if(!file.good())
|
||||
return {};
|
||||
|
||||
file.read(reinterpret_cast<char *>(data(buf)), size(buf));
|
||||
return { reinterpret_cast<const char *>(data(buf)), file.gcount() };
|
||||
}
|
||||
|
||||
void
|
||||
ircd::fs::chdir(const std::string &path)
|
||||
try
|
||||
|
|
Loading…
Reference in a new issue