summaryrefslogtreecommitdiff
path: root/examples/iochannel_stream/fdstream.cc
blob: be97676f3b1311b832cc745d8e47d65f019bd864 (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/* Copyright (C) 2004 The glibmm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "fdstream.h"

#include <glibmm/main.h>

fdstreambuf::fdstreambuf()
{
  reset();
}

fdstreambuf::fdstreambuf(int fd, bool manage)
{
  create_iochannel(fd, manage);
}

fdstreambuf::~fdstreambuf()
{
  sync();
}

void fdstreambuf::reset()
{
  setg(putback_buffer + 1, putback_buffer + 1, putback_buffer + 1);
  error_condition.error = false;
}

void fdstreambuf::create_iochannel(int fd, bool manage)
{
  sync();
  reset();

  if(fd >= 0)
  {
    iochannel_ = Glib::IOChannel::create_from_fd(fd);

    iochannel_->set_encoding("");
   
    iochannel_->set_buffered(true);
    iochannel_->set_close_on_unref(manage);
  }  
}

void fdstreambuf::detach_fd()
{
  iochannel_->set_close_on_unref(false);
}

void fdstreambuf::connect(const sigc::slot<bool, Glib::IOCondition>& callback,
			  Glib::IOCondition condition)
{
  Glib::signal_io().connect(callback, iochannel_, condition);
}

fdstream_error fdstreambuf::get_error() const
{
  return error_condition;
}

// the standard requires sync to return 0 for success and -1 for error
int fdstreambuf::sync()
{
  if (!iochannel_)
    return -1;

  try
  {
    iochannel_->flush();
  }
  catch(Glib::IOChannelError& io_error)
  {
    error_condition.error = true;
    error_condition.code = io_error.code();
    return -1;
  }

  return 0;
}

void fdstreambuf::close_iochannel()
{
  iochannel_->set_close_on_unref(false);
  reset();

  try
  {
    iochannel_->close(true);
  }
  catch(Glib::IOChannelError& io_error)
  {
    error_condition.error = true;
    error_condition.code = io_error.code();
  }

}

// the standard requires this to return either the character
// written on overflow or traits_type::eof() (= EOF with char_type == char)
fdstreambuf::traits_type::int_type fdstreambuf::overflow(int_type c)
{
  if(!traits_type::eq_int_type(c, traits_type::eof()))
  {
    try
    {
      gsize result = 0;
      char write_char = c;
      iochannel_->write(&write_char, 1, result);
    }
    catch(Glib::IOChannelError& io_error)
    {
      error_condition.error = true;
      error_condition.code = io_error.code();
      return traits_type::eof();
    }
  }
  return traits_type::not_eof(c);
}

// the standard requires this to return the number of characters written
// (which will be 0 for stream failure - it is not correct to return EOF)
std::streamsize fdstreambuf::xsputn(const char* source, std::streamsize num)
{
  gsize result = 0;

  // the documentation for Glib::IOChannel indicates that Glib::IOChannel::write()
  // will only do a short write in the event of stream failure, so there is no
  // need to check result and have a second bite (byte) at it as would be
  // necessary with Unix write()
  try
  {
    iochannel_->write(source, num, result);
  }
  catch(Glib::IOChannelError& io_error)
  {
    error_condition.error = true;
    error_condition.code = io_error.code();
    result = 0;
  }

  return result;
}

// the standard requires this to return the first character available
// on underflow or traits_type::eof() (= EOF with char_type == char)
fdstreambuf::traits_type::int_type fdstreambuf::underflow()
{
  if(gptr() < egptr())
    return traits_type::to_int_type(*gptr());

  // copy the character in bump position (if any) to putback position
  if(gptr() - eback())
    *putback_buffer = *(gptr() - 1);

  // now insert a character into the bump position
  gsize result = 0;
  try
  {
    iochannel_->read(putback_buffer + 1, 1, result);
  }
  catch(Glib::IOChannelError& io_error)
  {
    error_condition.error = true;
    error_condition.code = io_error.code();
    return traits_type::eof();
  }

  // some other error - is this possible?  In case it is, cater for it
  if (result == 0)
    return traits_type::eof();
  
  // reset buffer pointers
  setg(putback_buffer,
       putback_buffer + 1,
       putback_buffer + 2);

  // return character in bump/peek position
  return traits_type::to_int_type(*gptr()); // == *(putback_buffer + 1)
}

// the standard requires this to return the number of characters fetched
// (which will be 0 for stream failure - it is not correct to return EOF)
std::streamsize fdstreambuf::xsgetn(char* dest, std::streamsize num)
{
  std::streamsize chars_read = 0;

  // available would normally be 0, but could be up to 2 if there
  // have been putbacks or a peek and a putback
  std::streamsize available = egptr() - gptr();

  // if num is less than or equal to the characters already in the
  // putback buffer, extract from buffer
  if (num <= available)
  {
    traits_type::copy(dest, gptr(), num);
    gbump(num);
    chars_read = num;
  }
  else
  {
    // first copy out putback buffer
    if (available)
    {
      traits_type::copy(dest, gptr(), available);
      chars_read = available;
    }

    // read up to everything else we need with Glib::IOChannel::read()
    gsize result = 0;
    try
    {
      do
      {
	iochannel_->read(dest + chars_read,
			 num - chars_read,
			 result);

	if (result > 0)
          chars_read += result;
      }
      while (result > 0 && result < static_cast<gsize>(num - chars_read));
    }
    catch(Glib::IOChannelError& io_error)
    {
      error_condition.error = true;
  
      error_condition.code = io_error.code();
      return chars_read;
    }

    if(chars_read)
    {
      // now mimic extraction of all characters by sbumpc() by putting
      // two characters into the buffer (if available) and resetting the
      // buffer pointers
      int putback_count = 0;
      if(chars_read >= 2)
      {
	*putback_buffer = *(dest + (chars_read - 2));
	putback_count = 2;
      }
      else
      {      // if we have reached here then we have only fetched
             // one character and it must have been read with
             // Glib::IOChannel::read() and not taken from the
             // putback buffer - otherwise we would have ended
             // at the first if block in this method
             // - and this also means that gptr() == egptr()
	if(gptr() - eback())
	{
	  *putback_buffer = *(gptr() - 1);
	  putback_count = 2;
	}
	else putback_count = 1;
      }

      *(putback_buffer + 1) = *(dest + (chars_read - 1));

      // reset buffer pointers
      this->setg(putback_buffer + (2 - putback_count),
		 putback_buffer + 2,
		 putback_buffer + 2);
    }
  }
  return chars_read;
}

fdstream::fdstream(int fd, bool manage)
: std::istream(0),
  std::ostream(0),
  buf(fd, manage)
{
  std::istream::rdbuf(&buf);
  std::ostream::rdbuf(&buf);
}

fdstream::fdstream()
: std::istream(0),
  std::ostream(0)
{
  std::istream::rdbuf(&buf);
  std::ostream::rdbuf(&buf);
}

void fdstream::attach(int fd, bool manage)
{
  buf.create_iochannel(fd, manage);
}

void fdstream::detach()
{
  buf.detach_fd();
}

void fdstream::close()
{
  buf.close_iochannel();
}

void fdstream::connect(const sigc::slot<bool, Glib::IOCondition>& callback,
	     Glib::IOCondition condition)
{
  buf.connect(callback, condition);
}

fdstream_error fdstream::get_error() const
{
  return buf.get_error();
}