Ensure property-list file storage is in UTF8 or ASCII

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@13181 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-Macdonald 2002-03-21 09:45:30 +00:00
parent e7affc80ba
commit 51f3983b3d
4 changed files with 112 additions and 10 deletions

View file

@ -29,6 +29,7 @@
#include <base/behavior.h>
#include <Foundation/NSDictionary.h>
#include <Foundation/NSArray.h>
#include <Foundation/NSData.h>
#include <Foundation/NSUtilities.h>
#include <Foundation/NSString.h>
#include <Foundation/NSException.h>
@ -412,12 +413,28 @@ static SEL appSel;
return self;
}
/**
* <p>Initialises the dictionary with the contents of the specified file,
* which must contain a dictionary in property-list format.
* </p>
* <p>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.
* </p>
* <p>If there is a failure to load the file for any reason, the receiver
* will be released and the method will return nil.
* </p>
*/
- (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)
}
}
/**
* <p>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).
* </p>
* <p>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.
* </p>
* <p>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.
* </p>
* <p>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.
* </p>
*/
- (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];
}
/**
* <p>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.
* </p>
*/
- (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