docs for FileIO, a very enjoyable namespace

This commit is contained in:
apio 2022-06-08 20:19:01 +02:00
parent 07c58aab41
commit 9ccb5e1a6b
2 changed files with 4 additions and 1 deletions

View File

@ -8,6 +8,7 @@
std::string FileIO::read_all(const std::string& filename) std::string FileIO::read_all(const std::string& filename)
{ {
if(std::filesystem::is_directory(std::filesystem::status(filename))) Error::throw_error_without_location("unable to open file "+ filename + ": Is a directory");
std::ifstream file; std::ifstream file;
file.exceptions(std::ios::badbit | std::ios::failbit); file.exceptions(std::ios::badbit | std::ios::failbit);
try try
@ -19,7 +20,6 @@ std::string FileIO::read_all(const std::string& filename)
Error::throw_error_without_location("unable to open file "+ filename + ": " + strerror(errno)); Error::throw_error_without_location("unable to open file "+ filename + ": " + strerror(errno));
return ""; return "";
} }
if(std::filesystem::is_directory(std::filesystem::status(filename))) Error::throw_error_without_location("unable to open file "+ filename + ": Is a directory");
file.exceptions(std::ios::goodbit); file.exceptions(std::ios::goodbit);
std::vector<char> file_chars; std::vector<char> file_chars;
char fchar; char fchar;

View File

@ -1,8 +1,11 @@
#pragma once #pragma once
#include <string> #include <string>
/* Namespace for simple file operations. */
namespace FileIO // namespaces are just so neat namespace FileIO // namespaces are just so neat
{ {
/* Helper function to read all of a file's contents. */
std::string read_all(const std::string& filename); std::string read_all(const std::string& filename);
/* Helper function to write a string to a file. */
void write_all(const std::string& filename, const std::string& contents); void write_all(const std::string& filename, const std::string& contents);
} }