//By音符,QQ:337855632 Time:2020-03-17 功能定制:20元起
function File_Class() {
    var me = this;
    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)
        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 me.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 (var i = 0; !fenum.atEnd(); fenum.moveNext(), i++) {
                var file = fenum.item()
                if (fso.GetExtensionName(file.Path).toLowerCase == Extension) {
                    var ft = file.DateCreated
                    if (dt < ft) {
                        dt = ft
                        filer = file
                    }
                }
            }
        }
        return filer
    }
    this.ReadType = function (FileName) {
        if (!me.FileExists(FileName)) return;
        var m = Api.Malloc()
        with (new ActiveXObject('Adodb.Stream')) {
            Type = 1;
            mode = 3;
            Open();
            Position = 0
            LoadFromFile(FileName);
            m.FromBytes = read(2);
            close();
        }
        v1 = m.ReadVal(0, 1);
        v2 = m.ReadVal(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) {
        if (fso.FileExists(FileName)) {
            format = format || -2;
            var myfile = fso.OpenTextFile(FileName, 1, false, format);
            // Api.ECall("User32.dll", "MessageBoxW", 0, myfile.ReadAll().toString(),FileName.toString(), 3);
            if (!myfile.AtEndOfStream)
                var ReadFile = 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) {
        var format = charset || me.ReadType(FileName);
        var Adodb = new ActiveXObject('Adodb.Stream')
        with (Adodb) {
            Type = 2;
            mode = 3;
            charset = format;
            Open();
        }
        return Adodb
    }
    this.ReadTextEx = function (FileName, charset) {
        if (!me.FileExists(FileName)) return;
        with (Adodb(FileName, charset)) {
            LoadFromFile(FileName);
            var ReadFileEx = readtext();
            close();
        }
        return ReadFileEx
    }
    this.WriteTextEx = function (FileName, Text, iomode, charset, rep) {
        var t = me.FileExists(FileName);
        if (!t) charset = charset || "gbk";
        with (Adodb(FileName, charset)) {
            if (iomode && t) LoadFromFile(FileName);
            if (t) {
                if (iomode == -1) { Position = Size }     //文件末追加
                else if (iomode > 0) {
                    Position = iomode - 1 //插入    rep 替换
                    if (!rep) { Text += readtext(); Position = iomode - 1 };
                }
            }
            WriteText(Text);
            SaveToFile(FileName, t ? 2 : 1);
            close();
        }
    }
    this.WriteFlie = function (FileName, Data, iomode) {
        iomode = iomode || me.FileExists(FileName);
        with (new ActiveXObject('Adodb.Stream')) {
            Type = 1;
            Open();
            Write(Data);
            SaveToFile(FileName, iomode ? 2 : 1);
            close();
        }
    }
}