home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d03xx / d0303.lha / FixIcons / FixIcons.c < prev    next >
C/C++ Source or Header  |  1990-01-08  |  9KB  |  277 lines

  1. /*****************************************************************************/
  2. #include <libraries/dos.h>
  3. #include <exec/memory.h>
  4. #include <workbench/workbench.h>
  5. #include <workbench/icon.h>
  6. #include <stdio.h>
  7. #include <ctype.h>
  8.  
  9. struct Library *OpenLibrary();
  10. void *AllocMem();
  11. struct Lock *Lock();
  12.  
  13. struct Library *IconBase=NULL;
  14.  
  15. char *Tools[200],*Replace[30],GetSubDirs=1,CDName[1000],WantedTool[80];
  16. int ReplaceNr[200];
  17. /*****************************************************************************/
  18. OpenAll()
  19. {  
  20.     if (!(IconBase = (struct Library *) OpenLibrary("icon.library",0L)))
  21.         abort("ERROR: No icon.library in libs:");
  22.     if ((Tools[0]=AllocMem(80L,0L))==NULL) abort("Not enough memory!");
  23.     if ((Replace[0]=AllocMem(80L,0L))==NULL) abort("Not enough memory!");
  24. }
  25. /*****************************************************************************/
  26. abort(errortext)
  27. char *errortext;
  28. {
  29.     int i;
  30.  
  31.     if (errortext!=0) puts(errortext);
  32.     for (i=0;Tools[i]!=0;i++)
  33.     {
  34.         if (Tools[i]!=0) FreeMem(Tools[i],80L);
  35.         if ((Replace[i]!=0)&&(i<30)) FreeMem(Replace[i],80L);
  36.     }
  37.     if (IconBase) CloseLibrary(IconBase);
  38.     exit(0);
  39. }
  40. /*****************************************************************************/
  41. PrintUsage()
  42. {
  43.     puts("FixIcons ver 1.2 by Lars R. Clausen");
  44.     puts("This program is PD, copy it as you want. (Boykot hacking!)");
  45.     puts("Usage: FixIcons [rootdir] [-r|-w|-lTool] [-s] [-v] [-n] [-ddefinefile] ");
  46.     puts("         Modes:       -r        Read only.");
  47.     puts("                      -w        Also write replacements.");
  48.     puts("                      -l[Tool]  Search for defaulttool Tool only.");
  49.     puts("         Other stuff: -s        Don't scan subdirectories.");
  50.     puts("                      -v        Be verbose.");
  51.     puts("                      -n        Don't load definefile.");
  52.     puts("                      -d[File]  Use File as definefile.");
  53.     puts("  Default is: 'Fixicons dh0: -w -dS:FixIcons.Define'");
  54.     abort(0L);
  55. }
  56. /*****************************************************************************/
  57. int strcmpi(s,t)
  58. char *s,*t;
  59. {
  60.     for (;toupper(*s)==toupper(*t);s++,t++)
  61.         if (*s=='\0') return(0);
  62.     return(*s-*t);
  63. }
  64.  
  65. /*****************************************************************************/
  66. int RemoveDups(list,string,maxstring)
  67. char **list;
  68. char *string;
  69. int maxstring;
  70. {
  71.     int i;
  72.     
  73.     for (i=0;i<maxstring;i++)
  74.         if (strcmpi(list[i],list[maxstring])==0)
  75.             return(i);
  76.     return(-1);
  77. }
  78. /*****************************************************************************/
  79. int GetDefineFile(filename,mode)
  80. char *filename;
  81. int mode;
  82. {
  83.     int i=1,j,k,l,dupnr;
  84.     char replacetool[80];
  85.     FILE *definefile;
  86.     
  87.     if ((definefile=fopen(filename,"r"))!=0)
  88.     {
  89.         for (i=0,j=0,l=0;(k=getc(definefile))!=EOF;j++)
  90.         {
  91.             if (k=='\n')
  92.             {
  93.                 Tools[i][j]=0;
  94.                 j=0;
  95.                 i++;
  96.                 if ((Tools[i]=AllocMem(80L,0L))==NULL) abort("Not enough memory!");
  97.             }
  98.             if (k=='=')
  99.             {
  100.                 for (j--;(isspace(Tools[i][j]))&&(j>=0);Tools[i][j]=0,j--);
  101.                 for (k=getc(definefile);isspace(k);k=getc(definefile));
  102.                 replacetool[0]=k;
  103.                 for (j=1;(k=getc(definefile))!='\n';j++)
  104.                     replacetool[j]=k;
  105.                 replacetool[j]=0;
  106.                 strcpy(Replace[l],replacetool);
  107.                 dupnr=RemoveDups(Replace,Replace[l],l);
  108.                 ReplaceNr[i]=l;
  109.                 if (dupnr==-1)
  110.                 {
  111.                     if ((Replace[++l]=AllocMem(80L,0L))==NULL) abort("Not enough memory!");
  112.                 }
  113.                 else ReplaceNr[i]=dupnr;
  114.                 j=0;
  115.                 i++;
  116.                 if ((Tools[i]=AllocMem(80L,0L))==NULL) abort("Not enough memory!");
  117.             }
  118.             Tools[i][j]=k;
  119.             if (k=='\n') j--;
  120.         }
  121.         fclose(definefile);
  122.     }
  123.     else
  124.     if (mode%3==1) abort("Couldn't find definefile");
  125.     if (mode==1)
  126.     {
  127.         puts("The following replacements will be used:");
  128.         for (j=0;j<i;j++)
  129.         if (ReplaceNr[j]!=-1) printf("%-35s => %s.\n",Tools[j],Replace[ReplaceNr[j]]);
  130.     }
  131.     return(i);
  132. }
  133. /*****************************************************************************/
  134. PutDefineFile(tools,filename)
  135. int tools;
  136. char *filename;
  137. {
  138.     FILE *definefile;
  139.     int i;
  140.  
  141.     if ((definefile=fopen(filename,"w"))!=0)
  142.     {
  143.         printf("Writing defines to file %s.\n",filename);
  144.         for (i=0;i<tools;i++)
  145.             if (ReplaceNr[i]!=-1)
  146.                 fprintf(definefile,"%s = %s\n",Tools[i],Replace[ReplaceNr[i]]);
  147.             else
  148.                 fprintf(definefile,"%s\n",Tools[i]);
  149.         fclose(definefile);
  150.     }
  151.     else printf("Couldn't write defines to file '%s'.\n",filename);
  152. }
  153. /*****************************************************************************/
  154. int GetTools(dir,toolnr,mode)
  155. char *dir;
  156. int toolnr;
  157. int mode;
  158. {
  159.     struct Lock *lock;
  160.     int olddirlen,cdlen,internaltoolnr=toolnr,dupnr;
  161.     short succes=1;
  162.     char filename[100];
  163.     struct FileInfoBlock *fileinfoblock;
  164.     struct DiskObject *diskobj;
  165.  
  166.     olddirlen=strlen(CDName);
  167.     if ((CDName[olddirlen-1]!=':')&&(olddirlen>0))
  168.         strcat(CDName,"/");
  169.     strcat(CDName,dir);
  170.     if (mode<2) printf("Now examining %s...\n",CDName);
  171.     if ((fileinfoblock=AllocMem((long)sizeof(struct FileInfoBlock),(long)0L))==NULL)
  172.         abort("Couldn't allocate FileInfoBlock");
  173.     if ((lock=Lock(CDName,ACCESS_READ))==0) abort("Couldn't find directory.");
  174.     if (Examine(lock,fileinfoblock)==0) abort("Couldn't examine directory.");
  175.     succes=ExNext(lock,fileinfoblock);
  176.     strcpy(filename,fileinfoblock->fib_FileName);
  177.     for (;(internaltoolnr<100)&&(succes!=0);)
  178.     {
  179.         if ((fileinfoblock->fib_DirEntryType>0)&&(GetSubDirs==1))
  180.             internaltoolnr=GetTools(fileinfoblock->fib_FileName,internaltoolnr,mode);
  181.         else
  182.             if (strcmp(&filename[strlen(filename)-5],".info")==0)
  183.             {
  184.                 filename[strlen(filename)-5]=0;
  185.                 cdlen=strlen(CDName);
  186.                 if ((CDName[cdlen-1]!=':')&&(cdlen>0))
  187.                     strcat(CDName,"/");
  188.                 strcat(CDName,filename);
  189.                 if ((diskobj=GetDiskObject(CDName))!=0)
  190.                     if (diskobj->do_Type==WBPROJECT)
  191.                     {
  192.                         if (mode%3==2)
  193.                         {
  194.                             if (strcmpi(diskobj->do_DefaultTool,WantedTool)==0)
  195.                                 printf("Tool %s found in %s.info.\n",WantedTool,CDName);
  196.                         }
  197.                         else
  198.                         {
  199.                             strcpy(Tools[internaltoolnr],diskobj->do_DefaultTool);
  200.                             dupnr=RemoveDups(Tools,Tools[internaltoolnr],internaltoolnr);
  201.                             if (dupnr==-1)
  202.                             {
  203.                                 internaltoolnr++;
  204.                                 if (mode<2) printf("Found new tool '%s' in '%s'.\n",diskobj->do_DefaultTool,CDName);
  205.                                 if ((Tools[internaltoolnr]=AllocMem(80L,0L))==NULL)
  206.                                 {
  207.                                     FreeDiskObject(diskobj);
  208.                                     abort("Not enough memory!");
  209.                                 }
  210.                             }
  211.                             else
  212.                             {
  213.                                 if (((mode==1)||(mode==4))&&(ReplaceNr[dupnr]!=-1))
  214.                                 {
  215.                                     if (mode<2) printf("Changing %s.info from %s to %s.\n",
  216.                                         CDName,diskobj->do_DefaultTool,Replace[ReplaceNr[dupnr]]);
  217.                                     strcpy(diskobj->do_DefaultTool,Replace[ReplaceNr[dupnr]]);
  218.                                     if (PutDiskObject(CDName,diskobj)==0)
  219.                                         printf("Hey! Disk or file %s is write-protected!\n",CDName);
  220.                                 }
  221.                             }
  222.                         }
  223.                         FreeDiskObject(diskobj);
  224.                     }
  225.                     else FreeDiskObject(diskobj);
  226.                 CDName[cdlen]=0;
  227.             }
  228. /* Not all disk perform ExNext properly, it seems. Could it be Aztec? */
  229.         succes=ExNext(lock,fileinfoblock);
  230.         strcpy(filename,fileinfoblock->fib_FileName);
  231.     }
  232.     UnLock(lock);
  233.     FreeMem(fileinfoblock,(long)sizeof(struct FileInfoBlock));
  234.     CDName[olddirlen]=0;
  235.     return(internaltoolnr);
  236. }
  237.         
  238. /*****************************************************************************/
  239. main(argc,argv)
  240. int argc;
  241. char **argv;
  242. {
  243.     int tools=0,i,mode=4,erasedefine=0;
  244.     char *filename="dh0:",*definefilename="S:FixIcons.define";
  245.     
  246.     if ((argc==2)&&((strcmpi(argv[1],"help")==0)||(strcmp(argv[1],"?")==0))) PrintUsage();
  247.     printf("FixIcons ver 1.2 (c) 1989 by Lars R. Clausen.\n");
  248.     for (i=1;i<argc;i++)
  249.     {
  250.         if (strcmp(argv[i],"-s")==0) GetSubDirs=0;
  251.         if (strcmp(argv[i],"-r")==0) mode=3;/* Read-only mode */
  252.         if (strcmp(argv[i],"-w")==0) mode=4;/* Also write defaults */
  253.         if (strcmp(argv[i],"-v")==0) mode-=3;/* Verbose mode */
  254.         if (strcmp(argv[i],"-n")==0) erasedefine=1;
  255.         if (strncmp(argv[i],"-l",2)==0)
  256.         {
  257.             mode=5;
  258.             strcpy(WantedTool,&argv[i][2]);/* Search for tool */
  259.         }
  260.         if (strncmp(argv[i],"-d",2)==0)    definefilename=&argv[i][2];
  261.         if (strncmp(argv[i],"-",1)!=0) filename=argv[i];
  262.     }
  263.     for (i=0;i<200;i++) {ReplaceNr[i]=-1;Tools[i]=0;};
  264.     for (i=0;i<30;i++) Replace[i]=0;
  265. /* Allocate first tool and replacement, and open IconLibrary */
  266.     OpenAll();
  267. /* Load definefile */
  268.     if ((mode%3!=2)&&(erasedefine==0)) tools=GetDefineFile(definefilename,mode);
  269.  
  270.     if (mode==2) printf("Searching for %s in %s.\n",WantedTool,filename);
  271. /* Core of the program - recursive scanning routine */
  272.     tools=GetTools(filename,tools,mode);
  273. /* Output definefile */
  274.     if (mode%3!=2) PutDefineFile(tools,definefilename);
  275.     abort(0L);
  276. }
  277.