2 /* $Id: fileutils.ccg,v 1.1 2003/01/07 16:58:25 murrayc Exp $ */
4 /* Copyright (C) 2002 The gtkmm Development Team
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <glibmm/utility.h>
28 /**** Glib::Dir ************************************************************/
30 Dir::Dir(const std::string& path)
33 gobject_ = g_dir_open(path.c_str(), 0, &error);
36 Glib::Error::throw_exception(error);
39 Dir::Dir(GDir* gobject)
47 g_dir_close(gobject_);
50 std::string Dir::read_name()
52 const char *const name = g_dir_read_name(gobject_);
53 return Glib::convert_const_gchar_ptr_to_stdstring(name);
58 g_dir_rewind(gobject_);
65 g_dir_close(gobject_);
70 DirIterator Dir::begin()
72 g_dir_rewind(gobject_);
73 return DirIterator(gobject_, g_dir_read_name(gobject_));
76 DirIterator Dir::end()
78 return DirIterator(gobject_, 0);
82 /**** Glib::DirIterator ****************************************************/
84 DirIterator::DirIterator()
90 DirIterator::DirIterator(GDir* gobject, const char* current)
96 std::string DirIterator::operator*() const
98 return (current_) ? std::string(current_) : std::string();
101 DirIterator& DirIterator::operator++()
103 current_ = g_dir_read_name(gobject_);
107 void DirIterator::operator++(int)
109 current_ = g_dir_read_name(gobject_);
112 bool DirIterator::operator==(const DirIterator& rhs) const
114 return (current_ == rhs.current_);
117 bool DirIterator::operator!=(const DirIterator& rhs) const
119 return (current_ != rhs.current_);
123 bool file_test(const std::string& filename, FileTest test)
125 return g_file_test(filename.c_str(), static_cast<GFileTest>(unsigned(test)));
128 int mkstemp(std::string& filename_template)
130 const ScopedPtr<char> buf (g_strndup(filename_template.data(), filename_template.size()));
131 const int fileno = g_mkstemp(buf.get());
133 filename_template = buf.get();
137 int file_open_tmp(std::string& name_used, const std::string& prefix)
139 std::string basename_template (prefix);
140 basename_template += "XXXXXX"; // this sillyness shouldn't be in the interface
143 ScopedPtr<char> buf_name_used;
145 const int fileno = g_file_open_tmp(basename_template.c_str(), buf_name_used.addr(), &error);
148 Glib::Error::throw_exception(error);
150 name_used = buf_name_used.get();
154 int file_open_tmp(std::string& name_used)
157 ScopedPtr<char> buf_name_used;
159 const int fileno = g_file_open_tmp(0, buf_name_used.addr(), &error);
162 Glib::Error::throw_exception(error);
164 name_used = buf_name_used.get();
168 std::string file_get_contents(const std::string& filename)
170 ScopedPtr<char> contents;
174 g_file_get_contents(filename.c_str(), contents.addr(), &length, &error);
177 Glib::Error::throw_exception(error);
179 return std::string(contents.get(), length);