1 /* vim:set et ts=4 sts=4:
2  *
3  * libpyzy - The Chinese PinYin and Bopomofo conversion library.
4  *
5  * Copyright (c) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
20  * USA
21  */
22 #include "PhoneticContext.h"
23 
24 #include <glib.h>
25 #include <string>
26 
27 #include "BopomofoContext.h"
28 #include "Database.h"
29 #include "DoublePinyinContext.h"
30 #include "FullPinyinContext.h"
31 
32 namespace PyZy {
33 
34 void
init()35 InputContext::init ()
36 {
37     char *cache_dir =
38         g_build_filename (g_get_user_cache_dir (), "pyzy", NULL);
39     char *config_dir =
40         g_build_filename (g_get_user_config_dir (), "pyzy", NULL);
41     init (cache_dir, config_dir);
42 
43     g_free (cache_dir);
44     g_free (config_dir);
45 }
46 void
init(const std::string & user_cache_dir,const std::string & user_config_dir)47 InputContext::init (const std::string & user_cache_dir,
48                     const std::string & user_config_dir)
49 {
50     if (user_cache_dir.empty ()) {
51         g_error ("Error: user_cache_dir should not be empty");
52     }
53     if (user_config_dir.empty ()) {
54         g_error ("Error: user_config_dir should not be empty");
55     }
56 
57     Database::init (user_cache_dir);
58     SpecialPhraseTable::init (user_config_dir);
59 }
60 
61 void
finalize()62 InputContext::finalize ()
63 {
64     Database::finalize ();
65 }
66 
67 InputContext *
create(InputContext::InputType type,InputContext::Observer * observer)68 InputContext::create (InputContext::InputType type,
69                       InputContext::Observer * observer) {
70     switch (type) {
71     case FULL_PINYIN:
72         return new FullPinyinContext (observer);
73     case DOUBLE_PINYIN:
74         return new DoublePinyinContext (observer);
75     case BOPOMOFO:
76         return new BopomofoContext (observer);
77     default:
78         g_warning ("unknown context type.\n");
79         return NULL;
80     }
81 }
82 
83 }  // namespace PyZy
84