//By音符,QQ:337855632 Time:2020-03-17 功能定制:20元起
function File_Class() {
    var hModule = Api.ProcAddress("kernel32");
    var WritePrivateProfileString = Api.ProcAddress(hModule, "WritePrivateProfileStringW");
    var GetPrivateProfileString = Api.ProcAddress(hModule, "GetPrivateProfileStringW");
    Api.ProcAddress(-hModule);      //'FreeLibrary'
    var fso = new ActiveXObject('scripting.FileSystemObject');

    this.ReadINI = function (ApplicationName, KeyName, IniPath, Default, IsNunber) {
        if (Default === undefined) Default = "";
        if (IsNunber === undefined && typeof (Default) == 'number') IsNunber = true
        var retstr = Api.Malloc(2550);
        var l = Api.ECall(GetPrivateProfileString, false, ApplicationName, KeyName, Default.toString(), retstr.GetPtr(0), 2550, IniPath)
        // CollectGarbage()
        return IsNunber ? Api.val(retstr.toString.substr(0, l)) : retstr.toString.substr(0, l)
    }
    this.WriteINI = function (ApplicationName, KeyName, Str, IniPath) {
        Api.ECall(WritePrivateProfileString, false, ApplicationName, KeyName, Str.toString(), IniPath)
    }
    this.GetFolder = function (GPath) {
        return GPath ? Globals("App").Path : fso.GetFolder(".").Path;
    }
    this.FileExists = function (FileName) {
        return fso.FileExists(FileName);
    }
    this.GetPathName = function (FileName) {
        return fso.GetAbsolutePathName(FileName);
    }

    this.FolderExists = function (FileName, Create) {
        FileName = FileName.replace(/\//g, "\\")
        if (Create & 2) {    //传文件名
            var last = FileName.lastIndexOf("\\")
            if (last > 0) {
                if (FileName.substr(last).lastIndexOf(".") > 0)
                    FileName = FileName.substr(0, last)
            }
        }
        if (!fso.FolderExists(FileName)) {    //自动创建
            if (Create & 1)
                return this.FolderCreate(FileName)
            else
                return;
        }
        return FileName
    }
    this.FolderCreate = function (FileName) {
        var FileName = fso.GetAbsolutePathName(FileName)
        Api.ECall("shell32", "SHCreateDirectoryExW", 0, FileName, 0)
        return FileName
    }
    this.Delete = function (FileName, mode) {
        if (mode & 1 ? fso.FolderExists(FileName) : fso.FileExists(FileName)) {
            mode & 1 ? fso.DeleteFolder(FileName, mode & 2) : fso.DeleteFile(FileName, mode & 2)
        }
    }
    this.IsLock = function (Path) {
        var GENERIC_READ = 0x80000000
        var GENERIC_WRITE = 0x40000000
        var OPEN_EXISTING = 3
        var FILE_ATTRIBUTE_NORMAL = 0x80
        var i = Api.ECall("kernel32.dll", "CreateFileW", Path, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
        if (i != -1) {
            Api.ECall("kernel32.dll", "CloseHandle", i)
        }
        return (i == -1);
    }
    this.GetFile = function (Path, Extension, mode) {
        var dt = new Date("1990/1/1");
        var filer = new Object()
        Extension = Extension.toLowerCase
        if (fso.FolderExists(Path)) {
            var Folder = fso.GetFolder(Path)
            // for (var file in Folder.Files) {
            var fenum = new Enumerator(Folder.Files);
            for (; !fenum.atEnd(); fenum.moveNext()) {
                var file = fenum.item()
                if (fso.GetExtensionName(file.Path).toLowerCase == Extension) {
                    var ft = file.DateCreated
                    if (dt < ft) {
                        dt = ft
                        filer = file
                    }
                }
            }
        }
        return filer
    }
    // Private Type tagOPENFILENAME
    //     lStructSize As Long        0 '结构大小
    //     hwndOwner As Long          4 '
    //     hInstance As Long          8 '
    //     strFilter As String        12    '过滤器字符串
    //     strCustomFilter As String  16    '选中的过滤器(过滤器索引所指的过滤器)字符串
    //     nMaxCustFilter As Long     20    '过滤器最大长度
    //     nFilterIndex As Long       24    '选中的过滤器索引,意义与 CommonDialog 控件相同
    //     strFile As String          28    '选中的全路径文件名
    //     nMaxFile As Long           32    '装载全路径文件名的字符串长度
    //     strFileTitle As String     36    '去掉了路径的文件名
    //     nMaxFileTitle As Long      40    '装载去掉了路径的文件名字符串长度
    //     strInitialDir As String    44    '去掉了文件名的路径(没有最后的反斜杠)
    //     strTitle As String         48    '对话框标题,意义与 CommonDialog 控件相同
    //     flags As Long              52    '标志,意义与 CommonDialog 控件相同
    //     nFileOffset As Integer     56    '路径长度(包括最后的反斜杠)
    //     nFileExtension As Integer  58    '全路径文件名长度(不计算前面 3 个表示盘符的字符,如 D:\)
    //     strDefExt As String        60    '默认提取
    //     lCustData As Long          64    '
    //     lpfnHook As Long           68    '勾子函数地址
    //     lpTemplateName As String   72    '
    // End Type
    this.Select = function (Title, Filter, FileName, Dir, DlgType, flags) {
        var Malloc = Api.Malloc(256)
        var i = Api.ECall("kernel32.dll", "GetCurrentDirectoryW", 256, Malloc.GetPtr(0))
        Malloc.ReSize((i) * 2, true)  //需要Chr(0)
        var old = Malloc.toString
        if (Filter) Filter = Filter.replace(/\|/g, "\0")
        var Length = flags ? 25500 : 255
        var strFile = Api.Malloc(Length * 2)
        if (FileName) strFile.SetVal(0, 0) = FileName
        Malloc.ReSize(75)
        Malloc.SetVal(0, 4) = 76
        if (Filter) {
            var iFilter = Api.Malloc()
            iFilter.FromString = Filter + "\0"
            Malloc.SetVal(12, 4) = iFilter.GetPtr(0)
        }
        Malloc.SetVal(24, 4) = 1
        Malloc.SetVal(28, 4) = strFile.GetPtr(0)
        Malloc.SetVal(32, 4) = 25500
        if (Title) {
            var iTitle = Api.Malloc()
            iTitle.FromString = Title + "\0"
            Malloc.SetVal(48, 4) = iTitle.GetPtr(0)
        }
        Malloc.SetVal(52, 4) = flags
        var iDir = Api.Malloc()
        iDir.FromString = (Dir ? Dir : ".") + "\0"
        Malloc.SetVal(44, 4) = iDir.GetPtr(0)
        i = Api.ECall("comdlg32.dll", DlgType ? "GetSaveFileNameW" : "GetOpenFileNameW", Malloc.GetPtr(0))
        Api.ECall("kernel32.dll", "SetCurrentDirectoryW", old)
        if (i) {
            FileName = strFile.toString
            i = FileName.indexOf("\0\0")
            return FileName.slice(0, i).replace(/\0/g, "|");
        }
    }

    this.ReadType = function (FileName) {
        if (!this.FileExists(FileName)) return;
        var m = Api.Malloc()
        var ADO = new ActiveXObject('Adodb.Stream')
        ADO.Type = 1;
        ADO.mode = 3;
        ADO.Open();
        ADO.Position = 0
        ADO.LoadFromFile(FileName);
        m.FromBytes = ADO.read(2);
        ADO.close();
        v1 = m.Read(0, 1);
        v2 = m.Read(1, 1);
        if (v1 == 0xEF && v2 == 0xBB)    //有bom 的
            return "utf-8"
        else if (v1 == 0xFF && v2 == 0xFE)
            return "unicode"
        else if (v1 == 0xFE && v2 == 0xFF)   //unicode big endian
            return "unicode"
        else
            return "gbk"
    }
    this.ReadText = function (FileName, format, characters, Skip) {
        if (fso.FileExists(FileName)) {
            var myfile = fso.OpenTextFile(FileName, 1, false, format || -2);
            // Api.ECall("User32.dll", "MessageBoxW", 0, myfile.ReadAll().toString(),FileName.toString(), 3);
            if (!myfile.AtEndOfStream) {
                if (characters < 0) {
                    for (var i = 0; i < Skip; i++) {
                        myfile.SkipLine()
                    }
                    var Lines = new Array()
                    for (var i = characters; i < 0; i++) {
                        Lines.push(myfile.ReadLine())
                    }
                    var ReadFile = Lines.join("\n")
                } else {
                    if (Skip) myfile.Skip(Skip)
                    var ReadFile = characters ? myfile.Read(characters) : myfile.ReadAll();
                }
            }
            myfile.Close();
            return ReadFile
        }
    }
    this.WriteText = function (FileName, Text, iomode, format) {
        var myfile = fso.OpenTextFile(FileName, iomode ? 8 : 2, true, format ? -1 : 0);
        myfile.Write(Text);
        myfile.Close();
    }
    function Adodb(FileName, charset, me) {
        var format = charset || me.ReadType(FileName);
        var Adodb = new ActiveXObject('Adodb.Stream')
        Adodb.Type = 2;
        Adodb.mode = 3;
        Adodb.charset = format;
        Adodb.Open();
        return Adodb
    }
    this.ReadTextEx = function (FileName, charset, characters, Skip) {
        if (!this.FileExists(FileName)) return;
        var ADO = Adodb(FileName, charset, this)
        ADO.LoadFromFile(FileName);
        if (Skip < 0) {
            for (var i = Skip; i < 0; i++) {
                myfile.SkipLine()
            }
        } else {
            ADO.Position = Skip || 0
        }
        var ReadFileEx = ADO.readtext(characters || -1);
        ADO.close();
        return ReadFileEx
    }
    this.WriteTextEx = function (FileName, Text, characters, charset, rep) {
        var t = this.FileExists(FileName);
        if (!t) charset = charset || "gbk";
        var ADO = Adodb(FileName, charset, this)
        if (characters && t) {
            ADO.LoadFromFile(FileName);
            if (characters == -1) { ADO.Position = Size }     //文件末追加
            else if (characters > 0) {
                ADO.Position = characters - 1 //插入    rep 替换
                if (!rep) { Text += ADO.readtext(); ADO.Position = characters - 1 };
            }
        }
        ADO.WriteText(Text);
        ADO.SaveToFile(FileName, t ? 2 : 1);
        ADO.close();
    }
    this.WriteFlie = function (FileName, Data, iomode) {
        iomode = iomode || this.FileExists(FileName);
        var ADO = new ActiveXObject('Adodb.Stream')
        ADO.Type = 1;
        ADO.Open();
        ADO.Write(Data);
        ADO.SaveToFile(FileName, iomode ? 2 : 1);
        ADO.close();
    }
}