summaryrefslogtreecommitdiff
path: root/plugins/Utils.pas/io.pas
blob: 9a587c660fb2096b301857f9f2a98849cb4df272 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
unit io;

interface
uses windows;

function Reset  (fname:PWideChar):THANDLE; overload;
function Reset  (fname:PAnsiChar):THANDLE; overload;
function ReWrite(fname:PWideChar):THANDLE; overload;
function ReWrite(fname:PAnsiChar):THANDLE; overload;
function Append (fname:PWideChar):THANDLE; overload;
function Append (fname:PAnsiChar):THANDLE; overload;

function GetFSize(name:PWideChar):dword; overload;
function GetFSize(name:PAnsiChar):dword; overload;
function FileExists(fname:PAnsiChar):Boolean; overload;
function FileExists(fname:PWideChar):Boolean; overload;

function Skip(f:THANDLE;count:integer):integer;
function Seek(f:THANDLE;pos:integer):integer;
function FilePos(f:THANDLE):dword;
function FileSize(f:THANDLE):dword;
function Eof(f:THANDLE):boolean;

function BlockRead (f:THANDLE;var buf;size:integer):dword;
function BlockWrite(f:THANDLE;var buf;size:integer):dword;

function ForceDirectories(path:PAnsiChar):boolean; overload;
function ForceDirectories(path:PWideChar):boolean; overload;
function DirectoryExists(Directory:PAnsiChar):Boolean; overload;
function DirectoryExists(Directory:PWideChar):Boolean; overload;

implementation

function Reset(fname:PWideChar):THANDLE;
begin
  result:=CreateFileW(fname,GENERIC_READ,FILE_SHARE_READ+FILE_SHARE_WRITE,nil,OPEN_EXISTING,0,0);
end;

function Reset(fname:PAnsiChar):THANDLE;
begin
  result:=CreateFileA(fname,GENERIC_READ,FILE_SHARE_READ+FILE_SHARE_WRITE,nil,OPEN_EXISTING,0,0);
end;

function Append(fname:PWideChar):THANDLE;
begin
  result:=CreateFileW(fname,GENERIC_WRITE,FILE_SHARE_READ,nil,OPEN_ALWAYS,0,0);
  SetFilePointer(result,0,nil,FILE_END);
end;

function Append(fname:PAnsiChar):THANDLE;
begin
  result:=CreateFileA(fname,GENERIC_WRITE,FILE_SHARE_READ,nil,OPEN_ALWAYS,0,0);
  SetFilePointer(result,0,nil,FILE_END);
end;

function ReWrite(fname:PWideChar):THANDLE; overload;
begin
  result:=CreateFileW(fname,GENERIC_WRITE,FILE_SHARE_READ,nil,CREATE_ALWAYS,0,0);
end;

function ReWrite(fname:PAnsiChar):THANDLE; overload;
begin
  result:=CreateFileA(fname,GENERIC_WRITE,FILE_SHARE_READ,nil,CREATE_ALWAYS,0,0);
end;

function Skip(f:THANDLE;count:integer):integer;
begin
  result:=SetFilePointer(f,count,nil,FILE_CURRENT);
end;

function Eof(f:THANDLE):boolean;
begin
  result:=FilePos(f)>=FileSize(f);
end;

function Seek(f:THANDLE;pos:integer):integer;
begin
  result:=SetFilePointer(f,pos,nil,FILE_BEGIN);
end;

function FilePos(f:THANDLE):dword;
begin
  result:=SetFilePointer(f,0,nil,FILE_CURRENT);
end;

function FileSize(f:THANDLE):dword;
begin
  result:=GetFileSize(f,nil);
end;

function BlockRead(f:THANDLE;var buf;size:integer):dword;
begin
  ReadFile(f,buf,size,result,nil);
end;

function BlockWrite(f:THANDLE;var buf;size:integer):dword;
begin
  WriteFile(f,buf,size,result,nil);
end;

function GetFSize(name:PWideChar):dword;
var
  lRec:WIN32_FIND_DATAW;
  h:THANDLE;
begin
  h:=FindFirstFileW(name,lRec);
  if h=THANDLE(INVALID_HANDLE_VALUE) then
    result:=0
  else
  begin
    result:=lRec.nFileSizeLow;
    FindClose(h);
  end;
end;

function GetFSize(name:PAnsiChar):dword;
var
  lRec:TWin32FindDataA;
  h:THANDLE;
begin
  h:=FindFirstFileA(name,lRec);
  if h=THANDLE(INVALID_HANDLE_VALUE) then
    result:=0
  else
  begin
    result:=lRec.nFileSizeLow;
    FindClose(h);
  end;
end;

function ForceDirectories(path:PAnsiChar):boolean;
var
  p,pc:PAnsiChar;
  i:cardinal;
  c:AnsiChar;
begin
  result:=true;
  if DirectoryExists(path) then exit;
  if (path<>nil) and (path^<>#0) then
  begin
    i:=lstrlena(path)+1;
    GetMem(pc,i);
    move(path^,pc^,i);
    p:=pc;
    if (p^ in ['A'..'Z','a'..'z']) and (p[1]=':') then inc(p,2);
    if p^ in ['/','\'] then inc(p);
    c:=#0;
    while p^<>#0 do
    begin
      c:=' ';
      if (p^ in ['/','\']) and (p[1]<>#0) then
      begin
        c:=p^;
        p^:=#0;
        if not CreateDirectoryA(pc,nil) then
        begin
          if GetLastError<>ERROR_ALREADY_EXISTS then
          begin
            result:=false;
            FreeMem(pc);
            exit;
          end;
        end;
        p^:=c;
      end;
      inc(p);
    end;
    if (c<>#0) and (c=' ') then
      if not CreateDirectoryA(pc,nil) then
        result:=false;
    FreeMem(pc);
  end;
end;

function ForceDirectories(path:PWideChar):boolean;
var
  p,pc:PWideChar;
  i:cardinal;
  c:WideChar;
begin
  result:=true;
  if DirectoryExists(path) then exit;
  if (path<>nil) and (path^<>#0) then
  begin
    i:=(lstrlenw(path)+1)*SizeOf(WideChar);
    GetMem(pc,i);
    move(path^,pc^,i);
    p:=pc;
    if (((p^>='A') and (p^<='Z')) or ((p^>='a') and (p^<='z'))) and (p[1]=':') then inc(p,2);
    if (p^='/') or (p^='\') then inc(p);
    c:=#0;
    while p^<>#0 do
    begin
      c:=' ';
      if ((p^='/') or (p^='\')) and (p[1]<>#0) then
      begin
        c:=p^;
        p^:=#0;
        if not CreateDirectoryW(pc,nil) then
          if GetLastError<>ERROR_ALREADY_EXISTS then
          begin
            result:=false;
            FreeMem(pc);
            exit;
          end;
        p^:=c;
      end;
      inc(p);
    end;
    if (c<>#0) and (c=' ') then
      if not CreateDirectoryW(pc,nil) then
        result:=false;
    FreeMem(pc);
  end;
end;

function DirectoryExists(Directory:PAnsiChar):Boolean;
var
  Code: Integer;
begin
  Code := GetFileAttributesA(Directory);
  Result := (Code<>-1) and ((Code and FILE_ATTRIBUTE_DIRECTORY)<>0);
end;

function DirectoryExists(Directory:PWideChar):Boolean;
var
  Code: Integer;
begin
  Code := GetFileAttributesW(Directory);
  Result := (Code<>-1) and ((Code and FILE_ATTRIBUTE_DIRECTORY)<>0);
end;

function FileExists(fname:PAnsiChar):Boolean;
var
  Code: Integer;
begin
  Code := GetFileAttributesA(fname);
  Result := (Code<>-1) and ((Code and FILE_ATTRIBUTE_DIRECTORY)=0);
end;

function FileExists(fname:PWideChar):Boolean;
var
  Code: Integer;
begin
  Code := GetFileAttributesW(fname);
  Result := (Code<>-1) and ((Code and FILE_ATTRIBUTE_DIRECTORY)=0);
end;

end.