Tomotaka SUWA
t-suw****@users*****
2006年 6月 10日 (土) 10:51:02 JST
Index: AquaSKK/ChangeLog diff -u AquaSKK/ChangeLog:1.27 AquaSKK/ChangeLog:1.28 --- AquaSKK/ChangeLog:1.27 Mon Jun 5 21:27:01 2006 +++ AquaSKK/ChangeLog Sat Jun 10 10:51:01 2006 @@ -1,3 +1,12 @@ +2006-06-10 Tomotaka SUWA <t.suw****@mac*****> + + * SKKDictionary.*: SKKDictionary NXÍ 10 ªÔuÅt@CÌXV + útð`FbNµAÅVÌàeð½f·éæ¤ÉC³BܽAURL Ì[x + ÉÖíç¸A«ÍCu¼ºÉ_E[h·éæ¤ÉC³B + + * NumericConverter.cpp (NumericConverter::Apply): #[0-59] Ìv[ + Xz_ª©Â©çȢƳÀ[v·ésïðC³B + 2006-06-05 Tomotaka SUWA <t.suw****@mac*****> * SKKDictionary.cpp: â®ÉêñuóUèv·é»ÛðC³B Index: AquaSKK/NumericConverter.cpp diff -u AquaSKK/NumericConverter.cpp:1.1 AquaSKK/NumericConverter.cpp:1.2 --- AquaSKK/NumericConverter.cpp:1.1 Sat Jun 3 10:23:18 2006 +++ AquaSKK/NumericConverter.cpp Sat Jun 10 10:51:01 2006 @@ -1,5 +1,5 @@ /* - $Id: NumericConverter.cpp,v 1.1 2006/06/03 01:23:18 t-suwa Exp $ + $Id: NumericConverter.cpp,v 1.2 2006/06/10 01:51:01 t-suwa Exp $ MacOS X implementation of the SKK input method. @@ -268,8 +268,8 @@ int index = 0; do { - pos = src.find_first_of(numbers, pos); - if(0 < pos && src[pos - 1] == '#') { + pos = src.find_first_of(numbers, pos + 1); + if(src[pos - 1] == '#') { switch(src[pos]) { case '0': // 無変換 src.replace(pos - 1, 2, params_[index]); @@ -293,7 +293,7 @@ src.replace(pos - 1, 2, ConvertType9(params_[index])); break; } - pos += params_[index].size(); + pos = pos - 1 + params_[index].size(); ++ index; } } while(pos != std::string::npos); Index: AquaSKK/SKKDictionary.cpp diff -u AquaSKK/SKKDictionary.cpp:1.9 AquaSKK/SKKDictionary.cpp:1.10 --- AquaSKK/SKKDictionary.cpp:1.9 Mon Jun 5 21:27:01 2006 +++ AquaSKK/SKKDictionary.cpp Sat Jun 10 10:51:01 2006 @@ -1,5 +1,5 @@ /* - $Id: SKKDictionary.cpp,v 1.9 2006/06/05 12:27:01 t-suwa Exp $ + $Id: SKKDictionary.cpp,v 1.10 2006/06/10 01:51:01 t-suwa Exp $ MacOS X implementation of the SKK input method. @@ -37,7 +37,9 @@ #include "SKKConfig.h" #include "socketstream.h" -#pragma mark --- SKK common functions --- +// ====================================================================== +// ¤ÊÖ +// ====================================================================== // SKKDictionary Æ SKKUserDictionary ɤʷéèâì namespace skkdic { @@ -45,6 +47,9 @@ const std::string OkuriNasiMark = ";; okuri-nasi entries."; const int SAVE_LIMIT_COUNT = 50; const int SAVE_TIMEOUT = 10 * 60; // 10 ª + const std::string RemoteHOST = "openlab.ring.gr.jp"; + const std::string RemoteURL = "/skk/skk/dic/"; + const int DOWNLOAD_INTERVAL = 60 * 60 * 6; // 6 Ô // «t@CðJ¢ÄAokuri-ari ÌæªÜÅV[N·é bool OpenAndSeek(const std::string& path, std::ifstream& dic) { @@ -133,7 +138,9 @@ }; }; -#pragma mark --- SKKDictionary --- +// ====================================================================== +// SKKDictionary C^tF[X +// ====================================================================== SKKDictionary::SKKDictionary() { // empty @@ -143,52 +150,37 @@ // empty } -void SKKDictionary::initializeContainer(EntryContainer& okuriAri, EntryContainer& okuriNasi) { - std::string line; - std::string index; - std::ifstream dic; - - if(!skkdic::OpenAndSeek(path_, dic)) { - return; - } - - while(skkdic::GetEntry(dic, index, line)) { - if(skkdic::OkuriNasiMark.find(line) != std::string::npos) break; - - // èȵGgÍ~Å\[g³êÄ¢é͸ - okuriAri.push_front(SKKPair(index, line)); - } - - while(skkdic::GetEntry(dic, index, line)) { - // è èGg͸Å\[g³êÄ¢é͸ - okuriNasi.push_back(SKKPair(index, line)); - } -} - void SKKDictionary::load(const std::string& path) { path_ = path; // «ð[h - initializeContainer(okuriAri_, okuriNasi_); - - // Ggð\[g·é - std::sort(okuriAri_.begin(), okuriAri_.end(), skkdic::EntryCompare()); - std::sort(okuriNasi_.begin(), okuriNasi_.end(), skkdic::EntryCompare()); + reloadContainer(); std::cerr << "SKK Dictionary(" << path_ << ")" << std::endl << " okuri-ari: " << countOkuriAri() << " entries." << std::endl << " okuri-nasi: " << countOkuriNasi() << " entries." << std::endl << std::endl; + + // tbV pÌXbhðç¹é + pthread_t pth = 0; + if(pthread_create(&pth, NULL, SKKDictionary::refresh, this) == 0) { + pthread_detach(pth); + } else { + std::cerr << "pthread_create() failed: " << errno << std::endl; + } } int SKKDictionary::countOkuriAri() { + Guard g(mutex_); return okuriAri_.size(); } int SKKDictionary::countOkuriNasi() { + Guard g(mutex_); return okuriNasi_.size(); } std::vector<OkuriganaEntry> SKKDictionary::findOkuriAri(const CppCFString& query) { + Guard g(mutex_); std::string index = query.toStdString(); // ©t©çÈ¢H @@ -202,6 +194,7 @@ } std::vector<CppCFString> SKKDictionary::findOkuriNasi(const CppCFString& query) { + Guard g(mutex_); std::string index = query.toStdString(); // ©Â©çÈ¢H @@ -214,9 +207,69 @@ return SKKEntryConverter::OkuriNasi(SKKEntry::ParseOkuriNasi(i->first, i->second)); } -#pragma mark --- SKKAutoUpdateDictionary --- +// ---------------------------------------------------------------------- + +bool SKKDictionary::reloadContainer() { + std::string line; + std::string index; + std::ifstream dic; + struct stat st; + static std::time_t lastupdate = 0; + + EntryContainer tmpOkuriAri; + EntryContainer tmpOkuriNasi; + + if(!skkdic::OpenAndSeek(path_, dic)) { + return false; + } + + // «t@CªXV³êĢȯêÎA½àµÈ¢ + if(stat(path_.c_str(), &st) < 0 || lastupdate == st.st_mtime) { + return false; + } + lastupdate = st.st_mtime; + + while(skkdic::GetEntry(dic, index, line)) { + if(skkdic::OkuriNasiMark.find(line) != std::string::npos) break; + + // èȵGgÍ~Å\[g³êÄ¢é͸ + tmpOkuriAri.push_front(SKKPair(index, line)); + } + + while(skkdic::GetEntry(dic, index, line)) { + // è èGg͸Å\[g³êÄ¢é͸ + tmpOkuriNasi.push_back(SKKPair(index, line)); + } + + // Ggð\[g·é + std::sort(tmpOkuriAri.begin(), tmpOkuriAri.end(), skkdic::EntryCompare()); + std::sort(tmpOkuriNasi.begin(), tmpOkuriNasi.end(), skkdic::EntryCompare()); + + // u«·¦ + Guard g(mutex_); + tmpOkuriAri.swap(okuriAri_); + tmpOkuriNasi.swap(okuriNasi_); + + return true; +} + +void* SKKDictionary::refresh(void* param) { + SKKDictionary* obj = reinterpret_cast<SKKDictionary*>(param); + + while(true) { + sleep(skkdic::SAVE_TIMEOUT); -SKKAutoUpdateDictionary::SKKAutoUpdateDictionary() : host_("openlab.ring.gr.jp"), url_("/skk/skk/dic/") { + if(obj->reloadContainer()) { + std::cerr << "SKKDictionary: " << obj->path_ << " has been updated"; + } + } +} + +// ====================================================================== +// SKKAutoUpdateDictionary C^tF[X +// ====================================================================== + +SKKAutoUpdateDictionary::SKKAutoUpdateDictionary() : host_(skkdic::RemoteHOST), url_(skkdic::RemoteURL) { // empty } @@ -226,8 +279,17 @@ void SKKAutoUpdateDictionary::load(const std::string& path) { url_ += path; - path_ = SKKConfig::LibraryDirectory() + path; - initializeContainer(okuriAri_, okuriNasi_); + + // «Ì_E[hæÍCu¼º(URL Ì[³É˶µÈ¢) + std::string::size_type pos = path.find_last_of('/'); + if(pos != std::string::npos) { + path_ = SKKConfig::LibraryDirectory() + path.substr(pos + 1); + } else { + path_ = SKKConfig::LibraryDirectory() + path; + } + + // «ð[h + reloadContainer(); std::cerr << "SKK AutoUpdate Dictionary(" << path_ << ")" << std::endl << " okuri-ari: " << countOkuriAri() << " entries." << std::endl @@ -242,31 +304,12 @@ } } -int SKKAutoUpdateDictionary::countOkuriAri() { - Guard g(mutex_); - return SKKDictionary::countOkuriAri(); -} - -int SKKAutoUpdateDictionary::countOkuriNasi() { - Guard g(mutex_); - return SKKDictionary::countOkuriNasi(); -} - -std::vector<OkuriganaEntry> SKKAutoUpdateDictionary::findOkuriAri(const CppCFString& query) { - Guard g(mutex_); - return SKKDictionary::findOkuriAri(query); -} - -std::vector<CppCFString> SKKAutoUpdateDictionary::findOkuriNasi(const CppCFString& query) { - Guard g(mutex_); - return SKKDictionary::findOkuriNasi(query); -} +// ---------------------------------------------------------------------- void* SKKAutoUpdateDictionary::download(void* param) { - SKKAutoUpdateDictionary* obj = static_cast<SKKAutoUpdateDictionary*>(param); + SKKAutoUpdateDictionary* obj = reinterpret_cast<SKKAutoUpdateDictionary*>(param); while(true) { - std::cerr << "SKKAutoUpdateDictionary: checking " << obj->host_ << obj->url_ << " ..." << std::endl; socket_stream session(obj->host_.c_str(), 80); if(session) { // «ÌXVútðæ¾ @@ -295,8 +338,8 @@ if(response == "\r") break; } + // ÏX³êÄ¢êÎA_E[hµÄ[h·é if(modified) { - // Û¶ std::ofstream ofs(obj->path_.c_str()); while(true) { getline(session, response); @@ -305,16 +348,7 @@ } ofs.close(); - // [h - EntryContainer tmpOkuriAri; - EntryContainer tmpOkuriNasi; - obj->initializeContainer(tmpOkuriAri, tmpOkuriNasi); - - // u«·¦ - Guard g(obj->mutex_); - tmpOkuriAri.swap(obj->okuriAri_); - tmpOkuriNasi.swap(obj->okuriNasi_); - + obj->reloadContainer(); std::cerr << "SKKAutoUpdateDictionary: " << obj->path_ << " has been updated" << std::endl; } } else { @@ -322,13 +356,15 @@ } // 10 ªÔuÅA6 Ôoß·éÌð`FbN·é - for(time_t nextTime = time(0) + (60 * 60 * 6); time(0) < nextTime; sleep(60 * 10)) { + for(time_t nextTime = time(0) + skkdic::DOWNLOAD_INTERVAL; time(0) < nextTime; sleep(skkdic::SAVE_TIMEOUT)) { // ±¤µÈ¢ÆAOS ªX[v[h©çAµ½É~¦È¢ } } } -#pragma mark --- SKKUserDictionary --- +// ====================================================================== +// SKKUserDictionary C^tF[X +// ====================================================================== SKKUserDictionary::SKKUserDictionary() { // empty @@ -492,7 +528,7 @@ save(); } -// ------------------------------------------------------------------ +// ---------------------------------------------------------------------- void SKKUserDictionary::save(bool force) { static int updateCount = 0; Index: AquaSKK/SKKDictionary.h diff -u AquaSKK/SKKDictionary.h:1.6 AquaSKK/SKKDictionary.h:1.7 --- AquaSKK/SKKDictionary.h:1.6 Wed Apr 26 22:36:12 2006 +++ AquaSKK/SKKDictionary.h Sat Jun 10 10:51:01 2006 @@ -1,5 +1,5 @@ /* - $Id: SKKDictionary.h,v 1.6 2006/04/26 13:36:12 t-suwa Exp $ + $Id: SKKDictionary.h,v 1.7 2006/06/10 01:51:01 t-suwa Exp $ MacOS X implementation of the SKK input method. @@ -21,7 +21,8 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#pragma once +#ifndef INC__SKKDictionary__ +#define INC__SKKDictionary__ #include <deque> #include "Dictionary.h" @@ -39,12 +40,15 @@ // SKK ¤L« class SKKDictionary: public Dictionary { + static void* refresh(void* param); + protected: std::string path_; EntryContainer okuriAri_; EntryContainer okuriNasi_; + Mutex mutex_; - void initializeContainer(EntryContainer& okuriAri, EntryContainer& okuriNasi); + bool reloadContainer(); public: SKKDictionary(); @@ -63,7 +67,6 @@ class SKKAutoUpdateDictionary: public SKKDictionary { std::string host_; std::string url_; - Mutex mutex_; static void* download(void* param); @@ -72,12 +75,6 @@ virtual ~SKKAutoUpdateDictionary(); virtual void load(const std::string& path); - - virtual int countOkuriAri(); - virtual int countOkuriNasi(); - - virtual std::vector<OkuriganaEntry> findOkuriAri(const CppCFString& query); - virtual std::vector<CppCFString> findOkuriNasi(const CppCFString& query); }; // SKK [U[« @@ -108,3 +105,5 @@ virtual void removeOkuriAri(const CppCFString& index, const CppCFString& kanji); virtual void removeOkuriNasi(const CppCFString& index, const CppCFString& kanji); }; + +#endif // INC__SKKDictionary__