home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Computing 59
/
ac059.adf
/
SupportFiles
/
files.doc
/
files.doc
Wrap
Text File
|
1993-02-05
|
8KB
|
132 lines
FILING SYSTEM
No less than three demonstration programs are on the support disc this time!
The first two explain to new Assembler and 'C' programmers how to read
and write files from a disc. The third demo program contains some more
advanced features that might interest regular readers of this page.
COMMAND OF THE LANGUAGE
The last two programming pages introduced the Amiga Library, with routines
that can be used in 'C' and Assembler programs. C programmers passed the input
parameters to the routine (as to any other function), and any result was
returned. Assembler programmers put the inputs in the appropriate registers,
and called the routine as if it was a subroutine. Any return values came back
in register D0. The short demo programs opened the Dos library, for the
Output() and Write() commands to write a message to the screen.
The current demo programs use Dos library routines to read data from an ASCII
file in the current directory, and write data to a similar file. Two Exec
library routines are needed to allocate a block of memory to store this data,
and to free the memory afterwards for general use.
AllocMem(bytesize,requirements) (D0,D1) 'puts by' an area of memory
'bytesize' is the number of bytes wanted
'requirements' is the type of memory wanted
If the command is successful, a pointer to a block of memory is returned.
The memory block will be available for you, and not overwritten by another
program when multitasking.
FreeMem(memorypointer,bytesize) (A1,D0) frees the block of memory
'memorypointer' is a pointer to a block of allocated memory
'bytesize' is the number of bytes to be freed
Open(name,mode) (D1,d2) opens the chosen file
'name' is the name of the file
'mode' is the access mode
A pointer to a 'filehandle' or block of information for the file, is returned
(the system uses the block of information, when processing the file).
Read(filehandle,buffer,length) (D1,D2,D3) will read from a file
'filehandle' is its filehandle pointer
'buffer' is a pointer to the memory area to hold the data
'length' is the number of bytes to be read
The number of bytes successfully read, is returned.
Close(filehandle) (D1) closes the file
'filehandle' is its filehandle pointer
INCLUSION POLICY
The main compiler (or assembler) header directory contains a subdirectory of
'header files' for each library. These files contain useful macros and
definitions for some of the library functions. A header file can be 'included'
with any source code.
The file 'exec/memory.h' (C) or 'exec/memory.i' (Assembler) is included
with the source code as it contains the memory requirement definitions for
use with the AllocMem() command. The program asks for memory that is
'public', 'clear' but not necessarily 'chip'.
The file 'libraries/dos.h' or 'libraries/dos.i' is used for the access mode
definitions when opening a file. The file to be read (limerick) is opened
using MODE_OLDFILE, which opens an existing file for reading or writing.
The file to be written (limerick2) is opened using MODE_NEWFILE which will
overwrite a file if it already exists, and open a new one otherwise.
*****************************************************************************
THE ASSEMBLER DEMO PROGRAM
The complete program source code listing (afiles.a) is available on the
support disc. The files "exec/types.i" has been included as it contains
definitions used by "libraries/dos.i" and "exec/memory.i". Instead of having
all the code in the main program, groups of commands have been built into
separate subroutines. The main program checks the value returned from each
subroutine to see whether the subroutine is successful. If all is well the
program can continue, otherwise it tidies up and exits gracefully. The
cleanup routine closes any open files or libraries, and frees any allocated
memory. It also calls a 'message' subroutine, entering the routine with the
a pointer to the message data in register D2, and the length of the message
in register D3. The message subroutine only has to put the filehandle of the
console in register D1, the address of the Dos library in A6, and call the
Amiga library Write() routine to display the data on the screen.
When the object program (afiles) is run, the program opens the
DOS library. If the library opens successfully, the program asks the Amiga
to allocate a memory buffer for 152 bytes of data. If the memory is
forthcoming, the program can open a short ASCII text file ('limerick') in
the current directory, and read the text into the memory buffer.
If the data has been read successfully, the program opens a new file,
('limerick2'), writes the text 'This is a new file' into it, and then apends
the text from the memory buffer. The program writes the text from the
buffer to the screen, and waits for a few seconds. The program can then
display a message of success on the screen, tidy up, and exit.
******************************************************************************
THE C PROGRAM
The support disc contains the program source code listing (files.c) and the
object code (files). The source code includes "libraries/dos.h" and
"exec/memory.h" for some of its definitions. Some of the commands have been
grouped into functions which are called from the main program. The functions
are listed separately after the main program but their 'prototypes' are
declared earlier. The main program is then aware of the name of each function
and the datatype of its return value.
The 'cleanup' function has a parameter passed to it, a pointer to a string
of text. The function displays the text on the screen, tidies up the files,
libraries, and allocated memory. The main program can call this function with
a suitable message for all occaisions.
When the object program (files) is run, the program opens the library, and
allocates a memory buffer. The short text file is read into the buffer, and
the contents displayed on the screen. After a short delay a text message and
the buffer contents are written to the 'limerick2' file, and the program
tidies itself up and exits.
*****************************************************************************
ADVANCED FEATURES
When a file is opened for reading, information about the file is put into
a parameter block in memory, called a 'filehandle', and a pointer to a
FileHandle structure is returned. When the file is first opened, the
read/write position, or file pointer, will be at the beginning of the file.
When a number of bytes of information are read or written, the file pointer
will move a corresponding number of bytes. If you do not want any other
process to interfere with the file, you can open the file with access mode
MODE_READWRITE. This access mode opens an existing file for reading or writing,
but locks the file so that as long as it is open, no other process on the
multitasking Amiga can access it.
It is also possible to lock files, without opening them for reading
or writing. This can be especially useful for a file requester program,
it can obtain a lock on all the available files and directories, and ensure
that they remain undisturbed.
LOCK UP YOUR FILES
The source code (advfiles.c) and the object code (advfiles) are both available
on the support disc. When the program is run it opens the Dos library,
and opens the text file 'limerick' in MODE_READWRITE.
To demonstrate this, 'advfiles' calls the 'files' program (Process 1) which
is unable to open the 'limerick' text file for reading. After 'advfiles'
closes the file, it calls the 'files' program again (Process 2) which can
successfully read the 'limerick' file and write the 'limerick2' file.
'Advfiles' obtains a lock for the second file, 'limerick2'. The 'files'
program is called (Process 3) and there will be a message to the effect that
the 'limerick2' file cannot be written. When the file is unlocked and the
'files' program called again (Process 4) the files can be read and written
successfully.
******************************************************************************