home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / dev / dropbox-1.1.lha / DropBox / src / DropBox.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-05  |  15.5 KB  |  846 lines

  1. /** DoRev Header ** Do not edit! **
  2. *
  3. * Name   !         :  DropBox.c
  4. * Copyrigit        :  Copyright 1993 Steve Anichini. All Rights Reserved.
  5. * Creation date    :  11-Jun-93
  6. * Translator       :  SAS/C 5.1b
  7. *
  8. * Date       Rev  Author               Comment
  9. * ---------  ---  -------------------  ----------------------------------------
  10. * 26-Jun-93    4  Steve Anichini       Split DropBox.c into DropBox.c and DropE
  11. * 21-Jun-93    3  Steve Anichini       Added support for underscore in gadgets.
  12. * 21-Jun-93    2  Steve Anichini       First Release.
  13. * 12-Jun-93    1  Steve Anichini       Beta Release 1.0
  14. * 11-Jun-93    0  Steve Anichini       None.
  15. *
  16. *** DoRev End **/
  17.  
  18.  
  19. #include "DropBox.h"
  20. #include "window.h"
  21.  
  22. #ifdef LATTICE
  23. int CXBRK(void) { return(0);}
  24. int chkabort(void) { return(0);}
  25. #endif
  26.  
  27. struct Library *IFFParseBase;
  28. struct Library *IconBase;
  29. struct Library *WorkbenchBase;
  30. struct Library *CxBase;
  31. struct Library *GadToolsBase;
  32. struct Library *UtilityBase;
  33. struct IntuitionBase *IntuitionBase = NULL;
  34. struct GfxBase *GfxBase = NULL;
  35.  
  36. struct DiskObject *dobj = NULL;
  37. struct MsgPort *myport = NULL, *brokerport = NULL;
  38. struct AppIcon *appicon = NULL;
  39. struct AppMenuItem *appitem = NULL;
  40. CxObj *broker, *filter, *sender;
  41.  
  42. struct NewBroker mybroker =
  43. {
  44.     NB_VERSION,
  45.     NULL,
  46.     NULL,
  47.     NULL,
  48.     0,
  49.     COF_SHOW_HIDE,
  50.     NULL,
  51.     0,
  52. };
  53.  
  54. ULONG cxsigflag, apsigflag, winsigflag = 0;
  55. BOOL end_flag = FALSE;
  56. BOOL FirstSave = TRUE;
  57. UBYTE **ttypes;
  58. BOOL modified = FALSE;
  59.  
  60. struct GenPref MainPrefs =
  61. {
  62.     GPRF_VERSION,
  63.     0,
  64.     GFLG_NONE,
  65.     0,50,
  66.     640,100,
  67.     {0,0,0}
  68. };
  69.     
  70. /* Globals for SAS/C cback.o */
  71. long _stack = 5120;
  72. char *_procname = "DropBox";
  73. long _priority = 0;
  74. long _BackGroundIO = 0;
  75.  
  76. void InitLibraries()
  77. {
  78.     if(!(IntuitionBase = (struct IntuitionBase *)
  79.             OpenLibrary("intuition.library", DEF_LOWEST_REV)))
  80.         leave(NO_INTUILIB);
  81.         
  82.     if(!(GfxBase = (struct GfxBase *)
  83.             OpenLibrary("graphics.library", DEF_LOWEST_REV)))
  84.         leave(NO_GFXLIB);
  85.         
  86.     if(!(GadToolsBase = OpenLibrary("gadtools.library", DEF_LOWEST_REV)))
  87.         leave(NO_GADLIB);
  88.     
  89.     if(!(UtilityBase = OpenLibrary("utility.library", DEF_LOWEST_REV)))
  90.         leave(NO_UTILLIB);
  91.             
  92.     if(!(IconBase = OpenLibrary("icon.library", DEF_LOWEST_REV)))
  93.         leave(NO_ICONLIB);
  94.     
  95.     if(!(WorkbenchBase = OpenLibrary("workbench.library", DEF_LOWEST_REV)))
  96.         leave(NO_WORKLIB);
  97.  
  98.     if(!(CxBase = OpenLibrary("commodities.library", DEF_LOWEST_REV)))
  99.         leave(NO_CXLIB);
  100. }
  101.     
  102. void InitCX(int argc, char **argv)
  103. {
  104.     UBYTE *hotkey;
  105.     
  106.     /* Commodities Stuff */
  107.     if(!(brokerport = CreateMsgPort()))
  108.         leave(NO_PORT);
  109.     
  110.     mybroker.nb_Port = brokerport;
  111.     cxsigflag = 1 << brokerport->mp_SigBit;
  112.     
  113.     /* Parse Args */
  114.     ttypes = (UBYTE **) ArgArrayInit(argc, argv);
  115.     
  116.     mybroker.nb_Pri = (BYTE) ArgInt(ttypes, "CX_PRIORITY", 0);
  117.     mybroker.nb_Name = NAME;
  118.     mybroker.nb_Title = TITLE;
  119.     mybroker.nb_Descr = DESC;
  120.     
  121.     hotkey = (UBYTE *) ArgString(ttypes, "HOTKEY", THEHOTKEY);
  122.      
  123.     if(!(broker = CxBroker(&mybroker, NULL)))
  124.         leave(NO_BROKER);
  125.     
  126.     if(!(filter = CxFilter(hotkey)))
  127.         leave(NO_FILTER);
  128.         
  129.     AttachCxObj(broker, filter);
  130.  
  131.     if(!(sender = CxSender(brokerport, EVT_HOTKEY)))
  132.         leave(NO_SENDER);
  133.         
  134.     AttachCxObj(filter, sender);
  135.     
  136.     if(CxObjError(filter))
  137.         leave(NO_FILTER);
  138. }
  139.  
  140. void InitIcons()
  141. {
  142.     UBYTE *menu;
  143.     
  144.     if(!(dobj = GetDiskObjectNew("DropBox")))
  145.         leave(NO_ICON);
  146.  
  147.     dobj->do_Type = NULL;
  148.     
  149.     if(!(myport = CreateMsgPort()))
  150.         leave(NO_PORT);
  151.     
  152.     apsigflag = 1 << myport->mp_SigBit;
  153.     
  154.     dobj->do_CurrentX = ArgInt(ttypes, "ICON_X", NO_ICON_POSITION);
  155.     dobj->do_CurrentY = ArgInt(ttypes, "ICON_Y", NO_ICON_POSITION);
  156.     
  157.     if(!(appicon = AddAppIconA(0, 0, NAME,
  158.             myport, NULL, dobj, NULL)))
  159.         leave(NO_APPICON);
  160.  
  161.     menu = (UBYTE *) ArgString(ttypes, "APPMENU", "YES");
  162.     if(!stricmp(menu, "YES"))
  163.     {
  164.         if(!(appitem = AddAppMenuItemA(1, 0, "DropBox...", myport,
  165.                 NULL)))
  166.         leave(NO_APPITEM);
  167.     }
  168.  
  169.     ActivateCxObj(broker, 1);
  170. }
  171.  
  172. void initialize(int argc, char **argv)
  173. {
  174.     ULONG err = 0;
  175.     UBYTE *popup;
  176.     UBYTE *file, *dir = NULL, buf[DEFLEN];
  177.     UBYTE *temp = NULL;
  178.     struct DimensionInfo dinfo;
  179.     LONG x, y;
  180.     LONG pri;
  181.  
  182.     InitLibraries(); /* Open all required libraries */
  183.     InitCX(argc, argv);          /* Add us!to the commodities broker list */
  184.  
  185.     pri = ArgInt(ttypes, "TASK_PRI",0); /* Get task priority tool type */
  186.     if(pri)
  187.         SetTaskPri(FindTask(NULL), pri);
  188.  
  189.     file = (UBYTE *) ArgString(ttypes, "PREFS", NULL);
  190.     if(file)
  191.     {
  192.         strcpy(buf, file);
  193.         temp = PathPart(buf);
  194.         *temp = '\0';
  195.         dir = buf;
  196.         file = FilePart(file);
  197.     }
  198.  
  199.     InitIO(file, dir, NULL); /* Init the filenames */
  200.     if(err = JustLoad())
  201.     {
  202.         DisplayErr(err);
  203.         CleanDB();
  204.         InitDB();
  205.     }
  206.     else
  207.         FirstSave = FALSE;
  208.     InitIcons();
  209.  
  210.     if(!SetupScreen()) /* Set windows to be centered */
  211.     {
  212.         if(GetDisplayInfoData(NULL,(UBYTE *)&dinfo,
  213.             sizeof(struct DimensionInfo),DTAG_DIMS,
  214.             GetVPModeID(&(Scr->ViewPort))))
  215.         {
  216.             x = dinfo.Nominal.MaxX - dinfo.Nominal.MinX;
  217.             y = dinfo.Nominal.MaxY - dinfo.Nominal.MinY;
  218.             DropBoxLeft = (x- DropBoxWidth)/2;
  219.             DropBoxTop = (y - DropBoxHeight)/2;
  220.             SelectLeft = (x - SelectWidth)/2;
  221.             SelectTop  = (y - SelectTop)/2;
  222.         }
  223.  
  224.         CloseDownScreen();
  225.     }
  226.  
  227.     popup = (UBYTE *) ArgString(ttypes, "CX_POPUP", "NO");
  228.     if(!strnicmp(popup, "YES", 3))
  229.         ShowWindow();
  230. }
  231.  
  232. void CleanWindow(struct Window *win)
  233. {
  234.     struct IntuiMessage *imsg = NULL;
  235.  
  236.     while(imsg = GT_GetIMsg(win->UserPort))
  237.         GT_ReplyIMsg(imsg);
  238. }
  239.  
  240. void CleanApp()
  241. {
  242.     struct AppMessage *msg = NULL;
  243.     
  244.     while(msg = (struct AppMessage *)GetMsg(myport))
  245.         ReplyMsg((struct Message *)msg);
  246. }
  247.  
  248. struct MenuItem *GetItem(struct Menu *menu, UWORD mnum, UWORD inum)
  249. {
  250.     register struct MenuItem *temp = NULL;
  251.     
  252.     while(mnum && menu)
  253.     {
  254.          menu = menu->NextMenu;
  255.         mnum--;
  256.     }
  257.     
  258.     if(menu)
  259.     {
  260.         temp = menu->FirstItem;
  261.         
  262.         while(inum && temp)
  263.         {
  264.             temp = temp->NextItem;
  265.             inum--;
  266.         }
  267.     }
  268.     
  269.     return temp;
  270. }
  271.  
  272. void ExtractExt(char *ext, char *src, UWORD length)
  273. {
  274.     char *temp, *file;
  275.     
  276.     if(file = FilePart(src))
  277.     {
  278.         temp = strrchr(file, '.');
  279.         if(temp)
  280.         {
  281.             strncpy(ext, "#?", length);
  282.             strncat(ext, temp, length);
  283.         }
  284.         else
  285.             strncpy(ext, "", length);
  286.     }
  287.     else
  288.         strncpy(ext, "", length);    
  289. }
  290.  
  291. void NewEntry(char *name)
  292. {
  293.     BOOL ans = FALSE;
  294.     int error = 0;
  295.     struct DBNode *temp;
  296.     struct PatNode *ptemp;
  297.     char ext[DEFLEN];
  298.     
  299.     struct EasyStruct new =
  300.     {
  301.         sizeof(struct EasyStruct),
  302.         0,
  303.         NULL,
  304.         NULL,
  305.         NULL
  306.     };
  307.  
  308.     new.es_Title = ENTRY;
  309.     new.es_TextFormat = ENTRYTEXTFORMAT;
  310.     new.es_GadgetFormat = ENTRYGADGETFORMAT;
  311.     
  312.     ans = EasyRequest(DropBoxWnd, &new, 0);
  313.         
  314.     if(ans)
  315.     {
  316.         if(!DropBoxWnd)
  317.             error = ShowWindow();
  318.  
  319.         if(!error)
  320.         {
  321.             if((temp = (struct DBNode *) NewNode(NT_DBNODE)) &&
  322.                (ptemp = (struct PatNode *) NewNode(NT_PATNODE)))
  323.             {
  324.                 UpdateDB();
  325.             
  326.                 GT_SetGadgetAttrs(DropBoxGadgets[GD_File_Types], 
  327.                     DropBoxWnd, NULL, GTLV_Labels, (ULONG) ~0, TAG_END);
  328.                     
  329.                 AddNode((struct Node *)temp,DataBase);
  330.                 
  331.                 GT_SetGadgetAttrs(DropBoxGadgets[GD_File_Types], DropBoxWnd, NULL, 
  332.                     GTLV_Labels, (ULONG) DataBase, TAG_END);    
  333.                 
  334.                 ExtractExt(ext,name, DEFLEN);
  335.                 FillPatNode(ptemp, ext, PFLG_NOFLAG);
  336.                 AddNode((struct Node *)ptemp, temp->db_Pats);
  337.                 
  338.                 UpdateDB();
  339.                 Select(temp);
  340.                 ActivateGadget(DropBoxGadgets[GD_Name], DropBoxWnd, NULL);
  341.                 
  342.                 modified = TRUE;
  343.             
  344.             }
  345.             else
  346.                 DisplayErr(NO_MEM);
  347.         }
  348.         else
  349.             DisplayErr(NO_WINDOW);        
  350.     }
  351. }
  352.  
  353. struct DBNode *PutUpSelection(struct List *found)
  354. {
  355.     ULONG oldflags;
  356.     BOOL hide = FALSE, ok = FALSE;
  357.     struct IntuiMessage *msg;
  358.     struct Gadget *gad = NULL;
  359.     ULONG class;
  360.     UWORD code, select = 0;
  361.     
  362.     if(!Scr)
  363.         if(SetupScreen())
  364.             return (struct DBNode *)found->lh_Head;
  365.     
  366.     if(OpenSelectWindow())
  367.     {
  368.         if(!DropBoxWnd)
  369.             CloseDownScreen();
  370.             
  371.         return (struct DBNode *)found->lh_Head;
  372.     }
  373.     
  374.     if(DropBoxWnd)
  375.     {
  376.         oldflags = DropBoxWnd->IDCMPFlags;
  377.         ModifyIDCMP(DropBoxWnd, NULL);
  378.     }
  379.         
  380.     GT_SetGadgetAttrs(SelectGadgets[GD_SelectGad], SelectWnd, NULL, 
  381.         GTLV_Labels, (ULONG) found, TAG_END);
  382.  
  383.     GT_SetGadgetAttrs(SelectGadgets[GD_SelectGad], SelectWnd, NULL,
  384.         GTLV_Selected, select, TAG_END);
  385.         
  386.     while(!hide)
  387.     {
  388.         WaitPort(SelectWnd->UserPort);
  389.         
  390.         while(msg = GT_GetIMsg(SelectWnd->UserPort))
  391.         {
  392.             gad = (struct Gadget *) msg->IAddress;
  393.             class = msg->Class;
  394.             code = msg->Code;
  395.             
  396.             GT_ReplyIMsg(msg);
  397.             
  398.             switch(class)
  399.             {
  400.                 case IDCMP_GADGETDOWN:
  401.                 case IDCMP_GADGETUP:
  402.                     if(gad == SelectGadgets[GD_Cancel])
  403.                     {
  404.                         hide = TRUE;
  405.                         ok = FALSE;
  406.                     }
  407.                     else
  408.                         if(gad == SelectGadgets[GD_Ok])
  409.                         {
  410.                             hide = TRUE;
  411.                             ok = TRUE;
  412.                         }
  413.                         else
  414.                             if(gad == SelectGadgets[GD_SelectGad])
  415.                                 select = code;
  416.                     break;
  417.                 
  418.                 case IDCMP_REFRESHWINDOW:
  419.                     GT_BeginRefresh(SelectWnd);
  420.                     GT_EndRefresh(SelectWnd, TRUE);
  421.                     break;
  422.             }
  423.         }    
  424.     }
  425.     
  426.     CloseSelectWindow();
  427.     
  428.     if(DropBoxWnd)
  429.         ModifyIDCMP(DropBoxWnd, oldflags);
  430.  
  431.     if(ok)
  432.         return (struct DBNode *) OrdToPtr(select, found);
  433.     else
  434.         return NULL;
  435. }
  436.      
  437. void ExecuteCommand(struct AppMessage *appmsg)
  438. {
  439.     char com[DEFLEN*2];
  440.     struct DBNode *node = NULL;
  441.     struct List *fnodes = NULL;
  442.     BPTR con;
  443.     register int i;
  444.     ULONG err = NO_ERROR;
  445.     
  446.     for(i = 0; i < appmsg->am_NumArgs; i++)
  447.         if(fnodes = FindDBNode(appmsg->am_ArgList[i].wa_Name))
  448.         {
  449.             if((MainPrefs.gp_Flags&GFLG_SELECTWIN)
  450.                 && (CountNodes(fnodes) > 1))
  451.                 node = PutUpSelection(fnodes);
  452.             else
  453.                 node = (struct DBNode *)fnodes->lh_Head;
  454.             
  455.             if(node)
  456.             {
  457.                 if(err = CreateCommand(node, &appmsg->am_ArgList[i], com))
  458.                     DisplayErr(err);
  459.                 else
  460.                 {    
  461.                     char constr[DEFLEN];
  462.  
  463.                     if(node->db_Flags & DFLG_SUPOUTPUT)
  464.                         strcat(com, " <nil: >nil: ");
  465.  
  466.                     if(node->db_Flags & DFLG_CREATE)
  467.                     {
  468.                         struct DiskObject *dj;
  469.                         char dir[DEFLEN], *st;
  470.                         BPTR lock;
  471.  
  472.                         lock = NULL;
  473.  
  474.                         strcpy(dir, node->db_Dest);
  475.                         AddPart(dir, appmsg->am_ArgList[i].wa_Name,DEFLEN);
  476.                         st = strrchr(dir, '.');
  477.                         if(st)
  478.                             *st = '\0';
  479.                         lock = Lock(dir, ACCESS_READ);
  480.  
  481.                         if(!lock)
  482.                             lock = CreateDir(dir);
  483.  
  484.                         if(!lock)
  485.                             DisplayErr(NO_CREATEDIR);
  486.                         else
  487.                         {
  488.                             dj = NULL;
  489.  
  490.                             if(MainPrefs.gp_Flags & GFLG_SAVEICON)
  491.                                 if(dj = GetDefDiskObject(WBDRAWER))
  492.                                 {
  493.                                     PutDiskObject(dir, dj);
  494.                                     FreeDiskObject(dj);
  495.                                 }
  496.  
  497.                             UnLock(lock);
  498.                         }
  499.                     }
  500.  
  501.                     sprintf(constr, "CON:%d/%d/%d/%d/DropBox Output/SIMPLE",
  502.                         MainPrefs.gp_IOLeft, MainPrefs.gp_IOTop,
  503.                         max(MainPrefs.gp_IOWidth,80),
  504.                         max(MainPrefs.gp_IOHeight, 40));
  505.  
  506.                     if(!(node->db_Flags & DFLG_SUPOUTPUT))
  507.                     {
  508.                         if(con = Open(constr, MODE_OLDFILE))
  509.                         {
  510.                             if(SystemTags(com, SYS_Input, (ULONG) con,
  511.                                 SYS_Output, (ULONG) con, TAG_DONE))
  512.                                 DisplayErr(STAGS_FAIL);
  513.                             Close(con);
  514.                         }
  515.                         else
  516.                             if(SystemTags(com,TAG_DONE))
  517.                                 DisplayErr(STAGS_FAIL);
  518.                     }
  519.                     else
  520.                         if(SystemTags(com,TAG_DONE))
  521.                             DisplayErr(STAGS_FAIL);
  522.  
  523.                 } // End else/CreateCommand()
  524.             } // end If(node)
  525.  
  526.             CleanList(fnodes);
  527.         }
  528.         else
  529.             NewEntry(appmsg->am_ArgList[i].wa_Name);
  530. }
  531.  
  532. BOOL Safe(struct Window *wnd)
  533. {
  534.     LONG err = 0;
  535.  
  536.     struct EasyStruct safe =
  537.     {
  538.         sizeof(struct EasyStruct),
  539.         0,
  540.         NULL,
  541.         NULL,
  542.         NULL
  543.     };
  544.     
  545.     safe.es_Title = SAFE;
  546.     safe.es_TextFormat = SAFETEXTFORMAT;
  547.     safe.es_GadgetFormat = SAFEGADGETFORMAT;
  548.     
  549.     switch(EasyRequest(wnd, &safe, 0))
  550.     {
  551.         case 0:
  552.             return FALSE;
  553.             
  554.         case 1:
  555.             return TRUE;
  556.             
  557.         case 2:
  558.             if(FirstSave)
  559.             {
  560.                 FirstSave = FALSE;
  561.                 PrefIO(TRUE);
  562.             }
  563.             else
  564.                 if(err = JustSave())
  565.                     DisplayErr(err);
  566.                 else
  567.                     modified = FALSE;
  568.             return !modified;
  569.     }
  570. }
  571.  
  572. void HandleAppMsg()
  573. {
  574.     struct AppMessage *appmsg = NULL;
  575.     
  576.     while(appmsg = (struct AppMessage *)
  577.             GetMsg(myport))
  578.     {
  579.         switch(appmsg->am_ID)
  580.         {
  581.             case APPICON:
  582.                 if(!appmsg->am_NumArgs)
  583.                     ShowWindow();
  584.                 else
  585.                     ExecuteCommand(appmsg);
  586.                 break;
  587.             
  588.             case APPMENU:
  589.                 ShowWindow();
  590.                 break;
  591.         
  592.             default:
  593.                 break;
  594.         }
  595.         
  596.         ReplyMsg((struct Message *) appmsg);
  597.     }
  598. }
  599.  
  600. void HandleCxMsg()
  601. {
  602.     CxMsg *msg;
  603.     ULONG msgid, msgtype;
  604.     
  605.     while(msg = (CxMsg *) GetMsg(brokerport))
  606.     {
  607.         msgid = CxMsgID(msg);
  608.         msgtype = CxMsgType(msg);
  609.         ReplyMsg((struct Message *) msg);
  610.         
  611.         switch(msgtype)
  612.         {
  613.             case CXM_IEVENT:
  614.                 switch(msgid)
  615.                 {
  616.                     case EVT_HOTKEY:
  617.                         ShowWindow();
  618.                         break;
  619.         
  620.                     default:
  621.                         break;
  622.                 }
  623.                 break;
  624.                 
  625.             case CXM_COMMAND:
  626.                 switch(msgid)
  627.                 {
  628.                     case CXCMD_APPEAR:
  629.                         ShowWindow();
  630.                         break;
  631.                     
  632.                     case CXCMD_DISAPPEAR:
  633.                         HideWindow();
  634.                         break;
  635.                         
  636.                     case CXCMD_DISABLE:
  637.                         ActivateCxObj(broker, 0);
  638.                         break;
  639.                     
  640.                     case CXCMD_ENABLE:
  641.                         ActivateCxObj(broker, 1);
  642.                         break;
  643.                         
  644.                     case CXCMD_KILL:
  645.                         if(modified)
  646.                         {
  647.                             if(Safe(DropBoxWnd))
  648.                                 end_flag = TRUE;    
  649.                         }
  650.                         else
  651.                             end_flag = TRUE;
  652.                         break;
  653.                             
  654.                     default:
  655.                         break;
  656.                 }
  657.         }
  658.     }        
  659. }
  660.     
  661. void loop()
  662. {
  663.     ULONG sigrcvd;
  664.     
  665.     sigrcvd = Wait(apsigflag|cxsigflag|winsigflag);
  666.                     
  667.     if(sigrcvd&apsigflag)
  668.         HandleAppMsg();
  669.             
  670.     if(sigrcvd&cxsigflag)
  671.         HandleCxMsg();
  672.         
  673.     if((sigrcvd&winsigflag) && DropBoxWnd)
  674.         HandleIntuiMsg();                                    
  675. }
  676.  
  677. void CleanCX()
  678. {
  679.     struct AppMessage *appmsg = NULL;
  680.  
  681.     if(broker)
  682.         DeleteCxObjAll(broker);
  683.     
  684.     if(appitem)
  685.         RemoveAppMenuItem(appitem);
  686.         
  687.     if(appicon)
  688.         RemoveAppIcon(appicon);
  689.     
  690.     if(brokerport)
  691.     {
  692.         while(appmsg=(struct AppMessage *)GetMsg(brokerport))
  693.             ReplyMsg((struct Message *)appmsg);
  694.         
  695.         DeletePort(brokerport);
  696.     }
  697.     
  698.     if(myport)
  699.     {
  700.         while(appmsg=(struct AppMessage *)GetMsg(myport))
  701.             ReplyMsg((struct Message *)appmsg);
  702.     
  703.         DeleteMsgPort(myport);
  704.     }
  705.     
  706.     if(dobj)
  707.         FreeDiskObject(dobj);
  708.     
  709.     ArgArrayDone();
  710. }
  711.     
  712. void CleanLibraries()
  713. {
  714.     if(CxBase)
  715.         CloseLibrary(CxBase);
  716.         
  717.     if(WorkbenchBase)
  718.         CloseLibrary(WorkbenchBase);
  719.     
  720.     if(IconBase)
  721.         CloseLibrary(IconBase);
  722.  
  723.     if(GadToolsBase)
  724.         CloseLibrary(GadToolsBase);
  725.         
  726.     if(GfxBase)
  727.         CloseLibrary((struct Library *)GfxBase);
  728.     
  729.     if(IntuitionBase)
  730.         CloseLibrary((struct Library *)IntuitionBase);
  731. }
  732.  
  733. void leave(ULONG err)
  734. {
  735.     
  736.     if(err)
  737.         DisplayErr(err);
  738.     
  739.     if(Clip)
  740.         FreeNode((struct Node *)Clip);
  741.         
  742.     if(DropBoxWnd)
  743.     {
  744.         CleanWindow(DropBoxWnd);
  745.         CloseDropBoxWindow();
  746.     }
  747.     
  748.     if(Scr)
  749.         CloseDownScreen();
  750.     
  751.     CleanDB();
  752.     CleanCX();
  753.     CleanLibraries();
  754.     
  755.     OpenWorkBench();
  756.  
  757.     exit(err);
  758. }
  759.  
  760. void DisplayErr(ULONG err)
  761. {
  762.     struct EasyStruct Errstruct = 
  763.     {
  764.         sizeof(struct EasyStruct),
  765.         0,
  766.         "DropBox Warning!",
  767.         "Error:\n  %s\nDos Error:\n  %s",
  768.         "OK"
  769.     };
  770.     
  771.     struct AlertMessage MyAlert[] =
  772.     {
  773.         {0,14, "                            DropBox                        ", 0xFF},
  774.         {0,24, "                             Alert                         ", 0xFF},
  775.         {80,34,"                                                           ", 0x00}
  776.     };
  777.     LONG DosErr = 0;
  778.     register WORD i;
  779.     WORD temp;
  780.     char Buffer[100];
  781.     BOOL usealert = FALSE;
  782.     
  783.     DosErr = IoErr();
  784.     if(DropBoxWnd)
  785.     {
  786.         DisplayBeep(Scr);
  787.         if(DosErr <= 0)
  788.         {
  789.             Errstruct.es_TextFormat = (UBYTE *)"Error:\n  %s";
  790.             (void)EasyRequest(DropBoxWnd, &Errstruct,NULL, error[err]);
  791.         }
  792.         else
  793.         {
  794.             Fault(DosErr, NULL, Buffer, 100);
  795.             EasyRequest(DropBoxWnd, &Errstruct,NULL, error[err],
  796.                                      Buffer);
  797.         }
  798.     }
  799.     else
  800.     {
  801.         if(!IntuitionBase)
  802.         {
  803.             if(IntuitionBase = (struct IntuitionBase *)
  804.                                         OpenLibrary("intuition.library", 33))
  805.                 usealert = TRUE;
  806.         }
  807.         else
  808.         {
  809.             DisplayBeep(NULL);
  810.             if(DosErr <= 0)
  811.             {
  812.                 Errstruct.es_TextFormat = (UBYTE *)"Error:\n  %s";
  813.                 (void)EasyRequest(NULL, &Errstruct,NULL, error[err]);
  814.             }
  815.             else
  816.             {
  817.                 Fault(DosErr, NULL, Buffer, 100);
  818.                 EasyRequest(NULL, &Errstruct,NULL, error[err],
  819.                                      Buffer);
  820.             }
  821.         }
  822.                                 
  823.         if(usealert)
  824.         {
  825.             strcpy(MyAlert[2].AlertText, error[err]);
  826.             if((temp = strlen(MyAlert[2].AlertText)) < 59)
  827.                 for(i =0; i < 59-temp; i++)
  828.                     strcat(MyAlert[2].AlertText, " ");
  829.             DisplayAlert(RECOVERY_ALERT, (UBYTE *) &MyAlert, 40);
  830.         }
  831.     
  832.     }    
  833. }
  834.  
  835. void main(int argc, char **argv)
  836. {
  837.     
  838.     initialize(argc, argv);
  839.     
  840.     while(!end_flag)
  841.         loop();
  842.     
  843.     leave(NO_ERROR);
  844. }
  845.  
  846.