home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 15 / CDACTUAL15.iso / cdactual / program / pascal / PASLIBR.ZIP / CH5_1.DOC < prev    next >
Encoding:
Text File  |  1991-03-15  |  18.9 KB  |  775 lines

  1. ..pgno01
  2. ..foot60A5-##
  3. ..head02L──────────────────────────────────────────────────────────────────────
  4. ..head04L──────────────────────────────────────────────────────────────────────
  5. ..head03LGlobal Declarations
  6.  
  7. Type
  8.    TFindRec = Record
  9.                  Attr : Word;
  10.                  Time : Word;
  11.                  Date : Word;
  12.                  Size : LongInt;
  13.                  Name : String;
  14.               End;
  15.  
  16.  
  17.  
  18. Var
  19.    DosErrNo   : Integer  the error code for function result errors.
  20.  
  21.    CErrCode   : Integer  the error code for critical errors.
  22.  
  23.    CErrType   : Integer  contains the type of device for the critical
  24.                          error.
  25.                             0 = Block device
  26.                             1 = Character device
  27.  
  28.    CErrDrive  : Integer  contains the number of the disk drive where
  29.                          the critical error occurred.  If the critical
  30.                          error is not a disk drive the value will then
  31.                          remain as -1.
  32.  
  33.    CErrDevice : String   contains the name of the device in error.
  34. ..page
  35. ..head03ACloseFile
  36. ■ Description
  37.  
  38.   Closes a file and flushes all file buffers for a file.
  39.  
  40. ■ Summary
  41.  
  42.    Procedure CloseFile( Handle : Integer );
  43.  
  44.    Handle     the handle number of a file that was opened using the
  45.               OpenFile operation.
  46.  
  47.  
  48. ■ Remarks
  49.  
  50.   If an error occurred DosErrNo will contain one of the following
  51.   error codes.
  52.  
  53.      DosErrNo     Description
  54.      ──────── ───────────────────
  55.         6     Invalid file handle
  56.  
  57.  
  58. ■ Example
  59.  
  60.   Program Example;
  61.   Uses FPDisk;
  62.   Var
  63.      Handle : Integer;
  64.   Begin
  65.      OpenFile( 'Temp.Fil', READ_ONLY, Handle );
  66.              .
  67.              .
  68.              .
  69.      CloseFile( Handle );
  70.   End.
  71.  
  72.   This example will close the file that is associate with the handle
  73.   number returned in the OpenFile function.  If the close operation
  74.   was successful DosErrNo will contain a zero, provided the variable
  75.   was reset to zero after the last I/O error.  If an error occurred
  76.   DosErrNo should contain the value 6.
  77. ..page
  78. ..head03ACreateFile
  79. ■ Description
  80.  
  81.   Opens an existing file or creates a new file if it does not exist.
  82.  
  83.  
  84. ■ Summary
  85.  
  86.   Procedure CreateFile(    Path   : String;
  87.                            Attr   : Integer;
  88.                        Var Handle : Integer );
  89.  
  90.   Path        the path and filename of the file to be opened or
  91.               created.
  92.  
  93.   Attr        mode to set the file's attribute to.
  94.  
  95.   Handle      returns an integer that this file is assoicated with for
  96.               access.
  97.  
  98.  
  99. ■ Remarks
  100.  
  101.   This operation will open a new or existing file for read/write
  102.   access. CreateFile assumes that the file is opened as an output file
  103.   and will set the length of any existing file to zero.  If any errors
  104.   occur DosErrNo will have the error code.
  105.  
  106.   DosErrNo    Description
  107.   ──────── ───────────────────
  108.      3     Path not found
  109.      4     No handle available
  110.      5     Access denied
  111.      6     Invalid file handle
  112.  
  113.  
  114. ■ See Also
  115.  
  116.   OpenFile
  117. ..page
  118. ■ Example
  119.  
  120.   Program Example;
  121.   Uses FPDisk;
  122.   Begin
  123.      CreateFile( 'NewFile', 32, Handle );
  124.              .
  125.              .
  126.              .
  127.   End.
  128.  
  129.   If "Newfile" exists in the current directory then CreateFile will
  130.   open the file for output and assign a length of zero to the file.
  131.   If "Newfile" does not exist then a new file will be created.  The
  132.   variable Handle will return an integer that is associated with the
  133.   file "Newfile". DosErrNo will contain zero if the CreateFile
  134.   operation was successful.
  135. ..page
  136. ..head03ADosFindFirst
  137. ■ Description
  138.  
  139.   Find the first file that matches the given filespec and attributes.
  140.  
  141.  
  142. ■ Summary
  143.  
  144.   Procedure DosFindFirst(    Path    : String;
  145.                              Attr    : Integer;
  146.                          Var FindRec : TFindRec );
  147.  
  148.   Path        the path and filename of the file to search for.
  149.  
  150.   Attr        attributes of the files to include in the file search
  151.  
  152.   FindRec     the result of the search is returned in a variable of
  153.               TFindRec. (See Global Structures page 5-1).
  154.  
  155.  
  156. ■ Remarks
  157.  
  158.   This operation will search for the specified file.  The characters *
  159.   and ? may be used for the filespec.  If any errors occur DosErrNo
  160.   will have the error code.
  161.  
  162.   DosErrNo    Description
  163.   ──────── ───────────────────
  164.      2     Directory not found
  165.      3     Path not found
  166.     18     No more files
  167.  
  168.   The search attibute values may be one or any combination of values.
  169.   (e.g. to search for a hidden system file use value 6).
  170.  
  171.   Bit(s)   Value   Description
  172.   ──────   ─────   ───────────
  173.    0         1     read-only
  174.    1         2     hidden
  175.    2         4     system
  176.    3         8     volumne label
  177.    4        16     directory
  178.    5        32     archive
  179.    6-15            reserved
  180.  
  181.  
  182. ■ See Also
  183.  
  184.   DosFindNext
  185. ..page
  186. ■ Example
  187.  
  188.   Program Example;
  189.   Uses FPDisk;
  190.   Var
  191.      Rec : TFindRec;
  192.   Begin
  193.       DosFindFirst( '*.PAS', 32, Rec );
  194.   End.
  195.  
  196.   This example will find the first occurrance of a file in the current
  197.   directory that has the extension of "PAS".
  198. ..page
  199. ..head03ADosFindNext
  200. ■ Description
  201.  
  202.   Returns the next file entry that matches the filespec and attributes
  203.   used in a previous call to DosFindFirst.
  204.  
  205.  
  206. ■ Summary
  207.  
  208.   Procedure DosFindNext( Var FindRec : TFindRec );
  209.  
  210.   FindRec     the result of the search is returned in the variable of
  211.               TFindRec. (See Global Structures page 5-1).
  212.  
  213.  
  214. ■ Remarks
  215.  
  216.   DosFindNext must be called after DosFindFirst and before any other
  217.   calls that transfer data into the DTA at the time of the call to
  218.   DosFindFirst.
  219.  
  220.   DosErrNo    Description
  221.   ──────── ──────────────────
  222.      18    No more files
  223.  
  224.  
  225. ■ See Also
  226.  
  227.   DosFindFirst
  228.  
  229.  
  230. ■ Example
  231.  
  232.   Program Example;
  233.   Uses FPDisk;
  234.   Var
  235.      Rec : TFindRec;
  236.   Begin
  237.      DosFindFirst( '*.PAS', 32, Rec );
  238.      While ( Not DosErrorNo ) Do Begin
  239.         Writeln( Rec.Name );
  240.         DosFindNext( Rec );
  241.      End;
  242.   End.
  243.  
  244.   This example will find the all occurrances of files in the current
  245.   directory that have the extension of "PAS".
  246. ..page
  247. ..head03AFSeek
  248. ■ Description
  249.  
  250.   Change the logical read/write position of the file pointer.
  251.  
  252.  
  253. ■ Summary
  254.  
  255.   Function FSeek( Handle,Orgin : Integer; Offset : LongInt) : LongInt;
  256.  
  257.   Handle      file handle that was assigned to a file using either the
  258.               OpenFile or CreateFile operaton.
  259.  
  260.   Orgin       gives the starting location to use when moving the file
  261.               pointer.
  262.  
  263.               Method           Description
  264.               ────── ────────────────────────────────
  265.                0     beginning of file
  266.                1     current location of file pointer
  267.                2     EOF location
  268.  
  269.   Offset      gives the number of bytes to move the file pointer.
  270.  
  271.  
  272. ■ Remarks
  273.  
  274.   This operation will move the file pointer for the specified number
  275.   of bytes given.  On return this function reports the number of bytes
  276.   the file pointer is from the beginning of the file.  If any errors
  277.   occur DosErrNo will have the error code.
  278.  
  279.   DosErrNo Description
  280.   ──────── ───────────────────
  281.       1    Invalid method code
  282.       6    Invalid file handle
  283. ..page
  284. ■ Example
  285.  
  286.   Program Example;
  287.   Uses FPDisk;
  288.   Var
  289.      Handle : Integer;
  290.      Offset : LongInt;
  291.   Begin
  292.      Openfile( 'file.dat', 2, Handle );
  293.      If ( DosErrNo = 0 ) Then
  294.         Offset := FSeek( Handle, 2, 0 );
  295.   End.
  296.  
  297.   Executing FSeek with an offset of zero, and the seek beginning at
  298.   the end of the file, the variable Offset will contain a long integer
  299.   of the file size in bytes.  FSeek always returns the number of bytes
  300.   the file pointer is located from the beginning of the file.
  301. ..page
  302. ..head03AGetDrive
  303. ■ Description
  304.  
  305.   Reports the current default disk drive
  306.  
  307.  
  308. ■ Summary
  309.  
  310.   Function GetDrive : Integer;
  311.  
  312.  
  313. ■ Remarks
  314.  
  315.   GetDrive returns an integer to indicate which disk drive is the
  316.   current default drive where:
  317.  
  318.      Drive A = 1
  319.      Drive B = 2
  320.      Drive C = 3
  321.  
  322.  
  323. ■ Example
  324.  
  325.   Program Example;
  326.   Uses FPDisk;
  327.   Begin
  328.      Writeln( 'Current Drive is ', Chr( GetDrive + 65 ) );
  329.   End.
  330.  
  331.   The write statement will display the letter of the drive that is the
  332.   current default drive.
  333. ..page
  334. ..head03AGetDTA
  335. ■ Description
  336.  
  337.   Returns the current disk transfer area
  338.  
  339.  
  340. ■ Summary
  341.  
  342.   Procedure GetDTA( Var Segment, Offset : Integer );
  343.  
  344.   Segment     returns the segment of the current DTA.
  345.  
  346.   Offset      returns the offset of the current DTA.
  347.  
  348.  
  349. ■ Example
  350.  
  351.   Program Example;
  352.   Uses FPDisk;
  353.   Var
  354.      Segment,Offset : Word;
  355.   Begin
  356.      GetDTA( Segment, Offset );
  357.      Write( 'DTA Segment = ', Segment, '   ' );
  358.      Writeln( 'DTA Offset  = , Offset );
  359.   End.
  360.  
  361.   After the call to GetDTA the integer variables segment and offset
  362.   will contain the Segment:Offset address for the current disk
  363.   transfer area.
  364. ..page
  365. ..head03AGetFileSize
  366. ■ Description
  367.  
  368.   Report the number of bytes in a disk file.
  369.  
  370.  
  371. ■ Summary
  372.  
  373.   Function GetFileSize( Handle : Integer ) : LongInt;
  374.  
  375.   Handle      gives a file handle that was assigned to a file using
  376.               the OpenFile or CreateFile operaton.
  377.  
  378.  
  379. ■ Remarks
  380.  
  381.   GetFileSize reports the number of bytes in the file associated with
  382.   the handle number.
  383.  
  384.  
  385. ■ Example
  386.  
  387.   Program Example;
  388.   Uses FPDisk;
  389.   Var
  390.      Handle : Integer;
  391.      Offset : LongInt;
  392.   Begin
  393.      Openfile( 'file.dat', 2, Handle );
  394.      Writeln( 'Total bytes in file.dat = ', GetFileSize( Handle ):1 );
  395.   End.
  396.  
  397.   In this example the Writeln statement will display the total number
  398.   of bytes in the file "file.dat".
  399. ..page
  400. ..head03AGetNDrvs
  401. ■ Description
  402.  
  403.   Report the number of disk drives.
  404.  
  405.  
  406. ■ Summary
  407.  
  408.   Function GetNDrvs : Integer;
  409.  
  410.  
  411. ■ Remarks
  412.  
  413.   GetNDrvs reports the total number of diskette and fixed disk drives
  414.   installed for the current system.
  415.  
  416.  
  417. ■ Example
  418.  
  419.   Program Example;
  420.   Uses FPDisk;
  421.   Begin
  422.      Writeln( 'Number of disk drives installed = ', GetNDrvs:1 );
  423.   End.
  424.  
  425.   In this example the Writeln statement will display the numeric value
  426.   for the current number of disk drives currently installed on the
  427.   system.
  428. ..page
  429. ..head03AOpenFile
  430. ■ Description
  431.  
  432.   Open the file given in the string passed.
  433.  
  434.  
  435. ■ Summary
  436.  
  437.   Procedure OpenFile(    Path   : String;
  438.                          Attr   : Integer;
  439.                      Var Handle : Integer );
  440.  
  441.   Path        the path and filename of the file to open.
  442.  
  443.   Attr        the type of access to be allowed for this file.
  444.  
  445.                  Access Code Description
  446.                  ──────────- ──────────────────
  447.                       0      Read Only access
  448.                       1      Write Only access
  449.                       2      Read/write access
  450.  
  451.   Handle      returns an integer that represents the file handle that
  452.               is associated with the file given in Path.  The file
  453.               handle is to be used when any operations on the file are
  454.               to take place.
  455.  
  456. ■ Remarks
  457.  
  458.   If an error occurred DosErrNo will contain one of the following
  459.   error codes.
  460.  
  461.   DosErrNo Description
  462.   ──────── ───────────────────
  463.      2     File not found
  464.      4     No handle available
  465.      5     access denied
  466.     12     invalid access code
  467.  
  468.  
  469. ■ See Also
  470.  
  471.   CreateFile
  472. ..page
  473. ■ Example
  474.  
  475.   Program Example;
  476.   Uses FPDisk;
  477.   Var
  478.      Handle : Integer;
  479.   Begin
  480.      OpenFile( 'file.dat', 0, Handle );
  481.   End.
  482.  
  483.   On return from opening the file, if DosErrNo contains a zero, handle
  484.   will contain the integer of the file handle for the file 'File.dat'.
  485.   The file is opened for read only access.
  486. ..page
  487. ..head03AReadFile
  488. ■ Description
  489.  
  490.   Read a file or device for a specified number of bytes.
  491.  
  492.  
  493. ■ Summary
  494.  
  495.   Procedure ReadFile(     Handle : Integer;
  496.                           NBytes : Integer;
  497.                       Var Buffer          ;
  498.                       Var RBytes : Integer );
  499.  
  500.   Handle      file handle associated with the appropriate file or
  501.               device for the read operation to act on.
  502.  
  503.   NBytes      number of bytes to read from the file or device.
  504.  
  505.   Buffer      buffer area to place the data read.
  506.  
  507.   RBytes      returns the number of bytes actually read from the file
  508.               or device.  If RBytes is zero then an attempt was made
  509.               to read from the end of a file.
  510.  
  511.  
  512. ■ Remarks
  513.  
  514.   If an error occurred DosErrNo will contain one of the following
  515.   error codes.
  516.  
  517.   DosErrNo Description
  518.   ──────── ───────────────────
  519.      5     Access denied
  520.      6     Invalid handle
  521.  
  522.  
  523. ■ Example
  524.  
  525.   Program Example;
  526.   Uses FPDisk;
  527.   Var
  528.      Buffer               : Array[1..128] Of Byte;
  529.      Handle,NBytes,RBytes : Integer;
  530.      Offset               : LongInt;
  531.   Begin
  532.      OpenFile( 'file.dat', 0, Handle );
  533.      Offset := FSeek( Handle, 0, 0 );
  534.      ReadFile( Handle, 128, Buffer, RBytes );
  535.   End.
  536.  
  537.   Assuming no errors, 128 bytes will be read from the file and placed
  538.   into the variable Buffer.  On return from ReadFile the variable
  539.   RBytes will contain the actual number of bytes that were read from
  540.   the file.
  541. ..page
  542. ..head03AResetDisk
  543. ■ Description
  544.  
  545.   Resets the disk and flushes all file buffers.
  546.  
  547.  
  548. ■ Summary
  549.  
  550.   Procedure ResetDisk;
  551.  
  552.  
  553. ■ Remarks
  554.  
  555.   This routine does not close any files.  To ensure that the length of
  556.   a file is recorded properly in the file directory you should first
  557.   close the file before using this routine.
  558. ..page
  559. ..head03AResetErrCodes
  560. ■ Description
  561.  
  562.   Resets global variables that indicate device errors to their initial
  563.   settings.
  564.  
  565.  
  566. ■ Summary
  567.  
  568.   Procedure ResetErrCodes;
  569.  
  570.  
  571. ■ Remarks
  572.  
  573.   The following variables are set as followings:
  574.  
  575.      DosErrNo   =  0
  576.      CErrCode   =  0
  577.      CErrType   = -1
  578.      CErrDrive  = -1
  579.      CErrDevice = ''
  580.  
  581. ■ See Also
  582.  
  583.   SetInt24
  584. ..page
  585. ..head03ARestInt24
  586. ■ Description
  587.  
  588.   Uninstalls or restores the programs critical interrupt error
  589.   handler.
  590.  
  591.  
  592. ■ Summary
  593.  
  594.   Procedure RestInt24;
  595.  
  596.  
  597. ■ See Also
  598.  
  599.   SetInt24
  600.  
  601.  
  602. ■ Remarks
  603.  
  604.   This procedure will uninstall the critical interrupt error handler
  605.   that was installed with the SetInt24 procedure.  This procedure uses
  606.   the SegInt24 and OfsInt24 variables to restore the interrupt handler
  607.   in use before the interrupt handler was installed.
  608. ..page
  609. ..head03ASetDTA
  610. ■ Description
  611.  
  612.   Set the file attribute for the file specified.
  613.  
  614.  
  615. ■ Summary
  616.  
  617.   Procedure SetDTA( Segment, Offset : Word );
  618.  
  619.   Segment     segment address for the new DTA.
  620.  
  621.   Offset      offset within the segment for the new DTA.
  622.  
  623.  
  624. ■ Example
  625.  
  626.   Program Example;
  627.   Uses FPDisk;
  628.   Var
  629.      Buffer : Array[1..4096] Of Byte;
  630.   Begin
  631.      SetDTA( Seg( Buffer ), Ofs( Buffer ) );
  632.   End.
  633.  
  634.   The new DTA will point to the data variable Buffer and all the
  635.   information associated with the Disk Transfer Area will be placed in
  636.   the data variable Buffer until the DTA address is changed.
  637. ..page
  638. ..head03ASetInt24
  639. ■ Description
  640.  
  641.   Initializes the critical error handler routine and its global
  642.   variables for use with your system.
  643.  
  644.  
  645. ■ Summary
  646.  
  647.   Procedure SetInt24;
  648.  
  649.  
  650. ■ Remarks
  651.  
  652.   Only one call needs to be made to this routine for a program that
  653.   wants to use the critical error handler routine.  In the event of a
  654.   critical error, the variables DosErrNo, CErrCode, CErrType,
  655.   CErrDrive and CErrDevice are set to the appropiate values.  Refer to
  656.   the global variable section at the beginning of this chapter for a
  657.   description of each variable name.
  658.  
  659.   The following are the I/O error values that are set in the CErrCode
  660.   global variable.  These codes match the same codes DOS function 59h
  661.   (Get Extended Error Code) return.  Notice that all values are
  662.   hexadecimal.
  663.  
  664.   Error
  665.   Code        Description
  666.   ───── ──────────────────────────────-
  667.    13   Write-protect error
  668.    14   Unknown unit
  669.    15   Disk drive not ready
  670.    16   Unknown command
  671.    17   Data error (bad CRC)
  672.    18   A bad request structure length
  673.    19   Data seek error
  674.    1A   Unknown media type
  675.    1B   Disk sector not found
  676.    1C   Printer is out of paper
  677.    1D   Write fault
  678.    1E   Read fault
  679.    1F   General failure
  680.  
  681.  
  682. ■ See Also
  683.  
  684.   ResetErrCodes, RestInt24
  685. ..page
  686. ■ Example
  687.  
  688.   Program Example;
  689.   Uses FPDisk;
  690.   Var
  691.      Handle : Integer;
  692.   Begin
  693.      SetInt24();
  694.  
  695.      CreateFile( 'a:file.dat', 0, Handle );
  696.  
  697.      If ( !DosErrNo ) Then Begin
  698.         Writeln( 'DosErrNo   = ', DosErrNo:1   );
  699.         Writeln( 'CErrCode   = ', CErrCode:1   );
  700.         Writeln( 'CErrType   = ', CErrType:1   );
  701.         Writeln( 'CErrDrive  = ', CErrDrive:1  );
  702.         Writeln( 'CErrDevice = ', CErrDevice:1 );
  703.      End
  704.      Else Begin
  705.         Writeln( 'file opened ok...' );
  706.         CloseFile( Handle );
  707.      End;
  708.   End.
  709.  
  710.   If this program was executed with the A: drive door open, a critical
  711.   error would occur.  On return from CreateFile the critical error
  712.   variables should be set to the following values:
  713.  
  714.      DosErrNo   = 3             Path not found
  715.      CErrCode   = 21            Disk drive not ready
  716.      CErrType   = 0             0=Block    1=Character
  717.      CErrDrive  = 0             Drive A=0, B=1, C=2, etc
  718.      CErrDevice = DISKETTE      Device name
  719. ..page
  720. ..head03AWriteFile
  721. ■ Description
  722.  
  723.   Write to a file or device for a specified number of bytes.
  724.  
  725.  
  726. ■ Summary
  727.  
  728.   Procedure WriteFile(    Handle : Integer;
  729.                           NBytes : Word   ;
  730.                       Var Buffer          ;
  731.                       Var WBytes : Word   );
  732.  
  733.   Handle      the file handle that is associated with the appropriate
  734.               file or device for the read operation to act on.
  735.  
  736.   NBytes      number of bytes to write to the file or device.
  737.  
  738.   Buffer      this is the data area that contains the data that is to
  739.               be written to the file or device.
  740.  
  741.   WBytes      returns the actual number of bytes that were written out
  742.               to the file or device.
  743.  
  744.  
  745. ■ Remarks
  746.  
  747.   DosErrNo will contain one of the following error codes when an error
  748.   occurs.
  749.  
  750.   DosErrNo    Description
  751.   ──────── ───────────────────
  752.      5     Access denied
  753.      6     Invalid handle
  754.     20     insufficent disk space
  755.  
  756.  
  757. ■ Example
  758.  
  759.   Program Example;
  760.   Uses FPDisk;
  761.   Var
  762.      Buffer        : Array[1..128] Of Byte;
  763.      Handle,WBytes : Integer;
  764.   Begin
  765.      CreateFile( 'file.dat', 2, Handle );
  766.      FillChar( Buffer, 128, 'A' );
  767.      WriteFile( Handle, 128, Buffer, WBytes );
  768.      CloseFile( Handle );
  769.   End.
  770.  
  771.   This example will create a file and write out 128 A's to the new
  772.   file.  WBytes on return should contain the value 128.
  773. ..page
  774. 
  775.