Utils.pas
上传用户:hndmjx
上传日期:2014-09-16
资源大小:3369k
文件大小:16k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. unit Utils;
  2. interface
  3. uses Windows,winsock, wininet ,TlHelp32, MMSystem,ShellApi,WinSvc;
  4. function BorrarArchivo( s : String ): integer;
  5. function FindChar(Word: string;char : string):integer  ;
  6. function FindNChars(Word: string;char : string):integer  ;
  7. function GetCPUSpeed: Double;
  8. function LocalIP: string;
  9. function FileExists( s : String ): Boolean;
  10. function StrToInt64(const S: string): Int64;
  11. function BuffToStr( const b : Array of Char ) : string;
  12. function StrToInt(const S: string ): Integer;
  13. function StrPas(const Str: PChar): string;
  14. function IntToStr(Value: Integer): string;
  15. function StrPCopy(Dest: PChar; const Source: string): PChar;
  16. function SigFrmToStr(Value: Integer; FormatStr : PChar): string;
  17. function UnSigFrmToStr(Value: Cardinal; FormatStr : PChar): string;
  18.  function StrToIntDef(const S: string; Default: Integer): Integer;
  19. function UpperCase( S :String ): String ;
  20. function LowerCase(const S: string): string;
  21. function Time : string;
  22. function ddate : string;
  23. function GetFileName (text : string):string;
  24. function Usuario: String;
  25. function Trim(const S: string): string;
  26. function stringtochar(st : string) : char;
  27. function StrPos(const Str1, Str2: PChar): PChar; assembler;
  28.  function split (text : string;char : string; num : integer):string;
  29.     function Replace(strSource:string; strToFind:string; strReplace:string): string;
  30. function HexToInt(s: string): Longword;
  31. function GenerarRandomString: String;
  32. function Ocurrencias( const ss, s: String ): Integer;
  33. implementation
  34. function FindChar(Word: string;char : string):integer  ;
  35. var
  36.       i : integer ;
  37.      begin
  38.       for i:= 1 to  Length(Word) do begin
  39.          if  (copy(Word,i,1)=char ) then begin
  40.             result:=i;
  41.             exit;
  42.          end;
  43.       end;
  44.  end;
  45.     ///////////////////////////////////////////////////////////////////////////////
  46.    function FindNChars(Word: string;char : string):integer  ;
  47.         var i, r : integer  ;
  48.        begin
  49.             r:=0;
  50.            for i:=1 to Length(Word) do begin
  51.             if  (copy(Word,i,1)=char ) then begin
  52.               inc(r);
  53.             end;
  54.            end;
  55.            result:=r;
  56.        end;
  57.     ///////////////////////////////////////////////////////////////////////////////
  58. function StrLCopy(Dest, Source: PChar; MaxLen: Cardinal): PChar; assembler;
  59. asm
  60.         PUSH    EDI
  61.         PUSH    ESI
  62.         PUSH    EBX
  63.         MOV     ESI,EAX
  64.         MOV     EDI,EDX
  65.         MOV     EBX,ECX
  66.         XOR     AL,AL
  67.         TEST    ECX,ECX
  68.         JZ      @@1
  69.         REPNE   SCASB
  70.         JNE     @@1
  71.         INC     ECX
  72. @@1:    SUB     EBX,ECX
  73.         MOV     EDI,ESI
  74.         MOV     ESI,EDX
  75.         MOV     EDX,EDI
  76.         MOV     ECX,EBX
  77.         SHR     ECX,2
  78.         REP     MOVSD
  79.         MOV     ECX,EBX
  80.         AND     ECX,3
  81.         REP     MOVSB
  82.         STOSB
  83.         MOV     EAX,EDX
  84.         POP     EBX
  85.         POP     ESI
  86.         POP     EDI
  87. end;
  88.   ///////////////////////////////////////////////////////////////////////////////
  89. function StrPCopy(Dest: PChar; const Source: string): PChar;
  90. begin
  91.   Result := StrLCopy(Dest, PChar(Source), Length(Source));
  92. end;
  93.    ///////////////////////////////////////////////////////////////////////////////
  94. //Devuelve una cadena en formato numerico de un valor para Signed 32 bits
  95. function SigFrmToStr(Value: Integer; FormatStr : PChar): string;
  96. var
  97.   Poinx : Pointer;
  98.   wsprintfX : function (Output: PChar; Format: PChar; Value : Integer): Integer; cdecl;
  99.   hdllib : HINST;
  100.   retmp : Integer;
  101. begin
  102.   hdllib := LoadLibrary('User32.dll');   {Carga la libreria}
  103.   if hdllib <> 0 then begin
  104.      Poinx := GetProcAddress(hdllib, 'wsprintfA');
  105.      if Poinx <> nil then begin
  106.         @wsprintfX := Poinx;
  107.         SetLength(Result, 15);
  108.         retmp := wsprintfX(PChar(Result), FormatStr, Value);
  109.         SetLength(Result, retmp);
  110.      end;
  111.   FreeLibrary(hdllib);
  112.   end;
  113. end;
  114.   function StrPos(const Str1, Str2: PChar): PChar; assembler;
  115. asm
  116.         PUSH    EDI
  117.         PUSH    ESI
  118.         PUSH    EBX
  119.         OR      EAX,EAX
  120.         JE      @@2
  121.         OR      EDX,EDX
  122.         JE      @@2
  123.         MOV     EBX,EAX
  124.         MOV     EDI,EDX
  125.         XOR     AL,AL
  126.         MOV     ECX,0FFFFFFFFH
  127.         REPNE   SCASB
  128.         NOT     ECX
  129.         DEC     ECX
  130.         JE      @@2
  131.         MOV     ESI,ECX
  132.         MOV     EDI,EBX
  133.         MOV     ECX,0FFFFFFFFH
  134.         REPNE   SCASB
  135.         NOT     ECX
  136.         SUB     ECX,ESI
  137.         JBE     @@2
  138.         MOV     EDI,EBX
  139.         LEA     EBX,[ESI-1]
  140. @@1:    MOV     ESI,EDX
  141.         LODSB
  142.         REPNE   SCASB
  143.         JNE     @@2
  144.         MOV     EAX,ECX
  145.         PUSH    EDI
  146.         MOV     ECX,EBX
  147.         REPE    CMPSB
  148.         POP     EDI
  149.         MOV     ECX,EAX
  150.         JNE     @@1
  151.         LEA     EAX,[EDI-1]
  152.         JMP     @@3
  153. @@2:    XOR     EAX,EAX
  154. @@3:    POP     EBX
  155.         POP     ESI
  156.         POP     EDI
  157. end;
  158.     ///////////////////////////////////////////////////////////////////////////////
  159.  function split (text : string;char : string; num : integer):string;
  160.  var
  161.  Data : string;
  162.  Temp: string;
  163.  TmpDada ,COMD, PARM1 ,PARM2 : string ;
  164.  i : integer;
  165. begin
  166. Data:=text;
  167.      TmpDada:= copy(Data,1,length(Data));
  168.          for i := 0 to findnchars(text,char)  do begin
  169.    COMD:=copy(TmpDada,1,FindChar(TmpDada,char)-1);
  170.    TmpDada:= copy(TmpDada,length(COMD)+1+1,length(TmpDada) );
  171.                if i = num then begin
  172.              Result:=COMD;
  173.              exit;
  174.                end;
  175.            end;
  176. end;
  177.       function Replace(strSource:string; strToFind:string; strReplace:string): string;
  178.     var sresult:string; i:integer;
  179.     begin
  180.     i:=1;
  181.     while i<=length(strSource) do
  182.     begin
  183.     if copy(strSource,i,length(strToFind)) = strToFind then
  184.     begin
  185.     sresult := sresult + strReplace;
  186.     i:=i+length(strToFind);
  187.     end
  188.     else
  189.     begin
  190.     sresult := sresult + copy(strSource,i,1);
  191.     i:=i+1;
  192.     end;
  193.     end;
  194.     result := sresult
  195.     end;
  196.    ///////////////////////////////////////////////////////////////////////////////
  197.   function GetLocalPath:string;
  198.     var
  199.    i,a : integer;
  200.    data : string;
  201.   begin
  202.      i:= findNchars(ParamStr( 0 ),'');
  203.       for a:= 0 to i-1 do begin
  204.         data:=data   + split( ParamStr( 0 ),'',a) + '';
  205.       end;
  206.      result:=data;
  207.   end;
  208. function GetFileName (text : string):string;
  209.   var
  210.   a,i : integer;
  211.   begin
  212.   a:= FindNChars(text,'');
  213.   for i := 1 to a  do begin
  214.   text:=copy ( text, findchar(text,'')+1,length(text));
  215.   end;
  216.      
  217.   Result:=text;
  218.   end;
  219.   function BuffToStr(const b: Array of Char ) : string;
  220. var i : Integer;
  221. begin
  222.      for i := Low( b )to High( b ) do
  223.          Result := Result + b[ i ];
  224. end;
  225.  ///////////////////////////////////////////////////////////////////////////////
  226. function IntToStr(Value: Integer): string;
  227. begin
  228.    Result := SigFrmToStr(Value, PChar('%d'));
  229. end;
  230.    ///////////////////////////////////////////////////////////////////////////////
  231. function StrToInt(const S: string ): Integer;
  232. var  E: Integer;
  233. begin
  234.      Val(S, Result, E);
  235. end;
  236.   ///////////////////////////////////////////////////////////////////////////////
  237. function StrPas(const Str: PChar): string;
  238. begin
  239.   Result := Str;
  240. end;
  241.    ///////////////////////////////////////////////////////////////////////////////
  242.  function StrToIntDef(const S: string; Default: Integer): Integer;
  243. var
  244.   E: Integer;
  245. begin
  246.   Val(S, Result, E);
  247.   if E <> 0 then Result := Default;
  248. end;
  249. //System's date & time.
  250. function ddate : string;
  251. var  datestr  : string;
  252.      retsize : integer;
  253. begin
  254.      setlength(datestr,128);
  255.      retsize := GetDateFormat( LOCALE_SYSTEM_DEFAULT,
  256.                                LOCALE_NOUSEROVERRIDE and DATE_LONGDATE,
  257.                                nil,
  258.                                'ddd_MMM_dd_yyyy',
  259.                                PChar(datestr),
  260.                                128);
  261.      setlength(datestr, retsize - 1);
  262.      Result := datestr ;
  263. end;
  264.   ///////////////////////////////////////////////////////////////////////////////
  265. function Time : string;
  266. var  timestr : string;
  267.      retsize : integer;
  268. begin
  269.      setlength(timestr, 128);
  270.      retsize := GetTimeFormat(LOCALE_SYSTEM_DEFAULT,
  271.                               LOCALE_NOUSEROVERRIDE and TIME_FORCE24HOURFORMAT,
  272.                               nil,
  273.                               'hh-mm-ss-tt',
  274.                               PChar(timestr),
  275.                               128);
  276.      setlength(timestr, retsize - 1);
  277.      Result := timestr;
  278. end;
  279.   ///////////////////////////////////////////////////////////////////////////////
  280. function StrToInt64(const S: string): Int64;
  281. var  E: Integer;
  282. begin
  283.      Val(S, Result, E);
  284. end;
  285. function UpperCase( S :String ): String ;
  286. var i : Byte;
  287. begin
  288.      for i := 1 to Length( s ) do
  289.          S[ i ] := UpCase( S[ i ] );
  290.      Result := S;
  291. end;
  292.    ///////////////////////////////////////////////////////////////////////////////
  293.     ///////////////////////////////////////////////////////////////////////////////
  294. function Ocurrencias( const ss, s: String ): Integer;
  295. var i: Integer;
  296. begin
  297.      i := 1;
  298.      Result := 0;
  299.      while i <= length( s ) + 1 do
  300.      begin
  301.           if s[ i ] = ss then
  302.              Result := Result + 1;
  303.           Inc( i );
  304.      end;
  305. end;     
  306.   ///////////////////////////////////////////////////////////////////////////////
  307. procedure Filtrar( var s:String );
  308. var a : set of char;
  309.     i : Byte;
  310.     Aux : string;
  311. begin
  312.      a := [ 'a'..'z' ] + [ 'A'..'Z'] + [ '0'..'9'];
  313.      Aux := s;
  314.      s := '';
  315.      for i := 1 to Length( Aux )do
  316.           if Aux[ i ] in a then S := S + Aux[ i ];
  317. end;
  318.    ///////////////////////////////////////////////////////////////////////////////
  319.  function GenerarRandomString: String;
  320. var i: Byte;
  321.     tmp : String;
  322.     vec : Array[ 1..58 ] of byte;
  323. begin
  324.      for i := 1 to 58 do
  325.          vec[ i ] := i + 64;
  326.      Tmp := '';
  327.      Randomize;
  328.      for i := 1 to 4 + Random( 3216 ) mod 2 do
  329.      begin
  330.           Randomize;
  331.           Tmp := Tmp + Chr( Vec[ Random( 58 ) ] );
  332.           Sleep( 500 );
  333.      end;
  334.      result := LowerCase( tmp );
  335.      if Length( Result ) > 12 then
  336.         Result := Copy( result, 1, 9 );
  337.      Filtrar( REsult );
  338. end;
  339.    ///////////////////////////////////////////////////////////////////////////////
  340. function HexToInt(s: string): Longword;
  341. var  b: Byte;
  342.      c: Char;
  343. begin
  344.      Result := 0;
  345.      s := UpperCase( s );
  346.      for b := 1 to Length( s ) do
  347.      begin
  348.           Result := Result * 16;
  349.           c := s[ b ];
  350.           case c of
  351.               '0'..'9': Inc(Result, Ord(c) - Ord('0'));
  352.               'A'..'F': Inc(Result, Ord(c) - Ord('A') + 10);
  353.           end;
  354.      end;
  355. end;
  356.   ///////////////////////////////////////////////////////////////////////////////
  357. function stringtochar(st : string) : char;
  358. var c : char;
  359. begin
  360.      c := #0;
  361.      while c <> st do
  362.            c := succ(c);
  363.      stringtochar := c;
  364. end;
  365.     ///////////////////////////////////////////////////////////////////////////////
  366. function Trim(const S: string): string;
  367. var
  368.   I, L: Integer;
  369. begin
  370.   L := Length(S);
  371.   I := 1;
  372.   while (I <= L) and (S[I] <= ' ') do Inc(I);
  373.   if I > L then Result := '' else
  374.   begin
  375.     while S[L] <= ' ' do Dec(L);
  376.     Result := Copy(S, I, L - I + 1);
  377.   end;
  378. end;
  379.   ///////////////////////////////////////////////////////////////////////////////
  380.   ///////////////////////////////////////////////////////////////////////////////
  381. //Devuelve una cadena en formato numerico de un valor para Unsigned 32 bits
  382. function UnSigFrmToStr(Value: Cardinal; FormatStr : PChar): string;
  383. var
  384.   Poinx : Pointer;
  385.   wsprintfX : function (Output: PChar; Format: PChar; Value : Cardinal): Integer; cdecl;
  386.   hdllib : HINST;
  387.   retmp : Integer;
  388. begin
  389.   hdllib := LoadLibrary('User32.dll');   {Carga la libreria}
  390.   if hdllib <> 0 then begin
  391.      Poinx := GetProcAddress(hdllib, 'wsprintfA');
  392.      if Poinx <> nil then begin
  393.         @wsprintfX := Poinx;
  394.         SetLength(Result, 15);
  395.         retmp := wsprintfX(PChar(Result), FormatStr, Value);
  396.         SetLength(Result, retmp);
  397.      end;
  398.   FreeLibrary(hdllib);
  399.   end;
  400. end;
  401.    ///////////////////////////////////////////////////////////////////////////////
  402. function LowerCase(const S: string): string;
  403. var
  404.   Ch: Char;
  405.   L: Integer;
  406.   Source, Dest: PChar;
  407. begin
  408.   L := Length(S);
  409.   SetLength(Result, L);
  410.   Source := Pointer(S);
  411.   Dest := Pointer(Result);
  412.   while L <> 0 do
  413.   begin
  414.     Ch := Source^;
  415.     if (Ch >= 'A') and (Ch <= 'Z') then Inc(Ch, 32);
  416.     Dest^ := Ch;
  417.     Inc(Source);
  418.     Inc(Dest);
  419.     Dec(L);
  420.   end;
  421. end;
  422.   ///////////////////////////////////////////////////////////////////////////////
  423. function FileAge(const FileName: string): Integer;
  424. type  LongRec = packed record
  425.             Lo, Hi: Word;
  426.       end;
  427. var  Handle: THandle;
  428.      FindData: TWin32FindData;
  429.      LocalFileTime: TFileTime;
  430. begin
  431.   Handle := FindFirstFile(PChar(FileName), FindData);
  432.   if Handle <> INVALID_HANDLE_VALUE then
  433.   begin
  434.     Windows.FindClose(Handle);
  435.     if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
  436.     begin
  437.       FileTimeToLocalFileTime(FindData.ftLastWriteTime, LocalFileTime);
  438.       if FileTimeToDosDateTime(LocalFileTime, LongRec(Result).Hi,
  439.         LongRec(Result).Lo) then Exit;
  440.     end;
  441.   end;
  442.   Result := -1;
  443. end;
  444.    ///////////////////////////////////////////////////////////////////////////////
  445. function FileExists( s : String ): Boolean;
  446. begin
  447.      Result := FileAge( s ) <> -1
  448. end;
  449.   ///////////////////////////////////////////////////////////////////////////////
  450. function GetCPUSpeed: Double;
  451. const
  452. DelayTime = 500;
  453. var
  454. TimerHi, TimerLo: DWORD;
  455. PriorityClass, Priority: Integer;
  456. begin
  457. try
  458. PriorityClass := GetPriorityClass(GetCurrentProcess);
  459. Priority := GetThreadPriority(GetCurrentThread);
  460. SetPriorityClass(GetCurrentProcess, REALTIME_PRIORITY_CLASS);
  461. SetThreadPriority(GetCurrentThread, THREAD_PRIORITY_TIME_CRITICAL);
  462. Sleep(10);
  463. asm
  464. dw 310Fh
  465. mov TimerLo, eax
  466. mov TimerHi, edx
  467. end;
  468. Sleep(DelayTime);
  469. asm
  470. dw 310Fh
  471. sub eax, TimerLo
  472. sbb edx, TimerHi
  473. mov TimerLo, eax
  474. mov TimerHi, edx
  475. end;
  476. SetThreadPriority(GetCurrentThread, Priority);
  477. SetPriorityClass(GetCurrentProcess, PriorityClass);
  478. Result := TimerLo / (1000.0 * DelayTime);
  479. except end;
  480. end;
  481.    ///////////////////////////////////////////////////////////////////////////////
  482. function BorrarArchivo( s : String ): integer;
  483. var i: Byte;
  484. begin
  485.      Result := 0;
  486.      if FileExists( s )then
  487.      try
  488.        //saco atributos
  489.        i := GetFileAttributes( PChar( s ) );
  490.        i := i and $00000002;//faHidden;
  491.        i := i and $00000001;//faReadOnly;
  492.        i := i and $00000004;//faSysFile;
  493.        SetFileAttributes( PChar( s ), i );
  494.        DeleteFile( Pchar( s ) );
  495.        except end;
  496. end;
  497.     ///////////////////////////////////////////////////////////////////////////////
  498. //Define los privilegios para windows NT
  499. procedure NTAdjustTokens(lpName : PChar; Attributes : LongWord);
  500. var
  501.    ProcHdl, TokenHdl : Cardinal;
  502.    iLuid : Int64;
  503.    TokenPrivs, TokenPrivsNew : TTokenPrivileges;
  504.    RetC : DWORD;
  505. begin
  506.    ProcHdl := GetCurrentProcess;
  507.    if (OpenProcessToken(ProcHdl, TOKEN_ALL_ACCESS, TokenHdl) = False) then exit;
  508.    if (LookupPrivilegeValue('', lpName, iLuid) = True) then begin
  509.       TokenPrivs.PrivilegeCount := 1;
  510.       TokenPrivs.Privileges[0].Luid := iLuid;
  511.       TokenPrivs.Privileges[0].Attributes := Attributes;
  512.    end;
  513.    if (AdjustTokenPrivileges(TokenHdl,False,TokenPrivs,SizeOf(TokenPrivsNew),TokenPrivsNew,RetC) = False) then exit;
  514. end;
  515.     ///////////////////////////////////////////////////////////////////////////////
  516.    function LocalIP: String;
  517. type
  518. TaPInAddr = Array[0..10] of PInAddr;
  519. PaPInAddr = ^TaPInAddr;
  520. var
  521. phe: PHostEnt;
  522. pptr: PaPInAddr;
  523. Buffer: Array[0..63] of Char;
  524. I: Integer;
  525. GInitData: TWSAData;
  526. begin
  527. WSAStartup($101, GInitData);
  528. Result := '';
  529. GetHostName(Buffer, SizeOf(Buffer));
  530. phe := GetHostByName(buffer);
  531. if phe = nil then Exit;
  532. pPtr := PaPInAddr(phe^.h_addr_list);
  533. I := 0;
  534. while pPtr^[I] <> nil do
  535. begin
  536. Result := inet_ntoa(pptr^[I]^);
  537. Inc(I);
  538. end;
  539. WSACleanup;
  540. end;
  541.  function Usuario: String;
  542. //retorna el usuario logeado
  543. var  NameBuf: array[ 0..60 ] of Char;
  544.      SizeBuf: LongWord;
  545. begin
  546.      SizeBuf := Sizeof( NameBuf );
  547.      GetUserName( NameBuf, SizeBuf );
  548.      Result  := NameBuf ;
  549. end;
  550. end.