Should I use Resource files or files in a Resources folder ?

Monday, July 21 2003 @ 12:47 PM UTC

Contributed by: witness

What are the advantages of Resources over files in a package, and when should I use what?

I like resources, they're a great idea, and when I started out with programming they were great for doing small databases without messing with data offsets and length bytes too much. However, if you look at the documentation of the resource file format in "Inside Macintosh: More Macintosh Toolbox", it quickly becomes obvious that this concept was created when 20 MB of hard disk space were _a lot_. Below is a rough description of the various limits of the resource file format. They should be enough to convince you not to try storing 5000 true-color PICTs in the resource fork.

Although resource data entries use 4-bytes to store their size (about 4 billion is the maximum they can hold), the relative offsets associating a resource IDs with resource names, and resource types with resource IDs are only 2 bytes in size, and even _signed_. This means as soon as the list of names, types or resource IDs grows beyond 32k, you have a problem. Also, this is very hard to calculate in advance, because some resources don't have names, and names are of variable size. If you use very long names, you should be having problems already at somewhere beyond 128 (named) resources.

Otherwise, about 4095 resource types (in total) will cause a minor stroke to the type list. But you'll never reach that limit, since about 2730 resources will already nuke the resource list. However, these numbers don't take into account that another 2-byte offset forces the resource type list, the resource list _and_ the start of the resource name list to lie within 32k of the resource map's start.

Whew, there's so much interrelated stuff and relative offsets to this and that in the resource file format that I'm not surprised that the resource manager just crashes if you exceed one of its limits ...

So, unless you badly need to save disk space (every file takes a full allocation block on your disk, which can be around 8k), you're storing a number of small thingies that are each only around 1k in size, you care trying to attach additional data to an existing file (e.g. a JPEG to which you want to attach a preview), or you need better backwards compatibility with MacOS 9, you will want to use several files in a package's "Resources" folder instead of a resource file.