home *** CD-ROM | disk | FTP | other *** search
- ; DOSIO.TXT - GRASP 3.5 DOS Interrupt Routines - January 1990
- ;
- ; This file contains sample Grasp routines that use the INT commmand to perform
- ; text file manipulation (reading and writing) and common DOS disk tasks:
- ;
- ; DOSFUNC Calls a DOS function using interrupt 21.
- ; DOSERR Displays an error encountered during a DOS interrupt call.
- ; OPEN, SEEK, READ, READFILE, CLOSE Read operations on text files.
- ; APPEND, CREATE, WRITE, WRITESTR, CLOSE Write operations on text files.
- ; MKDIR, RMDIR, CHDIR, DELETE, RENAME, SETDRIVE Common DOS commands.
- ; VARS Displays the values of all variables passed to it.
- ; RANDOM Generates a "random" number.
- ;
- ; The command "MERGE DOSIO" can be used in your own program to incorporate
- ; the routines at runtime, or you can copy them in while editing it.
- ; Here's a small sample program that uses the routines to create a file
- ; called TEMP.TXT, write line numbers into it, and close the file.
- ; The program then reads the file and displays it onscreen.
- create temp.txt ;create the file TEMP.TXT in the current directory
- set fs @0 ;set the file stream number for TEMP.TXT
- set cnt 0 ;set up a line counter variable
- mark 20 ;set up to loop 20 times
- set cnt @cnt+1 ;increment the line counter
- writestr @fs " Line "$@cnt$chr(13)$chr(10)
- ;write the line number into the file
- loop ;repeat the loop
- close @fs ;close TEMP.TXT (always necessary after writing)
- readfile temp.txt ;open TEMP.TXT and read it into variable @0
- text @0 ;display the file's contents
- delete temp.txt ;delete the file from the disk
- waitkey ;wait for a keystroke
- exit ;stop the program
-
-
- ; DOSFUNC(FUNC,STRING,CMD)
- ;
- ; Calls any DOS function that requires only a single string.
- ;
- dosfunc:
- set cmd @3
- int 0x21 @1,,,ofs(@2),,,seg(@2)
- if @0&1 doserr
- return @ax
-
-
- ; DOSERR()
- ;
- ; Displays a DOS error message (error number is in @AX).
- ;
- doserr:
- databegin errs
- dataskip @ax-1
- text chr(10)$@cmd$": "$@$chr(10)
- waitkey
- exit
-
- errs:
- "Invalid function number"
- "File not found"
- "Path not found"
- "Too many open files"
- "Access denied"
- "Invalid handle"
- "Memory control blocks destroyed"
- "Insufficient Memory"
- "Invalid memory block address"
- "Invalid environment"
- "Invalid format"
- "Invalid access code"
- "Invalid data"
- ""
- "Invalid drive was specified"
- "Can't remove current directory"
- "Not same device"
- "No more files"
-
-
- ; OPEN(FNAME)
- ;
- ; Opens an existing text file for reading. Sets the variable FS (file stream
- ; number) which is used by other routines to refer to the file.
- ;
- open:
- dosfunc 0x3d00 @1 @0
- return @0
-
-
- ; SEEK(FS,POS,REL)
- ;
- ; Moves to a given position within a file. FS is file stream number, set by
- ; OPEN. POS is position in the file. REL can be 0, 1, or 2.
- ; REL = 0: POS is relative to beginning of file.
- ; REL = 1: POS is relative to current position in file.
- ; REL = 2: POS is relative to end of file.
- ;
- seek:
- set cmd @0
- if @1>=5
- int 0x21 0x4200+@3,@1,@2,@2>>16
- if @0&1 doserr
- return (@dx<<16)+@ax
- else
- return 0
- endif
-
-
- ; READ(FS,SEG,OFS,COUNT)
- ;
- ; Reads from an open text file to the given memory location (SEG and OFS)
- ; for the given number of bytes (COUNT). FS is file stream number, set by
- ; OPEN.
- ;
- read:
- set cmd @0
- int 0x21 0x3F00,@1,@4,@3,,,@2
- if @0&1 doserr
- return @0
-
-
- ; READFILE(FNAME)
- ;
- ; Returns the contents of a text file as a string (in the return variable @0).
- ;
- readfile:
- open @1
- local fs @0
- seek @fs 0 2
- local len @0
- if @len>0
- set len @len-1
- endif
- seek @fs 0
- local buf ofs(string(@len," "))
- read @fs seg(@buf) @buf @len
- close @fs
- return @((seg(@buf)<<16)+@buf)
-
-
- ; CLOSE(FS)
- ;
- ; Closes a text file (any file that was opened must be closed) using the file
- ; stream number set by OPEN, APPEND, or CREATE.
- ;
- close:
- set cmd @0
- if @1>=5
- int 0x21 0x3e00,@1
- if @0&1 doserr
- endif
- return 0
-
-
- ; APPEND(FNAME)
- ;
- ; Opens an existing text file for writing. Sets the variable FS (file stream
- ; number) which is used by other routines to refer to the file. Seeks to the
- ; end of the file so WRITE/WRITESTR will append text to the file.
- ;
- ;
- append:
- dosfunc 0x3d02 @1 @0
- local fs @0
- seek @fs 0 2 ;seek to end of file
- return @fs
-
-
- ; CREATE(FNAME)
- ;
- ; Opens a new text file for reading. Sets the variable FS (file stream
- ; number) which is used by other routines to refer to the file. If a file
- ; of the same name already exists, it will be deleted.
- ;
- create:
- dosfunc 0x3c00 @1 @0
- return @0
-
-
- ; WRITE(FS,SEG,OFS,COUNT)
- ;
- ; Writes to an open text file from the given memory location (SEG and OFS)
- ; for the given number of bytes (COUNT). FS is file stream number, set by
- ; APPEND or CREATE.
- ;
- write:
- set cmd @0
- int 0x21 0x4000,@1,@4,@3,,,@2
- if @0&1 doserr
- return @0
-
-
- ; WRITESTR(FS,STRING)
- ;
- ; Writes text (in variable STRING) into a text file at the current position.
- ; FS is file stream number, set by APPEND or CREATE.
- ;
- writestr:
- write @1,seg(@2),ofs(@2),len(@2)
- return @0
-
-
- ; MKDIR(PATH) make a new disk directory
- ;
- mkdir:
- dosfunc 0x3900 @1 @0
- return @0
-
-
- ; RMDIR(PATH) remove a directory (must be empty)
- ;
- rmdir:
- dosfunc 0x3a00 @1 @0
- return @0
-
-
- ; CHDIR(PATH) change the current directory
- ;
- chdir:
- dosfunc 0x3b00 @1 @0
- return @0
-
-
- ; DELETE(FNAME) delete a disk file
- ;
- ; delete file FNAME
- ;
- delete:
- dosfunc 0x4100 @1 @0
- return @0
-
-
- ; RENAME(FNAME1,FNAME2) rename a disk file
- ;
- rename:
- set cmd @0
- int 0x21 0x5600,,,ofs(@1),,ofs(@2),seg(@1),seg(@2)
- if @0&1 doserr
- return 0
-
-
- ; SETDRIVE(DRIVE) change the logged drive (DRIVE is a single letter)
- ;
- setdrive:
- set cmd @0
- int 0x21 0x0e00,,,asc(@1)-asc("a")
- return 0
-
-
- ; VARS(VAR,VAR,VAR,VAR,....) display a list of variables and their values
- ;
- vars:
- local cnt 1
- mark 24
- if @@cnt!=""
- text "@"$@@cnt$"="$@@@cnt$" "
- endif
- set cnt @cnt+1
- loop
- text chr(10)
- return
-
-
- ; RANDOM(LOW,HIGH)
- ;
- ; Generate a random number between LOW and HIGH
- ; Before calling, set up random seed variable RSEED in the calling routine.
- ; One approach is: SET rseed peekl(0x0040006c)
- ;
- random:
- set rseed (@rseed>>13)^@rseed
- set rseed ((@rseed<<18)^@rseed)&0x7fffffff
- return @rseed%((@2-@1)+1)+@1
-