Rev 198 | Rev 226 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | toby | 1 | /* |
18 | toby | 2 | This file is part of "Filter Foundry", a filter plugin for Adobe Photoshop |
192 | daniel-mar | 3 | Copyright (C) 2003-2009 Toby Thain, toby@telegraphics.com.au |
206 | daniel-mar | 4 | Copyright (C) 2018-2021 Daniel Marschall, ViaThinkSoft |
2 | toby | 5 | |
6 | This program is free software; you can redistribute it and/or modify |
||
103 | toby | 7 | it under the terms of the GNU General Public License as published by |
2 | toby | 8 | the Free Software Foundation; either version 2 of the License, or |
9 | (at your option) any later version. |
||
10 | |||
11 | This program is distributed in the hope that it will be useful, |
||
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
14 | GNU General Public License for more details. |
||
15 | |||
103 | toby | 16 | You should have received a copy of the GNU General Public License |
2 | toby | 17 | along with this program; if not, write to the Free Software |
18 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
19 | */ |
||
20 | |||
21 | #include "ff.h" |
||
22 | |||
23 | #include "file_compat.h" |
||
24 | #include "sprintf_tiny.h" |
||
25 | |||
26 | enum{ CHOPLINES = 63 }; |
||
27 | |||
28 | OSErr putstr(Handle h,char *s); |
||
29 | |||
30 | OSErr putstr(Handle h,char *s){ |
||
31 | Ptr p; |
||
32 | OSErr e; |
||
185 | dmarschall | 33 | size_t size = PIGETHANDLESIZE(h),n = strlen(s); |
103 | toby | 34 | |
185 | dmarschall | 35 | if(!(e = PISETHANDLESIZE(h,(int32)(size+n)))){ |
2 | toby | 36 | p = PILOCKHANDLE(h,false); |
37 | memcpy(p+size,s,n); |
||
38 | PIUNLOCKHANDLE(h); |
||
39 | } |
||
40 | return e; |
||
41 | } |
||
42 | |||
43 | OSErr saveparams(Handle h){ |
||
44 | char outbuf[CHOPLINES*2+2],*q,*p,*r,*start; |
||
185 | dmarschall | 45 | size_t n, chunk, j; |
46 | int i; |
||
2 | toby | 47 | OSErr e; |
185 | dmarschall | 48 | size_t est; |
124 | dmarschall | 49 | static char afs_sig[] = "%RGB-1.0\r"; |
103 | toby | 50 | |
15 | toby | 51 | if(!h) DBG("saveparams: Null handle!"); |
103 | toby | 52 | |
52 | toby | 53 | est = strlen(expr[0]) + strlen(expr[1]) + strlen(expr[2]) + strlen(expr[3]); |
54 | // do not be tempted to combine into one expression: 'est' is referenced below |
||
55 | est += strlen(afs_sig) + est/CHOPLINES + 4 + 8*6 + 64 /*slop*/ ; |
||
103 | toby | 56 | |
15 | toby | 57 | PIUNLOCKHANDLE(h); // should not be necessary |
185 | dmarschall | 58 | if( !(e = PISETHANDLESIZE(h,(int32)(est))) && (p = start = PILOCKHANDLE(h,false)) ){ |
52 | toby | 59 | // build one long string in AFS format |
60 | p = cat(p,afs_sig); // first the header signature |
||
103 | toby | 61 | |
52 | toby | 62 | /* then slider values, one per line */ |
102 | toby | 63 | for( i=0 ; i<8 ; ++i ) |
124 | dmarschall | 64 | p += sprintf(p, "%ld\r", slider[i]); |
103 | toby | 65 | |
52 | toby | 66 | /* expressions, broken into lines no longer than CHOPLINES characters */ |
15 | toby | 67 | for( i=0 ; i<4 ; ++i ){ |
23 | toby | 68 | if( (r = expr[i]) ) |
15 | toby | 69 | for( n = strlen(r) ; n ; n -= chunk ){ |
198 | daniel-mar | 70 | chunk = n> (int)CHOPLINES ? (int)CHOPLINES : n; |
15 | toby | 71 | for( j = chunk,q = outbuf ; j-- ; ) |
72 | if(*r == CR){ |
||
73 | *q++ = '\\'; |
||
74 | *q++ = 'r'; |
||
75 | ++r; |
||
103 | toby | 76 | }else |
15 | toby | 77 | *q++ = *r++; |
124 | dmarschall | 78 | *q++ = '\r'; |
15 | toby | 79 | *q = 0; |
80 | p = cat(p,outbuf); |
||
81 | } |
||
82 | else |
||
198 | daniel-mar | 83 | p = cat(p,_strdup("(null expr)\r")); // this shouldn't happen |
124 | dmarschall | 84 | *p++ = '\r'; |
15 | toby | 85 | } |
86 | |||
87 | // *p = 0; dbg(start); |
||
88 | |||
2 | toby | 89 | PIUNLOCKHANDLE(h); |
185 | dmarschall | 90 | e = PISETHANDLESIZE(h,(int32)(p - start)); // could ignore this error, maybe |
2 | toby | 91 | } |
23 | toby | 92 | |
2 | toby | 93 | return e; |
94 | } |
||
95 | |||
96 | OSErr savehandleintofile(Handle h,FILEREF r){ |
||
97 | Ptr p = PILOCKHANDLE(h,false); |
||
98 | long n = PIGETHANDLESIZE(h); |
||
99 | OSErr e = FSWrite(r,&n,p); |
||
100 | PIUNLOCKHANDLE(h); |
||
101 | return e; |
||
102 | } |
||
103 | |||
104 | Boolean savefile(StandardFileReply *sfr){ |
||
105 | FILEREF r; |
||
106 | Handle h; |
||
107 | Boolean res = false; |
||
198 | daniel-mar | 108 | char *reasonstr = _strdup(""); |
2 | toby | 109 | |
110 | FSpDelete(&sfr->sfFile); |
||
188 | dmarschall | 111 | if(FSpCreate(&sfr->sfFile,SIG_SIMPLETEXT,TEXT_FILETYPE,sfr->sfScript) == noErr) |
112 | if(FSpOpenDF(&sfr->sfFile,fsWrPerm,&r) == noErr){ |
||
2 | toby | 113 | |
167 | dmarschall | 114 | if( (h = PINEWHANDLE(1)) ){ // don't set initial size to 0, since some hosts (e.g. GIMP/PSPI) are incompatible with that. res = !(saveparams(h) || savehandleintofile(h,r)) ; |
2 | toby | 115 | res = !(saveparams(h) || savehandleintofile(h,r)) ; |
116 | PIDISPOSEHANDLE(h); |
||
117 | } |
||
118 | |||
119 | FSClose(r); |
||
198 | daniel-mar | 120 | }else reasonstr = (_strdup("Could not open the file.")); |
121 | else reasonstr = (_strdup("Could not create the file.")); |
||
2 | toby | 122 | |
123 | if(!res) |
||
198 | daniel-mar | 124 | alertuser(_strdup("Could not save settings."),reasonstr); |
2 | toby | 125 | |
126 | return res; |
||
127 | } |