summaryrefslogtreecommitdiff
path: root/libcult/cult/dr/xdr/input-stream.cxx
blob: 173076311a24fe239586198b1313bc3c764a0335 (plain)
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
// file      : cult/dr/xdr/input-stream.cxx
// author    : Boris Kolpackov <boris@kolpackov.net>
// copyright : Copyright (c) 2005-2010 Boris Kolpackov
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#include <cult/dr/xdr/input-stream.hxx>

namespace Cult
{
  namespace DR
  {
    namespace XDR
    {
      InputStream::
      InputStream (Shptr<Buffer> buffer)
          : buffer_ (buffer)
      {
        xdrmem_create (&xdr_, buffer_->data (), buffer_->size (), XDR_DECODE);
      }

      InputStream::
      ~InputStream ()
      {
        xdr_destroy (&xdr_);
      }

      InputStream& InputStream::
      operator>> (Boolean& v)
      {
        bool_t b;

        if (!xdr_bool (&xdr_, &b))
          throw Extraction ();

        v = b;

        return *this;
      }

      InputStream& InputStream::
      operator>> (Int8& v)
      {
        if (!xdr_int8_t (&xdr_, &v))
          throw Extraction ();

        return *this;
      }

      InputStream& InputStream::
      operator>> (UnsignedInt8& v)
      {
        if (!xdr_uint8_t (&xdr_, &v))
          throw Extraction ();

        return *this;
      }

      InputStream& InputStream::
      operator>> (Int16& v)
      {
        if (!xdr_int16_t (&xdr_, &v))
          throw Extraction ();

        return *this;
      }

      InputStream& InputStream::
      operator>> (UnsignedInt16& v)
      {
        if (!xdr_uint16_t (&xdr_, &v))
          throw Extraction ();

        return *this;
      }

      InputStream& InputStream::
      operator>> (Int32& v)
      {
        if (!xdr_int32_t (&xdr_, &v))
          throw Extraction ();

        return *this;
      }

      InputStream& InputStream::
      operator>> (UnsignedInt32& v)
      {
        if (!xdr_uint32_t (&xdr_, &v))
          throw Extraction ();

        return *this;
      }

      InputStream& InputStream::
      operator>> (Int64& v)
      {
        if (!xdr_int64_t (&xdr_, (int64_t*)&v))
          throw Extraction ();

        return *this;
      }

      InputStream& InputStream::
      operator>> (UnsignedInt64& v)
      {
        if (!xdr_uint64_t (&xdr_, (uint64_t*)&v))
          throw Extraction ();

        return *this;
      }

      InputStream& InputStream::
      operator>> (String& v)
      {
        UnsignedInt32 size;

        if (!xdr_uint32_t (&xdr_, &size))
          throw Extraction ();

        // I dare to change the guts!
        //
        v.resize (size);

        Char* p (const_cast<Char*> (v.data ()));

        if (!xdr_opaque (&xdr_, p, size))
          throw Extraction ();

        return *this;
      }

      Void InputStream::
      read (Buffer& buffer, Size size)
      {
        Size new_size (buffer.position () + size);

        buffer.capacity (new_size);

        if (!xdr_opaque (&xdr_, buffer.data () + buffer.position (), size))
          throw Extraction ();

        buffer.size (new_size);
      }

      Boolean InputStream::
      eos () const
      {
        return xdr_getpos (&xdr_) >= buffer_->size ();
      }
    }
  }
}