Subversion Repositories autosfx

Rev

Rev 2 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 daniel-mar 1
unit Functions;
2
 
3
interface
4
 
5
uses
3 daniel-mar 6
  Forms, Windows, Classes, SysUtils, ShellAPI,
7
  ZipMstr19, ZmUtils19, ShlObj, ActiveX;
1 daniel-mar 8
 
9
type
10
  TLineBreak = (lbWindows, lbLinux, lbMac);
11
 
12
function RawFileCopy(ASrc, ADst: string): boolean;
13
function ShellExecuteAndWait(FileName: string; Params: string): boolean;
14
function FileSize(AFilename: string): int64;
15
function LooksLikeDir(s: string): boolean;
16
function GetTempDirectory: String;
17
function NormalizeLineBreaks(s: string; mode: TLineBreak): string;
18
function ExtractFileNameWithoutExt(const fil: string): string;
2 daniel-mar 19
function SearchNextFreeName(s: string; wantDir: boolean): string;
20
function GetSpecialFolderPath(const Folder: integer): string;
21
function IsExtractable(AFilename: string): boolean;
1 daniel-mar 22
 
23
implementation
24
 
25
{$IFNDEF UNICODE}
26
type
27
  TCharSet = set of AnsiChar;
28
{$ENDIF}
29
 
30
{$IFNDEF UNICODE}
31
function CharInSet(C: AnsiChar; const CharSet: TCharSet): Boolean;// overload;
32
begin
33
  Result := c in CharSet;
34
end;
35
{$ENDIF}
36
 
37
function LooksLikeDir(s: string): boolean;
38
begin
39
  result := CharInSet(s[Length(s)], ['/', '\']);
40
end;
41
 
42
function RawFileCopy(ASrc, ADst: string): boolean;
43
var
44
  SSrc, SDst: TFileStream;
45
begin
46
  DeleteFile(PChar(ADst));
47
 
48
  SSrc := TFileStream.Create(ASrc, fmOpenRead);
49
  try
50
    SDst := TFileStream.Create(ADst, fmCreate);
51
    try
52
      SDst.CopyFrom(SSrc, SSrc.Size);
53
    finally
54
      SDst.Free;
55
    end;
56
  finally
57
    SSrc.Free;
58
  end;
59
 
60
  result := true;
61
end;
62
 
63
// http://www.delphipraxis.net/32234-shellexecuteandwait-status-abfragen.html
64
// Modified. Now CONSOLE compatible and return false if ErrorLevel <> 0
65
function ShellExecuteAndWait(FileName: string; Params: string): boolean;
66
var
67
  exInfo: TShellExecuteInfo;
68
  Ph: DWORD;
69
  lExitCode: DWord;
70
begin
71
  Try
72
    FillChar(exInfo, SizeOf(exInfo), 0);
73
    with exInfo do
74
    begin
75
      cbSize := SizeOf(exInfo);
76
      fMask := SEE_MASK_NOCLOSEPROCESS or SEE_MASK_FLAG_DDEWAIT;
77
      Wnd := GetActiveWindow();
78
      FileName := ExpandUNCFileName(FileName);
79
      ExInfo.lpVerb := 'open';
80
      ExInfo.lpParameters := PChar(Params);
81
//      ExInfo.lpDirectory := PChar(ExtractFilePath(FileName));
82
      lpFile := PChar(FileName);
83
      nShow := SW_SHOWNORMAL;
84
    end;
85
    if ShellExecuteEx(@exInfo) then
86
    begin
87
      Ph := exInfo.HProcess;
88
    end
89
    else
90
    begin
91
      WriteLn(SysErrorMessage(GetLastError));
92
      Result := False;
93
      Exit;
94
    end;
95
    while WaitForSingleObject(ExInfo.hProcess, 50) <> WAIT_OBJECT_0 do ;
96
    (* begin
97
      Application.ProcessMessages;
98
    end; *)
99
    GetExitCodeProcess(Ph, lExitCode);
100
    Result := lExitCode = 0;
101
    CloseHandle(Ph);
102
  Except
103
    Result := False;
104
    Exit;
105
  End;
106
end;
107
 
108
function FileSize(AFilename: string): int64;
109
var
110
  s: TFileStream;
111
begin
112
  s := TFileStream.Create(AFilename, fmOpenRead or fmShareDenyWrite);
113
  try
114
    result := s.Size;
115
  finally
116
    s.Free;
117
  end;
118
end;
119
 
120
// http://www.cryer.co.uk/brian/delphi/howto_get_temp.htm
121
function GetTempDirectory: String;
122
var
123
  tempFolder: array[0..MAX_PATH] of Char;
124
begin
125
  GetTempPath(MAX_PATH, @tempFolder);
126
  result := StrPas(tempFolder);
127
end;
128
 
129
function NormalizeLineBreaks(s: string; mode: TLineBreak): string;
130
begin
131
  if mode = lbWindows then
132
  begin
133
    s := StringReplace(s, #13#10, #10, [rfReplaceAll]);
134
    s := StringReplace(s, #13,    #10, [rfReplaceAll]);
135
    s := StringReplace(s, #10, #13#10, [rfReplaceAll]);
136
  end
137
  else if mode = lbLinux then
138
  begin
139
    s := StringReplace(s, #13#10, #13, [rfReplaceAll]);
140
    s := StringReplace(s, #10,    #13, [rfReplaceAll]);
141
  end
142
  else if mode = lbMac then
143
  begin
144
    s := StringReplace(s, #13#10, #10, [rfReplaceAll]);
145
    s := StringReplace(s, #13,    #10, [rfReplaceAll]);
146
  end;
147
  result := s;
148
end;
149
 
150
// http://www.viathinksoft.de/?page=codelib&showid=70
151
function ExtractFileNameWithoutExt(const fil: string): string;
152
begin
153
  result := Copy(ExtractFileName(fil), 1, Length(ExtractFileName(fil))-Length(ExtractFileExt(fil)));
154
end;
155
 
2 daniel-mar 156
function SearchNextFreeName(s: string; wantDir: boolean): string;
1 daniel-mar 157
var
158
  i: integer;
159
begin
160
  if not FileExists(s) and not DirectoryExists(s) then
161
  begin
162
    result := s;
2 daniel-mar 163
    if wantDir then result := IncludeTrailingPathDelimiter(result);
1 daniel-mar 164
    Exit;
165
  end;
166
 
167
  i := 2;
168
 
2 daniel-mar 169
  if wantDir then
1 daniel-mar 170
  begin
2 daniel-mar 171
    s := ExcludeTrailingPathDelimiter(s);
1 daniel-mar 172
    repeat
2 daniel-mar 173
      result := Format('%s (%d)', [s, i]);
1 daniel-mar 174
      inc(i);
2 daniel-mar 175
    until not DirectoryExists(result) and not FileExists(result);
176
    result := IncludeTrailingPathDelimiter(result);
1 daniel-mar 177
  end
2 daniel-mar 178
  else
1 daniel-mar 179
  begin
180
    repeat
2 daniel-mar 181
      result := Format('%s (%d)%s', [ExtractFilePath(s)+ExtractFileNameWithoutExt(s), i, ExtractFileExt(s)]);
1 daniel-mar 182
      inc(i);
2 daniel-mar 183
    until not DirectoryExists(result) and not FileExists(result);
1 daniel-mar 184
  end;
185
end;
186
 
3 daniel-mar 187
// GetSpecialFolderPath
188
// Ref: http://www.wer-weiss-was.de/theme159/article1058561.html
2 daniel-mar 189
function GetSpecialFolderPath(const Folder: integer): string;
190
var
191
  PIDL: PItemIDList;
192
  Path: array[0..MAX_PATH] of char;
193
  Malloc: IMalloc;
194
begin
195
  Path := '';
196
  if Succeeded((SHGetSpecialFolderLocation(0, Folder, PIDL))) then
197
    if (SHGetPathFromIDList(PIDL, Path)) then
198
      if Succeeded(ShGetMalloc(Malloc)) then
199
      begin
200
        Malloc.Free(PIDL);
201
        Malloc := nil;
202
      end;
203
  Result := Path;
204
end;
205
 
206
function IsExtractable(AFilename: string): boolean;
207
var
208
  q: integer;
209
  uz: TZipMaster19;
210
begin
211
  // TODO: Ist die Funktion gut? Fraglich, ob EOC64 ein Teil von EOC ist.
212
  uz := TZipMaster19.Create(nil);
213
  try
214
    q := uz.QueryZip(AFilename);
215
    result := true;
216
    if (q and zqbHasLocal) <> zqbHasLocal then result := false;
217
    if (q and zqbHasCentral) <> zqbHasCentral then result := false;
218
    if ((q and zqbHasEOC) <> zqbHasEOC) and
3 daniel-mar 219
       ((q and zqbHasEOC64) <> zqbHasEOC64) then result := false;
2 daniel-mar 220
  finally
221
    uz.Free;
222
  end;
223
end;
224
 
1 daniel-mar 225
end.