| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- /*
- Copyright (c) 2013 Daniele Bartolini, Michele Rossi
- Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
- Permission is hereby granted, free of charge, to any person
- obtaining a copy of this software and associated documentation
- files (the "Software"), to deal in the Software without
- restriction, including without limitation the rights to use,
- copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the
- Software is furnished to do so, subject to the following
- conditions:
- The above copyright notice and this permission notice shall be
- included in all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- OTHER DEALINGS IN THE SOFTWARE.
- */
- #include "Filesystem.h"
- #include "Log.h"
- #include "OS.h"
- #include "DiskFile.h"
- #include "Memory.h"
- namespace crown
- {
- //-----------------------------------------------------------------------------
- Filesystem::Filesystem(const char* root_path)
- {
- CE_ASSERT(root_path != NULL, "Root path must be != NULL");
- CE_ASSERT(os::is_absolute_path(root_path), "Root path must be absolute");
- string::strncpy(m_root_path, root_path, MAX_PATH_LENGTH);
- }
- //-----------------------------------------------------------------------------
- Filesystem::~Filesystem()
- {
- }
- //-----------------------------------------------------------------------------
- const char* Filesystem::root_path() const
- {
- return m_root_path;
- }
- //-----------------------------------------------------------------------------
- const char* Filesystem::build_os_path(const char* base_path, const char* relative_path)
- {
- static char os_path[MAX_PATH_LENGTH];
- string::strncpy(os_path, base_path, MAX_PATH_LENGTH);
- size_t base_path_len = string::strlen(base_path);
- os_path[base_path_len] = PATH_SEPARATOR;
- os_path[base_path_len + 1] = '\0';
- string::strcat(os_path, relative_path);
- // FIXME FIXME FIXME Replace Crown-specific path separator with OS-speficic one
- for (size_t j = 0; j < string::strlen(os_path); j++)
- {
- if (os_path[j] == '/')
- {
- os_path[j] = PATH_SEPARATOR;
- }
- }
- return os_path;
- }
- //-----------------------------------------------------------------------------
- bool Filesystem::get_info(const char* relative_path, FilesystemEntry& info)
- {
- // Entering OS-DEPENDENT-PATH-MODE
- // (i.e. os_path is of the form: C:\foo\relative_path or /foo/relative_path)
- const char* os_path = build_os_path(m_root_path, relative_path);
-
- string::strncpy(info.os_path, os_path, MAX_PATH_LENGTH);
- string::strncpy(info.relative_path, relative_path, MAX_PATH_LENGTH);
- if (os::is_reg(os_path))
- {
- info.type = FilesystemEntry::FILE;
- return true;
- }
- else if (os::is_dir(os_path))
- {
- info.type = FilesystemEntry::DIRECTORY;
- return true;
- }
-
- info.type = FilesystemEntry::UNKNOWN;
- return false;
- }
- //-----------------------------------------------------------------------------
- bool Filesystem::exists(const char* relative_path)
- {
- FilesystemEntry dummy;
- return get_info(relative_path, dummy);
- }
- //-----------------------------------------------------------------------------
- bool Filesystem::is_file(const char* relative_path)
- {
- FilesystemEntry info;
- if (get_info(relative_path, info))
- {
- return info.type == FilesystemEntry::FILE;
- }
- return false;
- }
- //-----------------------------------------------------------------------------
- bool Filesystem::is_dir(const char* relative_path)
- {
- FilesystemEntry info;
- if (get_info(relative_path, info))
- {
- return info.type == FilesystemEntry::DIRECTORY;
- }
- return false;
- }
- //-----------------------------------------------------------------------------
- bool Filesystem::create_file(const char* relative_path)
- {
- const char* os_path = build_os_path(m_root_path, relative_path);
- return os::mknod(os_path);
- }
- //-----------------------------------------------------------------------------
- bool Filesystem::create_dir(const char* relative_path)
- {
- const char* os_path = build_os_path(m_root_path, relative_path);
- return os::mkdir(os_path);
- }
- //-----------------------------------------------------------------------------
- bool Filesystem::delete_file(const char* relative_path)
- {
- const char* os_path = build_os_path(m_root_path, relative_path);
- return os::unlink(os_path);
- }
- //-----------------------------------------------------------------------------
- bool Filesystem::delete_dir(const char* relative_path)
- {
- const char* os_path = build_os_path(m_root_path, relative_path);
- return os::rmdir(os_path);
- }
- //-----------------------------------------------------------------------------
- const char* Filesystem::os_path(const char* relative_path)
- {
- static char os_path[MAX_PATH_LENGTH];
- FilesystemEntry entry;
- get_info(relative_path, entry);
- string::strncpy(os_path, entry.os_path, MAX_PATH_LENGTH);
- return os_path;
- }
- //-----------------------------------------------------------------------------
- DiskFile* Filesystem::open(const char* relative_path, FileOpenMode mode)
- {
- CE_ASSERT(exists(relative_path), "File does not exist: %s", relative_path);
- CE_ASSERT(is_file(relative_path), "File is not a regular file: %s", relative_path);
- return CE_NEW(m_allocator, DiskFile)(mode, os_path(relative_path));
- }
- //-----------------------------------------------------------------------------
- void Filesystem::close(DiskFile* stream)
- {
- CE_DELETE(m_allocator, stream);
- }
- } // namespace crown
|