More gracefully handle type mismatch errors when doing playlist things (just ignore...
[ardour.git] / libs / glibmm2 / glib / src / regex.ccg
1 /* Copyright (C) 2007 The glibmm Development Team
2  *
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Library General Public
5  * License as published by the Free Software Foundation; either
6  * version 2 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * Library General Public License for more details.
12  *
13  * You should have received a copy of the GNU Library General Public
14  * License along with this library; if not, write to the Free
15  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16  */
17
18 #include <glibmm/utility.h>
19
20 namespace Glib
21 {
22
23 #ifdef GLIBMM_EXCEPTIONS_ENABLED
24 Glib::RefPtr<Glib::Regex> Regex::create(const Glib::ustring& pattern,
25                                         RegexCompileFlags compile_options,
26                                         RegexMatchFlags match_options)
27 #else
28 Glib::RefPtr<Glib::Regex> Regex::create(const Glib::ustring& pattern,
29                                         RegexCompileFlags compile_options,
30                                         RegexMatchFlags match_options,
31                                         std::auto_ptr<Glib::Error>& error)
32 #endif /* GLIBMM_EXCEPTIONS_ENABLED */
33 {
34   GError* gerror = 0;
35   GRegex* regex  = g_regex_new(pattern.c_str(), (GRegexCompileFlags)compile_options,
36                                (GRegexMatchFlags)match_options, &gerror);
37
38   if(gerror)
39 #ifdef GLIBMM_EXCEPTIONS_ENABLED
40     Glib::Error::throw_exception(gerror);
41 #else
42     error = Glib::Error::throw_exception(gerror);
43 #endif
44   return Glib::wrap(regex);
45 }
46
47 // static
48 Glib::ustring Regex::escape_string(const Glib::ustring& string)
49 {
50   const Glib::ScopedPtr<char> buf (g_regex_escape_string(string.raw().c_str(),
51                                                          string.raw().size()));
52   return Glib::ustring(buf.get());
53 }
54
55 bool Regex::match(const Glib::ustring& string, RegexMatchFlags match_options)
56 {
57   return g_regex_match(gobj(), string.c_str(), (GRegexMatchFlags)(match_options), 0);
58 }
59
60
61 #ifdef GLIBMM_EXCEPTIONS_ENABLED
62 bool Regex::match(const Glib::ustring& string, int start_position, RegexMatchFlags match_options)
63 #else
64 bool Regex::match(const Glib::ustring& string, int start_position, RegexMatchFlags match_options, std::auto_ptr<Glib::Error>& error)
65 #endif //GLIBMM_EXCEPTIONS_ENABLED
66 {
67   GError* gerror = 0;
68   bool retvalue = g_regex_match_full(gobj(), string.c_str(), -1, start_position, ((GRegexMatchFlags)(match_options)), 0, &(gerror));
69 #ifdef GLIBMM_EXCEPTIONS_ENABLED
70   if(gerror)
71     ::Glib::Error::throw_exception(gerror);
72 #else
73   if(gerror)
74     error = ::Glib::Error::throw_exception(gerror);
75 #endif //GLIBMM_EXCEPTIONS_ENABLED
76
77   return retvalue;
78 }
79
80 #ifdef GLIBMM_EXCEPTIONS_ENABLED
81 bool Regex::match(const Glib::ustring& string, gssize string_len, int start_position, RegexMatchFlags match_options)
82 #else
83 bool Regex::match(const Glib::ustring& string, gssize string_len, int start_position, RegexMatchFlags match_options, std::auto_ptr<Glib::Error>& error)
84 #endif //GLIBMM_EXCEPTIONS_ENABLED
85 {
86   GError* gerror = 0;
87   bool retvalue = g_regex_match_full(gobj(), string.c_str(), string_len, start_position, ((GRegexMatchFlags)(match_options)), 0, &(gerror));
88 #ifdef GLIBMM_EXCEPTIONS_ENABLED
89   if(gerror)
90     ::Glib::Error::throw_exception(gerror);
91 #else
92   if(gerror)
93     error = ::Glib::Error::throw_exception(gerror);
94 #endif //GLIBMM_EXCEPTIONS_ENABLED
95
96   return retvalue;
97 }
98
99
100 bool Regex::match_all(const Glib::ustring& string, RegexMatchFlags match_options)
101 {
102   return g_regex_match_all(gobj(), string.c_str(), ((GRegexMatchFlags)(match_options)), 0);
103 }
104
105
106 #ifdef GLIBMM_EXCEPTIONS_ENABLED
107 bool Regex::match_all(const Glib::ustring& string, int start_position, RegexMatchFlags match_options)
108 #else
109 bool Regex::match_all(const Glib::ustring& string, int start_position, RegexMatchFlags match_options, std::auto_ptr<Glib::Error>& error)
110 #endif //GLIBMM_EXCEPTIONS_ENABLED
111 {
112   GError* gerror = 0;
113   bool retvalue = g_regex_match_all_full(gobj(), string.c_str(), -1, start_position, ((GRegexMatchFlags)(match_options)), 0, &(gerror));
114 #ifdef GLIBMM_EXCEPTIONS_ENABLED
115   if(gerror)
116     ::Glib::Error::throw_exception(gerror);
117 #else
118   if(gerror)
119     error = ::Glib::Error::throw_exception(gerror);
120 #endif //GLIBMM_EXCEPTIONS_ENABLED
121
122   return retvalue;
123 }
124
125 #ifdef GLIBMM_EXCEPTIONS_ENABLED
126 bool Regex::match_all(const Glib::ustring& string, gssize string_len, int start_position, RegexMatchFlags match_options)
127 #else
128 bool Regex::match_all(const Glib::ustring& string, gssize string_len, int start_position, RegexMatchFlags match_options, std::auto_ptr<Glib::Error>& error)
129 #endif //GLIBMM_EXCEPTIONS_ENABLED
130 {
131   GError* gerror = 0;
132   bool retvalue = g_regex_match_all_full(gobj(), string.c_str(), string_len, start_position, ((GRegexMatchFlags)(match_options)), 0, &(gerror));
133 #ifdef GLIBMM_EXCEPTIONS_ENABLED
134   if(gerror)
135     ::Glib::Error::throw_exception(gerror);
136 #else
137   if(gerror)
138     error = ::Glib::Error::throw_exception(gerror);
139 #endif //GLIBMM_EXCEPTIONS_ENABLED
140
141   return retvalue;
142 }
143
144
145
146 #ifdef GLIBMM_EXCEPTIONS_ENABLED
147 Glib::ustring Regex::replace(const Glib::ustring& string, int start_position, const Glib::ustring& replacement, RegexMatchFlags match_options)
148 #else
149 Glib::ustring Regex::replace(const Glib::ustring& string, int start_position, const Glib::ustring& replacement, RegexMatchFlags match_options, std::auto_ptr<Glib::Error>& error)
150 #endif //GLIBMM_EXCEPTIONS_ENABLED
151 {
152   GError* gerror = 0;
153   Glib::ustring retvalue = Glib::convert_return_gchar_ptr_to_ustring(g_regex_replace(gobj(), string.c_str(), -1, start_position, replacement.c_str(), ((GRegexMatchFlags)(match_options)), &(gerror)));
154 #ifdef GLIBMM_EXCEPTIONS_ENABLED
155   if(gerror)
156     ::Glib::Error::throw_exception(gerror);
157 #else
158   if(gerror)
159     error = ::Glib::Error::throw_exception(gerror);
160 #endif //GLIBMM_EXCEPTIONS_ENABLED
161
162   return retvalue;
163 }
164
165
166 #ifdef GLIBMM_EXCEPTIONS_ENABLED
167 Glib::ustring Regex::replace_literal(const Glib::ustring& string, int start_position, const Glib::ustring& replacement, RegexMatchFlags match_options)
168 #else
169 Glib::ustring Regex::replace_literal(const Glib::ustring& string, int start_position, const Glib::ustring& replacement, RegexMatchFlags match_options, std::auto_ptr<Glib::Error>& error)
170 #endif //GLIBMM_EXCEPTIONS_ENABLED
171 {
172   GError* gerror = 0;
173   Glib::ustring retvalue = Glib::convert_return_gchar_ptr_to_ustring(g_regex_replace_literal(gobj(), string.c_str(), -1, start_position, replacement.c_str(), ((GRegexMatchFlags)(match_options)), &(gerror)));
174 #ifdef GLIBMM_EXCEPTIONS_ENABLED
175   if(gerror)
176     ::Glib::Error::throw_exception(gerror);
177 #else
178   if(gerror)
179     error = ::Glib::Error::throw_exception(gerror);
180 #endif //GLIBMM_EXCEPTIONS_ENABLED
181
182   return retvalue;
183 }
184
185 #ifdef GLIBMM_EXCEPTIONS_ENABLED
186 Glib::StringArrayHandle Regex::split(const Glib::ustring& string, int start_position, RegexMatchFlags match_options, int max_tokens) const
187 #else
188 Glib::StringArrayHandle Regex::split(const Glib::ustring& string, int start_position, RegexMatchFlags match_options, int max_tokens, std::auto_ptr<Glib::Error>& error) const
189 #endif //GLIBMM_EXCEPTIONS_ENABLED
190 {
191   GError* gerror = 0;
192   Glib::StringArrayHandle retvalue = Glib::StringArrayHandle(g_regex_split_full(const_cast<GRegex*>(gobj()), string.c_str(), -1, start_position, ((GRegexMatchFlags)(match_options)), max_tokens, &(gerror)), Glib::OWNERSHIP_DEEP);
193 #ifdef GLIBMM_EXCEPTIONS_ENABLED
194   if(gerror)
195     ::Glib::Error::throw_exception(gerror);
196 #else
197   if(gerror)
198     error = ::Glib::Error::throw_exception(gerror);
199 #endif //GLIBMM_EXCEPTIONS_ENABLED
200
201   return retvalue;
202 }
203
204 } // namespace Glib