📄 delphifiles.inc
字号:
{
This part of the unit modified by Tim Slusher and Vladimir Kladov.
}
{* Set of utility methods to work with files
and reqistry.
When programming KOL, which is Windows API-oriented, You should
avoid alien (for Windows) embedded Pascal files handling, and
use API-calls which implemented very well. This set of functions
is intended to make this easier.
Also TDirList object implementation present here and some registry
access functions, which allow to make code more elegant.
}
{$UNDEF ASM_LOCAL}
{$IFDEF ASM_VERSION}
{$DEFINE ASM_LOCAL}
{$ENDIF ASM_VERSION}
{$IFDEF ASM_VERSION}
function FileCreate(const FileName: string; OpenFlags: DWord): THandle;
asm
XOR ECX, ECX
PUSH ECX
MOV ECX, EDX
SHR ECX, 16
AND CX, $1FFF
JNZ @@1
MOV CL, FILE_ATTRIBUTE_NORMAL
@@1: PUSH ECX
MOV CL, DH
PUSH ECX // CreationMode
PUSH 0
MOV CL, DL
PUSH ECX // ShareMode
MOV DX, 0
PUSH EDX // AccessMode
//CALL System.@LStrToPChar // FileName must not be ''
PUSH EAX
CALL CreateFile
end;
{$ELSE ASM_VERSION} //Pascal
function FileCreate(const FileName: string; OpenFlags: DWord): THandle;
var Attr: DWORD;
begin
Attr := (OpenFlags shr 16) and $1FFF;
if Attr = 0 then Attr := FILE_ATTRIBUTE_NORMAL;
Result := CreateFile( PChar(FileName), OpenFlags and $F0000000,
OpenFlags and $F, nil, (OpenFlags shr 8) and $F,
Attr, 0 );
end;
{$ENDIF ASM_VERSION}
{$IFDEF ASM_VERSION}
function FileClose(Handle: THandle): Boolean;
asm
PUSH EAX
CALL CloseHandle
TEST EAX, EAX
SETNZ AL
end;
{$ELSE ASM_VERSION} //Pascal
function FileClose(Handle: THandle): boolean;
begin
Result := CloseHandle(Handle);
end;
{$ENDIF ASM_VERSION}
{$IFDEF ASM_VERSION}
function FileExists( const FileName : String ) : Boolean;
const size_TWin32FindData = sizeof( TWin32FindData );
asm
CALL EAX2PChar
PUSH EAX
CALL GetFileAttributes
INC EAX
JZ @@exit
DEC EAX
{$IFDEF PARANOIA}
DB $24, FILE_ATTRIBUTE_DIRECTORY
{$ELSE}
AND AL, FILE_ATTRIBUTE_DIRECTORY
{$ENDIF}
SETZ AL
@@exit:
end;
{$ELSE ASM_VERSION} //Pascal
function FileExists( const FileName : String ) : Boolean;
var
Code: Integer;
begin
Code := GetFileAttributes(PChar(FileName));
Result := (Code <> -1) and (FILE_ATTRIBUTE_DIRECTORY and Code = 0);
end;
{$ENDIF ASM_VERSION}
{$IFDEF ASM_VERSION}
function FileSeek(Handle: THandle; MoveTo: integer; MoveMethod: TMoveMethod): DWord;
asm
MOVZX ECX, CL
PUSH ECX
PUSH 0
PUSH EDX
PUSH EAX
CALL SetFilePointer
end;
{$ELSE ASM_VERSION} //Pascal
function FileSeek(Handle: THandle; MoveTo: integer; MoveMethod: TMoveMethod): DWord;
begin
Result := SetFilePointer(Handle, MoveTo, nil, Ord( MoveMethod ) );
end;
{$ENDIF ASM_VERSION}
{$IFDEF ASM_VERSION}
function FileRead(Handle: THandle; var Buffer; Count: DWord): DWord;
asm
PUSH EBP
PUSH 0
MOV EBP, ESP
PUSH 0
PUSH EBP
PUSH ECX
PUSH EDX
PUSH EAX
CALL ReadFile
TEST EAX, EAX
POP EAX
JNZ @@exit
XOR EAX, EAX
@@exit:
POP EBP
end;
{$ELSE ASM_VERSION} //Pascal
function FileRead(Handle: THandle; var Buffer; Count: DWord): DWord;
begin
if not ReadFile(Handle, Buffer, Count, Result, nil) then
Result := 0;
end;
{$ENDIF ASM_VERSION}
{$IFDEF ASM_VERSION}
function File2Str(Handle: THandle): String;
asm
PUSH EDX
TEST EAX, EAX
JZ @@exit // return ''
PUSH EBX
MOV EBX, EAX // EBX = Handle
XOR EDX, EDX
XOR ECX, ECX
INC ECX
CALL FileSeek
PUSH EAX // Pos
PUSH 0
PUSH EBX
CALL GetFileSize
POP EDX
SUB EAX, EDX // EAX = Size - Pos
JZ @@exitEBX
PUSH EAX
CALL System.@GetMem
XCHG EAX, EBX
MOV EDX, EBX
POP ECX
PUSH ECX
CALL FileRead
POP ECX
MOV EDX, EBX
POP EBX
POP EAX
PUSH EDX
{$IFDEF _D2}
CALL _LStrFromPCharLen
{$ELSE}
CALL System.@LStrFromPCharLen
{$ENDIF}
JMP @@freebuf
@@exitEBX:
POP EBX
@@exit:
XCHG EDX, EAX
POP EAX // @Result
PUSH EDX
CALL System.@LStrFromPChar
@@freebuf:
POP EAX
TEST EAX, EAX
JZ @@fin
CALL System.@FreeMem
@@fin:
end;
{$ELSE ASM_VERSION} //Pascal
function File2Str(Handle: THandle): String;
var Pos, Size: DWORD;
begin
Result := '';
if Handle = 0 then Exit;
Pos := FileSeek( Handle, 0, spCurrent );
Size := GetFileSize( Handle, nil );
SetString( Result, nil, Size - Pos + 1 );
FileRead( Handle, Result[ 1 ], Size - Pos );
Result[ Size - Pos + 1 ] := #0;
end;
{$ENDIF ASM_VERSION}
{$IFDEF ASM_VERSION}
function FileWrite(Handle: THandle; const Buffer; Count: DWord): DWord;
asm
PUSH EBP
PUSH EBP
MOV EBP, ESP
PUSH 0
PUSH EBP
PUSH ECX
PUSH EDX
PUSH EAX
CALL WriteFile
TEST EAX, EAX
POP EAX
JNZ @@exit
XOR EAX, EAX
@@exit:
POP EBP
end;
{$ELSE ASM_VERSION} //Pascal
function FileWrite(Handle: THandle; const Buffer; Count: DWord): DWord;
begin
if not WriteFile(Handle, Buffer, Count, Result, nil) then
Result := 0;
end;
{$ENDIF ASM_VERSION}
{$IFDEF ASM_VERSION}
function FileEOF( Handle: THandle ) : Boolean;
asm
PUSH EAX
PUSH 0
PUSH EAX
CALL GetFileSize
XCHG EAX, [ESP]
MOV CL, spCurrent
XOR EDX, EDX
CALL FileSeek
POP EDX
CMP EAX, EDX
SETGE AL
end;
{$ELSE ASM_VERSION} //Pascal
function FileEOF( Handle: THandle ) : Boolean;
var Siz, Pos : DWord;
begin
Siz := GetFileSize( Handle, nil );
Pos := FileSeek( Handle, 0, spCurrent );
Result := Pos >= Siz;
end;
{$ENDIF ASM_VERSION}
{$IFDEF ASM_noVERSION}
function FileFullPath( const FileName: String ) : String;
const
BkSlash: String = '\';
szTShFileInfo = sizeof( TShFileInfo );
asm
PUSH EBX
PUSH ESI
MOV EBX, EDX
PUSH EAX
XCHG EAX, EDX
CALL System.@LStrClr
POP EDX
PUSH 0
MOV EAX, ESP
CALL System.@LStrAsg
MOV ESI, ESP
@@loo: CMP dword ptr [ESI], 0
JZ @@fin
MOV EAX, ESI
MOV EDX, [BkSlash]
PUSH 0
MOV ECX, ESP
CALL Parse
CMP dword ptr [EBX], 0
JE @@1
MOV EAX, EBX
MOV EDX, [BkSlash]
CALL System.@LStrCat
JMP @@2
@@1:
POP EAX
PUSH EAX
CALL System.@LStrLen
CMP EAX, 2
JNE @@2
POP EAX
PUSH EAX
CMP byte ptr [EAX+1], ':'
JNE @@2
MOV EAX, EBX
POP EDX
PUSH EDX
CALL System.@LStrAsg
JMP @@3
@@2:
PUSH 0
MOV EAX, ESP
MOV EDX, [EBX]
CALL System.@LStrAsg
MOV EAX, ESP
MOV EDX, [ESP+4]
CALL System.@LStrCat
POP EAX
PUSH EAX
SUB ESP, szTShFileInfo
MOV EDX, ESP
PUSH SHGFI_DISPLAYNAME
PUSH szTShFileInfo
PUSH EDX
PUSH 0
PUSH EAX
CALL ShGetFileInfo
LEA EDX, [ESP].TShFileInfo.szDisplayName
CMP byte ptr [EDX], 0
JE @@clr_stk
LEA EAX, [ESP+szTShFileInfo+4]
CALL System.@LStrFromPChar
@@clr_stk:
ADD ESP, szTShFileInfo
CALL RemoveStr
POP EDX
PUSH EDX
MOV EAX, EBX
CALL System.@LStrCat
@@3: CALL RemoveStr
JMP @@loo
@@fin: CALL RemoveStr
POP ESI
POP EBX
end;
{$ELSE ASM_VERSION} //Pascal
function FileFullPath( const FileName: String ) : String;
var SFI: TShFileInfo;
Src, S: String;
begin
Result := '';
Src := FileName;
while Src <> '' do
begin
S := Parse( Src, '\' );
if Result <> '' then
Result := Result + '\';
if (Result = '') and (Length( S ) = 2) and (S[ 2 ] = ':') then
Result := S
else
begin
ShGetFileInfo( PChar( Result + S ), 0, SFI, Sizeof( SFI ),
SHGFI_DISPLAYNAME );
if SFI.szDisplayName[ 0 ] <> #0 then
S := SFI.szDisplayName;
Result := Result + S;
end;
end;
if ExtractFileExt( Result ) = '' then
// case when flag 'Hide extensions for registered file types' is set on
// in the Explorer:
Result := Result + ExtractFileExt( FileName );
end;
{$ENDIF ASM_VERSION}
function FileShortPath( const FileName: String ): String;
var Buf: array[ 0..MAX_PATH ] of Char;
begin
GetShortPathName( PChar( FileName ), Buf, Sizeof( Buf ) );
Result := Buf;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -