Subversion Repositories filter_foundry

Rev

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

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