Changeset 302 for cpp/frams/virtfile


Ignore:
Timestamp:
01/20/15 10:24:05 (10 years ago)
Author:
Maciej Komosinski
Message:

Better macro name; source formatting

Location:
cpp/frams/virtfile
Files:
2 edited

Legend:

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

    r301 r302  
    1212VirtFileSystem *VirtFILE::vfs = NULL;
    1313
    14 //#define DEBUG_DLL_CALLS
    15 
    16 VirtFILE *Vfopen(const char* path,const char* mode)
    17 {
    18 #ifdef DEBUG_DLL_CALLS
    19 printf("VirtFILE::Vfopen %s %s (vfs=%p)\n",path,mode,VirtFILE::vfs);
    20 #endif
    21 return VirtFILE::vfs ? VirtFILE::vfs->Vfopen(path,mode) : NULL;
     14//#define DEBUG_VIRTFILE
     15
     16VirtFILE *Vfopen(const char* path, const char* mode)
     17{
     18#ifdef DEBUG_VIRTFILE
     19        printf("VirtFILE::Vfopen %s %s (vfs=%p)\n",path,mode,VirtFILE::vfs);
     20#endif
     21        return VirtFILE::vfs ? VirtFILE::vfs->Vfopen(path, mode) : NULL;
    2222}
    2323
    2424VirtDIR *Vopendir(const char* path)
    2525{
    26 #ifdef DEBUG_DLL_CALLS
    27 printf("VirtFILE::Vfopendir %s (vfs=%p)\n",path,VirtFILE::vfs);
    28 #endif
    29 return VirtFILE::vfs ? VirtFILE::vfs->Vopendir(path) : NULL;
     26#ifdef DEBUG_VIRTFILE
     27        printf("VirtFILE::Vfopendir %s (vfs=%p)\n",path,VirtFILE::vfs);
     28#endif
     29        return VirtFILE::vfs ? VirtFILE::vfs->Vopendir(path) : NULL;
    3030}
    3131
    3232bool Vfexists(const char* path)
    3333{
    34 return VirtFILE::vfs ? VirtFILE::vfs->Vfexists(path) : false;
    35 }
    36 
    37 bool Vdirexists(const char* path,bool is_writable)
    38 {
    39 return VirtFILE::vfs ? VirtFILE::vfs->Vdirexists(path,is_writable) : false;
     34        return VirtFILE::vfs ? VirtFILE::vfs->Vfexists(path) : false;
     35}
     36
     37bool Vdirexists(const char* path, bool is_writable)
     38{
     39        return VirtFILE::vfs ? VirtFILE::vfs->Vdirexists(path, is_writable) : false;
    4040}
    4141
    4242bool Vmkdir(const char* path)
    4343{
    44 return VirtFILE::vfs ? VirtFILE::vfs->Vmkdir(path) : false;
     44        return VirtFILE::vfs ? VirtFILE::vfs->Vmkdir(path) : false;
    4545}
    4646
    4747bool Vmkdirs(const char* path)
    4848{
    49 return VirtFILE::vfs ? VirtFILE::vfs->Vmkdirs(path) : false;
     49        return VirtFILE::vfs ? VirtFILE::vfs->Vmkdirs(path) : false;
    5050}
    5151
     
    5555void VirtFILE::selectFileSystem(VirtFileSystem *s)
    5656{
    57 vfs=s;
    58 #ifdef DEBUG_DLL_CALLS
    59 ::printf("VirtFILE::selectFileSystem: %p := %p\n",vfs,s);
     57        vfs = s;
     58#ifdef DEBUG_VIRTFILE
     59        ::printf("VirtFILE::selectFileSystem: %p := %p\n",vfs,s);
    6060#endif
    6161}
     
    6363int VirtFILE::Vprintf(const char *format, va_list args)
    6464{
    65 string s=ssprintf_va(format,args);
    66 return Vwrite(s.c_str(),1,s.size());
     65        string s = ssprintf_va(format, args);
     66        return Vwrite(s.c_str(), 1, s.size());
    6767}
    6868
    6969int VirtFILE::printf(const char *format, ...)
    7070{
    71 int ret; va_list argptr;
    72 va_start(argptr,format);
    73 ret=Vprintf(format,argptr);
    74 va_end(argptr);
    75 return ret;
     71        int ret; va_list argptr;
     72        va_start(argptr, format);
     73        ret = Vprintf(format, argptr);
     74        va_end(argptr);
     75        return ret;
    7676}
    7777
    7878int VirtFILE::getSize()
    7979{
    80   int saved_pos = Vtell();
    81   Vseek(0, SEEK_END);
    82   int size = Vtell();
    83   Vseek(saved_pos, SEEK_SET);
    84   return size;
    85 }
    86 
    87 void VirtFILE::setVstdin(VirtFILE *f) {Vstdin=f;}
    88 void VirtFILE::setVstdout(VirtFILE *f) {Vstdout=f;}
    89 void VirtFILE::setVstderr(VirtFILE *f) {Vstderr=f;}
    90 VirtFILE* VirtFILE::getVstdin() {return Vstdin;}
    91 VirtFILE* VirtFILE::getVstdout() {return Vstdout;}
    92 VirtFILE* VirtFILE::getVstderr() {return Vstderr;}
     80        int saved_pos = Vtell();
     81        Vseek(0, SEEK_END);
     82        int size = Vtell();
     83        Vseek(saved_pos, SEEK_SET);
     84        return size;
     85}
     86
     87void VirtFILE::setVstdin(VirtFILE *f) { Vstdin = f; }
     88void VirtFILE::setVstdout(VirtFILE *f) { Vstdout = f; }
     89void VirtFILE::setVstderr(VirtFILE *f) { Vstderr = f; }
     90VirtFILE* VirtFILE::getVstdin() { return Vstdin; }
     91VirtFILE* VirtFILE::getVstdout() { return Vstdout; }
     92VirtFILE* VirtFILE::getVstderr() { return Vstderr; }
    9393//////////////////////////////////////////////////////////////////////////
    9494
    9595// base class only returns NULL/false/not supported - implementations perform the actual work
    96 VirtFILE* VirtFileSystem::Vfopen(const char* path,const char* mode) {return NULL;}
    97 bool VirtFileSystem::Vfexists(const char* path) {return false;}
    98 VirtDIR* VirtFileSystem::Vopendir(const char* path) {return NULL;}
    99 bool VirtFileSystem::Vmkdir(const char* path) {return false;}
    100 bool VirtFileSystem::Vdirexists(const char* path,bool is_writable) {return false;}
     96VirtFILE* VirtFileSystem::Vfopen(const char* path, const char* mode) { return NULL; }
     97bool VirtFileSystem::Vfexists(const char* path) { return false; }
     98VirtDIR* VirtFileSystem::Vopendir(const char* path) { return NULL; }
     99bool VirtFileSystem::Vmkdir(const char* path) { return false; }
     100bool VirtFileSystem::Vdirexists(const char* path, bool is_writable) { return false; }
    101101
    102102//////////////////////////////////////////////////////////////////////////
     
    104104
    105105
    106 int fread(void *ptr, size_t size, size_t nmemb, VirtFILE* f) {return f->Vread(ptr,size,nmemb);}
    107 int fwrite(const void *ptr, size_t size, size_t nmemb, VirtFILE* f) {return f->Vwrite(ptr,size,nmemb);}
     106int fread(void *ptr, size_t size, size_t nmemb, VirtFILE* f) { return f->Vread(ptr, size, nmemb); }
     107int fwrite(const void *ptr, size_t size, size_t nmemb, VirtFILE* f) { return f->Vwrite(ptr, size, nmemb); }
    108108
    109109
    110110//since we want our own feof(VirtFILE*) function and some systems unfortunately define feof as a macro, we need to #undef it. Same as in virtfile.h
    111111#if defined _MSC_VER || defined __CYGWIN__ || defined SHP || defined __ANDROID__
    112  #pragma push_macro("feof")
    113  #undef feof
     112#pragma push_macro("feof")
     113#undef feof
    114114#endif
    115115#if defined __BORLANDC__ //does not support #pragma push_macro/pop_macro
    116  #undef feof
    117 #endif
    118 
    119 int feof(VirtFILE* f) {return f->Veof();}
     116#undef feof
     117#endif
     118
     119int feof(VirtFILE* f) { return f->Veof(); }
    120120
    121121//...and then restore the original macro:
    122122#if defined _MSC_VER || defined __CYGWIN__ || defined SHP || defined __ANDROID__
    123  #pragma pop_macro("feof")
     123#pragma pop_macro("feof")
    124124#endif
    125125#if defined __BORLANDC__
    126  #define feof(__f)     ((__f)->flags & _F_EOF)
    127 #endif
    128 
    129 
    130 int fputc(int c,VirtFILE* f) {return f->Vputc(c);}
    131 int fputs(const char *s,VirtFILE* f) {return f->Vputs(s);}
    132 int fgetc(VirtFILE* f) {return f->Vgetc();}
    133 int fseek(VirtFILE* f,long offset, int whence) {return f->Vseek(offset,whence);}
    134 int ftell(VirtFILE* f) {return f->Vtell();}
    135 void rewind(VirtFILE* f) {f->Vrewind();}
    136 int fflush(VirtFILE* f) {return f->Vflush();}
    137 char *fgets(char *s, int size, VirtFILE* f) {return f->Vgets(s,size);}
    138 int fprintf(VirtFILE* f,const char *format, ...)
    139         {
     126#define feof(__f)     ((__f)->flags & _F_EOF)
     127#endif
     128
     129
     130int fputc(int c, VirtFILE* f) { return f->Vputc(c); }
     131int fputs(const char *s, VirtFILE* f) { return f->Vputs(s); }
     132int fgetc(VirtFILE* f) { return f->Vgetc(); }
     133int fseek(VirtFILE* f, long offset, int whence) { return f->Vseek(offset, whence); }
     134int ftell(VirtFILE* f) { return f->Vtell(); }
     135void rewind(VirtFILE* f) { f->Vrewind(); }
     136int fflush(VirtFILE* f) { return f->Vflush(); }
     137char *fgets(char *s, int size, VirtFILE* f) { return f->Vgets(s, size); }
     138int fprintf(VirtFILE* f, const char *format, ...)
     139{
    140140        int ret; va_list argptr;
    141         va_start(argptr,format);
    142         ret=f->Vprintf(format,argptr);
     141        va_start(argptr, format);
     142        ret = f->Vprintf(format, argptr);
    143143        va_end(argptr);
    144144        return ret;
    145         }
    146 int fclose(VirtFILE* f) {delete f; return 0;}
    147 
    148 int closedir(VirtDIR* d) {delete d; return 0;}
    149 dirent* readdir(VirtDIR* d) {return d->Vreaddir();}
     145}
     146int fclose(VirtFILE* f) { delete f; return 0; }
     147
     148int closedir(VirtDIR* d) { delete d; return 0; }
     149dirent* readdir(VirtDIR* d) { return d->Vreaddir(); }
    150150
    151151/////////
     
    153153bool VirtFileSystem::Vmkdirs(const char* path)
    154154{
    155 if (Vdirexists(path,true)) return true;
    156 string parentdir = getFileDir(path);
    157 if (!Vmkdirs(parentdir.c_str())) return false;
    158 return Vmkdir(path);
     155        if (Vdirexists(path, true)) return true;
     156        string parentdir = getFileDir(path);
     157        if (!Vmkdirs(parentdir.c_str())) return false;
     158        return Vmkdir(path);
    159159}
    160160
     
    164164ChainFileSystem::ChainFileSystem(VirtFileSystem *_chain)
    165165{
    166         chain=_chain;
    167 #ifdef DEBUG_DLL_CALLS
    168 printf("ChainFileSystem constructor: %p := %p\n",chain,_chain);
     166        chain = _chain;
     167#ifdef DEBUG_VIRTFILE
     168        printf("ChainFileSystem constructor: %p := %p\n",chain,_chain);
    169169#endif
    170170}
     
    173173VirtFILE *ChainFileSystem::Vfopen(const char* path, const char* mode)
    174174{
    175 #ifdef DEBUG_DLL_CALLS
    176 printf("ChainFileSystem::Vfopen %s %s (chain=%p)\n",path,mode,chain);
     175#ifdef DEBUG_VIRTFILE
     176        printf("ChainFileSystem::Vfopen %s %s (chain=%p)\n",path,mode,chain);
    177177#endif
    178178        return (chain != NULL) ? chain->Vfopen(path, mode) : NULL;
     
    186186VirtDIR *ChainFileSystem::Vopendir(const char* path)
    187187{
    188 #ifdef DEBUG_DLL_CALLS
    189 printf("ChainFileSystem::Vfopendir %s (chain=%p)\n",path,chain);
     188#ifdef DEBUG_VIRTFILE
     189        printf("ChainFileSystem::Vfopendir %s (chain=%p)\n",path,chain);
    190190#endif
    191191        return (chain != NULL) ? chain->Vopendir(path) : NULL;
     
    202202}
    203203
    204 bool ChainFileSystem::Vdirexists(const char* path,bool is_writable)
    205 {
    206         return (chain != NULL) ? chain->Vdirexists(path,is_writable) : false;
    207 }
     204bool ChainFileSystem::Vdirexists(const char* path, bool is_writable)
     205{
     206        return (chain != NULL) ? chain->Vdirexists(path, is_writable) : false;
     207}
  • cpp/frams/virtfile/virtfile.h

    r301 r302  
    2626class DLLEXP VirtFILE
    2727{
    28   protected:
    29 string path;
    30   public:
    31 virtual size_t Vread(void *ptr, size_t size, size_t nmemb)=0;
    32 virtual size_t Vwrite(const void *ptr, size_t size, size_t nmemb)=0;
    33 virtual int Veof()=0;
    34 virtual int Vputc(int c) {unsigned char data=(unsigned char)c; return (Vwrite(&data,1,1)==1)?data:EOF;}
    35 virtual int Vputs(const char *s)=0;
    36 virtual int Vgetc() {unsigned char data; if (Vread(&data,1,1)==1) return data; else return EOF;}
    37 virtual int Vseek(long offset, int whence)=0;
    38 virtual long Vtell()=0;
    39 virtual void Vrewind() {Vseek(0,SEEK_SET);}
    40 virtual int Vflush()=0;
    41 virtual char *Vgets(char *s, int size)=0;
    42 virtual int Vprintf(const char *format, va_list args);
    43 int printf(const char *format, ...);
    44 virtual const char *VgetPath() {return path.c_str();}
    45 virtual int getSize();
    46 VirtFILE(const char* _path):path(_path) {}
    47 virtual ~VirtFILE();
    48 static VirtFILE *Vstdin,*Vstdout,*Vstderr;
    49 static void setVstdin(VirtFILE *);
    50 static void setVstdout(VirtFILE *);
    51 static void setVstderr(VirtFILE *);
    52 static VirtFILE* getVstdin();
    53 static VirtFILE* getVstdout();
    54 static VirtFILE* getVstderr();
    55 static VirtFileSystem *vfs;
    56 static void selectFileSystem(VirtFileSystem *s);
     28protected:
     29        string path;
     30public:
     31        virtual size_t Vread(void *ptr, size_t size, size_t nmemb) = 0;
     32        virtual size_t Vwrite(const void *ptr, size_t size, size_t nmemb) = 0;
     33        virtual int Veof() = 0;
     34        virtual int Vputc(int c) { unsigned char data = (unsigned char)c; return (Vwrite(&data, 1, 1) == 1) ? data : EOF; }
     35        virtual int Vputs(const char *s) = 0;
     36        virtual int Vgetc() { unsigned char data; if (Vread(&data, 1, 1) == 1) return data; else return EOF; }
     37        virtual int Vseek(long offset, int whence) = 0;
     38        virtual long Vtell() = 0;
     39        virtual void Vrewind() { Vseek(0, SEEK_SET); }
     40        virtual int Vflush() = 0;
     41        virtual char *Vgets(char *s, int size) = 0;
     42        virtual int Vprintf(const char *format, va_list args);
     43        int printf(const char *format, ...);
     44        virtual const char *VgetPath() { return path.c_str(); }
     45        virtual int getSize();
     46        VirtFILE(const char* _path) :path(_path) {}
     47        virtual ~VirtFILE();
     48        static VirtFILE *Vstdin, *Vstdout, *Vstderr;
     49        static void setVstdin(VirtFILE *);
     50        static void setVstdout(VirtFILE *);
     51        static void setVstderr(VirtFILE *);
     52        static VirtFILE* getVstdin();
     53        static VirtFILE* getVstdout();
     54        static VirtFILE* getVstderr();
     55        static VirtFileSystem *vfs;
     56        static void selectFileSystem(VirtFileSystem *s);
    5757};
    5858
    5959/** can be used directly or as a base class for implementations delegating VirtFILE calls to another VirtFILE object */
    60 class DLLEXP DelegatedFILE: public VirtFILE
     60class DLLEXP DelegatedFILE : public VirtFILE
    6161{
    62 VirtFILE *delegate;
    63   public:
    64 size_t Vread(void *ptr, size_t size, size_t nmemb) {return delegate->Vread(ptr,size,nmemb);}
    65 size_t Vwrite(const void *ptr, size_t size, size_t nmemb) {return delegate->Vwrite(ptr,size,nmemb);}
    66 int Veof() {return delegate->Veof();}
    67 int Vputc(int c) {return delegate->Vputc(c);}
    68 int Vputs(const char *s) {return delegate->Vputs(s);}
    69 int Vgetc() {return delegate->Vgetc();}
    70 int Vseek(long offset, int whence) {return delegate->Vseek(offset,whence);}
    71 long Vtell() {return delegate->Vtell();}
    72 void Vrewind() {delegate->Vrewind();}
    73 int Vflush() {return delegate->Vflush();}
    74 char *Vgets(char *s, int size) {return delegate->Vgets(s,size);}
    75 int Vprintf(const char *format, va_list args) {return delegate->Vprintf(format,args);}
    76 int getSize() {return delegate->getSize();}
    77 // not overriden: VgetPath()
     62        VirtFILE *delegate;
     63public:
     64        size_t Vread(void *ptr, size_t size, size_t nmemb) { return delegate->Vread(ptr, size, nmemb); }
     65        size_t Vwrite(const void *ptr, size_t size, size_t nmemb) { return delegate->Vwrite(ptr, size, nmemb); }
     66        int Veof() { return delegate->Veof(); }
     67        int Vputc(int c) { return delegate->Vputc(c); }
     68        int Vputs(const char *s) { return delegate->Vputs(s); }
     69        int Vgetc() { return delegate->Vgetc(); }
     70        int Vseek(long offset, int whence) { return delegate->Vseek(offset, whence); }
     71        long Vtell() { return delegate->Vtell(); }
     72        void Vrewind() { delegate->Vrewind(); }
     73        int Vflush() { return delegate->Vflush(); }
     74        char *Vgets(char *s, int size) { return delegate->Vgets(s, size); }
     75        int Vprintf(const char *format, va_list args) { return delegate->Vprintf(format, args); }
     76        int getSize() { return delegate->getSize(); }
     77        // not overriden: VgetPath()
    7878
    79 DelegatedFILE(const char* _path,VirtFILE *_delegate):VirtFILE(_path),delegate(_delegate) {}
    80 virtual ~DelegatedFILE() {if (delegate) delete delegate; delegate=NULL;}
     79        DelegatedFILE(const char* _path, VirtFILE *_delegate) :VirtFILE(_path), delegate(_delegate) {}
     80        virtual ~DelegatedFILE() { if (delegate) delete delegate; delegate = NULL; }
    8181};
    8282
    8383class DLLEXP VirtDIR
    8484{
    85   public:
    86 virtual ~VirtDIR() {}
    87 virtual dirent* Vreaddir() {return 0;}
     85public:
     86        virtual ~VirtDIR() {}
     87        virtual dirent* Vreaddir() { return 0; }
    8888};
    8989
     
    9191{
    9292public:
    93 virtual VirtFILE *Vfopen(const char* path,const char*mode);
    94 virtual bool Vfexists(const char* path);
    95 virtual VirtDIR *Vopendir(const char* path);
    96 virtual bool Vmkdir(const char* path);
    97 virtual bool Vmkdirs(const char* path);
    98 virtual bool Vdirexists(const char* path,bool is_writable);
     93        virtual VirtFILE *Vfopen(const char* path, const char*mode);
     94        virtual bool Vfexists(const char* path);
     95        virtual VirtDIR *Vopendir(const char* path);
     96        virtual bool Vmkdir(const char* path);
     97        virtual bool Vmkdirs(const char* path);
     98        virtual bool Vdirexists(const char* path, bool is_writable);
    9999};
    100100
     
    110110        bool Vmkdir(const char* path);
    111111        bool Vmkdirs(const char* path);
    112         bool Vdirexists(const char* path,bool is_writable);
     112        bool Vdirexists(const char* path, bool is_writable);
    113113};
    114114
    115115
    116 DLLEXP VirtFILE *Vfopen(const char* path,const char*mode);
     116DLLEXP VirtFILE *Vfopen(const char* path, const char*mode);
    117117DLLEXP VirtDIR *Vopendir(const char* path);
    118118DLLEXP bool Vfexists(const char* path);
    119119DLLEXP bool Vmkdir(const char* path);
    120120DLLEXP bool Vmkdirs(const char* path);
    121 DLLEXP bool Vdirexists(const char* path,bool is_writable);
     121DLLEXP bool Vdirexists(const char* path, bool is_writable);
    122122
    123123DLLEXP int fread(void *ptr, size_t size, size_t nmemb, VirtFILE* f);
     
    127127//since we want our own feof(VirtFILE*) function and some systems unfortunately define feof as a macro, we need to #undef it. Same as in virtfile.cpp
    128128#if defined _MSC_VER || defined __CYGWIN__ || defined SHP || defined __ANDROID__
    129  #pragma push_macro("feof")
    130  #undef feof
     129#pragma push_macro("feof")
     130#undef feof
    131131#endif
    132132#if defined __BORLANDC__ //does not support #pragma push_macro/pop_macro
    133  #undef feof
     133#undef feof
    134134#endif
    135  
     135
    136136DLLEXP int feof(VirtFILE* f);// {return f->Veof();}
    137137
    138138//...and then restore the original macro:
    139139#if defined _MSC_VER || defined __CYGWIN__ || defined SHP || defined __ANDROID__
    140  #pragma pop_macro("feof")
     140#pragma pop_macro("feof")
    141141#endif
    142142#if defined __BORLANDC__
    143  #define feof(__f)     ((__f)->flags & _F_EOF)
     143#define feof(__f)     ((__f)->flags & _F_EOF)
    144144#endif
    145145
    146146
    147 DLLEXP int fputc(int c,VirtFILE* f);
    148 DLLEXP int fputs(const char *s,VirtFILE* f);
     147DLLEXP int fputc(int c, VirtFILE* f);
     148DLLEXP int fputs(const char *s, VirtFILE* f);
    149149DLLEXP int fgetc(VirtFILE* f);
    150 DLLEXP int fseek(VirtFILE* f,long offset, int whence);
     150DLLEXP int fseek(VirtFILE* f, long offset, int whence);
    151151DLLEXP int ftell(VirtFILE* f);
    152152DLLEXP void rewind(VirtFILE* f);
    153153DLLEXP int fflush(VirtFILE* f);
    154154DLLEXP char *fgets(char *s, int size, VirtFILE* f);
    155 DLLEXP int fprintf(VirtFILE* f,const char *format, ...);
     155DLLEXP int fprintf(VirtFILE* f, const char *format, ...);
    156156DLLEXP int fclose(VirtFILE* f);
    157157
Note: See TracChangeset for help on using the changeset viewer.