home *** CD-ROM | disk | FTP | other *** search
/ MACD 4 / MACD4.iso / cdity / findfiles.lha / FindFile / FFSource.lha / FindFile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-03  |  13.9 KB  |  610 lines

  1. /************************************************************************************
  2.                     FindFile
  3.  
  4.             An AmigaDOS Release 2 Commodity
  5.  
  6.             Code By :     Russell R. Steffen
  7.  
  8.             Home Address :     11551 Sherman Rd.
  9.                      Cedarburg WI, 53012
  10.                      USA
  11.                      ( Stable )
  12.  
  13.             Email Address  : STEFFENR@UWSTOUT.EDU
  14.                      ( Valid ubtil May 1993 )
  15.  
  16.             School Address : 318 Callahan Hall
  17.                      Menomonie, WI 54751
  18.                      USA
  19.                      ( Valid until May 1993 )
  20.  
  21.  
  22.       Purpose -- To Emulate the FindFile Desk Accessory of the Macintosh.
  23.  
  24.       Legal   -- This code is copyrighted by the author. However permission is
  25.          granted for private use. Commercial use is prohibited without
  26.          written consent of the author. This program is distrbuted
  27.          WITHOUT ANY WARRANTY or IMPLIED WARRANTY of MERCHANTABILTY
  28.          or FITNESS FOR A PARTICULAR PURPOSE.
  29.  
  30.  
  31.       CopyRight © 1992 -- Russell R. Steffen. All rights reserved.
  32.  
  33.       History
  34.       -----------------------------------------------------------------------------
  35.       29 JUN 1992 | File Creation, Inital Version
  36.       -----------------------------------------------------------------------------
  37.       26 JUL 1992 | Competetion of Inital Version 1.00
  38.           | Implemented ToolTypes : CX_POPKEY, CX_PRIORITY, CX_POPUP,
  39.           |                DEF_PATTERN, CENTERWINDOW, IGNOREICONS
  40.       -----------------------------------------------------------------------------
  41.       30 JUL 1992 | Optimizations and cleanups. Added SORTLIST Tooltype
  42.           | Version 1.00a
  43.       -----------------------------------------------------------------------------
  44.       31 JUL 1992 | Added key command for found-list gadget. Version 1.00b
  45.           | Fixed bug in ScanDir(), wasn't honoring a user quit request
  46.           | properly.
  47.       -----------------------------------------------------------------------------
  48.       04 NOV 1992 | Fixed another bug in ScanDir() with a near total rewrite.
  49.           | Now works properly when launched from Workbench.
  50.           | Fixed Window opening routine to use WA_InnerWidth and
  51.           | and WA_InnerHeight. Window was a bit to small when the
  52.           | prefs screen font was larger than 8 point.
  53.           | Fixed problem with 'D' key, wasn't wraping around on
  54.           | the device list.
  55.           | Version 1.00c
  56.       -----------------------------------------------------------------------------
  57.       26 FEB 1993 | Porting Source into the RCS environment. Version numbers
  58.           | and history log will appear in that format from now
  59.           | on.
  60.       -----------------------------------------------------------------------------
  61.  
  62. FindFile.c 1.14 1993/04/03 14:01:27 RUSS Exp
  63.  
  64. 1.14
  65.  
  66. FindFile.c
  67.  * Revision 1.14  1993/04/03  14:01:27  RUSS
  68.  * Added INITIAL_DIR tooltype.
  69.  *
  70.  * Revision 1.13  1993/04/03  13:34:06    RUSS
  71.  * Fixed crashing problem when starting a second FindFile.
  72.  *
  73.  * Revision 1.12  1993/04/03  02:02:24    RUSS
  74.  * Changed Deg cyclegadget to string gadget,
  75.  *   added file requester gadget.
  76.  *
  77.  * Revision 1.11  1993/04/01  22:11:33    RUSS
  78.  * Added date to window title and CX description
  79.  *
  80.  * Revision 1.10  1993/04/01  21:47:17    RUSS
  81.  * Major. Changed window format to single list
  82.  * gagdet for found files. Changed default value
  83.  * for CX_POPUP tooltype.
  84.  *
  85.  * Revision 1.9  1993/03/28  14:44:47  RUSS
  86.  * Added sanity checking on memory allocs
  87.  *
  88.  * Revision 1.8  1993/02/28  00:59:03  RUSS
  89.  * Removed DOS interface to FFSearch.c
  90.  *
  91.  * Revision 1.7  1993/02/28  00:39:57  RUSS
  92.  * Changed controlpanel to use arrays of gadgetpointers
  93.  *
  94.  * Revision 1.6  1993/02/27  18:48:18  RUSS
  95.  * Removed CX code, placed it into CX.c
  96.  *
  97.  * Revision 1.5  1993/02/27  17:20:42  RUSS
  98.  * Window management exported to FFWindow.c
  99.  *
  100.  * Revision 1.4  1993/02/27  17:06:42  RUSS
  101.  * Fixed internal version string
  102.  *
  103.  * Revision 1.3  1993/02/27  17:00:41  RUSS
  104.  * Fixed window title
  105.  *
  106.  * Revision 1.2  1993/02/27  16:57:02  RUSS
  107.  * Fixed Commodites Broker init string
  108.  *
  109.  * Revision 1.1  1993/02/27  15:48:24  RUSS
  110.  * Initial revision
  111.  *
  112.  
  113.  
  114. **********************************************************************************/
  115.  
  116. #include "FindFile.h"
  117.  
  118. /***************************** Global Vars ***************************************/
  119.  
  120. struct Library *IntuitionBase;
  121. struct Library *GadToolsBase;
  122. struct Library *CxBase;
  123. struct Library *IconBase;
  124. struct Library *AslBase;
  125.  
  126. extern int Enable_Abort; /* needed to disable Aztec C's ^C checking */
  127.  
  128. static const UBYTE *verTag = "\0$VER: " NAME " 1.14 " __AMIGADATE__;
  129. const UBYTE *windowTitle = NAME " 1.14 " __AMIGADATE__;
  130. const UBYTE *cxTitle = NAME " 1.14 "__AMIGADATE__;
  131.  
  132. /** This TextAttr should be gone in a future release. I want to
  133.     write the window code to be Prefs font sensitive, eventually :)  **/
  134.  
  135. struct TextAttr DefaultFont = {
  136.   (UBYTE *)"topaz.font",
  137.   8,
  138.   FS_NORMAL,
  139.   FPF_ROMFONT
  140. };
  141.  
  142. /**************************** Code Starts Here ***********************************/
  143.  
  144. struct Process;
  145. struct WBStartup;
  146.  
  147.  
  148. void
  149. main(int argc, char **argv)
  150. {
  151.   struct controlPanel *cp;
  152.  
  153.   Enable_Abort = 0; /* turn off auto CTRL-C handling */
  154.  
  155.   cp = AllocVec( sizeof( struct controlPanel ) , MEMF_PUBLIC | MEMF_CLEAR );
  156.  
  157.   if( !cp )
  158.     Shutdown( cp, 20 );
  159.  
  160.   DB("Got controlpanel")
  161.  
  162.   Init( cp );
  163.  
  164.   DB("Initialized.")
  165.   SetUp( cp , argc , argv );
  166.  
  167.   DB("Set up compete.");
  168.  
  169.   FOREVER
  170.   {
  171.     ULONG mask;
  172.  
  173.     mask = GetWaitMask( cp );
  174.  
  175.     DB("Waiting..")
  176.  
  177.     mask = Wait( mask );
  178.  
  179.     if( mask & SIGBREAKF_CTRL_C )
  180.       CMD_Quit( cp );
  181.  
  182.     if( cp->ffWindow )
  183.       if( mask & 1 << cp->ffWindow->UserPort->mp_SigBit )
  184.     HandleFFWindowCommand( cp );
  185.  
  186.     if( cp->cxInterface.cxPort )
  187.       if( mask & 1 << cp->cxInterface.cxPort->mp_SigBit )
  188.     HandleCxCommand( cp );
  189.   }
  190.  
  191. }
  192.  
  193. void
  194. SetUp( struct controlPanel *cp, int argc, char **argv )
  195. {
  196.   UBYTE    **toolTypes;
  197.   STRPTR     strTemp;
  198.   BOOL         openWindow=FALSE;
  199.  
  200.  
  201.  
  202.   /**** Parse WB & CLI Options ***************/
  203.  
  204.   toolTypes = ArgArrayInit( argc, argv );
  205.  
  206.   cp->cxInterface.cxPriority = ArgInt( toolTypes, "CX_PRIORITY", 0 );
  207.  
  208.   strTemp = ArgString( toolTypes, "CX_POPKEY", "LCommand help" );
  209.   strncpy( cp->cxInterface.cxHotKeyString , strTemp, MAXSTR );
  210.  
  211.   strTemp = ArgString( toolTypes, "CX_POPUP", "YES" );
  212.   if( ! strnicmp( strTemp, "YES" , MAXSTR ) )
  213.     openWindow = TRUE;
  214.  
  215.   strTemp = ArgString( toolTypes, "DEF_PATTERN", "#?" );
  216.   strncpy( cp->searchString , strTemp , MAXSTR );
  217.  
  218.   strTemp = ArgString( toolTypes, "CENTERWINDOW", "YES" );
  219.   if( ! strnicmp( strTemp, "YES", MAXSTR ) )
  220.     cp->centerWindow = TRUE;
  221.  
  222.   strTemp = ArgString( toolTypes, "IGNOREICONS" , "NO" );
  223.   if( ! strnicmp( strTemp, "YES", MAXSTR ) )
  224.   {
  225.     cp->ignoreIcons = TRUE;
  226.     ParsePatternNoCase( "#?.info" , cp->ignoreIconsPat, MAXPAT );
  227.   }
  228.   else
  229.     ParsePatternNoCase( "#?" , cp->ignoreIconsPat, MAXPAT );
  230.  
  231.   strTemp = ArgString( toolTypes, "SORTLIST", "YES" );
  232.   if( ! stricmp( strTemp, "YES" , MAXSTR ) )
  233.     cp->sortList = TRUE;
  234.  
  235.   strTemp = ArgString( toolTypes, "INITIAL_DIR", "SYS:" );
  236.   strncpy( cp->searchPath, strTemp, MAXSTR );
  237.  
  238.   ArgArrayDone();
  239.   DB("Tooltypes parsed..")
  240.  
  241.  
  242.   NewList( (struct List *)&(cp->foundList) );
  243.   DB("Found list init")
  244.  
  245.   OpenCxInterface( cp );
  246.   DB("CXInterface Open")
  247.  
  248.   cp->lastX = cp->lastY = -1L;
  249.  
  250.  
  251.   if( openWindow )
  252.     OpenFFWindow( cp );
  253.  
  254.   DB("Opened window (I.N.)")
  255. }
  256.  
  257. void
  258. Init( struct controlPanel *cp )
  259. {
  260.  
  261.   if( ! ( IntuitionBase = OpenLibrary( "intuition.library", 37L ) ) )
  262.       Shutdown( cp , 20 );
  263.  
  264.   if( ! ( GadToolsBase = OpenLibrary( "gadtools.library", 37L ) ) )
  265.       Shutdown( cp , 20 );
  266.  
  267.   if( ! ( CxBase = OpenLibrary( "commodities.library" , 37L ) ) )
  268.       Shutdown( cp , 20 );
  269.  
  270.   if( ! ( IconBase = OpenLibrary( "icon.library", 37L ) ) )
  271.       Shutdown( cp , 20 );
  272.  
  273.   if( ! ( AslBase = OpenLibrary( "asl.library", 37L ) ) )
  274.       Shutdown( cp, 20 );
  275. }
  276.  
  277. ULONG
  278. GetWaitMask( struct controlPanel *cp )
  279. {
  280.   ULONG mask;
  281.  
  282.   mask = SIGBREAKF_CTRL_C;
  283.  
  284.   if( cp->ffWindow )
  285.     mask |= 1 << cp->ffWindow->UserPort->mp_SigBit;
  286.  
  287.   if( cp->cxInterface.cxPort )
  288.     mask |= 1 << cp->cxInterface.cxPort->mp_SigBit;
  289.  
  290.   return( mask );
  291. }
  292.  
  293.  
  294. struct foundNode *
  295. NodeAddress( struct MinList *fl , UWORD ord )
  296. {
  297.   struct foundNode *fn;
  298.  
  299.   fn = (struct foundNode *)fl->mlh_Head;
  300.  
  301.   while( ord-- )
  302.   {
  303.     fn = (struct foundNode *)fn->fn_Node.ln_Succ;
  304.   }
  305.  
  306.   return( fn );
  307. }
  308.  
  309. BOOL
  310. QuitCheck( struct controlPanel *cp )
  311. {
  312.   struct IntuiMessage *msg;
  313.   BOOL               quit = FALSE;
  314.  
  315.   while(  msg = GT_GetIMsg( cp->ffWindow->UserPort ) )
  316.   {
  317.     struct Gadget *gad;
  318.     ULONG       class;
  319.     UWORD       code;
  320.  
  321.     gad = msg->IAddress;
  322.     class = msg->Class;
  323.     code  = msg->Code;
  324.  
  325.     GT_ReplyIMsg( msg );
  326.  
  327.     if( class == IDCMP_GADGETUP )
  328.     {
  329.       if( gad->GadgetID == ID_ABORT )
  330.       {
  331.     quit = TRUE;
  332.       }
  333.     }
  334.     else
  335.       if( class == IDCMP_VANILLAKEY )
  336.       {
  337.     if( code == 'a' || code == 'A' )
  338.     {
  339.       quit = TRUE;
  340.     }
  341.       }
  342.       else
  343.     if( class == IDCMP_REFRESHWINDOW )
  344.     {
  345.       GT_BeginRefresh(cp->ffWindow);
  346.       GT_EndRefresh(cp->ffWindow , TRUE);
  347.     }
  348.  
  349.   }
  350.  
  351.   return( quit );
  352.  
  353. }
  354.  
  355. /*********************************************************************************/
  356.  
  357. /******************************** Custom List Mgmt *******************************/
  358.  
  359. struct foundNode *
  360. AddFoundNode( struct MinList *list , STRPTR foundName , BOOL sort )
  361. {
  362.   struct foundNode *fn;
  363.   struct Node       *node,
  364.            *pred;
  365.  
  366.   fn = AllocVec( sizeof( struct foundNode ) , MEMF_ANY|MEMF_CLEAR );
  367.  
  368.   fn->fn_Node.ln_Name = fn->fn_Name;
  369.  
  370.   strcpy( fn->fn_Name , foundName );
  371.  
  372.   if( sort )
  373.   {
  374.     pred = NULL;
  375.  
  376.     for( node = (struct Node *)list->mlh_Head ; node->ln_Succ ; node = node->ln_Succ )
  377.     {
  378.       if( stricmp( node->ln_Name , foundName ) > 0 )
  379.     break;
  380.       pred = node;
  381.     }
  382.  
  383.   }
  384.   else
  385.     pred = (struct Node *)&list->mlh_Tail;
  386.  
  387.   Insert( (struct List *)list, (struct Node *)fn, pred );
  388.  
  389.   return( fn );
  390. }
  391.  
  392. void
  393. DeleteFoundList( struct MinList *list )
  394. {
  395.   while( ! IsListEmpty( (struct List *)list ))
  396.   {
  397.     struct foundNode *fn;
  398.  
  399.     fn = (struct foundNode *)RemHead( (struct List *)list );
  400.     FreeVec( fn );
  401.   }
  402. }
  403.  
  404. void
  405. AddFoundFile( struct controlPanel *cp, BPTR dir, STRPTR file )
  406. {
  407.   struct foundNode *fn;
  408.   UBYTE  fullName[MAXSTR];
  409.  
  410.   NameFromLock( dir, fullName, MAXSTR );
  411.  
  412.   AddPart( fullName, file, MAXSTR );
  413.  
  414.   fn = AddFoundNode(&cp->foundList, fullName, cp->sortList );
  415.   if( !fn )
  416.     Shutdown( cp, 20 );
  417.  
  418.   ++(cp->numFound);
  419. }
  420.  
  421. /***************  Command functions start here *****************************/
  422.  
  423. void
  424. CMD_Show( struct controlPanel *cp )
  425. {
  426.   OpenFFWindow( cp );
  427. }
  428.  
  429. void
  430. CMD_Hide( struct controlPanel *cp )
  431. {
  432.   CloseFFWindow( cp );
  433. }
  434.  
  435. void
  436. CMD_Search( struct controlPanel *cp )
  437. {
  438.  
  439.   GT_SetGadgetAttrs( cp->ffWinGads[ID_PATGAD],
  440.              cp->ffWindow, NULL,
  441.              GA_Disabled,  TRUE,
  442.              TAG_END );
  443.  
  444.   GT_SetGadgetAttrs( cp->ffWinGads[ID_DEVGAD],
  445.              cp->ffWindow, NULL,
  446.              GA_Disabled,  TRUE,
  447.              TAG_END );
  448.  
  449.   GT_SetGadgetAttrs( cp->ffWinGads[ID_FILEREQ],
  450.              cp->ffWindow, NULL,
  451.              GA_Disabled,  TRUE,
  452.              TAG_END );
  453.  
  454.   GT_SetGadgetAttrs( cp->ffWinGads[ID_SEARCH],
  455.              cp->ffWindow, NULL,
  456.              GA_Disabled,  TRUE,
  457.              TAG_END );
  458.  
  459.   GT_SetGadgetAttrs( cp->ffWinGads[ID_HIDE],
  460.              cp->ffWindow, NULL,
  461.              GA_Disabled,  TRUE,
  462.              TAG_END );
  463.  
  464.   GT_SetGadgetAttrs( cp->ffWinGads[ID_QUIT],
  465.              cp->ffWindow, NULL,
  466.              GA_Disabled,  TRUE,
  467.              TAG_END );
  468.  
  469.   GT_SetGadgetAttrs( cp->ffWinGads[ID_ABORT],
  470.              cp->ffWindow, NULL,
  471.              GA_Disabled,  FALSE,
  472.              TAG_END );
  473.  
  474.   GT_SetGadgetAttrs( cp->ffWinGads[ID_FOUNDLIST],
  475.              cp->ffWindow, NULL,
  476.              GTLV_Labels,  NULL,
  477.              TAG_END );
  478.  
  479.   DeleteFoundList( &cp->foundList );
  480.  
  481.   cp->numFound = 0;
  482.   cp->currFound = ~0;
  483.  
  484.   if( ParsePatternNoCase( cp->searchString , cp->searchPattern , MAXPAT ) == -1 )
  485.     return;
  486.  
  487.   ScanDir( cp->searchPath, cp );
  488.  
  489.   GT_SetGadgetAttrs( cp->ffWinGads[ID_FOUNDLIST],
  490.              cp->ffWindow, NULL,
  491.              GTLV_Labels,  (&cp->foundList),
  492.              GTLV_Selected, ~0L,
  493.              TAG_DONE );
  494.  
  495.   GT_SetGadgetAttrs( cp->ffWinGads[ID_PATGAD],
  496.              cp->ffWindow, NULL,
  497.              GA_Disabled,  FALSE,
  498.              TAG_END );
  499.  
  500.   GT_SetGadgetAttrs( cp->ffWinGads[ID_DEVGAD],
  501.              cp->ffWindow, NULL,
  502.              GA_Disabled,  FALSE,
  503.              TAG_END );
  504.  
  505.   GT_SetGadgetAttrs( cp->ffWinGads[ID_FILEREQ],
  506.              cp->ffWindow, NULL,
  507.              GA_Disabled,  FALSE,
  508.              TAG_END );
  509.  
  510.   GT_SetGadgetAttrs( cp->ffWinGads[ID_SEARCH],
  511.              cp->ffWindow, NULL,
  512.              GA_Disabled,  FALSE,
  513.              TAG_END );
  514.  
  515.   GT_SetGadgetAttrs( cp->ffWinGads[ID_HIDE],
  516.              cp->ffWindow, NULL,
  517.              GA_Disabled,  FALSE,
  518.              TAG_END );
  519.  
  520.   GT_SetGadgetAttrs( cp->ffWinGads[ID_QUIT],
  521.              cp->ffWindow, NULL,
  522.              GA_Disabled,  FALSE,
  523.              TAG_END );
  524.  
  525.   GT_SetGadgetAttrs( cp->ffWinGads[ID_ABORT],
  526.              cp->ffWindow, NULL,
  527.              GA_Disabled,  TRUE,
  528.              TAG_END );
  529.  
  530. }
  531.  
  532. void
  533. CMD_Quit( struct controlPanel *cp )
  534. {
  535.   Shutdown( cp , 0 );
  536. }
  537.  
  538. /**********************************************************************************/
  539.  
  540. /***************  Comprehensive Shutdown Code *************************************/
  541. void
  542. Shutdown( struct controlPanel *cp , int retCode )
  543. {
  544.  
  545.   if( cp )
  546.   {
  547.  
  548.     /*************************** Close Cx port and interface ************************/
  549.  
  550.     DB("Stage 1")
  551.     if( cp->cxInterface.cxPort )
  552.     {
  553.       DB("Closing CX Interface")
  554.       CloseCxInterface( cp );
  555.     }
  556.  
  557.     /*************************** Get Rid of Window and Related GadTools Stuff *******/
  558.  
  559.     DB("Stage 2")
  560.     if( cp->ffWindow )
  561.     {
  562.       CloseFFWindow( cp );
  563.       cp->ffWindow = NULL;
  564.     }
  565.  
  566.     DB("Stage 3")
  567.     if( cp->ffWinGadList )
  568.     {
  569.       FreeGadgets( cp->ffWinGadList );
  570.       cp->ffWinGadList = NULL;
  571.     }
  572.  
  573.     DB("Stage 4")
  574.     if( cp->ffWinVi );
  575.     {
  576.       FreeVisualInfo( cp->ffWinVi );
  577.       cp->ffWinGadList = NULL;
  578.     }
  579.  
  580.     DB("Stage 5")
  581.     DeleteFoundList( &cp->foundList );
  582.  
  583.     DB("Stage 5a")
  584.     FreeVec( cp );
  585.  
  586.   }
  587.   /******************************* Close Libraries & exit ************************/
  588.  
  589.   DB("Stage 6")
  590.   if( IntuitionBase )
  591.     CloseLibrary( IntuitionBase );
  592.  
  593.   if( GadToolsBase )
  594.     CloseLibrary( GadToolsBase );
  595.  
  596.   if( IconBase )
  597.     CloseLibrary( IconBase );
  598.  
  599.   if( CxBase )
  600.     CloseLibrary( CxBase );
  601.  
  602.   if( AslBase )
  603.     CloseLibrary( AslBase );
  604.  
  605.   exit( retCode );
  606.  
  607. }
  608. /********************************************************************************/
  609.  
  610.