home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 276.lha / InfoChange_v1.0 / InfoChange.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-07  |  8.2 KB  |  379 lines

  1. #include <ctype.h>
  2. #include <libraries/dosextens.h>
  3. #include <intuition/intuition.h>
  4. #include <workbench/startup.h>
  5. #include <workbench/workbench.h>
  6. #include <workbench/icon.h>
  7.  
  8. #define REGS register
  9.  
  10.  
  11. /*-------------------------------------------------------------------------+
  12.  |                                       |
  13.  | Name:    atos                               |
  14.  | Purpose: convert a string to a short integer                |
  15.  |                                       |
  16.  | Author:  RWA                    Date: 6/89           |
  17.  +-------------------------------------------------------------------------*/
  18. short atos(s)
  19. REGS char *s;
  20. {
  21. REGS short n;
  22. for( n = 0; (*s) >= '0' && (*s) <= '9'; s++)
  23.    n = 10 * n + (*s) - '0';
  24. return(n);
  25. }
  26.  
  27. typedef struct
  28. {
  29. short ToolNum;
  30. char *Tool;
  31. } ToolType;
  32.  
  33. #define PROJECT_ONLY 1
  34. #define TOOL_ONLY 2
  35.  
  36. typedef struct
  37. {
  38. short only;
  39. short verify;
  40. char *files[21];
  41. short StackSize;
  42. char *DefaultTool;
  43. ToolType ToolTypes[21];
  44. } NewInfo;
  45.  
  46. /*-------------------------------------------------------------------------+
  47.  |                                       |
  48.  | Name:    message                               |
  49.  | Purpose: dispalys a string on the CLI                   |
  50.  |                                       |
  51.  | Author:  RWA                    Date: 7/89           |
  52.  +-------------------------------------------------------------------------*/
  53.  
  54. struct FileHandle *cliout;
  55.  
  56. void message(str)
  57. char *str;
  58. {
  59. Write(cliout,str,(long)strlen(str));
  60. }
  61.  
  62. void messages(strs)
  63. char **strs;
  64. {
  65. while( *strs ) { message(*strs); strs++; }
  66. }
  67.  
  68. char *help[]=
  69. {
  70. "\nInfoChange V1.0 by Robert W. Albrecht\n",
  71. "\n",
  72. "SYNTAX:\n",
  73. "\n",
  74. "InfoChange <files> -Yn<tool type> -S<stacksize> -D<tool> -V -T -P\n",
  75. "\n",
  76. "Where:\n",
  77. "-D  sets the default tool to <tool>\n",
  78. "-S  changes the stack size used by default tool\n",
  79. "-Yn sets tool type number 'n' to <tool type>\n",
  80. "-V  will ask for verification before changing an icon\n",
  81. "-T  do only \"Tool\" (program) icons\n",
  82. "-P  do only \"Project\" (file) icons\n",
  83. "\n",
  84. "Wild cards are accepted for the file name(s). (* and ?)\n",
  85. "Only Project and Tool \".info\" files can be changed.\n\n",
  86. 0L,
  87. };
  88.  
  89. /*-------------------------------------------------------------------------+
  90.  |                                       |
  91.  | Name:    process_args                           |
  92.  | Purpose: processes command line arguments                   |
  93.  |                                       |
  94.  | Author:  RWA                    Date: 7/89           |
  95.  +-------------------------------------------------------------------------*/
  96. short process_args(ni,ac,av)
  97. NewInfo *ni;
  98. short ac;
  99. char *av[];
  100. {
  101. REGS short i;
  102. ToolType *ToolTypes;
  103. char **files, *a;
  104. short n;
  105.  
  106. ToolTypes = ni->ToolTypes;
  107. files = ni->files;
  108.  
  109. for( i = 1; i < ac; i++)
  110.    {
  111.    if( av[i][0] == '-' )
  112.       {
  113.       switch( _toupper(av[i][1]) )
  114.      {
  115.      case 'Y':
  116.         if( (n = atos(&av[i][2])) > 0 )
  117.            {
  118.            ToolTypes->ToolNum = n;
  119.            a = &av[i][2];
  120.            while( *a >= '0' && *a <= '9' ) a++;
  121.            ToolTypes->Tool = a;
  122.            ToolTypes++;
  123.            }
  124.      break;
  125.  
  126.      case 'S':
  127.         if( (n = atos(&av[i][2])) > 0 )
  128.            ni->StackSize = n;
  129.      break;
  130.  
  131.      case 'D':
  132.         ni->DefaultTool = &av[i][2];
  133.      break;
  134.  
  135.      case 'V':
  136.         ni->verify = 1;
  137.      break;
  138.  
  139.      case 'P':
  140.         ni->only = PROJECT_ONLY;
  141.      break;
  142.  
  143.      case 'T':
  144.         ni->only = TOOL_ONLY;
  145.      break;
  146.  
  147.      default:
  148.         *files = av[i];
  149.         files++;
  150.      break;
  151.      }
  152.       }
  153.    else
  154.       {
  155.       *files = av[i];
  156.       files++;
  157.       }
  158.    }
  159. }
  160.  
  161. /*-------------------------------------------------------------------------+
  162.  |                                       |
  163.  | Name:    check_todo                               |
  164.  | Purpose: makes sure there is something to do                |
  165.  |                                       |
  166.  | Author:  RWA                    Date: 7/89           |
  167.  +-------------------------------------------------------------------------*/
  168. short check_todo(ni)
  169. NewInfo *ni;
  170. {
  171. if( ni->DefaultTool || ni->StackSize || ni->ToolTypes[0].ToolNum )
  172.    return(1);
  173. return(0);
  174. }
  175.  
  176. /*-------------------------------------------------------------------------+
  177.  |                                       |
  178.  | Name:    strlwr                               |
  179.  | Purpose: converts a string to all lower case                |
  180.  |                                       |
  181.  | Author:  RWA                    Date: 7/89           |
  182.  +-------------------------------------------------------------------------*/
  183. void strlwr(s)
  184. char *s;
  185. {
  186. while( *s )
  187.    {
  188.    *s = _tolower(*s);
  189.    s++;
  190.    }
  191. }
  192.  
  193. /*-------------------------------------------------------------------------+
  194.  |                                       |
  195.  | Name:    fix_icon                               |
  196.  | Purpose: changes a disk object structure and stores it to disk       |
  197.  |                                       |
  198.  | Author:  RWA                    Date: 7/89           |
  199.  +-------------------------------------------------------------------------*/
  200. void fix_icon(dobj,ni,fname)
  201. struct DiskObject *dobj;
  202. NewInfo *ni;
  203. char *fname;
  204. {
  205. long PutDiskObject();
  206. struct DiskObject new_dobj;
  207. char *tooltypes[30], *ptr = 0L;
  208.  
  209. if( ni->verify )
  210.    {
  211.    char ibuf[4];
  212.    message("Change");
  213.    if( dobj->do_Type == WBTOOL )
  214.       message(" Tool?");
  215.    else message(" Project?");
  216.    message(" <Y/N> [Y]");
  217.    Read(cliout,ibuf,4L);
  218.    if( ibuf[0] == 'n' || ibuf[0] == 'N' )
  219.       return;
  220.    }
  221.  
  222. movmem(dobj,&new_dobj,sizeof(struct DiskObject));
  223.  
  224. if( dobj->do_Type == WBPROJECT && ni->DefaultTool )
  225.    new_dobj.do_DefaultTool = ni->DefaultTool;
  226.  
  227. if( ni->StackSize ) new_dobj.do_StackSize = ni->StackSize;
  228.  
  229. if( ni->ToolTypes[0].ToolNum )
  230.    {
  231.    ToolType *t;
  232.    REGS short num_tooltypes, i;
  233.    REGS char **a;
  234.  
  235.    num_tooltypes = 0;
  236.  
  237.    if( a = dobj->do_ToolTypes )
  238.       {
  239.       while( *a++ )
  240.      if( (num_tooltypes++) >= 30 ) break;
  241.  
  242.       for( i = 0, a = dobj->do_ToolTypes; i < num_tooltypes && i < 30; i++)
  243.      tooltypes[i] = a[i];
  244.       }
  245.  
  246.    for(t = ni->ToolTypes; t->ToolNum; t++)
  247.       {
  248.       if( t->ToolNum > num_tooltypes && num_tooltypes < 30 )
  249.      tooltypes[num_tooltypes++] = t->Tool;
  250.       else if( t->ToolNum < 30 ) tooltypes[t->ToolNum - 1] = t->Tool;
  251.       }
  252.  
  253.    if( num_tooltypes )
  254.       {
  255.       tooltypes[num_tooltypes] = 0L;
  256.       new_dobj.do_ToolTypes = tooltypes;
  257.       }
  258.    }
  259.  
  260. if( PutDiskObject(fname,&new_dobj) )
  261.    message("Done");
  262. else message("Error Saving");
  263. }
  264.  
  265. /*-------------------------------------------------------------------------+
  266.  |                                       |
  267.  | Name:    icon_change                            |
  268.  | Purpose: performs changes on a icon                       |
  269.  |                                       |
  270.  | Author:  RWA                    Date: 7/89           |
  271.  +-------------------------------------------------------------------------*/
  272. void icon_change(fname,ni)
  273. char *fname;
  274. NewInfo *ni;
  275. {
  276. char *a, buf[256], *strcpy();
  277. struct DiskObject *dobj, *GetDiskObject();
  278.  
  279. strcpy(buf,fname);
  280. strlwr(a = buf);
  281.  
  282. while( *a )   /* look for the .info extention */
  283.    {
  284.    if( *a == '.' ) if( !strcmp(a,".info") ) break;
  285.    a++;
  286.    }
  287. if( *a )
  288.    {
  289.    message(strcpy(buf,fname));
  290.    message(" ... ");
  291.    *a = '\0';
  292.  
  293.    if( dobj = GetDiskObject(buf) )
  294.       {
  295.       if( dobj->do_Type == WBTOOL && ni->only != PROJECT_ONLY )
  296.      fix_icon(dobj,ni,buf);
  297.       else if( dobj->do_Type == WBPROJECT && ni->only != TOOL_ONLY )
  298.      fix_icon(dobj,ni,buf);
  299.       else
  300.      message("Icon of Wrong Type");
  301.       FreeDiskObject(dobj);
  302.       }
  303.    else
  304.       message("Can't be Loaded");
  305.  
  306.    message("\n");
  307.    }
  308. }
  309.  
  310. /*-------------------------------------------------------------------------+
  311.  |                                       |
  312.  | Name:    change_icons                           |
  313.  | Purpose: perform the specified changes on the specified icons       |
  314.  |                                       |
  315.  | Author:  RWA                    Date: 7/89           |
  316.  +-------------------------------------------------------------------------*/
  317. void change_icons(ni)
  318. NewInfo *ni;
  319. {
  320. char **files, *fname, *scdir();
  321.  
  322. files = ni->files;
  323.  
  324. while( *files )
  325.    {
  326.    while( fname = scdir(*files) )
  327.       icon_change(fname,ni);
  328.    files++;
  329.    }
  330.  
  331. }
  332.  
  333. /*-------------------------------------------------------------------------+
  334.  |                                       |
  335.  | Name:    main                               |
  336.  | Purpose: main routine                           |
  337.  |                                       |
  338.  | Author:  RWA                    Date: 7/89           |
  339.  +-------------------------------------------------------------------------*/
  340.  
  341. main(argc,argv)
  342. short argc;
  343. char *argv[];
  344. {
  345. static NewInfo newinfo;
  346. static char copyright[] =
  347. {"Copyright (C) 1989 by Robert W. Albrecht, All Rights Reserved."};
  348. extern void *OpenLibrary(), CloseLibrary(), *IconBase, *Open(), Close();
  349.  
  350. if( !argc ) _exit(0);
  351.  
  352. if( !(IconBase = OpenLibrary(ICONNAME,0L)) )
  353.    _exit(11);
  354.  
  355. /* open up a way to the outside world */
  356. if( !(cliout = Open("*",(long)MODE_OLDFILE)) )
  357.    {
  358.    CloseLibrary(IconBase);
  359.    _exit(12);
  360.    }
  361.  
  362. if( argc == 1 ) /* no arguments */
  363.    messages(help);
  364. else
  365.    {
  366.    setmem(&newinfo,sizeof(newinfo),0);
  367.    process_args(&newinfo,argc,argv);
  368.    if( check_todo(&newinfo) )
  369.       change_icons(&newinfo);
  370.    else
  371.       message("I have nothing to do!\n");
  372.    }
  373.  
  374. Close(cliout);
  375. CloseLibrary(IconBase);
  376. _exit(0);
  377. }
  378.  
  379.