Special

Introducing the “Welcome to Xojo” Bundle!

New to Xojo and looking for guidance? We've put together a terrific bundle to welcome you! Xojo Bundle

This bundle includes six back issues of the magazine -- all of year 21 in printed book and digital formats -- plus a one-year subscription (beginning with 22.1) so you'll be learning all about Xojo for the next year. It's the perfect way to get started programming with Xojo. And you save as much as $35 over the non-bundle price!

This offer is only available for a limited time as supplies are limited, so hurry today and order this special bundle before the offer goes away!

Article Preview


Buy Now

Issue 2.4

COLUMN

Accessing the File System Directly

Mac OS 9 and X File Declares

Issue: 2.4 (March/April 2004)
Author: Thomas Reed
Author Bio: Thomas Reed has been programming as a hobbyist for more than 20 years, and fell in love with the Mac in 1984.
Article Description: No description available.
Article Length (in bytes): 9,532
Starting Page Number: 32
Article Number: 2414
Resource File(s):

Download Icon 2414.zip Updated: 2013-03-11 19:07:57

Related Web Link(s):

http://www.tildesoft.com/Code.html
http://www.bitjuggler.com/extra/
http://developer.apple.com/technotes/tb/tb_535.html

Excerpt of article text...

REALbasic provides a large set of high-level routines for dealing with the file system. These routines are centered around the FolderItem, and provide nearly everything you need for every supported platform. However, at times you need access to the file system in ways that are not provided for you by REALbasic. At these times, you need to call the underlying file system routines manually. This article will cover some techniques for interacting with files in Mac OS 9 and X. Note that it assumes you are familiar with using declare statements to call OS routines. If you are not, this topic was covered in the Advanced Techniques column of the first issue of REALbasic Developer.

Consider the following example. The FolderItem class provides the GetSaveInfo function, which generates an alias-like string. This string can be used later to recreate the FolderItem, even if the original file has been moved or renamed in the interim. (Note that this is not true on Windows or Linux, where the GetSaveInfo data functions more like a path.) This is extremely useful for a variety of applications, but is not always adequate. Suppose that you want actual alias data, perhaps because you intend to use it to manually create an alias file. The data returned by GetSaveInfo on the Mac OS does contain alias data. However, since it is enclosed in a custom opaque data type that is not documented, it is not available for use. You will need to call file system routines directly to obtain alias data.

The most significant difficulty in calling file system routines is converting a FolderItem into its operating system analog. Before OS X, the only FolderItem equivalent was the FSSpec, which used a volume reference number, directory ID, and name to identify a file. The FolderItem class has MacVRefNum, MacDirID, and Name properties, making it easy to create an FSSpec from a FolderItem. However, since the first two of these properties are read-only, the reverse is difficult.

With the addition of OS X to the mix, things have become even more complex. The FSSpec is not sufficient to represent all files in OS X. It is limited to 31-character file names, while the file system in OS X is capable of much longer names. In addition, the file name in an FSSpec is stored using the system's default encoding, while OS X uses Unicode, which means some characters possible in an OS X file name cannot be represented in an FSSpec. In OS X, the FSRef provides the same function as the FSSpec. It is capable of representing files with names longer than 31 characters or containing unusual characters.

The decision whether to use FSSpecs or FSRefs is easy. If your application will only run in OS 9 and earlier, FSSpecs are necessary. If your application will run in OS X, you should use FSRefs. Do not be tempted to use the FSSpec in an OS X application! Although it will appear to work, your code will fail for some files. In Carbon, for applications that will run on both OS 9 and X, using an FSRef should work, though I should say I have little practical experience using FSRefs in this situation.

Much of the code that I will discuss from this point on is too lengthy to include here. Instead, I will refer you to particular routines in the sample project associated with this article. (Download instructions can be found on page 4.) Routine references will take the form "ModuleName.RoutineName". Note that the code comes from either the well-known Carbon Declare Library (found at http://www.tildesoft.com/Code.html) or my own XFile module (found at http://www.bitjuggler.com/extra/).

...End of Excerpt. Please purchase the magazine to read the full article.