1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
3 | // See LICENSE.txt for details. |
---|
4 | |
---|
5 | #include "stringfile.h" |
---|
6 | #include <stdio.h> |
---|
7 | #include <errno.h> //EINVAL |
---|
8 | |
---|
9 | size_t StringFILE::Vread(void *ptr, size_t size, size_t nmemb) |
---|
10 | { |
---|
11 | int have = int(str.size()) - 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, str.c_str() + pos, need); |
---|
16 | pos += need; |
---|
17 | return nmemb; |
---|
18 | } |
---|
19 | |
---|
20 | int StringFILE::Vgetc() |
---|
21 | { |
---|
22 | if (pos >= int(str.size())) |
---|
23 | return EOF; |
---|
24 | else |
---|
25 | return str.operator[]((int)pos++); |
---|
26 | } |
---|
27 | |
---|
28 | char *StringFILE::Vgets(char *s, int size) |
---|
29 | { |
---|
30 | int have = int(str.size()) - pos; |
---|
31 | if (have <= 0) return 0; |
---|
32 | if (size < 0) size = 0; |
---|
33 | if (have > size) have = size - 1; |
---|
34 | const char* src = str.c_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::Vseek(long offset, int whence) |
---|
46 | { |
---|
47 | switch (whence) |
---|
48 | { |
---|
49 | case SEEK_SET: pos = offset; break; |
---|
50 | case SEEK_CUR: pos += offset; break; |
---|
51 | case SEEK_END: pos = int(str.size()) - offset; break; |
---|
52 | default: return EINVAL; |
---|
53 | } |
---|
54 | if (pos < 0) pos = 0; else if (pos > int(str.size())) pos = int(str.size()); |
---|
55 | return 0; |
---|
56 | } |
---|
57 | |
---|
58 | const char StringFileSystem::PREFIX[] = "string://"; |
---|
59 | |
---|
60 | bool StringFileSystem::isStringPath(const char* path) |
---|
61 | { |
---|
62 | return !strncmp(path, PREFIX, sizeof(PREFIX) - 1); |
---|
63 | } |
---|
64 | |
---|
65 | VirtFILE *StringFileSystem::Vfopen(const char* path, const char*mode) |
---|
66 | { |
---|
67 | if ((*mode == 'r') && isStringPath(path)) |
---|
68 | { |
---|
69 | return new StringFILE2(string(path + sizeof(PREFIX) - 1)); |
---|
70 | } |
---|
71 | return (chain != NULL) ? chain->Vfopen(path, mode) : NULL; |
---|
72 | } |
---|