File: BinaryStream.cpp

package info (click to toggle)
intel-graphics-compiler2 2.16.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 106,644 kB
  • sloc: cpp: 805,640; lisp: 287,672; ansic: 16,414; python: 3,952; yacc: 2,588; lex: 1,666; pascal: 313; sh: 186; makefile: 35
file content (129 lines) | stat: -rw-r--r-- 2,673 bytes parent folder | download
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
/*========================== begin_copyright_notice ============================

Copyright (C) 2017-2021 Intel Corporation

SPDX-License-Identifier: MIT

============================= end_copyright_notice ===========================*/

#include "BinaryStream.h"

namespace Util {

BinaryStream::BinaryStream() : m_membuf(std::ios::in | std::ios::out | std::ios::binary) {
  // Nothing!
}

BinaryStream::~BinaryStream() {
  // Nothing!
}

bool BinaryStream::Write(const char *s, std::streamsize n) {
  bool retValue = false;

  std::streamsize prevSize = Size();

  m_membuf.write(s, n);

  if ((Size() - prevSize) == n) {
    retValue = true;
  }

  if (m_membuf.fail()) {
    retValue = false;
  }

#if defined(_DEBUG)
  GetLinearPointer();
#endif

  return retValue;
}

bool BinaryStream::Write(const BinaryStream &in) {
  bool retValue = false;

  std::streamsize prevSize = Size();

  m_membuf << in.m_membuf.str();

  if ((Size() - prevSize) == in.Size()) {
    retValue = true;
  }

  if (m_membuf.fail()) {
    retValue = false;
  }

#if defined(_DEBUG)
  GetLinearPointer();
#endif

  return retValue;
}

bool BinaryStream::WriteAt(const char *s, std::streamsize n, std::streamsize loc) {
  bool retValue = true;

  // Give this function name it seems like this function should enlarge the stream if needed. Discuss.
  if ((n + loc) < Size()) {
    m_membuf.seekp(loc, std::ios_base::beg);
    Write(s, n);
    m_membuf.seekp(0, std::ios_base::end);
  } else {
    retValue = false;
  }

  return retValue;
}

const char *BinaryStream::GetLinearPointer() {
  m_LinearPointer = m_membuf.str();
  return m_LinearPointer.c_str();
}

bool BinaryStream::Align(std::streamsize alignment) {
  bool retValue = true;

  std::streamsize currentSize = Size();

  std::streamsize modulo = currentSize % alignment;

  if (modulo) {
    std::streamsize offset = alignment - modulo;

    retValue = AddPadding(offset);
  }

  retValue &= (Size() % alignment) == 0;

  return retValue;
}

bool BinaryStream::AddPadding(std::streamsize padding) {
  bool retValue = true;
  if (padding > 0) {
    std::streamsize prevSize = Size();
    // Writes zeros to the width of "padding"
    m_membuf.width(padding);
    m_membuf.fill((char)0x0);
    m_membuf << '\0';

    if ((Size() - prevSize) != padding) {
      retValue = false;
    } else if (m_membuf.fail()) {
      retValue = false;
    }
  }
  return retValue;
}

std::streamsize BinaryStream::Size() const {
  // tellp is a non const function
  // TODO: Is there a better way to do this?
  return const_cast<BinaryStream *>(this)->Size();
}

std::streamsize BinaryStream::Size() { return m_membuf.tellp(); }

} // namespace Util