Code: Select all
#NoEnv
#Warn ClassOverwrite
#Requires AutoHotkey v1.1.36.02
class UCharDet
{
__DllLoad() {
static _ := UCharDet.__DllLoad()
if hModule := DllCall("LoadLibrary", "Str", "libuchardet.dll", "Ptr")
DllCall("GetModuleHandleEx", "UInt", 1, "Str", "libuchardet.dll", "Ptr*", 0) ; GET_MODULE_HANDLE_EX_FLAG_PIN
else
throw Exception("Failed to load 'libuchardet.dll'.", -1, A_LastError)
}
/**
* Create an encoding detector.
* @return an instance of uchardet_t.
*
* UCHARDET_INTERFACE uchardet_t uchardet_new(void);
*/
Ptr := DllCall("libuchardet\uchardet_new", "CDecl Ptr")
/**
* Delete an encoding detector.
* @param ud [in] the uchardet_t handle to delete.
*
* UCHARDET_INTERFACE void uchardet_delete(uchardet_t ud);
*/
__Delete() {
if this.Ptr
DllCall("libuchardet\uchardet_delete", "Ptr", this.Ptr, "CDecl")
}
/**
* Feed data to an encoding detector.
* The detector is able to shortcut processing when it reaches certainty
* for an encoding, so you should not worry about limiting input data.
* As far as you should be concerned: the more the better.
*
* @param ud [in] handle of an instance of uchardet
* @param data [in] data
* @param len [in] number of byte of data
* @return non-zero number on failure.
*
* UCHARDET_INTERFACE int uchardet_handle_data(uchardet_t ud, const char * data, size_t len);
*/
HandleData(pBytes, cBytes) {
return DllCall("libuchardet\uchardet_handle_data", "Ptr", this.Ptr, "Ptr", pBytes, "Ptr", cBytes, "CDecl Int")
}
/**
* Notify an end of data to an encoding detector.
* @param ud [in] handle of an instance of uchardet
*
* UCHARDET_INTERFACE void uchardet_data_end(uchardet_t ud);
*/
DataEnd() {
return DllCall("libuchardet\uchardet_data_end", "Ptr", this.Ptr, "CDecl")
}
/**
* Reset an encoding detector.
* @param ud [in] handle of an instance of uchardet
*
* UCHARDET_INTERFACE void uchardet_reset(uchardet_t ud);
*/
Reset() {
return DllCall("libuchardet\uchardet_reset", "Ptr", this.Ptr, "CDecl")
}
/**
* Get an iconv-compatible name of the encoding that was detected.
* @param ud [in] handle of an instance of uchardet
* @return name of charset on success and "" on failure.
*
* UCHARDET_INTERFACE const char * uchardet_get_charset(uchardet_t ud);
*/
GetCharset() {
return DllCall("libuchardet\uchardet_get_charset", "Ptr", this.Ptr, "CDecl AStr")
}
DetectBytes(pBytes, cBytes) {
this.Reset()
if res := this.HandleData(pBytes, cBytes)
throw Exception("Internal 'libuchardet' error, uchardet_handle_data() returned " res)
this.DataEnd()
charset := this.GetCharset()
this.Reset()
return charset
}
}
UCD := new UCharDet()
f := FileOpen("txt.txt", "r")
f.Pos := 0
cBytes := f.RawRead(Bytes, f.Length)
MsgBox % UCD.DetectBytes(&Bytes, cBytes) ; GB18030