Skip to content

Commit

Permalink
ICU4Cによる文字コード検出機能を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
berryzplus committed Nov 30, 2019
1 parent 00410f2 commit fc94768
Show file tree
Hide file tree
Showing 8 changed files with 304 additions and 0 deletions.
4 changes: 4 additions & 0 deletions sakura/sakura.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@
<ClInclude Include="..\sakura_core\charset\CUnicodeBe.h" />
<ClInclude Include="..\sakura_core\charset\CUtf7.h" />
<ClInclude Include="..\sakura_core\charset\CUtf8.h" />
<ClInclude Include="..\sakura_core\charset\icu4c\CharsetDetector.h" />
<ClInclude Include="..\sakura_core\CHokanMgr.h" />
<ClInclude Include="..\sakura_core\CKeyWordSetMgr.h" />
<ClInclude Include="..\sakura_core\CLoadAgent.h" />
Expand Down Expand Up @@ -413,6 +414,7 @@
<ClInclude Include="..\sakura_core\extmodule\CBregexpDll2.h" />
<ClInclude Include="..\sakura_core\extmodule\CDllHandler.h" />
<ClInclude Include="..\sakura_core\extmodule\CHtmlHelp.h" />
<ClInclude Include="..\sakura_core\extmodule\CIcu4cI18n.h" />
<ClInclude Include="..\sakura_core\extmodule\CMigemo.h" />
<ClInclude Include="..\sakura_core\extmodule\CUxTheme.h" />
<ClInclude Include="..\sakura_core\Funccode_define.h" />
Expand Down Expand Up @@ -647,6 +649,7 @@
<ClCompile Include="..\sakura_core\charset\CUnicodeBe.cpp" />
<ClCompile Include="..\sakura_core\charset\CUtf7.cpp" />
<ClCompile Include="..\sakura_core\charset\CUtf8.cpp" />
<ClCompile Include="..\sakura_core\charset\icu4c\CharsetDetector.cpp" />
<ClCompile Include="..\sakura_core\CHokanMgr.cpp" />
<ClCompile Include="..\sakura_core\CKeyWordSetMgr.cpp" />
<ClCompile Include="..\sakura_core\CLoadAgent.cpp" />
Expand Down Expand Up @@ -776,6 +779,7 @@
<ClCompile Include="..\sakura_core\extmodule\CBregexpDll2.cpp" />
<ClCompile Include="..\sakura_core\extmodule\CDllHandler.cpp" />
<ClCompile Include="..\sakura_core\extmodule\CHtmlHelp.cpp" />
<ClCompile Include="..\sakura_core\extmodule\CIcu4cI18n.cpp" />
<ClCompile Include="..\sakura_core\extmodule\CMigemo.cpp" />
<ClCompile Include="..\sakura_core\extmodule\CUxTheme.cpp" />
<ClCompile Include="..\sakura_core\func\CFuncKeyWnd.cpp" />
Expand Down
15 changes: 15 additions & 0 deletions sakura/sakura.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@
<Filter Include="Cpp Source Files\uiparts">
<UniqueIdentifier>{930f3f82-ab3f-49e3-af4a-d4f9c2d51f46}</UniqueIdentifier>
</Filter>
<Filter Include="Cpp Source Files\charset\icu4c">
<UniqueIdentifier>{e4629f85-3be8-4dda-80db-1be310929433}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\sakura_core\Funccode_define.h">
Expand Down Expand Up @@ -1085,6 +1088,12 @@
<ClInclude Include="..\sakura_core\mem\CPoolResource.h">
<Filter>Cpp Source Files\mem</Filter>
</ClInclude>
<ClInclude Include="..\sakura_core\extmodule\CIcu4cI18n.h">
<Filter>Cpp Source Files\extmodule</Filter>
</ClInclude>
<ClInclude Include="..\sakura_core\charset\icu4c\CharsetDetector.h">
<Filter>Cpp Source Files\charset\icu4c</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\resource\auto_scroll_center.cur">
Expand Down Expand Up @@ -2252,6 +2261,12 @@
<ClCompile Include="..\sakura_core\dlg\CDlgOpenFile_CommonItemDialog.cpp">
<Filter>Cpp Source Files\dlg</Filter>
</ClCompile>
<ClCompile Include="..\sakura_core\extmodule\CIcu4cI18n.cpp">
<Filter>Cpp Source Files\extmodule</Filter>
</ClCompile>
<ClCompile Include="..\sakura_core\charset\icu4c\CharsetDetector.cpp">
<Filter>Cpp Source Files\charset\icu4c</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Image Include="..\resource\auto_scroll_center.bmp">
Expand Down
2 changes: 2 additions & 0 deletions sakura_core/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ charset/CUnicode.o \
charset/CUnicodeBe.o \
charset/CUtf7.o \
charset/CUtf8.o \
charset/icu4c/CharsetDetector.o \
cmd/CViewCommander.o \
cmd/CViewCommander_Bookmark.o \
cmd/CViewCommander_Clipboard.o \
Expand Down Expand Up @@ -228,6 +229,7 @@ extmodule/CBregexp.o \
extmodule/CBregexpDll2.o \
extmodule/CDllHandler.o \
extmodule/CHtmlHelp.o \
extmodule/CIcu4cI18n.o \
extmodule/CMigemo.o \
extmodule/CUxTheme.o \
func/CFuncKeyWnd.o \
Expand Down
8 changes: 8 additions & 0 deletions sakura_core/charset/CCodeMediator.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*! @file */
#include "StdAfx.h"
#include "charset/CCodeMediator.h"
#include "charset/icu4c/CharsetDetector.h"
#include "charset/CESI.h"
#include "io/CBinaryStream.h"

Expand All @@ -23,6 +24,13 @@ ECodeType CCodeMediator::CheckKanjiCode(const char* buff, size_t size) noexcept
return m_sEncodingConfig.m_eDefaultCodetype;
}

// ICU4CのDLL群が利用できる場合、ICU4Cによる判定を試みる
CharsetDetector csd;
if (csd.IsAvailable()) {
auto code = csd.Detect(std::string_view(buff, size));
if (code != CODE_ERROR) return code;
}

CESI cesi(m_sEncodingConfig);
return cesi.CheckKanjiCode(buff, size);
}
Expand Down
77 changes: 77 additions & 0 deletions sakura_core/charset/icu4c/CharsetDetector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*! @file */
/*
Copyright (C) 2018-2019 Sakura Editor Organization
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "StdAfx.h"
#include "CharsetDetector.h"

CharsetDetector::CharsetDetector() noexcept
: _icuin()
, _csd(nullptr)
{
_icuin.InitDll();
}

CharsetDetector::~CharsetDetector() noexcept
{
if (_icuin.IsAvailable()) {
_icuin.ucsdet_close(_csd);
}
}

ECodeType CharsetDetector::Detect(const std::string_view& bytes)
{
UErrorCode status = U_ZERO_ERROR;

_csd = _icuin.ucsdet_open(&status);
if (status != U_ZERO_ERROR) {
return CODE_ERROR;
}

_icuin.ucsdet_setText(_csd, bytes.data(), bytes.length(), &status);
if (status != U_ZERO_ERROR) {
return CODE_ERROR;
}

const auto csm = _icuin.ucsdet_detect(_csd, &status);
if (status != U_ZERO_ERROR) {
return CODE_ERROR;
}

std::string_view name = _icuin.ucsdet_getName(csm, &status);
if (status != U_ZERO_ERROR) {
return CODE_ERROR;
}

// 文字セット名⇒サクラエディタ内部コードの変換
if (name == "UTF-8") return CODE_UTF8;
if (name == "SHIFT_JIS") return CODE_SJIS;
if (name == "UTF-16BE") return CODE_UNICODEBE;
if (name == "UTF-16LE") return CODE_UNICODE;
if (name == "EUC-JP") return CODE_EUC;
if (name == "ISO-2022-JP") return CODE_JIS;
if (name == "UTF-7") return CODE_UTF7;
if (name == "ISO-8859-1") return CODE_LATIN1;

return CODE_ERROR;
}
48 changes: 48 additions & 0 deletions sakura_core/charset/icu4c/CharsetDetector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*! @file */
/*
Copyright (C) 2018-2019 Sakura Editor Organization
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#pragma once

#include <string_view>

#include "extmodule/CIcu4cI18n.h"

/*!
* @brief 文字コード検出クラス
*/
class CharsetDetector final
{
CIcu4cI18n _icuin;
UCharsetDetector* _csd;

public:
CharsetDetector() noexcept;
~CharsetDetector() noexcept;

bool IsAvailable() const noexcept {
return _icuin.IsAvailable();
}

ECodeType Detect(const std::string_view& bytes);
};
69 changes: 69 additions & 0 deletions sakura_core/extmodule/CIcu4cI18n.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*! @file */
/*
Copyright (C) 2018-2019 Sakura Editor Organization
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "StdAfx.h"
#include "CIcu4cI18n.h"

CIcu4cI18n::CIcu4cI18n() noexcept
: _ucsdet_open(nullptr)
, _ucsdet_setText(nullptr)
, _ucsdet_detect(nullptr)
, _ucsdet_close(nullptr)
{
}

CIcu4cI18n::~CIcu4cI18n() noexcept
{
}

/*!
* @brief DLLの名前を返す
*/
LPCWSTR CIcu4cI18n::GetDllNameImp(int index)
{
(void*)index;
return L"icuin66.dll"; //バージョンは固定
}

/*!
DLLの初期化
関数のアドレスを取得してメンバに保管する.
@retval true 成功
@retval false アドレス取得に失敗
*/
bool CIcu4cI18n::InitDllImp()
{
//DLL内関数名リスト
const ImportTable table[] = {
{ &_ucsdet_open, "ucsdet_open_66" }, //バージョンは固定
{ &_ucsdet_setText, "ucsdet_setText_66" }, //バージョンは固定
{ &_ucsdet_detect, "ucsdet_detect_66" }, //バージョンは固定
{ &_ucsdet_getName, "ucsdet_getName_66" }, //バージョンは固定
{ &_ucsdet_close, "ucsdet_close_66" }, //バージョンは固定
{ NULL, 0 }
};
return RegisterEntries(table);
}
81 changes: 81 additions & 0 deletions sakura_core/extmodule/CIcu4cI18n.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*! @file */
/*
Copyright (C) 2018-2019 Sakura Editor Organization
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#pragma once

#include "CDllHandler.h"

//ICU4Cの型定義
class UCharsetDetector;
class UCharsetMatch;

typedef enum UErrorCode {
U_ZERO_ERROR = 0, /**< No error, no warning. */
} UErrorCode;

/*!
* ICU4C の i18n ライブラリ(icuin.dll) をラップするクラス
*/
class CIcu4cI18n final : public CDllImp
{
// DLL関数型定義
typedef UCharsetDetector* (_cdecl *ucsdet_open_t)(UErrorCode *status);
typedef void (_cdecl *ucsdet_setText_t)(UCharsetDetector *ucsd, const char *textIn, int32_t len, UErrorCode *status);
typedef const UCharsetMatch * (_cdecl *ucsdet_detect_t)(UCharsetDetector *ucsd, UErrorCode *status);
typedef const char* (_cdecl *ucsdet_getName_t)(const UCharsetMatch *ucsm, UErrorCode *status);
typedef void (_cdecl *ucsdet_close_t)(UCharsetDetector *ucsd);

// メンバ定義
ucsdet_open_t _ucsdet_open;
ucsdet_setText_t _ucsdet_setText;
ucsdet_detect_t _ucsdet_detect;
ucsdet_getName_t _ucsdet_getName;
ucsdet_close_t _ucsdet_close;

public:
CIcu4cI18n() noexcept;
virtual ~CIcu4cI18n() noexcept;

protected:
// CDllImpインタフェース
LPCWSTR GetDllNameImp(int nIndex) override;
bool InitDllImp() override;

public:
inline UCharsetDetector* ucsdet_open(UErrorCode *status) const {
return _ucsdet_open(status);
}
inline void ucsdet_setText(UCharsetDetector *ucsd, const char *textIn, int32_t len, UErrorCode *status) const {
return _ucsdet_setText(ucsd, textIn, len, status);
}
inline const UCharsetMatch* ucsdet_detect(UCharsetDetector *ucsd, UErrorCode *status) const {
return _ucsdet_detect(ucsd, status);
}
inline const char* ucsdet_getName(const UCharsetMatch *ucsm, UErrorCode *status) const {
return _ucsdet_getName(ucsm, status);
}
inline void ucsdet_close(UCharsetDetector *ucsd) const {
return _ucsdet_close(ucsd);
}
};

0 comments on commit fc94768

Please sign in to comment.