_make_boundary(): Fix for SF bug #745478, broken boundary calculation
[python/dscho.git] / Mac / OSX / PythonLauncher / FileSettings.m
blob7b28daae017782d0fd80bc8a306fc1ec66ceab9f
1 //
2 //  FileSettings.m
3 //  PythonLauncher
4 //
5 //  Created by Jack Jansen on Sun Jul 21 2002.
6 //  Copyright (c) 2002 __MyCompanyName__. All rights reserved.
7 //
9 #import "FileSettings.h"
11 @implementation FileSettings
13 + (id)getFactorySettingsForFileType: (NSString *)filetype
15     static FileSettings *fsdefault_py, *fsdefault_pyw, *fsdefault_pyc;
16     FileSettings **curdefault;
17     
18     if ([filetype isEqualToString: @"Python Script"]) {
19         curdefault = &fsdefault_py;
20     } else if ([filetype isEqualToString: @"Python GUI Script"]) {
21         curdefault = &fsdefault_pyw;
22     } else if ([filetype isEqualToString: @"Python Bytecode Document"]) {
23         curdefault = &fsdefault_pyc;
24     } else {
25         NSLog(@"Funny File Type: %@\n", filetype);
26         curdefault = &fsdefault_py;
27         filetype = @"Python Script";
28     }
29     if (!*curdefault) {
30         *curdefault = [[FileSettings new] initForFSDefaultFileType: filetype];
31     }
32     return *curdefault;
35 + (id)getDefaultsForFileType: (NSString *)filetype
37     static FileSettings *default_py, *default_pyw, *default_pyc;
38     FileSettings **curdefault;
39     
40     if ([filetype isEqualToString: @"Python Script"]) {
41         curdefault = &default_py;
42     } else if ([filetype isEqualToString: @"Python GUI Script"]) {
43         curdefault = &default_pyw;
44     } else if ([filetype isEqualToString: @"Python Bytecode Document"]) {
45         curdefault = &default_pyc;
46     } else {
47         NSLog(@"Funny File Type: %@\n", filetype);
48         curdefault = &default_py;
49         filetype = @"Python Script";
50     }
51     if (!*curdefault) {
52         *curdefault = [[FileSettings new] initForDefaultFileType: filetype];
53     }
54     return *curdefault;
57 + (id)newSettingsForFileType: (NSString *)filetype
59     FileSettings *cur;
60     
61     cur = [FileSettings new];
62     [cur initForFileType: filetype];
63     return [cur retain];
66 - (id)initWithFileSettings: (FileSettings *)source
68     self = [super init];
69     if (!self) return self;
70     
71     interpreter = [source->interpreter retain];
72     honourhashbang = source->honourhashbang;
73     debug = source->debug;
74     verbose = source->verbose;
75     inspect = source->inspect;
76     optimize = source->optimize;
77     nosite = source->nosite;
78     tabs = source->tabs;
79     others = [source->others retain];
80     with_terminal = source->with_terminal;
81     prefskey = source->prefskey;
82     if (prefskey) [prefskey retain];
83     
84     return self;
87 - (id)initForFileType: (NSString *)filetype
89     FileSettings *defaults;
90     
91     defaults = [FileSettings getDefaultsForFileType: filetype];
92     self = [self initWithFileSettings: defaults];
93     origsource = [defaults retain];
94     return self;
97 //- (id)init
98 //{
99 //    self = [self initForFileType: @"Python Script"];
100 //    return self;
103 - (id)initForFSDefaultFileType: (NSString *)filetype
105     int i;
106     NSString *filename;
107     NSDictionary *dict;
108     static NSDictionary *factorySettings;
109     
110     self = [super init];
111     if (!self) return self;
112     
113     if (factorySettings == NULL) {
114         NSBundle *bdl = [NSBundle mainBundle];
115         NSString *path = [ bdl pathForResource: @"factorySettings"
116                 ofType: @"plist"];
117         factorySettings = [[NSDictionary dictionaryWithContentsOfFile:
118             path] retain];
119         if (factorySettings == NULL) {
120             NSLog(@"Missing %@", path);
121             return NULL;
122         }
123     }
124     dict = [factorySettings objectForKey: filetype];
125     if (dict == NULL) {
126         NSLog(@"factorySettings.plist misses file type \"%@\"", filetype);
127         interpreter = [@"no default found" retain];
128         return NULL;
129     }
130     [self applyValuesFromDict: dict];
131     interpreters = [dict objectForKey: @"interpreter_list"];
132     interpreter = NULL;
133     for (i=0; i < [interpreters count]; i++) {
134         filename = [interpreters objectAtIndex: i];
135         filename = [filename stringByExpandingTildeInPath];
136         if ([[NSFileManager defaultManager] fileExistsAtPath: filename]) {
137             interpreter = [filename retain];
138             break;
139         }
140     }
141     if (interpreter == NULL)
142         interpreter = [@"no default found" retain];
143     origsource = NULL;
144     return self;
147 - (void)applyUserDefaults: (NSString *)filetype
149     NSUserDefaults *defaults;
150     NSDictionary *dict;
151     
152     defaults = [NSUserDefaults standardUserDefaults];
153     dict = [defaults dictionaryForKey: filetype];
154     if (!dict)
155         return;
156     [self applyValuesFromDict: dict];
158     
159 - (id)initForDefaultFileType: (NSString *)filetype
161     FileSettings *fsdefaults;
162     
163     fsdefaults = [FileSettings getFactorySettingsForFileType: filetype];
164     self = [self initWithFileSettings: fsdefaults];
165     if (!self) return self;
166     interpreters = [fsdefaults->interpreters retain];
167     [self applyUserDefaults: filetype];
168     prefskey = [filetype retain];
169     return self;
172 - (void)reset
174     if (origsource) {
175         [self updateFromSource: origsource];
176     } else {
177         FileSettings *fsdefaults;
178         fsdefaults = [FileSettings getFactorySettingsForFileType: prefskey];
179         [self updateFromSource: fsdefaults];
180     }
183 - (void)updateFromSource: (id <FileSettingsSource>)source
185     interpreter = [[source interpreter] retain];
186     honourhashbang = [source honourhashbang];
187     debug = [source debug];
188     verbose = [source verbose];
189     inspect = [source inspect];
190     optimize = [source optimize];
191     nosite = [source nosite];
192     tabs = [source tabs];
193     others = [[source others] retain];
194     with_terminal = [source with_terminal];
195     // And if this is a user defaults object we also save the
196     // values
197     if (!origsource) {
198         NSUserDefaults *defaults;
199         NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
200             interpreter, @"interpreter",
201             [NSNumber numberWithBool: honourhashbang], @"honourhashbang",
202             [NSNumber numberWithBool: debug], @"debug",
203             [NSNumber numberWithBool: verbose], @"verbose",
204             [NSNumber numberWithBool: inspect], @"inspect",
205             [NSNumber numberWithBool: optimize], @"optimize",
206             [NSNumber numberWithBool: nosite], @"nosite",
207             [NSNumber numberWithBool: nosite], @"nosite",
208             others, @"others",
209             [NSNumber numberWithBool: with_terminal], @"with_terminal",
210             nil];
211         defaults = [NSUserDefaults standardUserDefaults];
212         [defaults setObject: dict forKey: prefskey];
213     }
216 - (void)applyValuesFromDict: (NSDictionary *)dict
218     id value;
219     
220     value = [dict objectForKey: @"interpreter"];
221     if (value) interpreter = [value retain];
222     value = [dict objectForKey: @"honourhashbang"];
223     if (value) honourhashbang = [value boolValue];
224     value = [dict objectForKey: @"debug"];
225     if (value) debug = [value boolValue];
226     value = [dict objectForKey: @"verbose"];
227     if (value) verbose = [value boolValue];
228     value = [dict objectForKey: @"inspect"];
229     if (value) inspect = [value boolValue];
230     value = [dict objectForKey: @"optimize"];
231     if (value) optimize = [value boolValue];
232     value = [dict objectForKey: @"nosite"];
233     if (value) nosite = [value boolValue];
234     value = [dict objectForKey: @"nosite"];
235     if (value) tabs = [value boolValue];
236     value = [dict objectForKey: @"others"];
237     if (value) others = [value retain];
238     value = [dict objectForKey: @"with_terminal"];
239     if (value) with_terminal = [value boolValue];
242 - (NSString *)commandLineForScript: (NSString *)script
244     NSString *cur_interp = NULL;
245     char hashbangbuf[1024];
246     FILE *fp;
247     char *p;
248     
249     if (honourhashbang &&
250        (fp=fopen([script cString], "r")) &&
251        fgets(hashbangbuf, sizeof(hashbangbuf), fp) &&
252        strncmp(hashbangbuf, "#!", 2) == 0 &&
253        (p=strchr(hashbangbuf, '\n'))) {
254             *p = '\0';
255             p = hashbangbuf + 2;
256             while (*p == ' ') p++;
257             cur_interp = [NSString stringWithCString: p];
258     }
259     if (!cur_interp)
260         cur_interp = interpreter;
261         
262     return [NSString stringWithFormat:
263         @"\"%@\"%s%s%s%s%s%s %@ \"%@\" %s",
264         cur_interp,
265         debug?" -d":"",
266         verbose?" -v":"",
267         inspect?" -i":"",
268         optimize?" -O":"",
269         nosite?" -S":"",
270         tabs?" -t":"",
271         others,
272         script,
273         with_terminal? "&& echo Exit status: $? && exit 1" : " &"];
276 - (NSArray *) interpreters { return interpreters;};
278 // FileSettingsSource protocol 
279 - (NSString *) interpreter { return interpreter;};
280 - (BOOL) honourhashbang { return honourhashbang; };
281 - (BOOL) debug { return debug;};
282 - (BOOL) verbose { return verbose;};
283 - (BOOL) inspect { return inspect;};
284 - (BOOL) optimize { return optimize;};
285 - (BOOL) nosite { return nosite;};
286 - (BOOL) tabs { return tabs;};
287 - (NSString *) others { return others;};
288 - (BOOL) with_terminal { return with_terminal;};
290 @end