Debian Codegraph @

src/style_file.cpp

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * Copyright (C) 2005 Takuro Ashie
4 * Copyright (C) 2012 CSSlayer
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21 #include <fstream>
22 #include <fcitx-utils/log.h>
23
24 #include "style_file.h"
25 #include "key2kana_table.h"
26
27 const int MAX_LINE_LENGTH = 4096;
28
29 static std::string
30 escape (const std::string &str)
31 {
32 std::string dest = str;
33
34 for (unsigned int i = 0; i < dest.size (); i++) {
35 if (dest[i] == '#' || // for comment
36 dest[i] == '\\' || // for backslash itself
37 dest[i] == '=' || // for separatort
38 dest[i] == '[' || dest[i] == ']' || // for section
39 dest[i] == ',' || // for array
40 dest[i] == ' ' || dest[i] == '\t') // for space
41 {
42 dest.insert (i, "\\");
43 i++;
44 }
45 }
46
47 return dest;
48 }
49
50 static std::string
51 unescape (const std::string &str)
52 {
53 std::string dest = str;
54
55 for (unsigned int i = 0; i < dest.size (); i++) {
56 if (dest[i] == '\\') {
57 dest.erase (i, 1);
58 if (i < dest.size () && dest[i] == '\\')
59 i++;
60 }
61 }
62
63 return dest;
64 }
65
66 StyleLine::StyleLine (StyleFile *style_file, std::string line)
67 : m_style_file (style_file),
68 m_line (line),
69 m_type (FCITX_ANTHY_STYLE_LINE_UNKNOWN)
70 {
71 }
72
73 StyleLine::StyleLine (StyleFile *style_file, std::string key, std::string value)
74 : m_style_file (style_file),
75 m_line (escape (key) + std::string ("=")),
76 m_type (FCITX_ANTHY_STYLE_LINE_KEY)
77 {
78 set_value (value);
79 }
80
81 StyleLine::StyleLine (StyleFile *style_file, std::string key,
82 std::vector<std::string> &value)
83 : m_style_file (style_file),
84 m_line (escape (key) + std::string("=")),
85 m_type (FCITX_ANTHY_STYLE_LINE_KEY)
86 {
87 set_value_array (value);
88 }
89
90 StyleLine::~StyleLine ()
91 {
92 }
93
94 StyleLineType
95 StyleLine::get_type (void)
96 {
97 if (m_type != FCITX_ANTHY_STYLE_LINE_UNKNOWN)
98 return m_type;
99
100 int spos, epos;
101 for (spos = 0;
102 spos < m_line.length () && isspace (m_line[spos]);
103 spos++);
104 if (m_line.length() > 0) {
105 for (epos = m_line.length () - 1;
106 epos >= 0 && isspace (m_line[epos]);
107 epos--);
108 } else {
109 epos = 0;
110 }
111
112 if (m_line.length() == 0 || spos >= m_line.length()) {
113 m_type = FCITX_ANTHY_STYLE_LINE_SPACE;
114 return m_type;
115
116 } else if (m_line[spos] == '#') {
117 m_type = FCITX_ANTHY_STYLE_LINE_COMMENT;
118 return m_type;
119
120 } else if (m_line[spos] == '[' && m_line[epos] == ']') {
121 m_type = FCITX_ANTHY_STYLE_LINE_SECTION;
122 return m_type;
123 }
124
125 m_type = FCITX_ANTHY_STYLE_LINE_KEY;
126 return m_type;
127 }
128
129 bool
130 StyleLine::get_section (std::string &section)
131 {
132 if (get_type () != FCITX_ANTHY_STYLE_LINE_SECTION)
133 return false;
134
135 int spos, epos;
136 for (spos = 0;
137 spos < m_line.length () && isspace (m_line[spos]);
138 spos++);
139 for (epos = m_line.length () - 1;
140 epos >= 0 && isspace (m_line[epos]);
141 epos--);
142 spos++;
143
144 if (spos < epos)
145 section = m_line.substr (spos, epos - spos);
146 else
147 section = std::string ();
148
149 return true;
150 }
151
152 bool
153 StyleLine::get_key (std::string &key)
154 {
155 if (get_type () != FCITX_ANTHY_STYLE_LINE_KEY)
156 return false;
157
158 int spos, epos;
159 for (spos = 0;
160 spos < m_line.length () && isspace (m_line[spos]);
161 spos++);
162
163 for (epos = spos;
164 epos < m_line.length ();
165 epos++)
166 {
167 if (m_line[epos] == '\\') {
168 epos++;
169 continue;
170 }
171 if (m_line[epos] == '=') {
172 break;
173 }
174 }
175 for (--epos;
176 epos >= spos && isspace (m_line[epos]);
177 epos--);
178 if (!isspace(m_line[epos]))
179 epos++;
180
181 if (spos >= 0 && spos < epos && epos <= m_line.length ()) {
182 key = unescape (m_line.substr (spos, epos - spos));
183 } else
184 key = std::string ();
185
186 return true;
187 }
188
189 static int
190 get_value_position (std::string &str)
191 {
192 unsigned int spos;
193 for (spos = 0;
194 spos < str.length ();
195 spos++)
196 {
197 if (str[spos] == '\\') {
198 spos++;
199 continue;
200 }
201 if (str[spos] == '=') {
202 break;
203 }
204 }
205 if (spos >= str.length ())
206 return true;
207 else
208 spos++;
209 for (;
210 spos < str.length () && isspace(str[spos]);
211 spos++);
212
213 return spos;
214 }
215
216 bool
217 StyleLine::get_value (std::string &value)
218 {
219 if (get_type () != FCITX_ANTHY_STYLE_LINE_KEY)
220 return false;
221
222 unsigned int spos = get_value_position (m_line);
223 unsigned int epos = m_line.length ();
224
225 value = unescape (m_line.substr (spos, epos - spos));
226
227 return true;
228 }
229
230 void
231 StyleLine::set_value (std::string value)
232 {
233 std::string key;
234 get_key (key);
235 m_line = escape (key) + std::string ("=") + escape (value);
236 }
237
238 bool
239 StyleLine::get_value_array (std::vector<std::string> &value)
240 {
241 if (get_type () != FCITX_ANTHY_STYLE_LINE_KEY)
242 return false;
243
244 unsigned int spos = get_value_position (m_line);
245 unsigned int epos = m_line.length ();
246
247 unsigned int head_of_element = spos;
248 for (unsigned int i = spos; i <= epos; i++) {
249 if (i < epos && m_line[i] == '\\') {
250 i++;
251 continue;
252 }
253
254 if (i == epos || m_line[i] == ',') {
255 std::string str;
256 if (head_of_element == epos)
257 str = std::string ();
258 else
259 str = unescape (m_line.substr (head_of_element,
260 i - head_of_element));
261 value.push_back (str);
262 head_of_element = i + 1;
263 }
264 }
265
266 return true;
267 }
268
269 void
270 StyleLine::set_value_array (std::vector<std::string> &value)
271 {
272 std::string key;
273 get_key (key);
274
275 m_line = escape (key) + std::string ("=");
276 for (unsigned int i = 0; i < value.size (); i++) {
277 if (i != 0)
278 m_line += ",";
279 m_line += escape (value[i]);
280 }
281 }
282
283
284 StyleFile::StyleFile ()
285 {
286 setup_default_entries ();
287 }
288
289 StyleFile::~StyleFile ()
290 {
291 }
292
293 bool
294 StyleFile::load (const char *filename)
295 {
296 clear ();
297 setup_default_entries ();
298 m_filename = filename;
299
300 std::ifstream in_file (filename);
301 if (!in_file)
302 return false;
303
304 clear ();
305
306 m_sections.push_back (StyleLines ());
307 StyleLines *section = &m_sections[0];
308 unsigned int section_id = 0;
309
310 char buf[MAX_LINE_LENGTH];
311 do {
312 in_file.getline (buf, MAX_LINE_LENGTH);
313 if (in_file.eof ())
314 break;
315
316 std::string dest = buf;
317 StyleLine line (this, dest);
318 StyleLineType type = line.get_type ();
319
320 if (type == FCITX_ANTHY_STYLE_LINE_SECTION) {
321 m_sections.push_back (StyleLines ());
322 section = &m_sections.back();
323 section_id++;
324 }
325
326 section->push_back (line);
327
328 if (section_id == 0) {
329 std::string key;
330 line.get_key (key);
331 if (key == "FormatVersion") {
332 line.get_value (m_format_version);
333
334 } else if (key == "Title") {
335 line.get_value (m_title);
336
337 } else if (key == "Version") {
338 line.get_value (m_version);
339 }
340 }
341 } while (!in_file.eof ());
342
343 in_file.close ();
344
345 m_filename = filename;
346
347 return true;
348 }
349
350 bool
351 StyleFile::save (const char *filename)
352 {
353 std::ofstream out_file (filename);
354 if (!out_file)
355 return false;
356
357 StyleSections::iterator it;
358 for (it = m_sections.begin (); it != m_sections.end (); it++) {
359 StyleLines::iterator lit;
360 for (lit = it->begin (); lit != it->end (); lit++) {
361 std::string line, dest;
362 lit->get_line (line);
363 dest = line;
364 out_file << dest.c_str () << std::endl;
365 }
366 }
367
368 out_file.close ();
369
370 m_filename = filename;
371
372 return true;
373 }
374
375 void
376 StyleFile::clear (void)
377 {
378 m_filename = std::string ();
379 m_format_version = std::string ();
380 m_title = std::string ();
381 m_version = std::string ();
382 m_sections.clear ();
383 }
384
385 std::string
386 StyleFile::get_title (void)
387 {
388 return m_title;
389 }
390
391 std::string
392 StyleFile::get_file_name (void)
393 {
394 return m_filename;
395 }
396
397 bool
398 StyleFile::get_string (std::string &value, std::string section, std::string key)
399 {
400 StyleSections::iterator it;
401 for (it = m_sections.begin (); it != m_sections.end (); it++) {
402 if (it->size () <= 0)
403 continue;
404
405 std::string s, k;
406 (*it)[0].get_section (s);
407
408 if (s != section)
409 continue;
410
411 StyleLines::iterator lit;
412 for (lit = it->begin (); lit != it->end (); lit++) {
413 lit->get_key (k);
414 if (k == key) {
415 lit->get_value (value);
416 return true;
417 }
418 }
419 }
420
421 return false;
422 }
423
424 bool
425 StyleFile::get_string_array (std::vector<std::string> &value,
426 std::string section, std::string key)
427 {
428 StyleLines *lines = find_section (section);
429 if (!lines)
430 return false;
431
432 // find entry
433 StyleLines::iterator lit;
434 for (lit = lines->begin (); lit != lines->end (); lit++) {
435 std::string k;
436 lit->get_key (k);
437 if (k == key) {
438 lit->get_value_array (value);
439 return true;
440 }
441 }
442
443 return false;
444 }
445
446 void
447 StyleFile::set_string (std::string section, std::string key, std::string value)
448 {
449 StyleLines *lines = find_section (section);
450 if (lines) {
451 // find entry
452 StyleLines::iterator lit, last = lines->begin () + 1;
453 for (lit = last; lit != lines->end (); lit++) {
454 StyleLineType type = lit->get_type ();
455 if (type != FCITX_ANTHY_STYLE_LINE_SPACE)
456 last = lit + 1;
457
458 std::string k;
459 lit->get_key (k);
460
461 // replace existing entry
462 if (k.length () > 0 && k == key) {
463 lit->set_value (value);
464 return;
465 }
466 }
467
468 // append new entry if no mathced entry exists.
469 lines->insert (last, StyleLine (this, key, value));
470
471 } else {
472 StyleLines &newsec = append_new_section (section);
473
474 // append new entry
475 newsec.push_back (StyleLine (this, key, value));
476 }
477 }
478
479 void
480 StyleFile::set_string_array (std::string section, std::string key,
481 std::vector<std::string> &value)
482 {
483 StyleLines *lines = find_section (section);
484 if (lines) {
485 // find entry
486 StyleLines::iterator lit, last = lines->begin () + 1;
487 for (lit = last; lit != lines->end (); lit++) {
488 StyleLineType type = lit->get_type ();
489 if (type != FCITX_ANTHY_STYLE_LINE_SPACE)
490 last = lit;
491
492 std::string k;
493 lit->get_key (k);
494
495 // replace existing entry
496 if (k.length () > 0 && k == key) {
497 lit->set_value_array (value);
498 return;
499 }
500 }
501
502 // append new entry if no mathced entry exists.
503 lines->insert (last + 1, StyleLine (this, key, value));
504
505 } else {
506 StyleLines &newsec = append_new_section (section);
507
508 // append new entry
509 newsec.push_back (StyleLine (this, key, value));
510 }
511 }
512
513 bool
514 StyleFile::get_section_list (StyleSections &sections)
515 {
516 sections = m_sections;
517 return true;
518 }
519
520 bool
521 StyleFile::get_entry_list (StyleLines &lines, std::string section)
522 {
523 StyleSections::iterator it;
524 for (it = m_sections.begin (); it != m_sections.end (); it++) {
525 if (it->size () <= 0)
526 continue;
527
528 std::string s;
529 (*it)[0].get_section (s);
530 if (s == section) {
531 lines = (*it);
532 return true;
533 }
534 }
535
536 return false;
537 }
538
539 bool
540 StyleFile::get_key_list (std::vector<std::string> &keys, std::string section)
541 {
542 StyleLines *lines = find_section (section);
543 if (!lines)
544 return false;
545
546 StyleLines::iterator lit;
547 for (lit = lines->begin (); lit != lines->end (); lit++) {
548 if (lit->get_type () != FCITX_ANTHY_STYLE_LINE_KEY)
549 continue;
550
551 std::string key;
552 lit->get_key (key);
553 keys.push_back (key);
554 }
555 return true;
556 }
557
558 void
559 StyleFile::delete_key (std::string section, std::string key)
560 {
561 StyleLines *lines = find_section (section);
562 if (!lines)
563 return;
564
565 // find entry
566 StyleLines::iterator lit;
567 for (lit = lines->begin (); lit != lines->end (); lit++) {
568 std::string k;
569 lit->get_key (k);
570 if (k == key) {
571 lines->erase (lit);
572 return;
573 }
574 }
575 }
576
577 void
578 StyleFile::delete_section (std::string section)
579 {
580 StyleSections::iterator it;
581 for (it = m_sections.begin (); it != m_sections.end (); it++) {
582 if (it->size () <= 0)
583 continue;
584
585 StyleLines::iterator lit;
586 std::string s;
587 (*it)[0].get_section (s);
588 if (s == section) {
589 m_sections.erase (it);
590 return;
591 }
592 }
593 }
594
595 Key2KanaTable *
596 StyleFile::get_key2kana_table (std::string section)
597 {
598 Key2KanaTable *table = NULL;
599
600 std::vector<std::string> keys;
601 bool success = get_key_list (keys, section);
602 if (success) {
603 table = new Key2KanaTable (get_title ());
604 std::vector<std::string>::iterator it;
605 for (it = keys.begin (); it != keys.end (); it++) {
606 std::vector<std::string> array;
607 get_string_array (array, section, *it);
608 table->append_rule (*it, array);
609 }
610 }
611
612 return table;
613 }
614
615 void
616 StyleFile::setup_default_entries (void)
617 {
618 m_title = "User defined";
619 m_sections.push_back (StyleLines ());
620
621 m_sections.push_back (StyleLines ());
622 StyleLines &newsec = m_sections.back ();
623 std::string str = std::string ("Title") + std::string ("=") + escape (m_title);
624 newsec.push_back (StyleLine (this, str.c_str ()));
625 }
626
627 StyleLines *
628 StyleFile::find_section (const std::string &section)
629 {
630 // find section
631 StyleSections::iterator it;
632 for (it = m_sections.begin (); it != m_sections.end (); it++) {
633 if (it->size () <= 0)
634 continue;
635
636 std::string s;
637 (*it)[0].get_section (s);
638
639 if (s == section)
640 return &(*it);
641 }
642
643 return NULL;
644 }
645
646 StyleLines &
647 StyleFile::append_new_section (const std::string &section)
648 {
649 // append space before new section
650 if (!m_sections.empty()) {
651 StyleLines &sec = m_sections.back ();
652 if (sec.empty() ||
653 sec.back().get_type() != FCITX_ANTHY_STYLE_LINE_SPACE)
654 {
655 sec.push_back (StyleLine (this, ""));
656 }
657 }
658
659 //
660 // append new section
661 //
662 m_sections.push_back (StyleLines ());
663 StyleLines &newsec = m_sections.back ();
664
665 // new section entry
666 std::string str = std::string ("[") + std::string (section) + std::string ("]");
667 newsec.push_back (StyleLine (this, str.c_str ()));
668
669 return newsec;
670 }