~ubuntu-branches/ubuntu/karmic/me-tv/karmic

« back to all changes in this revision

Viewing changes to src/string_utility.cc

  • Committer: Bazaar Package Importer
  • Author(s): Michael Lamothe
  • Date: 2007-12-19 23:30:16 UTC
  • Revision ID: james.westby@ubuntu.com-20071219233016-2ng2clfh00xtlevc
Tags: upstream-0.4.19
ImportĀ upstreamĀ versionĀ 0.4.19

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2007 Michael Lamothe
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2 of the License, or
 
7
 * (at your option) any later version.
 
8
 * 
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU Library General Public License for more details.
 
13
 * 
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA
 
17
 */
 
18
 
 
19
#include "string_utility.h"
 
20
#include "exception.h"
 
21
 
 
22
Glib::ustring StringUtility::format(const char* fmt, ...)
 
23
{
 
24
        va_list ap;
 
25
        
 
26
        va_start(ap, fmt);
 
27
        gchar* text = g_strdup_vprintf(fmt, ap);
 
28
        Glib::ustring result = text;
 
29
        g_free(text);
 
30
        va_end(ap);
 
31
        
 
32
        return result;
 
33
}
 
34
        
 
35
Glib::ustring StringUtility::get_buffer(const char* buffer, unsigned int length)
 
36
{
 
37
        Glib::ustring result;
 
38
        result.append(buffer, length);
 
39
        return result;
 
40
}
 
41
 
 
42
Glib::ustring StringUtility::escape(const Glib::ustring& source)
 
43
{
 
44
        Glib::ustring result;
 
45
        const gchar* destination = g_strescape(source.c_str(), NULL);
 
46
        result = destination;
 
47
        delete [] destination;
 
48
        return result;
 
49
}
 
50
 
 
51
StringSplitter::StringSplitter(const Glib::ustring& text, const char* deliminator, int max_length)
 
52
{
 
53
        count = 0;
 
54
        parts = g_strsplit(text.c_str(), deliminator, max_length);
 
55
        gchar** iterator = parts;
 
56
        while (*iterator++ != NULL) count++;
 
57
}
 
58
 
 
59
StringSplitter::~StringSplitter()
 
60
{
 
61
        g_strfreev(parts);
 
62
}
 
63
 
 
64
const gchar* StringSplitter::get_value(int index)
 
65
{
 
66
        if (index >= count)
 
67
        {
 
68
                throw Exception("Index out of bounds");
 
69
        }
 
70
        return parts[index];
 
71
}
 
72
 
 
73
int StringSplitter::get_int_value(int index)
 
74
{
 
75
        return atoi(get_value(index));
 
76
}