diff options
author | Hiroshi Sumita <hsumita@chromium.org> | 2012-05-25 21:18:41 +0900 |
---|---|---|
committer | Hiroshi Sumita <hsumita@chromium.org> | 2012-05-25 21:18:41 +0900 |
commit | f8c1629396ce529d6c5f3230e5437ae6da164262 (patch) | |
tree | 8520c2b687c2c32fe6038e6bf92045b18e2796ab | |
parent | 793f70ba103d78a4deae2b38a4e23ddbbe91ec13 (diff) | |
download | pyzy-f8c1629396ce529d6c5f3230e5437ae6da164262.tar.gz |
Provides a method to use original special phrase.
BUG=None
TEST=Manual
Review URL: https://codereview.appspot.com/6252043
-rw-r--r-- | src/PyZyDatabase.h | 2 | ||||
-rw-r--r-- | src/PyZyInputContext.cc | 22 | ||||
-rw-r--r-- | src/PyZyInputContext.h | 16 | ||||
-rw-r--r-- | src/PyZySpecialPhraseTable.cc | 28 | ||||
-rw-r--r-- | src/PyZySpecialPhraseTable.h | 7 |
5 files changed, 57 insertions, 18 deletions
diff --git a/src/PyZyDatabase.h b/src/PyZyDatabase.h index 0d49d2a..68984e0 100644 --- a/src/PyZyDatabase.h +++ b/src/PyZyDatabase.h @@ -80,7 +80,7 @@ public: static Database & instance (void) { if (m_instance == NULL) { - g_error ("Error: Please call PhoneticContext::init () !"); + g_error ("Error: Please call InputContext::init () !"); } return *m_instance; } diff --git a/src/PyZyInputContext.cc b/src/PyZyInputContext.cc index 1795f9f..3da5cb2 100644 --- a/src/PyZyInputContext.cc +++ b/src/PyZyInputContext.cc @@ -34,15 +34,25 @@ namespace PyZy { void InputContext::init () { - const std::string cache_dir = g_get_user_cache_dir (); - const std::string data_dir = cache_dir + G_DIR_SEPARATOR_S + "pyzy"; - init (data_dir); + const std::string cache_dir = + g_build_filename (g_get_user_cache_dir (), "pyzy", NULL); + const std::string config_dir = + g_build_filename (g_get_user_config_dir (), "pyzy", NULL); + init (cache_dir, config_dir); } - void -InputContext::init (const std::string & user_data_dir) +InputContext::init (const std::string & user_cache_dir, + const std::string & user_config_dir) { - Database::init (user_data_dir); + if (user_cache_dir.empty ()) { + g_error ("Error: user_cache_dir should not be empty"); + } + if (user_config_dir.empty ()) { + g_error ("Error: user_config_dir should not be empty"); + } + + Database::init (user_cache_dir); + SpecialPhraseTable::init (user_config_dir); } void diff --git a/src/PyZyInputContext.h b/src/PyZyInputContext.h index 40b94db..49304a3 100644 --- a/src/PyZyInputContext.h +++ b/src/PyZyInputContext.h @@ -360,18 +360,26 @@ public: /** * \brief Initializes a InputContext class. * - * You should call it at first. + * This is a wrapper function of input (user_cache_dir, user_config_dir). + * Default values are set to user_cache_dir and user_config_dir. + * You should call this function at first. */ static void init (); /** * \brief Initializes a InputContext class. - * @param user_data_dir Directory which stores a user data. + * @param user_cache_dir Directory which stores a user cache data. + * (input history, etc.) + * @param user_config_dir Directory which stores a user config data. + * If you want to use original special phrase table, please create + * "phrases.txt" under this directory. * * Specifies a directory to stores user data. - * You should call it at first. + * You can set a same directory to user_cache_dir and user_config_dir. + * You should call this function at first. */ - static void init (const std::string & user_data_dir); + static void init (const std::string & user_cache_dir, + const std::string & user_config_dir); /** * \brief Finalizes a InputContext class. diff --git a/src/PyZySpecialPhraseTable.cc b/src/PyZySpecialPhraseTable.cc index d1c23c0..206d396 100644 --- a/src/PyZySpecialPhraseTable.cc +++ b/src/PyZySpecialPhraseTable.cc @@ -26,7 +26,7 @@ namespace PyZy { -SpecialPhraseTable SpecialPhraseTable::m_instance; +std::unique_ptr<SpecialPhraseTable> SpecialPhraseTable::m_instance; class StaticSpecialPhrase : public SpecialPhrase { public: @@ -40,10 +40,10 @@ private: std::string m_text; }; -SpecialPhraseTable::SpecialPhraseTable (void) +SpecialPhraseTable::SpecialPhraseTable (const std::string &config_dir) { - char * path = g_build_filename (g_get_user_config_dir (), - "pyzy", "phrases.txt", NULL); + char * path = + g_build_filename (config_dir.c_str(), "pyzy", "phrases.txt", NULL); load ("phrases.txt") || load (path) || @@ -100,4 +100,24 @@ SpecialPhraseTable::load (const char *file) return true; } +void +SpecialPhraseTable::init (const std::string &config_dir) +{ + if (config_dir.empty ()) { + g_error ("Error: An argument of init is empty string."); + return; + } + m_instance.reset (new SpecialPhraseTable (config_dir)); +} + +SpecialPhraseTable & +SpecialPhraseTable::instance (void) +{ + if (m_instance.get () == NULL) { + g_error ("Error: Please call PyZy::InputContext::init () !"); + } + return *m_instance; +} + + }; // namespace PyZy diff --git a/src/PyZySpecialPhraseTable.h b/src/PyZySpecialPhraseTable.h index 61feb54..5a1dd5d 100644 --- a/src/PyZySpecialPhraseTable.h +++ b/src/PyZySpecialPhraseTable.h @@ -35,7 +35,7 @@ typedef std::shared_ptr<SpecialPhrase> SpecialPhrasePtr; class SpecialPhraseTable { private: - SpecialPhraseTable (void); + explicit SpecialPhraseTable (const std::string &config_dir); public: bool lookup (const std::string &command, std::vector<std::string> &result); @@ -44,14 +44,15 @@ private: bool load (const char *file); public: - static SpecialPhraseTable & instance (void) { return m_instance; } + static void init (const std::string &config_dir); + static SpecialPhraseTable & instance (void); private: typedef std::multimap<std::string, SpecialPhrasePtr> Map; Map m_map; private: - static SpecialPhraseTable m_instance; + static std::unique_ptr<SpecialPhraseTable> m_instance; }; }; // namespace PyZy |