Changeset 282 for cpp/frams/virtfile


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

Formatted source

Location:
cpp/frams/virtfile
Files:
2 edited

Legend:

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

    r247 r282  
    99size_t StringFILE::Vread(void *ptr, size_t size, size_t nmemb)
    1010{
    11 int have=(int)(str.len()-pos);
    12 if (have<=0) return 0;
    13 int need=(int)(size*nmemb);
    14 if (need>have) {nmemb=have/size; need=(int)(size*nmemb);}
    15 memcpy(ptr,((const char*)str)+pos,need);
    16 pos+=need;
    17 return nmemb;
     11        int have = (int)(str.len() - pos);
     12        if (have <= 0) return 0;
     13        int need = (int)(size*nmemb);
     14        if (need > have) { nmemb = have / size; need = (int)(size*nmemb); }
     15        memcpy(ptr, ((const char*)str) + pos, need);
     16        pos += need;
     17        return nmemb;
    1818}
    1919
    2020int StringFILE::Vgetc()
    2121{
    22 if (pos >= str.len()) //...i znowu byl bug roku! :O
    23         return EOF;
    24 else
    25         return str.operator[]((int)pos++);
     22        if (pos >= str.len()) //...i znowu byl bug roku! :O
     23                return EOF;
     24        else
     25                return str.operator[]((int)pos++);
    2626}
    2727
    2828char *StringFILE::Vgets(char *s, int size)
    2929{
    30 int have=str.len()-(int)pos;
    31 if (have<=0) return 0;
    32 if (size<0) size=0;
    33 if (have>size) have=size-1;
    34 const char* src=((const char*)str)+pos;
    35 char *dest=s;
    36 while(have-- > 0)
     30        int have = str.len() - (int)pos;
     31        if (have <= 0) return 0;
     32        if (size < 0) size = 0;
     33        if (have > size) have = size - 1;
     34        const char* src = ((const char*)str) + pos;
     35        char *dest = s;
     36        while (have-- > 0)
    3737        {
    38         *(dest++) = *(src++); pos++;
    39         if (dest[-1]=='\n') break;
     38                *(dest++) = *(src++); pos++;
     39                if (dest[-1] == '\n') break;
    4040        }
    41 *dest=0;
    42 return s;
     41        *dest = 0;
     42        return s;
    4343}
    4444
    4545int StringFILE::Vseek(long offset, int whence)
    4646{
    47 switch(whence)
     47        switch (whence)
    4848        {
    49         case SEEK_SET: pos=offset; break;
    50         case SEEK_CUR: pos+=offset; break;
    51         case SEEK_END: pos=str.len()-offset; break;
     49        case SEEK_SET: pos = offset; break;
     50        case SEEK_CUR: pos += offset; break;
     51        case SEEK_END: pos = str.len() - offset; break;
    5252        default: return EINVAL;
    5353        }
    54 if (pos < 0) pos=0; else if (pos>str.len()) pos=str.len();
    55 return 0;
     54        if (pos < 0) pos = 0; else if (pos>str.len()) pos = str.len();
     55        return 0;
    5656}
    5757
    58 const char StringFileSystem::PREFIX[]="string://";
     58const char StringFileSystem::PREFIX[] = "string://";
    5959
    6060bool StringFileSystem::isStringPath(const char* path)
    6161{
    62 return !strncmp(path,PREFIX,sizeof(PREFIX)-1);
     62        return !strncmp(path, PREFIX, sizeof(PREFIX) - 1);
    6363}
    6464
    65 VirtFILE *StringFileSystem::Vfopen(const char* path,const char*mode)
     65VirtFILE *StringFileSystem::Vfopen(const char* path, const char*mode)
    6666{
    67 if ((*mode=='r') && isStringPath(path))
     67        if ((*mode == 'r') && isStringPath(path))
    6868        {
    69         return new StringFILE2(SString(path+sizeof(PREFIX)-1));
     69                return new StringFILE2(SString(path + sizeof(PREFIX) - 1));
    7070        }
    71 return (chain!=NULL) ? chain->Vfopen(path,mode) : NULL;
     71        return (chain != NULL) ? chain->Vfopen(path, mode) : NULL;
    7272}
    7373
    7474int StringFileSystem::Vfexists(const char* path)
    75 { return (chain!=NULL) ? chain->Vfexists(path) : 0; }
     75{
     76        return (chain != NULL) ? chain->Vfexists(path) : 0;
     77}
    7678
    7779VirtDIR *StringFileSystem::Vopendir(const char* path)
    78 { return (chain!=NULL) ? chain->Vopendir(path) : NULL; }
     80{
     81        return (chain != NULL) ? chain->Vopendir(path) : NULL;
     82}
  • cpp/frams/virtfile/stringfile.h

    r247 r282  
    99#include <frams/util/sstring.h>
    1010
    11 class StringFILE: public VirtFILE
     11class StringFILE : public VirtFILE
    1212{
    13   protected:
    14 SString& str;
    15 long pos;
    16   public:
    17 StringFILE(SString& s):str(s),pos(0) {}
    18 size_t Vread(void *ptr, size_t size, size_t nmemb);
    19 size_t Vwrite(const void *ptr, size_t size, size_t nmemb) {str.append((const char*)ptr,(int)(size*nmemb)); return size*nmemb;}
    20 int Veof() {return pos>=str.len();}
    21 int Vputc(int c) {str+=(char)c; return c;}
    22 int Vputs(const char *s) {str.append(s,(int)strlen(s)); return 0;}
    23 int Vgetc();
    24 char *Vgets(char *s, int size);
    25 int Vseek(long offset, int whence);
    26 long Vtell() {return pos;}
    27 int Vflush() {return 0;}
     13protected:
     14        SString& str;
     15        long pos;
     16public:
     17        StringFILE(SString& s) :str(s), pos(0) {}
     18        size_t Vread(void *ptr, size_t size, size_t nmemb);
     19        size_t Vwrite(const void *ptr, size_t size, size_t nmemb) { str.append((const char*)ptr, (int)(size*nmemb)); return size*nmemb; }
     20        int Veof() { return pos >= str.len(); }
     21        int Vputc(int c) { str += (char)c; return c; }
     22        int Vputs(const char *s) { str.append(s, (int)strlen(s)); return 0; }
     23        int Vgetc();
     24        char *Vgets(char *s, int size);
     25        int Vseek(long offset, int whence);
     26        long Vtell() { return pos; }
     27        int Vflush() { return 0; }
    2828};
    2929
    3030/** this version owns the string object */
    31 class StringFILE2: public StringFILE
     31class StringFILE2 : public StringFILE
    3232{
    33 SString string;
    34   public:
    35 StringFILE2(const SString& s):StringFILE(string),string(s) {}
    36 StringFILE2():StringFILE(string) {}
    37 const SString& getString() {return string;}
     33        SString string;
     34public:
     35        StringFILE2(const SString& s) :StringFILE(string), string(s) {}
     36        StringFILE2() :StringFILE(string) {}
     37        const SString& getString() { return string; }
    3838};
    3939
    40 class StringFileSystem: public VirtFileSystem
     40class StringFileSystem : public VirtFileSystem
    4141{
    42   public:
    43 VirtFileSystem *chain;
    44 StringFileSystem(VirtFileSystem *_chain=NULL):chain(_chain) {}
    45 VirtFILE *Vfopen(const char* path,const char*mode);
    46 int Vfexists(const char* path);
    47 VirtDIR *Vopendir(const char* path);
    48 static const char PREFIX[];
    49 static bool isStringPath(const char* path);
     42public:
     43        VirtFileSystem *chain;
     44        StringFileSystem(VirtFileSystem *_chain = NULL) :chain(_chain) {}
     45        VirtFILE *Vfopen(const char* path, const char*mode);
     46        int Vfexists(const char* path);
     47        VirtDIR *Vopendir(const char* path);
     48        static const char PREFIX[];
     49        static bool isStringPath(const char* path);
    5050};
    5151
Note: See TracChangeset for help on using the changeset viewer.