理解MHS(10):扩展函数

因为种种原因,脚本原生自带的系统函数无法满足应用开发需求,或者某些原生系统函数没有进行导入,所以在MHS的脚本系统中对很多函数进行了扩展,这些函数大部分都在教程中(Lessons目录)有实际应用,对于没有实际应用的函数,可根据函数定义进行使用。
关于SysUtils单元扩展的内容:


一、扩展类型:


type
  RawByteString = AnsiString;


二、扩展变量:

const
  sLineBreak = #13#10;

三、扩展函数:

1.日志相关:

function WriteFmt(const S: string; const Args: array of const): string;
function WritelnFmt(const S: string; const Args: array of const): string;
function WriteLog(const S: string; const Args: array of const): string;
function WritelnLog(const S: string; const Args: array of const): string;
function WriteRuntimeLogFile(const S: string): Boolean;

2.系统扩展

//字符集相关

function UTF8Encode(const WS: WideString): UTF8String;
function UTF8Decode(const S: UTF8String): WideString;
function AnsiToUtf8(const S: string): UTF8String;
function Utf8ToAnsi(const S: UTF8String): string;
function StringReplaceAll(const S, OldPattern, NewPattern: string; IgnoreCase: Boolean = True): string; overload;
function WideStringReplaceAll(const S, OldPattern, NewPattern: WideString; IgnoreCase: Boolean = True): WideString; overload;

function MnVarArrayOf(const Values: array of Variant): Variant;
function IsUtf8Format(Value: RawByteString; MaxSize: Int64 = -1): Boolean;

//文件搜索

function SearchFile(MainPath: string; FileName: string; FoundList: TStrings; IncludeSubDir: Boolean = True; IncludeDirName: Boolean = False): Boolean;

//WideString操作

function Trim(const S: WideString): WideString; overload;
function TrimLeft(const S: WideString): WideString; overload;
function TrimRight(const S: WideString): WideString; overload;

procedure WideCharLenToStrVar(Source: PWideChar; SourceLen: Integer; var Dest: string);
function WideCharToString(Source: PWideChar): string;
procedure WideCharToStrVar(Source: PWideChar; var Dest: string);
function WideCompareStr(const S1, S2: WideString): Integer;
function WideCompareText(const S1, S2: WideString): Integer;
procedure WideFmtStr(var Result: WideString; const Format: WideString; const Args: array of const); overload;
procedure WideFmtStr(var Result: WideString; const Format: WideString; const Args: array of const; const FormatSettings: TFormatSettings); overload;
function WideFormat(const Format: WideString; const Args: array of const): WideString; overload;
function WideFormat(const Format: WideString; const Args: array of const; const FormatSettings: TFormatSettings): WideString; overload;
function WideFormatBuf(var Buffer; BufLen: Cardinal; const Format; FmtLen: Cardinal; const Args: array of const): Cardinal; overload;
function WideFormatBuf(var Buffer; BufLen: Cardinal; const Format; FmtLen: Cardinal; const Args: array of const; const FormatSettings: TFormatSettings): Cardinal; overload;
function WideLowerCase(const S: WideString): WideString;
function WideSameStr(const S1, S2: WideString): Boolean;
function WideSameText(const S1, S2: WideString): Boolean;
function WideUpperCase(const S: WideString): WideString;
function WideStringToUCS4String(const S: WideString): UCS4String;

//Windows函数

function QueryPerformanceCounter(var lpPerformanceCount: Int64): BOOL; stdcall;
function QueryPerformanceFrequency(var lpFrequency: Int64): BOOL; stdcall;
function SetProcessWorkingSetSize(hProcess: Cardinal; dwMinimumWorkingSetSize, dwMaximumWorkingSetSize: Cardinal): BOOL; stdcall;
function GetCurrentProcess: Cardinal; stdcall;
function GetCurrentProcessId: Cardinal; stdcall;

//Md5

function MD5Hex(const Value: AnsiString): AnsiString;
function MD5(const Value: AnsiString): AnsiString;
function MD5Stream(AStream: TStream): AnsiString;
function MD5StreamHex(AStream: TStream): AnsiString;

function SHA1(Input: string; const IsLowerCase: Boolean = True): string;

//只有装载OpenSSL成功才加载下述4个函数

function SHA224(Input: string; const IsLowerCase: Boolean = True): string;
function SHA256(Input: string; const IsLowerCase: Boolean = True): string;
function SHA384(Input: string; const IsLowerCase: Boolean = True): string;
function SHA512(Input: string; const IsLowerCase: Boolean = True): string;

//Base64

function B64Encode(const Input: string): string;
function B64Decode(const Input: string): string;
function VarHasValue(const Value: Variant): Boolean;

//Hash64

function HashOf64(const Value: string): Int64;

//HttpGet

function HttpRequestExecute(const URL, QueryData: string; UserAgent: string; Port: Integer; IsBase64: Boolean): string;
function HttpReqExec(const URL, QueryData: string; UseUtf8: Boolean; UserAgent: string; Port: Integer): string;

//雪花算法

function SnowInit(const ACurMachineID: Word): Boolean;
function SnowID: Int64;

//文件信息

function FileAge(const FileName: string): Integer;
function FileAgeToDateTime(const FileName: string): TDateTime;
function FileSize(const FileName: string): Int64;
function DateTimeToHTTPDate(UTCDateTime: TDateTime): string;

//日期函数

function UnixTimeToDateTime(const UnixTime: Int64): TDateTime;
function DateTimeToUnixTime(const AValue: TDateTime): Int64;
function UnixTimeUTC: Int64;
function UnixMSTimeToDateTime(const UnixMSTime: Int64): TDateTime;
function DateTimeToUnixMSTime(const AValue: TDateTime): Int64;
function UnixMSTimeToString(const UnixMSTime: Int64; Expanded: boolean; FirstTimeChar: AnsiChar; const TZD: string): string;
function NowUTC: TDateTime;

//线程

function GetCurrentThreadID: Cardinal;
function GetTickCount64: Int64;

//Guid、随机数、Hex转换、Url转换

function NewGuid: TGUID;
function NewGuidStr(IsLowerCase: Boolean = False): string;
function GUIDToString(const guid: TGUID): string;
function Random32(max: cardinal): Cardinal;
function Rand64(const IsUInt64: Boolean = False): Int64;
function StrToHex(Str: string): string;
function HexToStr(Hex: string): string;

function UrlEncode(const svar: string): string;
function UrlDecode(U: string): string;

//压缩算法

function GzCompress(var DataRawByteString: RawByteString; Compress: Boolean): RawByteString;
function DeflateCompress(var DataRawByteString: RawByteString; Compress: Boolean): RawByteString;
function ZLibCompress(var DataRawByteString: RawByteString; Compress: Boolean): RawByteString;

//文件读写

function StrToFile(const AValue: string; AFileName: string): Integer;
function StrFromFile(const AFileName: string; const IgnoreUtfFlag: Boolean = True): string;

//获取文件完整名称

function FullAppFile(AFileName: string; AParentPath: string = ''''): string;
function FullWwwFile(AFileName: string; AParentPath: string = ''''): string;

//解析上传文件

function RawStrToVariant(const Value: string): Variant;
function VariantToRawStr(const Value: Variant): string;
function ExtractUploadFile(const Value: AnsiString; var AFiles: Variant): Integer;

//创建系统Timer并允许运行,用户单元使用

function MhsNewSysTimer(const AInterval: Integer; const AScriptFileName: string; RunImediatly: Boolean; const ATimerName: string): Boolean;
function MhsGetSysComponent(const AName: string): TComponent;
function MhsLockDbConnection(const ADbName: string): TObject;
function MhsUnlockDbConnection(const ADBC: TObject): Boolean;
function MhsNewQuery(AQueryName: string; ADBConnection: TObject): TWmQuery;

//Variants单元扩充内容

function VarToWideStr(const V: Variant): WideString;
function VarToWideStrDef(const V: Variant; const ADefault: WideString): WideString;

//Graphics单元扩充内容

function Rect(ALeft, ATop, ARight, ABottom: Integer): TRect;
function RGB(r, g, b: Byte): Cardinal;
function MnBmpToJpg(ABmp: TBitmap; ACompressRate: Byte = 75): AnsiString;
function MnLoadJpg(ABmp: TBitmap; const Value: AnsiString): Boolean;
function MnLoadJpgFromFile(ABmp: TBitmap; const AFileName: AnsiString): Boolean;
function MnBmpToPng(ABmp: TBitmap; ATransColor: TColor = clNone): AnsiString;
function MnLoadPng(ABmp: TBitmap; const Value: AnsiString): Boolean;
function MnLoadPngFromFile(ABmp: TBitmap; const AFileName: AnsiString): Boolean;

//ActiveX单元扩充内容

function CoInitialize(pvReserved: Pointer): HResult; stdcall;
procedure CoUninitialize; stdcall;

//0.3.2706版本新加函数如下

function DirectoryExists(const Directory: string): Boolean;
function ForceDirectories(Dir: string): Boolean;
function LastDelimiter(const Delimiters, S: string): Integer;
function ParamCount: Integer;
function ParamStr(Index: Integer): string;
function FindCmdLineSwitch(const Switch: string; IgnoreCase: Boolean): Boolean; overload;

function ShellExecute(hWnd: HWND; Operation, FileName, Parameters, Directory: PChar; ShowCmd: Integer): HINST; stdcall;
function ShellAbout(Wnd: HWND; szApp, szOtherStuff: PChar; Icon: HICON): Integer; stdcall;

function RightPos(const SubStr: string; const Value: string): Integer;
function GetSimpleJsonStr(Keys: array of WideString; Values: Variant; IsToJson: Boolean = False): WideString;
function SimpleJsonStrToVariant(const Source: string; AValueNames: array of string; var Value: Variant): Boolean;
function GetSimplePostStr(Keys: array of string; Values: Variant; ALineBreak: string = sLineBreak): string;
function StrToArray(const SrcStr: string; var Value: TStrArray; Spec: string = ';'; bIncludeBlank: Boolean = True; const InitArraySize: Integer = 64): Integer; overload;
function StrToArrayV(const SrcStr: string; var Value: Variant; Spec: string = ';'; bIncludeBlank: Boolean = True; const InitArraySize: Integer = 64): Integer; overload;
function VarNewArray(ACount: Integer; AVarType: Word = varVariant): Variant;

function WinRunFile(AFileName: string; AShowWindow: Integer = 0; ANeedWaitTime: Cardinal = 0): Boolean;

 

//0.3.3007新加函数如下:

//通过设置数据库配置的表前缀信息来获取完整的数据库表名,详细使用方法请参考lesson121.
function GetDefFullDbTableName(ATableName: string; ADBName: string = ''; ATablePrefix: string = ''; ADBOwner: string = ''; ATableCommLeft: string = ''; ATableCommRight: string = ''): string;
function SetDefDbTablePrefix(ADBName, ATablePrefix, ADBOwner, ATableCommLeft, ATableCommRight: string; IsForceAdd: Boolean = False): Integer;
function DebugDefDbTablePrefix(IncludeVirtual: Boolean = True): string;
function MhsVersion(): string;//获取当前MHS版本

 

 

0 Comments
因网站受黑客垃圾攻击,留言暂时关闭,如有需要,请自行下载源码参考,谢谢。
留言