Added a crude implemention for [initWithSerializedRepresentation:]

and [serializedRepresentation]. Also scattered some AUTORELEASE and
RELEASE statements.


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@6812 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Fred Kiefer 2000-06-26 22:21:48 +00:00
parent 0177742a92
commit 4932d1be98

View file

@ -35,6 +35,7 @@
#include <AppKit/NSWorkspace.h> #include <AppKit/NSWorkspace.h>
#include <Foundation/NSArray.h> #include <Foundation/NSArray.h>
#include <Foundation/NSData.h> #include <Foundation/NSData.h>
#include <Foundation/NSValue.h>
#include <Foundation/NSException.h> #include <Foundation/NSException.h>
#include <Foundation/NSFileManager.h> #include <Foundation/NSFileManager.h>
@ -54,7 +55,7 @@
[super init]; [super init];
_wrapperType = GSFileWrapperDirectoryType; _wrapperType = GSFileWrapperDirectoryType;
_wrapperData = [NSMutableDictionary dictionaryWithCapacity: [docs count]]; ASSIGN(_wrapperData, [NSMutableDictionary dictionaryWithCapacity: [docs count]]);
enumerator = [docs keyEnumerator]; enumerator = [docs keyEnumerator];
while ((key = [enumerator nextObject])) while ((key = [enumerator nextObject]))
@ -91,7 +92,8 @@
return self; return self;
} }
// Init an instance from the file,
// Init an instance from the file,
// directory, or symbolic link at path. // directory, or symbolic link at path.
// This can create a tree of instances // This can create a tree of instances
// with a directory instance at the top // with a directory instance at the top
@ -118,16 +120,17 @@
while ((filename = [enumerator nextObject])) while ((filename = [enumerator nextObject]))
{ {
[fileWrappers addObject: [fileWrappers addObject:
[[NSFileWrapper alloc] initWithPath: AUTORELEASE([[NSFileWrapper alloc] initWithPath:
[path stringByAppendingPathComponent: filename]]]; [path stringByAppendingPathComponent: filename]])];
} }
self = [self initDirectoryWithFileWrappers: self = [self initDirectoryWithFileWrappers:
[NSDictionary dictionaryWithObjects: fileWrappers forKeys: filenames]]; [NSDictionary dictionaryWithObjects: fileWrappers forKeys: filenames]];
RELEASE(fileWrappers);
} }
else if ([fileType isEqualToString: @"NSFileTypeRegular"]) else if ([fileType isEqualToString: @"NSFileTypeRegular"])
{ {
self = [self initRegularFileWithContents: self = [self initRegularFileWithContents:
[[NSData alloc] initWithContentsOfFile: path]]; AUTORELEASE([[NSData alloc] initWithContentsOfFile: path])];
} }
else if ([fileType isEqualToString: @"NSFileTypeSymbolicLink"]) else if ([fileType isEqualToString: @"NSFileTypeSymbolicLink"])
{ {
@ -137,7 +140,8 @@
return self; return self;
} }
// Init an instance from data in std
// Init an instance from data in std
// serial format. Serial format is the // serial format. Serial format is the
// same as that used by NSText's // same as that used by NSText's
// RTFDFromRange: method. This can // RTFDFromRange: method. This can
@ -147,7 +151,39 @@
// FIXME - implement // FIXME - implement
- (id) initWithSerializedRepresentation: (NSData*)data - (id) initWithSerializedRepresentation: (NSData*)data
{ {
return nil; NSDictionary *dict = [NSDeserializer deserializePropertyListFromData: data
mutableContainers: NO];
NSNumber *type = [dict objectForKey: @"Type"];
switch ([type intValue])
{
case GSFileWrapperDirectoryType:
{
NSMutableDictionary *dict2 = [dict objectForKey: @"Contents"];
NSEnumerator *enumerator = [dict2 keyEnumerator];
NSString *key;
while ((key = (NSString*)[enumerator nextObject]))
{
[dict2 setObject: AUTORELEASE([[NSFileWrapper alloc]
initWithSerializedRepresentation:
[dict2 objectForKey: key]]) forKey: key];
}
[self initDirectoryWithFileWrappers: dict2];
}
case GSFileWrapperRegularFileType:
{
[self initRegularFileWithContents: [dict objectForKey: @"Contents"]];
}
case GSFileWrapperSymbolicLinkType:
{
[self initSymbolicLinkWithDestination: [dict objectForKey: @"Contents"]];
}
}
[self setFileAttributes: [dict objectForKey: @"Attributes"]];
return self;
} }
- (void) dealloc - (void) dealloc
@ -217,7 +253,33 @@
// FIXME - implement // FIXME - implement
- (NSData*) serializedRepresentation - (NSData*) serializedRepresentation
{ {
return nil; NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject: [NSNumber numberWithInt: _wrapperType] forKey: @"Type"];
[dict setObject: [self fileAttributes] forKey: @"Attributes"];
switch (_wrapperType)
{
case GSFileWrapperDirectoryType:
{
NSMutableDictionary *dict2 = [NSMutableDictionary dictionary];
NSEnumerator *enumerator = [_wrapperData keyEnumerator];
NSString *key;
while ((key = (NSString*)[enumerator nextObject]))
{
[dict2 setObject: [[_wrapperData objectForKey: key]
serializedRepresentation] forKey: key];
}
[dict setObject: dict2 forKey: @"Contents"];
}
case GSFileWrapperRegularFileType:
case GSFileWrapperSymbolicLinkType:
{
[dict setObject: _wrapperData forKey: @"Contents"];
}
}
return [NSSerializer serializePropertyList: dict];
} }
- (void) setFilename: (NSString*)filename - (void) setFilename: (NSString*)filename
@ -292,7 +354,7 @@
- (NSImage*) icon; - (NSImage*) icon;
{ {
if (!_iconImage) if (_iconImage == nil)
return [[NSWorkspace sharedWorkspace] iconForFile: [self filename]]; return [[NSWorkspace sharedWorkspace] iconForFile: [self filename]];
else else
return _iconImage; return _iconImage;
@ -385,7 +447,7 @@
NSFileWrapper *wrapper; NSFileWrapper *wrapper;
GSFileWrapperDirectoryTypeCheck(); GSFileWrapperDirectoryTypeCheck();
wrapper = [[NSFileWrapper alloc] initWithPath: path]; wrapper = AUTORELEASE([[NSFileWrapper alloc] initWithPath: path]);
if (wrapper != nil) if (wrapper != nil)
return [self addFileWrapper: wrapper]; return [self addFileWrapper: wrapper];
else else
@ -398,7 +460,7 @@
NSFileWrapper *wrapper; NSFileWrapper *wrapper;
GSFileWrapperDirectoryTypeCheck(); GSFileWrapperDirectoryTypeCheck();
wrapper = [[NSFileWrapper alloc] initRegularFileWithContents: data]; wrapper = AUTORELEASE([[NSFileWrapper alloc] initRegularFileWithContents: data]);
if (wrapper != nil) if (wrapper != nil)
{ {
[wrapper setPreferredFilename: filename]; [wrapper setPreferredFilename: filename];
@ -414,7 +476,7 @@
NSFileWrapper *wrapper; NSFileWrapper *wrapper;
GSFileWrapperDirectoryTypeCheck(); GSFileWrapperDirectoryTypeCheck();
wrapper = [[NSFileWrapper alloc] initSymbolicLinkWithDestination: path]; wrapper = AUTORELEASE([[NSFileWrapper alloc] initSymbolicLinkWithDestination: path]);
if (wrapper != nil) if (wrapper != nil)
{ {
[wrapper setPreferredFilename: filename]; [wrapper setPreferredFilename: filename];