/* 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(), "rb"); 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(), "rb"); 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(), "rb"); 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(), "rb"); if (diskfile_handle) return true; //con_error << "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