Changeset 282
- Timestamp:
- 12/27/14 01:02:21 (10 years ago)
- Location:
- cpp/frams/virtfile
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/virtfile/stringfile.cpp
r247 r282 9 9 size_t StringFILE::Vread(void *ptr, size_t size, size_t nmemb) 10 10 { 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; 18 18 } 19 19 20 20 int StringFILE::Vgetc() 21 21 { 22 if (pos >= str.len()) //...i znowu byl bug roku! :O23 return EOF;24 else25 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++); 26 26 } 27 27 28 28 char *StringFILE::Vgets(char *s, int size) 29 29 { 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) 37 37 { 38 *(dest++) = *(src++); pos++;39 if (dest[-1]=='\n') break;38 *(dest++) = *(src++); pos++; 39 if (dest[-1] == '\n') break; 40 40 } 41 *dest=0;42 return s;41 *dest = 0; 42 return s; 43 43 } 44 44 45 45 int StringFILE::Vseek(long offset, int whence) 46 46 { 47 switch(whence)47 switch (whence) 48 48 { 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; 52 52 default: return EINVAL; 53 53 } 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; 56 56 } 57 57 58 const char StringFileSystem::PREFIX[] ="string://";58 const char StringFileSystem::PREFIX[] = "string://"; 59 59 60 60 bool StringFileSystem::isStringPath(const char* path) 61 61 { 62 return !strncmp(path,PREFIX,sizeof(PREFIX)-1);62 return !strncmp(path, PREFIX, sizeof(PREFIX) - 1); 63 63 } 64 64 65 VirtFILE *StringFileSystem::Vfopen(const char* path, const char*mode)65 VirtFILE *StringFileSystem::Vfopen(const char* path, const char*mode) 66 66 { 67 if ((*mode=='r') && isStringPath(path))67 if ((*mode == 'r') && isStringPath(path)) 68 68 { 69 return new StringFILE2(SString(path+sizeof(PREFIX)-1));69 return new StringFILE2(SString(path + sizeof(PREFIX) - 1)); 70 70 } 71 return (chain!=NULL) ? chain->Vfopen(path,mode) : NULL;71 return (chain != NULL) ? chain->Vfopen(path, mode) : NULL; 72 72 } 73 73 74 74 int StringFileSystem::Vfexists(const char* path) 75 { return (chain!=NULL) ? chain->Vfexists(path) : 0; } 75 { 76 return (chain != NULL) ? chain->Vfexists(path) : 0; 77 } 76 78 77 79 VirtDIR *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 9 9 #include <frams/util/sstring.h> 10 10 11 class StringFILE : public VirtFILE11 class StringFILE : public VirtFILE 12 12 { 13 14 SString& str;15 long pos;16 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;}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; } 28 28 }; 29 29 30 30 /** this version owns the string object */ 31 class StringFILE2 : public StringFILE31 class StringFILE2 : public StringFILE 32 32 { 33 SString string;34 35 StringFILE2(const SString& s):StringFILE(string),string(s) {}36 StringFILE2():StringFILE(string) {}37 const SString& getString() {return string;}33 SString string; 34 public: 35 StringFILE2(const SString& s) :StringFILE(string), string(s) {} 36 StringFILE2() :StringFILE(string) {} 37 const SString& getString() { return string; } 38 38 }; 39 39 40 class StringFileSystem : public VirtFileSystem40 class StringFileSystem : public VirtFileSystem 41 41 { 42 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);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); 50 50 }; 51 51
Note: See TracChangeset
for help on using the changeset viewer.