summaryrefslogtreecommitdiff
path: root/glib/glibmm/streamiochannel.cc
blob: 674960dc91712c4a5b584634873bf23667e07d4a (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
/* $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 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 <glibmm/streamiochannel.h>
#include <glibmm/main.h> //For Source
#include <glib.h>
#include <fstream>
#include <iostream>

namespace Glib
{

#ifndef GLIBMM_DISABLE_DEPRECATED

// static
Glib::RefPtr<StreamIOChannel> StreamIOChannel::create(std::istream& stream)
{
  return Glib::RefPtr<StreamIOChannel>(new StreamIOChannel(&stream, 0));
}

// static
Glib::RefPtr<StreamIOChannel> StreamIOChannel::create(std::ostream& stream)
{
  return Glib::RefPtr<StreamIOChannel>(new StreamIOChannel(0, &stream));
}

// static
Glib::RefPtr<StreamIOChannel> StreamIOChannel::create(std::iostream& stream)
{
  return Glib::RefPtr<StreamIOChannel>(new StreamIOChannel(&stream, &stream));
}

StreamIOChannel::StreamIOChannel(std::istream* stream_in, std::ostream* stream_out)
:
  stream_in_  (stream_in),
  stream_out_ (stream_out)
{
  get_flags_vfunc(); // initialize GIOChannel flag bits
}

StreamIOChannel::~StreamIOChannel()
{}

IOStatus StreamIOChannel::read_vfunc(char* buf, gsize count, gsize& bytes_read)
{
  g_return_val_if_fail(stream_in_ != nullptr, IO_STATUS_ERROR);

  stream_in_->clear();
  stream_in_->read(buf, count);
  bytes_read = stream_in_->gcount();

  if(stream_in_->eof())
    return IO_STATUS_EOF;

  if(stream_in_->fail())
  {
    throw Glib::Error(G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_FAILED, "Reading from stream failed");
  }

  return IO_STATUS_NORMAL;
}

IOStatus StreamIOChannel::write_vfunc(const char* buf, gsize count, gsize& bytes_written)
{
  g_return_val_if_fail(stream_out_ != nullptr, IO_STATUS_ERROR);

  bytes_written = 0;

  stream_out_->clear();
  stream_out_->write(buf, count);

  if(stream_out_->fail())
  {
    throw Glib::Error(G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_FAILED, "Writing to stream failed");
  }

  bytes_written = count; // all or nothing ;)

  return IO_STATUS_NORMAL;
}

IOStatus StreamIOChannel::seek_vfunc(gint64 offset, SeekType type)
{
  std::ios::seekdir direction = std::ios::beg;

  switch(type)
  {
    case SEEK_TYPE_SET: direction = std::ios::beg; break;
    case SEEK_TYPE_CUR: direction = std::ios::cur; break;
    case SEEK_TYPE_END: direction = std::ios::end; break;
  }

  bool failed = false;

  if(stream_in_)
  {
    stream_in_->clear();
    stream_in_->seekg(offset, direction);
    failed = stream_in_->fail();
  }
  if(stream_out_)
  {
    stream_out_->clear();
    stream_out_->seekp(offset, direction);
    failed = (failed || stream_out_->fail());
  }

  if(failed)
  {
    throw Glib::Error(G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_FAILED, "Seeking into stream failed");
  }

  return Glib::IO_STATUS_NORMAL;
}

IOStatus StreamIOChannel::close_vfunc()
{
  bool failed = false;

  if(std::fstream *const fstream = dynamic_cast<std::fstream*>(stream_in_))
  {
    fstream->clear();
    fstream->close();
    failed = fstream->fail();
  }
  else if(std::ifstream *const ifstream = dynamic_cast<std::ifstream*>(stream_in_))
  {
    ifstream->clear();
    ifstream->close();
    failed = ifstream->fail();
  }
  else if(std::ofstream *const ofstream = dynamic_cast<std::ofstream*>(stream_out_))
  {
    ofstream->clear();
    ofstream->close();
    failed = ofstream->fail();
  }
  else
  {
    throw Glib::Error(G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_FAILED,
                      "Attempt to close non-file stream");
  }

  if(failed)
  {
    throw Glib::Error(G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_FAILED, "Failed to close stream");
  }

  return IO_STATUS_NORMAL;
}

IOStatus StreamIOChannel::set_flags_vfunc(IOFlags)
{
  return IO_STATUS_NORMAL;
}

IOFlags StreamIOChannel::get_flags_vfunc()
{
  gobj()->is_seekable  = 1;
  gobj()->is_readable  = (stream_in_  != nullptr);
  gobj()->is_writeable = (stream_out_ != nullptr);

  IOFlags flags = IO_FLAG_IS_SEEKABLE;

  if(stream_in_)
    flags |= IO_FLAG_IS_READABLE;
  if(stream_out_)
    flags |= IO_FLAG_IS_WRITEABLE;

  return flags;
}

Glib::RefPtr<Glib::Source> StreamIOChannel::create_watch_vfunc(IOCondition)
{
  g_warning("Glib::StreamIOChannel::create_watch_vfunc() not implemented");
  return Glib::RefPtr<Glib::Source>();
}

#endif //GLIBMM_DISABLE_DEPRECATED

} // namespace Glib