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