/* ----------------------------------------------------------------------------- GSFramework Copyright 2001-2013 Emmanuel Julien. All Rights Reserved. ----------------------------------------------------------------------------- */ #if __PLATFORM_POSIX__ #include #include #elif __PLATFORM_WINDOWS__ #include #endif #include "filesystem/io_cfile.h" #include "memory/nauto_ptr.h" using GS::String; using namespace GS::IO; //------------------------------------------------------------------------------ void CFile::SetRootPath(const char *_root) { root = _root; } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ uint CFile::GetCaps() const { uint flags = CanRead | CanWrite | CanSeek | CanMkDir; #ifndef __PLATFORM_WINDOWS__ flags |= IsCaseSensitive; #endif return flags; } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ String CFile::MapToAbsolute(const char *uri) const { if (!root.IsEmpty()) return root + "/" + uri; return String(uri); } String CFile::MapToRelative(const char *path) const { String _path(path); if (!root.IsEmpty() && _path.StartsWith(root, GetCaps() & IsCaseSensitive ? String::CaseSensitive : String::CaseInsensitive)) return _path.Mid(root.Len()); return _path; } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ Handle *CFile::Open(const char *path, Mode mode) { String _path(root.IsEmpty() ? path : (root + "/" + path).c_str()), _access(mode == ModeRead ? "rb" : "wb"); AutoPtr h(new CFileHandle(this)); #if __PLATFORM_WINDOWS__ if (h->file = _wfopen((const wchar_t *)_path.toUcs2().c_ptr(), (const wchar_t *)_access.toUcs2().c_ptr())) return h.Detach(); #else if ((h->file = fopen(_path, _access)) != NULL) return h.Detach(); #endif return NULL; } void CFile::Close(Handle *h) { if (CFileHandle *ch = (CFileHandle *)h) if (ch->file) fclose(ch->file); } bool CFile::Delete(const char *uri) { return asbool(unlink(root + "/" + uri) == 0); } size_t CFile::Seek(Handle *h, ptrdiff_t offset, SeekRef ref) { if (CFileHandle *ch = (CFileHandle *)h) if (ch->file) { int c_seek[] = { SEEK_SET, SEEK_CUR, SEEK_END }; return fseek(ch->file, offset, c_seek[ref]); } return (size_t)-1; } size_t CFile::Tell(Handle *h) { if (CFileHandle *ch = (CFileHandle *)h) if (ch->file) return ftell(ch->file); return (size_t)-1; } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ size_t CFile::Read(Handle *h, void *b, size_t size) { if (CFileHandle *ch = (CFileHandle *)h) if (ch->file) return fread(b, 1, size, ch->file); return 0; } size_t CFile::Write(Handle *h, const void *b, size_t size) { if (CFileHandle *ch = (CFileHandle *)h) if (ch->file) return fwrite(b, size, 1, ch->file) * size; return 0; } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ bool CFile::MkDir(const char *path) { String _path(root.IsEmpty() ? path : (root + "/" + path).c_str()); bool r = false; #if __PLATFORM_WINDOWS__ r = _wmkdir((const wchar_t *)_path.toUcs2().c_ptr()) == 0; #else r = mkdir(path, 01777) == 0; #endif return r; } //------------------------------------------------------------------------------ CFile::CFile(const char *root_path) { SetRootPath(root_path); } //------------------------------------------------------------------------------ CFileHandle::CFileHandle(Base *io) : Handle(io) { file = NULL; } CFileHandle::~CFileHandle() { GetIOSystem()->Close(this); } //------------------------------------------------------------------------------