presage
0.8.7
|
00001 00002 /****************************************************** 00003 * Presage, an extensible predictive text entry system 00004 * --------------------------------------------------- 00005 * 00006 * Copyright (C) 2008 Matteo Vescovi <matteo.vescovi@yahoo.co.uk> 00007 00008 This program is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 2 of the License, or 00011 (at your option) any later version. 00012 00013 This program is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License along 00019 with this program; if not, write to the Free Software Foundation, Inc., 00020 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00021 * 00022 **********(*)*/ 00023 00024 00025 #include "core/variable.h" 00026 00027 #include <iostream> 00028 00029 Variable::Variable(const char* name) 00030 { 00031 m_name = name; 00032 m_name_vector = string_to_vector (name); 00033 } 00034 00035 Variable::Variable(const std::string& name) 00036 { 00037 m_name = name; 00038 m_name_vector = string_to_vector(name); 00039 } 00040 00041 Variable::Variable(const std::vector<std::string>& name) 00042 { 00043 m_name = vector_to_string (name); 00044 m_name_vector = name; 00045 } 00046 00047 Variable::~Variable() 00048 { 00049 // nothing to do 00050 } 00051 00052 std::string Variable::get_name () const 00053 { 00054 return m_name; 00055 } 00056 00057 std::vector<std::string> Variable::get_name_vector () const 00058 { 00059 return m_name_vector; 00060 } 00061 00062 std::string Variable::get_value () const 00063 { 00064 return m_value; 00065 } 00066 00067 void Variable::set_value (std::string value) 00068 { 00069 m_value = value; 00070 00071 notify (); // notify all observers 00072 } 00073 00082 std::vector<std::string> Variable::string_to_vector(const std::string& str) 00083 { 00084 const char SEPARATOR = '.'; 00085 00086 std::vector<std::string> result; 00087 00088 size_t length = str.size(); 00089 size_t i = 0; 00090 std::string acc; 00091 while (i < length) { 00092 if (str[i] == SEPARATOR) { 00093 result.push_back(acc); 00094 acc.clear(); 00095 } else { 00096 acc += str[i]; 00097 } 00098 i++; 00099 } 00100 if (!acc.empty()) { 00101 result.push_back(acc); 00102 } 00103 00104 /* 00105 std::string::size_type start_pos = 0; 00106 std::string::size_type end_pos = str.find_first_of(SEPARATOR); 00107 while (start_pos != std::string::npos && end_pos != std::string::npos) { 00108 result.push_back(str.substr(start_pos, end_pos - start_pos)); 00109 start_pos = end_pos + 1; 00110 end_pos = str.find_first_of(SEPARATOR, start_pos); 00111 } 00112 */ 00113 00114 // DEBUG 00115 // std::cout << "string_to_vector():" << std::endl 00116 // << "string : " << str << std::endl 00117 // << "variable: "; 00118 // for (size_t i = 0; i < result.size(); i++) { 00119 // std::cout << result[i]; 00120 // if (i < result.size() - 1) { 00121 // std::cout << '.'; 00122 // } 00123 // } 00124 // std::cout << "| variable size: " << result.size() << std::endl; 00125 // std::cout << std::endl; 00126 // DEBUG 00127 00128 return result; 00129 } 00130 00131 std::string Variable::vector_to_string(const std::vector<std::string>& variable) 00132 { 00133 std::string result; 00134 for (size_t i = 0; i < variable.size(); i++) { 00135 result += variable[i]; 00136 if (i < variable.size() - 1) { 00137 result += '.'; 00138 } 00139 } 00140 00141 // DEBUG 00142 // std::cout << "vector_to_string():" << std::endl 00143 // << "variable: "; 00144 // for (size_t i = 0; i < variable.size(); i++) { 00145 // std::cout << variable[i]; 00146 // if (i < variable.size() - 1) { 00147 // std::cout << '.'; 00148 // } 00149 // } 00150 // std::cout << "| variable size: " << variable.size() << std::endl; 00151 // std::cout << "string : " << result << std::endl; 00152 // DEBUG 00153 00154 return result; 00155 }