diff options
Diffstat (limited to 'src/filesystem/diskfile.cc')
-rw-r--r-- | src/filesystem/diskfile.cc | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/src/filesystem/diskfile.cc b/src/filesystem/diskfile.cc new file mode 100644 index 0000000..85f6ccc --- /dev/null +++ b/src/filesystem/diskfile.cc @@ -0,0 +1,95 @@ +/* + filesystem/file.c + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#include "filesystem/diskfile.h" + +namespace filesystem { + +DiskFile::DiskFile() +{ + diskfile_handle = 0; +} + +DiskFile::~DiskFile() +{ + if (diskfile_handle) + fclose(diskfile_handle); +} + +bool DiskFile::open(const char *filename) +{ + if (diskfile_handle) { + con_warn << file_name << " already open!" << std::endl; + return false; + } + + file_name.assign(filename); + std::string fn; + + // if moddir is set, try the mods subdir first + if (moddir.size()) { + // try homedir + moddir + file_path = homedir; + file_path.append(moddir); + fn = file_path; + fn.append(filename); + diskfile_handle = fopen(fn.c_str(), "r"); + if (diskfile_handle) + return true; + + // try datadir + moddir + file_path = datadir; + file_path.append(moddir); + fn = file_path; + fn.append(filename); + diskfile_handle = fopen(fn.c_str(), "r"); + if (diskfile_handle) + return true; + } + // try homedir + basedir + file_path = homedir; + file_path.append(basedir); + fn = file_path; + fn.append(filename); + diskfile_handle = fopen(fn.c_str(), "r"); + if (diskfile_handle) + return true; + + // try datadir + basedir + file_path = datadir; + file_path.append(basedir); + fn = file_path; + fn.append(filename); + diskfile_handle = fopen(fn.c_str(), "r"); + if (diskfile_handle) + return true; + + con_warn << "Could not open " << filename << std::endl; + return false; +} + +void DiskFile::close() +{ + if (diskfile_handle) + fclose(diskfile_handle); + diskfile_handle = 0; +} + +size_t DiskFile::read(void *buffer, const size_t count) +{ + if (!diskfile_handle) + return 0; + + return (fread(buffer, count, 1, diskfile_handle)); +} + +void DiskFile::skip(size_t count) +{ + fseek(diskfile_handle, (long) count, SEEK_CUR); +} + +} // namespace filesystem + |