[121] | 1 | // This file is a part of the Framsticks GDK. |
---|
| 2 | // Copyright (C) 2002-2014 Maciej Komosinski and Szymon Ulatowski. See LICENSE.txt for details. |
---|
| 3 | // Refer to http://www.framsticks.com/ for further information. |
---|
| 4 | |
---|
[109] | 5 | #include "stringfile.h" |
---|
| 6 | #include <stdio.h> |
---|
| 7 | #include <errno.h> //EINVAL |
---|
| 8 | |
---|
| 9 | int StringFILE::Vread(void *ptr, size_t size, size_t nmemb) |
---|
| 10 | { |
---|
| 11 | int have=str.len()-pos; |
---|
| 12 | if (have<=0) return 0; |
---|
| 13 | int need=size*nmemb; |
---|
| 14 | if (need>have) {nmemb=have/size; need=size*nmemb;} |
---|
| 15 | memcpy(ptr,((const char*)str)+pos,need); |
---|
| 16 | pos+=need; |
---|
| 17 | return nmemb; |
---|
| 18 | } |
---|
| 19 | |
---|
| 20 | int StringFILE::Vgetc() |
---|
| 21 | { |
---|
| 22 | if (pos >= str.len()) //...i znowu byl bug roku! :O |
---|
| 23 | return EOF; |
---|
| 24 | else |
---|
| 25 | return str[pos++]; |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | char *StringFILE::Vgets(char *s, int size) |
---|
| 29 | { |
---|
| 30 | int have=str.len()-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 | { |
---|
| 38 | *(dest++) = *(src++); pos++; |
---|
| 39 | if (dest[-1]=='\n') break; |
---|
| 40 | } |
---|
| 41 | *dest=0; |
---|
| 42 | return s; |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | int StringFILE::Vprintf(const char *format, va_list args) |
---|
| 46 | { |
---|
| 47 | char *buf=str.directAppend(10000); |
---|
| 48 | vsnprintf(buf,10000,format,args); |
---|
| 49 | int n=strlen(buf); // todo: strlen moze wyjsc poza siebie jezeli printf nie da 0S |
---|
| 50 | str.endAppend(n); |
---|
| 51 | return n; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | int StringFILE::Vseek(long offset, int whence) |
---|
| 55 | { |
---|
| 56 | switch(whence) |
---|
| 57 | { |
---|
| 58 | case SEEK_SET: pos=offset; break; |
---|
| 59 | case SEEK_CUR: pos+=offset; break; |
---|
| 60 | case SEEK_END: pos=str.len()-offset; break; |
---|
| 61 | default: return EINVAL; |
---|
| 62 | } |
---|
| 63 | if (pos < 0) pos=0; else if (pos>str.len()) pos=str.len(); |
---|
| 64 | return 0; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | const char StringFileSystem::PREFIX[]="string://"; |
---|
| 68 | |
---|
| 69 | bool StringFileSystem::isStringPath(const char* path) |
---|
| 70 | { |
---|
| 71 | return !strncmp(path,PREFIX,sizeof(PREFIX)-1); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | VirtFILE *StringFileSystem::Vfopen(const char* path,const char*mode) |
---|
| 75 | { |
---|
| 76 | if ((*mode=='r') && isStringPath(path)) |
---|
| 77 | { |
---|
| 78 | return new StringFILE2(SString(path+sizeof(PREFIX)-1)); |
---|
| 79 | } |
---|
| 80 | return chain->Vfopen(path,mode); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | int StringFileSystem::Vfexists(const char* path) |
---|
| 84 | { return chain->Vfexists(path); } |
---|
| 85 | |
---|
| 86 | VirtDIR *StringFileSystem::Vopendir(const char* path) |
---|
| 87 | { return chain->Vopendir(path); } |
---|