summaryrefslogtreecommitdiff
path: root/protocols/ace/INet/String_IOStream.cpp
blob: 185bc64950d838ea603b093c5fdb45f15300268f (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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// $Id$

#ifndef ACE_IOS_STRING_IOSTREAM_CPP
#define ACE_IOS_STRING_IOSTREAM_CPP

#include "ace/INet/String_IOStream.h"
#include "ace/INet/IOS_util.h"
#include "ace/Truncate.h"

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

namespace ACE
{
  namespace IOS
  {

    template <class ACE_CHAR_T, class TR>
    String_StreamBufferBase<ACE_CHAR_T, TR>::String_StreamBufferBase (openmode mode)
      : BasicBufferedStreamBuffer<ACE_CHAR_T, TR> (BUFFER_SIZE, mode),
        string_ref_ (&string_),
        rd_ptr_ (0)
      {
      }

    template <class ACE_CHAR_T, class TR>
    String_StreamBufferBase<ACE_CHAR_T, TR>::String_StreamBufferBase (string_type& string, openmode mode)
      : BasicBufferedStreamBuffer<ACE_CHAR_T, TR> (BUFFER_SIZE, mode),
        string_ref_ (&string),
        rd_ptr_ (0)
      {
      }

    template <class ACE_CHAR_T, class TR>
    String_StreamBufferBase<ACE_CHAR_T, TR>::~String_StreamBufferBase ()
      {
      }

    template <class ACE_CHAR_T, class TR>
    typename String_StreamBufferBase<ACE_CHAR_T, TR>::pos_type
    String_StreamBufferBase<ACE_CHAR_T, TR>::seekoff (
        off_type off,
        seekdir way,
        openmode which)
      {

        if (which != this->get_mode () || which == std::ios::out)
          return pos_type (-1);

        size_type spos = 0;
        if (way == std::ios::cur)
          spos = this->rd_ptr_;
        else if (way == std::ios::end)
          spos = this->string_ref_->length ();
        spos += off;
        if (spos < this->string_ref_->length ())
          this->rd_ptr_ = spos;
        else
          this->rd_ptr_ = this->string_ref_->length ();

        this->setg (this->eback (), this->eback (), this->eback ());
        return pos_type (this->rd_ptr_);
      }

    template <class ACE_CHAR_T, class TR>
    typename String_StreamBufferBase<ACE_CHAR_T, TR>::pos_type
    String_StreamBufferBase<ACE_CHAR_T, TR>::seekpos (
        pos_type pos,
        openmode which)
      {
        return this->seekoff (pos_type (pos), std::ios::beg, which);
      }

    template <class ACE_CHAR_T, class TR>
    const typename String_StreamBufferBase<ACE_CHAR_T, TR>::string_type&
    String_StreamBufferBase<ACE_CHAR_T, TR>::str () const
      {
        const_cast<String_StreamBufferBase<ACE_CHAR_T, TR>*> (this)->sync ();
        return *this->string_ref_;
      }

    template <class ACE_CHAR_T, class TR>
    void String_StreamBufferBase<ACE_CHAR_T, TR>::close_string ()
      {
        this->sync ();
        this->string_ref_ = 0;
      }

    template <class ACE_CHAR_T, class TR>
    void String_StreamBufferBase<ACE_CHAR_T, TR>::clear_string ()
      {
        this->sync ();
        this->string_ref_->clear ();
      }

    template <class ACE_CHAR_T, class TR>
    int String_StreamBufferBase<ACE_CHAR_T, TR>::read_from_stream (char_type* buffer, std::streamsize length)
      {
        int n = 0;
        if (this->string_ref_)
          {
            if ((this->rd_ptr_ + length) > this->string_ref_->length ())
              {
                length = this->string_ref_->length () - this->rd_ptr_;
              }
            ACE_OS::memmove (buffer, &(*this->string_ref_)[this->rd_ptr_], length * sizeof (char_type));
            this->rd_ptr_ += length;
            n = ACE_Utils::truncate_cast<int> (length);
          }
        return n;
      }

    template <class ACE_CHAR_T, class TR>
    int String_StreamBufferBase<ACE_CHAR_T, TR>::write_to_stream (const char_type* buffer, std::streamsize length)
      {
        int n = 0;
        if (this->string_ref_)
          {
            this->string_ref_->append (buffer, length);
            n = ACE_Utils::truncate_cast<int> (length);
          }
        return n;
      }

    template <class ACE_CHAR_T, class TR>
    String_IOSBase<ACE_CHAR_T, TR>::String_IOSBase (openmode mode)
      : streambuf_ (mode)
      {
        ace_ios_init (&this->streambuf_);
      }

    template <class ACE_CHAR_T, class TR>
    String_IOSBase<ACE_CHAR_T, TR>::String_IOSBase (string_type& string, openmode mode)
      : streambuf_ (string, mode)
      {
        ace_ios_init (&this->streambuf_);
      }

    template <class ACE_CHAR_T, class TR>
    String_IOSBase<ACE_CHAR_T, TR>::~String_IOSBase ()
      {
        this->close ();
      }

    template <class ACE_CHAR_T, class TR>
    typename String_IOSBase<ACE_CHAR_T, TR>::buffer_type*
    String_IOSBase<ACE_CHAR_T, TR>::rdbuf ()
      {
        return &this->streambuf_;
      }

    template <class ACE_CHAR_T, class TR>
    void String_IOSBase<ACE_CHAR_T, TR>::close ()
      {
        this->streambuf_.close_string ();
      }

    template <class ACE_CHAR_T, class TR>
    const typename String_IOSBase<ACE_CHAR_T, TR>::buffer_type&
    String_IOSBase<ACE_CHAR_T, TR>::stream () const
      {
        return this->streambuf_;
      }

    template <class ACE_CHAR_T, class TR>
    String_OStreamBase<ACE_CHAR_T, TR>::String_OStreamBase()
      : String_IOSBase<ACE_CHAR_T, TR> (std::ios::out),
        std::basic_ostream<ACE_CHAR_T, TR> (String_IOSBase<ACE_CHAR_T, TR>::rdbuf ())
      {
      }

    template <class ACE_CHAR_T, class TR>
    String_OStreamBase<ACE_CHAR_T, TR>::String_OStreamBase(string_type& string)
      : String_IOSBase<ACE_CHAR_T, TR> (string, std::ios::out),
        std::basic_ostream<ACE_CHAR_T, TR> (String_IOSBase<ACE_CHAR_T, TR>::rdbuf ())
      {
      }

    template <class ACE_CHAR_T, class TR>
    String_OStreamBase<ACE_CHAR_T, TR>::~String_OStreamBase()
      {
      }

    template <class ACE_CHAR_T, class TR>
    const typename String_OStreamBase<ACE_CHAR_T, TR>::string_type&
    String_OStreamBase<ACE_CHAR_T, TR>::str () const
      {
        return this->stream ().str ();
      }

    template <class ACE_CHAR_T, class TR>
    void String_OStreamBase<ACE_CHAR_T, TR>::clear ()
      {
        return this->rdbuf ()->clear_string ();
      }

    template <class ACE_CHAR_T, class TR>
    String_IStreamBase<ACE_CHAR_T, TR>::String_IStreamBase()
      : String_IOSBase<ACE_CHAR_T, TR> (std::ios::in),
        std::basic_istream<ACE_CHAR_T, TR> (String_IOSBase<ACE_CHAR_T, TR>::rdbuf ())
      {
      }

    template <class ACE_CHAR_T, class TR>
    String_IStreamBase<ACE_CHAR_T, TR>::String_IStreamBase(const string_type& string)
      : String_IOSBase<ACE_CHAR_T, TR> (const_cast<string_type&> (string),
                                        std::ios::in),
        std::basic_istream<ACE_CHAR_T, TR> (String_IOSBase<ACE_CHAR_T, TR>::rdbuf ())
      {
      }

    template <class ACE_CHAR_T, class TR>
    String_IStreamBase<ACE_CHAR_T, TR>::~String_IStreamBase()
      {
      }

    template <class ACE_CHAR_T, class TR>
    String_IStreamBase<ACE_CHAR_T, TR>&
    String_IStreamBase<ACE_CHAR_T, TR>::rewind ()
      {
        this->rdbuf ()->pubseekpos (0, std::ios::in);
        this->clear ();
        return *this;
      }
  }
}

ACE_END_VERSIONED_NAMESPACE_DECL

#endif /* ACE_IOS_STRING_IOSTREAM_CPP */