moved basic file I/O from config.cpp to filesystem.cpp
This commit is contained in:
parent
f67c2da8bf
commit
62d192562c
5 changed files with 107 additions and 104 deletions
|
@ -28,7 +28,6 @@
|
|||
#include "game_events.hpp"
|
||||
#include "gettext.hpp"
|
||||
#include "log.hpp"
|
||||
#include "scoped_resource.hpp"
|
||||
#include "util.hpp"
|
||||
#include "wesconfig.h"
|
||||
|
||||
|
@ -75,108 +74,12 @@ line_source get_line_source(const std::vector<line_source>& line_src, int line)
|
|||
return res;
|
||||
}
|
||||
|
||||
void read_file_internal(const std::string& fname, std::string& res)
|
||||
{
|
||||
const int size = file_size(fname);
|
||||
if(size < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<char> v;
|
||||
v.reserve(size);
|
||||
|
||||
//const util::scoped_resource<FILE*,close_FILE> file(fopen(fname.c_str(),"rb"));
|
||||
const util::scoped_FILE file(fopen(fname.c_str(),"rb"));
|
||||
|
||||
if(file == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
const int block_size = 65536;
|
||||
|
||||
while(v.size() < size) {
|
||||
const size_t expected = minimum<size_t>(block_size,size - v.size());
|
||||
|
||||
if(expected > 0) {
|
||||
v.resize(v.size() + expected);
|
||||
const size_t nbytes = fread(&v[v.size() - expected],1,expected,file);
|
||||
if(nbytes < expected) {
|
||||
v.resize(v.size() - (expected - nbytes));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
res.resize(v.size());
|
||||
std::copy(v.begin(),v.end(),res.begin());
|
||||
}
|
||||
|
||||
} //end anon namespace
|
||||
|
||||
std::string read_stdin()
|
||||
{
|
||||
std::vector<char> v;
|
||||
const int block_size = 65536;
|
||||
|
||||
size_t nbytes = 1;
|
||||
while(nbytes > 0) {
|
||||
v.resize(v.size() + block_size);
|
||||
nbytes = fread(&v[v.size() - block_size],1,block_size,stdin);
|
||||
if(nbytes < block_size) {
|
||||
v.resize(v.size() - (block_size - nbytes));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
std::string res;
|
||||
res.resize(v.size());
|
||||
std::copy(v.begin(),v.end(),res.begin());
|
||||
return res;
|
||||
}
|
||||
|
||||
const char* io_exception::what() const throw() {
|
||||
return message.c_str();
|
||||
}
|
||||
|
||||
std::string read_file(const std::string& fname)
|
||||
{
|
||||
//if we have a path to the data,
|
||||
//convert any filepath which is relative
|
||||
if(!fname.empty() && fname[0] != '/' && !game_config::path.empty()) {
|
||||
std::string res;
|
||||
read_file_internal(game_config::path + "/" + fname,res);
|
||||
if(!res.empty()) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
std::string res;
|
||||
read_file_internal(fname,res);
|
||||
return res;
|
||||
}
|
||||
|
||||
//throws io_exception if an error occurs
|
||||
void write_file(const std::string& fname, const std::string& data)
|
||||
{
|
||||
//const util::scoped_resource<FILE*,close_FILE> file(fopen(fname.c_str(),"wb"));
|
||||
const util::scoped_FILE file(fopen(fname.c_str(),"wb"));
|
||||
if(file.get() == NULL) {
|
||||
throw io_exception("Could not open file for writing: '" + fname + "'");
|
||||
}
|
||||
|
||||
const size_t block_size = 4096;
|
||||
char buf[block_size];
|
||||
|
||||
for(size_t i = 0; i < data.size(); i += block_size) {
|
||||
const size_t bytes = minimum<size_t>(block_size,data.size() - i);
|
||||
std::copy(data.begin() + i, data.begin() + i + bytes,buf);
|
||||
const size_t res = fwrite(buf,1,bytes,file.get());
|
||||
if(res != bytes) {
|
||||
throw io_exception("Error writing to file: '" + fname + "'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
//this function takes a macro and parses it into the macro followed by its
|
||||
|
|
|
@ -53,12 +53,6 @@ private:
|
|||
std::string message;
|
||||
};
|
||||
|
||||
//basic disk I/O
|
||||
std::string read_file(const std::string& fname);
|
||||
//throws io_exception if an error occurs
|
||||
void write_file(const std::string& fname, const std::string& data);
|
||||
std::string read_stdin();
|
||||
|
||||
struct preproc_define {
|
||||
preproc_define() {}
|
||||
explicit preproc_define(const std::string& val) : value(val) {}
|
||||
|
|
|
@ -59,6 +59,7 @@ BPath be_path;
|
|||
#include "config.hpp"
|
||||
#include "filesystem.hpp"
|
||||
#include "game_config.hpp"
|
||||
#include "scoped_resource.hpp"
|
||||
#include "util.hpp"
|
||||
|
||||
namespace {
|
||||
|
@ -327,6 +328,105 @@ std::string get_user_data_dir()
|
|||
#endif
|
||||
}
|
||||
|
||||
namespace {
|
||||
void read_file_internal(const std::string& fname, std::string& res)
|
||||
{
|
||||
const int size = file_size(fname);
|
||||
if(size < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<char> v;
|
||||
v.reserve(size);
|
||||
|
||||
//const util::scoped_resource<FILE*,close_FILE> file(fopen(fname.c_str(),"rb"));
|
||||
const util::scoped_FILE file(fopen(fname.c_str(),"rb"));
|
||||
|
||||
if(file == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
const int block_size = 65536;
|
||||
|
||||
while(v.size() < size) {
|
||||
const size_t expected = minimum<size_t>(block_size,size - v.size());
|
||||
|
||||
if(expected > 0) {
|
||||
v.resize(v.size() + expected);
|
||||
const size_t nbytes = fread(&v[v.size() - expected],1,expected,file);
|
||||
if(nbytes < expected) {
|
||||
v.resize(v.size() - (expected - nbytes));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
res.resize(v.size());
|
||||
std::copy(v.begin(),v.end(),res.begin());
|
||||
}
|
||||
} //end anon namespace
|
||||
|
||||
std::string read_stdin()
|
||||
{
|
||||
std::vector<char> v;
|
||||
const int block_size = 65536;
|
||||
|
||||
size_t nbytes = 1;
|
||||
while(nbytes > 0) {
|
||||
v.resize(v.size() + block_size);
|
||||
nbytes = fread(&v[v.size() - block_size],1,block_size,stdin);
|
||||
if(nbytes < block_size) {
|
||||
v.resize(v.size() - (block_size - nbytes));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
std::string res;
|
||||
res.resize(v.size());
|
||||
std::copy(v.begin(),v.end(),res.begin());
|
||||
return res;
|
||||
}
|
||||
|
||||
std::string read_file(const std::string& fname)
|
||||
{
|
||||
//if we have a path to the data,
|
||||
//convert any filepath which is relative
|
||||
if(!fname.empty() && fname[0] != '/' && !game_config::path.empty()) {
|
||||
std::string res;
|
||||
read_file_internal(game_config::path + "/" + fname,res);
|
||||
if(!res.empty()) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
std::string res;
|
||||
read_file_internal(fname,res);
|
||||
return res;
|
||||
}
|
||||
|
||||
//throws io_exception if an error occurs
|
||||
void write_file(const std::string& fname, const std::string& data)
|
||||
{
|
||||
//const util::scoped_resource<FILE*,close_FILE> file(fopen(fname.c_str(),"wb"));
|
||||
const util::scoped_FILE file(fopen(fname.c_str(),"wb"));
|
||||
if(file.get() == NULL) {
|
||||
throw io_exception("Could not open file for writing: '" + fname + "'");
|
||||
}
|
||||
|
||||
const size_t block_size = 4096;
|
||||
char buf[block_size];
|
||||
|
||||
for(size_t i = 0; i < data.size(); i += block_size) {
|
||||
const size_t bytes = minimum<size_t>(block_size,data.size() - i);
|
||||
std::copy(data.begin() + i, data.begin() + i + bytes,buf);
|
||||
const size_t res = fwrite(buf,1,bytes,file.get());
|
||||
if(res != bytes) {
|
||||
throw io_exception("Error writing to file: '" + fname + "'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::string read_map(const std::string& name)
|
||||
{
|
||||
std::string res = read_file("data/maps/" + name);
|
||||
|
|
|
@ -44,6 +44,12 @@ std::string get_cwd();
|
|||
|
||||
void make_directory(const std::string& dirname);
|
||||
|
||||
//basic disk I/O
|
||||
std::string read_file(const std::string& fname);
|
||||
//throws io_exception if an error occurs
|
||||
void write_file(const std::string& fname, const std::string& data);
|
||||
std::string read_stdin();
|
||||
|
||||
std::string read_map(const std::string& name);
|
||||
|
||||
//function which returns true iff the given file is a directory
|
||||
|
|
|
@ -13,8 +13,8 @@
|
|||
|
||||
#include "global.hpp"
|
||||
|
||||
#include "config.hpp"
|
||||
#include "font.hpp"
|
||||
#include "filesystem.hpp"
|
||||
#include "game_config.hpp"
|
||||
#include "language.hpp"
|
||||
#include "log.hpp"
|
||||
|
|
Loading…
Add table
Reference in a new issue