blob: 5a6ab9e84e5d4d683d5a61d47351c2765f574932 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
//========================================================================
//
// InMemoryFile.h
//
// Represents a file in-memory with GNU's stdio wrappers.
// NOTE as of this writing, open() depends on the glibc 'fopencookie'
// extension and is not supported on other platforms. The
// HAVE_IN_MEMORY_FILE macro is intended to reflect whether this class is
// usable.
//
// This file is licensed under the GPLv2 or later
//
// Copyright (C) 2018, 2019 Greg Knight <lyngvi@gmail.com>
//
//========================================================================
#ifndef IN_MEMORY_FILE_H
#define IN_MEMORY_FILE_H
#include <cstdio>
#include <string>
#include <vector>
#if defined(__USE_GNU) && !defined(__ANDROID_API__)
# define HAVE_IN_MEMORY_FILE (1)
# define HAVE_IN_MEMORY_FILE_FOPENCOOKIE (1) // used internally
#endif
class InMemoryFile {
private:
size_t iohead;
std::vector<char> data;
FILE *fptr;
#ifdef HAVE_IN_MEMORY_FILE_FOPENCOOKIE
ssize_t _read(char* buf, size_t sz);
ssize_t _write(const char* buf, size_t sz);
int _seek(off64_t* offset, int whence);
#endif
public:
InMemoryFile();
public:
/* Returns a file handle for this file. This is scoped to this object
* and must be fclosed() by the caller before destruction. */
FILE* open(const char* mode);
const std::vector<char>& getBuffer() const
{ return data; }
};
#endif // IN_MEMORY_FILE_H
|