diff options
author | rowanbeentje <rowan@beent.je> | 2013-09-08 20:37:21 +0000 |
---|---|---|
committer | rowanbeentje <rowan@beent.je> | 2013-09-08 20:37:21 +0000 |
commit | 207386000afff61ec1b661a17fa9747bf7fb4407 (patch) | |
tree | 4bb62890e27e1c443efa82cad1a3225265f68c19 /Source/SPFileManagerAdditions.m | |
parent | f800cc92469c2615fe5fb6cc351a24b1cfd6e944 (diff) | |
download | sequelpro-207386000afff61ec1b661a17fa9747bf7fb4407.tar.gz sequelpro-207386000afff61ec1b661a17fa9747bf7fb4407.tar.bz2 sequelpro-207386000afff61ec1b661a17fa9747bf7fb4407.zip |
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
Diffstat (limited to 'Source/SPFileManagerAdditions.m')
-rw-r--r-- | Source/SPFileManagerAdditions.m | 27 |
1 files changed, 27 insertions, 0 deletions
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 <http://code.google.com/p/sequel-pro/> #import "SPFileManagerAdditions.h" +#import "SPFileHandle.h" +#import <UniversalDetector/UniversalDetector.h> 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(); |