More gracefully handle type mismatch errors when doing playlist things (just ignore...
[ardour.git] / libs / glibmm2 / glib / glibmm / stringutils.cc
1 // -*- c++ -*-
2 /* $Id: stringutils.cc 291 2006-05-12 08:08:45Z murrayc $ */
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 <glibmm/stringutils.h>
22 #include <glibmm/utility.h>
23 #include <glib.h>
24 #include <cerrno>
25 #include <stdexcept>
26 #include <glibmmconfig.h>
27
28 GLIBMM_USING_STD(out_of_range)
29 GLIBMM_USING_STD(overflow_error)
30 GLIBMM_USING_STD(underflow_error)
31
32
33 bool Glib::str_has_prefix(const std::string& str, const std::string& prefix)
34 {
35   return g_str_has_prefix(str.c_str(), prefix.c_str());
36 }
37
38 bool Glib::str_has_suffix(const std::string& str, const std::string& suffix)
39 {
40   return g_str_has_suffix(str.c_str(), suffix.c_str());
41 }
42
43 double Glib::Ascii::strtod(const std::string& str)
44 {
45   std::string::size_type dummy;
46   return Glib::Ascii::strtod(str, dummy, 0);
47 }
48
49 double Glib::Ascii::strtod(const std::string&      str,
50                            std::string::size_type& end_index,
51                            std::string::size_type  start_index)
52 {
53   if(start_index > str.size())
54   {
55     #ifdef GLIBMM_EXCEPTIONS_ENABLED
56     throw std::out_of_range("out of range (strtod): start_index > str.size()");
57     #else
58       return 0;
59     #endif //GLIBMM_EXCEPTIONS_ENABLED
60   }
61
62   const char *const bufptr = str.c_str();
63   char* endptr = 0;
64
65   const double result = g_ascii_strtod(bufptr + start_index, &endptr);
66   const int    err_no = errno;
67
68   if(err_no != 0)
69   {
70     g_return_val_if_fail(err_no == ERANGE, result);
71
72     #ifdef GLIBMM_EXCEPTIONS_ENABLED
73     //Interpret the result in the event of an error:
74     if(result > 0.0)
75       throw std::overflow_error("overflow (strtod): positive number too large");
76
77     if(result < 0.0)
78       throw std::overflow_error("overflow (strtod): negative number too large");
79
80     throw std::underflow_error("underflow (strtod): number too small");
81     #else
82     return result;
83     #endif // GLIBMM_EXCEPTIONS_ENABLED
84   }
85
86   if(endptr)
87     end_index = endptr - bufptr;
88   else
89     end_index = str.size();
90
91   return result;
92 }
93
94 std::string Glib::Ascii::dtostr(double d)
95 {
96   char buf[G_ASCII_DTOSTR_BUF_SIZE];
97
98   return g_ascii_dtostr(buf, sizeof(buf), d);
99 }
100
101 std::string Glib::strescape(const std::string& source)
102 {
103   const Glib::ScopedPtr<char> buf (g_strescape(source.c_str(), 0));
104   return buf.get();
105 }
106
107 std::string Glib::strescape(const std::string& source, const std::string& exceptions)
108 {
109   const Glib::ScopedPtr<char> buf (g_strescape(source.c_str(), exceptions.c_str()));
110   return buf.get();
111 }
112
113 std::string Glib::strcompress(const std::string& source)
114 {
115   const Glib::ScopedPtr<char> buf (g_strcompress(source.c_str()));
116   return buf.get();
117 }
118
119 Glib::ustring Glib::strerror(int errnum)
120 {
121   return g_strerror(errnum);
122 }
123
124 Glib::ustring Glib::strsignal(int signum)
125 {
126   return g_strsignal(signum);
127 }
128