1 /* $Id: macmisc.c,v 1.3 2003/03/29 23:07:55 ben Exp $ */
3 * Copyright (c) 1999, 2003 Ben Harris
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
23 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 #include <MacWindows.h>
32 #include <Processes.h>
33 #include <Quickdraw.h>
34 #include <TextUtils.h>
42 #if TARGET_API_MAC_CARBON
44 * This is used by (I think) CarbonStdCLib, but only exists in
45 * CarbonLib 1.1 and later. Muppets. Happily, it's documented to be
49 const CFAllocatorRef kCFAllocatorDefault = NULL;
55 * Like FrontWindow(), but return NULL if we aren't the front process
56 * (i.e. the front window isn't one of ours).
58 WindowPtr mac_frontwindow(void)
60 ProcessSerialNumber frontpsn;
61 ProcessSerialNumber curpsn = { 0, kCurrentProcess };
64 GetFrontProcess(&frontpsn);
65 if (SameProcess(&frontpsn, &curpsn, &result) == noErr && result)
70 void fatalbox(char *fmt, ...) {
75 /* We'd like stuff to be a Pascal string */
76 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
78 ParamText(stuff, NULL, NULL, NULL);
83 void modalfatalbox(char *fmt, ...) {
88 /* We'd like stuff to be a Pascal string */
89 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
91 ParamText(stuff, NULL, NULL, NULL);
96 Filename filename_from_str(const char *str)
101 /* XXX This fails for filenames over 255 characters long. */
103 FSMakeFSSpec(0, 0, tmp, &ret.fss);
108 * Convert a filename to a string for display purposes.
109 * See pp 2-44--2-46 of IM:Files
111 * XXX static storage considered harmful
113 const char *filename_to_str(const Filename *fn)
118 static char *path = NULL;
121 if (path != NULL) sfree(path);
122 path = snewn(fn->fss.name[0], char);
123 p2cstrcpy(path, fn->fss.name);
124 pb.dirInfo.ioNamePtr = dirname;
125 pb.dirInfo.ioVRefNum = fn->fss.vRefNum;
126 pb.dirInfo.ioDrParID = fn->fss.parID;
127 pb.dirInfo.ioFDirIndex = -1;
129 pb.dirInfo.ioDrDirID = pb.dirInfo.ioDrParID;
130 err = PBGetCatInfoSync(&pb);
132 /* XXX Assume not A/UX */
133 newpath = snewn(strlen(path) + dirname[0] + 2, char);
134 p2cstrcpy(newpath, dirname);
135 strcat(newpath, ":");
136 strcat(newpath, path);
139 } while (pb.dirInfo.ioDrDirID != fsRtDirID);
143 int filename_equal(Filename f1, Filename f2)
146 return f1.fss.vRefNum == f2.fss.vRefNum &&
147 f1.fss.parID == f2.fss.parID &&
148 f1.fss.name[0] == f2.fss.name[0] &&
149 memcmp(f1.fss.name + 1, f2.fss.name + 1, f1.fss.name[0]) == 0;
152 int filename_is_null(Filename fn)
155 return fn.fss.vRefNum == 0 && fn.fss.parID == 0 && fn.fss.name[0] == 0;
158 FILE *f_open(Filename fn, char const *mode)
165 HGetVol(NULL, &savevol, &savedir);
166 if (HSetVol(NULL, fn.fss.vRefNum, fn.fss.parID) == noErr) {
167 p2cstrcpy(tmp, fn.fss.name);
168 ret = fopen(tmp, mode);
171 HSetVol(NULL, savevol, savedir);
177 * c-file-style: "simon"