Skip to content

Commit

Permalink
文字コード自動判別にWindows10に統合されたICU4Cを使う
Browse files Browse the repository at this point in the history
  • Loading branch information
berryzplus committed Jan 30, 2022
1 parent 66fecdc commit a2d01d3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 38 deletions.
2 changes: 1 addition & 1 deletion sakura_core/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ githash.h:

icu4MinGW/icu.h:
-$(MKDIR) icu4MinGW > $(DEVNULL) 2>&1
cp $(subst /, $(DIRSEP), "C:/Program Files (x86)/Windows Kits/10/Include/10.0.18362.0/um/icu.h") $(subst /, $(DIRSEP), ./)
cp "C:/Program Files (x86)/Windows Kits/10/Include/10.0.18362.0/um/icu.h" ./icu4MinGW/

StdAfx.h.gch: StdAfx.h githash.h Funccode_enum.h icu4MinGW/icu.h
ifneq ($(SRCDIR),.)
Expand Down
20 changes: 6 additions & 14 deletions sakura_core/extmodule/CIcu4cI18n.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,12 @@
#include "StdAfx.h"
#include "CIcu4cI18n.h"

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

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

/*!
Expand All @@ -53,11 +45,11 @@ 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" }, //バージョンは固定
{ &_ucsdet_open, "ucsdet_open" },
{ &_ucsdet_setText, "ucsdet_setText" },
{ &_ucsdet_detect, "ucsdet_detect" },
{ &_ucsdet_getName, "ucsdet_getName" },
{ &_ucsdet_close, "ucsdet_close" },
{ NULL, 0 }
};
return RegisterEntries(table);
Expand Down
40 changes: 18 additions & 22 deletions sakura_core/extmodule/CIcu4cI18n.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*! @file */
/*
Copyright (C) 2018-2021, Sakura Editor Organization
Copyright (C) 2018-2022, 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
Expand All @@ -26,37 +26,33 @@
#define SAKURA_CICU4CI18N_16EE9B14_2791_4C5D_AE1E_E78FBF5CB083_H_
#pragma once

#include "CDllHandler.h"

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

typedef enum UErrorCode {
U_ZERO_ERROR = 0, /**< No error, no warning. */
} UErrorCode;
#include <icu.h>

/*!
* ICU4C の i18n ライブラリ(icuin.dll) をラップするクラス
* ICU4C のライブラリ(icu.dll) をラップするクラス
*
* Windows 10 1703でICU4CがWindowsに統合された。
* この実装では最新1903以降で導入されたicu.dllを使うようにしている。
* 1703~1809だと実装DLLが2つに分かれていてicu.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);
using ucsdet_open_t = decltype(::ucsdet_open);
using ucsdet_setText_t = decltype(::ucsdet_setText);
using ucsdet_detect_t = decltype(::ucsdet_detect);
using ucsdet_getName_t = decltype(::ucsdet_getName);
using ucsdet_close_t = decltype(::ucsdet_close);

// メンバ定義
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;
ucsdet_open_t* _ucsdet_open = nullptr;
ucsdet_setText_t* _ucsdet_setText = nullptr;
ucsdet_detect_t* _ucsdet_detect = nullptr;
ucsdet_getName_t* _ucsdet_getName = nullptr;
ucsdet_close_t* _ucsdet_close = nullptr;

public:
CIcu4cI18n() noexcept;
CIcu4cI18n() noexcept = default;

protected:
// CDllImpインタフェース
Expand Down
2 changes: 1 addition & 1 deletion tests/unittests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ $(exe): $(OBJS) $(SAKURA_OBJS)

icu4MinGW/icu.h:
-$(MKDIR) icu4MinGW > $(DEVNULL) 2>&1
cp $(subst /, $(DIRSEP), "C:/Program Files (x86)/Windows Kits/10/Include/10.0.18362.0/um/icu.h") $(subst /, $(DIRSEP), ./)
cp "C:/Program Files (x86)/Windows Kits/10/Include/10.0.18362.0/um/icu.h" ./icu4MinGW/

.cpp.o: icu4MinGW/icu.h
$(CXX) $(CXXFLAGS) -o $@ -c $<
Expand Down

0 comments on commit a2d01d3

Please sign in to comment.