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

Last change on this file since 350 was 348, checked in by Maciej Komosinski, 10 years ago
  • explicit c_str() in SString instead of (const char*) cast
  • genetic converters and GenMan? are now thread-local which enables multi-threaded simulator separation
  • Property svn:eol-style set to native
File size: 1.7 KB
RevLine 
[286]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.
[121]4
[109]5#include "stringfile.h"
6#include <stdio.h>
7#include <errno.h> //EINVAL
8
[247]9size_t StringFILE::Vread(void *ptr, size_t size, size_t nmemb)
[109]10{
[282]11        int have = (int)(str.len() - pos);
12        if (have <= 0) return 0;
13        int need = (int)(size*nmemb);
14        if (need > have) { nmemb = have / size; need = (int)(size*nmemb); }
[348]15        memcpy(ptr, str.c_str() + pos, need);
[282]16        pos += need;
17        return nmemb;
[109]18}
19
20int StringFILE::Vgetc()
21{
[282]22        if (pos >= str.len()) //...i znowu byl bug roku! :O
23                return EOF;
24        else
25                return str.operator[]((int)pos++);
[109]26}
27
28char *StringFILE::Vgets(char *s, int size)
29{
[282]30        int have = str.len() - (int)pos;
31        if (have <= 0) return 0;
32        if (size < 0) size = 0;
33        if (have > size) have = size - 1;
[348]34        const char* src = str.c_str() + pos;
[282]35        char *dest = s;
36        while (have-- > 0)
[109]37        {
[282]38                *(dest++) = *(src++); pos++;
39                if (dest[-1] == '\n') break;
[109]40        }
[282]41        *dest = 0;
42        return s;
[109]43}
44
45int StringFILE::Vseek(long offset, int whence)
46{
[282]47        switch (whence)
[109]48        {
[282]49        case SEEK_SET: pos = offset; break;
50        case SEEK_CUR: pos += offset; break;
51        case SEEK_END: pos = str.len() - offset; break;
[109]52        default: return EINVAL;
53        }
[282]54        if (pos < 0) pos = 0; else if (pos>str.len()) pos = str.len();
55        return 0;
[109]56}
57
[282]58const char StringFileSystem::PREFIX[] = "string://";
[109]59
60bool StringFileSystem::isStringPath(const char* path)
61{
[282]62        return !strncmp(path, PREFIX, sizeof(PREFIX) - 1);
[109]63}
64
[282]65VirtFILE *StringFileSystem::Vfopen(const char* path, const char*mode)
[109]66{
[282]67        if ((*mode == 'r') && isStringPath(path))
[109]68        {
[282]69                return new StringFILE2(SString(path + sizeof(PREFIX) - 1));
[109]70        }
[282]71        return (chain != NULL) ? chain->Vfopen(path, mode) : NULL;
[109]72}
Note: See TracBrowser for help on using the repository browser.