home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / basic / bmag / diru.bas < prev    next >
Encoding:
BASIC Source File  |  1994-04-13  |  10.7 KB  |  235 lines

  1. '─ Area: F-PowerBASIC ─────────────────────────────────────────────────────────
  2. '  Msg#: 26                                           Date: 07 Apr 94  20:03:32
  3. '  From: Frank Cox                                    Read: Yes    Replied: No 
  4. '    To: David Wrobel                                 Mark:                     
  5. '  Subj: Re: The Rules
  6. '──────────────────────────────────────────────────────────────────────────────
  7. '=========================================================================
  8. '                                 DIRU.BAS
  9. '                            Unit in PowerBASIC
  10. '                   for Retrieval of Directory Information
  11. '
  12. ' CopyRight 1991 by Dave Navarro, Jr.             All Rights Reserved
  13. '               Documentation "Prettified" by Ann Goodman
  14. '=========================================================================
  15. ' Permission is granted to use these routines in your programs with the
  16. ' following restrictions:
  17. '  - You may NOT distribute this source in part or whole as part of
  18. '    any library for PowerBASIC or Turbo BASIC
  19. '  - If you use these routines in any commercial program (commercial
  20. '    meaning anything you get paid for, including shareware), you must
  21. '    give the original author, Dave Navarro credit in either the program
  22. '    or in the documentation.
  23. '  - In no way shape or form may you remove the copyright notice from
  24. '    this, or any part of the source.
  25. ' These routines were tested on a generic XT clone and a 486 machine and
  26. ' found to work without fail, however, should you encounter any problems,
  27. ' the author cannot be held responsible for any problems/damage you may
  28. ' encounter, you use these at your own risk.
  29. '=========================================================================
  30. ' Author can be contacted on The Bard's Lair BBS (718) 381-3651
  31. '=========================================================================
  32. '
  33. ' About DIRU:BAS:
  34. ' --------------
  35. '      This unit contains routines for obtaining directory information
  36. ' for files names found by the PowerBASIC function DIR$ (PowerBASIC
  37. ' version 2.1).
  38. '
  39. '     The DIR$ function uses functions 11H or 12H of DOS interrupt
  40. ' 21H (the "Find First" and "Find Next" file functions) to find
  41. ' a directory entry that satisfies the arguments to the DIR$ function,
  42. ' and returns the name of this file.  However, since the whole
  43. ' directory entry for the found file is placed in the DOS Data Transfer
  44. ' Area (the DTA) by the interrupt, the rest of the directory information
  45. ' for the file, (file size, date and time stamps, and file type), can be
  46. ' retrieved from there.
  47. '      Though the DTA is normally found at offset 80H of the PSP,
  48. ' the DOS interrupts used by DIR$ can move it, so the current address
  49. ' of the DTA must be determined after each execution of DIR$ before
  50. ' attempting to access the directory information.
  51. '
  52. '      The routines in this unit retrieve the directory information
  53. ' and put it in usable format.
  54. '    The routines are:
  55. '
  56. '  FUNCTION   GetAttrib%      Returns the attribute code of the file
  57. '                             as an integer.
  58. '
  59. '  FUNCTION   GetFileSize&    Returns the file size as a long integer
  60. '
  61. '  FUNCTION   GetFileDate$    Retrieves the file date stamp from DTA
  62. '                             and returns date as string MM-DD-YY
  63. '
  64. '  FUNCTION   GetFileTime$    Retrieves the file time stamp from DTA
  65. '                             and returns time as string HH:MMa or HH:MMp
  66. '
  67. '  FUNCTION   DOS2Date$       Decrypts file date stamp and converts it
  68. '                             to string of format MM-DD-YY
  69. '
  70. '  FUNCTION   DOS2Time$       Decrypts file time stamp and converts it
  71. '                             to string of format HH:MMa or HH:MMp.
  72. '
  73. '  SUB        GetDTA          Gets current address of Data Transfer Area.
  74. '
  75. '========================================================================
  76.  
  77. '========================================================================
  78. '|  Setup for Unit                                                      |
  79. '========================================================================
  80. $CPU 8086               'Compile with 8086 specific code
  81. $ERROR ALL OFF          'No error checking needed
  82. $COMPILE UNIT           'Compile to a UNIT (PBU)
  83. $STACK 0                'Requires no Stack Space
  84. $LIB ALL OFF            'Requires no PB libraries
  85.  
  86. '=======================================================================
  87. '|                  FUNCTIONS AND SUB-ROUTINES                         |
  88. '=======================================================================
  89. '-----------------------------------------------------------------------
  90. '| FUNCTION: FileAttrib%   | Get file attribute of filename returned   |
  91. '|                         | by last DIR$ statement executed.          |
  92. '-----------------------------------------------------------------------
  93.  
  94. ' The file attribute is a single byte located at offset 21 of the DTA.
  95. ' This function retrieves it and returns it as an integer.
  96.  
  97. FUNCTION FileAttrib% PUBLIC
  98.   CALL GetDTA(DtaSeg%, DtaOfs%)   'Get DTA address. (See SUB GetDTA
  99.       '                  in this unit.)
  100.   DEF SEG=DtaSeg%                 'Set segment to DTA address segment.
  101.     FileAttrib%=PEEK(DtaOfs%+21)  'Use PEEK to retrieve attribute byte.
  102.   DEF SEG                         'Return previous value of segment.
  103. END FUNCTION
  104.  
  105. '-----------------------------------------------------------------------
  106. '| FUNCTION: FileDate$     | Get file date of filename returned by     |
  107. '|                         | last execution of DIR$ function.          |
  108. '-----------------------------------------------------------------------
  109.  
  110. ' The file date stamp is found at offset 25 of the DTA as an encrypted
  111. ' integer (two bytes).  This function retrieves the date stamp and uses
  112. ' DOS2Date$ to decrypt it and to convert it to a string of format MM-DD-YY.
  113.  
  114. FUNCTION FileDate$ PUBLIC
  115.   CALL GetDTA(DtaSeg%, DtaOfs%)   'Get DTA address. (See SUB GetDTA
  116.       '                  in this unit.)
  117.   DEF SEG=DtaSeg%                 ' Set segment to DTA address segment.
  118.     FileDate$=DOS2Date$(PEEKI(Dtaofs% + 24))
  119.       ' use PB's PEEKI to retrieve two-byte
  120.       ' date stamp.  Use DOS2Date$ to decrypt
  121.       ' the date and convert it to a string.
  122.   DEF SEG                         ' Return previous value of segment.
  123. END FUNCTION
  124.  
  125. '-----------------------------------------------------------------------
  126. '| FUNCTION: FileTime$     | Get time stamp from file name returned    |
  127. '|                         | by last DIR$ statement executed.          |
  128. '-----------------------------------------------------------------------
  129.  
  130. ' The file time stamp is found at offset 22 of the DTA as an encrypted
  131. ' integer (two bytes).  This function retrieves the time stamp and uses
  132. ' DOS2Time$ to decrypt it and to convert it to a string of format HH:MMa.
  133.  
  134.  
  135.  
  136. FUNCTION FileTime$ PUBLIC
  137.   CALL GetDTA(DtaSeg%, DtaOfs%)   'Get DTA address. (See SUB GetDTA
  138.       '                  in this unit.)
  139.   DEF SEG=DtaSeg%                 ' Set segment to DTA address segment.
  140.     FileTime$=DOS2Time$(PEEKI(DtaOfs%+22))  'use PB's PEEKI to convert
  141.       ' use PB's PEEKI to retrieve two-byte
  142.       ' time stamp.  Use DOS2Time$ to decrypt
  143.       ' the time and convert it to a string.
  144.   DEF SEG                         ' Return segment to previous value.
  145. END FUNCTION
  146.  
  147. '-----------------------------------------------------------------------
  148. '| FUNCTION: FileSize$     | Get file size of file name returned       |
  149. '|                         | by last DIR$ statement executed.          |
  150. '-----------------------------------------------------------------------
  151.  
  152.  
  153. ' The file size is found at offset 26 of the DTA as a
  154. ' long integer.
  155.  
  156. FUNCTION FileSize& PUBLIC
  157.   CALL GetDTA(DtaSeg%, DtaOfs%)   'Get DTA address. (See SUB GetDTA
  158.       '                  in this unit.)
  159.   DEF SEG=DtaSeg%                 ' Set segment to DTA address segment
  160.      FileSize&=PEEKL(DtaOfs%+26)  ' Use PB's PEEKL to retrieve four-byte
  161.       ' file size and convert to long integer.
  162.   DEF SEG                         ' Return segment to previous value.
  163. END FUNCTION
  164.  
  165.  
  166. '-----------------------------------------------------------------------
  167. '| FUNCTION: DOS2Date$     | Convert encrypted date stamp from DTA     |
  168. '|                         | to time string of format MM-DD-YY         |
  169. '-----------------------------------------------------------------------
  170. '
  171. ' The file date stamp is encrypted by the following formula:
  172. '      Stamp = 512 * (Year - 1980)  + 32 * Month + Day
  173. ' The following routine decrypts the date stamp and converts the
  174. ' date to a string of format MM-DD-YY.
  175. '
  176. '
  177. FUNCTION DOS2Date$(Fd%) PUBLIC
  178.   Year% = Fd% \ 512  + 1980
  179.   Remainder% = Fd% MOD 512
  180.   Year$ = RIGHT$(STR$(Year%),2)
  181.   Month% = Remainder% \ 32
  182.   Month$ = MID$(STR$(Month%),2)
  183.   Day$ = MID$(STR$(Remainder% MOD 32),2)
  184.   IF LEN(day$) = 1 THEN   day$ = "0" + day$
  185.   IF LEN(MONTH$) = 1 THEN   MONTH$ = " " + MONTH$
  186.   DOS2Date$ = Month$ + "-" + Day$ + "-" + Year$
  187. END FUNCTION
  188.  
  189. '-----------------------------------------------------------------------
  190. '| FUNCTION: DOS2Time$     | Convert encrypted time stamp from DTA     |
  191. '|                         | to time string of format HH:MMp.          |
  192. '-----------------------------------------------------------------------
  193.  
  194. 'The file time stamp is accurate to the nearest even second.
  195. 'This function rounds it to minutes, makes a string of format HH:MM
  196. 'and appends "a" or "p" for AM or PM.
  197.  
  198. FUNCTION DOS2Time$(Ft%) PUBLIC
  199.   Ft%=INT(Ft%/2)
  200.   Hours%=(Ft% AND &B111110000000000)\1024
  201.   IF Hours%>12 THEN
  202.     DECR Hours%,12
  203.     Pm% = -1
  204.   END IF
  205.   Hour$=MID$(STR$(Hours%),2)
  206.   Minutes=(Ft%\16) AND &B111111
  207.   Minutes$=MID$(STR$(Minutes),2)
  208.   IF LEN(Minutes$)=1 THEN Minutes$="0"+Minutes$
  209.   IF LEN(Hour$)=1 THEN Hour$=" "+Hour$
  210.   IF Pm% THEN
  211.     Minutes$=Minutes$+"p"
  212.   ELSE
  213.     Minutes$=Minutes$+"a"
  214.   END IF
  215.   DOS2Time$=Hour$+":"+Minutes$
  216. END FUNCTION
  217.  
  218.  
  219. '-----------------------------------------------------------------------
  220. '| SUB : GetDTA         |  Get Disk Transfer Address (DTA) from DOS    |
  221. '-----------------------------------------------------------------------
  222. '
  223. '  The DTA is usually located in the PSP.  However, any routine that
  224. '  calls the FINDFIRST, FINDNEXT functions from DOS (such as the
  225. '  PowerBASIC statement DIR$) can change the location of the DTA.
  226. '  This sub-routine uses DOS interrupt 33, function 47, to get
  227. '  the current location of the DTA.
  228.  
  229. SUB GetDTA(DtaSeg%, DtaOfs%) PUBLIC
  230.   REG 1, &h2F00            ' Set function number to 47.
  231.   CALL INTERRUPT &h21      ' Call interrupt 33 (Hex 21).
  232.   DtaSeg%=REG(9)           ' Register 9 contains Segment of DTA address.
  233.   DtaOfs%=REG(2)           ' Register 2 contains Offset of DTA address
  234. END SUB
  235.