Changeset 206 for cpp/frams


Ignore:
Timestamp:
04/03/14 01:24:30 (11 years ago)
Author:
Maciej Komosinski
Message:

VirtFILE base class provides Vprintf, Vgetc, Vputc, Vrewind, so that subclasses do not have to implement these functions

Location:
cpp/frams/virtfile
Files:
4 edited

Legend:

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

    r197 r206  
    1111{
    1212        //printFM("Vfopen %s %s",path,mode);
     13#ifdef USE_MFILE
     14        MFILE *f=mfopen(path,mode);
     15#else
    1316        FILE *f=fopen(path,mode);
     17#endif
    1418        //printFM("%p",f);
    1519        if (f) return new StdioFILE(f,path); else return 0;
  • cpp/frams/virtfile/stdiofile.h

    r197 r206  
    88#include "virtfile.h"
    99#include <frams/util/sstring.h>
     10#ifdef USE_MFILE
     11#include <common/nonstd_stdio.h>
     12#else
    1013#include <stdio.h>
     14#endif
    1115#include <common/nonstd_dir.h>
    1216
     
    1923};
    2024
     25#ifdef USE_MFILE
     26class StdioFILE: public VirtFILE
     27{
     28  protected:
     29MFILE *file;
     30SString path;
     31  public:
     32StdioFILE(MFILE *f) {file=f;}
     33StdioFILE(MFILE *f,const SString& p) {file=f;path=p;}
     34static void setStdio();
     35int Vread(void *ptr, size_t size, size_t nmemb) {return mfread(ptr,size,nmemb,file);}
     36int Vwrite(const void *ptr, size_t size, size_t nmemb) {return mfwrite(ptr,size,nmemb,file);}
     37int Veof() {return mfeof(file);}
     38int Vputs(const char *s) {return mfputs(s,file);}
     39char *Vgets(char *s, int size) {return mfgets(s,size,file);}
     40int Vseek(long offset, int whence) {return mfseek(file,offset,whence);}
     41int Vtell() {return mftell(file);}
     42int Vflush() {return 0;/*NOT IMPLEMENTED!*/;}
     43const char* VgetPath() {return path;}
     44
     45~StdioFILE() {if (file) fclose(file);}
     46};
     47#else
    2148class StdioFILE: public VirtFILE
    2249{
     
    4471~StdioFILE() {if (file) fclose(file);}
    4572};
     73#endif
    4674
    4775class StdioDIR: public VirtDIR
     
    5785{
    5886  public:
     87#ifdef USE_MFILE
     88StdioFILEDontClose(MFILE *f):StdioFILE(f) {}
     89#else
    5990StdioFILEDontClose(FILE *f):StdioFILE(f) {}
     91#endif
    6092~StdioFILEDontClose() {file=0;}
    6193};
  • cpp/frams/virtfile/virtfile.cpp

    r197 r206  
    44
    55#include "virtfile.h"
     6#include <common/stl-util.h>
    67
    78VirtFILE *VirtFILE::Vstdin=0;
     
    3031
    3132void VirtFILE::selectFileSystem(VirtFileSystem *s) {vfs=s;}
     33
     34int VirtFILE::Vprintf(const char *format, va_list args)
     35{
     36string s=ssprintf_va(format,args);
     37return Vwrite(s.c_str(),1,s.size());
     38}
    3239
    3340int VirtFILE::printf(const char *format, ...)
  • cpp/frams/virtfile/virtfile.h

    r197 r206  
    2626virtual int Vwrite(const void *ptr, size_t size, size_t nmemb)=0;
    2727virtual int Veof()=0;
    28 virtual int Vputc(int c)=0;
     28virtual int Vputc(int c) {unsigned char data=c; return (Vwrite(&data,1,1)==1)?data:EOF;}
    2929virtual int Vputs(const char *s)=0;
    30 virtual int Vgetc()=0;
     30virtual int Vgetc() {unsigned char data; if (Vread(&data,1,1)==1) return data; else return EOF;}
    3131virtual int Vseek(long offset, int whence)=0;
    3232virtual int Vtell()=0;
    33 virtual void Vrewind()=0;
     33virtual void Vrewind() {Vseek(0,SEEK_SET);}
    3434virtual int Vflush()=0;
    3535virtual char *Vgets(char *s, int size)=0;
    36 virtual int Vprintf(const char *format, va_list args)=0;
     36virtual int Vprintf(const char *format, va_list args);
    3737int printf(const char *format, ...);
    3838virtual const char *VgetPath() {return 0;} // 0=unspecified path
Note: See TracChangeset for help on using the changeset viewer.