Path: utzoo!attcan!uunet!husc6!bloom-beacon!bu-cs!dartvax!eleazar.dartmouth.edu!earleh From: earleh@eleazar.dartmouth.edu (Earle R. Horton) Newsgroups: comp.sys.mac.programmer Subject: Re: Writing C code that works with both MPW and LightspeedC Message-ID: <8560@dartvax.Dartmouth.EDU> Date: 24 May 88 19:09:09 GMT References: <332@piring.cwi.nl> Sender: news@dartvax.Dartmouth.EDU Reply-To: earleh@eleazar.dartmouth.edu (Earle R. Horton) Organization: Dartmouth College, Hanover, NH Lines: 177 In article <332@piring.cwi.nl> guido@cwi.nl (Guido van Rossum) writes: > Writing C code that works with both MPW and LightspeedC ... >3) LSC wants some points passed by value to the toolbox; >MPW passes all points by address. ... >5) LSC needs Pascal strings as toolbox parameters, >MPW needs C strings. Referring to the MPW C 2.0.2 Manual, appendix H: The new MPW C header files contain declarations which allow you to access the ToolBox routines in the same fashion as LightSpeedC and Pascal, so that most functions can be called the same way they are declared in Inside Macintosh. The only substantial difference is that a Pascal String is declared in LightSpeed as an array of char, while in MPW it is a struct. In MPW, the ToolBox calls for the C interface are declared using the spelling in Inside Macintosh, while the calls for the Pascal Interface are declared using all upper-case. I have found that using the Pascal interface exclusively results in smaller code, code which is more easily portable between the two compilers, and code which is easier to debug. Debugging is easier because many of the Pascal Interface calls are actually macro expansions of Trap calls, and you get to see these Traps right in the middle of your code. I include the following header file in ALL of my MPW source which uses the Macintosh Interface, and AFTER any other Macintosh-specific header files. It merely remaps the upper-case spelling of the Pascal interface back to the Inside Macintosh spelling. If you use this file then you can ignore the differences implied by Giudo's points (3) and (5), except for some minor caveats: a) You MUST include the function declarations for the routines you use in MPW. LightSpeedC has most of these built into the compiler, while MPW C gets them from the #include file. b) MPW C doesn't allow you to pass constant longs to routines which want Points. LightSpeedC allows this. c) MPW C Str255 is a struct, while LightSpeedC Str255 is an array. A PASSPT() macro neatly sidesteps this issue when passing strings to the ToolBox, but does not resolve the problem of how to access the characters individually. d) If you use this header file as I do, then you don't use ANY of the C Interface glue routines to the ToolBox. (If you care about performance and code size, then you shouldn't want the C Interface, anyway.) I call this file "compat.h", put a copy of it in {CIncludes}, and include it after all Macintosh interface #include files in the MPW-specific part of my C program headers. If there are any omissions or mistakes, I would appreciate hearing about them (not bloody likely). Earle /* * This FILE is intended for use with the MPW C 2.0 Interface files * which generate InLine code for routines that call the * ToolBox with points by value and strings as Pascal strings. * It should be included AFTER any Macintosh #include files. See * Appendix H of the MPW C 2.0 manual for details. * * This file allows use of MPW C 2.0 standardized ToolBox calls using * the spelling from Inside Macinstosh, and not all upper case, as is used * in the MPW C 2.0 header files. * * If you like to pass C strings to the ToolBox, do NOT include this * FILE. * * Earle R. Horton. Friday, January 22, 1988 11:23:07 pm */ #define NewControl NEWCONTROL #define SetCTitle SETCTITLE #define GetCTitle GETCTITLE #define DragControl DRAGCONTROL #define TestControl TESTCONTROL #define TrackControl TRACKCONTROL #define FindControl FINDCONTROL #define OpenDeskAcc OPENDESKACC #define FindDItem FINDDITEM #define NewDialog NEWDIALOG #define ParamText PARAMTEXT #define GetIText GETITEXT #define SetIText SETITEXT #define NewDialog NEWDIALOG #define GetVol GETVOL #define SetVol SETVOL #define UnmountVol UNMOUNTVOL #define Eject EJECT #define FlushVol FLUSHVOL #define Create CREATE #define FSDelete FSDELETE #define FSOpen FSOPEN #define OpenRF OPENRF #define Rename RENAME #define GetFInfo GETFINFO #define SetFInfo SETFINFO #define SetFLock SETFLOCK #define RstFLock RSTFLOCK #define GetFontName GETFONTNAME #define GetFNum GETFNUM #define LCellSize LCELLSIZE #define LNew LNEW #define NewMenu NEWMENU #define AppendMenu APPENDMENU #define SetItem SETITEM #define GetItem GETITEM #define InsMenuItem INSMENUITEM #define MenuSelect MENUSELECT #define EqualString EQUALSTRING #define RelString RELSTRING #define UprString UPRSTRING #define DIZero DIZERO #define NumToString NUMTOSTRING #define StringToNum STRINGTONUM #define SFPutFile SFPUTFILE #define SFPPutFile SFPPUTFILE #define SFGetFile SFGETFILE #define SFPGetFile SFPGETFILE #define IUDateString IUDATESTRING #define IUDatePString IUDATEPSTRING #define IUTimeString IUTIMESTRING #define IUTimePString IUTIMEPSTRING #define IUCompString IUCOMPSTRING #define IUEqualString IUEQUALSTRING #define DIBadMount DIBADMOUNT #define DrawString DRAWSTRING #define StringWidth STRINGWIDTH #define StuffHex STUFFHEX #define AddPt ADDPT #define SubPt SUBPT #define EqualPt EQUALPT #define PtInRect PTINRECT #define Pt2Rect PT2RECT #define PtToAngle PTTOANGLE #define PtInRgn PTINRGN #define StdText STDTEXT #define CreateResFile CREATERESFILE #define OpenResFile OPENRESFILE #define OpenRFPerm OPENRFPERM #define GetNamedResource GETNAMEDRESOURCE #define Get1NamedResource GET1NAMEDRESOURCE #define GetResInfo GETRESINFO #define SetResInfo SETRESINFO #define AddResource ADDRESOURCE #define GetAppParms GETAPPPARMS #define OpenDriver OPENDRIVER #define TEGetOffset TEGETOFFSET #define TEGetPoint TEGETPOINT #define TEClick TECLICK #define NewString NEWSTRING #define SetString SETSTRING #define GetIndString GETINDSTRING #define DeltaPoint DELTAPOINT #define ShieldCursor SHIELDCURSOR #define NewWindow NEWWINDOW #define SetWTitle SETWTITLE #define GetWTitle GETWTITLE #define NewCWindow NEWCWINDOW #define GrowWindow GROWWINDOW #define DragWindow DRAGWINDOW #define TrackGoAway TRACKGOAWAY #define FindWindow FINDWINDOW #define PinRect PINRECT #define DragGrayRgn DRAGGRAYRGN #define TrackBox TRACKBOX /* This is for programs which are designed to be compiled in either * LSC or MPW C and with the same usage for Str255. */ #define PASS_STR(a) &(a) ********************************************************************* *Earle R. Horton, H.B. 8000, Dartmouth College, Hanover, NH 03755 * *********************************************************************