home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Reference / FAQs on CD / csmp PD FAQ < prev    next >
Encoding:
Text File  |  1997-05-05  |  89.4 KB  |  2,187 lines  |  [TEXT/R*ch]

  1. Newsgroups: comp.sys.mac.programmer.misc,comp.sys.mac.programmer.info,comp.answers,news.answers
  2. Subject: The Mac Programming FAQ Answer sheet. [READ ME!]
  3. Followup-To: comp.sys.mac.programmer.misc
  4. Approved: news-answers-request@MIT.Edu
  5. Expires: 23 May 1995 00:00:00 GMT
  6.  
  7. Archive-name: macintosh/programming-faq
  8.  
  9. Mac Programming Frequently Asked Questions Answer Sheet
  10. Last update: 10 May 95 - sharp dressed FAQ
  11.  
  12. Please download a copy of this answer sheet and search it before you 
  13. post to the 'net, to help reduce bandwidth.
  14.  
  15. Please send all correspondence regarding content directly to the current 
  16. caretaker and content editor, Chris Thomas, <thunderone@delphi.com>.  
  17. All submissions sent will be considered to be in the public domain 
  18. unless stated otherwise (in which case they will not be included in this 
  19. FAQ sheet).  When writing, be sure that you include something 
  20. appropriate in the subject line.  I'm now automatically deleting mail 
  21. with blank or whitespace subjects unread due to the growing nasty 
  22. practice of sending junk email without a subject.  The idea, apparently, 
  23. is to be cute and get you to read the mail without deleting it.  This 
  24. sheet was started and is distributed by Jon Watte, whom you may reach as 
  25. <h+@austin.metrowerks.com>.
  26.  
  27. This sheet is currently archived on nada.kth.se where you can reach it 
  28. using afs as /afs/nada.kth.se/public/ftp/pub/hacks/mac-faq/CSMP_PD_FAQ 
  29. or using anonymous FTP as pub/hacks/mac-faq/CSMP_PD_FAQ You can also 
  30. find it on rtfm.mit.edu unedr the name macintosh/programming-faq.  WWW 
  31. version is available as <http://www.nada.kth.se/~d88-jwa/mac-faq.html>.
  32.  
  33. No FAQ can substitute for real documentation (some of which is 
  34. mentioned in this FAQ) If you ask a question in comp.sys.mac.programmer 
  35. which has a good answer in one of the important sources, you will probably 
  36. not get an answer.  (Inside Macintosh, Macintosh Technical Notes being 
  37. important sources).
  38.  
  39. There is NO or VERY LIMITED error checking in the code examples, FOR 
  40. BREVITY ONLY.  You should make sure you ALWAYS check ALL return codes, 
  41. and handle any that you are not prepared to deal with appropriately.  
  42. Needless to say, do not use the code as is.
  43.  
  44. Exciting new stuff:  More general dev tools update.  See especially 
  45. question 1.2.  List Manager replacements.  The usual tweaking.
  46.  
  47. Topics:
  48. [search for *number* to find a topic quickly]
  49. [topics changed since last FAQ are marked with "+", new topics with ">"]
  50.  
  51.  1. +Development Tools
  52.      getting started, tool-specific issues
  53.  2. Memory
  54.      handles, large arrays, resource handles
  55.  3. User Interfacing
  56.      menus, windows, events, multitasking
  57.  4. Files
  58.      Mac fopen, wdrefnums, getting full pathnames
  59.  5. Imaging
  60.      QuickDraw and the means to avoid it
  61.  6. Text
  62.      Text editing packages, string conversion
  63.  7. Communications and Networking
  64.      Serial ports, TCP/IP, sockets
  65.  8. Interapplication Communication
  66.      AppleEvents, OSA, Scripting, and You
  67.  9. Dynamic Linking & Code Resources
  68.      the dynamics of code resources & trap patches
  69. 10. Compatibility
  70.      gestalt & glue
  71. 11. Optional System Software
  72.    11.1. QuickTime
  73.        codec details and the lack thereof
  74. 12. Third-Party Solutions
  75.    12.1. Databases
  76.        client/server solutions for the masses
  77.    12.2. >Circumventing Toolbox Limitations
  78.        foul baggage begone- List Manager replacements
  79. 13. Dessert
  80.      yummies the Macintosh Way
  81. 14. Contributors
  82.      whodunnit
  83.  
  84. *1* Development and debugging tools for the Macintosh 
  85.  
  86. 1.1) Q: What do I need to start writing Macintosh software? 
  87.  
  88. A: A Mac, a lot of time, and a few hundred $.  Although you can develop 
  89. software on a Classic-type machine, it is not to be attempted by the 
  90. weak of heart or stressed of time.  If you're doing paid work and/or 
  91. work for a company, a Quadra-class machine is a must; remember that your 
  92. time costs your employer much more than just your salary.  A PowerMac is 
  93. highly preferable.  16 MB is a minimum to run at all comfortably (40 MB 
  94. recommended), and Virtual Memory (including RamDoubler, unfortunately) 
  95. is not suited for development work.  Similarly, if you don't have at 
  96. least 80 MB free on your hard disk you need to buy more space.  You will 
  97. also need a CD-ROM drive.
  98.  
  99. You need a development system such as CodeWarrior, MPW Pro, Symantec C++ 
  100. 8.0 or Prograph, you need at least some of the New Inside Mac books 
  101. (Toolbox Essentials, Files, Memory come to mind) and a good entry-level 
  102. third-party book may help.
  103.  
  104. Once you are up to speed on the general layout of the Mac and its 
  105. toolboxes, you should call APDA and order the monthly developer mailing, 
  106. which will give you a CD chock full of documentation, utilities and 
  107. system software once a month.  You will also, obviously, need a CD 
  108. player; Apple's own CD600 is a very good buy at the time of this 
  109. writing.  If you don't have the dough for the monthly mailing 
  110. ($250/year) you can order a _develop_ subscription; this quarterly 
  111. magazine ($30-$50/year) comes with a CD containing most Inside Mac 
  112. documentation.  Another good product to order is the MacOS SDK, which 
  113. for $99 gives you a CD with every API in existence up to and including 
  114. the 7.5 Mac Toolbox additions.  It's somewhat redundant if you already 
  115. have the Developer CD subscription.  <apda@appplelink.apple.com> Apple's 
  116. Developer Web has almost all of the contents of the Developer CD online.  
  117. <http://www.info.apple.com/dev>
  118.  
  119. If you don't know how to program, go learn your language of choice 
  120. BEFORE attempting a "real" Mac application.  Programming is a discipline 
  121. often requiring different thought processes than your normal day job.  A 
  122. beginning book, like Lippman: The C++ Primer, one of the Teach Yourself 
  123. C++ books, or the primers available on the CodeWarrior CD, might help.
  124.  
  125. An indispensable Mac programming tool is the Macintosh Programmer's 
  126. Toolbox Reference (MPTA), an up-to-date hypertext reference guide 
  127. containing reference material on the New Inside Mac-documented portions 
  128. of the Toolbox with lightning-fast look-up and mostly correct usage 
  129. hints and code snippets.  MPTA can be found on the Developer CD, and is 
  130. also offered on a seperate $99 CD.  <apda@applelink.apple.com>
  131.  
  132. Think Reference version 2.0.1, precursor to MPTA, contains reference 
  133. material on many parts of the Mac toolbox with lightning-fast look-up 
  134. and mostly correct usage hints and code snippets.  While it does not 
  135. cover any post-System 7 system additions, nor the modern "universal" 
  136. headers format, it does include information on the standard C/C++ 
  137. libraries.  Available wherever fine Symantec products are sold - 
  138. <apda@applelink.apple.com> for one.
  139.  
  140. 1.2) Q: What is the most used Macintosh development system? 
  141.  
  142. A: Currently, the three most widely used are CodeWarrior (CW), MPW, and 
  143. Symantec C++, probably in that order, though I don't have any 
  144. statistics.  The latest version of any one of these is adequate for Mac 
  145. development.
  146.  
  147. CodeWarrior: In early 1994, CodeWarrior came out of nowhere and grabbed 
  148. a large share of the market visibility because they had the fastest 
  149. compiler and they generated PowerPC code as well as 68K code.  Today, 
  150. CodeWarrior has the smoothest development environment and most complete 
  151. C++ implementation, supporting both templates and exceptions.  CW/6 
  152. includes C, C++, and Object Pascal support, and can generate x86 
  153. binaries.  Among hobbyists, CW seems to be the most popular because of 
  154. it's low price, ultra-fast compile time, and support that no other 
  155. company on the planet can match.  See comp.sys.mac.programmer.codewarrior
  156. for more information and CodeWarrior-related praise.  Contact 
  157. <sales@metrowerks.com>.
  158.  
  159. MPW: The grandaddy of all Mac development environments, descended from 
  160. the original Mac development environment based on a Lisa.  MPW is an 
  161. extremely flexible, powerful, Unix-like command line environment with 
  162. makefile, multiple windows and split-pane support.  Many development 
  163. tools are MPW-based.  MPW Pro comes with C, C++, Pascal, assemblers for 
  164. both 68k and PowerMac, various other useful tools, and the C++-based 
  165. framework MacApp for a reasonable price.  MPW has in the past been 
  166. extremely slow, but shows signs of redemption.  Contact 
  167. <apda@applelink.apple.com>.
  168.  
  169. Symantec C++: This is the eighth-generation descendant of the C
  170. environment favored by Mac developers for over five years.  Symantec C++ 
  171. 8.0 is a complete, scriptable, modular development environment including 
  172. C, C++, and soon, a PowerPC assembler.  The C++ implementation supports 
  173. templates.  A development version of a PowerPC C++ compiler supporting 
  174. exceptions and RTTI is available at Symantec's devtools site.  SC++ 8.0 
  175. doesn't at this time support 68k Mac development.  For that purpose, 
  176. you're required to use the old, decrepit TPM development environment, 
  177. which is included.  Contact <sales@devtools.symantec.com>.
  178.  
  179. There are also at least two Fortran compilers, at least three SmallTalk 
  180. implementations (ObjectWorks, SmallTalk/V and SmallTalkAgents) and 
  181. others.  There are ways of stripping SmallTalk apps so they're smaller 
  182. and faster as standalone apps than in the environment.
  183.  
  184. Languge Systems has Object Pascal and Fortran for PowerMac.  Absoft has 
  185. Fortran and C++ for PowerMac.  These all require MPW.
  186.  
  187. There's also a world-class LISP/CLOS implementation from Apple called 
  188. Macintosh Common Lisp.  Recently, Apple announced that DigiTool has 
  189. licensed MCL with the intent (among other things) to provide a PowerMac 
  190. version and other updates.
  191.  
  192. Zedcor has FutureBasic, which seems to be a very popular...  It also 
  193. seems to be the only well-supported implementation of BASIC on 
  194. Macintosh.
  195.  
  196. CSI has MacForth, of which I only know the name and someone who says 
  197. it's pretty good.
  198.  
  199. There is another good Common Lisp implementation: Procyon Common Lisp.  
  200. I don't know if it is actively supported, but Procyon CL is also 
  201. available for DOS, OS/2 and Windows (as Allegro CL/PC) and actively 
  202. developed.
  203.  
  204. A new possible up-and-coming languge is Apple's Dylan, which is 
  205. something of a cross between BASIC, Pascal, and C.  C-based code can be 
  206. used directly from Dylan, but Dylan can't yet be used directly from C.  
  207. The Apple Dylan environment is rumored to be as far beyond MPW as MPW is 
  208. beyond thick bundles of FORTRAN punch cards.
  209.  
  210. 1.3) Q: Okay, which is the most used Mac programming languge?
  211.  
  212. A: The existing Macintosh code base is mostly C, with C++ second, and 
  213. Pascal finding it's niche in third.  Few people are writing mainstream 
  214. software in Pascal anymore, probably because (a) it's rather hard to 
  215. move to non-Mac platforms (b) Pascal is only rarely being taught past 
  216. the first year in Computer Science.
  217.  
  218. 1.4) Q: Where do I find a free/share/copyleftware C compiler for the Mac? 
  219. Is there a GCC for the mac? What about the FSF boycott of Apple products?
  220.  
  221. A: There is no really good solution for a "for-free" C development 
  222. system for the Mac.  GCC has been ported, but requires the MPW shell and 
  223. MPW assembler to run; these have to be bought from APDA.  There is a 
  224. standalone port of GCC 1.37 on nic.switch.ch:software/mac/src/think_c.  
  225.  
  226. gcc-1.37r14 V1.1 standalone is available for ftp at nic.switch.ch: 
  227. software/mac/src/think_c.
  228.  
  229. A not-entirely-stable port of GCC 2.3.3 to MPW is available for ftp at 
  230. atg.apple.com [anyone know the directory?].  A much more solid port of 
  231. GCC 1.37 is available for MPW as well.
  232.  
  233. Stan Shebs <shebs@cygnus.com>, the driving force behind all of the MPW 
  234. GCC ports, is working on a new port of GCC 2.5.8.
  235.  
  236. For those whose main interest is in developing only text-based C/C++ 
  237. programs, using GCC under MacMiNT might be appropriate.  MacMiNT is a 
  238. UNIX like operating system ported from the Atari ST which supports many 
  239. freely available UNIX utilities like GCC, GDB, make, tcsh, byacc, perl, 
  240. and more.  MacMiNT stuff can be found at nic.switch.ch in 
  241. 'software/mac/src/macmint'.
  242.  
  243. The FSF/LPF boycott of Apple products is over as of January 1995, which 
  244. means they will now incorporate changes made for Macintosh into their 
  245. main code base, if such changes are easily incorporated, and they 
  246. won't be any more antagonistic to Mac programmers than they would be 
  247. to any other micro-to-workstation-class programmers.
  248.  
  249. So what are you waiting for? Go out and port something from GNU! Send in 
  250. the changes!  We still lack decent free development tools!
  251.  
  252. 1.5) Q: Are there any other free Mac development platforms? 
  253.  
  254. A: The best source for information on free compilers/interpreters is the 
  255. Free Compilers FAQ which is written by Brian Connors 
  256. <connorbd@cleo.bc.edu>.  Watch for it in c.s.m.p.info.
  257.  
  258. 1.6) Q: What's the difference between the MPW, Think and CodeWarrior 
  259. environments?
  260.  
  261. A: As of CodeWarrior/6, MPW 3.3, and Symantec C++ 8.0.1 (SC++), 
  262. CodeWarrior will allow faster turnaround times, MPW will provide the 
  263. most flexibility and overall power, and Symantec C++ has the edge with 
  264. regard to helpful project browsing features.
  265.  
  266. CW C++ supports templates and exceptions, Symantec C++ supports only 
  267. templates, MPW's new compilers are based on Symantec C++.
  268.  
  269. SC++ 8 doesn't generate 68k code, while pre-CW/6 CodeWarrior requires 
  270. you to use seperate (virtually identical) environments, and builds are 
  271. controlled from a Makefile in MPW, of course.
  272.  
  273. All three need much hard disk space.  SC++ requires the most RAM.  MPW 
  274. requires the most disk space.
  275.  
  276. MPW is the slowest, followed distantly by SC++, which is followed 
  277. closely by CodeWarrior.
  278.  
  279. The best thing about MPW is that you can write scripts and make files to 
  280. do anything you want in the way you want it.  SC++ and CodeWarrior can 
  281. be AppleScripted to do builds that require more than one link operation, 
  282. but the process is more involved, and CW doesn't currently support 
  283. scripting in full.  SC++ 8 can do builds which require more than one 
  284. link operation.
  285.  
  286. For the MPW and CodeWarrior environments, there are four source level 
  287. debuggers; Metrowerks, SADE, SourceBug, Voodoo Monkey.  The latter is an 
  288. experimental debugger with support for threads debugging; the middle is 
  289. bundled with MPW while SADE has to be bought separately (but is fully 
  290. scriptable in its own scripting language).  Metrowerks Debugger is 
  291. included with CodeWarrior.
  292.  
  293. The Think environments have their own integrated debuggers; the Think 
  294. Pascal one has a lot of useful features while the Think C/C++ one is a 
  295. little more basic (but is gaining in functionality with each release).
  296.  
  297. Metrowerks has their own debugger which works like the MPW debuggers; i e 
  298. it runs the application standalone and pokes at it from the outside, 
  299. while the Think debuggers run the application "wrapped" in a special 
  300. environment, making for some subtle interferences with your heap (which 
  301. you usually don't notice).  The Metrowerks Debugger is Thread 
  302. Manager-savvy on the 68k side.
  303.  
  304. BOTTOM LINE:
  305.  
  306. If you're developing for both Power and regular 68k Macs, you need 
  307. CodeWarrior.  MPW is an option which makes sense if you need to develop 
  308. code for non-CFM OpenTransport or if you have a ridiculously large 
  309. number of independent code modules to compile, or if you're a Unix 
  310. person.  CodeWarrior, in addition to it's own integrated environment, 
  311. includes the non-compiler parts of MPW and MPW-hosted Metrowerks 
  312. compilers/linkers.  SC++ is an option only for PowerMac development.
  313.  
  314. 1.7) Q: What is a good low-level debugger for the Mac? 
  315.  
  316. A: MacsBug is freely available for ftp from <ftp://ftp.apple.com>; log 
  317. in as user anonymous and give your FULL e-mail address as password.  
  318. MacsBug is your basic monitor-type debugger that takes a few hundred Ks 
  319. of memory, and lets you break, step, disassemble, look at the stack etc 
  320. of most anything running on your Mac.  Since it's free (it's also on the 
  321. developer CDs) and provides most of the functionality you need, this is 
  322. a popular choice.  As of 6.5d10, Macsbug supports PowerPC debugging.
  323.  
  324. Jasik Designs has a debugger called The Debugger which can do both low- 
  325. and high-level debugging, with or without source and for all types of 
  326. code, application, code resources, everything.  This is the debugger of 
  327. choice for many large developers because of its high power and many 
  328. features not found anywhere else.  However; newcomers beware! This is 
  329. the Lamborghini of debuggers; if you know how to drive it, it is the 
  330. fastest way from A to B; if you don't, you'll just end up in the ditch.  
  331. The Debugger is PowerMac native and supports PowerPC disassembly.  It 
  332. includes an excellent code coverage tool and MacNosy, a general 
  333. disassembler.  Support is direct from the author and generally great.
  334. <macnosy@jasik.com>
  335.  
  336. 1.8) Q: Are there any visual developments environments for the 
  337. Mac (comparable to Visual C++)?
  338.  
  339. A: There is no Visual C++ as such.  However, there is a C++ parser/editor 
  340. called ObjectMaster which provides good browsing and editing capabilities 
  341. if you already have a C++ compiler.  A demo is available on the CodeWarrior 
  342. CD.  Think C++ comes with a browser built-in, and you can edit 
  343. dialogs/windows using plain old ResEdit, even for your custom view types.
  344.  
  345. Symantec C++ 7.0 also bundles a view editor/code generator called Visual 
  346. Architect; it is fairly complete and has a good level of integration 
  347. into the Think Project Manager.
  348.  
  349. AppMaker is a GUI builder/code generator.  Granted, it's not as nice as 
  350. VC++, but it's quite a product in any case.
  351.  
  352. MarksMan version 3.0 has totally revised TCL templates, and now 
  353. generates well-thought-out TCL code.  It can also generate ANSI C code 
  354. etc.
  355.  
  356. Also, Neuron Data has their UI tool called Open Interface, which is 
  357. better than VC++ and creates code portable across 35 platforms.  
  358. Unfortunately it's $2500 per developer per platform.  There's also two 
  359. other cross-platform products called XVT and Galaxy, the former has 
  360. gotten flak on UseNet while the latter reportedly is the premier 
  361. cross-platform application builder framework; with everything from 
  362. styled text to network support.
  363.  
  364. There is a fully visual, dynamic, object oriented data-flow-driven
  365. programming language for the Mac called Prograph CPX.  It features a
  366. full-featured class library, a powerful, user-extensible GUI Builder, full
  367. access to the entire Mac toolbox, a database engine, high-level interfaces
  368. to SQL, Oracle, etc.  But the coolest thing about Prograph is its
  369. interpretative debugger, fully integrated with the visual code editor,
  370. which lets you write your code _while it's running_.  Execution
  371. automatically rolls back to where changes you make have relevance.  A
  372. PowerMac-native compiler and a Windows version are expected in '95.  A
  373. complete demo version is available from <sales@prograph.com>.  Cost is 
  374. $695 ($395 for students).
  375.  
  376. SmalltalkAgents comes with a GUI builder, which lets you draw your
  377. interface, and then outputs the code for you.
  378.  
  379. If you'd rather do Common Lisp, Macintosh Common Lisp offers a Common 
  380. Lisp Object System with support for most Mac interface items; you can 
  381. edit code while it is running and build stand-alone applications.
  382.  
  383. However, all of these tools generate rather larger binaries with larger 
  384. system demands than a program written in C.  On the other hand; C++ 
  385. programs require more memory and disk space than programs written in 
  386. assembly.  It's a trade-off, and I believe this type of tools is the 
  387. wave of the near future.
  388.  
  389. 1.9) Q: What class libraries are there for the Mac? 
  390.  
  391. A: Apart from the libraries mentioned above, there are three contenders: 
  392. MacApp, TCL, and PowerPlant.  "Bedrock" will never be released as a 
  393. product, although parts of it surface in TCL 2.0 and other parts will be 
  394. the base for the OpenDoc Parts Framework.
  395.  
  396. MacApp is a heavy-duty class library that has tons of features and a 
  397. steep learning curve; it runs under MPW with Pascal or C++, and also 
  398. under Think Pascal 4.0 A major application written in MacApp is 
  399. PhotoShop.
  400.  
  401. TCL stands for Think Class Library and comes with Think Pascal, C or 
  402. C++.  It is a smaller library that still fills most peoples needs; since 
  403. Think C implements a subset of C++ (the most important OO concepts such 
  404. as virtual functions and inheritance) and the TCL is carefully written 
  405. not to take advantage of any C++ features not in Think C, you can use it 
  406. with Think C.  A major application written in TCL is Lotus 1-2-3.  (TCL 
  407. 1.1.3) Starting with Symantec C++ 7.0, Think Class Library 2.0 using 
  408. templates and "real" C++ objects is shipping.
  409.  
  410. The C++ Standard Template Library (STL) compiles under Symantec C++ 
  411. and CodeWarrior, and versions are available for both.
  412.  
  413. PowerPlant is the Metrowerks CodeWarrior offering; it's written by the 
  414. guy who designed the Think Class Library, but it has a lot of 
  415. differences from the original TCL; for one, it's not a monolithic one 
  416. base class framework.  On the other hand, it has some catching up to do 
  417. before it reaches the level of MacApp.  It is gaining quite fast on TCL, 
  418. but isn't all there yet.  It has the most complete support for 
  419. AppleEvents & scripting & drag & drop other modern features.
  420.  
  421. 1.10) Q: How should I debug and test my software? 
  422.  
  423. A: Get ahold of, and install, the extensions DoubleTrouble, 
  424. DisposeResource and EvenBetterBusError.  They will catch 80% of any 
  425. memory related bugs you may have, including many bugs that follow NULL 
  426. handles or pointers.  (Jasik's Debugger (see above) obviates the need 
  427. for these.)
  428.  
  429. A low-level debugger is required, and while you install it, install the 
  430. "leaks" dcmd which will help you catch memory leaks in your application.  
  431. All of these tools are available from <ftp.apple.com>.
  432.  
  433. 1.11) Q: Are there any good Mac programming magazines?
  434.  
  435. A: One Mac programming magazine I know of is MacTech Magazine (formerly 
  436. MacTutor).  It covers a variety of Mac programming topics on various 
  437. levels.  Operating independently from Apple, it has a lot of stuff for 
  438. the beginning Mac programmer, as well as occasional nuggets for the more 
  439. experienced of us.  <custservice@xplain.com>
  440.  
  441. Another VERY GOOD Magazine is _develop_ which is put out by Apple four 
  442. times a year; it comes with a CD containing code for all articles ever 
  443. published in _develop_, and a lot of documentation and system software 
  444. freebies as well.  $30/year in the US.  <dev.subs@applelink.apple.com>
  445.  
  446. 1.12) Q: What about protected memory? I'm sick and tired of re-booting 
  447. when my application crashes.
  448.  
  449. A: Write better software! 
  450.  
  451. Or install The Debugger from Jasik Designs, which can provide your 
  452. application with write-protection of critical parts of memory, if you have 
  453. a 68030-equipped Mac.
  454.  
  455. Making the Mac OS memory-protected is tricky, because applications expect 
  456. to be able to write to low memory, the system heap, temporary memory, 
  457. window lists, and even each other's heaps in some interapplication 
  458. communication solutions that date back to before AppleEvents and the PPC 
  459. Toolbox.  To add to the burden, Apple's own software tends to be the 
  460. worst offender in these cases.
  461.  
  462. But fear not, Mac fans! Jonathan Kimmitt has written Patmos, the 
  463. "Protected address translation mode operating system".  It is an 
  464. application that brings the advantages of protected mode programs to 
  465. your Quadra class Macintosh by the simple expedient of taking over the 
  466. memory management unit of the 68040 in a very simple kernel (<100K in 
  467. size), we immediately gain compatibility with the BSD unix program 
  468. environment.  The advantages of this are as follows:
  469.  
  470. (a) You can run certain programs (such as /bin/sh) designed for MacBSD
  471. (b) You can compile almost all GNU software including C and C++ without.
  472. modifying the source code in any way
  473. (c) All programs run with a flat 32-meg address space, with no worries
  474. about 32K segments or the other mac paraphernalia.
  475. (d) The majority of program bugs can be caught cleanly without crashing
  476.  your mac
  477. (e) All your files are shared between Patmos and MacOS so you can edit 
  478. using your favourite mac editor, then immediately compile in Patmos 
  479. without having to reboot or copy files around.
  480.  
  481. The downside is that not all macs use the memory management unit in the 
  482. same way, or even have the same kind of MMU, so Patmos may not run on 
  483. your particular mac model.  However, since the kernel source code is 
  484. very small, the task of adapting it to a new environment is very simple, 
  485. and once achieved, all application programs running in user mode are 
  486. enabled to run without even recompiling.
  487.  
  488. <ftp://nic.switch.ch in /software/mac/src/patmos>
  489.  
  490. 1.13) Q: I have this library written in (Think) Pascal that I want to use 
  491. from Think C/Symantec C, but I get link errors/don't know how to do it.  
  492. What should I do?
  493.  
  494. A: Start by writing a .h file describing the interface.  Remember to 
  495. declare the Pascal functions "pascal".  Build a library with Think Pascal 
  496. and convert it with oConv.
  497.  
  498. Do you get link errors on symbols defined in your Pascal lib? Check the 
  499. capitalisation used.
  500.  
  501. Do you get errors on symbols like LMUL and LDIV? Those functions are 
  502. defined in the Think Pascal library Runtime.lib or uRuntime.lib.  Include 
  503. uRuntime.lib and try again.
  504.  
  505. Do you get link errors on standard symbols like thePort? This is due to 
  506. bad capitalization in Symantec's libs.  Run oConv with .v checked.  This 
  507. will create a TEXT file with a .v extension.  Open that with a text 
  508. editor and correct the capitalization.  Run oConv again, with .v checked 
  509. this time too.
  510.  
  511. Do you still get errors on standard symbols? Are you using Think 
  512. C/Symantec C++ version 6 or higher? Then you must open the library (after 
  513. converting it) from Think C version 5, and remove the unit named 
  514. %_TOOLBOX.  (If I'm not mistaken, this is the toolbox init unit, which 
  515. you won't need anyway.)
  516.  
  517. 1.14) Q: CodeWarrior vs. Think/Symantec C++:  Which is better?
  518.  
  519. A:  See the above discussion on CodeWarrior, Think, and MPW for a full 
  520. understanding of the issues involved.
  521.  
  522. 1.15) Q: Can CodeWarrior read Think libraries?
  523.  
  524. A: Yes, in a way.  Here are the steps required.
  525.  
  526. 1) Secure a copy of Think Pascal and a machine that can run it
  527. 2) Import the Think C library into Think Pascal
  528. 3) Build a Think Pascal library (in MPW format)
  529. 4) Import MPW format library into CodeWarrior
  530.  
  531. 1.16) Q: What are some good books on the subject of learning the Mac 
  532. Toolbox?
  533.  
  534. A: Any of Dan Parks Sydow's numerous books on the subject.  Recommended 
  535. also is Dave Mark's _Ultimate Mac Programming_ (a Macworld book, for 
  536. some odd reason).  Stay away from Dave Mark's _Learn C on the Mac_ and 
  537. _Learn C++ on the Mac_.
  538.  
  539. See also Nick DeMello's books review, posted in c.s.m.p.info from time 
  540. to time.
  541.  
  542. 1.17) Q:  Source code!  I want source code!  Where can I find some?
  543.  
  544. A: Celestin Company, Inc. sells the Apprentice 2 CD-ROM.  Apprentice 
  545. contains over 600 megabytes of programmer utilities and up-to-date 
  546. source code in CodeWarrior, Symantec, and MPW projects for C, C++, and 
  547. Pascal.  <ftp://ftp.teleport.com/vendors/cci/apprentice/apprentice.hqx>
  548. for an index and info.  <celestin@olympus.net>
  549.  
  550. Also, the alt.sources.mac archive at <ftp://ftpbio.bgsu.edu> contains a 
  551. lot of misc. source code and snippets not found elsewhere.
  552.  
  553. <ftp://nic.switch.ch> is another good source for unique source code.
  554.  
  555. Info-mac is a good source for source, info-mac/dev/src.
  556.  
  557. 1.18) Q: I'm trying to use a largish array in Think C, but get a "code 
  558. overflow" error.  This is valid C, why doesn't it work?
  559.  
  560. A: The ANSI standard does not guarantee that any structure larger than 
  561. 32767 bytes be correctly handled.  Because of historical constraints, 
  562. the Mac memory model is built around several small blocks of size 32K or 
  563. less; these are used both for code and global/static data.  If you want 
  564. to use more code or data, you have to turn on "far code" or "far data" - 
  565. you still will not get around the restriction of 32K code or data per 
  566. compiled file, though.
  567.  
  568. This is one area where CodeWarrior's 68k support shines; it works around 
  569. most such limitations and it doesn't cost much in performance either!
  570.  
  571. As opposed to, say, DOS or Windows, however, you can allocate as much 
  572. memory as you want (and there is in the machine) and step through it 
  573. using ordinary pointers; it's just that global and static data space is 
  574. addressed off the A5 register using a 16bit displacement addressing mode 
  575. in the 68000 processor.
  576.  
  577. On the PowerPC, everything is 32bit from the start; that runtime model 
  578. is much more like UNIX.  The horizon is the limit.
  579.  
  580. *2* Memory
  581.  
  582. 2.1) Q: What is a handle? 
  583.  
  584. A: A handle is a pointer to a pointer to something.  However, it is more 
  585. than that; creating a handle by taking the address of one of your own 
  586. pointers does NOT create a Handle; the Memory Manager will only deal 
  587. properly with Handles that are created using NewHandle or something that 
  588. calls it (such as NewRgn or GetResource).
  589.  
  590. 2.2) Q: When do I have to lock a Handle? 
  591.  
  592. A: The contents of a Handle may move, and when it does, the pointer your 
  593. handle is pointing to is changed to point to the new address so your 
  594. handle is always valid.  The toolbox may call the memory manager to 
  595. allocate more memory pretty much anytime you call it (the toolbox) and 
  596. when memory is allocated, your handle may move in memory.  Don't 
  597. dereference a handle into a pointer (or take the address of a field in a 
  598. record a handle is double-pointing to) and then call the toolbox and 
  599. expect the pointer to still be valid.  The only way to ensure that the 
  600. pointer will still be valid is to call HLock on the handle to lock it.
  601.  
  602. Use HGetState and HSetState to save & restore the "locked" state of a 
  603. handle when you lock it.
  604.  
  605. 2.3) Q: How do I dispose of Handles? 
  606.  
  607. A: DisposeHandle (formerly called DisposHandle) once and ONLY once will 
  608. do the trick.  Trying to dispose of an already disposed Handle is an 
  609. error.  DoubleTrouble (see above) will catch such bugs when they do 
  610. occur.
  611.  
  612. 2.4) Q: What about resources? 
  613.  
  614. A: Calling GetResource returns NULL if the resource is not found or 
  615. there is not enough memory, else it returns a handle to the resource.  
  616. This handle may be moved or locked like any other handle, but DO NOT 
  617. call DisposeHandle to get rid of a resource handle - call 
  618. ReleaseResource.  DisposeResource (see above) will catch this kind of 
  619. bug.
  620.  
  621. Remember that AddResource makes a resource handle out of an ordinary 
  622. handle, and RemoveResource or DetachResource makes an ordinary handle 
  623. out of a resource handle.  You cannot call AddResource with a resource 
  624. handle; you have to DetachResource it first.
  625.  
  626. Resource handles are automagically disposed when the resource file they 
  627. belong to is closed.
  628.  
  629. *3* User / Machine interaction
  630.  
  631. 3.1) Q: How do I read the modifier keys of the keyboard? 
  632.  
  633. A: Just call EventAvail and check the event.modifiers field. 
  634. Only works when you are in the foreground. You can also use 
  635. GetKeys(), or (as a last resort) check the lo-mem global KeyMap 
  636. directly. 
  637.  
  638. 3.2) Q: How do I move the mouse cursor to a specific position?
  639.  
  640. A: Wait! Don't do it! There has to be a better way!
  641.  
  642. If you feel you HAVE to do it (for a game or VERY special simulation 
  643. situation) you can use the Cursor Device Manager documented in the tech 
  644. notes on <ftp.apple.com>.  If that manager is not installed, as it's not 
  645. on older Macs, you can use the following code:
  646.  
  647. You need to have some low-memory globals defined.  they may be defined 
  648. in SysEqu.h.
  649.  
  650. #define MTemp 0x828
  651. #define RawMouse 0x82c
  652. #define CrsrNewCouple 0x8ce
  653.  
  654. Note that CrsrNewCouple is actually a combination of two globals, just 
  655. to make our life slightly easier.
  656.  
  657. The code I use to move the mouse is:
  658.  
  659. *code*
  660. void
  661. MoveMouseTo ( Point where ) {
  662.  
  663.     HideCursor ( ) ;
  664.     * ( Point * ) RawMouse = where ;
  665.     * ( Point * ) MTemp = where ;
  666.     * ( short * ) CrsrNewCouple = -1 ;
  667.     ShowCursor ( ) ;
  668. }
  669. *end*
  670.  
  671. You need to hit a couple more global variables if you want this to work 
  672. properly in a multiple-monitor system, but i forget what they are 
  673. offhand.  poke through SysEqu.h, and you should be able to figure it out 
  674. without a problem.
  675.  
  676. On the PowerPC, these lo-mem globals may not be available for native 
  677. applications; however, all Power Macintoshes implement the Cursor Device 
  678. Manager.  All Macs made after March '93 (including Centris 650 and 610) 
  679. implement the Cursor Device Manager, in fact.
  680.  
  681. There is also a file on nada.kth.se:pub/hacks/mac-faq/MoveMouse.c which 
  682. shows how to use the Cursor Device Manager, written by an excellent 
  683. Apple engineer.  Grab!
  684.  
  685. Careful, version 1.0 of the Universal Header "CursorDevices.h" file was 
  686. completely incorrect.  Use 2.0a3 or later.
  687.  
  688. 3.3) Q: My menus don't show up in the menu bar
  689.  
  690. A: If your menus are hiearchical, you'll have to install them manually; 
  691. GetNewMBar won't do it for you.  See also 3.5.
  692.  
  693. 3.4) Q: When the user selects my menus, I get strange results back; they 
  694. seem to have different menu IDs than my menus?
  695.  
  696. A: The Menu ID as used by the menu manager is NOT the same thing as the 
  697. MENU resource ID (used in the MBAR resource and with GetMenu()) When you 
  698. create a MENU, ResEdit sets the menu ID to the MENU resource ID, but if 
  699. you re-number the resource, you will have to open the menu in ResEdit 
  700. and change the menu ID using the "Edit MENU ID" menu item.
  701.  
  702. 3.5) Q: I use GetMenu() to find a menu in the menu bar, and then change 
  703. it, but it seems I have a memory leak OR my changes don't "punch 
  704. through"
  705.  
  706. A: GetMenu() is only intended if you don't already have the menu "in 
  707. memory." The call you should use almost all the time is GetMHandle() 
  708. which gets the handle to a menu in the current menu bar by its menu ID 
  709. (not resource id).
  710.  
  711. 3.6) Q: What about pre-emptive multitasking? 
  712.  
  713. A: To the user, the Mac multitasking method, which builds upon each 
  714. application calling WaitNextEvent, GetNextEvent or EventAvail every so 
  715. often and the Process Manager/MultiFinder switching applications only at 
  716. such calls, is at least as good as preemtive multitasking, because the 
  717. present system priotitizes user interface responsiveness over everything 
  718. else.  The only shortfall about this is formatting floppies, which locks 
  719. up the Mac CPU.  This is because the Mac floppy controller is really 
  720. stupid, and would happen even if the Mac multitasked preemptively.
  721.  
  722. There IS "real" pre-emptive multitasking available for use in Mac 
  723. applications; the expensive way is buying A/UX 3.0 which can have Mac 
  724. applications written as UNIX processes; the cheap way is installing the 
  725. Thread Manager which will allow you to create pre-emptive threads.  
  726. However, the restrictions on those threads are the same as those on Time 
  727. Manager tasks: don't call any function in an unloaded segment, and don't 
  728. call QuickDraw or any toolbox call which may move memory (which are most 
  729. ToolBox calls; paradoxally, BlockMove is safe :-) as are, surprisingly, 
  730. FSRead and FSWrite).  The latest word from Apple is that this 
  731. preemptive support is going away, to be replaced by something else in 
  732. Copland.
  733.  
  734. There are several problems with making the Mac OS preemptive; including 
  735. apps that draw outside their windows or directly to screen, user 
  736. dragging and other issues.  The system is being reimplemented for 8.0 
  737. (Copland) to solve these problems.
  738.  
  739. *4* Files
  740.  
  741. 4.1) Q: How do I tell fopen() to open a file the user has selected using 
  742. StandardGetFile?
  743.  
  744. A: The "standard" ANSI C file functions are less than well suited for 
  745. the Macintosh way of doing things.  However, if you are doing a port for 
  746. your own enjoyment and benefit (or maybe for in-house work) you can use 
  747. the following function: (see below about converting a wdRefNum into a 
  748. vRefNum/parID pair)
  749.  
  750. *code*
  751. FILE *
  752. fopen_mac ( short vRefNum , long parID , char * fileName , char * mode ) {
  753.  
  754. short oldVol ;
  755. short aVol ;
  756. long aDir , aProc ;
  757. FILE * ret = NULL ;
  758.  
  759.     if ( GetVol ( NULL , & oldVol ) ) {
  760.         return NULL ;
  761.     }
  762.     if ( GetWDInfo ( oldVol , & aVol , & aDir , & aProc ) ) {
  763.         return NULL  ;
  764.     }
  765.     if ( HSetVol ( NULL , vRefNum , parID ) ) {
  766.         return NULL ;
  767.     }
  768.     ret = fopen ( fileName , mode ) ;
  769.     if ( HSetVol ( NULL, aVol , aDir ) ) {
  770.         /* an error we can't currently handle */
  771.     }
  772.     if ( SetVol ( NULL, oldVol ) ) {
  773.         /* an error we can't currently handle */
  774.     }
  775.     return ret ;
  776. }
  777. *end*
  778.  
  779. All of the above is necessary for one reason or another - if you are 
  780. interested, by all means look HSetVol up in Think Reference 2.0 or New 
  781. Inside Mac: Files.
  782.  
  783. In older versions of MPW; this wouldn't work since the MPW libraries 
  784. used to do a GetVol and explicitly use that value by itself.
  785.  
  786. 4.2) Q: When can I use the HOpen, HCreate etc file calls? Are they only 
  787. System 7 calls?
  788.  
  789. A: All the HXxx calls that take a vRefNum and parID as well as the file 
  790. name are implemented in glue that works on any system that has HFS 
  791. (meaning 3.2 and up with the HD20 INIT, and all systems from System 6 
  792. and up)
  793.  
  794. The glue is available in MPW 3.2 and up, and Think C 5.0 and up.  This 
  795. goes for all HXxx calls except HOpenDF; therefore, if you are interested 
  796. in System 6 compatibility, use HOpen instead and make sure you don't 
  797. allow file names beginning with a period.
  798.  
  799. 4.3) Q: Why do you say wdRefNum sometimes and vRefNum sometimes? 
  800. Why do you say parID sometimes and dirID sometimes? 
  801.  
  802. A: When the Mac first made an appearance in 1984, it identified files by 
  803. using a vRefNum (volume reference number meaning a floppy disk or later 
  804. hard disk) and a name.  Once HFS saw the light of day, folders within 
  805. folders became a reality, and you needed a dirID as well to point out 
  806. what folder you really meant on the volume.  However, older programs 
  807. that weren't being rewritten still knew nothing about directory IDs, so 
  808. Apple had SFGetFile make up "fake" vRefNums that didn't just specify a 
  809. volume, but also a parent folder.  These are called wdRefNums (for 
  810. working directory) and were a necessary evil invented in 1985.  You 
  811. should not create (or, indeed, use) wdRefNums yourself.
  812.  
  813. There is a system-wide table that maps wdRefNums onto vRefNum/parID 
  814. pairs.  There is a limit to the size of this table.  A dirID and a parID 
  815. is almost the same thing; you say "parID" when you mean the folder 
  816. something is in, while you say a "dirID" when you mean the folder 
  817. itself.  If you for instance have a folder called "Foo" with a folder 
  818. called "Bar" in it, the parID for "Bar" would be the dirID for "Foo."
  819.  
  820. 4.4) Q: How do I convert a wdRefNum as returned by SFGetFile 
  821. into a vRefNum/parID pair to use with the HXxx calls?
  822.  
  823. A: Use GetWDInfo, which is declared as: 
  824.  
  825. Pascal OSErr GetWDInfo ( short wdRefNum , short * vRefNum , long * parID 
  826. , OSType * procID ) ;
  827.  
  828. The procID parameter must be non-NULL and point to an OSType variable, 
  829. but the value of that variable can and should be ignored.
  830.  
  831. It is recommended that, as soon as you get your hands on a wdRefNum, for 
  832. instance from SFGetFile, you directly convert it into a vRefNum/parID 
  833. pair and always use the latter to reference the folder.
  834.  
  835. 4.5) Q: How do I select a folder using SFGetFile? 
  836.  
  837. A: This requires a custom dialog with a filter proc.  It is too 
  838. complicated to show here, but not totally impossible to comprehend.  
  839. There is sample code on ftp.apple.com, in the directory dts/snippets, on 
  840. how to do this.
  841.  
  842. 4.6) Q: How do I get the full path of a file referenced by a vRefNum, 
  843. parID and name?
  844.  
  845. A: You don't. 
  846.  
  847. OK, I cheated you.  There is exactly ONE valid reason to get the full 
  848. path of a file (or folder, for that matter) and that is to display its 
  849. location to the user in, say, a settings dialog.  To actually save the 
  850. location of the file you should do this: (assuming the file is in an 
  851. FSSpec called theFile - you can use FSSpecs in your program even if you 
  852. don't run under System 7; just make your own MyFSMakeFSSpec that fills 
  853. in the FSSpec manually if it's not implemented)
  854.  
  855. *code*
  856. if ( ! aliasManagerAvailable ) { /* System 6 ? */
  857.     GetVolumeName ( theFile -> vRefNum , vName ) ;
  858.     GetVolumeModDate ( vRefNum , & date ) ;
  859.     Save ( vName , date , parID , fileName ) ;
  860. } else {
  861.     NewAlias ( NULL , theFile , & theAlias ) ;
  862.     Save ( theAlias ) ;
  863.     DisposeHandle ( ( Handle ) theAlias ) ;
  864. }
  865. *end*
  866.  
  867. If you are really concerned about these issues (of course you are!) you 
  868. should save BOTH of these methods when available, and load back whatever 
  869. is there that you can handle; since users may be using your application 
  870. in a mixed System 6/System 7 environment.
  871.  
  872. To get back to the file is left as an exercise for the reader. 
  873.  
  874. To open a file using fopen() or the Pascal equivalent, see above about 
  875. using and not using HSetVol.
  876.  
  877. 4.7) Q: What about actually getting the full path for a file? I promise 
  878. I will only use it to show the location of a file to the user!
  879.  
  880. A: Enter PBGetCatInfo, the Vegimatic of the Mac file system.  Any Mac 
  881. hacker of knowledge has taken this system call to his heart.  Note that 
  882. this sample code isn't all there, but should point you in the right 
  883. direction:
  884.  
  885. *code*
  886. Boolean IsFolder(FSSpec *fs) // this function is called later
  887. {
  888.         CInfoPBRec rec;
  889.  
  890.         rec.hFileInfo.ioNamePtr = fs -> name;
  891.         rec.hFileInfo.ioVRefNum = fs -> vRefNum;
  892.         rec.hFileInfo.ioDirID = fs -> parID;
  893.  
  894.  
  895.         if ( !fs -> name [ 0 ] )
  896.         {
  897.                 rec . hFileInfo . ioFDirIndex = 0 ;
  898.         } else
  899.         {
  900.                 rec . hFileInfo . ioFDirIndex = -1 ;
  901.         }
  902.  
  903.         rec . hFileInfo . ioFVersNum = 0 ;
  904.         PBGetCatInfoSync (&rec);
  905.  
  906.         return(!rec.hFileInfo.ioFlAttrib & 0x10);
  907. }
  908. *end*
  909.  
  910. *code*
  911. OSErr GetFolderParent(FSSpec *fss, FSSpec *parent)
  912. {
  913.     CInfoPBRec rec;
  914.     short err;
  915.  
  916.     *parent = *fss;
  917.     rec.hFileInfo.ioNamePtr = parent -> name ;
  918.     rec.hFileInfo.ioVRefNum = parent -> vRefNum ;
  919.     rec.hFileInfo.ioDirID = parent -> parID ;
  920.  
  921.     if ( !parent -> name [ 0 ] )  // dougw -- neg of FAQ
  922.     {
  923.         rec . hFileInfo . ioFDirIndex = 0 ;
  924.     }
  925.     else
  926.     {
  927.         rec . hFileInfo . ioFDirIndex = -1 ;
  928.     }
  929.  
  930.     rec . hFileInfo . ioFVersNum = 0 ;
  931.     err = PBGetCatInfoSync ( & rec ) ;
  932.  
  933.     if ( ! ( rec . hFileInfo . ioFlAttrib & 0x10 ) ) { /* Not a folder */
  934.         if ( ! err ) {
  935.             err = dirNFErr ;
  936.         }
  937.     } else {
  938.         parent -> parID = rec . dirInfo . ioDrParID ;
  939.         BlockMove(rec.dirInfo.ioNamePtr, parent->name, rec.dirInfo.ioNamePtr[0]);
  940.     }
  941.     return err ;
  942. }
  943. *end*
  944.  
  945. *code*
  946. OSErr GetFullPathHandle ( FSSpec * fss , Handle * h )
  947. {
  948.     Handle  tempH = NULL;
  949.     FSSpec fs = * fss ;
  950.     FSSpec sSpec;
  951.  
  952.     if(*h == NULL) // allocate a handle if needed
  953.     {
  954.         *h = NewHandle(0);
  955.     }
  956.  
  957.     while ( fs . parID > 1 )
  958.     {
  959.         tempH = NULL ;
  960.         PtrToHand ( & fs . name [ 1 ] , & tempH , fs . name [ 0 ] ) ;
  961.         PtrAndHand ( ( void * ) ":" , tempH , 1 ) ;
  962.         HandAndHand ( * h , tempH ) ;
  963.         SetHandleSize ( * h , 0L ) ;
  964.         HandAndHand ( tempH , * h ) ;
  965.         DisposeHandle ( tempH ) ;
  966.  
  967.         tempH = NULL ;
  968.         GetFolderParent ( & fs , & sSpec ) ;
  969.         fs = sSpec ;
  970.     }
  971.     
  972.     // fs should now contain info about the volume itself
  973.     PtrToHand ( & fs . name [ 1 ] , & tempH , fs . name [ 0 ] ) ;
  974.     PtrAndHand ( ( void * ) ":" , tempH , 1 ) ;
  975.     HandAndHand ( * h , tempH ) ;
  976.     SetHandleSize ( * h , 0L ) ;
  977.     HandAndHand ( tempH , * h ) ;
  978.     DisposeHandle ( tempH ) ;
  979.     tempH = NULL ;
  980.  
  981.     if (!IsFolder ( fss ) )
  982.     {
  983.         SetHandleSize ( * h , GetHandleSize ( * h ) - 1 ) ;
  984.     }
  985.  
  986.     DisposeHandle(tempH);
  987.     return 0 ;
  988. }
  989. *end*
  990.  
  991. 4.8) Q: So how do I get the names of the files in a directory? 
  992.  
  993. A: You use PBGetCatInfo again, but this time you set ioFDirIndex to 1 or 
  994. more (you need to know the dirID and vRefNum of the folder you're 
  995. interested in) You then call PBGetCatInfoSync for values of ioFDirIndex 
  996. from 1 and up, until you get an fnfErr.  Any other err means you are not 
  997. allowed to get info about THAT item, but you may be for the next.  Then 
  998. collect the names in the string you made ioNamePtr point to as you go 
  999. along.  Note that you need to fill in the ioDirID field for each 
  1000. iteration through the loop, and preferably clear the ioFVersNum as well.
  1001.  
  1002. Note that the contents of a directory may very well change while you are 
  1003. iterating over it; this is most likely on a file server that more than 
  1004. one user uses, or under System 7 where you run Personal File Share.
  1005.  
  1006. 4.9) Q: How do I find the name of a folder for which I only know the 
  1007. dirID and vRefNum?
  1008.  
  1009. A: You call (surprise!) PBGetCatInfo! Make ioNamePtr point to an empty 
  1010. string (but NOT NULL) of length 63 (like, an Str63) and ioFDirIndex 
  1011. negative (-1 is a given winner) - this makes PBGetCatInfo return 
  1012. information about the vRefNum/dirID folder instead of the file/folder 
  1013. specified by vRefNum, parID and name.
  1014.  
  1015. 4.10) Q: How do I make the Finder see a new file that I created? Or if I 
  1016. changed the type of it; how do I display a new icon for it?
  1017.  
  1018. A: You call (surprise!) PBGetCatInfo followed by PBSetCatInfo for the 
  1019. FOLDER the file is in.  Inbetween, you should set ioDrMdDat to the 
  1020. current date&time.  Code:
  1021.  
  1022. *code*
  1023. OSErr
  1024. TouchFolder ( short vRefNum , long parID ) {
  1025.  
  1026. CInfoPBRec rec ;
  1027. Str63 name ;
  1028. short err ;
  1029.  
  1030.     rec . hFileInfo . ioNamePtr = name ;
  1031.     name [ 0 ] = 0 ;
  1032.     rec . hFileInfo . ioVRefNum = vRefNum ;
  1033.     rec . hFileInfo . ioDirID = parID ;
  1034.     rec . hFileInfo . ioFDirIndex = -1 ;
  1035.     rec . hFileInfo . ioFVersNum = 0 ;
  1036.     err = PBGetCatInfoSync ( & rec ) ;
  1037.     if ( err ) {
  1038.         return err ;
  1039.     }
  1040.     GetDateTime ( & rec . dirInfo . ioDrMdDat ) ;
  1041.     rec . hFileInfo . ioVRefNum = vRefNum ;
  1042.     rec . hFileInfo . ioDirID = parID ;
  1043.     rec . hFileInfo . ioFDirIndex = -1 ;
  1044.     rec . hFileInfo . ioFVersNum = 0 ;
  1045.     rec . hFileInfo . ioNamePtr [ 0 ] = 0 ;
  1046.     err = PBSetCatInfoSync ( & rec ) ;
  1047.     return err ;
  1048. }
  1049. *end*
  1050.  
  1051. 4.11) Q: Aren't we done with PBGetCatInfo soon? 
  1052.  
  1053. A: Well, it turns out that you can also find out whether an FSSpec is a 
  1054. file or a folder by calling PBGetCatInfo and check bit 4 (0x10) of 
  1055. ioFlAttr to see whether it is a folder.  You may prefer to call 
  1056. ResolveAliasFile for this instead.
  1057. You can also check the script of the file's title using PBGetCatInfo and 
  1058. check the ioFlFndrXInfo field if you want to work with other script 
  1059. systems than the Roman system.
  1060.  
  1061. Another common use is to find out how many items are in a folder; the 
  1062. modification date of something or the correct capitalization of its name 
  1063. (since the Mac file system is case independent BUT preserves the case 
  1064. the user uses)
  1065.  
  1066. 4.12) Q: How do I set what folder should initially be shown in the 
  1067. SFGetFile boxes?
  1068.  
  1069. A: You stuff the dirID you want to show into the lo-mem global 
  1070. CurDirStore, and the NEGATIVE of the vRefNum you want into the lo-mem 
  1071. global SFSaveDisk.
  1072.  
  1073. If you are using CustomGetFile and return sfSelectionChanged from an 
  1074. "init" message handler, you must remember to clear the script code, else 
  1075. the selection will not change.
  1076.  
  1077. 4.13) Q: How do I find the folder my application started from? How do I 
  1078. find the application file that's running?
  1079.  
  1080. A: Under System 7, you call GetProcessInformation using the 
  1081. ProcessSerialNumber kCurrentProcess with a pointer to an existing FSSpec 
  1082. in the parameter block.  This will give you your file, and, by using the 
  1083. vRefNum and parID, the folder the application is in.
  1084.  
  1085. *code*
  1086. OSErr   CurrentProcessLocation(FSSpec *applicationSpec)
  1087. {
  1088.     ProcessSerialNumber currentPSN;
  1089.     ProcessInfoRec info;
  1090.     
  1091.     currentPSN.highLongOfPSN = 0;
  1092.     currentPSN.lowLongOfPSN = kCurrentProcess;
  1093.     info.processInfoLength = sizeof(ProcessInfoRec);
  1094.     info.processName = NULL;
  1095.     info.processAppSpec = applicationSpec;
  1096.     return ( GetProcessInformation(¤tPSN, &info) );
  1097. }
  1098. *end*
  1099.  
  1100. Beware from writing to your applications resource or data forks; the 
  1101. former breaks on CDs/write protected floppies/file servers/virus 
  1102. checkers, the latter fails on PowerPC as well as in the above cases.
  1103.  
  1104. 4.14) Q: When can I use those nifty, easy to use FSSpec calls?
  1105.  
  1106. A: In Systems >= 7, in System 6 with QuickTime installed, in any system 
  1107. if you use the FSpCompat functions provided by MoreFiles [see below].
  1108.  
  1109. 4.15) Q: I hate dealing with the low-level file manager stuff;  why 
  1110. didn't Apple provide a complete high-level interface using FSSpecs?
  1111.  
  1112. A: Good question.  Apple, in the guise of Jim Luther of Mac Developer 
  1113. Technical Support, has written a library called MoreFiles, which not 
  1114. only provides a high-level interface to low-level file stuff, but 
  1115. provides FSSpec glue for Systems below 7.  MoreFiles is available on the 
  1116. Developer CD's (see above) and also at Appple's developer ftp site.
  1117.  
  1118. *5* Imaging with QuickDraw
  1119.  
  1120. 5.1) Q: Why is CopyBits so slow? 
  1121.  
  1122. A: It is not.  It just requires some hand-holding to get good results.  
  1123. The main rules are: Make sure the source and destination pixMaps are of 
  1124. the same depth.
  1125.  
  1126. Make sure the front color is black and the back color is white.
  1127.  
  1128. Use srcCopy and don't use a masking region, unless it's rectangular. 
  1129.  
  1130. Copy to an unclipped window (the frontmost window).  Make sure the 
  1131. ctSeed values of the source pixMap and dest pixMap match. 
  1132.  
  1133. Copying few and large pixMaps is faster than copying many and small 
  1134. ones.  Icon-sized sprites count as small ones.
  1135.  
  1136. Make sure your source bitmap or pixelMap has the same alignment, when 
  1137. adjusted for the source and destination rect expressed in global screen 
  1138. coordinates.  The necessary alignment is 32 bits (4 bytes), although 128 
  1139. bit (16 byte) alignment is probably even better on 68040 macs and won't 
  1140. hurt on other macs.
  1141.  
  1142. Example of global alignment: 
  1143.  
  1144. Your window is positioned at (42,100) (H,V) 
  1145.  
  1146. Your destination rectangle is (10,20)-(74,52) 
  1147.  
  1148. The alignment coefficient of the rectangle in global coordinates is 
  1149. (42+10)*bitDepth where bitDepth is one of 1,2,4,8,16 or 32.
  1150.  
  1151. Make sure your source pixmap rect has the same coeffecient modulo your 
  1152. alignment factor (in bits) For black&white macs, this is still true, 
  1153. although bitDepth is fix at 1.  Offscreen pixMaps can calculate with a 
  1154. "global posistion" of 0,0 and get correct results.
  1155.  
  1156. 5.2) Q: Why is CopyBits still too slow? 
  1157.  
  1158. A: Because there is always some overhead involved in calling QuickDraw; 
  1159. you have the trap dispatcher, clipping checks, and checking whether the 
  1160. CopyBits call is being recorded in a PICT handle (if you called 
  1161. OpenPicture)
  1162.  
  1163. If you can't live with this, look at 4.8 below, but PLEASE try and make 
  1164. CopyBits work, and retain the CopyBits code in your application, so 
  1165. users with special monitors (accellerator cards, PowerBook color 
  1166. screens, Radius Pivot screens) can still play your game.  (non-game 
  1167. applications don't need more speed than CopyBits can give at its max.  
  1168. Promise!)
  1169.  
  1170. 5.3) Q: What is the fastest way to set one pixel? 
  1171.  
  1172. A: On 68k Macs, NOT SetCPixel()! Assuming you have the correct ForeColor() 
  1173. set, you can set the pen size to (1,0) and call Line (0,1)
  1174.  
  1175. I have heard PaintRect is good for this but requires slightly more code.  
  1176. Using PaintRect eliminates a trap call.
  1177.  
  1178. 5.4) Q: Why do pictures I record suddenly draw as empty space or not 
  1179. draw at all?
  1180.  
  1181. A: When recording pictures, you have to set the clipping area to exactly 
  1182. the frame of the picture you are recording.  This is because it is 
  1183. initally set at -32768,32727 in both directions, and offsetting the 
  1184. picture even one pixel when drawing it will result in the region 
  1185. wrapping around and becoming empty.
  1186.  
  1187. When recording pictures, do this: 
  1188.  
  1189. *code*
  1190. PicHandle h = OpenPicture ( & theRect ) ;
  1191. ClipRect ( & theRect ) ;
  1192.     /* draw the picture */
  1193. ClosePicture ( ) ;
  1194. *end*
  1195.  
  1196. 5.5) Q: Where can I find the format of picture files and 
  1197. resources? 
  1198.  
  1199. A: The format of a picture resource version 1 is defined in a technical 
  1200. note.  This format is obsolete.
  1201.  
  1202. The format of a picture resource version 2 is defined in Old Inside Mac 
  1203. vol V, with addenda in Old Inside Mac vol VI.
  1204.  
  1205. Some things happen with QuickTime compressed pictures; try the Inside 
  1206. Mac: QuickTime book or turn to Inside Mac: Imaging with QuickDraw which 
  1207. is the definite reference on QuickDraw.
  1208.  
  1209. The format of a picture file is the same as that of a picture resource 
  1210. with 512 added 0 bytes in front.
  1211.  
  1212. 5.6) Q: GWorlds? 
  1213.  
  1214. A: What about them?  They're great.  Look them up in IM: Imaging With 
  1215. QuickDraw.  Don't forget to SetGWorld back to what it was before calling 
  1216. WaitNextEvent.
  1217.  
  1218. 5.7) Q: How do I find the current depth of the screen? 
  1219.  
  1220. A: My question to you is: What screen? Many macs have more than one 
  1221. screen attached.  You can use GetDeviceList and walk the devices to find 
  1222. the screen you're looking for (use TestDeviceAttribute to see whether 
  1223. it's a screen) or you can call GetMaxDevice() to find the deepest device 
  1224. your window intersects.
  1225.  
  1226. Once you have the device handle, finding the depth is just a 
  1227. matter of looking at the gdPMap pixMapHandle, and dereference it 
  1228. to the pmSize field. Done. 
  1229.  
  1230. 5.8) Q: Why is it a bad idea to draw directly to screen? 
  1231.  
  1232. A: Because of several reasons: 
  1233.  
  1234. - You will be incompatible with future display hardware. 
  1235.  
  1236. - You will be incompatible with some present-day display 
  1237. hardware, such as Radius Pivots and PowerBook color screens. 
  1238.  
  1239. - You have to think about a lot of things; testing it all on 
  1240. your own machine is not possible and the chances of crashing are 
  1241. great. 
  1242.  
  1243. - You will be incompatible with future hardware where devices 
  1244. may live in some unaccessible I/O space. 
  1245.  
  1246. 5.9) Q: But I really need to do it.  I can't make my animation into a 
  1247. QuickTime movie, and CopyBits is too slow, even when syncing to the 
  1248. screen retrace and with my source GWorld aligned properly.
  1249.  
  1250. A: You have to prepare yourself, and ask these questions: 
  1251.  
  1252. - Do I want to support all screens, or just 8-bit devices?
  1253.  
  1254. - Do I have a few weeks of free time to make it work? 
  1255.  
  1256. - Do I want to get nasty mail when I break on some hardware and 
  1257.    have to rev the application - even if I may not be able to get 
  1258.    ahold of the hardware that makes it break? 
  1259.  
  1260. If all you're doing is rendering an image pixel-by-pixel or 
  1261. line-by-line, maybe you can draw directly into an offscreen 
  1262. pixMap/GWorld and then CopyBits the entire GWorld to screen? 
  1263. That will be more compatible, especially if you use the 
  1264. keepLocal flag when creating the GWorld. 
  1265.  
  1266. 5.10) Q: Okay, so how do I get the base address of the screen? 
  1267.  
  1268. A: "The" screen? Which screen? There may be several. The base 
  1269. address may be on an accellerated screen card. There may be more 
  1270. than one screen covering the same desktop area. 
  1271.  
  1272. Due to unfortunate circumstances, there is a bug in 
  1273. GetPixBaseAddr() that causes it to return incorrect results for 
  1274. early versions of System 7. Instead, get the baseAddr directly 
  1275. from the gdPMap handle of the GDHandle for the screen you draw 
  1276. to. This address may need switching to 32bit mode to be valid. 
  1277.  
  1278. 5.11) Q: Quit stalling and give me code! 
  1279.  
  1280. A: Okay, but I'll let you sweat over Inside Mac to figure out 
  1281. what it does. All of it is important; believe me! To make this 
  1282. code run faster, a lot of the things it does can be done once 
  1283. before starting to draw. 
  1284.  
  1285. Make sure that you have a window that covers the area where you 
  1286. are drawing, so other windows will not be overdrawn. Also make 
  1287. sure that you do not do direct-to-screen-drawing while you are 
  1288. in the background. 
  1289.  
  1290. *code*
  1291. /* This is presently untested code */
  1292. /* Value is dependent on what depth the screen has */
  1293. /* This code doesn't work on non-color-quickdraw Macs (i e the MacClassic) */
  1294. /* "where" is in GLOBAL coordinates */
  1295. void
  1296. SetPixel ( Point where , unsigned long value ) {
  1297.  
  1298. Rect r ;
  1299. GDHandle theGD ;
  1300. char * ptr ;
  1301. long rowBytes ;
  1302. short bitsPerPixel ;
  1303. PixMapHandle pmh ;
  1304. Boolean oldMode ;
  1305.  
  1306.     r . left = where . h ;
  1307.     r . top = where . v ;
  1308.     r . right = r . left + 1 ;
  1309.     r . bottom = r . top + 1 ;
  1310.     theGD = GetMaxDevice ( & r ) ;
  1311.     if ( theGD ) {
  1312.         where . v -= ( * theGD ) -> gdRect . left ;
  1313.         where . h -= ( * theGD ) -> gdRect . top ;
  1314.         pmh = ( * theGD ) -> gdPMap ;
  1315.         rowBytes = ( ( * pmh ) -> rowBytes ) & 0x3fff ;
  1316.         ptr = ( char * ) ( * pmh ) -> baseAddr ;
  1317.         bitsPerPixel = ( * pmh ) -> pixelSize ;
  1318.         oldMode = true32b ;
  1319.         ptr += where . v * rowBytes ;
  1320.         SwapMMUMode ( & oldMode ) ;
  1321.         switch ( bitsPerPixel ) {
  1322.         case 1 :
  1323.             if ( value & 1 ) { 
  1324.                 ptr [ where . h >> 3 ] |= ( 128 >> ( where . h & 7 ) ) ;
  1325.             } else {
  1326.                 ptr [ where . h >> 3 ] &= ~( 128 >> ( where . h & 7 ) ) ;
  1327.             }
  1328.             break ;
  1329.         case 2 :
  1330.             ptr [ where . h >> 2 ] &= ( 192 >> 2 * ( where . h & 3 ) ) ;
  1331.             ptr [ where . h >> 2 ] |= ( value & 3 ) << 2 * ( 3 - ( where . h & 3 ) ) ;
  1332.             break ;
  1333.         case 4 :
  1334.             ptr [ where . h >> 1 ] &= ( where . h & 1 ) ? 0xf : 0xf0 ;
  1335.             ptr [ where . h >> 1 ] |= ( value & 15 ) << 4 * ( 1 - ( where . h & 1 ) ) ;
  1336.             break ;
  1337.         case 8 :
  1338.             ptr [ where . h ] = value ;
  1339.             break ;
  1340.         case 16 :
  1341.             ( ( unsigned short * ) ptr ) [ where . h ] = value ;
  1342.             break ;
  1343.         case 32 :
  1344.             ( ( unsigned long * ) ptr ) [ where . h ] = value ;
  1345.             break ;
  1346.         default :
  1347.             abort ( ) ; /* Should never get here */
  1348.         }
  1349.         SwapMMUMode ( & oldMode ) ;
  1350.     }
  1351. }
  1352. *end*
  1353.  
  1354. *6* Text
  1355.  
  1356. 6.1) Q: How do I get TextEdit to display more than 32k of text?
  1357.  
  1358. A: You don't.  Truly, it's not worth it.  There's a call-for-call 
  1359. TextEdit replacement called TE32k which does > 32k text, and is 
  1360. available from any recent Info-Mac mirror.  It doesn't do styled text, 
  1361. though.
  1362.  
  1363. 6.2) Q: How do I get TextEdit to display more than 32k of __styled__ 
  1364. text *and* embedded objects in the text (such as pictures)?
  1365.  
  1366. WASTE, available at ftp://ghost.dsi.unimi.it/pub2/papers/piovanel, is a 
  1367. vast improvement over TextEdit.  Version 1.0 does >32k styled text 
  1368. retains compatibility with the TextEdit style scrap (which is used to 
  1369. store styled text in files such as SimpleText's, as well as in the 
  1370. clipboard), includes source code and is freeware.  Really worth the 
  1371. download.  Version 1.1 adds embedded objects within the text, such as 
  1372. pictures, intelligent cut-and-paste, built-in Drag Manager support, 
  1373. built-in Undo support.  1.1 is currently in alpha, but seems to be very 
  1374. stable.
  1375.  
  1376. "But," you nervously stutter, "WASTE is in Pascal!  And it's munged so 
  1377. that it won't work as an imported library w/Metrowerks C!  What now?"
  1378.  
  1379. Dan Crevier is maintaining a C port of WASTE, and the current version 
  1380. along with other WASTE goodies is at <ftp://rhino.harvard.edu/pub/dan/>.
  1381.  
  1382. 6.3) Q: I'm too back-asswards to use WASTE 1.1.  How do I include 
  1383. pictures in text using TextEdit?
  1384.  
  1385. A: There's no really easy way (such as a TEAddPict() call), and it will 
  1386. be a nasty kludge if you do get it working, but if you can live with 
  1387. that, here's how to do it.  I do recommend that you take a look at Q 
  1388. 6.2, above.
  1389.  
  1390. Write an algorithm to get the position of a special marker character 
  1391. [Teach/SimpleText uses option-space] or text attribute that the user 
  1392. will insert wherever he wants a picture.  Pass this position to a 
  1393. function similar to the one below.
  1394.  
  1395. *code* 
  1396. /*
  1397.     TEDrawPicture
  1398.     Draw a picture within TextEdit's text.
  1399.     
  1400.     input:
  1401.         pos - the position of the special character in the text where the user 
  1402.                 wants a picture.
  1403.         r   - size of picture
  1404.     
  1405.     output:
  1406.         r   - frame in which picture was drawn
  1407. */
  1408.  
  1409. void TEDrawPicture(short pos,PicHandle pic,Rect &r,TEHandle theTE)
  1410. {
  1411.     Point   bottomLeft;  //I think TT/ST uses topleft
  1412.     
  1413.     bottomLeft = TEGetPoint(pos, theTE);
  1414.     
  1415.     r.right = bottomLeft.h + (r.right - r.left);
  1416.     r.top = bottomLeft.v - (r.bottom - r.top);
  1417.     r.left = bottomLeft.h;
  1418.     r.bottom = bottomLeft.v;
  1419.     
  1420.     DrawPicture(pic, &r);
  1421. }
  1422.  
  1423. *end*
  1424.  
  1425. I'll leave selection and hiliting as an exercise for the reader (don't 
  1426. ya love it when people say that?).
  1427.  
  1428. Thereotically, it should be possible to kludge up TextEdit to the point 
  1429. where it would treat pictures as if they were actually letters (rather big 
  1430. letters, but letters just the same).  That's what the width and word break 
  1431. hooks are for, after all.  However, this would be a lot of Work, and I've 
  1432. never seen it done.  One is lead to believe that it's less work to create 
  1433. an improved version of TextEdit from the ground up with picture support.  
  1434. WASTE 1.1, in fact, does this rather nicely.
  1435.  
  1436. 6.4) Q: I have all this money, and I want to get rid of it. How do I
  1437. edit more than 32k of styled text now?
  1438.  
  1439. A: There are at least three solutions.
  1440.  
  1441. 1) The Galaxy application framework comes with a styled text editting
  1442. engine which does more than 32k of text. It also happens to support
  1443. cross-platform application development. Pricing starts at $10000.
  1444. <galaxy@visix.com>
  1445.  
  1446. 2) DataPak is selling a cross-platform library called "PAIGE".  It is 
  1447. written to be customizable to any extent, and you can do _anything_ in 
  1448. it (want quicktime movies that play and flow with the text while 
  1449. editting? Three pages of code; you can adapt their picture sample code.) 
  1450. Available for Mac, Windows & Never Trusted and Power Mac.  Pricing at 
  1451. $5000 ($25000 for source code) - it might be cheaper if you talk to 
  1452. them.  Customer support is reputed to be "lousy".
  1453.  
  1454. 3) Or you could use WASTE 1.1 with it's embedded objects implementation 
  1455. (want quicktime movies that play and flow with the text while editing? 
  1456. One page of code, if that much.)
  1457.  
  1458. 6.5) Q: How do I convert from a string to a floating point type?
  1459.  
  1460. A: Once you've got an Str255, you want to call the routine 
  1461. StringToExtended to change the number into type extended80, which is the 
  1462. 80-bit floating point type.  The nice thing about StringToExtended is 
  1463. that it even works in funky foreign language scripts like Chinese.  To 
  1464. use the routine, you must pass it not only the Str255 that you want 
  1465. converted, but also the results of the StringToFormatRec routine and the 
  1466. GetIntlResourceTable routine.  The documentation is generally difficult, 
  1467. so I'll describe the parameters and give you a code example below.  
  1468. Here's what you pass to StringToExtended:
  1469.  
  1470. - source:
  1471. Obviously the string representation of the number.
  1472.  
  1473. - myFormatRec:
  1474. This is simply the format that you expect the number to be.  For 
  1475. example, if you wanted a positive integer with up to 3 decimal places, 
  1476. you would want the format to be "###".  (The "#" symbol means a non-zero 
  1477. filled digit, whereas a "0" would mean zero filled.) If you wanted an 
  1478. floating point exponential notation with up to 2 digit integer portion, 
  1479. exactly 2 digit decimal portion, and exactly 1 digit after the "E", your 
  1480. format would be "##.00E+0".  (Actually, in Canada, you folks might use a 
  1481. "," instead of a "." for the decimal point; if you do, then you would 
  1482. put the locally correct symbol in there.) In a format string, you can 
  1483. put the format for a positive value, a negative value, and 0 seperated 
  1484. by semicolons (e.g.  "##.00E+0;-##.00E+0;0.00E+0").
  1485.  
  1486. But myFormatRec is not the format string itself, but is an internal 
  1487. format that you get from calling StringToFormatRec.  This is so you can 
  1488. store the format returned to you by StringToFormatRec and use it on 
  1489. different international systems.  There is a nice editor for ResEdit for 
  1490. 'FMAT' resources that lets you type the format string and will call 
  1491. StringToFormatRec for you and create an 'FMAT' resource out of the 
  1492. result.  Then you can load the 'FMAT' at run time and pass it to 
  1493. myFormatRec.
  1494.  
  1495. - partsTable:
  1496. The number parts table from the 'itl4' resource.  If you are using 
  1497. System 7 or later, you call GetIntlResourceTable, asking it for the 
  1498. number parts table.  You don't need to worry about HLock'ing itlHandle 
  1499. because StringToExtended doesn't move memory; just don't do anything 
  1500. between your call to GetIntlResourceTable and the StringToFormatRec 
  1501. routine.  If you are using System 6, you need to perform the 
  1502. GetIntlResourceTable by hand.  It's a few lines of code; left as an 
  1503. exercise to the reader.
  1504.  
  1505. - x:
  1506. The extended number to put the thing into.
  1507.  
  1508. So, let's say you expect the user to enter a string containing up to 5 
  1509. digits in the integer with a thousand marker between the 3rd and fourth 
  1510. digit, exactly two digits after the decimal, put it in parenthesis if 
  1511. it's negative, and have it just be "0.00" if it's 0.  (I will use "." 
  1512. for the decimal and "," for the thousand marker.  Here's the C code 
  1513. (passing in the source string):
  1514.  
  1515. *code*
  1516. Handle itlHandle;               /* The 'itl4' resource handle */
  1517. long offset, length;            /* Offset-length of parts table */
  1518. NumFormatStringRec myFormatRec; /* Canonical format record */
  1519. extended80 x;                   /* The number to put it into */
  1520.  
  1521. GetIntlResourceTable (smCurrentScript, smNumberPartsTable,
  1522.                       &itlHandle, &offset, &length);
  1523. StringToFormatRec ("\p##,###.00;(##,###.00);0.00",
  1524.                    (NumberParts *)(*itlHandle + offset),
  1525.                    &myFormatRec);
  1526. StringToExtended (source, &myFormatRec,
  1527.                   (NumberParts *)(*itlHandle + offset),
  1528.                   &x);
  1529. *end*
  1530.  
  1531. If you saved a format record in a resource using the 'FMAT' editor 
  1532. (which is really the preferred way to do it), you would change the code 
  1533. to look like this:
  1534.  
  1535. *code*
  1536. Handle itlHandle;                 /* The 'itl4' resource handle */
  1537. long offset, length;              /* Offset-length of parts table */
  1538. NumFormatStringRec **myFormatRec; /* Canonical format resource */
  1539. extended80 x;                     /* The number to put it into */
  1540.  
  1541. myFormatRec = (NumFormatStringRec **)GetResource('FMAT', MY_FMAT);
  1542. GetIntlResourceTable (smCurrentScript, smNumberPartsTable,
  1543.                       &itlHandle, &offset, &length);
  1544. StringToExtended (source, *myFormatRec,
  1545.                   (NumberParts *)(*itlHandle + offset),
  1546.                   &x);
  1547. *end*
  1548.  
  1549. The StringToExtended routines will return a result that will tell you if 
  1550. there are any parsing errors.  Error checking is also left as an 
  1551. exercise to the reader.
  1552.  
  1553. Of course, the reverse of the process (using ExtendedToString) works 
  1554. very much the same way, passing the same sorts of parameters.
  1555.  
  1556. *7* Communications and Networking
  1557.  
  1558. 7.1) Q: How do I get at the serial ports? 
  1559.  
  1560. A: You call OpenDriver for the names "\p.AOut" and "\p.AIn" to get at 
  1561. the modem port, and "\p.BOut" and "\p.BIn" for the printer port.  The 
  1562. function RAMSDOpen was designed for the original Mac with 128 kB of 
  1563. memory and 64 kB of ROM, and has been extinct for several years.
  1564.  
  1565. However, many users use their serial ports for MIDI, LocalTalk, graphic 
  1566. tablets, or what have you and have installed an additional serial port 
  1567. card to get more ports.  What you SHOULD do as a good application is to 
  1568. use the Comm Toolbox Resource Manager to search for serial resources; 
  1569. this requires that the Comms Toolbox is present (true on earlier System 
  1570. 6 with an INIT, on later System 6 and System 7 always, as well as on 
  1571. A/UX) and that you have initialized the comms resource manager.  The 
  1572. exact code follows (adapted from Inside Mac Comms Toolbox):
  1573.  
  1574. *code*
  1575. #include "CommResources.h"
  1576. OSErr
  1577. FindPorts ( Handle * portOutNames , Handle * portInNames , Handle * names , Handle * iconHandles ) {
  1578.  
  1579. OSErr ret = noErr ;
  1580. short old = 0 ;
  1581. CRMRec theCRMRec , * found ;
  1582. CRMSerialRecord * serial ;
  1583.  
  1584.     * portOutNames = NewHandle ( 0L ) ;
  1585.     * portInNames = NewHandle ( 0L ) ;
  1586.     * names = NewHandle ( 0L ) ;
  1587.     * iconHandles = NewHandle ( 0L ) ;
  1588.     while ( ! ret ) {
  1589.         theCRMRec . crmDeviceType = crmSerialDevice ;
  1590.         theCRMRec . crmDeviceID = old ;
  1591.         found = ( CRMRec * ) CRMSearch ( ( QElementPtr ) & theCRMRec ) ;
  1592.         if ( found ) {
  1593.             serial = ( CRMSerialRecord * ) found -> crmAttributes ;
  1594.             old = found -> crmDeviceID ;
  1595.             PtrAndHand ( & serial -> outputDriverName , * portOutNames ,
  1596.                 sizeof ( serial -> outputDriverName ) ) ;
  1597.             PtrAndHand ( & serial -> inputDriverName , * portInNames ,
  1598.                 sizeof ( serial -> inputDriverName ) ) ;
  1599.             PtrAndHand ( & serial -> name , * names , 
  1600.                 sizeof ( serial -> name ) ) ;
  1601.             PtrAndHand ( & serial -> deviceIcon , * iconHandles ,
  1602.                 sizeof ( serial -> deviceIcon ) ) ;
  1603.         } else {
  1604.             break ;
  1605.         }
  1606.     }
  1607.     return err ;
  1608. }
  1609. *end*
  1610.  
  1611. This will create four handles with the driver names, device names and 
  1612. driver icon handles for all of the available serial devices.  Then let 
  1613. the user choose with a pop-up menu or scrolling list, and save the 
  1614. choice in your settings file.
  1615.  
  1616. You can use OpenDriver, SetReset, SetHShake, SetSetBuf, SerGetBuf and 
  1617. the other Serial Manager functions on these drivers.  To write to the 
  1618. serial port, use FSWrite for synchronous writes that wait until all is 
  1619. written, or PBWrite asynchronously for queuing up data that is supposed 
  1620. to go out but you don't want to wait for it.  At least once each time 
  1621. through your event loop, you should call SerGetBuf on the in driver 
  1622. reference number you got from OpenDriver, and call FSRead for that many 
  1623. bytes - neither more nor less.
  1624.  
  1625. If you are REALLY interested in doing the right thing, you will use the 
  1626. Communications Toolbox Connection Manager instead; this will give you 
  1627. access to modems, direct lines, and networks of various kinds using the 
  1628. same API! Great for stuff like BBSes that may be on a network as well 
  1629. etc.  The Comms Toolbox also provides modularized terminal emulation 
  1630. and file transfer tools, although the Apple-suplied VT102 tool is pretty 
  1631. lame, as is the VT102 mode of the VT320 tool.  And it seems as though 
  1632. it was designed by people who'd never used a Mac before... more in the 
  1633. Unix style, right down to the lack of documentation and "magicCookie."
  1634.  
  1635. 7.2) Q: Where is a Berkley sockets library for the Mac? 
  1636.  
  1637. A: There are some problems with that.  MacTCP, the Mac Toolbox 
  1638. implementation of TCP/IP, doesn't have an API that looks at all like 
  1639. Berkley sockets.  For instance, there is ONE paramater-block call to do 
  1640. a combined listen()/accept()/bind() - sort of.  I have heard that there 
  1641. may be a socket library available by ftp from MIT but haven't seen it 
  1642. myself.
  1643.  
  1644. There is also a pretty good C++ TCP implementation called GUSI which is 
  1645. easily handled, and it also is callable from C using the Berkley socket 
  1646. API.  Apart from TCP, it also handles "standard" Mac network protocols 
  1647. such as ADSP.  The big disadvantage is that it is currently only 
  1648. implemented for MPW.  The ftp site is nic.switch.ch, 
  1649. software/mac/src/mpw_c.
  1650.  
  1651. I can also recommend the Communications Toolbox; for the price of using 
  1652. an API that is a bigger pain in the ass than configuring Windoze to use 
  1653. a new peripheral device, you get the benefit of being able to use any 
  1654. kind of connection (TCP tools are available).
  1655.  
  1656. Instead of the Comm Toolbox, one might use Apple's much improved Open 
  1657. Transport architecture.  Preliminary docs are available for ftp at 
  1658. seeding.apple.com.  Apple's new networking products team seems to have 
  1659. it's head screwed on tight.  A refreshing change.
  1660.  
  1661. Novell and Wollogong offer commercial socket-like libraries. 
  1662.  
  1663. 7.3) Q: Where do I find MacTCP? 
  1664.  
  1665. A: You can buy the MacTCP developers kit from APDA.  It is also 
  1666. available on E T O, and if you want saner headers than those, try ftp to 
  1667. seeding.apple.com.  MacTCP (the control panel) is included with System 
  1668. 7.5 and above.
  1669.  
  1670. 7.4) Q: I'm trying to write to the serial port.  It works fine on the 
  1671. following Machines, Quadra700, IIFX, Powerbook 170, Quadra 840av, but 
  1672. freezes on the Duo 230 and 280c.
  1673.  
  1674. A: You need to call SerHShake.  Otherwise you get screwed on some 
  1675. machines without a hardware handshaking cable because the port default to 
  1676. hardware handshaking.
  1677.  
  1678. *8* IAC
  1679.  
  1680. 8.1) Q: What are AppleEvents? 
  1681.  
  1682. A: AppleEvents are a level-5 network protocol.  If you are not familiar 
  1683. with the ISO network stack, this means it's a way of structuring sessions 
  1684. between network entities (programs) that is not dependent on the 
  1685. underlying protocol (such as PPC or TCP/IP) Despite being a network 
  1686. protocol, they can be very useful on Macs that are not on a network.  In 
  1687. short, they provide applications with a comprehensive way to send 
  1688. arbitrary structured data to other applications (or themselves) which 
  1689. receive the events through their main event loop.
  1690.  
  1691. The AppleEvent Object Model is a way of looking at applications and the 
  1692. data they contain, and also a level-6 network protocol.  You _can_ send 
  1693. AppleEvent Object Model data through AppleEvents (and the standard 
  1694. AppleEvents defined in the AppleEvent Registry use it) but you don't have 
  1695. to - unless you want to talk with other applications, of course, then the 
  1696. AEOM is a lingua franca.
  1697.  
  1698. 8.2) Q: What are the four required AppleEvents? 
  1699.  
  1700. A: There are four events your application really must implement if you 
  1701. want to sell it: the kCoreEventClass class, kAEOpenApplication, 
  1702. kAEQuitApplication, kAEOpenDocuments and kAEPrintDocuments events IDs.  
  1703. When you support these events (or any AppleEvents) you will not get 
  1704. startup info through GetAppParams() anymore, unless you run under System 
  1705. 6 of course.  The kAEOpenApplication event will be sent to you when the 
  1706. user double-clicks your app and it's not started yet.  When receiving it, 
  1707. you can put up a new untitled window.
  1708.  
  1709. kAEOpenDocuments is sent when the user double-clicks your apps documents.  
  1710. Note that if the first AppleEvent you receive is a kAEOpenDocuments 
  1711. event, the user started your app by double-clicking its documents.
  1712.  
  1713. kAEPrintDocuments is sent when the user selects your documents and 
  1714. chooses "Print" in the Finder menu.  If this is the first AppleEvent you 
  1715. receive, you should print the documents and then quit the application 
  1716. again; if you received a kAEOpenApplication or kAEOpenDocuments event 
  1717. before this, you should just print the documents and close them when 
  1718. you're done.
  1719.  
  1720. kAEQuitApplication is sent to you when the user chooses "Shutdown" or 
  1721. "Restart" from the Apple Menu.  You should ask the user whether he wants 
  1722. to save any unsaved changed documents, and then quit unless the user 
  1723. presses Cancel.
  1724.  
  1725. Interestingly enough, you can use these four AppleEvents to send even to 
  1726. non-AE-aware applications, and the system will translate these events 
  1727. into fake menu selections for you.
  1728.  
  1729. A good way of shutting down the Finder is to send it a Quit AppleEvent.  
  1730. You should send a Quit AppleEvent to File Sharing Extension before you 
  1731. shut down the Finder, though; the FSE is found by looking for a process 
  1732. with the creator 'hhgg'.
  1733.  
  1734. 8.3) Q: Are there any limits or tradeoffs with AppleEvents? 
  1735.  
  1736. A: As always, more power means more responsibility. 
  1737.  
  1738. AppleEvents sent to applications on other Macs require authentification 
  1739. the first time they are sent.  If the remote Mac allows Guests to link to 
  1740. programs, the INIT AutoGuest 2.0 might help (or the code solution that 
  1741. comes with it and you can build into your application)
  1742.  
  1743. In the first version of the AppleEvent manager, there was a total 64K 
  1744. limit on the size of data and overhead.  This limit has been lifted with 
  1745. the version of the AppleEvent manager that comes with AppleScript.
  1746.  
  1747. AppleEvents require a lot of memory copying and handle resizing in their 
  1748. construction; this means that large AppleEvents may be slow in 
  1749. construction, especially when compared to a pure PPC Toolbox or 
  1750. ADSP/ASDSP link.  The way around this is to use Jens Alfke's AEGizmos, 
  1751. available on the Developer CD, to create AppleEvents.  AEGizmos are 
  1752. being integrated into System Software, so You Will be doing the right 
  1753. thing.
  1754.  
  1755. You should use your own application signature as event class for 
  1756. AppleEvents you make up, in order not to collide with anybody else.  
  1757. Other than that, you are free to make your own events for your own 
  1758. needs, though supporting the required events and at least a subset of 
  1759. the Core event suite will buy you a lot of functinality from within 
  1760. AppleScript.  Especially important are the Get Current Selection and Set 
  1761. Current Selection events (which are really Get/Set Data on the contents 
  1762. of the current selection of the application)
  1763.  
  1764. The signature for your application SHOULD be registered with DTS to 
  1765. avoid conflicts; this is done through e-mail to 
  1766. DEVSUPPORT@AppleLink.Apple.Com and the form you use is located on the 
  1767. developer CDs and found on ftp.apple.com.
  1768.  
  1769. 8.4) Q: How do AppleEvents interface with AppleScript? 
  1770.  
  1771. A: AppleEvents are the meat and potatoes of AppleScript.  If you support 
  1772. the AppleEvent Object Model from within your application, users can 
  1773. control you through AppleScript.
  1774.  
  1775. The first thing you should do is get ahold of Inside Mac: 
  1776. Interapplication Communication, and a copy of the AppleEvents Registry.  
  1777. The former tells you all you ever need to know about AppleEvents, while 
  1778. the latter is paramount for implementing the right standard events.  If 
  1779. everybody use the standard events, dynamic data interchange between any 
  1780. applications will become sweet reality!
  1781.  
  1782. Then there is the 'aete' resource which lets you put names on the events 
  1783. you support, so that users can "Open Terminology" on your application 
  1784. from within the Apple Script Editor and use the proper AppleScript 
  1785. commands in their scripts.  The format of an aete resource is defined in 
  1786. Inside Macintosh: Interapplication Communication.
  1787.  
  1788. 8.5) Q: Can I compile and run scripts from within my application?
  1789.  
  1790. A: Yes, this is very simple.  There are toolbox calls for reading 
  1791. scripts, compiling scripts, and executing scripts.  (OSACompile, 
  1792. OSAExecute) These are all documented in Inside Mac: Interapplication 
  1793. Communication.
  1794.  
  1795. 8.6) Q: Is this a good way of getting a macro language almost 
  1796. for free? 
  1797.  
  1798. A: "Good" is an understatement.  Just let users write scripts, load them 
  1799. into menu items and go.  Total systems integration in under a week, 
  1800. including adding support for the AEOM to your application.
  1801.  
  1802. There is source code for an application called "MenuScripter" on the 
  1803. developer CD which shows you how to do an application with all of the 
  1804. menus being AppleScript scripts.  
  1805.  
  1806. 8.7) Q: Why do I get error -903 (noPortErr) when I try to send an Apple 
  1807. Event?
  1808.  
  1809. A: Make sure the isHighLevelEventAware flag in your application's SIZE 
  1810. resource is set.  The AE Manager won't allow you to send events if you're 
  1811. not able to receive them.
  1812.  
  1813. *9* Standalone Code & Trap Patching
  1814.  
  1815. 9.1) Q: How do I do code resources/extensions/external functions, much 
  1816. like HyperCard XCMDs only for my 68k app/extension?
  1817.  
  1818. A: Here is what you need to do:
  1819.  
  1820. Define a storage location for the plug-ins.  such as, for an application 
  1821. called Foo, a folder called "Foo Pouch/ PlugIns/ Modules/ Tools/ Etc", 
  1822. which your installer creates, and which you search for in the same 
  1823. folder as your application, the preferences folder, and the System 
  1824. Folder (for System 6 (no preferences)) the name of the pouch folder 
  1825. should be in a 'STR ' or 'STR#' resource for easy internationalization.
  1826.  
  1827. Next, define a resource type to hold your plug-in exectuable code.  You 
  1828. should allow any resID number, since some development systems have 
  1829. restrictions on which id numbers can be used for multi-segment code 
  1830. resources.
  1831.  
  1832. Decide whether you will allow multiple code resources per file.  For 
  1833. example, Photoshop lets you bundle an import resource, an export 
  1834. resource, and a filter into the same file, each is its own resource 
  1835. type.  It uses the resource name to tell the user, so each has its own 
  1836. name.
  1837.  
  1838. Decide on the user interface to plug-ins.  Commonly, at program 
  1839. start-up, you scan the pouch and collect all the files of the correct 
  1840. types.  Then you scan for appropriate code resources, then put the names 
  1841. of the resources in the menus.  You can also use the file names, or run 
  1842. the code resources and ask them for the correct name.
  1843.  
  1844. Decide on the calling convention.  MPW likes to pass parameters as 
  1845. longs, and pascal development systems usually can't generate C 
  1846. interfaces, so your best bet is to have the entry point of your code 
  1847. resource be something like:
  1848.  
  1849. extern pascal long PlugIn(long selector, CallbackPtr callback,
  1850.         long param1, long param2, long param3, long refcon);
  1851.  
  1852. where params 1 through 3 will have specific meanings depending upon
  1853. which function selector is asking to be performed.
  1854.  
  1855. CallbackPtr is something like this:
  1856. typedef struct CallbackRec{
  1857.         DrawFunc        erase;
  1858.         DrawFunc        paint;
  1859. }CallbackRec, *CallbackPtr;
  1860.  
  1861. where DrawFunc is something like:
  1862. typedef pascal void (*DrawFunc)(Rect *);
  1863.  
  1864. This is the place where you define services that your application will 
  1865. perform when asked to by the code resource of the plug-in.
  1866.  
  1867. You'll need to define a bunch of selectors for what you want the plug-in
  1868. to do.
  1869.  
  1870. You should definately give the plug-in an Init selector, and a dispose 
  1871. selector, and have Init return an error or a refcon, which will be 
  1872. passed back to the plug-in on each call, so the plug-in can have some 
  1873. persistent storage.  You should definately set the CurResFile to the 
  1874. plug-in's file while it is running, so it can easily get at any 
  1875. resources it needs.
  1876.  
  1877. The Init selector should pass the interface version in param1 and the 
  1878. program version in param2, so that the plug-in can cope with different 
  1879. versions of your program.
  1880.  
  1881. The actual call to the plug-in looks like:
  1882.  
  1883. *code*
  1884.     h = GetResource('PLUG', 1);
  1885.     HLock(h);
  1886.     xh = StripAddress(h);
  1887.     ((PlugInFunc) *xh)(kInit, callbacks, kInterfaceVer1, kProgVer1, 0, 0);
  1888.     HUnlock(h);
  1889. *end*
  1890.  
  1891. The StripAddress is important; if your app is running in 24bit mode, the 
  1892. resource handle may contain tag bits and you don't want strange things 
  1893. to happen if the code resource switches into 32bit mode (which QuickDraw 
  1894. may do, incidentally)
  1895.  
  1896. Exactly how you structure your calling conventions is up to you; there 
  1897. is no accepted standard (except for HyperCard XCMDs, but that is 
  1898. probably overkill for you).
  1899.  
  1900. 9.2) Q: How do I fat-patch a trap (that is, how do I patch a trap with 
  1901. both 68k and PPC code)?
  1902.  
  1903. A: Create a normal fat routine descriptor and pass it to 
  1904. Set{Tool, OS}TrapAddress().
  1905.  
  1906. *10* Compatibility
  1907.  
  1908. 10.1) Q: I see all these people call Gestalt without first 
  1909. checking whether it's implemented. Isn't that bad? 
  1910.  
  1911. A: No; Gestalt and a few other traps (the HXxx file manager 
  1912. traps, and FindFolder) are implemented using glue so they do the 
  1913. right thing even if the trap is not implemented. 
  1914.  
  1915. If you want to get rid of the glue, you can #define 
  1916. SystemSevenOrLater (and, using Think C/C++, re-pre-compile 
  1917. MacHeaders) However, then you will be responsible for checking 
  1918. for these features before you use them. 
  1919.  
  1920. 10.2) Q: What more functions are implemented in glue? 
  1921.  
  1922. A: Wake Up and Smell the Glue!
  1923.  
  1924. How often have you wished you could use that cool new ToolBox 
  1925. call, but didn't want to make your application System 7 
  1926. dependent? Well, it might be that you *could* in fact have used 
  1927. the call. Several traps are implemented in glue, that is, much 
  1928. of their functionality is linked into your application and thus 
  1929. available even if you are running under an old System. 
  1930.  
  1931. This list applies to MPW 3.2 and should also be valid for the 
  1932. current version of Think C. If you find any inaccuracies, please 
  1933. report them to me. (neeri@iis.ee.ethz.ch) 
  1934.  
  1935. FSOpen: Tries first OpenDF, then Open. 
  1936.  
  1937. HOpenResFile: Full functionality emulated if trap not available 
  1938.  
  1939. HCreateResFile: Full functionality emulated if trap not 
  1940. available 
  1941.  
  1942. FindFolder: Under System 6, understands the following values for 
  1943. folderType and returns the System Folder for all of them: 
  1944.  
  1945. kAppleMenuFolderType 
  1946.  
  1947. kControlPanelFolderType 
  1948.  
  1949. kExtensionFolderType 
  1950.  
  1951. kPreferencesFolderType 
  1952.  
  1953. kPrintMonitorDocsFolderType 
  1954.  
  1955. kStartupFolderType 
  1956.  
  1957. kSystemFolderType 
  1958.  
  1959. kTemporaryFolderType 
  1960.  
  1961. SysEnvirons: Full functionality emulated if trap not available 
  1962.  
  1963. NewGestalt: Returns an error if not implemented 
  1964.  
  1965. ReplaceGestalt: Returns an error if not implemented 
  1966.  
  1967. Gestalt: The following selectors are always implemented: 
  1968.  
  1969. vers    mach    sysv    proc    fpu     
  1970.  
  1971. qd      kbd     atlk    ram     lram    
  1972.  
  1973. 10.3) Q: I have to support System 6, don't I? 
  1974.  
  1975. A: It would be foolish to lock yourself out of the many benefits 
  1976. the System 7 API provides for software that you start to write 
  1977. now. Some of the System 6 and older things (likely SFGetFile and 
  1978. wdRefNums among others) will be phased out of the interfaces and 
  1979. lose support; especially on future platforms. 
  1980.  
  1981. The installed base of System 7 is larger than that of System 6; 
  1982. this is not surprising because Apple has been shipping System 7 
  1983. for several years with all new machines, including the LCII, 
  1984. Classic II, Performas and Color Classic. Another argument is 
  1985. that newer computer owners (having System 7) are much more 
  1986. likely to buy new software than old computer owners who have 
  1987. systems that already do what they want them to. 
  1988.  
  1989. The added work to support both System 6 and System 7 is 
  1990. significant; if you have the time and money you may want to do 
  1991. it, but only supporting System 6 and not System 7 is doomed to 
  1992. fail in the market of today. 
  1993.  
  1994. Some may call this position subjective; I call it business sense 
  1995. based on market demographics. A rule of thumb may be that if you 
  1996. target color machines only, you can just as well demand System 7 
  1997. as well. 
  1998.  
  1999. 10.4) Q: Why does my application work on an SE with accellerator 
  2000. (or a Mac II or Quadra), but not on one without? 
  2001.  
  2002. A: Assuming you're not calling Color QuickDraw (which is not 
  2003. available on accellerated SEs), you most probably have an 
  2004. odd-aligned word access somewhere. 
  2005.  
  2006. The 68000 does not allow words or longwords to be read from odd 
  2007. addresses, while the 68020 and up relaxes this restriction (it 
  2008. still is slower than aligned-word access though) 
  2009.  
  2010. This may or may not crash depending on your compiler: 
  2011.  
  2012. *code*
  2013. struct foo {
  2014.     char c1 ;
  2015.     char c2 ;
  2016.     char c3 ;
  2017.     char c4 ;
  2018.     char c5 ;
  2019. } bar ;
  2020.  
  2021.     long * x = ( long * ) & bar . c2 ;
  2022.     * x = 0x12345678 ; /* X is odd if compiler doesn't pad */
  2023.  
  2024. This WILL crash on an SE/Plus/Classic/PB100:
  2025.  
  2026. char foo [ 10 ] ;
  2027.  
  2028.     long * x = ( long * ) & foo [ 1 ] ;
  2029.     * x = 0x12345678 ;
  2030.  
  2031. *end*
  2032.  
  2033. 10.5) Q: Why does my application work on a IIci and a pre-1995 PowerMac 
  2034. but not on a Quadra?
  2035.  
  2036. A: Two reasons: 
  2037.  
  2038. 1) The Quadras 900 and 950 have special processors that handle the serial 
  2039. ports; if you write directly to the serial chips, you will crash (this 
  2040. goes for the IIfx as well)
  2041.  
  2042. 2) The Quadras have 68040 processors, as have the Centrises.  These 
  2043. processors have separate instruction and data caches (like the 68030) but 
  2044. they are larger (4K each) and unlike the 68030 which is write-through 
  2045. data cached, the 68040 is copy-back data cached.  This means that changes 
  2046. you make to "your code" aren't really changed all the time, since the 
  2047. changes may still be in the data cache and not written to memory when the 
  2048. CPU reads that part of memory into its I-cache.  Even worse; that part 
  2049. might already have been read into the I-cache before you change it in the 
  2050. D-cache, meaning that writing out the D-cache will still not be enough.  
  2051. You need to flush both the caches when writing self-modifying code.
  2052.  
  2053. Self-modifying code includes code that builds its own jump tables and 
  2054. code that decrypts itself and code that "stubs" MDEFs or WDEFs to jump 
  2055. back into the application code.
  2056.  
  2057. You flush the cache using FlushDataCache() which is implemented if 
  2058. Gestalt says you have a 68020 or better processor (or if the _HwDispatch 
  2059. trap is implemented)
  2060.  
  2061. The current PowerMac 68k emulator doesn't have the self-modifying cache 
  2062. problem, but a future faster version will.
  2063.  
  2064. 10.6) Q: Why does my application work on my Quadra but not on my 
  2065. accellerated SE?
  2066.  
  2067. A: You're probably calling Color QuickDraw without first checking if it's 
  2068. available.  The following machines do not have color QuickDraw in ROM nor 
  2069. RAM:
  2070.  
  2071. Mac Plus, Mac SE, Mac Classic, Mac Luggable, PowerBook 100, Outbound
  2072.  
  2073. 10.7) Q: I do check for color quickdraw, but crash nevertheless. 
  2074.  
  2075. A: _Gestalt lies under some versions of System 7; it says that non-color 
  2076. machines HAVE color QuickDraw when you test using the 
  2077. gestaltQuickdrawFeatures selector.
  2078.  
  2079. Instead, check the gestaltQuickdrawVersion selector, if it returns >= 
  2080. gestalt8BitQuickdraw then you can safely use gestaltQuickdrawFeatures, 
  2081. else you only have b/w QuickDraw.
  2082.  
  2083. *11* Optional System Software
  2084.  
  2085.   *11.1* QuickTime
  2086.  
  2087. 11.1.1) Q: I want to write a Amiga QuickTime player and need the 
  2088. CODEC format details. 
  2089.  
  2090. A: Although the structure of QuickTime movies is well documented in 
  2091. Inside Mac: QuickTime, the inner workings of the Apple compression 
  2092. modules is a trade secret that Apple will only license to you at great 
  2093. cost.  Perhaps it's time for a freeware, cross-platform QuickTime codec?
  2094.  
  2095. *12* Third-Party Solutions
  2096.  
  2097.   *12.1* Databases
  2098.  
  2099. 12.1.1) Q: What are some royalty-free databases available for the 
  2100. Macintosh?
  2101.  
  2102. A: These are commercial with no run-time licenses:
  2103.  
  2104. - Prograph CPX comes with a database that can do indexes and tables.  
  2105. <sales@prograph.com>
  2106.  
  2107. - CXBase Pro is an engine that comes with source, does tables and indexes 
  2108. in one compound file.  <tse.int@applelink.apple.com>
  2109.  
  2110. - dtF is a relational database library.  <DTF.AMERICA@applelink.apple.com>
  2111.  
  2112. NeoAccess is an object-oriented database, where you can store objects and 
  2113. retrieve by field value; it handles inheritance as well.  Comes as a 
  2114. demo on the CodeWarrior CD with an unlockable archive of the real thing.
  2115.  
  2116. 12.1.2) Q: I need a robust Client/Server database library.  What do I do?
  2117.  
  2118. A: Get the Macintosh Client/Server Database Development Summary, by Liam 
  2119. Breck.  It covers software applicable to Macintosh (and cross-platform) 
  2120. client/server database development in three categories:
  2121.  
  2122.   + Client application development tools
  2123.   + Data access layers
  2124.   + Database servers
  2125.  
  2126. The eight page (20 Kbytes ASCII text) document includes explanations of the
  2127. three categories and describes over 30 products. Its sources are vendors'
  2128. product literature, industry periodicals, and discussions with users and
  2129. vendors' tech support staff. It is purely informational and contains no
  2130. propaganda, as the author is a neutral party.
  2131.  
  2132. To receive a copy electronically, email a request to: 
  2133. <maccsdb@external.umass.edu>.  Tell 'em the Mac Programming FAQ sent 
  2134. you!
  2135.  
  2136.    *12.2* Circumvention of Toolbox Limitations
  2137.  
  2138. 12.2.1) Q: Help!  The List Manager is driving me right UP THE WALL!  I 
  2139. want to throw it out and start over.  What'r my options?
  2140.  
  2141. A: The only non-framework List Manager replacement of which I'm aware is 
  2142. StoneTable, a commerical replacement library for the List Manager which 
  2143. provides many extensions including variable size columns/rows, 
  2144. column/row titles, different style/font/size/color per cell, sorting, 
  2145. edit in cell, and much more. <http://www.teleport.com/~stack>
  2146.  
  2147. TCL, PowerPlant, and MacApp all have their own integrated list 
  2148. management solutions.
  2149.  
  2150. *13* Dessert
  2151.  
  2152. 13.1) Q: Dessert? 
  2153.  
  2154. A: Honey Hill Farms Cookie Jar Frozen Yoghurt or Haagen-Dazs Raspberry & 
  2155. Cream Ice Cream.
  2156.  
  2157. Hokey-Pokey icecream with chocolate sauce and (for those who like their 
  2158. brain food firmer) Almond and Double Chocolate CookieTime cookies!?
  2159.  
  2160. *14* Contributors
  2161.  
  2162. [do let me know if I've forgotten you]
  2163.  
  2164. Denis Birnie
  2165. Liam Breck
  2166. Scott Bronson <urge@mcl.mcl.ucsb.edu>
  2167. Tim Converse <converse@cs.uchicago.edu>
  2168. Bob Hablutzel
  2169. Julian Harris
  2170. Bruce Hoult <Bruce@hoult.actrix.gen.nz>
  2171. Peter Jensen <pjensen@netcom.com>
  2172. Steve Jasik <macnosy@jasik.com>
  2173. Jonathan Kimmitt <jrrk@camcon.co.uk>
  2174. Matthias Neeracher <neeri@iis.ee.ethz.ch>
  2175. David P. Oster <oster@netcom.com>
  2176. Louis M. Pecora
  2177. Ingemar Ragnemalm <ingemar@lysator.liu.se>
  2178. Pete Resnick <resnick@uiuc.edu>
  2179. Phil Shapiro <phils@bedford.symantec.com>
  2180. eric slosser <slosser@apple.com>
  2181. M Sooriarachchi
  2182. Bill <stack@teleport.com>
  2183. Chris Thomas <thunderone@delphi.com>
  2184. Doug W. <dougw@as.arizona.edu>
  2185. Jon Watte <h+@austin.metrowerks.com>
  2186. Rick Zaccone <zaccone@bucknell.edu>
  2187.