Ignore:
Timestamp:
01/15/15 22:43:01 (9 years ago)
Author:
Maciej Komosinski
Message:

Reorganizations and extensions of directory/file/filesystem IO classes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/common/nonstd_stdio.cpp

    r286 r295  
    88#include <common/stl-util.h>
    99
    10 #if defined _WIN32 && !defined SHP
    11 //<unistd.h> not needed for unlink()
     10#ifdef _WIN32
    1211#include "Shlwapi.h" //PathIsRelative()
     12#ifdef __BORLANDC__
     13#pragma link "Shlwapi.lib" //PathIsRelative()
     14#endif
    1315#include <sys/stat.h> //_stat
    1416#else
     
    2931}
    3032
    31 bool directoryExists(const char* path)
     33#ifdef _WIN32
     34bool 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
     52bool directoryExists(const char* path, bool is_writable)
    3253{
    3354        struct _stat s;
     55        if (path[0] == 0) path = ".";
    3456#ifdef _WIN32
    3557        if (_wstat(Convert::utf8ToUtf16(path).c_str(), &s) != 0) return false;
     
    3759        if (_stat(path, &s) != 0) return false;
    3860#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;
    4085}
    4186
     
    5196bool makeDirectories(const char* path)
    5297{
    53         if (directoryExists(path)) return true;
     98        if (directoryExists(path,false)) return true;
    5499        string parentdir = getFileDir(path);
    55100        if (!makeDirectories(parentdir.c_str())) return false;
     
    92137{
    93138        if (fname == NULL) return false; //SplitFileSystem never passes NULL but this function is public so we never know
    94 #if defined _WIN32
     139#ifdef _WIN32
    95140        return PathIsRelativeW(Convert::utf8ToUtf16(fname).c_str()) == FALSE; //http://msdn.microsoft.com/en-us/library/bb773660%28v=vs.85%29.aspx
    96141#else
     
    201246MFILE *mfopen(const char *path, const char *mode)
    202247{
    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::string
     248        string respath=getAppResources();
    204249        //printFM("Opening '%s', mode='%s'",path,mode);
    205         //printFM("GET_APP_RESOURCES='%s'",respath.c_str());
     250        //printFM("getAppResources()='%s'",respath.c_str());
    206251        NvFile *rfile=NULL; //can only read
    207252        FILE *rwfile=NULL;
Note: See TracChangeset for help on using the changeset viewer.