Subversion Repositories filter_foundry

Rev

Rev 11 | Rev 18 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.         This file is part of "Filter Foundry", a filter plugin for Adobe Photoshop
  3.     Copyright (C) 2003-5 Toby Thain, toby@telegraphics.com.au
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by  
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License  
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18. */
  19.  
  20. #include <plstringfuncs.h>
  21. #include <ASRegistry.h>
  22. #include <ctype.h>
  23.  
  24. #include "ff.h"
  25.  
  26. #include "file_compat.h"
  27.  
  28. // MoreFiles headers
  29. #include "FileCopy.h"
  30. #include "MoreFilesExtras.h"
  31.  
  32. OSErr wrstr(FILEREF rn,char *s);
  33. OSErr dopkginfo(FILEREF rn);
  34. OSErr make_bundle(StandardFileReply *sfr, short plugvol,long plugdir,StringPtr plugname);
  35. OSErr doresources(FSSpec *srcplug, FSSpec *rsrccopyfss);
  36. OSErr make_singlefile(StandardFileReply *sfr, short plugvol,long plugdir,StringPtr plugname);
  37. OSErr copyplist(FSSpec *fss, short dstvol,long dstdir);
  38. int copyletters(char *dst,StringPtr src);
  39.  
  40. // prototype for a function included in Carbon's stdlib and declared in /usr/include/string.h
  41. // but missing from MPW Universal header string.h
  42. #ifndef _STRING_H_
  43.         char    *strnstr(const char *, const char *, size_t);
  44. #endif
  45.  
  46. OSErr wrstr(FILEREF rn,char *s){
  47.         long count = strlen(s);
  48.         return FSWrite(rn,&count,s);
  49. }
  50.  
  51. OSErr doresources(FSSpec *srcplug, FSSpec *rsrccopy){
  52.         short srcrn,dstrn;
  53.         Handle hpipl,h;
  54.         long origsize,newsize;
  55.         OSErr e = noErr;
  56.         Str255 title;
  57.  
  58. #ifdef MACMACHO
  59.   // work with resources in data fork
  60.   if( !(e = FSpMakeFSRef(srcplug,&inref))
  61.     &&!(e = FSOpenResourceFile(&inref,0/*forkNameLength*/,NULL/*forkName*/,fsRdPerm,&srcrn))
  62.     &&!(e = FSpMakeFSRef(rsrccopy,&outref))
  63.     && (e = FSOpenResourceFile(&outref,0/*forkNameLength*/,NULL/*forkName*/,fsWrPerm,&dstrn)) )
  64.       CloseResFile(srcrn);
  65. #else
  66.   // ordinary resource fork files
  67.   srcrn = FSpOpenResFile(srcplug,fsRdPerm);
  68.   if(srcrn != -1){
  69.     dstrn = FSpOpenResFile(rsrccopy,fsWrPerm);
  70.     if(dstrn == -1){
  71.       e = ResError();
  72.       CloseResFile(srcrn);
  73.     }
  74.   }else e = ResError();
  75. #endif
  76.  
  77.         if(!e){
  78.  
  79.                 hpipl = Get1Resource('DATA',16000);
  80.                
  81.                 /* create a new PiPL resource for the standalone plugin,
  82.                    with updated title and category strings */
  83.        
  84.                 if(h = Get1Resource('PiPL',16000)){
  85.                         RemoveResource(h);
  86.                        
  87.                         DetachResource(hpipl);
  88.  
  89.                         PLstrcpy(title,gdata->parm.title);
  90.                         if(gdata->parm.popDialog)
  91.                                 PLstrcat(title,"\pÉ");
  92.  
  93.                         origsize = GetHandleSize(hpipl);
  94.                         SetHandleSize(hpipl,origsize+0x300); /* some slop for fixup to work with */
  95.                         HLock(hpipl);
  96.                         newsize = fixpipl((PIPropertyList*) *hpipl,origsize,title);
  97.                         HUnlock(hpipl);
  98.                         SetHandleSize(hpipl,newsize);
  99.                        
  100.                         AddResource(hpipl,'PiPL',16000,"\p");
  101.                 }
  102.  
  103.                 /* do a similar trick with the terminology resource,
  104.                    so the scripting system can distinguish the standalone plugin */
  105.  
  106.                 if(h = Get1Resource(typeAETE,AETE_ID)){
  107.                         origsize = GetHandleSize(h);
  108.                         SetHandleSize(h,origsize+0x100); /* slop for fixup to work with */
  109.                         HLock(h);
  110.                         newsize = fixaete((unsigned char*)*h,origsize,gdata->parm.title);
  111.                         HUnlock(h);
  112.                         SetHandleSize(h,newsize);
  113.                        
  114.                         ChangedResource(h);
  115.                 }
  116.  
  117.                 if( !(e = ResError()) ){
  118.                
  119.                         /* now add PARM resource */
  120.                        
  121.                         if( !(e = PtrToHand(&gdata->parm,&h,sizeof(PARM_T))) ){
  122.                                 AddResource(h,'PARM',PARM_ID,"\p");
  123.                                 e = ResError();
  124.                         }
  125.                 }
  126.                
  127.                 CloseResFile(dstrn);
  128.                 CloseResFile(srcrn);
  129.         }
  130.        
  131.         return e;
  132. }
  133.  
  134. int copyletters(char *dst,StringPtr src){
  135.   int i,n=0;
  136.   for(i=src[0];i--;)
  137.     if(isalpha(*++src)){
  138.       *dst++ = *src;
  139.       ++n;
  140.     }
  141.   return n;
  142. }
  143.  
  144. // Info.plist in new standalone copy needs to be edited
  145. // at least the CFBundleIdentifier property must be unique
  146.  
  147. OSErr copyplist(FSSpec *fss, short dstvol,long dstdir){
  148.   static char *key = "com.telegraphics.FilterFoundry";
  149.   static unsigned char *fname="\pInfo.plist";
  150.   char *buf,*save,*p;
  151.   short rn,dstrn,i,n,m;
  152.   long eof,count;
  153.   OSErr e;
  154.  
  155.   if( !(e = HCreate(dstvol,dstdir,fname,'pled','TEXT')) ){
  156.     if( !(e = HOpenDF(dstvol,dstdir,fname,fsWrPerm,&dstrn)) ){
  157.       if(!(e = FSpOpenDF(fss,fsRdPerm,&rn))){
  158.         if( !(e = GetEOF(rn,&eof)) && (buf = malloc(eof+1024)) ){
  159.           if(!(e = FSRead(rn,&eof,buf))){
  160.             buf[eof] = 0;
  161.             if( (p = strnstr(buf,key,eof)) && (save = malloc(eof-(p-buf)+1)) ){
  162.               p += strlen(key);
  163.               // store text after matched string
  164.               strcpy(save,p);
  165.    
  166.               *p++ = '.';
  167.               n = copyletters(p,gdata->parm.category);
  168.               p += n;
  169.               if(n) *p++ = '.';
  170.               m = copyletters(p,gdata->parm.title);
  171.               p += m;
  172.               if(!m){
  173.                 // generate a random ASCII identifier
  174.                 srand(TICKCOUNT());
  175.                 for(i=8;i--;) *p++ = 'a' + (rand() % 26);
  176.               }
  177.               strcpy(p,save);
  178.    
  179.               count = strlen(buf);
  180.               e = FSWrite(dstrn,&count,buf);
  181.    
  182.               free(save);
  183.             }else e = paramErr; // not found?? shouldn't happen
  184.           }
  185.           free(buf);
  186.         }
  187.         FSClose(rn);
  188.       }
  189.       FSClose(dstrn);
  190.     }
  191.     if(e) HDelete(dstvol,dstdir,fname);
  192.   }
  193.   return e;
  194. }
  195.  
  196. OSErr make_bundle(StandardFileReply *sfr, short plugvol,long plugdir,StringPtr plugname){
  197.         short dstvol = sfr->sfFile.vRefNum;
  198.         long bundledir,contentsdir,macosdir,rsrcdir;
  199.         DInfo fndrInfo;
  200.         OSErr e;
  201.         FSSpec fss,macosfss,rsrcfss,rsrccopyfss;
  202.  
  203.         if( !(e = FSpDirCreate(&sfr->sfFile,sfr->sfScript,&bundledir)) ){
  204.                 if(!(e = FSpGetDInfo(&sfr->sfFile,&fndrInfo)) ){
  205.                         fndrInfo.frFlags |= kHasBundle;
  206.                         FSpSetDInfo(&sfr->sfFile,&fndrInfo);
  207.                 }
  208.                 if( !(e = DirCreate(dstvol,bundledir,"\pContents",&contentsdir)) ){
  209.                         if( !(e = DirCreate(dstvol,contentsdir,"\pMacOS",&macosdir)) ){
  210.                                 if( !(e = DirCreate(dstvol,contentsdir,"\pResources",&rsrcdir)) ){
  211.  
  212.                                         /* directories created ; now we need to copy the Info.plist file, resource file, and executable */
  213.  
  214.                                         if( !(e = FSMakeFSSpec(plugvol,plugdir,"\p::MacOS:FilterFoundry",&macosfss))
  215.                                          && !(e = FileCopy(macosfss.vRefNum,macosfss.parID,macosfss.name, dstvol,macosdir,NULL, NULL,NULL,0,false)) ){
  216.                                                 /* now we add PARM resources to each binary, and edit PiPLs */
  217.             if( !(e = FSMakeFSSpec(plugvol,plugdir,"\p::Resources:FilterFoundry.rsrc",&rsrcfss))
  218.                && !(e = FileCopy(rsrcfss.vRefNum,rsrcfss.parID,rsrcfss.name, dstvol,rsrcdir,NULL, NULL,NULL,0,false))
  219.                && !(e = FSMakeFSSpec(dstvol,rsrcdir,"\pFilterFoundry.rsrc",&rsrccopyfss)) ){
  220.                
  221.                                                    if( !(e = doresources(&rsrcfss, &rsrccopyfss))
  222.               && !(e = FSMakeFSSpec(plugvol,plugdir,"\p::Info.plist",&fss)) ){
  223.                 e = copyplist(&fss,dstvol,contentsdir);
  224.                
  225.                 if(e) FSpDelete(&rsrccopyfss);
  226.                }
  227.                
  228.               if(e) HDelete(dstvol,macosdir,"\pFilterFoundry");
  229.                                                 }
  230.                                                 if(e) HDelete(dstvol,rsrcdir,plugname);
  231.                                         }
  232.                                         if(e) HDelete(dstvol,contentsdir,"\pResources");
  233.                                 }
  234.                                 if(e) HDelete(dstvol,contentsdir,"\pMacOS");
  235.                         }
  236.                         if(e) HDelete(dstvol,bundledir,"\pContents");
  237.                 }
  238.                 if(e) FSpDelete(&sfr->sfFile);
  239.         }
  240.                
  241.         return e;
  242. }
  243.  
  244. OSErr make_singlefile(StandardFileReply *sfr, short plugvol,long plugdir,StringPtr plugname){
  245.         OSErr e;
  246.         FSSpec origfss;
  247.  
  248.         e = FSpDelete(&sfr->sfFile);
  249.         if(e && e != fnfErr){
  250.                 alertuser("Can't replace the existing file. Try a different name or location.","");
  251.                 return userCanceledErr;
  252.         }
  253.  
  254.         if( !(e = FileCopy(plugvol,plugdir,plugname, sfr->sfFile.vRefNum,sfr->sfFile.parID,NULL, sfr->sfFile.name,NULL,0,false))
  255.         && !(e = FSMakeFSSpec(plugvol,plugdir,plugname,&origfss)) )
  256.                 /* now we add PARM resources, and edit PiPL */
  257.                 e = doresources(&origfss, &sfr->sfFile);
  258.  
  259.         return e;
  260. }
  261.  
  262. OSErr make_standalone(StandardFileReply *sfr){
  263.         OSErr e;
  264.         short plugvol;
  265.         long plugdir;
  266.         Str255 plugname;
  267.        
  268.         if(!(e = GetFileLocation(CurResFile(),&plugvol,&plugdir,plugname)))
  269. #ifdef MACMACHO
  270.     e = make_bundle(sfr,plugvol,plugdir,plugname);
  271. #else
  272.                 e = make_singlefile(sfr,plugvol,plugdir,plugname);
  273. #endif
  274.  
  275.         if(e && e != userCanceledErr)
  276.                 alertuser("Could not create standalone plugin.","");
  277.        
  278.         return e;
  279. }
  280.