Preference file I/O in Carbon
Hi all, I'm ripping out my old legacy prefs handling code and am frankly baffled as to which particular API elements I should use when writing and reading files these days...
(Keep in mind that this is Carbon, OS X only, and due to the nature of the data I do not want to use the CFPreferences API at this stage.)
The current code uses FindFolder, HCreate, HOpen, SetFPos and so forth. Apart from the fact that these are deprecated, they do not support long file names.
So what functions (apart from CFPreferences) should I be using? Files.h variously tells me to use mixtures of FSCreateFileUnicode and FSOpenFork, but not in any particularly useful way.
Any sample code would be appreciated.
thanks.
(Keep in mind that this is Carbon, OS X only, and due to the nature of the data I do not want to use the CFPreferences API at this stage.)
The current code uses FindFolder, HCreate, HOpen, SetFPos and so forth. Apart from the fact that these are deprecated, they do not support long file names.
So what functions (apart from CFPreferences) should I be using? Files.h variously tells me to use mixtures of FSCreateFileUnicode and FSOpenFork, but not in any particularly useful way.
Any sample code would be appreciated.
thanks.
mars Wrote:(Keep in mind that this is Carbon, OS X only, and due to the nature of the data I do not want to use the CFPreferences API at this stage.)
Why not? This is the best way for reading/writing prefs in Carbon, I believe.
"When you dream, there are no rules..."
CFPreferences is by far the easiest approach, although it has the standard CF issue of not reporting errors if something goes wrong.
If you want to read/write your preferences by hand, i.e., dumping a struct or whatever to disk, then you would use FSFindFolder(kUserDomain, kPreferencesFolderType) to find the preferences folder followed by FSCreateFileUnicode to create your file.
You can then use FSRef-based APIs to read/write your data, which would be FSGetDataForkName, FSOpenFork, FSWriteFork, then FSCloseFork. Alternatively you can use FSRefMakePath to get a POSIX path to your file, then fopen/fwrite/fclose (fopen will fail if the path is too long).
If you do dump a struct out, ensure you tag your file/structure with both a byte-order marker and a version field. This will let you introduce swapping later if your prefs are ever moved to an x86 box after being written on PPC (or vice versa), and allow you to change the structure in the future and detect older preference files.
This really applies to any binary format - when doing it by hand like this, you should ensure that your app can safely ignore unrecognised file formats and can deal with a file which was written on a different platform.
If you can, using CFPreferences or CFPropertyList is a lot easier - they will take care of handling endian swapping and version changes for you.
If you want to read/write your preferences by hand, i.e., dumping a struct or whatever to disk, then you would use FSFindFolder(kUserDomain, kPreferencesFolderType) to find the preferences folder followed by FSCreateFileUnicode to create your file.
You can then use FSRef-based APIs to read/write your data, which would be FSGetDataForkName, FSOpenFork, FSWriteFork, then FSCloseFork. Alternatively you can use FSRefMakePath to get a POSIX path to your file, then fopen/fwrite/fclose (fopen will fail if the path is too long).
If you do dump a struct out, ensure you tag your file/structure with both a byte-order marker and a version field. This will let you introduce swapping later if your prefs are ever moved to an x86 box after being written on PPC (or vice versa), and allow you to change the structure in the future and detect older preference files.
This really applies to any binary format - when doing it by hand like this, you should ensure that your app can safely ignore unrecognised file formats and can deal with a file which was written on a different platform.
If you can, using CFPreferences or CFPropertyList is a lot easier - they will take care of handling endian swapping and version changes for you.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Xcode Preference to open sourcefile in the editor | Bersaelor | 2 | 3,817 |
Oct 18, 2009 04:54 AM Last Post: Bersaelor |
|

