2
/******************************************************
3
* Presage, an extensible predictive text entry system
4
* ---------------------------------------------------
6
* Copyright (C) 2008 Matteo Vescovi <matteo.vescovi@yahoo.co.uk>
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.
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.
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.
25
#include "core/variable.h"
29
Variable::Variable(const char* name)
32
m_name_vector = string_to_vector (name);
35
Variable::Variable(const std::string& name)
38
m_name_vector = string_to_vector(name);
41
Variable::Variable(const std::vector<std::string>& name)
43
m_name = vector_to_string (name);
52
std::string Variable::get_name () const
57
std::vector<std::string> Variable::get_name_vector () const
62
std::string Variable::get_value () const
67
void Variable::set_value (std::string value)
71
notify (); // notify all observers
75
* Tokenize string on '.' char
82
std::vector<std::string> Variable::string_to_vector(const std::string& str)
84
const char SEPARATOR = '.';
86
std::vector<std::string> result;
88
size_t length = str.size();
92
if (str[i] == SEPARATOR) {
93
result.push_back(acc);
101
result.push_back(acc);
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);
115
// std::cout << "string_to_vector():" << std::endl
116
// << "string : " << str << std::endl
118
// for (size_t i = 0; i < result.size(); i++) {
119
// std::cout << result[i];
120
// if (i < result.size() - 1) {
124
// std::cout << "| variable size: " << result.size() << std::endl;
125
// std::cout << std::endl;
131
std::string Variable::vector_to_string(const std::vector<std::string>& variable)
134
for (size_t i = 0; i < variable.size(); i++) {
135
result += variable[i];
136
if (i < variable.size() - 1) {
142
// std::cout << "vector_to_string():" << std::endl
144
// for (size_t i = 0; i < variable.size(); i++) {
145
// std::cout << variable[i];
146
// if (i < variable.size() - 1) {
150
// std::cout << "| variable size: " << variable.size() << std::endl;
151
// std::cout << "string : " << result << std::endl;