-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileMemoryStream.h
More file actions
64 lines (55 loc) · 1.69 KB
/
FileMemoryStream.h
File metadata and controls
64 lines (55 loc) · 1.69 KB
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
54
55
56
57
58
59
60
61
62
63
// This file is part of libigl, a simple c++ geometry processing library.
//
// Copyright (C) 2020 Jérémie Dumas <jeremie.dumas@ens-lyon.org>
// Copyright (C) 2021 Alec Jacobson <alecjacobson@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public License
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
#ifndef IGL_FILEMEMORYSTREAM_H
#define IGL_FILEMEMORYSTREAM_H
#include "igl_inline.h"
#include <streambuf>
#include <istream>
#include <string>
namespace igl {
struct FileMemoryBuffer : public std::streambuf
{
char *p_start{nullptr};
char *p_end{nullptr};
size_t size;
FileMemoryBuffer(char const *first_elem, size_t size)
: p_start(const_cast<char *>(first_elem)), p_end(p_start + size),
size(size)
{
setg(p_start, p_start, p_end);
}
pos_type seekoff(
off_type off,
std::ios_base::seekdir dir,
std::ios_base::openmode /*which*/) override
{
if (dir == std::ios_base::cur)
{
gbump(static_cast<int>(off));
}else
{
setg(p_start,(dir==std::ios_base::beg ? p_start : p_end) + off,p_end);
}
return gptr() - p_start;
}
pos_type seekpos(pos_type pos, std::ios_base::openmode which) override
{
return seekoff(pos, std::ios_base::beg, which);
}
};
/// Class to convert a FILE * to an std::istream
struct FileMemoryStream : virtual FileMemoryBuffer, public std::istream
{
FileMemoryStream( char const *first_elem, size_t size)
: FileMemoryBuffer(first_elem, size),
std::istream( static_cast<std::streambuf *>(this))
{}
};
}
#endif