summaryrefslogtreecommitdiff
path: root/glib/src/fileutils.ccg
blob: 43dc1857cf701643a6e5190245288f1b07e02b80 (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
/* 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 <glib.h>
#include <glibmm/utility.h>


namespace Glib
{

/**** Glib::Dir ************************************************************/

Dir::Dir(const std::string& path)
{
  GError* error = nullptr;
  gobject_ = g_dir_open(path.c_str(), 0, &error);

  if(error)
    Glib::Error::throw_exception(error);
}

Dir::Dir(GDir* gobject)
:
  gobject_ (gobject)
{}

Dir::~Dir()
{
  if(gobject_)
    g_dir_close(gobject_);
}

std::string Dir::read_name()
{
  const char *const name = g_dir_read_name(gobject_);
  return Glib::convert_const_gchar_ptr_to_stdstring(name);
}

void Dir::rewind()
{
  g_dir_rewind(gobject_);
}

void Dir::close()
{
  if(gobject_)
  {
    g_dir_close(gobject_);
    gobject_ = nullptr;
  }
}

DirIterator Dir::begin()
{
  g_dir_rewind(gobject_);
  return DirIterator(gobject_, g_dir_read_name(gobject_));
}

DirIterator Dir::end()
{
  return DirIterator(gobject_, 0);
}


/**** Glib::DirIterator ****************************************************/

DirIterator::DirIterator()
:
  gobject_ (0),
  current_ (0)
{}

DirIterator::DirIterator(GDir* gobject, const char* current)
:
  gobject_ (gobject),
  current_ (current)
{}

std::string DirIterator::operator*() const
{
  return convert_const_gchar_ptr_to_stdstring(current_);
}

DirIterator& DirIterator::operator++()
{
  current_ = g_dir_read_name(gobject_);
  return *this;
}

void DirIterator::operator++(int)
{
  current_ = g_dir_read_name(gobject_);
}

bool DirIterator::operator==(const DirIterator& rhs) const
{
  return (current_ == rhs.current_);
}

bool DirIterator::operator!=(const DirIterator& rhs) const
{
  return (current_ != rhs.current_);
}


bool file_test(const std::string& filename, FileTest test)
{
  return g_file_test(filename.c_str(), static_cast<GFileTest>(unsigned(test)));
}

int mkstemp(std::string& filename_template)
{
  const ScopedPtr<char> buf (g_strndup(filename_template.data(), filename_template.size()));
  const auto fileno = g_mkstemp(buf.get());

  filename_template = buf.get();
  return fileno;
}

int file_open_tmp(std::string& name_used, const std::string& prefix)
{
  std::string basename_template (prefix);
  basename_template += "XXXXXX"; // this sillyness shouldn't be in the interface

  GError* error = nullptr;
  ScopedPtr<char> buf_name_used;

  const auto fileno = g_file_open_tmp(basename_template.c_str(), buf_name_used.addr(), &error);

  if(error)
    Glib::Error::throw_exception(error);

  name_used = buf_name_used.get();
  return fileno;
}

int file_open_tmp(std::string& name_used)
{
  GError* error = nullptr;
  ScopedPtr<char> buf_name_used;

  const auto fileno = g_file_open_tmp(0, buf_name_used.addr(), &error);

  if(error)
    Glib::Error::throw_exception(error);

  name_used = buf_name_used.get();
  return fileno;
}

std::string file_get_contents(const std::string& filename)
{
  ScopedPtr<char> contents;
  gsize   length = 0;
  GError* error  = nullptr;

  g_file_get_contents(filename.c_str(), contents.addr(), &length, &error);

  if(error)
    Glib::Error::throw_exception(error);

  return std::string(contents.get(), length);
}

void
file_set_contents(const std::string& filename,
                  const gchar *contents,
                  gssize length)
{
  GError* error  = nullptr;

  g_file_set_contents(filename.c_str(), contents, length, &error);

  if(error)
    Glib::Error::throw_exception(error);
}

void
file_set_contents (const std::string& filename, const std::string& contents)
{
    file_set_contents(filename, contents.c_str(), contents.size());
}

} // namespace Glib