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