Changeset 295 for cpp/common
- Timestamp:
- 01/15/15 22:43:01 (10 years ago)
- Location:
- cpp/common
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/common/nonstd.h
r286 r295 61 61 #endif 62 62 63 #ifdef IPHONE64 #include <string>65 std::string getAppHome();66 std::string getAppResources();67 #define GET_APP_HOME getAppHome()68 #define GET_APP_RESOURCES getAppResources()69 #endif70 71 #ifdef SHP72 #define GET_APP_HOME "/Home/"73 #define GET_APP_RESOURCES "/Res/"74 #endif75 76 #ifdef TIZEN77 #define GET_APP_HOME "/data/"78 #define GET_APP_RESOURCES "/res/"79 #endif80 81 #ifdef __ANDROID__82 #define GET_APP_HOME getAppHome()83 #define GET_APP_RESOURCES "/resrc/" //inside APK, resources are kept in the "assets" subdirectory (in the "res" subdirectory there is no support for subdirectories nor accessing files by name). The prefix /resrc/ is just an indication that lets mfile easily discriminate between HOME (r/w) and RESOURCES (r) locations84 #endif85 86 #ifdef LINUX87 #define GET_APP_HOME "./"88 #define GET_APP_RESOURCES "./"89 #endif90 91 #ifdef MACOS92 #ifdef MACOS_APP_DIR //separate home/resources93 #include <string>94 std::string getAppHome();95 #define GET_APP_HOME getAppHome()96 #else //old style97 #define GET_APP_HOME "./"98 #endif99 #define GET_APP_RESOURCES "./"100 #endif101 102 #if defined(_WIN32) && !defined(SHP)103 #define GET_APP_HOME ".\\"104 #define GET_APP_RESOURCES ".\\"105 #endif106 107 #ifdef INITIAL_DIR_IS_HOME108 #include "cwd.h"109 #ifdef GET_APP_HOME110 #undef GET_APP_HOME111 #endif112 #define GET_APP_HOME getAppHome()113 #endif114 115 #ifdef INITIAL_DIR_IS_RES116 #include "cwd.h"117 #ifdef GET_APP_RESOURCES118 #undef GET_APP_RESOURCES119 #endif120 #define GET_APP_RESOURCES getAppResources()121 #endif122 123 63 //typedef unsigned char boolean; //niestety nie mozna uzyc 'bool' bo VC w rpcndr.h wlasnie tak definiuje booleana, jako unsigned char 124 64 //typedef char byte; //rozne srodowiska c++ definiuja byte jako unsigned char! w javie jest inaczej -> trzeba i tak zmienic w portowanych zrodlach byte na char. -
cpp/common/nonstd_stdio.cpp
r286 r295 8 8 #include <common/stl-util.h> 9 9 10 #if defined _WIN32 && !defined SHP 11 //<unistd.h> not needed for unlink() 10 #ifdef _WIN32 12 11 #include "Shlwapi.h" //PathIsRelative() 12 #ifdef __BORLANDC__ 13 #pragma link "Shlwapi.lib" //PathIsRelative() 14 #endif 13 15 #include <sys/stat.h> //_stat 14 16 #else … … 29 31 } 30 32 31 bool directoryExists(const char* path) 33 #ifdef _WIN32 34 bool isDirWritable(const char* path) //dir must not end with '\' 35 { 36 wstring dir = Convert::utf8ToUtf16(path); 37 CreateDirectoryW(dir.c_str(), 0); 38 dir += L"\\test_file.write"; 39 _wunlink(dir.c_str()); 40 FILE *f = _wfopen(dir.c_str(), L"wt"); 41 if (f) 42 { 43 fclose(f); 44 _wunlink(dir.c_str()); 45 return true; 46 } 47 else 48 return false; 49 } 50 #endif 51 52 bool directoryExists(const char* path, bool is_writable) 32 53 { 33 54 struct _stat s; 55 if (path[0] == 0) path = "."; 34 56 #ifdef _WIN32 35 57 if (_wstat(Convert::utf8ToUtf16(path).c_str(), &s) != 0) return false; … … 37 59 if (_stat(path, &s) != 0) return false; 38 60 #endif 39 return S_ISDIR(s.st_mode); 61 if (S_ISDIR(s.st_mode)) 62 { 63 if (is_writable) 64 { 65 #ifdef _WIN32 66 #ifndef W_OK 67 #define W_OK 2 //http://msdn.microsoft.com/en-us/library/1w06ktdy.aspx 68 #endif 69 //under Windows, access() is not a reliable way to check if a directory is writable 70 //http://stackoverflow.com/questions/198071/code-for-checking-write-permissions-for-directories-in-win2k-xp 71 //bool writable_access = _waccess(Convert::utf8ToUtf16(path).c_str(), W_OK) == 0; 72 bool writable_trial = isDirWritable(path); 73 //printf("Checking '%s' for writing(%d) using access(): result=%d\n", path, is_writable, writable_access); 74 //printf("File creation test: result=%d\n", writable_trial); 75 //return writable_access; 76 return writable_trial; 77 #else 78 return access(path, W_OK) == 0; 79 #endif 80 } 81 else 82 return true; 83 } 84 return false; 40 85 } 41 86 … … 51 96 bool makeDirectories(const char* path) 52 97 { 53 if (directoryExists(path )) return true;98 if (directoryExists(path,false)) return true; 54 99 string parentdir = getFileDir(path); 55 100 if (!makeDirectories(parentdir.c_str())) return false; … … 92 137 { 93 138 if (fname == NULL) return false; //SplitFileSystem never passes NULL but this function is public so we never know 94 #if defined_WIN32139 #ifdef _WIN32 95 140 return PathIsRelativeW(Convert::utf8ToUtf16(fname).c_str()) == FALSE; //http://msdn.microsoft.com/en-us/library/bb773660%28v=vs.85%29.aspx 96 141 #else … … 201 246 MFILE *mfopen(const char *path, const char *mode) 202 247 { 203 string respath= GET_APP_RESOURCES; //the macro can be char* or std::string, we don't know (nonstd.h, INITIAL_DIR_IS_RES, cwd.cpp) so we convert it to std::string248 string respath=getAppResources(); 204 249 //printFM("Opening '%s', mode='%s'",path,mode); 205 //printFM(" GET_APP_RESOURCES='%s'",respath.c_str());250 //printFM("getAppResources()='%s'",respath.c_str()); 206 251 NvFile *rfile=NULL; //can only read 207 252 FILE *rwfile=NULL; -
cpp/common/nonstd_stdio.h
r286 r295 7 7 8 8 bool fileExists(const char* path); 9 bool directoryExists(const char* path );9 bool directoryExists(const char* path,bool is_writable); 10 10 bool makeDirectory(const char* path); 11 11 bool makeDirectories(const char* path);
Note: See TracChangeset
for help on using the changeset viewer.