home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 2 / FFMCD02.bin / new / gfx / edit / tsmorph / args.c next >
C/C++ Source or Header  |  1993-12-21  |  9KB  |  300 lines

  1. // TSMorph - Amiga Morphing program
  2. // Copyright (C) © 1993  Topicsave Limited
  3.  
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; either version 2 of the License, or
  7. // any later version.
  8.  
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. // GNU General Public License for more details.
  13.  
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program; if not, write to the Free Software
  16. // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. // mpaddock@cix.compulink.co.uk
  19. //    $Author: M_J_Paddock $
  20. //    $Date: 1993/08/28 22:04:09 $
  21. //    $Revision: 1.6 $
  22.  
  23. /* This object is included in both TSMorph and TSMorph-render
  24.  * it includes all the stuff for determing arguments
  25.  * including settings file stuff
  26.  */
  27.  
  28. // include headers since does not include precompiled headers
  29. #include <workbench/workbench.h>
  30. #include <workbench/startup.h>
  31. #include <proto/icon.h>
  32. #include <proto/dos.h>
  33. #include <proto/exec.h>
  34. #include <exec/memory.h>
  35. #include <stddef.h>
  36. #include <string.h>
  37.  
  38. // The program icon for settings
  39. struct DiskObject *program_icon = NULL;
  40.  
  41. /* My version of ArgArrayDone
  42.  * does not use cx_lib version as would have to open commodities.library
  43.  * frees everything allocated by argArrayInit
  44.  */
  45. void
  46. argArrayDone( void ) {
  47.     if (program_icon) {
  48.         FreeDiskObject(program_icon);
  49.     }
  50. }
  51.  
  52. /* my version for ArgArrayInit
  53.  * returns   : pointer to the parameters or program tool types
  54.  * parameters: argc
  55.  *             argv - standard main() arguments
  56.  */
  57. char **
  58. argArrayInit( LONG argc, char **argv ) {
  59.     if (argc) {                // from the shell
  60.         argv[argc] = 0;    // HACK!!!!!
  61.         return argv;        // The above is required to prevent the following code
  62.     }                            // falling off the end. Not sure how safe this is!!!
  63.     else {
  64.         struct WBStartup *wbs = (struct WBStartup *) argv;                                // from the Workbench
  65.         if (program_icon = GetDiskObject((char *) wbs->sm_ArgList->wa_Name)) {    // get the program icon
  66.             return ((char **) program_icon->do_ToolTypes);                                // return tooltypes
  67.         }
  68.     }
  69.     return NULL;
  70. }
  71.  
  72. /* My version of ArgString
  73.  * returns   : string result
  74.  * parameters: arg1 - parameter array
  75.  *             arg2 - parameter name
  76.  *             arg3 - default value
  77.  */
  78. UBYTE *
  79. ArgString( UBYTE **arg1, UBYTE *arg2, UBYTE *arg3 ) {
  80.     UBYTE *s;
  81.     if (arg1) {
  82.         if (s = FindToolType(arg1,arg2)) {
  83.             return s;
  84.         }
  85.     }
  86.     return arg3;
  87. }
  88.  
  89. /* The following defines are what make this code specific to TSMorph
  90.  * they could be changed for another program
  91.  */
  92. #define PREFSDIR "ENV:TSMorph/"        // Directory where prefs may be held
  93. #define PREFSFILE "TSMorph.prefs"    // Name of prefs file
  94.  
  95. struct DiskObject    *project_icon             = NULL;    // The project Icon
  96. char                    **project_settings    = NULL;    // Parameters on project Icon
  97. extern char            **ArgArray;                            // Program Parameter/ToolTypes
  98. extern char            **ArgArraySettings;                // Parameters from settings file
  99.  
  100. BOOL                    FromWB;                                // Are we shell or Workbench
  101.  
  102. /* Similar to ArgArrayInit but knows
  103.  * how to handle settings files
  104.  * does not return a value - it is held in this file
  105.  */
  106. void
  107. MyArgArrayInit(int argc,char **argv) {
  108.     BPTR     oldcurrentdir     = NULL;    // Remember the current dir
  109.     BPTR     settings            = 0;        // Settings file handle
  110.     UBYTE    string[65];                    //    buffer to read in (should not be limited size)
  111.     int     kount                = 0,        // number of settings strings in file
  112.             kount1            = 0;        // index
  113.     UBYTE *settingsname;                // file name of settings
  114.     BPTR    projectdir        = 0;        // project directory
  115.     struct WBStartup    *argmsg;        // Workbench message
  116.     struct WBArg         *wb_arg;        // Workbench argument
  117.     /* check if from workbench    */
  118.     if (!argc) {
  119.         FromWB = TRUE;
  120.           argmsg = (struct WBStartup *)argv;
  121.         if (argmsg->sm_NumArgs > 1) {
  122.             wb_arg = argmsg->sm_ArgList;
  123.              wb_arg++;
  124.              projectdir = wb_arg->wa_Lock;                    // set up project dir for settings file
  125.             oldcurrentdir = CurrentDir(projectdir);    // switch to project dir
  126.             if (project_icon = GetDiskObject((char *) wb_arg->wa_Name)) {
  127.                 project_settings = (char **) project_icon->do_ToolTypes;    // and get project tool types if poss.
  128.             }
  129.             CurrentDir(oldcurrentdir);                        // switch back to original dir
  130.             oldcurrentdir = NULL;
  131.         }
  132.     }
  133.     else {
  134.         FromWB = FALSE;
  135.     }
  136.     // Do normal ArgArrayInit
  137.     ArgArray = argArrayInit(argc,argv);
  138.     // Get name of settings file
  139.     ArgArraySettings = NULL;
  140.     settingsname = ArgString(project_settings,"SETTINGS",NULL);    // first from SETTINGS= on project
  141.     if (settingsname) {
  142.         settings = Open(settingsname,MODE_OLDFILE);                    // try and open if present
  143.     }
  144.     if (!settings) {
  145.         settingsname = ArgString(ArgArray,"SETTINGS",NULL);        // if not open try Program parameters/tool types SETTINGS=
  146.     }
  147.     // try and open
  148.     if (settingsname && !settings) {
  149.         settings = Open(settingsname,MODE_OLDFILE);                    // and try and open if present
  150.     }
  151.     if (!settings) {
  152.         /* if not open then if from WorkBench then change
  153.          * to the project dir, if from CLI try current dir
  154.          */
  155.         if (projectdir) {
  156.             oldcurrentdir = CurrentDir(projectdir);
  157.         }
  158.         if (!(settings = Open(settingsname = PREFSFILE,MODE_OLDFILE))) {    // try and open again in "current" dir
  159.             // change back if it did not open
  160.             if (projectdir) {
  161.                 CurrentDir(oldcurrentdir);
  162.             }
  163.         }
  164.     }
  165.     // if still not open then try program directory
  166.     if (!settings) {
  167.         settings = Open(settingsname = "PROGDIR:"PREFSFILE,MODE_OLDFILE);    //    try and open again in program dir
  168.     }
  169.     // still not open so try ENV:
  170.     if (!settings) {
  171.         settings = Open(settingsname = PREFSDIR PREFSFILE,MODE_OLDFILE);    // and again!!! in ENV:/xxx/
  172.     }
  173.     if (settings) {                                                                        // We have a settings file!!!
  174.         // If we have found a file then count the relevant lines
  175.         while (FGets(settings,string,64)) {        // read in each line
  176.             if ((string[0] != '\n') &&                // ignore blank lines
  177.                  (string[0] != ';')) {                // and commented out lines
  178.                 ++kount;                                    // count the rest
  179.             }
  180.         }
  181.         Close(settings);                                // close the file
  182.         if (settings = Open(settingsname,MODE_OLDFILE)) {    // reopen it (why not seek?)
  183.             // Allocate memory for settings from file pointers (including zero at end
  184.             if (ArgArraySettings = AllocVec((kount+1) * sizeof(UBYTE *),MEMF_CLEAR)) {
  185.                 // Read in all settings
  186.                 while (FGets(settings,string,64) &&
  187.                          (kount1 < kount)) {                                    // Do not do too many
  188.                     // remove newline (from FGets)
  189.                     string[strlen(string)-1] = '\0';
  190.                     // ignore blank and comment lines
  191.                     if (string[0] && (string[0]!=';')) {
  192.                         // Clone settings in memory
  193.                         ArgArraySettings[kount1] = strdup(string);    // Note!
  194.                         ++kount1;                                                // strdup() not AllocMem() etc.
  195.                     }                                                                // so memory is only freed on quit
  196.                 }
  197.             }
  198.             // Close file and change directory back if required
  199.             Close(settings);
  200.         }
  201.         if (oldcurrentdir) {
  202.             CurrentDir(oldcurrentdir);
  203.         }
  204.     }
  205. }
  206.  
  207. /* Similar to ArgArrayDone but knows
  208.  * how to handle settings files
  209.  */
  210. void
  211. MyArgArrayDone(void) {
  212.     // Call argArrayDone
  213.     argArrayDone();
  214.     // and free settings file pointers
  215.     if (ArgArraySettings) {
  216.         FreeVec(ArgArraySettings);
  217.         ArgArraySettings = NULL;
  218.     }
  219.     // and the project icon
  220.     if (project_icon) {
  221.         FreeDiskObject(project_icon);
  222.         project_icon = NULL;
  223.     }
  224. }
  225.  
  226. /* Similar to ArgString but knows
  227.  * how to handle settings files
  228.  * missing parameter: arg1   - is internal
  229.  * extra parameter  : reopen - set if this is not the initial call but just a new project
  230.  */
  231. UBYTE *
  232. MyArgString(UBYTE *arg2,UBYTE *arg3,BOOL reopen) {
  233.     UBYTE *s;
  234.     // Look in project Parameters first
  235.     if (project_settings) {
  236.         if (s = FindToolType(project_settings,arg2)) {
  237.             return s;
  238.         }
  239.     }
  240.     if (!reopen) {
  241.         // then look in parameters
  242.         if (ArgArray) {
  243.             if (s = FindToolType(ArgArray,arg2)) {
  244.                 return s;
  245.             }
  246.         }
  247.         // then in settings file
  248.         if (ArgArraySettings) {
  249.             if (s = FindToolType(ArgArraySettings,arg2)) {
  250.                 return s;
  251.             }
  252.         }
  253.     }
  254.     return arg3;
  255. }
  256.  
  257. /* Similar to ArgInt but knows
  258.  * how to handle settings files
  259.  * See MyArgString() for different parameters
  260.  */
  261. LONG
  262. MyArgInt(UB