source: cpp/frams/virtfile/stringfile.cpp @ 109

Last change on this file since 109 was 109, checked in by sz, 10 years ago

source reorganization (see README)
new feature added: part/joint shapes (see frams/_demos/part_shapes.cpp)

  • Property svn:eol-style set to native
File size: 1.8 KB
Line 
1#include "stringfile.h"
2#include <stdio.h>
3#include <errno.h> //EINVAL
4
5int StringFILE::Vread(void *ptr, size_t size, size_t nmemb)
6{
7int have=str.len()-pos;
8if (have<=0) return 0;
9int need=size*nmemb;
10if (need>have) {nmemb=have/size; need=size*nmemb;}
11memcpy(ptr,((const char*)str)+pos,need);
12pos+=need;
13return nmemb;
14}
15
16int StringFILE::Vgetc()
17{
18if (pos >= str.len()) //...i znowu byl bug roku! :O
19        return EOF;
20else
21        return str[pos++];
22}
23
24char *StringFILE::Vgets(char *s, int size)
25{
26int have=str.len()-pos;
27if (have<=0) return 0;
28if (size<0) size=0;
29if (have>size) have=size-1;
30const char* src=((const char*)str)+pos;
31char *dest=s;
32while(have-- > 0)
33        {
34        *(dest++) = *(src++); pos++;
35        if (dest[-1]=='\n') break;
36        }
37*dest=0;
38return s;
39}
40
41int StringFILE::Vprintf(const char *format, va_list args)
42{
43char *buf=str.directAppend(10000);
44vsnprintf(buf,10000,format,args);
45int n=strlen(buf); // todo: strlen moze wyjsc poza siebie jezeli printf nie da 0S
46str.endAppend(n);
47return n;
48}
49
50int StringFILE::Vseek(long offset, int whence)
51{
52switch(whence)
53        {
54        case SEEK_SET: pos=offset; break;
55        case SEEK_CUR: pos+=offset; break;
56        case SEEK_END: pos=str.len()-offset; break;
57        default: return EINVAL;
58        }
59if (pos < 0) pos=0; else if (pos>str.len()) pos=str.len();
60return 0;
61}
62
63const char StringFileSystem::PREFIX[]="string://";
64
65bool StringFileSystem::isStringPath(const char* path)
66{
67return !strncmp(path,PREFIX,sizeof(PREFIX)-1);
68}
69
70VirtFILE *StringFileSystem::Vfopen(const char* path,const char*mode)
71{
72if ((*mode=='r') && isStringPath(path))
73        {
74        return new StringFILE2(SString(path+sizeof(PREFIX)-1));
75        }
76return chain->Vfopen(path,mode);
77}
78
79int StringFileSystem::Vfexists(const char* path)
80{ return chain->Vfexists(path); }
81
82VirtDIR *StringFileSystem::Vopendir(const char* path)
83{ return chain->Vopendir(path); }
Note: See TracBrowser for help on using the repository browser.