summaryrefslogtreecommitdiff
path: root/glib/src/iochannel.ccg
blob: 03783a4a85bb9ca8792c48a9a25611a12d478901 (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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
// -*- c++ -*-
/* $Id$ */

/* Copyright (C) 2002 The gtkmm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <glibmm/exceptionhandler.h>
#include <glibmm/iochannel.h>
#include <glibmm/utility.h>
#include <glibmm/main.h>
#include <glib.h>


namespace
{

// Glib::IOChannel reference counting issues:
//
// Normally, you'd expect that the C++ object stays around as long as the
// C instance does.  Also Glib::wrap() usually returns always the same C++
// wrapper object for a single C instance.
//
// Unfortunately it isn't possible to implement these features if we didn't
// create the underlying GIOChannel.  That is, when wrapping existing
// GIOChannel instances such as returned by e.g. g_io_channel_unix_new() or
// g_io_channel_new_file().  Neither is there a way to hook up a wrapper
// object in an existing GIOChannel, nor exists any destroy notification.
//
// So that means:  If the IOChannel is implemented in C++ -- that is, our
// GlibmmIOChannel backend is used -- we use the GIOChannel reference
// counting mechanism.  If the IOChannel backend is unknown, then the
// wrapper instance holds always exactly one reference to the GIOChannel.
// The wrapper object itself is then managed via our own refcounting
// mechanism.  To do that a utility class ForeignIOChannel is introduced to
// override reference() and unreference().

class ForeignIOChannel : public Glib::IOChannel
{
public:
  ForeignIOChannel(GIOChannel* gobject, bool take_copy)
    : Glib::IOChannel(gobject, take_copy), ref_count_(0) {}

  virtual void reference()   const;
  virtual void unreference() const;

private:
  mutable int ref_count_;
};

void ForeignIOChannel::reference() const
{
  ++ref_count_;
}

void ForeignIOChannel::unreference() const
{
  if (!(--ref_count_)) delete this;
}

} // anonymous namespace


namespace Glib
{

class GlibmmIOChannel
{
public:
  GIOChannel        base;
  Glib::IOChannel*  wrapper;

  static const GIOFuncs vfunc_table;

  static GIOStatus io_read(GIOChannel* channel, char* buf, gsize count,
                           gsize* bytes_read, GError** err);

  static GIOStatus io_write(GIOChannel* channel, const char* buf, gsize count,
                            gsize* bytes_written, GError** err);

  static GIOStatus io_seek (GIOChannel* channel, gint64 offset, GSeekType type, GError** err);
  static GIOStatus io_close(GIOChannel* channel, GError** err);

  static GSource*  io_create_watch(GIOChannel* channel, GIOCondition condition);
  static void      io_free(GIOChannel* channel);

  static GIOStatus io_set_flags(GIOChannel* channel, GIOFlags flags, GError** err);
  static GIOFlags  io_get_flags(GIOChannel* channel);
};

// static
const GIOFuncs GlibmmIOChannel::vfunc_table =
{
  &GlibmmIOChannel::io_read,
  &GlibmmIOChannel::io_write,
  &GlibmmIOChannel::io_seek,
  &GlibmmIOChannel::io_close,
  &GlibmmIOChannel::io_create_watch,
  &GlibmmIOChannel::io_free,
  &GlibmmIOChannel::io_set_flags,
  &GlibmmIOChannel::io_get_flags,
};


/**** GLib::IOChannel ******************************************************/

/* Construct a custom C++-implemented IOChannel.  GlibmmIOChannel is an
 * extended GIOChannel struct which allows us to hook up a pointer to this
 * persistent wrapper instance.
 */
IOChannel::IOChannel()
:
  gobject_ (static_cast<GIOChannel*>(g_malloc(sizeof(GlibmmIOChannel))))
{
  g_io_channel_init(gobject_);
  gobject_->funcs = const_cast<GIOFuncs*>(&GlibmmIOChannel::vfunc_table);

  reinterpret_cast<GlibmmIOChannel*>(gobject_)->wrapper = this;
}

/* Construct an IOChannel wrapper for an already created GIOChannel.
 * See the comment at the top of this file for an explanation of the
 * problems with this approach.
 */
IOChannel::IOChannel(GIOChannel* gobject, bool take_copy)
:
  gobject_ (gobject)
{
  // This ctor should never be called for GlibmmIOChannel instances.
  g_assert(gobject != 0);
  g_assert(gobject->funcs != &GlibmmIOChannel::vfunc_table);

  if(take_copy)
    g_io_channel_ref(gobject_);
}

IOChannel::~IOChannel()
{
  if(gobject_)
  {
    // Check whether this IOChannel is implemented in C++, i.e. whether it
    // uses our GlibmmIOChannel forwarding backend.  Normally, this will never
    // be true because the wrapper should only be deleted in the io_free()
    // callback, which clears gobject_ before deleting.  But in case the ctor
    // of a derived class threw an exception the GIOChannel must be destroyed
    // prematurely.
    //
    if(gobject_->funcs == &GlibmmIOChannel::vfunc_table)
    {
      // Disconnect the wrapper object so that it won't be deleted twice.
      reinterpret_cast<GlibmmIOChannel*>(gobject_)->wrapper = 0;
    }

    GIOChannel *const tmp_gobject = gobject_;
    gobject_ = 0;

    g_io_channel_unref(tmp_gobject);
  }
}

#ifdef GLIBMM_EXCEPTIONS_ENABLED
Glib::RefPtr<IOChannel> IOChannel::create_from_file(const std::string& filename, const std::string& mode)
#else
Glib::RefPtr<IOChannel> IOChannel::create_from_file(const std::string& filename, const std::string& mode, std::auto_ptr<Glib::Error>& error)
#endif //GLIBMM_EXCEPTIONS_ENABLED
{
  GError* gerror = 0;
  GIOChannel *const channel = g_io_channel_new_file(filename.c_str(), mode.c_str(), &gerror);

  if(gerror)
  {
    #ifdef GLIBMM_EXCEPTIONS_ENABLED
    Glib::Error::throw_exception(gerror);
    #else
    error = Glib::Error::throw_exception(gerror);
    #endif //GLIBMM_EXCEPTIONS_ENABLED
  }

  return Glib::wrap(channel, false);
}

Glib::RefPtr<IOChannel> IOChannel::create_from_fd(int fd)
{
  return Glib::wrap(g_io_channel_unix_new(fd), false);
}

#ifdef G_OS_WIN32

Glib::RefPtr<IOChannel> IOChannel::create_from_win32_fd(int fd)
{
  return Glib::wrap(g_io_channel_win32_new_fd(fd), false);
}

Glib::RefPtr<IOChannel> IOChannel::create_from_win32_socket(int socket)
{
  return Glib::wrap(g_io_channel_win32_new_socket(socket), false);
}

#endif /* G_OS_WIN32 */

#ifdef GLIBMM_EXCEPTIONS_ENABLED
IOStatus IOChannel::write(const Glib::ustring& str)
#else
IOStatus IOChannel::write(const Glib::ustring& str, std::auto_ptr<Glib::Error>& error)
#endif //GLIBMM_EXCEPTIONS_ENABLED
{
  gsize bytes_written = 0;
#ifdef GLIBMM_EXCEPTIONS_ENABLED
  return write(str.data(), str.bytes(), bytes_written);
#else
  return write(str.data(), str.bytes(), bytes_written, error);
#endif //GLIBMM_EXCEPTIONS_ENABLED
}

#ifdef GLIBMM_EXCEPTIONS_ENABLED
IOStatus IOChannel::read_line(Glib::ustring& line)
#else
IOStatus IOChannel::read_line(Glib::ustring& line, std::auto_ptr<Glib::Error>& error)
#endif //GLIBMM_EXCEPTIONS_ENABLED
{
  Glib::ScopedPtr<char> buf;
  GError* gerror = 0;
  gsize   bytes = 0;

  const GIOStatus status = g_io_channel_read_line(gobj(), buf.addr(), &bytes, 0, &gerror);

  if(gerror)
  {
    #ifdef GLIBMM_EXCEPTIONS_ENABLED
    Glib::Error::throw_exception(gerror);
    #else
    error = Glib::Error::throw_exception(gerror);
    #endif //GLIBMM_EXCEPTIONS_ENABLED
  }

  if(buf.get())
    line.assign(buf.get(), buf.get() + bytes);
  else
    line.erase();

  return (IOStatus) status;
}

#ifdef GLIBMM_EXCEPTIONS_ENABLED
IOStatus IOChannel::read_to_end(Glib::ustring& str)
#else
IOStatus IOChannel::read_to_end(Glib::ustring& str, std::auto_ptr<Glib::Error>& error)
#endif //GLIBMM_EXCEPTIONS_ENABLED
{
  Glib::ScopedPtr<char> buf;
  GError* gerror = 0;
  gsize   bytes = 0;

  const GIOStatus status = g_io_channel_read_to_end(gobj(), buf.addr(), &bytes, &gerror);

  if(gerror)
  {
    #ifdef GLIBMM_EXCEPTIONS_ENABLED
    Glib::Error::throw_exception(gerror);
    #else
    error = Glib::Error::throw_exception(gerror);
    #endif //GLIBMM_EXCEPTIONS_ENABLED
  }

  if(buf.get())
    str.assign(buf.get(), buf.get() + bytes);
  else
    str.erase();

  return (IOStatus) status;
}

#ifdef GLIBMM_EXCEPTIONS_ENABLED
IOStatus IOChannel::read(Glib::ustring& str, gsize count)
#else
IOStatus IOChannel::read(Glib::ustring& str, gsize count, std::auto_ptr<Glib::Error>& error)
#endif //GLIBMM_EXCEPTIONS_ENABLED
{
  Glib::ScopedPtr<char> buf (g_new(char, count));
  GError* gerror = 0;
  gsize   bytes = 0;

  const GIOStatus status = g_io_channel_read_chars(gobj(), buf.get(), count, &bytes, &gerror);

 if(gerror)
  {
    #ifdef GLIBMM_EXCEPTIONS_ENABLED
    Glib::Error::throw_exception(gerror);
    #else
    error = Glib::Error::throw_exception(gerror);
    #endif //GLIBMM_EXCEPTIONS_ENABLED
  }

  if(buf.get())
    str.assign(buf.get(), buf.get() + bytes);
  else
    str.erase();

  return (IOStatus) status;
}

#ifdef GLIBMM_EXCEPTIONS_ENABLED
IOStatus IOChannel::set_encoding(const std::string& encoding)
#else
IOStatus IOChannel::set_encoding(const std::string& encoding, std::auto_ptr<Glib::Error>& error)
#endif //GLIBMM_EXCEPTIONS_ENABLED
{
  GError* gerror = 0;

  const GIOStatus status = g_io_channel_set_encoding(
      gobj(), (encoding.empty()) ? 0 : encoding.c_str(), &gerror);

  if(gerror)
  {
    #ifdef GLIBMM_EXCEPTIONS_ENABLED
    Glib::Error::throw_exception(gerror);
    #else
    error = Glib::Error::throw_exception(gerror);
    #endif //GLIBMM_EXCEPTIONS_ENABLED
  }

  return (IOStatus) status;
}

std::string IOChannel::get_encoding() const
{
  const char *const encoding = g_io_channel_get_encoding(gobject_);
  return (encoding) ? std::string(encoding) : std::string();
}

void IOChannel::set_line_term(const std::string& term)
{
  if(term.empty())
    g_io_channel_set_line_term(gobj(), 0, 0);
  else
    g_io_channel_set_line_term(gobj(), term.data(), term.size());
}

std::string IOChannel::get_line_term() const
{
  int len = 0;
  const char *const term = g_io_channel_get_line_term(gobject_, &len);

  return (term) ? std::string(term, len) : std::string();
}

Glib::RefPtr<IOSource> IOChannel::create_watch(IOCondition condition)
{
  // The corresponding unreference() takes place in the dtor
  // of the Glib::RefPtr<IOChannel> object below.
  reference();
  return IOSource::create(Glib::RefPtr<IOChannel>(this), condition);
}

IOStatus IOChannel::read_vfunc(char*, gsize, gsize&)
{
  g_assert_not_reached();
  return IO_STATUS_ERROR;
}

IOStatus IOChannel::write_vfunc(const char*, gsize, gsize&)
{
  g_assert_not_reached();
  return IO_STATUS_ERROR;
}

IOStatus IOChannel::seek_vfunc(gint64, SeekType)
{
  g_assert_not_reached();
  return IO_STATUS_ERROR;
}

IOStatus IOChannel::close_vfunc()
{
  g_assert_not_reached();
  return IO_STATUS_ERROR;
}

Glib::RefPtr<Glib::Source> IOChannel::create_watch_vfunc(IOCondition)
{
  g_assert_not_reached();
  return Glib::RefPtr<Glib::Source>();
}

IOStatus IOChannel::set_flags_vfunc(IOFlags)
{
  g_assert_not_reached();
  return IO_STATUS_ERROR;
}

IOFlags IOChannel::get_flags_vfunc()
{
  g_assert_not_reached();
  return IOFlags(0);
}

void IOChannel::reference() const
{
  g_io_channel_ref(gobject_);
}

void IOChannel::unreference() const
{
  g_io_channel_unref(gobject_);
}

Glib::RefPtr<IOChannel> wrap(GIOChannel* gobject, bool take_copy)
{
  IOChannel* cpp_object = 0;

  if(gobject)
  {
    if(gobject->funcs == &GlibmmIOChannel::vfunc_table)
    {
      cpp_object = reinterpret_cast<GlibmmIOChannel*>(gobject)->wrapper;

      if(take_copy && cpp_object)
        cpp_object->reference();
    }
    else
    {
      cpp_object = new ForeignIOChannel(gobject, take_copy);
      cpp_object->reference(); // the refcount is initially 0
    }
  }

  return Glib::RefPtr<IOChannel>(cpp_object);
}


/**** Glib::GlibmmIOChannel ************************************************/

// static
GIOStatus GlibmmIOChannel::io_read(GIOChannel* channel, char* buf, gsize count,
                                   gsize* bytes_read, GError** err)
{
  IOChannel *const wrapper = reinterpret_cast<GlibmmIOChannel*>(channel)->wrapper;

  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  try
  {
  #endif //GLIBMM_EXCEPTIONS_ENABLED
    return (GIOStatus) wrapper->read_vfunc(buf, count, *bytes_read);
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  }
  catch(Glib::Error& error)
  {
    error.propagate(err);
  }
  catch(...)
  {
    Glib::exception_handlers_invoke();
  }
  #endif //GLIBMM_EXCEPTIONS_ENABLED

  return G_IO_STATUS_ERROR;
}

// static
GIOStatus GlibmmIOChannel::io_write(GIOChannel* channel, const char* buf, gsize count,
                                    gsize* bytes_written, GError** err)
{
  IOChannel *const wrapper = reinterpret_cast<GlibmmIOChannel*>(channel)->wrapper;

  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  try
  {
  #endif //GLIBMM_EXCEPTIONS_ENABLED
    return (GIOStatus) wrapper->write_vfunc(buf, count, *bytes_written);
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  }
  catch(Glib::Error& error)
  {
    error.propagate(err);
  }
  catch(...)
  {
    Glib::exception_handlers_invoke();
  }
  #endif //GLIBMM_EXCEPTIONS_ENABLED

  return G_IO_STATUS_ERROR;
}

// static
GIOStatus GlibmmIOChannel::io_seek(GIOChannel* channel, gint64 offset, GSeekType type, GError** err)
{
  IOChannel *const wrapper = reinterpret_cast<GlibmmIOChannel*>(channel)->wrapper;

  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  try
  {
  #endif //GLIBMM_EXCEPTIONS_ENABLED
    return (GIOStatus) wrapper->seek_vfunc(offset, (SeekType) type);
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  }
  catch(Glib::Error& error)
  {
    error.propagate(err);
  }
  catch(...)
  {
    Glib::exception_handlers_invoke();
  }
  #endif //GLIBMM_EXCEPTIONS_ENABLED

  return G_IO_STATUS_ERROR;
}

// static
GIOStatus GlibmmIOChannel::io_close(GIOChannel* channel, GError** err)
{
  IOChannel *const wrapper = reinterpret_cast<GlibmmIOChannel*>(channel)->wrapper;

  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  try
  {
  #endif //GLIBMM_EXCEPTIONS_ENABLED
    return (GIOStatus) wrapper->close_vfunc();
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  }
  catch(Glib::Error& error)
  {
    error.propagate(err);
  }
  catch(...)
  {
    Glib::exception_handlers_invoke();
  }
  #endif //GLIBMM_EXCEPTIONS_ENABLED
 

  return G_IO_STATUS_ERROR;
}

// static
GSource* GlibmmIOChannel::io_create_watch(GIOChannel* channel, GIOCondition condition)
{
  IOChannel *const wrapper = reinterpret_cast<GlibmmIOChannel*>(channel)->wrapper;

  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  try
  {
  #endif //GLIBMM_EXCEPTIONS_ENABLED
    const Glib::RefPtr<Source> source = wrapper->create_watch_vfunc((IOCondition) condition);
    return (source) ? source->gobj_copy() : 0;
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  }
  catch(...)
  {
    Glib::exception_handlers_invoke();
  }
  #endif //GLIBMM_EXCEPTIONS_ENABLED

  return 0;
}

// static
void GlibmmIOChannel::io_free(GIOChannel* channel)
{
  if(IOChannel *const wrapper = reinterpret_cast<GlibmmIOChannel*>(channel)->wrapper)
  {
    wrapper->gobject_ = 0;
    delete wrapper;
  }

  g_free(channel);
}

// static
GIOStatus GlibmmIOChannel::io_set_flags(GIOChannel* channel, GIOFlags flags, GError** err)
{
  IOChannel *const wrapper = reinterpret_cast<GlibmmIOChannel*>(channel)->wrapper;

  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  try
  {
  #endif //GLIBMM_EXCEPTIONS_ENABLED
    return (GIOStatus) wrapper->set_flags_vfunc((IOFlags) flags);
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  }
  catch(Glib::Error& error)
  {
    error.propagate(err);
  }
  catch(...)
  {
    Glib::exception_handlers_invoke();
  }
  #endif //GLIBMM_EXCEPTIONS_ENABLED

  return G_IO_STATUS_ERROR;
}

// static
GIOFlags GlibmmIOChannel::io_get_flags(GIOChannel* channel)
{
  IOChannel *const wrapper = reinterpret_cast<GlibmmIOChannel*>(channel)->wrapper;

  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  try
  {
  #endif //GLIBMM_EXCEPTIONS_ENABLED
    return (GIOFlags) wrapper->get_flags_vfunc();
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  }
  catch(...)
  {
    Glib::exception_handlers_invoke();
  }
  #endif //GLIBMM_EXCEPTIONS_ENABLED

  return GIOFlags(0);
}

} // namespace Glib