home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / iffconverter / iffconverter.c < prev    next >
C/C++ Source or Header  |  1997-01-07  |  15KB  |  503 lines

  1. /*
  2. **     $VER: IFFConverter.c V0.02 (08-07-95)
  3. **
  4. **     Author:  Gerben Venekamp
  5. **     Updates: 13-06-95  Version 0.01     Intital module
  6. **              08-07-95  Version 0.02     'ErrorHandler' added for
  7. **                                         flexable error handling.
  8. **              12-11-95  Version 0.03     Tooltypes added
  9. **
  10. **
  11. **  IFFConverter is as its name suggest an IFF Converter. It Converters
  12. **  IFF ILBM pictures to a RAW format.
  13. **  Here the 'main' function has its place. 'Main' initializes and starts
  14. **  IFFConverter. Also, functions for freeing the system resources and
  15. **  error handling are included.
  16. **
  17. */
  18.  
  19.  
  20. #include <dos/dos.h>
  21. #include <exec/memory.h>
  22. #include <intuition/intuition.h>
  23. #include <workbench/startup.h>
  24. #include <proto/dos.h>
  25. #include <proto/gadtools.h>
  26. #include <proto/icon.h>
  27. #include <proto/intuition.h>
  28.  
  29. #include "IFFConverter.h"
  30. #include "ErrorText.h"
  31.  
  32. // Include version number in main module
  33. static char VersionI[] = VersionText;
  34.  
  35. // Define prototypes
  36. void CleanExit(int);
  37. void CleanLibExit(int);
  38. LONG ErrorHandler(enum enum_ErrorNumber, APTR, ...);
  39. void __asm exit(register __d0 LONG ReturnValue);
  40.  
  41. LONG DisplayError(struct Window *, ULONG, STRPTR, STRPTR, STRPTR, APTR);
  42. void GetArguments(ULONG, char *);
  43. void GetToolTypes(struct WBStartup *);
  44. LONG MyDisplayError(APTR, APTR, APTR);
  45. void PreRelease(void);
  46.  
  47.  
  48. /*
  49. **  This is where is all begins.....
  50. **
  51. **  main will call all neseccary initializations in order to start
  52. **  IFFConverter. If something goes wrong during any of the initialization
  53. **  functions, the function will generate an error message and quit
  54. **  imediately after acknoledgement. Control is given back to the system
  55. **  and *not* to the main function!
  56. **
  57. */
  58. void __saveds main()
  59. {
  60.    extern ULONG argc;
  61.    extern char *argv;
  62.  
  63.    OpenLibraries();   // For savety reasons, make this the first function in main.
  64.    GetArguments(argc, argv);
  65.    PreRelease();
  66.    GetDiskFonts();
  67.    AllocateMemory();
  68.    OpenScreens();
  69.    InitGadgets();
  70.    OpenWindows();
  71.    AllocAsl_Requests();
  72.    HandleIntuiMessages();
  73.    CleanExit(0);
  74. }
  75.  
  76.  
  77. /*
  78. **  GetArgument(argc, argv)
  79. **
  80. **  pre:
  81. **  post:
  82. **
  83. */
  84. void GetArguments(ULONG argc, char *argv)
  85. {
  86.    if( argc == 0 )      // WorkBench 'argc' == 0 ; Shell 'argc' >= 1
  87.       GetToolTypes( (struct WBStartup *) argv );
  88.  
  89.    // Well, one or more arguments. So, we're called from shell
  90.    if( argc > 1 )
  91.    {
  92.       // Converter is called with arguments
  93.       char ArgumentsError[] = "Sorry, no argements accepted in this version.\n" \
  94.                               "Try a later version or register and I will implement argumets\n";
  95.  
  96.       BPTR Stdout = Output();
  97.       
  98.       Write( Stdout, &ArgumentsError, sizeof(ArgumentsError) );
  99.    }
  100.  
  101. }
  102.  
  103.  
  104. /*
  105. **  GetToolTypes()
  106. **
  107. **     Get the .info file for the ToolTypes.
  108. **     The first and only argument given through 'argv' is the name of
  109. **     the program itself. This will help us to get the .info file.
  110. **     'GetDiskObject' attaches the .info extention automaticly.
  111. **
  112. **  pre:  None.
  113. **  post: None.
  114. **
  115. */
  116. void GetToolTypes(struct WBStartup *startup)
  117. {
  118.    register UWORD i = 0;
  119.    struct DiskObject *diskobj;
  120.    char **ToolTypes;
  121.    STRPTR temp, Destination;
  122.  
  123.    if( !(diskobj = GetDiskObject(startup->sm_ArgList->wa_Name)) )
  124.       return;
  125.    
  126.    ToolTypes = diskobj->do_ToolTypes;
  127.    
  128.    // Extract GraphicsDrawer
  129.    if( temp = FindToolType(ToolTypes, "GraphicsDrawer") )
  130.    {
  131.       GraphicsDrawerSize = StringLength(temp) + 1;
  132.       AllocThisMem(&GraphicsDrawer, GraphicsDrawerSize, MEMF_CLEAR );
  133.       Destination = GraphicsDrawer;
  134.       while( *Destination++ = *temp ++ );
  135.    }
  136.  
  137.    // Extract FileMode
  138.    if( temp = FindToolType(ToolTypes, "FileMode") )
  139.       while( CYL_FileMode[i] )
  140.       {
  141.          if( StringCompare(temp, CYL_FileMode[i]) )
  142.             GT_FileMode[3] = i;
  143.          i++;
  144.       }
  145.    
  146.    // Extract RenderMode
  147.    if( temp = FindToolType(ToolTypes, "RenderMode") )
  148.    {
  149.       i = 0;
  150.       while( CYL_RenderMode[i] )
  151.       {
  152.          if( StringCompare(temp, CYL_RenderMode[i]) )
  153.             GT_RenderMode[3] = i;
  154.          i++;
  155.       }
  156.    }
  157.    
  158.    // Extract DrawCross
  159.    if( temp = FindToolType(ToolTypes, "DrawCross") )
  160.       if( StringCompare(temp, "TRUE" ) )
  161.       {
  162.          PanelGadgets[GD_DrawCross].MyGadgetTags = &MakeChecked;
  163.          DrawHairCross = TRUE;
  164.       }
  165.       else
  166.       {
  167.          PanelGadgets[GD_DrawCross].MyGadgetTags = &UnmakeChecked;
  168.          DrawHairCross = FALSE;
  169.       }
  170.    else
  171.       DrawHairCross = FALSE;
  172.  
  173.  
  174.    // Extract ByteBoundry
  175.    if( temp = FindToolType(ToolTypes, "ByteBoundry") )
  176.    {
  177.       i = 0;
  178.       while( MXL_ByteBoundry[i] )
  179.       {
  180.          if( StringCompare(temp, MXL_ByteBoundry[i]) )
  181.             GT_ByteBoundry[3] = ByteBoundry = i;
  182.          i++;
  183.       }
  184.    }
  185.    
  186.    // Extract TAB1
  187.    if( temp = FindToolType(ToolTypes, "TAB1") )
  188.       TAB1 = ConvertDecimal(temp);
  189.  
  190.    // Extract TAB2
  191.    if( temp = FindToolType(ToolTypes, "TAB2") )
  192.       TAB2 = ConvertDecimal(temp);
  193.  
  194.    // Extract NumberOfItems1
  195.    if( temp = FindToolType(ToolTypes, "NumberOfItems1") )
  196.       NumberOfItems1 = ConvertDecimal(temp);
  197.  
  198.    // Extract NumberOfItems2
  199.    if( temp = FindToolType(ToolTypes, "NumberOfItems2") )
  200.       NumberOfItems2 = ConvertDecimal(temp);
  201.  
  202.    // Extract BLPCON3OrMask
  203.    if( temp = FindToolType(ToolTypes, "BPLCON3OrMask") )
  204.    {
  205.       Destination = BPLCON3OrMask;
  206.          for(i=0; i<4; i++)
  207.             *Destination++ = *temp++;
  208.    }
  209.  
  210.    FreeDiskObject(diskobj);
  211. }
  212.  
  213.  
  214. /*
  215. **  num = ErrorHandler(ErrorNumber, ArgList)
  216. **
  217. **     Handles all error generated during execution of the program.
  218. **     Some errors may return to the system by calling 'CleanExit'
  219. **     So, no guarentees that this function will return always.
  220. **
  221. **  pre:  ErrorNumber - Number of error to handle.
  222. **        ArgList - Addition arguments for error message. NULL for
  223. **                  no arguments.
  224. **  post: num - Whatever EasyRequest returns. See AutoDocs for more
  225. **              information on EasyRequest.
  226. **
  227. */
  228. LONG ErrorHandler(enum IFF_ErrorNumber ErrorNumber, APTR ArgList, ...)
  229. {
  230.    register APTR IFFerror_attr;
  231.    register APTR IFFerror_message;
  232.    register enum ErrorExitType ErrorExit;
  233.    register LONG num;
  234.  
  235.    switch( ErrorNumber )
  236.    {
  237.       case IFFerror_NoIntuition:
  238.          CleanLibExit(RETURN_FAIL);    // Terminate IFFConverter
  239.  
  240.       case IFFerror_NoLibrary:
  241.          IFFerror_attr    = IFFerror__NULL_Title_Okay;
  242.          IFFerror_message = EST_OpenLibErr;
  243.          ErrorExit        = Lib_Exit;
  244.          break;
  245.       
  246.       case IFFerror_NoMemory:
  247.          IFFerror_attr    = IFFerror__PanelWindow_Title_Okay;
  248.          IFFerror_message = EST_AllocMemErr;
  249.          ErrorExit        = Clean_Exit;
  250.          break;
  251.          
  252.       case IFFerror_NoMemoryDoReturn:
  253.          IFFerror_attr    = IFFerror__PanelWindow_Title_Okay;
  254.          IFFerror_message = EST_AllocMemErr;
  255.          ErrorExit        = No_Exit;
  256.          break;
  257.          
  258.       case IFFerror_NoLock:
  259.          IFFerror_attr    = IFFerror__PanelWindow_Title_Okay;
  260.          IFFerror_message = EST_LockErr;
  261.          ErrorExit        = Clean_Exit;
  262.          break;
  263.          
  264.       case IFFerror_NoLockDoReturn:
  265.          IFFerror_attr    = IFFerror__PanelWindow_Title_RetryCancel;
  266.          IFFerror_message = EST_LockErr;
  267.          ErrorExit        = No_Exit;
  268.          break;
  269.          
  270.       case IFFerror_GadCreate:
  271.          IFFerror_attr    = IFFerror__PanelWindow_Title_Okay;
  272.          IFFerror_message = EST_GadCreate;
  273.          ErrorExit        = Clean_Exit;
  274.          break;
  275.          
  276.       case IFFerror_OpenErr:
  277.          IFFerror_attr    = IFFerror__NULL_Title_Okay;
  278.          IFFerror_message = EST_OpenErr;
  279.          ErrorExit        = Clean_Exit;
  280.          break;
  281.          
  282.       case IFFerror_NoVisIErr:
  283.          IFFerror_attr    = IFFerror__NULL_Title_Okay;
  284.          IFFerror_message = EST_NoVisIErr;
  285.          ErrorExit        = Clean_Exit;
  286.          break;
  287.          
  288.       case IFFerror_AllocErr:
  289.          IFFerror_attr    = IFFerror__PanelWindow_Title_Okay;
  290.          IFFerror_message = EST_AllocErr;
  291.          ErrorExit        = Clean_Exit;
  292.          break;
  293.          
  294.       case IFFerror_NotFound:
  295.          IFFerror_attr    = IFFerror__PanelWindow_Title_Okay;
  296.          IFFerror_message = EST_NotFound;
  297.          ErrorExit        = No_Exit;
  298.          break;
  299.          
  300.       case IFFerror_Fail:
  301.          IFFerror_attr    = IFFerror__PanelWindow_Title_Okay;
  302.          IFFerror_message = EST_Fail;
  303.          ErrorExit        = No_Exit;
  304.          break;
  305.          
  306.       case IFFerror_NoIFFErr:
  307.          IFFerror_attr    = IFFerror__PanelWindow_Title_Okay;
  308.          IFFerror_message = EST_NoIFFErr;
  309.          ErrorExit        = No_Exit;
  310.          break;
  311.          
  312.       case IFFerror_AslNoFreeStore:
  313.          IFFerror_attr    = IFFerror__PanelWindow_Title_Okay;
  314.          IFFerror_message = EST_AslNoFreeStore;
  315.          ErrorExit        = Clean_Exit;
  316.          break;
  317.          
  318.       case IFFerror_AslNoMoreEntries:
  319.          IFFerror_attr    = IFFerror__PanelWindow_Title_Okay;
  320.          IFFerror_message = EST_AslNoMoreEntries;
  321.          ErrorExit        = No_Exit;
  322.          break;
  323.          
  324.       case IFFerror_NotImplemented:
  325.          IFFerror_attr     = IFFerror__PanelWindow_Title_Okay;
  326.          IFFerror_message  = EST_NotImplemented;
  327.          ErrorExit         = No_Exit;
  328.          break;
  329.       
  330.       case IFFerror_NotOpen:
  331.          IFFerror_attr     = IFFerror__PanelWindow_Title_Okay;
  332.          IFFerror_message  = EST_OpenErr;
  333.          ErrorExit         = No_Exit;
  334.          break;
  335.          
  336.       case IFFerror_FileExistsAsk:
  337.          IFFerror_attr     = IFFerror__PanelWindow_Title_ROverWC;
  338.          IFFerror_message  = EST_FileExistsAsk;
  339.          ErrorExit         = No_Exit;
  340.          break;
  341.    }
  342.  
  343.    num = MyDisplayError(IFFerror_attr, IFFerror_message, &ArgList);
  344.    
  345.    switch( ErrorExit )
  346.    {
  347.       case Lib_Exit:
  348.          CleanLibExit(RETURN_FAIL);
  349.       case Clean_Exit:
  350.          CleanExit(RETURN_FAIL);
  351.       case No_Exit:
  352.          return( num );             // Return the result of DisplayError.
  353.    }
  354. }
  355.  
  356.  
  357. /*
  358. **  CleanExit(ReturnValue)
  359. **
  360. **     Exits the program. Before you can actualy leave the program you
  361. **     must give back all alloceted memory and structures, opened
  362. **     libraries and devices, etc. So, that's done here for you, before
  363. **     we quit.
  364. **
  365. **  pre:  ReturnValue - Error number to be passed on to exit(rv).
  366. **  post: None.
  367. **
  368. */
  369. void CleanExit(int ReturnValue)
  370. {
  371.    FreeAsl_Requests();
  372.    CloseWindows();
  373.    FreeGadgets(FirstGadget);
  374.    CloseScreens();
  375.    FreeMemory();
  376.    CloseFonts();
  377.    CloseLibraries();    // Close all open Libraries
  378.    
  379.    exit(ReturnValue);
  380. }
  381.  
  382.  
  383. /*
  384. **  CleanLibExit(ReturnValue)
  385. **
  386. **     Like 'CleanExit' but no all libraries need to be open, assuming
  387. **     so is dangerous. 'CleanLibExit' NEVER uses possible open libraries.
  388. **     This means that 'CleanLibExit' only tries to close all opened
  389. **     libraries through 'CloseLibraries'. Nothing else should be opened,
  390. **     allocated or anything else other than opend libraries.
  391. **
  392. **  pre:  ReturnValue - Error number to be passed on to exit(rv).
  393. **  post: None.
  394. **
  395. */
  396. void CleanLibExit(int ReturnValue)
  397. {
  398.    CloseLibraries();   // Save to call, because exec.library is always open!
  399.    
  400.    exit(ReturnValue);
  401. }
  402.  
  403.  
  404. /*
  405. **  PreRelease()
  406. **
  407. **     Shows a message to notify the user that he/she is using the
  408. **     pre-release version of IFFConverter. In it is stated that this
  409. **     version is *NOT* guaranteed to be in perfect working order. Nor
  410. **     all functions have to be implemented. Use of it is on the user
  411. **     own risk! This sub-routine returns when the user acknowleges
  412. **     the message.
  413. **
  414. ** pre:  None.
  415. ** post: None.
  416. **
  417. */
  418. void PreRelease()
  419. {
  420.    struct EasyStruct PreReleaseES = {
  421.       sizeof (struct EasyStruct),
  422.       0,
  423.       "Pre-release note:",
  424.       {
  425.          "\n"
  426.          "IFFConverter V" VersionQ "\n\n"
  427.          "Using this version is done on your own risk. This version\n"
  428.          "is under construction. Which means that I can give no\n"
  429.          "guarentees about its behaviour. In other words, some\n"
  430.          "functions may have been implemented but they may behave\n"
  431.          "not the way you'd expect, or even cause a software-failure\n"
  432.          "So, I suggest you'd better get V1.0+ of IFFConverter and\n"
  433.          "*not* use this pre-release version.\n"
  434.       },
  435.       "I'm supposed to be intelligent",
  436.    };
  437.    
  438.    ULONG iflags = 0;
  439.    
  440.    EasyRequest(NULL, &PreReleaseES, &iflags);   // Show EasyRequester
  441. }
  442.  
  443.  
  444.  
  445. /*
  446. **  num = DisplayError(ErrorWindow, IDCMPFlags, ErrorTitle, ErrorText,
  447. **                     ErrorGadgets, ArgList)
  448. **
  449. **     Displays an Error (or Message) through an EasyRequest. Take
  450. **     adventage of this function, DisplayError does the initialistions
  451. **     for you, so you don't have to be bored.
  452. **
  453. **  pre:  ErrorWindow  - Pointer to the window on which EasyRequest will
  454. **                        be shown.
  455. **        IDCMPFlags   - Your IDCMPFlags for EasyRequest.
  456. **        ErrorTitle   - What's the name of your EasyRequest Window.
  457. **        ErrorText    - What's the text to displayed by EasyRequest.
  458. **        ErrorGadgets - What are your Gadgets EasyRequest will use.
  459. **        ArgList      - Pointer to a list of arguments to be passed on
  460. **                       to EasyRequestArgs.
  461. **  post: num - Whatever EasyRequest returns. See AutoDocs for more
  462. **              information on EasyRequest.
  463. **
  464. */
  465. LONG DisplayError(struct Window *ErrorWindow, ULONG IDCMPFlags, STRPTR ErrorTitle, STRPTR ErrorText, STRPTR ErrorGadgets, APTR ArgList)
  466. {
  467.    struct EasyStruct ES_ErrorMessage;
  468.    ULONG IFlags = IDCMPFlags;
  469.    
  470.    ES_ErrorMessage.es_StructSize = sizeof (struct EasyStruct);
  471.    ES_ErrorMessage.es_Flags = 0;
  472.    ES_ErrorMessage.es_Title = ErrorTitle;
  473.    ES_ErrorMessage.es_TextFormat = ErrorText;
  474.    ES_ErrorMessage.es_GadgetFormat = ErrorGadgets;
  475.  
  476.    return( EasyRequestArgs(ErrorWindow, &ES_ErrorMessage, &IFlags, ArgList) );
  477. }
  478.  
  479.  
  480. /*
  481. **  num = MyDisplayError( IFFerror_attr[], IFFerror_message, ArgList )
  482. **
  483. **     Pre-process data for DisplayError. All necessary data is to be
  484. **     found in the array IFFError_attr and IFFerror_message.
  485. **
  486. **  pre:  IFFerror_attr - Address of array containing information on
  487. **                        the error message. See DisplayError and ErrorText.h
  488. **                        for more information.
  489. **        IFFerror_message - Address of message to be displayed.
  490. **  post: num - Whatever DisplayError returns.
  491. **
  492. */
  493. LONG MyDisplayError(APTR IFFerror_attr[], APTR IFFerror_message, APTR ArgList)
  494. {
  495.    if( IFFerror_attr[0] == NULL )
  496.       return( DisplayError(NULL, NULL, IFFerror_attr[1], IFFerror_message, IFFerror_attr[2], ArgList) );
  497.    else
  498.    {
  499.       APTR *Temp = IFFerror_attr[0];
  500.       return( DisplayError( *Temp, NULL, IFFerror_attr[1], IFFerror_message, IFFerror_attr[2], ArgList) );
  501.    }
  502. }
  503.