Loading...
Searching...
No Matches
pstream.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2003-2023, John Wiegley. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of New Artisans LLC nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
42#pragma once
43
44//#include <istream>
45//#include <streambuf>
46
47namespace ledger {
48
49class ptristream : public std::istream
50{
51 class ptrinbuf : public std::streambuf
52 {
53 ptrinbuf(const ptrinbuf&);
54 ptrinbuf& operator=(const ptrinbuf&);
55
56 protected:
57 char * ptr;
58 std::size_t len;
59
60 public:
61 ptrinbuf(char * _ptr, std::size_t _len) : ptr(_ptr), len(_len) {
62 if (*ptr && len == 0)
63 len = std::strlen(ptr);
64
65 setg(ptr, // beginning of putback area
66 ptr, // read position
67 ptr+len); // end position
68
69 TRACE_CTOR(ptrinbuf, "char *, std::size_t");
70 }
71 ~ptrinbuf() throw() {
72 TRACE_DTOR(ptrinbuf);
73 }
74
75 protected:
76 virtual int_type underflow() {
77 // is read position before end of buffer?
78 if (gptr() < egptr())
79 return traits_type::to_int_type(*gptr());
80 else
81 return EOF;
82 }
83
84 virtual pos_type seekoff(off_type off, ios_base::seekdir way,
85 ios_base::openmode)
86 {
87 // cast to avoid gcc '-Wswitch' warning
88 // as ios_base::beg/cur/end are not necessarily values of 'way' enum type ios_base::seekdir
89 // based on https://svn.boost.org/trac/boost/ticket/7644
90 switch (static_cast<int>(way)) {
91 case std::ios::cur:
92 setg(ptr, gptr()+off, ptr+len);
93 break;
94 case std::ios::beg:
95 setg(ptr, ptr+off, ptr+len);
96 break;
97 case std::ios::end:
98 setg(ptr, egptr()+off, ptr+len);
99 break;
100 }
101 return pos_type(gptr() - ptr);
102 }
103 };
104
105protected:
106 ptrinbuf buf;
107
108public:
109 ptristream(char * ptr, std::size_t len = 0)
110 : std::istream(0), buf(ptr, len) {
111 rdbuf(&buf);
112 }
113};
114
115} // namespace ledger
#define TRACE_DTOR(cls)
Definition utils.h:144
#define TRACE_CTOR(cls, args)
Definition utils.h:143
T & downcast(U &object)
Definition utils.h:468
ptristream(char *ptr, std::size_t len=0)
Definition pstream.h:109