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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
Unit _File;
InterFace
Uses Crt;
Const
R_Only : Byte = 0;
W_Only : Byte = 1;
R_Write : Byte = 2;
Procedure Err_Msg(Msg : String; Var Err : Byte);
Function Send_Err(Var Err : Byte) : String;
Procedure Open_File(Var F : File; F_Name :String; Mode : String);
Procedure open_Txt_File(Var F : Text; F_Name :String; Mode : String);
Procedure Creat_File(Var F : File; F_Name : String);
Procedure Creat_Txt_File(Var F : Text; F_Name : String);
Function File_Exist (F_Name : String) : Boolean;
Procedure Delete_File(F_Name : String);
Procedure Rename_File(Old_Name,New_Name : String);
Function File_Size(F_Name : String) : LongInt;
Function File_Name(F_Name : String) : String;
Function File_Ext(F_Name : String) : String;
Procedure Change_File_Ext(F_Name,Ext : String);
Function File_Copy(Source,Target : String; S_Pos,T_Pos : LongInt) : Byte;
Function Txt_File_Copy(Source,Target : String; T_Pos : Byte) : Byte;
Function Crypt_File(I_File,O_File,Pwd : String) : Byte;
Implementation
Procedure Err_Msg(Msg : String; Var Err : Byte);
Begin
Sound(220);
Delay(200);
NoSound;
TextBackground(Red);
TextColor(Yellow);
Writeln('Panic ! '+Msg);
TextBackground(Black);
TextColor(White);
Halt(Err);
End;
Function Send_Err(Var Err : Byte) : String;
Begin
Case Err Of
0 : Send_Err := 'No Error';
2 : Send_Err := 'File not found';
3 : Send_Err := 'Path not found';
4 : Send_Err := 'Too many open files';
5 : Send_Err := 'File access denied';
12 : Send_Err := 'Invalid file access code';
15 : Send_Err := 'Invalid drive number';
16 : Send_Err := 'Cannot remove current directory';
17 : Send_Err := 'Cannot rename across drives';
18 : Send_Err := 'No more files';
100 : Send_Err := 'Disk read error';
101 : Send_Err := 'Disk write error';
102 : Send_Err := 'File not assigned';
103 : Send_Err := 'File not open';
104 : Send_Err := 'File not open for input';
105 : Send_Err := 'File not open for output';
106 : Send_Err := 'Invalid numeric format';
150 : Send_Err := 'Disk is write-protected';
152 : Send_Err := 'Drive not ready';
END;
End;
Procedure Open_File(Var F : File; F_Name :String; Mode : String);
Var
Err : Byte;
Msg : String;
Begin
Assign(F,F_Name);
If Mode = 'r' Then FileMode := R_Only;
If Mode = 'w' Then FileMode := W_Only;
If Mode = 'rw' Then FileMode := R_Write;
{$I-}
Reset(F,1);
{$I+}
Err := IOResult;
Msg := F_Name + ' : ' + Send_Err(Err);
If Err <> 0 Then Err_Msg(Msg,Err);
End;
Procedure open_Txt_File(Var F : Text; F_Name :String; Mode : String);
Var
Err : Byte;
Msg : String;
Begin
Assign(F,F_Name);
If Mode = 'r' Then FileMode := R_Only;
If Mode = 'w' Then FileMode := W_Only;
If Mode = 'rw' Then FileMode := R_Write;
{$I-}
Reset(F);
{$I+}
Err := IOResult;
Msg := F_Name + ' : ' + Send_Err(Err);
If Err <> 0 Then Err_Msg(Msg,Err);
End;
Procedure Creat_File(Var F : File; F_Name : String);
Var
Msg : String;
Err : Byte;
Begin
Assign(F,F_Name);
{$I-}
Rewrite(F,1);
{$I+}
Err := IOResult;
Msg := F_Name + ' : ' + Send_Err(Err);
If Err <> 0 Then Err_Msg(Msg,Err);
End;
Procedure Creat_Txt_File(Var F : Text; F_Name : String);
Var
Msg : String;
Err : Byte;
Begin
Assign(F,F_Name);
{$I-}
Rewrite(F);
{$I+}
Err := IOResult;
Msg := F_Name + ' : ' + Send_Err(Err);
If Err <> 0 Then Err_Msg(Msg,Err);
End;
Function File_Exist (F_Name : String) : Boolean;
Var
F : File;
Begin
Assign(F,F_Name);
{$I-}
Reset(F,1);
{$I+}
If IOResult <> 0 Then File_Exist := False
Else
Begin
File_Exist := True;
Close(F);
End;
End;
Procedure Delete_File(F_Name : String);
Var
F : File;
Begin
open_File(F,F_Name,'w');
Close(F);
Erase(F);
End;
Procedure Rename_File(Old_Name,New_Name : String);
Var
F : File;
Begin
Open_File(F,Old_Name,'r');
Close(F);
Rename(F,New_Name);
End;
Function File_Size(F_Name : String) : LongInt;
Var
F : File;
Begin
Open_File(F,F_Name,'r');
File_Size := Filesize(F);
Close(F);
End;
Function File_Name(F_Name : String) : String;
Var
n : Byte;
Begin
n := Pos('.',F_Name);
File_Name := Copy(F_Name,1,n-1);
End;
Function File_Ext(F_Name : String) : String;
Var
n : Byte;
Begin
n := Pos('.',F_Name);
File_Ext := Copy(F_Name,n+1,length(F_Name)-n);
End;
Procedure Change_File_Ext(F_Name,Ext : String);
Var
S : String;
Begin
S := File_Name(F_Name) + '.' + Ext;
Rename_File(F_Name,S);
End;
Function File_Copy(Source,Target : String; S_Pos,T_Pos : LongInt) : Byte;
{Returned Value :
0 : Successful
1 : Source and target are the same
2 : Error During Copy
}
Const
Buff_Max = 65528;
Type BuffType = Array[1..Buff_Max] of Byte;
Var Source_F,
Target_F : File;
Buffer : ^BuffType;
BuffSize : Longint;
NumRead,
NumWrite : Word;
Begin
If Source = Target Then
Begin
File_Copy := 1;
Exit;
End;
Open_File(Source_F,Source,'r');
If Not File_Exist(Target) Then Creat_File(Target_F,Target);
Open_File(Target_F,Target,'w');
BuffSize := MaxAvail;
If BuffSize > Buff_Max Then BuffSize := MaxAvail;
GetMem(Buffer,BuffSize);
Seek(Source_F,S_Pos);
Seek(Target_F,T_Pos);
While Not EOF(Source_F) Do
Begin
BlockRead(Source_F,Buffer^,BuffSize,Numread);
Blockwrite(Target_f,Buffer^,Numread,NumWrite);
End;
If NumRead <> NumWrite Then
Begin
File_Copy := 2;
Exit;
End
Else
File_Copy := 0;
End;
Function Txt_File_Copy(Source,Target : String; T_Pos : Byte) : Byte;
{Returned Value :
0 : Successful
1 : Source and target are the same
}
Var Source_F,
Target_F : Text;
S : String;
Begin
If Source = Target Then
Begin
Txt_File_Copy := 1;
Exit;
End;
Open_Txt_File(Source_F,Source,'r');
If Not File_Exist(Target) Then Creat_Txt_File(Target_F,Target);
Open_Txt_File(Target_F,Target,'w');
If T_Pos = 0 Then Append(Target_F);
While Not EOF(Source_F) Do
Begin
Readln(Source_F,S);
Writeln(Target_F,S);
End;
Txt_File_Copy := 0;
End;
Function Crypt_File(I_File,O_File,Pwd : String) : Byte;
Var
I,
B : Byte;
In_File,
Out_File : File;
Begin
If I_File = O_File Then
Begin
Crypt_File := 1;
Exit;
End;
Open_File(In_File,I_File,'r');
Creat_File(Out_File,O_File);
I := 0;
While Not EOF(In_File) Do
Begin
BlockRead(In_File,B,1);
B := B Xor Ord(Pwd[i]);
If I < Length(Pwd) Then Inc(I)
Else I := 1;
BlockWrite(Out_File,B,1);
End;
Close(In_File);
Close(Out_File);
End;
BEGIN
End. |
Partager