Page Menu
Home
Phabricator (Chris)
Search
Configure Global Search
Log In
Files
F126933
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
3 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/util/file-system.cpp b/util/file-system.cpp
index d4cc41aa..3d9eeb96 100644
--- a/util/file-system.cpp
+++ b/util/file-system.cpp
@@ -1,35 +1,62 @@
#include "funcs.h"
#include "file-system.h"
+#include "system.h"
+#include <sstream>
#include <exception>
#include <string>
using namespace std;
namespace Filesystem{
NotFound::NotFound(const std::string & file):
-exception(){
+exception(),
+reason(file){
}
NotFound::~NotFound() throw(){
}
+static string userDirectory(){
+ ostringstream str;
+ str << getenv("HOME") << "/.paintown/";
+ return str.str();
+}
+
+static string lookup(const std::string & path) throw (NotFound){
+ /* first try the main data directory */
+ string final = Util::getDataPath2() + path;
+ if (System::readable(final)){
+ return final;
+ }
+ /* then try the user directory, like ~/.paintown */
+ final = userDirectory() + path;
+ if (System::readable(final)){
+ return final;
+ }
+ /* then just look in the cwd */
+ if (System::readable(path)){
+ return path;
+ }
+ throw NotFound("Cannot find " + path);
+}
+
std::string find(const std::string & path) throw (NotFound){
if (path.length() == 0){
throw NotFound("No path given");
}
if (path[0] == '/'){
string str(path);
str.erase(0, 1);
- return Util::getDataPath2() + str;
+ return lookup(str);
}
- return Util::getDataPath2() + path;
+ return lookup(path);
}
std::string cleanse(const std::string & path){
string str = path;
str.erase(0, Util::getDataPath2().length());
return str;
}
}
diff --git a/util/file-system.h b/util/file-system.h
index 159bc84a..7fe3021f 100644
--- a/util/file-system.h
+++ b/util/file-system.h
@@ -1,25 +1,32 @@
#ifndef _paintown_file_system_h
#define _paintown_file_system_h
#include <exception>
#include <string>
namespace Filesystem{
class NotFound: public std::exception {
public:
NotFound(const std::string & file);
virtual ~NotFound() throw();
+
+ const std::string & getReason() const {
+ return reason;
+ }
+
+ private:
+ std::string reason;
};
/* given a relative path like sounds/arrow.png, prepend the proper
* data path to it to give data/sounds/arrow.png
*/
std::string find(const std::string & path) throw (NotFound);
/* remove the data path from a string
* data/sounds/arrow.png -> sounds/arrow.png
*/
std::string cleanse(const std::string & path);
}
#endif
diff --git a/util/system.cpp b/util/system.cpp
index 07932258..a37a8ffe 100644
--- a/util/system.cpp
+++ b/util/system.cpp
@@ -1,33 +1,37 @@
#include <string>
#include "system.h"
#ifndef WINDOWS
#include <unistd.h>
#include <sys/stat.h>
#endif
#ifndef WINDOWS
static bool isReadable(const std::string & path){
if (access(path.c_str(), R_OK) == 0){
return true;
} else {
return false;
}
}
static bool isDirectory(const std::string & path){
struct stat info;
if (stat(path.c_str(), &info) == 0){
if (S_ISDIR(info.st_mode) == 1){
return true;
} else {
return false;
}
}
return false;
}
bool System::readableFile(const std::string & path){
return isReadable(path) && ! isDirectory(path);
}
+
+bool System::readable(const std::string & path){
+ return isReadable(path);
+}
#endif
diff --git a/util/system.h b/util/system.h
index 3aa0eb56..1393b767 100644
--- a/util/system.h
+++ b/util/system.h
@@ -1,12 +1,13 @@
#ifndef _paintown_system_h
#define _paintown_system_h
/* system utilities */
#include <string>
namespace System{
bool readableFile(const std::string & path);
+ bool readable(const std::string & path);
}
#endif
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Thu, Jun 11, 1:40 PM (3 w, 4 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
69289
Default Alt Text
(3 KB)
Attached To
Mode
R75 R-Tech1
Attached
Detach File
Event Timeline