rollback to 3428, before the mysterious removal of libs/* at 3431/3432
[ardour.git] / libs / glibmm2 / glib / glibmm / fileutils.cc
1 // Generated by gtkmmproc -- DO NOT MODIFY!
2
3
4 #include <glibmm/fileutils.h>
5 #include <glibmm/private/fileutils_p.h>
6
7 // -*- c++ -*-
8 /* $Id: fileutils.ccg,v 1.1 2003/01/07 16:58:25 murrayc Exp $ */
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 Library General Public
14  * License as published by the Free Software Foundation; either
15  * version 2 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  * Library General Public License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with this library; if not, write to the Free
24  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  */
26
27 #include <glib/gdir.h>
28 #include <glib/gfileutils.h>
29 #include <glib/gstrfuncs.h>
30 #include <glibmm/utility.h>
31
32
33 namespace Glib
34 {
35
36 /**** Glib::Dir ************************************************************/
37
38 Dir::Dir(const std::string& path)
39 {
40   GError* error = 0;
41   gobject_ = g_dir_open(path.c_str(), 0, &error);
42
43   if(error)
44     Glib::Error::throw_exception(error);
45 }
46
47 Dir::Dir(GDir* gobject)
48 :
49   gobject_ (gobject)
50 {}
51
52 Dir::~Dir()
53 {
54   if(gobject_)
55     g_dir_close(gobject_);
56 }
57
58 std::string Dir::read_name()
59 {
60   const char *const name = g_dir_read_name(gobject_);
61   return (name) ? std::string(name) : std::string();
62 }
63
64 void Dir::rewind()
65 {
66   g_dir_rewind(gobject_);
67 }
68
69 void Dir::close()
70 {
71   if(gobject_)
72   {
73     g_dir_close(gobject_);
74     gobject_ = 0;
75   }
76 }
77
78 DirIterator Dir::begin()
79 {
80   g_dir_rewind(gobject_);
81   return DirIterator(gobject_, g_dir_read_name(gobject_));
82 }
83
84 DirIterator Dir::end()
85 {
86   return DirIterator(gobject_, 0);
87 }
88
89
90 /**** Glib::DirIterator ****************************************************/
91
92 DirIterator::DirIterator()
93 :
94   gobject_ (0),
95   current_ (0)
96 {}
97
98 DirIterator::DirIterator(GDir* gobject, const char* current)
99 :
100   gobject_ (gobject),
101   current_ (current)
102 {}
103
104 std::string DirIterator::operator*() const
105 {
106   return (current_) ? std::string(current_) : std::string();
107 }
108
109 DirIterator& DirIterator::operator++()
110 {
111   current_ = g_dir_read_name(gobject_);
112   return *this;
113 }
114
115 void DirIterator::operator++(int)
116 {
117   current_ = g_dir_read_name(gobject_);
118 }
119
120 bool DirIterator::operator==(const DirIterator& rhs) const
121 {
122   return (current_ == rhs.current_);
123 }
124
125 bool DirIterator::operator!=(const DirIterator& rhs) const
126 {
127   return (current_ != rhs.current_);
128 }
129
130
131 bool file_test(const std::string& filename, FileTest test)
132 {
133   return g_file_test(filename.c_str(), static_cast<GFileTest>(unsigned(test)));
134 }
135
136 int mkstemp(std::string& filename_template)
137 {
138   const ScopedPtr<char> buf (g_strndup(filename_template.data(), filename_template.size()));
139   const int fileno = g_mkstemp(buf.get());
140
141   filename_template = buf.get();
142   return fileno;
143 }
144
145 int 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 = 0;
151   ScopedPtr<char> buf_name_used;
152
153   const int fileno = g_file_open_tmp(basename_template.c_str(), buf_name_used.addr(), &error);
154
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 {
164   GError* error = 0;
165   ScopedPtr<char> buf_name_used;
166
167   const int fileno = g_file_open_tmp(0, buf_name_used.addr(), &error);
168
169   if(error)
170     Glib::Error::throw_exception(error);
171
172   name_used = buf_name_used.get();
173   return fileno;
174 }
175
176 std::string file_get_contents(const std::string& filename)
177 {
178   ScopedPtr<char> contents;
179   gsize   length = 0;
180   GError* error  = 0;
181
182   g_file_get_contents(filename.c_str(), contents.addr(), &length, &error);
183
184   if(error)
185     Glib::Error::throw_exception(error);
186
187   return std::string(contents.get(), length);
188 }
189
190 } // namespace Glib
191
192
193 namespace
194 {
195 } // anonymous namespace
196
197
198 Glib::FileError::FileError(Glib::FileError::Code error_code, const Glib::ustring& error_message)
199 :
200   Glib::Error (G_FILE_ERROR, error_code, error_message)
201 {}
202
203 Glib::FileError::FileError(GError* gobject)
204 :
205   Glib::Error (gobject)
206 {}
207
208 Glib::FileError::Code Glib::FileError::code() const
209 {
210   return static_cast<Code>(Glib::Error::code());
211 }
212
213 #ifdef GLIBMM_EXCEPTIONS_ENABLED
214 void Glib::FileError::throw_func(GError* gobject)
215 {
216   throw Glib::FileError(gobject);
217 }
218 #else
219 //When not using exceptions, we just pass the Exception object around without throwing it:
220 std::auto_ptr<Glib::Error> Glib::FileError::throw_func(GError* gobject)
221 {
222   return std::auto_ptr<Glib::Error>(new Glib::FileError(gobject));
223 }
224 #endif //GLIBMM_EXCEPTIONS_ENABLED
225
226