| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- #include "common/FileSystem.h"
- #include "core/Logger.h"
- #include <stdio.h>
- unsigned int
- FileSystem::GetFileSize(const Ogre::String &path)
- {
- FILE* file = fopen(path.c_str(), "rb");
- if (file == NULL)
- {
- LOG_ERROR("Can't open file " + path + ".\n");
- return 0;
- }
- // set cursor to end of file
- fseek(file, 0, SEEK_END);
- unsigned int size = ftell(file);
- fclose(file);
- return size;
- }
- bool
- FileSystem::ReadFile(const Ogre::String &path, void* buffer, const unsigned int start, const unsigned int length)
- {
- FILE* file = fopen(path.c_str(), "rb");
- if (file == NULL)
- {
- LOG_ERROR("Can't open file " + path + ".\n");
- return false;
- }
- fseek(file, start, SEEK_SET);
- const auto ret = fread(buffer, sizeof(char), length, file);
- fclose(file);
- if (ret != sizeof(char))
- {
- LOG_ERROR("Failed to read all data\n");
- return false;
- }
- return true;
- }
- bool
- FileSystem::WriteFile(const Ogre::String &path, const void* buffer, const unsigned int length)
- {
- FILE* file = fopen(path.c_str(), "ab");
- if (file == NULL)
- {
- return false;
- }
- fwrite(buffer, sizeof(char), length, file);
- fclose(file);
- return true;
- }
- bool
- FileSystem::WriteNewFile(const Ogre::String &path, const void* buffer, unsigned int length)
- {
- RemoveFile(path);
- return !!(WriteFile(path, buffer, length));
- }
- bool
- FileSystem::RemoveFile(const Ogre::String &path)
- {
- return (remove(path.c_str()) == 0);
- }
|