mirror of
https://github.com/gnustep/libs-gui.git
synced 2025-04-23 07:00:46 +00:00
Merge Headers/AppKit/NSFileWrapper.h Source/NSFileWrapper.m
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/branches/gnustep_testplant_branch@38726 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
b70ec16e6c
commit
6c4ad9ef8d
2 changed files with 174 additions and 21 deletions
|
@ -32,12 +32,13 @@
|
|||
#import <GNUstepBase/GSVersionMacros.h>
|
||||
|
||||
#import <Foundation/NSObject.h>
|
||||
#import <AppKit/NSImage.h>
|
||||
|
||||
@class NSData;
|
||||
@class NSDictionary;
|
||||
@class NSError;
|
||||
@class NSMutableDictionary;
|
||||
@class NSString;
|
||||
@class NSURL;
|
||||
@class NSImage;
|
||||
|
||||
typedef enum
|
||||
|
@ -47,6 +48,20 @@ typedef enum
|
|||
GSFileWrapperSymbolicLinkType
|
||||
} GSFileWrapperType;
|
||||
|
||||
#if OS_API_VERSION(MAC_OS_X_VERSION_10_6, GS_API_LATEST)
|
||||
enum {
|
||||
NSFileWrapperReadingImmediate = 1,
|
||||
NSFileWrapperReadingWithoutMapping = 2,
|
||||
};
|
||||
typedef NSUInteger NSFileWrapperReadingOptions;
|
||||
|
||||
enum {
|
||||
NSFileWrapperWritingAtomic = 1,
|
||||
NSFileWrapperWritingWithNameUpdating = 2,
|
||||
};
|
||||
typedef NSUInteger NSFileWrapperWritingOptions;
|
||||
#endif
|
||||
|
||||
@interface NSFileWrapper : NSObject
|
||||
{
|
||||
NSString *_filename;
|
||||
|
@ -138,6 +153,22 @@ typedef enum
|
|||
|
||||
- (NSString *)symbolicLinkDestination;
|
||||
|
||||
#if OS_API_VERSION(MAC_OS_X_VERSION_10_6, GS_API_LATEST)
|
||||
- (id)initSymbolicLinkWithDestinationURL:(NSURL *)url;
|
||||
- (id)initWithURL:(NSURL *)url
|
||||
options:(NSFileWrapperReadingOptions)options
|
||||
error:(NSError **)outError;
|
||||
- (BOOL)matchesContentsOfURL:(NSURL *)url;
|
||||
- (BOOL)readFromURL:(NSURL *)url
|
||||
options:(NSFileWrapperReadingOptions)options
|
||||
error:(NSError **)outError;
|
||||
- (NSURL *)symbolicLinkDestinationURL;
|
||||
- (BOOL)writeToURL:(NSURL *)url
|
||||
options:(NSFileWrapperWritingOptions)options
|
||||
originalContentsURL:(NSURL *)originalContentsURL
|
||||
error:(NSError **)outError;
|
||||
#endif
|
||||
|
||||
@end
|
||||
|
||||
#endif // _GNUstep_H_NSFileWrapper
|
||||
|
|
|
@ -37,9 +37,11 @@
|
|||
#import <Foundation/NSDebug.h>
|
||||
#import <Foundation/NSException.h>
|
||||
#import <Foundation/NSFileManager.h>
|
||||
#import <Foundation/NSURL.h>
|
||||
#import <Foundation/NSValue.h>
|
||||
#import "AppKit/NSFileWrapper.h"
|
||||
#import "AppKit/NSFont.h"
|
||||
#import "AppKit/NSImage.h"
|
||||
#import "AppKit/NSWorkspace.h"
|
||||
|
||||
@implementation NSFileWrapper
|
||||
|
@ -101,6 +103,12 @@
|
|||
return self;
|
||||
}
|
||||
|
||||
- (id) initSymbolicLinkWithDestinationURL: (NSURL*)url
|
||||
{
|
||||
// FIXME
|
||||
return [self initSymbolicLinkWithDestination: [url path]];
|
||||
}
|
||||
|
||||
/**
|
||||
* Init an instance from the file, directory, or symbolic link at path.<br />
|
||||
* This can create a tree of instances with a directory instance at the top
|
||||
|
@ -109,17 +117,14 @@
|
|||
{
|
||||
CREATE_AUTORELEASE_POOL(arp);
|
||||
NSFileManager *fm = [NSFileManager defaultManager];
|
||||
NSDictionary *fileAttributes;
|
||||
NSString *fileType;
|
||||
|
||||
NSDebugLLog(@"NSFileWrapper", @"initWithPath: %@", path);
|
||||
|
||||
// Store the full path in filename, the specification is unclear in this point
|
||||
[self setFilename: path];
|
||||
[self setPreferredFilename: [path lastPathComponent]];
|
||||
[self setFileAttributes: [fm fileAttributesAtPath: path traverseLink: NO]];
|
||||
|
||||
fileType = [[self fileAttributes] fileType];
|
||||
if ([fileType isEqualToString: @"NSFileTypeDirectory"])
|
||||
fileAttributes = [fm fileAttributesAtPath: path traverseLink: NO];
|
||||
fileType = [fileAttributes fileType];
|
||||
if ([fileType isEqualToString: NSFileTypeDirectory])
|
||||
{
|
||||
NSString *filename;
|
||||
NSMutableArray *fileWrappers = [NSMutableArray array];
|
||||
|
@ -128,30 +133,54 @@
|
|||
|
||||
while ((filename = [enumerator nextObject]) != nil)
|
||||
{
|
||||
NSFileWrapper *w;
|
||||
|
||||
w = [[NSFileWrapper alloc] initWithPath:
|
||||
[path stringByAppendingPathComponent: filename]];
|
||||
[fileWrappers addObject: w];
|
||||
RELEASE(w);
|
||||
NSFileWrapper *w;
|
||||
|
||||
w = [[NSFileWrapper alloc] initWithPath:
|
||||
[path stringByAppendingPathComponent: filename]];
|
||||
[fileWrappers addObject: w];
|
||||
RELEASE(w);
|
||||
}
|
||||
self = [self initDirectoryWithFileWrappers:
|
||||
[NSDictionary dictionaryWithObjects: fileWrappers forKeys: filenames]];
|
||||
}
|
||||
else if ([fileType isEqualToString: @"NSFileTypeRegular"])
|
||||
else if ([fileType isEqualToString: NSFileTypeRegular])
|
||||
{
|
||||
self = [self initRegularFileWithContents:
|
||||
AUTORELEASE([[NSData alloc] initWithContentsOfFile: path])];
|
||||
AUTORELEASE([[NSData alloc] initWithContentsOfFile: path])];
|
||||
}
|
||||
else if ([fileType isEqualToString: @"NSFileTypeSymbolicLink"])
|
||||
else if ([fileType isEqualToString: NSFileTypeSymbolicLink])
|
||||
{
|
||||
self = [self initSymbolicLinkWithDestination:
|
||||
[fm pathContentOfSymbolicLinkAtPath: path]];
|
||||
}
|
||||
|
||||
// Store the full path in filename, the specification is unclear in this point
|
||||
[self setFilename: path];
|
||||
[self setPreferredFilename: [path lastPathComponent]];
|
||||
[self setFileAttributes: fileAttributes];
|
||||
|
||||
[arp drain];
|
||||
return self;
|
||||
}
|
||||
|
||||
- (id) initWithURL: (NSURL*)url
|
||||
options: (NSFileWrapperReadingOptions)options
|
||||
error: (NSError**)outError
|
||||
{
|
||||
// FIXME
|
||||
if ([self readFromURL: url
|
||||
options: options
|
||||
error: outError])
|
||||
{
|
||||
return self;
|
||||
}
|
||||
else
|
||||
{
|
||||
DESTROY(self);
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
|
||||
// Init an instance from data in std serial format. Serial format is the
|
||||
// same as that used by NSText's RTFDFromRange: method. This can
|
||||
// create a tree of instances with a directory instance at the top
|
||||
|
@ -435,10 +464,10 @@
|
|||
//
|
||||
|
||||
#define GSFileWrapperDirectoryTypeCheck() \
|
||||
if (_wrapperType != GSFileWrapperDirectoryType) \
|
||||
if (![self isDirectory]) \
|
||||
[NSException raise: NSInternalInconsistencyException \
|
||||
format: @"Can't invoke %@ on a file wrapper that" \
|
||||
@" does not wrap a directory!", _cmd];
|
||||
@" does not wrap a directory!", NSStringFromSelector(_cmd)];
|
||||
|
||||
- (NSString*) addFileWrapper: (NSFileWrapper*)doc
|
||||
{
|
||||
|
@ -544,7 +573,7 @@
|
|||
|
||||
- (NSData*) regularFileContents
|
||||
{
|
||||
if (_wrapperType == GSFileWrapperRegularFileType)
|
||||
if ([self isRegularFile])
|
||||
{
|
||||
return _wrapperData;
|
||||
}
|
||||
|
@ -563,7 +592,7 @@
|
|||
|
||||
- (NSString*) symbolicLinkDestination
|
||||
{
|
||||
if (_wrapperType == GSFileWrapperSymbolicLinkType)
|
||||
if ([self isSymbolicLink])
|
||||
{
|
||||
return _wrapperData;
|
||||
}
|
||||
|
@ -576,6 +605,99 @@
|
|||
return nil;
|
||||
}
|
||||
|
||||
- (NSURL *)symbolicLinkDestinationURL
|
||||
{
|
||||
// FIXME
|
||||
return [NSURL fileURLWithPath: [self symbolicLinkDestination]];
|
||||
}
|
||||
|
||||
- (BOOL) matchesContentsOfURL: (NSURL*)url
|
||||
{
|
||||
// FIXME
|
||||
// For
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (BOOL) readFromURL: (NSURL*)url
|
||||
options: (NSFileWrapperReadingOptions)options
|
||||
error: (NSError**)outError
|
||||
{
|
||||
// FIXME
|
||||
NSFileManager *fm = [NSFileManager defaultManager];
|
||||
NSString *path = [url path];
|
||||
NSDictionary *fileAttributes;
|
||||
NSString *fileType;
|
||||
|
||||
NSDebugLLog(@"NSFileWrapper", @"readFromURL: %@", path);
|
||||
|
||||
fileAttributes = [fm fileAttributesAtPath: path traverseLink: NO];
|
||||
fileType = [fileAttributes fileType];
|
||||
if ([fileType isEqualToString: NSFileTypeDirectory])
|
||||
{
|
||||
if (options & NSFileWrapperReadingImmediate)
|
||||
{
|
||||
NSString *filename;
|
||||
NSMutableArray *fileWrappers = [NSMutableArray array];
|
||||
NSArray *filenames = [fm directoryContentsAtPath: path];
|
||||
NSEnumerator *enumerator = [filenames objectEnumerator];
|
||||
|
||||
while ((filename = [enumerator nextObject]) != nil)
|
||||
{
|
||||
NSFileWrapper *w;
|
||||
|
||||
w = [[NSFileWrapper alloc] initWithPath:
|
||||
[path stringByAppendingPathComponent: filename]];
|
||||
[fileWrappers addObject: w];
|
||||
RELEASE(w);
|
||||
}
|
||||
self = [self initDirectoryWithFileWrappers:
|
||||
[NSDictionary dictionaryWithObjects: fileWrappers
|
||||
forKeys: filenames]];
|
||||
}
|
||||
else
|
||||
{
|
||||
self = [self initDirectoryWithFileWrappers: nil];
|
||||
}
|
||||
}
|
||||
else if ([fileType isEqualToString: NSFileTypeRegular])
|
||||
{
|
||||
if (options & NSFileWrapperReadingWithoutMapping)
|
||||
{
|
||||
self = [self initRegularFileWithContents:
|
||||
AUTORELEASE([[NSData alloc] initWithContentsOfFile: path])];
|
||||
}
|
||||
else
|
||||
{
|
||||
self = [self initRegularFileWithContents:
|
||||
AUTORELEASE([[NSData alloc] initWithContentsOfMappedFile: path])];
|
||||
}
|
||||
}
|
||||
else if ([fileType isEqualToString: NSFileTypeSymbolicLink])
|
||||
{
|
||||
self = [self initSymbolicLinkWithDestination:
|
||||
[fm pathContentOfSymbolicLinkAtPath: path]];
|
||||
}
|
||||
|
||||
// Store the full path in filename, the specification is unclear in this point
|
||||
[self setFilename: path];
|
||||
[self setPreferredFilename: [path lastPathComponent]];
|
||||
[self setFileAttributes: [fm fileAttributesAtPath: path traverseLink: NO]];
|
||||
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (BOOL) writeToURL: (NSURL*)url
|
||||
options: (NSFileWrapperWritingOptions)options
|
||||
originalContentsURL: (NSURL*)originalContentsURL
|
||||
error: (NSError**)outError
|
||||
{
|
||||
// FIXME
|
||||
return [self writeToFile: [url path]
|
||||
atomically: options & NSFileWrapperWritingAtomic
|
||||
updateFilenames: options & NSFileWrapperWritingWithNameUpdating];
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// Archiving
|
||||
//
|
||||
|
|
Loading…
Reference in a new issue