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

Refactors Config.

BUG=None
TEST=Manual

Review URL: https://codereview.appspot.com/6202056
parent 9d37a22f
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -24,7 +24,7 @@ m4_define([pyzy_released], [1])

m4_define([pyzy_major_version], [0])
m4_define([pyzy_minor_version], [0])
m4_define([pyzy_micro_version], [4])
m4_define([pyzy_micro_version], [5])
m4_define([pyzy_interface_age], [0])
m4_define([pyzy_binary_age],
          [m4_eval(100 * pyzy_minor_version + pyzy_micro_version)])
+9 −6
Original line number Diff line number Diff line
@@ -37,8 +37,9 @@ libpyzy=libpyzy-1.0.la

libpyzyincludedir = $(includedir)/pyzy-@PYZY_API_VERSION@
libpyzyinclude_HEADERS = \
	PyZyConst.h \
	PyZyInputContext.h \
	PyZyConfig.h \
	PyZyVariant.h \
	$(NULL)

lib_LTLIBRARIES = $(libpyzy)
@@ -51,30 +52,32 @@ libpyzy_built_h_sources = \
	PyZySimpTradConverterTable.h \
	$(NULL)
libpyzy_c_sources = \
	PyZyInputContext.cc \
	PyZyBopomofoContext.cc \
	PyZyConfig.cc \
	PyZyDatabase.cc \
	PyZyDoublePinyinContext.cc \
	PyZyDynamicSpecialPhrase.cc \
	PyZyFullPinyinContext.cc \
	PyZyInputContext.cc \
	PyZyPhoneticContext.cc \
	PyZyPhraseEditor.cc \
	PyZyPinyinContext.cc \
	PyZyPinyinParser.cc \
	PyZySimpTradConverter.cc \
	PyZyDynamicSpecialPhrase.cc \
	PyZySpecialPhraseTable.cc \
	PyZyVariant.cc \
	$(NULL)
#	PyZyTest.cc 
libpyzy_h_sources = \
	PyZyInputContext.h \
	PyZyBopomofo.h \
	PyZyBopomofoContext.h \
	PyZyConfig.h \
	PyZyConst.h \
	PyZyDatabase.h \
	PyZyDoublePinyinContext.h \
	PyZyDoublePinyinTable.h \
	PyZyDynamicSpecialPhrase.h \
	PyZyFullPinyinContext.h \
	PyZyInputContext.h \
	PyZyPhoneticContext.h \
	PyZyPhrase.h \
	PyZyPhraseArray.h \
@@ -84,11 +87,11 @@ libpyzy_h_sources = \
	PyZyPinyinParser.h \
	PyZySimpTradConverter.h \
	PyZySpecialPhrase.h \
	PyZyDynamicSpecialPhrase.h \
	PyZySpecialPhraseTable.h \
	PyZyString.h \
	PyZyTypes.h \
	PyZyUtil.h \
	PyZyVariant.h \
	$(NULL)

libpyzy_1_0_la_SOURCES = \
+40 −10
Original line number Diff line number Diff line
@@ -40,8 +40,9 @@ const static char * bopomofo_select_keys[] = {
    "qweasdzxcr"
};

BopomofoContext::BopomofoContext (Config & config, PhoneticContext::Observer *observer)
    : PhoneticContext (config, observer)
BopomofoContext::BopomofoContext (PhoneticContext::Observer *observer)
    : PhoneticContext (observer),
      m_bopomofo_schema (BOPOMOFO_KEYBOARD_STANDARD)
{
}

@@ -62,7 +63,7 @@ BopomofoContext::insert (char ch)

    m_text.insert (m_cursor++, ch);

    if (G_UNLIKELY (!(m_config.option () & PINYIN_INCOMPLETE_PINYIN))) {
    if (G_UNLIKELY (!(m_config.option & PINYIN_INCOMPLETE_PINYIN))) {
        updateSpecialPhrases ();
        updatePinyin ();
    }
@@ -247,9 +248,10 @@ BopomofoContext::updatePinyin (void)
            bopomofo += bopomofo_char[keyvalToBopomofo (*i)];
        }

        m_pinyin_len = PinyinParser::parseBopomofo (bopomofo,            // bopomofo
        m_pinyin_len = PinyinParser::parseBopomofo (
            bopomofo,            // bopomofo
            m_cursor,            // text length
                                                    m_config.option (),   // option
            m_config.option,     // option
            m_pinyin,            // result
            MAX_PHRASE_LEN);     // max result length
    }
@@ -396,7 +398,7 @@ BopomofoContext::updatePreeditText (void)
                const Phrase & candidate = m_phrase_editor.candidate (index - m_special_phrases.size ());
                if (m_text.size () == m_cursor) {
                    /* cursor at end */
                    if (m_config.modeSimp ())
                    if (m_config.modeSimp)
                        m_buffer << candidate;
                    else
                        SimpTradConverter::simpToTrad (candidate, m_buffer);
@@ -434,6 +436,34 @@ BopomofoContext::updatePreeditText (void)
    PhoneticContext::updatePreeditText ();
}

Variant
BopomofoContext::getProperty (PropertyName name) const
{
    if (name == PROPERTY_BOPOMOFO_SCHEMA) {
        return Variant::fromUnsignedInt(m_bopomofo_schema);
    }
    return PhoneticContext::getProperty (name);
}

bool
BopomofoContext::setProperty (PropertyName name, const Variant &variant)
{
    if (name == PROPERTY_BOPOMOFO_SCHEMA) {
        if (variant.getType () != Variant::TYPE_UNSIGNED_INT) {
            return false;
        }
        const unsigned int schema = variant.getUnsignedInt ();
        if (schema >= BOPOMOFO_KEYBOARD_LAST) {
            return false;
        }

        m_bopomofo_schema = schema;
        return true;
    }

    return PhoneticContext::setProperty (name, variant);
}

static int
keyboard_cmp (const void * p1, const void * p2)
{
@@ -445,7 +475,7 @@ keyboard_cmp (const void * p1, const void * p2)
int
BopomofoContext::keyvalToBopomofo(int ch)
{
    const unsigned int keyboard = m_config.bopomofoKeyboardMapping ();
    const unsigned int keyboard = m_bopomofo_schema;
    const unsigned char *brs;
    brs = (const unsigned char *) std::bsearch (GINT_TO_POINTER (ch),
                                       bopomofo_keyboard[keyboard],
+9 −4
Original line number Diff line number Diff line
@@ -27,12 +27,10 @@

namespace PyZy {

class Config;

class BopomofoContext : public PhoneticContext {

public:
    BopomofoContext (Config & config, PhoneticContext::Observer *observer);
    explicit BopomofoContext (PhoneticContext::Observer *observer);
    virtual ~BopomofoContext (void);

    /* API of InputContext */
@@ -51,13 +49,20 @@ public:
    virtual bool moveCursorToBegin (void);
    virtual bool moveCursorToEnd (void);

    virtual Variant getProperty (PropertyName name) const;
    virtual bool setProperty (PropertyName name, const Variant &variant);

protected:
    virtual void updateAuxiliaryText ();
    virtual void updatePinyin ();
    virtual void updatePreeditText ();

    bool processBopomofo (unsigned int keyval, unsigned int keycode, unsigned int modifiers);
    bool processBopomofo (
        unsigned int keyval, unsigned int keycode, unsigned int modifiers);
    int keyvalToBopomofo(int ch);

private:
    unsigned int m_bopomofo_schema;
};

};

src/PyZyConfig.cc

deleted100644 → 0
+0 −209
Original line number Diff line number Diff line
/* vim:set et ts=4 sts=4:
 *
 * libpyzy - The Chinese PinYin and Bopomofo conversion library.
 *
 * Copyright (c) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */
#include "PyZyConfig.h"

#include "PyZyUtil.h"
#include "PyZyDoublePinyinTable.h"

namespace PyZy {

const unsigned int PINYIN_DEFAULT_OPTION =
        PINYIN_INCOMPLETE_PINYIN |
        PINYIN_CORRECT_ALL |
        PINYIN_FUZZY_C_CH |
        // PINYIN_FUZZY_CH_C |
        PINYIN_FUZZY_Z_ZH |
        // PINYIN_FUZZY_ZH_Z |
        PINYIN_FUZZY_S_SH |
        // PINYIN_FUZZY_SH_S |
        PINYIN_FUZZY_L_N |
        // PINYIN_FUZZY_N_L |
        PINYIN_FUZZY_F_H |
        // PINYIN_FUZZY_H_F |
        // PINYIN_FUZZY_L_R |
        // PINYIN_FUZZY_R_L |
        PINYIN_FUZZY_K_G |
        PINYIN_FUZZY_G_K |
        PINYIN_FUZZY_AN_ANG |
        PINYIN_FUZZY_ANG_AN |
        PINYIN_FUZZY_EN_ENG |
        PINYIN_FUZZY_ENG_EN |
        PINYIN_FUZZY_IN_ING |
        PINYIN_FUZZY_ING_IN |
        // PINYIN_FUZZY_IAN_IANG |
        // PINYIN_FUZZY_IANG_IAN |
        // PINYIN_FUZZY_UAN_UANG |
        // PINYIN_FUZZY_UANG_UAN |
        0;

struct Config::ConfigImpl {
    bool         m_special_phrases;
    bool         m_mode_simp;
    unsigned int m_option;
    unsigned int m_option_mask;
    unsigned int m_double_pinyin_schema;
    unsigned int m_bopomofo_keyboard_mapping;
};

Config::Config ()
    : m_impl (new Config::ConfigImpl)
{
    readDefaultValues ();
}

Config::~Config ()
{
    delete m_impl;
}

/* Accessors */
unsigned int
Config::option (void) const
{
    return m_impl->m_option;
}

unsigned int
Config::doublePinyinSchema (void) const
{
    return m_impl->m_double_pinyin_schema;
}

bool
Config::specialPhrases (void) const
{
    return m_impl->m_special_phrases;
}

bool
Config::modeSimp (void) const
{
    return m_impl->m_mode_simp;
}

unsigned int
Config::bopomofoKeyboardMapping (void) const
{
    return m_impl->m_bopomofo_keyboard_mapping;
}

void
Config::setOption (unsigned int value)
{
    m_impl->m_option = value;
}

void
Config::setDoublePinyinSchema (unsigned int value)
{
    m_impl->m_double_pinyin_schema = value;
}

void
Config::setSpecialPhrases (bool value)
{
    m_impl->m_special_phrases = value;
}

void
Config::setModeSimp (bool value)
{
    m_impl->m_mode_simp = value;
}

void
Config::setBopomofoKeyboardMapping (unsigned int value)
{
    m_impl->m_bopomofo_keyboard_mapping = value;
}

void
Config::readDefaultValues ()
{
    m_impl->m_option = PINYIN_DEFAULT_OPTION;
    m_impl->m_option_mask = PINYIN_INCOMPLETE_PINYIN | PINYIN_CORRECT_ALL;
    m_impl->m_double_pinyin_schema = 0;
    m_impl->m_mode_simp = true;
    m_impl->m_special_phrases = true;
    m_impl->m_bopomofo_keyboard_mapping = 0;
}

/* Pinyin Config */
struct PinyinConfig::PinyinConfigImpl {
    static std::unique_ptr <PinyinConfig> m_instance;
};

std::unique_ptr<PinyinConfig> PinyinConfig::PinyinConfigImpl::m_instance;

PinyinConfig::PinyinConfig ()
    : Config ()
{
}

PinyinConfig &
PinyinConfig::instance ()
{
    if (PinyinConfigImpl::m_instance == NULL) {
        PinyinConfigImpl::m_instance.reset (new PinyinConfig ());
        PinyinConfigImpl::m_instance->readDefaultValues ();
    }
    return *PinyinConfigImpl::m_instance;
}

void
PinyinConfig::readDefaultValues ()
{
    Config::readDefaultValues ();
}


/* Bopomofo Config */

struct BopomofoConfig::BopomofoConfigImpl {
    static std::unique_ptr <BopomofoConfig> m_instance;
};

std::unique_ptr<BopomofoConfig> BopomofoConfig::BopomofoConfigImpl::m_instance;

BopomofoConfig::BopomofoConfig ()
    : Config ()
{
}

BopomofoConfig &
BopomofoConfig::instance ()
{
    if (BopomofoConfigImpl::m_instance == NULL) {
        BopomofoConfigImpl::m_instance.reset (new BopomofoConfig ());
        BopomofoConfigImpl::m_instance->readDefaultValues ();
    }
    return *BopomofoConfigImpl::m_instance;
}

void
BopomofoConfig::readDefaultValues (void)
{
    Config::readDefaultValues ();
    m_impl->m_special_phrases = false;
}

};  // namespace PyZy
Loading