Subversion Repositories filter_foundry

Rev

Rev 15 | Rev 23 | 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 "ff.h"
  21.  
  22. #include "file_compat.h"
  23.  
  24. enum{
  25.         BUFSIZE = (4L<<10),
  26.         MAXLINE = 0x200,
  27. };
  28.  
  29. Boolean readparams(Handle h,Boolean alerts,char **reason){
  30.         Boolean res = false;
  31.         char linebuf[MAXLINE+1],curexpr[MAXEXPR+1],*p,*dataend,*q;
  32.         int c,linecnt,lineptr,exprcnt;
  33.  
  34.         if(!h){
  35.                 *reason = "readparams: Null parameter handle.";
  36.                 return false;
  37.         }
  38.  
  39.         p = PILOCKHANDLE(h,false);
  40.         dataend = p + PIGETHANDLESIZE(h);
  41. //sprintf(s,"readparams: Input data size = %d",dataend-p); dbg(s);
  42.        
  43.         q = curexpr;
  44.         linecnt = exprcnt = lineptr = 0;
  45.  
  46.         *reason = "File was too short.";
  47.         while( p < dataend ){
  48.  
  49.                 c = *p++;
  50.  
  51.                 if(c==CR || c==LF){ /* detected end of line */
  52.                        
  53.                         /* look ahead to see if we need to skip a line feed (DOS EOL convention) */
  54.                         if(c == CR && *p == LF && p < dataend)
  55.                                 ++p;
  56.                        
  57.                         linebuf[lineptr] = 0; /* add terminating NUL to line buffer */
  58.                        
  59. //sprintf(s,"got line %d = \"%s\"",linecnt,linebuf); dbg(s);
  60.  
  61.                         /* process complete line */
  62.                         if(linecnt==0){
  63.                                 if(strcmp(linebuf,"%RGB-1.0")){
  64.                                         if(alerts)
  65.                                                 *reason = ("This doesn't look like a Filter Factory file (first line is not \"%RGB-1.0\").");
  66.                                         break;
  67.                                 }
  68.                         }else if(linecnt<=8){
  69.                                 slider[linecnt-1] = atoi(linebuf);
  70.                         }else{
  71.                                 if(lineptr){
  72.                                         /* it's not an empty line; append it to current expr string */
  73.                                         if( q+lineptr > curexpr+MAXEXPR ){
  74.                                                 *reason = ("Found an expression longer than 1024 characters.");
  75.                                                 break;
  76.                                         }
  77.                                         q = cat(q,linebuf);
  78.                                 }else{
  79.                                         /* it's an empty line: we've completed the expr string */
  80.                                         if(expr[exprcnt])
  81.                                                 free(expr[exprcnt]);
  82.                                         *q = 0;
  83.                                         if(!(expr[exprcnt] = my_strdup(curexpr))){
  84.                                                 *reason = ("Could not get memory for expression.");
  85.                                                 break;
  86.                                         }
  87.  
  88.                                         if(++exprcnt == 4){
  89.                                                 res = true;
  90.                                                 break; /* got everything we want */
  91.                                         }
  92.  
  93.                                         q = curexpr; /* empty current expr, ready for next one */
  94.                                 }
  95.                         }
  96.                        
  97.                         ++linecnt;
  98.                         lineptr = 0;
  99.                 }else{
  100.                         /* store character */
  101.                         if(c=='\\'){ /* escape sequence */
  102.                                 if(p < dataend){
  103.                                         c = *p++;
  104.                                         switch(c){
  105.                                         case 'r': c = CR;
  106.                                         case '\\': break;
  107.                                         default:
  108.                                                 if(alerts) alertuser("Warning:","Unknown escape sequence in input.");
  109.                                         }
  110.                                 }//else if(alerts) alertuser("truncated escape sequence ends input");
  111.                         }
  112.  
  113.                         if(lineptr < MAXLINE)
  114.                                 linebuf[lineptr++] = c;
  115.                 }
  116.         }
  117.        
  118.         PIUNLOCKHANDLE(h);
  119. //                      if(c == EOF) alertuser("end of file");
  120.  
  121.         return res;
  122. }
  123.  
  124. Boolean readPARM(Ptr p,PARM_T *pparm,char **reasonstr){
  125.         Boolean res = false;
  126.         int i;
  127.  
  128.         memcpy(pparm,p,sizeof(PARM_T));
  129. #ifdef WIN_ENV
  130.         /* Windows PARM resource stores C strings - convert to Pascal strings  */
  131.         myc2pstr((char*)pparm->category);
  132.         myc2pstr((char*)pparm->title);
  133.         myc2pstr((char*)pparm->copyright);
  134.         myc2pstr((char*)pparm->author);
  135.         for(i=0;i<4;++i)
  136.                 myc2pstr((char*)pparm->map[i]);
  137.         for(i=0;i<8;++i)
  138.                 myc2pstr((char*)pparm->ctl[i]);
  139. #endif
  140.  
  141.         for(i=0;i<4;++i){
  142.                 if(expr[i]) free(expr[i]);
  143.                 expr[i] = my_strdup(pparm->formula[i]);
  144.                 //dbg(expr[i]);
  145.         }
  146.  
  147.         for(i=0;i<8;++i)
  148.                 slider[i] = pparm->val[i];
  149.  
  150.         res = true;
  151.         //dbg("read pparm ok");
  152.  
  153.         return res;
  154. }
  155.  
  156. Handle readfileintohandle(FILEREF r){
  157.         long n;
  158.         Handle h;
  159.         Ptr p;
  160.  
  161.         if( !GetEOF(r,&n) && (h = PINEWHANDLE(n)) ){
  162.                 p = PILOCKHANDLE(h,false);
  163.                 if(!FSRead(r,&n,p)){
  164.                         PIUNLOCKHANDLE(h);
  165.                         return h;
  166.                 }
  167.                 PIDISPOSEHANDLE(h);
  168.         }
  169.         return NULL;
  170. }
  171.  
  172. Boolean readfile(StandardFileReply *sfr,char **reason){
  173.         FILEREF r;
  174.         Handle h;
  175.         Boolean res = false;
  176.  
  177.         if(!FSpOpenDF(&sfr->sfFile,fsRdPerm,&r)){
  178.                 if(h = readfileintohandle(r)){
  179.                         if(res = readparams(h,true,reason))
  180.                                 gdata->standalone = false; // so metadata fields will default, if user chooses Make...
  181.                         PIDISPOSEHANDLE(h);
  182.                 }
  183.                 FSClose(r);
  184.         }else
  185.                 *reason = ("Could not open the file.");
  186.  
  187.         return res;
  188. }
  189.