Fix three minor memory leaks in the Editor by using Gtk::manage
[ardour.git] / libs / glibmm2 / glibmm / stringutils.cc
1 // -*- c++ -*-
2 /* $Id$ */
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     throw std::out_of_range("out of range (strtod): start_index > str.size()");
55
56   const char *const bufptr = str.c_str();
57   char* endptr = 0;
58
59   const double result = g_ascii_strtod(bufptr + start_index, &endptr);
60   const int    err_no = errno;
61
62   if(err_no != 0)
63   {
64     g_return_val_if_fail(err_no == ERANGE, result);
65
66     if(result > 0.0)
67       throw std::overflow_error("overflow (strtod): positive number too large");
68
69     if(result < 0.0)
70       throw std::overflow_error("overflow (strtod): negative number too large");
71
72     throw std::underflow_error("underflow (strtod): number too small");
73   }
74
75   if(endptr)
76     end_index = endptr - bufptr;
77   else
78     end_index = str.size();
79
80   return result;
81 }
82
83 std::string Glib::Ascii::dtostr(double d)
84 {
85   char buf[G_ASCII_DTOSTR_BUF_SIZE];
86
87   return g_ascii_dtostr(buf, sizeof(buf), d);
88 }
89
90 std::string Glib::strescape(const std::string& source)
91 {
92   const Glib::ScopedPtr<char> buf (g_strescape(source.c_str(), 0));
93   return buf.get();
94 }
95
96 std::string Glib::strescape(const std::string& source, const std::string& exceptions)
97 {
98   const Glib::ScopedPtr<char> buf (g_strescape(source.c_str(), exceptions.c_str()));
99   return buf.get();
100 }
101
102 std::string Glib::strcompress(const std::string& source)
103 {
104   const Glib::ScopedPtr<char> buf (g_strcompress(source.c_str()));
105   return buf.get();
106 }
107
108 Glib::ustring Glib::strerror(int errnum)
109 {
110   return g_strerror(errnum);
111 }
112
113 Glib::ustring Glib::strsignal(int signum)
114 {
115   return g_strsignal(signum);
116 }
117