Subversion Repositories filter_foundry

Rev

Rev 188 | Rev 194 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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