1 // Generated by gmmproc 2.64.2 -- DO NOT MODIFY!
2 
3 
4 #include <glibmm.h>
5 
6 #include <glibmm/fileutils.h>
7 #include <glibmm/private/fileutils_p.h>
8 
9 
10 /* Copyright (C) 2002 The gtkmm Development Team
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 #include <glib.h>
27 #include <glibmm/utility.h>
28 
29 namespace Glib
30 {
31 
32 /**** Glib::Dir ************************************************************/
33 
Dir(const std::string & path)34 Dir::Dir(const std::string& path)
35 {
36   GError* error = nullptr;
37   gobject_ = g_dir_open(path.c_str(), 0, &error);
38 
39   if (error)
40     Glib::Error::throw_exception(error);
41 }
42 
Dir(GDir * gobject)43 Dir::Dir(GDir* gobject) : gobject_(gobject)
44 {
45 }
46 
~Dir()47 Dir::~Dir()
48 {
49   if (gobject_)
50     g_dir_close(gobject_);
51 }
52 
53 std::string
read_name()54 Dir::read_name()
55 {
56   const char* const name = g_dir_read_name(gobject_);
57   return Glib::convert_const_gchar_ptr_to_stdstring(name);
58 }
59 
60 void
rewind()61 Dir::rewind()
62 {
63   g_dir_rewind(gobject_);
64 }
65 
66 void
close()67 Dir::close()
68 {
69   if (gobject_)
70   {
71     g_dir_close(gobject_);
72     gobject_ = nullptr;
73   }
74 }
75 
76 DirIterator
begin()77 Dir::begin()
78 {
79   g_dir_rewind(gobject_);
80   return DirIterator(gobject_, g_dir_read_name(gobject_));
81 }
82 
83 DirIterator
end()84 Dir::end()
85 {
86   return DirIterator(gobject_, nullptr);
87 }
88 
89 /**** Glib::DirIterator ****************************************************/
90 
DirIterator()91 DirIterator::DirIterator() : gobject_(nullptr), current_(nullptr)
92 {
93 }
94 
DirIterator(GDir * gobject,const char * current)95 DirIterator::DirIterator(GDir* gobject, const char* current) : gobject_(gobject), current_(current)
96 {
97 }
98 
operator *() const99 std::string DirIterator::operator*() const
100 {
101   return convert_const_gchar_ptr_to_stdstring(current_);
102 }
103 
operator ++()104 DirIterator& DirIterator::operator++()
105 {
106   current_ = g_dir_read_name(gobject_);
107   return *this;
108 }
109 
operator ++(int)110 void DirIterator::operator++(int)
111 {
112   current_ = g_dir_read_name(gobject_);
113 }
114 
115 bool
operator ==(const DirIterator & rhs) const116 DirIterator::operator==(const DirIterator& rhs) const
117 {
118   return (current_ == rhs.current_);
119 }
120 
121 bool
operator !=(const DirIterator & rhs) const122 DirIterator::operator!=(const DirIterator& rhs) const
123 {
124   return (current_ != rhs.current_);
125 }
126 
127 bool
file_test(const std::string & filename,FileTest test)128 file_test(const std::string& filename, FileTest test)
129 {
130   return g_file_test(filename.c_str(), static_cast<GFileTest>(unsigned(test)));
131 }
132 
133 int
mkstemp(std::string & filename_template)134 mkstemp(std::string& filename_template)
135 {
136   const auto buf =
137     make_unique_ptr_gfree(g_strndup(filename_template.data(), filename_template.size()));
138   const auto fileno = g_mkstemp(buf.get());
139 
140   filename_template = buf.get();
141   return fileno;
142 }
143 
144 int
file_open_tmp(std::string & name_used,const std::string & prefix)145 file_open_tmp(std::string& name_used, const std::string& prefix)
146 {
147   std::string basename_template(prefix);
148   basename_template += "XXXXXX"; // this sillyness shouldn't be in the interface
149 
150   GError* error = nullptr;
151   char* pch_buf_name_used = nullptr;
152 
153   const auto fileno = g_file_open_tmp(basename_template.c_str(), &pch_buf_name_used, &error);
154   auto buf_name_used = make_unique_ptr_gfree(pch_buf_name_used);
155   if (error)
156     Glib::Error::throw_exception(error);
157 
158   name_used = buf_name_used.get();
159   return fileno;
160 }
161 
162 int
file_open_tmp(std::string & name_used)163 file_open_tmp(std::string& name_used)
164 {
165   GError* error = nullptr;
166   char* pch_buf_name_used = nullptr;
167 
168   const auto fileno = g_file_open_tmp(nullptr, &pch_buf_name_used, &error);
169   auto buf_name_used = make_unique_ptr_gfree(pch_buf_name_used);
170   if (error)
171     Glib::Error::throw_exception(error);
172 
173   name_used = buf_name_used.get();
174   return fileno;
175 }
176 
177 std::string
file_get_contents(const std::string & filename)178 file_get_contents(const std::string& filename)
179 {
180   gsize length = 0;
181   GError* error = nullptr;
182 
183   char* pch_contents = nullptr;
184   g_file_get_contents(filename.c_str(), &pch_contents, &length, &error);
185   auto contents = make_unique_ptr_gfree(pch_contents);
186   if (error)
187     Glib::Error::throw_exception(error);
188 
189   return std::string(contents.get(), length);
190 }
191 
192 void
file_set_contents(const std::string & filename,const gchar * contents,gssize length)193 file_set_contents(const std::string& filename, const gchar* contents, gssize length)
194 {
195   GError* error = nullptr;
196 
197   g_file_set_contents(filename.c_str(), contents, length, &error);
198 
199   if (error)
200     Glib::Error::throw_exception(error);
201 }
202 
203 void
file_set_contents(const std::string & filename,const std::string & contents)204 file_set_contents(const std::string& filename, const std::string& contents)
205 {
206   file_set_contents(filename, contents.c_str(), contents.size());
207 }
208 
209 } // namespace Glib
210 
211 namespace
212 {
213 } // anonymous namespace
214 
215 
FileError(Glib::FileError::Code error_code,const Glib::ustring & error_message)216 Glib::FileError::FileError(Glib::FileError::Code error_code, const Glib::ustring& error_message)
217 :
218   Glib::Error (G_FILE_ERROR, error_code, error_message)
219 {}
220 
FileError(GError * gobject)221 Glib::FileError::FileError(GError* gobject)
222 :
223   Glib::Error (gobject)
224 {}
225 
code() const226 Glib::FileError::Code Glib::FileError::code() const
227 {
228   return static_cast<Code>(Glib::Error::code());
229 }
230 
throw_func(GError * gobject)231 void Glib::FileError::throw_func(GError* gobject)
232 {
233   throw Glib::FileError(gobject);
234 }
235 
236 
237