Use readFromURL: instead of readFromData:

This commit is contained in:
1024jp 2016-03-15 01:40:27 +09:00
parent 3337989184
commit eeb3526a95
2 changed files with 11 additions and 9 deletions

View File

@ -14,6 +14,7 @@ develop
- Display the dialog asking encoding compatibility on saving as a document-modal sheet.
- Optimize document opening performance with large file.
- Improve recovering status of unsaved documents on window resume.

View File

@ -220,23 +220,24 @@ NSString *_Nonnull const CEIncompatibleConvertedCharKey = @"convertedChar";
// ------------------------------------------------------
/// load document from file and return whether it succeeded
- (BOOL)readFromData:(nonnull NSData *)data ofType:(nonnull NSString *)typeName error:(NSError * _Nullable __autoreleasing * _Nullable)outError
- (BOOL)readFromURL:(nonnull NSURL *)url ofType:(nonnull NSString *)typeName error:(NSError * _Nullable __autoreleasing * _Nullable)outError
// ------------------------------------------------------
{
// [caution] This method may be called from a background thread due to concurrent-opening.
NSData *data = [NSData dataWithContentsOfURL:url options:0 error:outError];
if (!data) { return NO; }
// store file hash (MD5) in order to check the file content identity in `presentedItemDidChange`
[self setFileMD5:[data MD5]];
// read file attributes
if ([self fileURL]) {
NSDictionary<NSString *, id> *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[[self fileURL] path] error:outError];
[self setFileAttributes:attributes];
[self setExecutable:([attributes filePosixPermissions] & S_IXUSR) != 0];
}
NSDictionary<NSString *, id> *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[url path] error:outError];
[self setFileAttributes:attributes];
[self setExecutable:([attributes filePosixPermissions] & S_IXUSR) != 0];
// try reading the `com.apple.TextEncoding` extended attribute
NSStringEncoding xattrEncoding = [self fileURL] ? [[self fileURL] getXattrEncoding] : NSNotFound;
NSStringEncoding xattrEncoding = [url getXattrEncoding];
// don't save xattr if file doesn't have it in order to avoid saving wrong encoding (2015-01 by 1024jp).
[self setShouldSaveXattr:(xattrEncoding != NSNotFound) || ([data length] == 0)];
@ -256,7 +257,7 @@ NSString *_Nonnull const CEIncompatibleConvertedCharKey = @"convertedChar";
}
// determine syntax style
NSString *styleName = [[CESyntaxManager sharedManager] styleNameFromFileName:[[self fileURL] lastPathComponent]];
NSString *styleName = [[CESyntaxManager sharedManager] styleNameFromFileName:[url lastPathComponent]];
if (!styleName && string) {
styleName = [[CESyntaxManager sharedManager] styleNameFromContent:string];
}
@ -389,7 +390,7 @@ NSString *_Nonnull const CEIncompatibleConvertedCharKey = @"convertedChar";
if (!error) {
// write encoding to the external file attributes (com.apple.TextEncoding)
if ([self shouldSaveXattr]) {
if (saveOperation == NSAutosaveElsewhereOperation || [self shouldSaveXattr]) {
[url setXattrEncoding:encoding];
}
// save text orientation state