22** macglue - A couple of mac-specific routines often needed.
33**
44** Jack Jansen, CWI, 1994.
5+ ** Some routines by Guido, moved here from macosmodule.c
6+ ** (since they are useable by other modules as well).
57*/
6-
78#ifdef HAVE_CONFIG_H
89#include "config.h"
910#endif
1819#include <Windows.h>
1920#include <Desk.h>
2021
22+ /* We should include Errors.h here, but it has a name conflict
23+ ** with the python errors.h. */
24+ #define fnfErr -43
25+
26+ /* Convert C to Pascal string. Returns pointer to static buffer. */
27+ unsigned char *
28+ Pstring (char * str )
29+ {
30+ static Str255 buf ;
31+ int len ;
32+
33+ len = strlen (str );
34+ buf [0 ] = (unsigned char )len ;
35+ strncpy ((char * )buf + 1 , str , len );
36+ return buf ;
37+ }
38+
2139/* Replace strerror with something that might work */
2240char * macstrerror (int err )
2341{
@@ -44,7 +62,6 @@ PyErr_Mac(PyObject *eobj, int err)
4462{
4563 char * msg ;
4664 PyObject * v ;
47- Handle h ;
4865
4966 if (err == 0 ) {
5067 Py_INCREF (Py_None );
@@ -70,17 +87,88 @@ PyMac_Idle()
7087 EventRecord ev ;
7188 WindowPtr wp ;
7289
73- #if 0
7490 SystemTask ();
75- if ( intrcheck () )
91+ if ( intrpeek () )
7692 return 0 ;
7793 if ( GetNextEvent (0xffff , & ev ) ) {
7894 if ( ev .what == mouseDown ) {
7995 if ( FindWindow (ev .where , & wp ) == inSysWindow )
8096 SystemClick (& ev , wp );
8197 }
8298 }
83- #endif
8499 return 1 ;
85100}
86101
102+
103+ /* Convert a ResType argument */
104+ int
105+ GetOSType (PyObject * v , ResType * pr )
106+ {
107+ if (!PyString_Check (v ) || PyString_Size (v ) != 4 ) {
108+ PyErr_SetString (PyExc_TypeError ,
109+ "OSType arg must be string of 4 chars" );
110+ return 0 ;
111+ }
112+ memcpy ((char * )pr , PyString_AsString (v ), 4 );
113+ return 1 ;
114+ }
115+
116+ /* Convert a Str255 argument */
117+ int
118+ GetStr255 (PyObject * v , Str255 pbuf )
119+ {
120+ int len ;
121+ if (!PyString_Check (v ) || (len = PyString_Size (v )) > 255 ) {
122+ PyErr_SetString (PyExc_TypeError ,
123+ "Str255 arg must be string of at most 255 chars" );
124+ return 0 ;
125+ }
126+ pbuf [0 ] = len ;
127+ memcpy ((char * )(pbuf + 1 ), PyString_AsString (v ), len );
128+ return 1 ;
129+ }
130+
131+ /*
132+ ** Convert anything resembling an FSSpec argument
133+ ** NOTE: This routine will fail on pre-sys7 machines.
134+ ** The caller is responsible for not calling this routine
135+ ** in those cases (which is fine, since everyone calling
136+ ** this is probably sys7 dependent anyway).
137+ */
138+ int
139+ GetFSSpec (PyObject * v , FSSpec * fs )
140+ {
141+ Str255 path ;
142+ short refnum ;
143+ long parid ;
144+ OSErr err ;
145+
146+ if ( PyString_Check (v ) ) {
147+ /* It's a pathname */
148+ if ( !PyArg_Parse (v , "O&" , GetStr255 , & path ) )
149+ return 0 ;
150+ refnum = 0 ; /* XXXX Should get CurWD here... */
151+ parid = 0 ;
152+ } else {
153+ PyErr_Clear ();
154+ if ( !PyArg_Parse (v , "(hlO&); FSSpec should be fullpath or (int,int,string)" ,
155+ & refnum , & parid , GetStr255 , & path ))
156+ return 0 ;
157+ }
158+ err = FSMakeFSSpec (refnum , parid , path , fs );
159+ if ( err && err != fnfErr ) {
160+ PyErr_SetString (PyExc_TypeError ,
161+ "FSMakeFSSpec error" );
162+ return 0 ;
163+ }
164+ return 1 ;
165+ }
166+
167+ /*
168+ ** Return a python object that describes an FSSpec
169+ */
170+ PyObject *
171+ PyMac_BuildFSSpec (FSSpec * fs )
172+ {
173+ return Py_BuildValue ("(iis#)" , fs -> vRefNum , fs -> parID , & fs -> name [1 ], fs -> name [0 ]);
174+ }
0 commit comments