diff --git a/ChangeLog b/ChangeLog index ea7ee0486..eb6c62aea 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2002-03-21 Richard Frith-Macdonald + + * Source/NSArray.m: Ensure property-list read/write uses UTF8 + * Source/NSDictionary.m: ditto + 2002-03-20 Richard Frith-Macdonald * Source/GSString.m: lossyCString_u() handle case where cString diff --git a/Headers/gnustep/base/NSArray.h b/Headers/gnustep/base/NSArray.h index 0223c35bd..dd9bd4124 100644 --- a/Headers/gnustep/base/NSArray.h +++ b/Headers/gnustep/base/NSArray.h @@ -89,9 +89,9 @@ - (NSString*) descriptionWithLocale: (NSDictionary*)locale indent: (unsigned int)level; -- (BOOL) writeToFile: (NSString*)path atomically: (BOOL)useAuxilliaryFile; +- (BOOL) writeToFile: (NSString*)path atomically: (BOOL)useAuxiliaryFile; #ifndef STRICT_OPENSTEP -- (BOOL) writeToURL: (NSURL*)url atomically: (BOOL)useAuxilliaryFile; +- (BOOL) writeToURL: (NSURL*)url atomically: (BOOL)useAuxiliaryFile; #endif @end diff --git a/Source/NSArray.m b/Source/NSArray.m index 30d16c01c..527df5b3a 100644 --- a/Source/NSArray.m +++ b/Source/NSArray.m @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -418,12 +419,29 @@ static SEL rlSel; return [self initWithObjects: 0 count: 0]; } +/** + *

Initialises the array with the contents of the specified file, + * which must contain an array in property-list format. + *

+ *

In GNUstep, the property-list format may be either the OpenStep + * format (ASCII data), or the MacOS-X format (URF8 XML data) ... this + * method will recognise which it is. + *

+ *

If there is a failure to load the file for any reason, the receiver + * will be released and the method will return nil. + *

+ */ - (id) initWithContentsOfFile: (NSString*)file { NSString *myString; + NSData *someData; - myString = [[NSString allocWithZone: NSDefaultMallocZone()] + someData = [[NSData allocWithZone: NSDefaultMallocZone()] initWithContentsOfFile: file]; + myString = [[NSString allocWithZone: NSDefaultMallocZone()] + initWithData: someData encoding: NSUTF8StringEncoding]; + RELEASE(someData); + if (myString) { id result; @@ -831,7 +849,30 @@ static NSString *indentStrings[] = { } } -- (BOOL) writeToFile: (NSString *)path atomically: (BOOL)useAuxilliaryFile +/** + *

Writes the contents of the array to the file specified by path. + * The file contents will be in property-list format ... under GNUstep + * this is either OpenStep style (ASCII characters using \U hexadecimal + * escape sequences for unicode), or MacOS-X style (XML in the UTF8 + * character set). + *

+ *

If the useAuxiliaryFile flag is YES, the file write operation is + * atomic ... the data is written to a temporary file, which is then + * renamed to the actual file name. + *

+ *

If the conversion of data into the correct property-list format fails + * or the write operation fails, the method returns NO, otherwise it + * returns YES. + *

+ *

NB. The fact that the file is in property-list format does not + * necessarily mean that it can be used to reconstruct the array using + * the -initWithContentsOfFile: method. If the original array contains + * non-property-list objects, the descriptions of those objects will + * have been written, and reading in the file as a property-list will + * result in a new array containing the string descriptions. + *

+ */ +- (BOOL) writeToFile: (NSString *)path atomically: (BOOL)useAuxiliaryFile { extern BOOL GSMacOSXCompatiblePropertyLists(); NSDictionary *loc; @@ -857,10 +898,17 @@ static NSString *indentStrings[] = { desc = result; } - return [desc writeToFile: path atomically: useAuxilliaryFile]; + return [[desc dataUsingEncoding: NSUTF8StringEncoding] + writeToFile: path atomically: useAuxiliaryFile]; } -- (BOOL) writeToURL: (NSURL *)url atomically: (BOOL)useAuxilliaryFile +/** + *

Writes the contents of the array to the specified url. + * This functions just like -writeToFile:atomically: except that the + * output may be written to any URL, not just a local file. + *

+ */ +- (BOOL) writeToURL: (NSURL *)url atomically: (BOOL)useAuxiliaryFile { extern BOOL GSMacOSXCompatiblePropertyLists(); NSDictionary *loc; @@ -886,7 +934,8 @@ static NSString *indentStrings[] = { desc = result; } - return [desc writeToURL: url atomically: useAuxilliaryFile]; + return [[desc dataUsingEncoding: NSUTF8StringEncoding] + writeToURL: url atomically: useAuxiliaryFile]; } @end diff --git a/Source/NSDictionary.m b/Source/NSDictionary.m index 4a45f159b..8805ad593 100644 --- a/Source/NSDictionary.m +++ b/Source/NSDictionary.m @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -412,12 +413,28 @@ static SEL appSel; return self; } +/** + *

Initialises the dictionary with the contents of the specified file, + * which must contain a dictionary in property-list format. + *

+ *

In GNUstep, the property-list format may be either the OpenStep + * format (ASCII data), or the MacOS-X format (URF8 XML data) ... this + * method will recognise which it is. + *

+ *

If there is a failure to load the file for any reason, the receiver + * will be released and the method will return nil. + *

+ */ - (id) initWithContentsOfFile: (NSString*)path { NSString *myString; + NSData *someData; - myString = [[NSString allocWithZone: NSDefaultMallocZone()] + someData = [[NSData allocWithZone: NSDefaultMallocZone()] initWithContentsOfFile: path]; + myString = [[NSString allocWithZone: NSDefaultMallocZone()] + initWithData: someData encoding: NSUTF8StringEncoding]; + RELEASE(someData); if (myString) { id result; @@ -630,6 +647,29 @@ compareIt(id o1, id o2, void* context) } } +/** + *

Writes the contents of the dictionary to the file specified by path. + * The file contents will be in property-list format ... under GNUstep + * this is either OpenStep style (ASCII characters using \U hexadecimal + * escape sequences for unicode), or MacOS-X style (XML in the UTF8 + * character set). + *

+ *

If the useAuxiliaryFile flag is YES, the file write operation is + * atomic ... the data is written to a temporary file, which is then + * renamed to the actual file name. + *

+ *

If the conversion of data into the correct property-list format fails + * or the write operation fails, the method returns NO, otherwise it + * returns YES. + *

+ *

NB. The fact that the file is in property-list format does not + * necessarily mean that it can be used to reconstruct the dictionary using + * the -initWithContentsOfFile: method. If the original dictionary contains + * non-property-list objects, the descriptions of those objects will + * have been written, and reading in the file as a property-list will + * result in a new dictionary containing the string descriptions. + *

+ */ - (BOOL) writeToFile: (NSString *)path atomically: (BOOL)useAuxiliaryFile { extern BOOL GSMacOSXCompatiblePropertyLists(); @@ -656,9 +696,16 @@ compareIt(id o1, id o2, void* context) desc = result; } - return [desc writeToFile: path atomically: useAuxiliaryFile]; + return [[desc dataUsingEncoding: NSUTF8StringEncoding] + writeToFile: path atomically: useAuxiliaryFile]; } +/** + *

Writes the contents of the dictionary to the specified url. + * This functions just like -writeToFile:atomically: except that the + * output may be written to any URL, not just a local file. + *

+ */ - (BOOL) writeToURL: (NSURL *)url atomically: (BOOL)useAuxiliaryFile { extern BOOL GSMacOSXCompatiblePropertyLists(); @@ -685,7 +732,8 @@ compareIt(id o1, id o2, void* context) desc = result; } - return [desc writeToURL: url atomically: useAuxiliaryFile]; + return [[desc dataUsingEncoding: NSUTF8StringEncoding] + writeToURL: url atomically: useAuxiliaryFile]; } - (NSString*) description