cpp_photon/src/util/filesys/filesys.cpp

173 lines
3.3 KiB
C++
Raw Normal View History

2005-02-06 21:30:09 +00:00
//This file is part of Photon (http://photon.sourceforge.net)
//Copyright (C) 2004-2005 James Turk
//
// Author:
// James Turk (jpt2433@rit.edu)
//
// Version:
2005-02-16 06:58:05 +00:00
// $Id: filesys.cpp,v 1.4 2005/02/16 06:58:26 cozman Exp $
2005-02-06 21:30:09 +00:00
2005-02-13 22:12:02 +00:00
#include "util/filesys/filesys.hpp"
2005-02-06 21:30:09 +00:00
namespace photon
{
namespace util
{
namespace filesys
{
std::vector<std::string> getCDDirs()
{
std::vector<std::string> dirs;
char** buf( PHYSFS_getCdRomDirs() );
if(buf == 0)
{
throw APIError(std::string("getCDDirs failed (") +
PHYSFS_getLastError() + ")");
}
//iterate over list, adding dirs
for(char** i(buf); *i != 0; ++i)
{
dirs.push_back(*i);
}
PHYSFS_freeList(buf); //free the memory
return dirs;
}
std::string getBaseDir()
{
return PHYSFS_getBaseDir();
}
std::string getUserDir()
{
return PHYSFS_getUserDir();
}
2005-02-07 01:48:50 +00:00
void addToSearchPath(const std::string& dir, bool append)
2005-02-06 21:30:09 +00:00
{
2005-02-07 01:48:50 +00:00
int success = PHYSFS_addToSearchPath(dir.c_str(), append);
if(!success)
2005-02-06 21:30:09 +00:00
{
2005-02-07 01:48:50 +00:00
throw APIError(std::string("addToSearchPath failed (") +
PHYSFS_getLastError() + ")");
2005-02-06 21:30:09 +00:00
}
}
2005-02-07 01:48:50 +00:00
void removeFromSearchPath(const std::string& dir)
2005-02-06 21:30:09 +00:00
{
//ignore return value (useless)
PHYSFS_removeFromSearchPath(dir.c_str());
}
std::vector<std::string> getSearchPath()
{
std::vector<std::string> dirs;
char** buf( PHYSFS_getSearchPath() );
if(buf == 0)
{
throw APIError(std::string("getSearchPath failed (") +
PHYSFS_getLastError() + ")");
}
//iterate over list, adding dirs
for(char** i(buf); *i != 0; ++i)
{
dirs.push_back(*i);
}
PHYSFS_freeList(buf); //free the memory
return dirs;
}
2005-02-07 01:48:50 +00:00
void setWriteDir(const std::string& dir)
{
//set write dir to either NULL (disabled) or the directory passed in
if(PHYSFS_setWriteDir( dir.empty() ? 0 : dir.c_str() ) == 0)
{
throw APIError(std::string("setWriteDir failed (") +
PHYSFS_getLastError() + ")");
}
}
std::string getWriteDir()
{
const char* dir = PHYSFS_getWriteDir();
//return name of directory or empty string if dir is null
return dir != 0 ? dir : std::string();
}
bool mkdir(const std::string& dir)
2005-02-06 21:30:09 +00:00
{
return PHYSFS_mkdir(dir.c_str()) != 0;
}
2005-02-07 01:48:50 +00:00
bool remove(const std::string& item)
2005-02-06 21:30:09 +00:00
{
return PHYSFS_delete(item.c_str()) != 0;
}
2005-02-07 01:48:50 +00:00
std::vector<std::string> listDir(const std::string& dir)
2005-02-06 21:30:09 +00:00
{
std::vector<std::string> files;
char** buf( PHYSFS_enumerateFiles(dir.c_str()) );
if(buf == 0)
{
throw APIError(std::string("listDir failed (") +
PHYSFS_getLastError() + ")");
}
//iterate over list, adding dirs
for(char** i(buf); *i != 0; ++i)
{
files.push_back(*i);
}
PHYSFS_freeList(buf); //free the memory
return files;
}
2005-02-07 01:48:50 +00:00
bool exists(const std::string& item)
2005-02-06 21:30:09 +00:00
{
return PHYSFS_exists(item.c_str()) != 0;
}
2005-02-07 01:48:50 +00:00
bool isDir(const std::string& item)
2005-02-06 21:30:09 +00:00
{
return PHYSFS_isDirectory(item.c_str()) != 0;
}
2005-02-07 01:48:50 +00:00
bool isSymbolicLink(const std::string& item)
2005-02-06 21:30:09 +00:00
{
return PHYSFS_isSymbolicLink(item.c_str()) != 0;
}
2005-02-07 01:48:50 +00:00
std::string getDirSeparator()
{
return PHYSFS_getDirSeparator();
}
void permitSymbolicLinks(bool allow)
{
PHYSFS_permitSymbolicLinks(allow);
}
PHYSFS_sint64 getModTime(const std::string& item)
2005-02-06 21:30:09 +00:00
{
return PHYSFS_getLastModTime(item.c_str());
}
}
}
}