home *** CD-ROM | disk | FTP | other *** search
/ Beijing Paradise BBS Backup / PARADISE.ISO / software / BBSDOORW / RADOOR30.ZIP / FILEOBJ.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1994-04-10  |  9.9 KB  |  368 lines

  1. {$I-}
  2. {╔═════════════════════════════════════════════════════════════════════════╗
  3.  ║                                                                         ║
  4.  ║                   (c) CopyRight LiveSystems 1990, 1994                  ║
  5.  ║                                                                         ║
  6.  ║ Author    : Gerhard Hoogterp                                            ║
  7.  ║ FidoNet   : 2:282/100.5   2:283/7.33                                    ║
  8.  ║ BitNet    : GERHARD@LOIPON.WLINK.NL                                     ║
  9.  ║                                                                         ║
  10.  ║ SnailMail : Kremersmaten 108                                            ║
  11.  ║             7511 LC Enschede                                            ║
  12.  ║             The Netherlands                                             ║
  13.  ║                                                                         ║
  14.  ║        This module is part of the RADoor BBS doorwriters toolbox.       ║
  15.  ║                                                                         ║
  16.  ╚═════════════════════════════════════════════════════════════════════════╝}
  17. {---------------------------------------------------------------------------|
  18.  
  19. Description:
  20.  
  21.    This unit has everything to handle Lowlevel access to typed files. It's
  22.    main purpose is to provide filesharing and easy buffered access for
  23.    record and untyped files. Recordlocking procedure are also included.
  24.  
  25.    Note that the ReadOnly constate redefines the ReadOnly Filemode constate
  26.    of the standard DOS unit!
  27.  
  28. |--------------------------------------------------------------------------}
  29.  
  30. Unit FileObj;
  31. Interface
  32. Uses Dos;
  33.  
  34. {---------------------------------------------------------------------------|
  35.   The filesharing constates. For RA you should open ALL the system files
  36.   with the ShareDenyNone bits set.
  37. |--------------------------------------------------------------------------}
  38.  
  39. Const ReadOnly           = $00;
  40.       WriteOnly          = $01;
  41.       ReadWrite          = $02;
  42.  
  43.       ShareCompatible    = $00;
  44.       ShareDenyReadWrite = $10;
  45.       ShareDenyWrite     = $20;
  46.       ShareDenyRead      = $30;
  47.       ShareDenyNone      = $40;
  48.  
  49.       Inheritance        = $80;
  50.  
  51. {---------------------------------------------------------------------------|
  52.   UseRecLocking  This boolean determines if Locking is applied when you
  53.                  call the Lock and UnLock procedures. It's an easy way
  54.                  to turn recordlocking on/off
  55.  
  56.   MaxOpenRetries is the number of retries the OPEN procedure uses when it's
  57.                  trying to open a file. After MaxOpenRetries it aborts
  58.                  with an errorlevel set.
  59.  
  60. |--------------------------------------------------------------------------}
  61.  
  62.       UseRecLocking      : Boolean = False;
  63.       MaxOpenRetries     : Byte    = 3;      { the number of tries to open a }
  64.                                              { file.                         }
  65.  
  66.  
  67. {---------------------------------------------------------------------------|
  68.   The FileObject.
  69.  
  70.   See the documentation for a description of all the fields and methodes.
  71. |--------------------------------------------------------------------------}
  72.  
  73.  
  74.  
  75. Type FileObject = Object
  76.                     F      : File;      { File handle   }
  77.                     Error  : Integer;   { Last IOerror  }
  78.                     Size   : Word;      { Record Size   }
  79.                     Mode   : Byte;      { Curr FileMode }
  80.                     IsOpen : Boolean;   { File open ?   }
  81.  
  82.                     { Open the file with the name NAME  }
  83.                     { The recordsize RECSIZE and the    }
  84.                     { filemode USEMODE                  }
  85.  
  86.                     Procedure Open( Name    : ComStr;
  87.                                     RecSize : Word;
  88.                                     UseMode : Byte);
  89.  
  90.                     { Create a new file                }
  91.  
  92.                     Procedure Create(Name : ComStr);
  93.  
  94.                     { Reset the filemode to USEMODE     }
  95.  
  96.                     Procedure ResetMode(UseMode : Byte);
  97.  
  98.                     { Read the next record              }
  99.  
  100.                     Procedure Read(Var Rec);
  101.                     Procedure ReadBuffer(Var Rec;Var Amount : Word);
  102.  
  103.                     { Append a record to the file       }
  104.  
  105.                     Procedure Append(Var Rec);
  106.  
  107.                     { Replace a record in the file      }
  108.  
  109.                     Procedure Replace(Var Rec;RecNo : LongInt);
  110.                     Procedure WriteBuffer(Var Rec; Amount : Word);
  111.  
  112.                     { Jump to a record in the file      }
  113.  
  114.                     Procedure GotoRecord(RecNo : LongInt);
  115.  
  116.                     { Close the file                    }
  117.  
  118.                     Procedure Close;
  119.  
  120.                     { Lock or unlock record RECNO       }
  121.  
  122.                     Procedure Lock(RecNo : LongInt);
  123.                     Procedure UnLock(RecNo : LongInt);
  124.  
  125.                     Function EOF:Boolean;      { True if EOF             }
  126.                     Function RecordNr:LongInt; { The CURRENT Rec. Nr     }
  127.                     Function IoError:Integer;  { The last ERROR          }
  128.                     Function NoRecords:LongInt;{ Total records in file   }
  129.  
  130.                     Procedure Erase;           { Erase the file          }
  131.                                                { Rename the file to NAME }
  132.                     Procedure Rename(Name : ComStr);
  133.                     Procedure Skip(B : LongInt);
  134.                   End; { Object }
  135.  
  136. Function DetectShare:Boolean;
  137.  
  138. Implementation
  139.  
  140. {---------------------------------------------------------------------------|
  141.   CompletePath  Complete's the path to a full featured dos path. Including
  142.                 the Drive, the full path and a trailing backslash.
  143. |--------------------------------------------------------------------------}
  144.  
  145. Procedure CompletePath(Var Path : ComStr);
  146. Begin
  147. Path:=FExpand(Path);
  148. If (Path[Length(Path)]<>'\') And
  149.    (Path[Length(Path)]<>':')
  150.    Then Path:=Path+'\';
  151. End;
  152.  
  153. {---------------------------------------------------------------------------|
  154.   The FileObject methodes
  155. |--------------------------------------------------------------------------}
  156.  
  157. Procedure FileObject.Open( Name     : ComStr;
  158.                            RecSize  : Word;
  159.                            UseMode  : Byte);
  160. Var Tries : Byte;
  161. Begin
  162. Mode:=UseMode;
  163. FileMode:=Mode;
  164. Size:=RecSize;
  165. Tries:=0;
  166. Repeat
  167.   Inc(Tries);
  168.   Error:=0;
  169.   Assign(F,Name);
  170.   Reset(F,RecSize);
  171.   Error:=IoResult;
  172. Until (Tries>MaxOpenRetries) Or (Error=0);
  173. IsOpen:=Error=0;
  174. End;
  175.  
  176. Procedure FileObject.Create(Name : ComStr);
  177. Begin
  178. FileMode:=WriteOnly+ShareDenyReadWrite;
  179. Assign(F,Name);
  180. Rewrite(F);
  181. System.Close(F);
  182. Error:=IoResult;
  183. End;
  184.  
  185. Procedure FileObject.Close;
  186. Begin
  187. If IsOpen
  188.    Then Begin
  189.         System.Close(F);
  190.         Error:=IoResult;
  191.         IsOpen:=False;
  192.         End;
  193. End;
  194.  
  195. Procedure FileObject.ResetMode(UseMode : Byte);
  196. Var OldPtr : LongInt;
  197.     Tries  : Byte;
  198. Begin
  199. OldPtr:=0;
  200. If IsOpen
  201.    Then Begin
  202.         OldPtr:=FilePos(F);
  203.         Close;
  204.         IsOpen:=False;
  205.         End;
  206.  
  207. Mode:=UseMode;
  208. FileMode:=Mode;
  209. Tries:=0;
  210. Repeat
  211.  Inc(Tries);
  212.  Error:=0;
  213.  Reset(F,Size);
  214.  Error:=IoResult;
  215. Until (Tries>MaxOpenRetries) Or (Error=0);
  216. Seek(F,OldPtr);
  217. Error:=IoResult;
  218. End;
  219.  
  220. Procedure FileObject.Read(Var Rec);
  221. Begin
  222. BlockRead(F,Rec,1);
  223. Error:=IoResult;
  224. End;
  225.  
  226. Procedure FileObject.ReadBuffer(Var Rec;Var Amount : Word);
  227. Begin
  228. BlockRead(F,Rec,Amount,Amount);
  229. Error:=IoResult;
  230. End;
  231.  
  232. Procedure FileObject.WriteBuffer(Var Rec;Amount : Word);
  233. Begin
  234. BlockWrite(F,Rec,Amount,Amount);
  235. Error:=IoResult;
  236. End;
  237.  
  238. Procedure FileObject.Append(Var Rec);
  239. Begin
  240. Seek(F,FileSize(F));
  241. BlockWrite(F,Rec,1);
  242. Error:=IoResult;
  243. End;
  244.  
  245. Procedure FileObject.Replace(Var Rec;RecNo : LongInt);
  246. Begin
  247. Seek(F,RecNo);
  248. BlockWrite(F,Rec,1);
  249. Error:=IoResult;
  250. End;
  251.  
  252. Procedure FileObject.GotoRecord(RecNo : LongInt);
  253. Begin
  254. Seek(F,RecNo);
  255. Error:=IoResult;
  256. End;
  257.  
  258. Function FileObject.RecordNr:LongInt;
  259. Begin
  260. RecordNr:=FilePos(F);
  261. End;
  262.  
  263. Function FileObject.EOF:Boolean;
  264. Begin
  265. EOF:=System.Eof(F);
  266. End;
  267.  
  268. Function FileObject.IoError:Integer;
  269. Begin
  270. IoError:=Error;
  271. Error:=0;
  272. End;
  273.  
  274. Function FileObject.NoRecords:LongInt;
  275. Begin
  276. NoRecords:=FileSize(F);
  277. End;
  278.  
  279. Procedure FileObject.Skip(B : LongInt);
  280. Begin
  281. Seek(F,FilePos(F)+B);
  282. End;
  283.  
  284. Procedure FileObject.Lock(RecNo : LongInt);
  285. Var Regs : Registers;
  286.     Start: LongInt;
  287. Begin
  288. If Not UseRecLocking
  289.    Then Exit;
  290. Dec(RecNo);
  291. Start:=RecNo*FileRec(F).RecSize;
  292. With Regs Do
  293.  Begin
  294.  AH:=$5C;
  295.  AL:=$00;
  296.  BX:=FileRec(F).Handle;
  297.  CX:=(Start And $FFFF0000) Shr 16;
  298.  DX:=(Start And $0000FFFF);
  299.  SI:=0;
  300.  DI:=FileRec(F).RecSize;
  301.  End;
  302. MSDos(Regs);
  303. If (Regs.Flags And FCarry)=FCarry
  304.    Then Error:=Regs.AX+200;
  305. End;
  306.  
  307. Procedure FileObject.UnLock(RecNo : LongInt);
  308. Var Regs : Registers;
  309.     Start: LongInt;
  310. Begin
  311. If Not UseRecLocking
  312.    Then Exit;
  313. Dec(RecNo);
  314. Start:=RecNo*FileRec(F).RecSize;
  315. With Regs Do
  316.  Begin
  317.  AH:=$5C;
  318.  AL:=$01;
  319.  BX:=FileRec(F).Handle;
  320.  CX:=(Start And $FFFF0000) Shr 16;
  321.  DX:=(Start And $0000FFFF);
  322.  SI:=0;
  323.  DI:=FileRec(F).RecSize;
  324.  End;
  325. MSDos(Regs);
  326. If (Regs.Flags And FCarry)=FCarry
  327.    Then Error:=Regs.AX+200;
  328. End;
  329.  
  330. Function DetectShare:Boolean;
  331. Var Regs  : Registers;
  332. Begin
  333. With Regs Do
  334.  Begin
  335.  AH:=$5C;
  336.  AL:=$01;
  337.  MSDOS(Regs);
  338.  End;
  339. DetectShare:= ((Regs.Flags And FCarry)=FCarry) And
  340.               (Regs.AX<>$01);
  341. End;
  342.  
  343. Procedure FileObject.Erase;
  344. Begin
  345. If IsOpen
  346.    Then Begin
  347.         IsOpen:=False;
  348.         System.Close(F);
  349.         End;
  350. System.Erase(F);
  351. Error:=IoResult;
  352. End;
  353.  
  354. Procedure FileObject.Rename(Name : ComStr);
  355. Begin
  356. If IsOpen
  357.    Then Begin
  358.         IsOpen:=False;
  359.         System.Close(F);
  360.         End;
  361. System.Rename(F,Name);
  362. FileMode:=Mode;
  363. System.Reset(F,Size);
  364. Error:=IoResult;
  365. End;
  366.  
  367. End.
  368.