summaryrefslogtreecommitdiff
path: root/glib/src/fileutils.ccg
diff options
context:
space:
mode:
Diffstat (limited to 'glib/src/fileutils.ccg')
-rw-r--r--glib/src/fileutils.ccg185
1 files changed, 185 insertions, 0 deletions
diff --git a/glib/src/fileutils.ccg b/glib/src/fileutils.ccg
new file mode 100644
index 00000000..144c48b5
--- /dev/null
+++ b/glib/src/fileutils.ccg
@@ -0,0 +1,185 @@
+// -*- c++ -*-
+/* $Id$ */
+
+/* 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 Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 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
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library 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/gdir.h>
+#include <glib/gfileutils.h>
+#include <glib/gstrfuncs.h>
+#include <glibmm/utility.h>
+
+
+namespace Glib
+{
+
+/**** Glib::Dir ************************************************************/
+
+Dir::Dir(const std::string& path)
+{
+ GError* error = 0;
+ 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 (name) ? std::string(name) : std::string();
+}
+
+void Dir::rewind()
+{
+ g_dir_rewind(gobject_);
+}
+
+void Dir::close()
+{
+ if(gobject_)
+ {
+ g_dir_close(gobject_);
+ gobject_ = 0;
+ }
+}
+
+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 (current_) ? std::string(current_) : std::string();
+}
+
+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 int 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 = 0;
+ ScopedPtr<char> buf_name_used;
+
+ const int 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 = 0;
+ ScopedPtr<char> buf_name_used;
+
+ const int 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 = 0;
+
+ g_file_get_contents(filename.c_str(), contents.addr(), &length, &error);
+
+ if(error)
+ Glib::Error::throw_exception(error);
+
+ return std::string(contents.get(), length);
+}
+
+} // namespace Glib
+