libigl v2.5.0
Loading...
Searching...
No Matches
FileMemoryStream.h
Go to the documentation of this file.
1// This file is part of libigl, a simple c++ geometry processing library.
2//
3// Copyright (C) 2020 Jérémie Dumas <jeremie.dumas@ens-lyon.org>
4// Copyright (C) 2021 Alec Jacobson <alecjacobson@gmail.com>
5//
6// This Source Code Form is subject to the terms of the Mozilla Public License
7// v. 2.0. If a copy of the MPL was not distributed with this file, You can
8// obtain one at http://mozilla.org/MPL/2.0/.
9#ifndef IGL_FILEMEMORYSTREAM_H
10#define IGL_FILEMEMORYSTREAM_H
11
12#include "igl_inline.h"
13
14#include <streambuf>
15#include <istream>
16#include <string>
17
18namespace igl {
19 struct FileMemoryBuffer : public std::streambuf
20 {
21 char *p_start{nullptr};
22 char *p_end{nullptr};
23 size_t size;
24
25 FileMemoryBuffer(char const *first_elem, size_t size)
26 : p_start(const_cast<char *>(first_elem)), p_end(p_start + size),
27 size(size)
28 {
29 setg(p_start, p_start, p_end);
30 }
31
32 pos_type seekoff(
33 off_type off,
34 std::ios_base::seekdir dir,
35 std::ios_base::openmode /*which*/) override
36 {
37 if (dir == std::ios_base::cur)
38 {
39 gbump(static_cast<int>(off));
40 }else
41 {
42 setg(p_start,(dir==std::ios_base::beg ? p_start : p_end) + off,p_end);
43 }
44 return gptr() - p_start;
45 }
46
47 pos_type seekpos(pos_type pos, std::ios_base::openmode which) override
48 {
49 return seekoff(pos, std::ios_base::beg, which);
50 }
51 };
52
54 struct FileMemoryStream : virtual FileMemoryBuffer, public std::istream
55 {
56 FileMemoryStream( char const *first_elem, size_t size)
57 : FileMemoryBuffer(first_elem, size),
58 std::istream( static_cast<std::streambuf *>(this))
59 {}
60 };
61}
62
63#endif
64
Definition AABB.h:17
Definition FileMemoryStream.h:20
size_t size
Definition FileMemoryStream.h:23
pos_type seekoff(off_type off, std::ios_base::seekdir dir, std::ios_base::openmode) override
Definition FileMemoryStream.h:32
pos_type seekpos(pos_type pos, std::ios_base::openmode which) override
Definition FileMemoryStream.h:47
char * p_start
Definition FileMemoryStream.h:21
char * p_end
Definition FileMemoryStream.h:22
FileMemoryBuffer(char const *first_elem, size_t size)
Definition FileMemoryStream.h:25
Class to convert a FILE * to an std::istream.
Definition FileMemoryStream.h:55
FileMemoryStream(char const *first_elem, size_t size)
Definition FileMemoryStream.h:56