1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * Copyright (C) 2005 Takuro Ashie
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include <sys/types.h>
25 #include "scim_anthy_utils.h"
26 #include "scim_anthy_default_tables.h"
28 namespace scim_anthy {
31 util_match_key_event (const KeyEventList &list, const KeyEvent &key,
34 KeyEventList::const_iterator kit;
36 for (kit = list.begin (); kit != list.end (); kit++) {
44 if (key.code == kit->code && mod1 == mod2)
51 util_split_string (String &str, std::vector<String> &str_list,
54 String::size_type start = 0, end;
56 for (int i = 0; (num > 0 && i < num) || start < str.length (); i++) {
57 end = str.find (delim, start);
58 if ((num > 0 && i == num - 1) || (end == String::npos))
61 if (start < str.length ()) {
62 str_list.push_back (str.substr (start, end - start));
63 start = end + strlen (delim);
65 str_list.push_back (String ());
71 util_convert_to_wide (WideString & wide, const String & str)
73 if (str.length () < 0)
76 for (unsigned int i = 0; i < str.length (); i++) {
78 char cc[2]; cc[0] = c; cc[1] = '\0';
81 for (unsigned int j = 0; scim_anthy_wide_table[j].code; j++) {
82 if ( scim_anthy_wide_table[j].code &&
83 *scim_anthy_wide_table[j].code == c)
85 wide += utf8_mbstowcs (scim_anthy_wide_table[j].wide);
92 wide += utf8_mbstowcs (cc);
97 util_convert_to_half (String & half, const WideString & str)
99 if (str.length () < 0)
102 for (unsigned int i = 0; i < str.length (); i++) {
103 WideString wide = str.substr (i, 1);
106 for (unsigned int j = 0; scim_anthy_wide_table[j].code; j++) {
107 if (scim_anthy_wide_table[j].wide &&
108 wide == utf8_mbstowcs (scim_anthy_wide_table[j].wide))
110 half += scim_anthy_wide_table[j].code;
117 half += utf8_wcstombs (wide);
122 util_convert_to_katakana (WideString & kata,
123 const WideString & hira,
126 if (hira.length () < 0)
129 for (unsigned int i = 0; i < hira.length (); i++) {
133 HiraganaKatakanaRule *table = scim_anthy_hiragana_katakana_table;
135 for (unsigned int j = 0; table[j].hiragana; j++) {
136 tmpwide = utf8_mbstowcs (table[j].hiragana);
137 if (hira.substr(i, 1) == tmpwide) {
139 kata += utf8_mbstowcs (table[j].half_katakana);
141 kata += utf8_mbstowcs (table[j].katakana);
148 kata += hira.substr(i, 1);
153 util_create_attributes (AttributeList &attrs,
157 unsigned int fg_color,
158 unsigned int bg_color)
160 if (type == "None") {
162 } else if (type == "Underline") {
163 attrs.push_back (Attribute (start, length,
165 SCIM_ATTR_DECORATE_UNDERLINE));
166 } else if (type == "Reverse") {
167 attrs.push_back (Attribute (start, length,
169 SCIM_ATTR_DECORATE_REVERSE));
170 } else if (type == "Highlight") {
171 attrs.push_back (Attribute (start, length,
173 SCIM_ATTR_DECORATE_HIGHLIGHT));
175 if (type == "Color" || type == "FGColor")
176 attrs.push_back (Attribute (start, length,
177 SCIM_ATTR_FOREGROUND,
179 if (type == "Color" || type == "BGColor")
180 attrs.push_back (Attribute (start, length,
181 SCIM_ATTR_BACKGROUND,
187 util_key_is_keypad (const KeyEvent &key)
190 case SCIM_KEY_KP_Equal:
191 case SCIM_KEY_KP_Multiply:
192 case SCIM_KEY_KP_Add:
193 case SCIM_KEY_KP_Separator:
194 case SCIM_KEY_KP_Subtract:
195 case SCIM_KEY_KP_Decimal:
196 case SCIM_KEY_KP_Divide:
214 util_keypad_to_string (String &str, const KeyEvent &key)
219 case SCIM_KEY_KP_Equal:
223 case SCIM_KEY_KP_Multiply:
227 case SCIM_KEY_KP_Add:
231 case SCIM_KEY_KP_Separator:
235 case SCIM_KEY_KP_Subtract:
239 case SCIM_KEY_KP_Decimal:
243 case SCIM_KEY_KP_Divide:
257 raw[0] = '0' + key.code - SCIM_KEY_KP_0;
261 if (isprint (key.get_ascii_code()))
262 raw[0] = key.get_ascii_code();
273 util_launch_program (const char *command)
275 if (!command) return;
278 unsigned int len = strlen (command);
280 strncpy (tmp, command, len);
284 std::vector<char *> array;
286 for (unsigned int i = 0; i < len + 1; i++) {
287 if (!tmp[i] || isspace (tmp[i])) {
290 array.push_back (str);
296 if (array.size () <= 0) return;
297 array.push_back (NULL);
299 char *args[array.size()];
300 for (unsigned int i = 0; i < array.size (); i++)
310 } else if (child_pid == 0) { /* child process */
311 pid_t grandchild_pid;
313 grandchild_pid = fork();
314 if (grandchild_pid < 0) {
317 } else if (grandchild_pid == 0) { /* grandchild process */
318 execvp(args[0], args);
324 } else { /* parent process */
326 waitpid(child_pid, &status, 0);