home *** CD-ROM | disk | FTP | other *** search
/ Boston 2 / boston-2.iso / DOS / PROGRAM / BASIC / POWBASIC / LIBRARY5 / LSTRAN.ZIP / FILEUTIL.INC next >
Text File  |  1990-06-07  |  6KB  |  176 lines

  1. '************************************************************************
  2. function GetHandle%(FileName$, Method$)    public
  3.     local FileHandle%
  4.  
  5. ' The function GetHandle% returns for the lowest available Turbo file
  6. '   handle with the requested file open in the specified mode.  If no
  7. '   handles are available, the function returns 55.
  8. ' If the handle returned is < 1, then an error occured and file is not
  9. '   open.  The value returned is the negative of the PB error code.
  10. ' For com ports FileName$ is "COM#" and Method$ is option block (CR, etc.).
  11. '   The LEN= option of basic is not supported and will always be 128 which
  12. '   is PB's default.
  13.  
  14. FileHandle% = freefile
  15. on error goto FileOpenError
  16. if left$(ucase$(FileName$),3) = "COM"_       ' CHECK IF COM PORT
  17.     and len(FileName$) = 4 then
  18.     open FileName$ +Method$ as FileHandle%   ' IF IT IS OPEN AS COM PORT
  19. else                                         ' OTHERWISE
  20.     open Method$, FileHandle%, FileName$     ' TRY TO OPEN AS FILE
  21. end if
  22. on error goto 0                              ' TURN OFF ERROR CHECKING
  23. GetHandle% = FileHandle%                     ' RETURN VALID HANDLE OR 0
  24. exit function
  25.  
  26. FileOpenError:
  27. GetHandle% = -err                             ' Return error code as negative
  28.  
  29. end function
  30.  
  31. '************************************************************************
  32. function Exists% (FileSpec$)    public
  33.  
  34. ' The function Exists% returns a %True value if the file specified by
  35. '   FileName$ is on the current/specified disk drive.
  36.  
  37. FileSpec$ = FileSpec$ +chr$(0)     ' Make filespec an ASCIIZ
  38. reg %AX, &h3D00                    ' DOS function 3D: Open File, read access
  39. reg %DS, strseg(FileSpec$)         ' Point DS:DX to FileSpec
  40. reg %DX, strptr(FileSpec$)
  41. call interrupt %DOS                ' call DOS
  42.  
  43. if (reg(%Flags) and 1) = 0 then    ' If carry flag clear then sucessful
  44.     reg %BX, reg(%AX)              '   Move DOS handle from AX to BX
  45.     reg %AX, &h3E00                '   DOS function 3E: Close File
  46.     call interrupt %DOS            '   call DOS
  47.     Exists% = %True             '   Return TRUE
  48. else                               ' Otherwise
  49.     Exists% = %False            '   Return FALSE
  50. end if
  51.  
  52. end function
  53.  
  54. '************************************************************************
  55. function FileNameProper$ (FileName$)    public
  56.     local NameProper$
  57.  
  58. NameProper$ = FileName$
  59. while instr(NameProper$, "\") > 0
  60.     NameProper$ = mid$(NameProper$, instr(NameProper$, "\") +1)
  61. wend
  62. if instr(NameProper$, ".") > 0 then
  63.     NameProper$ = left$(NameProper$, instr(NameProper$, ".") -1)
  64. end if
  65. if instr(NameProper$, ":") > 0 then
  66.     NameProper$ = mid$(NameProper$, instr(NameProper$, ":") +1)
  67. end if
  68. FileNameProper$ = NameProper$
  69.  
  70. end function
  71.  
  72. '************************************************************************
  73. function FileNamePath$ (FileName$)    public
  74.     local NamePath$
  75.  
  76. NamePath$ = FileName$
  77. if instr(NamePath$, "\") > 0 or instr(NamePath$, ":") > 0 then
  78.     while right$(NamePath$, 1) <> "\" and right$(NamePath$, 1) <> ":"
  79.         NamePath$ = left$ (NamePath$, len(NamePath$) -1)
  80.     wend
  81. else
  82.     NamePath$ = ""
  83. end if
  84. FileNamePath$ = NamePath$
  85.  
  86. end function
  87.  
  88. '************************************************************************
  89. function DefaultExt$ (FileName$, Ext$)    public
  90.  
  91. if instr(FileName$, ".") > 0 then
  92.     DefaultExt$ = FileName$
  93. else
  94.     DefaultExt$ = ChangeExt$ (FileName$, Ext$)
  95. end if
  96.  
  97. end function
  98.  
  99. '************************************************************************
  100. function ChangeExt$ (FileName$, Ext$)    public
  101.  
  102. if instr(Ext$, ".") > 0 then
  103.     Ext$ = mid$(Ext$, instr(Ext$, ".") +1)
  104. end if
  105. Ext$ = left$(Ext$, 3)
  106. ChangeExt$ = FileNamePath$ (FileName$) _
  107.             +FileNameProper$ (FileName$) + "." +Ext$
  108.  
  109. end function
  110.  
  111. '************************************************************************
  112. function GetBytes$ (Handle%, FileOffset&, BytesToGet%)    public
  113. ' Purpose ......| Read specified number of bytes from a binary file
  114. ' Author/Date ..| Michael E. Flenniken  1-18-90
  115. ' Notes ........| File must be opened in binary mode before this function is
  116. '               | called.  There is no error checking, so the program will
  117. '               | crash if the file isn't opened in binary mode.
  118.     local BytesRead$
  119.  
  120. seek Handle%, FileOffset&
  121. get$ Handle%, BytesToGet%, BytesRead$
  122. GetBytes$ = BytesRead$
  123.  
  124. end function
  125.  
  126. '************************************************************************
  127. function GetKeyPress$ (PromptStmt$, Valid$, Echo%)    public
  128. ' Purpose ......| Prompt user to press a key and validate, with or w/o echo
  129. ' Author/Date ..| Michael E. Flenniken  1-18-90
  130. ' Notes ........| PromptStmt$ is the text to prompt user with.  GetKeyPress$
  131. '               |   normally moves the cursor to the next line upon
  132. '               |   completion, but if PromptStmt$ is null and Echo% = 0 then
  133. '               |   cursor position will be unchanged.
  134. '               | Valid$ is a list of valid keys presses to accept.  If Valid$
  135. '               |   is null then all keys are valid.
  136. '               | If Echo% is 0 the validated key press will not be sent to
  137. '               |   the screen.
  138.     local KeyPress$
  139.  
  140. if PromptStmt$ <> "" then
  141.     ? PromptStmt$; " ";
  142. end if
  143. while len(KeyPress$) = 0
  144.     KeyPress$ = inkey$
  145.     if instr(Valid$, KeyPress$) = 0 and len(Valid$) > 0 then
  146.         KeyPress$ = ""
  147.     end if
  148. wend
  149. if Echo% then
  150.     ? KeyPress$;
  151. elseif len(PromptStmt$) > 0 then
  152. '    ?
  153. end if
  154. GetKeyPress$ = KeyPress$
  155.  
  156. end function
  157.  
  158. '************************************************************************
  159. sub StdOut (OutLine$, Term%)    public
  160. ' Purpose ......| Send output to DOS stdout
  161. ' Author/Date ..| Michael E. Flenniken  6-6-90
  162. ' Notes ........| OutLine$ is the text to send to DOS stdout
  163. '               | Term% is the terminating character(s) to send.  If Term% is
  164. '               |   13, a CR and LF is sent, otherwise it is ignored.
  165.  
  166. if Term% = 13 then
  167.     OutLine$ = OutLine$ +chr$(13, 10)
  168. end if
  169. OutLine$ = OutLine$ +"$"
  170. reg (%AX), &h0900
  171. reg (%DX), strptr (OutLine$)
  172. reg (%DS), strseg (OutLine$)
  173. call interrupt &h21
  174.  
  175. end sub
  176.