Subversion Repositories filter_foundry

Rev

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