177 lines
6.3 KiB
Plaintext
177 lines
6.3 KiB
Plaintext
{*******************************************************}
|
|
{ }
|
|
{ ProcessHwp }
|
|
{ }
|
|
{ Copyright (C) 2022 sunk }
|
|
{ }
|
|
{*******************************************************}
|
|
|
|
// DFRC HWP 아래한글 문서 필터
|
|
|
|
unit ProcessHwp;
|
|
|
|
interface
|
|
|
|
uses
|
|
System.Classes, Winapi.Windows, System.SysUtils;
|
|
|
|
const
|
|
DLL_DFRCFT_HWP = 'kvhvp.dll';
|
|
|
|
// 기본적으로 UNICODE로 텍스트를 버퍼에 저장하며, UTF8, CP949로의 변환이 가능하다.
|
|
DFRCFT_UNICODE = 10;
|
|
DFRCFT_UTF8 = 20;
|
|
DFRCFT_CP949 = 30;
|
|
DFRCFT_HWP = 1000;
|
|
|
|
type
|
|
PDfrcftBuffer = ^TDfrcftBuffer;
|
|
TDfrcftBuffer = record
|
|
dwLen: UINT; // 유니코드 길이
|
|
pBuf: UINT16; // 유니코드 버퍼
|
|
end;
|
|
|
|
Tdfrcft_detect_file = function(pFilePath: PByte): Integer; cdecl;
|
|
Tdfrcft_hwp_suminfo_file = function(pFilePath: PByte; pDfrcftBuf: PDfrcftBuffer): Integer; cdecl;
|
|
Tdfrcft_hwp_text_file = function(pFilePath: PByte; pDfrcftBuf: PDfrcftBuffer): Integer; cdecl;
|
|
Tdfrcft_convert_encoding = function(pDfrcftBuf: PDfrcftBuffer; pBuf: PByte; dwCharEncoding: DWORD): Integer; cdecl;
|
|
Tdfrcft_convert_encoding_free_mem = procedure(pBuf: PByte); cdecl;
|
|
Tdfrcft_save_file = procedure(pDfrcftBuf: PDfrcftBuffer; pFilePath: PByte; dwCharEncoding: DWORD); cdecl;
|
|
Tdfrcft_free_mem = procedure(pDfrcftBuf: PDfrcftBuffer); cdecl;
|
|
|
|
function dfrcft_detect_file(sFilePath: AnsiString): Integer;
|
|
function dfrcft_hwp_suminfo_file(sFilePath: AnsiString; pDfrcftBuf: PDfrcftBuffer): Integer;
|
|
function dfrcft_hwp_text_file(sFilePath: AnsiString; pDfrcftBuf: PDfrcftBuffer): Integer;
|
|
function dfrcft_convert_encoding(pDfrcftBuf: PDfrcftBuffer; pBuf: PByte; dwCharEncoding: DWORD): Integer;
|
|
procedure dfrcft_convert_encoding_free_mem(pBuf: PByte);
|
|
procedure dfrcft_save_file(pDfrcftBuf: PDfrcftBuffer; sFilePath: AnsiString; dwCharEncoding: DWORD);
|
|
procedure dfrcft_free_mem(pDfrcftBuf: PDfrcftBuffer);
|
|
|
|
function FilterHwp(sSrcPath, sDestPath: String): Boolean;
|
|
|
|
implementation
|
|
|
|
var
|
|
hDfrcftDLL: THandle = 0;
|
|
_fn_dfrcft_detect_file: Tdfrcft_detect_file = nil;
|
|
_fn_dfrcft_hwp_suminfo_file: Tdfrcft_hwp_suminfo_file = nil;
|
|
_fn_dfrcft_hwp_text_file: Tdfrcft_hwp_text_file = nil;
|
|
_fn_dfrcft_convert_encoding: Tdfrcft_convert_encoding = nil;
|
|
_fn_dfrcft_convert_encoding_free_mem: Tdfrcft_convert_encoding_free_mem = nil;
|
|
_fn_dfrcft_save_file: Tdfrcft_save_file = nil;
|
|
_fn_dfrcft_free_mem: Tdfrcft_free_mem = nil;
|
|
|
|
function InitHwpDfrcftProcedure: Boolean;
|
|
begin
|
|
if hDfrcftDLL = 0 then
|
|
begin
|
|
hDfrcftDLL := LoadLibrary('.\bin\' + DLL_DFRCFT_HWP);
|
|
if hDfrcftDLL = 0 then
|
|
hDfrcftDLL := LoadLibrary(DLL_DFRCFT_HWP);
|
|
|
|
if hDfrcftDLL <> 0 then
|
|
begin
|
|
@_fn_dfrcft_detect_file := GetProcAddress(hDfrcftDLL, 'dfrcft_detect_file');
|
|
@_fn_dfrcft_hwp_suminfo_file := GetProcAddress(hDfrcftDLL, 'dfrcft_hwp_suminfo_file');
|
|
@_fn_dfrcft_hwp_text_file := GetProcAddress(hDfrcftDLL, 'dfrcft_hwp_text_file');
|
|
@_fn_dfrcft_convert_encoding := GetProcAddress(hDfrcftDLL, 'dfrcft_convert_encoding');
|
|
@_fn_dfrcft_convert_encoding_free_mem := GetProcAddress(hDfrcftDLL, 'dfrcft_convert_encoding_free_mem');
|
|
@_fn_dfrcft_save_file := GetProcAddress(hDfrcftDLL, 'dfrcft_save_file');
|
|
@_fn_dfrcft_free_mem := GetProcAddress(hDfrcftDLL, 'dfrcft_free_mem');
|
|
end;
|
|
end;
|
|
Result := hDfrcftDLL <> 0;
|
|
end;
|
|
|
|
function FilterHwp(sSrcPath, sDestPath: String): Boolean;
|
|
var
|
|
DfrcftBuf: TDfrcftBuffer;
|
|
pBuf: PByte;
|
|
fs: TFileStream;
|
|
begin
|
|
Result := false;
|
|
try
|
|
ZeroMemory(@DfrcftBuf, SizeOf(DfrcftBuf));
|
|
if (dfrcft_hwp_text_file(sSrcPath, @DfrcftBuf) = 1) then
|
|
begin
|
|
fs := nil;
|
|
pBuf := nil;
|
|
try
|
|
dfrcft_convert_encoding(@DfrcftBuf, @pBuf, DFRCFT_UTF8);
|
|
fs := TFileStream.Create(sDestPath, fmCreate);
|
|
fs.Write(pBuf^, StrLen(PAnsiChar(pBuf)));
|
|
Result := true;
|
|
finally
|
|
if pBuf <> nil then
|
|
dfrcft_convert_encoding_free_mem(pBuf);
|
|
if DfrcftBuf.pBuf <> 0 then
|
|
dfrcft_free_mem(@DfrcftBuf);
|
|
if fs <> nil then
|
|
fs.Free;
|
|
end;
|
|
end
|
|
except
|
|
// ...
|
|
end;
|
|
end;
|
|
|
|
// FilePath의 파일이 한글 문서 파일인지 판단한다.
|
|
// return - DFRCFT_HWP : 한글 문서 파일
|
|
function dfrcft_detect_file(sFilePath: AnsiString): Integer;
|
|
begin
|
|
Result := 0;
|
|
if InitHwpDfrcftProcedure and Assigned(_fn_dfrcft_detect_file) then
|
|
Result := _fn_dfrcft_detect_file(@sFilePath[1]);
|
|
end;
|
|
|
|
// 문서 속성 정보를 추출한다.
|
|
// return - TRUE : 성공, FALSE : 실패
|
|
function dfrcft_hwp_suminfo_file(sFilePath: AnsiString; pDfrcftBuf: PDfrcftBuffer): Integer;
|
|
begin
|
|
Result := 0;
|
|
if InitHwpDfrcftProcedure and Assigned(_fn_dfrcft_hwp_suminfo_file) then
|
|
Result := _fn_dfrcft_hwp_suminfo_file(@sFilePath[1], pDfrcftBuf);
|
|
end;
|
|
|
|
// 본문 텍스트를 추출한다.
|
|
// return - TRUE : 성공, FALSE 실패
|
|
function dfrcft_hwp_text_file(sFilePath: AnsiString; pDfrcftBuf: PDfrcftBuffer): Integer;
|
|
begin
|
|
Result := 0;
|
|
if InitHwpDfrcftProcedure and Assigned(_fn_dfrcft_hwp_text_file) then
|
|
Result := _fn_dfrcft_hwp_text_file(@sFilePath[1], pDfrcftBuf);
|
|
end;
|
|
|
|
// 인코딩을 변환한다. (사용자 정의 상수 참조)
|
|
// 인코딩 변환에 사용한 버퍼는 dfrcft_convert_encoding_free_mem 함수를 호출해서 해제해야 한다.
|
|
// return - 변환된 문자열의 길이
|
|
function dfrcft_convert_encoding(pDfrcftBuf: PDfrcftBuffer; pBuf: PByte; dwCharEncoding: DWORD): Integer;
|
|
begin
|
|
Result := 0;
|
|
if InitHwpDfrcftProcedure and Assigned(_fn_dfrcft_convert_encoding) then
|
|
Result := _fn_dfrcft_convert_encoding(pDfrcftBuf, pBuf, dwCharEncoding);
|
|
end;
|
|
|
|
// 인코딩에 사용한 버퍼를 해제
|
|
procedure dfrcft_convert_encoding_free_mem(pBuf: PByte);
|
|
begin
|
|
if InitHwpDfrcftProcedure and Assigned(_fn_dfrcft_convert_encoding_free_mem) then
|
|
_fn_dfrcft_convert_encoding_free_mem(pBuf);
|
|
end;
|
|
|
|
// 필터링된 텍스트를 파일로 저장한다.
|
|
procedure dfrcft_save_file(pDfrcftBuf: PDfrcftBuffer; sFilePath: AnsiString; dwCharEncoding: DWORD);
|
|
begin
|
|
if InitHwpDfrcftProcedure and Assigned(_fn_dfrcft_save_file) then
|
|
_fn_dfrcft_save_file(pDfrcftBuf, @sFilePath[1], dwCharEncoding);
|
|
end;
|
|
|
|
// 텍스트 필터링에 사용한 PDfrcftBuffer 메모리를 해제한다.
|
|
procedure dfrcft_free_mem(pDfrcftBuf: PDfrcftBuffer);
|
|
begin
|
|
if InitHwpDfrcftProcedure and Assigned(_fn_dfrcft_free_mem) then
|
|
_fn_dfrcft_free_mem(pDfrcftBuf);
|
|
end;
|
|
|
|
end.
|