Fix crash on startup if an LV2 plugin has a bad .ttl file.
[ardour.git] / libs / glibmm2 / glib / src / fileutils.ccg
1 // -*- c++ -*-
2 /* $Id: fileutils.ccg,v 1.1 2003/01/07 16:58:25 murrayc Exp $ */
3
4 /* Copyright (C) 2002 The gtkmm Development Team
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include <glib.h>
22 #include <glibmm/utility.h>
23
24
25 namespace Glib
26 {
27
28 /**** Glib::Dir ************************************************************/
29
30 Dir::Dir(const std::string& path)
31 {
32   GError* error = 0;
33   gobject_ = g_dir_open(path.c_str(), 0, &error);
34
35   if(error)
36     Glib::Error::throw_exception(error);
37 }
38
39 Dir::Dir(GDir* gobject)
40 :
41   gobject_ (gobject)
42 {}
43
44 Dir::~Dir()
45 {
46   if(gobject_)
47     g_dir_close(gobject_);
48 }
49
50 std::string Dir::read_name()
51 {
52   const char *const name = g_dir_read_name(gobject_);
53   return Glib::convert_const_gchar_ptr_to_stdstring(name);
54 }
55
56 void Dir::rewind()
57 {
58   g_dir_rewind(gobject_);
59 }
60
61 void Dir::close()
62 {
63   if(gobject_)
64   {
65     g_dir_close(gobject_);
66     gobject_ = 0;
67   }
68 }
69
70 DirIterator Dir::begin()
71 {
72   g_dir_rewind(gobject_);
73   return DirIterator(gobject_, g_dir_read_name(gobject_));
74 }
75
76 DirIterator Dir::end()
77 {
78   return DirIterator(gobject_, 0);
79 }
80
81
82 /**** Glib::DirIterator ****************************************************/
83
84 DirIterator::DirIterator()
85 :
86   gobject_ (0),
87   current_ (0)
88 {}
89
90 DirIterator::DirIterator(GDir* gobject, const char* current)
91 :
92   gobject_ (gobject),
93   current_ (current)
94 {}
95
96 std::string DirIterator::operator*() const
97 {
98   return (current_) ? std::string(current_) : std::string();
99 }
100
101 DirIterator& DirIterator::operator++()
102 {
103   current_ = g_dir_read_name(gobject_);
104   return *this;
105 }
106
107 void DirIterator::operator++(int)
108 {
109   current_ = g_dir_read_name(gobject_);
110 }
111
112 bool DirIterator::operator==(const DirIterator& rhs) const
113 {
114   return (current_ == rhs.current_);
115 }
116
117 bool DirIterator::operator!=(const DirIterator& rhs) const
118 {
119   return (current_ != rhs.current_);
120 }
121
122
123 bool file_test(const std::string& filename, FileTest test)
124 {
125   return g_file_test(filename.c_str(), static_cast<GFileTest>(unsigned(test)));
126 }
127
128 int mkstemp(std::string& filename_template)
129 {
130   const ScopedPtr<char> buf (g_strndup(filename_template.data(), filename_template.size()));
131   const int fileno = g_mkstemp(buf.get());
132
133   filename_template = buf.get();
134   return fileno;
135 }
136
137 int file_open_tmp(std::string& name_used, const std::string& prefix)
138 {
139   std::string basename_template (prefix);
140   basename_template += "XXXXXX"; // this sillyness shouldn't be in the interface
141
142   GError* error = 0;
143   ScopedPtr<char> buf_name_used;
144
145   const int fileno = g_file_open_tmp(basename_template.c_str(), buf_name_used.addr(), &error);
146
147   if(error)
148     Glib::Error::throw_exception(error);
149
150   name_used = buf_name_used.get();
151   return fileno;
152 }
153
154 int file_open_tmp(std::string& name_used)
155 {
156   GError* error = 0;
157   ScopedPtr<char> buf_name_used;
158
159   const int fileno = g_file_open_tmp(0, buf_name_used.addr(), &error);
160
161   if(error)
162     Glib::Error::throw_exception(error);
163
164   name_used = buf_name_used.get();
165   return fileno;
166 }
167
168 std::string file_get_contents(const std::string& filename)
169 {
170   ScopedPtr<char> contents;
171   gsize   length = 0;
172   GError* error  = 0;
173
174   g_file_get_contents(filename.c_str(), contents.addr(), &length, &error);
175
176   if(error)
177     Glib::Error::throw_exception(error);
178
179   return std::string(contents.get(), length);
180 }
181
182 } // namespace Glib
183