home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / disks / disk463.lzh / FileIO / C / TestFileIO.c < prev   
C/C++ Source or Header  |  1991-03-09  |  11KB  |  381 lines

  1. /***************************************************************************
  2.   Set your editor's TAB width to 3
  3.  
  4.   A C example for using the dissidents requester.library to display and
  5.   choose a filename. This program opens the library and sets up two FileIOs
  6.   differently to demo various features of the library. Such features are
  7.   WB pattern matching, device and volume names, excluding logical assigns,
  8.   installing custom handlers, etc. It also explains how to manage 2 FileIOs
  9.   for disk IO, and how to initially set up the FileIO via ParsePath.
  10.  
  11. ****************************************************************************/
  12.  
  13. #include <functions.h>
  14. #include <exec/types.h>
  15. #include <exec/io.h>
  16. #include <stdio.h>
  17. #include <exec/memory.h>
  18. #include <intuition/intuition.h>
  19. #include <intuition/intuitionbase.h>
  20. #include <graphics/gfxbase.h>
  21. #include "FileIO.h"
  22.  
  23. /* extern long GetMsg(), OpenWindow(), IoErr(), OpenLibrary(); */
  24.  
  25. /* === System Global Variables ========================================== */
  26. struct IntuitionBase    *IntuitionBase = 0L;
  27. struct GfxBase            *GfxBase  = 0L;
  28. struct DosLibrary        *DosBase  = 0L;
  29. struct RequesterBase    *RequesterBase = 0L;
  30. struct FileIO            *myFileIO  = 0L;
  31. struct FileIO            *myFileIO2 = 0L;
  32. struct Window            *myWindow  = 0L;
  33. struct Screen            *myScreen  = 0L;
  34. ULONG  argcount;  /* Saves argc from main(). argcount==0, then run from WB. */
  35.  
  36. struct TextAttr  topaz80_font;
  37.  
  38. void TestFileIO();
  39.  
  40. struct NewScreen NewFileIOScreen =
  41.     {
  42.     0, 0,                /* LeftEdge, TopEdge */
  43.     640, 400,        /* Width, Height */
  44.     2,                    /* Depth */
  45.     0, 1,                /* Detail/BlockPens */
  46.     HIRES | LACE,    /* ViewPort Modes (must set/clear HIRES as needed) */
  47.     CUSTOMSCREEN,
  48.     &topaz80_font,    /* Font */
  49.     (UBYTE *)"Example FileIO Program's Screen",
  50.     0L,                /* Gadgets */
  51.     0L,                /* CustomBitMap */
  52.     };
  53.  
  54. struct NewWindow NewFileIOWindow =
  55.     {
  56.     168, 30,                /* LeftEdge, TopEdge */
  57.     303, 146,            /* Width, Height */
  58.     -1, -1,                /* Detail/BlockPens */
  59.     MENUPICK | MOUSEBUTTONS | CLOSEWINDOW | RAWKEY ,
  60.                             /* IDCMP Flags */
  61.     WINDOWDRAG | WINDOWDEPTH | SIZEBRIGHT | SMART_REFRESH |
  62.     WINDOWCLOSE | ACTIVATE | NOCAREREFRESH,
  63.                             /* Window Specification Flags */
  64.     0L,                    /* FirstGadget */
  65.     0L,                    /* Checkmark */
  66.     (UBYTE *)"FileIO Requester Window",  /* WindowTitle */
  67.     0L,                    /* Screen */
  68.     0L,                    /* SuperBitMap */
  69.     303, 146,            /* MinWidth, MinHeight */
  70.     600, 200,            /* MaxWidth, MaxHeight */
  71.     WBENCHSCREEN,
  72.     };
  73.  
  74.  
  75. struct IntuiText itxt[] = { 
  76.     {0,1,JAM1,19,1,NULL,(UBYTE *)"Multiple"},
  77.     {0,1,JAM1,19,1,NULL,(UBYTE *)"Handler"}, };
  78.  
  79. struct MenuItem mi[] = {
  80.     {&mi[1],0, 0,91,10,CHECKIT | MENUTOGGLE | ITEMTEXT | ITEMENABLED | HIGHCOMP,\
  81.     NULL,(APTR)&itxt[0],NULL,NULL},
  82.  
  83.     {NULL,0, 10,91,10,CHECKIT | MENUTOGGLE | ITEMTEXT | ITEMENABLED | HIGHCOMP,\
  84.     NULL,(APTR)&itxt[1],NULL,NULL},};
  85.  
  86. struct Menu myMenu = {NULL,0,0,72,0,MENUENABLED,  "Project",&mi[0]};
  87.  
  88.  
  89. BOOL myFunc();
  90.  
  91.  
  92. struct HandlerBlock hblock = {
  93.     /* APTR  StartUpCode; */        (APTR)myFunc,
  94.     /* APTR  DiskInsertedCode; */    0, 
  95.     /* APTR  GadgetCode; */            0, 
  96.     /* APTR  KeyCode; */                0,
  97.     /* APTR  MouseMoveCode; */        0 };
  98.  
  99. /* For the FileIO pathname */
  100. UBYTE fbuffer[204];
  101.  
  102.  
  103. /************************ MAIN ROUTINE *****************************/
  104.  
  105. VOID main(argc, argv)
  106. LONG argc;
  107. char **argv;
  108. {
  109.     LONG        class;
  110.     USHORT    code;
  111.     struct    IntuiMessage *message;
  112.     BOOL        end;
  113.     struct    WBStartup    *msg;
  114.  
  115.     argcount = argc;
  116.  
  117.     if (!(IntuitionBase = (struct IntuitionBase *)
  118.         OpenLibrary("intuition.library", 0L)))
  119.         exit_program("FileIO Demo: No intuition library. \n", 1L);
  120.  
  121.     if (!(GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 0L)))
  122.         exit_program("FileIO Demo: No graphics library. \n", 2L);
  123.  
  124.     /* NOW OPEN THE REQUESTER LIBRARY */
  125.     if (!(RequesterBase = (struct RequesterBase *)OpenLibrary("requester.library", 1L)))
  126.         exit_program("FileIO Demo: No requester.library. \n", 4L);
  127.  
  128.     if (argv)
  129.     {
  130.         /* OK, we started from CLI. If user supplied any args, open the file
  131.             requester on a HIRES, custom screen. Otherwise, open on WB screen. */
  132.         if (argc > 1)
  133.         {
  134.             if (myScreen = OpenScreen(&NewFileIOScreen))
  135.             {
  136.                 NewFileIOWindow.Screen = myScreen;
  137.                 NewFileIOWindow.Type = CUSTOMSCREEN;
  138.             }
  139.         }
  140.     }
  141.  
  142.     if (!(myWindow = (struct Window *) OpenWindow( &NewFileIOWindow ) ))
  143.         exit_program("FileIO Demo: Null Window.\n", 5L);
  144.  
  145.     SetMenuStrip( myWindow, &myMenu );
  146.  
  147.     /* GET 2 FileIO STRUCTURES */
  148.     if (!(myFileIO = GetFileIO() ))
  149.         exit_program("FileIO Demo: No FileIO 1.\n", 6L);
  150.     if (!(myFileIO2 = GetFileIO() ))
  151.         exit_program("FileIO Demo: No FileIO 2.\n", 7L);
  152.  
  153.     /* Set up the XY co-ordinates where the requester should open */
  154.     myFileIO->X = 6;
  155.     myFileIO->Y = 11;
  156.     myFileIO2->X = 6;
  157.     myFileIO2->Y = 11;
  158.  
  159.     /* Set default colors and DrawMode. This you must do! */
  160.     myFileIO->DrawMode = JAM2;
  161.     myFileIO->PenA = 1;
  162.     myFileIO->PenB = 0;
  163.     myFileIO2->DrawMode = JAM2;
  164.     myFileIO2->PenA = 1;
  165.     myFileIO2->PenB = 0;
  166.  
  167.     /* Setup buffers for pathname. In this example, we'll just use 1 buffer,
  168.      *    but if you're using 2 fileios, then you probably need two buffers.
  169.      */
  170.     myFileIO->Buffer = fbuffer;
  171.     myFileIO2->Buffer = fbuffer;
  172.  
  173.             /* pretty easy to set up, eh?  */
  174.  
  175.     /* Set up the flags for each FileIO differently to demo some features */
  176.     SetFlag(myFileIO2->Flags, WBENCH_MATCH | MATCH_OBJECTTYPE);
  177.     myFileIO2->MatchType = WBTOOL;
  178.     SetFlag(myFileIO2->Flags, USE_DEVICE_NAMES | EXCLUDE_ASSIGNS);
  179.  
  180.     /* Setup some custom handlers for the first fileio */
  181.     myFileIO->Custom = &hblock;
  182.     SetFileIOHandlers( &hblock );
  183.  
  184. /*
  185.    Note: if we expected to receive a filename from the user as one of the
  186.    argv arguments, we should call requester lib's ParentPath() now, passing
  187.    that arg. For example, assume that the user typed at the CLI:
  188.  
  189. TestProgram  df0:mydir/blort
  190.  
  191.    where df0:mydir/blort is some data file that TestProgram should load. We
  192.    would now call:
  193. */
  194.  
  195.     if (argv)
  196.     {
  197.         /* OK, we started from CLI. Assume that the first arg is some data file
  198.             to load.
  199.         */
  200.         if (argc > 1)
  201.         {
  202.             ParentPath(myFileIO, argv[1]);
  203.             /* Here we would load the filename in the FileIO's buffer */
  204.         }
  205.         else
  206.         {
  207.             /* Set the FileIO to first open in the current directory */
  208.             ParentPath(myFileIO,0L);
  209.         }
  210.     }
  211.     else
  212.     {
  213.         /* From Workbench. Assume first WBarg is the icon for a data file to load */
  214.         if( !(argv) )
  215.         {
  216.             if( argv->sm_NumArgs > 1 )
  217.             {
  218.                 arg = argv->sm_ArgList;
  219.                 arg++;
  220.                 ParentPath(myFileIO, arg->wa_Name);
  221.                 /* here we would load the filename in the FileIO's buffer */
  222.             }
  223.         else
  224.         {
  225.             /* Set the FileIO to first open in the current directory */
  226.             ParentPath(myFileIO,0L);
  227.         }
  228.     }
  229.  
  230. /*
  231.    This will copy "df0:" to the FileIO's Diskname[], "mydir" to the Drawername[],
  232.    and "blort" to the Filename[]. It will also set the FREEBYTES and FILESIZE
  233.    fields so that you know how much room is on the disk and whether the file
  234.    exists (FileIO->FileSize != 0). So, you can use the same load routine that
  235.    you would use for loading a data file after calling DoFileIO(). This also
  236.    ensures that the returned path is complete up to the root dir (so you never
  237.    need bother with changing dirs within your program).
  238.  
  239.     Because we're using two FileIOs for disk operation in this example. One of
  240.    them has to be used with DoFileIO, DoFileIOWindow, ParentPath, ParentLock,
  241.    or ParseString first. By setting up FileIO #1 via ParentPath, we know that
  242.    this is the first. We'll then free it last. Note that if you have multiple
  243.     FileIOs but the others are being used only for SPECIAL_REQ or with Prompt-
  244.     UserEntry(), then you don't have to worry about this.
  245.  
  246.     Note that if your program does not expect to receive a passed arg, then you
  247.     simply:
  248.  
  249.     ParentPath(myFileIO,0L);
  250.  
  251. */
  252.  
  253.     end = FALSE;
  254.     while (end == FALSE)
  255.     {
  256.         WaitPort(myWindow->UserPort);
  257.  
  258.         while (message = ( struct IntuiMessage *)GetMsg(myWindow->UserPort))
  259.         {
  260.             code = message->Code;
  261.             class = message->Class;
  262.             ReplyMsg(message);
  263.  
  264.             switch (class)
  265.             {
  266.                 case CLOSEWINDOW:
  267.                     end = TRUE;
  268.                     break;
  269.  
  270.                 case MENUPICK:
  271.                     switch( ITEMNUM( code ) )
  272.                     {
  273.                         case 0: 
  274.                             ToggleFlag( myFileIO->Flags, MULTIPLE_FILES );
  275.                             TestFileIO(myFileIO, myWindow)
  276.                             break;
  277.  
  278.                         case 1: 
  279.                             ToggleFlag( myFileIO->Flags, CUSTOM_HANDLERS );
  280.                             TestFileIO(myFileIO, myWindow))
  281.                             break;
  282.                     }    
  283.                     break;
  284.  
  285.                 case MOUSEBUTTONS:
  286.                     TestFileIO(myFileIO, myWindow)
  287.                     break;
  288.  
  289.                 case RAWKEY:
  290.                     TestFileIO(myFileIO2, myWindow)
  291.                     break;
  292.  
  293.                 default:
  294.                     break;
  295.             }
  296.         }
  297.     }
  298.     exit_program( 0L, 0L);
  299. }
  300.  
  301.  
  302. exit_program( error_words, error_code )      /* All exits through here. */
  303. char * error_words;
  304. ULONG error_code;
  305. {
  306.     if( argcount && error_words ) puts( error_words );
  307.  
  308.     if (myWindow) CloseWindow(myWindow);
  309.     if (myScreen) CloseScreen(myScreen);
  310.  
  311.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  312.     if (GfxBase)       CloseLibrary(GfxBase);
  313.     if (DosBase)       CloseLibrary(DosBase);
  314.     if (RequesterBase)
  315.     {
  316.         ReleaseFileIO(myFileIO2);    /* safe to pass a null address */
  317.         ReleaseFileIO(myFileIO);    /* Note that we free FileIO #1 last
  318.                                                 because it was the first one used for
  319.                                                 disk IO. When using more than 1 fileio
  320.                                                 for disk IO, this should be done. FileIOs
  321.                                                 used only for SPECIAL_REQ don't matter.
  322.                                              */
  323.         CloseLibrary(RequesterBase);
  324.     }
  325.     exit( error_code );
  326. }
  327.  
  328.  
  329.  
  330. void TestFileIO(fileio, wind)
  331. struct FileIO *fileio;
  332. struct Window *wind;
  333. {
  334.     UBYTE *address;
  335.     struct FileEntry *fentry1 = 0, *fentry2 = 0;
  336.  
  337. /* This guy calls DoFileIO(), displays the file name selected by the
  338.  * user, and returns TRUE if the user swapped disks during DoFileIO()
  339.  * (else returns FALSE) if an error, CANCEL, or no disk swap.
  340.  */
  341.  
  342.     address = DoFileIO(fileio, wind);
  343.  
  344.     if( address == fbuffer )
  345.     {
  346.     /* If user was positive and no error, display the (last selected) name */
  347.         AutoMessage( fbuffer, wind );
  348.  
  349.         /* If MULTIPLE_FILES, display all of the selected ones, including last */
  350.         if( fileio->Flags & MULTIPLE_FILES )
  351.         {
  352.             while( fentry2 = RetrieveEntry( &fentry1, fileio ) )
  353.             {
  354.                 AutoMessage( fentry2->filePart->EntryString, wind );
  355.             }
  356.         }
  357.    }
  358.  
  359.     if (!address)        AutoMessage("Error in operation", wind);
  360.     if (address==-1)    AutoMessage("Cancelled operation", wind);
  361. }
  362.  
  363.  
  364. /* This is automatically called by the library as soon as the requester is
  365.     first displayed. Note that this could actually return one of three values:
  366.     1 = don't redo the filename list, i.e. NO_CARE_REDRAW
  367.     0 = redo the filename list, i.e. relist the current dir (ignored if SPECIAL_REQ)
  368.     -1 = end the requester
  369.     But we only want the user to choose 0 or 1 here.
  370. */
  371. BOOL myFunc( extra,seconds,micros,mouseX,mouseY,fileio,xwind,iaddress )
  372. ULONG    extra;
  373. APTR    iaddress;
  374. struct Window *xwind;
  375. struct FileIO *fileio;
  376. SHORT    mouseY, mouseX;
  377. ULONG    micros,seconds;
  378. {
  379.     return(AutoPrompt3( "This is a custom StartHandler",0,0, xwind ));
  380. }
  381.