⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zkutils.pas

📁 在作数据库中个人认为必要的函数和过程
💻 PAS
📖 第 1 页 / 共 2 页
字号:

unit zkUtils;

interface
uses
  SysUtils, Windows, Classes, MD5,Variants;
  
type
  TBit = 0..31;
  
const
  { 一些有用的ASCII字符 }
  NULL            =  #0;
  BACKSPACE       =  #8;
  TAB             =  #9;
  LF              = #10;
  CR              = #13;
  ESC             = #27;
  BLANK           = #32;
  CRLF            = #13#10;
  SPACE           = BLANK;
  


  function FormatPath(const Value:String;const DIVChar: Char = '\'): String;
  {格式化路径字符串使之以'\'结束}
  {例子:FormatPath('E:\Temp') = 'E:\Temp\'}
  
  function Replace(Str,s1,s2: String; CaseSensitive: Boolean): String;
  {在目标字符串中替换所有子字符串}
  {例子: Replace('We know what we want','we','I',false) = 'I Know what I want'}
  
  function CopyFromChar(const s: String;const c: Char;const l: Integer): String;
  {从目标字符串s的c所在位置开始复制l个字符}
  {例子: CopyFromChar('Borland Delphi','a',3) = 'and'}
  
	function Get_ComputerName: String;
  {获取当前计算机名称}
  {例子: Get_ComputerName = 'JSZXServer'}

	function Get_IPAddress: String;
	{获取本地IP地址}
  {例子: Get_IPAddress = '146.128.20.203'}

	function Get_UserName: String;
  {获取当前计算机登录用户名称}
  {例子: Get_UserName = 'Gaisy'}

	function GetSystemDir: String;
  {获取Windows系统目录路径包括驱动器}
  {例子: GetSystemDir = 'C:\WINDOWS\SYSTEM\'}
  
  function GetWinDir: String;
  {获取Windows目录路径包括驱动器}
  {例子: GetWinDir = 'C:\WINDOWS\'}
  
  function GetCurrentDir: String;
  {获取系统当前目录路径包括驱动器}
  {例子: GetCurrentDir = 'C:\WINDOWS\'}
  
  function GetInstallDir: String;
  {返回exe文件的所在目录路径包括驱动器}
  {例子: GetInstallDir = 'C:\PROGRAM FILES\BORLAND\DELPHI\BIN\'}
  
  function GetFileDate(Filename: String): TDateTime;
  {返回文件的日期}
  function GetFileVersion(Filename: String): String;
  {返回某一文件的版本信息}


  function SQLString(const AStr: String): String;
  {返回字符串的TSQL编码}

  function MD5Hash(const Source: String): String;
  function  DataFieldToString(VData: Variant): String;
  function  DataFieldToInt(VData: Variant): Integer;
  function  DataFieldToCurr(VData: Variant): Currency;

  {-------------------------------------------------------------------------------
  过程名:    位操作函数组
  作者:      gaisy
  日期:      2004.06.14
  参数:      const Value: Integer; const TheBit: TBit
  Value为操作数, TheBit为操作位
  例子:      if BitSet(27, 0) then ...;
  判断27第0位
  -------------------------------------------------------------------------------}
  function bitSet(const Value: Integer;const TheBit: TBit): Boolean;
  function bitOn(const Value: Integer;const TheBit: TBit): Integer;
  function bitOff(const Value: Integer;const TheBit: TBit): Integer;
  function bitToggle(const Value: Integer;const TheBit: TBit): Integer;

  function BigbitSet(const Value: LongInt;const TheBit: TBit): Boolean;
  function BigbitOn(const Value: LongInt;const TheBit: TBit): LongInt;
  function BigbitOff(const Value: LongInt;const TheBit: TBit): LongInt;
  function BigbitToggle(const Value: LongInt;const TheBit: TBit): LongInt;
  
  {-------------------------------------------------------------------------------
  过程名:    去除字符串左右空格函数(扩展)
  作者:      gaisy
  日期:      2004.06.14
  参数:      const S: String; C: Char
  对字符串S去除左右为C的字符
  返回值:    String
  功能描述:
  -------------------------------------------------------------------------------}
  function strTrimChL(const S: String; C: Char): String;
  function strTrimChR(const S: String; C: Char): String;
  function strTrim(const S: String): String;

  /// <summary>
  /// 分解字符串函数
  /// </summary>
  /// <param name="Source">源字符串</param>
  /// <param name="ch">分割符</param>
  /// <returns>包含分割结果的字符串表</returns>
  function SplitString(const Source,ch:String): TStringList;


  {-------------------------------------------------------------------------------
  过程名:    关于日期编解码的函数和过程
  作者:      gaisy
  日期:      2004.06.14
  参数:      const Year: Integer; Month, Day: Word
  返回值:    TDateTime
  功能描述:
  -------------------------------------------------------------------------------}
  function EncodeDate(const Year: Integer; Month, Day: Word): TDateTime;
  procedure DecodeDate(Date: TDateTime;var Year, Month, Day: Word); overload;
  procedure DecodeDate(Date: TDateTime;var Year: Integer;var Month, Day: Word); overload;
  procedure DecodeDate(Date: TDateTime;var Year, Month, Day: Integer); overload;
  
  
  {-------------------------------------------------------------------------------
  函数名:    DateStr
  作者:      gaisy
  日期:      2004.06.14
  参数:      DateTime: TDateTime; DivChar: Char=' '
  返回值:    String
  功能描述:  返回DataTime类型装换成的字符串,以DivChar分隔
  -------------------------------------------------------------------------------}
  function DateStr(DateTime: TDateTime; DivChar: Char=' '): String;
  
  {-------------------------------------------------------------------------------
  过程名:    Year、Month、Day
  作者:      gaisy
  日期:      2004.06.14
  参数:      DateTime: TDateTime
  返回值:    Integer
  功能描述:  返回日期的年月日
  -------------------------------------------------------------------------------}
  function Year(DateTime: TDateTime): Integer;
  function Month(DateTime: TDateTime): Integer;
  function Day(DateTime: TDateTime): Integer;
  
  
  {-------------------------------------------------------------------------------
  过程名:    StrToHex、HexToStr
  作者:      gaisy
  日期:      2004.06.14
  参数:      AData: String
  返回值:    String
  功能描述:  转换10进制数到16进制数
  -------------------------------------------------------------------------------}
  function StrToHex(AData: String): String;
  function HexToStr(AData: String): String;
  
  
  {-------------------------------------------------------------------------------
  过程名:    CreateFolder
  作者:      gaisy
  日期:      2004.06.14
  参数:      const AFolderName: String
  返回值:    Boolean
  功能描述:  建立文件夹
  -------------------------------------------------------------------------------}
  function CreateFolder(const AFolderName: String): Boolean;
  
  
  {-------------------------------------------------------------------------------
  过程名:    IsInteger、IsNumeric
  作者:      gaisy
  日期:      2004.06.14
  参数:      Value: String
  返回值:    Boolean
  功能描述:  判断一个数是否是整数、数字
  -------------------------------------------------------------------------------}
  function IsInteger(Value: String): Boolean;
  function IsNumeric(Value: String): Boolean;
  
  
  {-------------------------------------------------------------------------------
  过程名:    GetPYIndexChar
  作者:      gaisy
  日期:      2004.06.14
  参数:      hzChar: String
  返回值:    Char
  功能描述:  返回汉字的首母
  -------------------------------------------------------------------------------}
  Function GetPYIndexChar(const hzChar: String): Char;
  
  
	{-------------------------------------------------------------------------------
  过程名:    NumberToChinese
  作者:      gaisy
  日期:      2004.06.14
  参数:      n0 : Real
  返回值:    String
  功能描述:  将数字转换为中文表示,用作财务处理
  -------------------------------------------------------------------------------}
  Function NumberToChinese(const n0 : Real): String;
  

	Function GetRandomString(const Len: Integer): String;

  //主要用户MD5运算结果的显示
	function ByteArrayToStr(D: array of Byte): String;
	function CharArrayToStr(D: array of Char): String;
	
implementation
uses
	Sockets;
const
	DaysPerYear       = 365.2422454;          // Solar Year
	DaysPerMonth      = DaysPerYear / 12;
	DateTimeBaseDay   = -693593;              //  1/1/0001
	EncodeDateMaxYear = 9999;
	SolarDifference   = 1.7882454;            //  Difference of Juliab Calendar to Solar Calendar at 1/1/10000
  DateTimeMaxDay    = 2958466;              //  12/31/EncodeDateMaxYear + 1;

function Get_ComputerName: String;
var
	c: array[0..127] of Char;
	sz: dword;
begin
	sz := SizeOf(c);
  GetComputerName(c, sz);
	Result := c;
end;

function Get_UserName: String;
var
	u: array[0..127] of Char;
	sz: dword;
begin
	sz := SizeOf(u);
  GetUserName(u, sz);
	Result := u;
end;

function Get_IPAddress: String;
var
	tp: TTcpClient;
	straddr: String;
begin
	tp := TTcpClient.Create(nil);
	tp.Close;
	tp.Open;
	straddr := tp.LocalHostAddr;
	Result := straddr;
	tp.Close;
	tp.Free;
end;

function CopyFromChar(const s:String; const c:Char; const l:Integer):String;
  var
    i:Integer;
begin
  	i := Pos(c,s);
  	Result := Copy(s,i,l);
end;

function StrToHex(AData: String): String;
var
  StrResult, Temp: String;
  i: Integer;
begin
  for i := 0 to Length(AData) - 1 do
  begin
    Temp := Format('%x', [Ord(AData[i + 1])]);
    if Length(Temp) = 1 then Temp := '0' + Temp;
    StrResult := StrResult + Temp;
  end;
  Result := StrResult;
end;

function HexToStr(AData: String): String;
function HexToInt(Hex: String): Integer;
var
  Res, I: Integer;
  Ch: Char;
begin
  Res := 0;
  for I := 0 to Length(Hex) - 1 do
  begin
    Ch := Hex[I + 1];
    if (Ch >= '0') and (Ch <= '9') then
      Res := Res * 16 + Ord(ch) - Ord('0')
    else if (Ch >= 'A') and (Ch <= 'F') then
        Res := Res * 16 + Ord(Ch) - Ord('A') + 10
      else if (Ch >= 'a') and (Ch <= 'f') then
          Res := Res * 16 + Ord(Ch) - Ord('a') + 10
  end;
  Result := Res;
end;

var
  Str, Temp: String;
  I: Integer;
begin
  Str := '';
  for I := 0 to Length(AData) div 2 - 1 do
  begin
    Temp := Copy(AData, I * 2 + 1, 2);
    Str := Str + Chr(HexToInt(Temp));
  end;
  Result := Str;
end;

function strTrim(const S: String): String;
begin
  Result:=StrTrimChR(StrTrimChL(S,BLANK),BLANK);
end;

function strTrimChL(const S: String; C: Char): String;
begin
  Result:=S;
  while (Length(Result)>0) and (Result[1]=C) do Delete(Result,1,1);
end;

function strTrimChR(const S: String; C: Char): String;
begin
  Result:=S;
  while (Length(Result)> 0) and (Result[Length(Result)]=C) do
    Delete(Result,Length(Result),1);
end;

function bitSet(const Value: Integer; const TheBit: TBit): Boolean;
begin
  Result := (Value and (1 shl TheBit)) <> 0;
end;

function bitOn(const Value: Integer; const TheBit: TBit): Integer;
begin
  Result := Value or (1 shl TheBit);
end;

function bitOff(const Value: Integer; const TheBit: TBit): Integer;
begin
  Result := Value and ((1 shl TheBit) xor $FFFFFFFF);
end;

function bitToggle(const Value: Integer; const TheBit: TBit): Integer;
begin
  Result := Value xor (1 shl TheBit);
end;

function BigbitSet(const Value: LongInt; const TheBit: TBit): Boolean;
begin
  Result := (Value and (1 shl TheBit)) <> 0;
end;

function BigbitOn(const Value: LongInt; const TheBit: TBit): LongInt;
begin
  Result := Value or (1 shl TheBit);
end;

function BigbitOff(const Value: LongInt; const TheBit: TBit): LongInt;
begin
  Result := Value and ((1 shl TheBit) xor $FFFFFFFFFFFFFFFF);
end;

function BigbitToggle(const Value: LongInt; const TheBit: TBit): LongInt;
begin
  Result := Value xor (1 shl TheBit);
end;

function FormatPath(const Value:String; const DIVChar: Char = '\'): String;
begin
  if (Value[Length(Value)]<>DIVChar) then
    Result := Value + DIVChar
  else
    Result := Value;
end;

function Replace(Str,s1,s2:String; CaseSensitive: Boolean): String;
var
  i: integer;
  s,t: String;
begin
  s := '';
  t := Str;
  repeat
    if CaseSensitive then
      i := pos(s1,t)
    else
      i := pos(LowerCase(s1),LowerCase(t));
    if i>0 then
    begin
      s := s + Copy(t,1,i-1) + s2;
      t := Copy(t,i+Length(s1),MaxInt);
    end
    else s := s+t;
  until i<=0;
  Result := s;
end;
function GetSystemDir: String;
var
  p: PChar;
  z: Integer;
begin
  z := 255;
  GetMem(p,z);
  GetSystemDirectory(p,z);
  Result := FormatPath(String(p));
  FreeMem(p,z);
end;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -