From 207386000afff61ec1b661a17fa9747bf7fb4407 Mon Sep 17 00:00:00 2001 From: rowanbeentje Date: Sun, 8 Sep 2013 20:37:21 +0000 Subject: Allow opened SQL files to be saved back to the original file, addressing Issue #1805: - Add new menu item for "Save Query", making the existing item "Save Query As" as an alternate - Track the opened SQL file and used encoding to be able to easily save the same file again - Standardise encoding detection to use the UniversalDetector framework and use that for opened SQL files where the encoding menu is not used --- Source/SPFileManagerAdditions.m | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'Source/SPFileManagerAdditions.m') diff --git a/Source/SPFileManagerAdditions.m b/Source/SPFileManagerAdditions.m index 2e63b120..7868c427 100644 --- a/Source/SPFileManagerAdditions.m +++ b/Source/SPFileManagerAdditions.m @@ -31,6 +31,8 @@ // More info at #import "SPFileManagerAdditions.h" +#import "SPFileHandle.h" +#import enum { @@ -153,6 +155,31 @@ static NSString *DirectoryLocationDomain = @"DirectoryLocationDomain"; return resolvedPath; } +/** + * Use the UniversalDetector library to attempt to detect the encoding at the file at + * the supplied URL. Only the first five megabytes are read if the file is larger. + * As with all encoding detection, this will return only best-guess result except + * for where encoding markers exist. + * Uses a SPFileHandle internally so it can detect the encoding within gzipped and + * bzipped files. + * Returns NSUTF8StringEncoding if the encoding cannot be detected. + */ +- (NSStringEncoding)detectEncodingforFileAtPath:(NSString *)aPath +{ + NSStringEncoding detectedEncoding; + SPFileHandle *detectorFileHandle = [SPFileHandle fileHandleForReadingAtPath:aPath]; + if (!detectorFileHandle) { + return NSUTF8StringEncoding; + } + + UniversalDetector *fileEncodingDetector = [[UniversalDetector alloc] init]; + [fileEncodingDetector analyzeData:[detectorFileHandle readDataOfLength:5000000]]; + detectedEncoding = [fileEncodingDetector encoding]; + [fileEncodingDetector release]; + + return detectedEncoding; +} + + (NSString *)temporaryDirectory { NSString *tempDir = NSTemporaryDirectory(); -- cgit v1.2.3