Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 83c74df

Browse files
committed
- Get preference filename from a resource
- Create the file if it doesn't exist and don't be fussy about it
1 parent d081b13 commit 83c74df

1 file changed

Lines changed: 53 additions & 31 deletions

File tree

Mac/Python/macgetpath.c

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,36 @@
3434
:Lib:test\n\
3535
:Lib:mac"
3636

37+
static void
38+
getpreffilefss(FSSpec *fssp)
39+
{
40+
static int diditbefore=0;
41+
static FSSpec fss;
42+
short prefdirRefNum;
43+
long prefdirDirID;
44+
Handle namehandle;
45+
46+
if ( !diditbefore ) {
47+
if ( FindFolder(kOnSystemDisk, 'pref', kDontCreateFolder, &prefdirRefNum,
48+
&prefdirDirID) != noErr ) {
49+
/* Something wrong with preferences folder */
50+
(void)StopAlert(NOPREFDIR_ID, NULL);
51+
exit(1);
52+
}
53+
54+
if ( (namehandle=GetNamedResource('STR ', PREFFILENAME_NAME)) == NULL ) {
55+
(void)StopAlert(NOPREFNAME_ID, NULL);
56+
exit(1);
57+
}
58+
59+
HLock(namehandle);
60+
(void)FSMakeFSSpec(prefdirRefNum, prefdirDirID, (unsigned char *)*namehandle, &fss);
61+
HUnlock(namehandle);
62+
ReleaseResource(namehandle);
63+
diditbefore = 1;
64+
}
65+
*fssp = fss;
66+
}
3767

3868
char *
3969
Py_GetPath()
@@ -44,21 +74,21 @@ Py_GetPath()
4474
** - Add :
4575
*/
4676
static char *pythonpath;
47-
char *curwd;
4877
char *p, *endp;
4978
int newlen;
79+
char *curwd;
5080
staticforward char *PyMac_GetPythonDir();
5181
#ifndef USE_BUILTIN_PATH
5282
staticforward char *PyMac_GetPythonPath();
5383
#endif
5484

5585
if ( pythonpath ) return pythonpath;
56-
curwd = PyMac_GetPythonDir();
5786
#ifndef USE_BUILTIN_PATH
58-
if ( pythonpath = PyMac_GetPythonPath(curwd) )
87+
if ( pythonpath = PyMac_GetPythonPath() )
5988
return pythonpath;
6089
printf("Warning: No pythonpath resource found, using builtin default\n");
6190
#endif
91+
curwd = PyMac_GetPythonDir();
6292
p = PYTHONPATH;
6393
endp = p;
6494
pythonpath = malloc(2);
@@ -90,6 +120,7 @@ Py_GetPath()
90120
return pythonpath;
91121
}
92122

123+
93124
/*
94125
** Open/create the Python Preferences file, return the handle
95126
*/
@@ -99,25 +130,16 @@ PyMac_OpenPrefFile()
99130
AliasHandle handle;
100131
FSSpec dirspec;
101132
short prefrh;
102-
short prefdirRefNum;
103-
long prefdirDirID;
104-
short action;
105133
OSErr err;
106134

107-
if ( FindFolder(kOnSystemDisk, 'pref', kDontCreateFolder, &prefdirRefNum,
108-
&prefdirDirID) != noErr ) {
109-
/* Something wrong with preferences folder */
110-
(void)StopAlert(NOPREFDIR_ID, NULL);
111-
exit(1);
112-
}
113-
114-
(void)FSMakeFSSpec(prefdirRefNum, prefdirDirID, "\pPython Preferences", &dirspec);
135+
getpreffilefss(&dirspec);
115136
prefrh = FSpOpenResFile(&dirspec, fsRdWrShPerm);
116137
if ( prefrh < 0 ) {
138+
#if 0
117139
action = CautionAlert(NOPREFFILE_ID, NULL);
118140
if ( action == NOPREFFILE_NO )
119141
exit(1);
120-
142+
#endif
121143
FSpCreateResFile(&dirspec, 'Pyth', 'pref', 0);
122144
prefrh = FSpOpenResFile(&dirspec, fsRdWrShPerm);
123145
if ( prefrh == -1 ) {
@@ -149,12 +171,16 @@ PyMac_OpenPrefFile()
149171
static char *
150172
PyMac_GetPythonDir()
151173
{
152-
static char name[256];
174+
static int diditbefore = 0;
175+
static char name[256] = {':', '\0'};
153176
AliasHandle handle;
154177
FSSpec dirspec;
155178
Boolean modified = 0;
156179
short oldrh, prefrh = -1, homerh;
157180

181+
if ( diditbefore )
182+
return name;
183+
158184
oldrh = CurResFile();
159185

160186
/* First look for an override in the application file */
@@ -168,14 +194,16 @@ PyMac_GetPythonDir()
168194
handle = (AliasHandle)Get1Resource('alis', PYTHONHOME_ID);
169195
if ( handle == NULL ) {
170196
(void)StopAlert(BADPREFFILE_ID, NULL);
171-
exit(1);
197+
diditbefore=1;
198+
return ":";
172199
}
173200
homerh = prefrh;
174201
}
175202
/* It exists. Resolve it (possibly updating it) */
176203
if ( ResolveAlias(NULL, handle, &dirspec, &modified) != noErr ) {
177204
(void)StopAlert(BADPREFFILE_ID, NULL);
178-
exit(1);
205+
diditbefore=1;
206+
return ":";
179207
}
180208
if ( modified ) {
181209
ChangedResource((Handle)handle);
@@ -192,18 +220,16 @@ PyMac_GetPythonDir()
192220
name[0] = 0;
193221
(void)getwd(name);
194222
}
223+
diditbefore = 1;
195224
return name;
196225
}
197226

198227
#ifndef USE_BUILTIN_PATH
199228
static char *
200-
PyMac_GetPythonPath(dir)
201-
char *dir;
229+
PyMac_GetPythonPath()
202230
{
203231
FSSpec dirspec;
204232
short oldrh, prefrh = -1;
205-
short prefdirRefNum;
206-
long prefdirDirID;
207233
char *rv;
208234
int i, newlen;
209235
Str255 pathitem;
@@ -228,15 +254,9 @@ char *dir;
228254
SetResLoad(1);
229255
UseResFile(oldrh);
230256

231-
/*
232-
** Remember old resource file and try to open preferences file
233-
** in the preferences folder.
234-
*/
235-
if ( FindFolder(kOnSystemDisk, 'pref', kDontCreateFolder, &prefdirRefNum,
236-
&prefdirDirID) == noErr ) {
237-
(void)FSMakeFSSpec(prefdirRefNum, prefdirDirID, "\pPython Preferences", &dirspec);
238-
prefrh = FSpOpenResFile(&dirspec, fsRdWrShPerm);
239-
}
257+
/* Open the preferences file only if there is no override */
258+
if ( resource_id != PYTHONPATHOVERRIDE_ID )
259+
prefrh = PyMac_OpenPrefFile();
240260
/* At this point, we may or may not have the preferences file open, and it
241261
** may or may not contain a sys.path STR# resource. We don't care, if it doesn't
242262
** exist we use the one from the application (the default).
@@ -252,6 +272,8 @@ char *dir;
252272
break;
253273
if ( pathitem[0] >= 9 && strncmp((char *)pathitem+1, "$(PYTHON)", 9) == 0 ) {
254274
/* We have to put the directory in place */
275+
char *dir = PyMac_GetPythonDir();
276+
255277
newlen = strlen(rv) + strlen(dir) + (pathitem[0]-9) + 2;
256278
if( (rv=realloc(rv, newlen)) == NULL)
257279
goto out;

0 commit comments

Comments
 (0)