File: GrcBinaryStream.h

package info (click to toggle)
grcompiler 5.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 24,020 kB
  • sloc: cpp: 48,200; ansic: 7,670; sh: 4,427; makefile: 197; xml: 190; perl: 127; sed: 21
file content (157 lines) | stat: -rw-r--r-- 4,314 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
/*--------------------------------------------------------------------*//*:Ignore this sentence.
Copyright (C) 1999, 2001 SIL International. All rights reserved.

Distributable under the terms of either the Common Public License or the
GNU Lesser General Public License, as specified in the LICENSING.txt file.

File: GrcBinaryStream.h
Responsibility: Sharon Correll
Last reviewed: Not yet.

Description:
-------------------------------------------------------------------------------*//*:End Ignore*/
#ifdef _MSC_VER
#pragma once
#endif
#ifndef GRC_BINSTRM_INCLUDED
#define GRC_BINSTRM_INCLUDED

#include <string>
#include <sstream>
using std::fstream;

/*----------------------------------------------------------------------------------------------
Class: GrcBinaryStream
Description: Our stream for writing to the TrueType font.
Hungarian: bstrm
----------------------------------------------------------------------------------------------*/


class GrcBinaryStream : public fstream
{
public:
	// Write a byte to the output stream.
    inline
    static void write_8bits_be(std::ostream & os, uint8_t x) {
        os.write(reinterpret_cast<char const *>(&x), sizeof x);
    }

    static void write_16bits_be(std::ostream & os, uint16_t x);

    static void write_32bits_be(std::ostream & os, uint32_t x);
 
	GrcBinaryStream(const char * stFileName)
		: fstream(stFileName, std::ios::binary | std::ios::out | std::ios::in | std::ios::trunc)
	{
	}

	~GrcBinaryStream()
	{
	}

public:
    template<typename T>
    void WriteByte(T iOutput) { write_8bits_be(*this, static_cast<uint8_t>(iOutput)); }

    template<typename T> 
    void WriteShort(T iOutput) { write_16bits_be(*this, static_cast<uint16_t>(iOutput)); }

    template<typename T>
    void WriteInt(T iOutput) { write_32bits_be(*this, static_cast<uint32_t>(iOutput)); }

    void Write(void * pbTable, size_t cbSize)
	{
		write(static_cast<char *>(pbTable), cbSize);
	}

	offset_t Position()
	{
		return offset_t(tellp());
	}

	void SetPosition(offset_t lPos)
	{
		seekp(offset_t(lPos));
	}

	offset_t SeekPadLong(offset_t ibOffset);

	void Close(void)
	{
		close();
	}
};


/*----------------------------------------------------------------------------------------------
Class: GrcSubStream
Description: A substream that will eventually be output on the main stream.
Hungarian: substrm
----------------------------------------------------------------------------------------------*/
class GrcSubStream
{
public:
     template<typename T>
    void WriteByte(T iOutput) { GrcBinaryStream::write_8bits_be(m_strm, static_cast<uint8_t>(iOutput)); }

    template<typename T> 
    void WriteShort(T iOutput) { GrcBinaryStream::write_16bits_be(m_strm, static_cast<uint16_t>(iOutput)); }

    template<typename T>
    void WriteInt(T iOutput) { GrcBinaryStream::write_32bits_be(m_strm, static_cast<uint32_t>(iOutput)); }


public:
	std::ostream m_strm;
};

/*----------------------------------------------------------------------------------------------
Class: GrcDiversion
Description: Divert output on a given stream a temporary in-memory buffer in constructor.
  Restore i/o to streams original target and write buffer on call to undivert() or the destructor.
Hungarian: dstrm
----------------------------------------------------------------------------------------------*/
class GrcDiversion : public std::stringbuf
{
    std::streambuf * m_sbSaved;
    std::iostream   & m_strm;

    // Disable copying of any sort
    GrcDiversion(const GrcDiversion &);
    GrcDiversion & operator = (const GrcDiversion &);

public:
    GrcDiversion(std::iostream & strm, const std::string sInitial="")
        : std::stringbuf(std::ios::in | std::ios::out | std::ios::ate), m_sbSaved(0), m_strm(strm)
    {
        divert(sInitial);
    }

    ~GrcDiversion()
    {
        undivert();
    }

    void divert(const std::string sInitial="")
    {
        undivert();
        m_strm.flush();
        str(sInitial);
        m_sbSaved = m_strm.rdbuf(this);
    }

    void undivert()
    {
        if (m_sbSaved)
        {
            m_strm.flush();
            m_strm.rdbuf(m_sbSaved);
            m_strm.write(str().data(), str().size());
            m_strm.flush();
            str("");
            m_sbSaved = 0;
        }
    }
};

#endif // !GRC_BINSTRM_INCLUDED