Subversion Repositories filter_foundry

Rev

Blame | 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 "ff.h"
  21.  
  22. #include "file_compat.h"
  23. #include "sprintf_tiny.h"
  24.  
  25. enum{ CHOPLINES = 63 };
  26.  
  27. OSErr putstr(Handle h,char *s);
  28.  
  29. OSErr putstr(Handle h,char *s){
  30.         Ptr p;
  31.         OSErr e;
  32.         long size = PIGETHANDLESIZE(h),n = strlen(s);
  33.        
  34.         if(!(e = PISETHANDLESIZE(h,size+n))){
  35.                 p = PILOCKHANDLE(h,false);
  36.                 memcpy(p+size,s,n);
  37.                 PIUNLOCKHANDLE(h);
  38.         }
  39.         return e;
  40. }
  41.  
  42. OSErr saveparams(Handle h){
  43.         char outbuf[CHOPLINES*2+2],*q,*p,*r,*start;
  44.         int i,j,chunk,n;
  45.         OSErr e;
  46.         long est;
  47.         static char afs_sig[] = "%RGB-1.0\n";
  48.        
  49.         est = strlen(expr[0])+strlen(expr[1])+strlen(expr[2])+strlen(expr[3]);
  50.         est += strlen(afs_sig) + est/CHOPLINES + 4 + 8*6 + 1024 /*slop*/ ;
  51.        
  52.         if( !(e = PISETHANDLESIZE(h,est)) && (p = start = PILOCKHANDLE(h,false)) ){
  53.                 p = cat(p,afs_sig);
  54.                
  55.                 /* slider values */
  56.                 for( i=0 ; i<8 ; ++i ){
  57.                         p = int_str(p,slider[i],10);
  58.                         *p++ = '\n';
  59.                 }
  60.                
  61.                 /* expressions */
  62.                 if(!e)
  63.                         for( i=0 ; i<4 ; ++i ){
  64.                                 if(r = expr[i])
  65.                                         for( n = strlen(r) ; n ; n -= chunk ){
  66.                                                 chunk = n>CHOPLINES ? CHOPLINES : n;
  67.                                                 for( j = chunk,q = outbuf ; j-- ; )
  68.                                                         if(*r == CR){
  69.                                                                 *q++ = '\\';
  70.                                                                 *q++ = 'r';
  71.                                                                 ++r;
  72.                                                         }else
  73.                                                                 *q++ = *r++;
  74.                                                 *q++ = '\n';
  75.                                                 *q = 0;
  76.                                                 p = cat(p,outbuf);
  77.                                         }
  78.                                 else
  79.                                         p = cat(p,"(null expr)\n"); // this shouldn't happen
  80.                                 *p++ = '\n';
  81.                         }
  82.                 PIUNLOCKHANDLE(h);
  83.                 if(!e)
  84.                         e = PISETHANDLESIZE(h,p - start);
  85.         }
  86. err:
  87.         return e;
  88. }
  89.  
  90. OSErr savehandleintofile(Handle h,FILEREF r){
  91.         Ptr p = PILOCKHANDLE(h,false);
  92.         long n = PIGETHANDLESIZE(h);
  93.         OSErr e = FSWrite(r,&n,p);
  94.         PIUNLOCKHANDLE(h);
  95.         return e;
  96. }
  97.  
  98. Boolean savefile(StandardFileReply *sfr){
  99.         FILEREF r;
  100.         Handle h;
  101.         Boolean res = false;
  102.         char *reasonstr = "";
  103.  
  104.         FSpDelete(&sfr->sfFile);
  105.         if(!FSpCreate(&sfr->sfFile,kPhotoshopSignature,'TEXT',sfr->sfScript))
  106.                 if(!FSpOpenDF(&sfr->sfFile,fsWrPerm,&r)){
  107.  
  108.                         if(h = PINEWHANDLE(0)){
  109.                                 res = !(saveparams(h) || savehandleintofile(h,r)) ;
  110.                                 PIDISPOSEHANDLE(h);
  111.                         }
  112.  
  113.                         FSClose(r);
  114.                 }else reasonstr = ("Could not open the file.");
  115.         else reasonstr = ("Could not create the file.");
  116.  
  117.         if(!res)
  118.                 alertuser("Could not save settings.",reasonstr);
  119.  
  120.         return res;
  121. }
  122.