Ignore:
Timestamp:
12/27/14 01:01:43 (9 years ago)
Author:
Maciej Komosinski
Message:

Support for wide char (unicode) names of files and directories under Windows, internally encoded as char* utf-8

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/common/nonstd_stdio.cpp

    r247 r281  
    55#include "nonstd_stdio.h"
    66#include "nonstd.h"
    7 #include "Convert.h" //strTOwstr()
     7#include "Convert.h" //utf8ToUtf16()
    88#include <common/stl-util.h>
    99
    1010#if defined _WIN32 && !defined SHP
    11  //<unistd.h> not needed for unlink()
    12  #include "Shlwapi.h" //PathIsRelative()
    13  #include <sys/stat.h> //_stat
    14 #else
    15  #include <unistd.h>
     11//<unistd.h> not needed for unlink()
     12#include "Shlwapi.h" //PathIsRelative()
     13#include <sys/stat.h> //_stat
     14#else
     15#include <unistd.h>
    1616#endif
    1717
    1818bool fileExists(const char* path)
    1919{
    20 //lepiej gdyby uzywalo stat bo mfopen mogloby cos niepotrzebnie wczytywac przy otwarciu pliku ale mfopen wiadomo ze zadziala wszedzie tak samo
    21 MFILE *f=mfopen(path,FOPEN_READ_BINARY);
    22 if (f==NULL) return false;
    23 mfclose(f);
    24 return true;
     20        //lepiej gdyby uzywalo stat bo mfopen mogloby cos niepotrzebnie wczytywac przy otwarciu pliku ale mfopen wiadomo ze zadziala wszedzie tak samo
     21#ifdef _WIN32
     22        MFILE *f = mfopen(Convert::utf8ToUtf16(path).c_str(), Convert::strTOwstr(FOPEN_READ_BINARY).c_str());
     23#else
     24        MFILE *f = mfopen(path, FOPEN_READ_BINARY);
     25#endif
     26        if (f == NULL) return false;
     27        mfclose(f);
     28        return true;
    2529}
    2630
    2731bool directoryExists(const char* path)
    2832{
    29 struct _stat s;
    30 if (_stat(path,&s)!=0) return false;
    31 return S_ISDIR(s.st_mode);
     33        struct _stat s;
     34#ifdef _WIN32
     35        if (_wstat(Convert::utf8ToUtf16(path).c_str(), &s) != 0) return false;
     36#else
     37        if (_stat(path, &s) != 0) return false;
     38#endif
     39        return S_ISDIR(s.st_mode);
    3240}
    3341
     
    3543{
    3644#ifdef _WIN32
    37 return mkdir(path)==0;
    38 #else
    39 return mkdir(path,0777)==0;
     45        return _wmkdir(Convert::utf8ToUtf16(path).c_str()) == 0;
     46#else
     47        return mkdir(path,0777) == 0;
    4048#endif
    4149}
     
    4351bool makeDirectories(const char* path)
    4452{
    45 if (directoryExists(path)) return true;
    46 string parentdir=getFileDir(path);
    47 if (!makeDirectories(parentdir.c_str())) return false;
    48 return makeDirectory(path);
     53        if (directoryExists(path)) return true;
     54        string parentdir = getFileDir(path);
     55        if (!makeDirectories(parentdir.c_str())) return false;
     56        return makeDirectory(path);
    4957}
    5058
    5159int getFileSize(const char* path)
    5260{
    53 int size;
    54 MFILE *f=mfopen(path,FOPEN_READ_BINARY);
    55 if (f==NULL) return -1;
    56 size=getFileSize(f);
    57 mfclose(f);
    58 return size;
     61        int size;
     62#ifdef _WIN32
     63        MFILE *f = mfopen(Convert::utf8ToUtf16(path).c_str(), Convert::strTOwstr(FOPEN_READ_BINARY).c_str());
     64#else
     65        MFILE *f = mfopen(path, FOPEN_READ_BINARY);
     66#endif
     67        if (f == NULL) return -1;
     68        size = getFileSize(f);
     69        mfclose(f);
     70        return size;
    5971}
    6072
    6173int getFileSize(MFILE *f)
    6274{
    63   int saved_pos = mftell(f);
    64   mfseek(f, 0, SEEK_END);
    65   int size = mftell(f);
    66   mfseek(f, saved_pos, SEEK_SET);
    67   return size;
     75        int saved_pos = mftell(f);
     76        mfseek(f, 0, SEEK_END);
     77        int size = mftell(f);
     78        mfseek(f, saved_pos, SEEK_SET);
     79        return size;
    6880}
    6981
    7082bool removeFile(const char* path)
    7183{
    72   return _unlink(path)==0; //VS: "The POSIX name is deprecated. Instead, use the ISO C++ conformant name: _unlink"
     84#ifdef _WIN32
     85        return _wunlink(Convert::utf8ToUtf16(path).c_str()) == 0;
     86#else
     87        return _unlink(path) == 0; //VS: "The POSIX name is deprecated. Instead, use the ISO C++ conformant name: _unlink"
     88#endif
    7389}
    7490
     
    7692{
    7793        if (fname == NULL) return false; //SplitFileSystem never passes NULL but this function is public so we never know
    78 #if defined _WIN32 && !defined SHP
    79  #ifdef __BORLANDC__
    80         return PathIsRelative(fname) == FALSE; //no wide char for old borland compiler
    81  #else
    82         return PathIsRelative(Convert::strTOwstr(fname).c_str()) == FALSE; //http://msdn.microsoft.com/en-us/library/bb773660%28v=vs.85%29.aspx
    83  #endif
     94#if defined _WIN32
     95        return PathIsRelativeW(Convert::utf8ToUtf16(fname).c_str()) == FALSE; //http://msdn.microsoft.com/en-us/library/bb773660%28v=vs.85%29.aspx
    8496#else
    8597        return fname[0] == PATH_SEPARATOR_CHAR;
Note: See TracChangeset for help on using the changeset viewer.