diff options
Diffstat (limited to 'src/filesystem')
-rw-r--r-- | src/filesystem/filestream.cc | 85 | ||||
-rw-r--r-- | src/filesystem/filestream.h | 36 |
2 files changed, 87 insertions, 34 deletions
diff --git a/src/filesystem/filestream.cc b/src/filesystem/filestream.cc index 53c72d8..8218a48 100644 --- a/src/filesystem/filestream.cc +++ b/src/filesystem/filestream.cc @@ -10,50 +10,56 @@ namespace filesystem { +/* ---- class IFileStream ------------------------------------------ */ + IFileStream::IFileStream(const char *name) : std::ifstream() { - if (name) - fstream_name.assign(name); - - open(name); + if (name && name[0]) { + open(name); + } } -IFileStream::IFileStream(const std::string &name) : std::ifstream(), fstream_name(name) +IFileStream::IFileStream(const std::string &name) : std::ifstream(), ifstream_name(name) { - open(fstream_name); + if (name.size()) { + open(ifstream_name); + } } void IFileStream::open(const char *name) { - if (!name) { + if (!name || !name[0]) { if (is_open()) { close(); } - fstream_name.clear(); - fstream_filename.clear(); + ifstream_name.clear(); + ifstream_filename.clear(); return; } - fstream_name.assign(name); - + ifstream_name.assign(name); + for (SearchPath::iterator path = searchpath().begin(); path != searchpath().end(); path++) { - fstream_filename.assign((*path)); - fstream_filename.append(fstream_name); + ifstream_filename.assign((*path)); + ifstream_filename.append(ifstream_name); /* #ifdef _WIN32 for (size_t i = 0; i < fstream_filename.size(); i++) if (fstream_filename[i] == '/') fstream_filename[i] = '\\'; #endif */ - - if (sys::file_exists(fstream_filename)) { - std::ifstream::open(fstream_filename.c_str()); - if (good()) + if (sys::file_exists(ifstream_filename)) { + std::ifstream::open(ifstream_filename.c_str()); + if (good()) { return; + } else { + con_warn << "Could not read file " << filename() << std::endl; + } + } } - fstream_filename.clear(); + ifstream_filename.clear(); } void IFileStream::open(const std::string &name) @@ -61,4 +67,47 @@ void IFileStream::open(const std::string &name) open(name.c_str()); } +/* ---- class OFileStream ------------------------------------------ */ + +OFileStream::OFileStream(const char *name) : std::ofstream() +{ + if (name && name[0]) { + open(name); + } +} + +OFileStream::OFileStream(const std::string &name) : std::ofstream(), ofstream_name(name) +{ + if (name.size()) { + open(ofstream_name); + } +} + +void OFileStream::open(const char *name) +{ + if (!name || !name[0]) { + if (is_open()) { + close(); + } + ofstream_name.clear(); + ofstream_filename.clear(); + return; + } + + ofstream_name.assign(name); + + ofstream_filename.assign(writedir()); + ofstream_filename.append(ofstream_name); + std::ofstream::open(ofstream_filename.c_str()); + + if (!good()) { + ofstream_filename.clear(); + } +} + +void OFileStream::open(const std::string &name) +{ + open(name.c_str()); +} + } diff --git a/src/filesystem/filestream.h b/src/filesystem/filestream.h index ceac6c5..eea2cf9 100644 --- a/src/filesystem/filestream.h +++ b/src/filesystem/filestream.h @@ -28,45 +28,49 @@ public: /// name of the file in the virtual filesystem inline const std::string &name() const { - return fstream_name; + return ifstream_name; } - /// actual dilename + /// name of the file in the real filesystem inline const std::string &filename() const { - return fstream_filename; + return ifstream_filename; } private: - std::string fstream_name; - std::string fstream_filename; + std::string ifstream_name; + std::string ifstream_filename; }; /** * @brief output stream for files in the home directory */ -/* class OFileStream : public std::ofstream { public: - IFileStream(const char *filename); + OFileStream(const char name = 0); + + OFileStream(const char *name); - IFileStream(const std::string &filename); + OFileStream(const std::string &name); - void open(const char *filename); + void open(const char *name); - void open(const std::string &filename); + void open(const std::string &name); /// name of the file in the virtual filesystem - inline const std::string &name() { return fstream_name; } + inline const std::string &name() { + return ofstream_name; + } - /// system filename - inline const std::string &filename() { return fstream_filename; } + /// name of the file in the real filesystem + inline const std::string &filename() { + return ofstream_filename; + } private: - std::string fstream_name; - std::string fstream_filename; + std::string ofstream_name; + std::string ofstream_filename; }; -*/ } // namespace filesystem |