Newer
Older
scim-wnn / honoka / src / skkdic.cpp
/***************************************************************************
 *   Copyright (C) 2005 by TAM(Teppei Tamra)                               *
 *   tam-t@par.odn.ne.jp                                                   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "skkdic.h"

SKKDic::SKKDic(String file)
{
    filename = file;
    iconv.set_encoding("EUC-JP");
    init();
}


SKKDic::~SKKDic()
{
}




/*!
    \fn SKKDic::init()
 */
void SKKDic::init()
{
    bool okuri = true;
    FILE *f = fopen(filename.c_str(),"r");
    if (!f) return;
    while(-1) {
        char s[1024];
        if(fgets(s,1024,f) == NULL) break;
        if (String(s) == String(";; okuri-ari entries.")) {
            okuri = true;
            continue;
        } else if (String(s) == String(";; okuri-nasi entries.")) {
            okuri = false;
            continue;
        }
        if (String(s).substr(0,2) == ";;") continue;
        vector<WideString> sList;
        WideString wstr;
        iconv.convert(wstr,String(s));
        uint pos = 0,count = 0;
        // "/"でsplit。
        while(-1) {
            if ((pos + count) >= wstr.size()) break;
            if (wstr.at(pos + count) == utf8_mbstowcs(String("/"))[0]) {
                if (count) sList.push_back(wstr.substr(pos,count));
                pos += count + 1;
                count = 0;
                continue;
            }
            count ++;
        }
        // 読みの空白を削除。
        // @todo okuri-ari
        wstr = WideString();
        if (!sList.size()) continue;
        for(unsigned int i = 0;i < sList[0].length();i ++) {
            if (sList[0][i] != utf8_mbstowcs(String(" "))[0]) {
                wstr = wstr + sList[0][i];
            }
        }
        sList[0] = wstr;
        // アノテーション/候補摘出。
        vector<SKKDicEntry> dic;
        for(unsigned int i = 1;i < sList.size();i ++) {
            SKKDicEntry e;
            for(unsigned int j = 0;j < sList[i].size();j ++) {
                if ((sList[i][j] == utf8_mbstowcs(String(";"))[0]) && ((j + 1) < sList[i].size()) && (j > 0)) {
                    e.kouho = sList[i].substr(0,j);
                    e.annotation = sList[i].substr(j + 1);
                    break;
                }
            }
            if (!e.annotation.length()) e.kouho = sList[i];
            dic.push_back(e);
        }
        dic_data.insert(pair<WideString,vector<SKKDicEntry> >(sList[0],dic));
    }
    fclose(f);
}


/*!
    \fn SKKDic::find(WideString text)
 */
const vector<SKKDicEntry> SKKDic::find(WideString text)
{
    map<WideString,vector<SKKDicEntry> >::iterator it = dic_data.find(text);
    if (it == dic_data.end()) return vector<SKKDicEntry>();
    return it->second;
}