File: mptBufferIO.h

package info (click to toggle)
libopenmpt 0.4.3-1+deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,724 kB
  • sloc: cpp: 99,820; sh: 4,503; ansic: 3,449; makefile: 480
file content (177 lines) | stat: -rw-r--r-- 4,584 bytes parent folder | download | duplicates (2)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * mptBufferIO.h
 * -------------
 * Purpose: A wrapper around std::stringstream, fixing MSVC tell/seek problems with empty streams.
 * Notes  : You should only ever use these wrappers instead of plain std::stringstream classes.
 * Authors: OpenMPT Devs
 * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
 */

#pragma once

#include "BuildSettings.h"

#include <ios>
#include <istream>
#include <ostream>
#include <sstream>
#include <streambuf>

OPENMPT_NAMESPACE_BEGIN


// MSVC std::stringbuf (and thereby std::ostringstream, std::istringstream and
// std::stringstream) fail seekpos() and seekoff() when the stringbuf is
// currently empty.
// seekpos() and seekoff() can get called via tell*() or seek*() iostream
// members. seekoff() (and thereby tell*()), but not seekpos(), has been fixed
// from VS2010 onwards to handle this specific case and changed to not fail
// when the stringbuf is empty.
// Work-around strategy:
// As re-implementing or duplicating the whole std::stringbuf semantics would be
// rather convoluted, we make use of the knowledge of specific inner workings of
// the MSVC implementation here and just fix-up where it causes problems. This
// keeps the additional code at a minimum size.


namespace mpt
{

#ifdef MPT_COMPILER_QUIRK_MSVC_STRINGSTREAM

class stringbuf
	: public std::stringbuf
{
public:
	stringbuf(std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
		: std::stringbuf(mode)
	{
		return;
	}
	stringbuf(const std::string &str, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
		: std::stringbuf(str, mode)
	{
		return;
	}
public:
	virtual pos_type seekoff(off_type off, std::ios_base::seekdir way, std::ios_base::openmode which = std::ios_base::in | std::ios_base::out)
	{
		pos_type result = std::stringbuf::seekoff(off, way, which);
		if(result == pos_type(-1))
		{
			if((which & std::ios_base::in) || (which & std::ios_base::out))
			{
				if(off == 0)
				{
					result = 0;
				}
			}
		}
		return result;
	}
	virtual pos_type seekpos(pos_type ptr, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
	{
		pos_type result = std::stringbuf::seekpos(ptr, mode);
		if(result == pos_type(-1))
		{
			if((mode & std::ios_base::in) || (mode & std::ios_base::out))
			{
				if(static_cast<std::streamoff>(ptr) == 0)
				{
					result = 0;
				}
			}
		}
		return result;
	}
};

class istringstream
	: public std::basic_istream<char>
{
private:
	mpt::stringbuf buf;
public:
	istringstream(std::ios_base::openmode mode = std::ios_base::in)
		: std::basic_istream<char>(&buf)
		, buf(mode | std::ios_base::in)
	{
	}
	istringstream(const std::string &str, std::ios_base::openmode mode = std::ios_base::in)
		: std::basic_istream<char>(&buf)
		, buf(str, mode | std::ios_base::in)
	{
	}
	~istringstream()
	{
	}
public:
	mpt::stringbuf *rdbuf() const { return const_cast<mpt::stringbuf*>(&buf); }
	std::string str() const { return buf.str(); }
	void str(const std::string &str) { buf.str(str); } 
};

class ostringstream
	: public std::basic_ostream<char>
{
private:
	mpt::stringbuf buf;
public:
	ostringstream(std::ios_base::openmode mode = std::ios_base::out)
		: std::basic_ostream<char>(&buf)
		, buf(mode | std::ios_base::out)
	{
	}
	ostringstream(const std::string &str, std::ios_base::openmode mode = std::ios_base::out)
		: std::basic_ostream<char>(&buf)
		, buf(str, mode | std::ios_base::out)
	{
	}
	~ostringstream()
	{
	}
public:
	mpt::stringbuf *rdbuf() const { return const_cast<mpt::stringbuf*>(&buf); }
	std::string str() const { return buf.str(); }
	void str(const std::string &str) { buf.str(str); } 
};

class stringstream
	: public std::basic_iostream<char>
{
private:
	mpt::stringbuf buf;
public:
	stringstream(std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
		: std::basic_iostream<char>(&buf)
		, buf(mode | std::ios_base::in | std::ios_base::out)
	{
	}
	stringstream(const std::string &str, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
		: std::basic_iostream<char>(&buf)
		, buf(str, mode | std::ios_base::in | std::ios_base::out)
	{
	}
	~stringstream()
	{
	}
public:
	mpt::stringbuf *rdbuf() const { return const_cast<mpt::stringbuf*>(&buf); }
	std::string str() const { return buf.str(); }
	void str(const std::string &str) { buf.str(str); } 
};

#else

typedef std::stringbuf stringbuf;
typedef std::istringstream istringstream;
typedef std::ostringstream ostringstream;
typedef std::stringstream stringstream;

#endif

} // namespace mpt


OPENMPT_NAMESPACE_END