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

Last change on this file since 197 was 197, checked in by Maciej Komosinski, 7 years ago

GDK used by developers since 1999, distributed on the web since 2002

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