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

Last change on this file since 207 was 207, checked in by Maciej Komosinski, 10 years ago

StringFILE's Vprintf can now handle strings of any length

  • Property svn:eol-style set to native
File size: 1.8 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::Vseek(long offset, int whence)
46{
47switch(whence)
48        {
49        case SEEK_SET: pos=offset; break;
50        case SEEK_CUR: pos+=offset; break;
51        case SEEK_END: pos=str.len()-offset; break;
52        default: return EINVAL;
53        }
54if (pos < 0) pos=0; else if (pos>str.len()) pos=str.len();
55return 0;
56}
57
58const char StringFileSystem::PREFIX[]="string://";
59
60bool StringFileSystem::isStringPath(const char* path)
61{
62return !strncmp(path,PREFIX,sizeof(PREFIX)-1);
63}
64
65VirtFILE *StringFileSystem::Vfopen(const char* path,const char*mode)
66{
67if ((*mode=='r') && isStringPath(path))
68        {
69        return new StringFILE2(SString(path+sizeof(PREFIX)-1));
70        }
71return (chain!=NULL) ? chain->Vfopen(path,mode) : NULL;
72}
73
74int StringFileSystem::Vfexists(const char* path)
75{ return (chain!=NULL) ? chain->Vfexists(path) : 0; }
76
77VirtDIR *StringFileSystem::Vopendir(const char* path)
78{ return (chain!=NULL) ? chain->Vopendir(path) : NULL; }
Note: See TracBrowser for help on using the repository browser.