home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 11 / 11.iso / m / m110 / 3.ddi / GRPCT2.EXE / DOSIO.TXT < prev    next >
Encoding:
Text File  |  1990-02-12  |  6.5 KB  |  272 lines

  1. ;       DOSIO.TXT - GRASP 3.5 DOS Interrupt Routines - January 1990
  2. ;
  3. ; This file contains sample Grasp routines that use the INT commmand to perform
  4. ; text file manipulation (reading and writing) and common DOS disk tasks:
  5. ;
  6. ; DOSFUNC                              Calls a DOS function using interrupt 21.
  7. ; DOSERR             Displays an error encountered during a DOS interrupt call.
  8. ; OPEN, SEEK, READ, READFILE, CLOSE              Read operations on text files.
  9. ; APPEND, CREATE, WRITE, WRITESTR, CLOSE        Write operations on text files.
  10. ; MKDIR, RMDIR, CHDIR, DELETE, RENAME, SETDRIVE            Common DOS commands.
  11. ; VARS                       Displays the values of all variables passed to it.
  12. ; RANDOM                     Generates a "random" number.
  13. ;
  14. ; The command "MERGE DOSIO" can be used in your own program to incorporate
  15. ; the routines at runtime, or you can copy them in while editing it.
  16. ; Here's a small sample program that uses the routines to create a file
  17. ; called TEMP.TXT, write line numbers into it, and close the file.
  18. ; The program then reads the file and displays it onscreen.
  19.   create temp.txt         ;create the file TEMP.TXT in the current directory
  20.   set fs @0               ;set the file stream number for TEMP.TXT
  21.   set cnt 0               ;set up a line counter variable
  22.   mark 20                 ;set up to loop 20 times
  23.     set cnt @cnt+1        ;increment the line counter
  24.     writestr @fs " Line "$@cnt$chr(13)$chr(10)
  25.                           ;write the line number into the file
  26.   loop                    ;repeat the loop
  27.   close @fs               ;close TEMP.TXT (always necessary after writing)
  28.   readfile temp.txt       ;open TEMP.TXT and read it into variable @0
  29.   text @0                 ;display the file's contents
  30.   delete temp.txt         ;delete the file from the disk
  31.   waitkey                 ;wait for a keystroke
  32.   exit                    ;stop the program
  33.  
  34.  
  35. ; DOSFUNC(FUNC,STRING,CMD)
  36. ;
  37. ; Calls any DOS function that requires only a single string.
  38. ;
  39. dosfunc:
  40.     set cmd @3
  41.     int 0x21 @1,,,ofs(@2),,,seg(@2)
  42.     if @0&1 doserr
  43.     return @ax
  44.  
  45.  
  46. ; DOSERR()
  47. ;
  48. ; Displays a DOS error message (error number is in @AX).
  49. ;
  50. doserr:
  51.     databegin errs
  52.     dataskip @ax-1
  53.     text chr(10)$@cmd$": "$@$chr(10)
  54.     waitkey
  55.     exit
  56.  
  57. errs:
  58.     "Invalid function number"
  59.     "File not found"
  60.     "Path not found"
  61.     "Too many open files"
  62.     "Access denied"
  63.     "Invalid handle"
  64.     "Memory control blocks destroyed"
  65.     "Insufficient Memory"
  66.     "Invalid memory block address"
  67.     "Invalid environment"
  68.     "Invalid format"
  69.     "Invalid access code"
  70.     "Invalid data"
  71.     ""
  72.     "Invalid drive was specified"
  73.     "Can't remove current directory"
  74.     "Not same device"
  75.     "No more files"
  76.  
  77.  
  78. ; OPEN(FNAME)
  79. ;
  80. ; Opens an existing text file for reading.  Sets the variable FS (file stream
  81. ; number) which is used by other routines to refer to the file.
  82. ;
  83. open:
  84.     dosfunc    0x3d00 @1 @0
  85.     return @0
  86.  
  87.  
  88. ; SEEK(FS,POS,REL)
  89. ;
  90. ; Moves to a given position within a file.  FS is file stream number, set by
  91. ; OPEN.  POS is position in the file.  REL can be 0, 1, or 2.
  92. ; REL = 0:  POS is relative to beginning of file.
  93. ; REL = 1:  POS is relative to current position in file.
  94. ; REL = 2:  POS is relative to end of file.
  95. ;
  96. seek:
  97.     set cmd @0
  98.     if @1>=5
  99.         int 0x21 0x4200+@3,@1,@2,@2>>16
  100.         if @0&1 doserr
  101.         return (@dx<<16)+@ax
  102.     else
  103.         return 0
  104.     endif
  105.  
  106.  
  107. ; READ(FS,SEG,OFS,COUNT)
  108. ;
  109. ; Reads from an open text file to the given memory location (SEG and OFS)
  110. ; for the given number of bytes (COUNT).  FS is file stream number, set by
  111. ; OPEN.
  112. ;
  113. read:
  114.     set cmd @0
  115.     int 0x21 0x3F00,@1,@4,@3,,,@2
  116.     if @0&1 doserr
  117.     return @0
  118.  
  119.  
  120. ; READFILE(FNAME)
  121. ;
  122. ; Returns the contents of a text file as a string (in the return variable @0).
  123. ;
  124. readfile:
  125.     open @1
  126.     local fs @0
  127.     seek @fs 0 2
  128.     local len @0
  129.     if @len>0
  130.         set len @len-1
  131.     endif
  132.     seek @fs 0
  133.     local buf ofs(string(@len," "))
  134.     read @fs seg(@buf) @buf @len
  135.     close @fs
  136.     return @((seg(@buf)<<16)+@buf)
  137.  
  138.  
  139. ; CLOSE(FS)
  140. ;
  141. ; Closes a text file (any file that was opened must be closed) using the file
  142. ; stream number set by OPEN, APPEND, or CREATE.
  143. ;
  144. close:
  145.     set cmd @0
  146.     if @1>=5
  147.         int 0x21 0x3e00,@1
  148.         if @0&1 doserr
  149.     endif
  150.     return 0
  151.  
  152.  
  153. ; APPEND(FNAME)
  154. ;
  155. ; Opens an existing text file for writing.  Sets the variable FS (file stream
  156. ; number) which is used by other routines to refer to the file.  Seeks to the
  157. ; end of the file so WRITE/WRITESTR will append text to the file.
  158. ;
  159. ;
  160. append:
  161.     dosfunc    0x3d02 @1 @0
  162.     local fs @0
  163.     seek @fs 0 2        ;seek to end of file
  164.     return @fs
  165.  
  166.  
  167. ; CREATE(FNAME)
  168. ;
  169. ; Opens a new text file for reading.  Sets the variable FS (file stream
  170. ; number) which is used by other routines to refer to the file.  If a file
  171. ; of the same name already exists, it will be deleted.
  172. ;
  173. create:
  174.     dosfunc    0x3c00 @1 @0
  175.     return @0
  176.  
  177.  
  178. ; WRITE(FS,SEG,OFS,COUNT)
  179. ;
  180. ; Writes to an open text file from the given memory location (SEG and OFS)
  181. ; for the given number of bytes (COUNT).  FS is file stream number, set by
  182. ; APPEND or CREATE.
  183. ;
  184. write:
  185.     set cmd @0
  186.     int 0x21 0x4000,@1,@4,@3,,,@2
  187.     if @0&1 doserr
  188.     return @0
  189.  
  190.  
  191. ; WRITESTR(FS,STRING)
  192. ;
  193. ; Writes text (in variable STRING) into a text file at the current position.
  194. ; FS is file stream number, set by APPEND or CREATE.
  195. ;
  196. writestr:
  197.     write @1,seg(@2),ofs(@2),len(@2)
  198.     return @0
  199.  
  200.  
  201. ; MKDIR(PATH)  make a new disk directory
  202. ;
  203. mkdir:
  204.     dosfunc    0x3900 @1 @0
  205.     return @0
  206.  
  207.  
  208. ; RMDIR(PATH)  remove a directory (must be empty)
  209. ;
  210. rmdir:
  211.     dosfunc    0x3a00 @1 @0
  212.     return @0
  213.  
  214.  
  215. ; CHDIR(PATH)  change the current directory
  216. ;
  217. chdir:
  218.     dosfunc    0x3b00 @1 @0
  219.     return @0
  220.  
  221.  
  222. ; DELETE(FNAME)  delete a disk file
  223. ;
  224. ; delete file FNAME
  225. ;
  226. delete:
  227.     dosfunc    0x4100 @1 @0
  228.     return @0
  229.  
  230.  
  231. ; RENAME(FNAME1,FNAME2)  rename a disk file
  232. ;
  233. rename:
  234.     set cmd @0
  235.     int 0x21 0x5600,,,ofs(@1),,ofs(@2),seg(@1),seg(@2)
  236.     if @0&1 doserr
  237.     return 0
  238.  
  239.  
  240. ; SETDRIVE(DRIVE)  change the logged drive (DRIVE is a single letter)
  241. ;
  242. setdrive:
  243.     set cmd @0
  244.     int 0x21 0x0e00,,,asc(@1)-asc("a")
  245.     return 0
  246.  
  247.  
  248. ; VARS(VAR,VAR,VAR,VAR,....)  display a list of variables and their values
  249. ;
  250. vars:
  251.     local cnt 1
  252.     mark 24
  253.         if @@cnt!=""
  254.             text "@"$@@cnt$"="$@@@cnt$" "
  255.         endif
  256.         set cnt @cnt+1
  257.     loop
  258.     text chr(10)
  259.     return
  260.  
  261.  
  262. ; RANDOM(LOW,HIGH)
  263. ;
  264. ; Generate a random number between LOW and HIGH
  265. ; Before calling, set up random seed variable RSEED in the calling routine.
  266. ; One approach is:  SET rseed peekl(0x0040006c)
  267. ;
  268. random:
  269.     set rseed (@rseed>>13)^@rseed
  270.     set rseed ((@rseed<<18)^@rseed)&0x7fffffff
  271.     return @rseed%((@2-@1)+1)+@1
  272.