~ubuntu-branches/ubuntu/trusty/presage/trusty-proposed

« back to all changes in this revision

Viewing changes to src/lib/core/variable.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Matteo Vescovi
  • Date: 2011-08-06 09:26:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110806092615-0wvhajaht9974ncx
Tags: upstream-0.8.6
ImportĀ upstreamĀ versionĀ 0.8.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/******************************************************
 
3
 *  Presage, an extensible predictive text entry system
 
4
 *  ---------------------------------------------------
 
5
 *
 
6
 *  Copyright (C) 2008  Matteo Vescovi <matteo.vescovi@yahoo.co.uk>
 
7
 
 
8
    This program is free software; you can redistribute it and/or modify
 
9
    it under the terms of the GNU General Public License as published by
 
10
    the Free Software Foundation; either version 2 of the License, or
 
11
    (at your option) any later version.
 
12
 
 
13
    This program is distributed in the hope that it will be useful,
 
14
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
    GNU General Public License for more details.
 
17
 
 
18
    You should have received a copy of the GNU General Public License along
 
19
    with this program; if not, write to the Free Software Foundation, Inc.,
 
20
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
21
                                                                             *
 
22
                                                                **********(*)*/
 
23
 
 
24
 
 
25
#include "core/variable.h"
 
26
 
 
27
#include <iostream>
 
28
 
 
29
Variable::Variable(const char* name)
 
30
{
 
31
    m_name = name;
 
32
    m_name_vector = string_to_vector (name);
 
33
}
 
34
 
 
35
Variable::Variable(const std::string& name)
 
36
{
 
37
    m_name = name;
 
38
    m_name_vector = string_to_vector(name);
 
39
}
 
40
 
 
41
Variable::Variable(const std::vector<std::string>& name)
 
42
{
 
43
    m_name = vector_to_string (name);
 
44
    m_name_vector = name;
 
45
}
 
46
 
 
47
Variable::~Variable()
 
48
{
 
49
    // nothing to do
 
50
}
 
51
 
 
52
std::string Variable::get_name () const
 
53
{
 
54
    return m_name;
 
55
}
 
56
 
 
57
std::vector<std::string> Variable::get_name_vector () const
 
58
{
 
59
    return m_name_vector;
 
60
}
 
61
 
 
62
std::string Variable::get_value () const
 
63
{
 
64
    return m_value;
 
65
}
 
66
 
 
67
void Variable::set_value (std::string value)
 
68
{
 
69
    m_value = value;
 
70
    
 
71
    notify ();     // notify all observers
 
72
}
 
73
 
 
74
/**
 
75
 * Tokenize string on '.' char
 
76
 *
 
77
 * foo.bar.foobar
 
78
 *
 
79
 * |foo|bar|foobar|
 
80
 *
 
81
 */
 
82
std::vector<std::string> Variable::string_to_vector(const std::string& str)
 
83
{
 
84
    const char SEPARATOR = '.';
 
85
    
 
86
    std::vector<std::string> result;
 
87
 
 
88
    size_t length = str.size();
 
89
    size_t i = 0;
 
90
    std::string acc;
 
91
    while (i < length) {
 
92
        if (str[i] == SEPARATOR) {
 
93
            result.push_back(acc);
 
94
            acc.clear();
 
95
        } else {
 
96
            acc += str[i];
 
97
        }
 
98
        i++;
 
99
    }
 
100
    if (!acc.empty()) {
 
101
        result.push_back(acc);
 
102
    }
 
103
 
 
104
/*
 
105
    std::string::size_type start_pos = 0;
 
106
    std::string::size_type end_pos = str.find_first_of(SEPARATOR);
 
107
    while (start_pos != std::string::npos && end_pos != std::string::npos) {
 
108
        result.push_back(str.substr(start_pos, end_pos - start_pos));
 
109
        start_pos = end_pos + 1;
 
110
        end_pos = str.find_first_of(SEPARATOR, start_pos);
 
111
    }
 
112
*/
 
113
 
 
114
    // DEBUG
 
115
    // std::cout << "string_to_vector():" << std::endl
 
116
    //        << "string  : " << str << std::endl
 
117
    //        << "variable: ";
 
118
    // for (size_t i = 0; i < result.size(); i++) {
 
119
    //  std::cout << result[i];
 
120
    //  if (i < result.size() - 1) {
 
121
    //      std::cout << '.';
 
122
    //  }
 
123
    // }
 
124
    // std::cout << "| variable size: " << result.size() << std::endl;
 
125
    // std::cout << std::endl;
 
126
    // DEBUG
 
127
 
 
128
    return result;
 
129
}
 
130
 
 
131
std::string Variable::vector_to_string(const std::vector<std::string>& variable)
 
132
{
 
133
    std::string result;
 
134
    for (size_t i = 0; i < variable.size(); i++) {
 
135
        result += variable[i];
 
136
        if (i < variable.size() - 1) {
 
137
            result += '.';
 
138
        }
 
139
    }
 
140
 
 
141
    // DEBUG
 
142
    // std::cout << "vector_to_string():" << std::endl
 
143
    //        << "variable: ";
 
144
    // for (size_t i = 0; i < variable.size(); i++) {
 
145
    //  std::cout << variable[i];
 
146
    //  if (i < variable.size() - 1) {
 
147
    //      std::cout << '.';
 
148
    //  }
 
149
    // }
 
150
    // std::cout << "| variable size: " << variable.size() << std::endl;
 
151
    // std::cout << "string  : " << result << std::endl;
 
152
    // DEBUG
 
153
 
 
154
    return result;
 
155
}