openspades/Sources/Core/DirectoryFileSystem.cpp

209 lines
4.9 KiB
C++
Raw Normal View History

2013-08-29 11:45:22 +09:00
/*
Copyright (c) 2013 yvt
This file is part of OpenSpades.
OpenSpades is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenSpades is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenSpades. If not, see <http://www.gnu.org/licenses/>.
*/
2013-08-18 16:18:06 +09:00
#include "DirectoryFileSystem.h"
//#include "StdStream.h"
#include "SdlFileStream.h"
2013-08-18 16:18:06 +09:00
#include "Exception.h"
#include <sys/stat.h>
#include "Debug.h"
#ifdef WIN32
#include <windows.h>
#include <io.h>
#ifdef _MSC_VER
#include <direct.h>
#define mkdir _mkdir
#endif
2013-08-18 16:18:06 +09:00
#else
#include <dirent.h>
#endif
namespace spades {
DirectoryFileSystem::DirectoryFileSystem(const std::string& r, bool canWrite):
rootPath(r), canWrite(canWrite) {
SPADES_MARK_FUNCTION();
2013-08-26 01:27:44 +09:00
SPLog("Directory File System Initialized: %s (%s)",
r.c_str(), canWrite ? "Read/Write" : "Read-only");
2013-08-18 16:18:06 +09:00
}
DirectoryFileSystem::~DirectoryFileSystem(){
SPADES_MARK_FUNCTION();
}
std::string DirectoryFileSystem::physicalPath(const std::string &lg) {
// TODO: check ".."?
return rootPath + '/' + lg;
}
2014-04-04 16:46:01 +09:00
#ifdef WIN32
static std::wstring Utf8ToWString(const char *s) {
auto *ws = (WCHAR*)SDL_iconv_string("UCS-2-INTERNAL", "UTF-8", (char *)(s), SDL_strlen(s)+1);
if(!ws) return L"";
std::wstring wss(ws);
SDL_free(ws);
return wss;
}
static std::string Utf8FromWString(const wchar_t *ws) {
auto *s = (char*)SDL_iconv_string("UTF-8", "UCS-2-INTERNAL", (char *)(ws), wcslen(ws)*2+2);
if(!s) return "";
std::string ss(s);
SDL_free(s);
return ss;
}
#endif
2013-08-18 16:18:06 +09:00
std::vector<std::string> DirectoryFileSystem::EnumFiles(const char *p){
2014-04-04 16:46:01 +09:00
SPADES_MARK_FUNCTION();
2013-08-18 16:18:06 +09:00
#ifdef WIN32
2014-04-04 16:46:01 +09:00
WIN32_FIND_DATAW fd;
2013-08-18 16:18:06 +09:00
HANDLE h;
std::vector<std::string> ret;
2014-04-04 16:46:01 +09:00
std::wstring filePath;
2013-08-18 16:18:06 +09:00
2014-04-04 16:46:01 +09:00
std::wstring path = Utf8ToWString(physicalPath(p).c_str());
2013-08-18 16:18:06 +09:00
// open the Win32 find handle.
2014-04-04 16:46:01 +09:00
h=FindFirstFileExW((path+L"\\*").c_str(),
2013-08-18 16:18:06 +09:00
FindExInfoStandard, &fd,
FindExSearchNameMatch,
NULL, 0);
if(h==INVALID_HANDLE_VALUE){
// couldn't open. return the empty vector.
return ret;
}
do{
// is it a directory?
if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
// "." and ".." mustn't be included.
2014-04-06 21:56:07 +09:00
if(wcscmp(fd.cFileName, L".") && wcscmp(fd.cFileName, L"..")){
2013-08-18 16:18:06 +09:00
filePath=fd.cFileName;
2014-04-04 16:46:01 +09:00
ret.push_back(Utf8FromWString(filePath.c_str()));
2013-08-18 16:18:06 +09:00
}
}else{
// usual file.
filePath=fd.cFileName;
2014-04-04 16:46:01 +09:00
ret.push_back(Utf8FromWString(filePath.c_str()));
2013-08-18 16:18:06 +09:00
}
// iterate!
2014-04-06 21:56:07 +09:00
}while(FindNextFileW(h, &fd));
2013-08-18 16:18:06 +09:00
// close the handle.
FindClose(h);
return ret;
#else
// open the directory.
std::string path = physicalPath(p);
DIR *dir=opendir(path.c_str());
struct dirent *ent;
std::vector<std::string> ret;
std::string filePath;
// if couldn't open the directory, return the empty vector.
if(!dir)
return ret;
// read an entry.
while((ent=readdir(dir))){
if(ent->d_name[0]=='.')
continue;
// make it full-path.
filePath=ent->d_name;
// add to the result vector.
ret.push_back(filePath);
}
// close the directory.
closedir(dir);
return ret;
#endif
}
IStream *DirectoryFileSystem::OpenForReading(const char *fn){
SPADES_MARK_FUNCTION();
std::string path = physicalPath(fn);
SDL_RWops *f = SDL_RWFromFile(path.c_str(), "rb");
2013-08-18 16:18:06 +09:00
if(f == NULL) {
2014-04-04 16:46:01 +09:00
SPRaise("I/O error while opening %s for reading: %s", fn, SDL_GetError());
2013-08-18 16:18:06 +09:00
}
return new SdlFileStream(f, true);
2013-08-18 16:18:06 +09:00
}
IStream *DirectoryFileSystem::OpenForWriting(const char *fn) {
SPADES_MARK_FUNCTION();
if(!canWrite){
SPRaise("Writing prohibited for root path '%s'", rootPath.c_str());
}
std::string path = physicalPath(fn);
// create required directory
if(path.find_first_of("/\\") != std::string::npos){
size_t pos = path.find_first_of("/\\") + 1;
2013-08-18 16:18:06 +09:00
while(pos < path.size()){
size_t nextPos = pos;
while(nextPos < path.size() &&
path[nextPos] != '/' &&
path[nextPos] != '\\')
2013-08-18 16:18:06 +09:00
nextPos++;
if(nextPos == path.size())
break;
#ifdef WIN32
mkdir(path.substr(0, nextPos).c_str());
#else
mkdir(path.substr(0, nextPos).c_str(), 0774);
#endif
pos = nextPos + 1;
}
}
SDL_RWops *f = SDL_RWFromFile(path.c_str(), "wb+");
2013-08-18 16:18:06 +09:00
if(f == NULL) {
SPRaise("I/O error while opening %s for writing", fn);
}
return new SdlFileStream(f, true);
2013-08-18 16:18:06 +09:00
}
// TODO: open for appending?
2013-08-18 16:18:06 +09:00
bool DirectoryFileSystem::FileExists(const char *fn) {
SPADES_MARK_FUNCTION();
std::string path = physicalPath(fn);
2014-04-04 16:46:01 +09:00
SDL_RWops *f = SDL_RWFromFile(path.c_str(), "rb");
if(f) {
SDL_RWclose(f);
2013-08-18 16:18:06 +09:00
return true;
}
2014-04-04 16:46:01 +09:00
return false;
2013-08-18 16:18:06 +09:00
}
}