Changeset 281 for cpp/frams


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

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

Location:
cpp/frams/virtfile
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/virtfile/stdiofile.cpp

    r227 r281  
    77#include <common/nonstd_stdio.h>
    88#include <common/framsg.h>
     9#include <common/Convert.h>
    910
    10 VirtFILE* StdioFileSystem::Vfopen(const char* path,const char*mode)
     11VirtFILE* StdioFileSystem::Vfopen(const char *path, const char *mode)
    1112{
    1213        //printFM("Vfopen %s %s",path,mode);
    1314#ifdef USE_MFILE
    14         MFILE *f=mfopen(path,mode);
     15        MFILE *f = mfopen(path, mode);
    1516#else
    16         FILE *f=fopen(path,mode);
     17#ifdef _WIN32
     18        FILE *f = _wfopen(Convert::utf8ToUtf16(path).c_str(), Convert::strTOwstr(mode).c_str());
     19#else
     20        FILE *f = fopen(path, mode);
     21#endif
    1722#endif
    1823        //printFM("%p",f);
    19         if (f) return new StdioFILE(f,path); else return 0;
     24        if (f) return new StdioFILE(f, path); else return 0;
    2025}
    2126
     
    2328{
    2429        //printFM("Vopendir %s",path);
    25         DIR *d=opendir(path);
     30#ifdef _WIN32
     31        DIRTYPE *d = wopendir(Convert::utf8ToUtf16(path).c_str());
     32#else
     33        DIR *d = opendir(path);
     34#endif
    2635        //printFM("%p",d);
    2736        if (d) return new StdioDIR(d); else return 0;
     
    4857{
    4958        //printFM("Vreaddir %s",dir);
     59#ifdef _WIN32
     60        wdirent *wde=wreaddir(dir);
     61        if (wde==NULL) return NULL;
     62        strcpy(de.d_name, Convert::wstrToUtf8(wde->d_name).c_str());
     63        return &de;
     64#else
    5065        return readdir(dir);
     66#endif
    5167}
  • cpp/frams/virtfile/stdiofile.h

    r247 r281  
    1515#include <common/nonstd_dir.h>
    1616
    17 class StdioFileSystem: public VirtFileSystem
     17class StdioFileSystem : public VirtFileSystem
    1818{
    1919public:
    20 VirtFILE *Vfopen(const char* path,const char*mode);
    21 int Vfexists(const char* path);
    22 VirtDIR *Vopendir(const char* path);
     20        VirtFILE *Vfopen(const char *path, const char *mode);
     21        int Vfexists(const char* path);
     22        VirtDIR *Vopendir(const char* path);
    2323};
    2424
    2525#ifdef USE_MFILE
    26 class StdioFILE: public VirtFILE
     26class StdioFILE : public VirtFILE
    2727{
    28   protected:
    29 MFILE *file;
    30 SString path;
    31   public:
    32 StdioFILE(MFILE *f) {file=f;}
    33 StdioFILE(MFILE *f,const SString& p) {file=f;path=p;}
    34 static void setStdio();
    35 size_t Vread(void *ptr, size_t size, size_t nmemb) {return mfread(ptr,size,nmemb,file);}
    36 size_t Vwrite(const void *ptr, size_t size, size_t nmemb) {return mfwrite(ptr,size,nmemb,file);}
    37 int Veof() {return mfeof(file);}
    38 int Vputs(const char *s) {return mfputs(s,file);}
    39 char *Vgets(char *s, int size) {return mfgets(s,size,file);}
    40 int Vseek(long offset, int whence) {return mfseek(file,offset,whence);}
    41 long Vtell() {return mftell(file);}
    42 int Vflush() {return 0;/*NOT IMPLEMENTED!*/;}
    43 const char* VgetPath() {return path;}
     28protected:
     29        MFILE *file;
     30        SString path;
     31public:
     32        StdioFILE(MFILE *f) { file = f; }
     33        StdioFILE(MFILE *f, const SString& p) { file = f; path = p; }
     34        static void setStdio();
     35        size_t Vread(void *ptr, size_t size, size_t nmemb) { return mfread(ptr, size, nmemb, file); }
     36        size_t Vwrite(const void *ptr, size_t size, size_t nmemb) { return mfwrite(ptr, size, nmemb, file); }
     37        int Veof() { return mfeof(file); }
     38        int Vputs(const char *s) { return mfputs(s, file); }
     39        char *Vgets(char *s, int size) { return mfgets(s, size, file); }
     40        int Vseek(long offset, int whence) { return mfseek(file, offset, whence); }
     41        long Vtell() { return mftell(file); }
     42        int Vflush() { return 0; /*NOT IMPLEMENTED!*/ }
     43        const char* VgetPath() { return path; }
    4444
    45 ~StdioFILE() {if (file) mfclose(file);}
     45        ~StdioFILE() { if (file) mfclose(file); }
    4646};
    4747#else
    48 class StdioFILE: public VirtFILE
     48class StdioFILE : public VirtFILE
    4949{
    50   protected:
    51 FILE *file;
    52 SString path;
    53   public:
    54 StdioFILE(FILE *f) {file=f;}
    55 StdioFILE(FILE *f,const SString& p) {file=f;path=p;}
    56 static void setStdio();
    57 size_t Vread(void *ptr, size_t size, size_t nmemb) {return fread(ptr,size,nmemb,file);}
    58 size_t Vwrite(const void *ptr, size_t size, size_t nmemb) {return fwrite(ptr,size,nmemb,file);}
    59 int Veof() {return feof(file);}
    60 int Vputc(int c) {return fputc(c,file);}
    61 int Vputs(const char *s) {return fputs(s,file);}
    62 int Vgetc() {return fgetc(file);}
    63 char *Vgets(char *s, int size) {return fgets(s,size,file);}
    64 int Vprintf(const char *format, va_list args) { return vfprintf(file,format,args); }
    65 int Vseek(long offset, int whence) {return fseek(file,offset,whence);}
    66 long Vtell() {return ftell(file);}
    67 void Vrewind() {rewind(file);}
    68 int Vflush() {return fflush(file);}
    69 const char* VgetPath() {return path;}
     50protected:
     51        FILE *file;
     52        SString path;
     53public:
     54        StdioFILE(FILE *f) { file = f; }
     55        StdioFILE(FILE *f, const SString& p) { file = f; path = p; }
     56        static void setStdio();
     57        size_t Vread(void *ptr, size_t size, size_t nmemb) { return fread(ptr, size, nmemb, file); }
     58        size_t Vwrite(const void *ptr, size_t size, size_t nmemb) { return fwrite(ptr, size, nmemb, file); }
     59        int Veof() { return feof(file); }
     60        int Vputc(int c) { return fputc(c, file); }
     61        int Vputs(const char *s) { return fputs(s, file); }
     62        int Vgetc() { return fgetc(file); }
     63        char *Vgets(char *s, int size) { return fgets(s, size, file); }
     64        int Vprintf(const char *format, va_list args) { return vfprintf(file, format, args); }
     65        int Vseek(long offset, int whence) { return fseek(file, offset, whence); }
     66        long Vtell() { return ftell(file); }
     67        void Vrewind() { rewind(file); }
     68        int Vflush() { return fflush(file); }
     69        const char* VgetPath() { return path; }
    7070
    71 ~StdioFILE() {if (file) fclose(file);}
     71        ~StdioFILE() { if (file) fclose(file); }
    7272};
    7373#endif
    7474
    75 class StdioDIR: public VirtDIR
     75
     76#ifdef _WIN32
     77#ifdef __BORLANDC__
     78 typedef wDIR DIRTYPE;
     79#else
     80 typedef WDIR DIRTYPE;
     81#endif
     82#else
     83 typedef DIR DIRTYPE;
     84#endif
     85
     86class StdioDIR : public VirtDIR
    7687{
    77 DIR *dir;
    78   public:
    79 StdioDIR(DIR* d):dir(d) {}
    80 ~StdioDIR() {if (dir) closedir(dir);}
    81 dirent* Vreaddir();
     88        DIRTYPE *dir;
     89#ifdef _WIN32
     90        dirent de; //only used to convert wide string names (wdirent) to utf8 (dirent)
     91#endif
     92public:
     93        StdioDIR(DIRTYPE *d) : dir(d) {}
     94        ~StdioDIR()
     95        {
     96#ifdef _WIN32
     97                if (dir) wclosedir(dir);
     98#else
     99                if (dir) closedir(dir);
     100#endif
     101        }
     102        dirent* Vreaddir();
    82103};
    83104
    84 class StdioFILEDontClose: public StdioFILE
     105class StdioFILEDontClose : public StdioFILE
    85106{
    86   public:
     107public:
    87108#ifdef USE_MFILE
    88 StdioFILEDontClose(MFILE *f):StdioFILE(f) {}
     109        StdioFILEDontClose(MFILE *f) : StdioFILE(f) {}
    89110#else
    90 StdioFILEDontClose(FILE *f):StdioFILE(f) {}
     111        StdioFILEDontClose(FILE *f) : StdioFILE(f) {}
    91112#endif
    92 ~StdioFILEDontClose() {file=0;}
     113        ~StdioFILEDontClose() { file = 0; }
    93114};
    94115
    95116#endif
     117
Note: See TracChangeset for help on using the changeset viewer.