home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fish 'n' More 2
/
fishmore-publicdomainlibraryvol.ii1991xetec.iso
/
fish
/
libraries
/
fileio_463
/
c
/
testfileio.c
< prev
Wrap
C/C++ Source or Header
|
1991-03-09
|
11KB
|
381 lines
/***************************************************************************
Set your editor's TAB width to 3
A C example for using the dissidents requester.library to display and
choose a filename. This program opens the library and sets up two FileIOs
differently to demo various features of the library. Such features are
WB pattern matching, device and volume names, excluding logical assigns,
installing custom handlers, etc. It also explains how to manage 2 FileIOs
for disk IO, and how to initially set up the FileIO via ParsePath.
****************************************************************************/
#include <functions.h>
#include <exec/types.h>
#include <exec/io.h>
#include <stdio.h>
#include <exec/memory.h>
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <graphics/gfxbase.h>
#include "FileIO.h"
/* extern long GetMsg(), OpenWindow(), IoErr(), OpenLibrary(); */
/* === System Global Variables ========================================== */
struct IntuitionBase *IntuitionBase = 0L;
struct GfxBase *GfxBase = 0L;
struct DosLibrary *DosBase = 0L;
struct RequesterBase *RequesterBase = 0L;
struct FileIO *myFileIO = 0L;
struct FileIO *myFileIO2 = 0L;
struct Window *myWindow = 0L;
struct Screen *myScreen = 0L;
ULONG argcount; /* Saves argc from main(). argcount==0, then run from WB. */
struct TextAttr topaz80_font;
void TestFileIO();
struct NewScreen NewFileIOScreen =
{
0, 0, /* LeftEdge, TopEdge */
640, 400, /* Width, Height */
2, /* Depth */
0, 1, /* Detail/BlockPens */
HIRES | LACE, /* ViewPort Modes (must set/clear HIRES as needed) */
CUSTOMSCREEN,
&topaz80_font, /* Font */
(UBYTE *)"Example FileIO Program's Screen",
0L, /* Gadgets */
0L, /* CustomBitMap */
};
struct NewWindow NewFileIOWindow =
{
168, 30, /* LeftEdge, TopEdge */
303, 146, /* Width, Height */
-1, -1, /* Detail/BlockPens */
MENUPICK | MOUSEBUTTONS | CLOSEWINDOW | RAWKEY ,
/* IDCMP Flags */
WINDOWDRAG | WINDOWDEPTH | SIZEBRIGHT | SMART_REFRESH |
WINDOWCLOSE | ACTIVATE | NOCAREREFRESH,
/* Window Specification Flags */
0L, /* FirstGadget */
0L, /* Checkmark */
(UBYTE *)"FileIO Requester Window", /* WindowTitle */
0L, /* Screen */
0L, /* SuperBitMap */
303, 146, /* MinWidth, MinHeight */
600, 200, /* MaxWidth, MaxHeight */
WBENCHSCREEN,
};
struct IntuiText itxt[] = {
{0,1,JAM1,19,1,NULL,(UBYTE *)"Multiple"},
{0,1,JAM1,19,1,NULL,(UBYTE *)"Handler"}, };
struct MenuItem mi[] = {
{&mi[1],0, 0,91,10,CHECKIT | MENUTOGGLE | ITEMTEXT | ITEMENABLED | HIGHCOMP,\
NULL,(APTR)&itxt[0],NULL,NULL},
{NULL,0, 10,91,10,CHECKIT | MENUTOGGLE | ITEMTEXT | ITEMENABLED | HIGHCOMP,\
NULL,(APTR)&itxt[1],NULL,NULL},};
struct Menu myMenu = {NULL,0,0,72,0,MENUENABLED, "Project",&mi[0]};
BOOL myFunc();
struct HandlerBlock hblock = {
/* APTR StartUpCode; */ (APTR)myFunc,
/* APTR DiskInsertedCode; */ 0,
/* APTR GadgetCode; */ 0,
/* APTR KeyCode; */ 0,
/* APTR MouseMoveCode; */ 0 };
/* For the FileIO pathname */
UBYTE fbuffer[204];
/************************ MAIN ROUTINE *****************************/
VOID main(argc, argv)
LONG argc;
char **argv;
{
LONG class;
USHORT code;
struct IntuiMessage *message;
BOOL end;
struct WBStartup *msg;
argcount = argc;
if (!(IntuitionBase = (struct IntuitionBase *)
OpenLibrary("intuition.library", 0L)))
exit_program("FileIO Demo: No intuition library. \n", 1L);
if (!(GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 0L)))
exit_program("FileIO Demo: No graphics library. \n", 2L);
/* NOW OPEN THE REQUESTER LIBRARY */
if (!(RequesterBase = (struct RequesterBase *)OpenLibrary("requester.library", 1L)))
exit_program("FileIO Demo: No requester.library. \n", 4L);
if (argv)
{
/* OK, we started from CLI. If user supplied any args, open the file
requester on a HIRES, custom screen. Otherwise, open on WB screen. */
if (argc > 1)
{
if (myScreen = OpenScreen(&NewFileIOScreen))
{
NewFileIOWindow.Screen = myScreen;
NewFileIOWindow.Type = CUSTOMSCREEN;
}
}
}
if (!(myWindow = (struct Window *) OpenWindow( &NewFileIOWindow ) ))
exit_program("FileIO Demo: Null Window.\n", 5L);
SetMenuStrip( myWindow, &myMenu );
/* GET 2 FileIO STRUCTURES */
if (!(myFileIO = GetFileIO() ))
exit_program("FileIO Demo: No FileIO 1.\n", 6L);
if (!(myFileIO2 = GetFileIO() ))
exit_program("FileIO Demo: No FileIO 2.\n", 7L);
/* Set up the XY co-ordinates where the requester should open */
myFileIO->X = 6;
myFileIO->Y = 11;
myFileIO2->X = 6;
myFileIO2->Y = 11;
/* Set default colors and DrawMode. This you must do! */
myFileIO->DrawMode = JAM2;
myFileIO->PenA = 1;
myFileIO->PenB = 0;
myFileIO2->DrawMode = JAM2;
myFileIO2->PenA = 1;
myFileIO2->PenB = 0;
/* Setup buffers for pathname. In this example, we'll just use 1 buffer,
* but if you're using 2 fileios, then you probably need two buffers.
*/
myFileIO->Buffer = fbuffer;
myFileIO2->Buffer = fbuffer;
/* pretty easy to set up, eh? */
/* Set up the flags for each FileIO differently to demo some features */
SetFlag(myFileIO2->Flags, WBENCH_MATCH | MATCH_OBJECTTYPE);
myFileIO2->MatchType = WBTOOL;
SetFlag(myFileIO2->Flags, USE_DEVICE_NAMES | EXCLUDE_ASSIGNS);
/* Setup some custom handlers for the first fileio */
myFileIO->Custom = &hblock;
SetFileIOHandlers( &hblock );
/*
Note: if we expected to receive a filename from the user as one of the
argv arguments, we should call requester lib's ParentPath() now, passing
that arg. For example, assume that the user typed at the CLI:
TestProgram df0:mydir/blort
where df0:mydir/blort is some data file that TestProgram should load. We
would now call:
*/
if (argv)
{
/* OK, we started from CLI. Assume that the first arg is some data file
to load.
*/
if (argc > 1)
{
ParentPath(myFileIO, argv[1]);
/* Here we would load the filename in the FileIO's buffer */
}
else
{
/* Set the FileIO to first open in the current directory */
ParentPath(myFileIO,0L);
}
}
else
{
/* From Workbench. Assume first WBarg is the icon for a data file to load */
if( !(argv) )
{
if( argv->sm_NumArgs > 1 )
{
arg = argv->sm_ArgList;
arg++;
ParentPath(myFileIO, arg->wa_Name);
/* here we would load the filename in the FileIO's buffer */
}
else
{
/* Set the FileIO to first open in the current directory */
ParentPath(myFileIO,0L);
}
}
/*
This will copy "df0:" to the FileIO's Diskname[], "mydir" to the Drawername[],
and "blort" to the Filename[]. It will also set the FREEBYTES and FILESIZE
fields so that you know how much room is on the disk and whether the file
exists (FileIO->FileSize != 0). So, you can use the same load routine that
you would use for loading a data file after calling DoFileIO(). This also
ensures that the returned path is complete up to the root dir (so you never
need bother with changing dirs within your program).
Because we're using two FileIOs for disk operation in this example. One of
them has to be used with DoFileIO, DoFileIOWindow, ParentPath, ParentLock,
or ParseString first. By setting up FileIO #1 via ParentPath, we know that
this is the first. We'll then free it last. Note that if you have multiple
FileIOs but the others are being used only for SPECIAL_REQ or with Prompt-
UserEntry(), then you don't have to worry about this.
Note that if your program does not expect to receive a passed arg, then you
simply:
ParentPath(myFileIO,0L);
*/
end = FALSE;
while (end == FALSE)
{
WaitPort(myWindow->UserPort);
while (message = ( struct IntuiMessage *)GetMsg(myWindow->UserPort))
{
code = message->Code;
class = message->Class;
ReplyMsg(message);
switch (class)
{
case CLOSEWINDOW:
end = TRUE;
break;
case MENUPICK:
switch( ITEMNUM( code ) )
{
case 0:
ToggleFlag( myFileIO->Flags, MULTIPLE_FILES );
TestFileIO(myFileIO, myWindow)
break;
case 1:
ToggleFlag( myFileIO->Flags, CUSTOM_HANDLERS );
TestFileIO(myFileIO, myWindow))
break;
}
break;
case MOUSEBUTTONS:
TestFileIO(myFileIO, myWindow)
break;
case RAWKEY:
TestFileIO(myFileIO2, myWindow)
break;
default:
break;
}
}
}
exit_program( 0L, 0L);
}
exit_program( error_words, error_code ) /* All exits through here. */
char * error_words;
ULONG error_code;
{
if( argcount && error_words ) puts( error_words );
if (myWindow) CloseWindow(myWindow);
if (myScreen) CloseScreen(myScreen);
if (IntuitionBase) CloseLibrary(IntuitionBase);
if (GfxBase) CloseLibrary(GfxBase);
if (DosBase) CloseLibrary(DosBase);
if (RequesterBase)
{
ReleaseFileIO(myFileIO2); /* safe to pass a null address */
ReleaseFileIO(myFileIO); /* Note that we free FileIO #1 last
because it was the first one used for
disk IO. When using more than 1 fileio
for disk IO, this should be done. FileIOs
used only for SPECIAL_REQ don't matter.
*/
CloseLibrary(RequesterBase);
}
exit( error_code );
}
void TestFileIO(fileio, wind)
struct FileIO *fileio;
struct Window *wind;
{
UBYTE *address;
struct FileEntry *fentry1 = 0, *fentry2 = 0;
/* This guy calls DoFileIO(), displays the file name selected by the
* user, and returns TRUE if the user swapped disks during DoFileIO()
* (else returns FALSE) if an error, CANCEL, or no disk swap.
*/
address = DoFileIO(fileio, wind);
if( address == fbuffer )
{
/* If user was positive and no error, display the (last selected) name */
AutoMessage( fbuffer, wind );
/* If MULTIPLE_FILES, display all of the selected ones, including last */
if( fileio->Flags & MULTIPLE_FILES )
{
while( fentry2 = RetrieveEntry( &fentry1, fileio ) )
{
AutoMessage( fentry2->filePart->EntryString, wind );
}
}
}
if (!address) AutoMessage("Error in operation", wind);
if (address==-1) AutoMessage("Cancelled operation", wind);
}
/* This is automatically called by the library as soon as the requester is
first displayed. Note that this could actually return one of three values:
1 = don't redo the filename list, i.e. NO_CARE_REDRAW
0 = redo the filename list, i.e. relist the current dir (ignored if SPECIAL_REQ)
-1 = end the requester
But we only want the user to choose 0 or 1 here.
*/
BOOL myFunc( extra,seconds,micros,mouseX,mouseY,fileio,xwind,iaddress )
ULONG extra;
APTR iaddress;
struct Window *xwind;
struct FileIO *fileio;
SHORT mouseY, mouseX;
ULONG micros,seconds;
{
return(AutoPrompt3( "This is a custom StartHandler",0,0, xwind ));
}