Maturity Index: Experimental
and then later,[myObject storeOn:"/tmp/aFile"];
The readFrom: returns an instance of the class that it finds in the file, not an AsciiFiler instance.myObject = [AsciiFiler readFrom:"/tmp/aFile"];
The methods storeOn: and readFrom: are declared in the Object class and are inherited by all objects.
Then the following demonstrates how the AsciiFiler class automatically converts instances of Record to a flat file text representation :\= Record : Object { id firstname; id familyname; } + new { self = [super new]; firstname = [String str:"Paco"]; familyname = [String str:"Rodriguez"]; return self; } \=:
Inspection of the file aFile would show :int main() { [[Record new] storeOn:"/tmp/aFile"]; }
This file can be used to read back an instance of Record into memory, and all this without the developer having to write a single line of code.#AsciiFiler i144 0 #Record @2 @3 0 #String i4 i5 *4"Paco 0 #String i9 i10 *9"Rodriguez
Each record in the file is newline terminated. The first record indicates that this is a (Stepstone) AsciiFiler file, version 3. Then follows object 1 on line 1, object 2 on line 2 and so on. The first field for each record is the number of indexed variables (this will always be 0 for the Portable Object Compiler). Then follows the isa pointer and the instance variables of each object.#AsciiFiler i3 10 #OrdCltn @2 i10 i3 10 #IdArray i10 @3 @3 @4 @0 @0 @0 @0 @0 @0 @0 12 #String i11 i11 *11"hello world 20 #Set @5 i20 i1 20 #IdArray i20 @0 @0 @0 @0 @0 @0 @0 @0 @0 @0 @0 @0 @0 @3 @0 @0 @0 @0 @0 @0
Here's a short list of the most commonly used notation in AsciiFiler files. Note that objects are stored using a notation that allows for back or forward pointers. For example, the object in record 5 may contain a reference to @3, the object in record 3, or to @10, the object in record 10.
However, the details about what is done automatically and what is left to the programmer differ. The Stepstone compiler attempts to automatically file all C types (ints, chars, unions, structs etc.).
The Portable Object Compiler emits for each class an implementation for the fileOutIdsFor: and fileInIdsFrom: methods. These methods take care of the archiving of id instance variables (objects). Unlike Stepstone, the Portable Object Compiler does not attempt to automatically archive C types; this must be done manually, if the programmer chooses to use instance variables that are raw C types instead of Objective-C objects.
Therefore, if a class adds only id instance variables to its superclass, then storeOn: and readFrom: will immediately work for the class. Otherwise manual archiving needs to be done, as explained in the following paragraphs.
The default implementation of fileOutOn:, as implemented in the Object class, uses the compiler generated fileOutIdsFor: method to store all id instance variables. This might suffice in many applications.
Variables of a different type, or objects that are indirectly owned by the object must be manually written on the Filer, by using the fileOut:type: method.
- fileOutOn:aFiler { [super fileOutOn:aFiler]; [aFiler fileOut:&myInt type:'i']; [aFiler fileOut:&cArray[0] type:'@']; [aFiler fileOut:&cArray[1] type:'@']; return self; }
The default implementation of fileInFrom:, as implemented in the Object class, uses the compiler generated fileInIdsFrom: method to restore all id instance variables. A method fileInFrom: needs to be implemented only to match a corresponding fileOutOn:.
If the type description character, passed as argument to fileIn:type: does not match what was used for fileOut:type:, the Filer will send itself a error: message and abort.- fileInFrom:aFiler { [super fileInFrom:aFiler]; [aFiler fileIn:&myInt type:'i']; [aFiler fileIn:&cArray[0] type:'@']; [aFiler fileIn:&cArray[1] type:'@']; return self; }
+initializeRegisters the AsciiFiler as default filer.
+newFactory method to create and return a new instance of the class. The default implementation clears (zeroes) the memory for instance variables and initializes the isa pointer.
-freeFrees the memory for this instance, and returns nil.
- (BOOL)store:anObjecton:(STR)aFileNameAsciiFiler reimplementation of storeOn:. Doesn't use the default Filer, but rather AsciiFiler itself as Filer class. If the default filer is AsciiFiler, then the following are equivalent :
and[AsciiFiler store:anObject on:aFileName];
[anObject storeOn:aFileName];
-readFrom:(STR)aFileNameAsciiFiler reimplements readFrom: and doesn't use the default Filer, but rather AsciiFiler itself as Filer class.
-fileOut:(void *)valuetype:(char)typeDescWrites out the data referenced by value to the filer.
-fileIn:(void *)valuetype:(char)typeDescReads from the filer and returns data by reference.