Commit f8c16293 authored by Hiroshi Sumita's avatar Hiroshi Sumita
Browse files

Provides a method to use original special phrase.

BUG=None
TEST=Manual

Review URL: https://codereview.appspot.com/6252043
parent 793f70ba
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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;
    }
+16 −6
Original line number Diff line number Diff line
@@ -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
+12 −4
Original line number Diff line number Diff line
@@ -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.
+24 −4
Original line number Diff line number Diff line
@@ -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
+4 −3
Original line number Diff line number Diff line
@@ -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