From 0b4391e4efa7fda146f3c07f1fd2c080ca3c2fe8 Mon Sep 17 00:00:00 2001 From: Bibiko Date: Fri, 21 Aug 2009 17:17:21 +0000 Subject: =?UTF-8?q?=E2=80=A2=20further=20work=20for=20open/save=20connecti?= =?UTF-8?q?on=20files=20(spf)=20=E2=80=A2=20added=20to=20NSDataAdditions?= =?UTF-8?q?=20the=20methods=20'compress'=20and=20'decompress'=20=E2=80=A2?= =?UTF-8?q?=20added=20libz.dylib=20to=20xcode=20project?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/SPDataAdditions.h | 2 + Source/SPDataAdditions.m | 79 ++++++++++++++++++++++++++++++++++++ Source/TableDocument.m | 43 ++++++++++++++++---- sequel-pro.xcodeproj/project.pbxproj | 2 + 4 files changed, 118 insertions(+), 8 deletions(-) diff --git a/Source/SPDataAdditions.h b/Source/SPDataAdditions.h index 1b787484..fcf2640c 100644 --- a/Source/SPDataAdditions.h +++ b/Source/SPDataAdditions.h @@ -29,5 +29,7 @@ - (NSString *) base64EncodingWithLineLength:(unsigned int)lineLength; - (NSString *) dataToFormattedHexString; - (NSString *) shortStringRepresentationUsingEncoding:(NSStringEncoding)encoding; +- (NSData *)compress; +- (NSData *)decompress; @end diff --git a/Source/SPDataAdditions.m b/Source/SPDataAdditions.m index 8018de8a..edac9baa 100644 --- a/Source/SPDataAdditions.m +++ b/Source/SPDataAdditions.m @@ -23,6 +23,7 @@ // More info at #import "SPDataAdditions.h" +#include static char base64encodingTable[64] = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P', @@ -100,6 +101,84 @@ static char base64encodingTable[64] = { return base64; } +- (NSData *)decompress +{ + if ([self length] == 0) return self; + + unsigned full_length = [self length]; + unsigned half_length = [self length] / 2; + + NSMutableData *unzipData = [NSMutableData dataWithLength: full_length + half_length]; + BOOL done = NO; + int status; + + z_stream zlibStream; + zlibStream.next_in = (Bytef *)[self bytes]; + zlibStream.avail_in = [self length]; + zlibStream.total_out = 0; + zlibStream.zalloc = Z_NULL; + zlibStream.zfree = Z_NULL; + + if(inflateInit(&zlibStream) != Z_OK) return nil; + + while(!done) + { + if (zlibStream.total_out >= [unzipData length]) + [unzipData increaseLengthBy: half_length]; + zlibStream.next_out = [unzipData mutableBytes] + zlibStream.total_out; + zlibStream.avail_out = [unzipData length] - zlibStream.total_out; + + status = inflate (&zlibStream, Z_SYNC_FLUSH); + if (status == Z_STREAM_END) done = YES; + else if (status != Z_OK) break; + } + if(inflateEnd (&zlibStream) != Z_OK) + return nil; + + if(done) { + [unzipData setLength: zlibStream.total_out]; + return [NSData dataWithData: unzipData]; + } + else + return nil; +} + +- (NSData *)compress +{ + if ([self length] == 0) return self; + + z_stream zlibStream; + + zlibStream.zalloc = Z_NULL; + zlibStream.zfree = Z_NULL; + zlibStream.opaque = Z_NULL; + zlibStream.total_out = 0; + zlibStream.next_in=(Bytef *)[self bytes]; + zlibStream.avail_in = [self length]; + + if (deflateInit(&zlibStream, Z_DEFAULT_COMPRESSION) != Z_OK) return nil; + + + NSMutableData *zipData = [NSMutableData dataWithLength:16384]; + + do{ + + if (zlibStream.total_out >= [zipData length]) + [zipData increaseLengthBy: 16384]; + + zlibStream.next_out = [zipData mutableBytes] + zlibStream.total_out; + zlibStream.avail_out = [zipData length] - zlibStream.total_out; + + deflate(&zlibStream, Z_FINISH); + + } while(zlibStream.avail_out == 0); + + deflateEnd(&zlibStream); + + [zipData setLength: zlibStream.total_out]; + return [NSData dataWithData: zipData]; +} + - (NSString *)dataToFormattedHexString /* returns the hex representation of the given data diff --git a/Source/TableDocument.m b/Source/TableDocument.m index 3abd32aa..52d3fe2c 100644 --- a/Source/TableDocument.m +++ b/Source/TableDocument.m @@ -40,6 +40,7 @@ #import "SPDatabaseData.h" #import "SPStringAdditions.h" #import "SPArrayAdditions.h" +#import "SPDataAdditions.h" #import "SPAppController.h" #import "SPExtendedTableInfo.h" #import "SPConnectionController.h" @@ -1695,7 +1696,29 @@ break; // open only the first SQL file } else if([[[filename pathExtension] lowercaseString] isEqualToString:@"spf"]) { - NSLog(@"open connection %@", filename); + + + NSError *readError = nil; + NSString *convError = nil; + NSPropertyListFormat format; + NSData *pData = [[NSData dataWithContentsOfFile:filename options:NSUncachedRead error:&readError] decompress]; + NSDictionary *spfData = [NSPropertyListSerialization propertyListFromData:pData + mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&convError]; + + if(!spfData || readError != nil || [convError length] | format != NSPropertyListBinaryFormat_v1_0) { + NSAlert *alert = [NSAlert alertWithMessageText:[NSString stringWithFormat:NSLocalizedString(@"Error while reading connection data file", @"error while reading connection data file")] + defaultButton:NSLocalizedString(@"OK", @"OK button") + alternateButton:nil + otherButton:nil + informativeTextWithFormat:NSLocalizedString(@"Connection data file couldn't be read.", @"error while reading connection data file")]; + + [alert setAlertStyle:NSCriticalAlertStyle]; + [alert runModal]; + return; + } + + NSLog(@"s: %@", [spfData description]); + } else { NSLog(@"Only files with the extensions ‘spf’ or ‘sql’ are allowed."); @@ -1810,7 +1833,6 @@ NSMutableDictionary *spf = [NSMutableDictionary dictionary]; NSIndexSet *contentSelectedIndexSet = [tableContentInstance selectedRowIndexes]; - NSIndexSet *customQuerySelectedIndexSet = [[customQueryInstance valueForKeyPath:@"customQueryView"] selectedRowIndexes]; [spf setObject:[NSNumber numberWithInt:1] forKey:@"version"]; @@ -1845,10 +1867,16 @@ if([[[[customQueryInstance valueForKeyPath:@"textView"] textStorage] string] length]) [spf setObject:[[[customQueryInstance valueForKeyPath:@"textView"] textStorage] string] forKey:@"queries"]; - if (contentSelectedIndexSet && [contentSelectedIndexSet count]) - [spf setObject:contentSelectedIndexSet forKey:@"contentSelectedIndexSet"]; - if (customQuerySelectedIndexSet && [customQuerySelectedIndexSet count]) - [spf setObject:customQuerySelectedIndexSet forKey:@"customQuerySelectedIndexSet"]; + if (contentSelectedIndexSet && [contentSelectedIndexSet count]) { + NSMutableArray *indices = [NSMutableArray array]; + unsigned indexBuffer[[contentSelectedIndexSet count]]; + unsigned limit = [contentSelectedIndexSet getIndexes:indexBuffer maxCount:[contentSelectedIndexSet count] inIndexRange:NULL]; + unsigned idx; + for (idx = 0; idx < limit; idx++) { + [indices addObject:[NSNumber numberWithInt:indexBuffer[idx]]]; + } + [spf setObject:indices forKey:@"contentSelectedIndexSet"]; + } NSString *err = nil; NSData *plist = [NSPropertyListSerialization dataFromPropertyList:spf @@ -1868,13 +1896,12 @@ } NSError *error = nil; - [plist writeToFile:fileName options:NSAtomicWrite error:&error]; + [[plist compress] writeToFile:fileName options:NSAtomicWrite error:&error]; if(error != nil){ NSAlert *errorAlert = [NSAlert alertWithError:error]; [errorAlert runModal]; return; } - return; } } diff --git a/sequel-pro.xcodeproj/project.pbxproj b/sequel-pro.xcodeproj/project.pbxproj index 3ef0efb7..cbbea8bc 100644 --- a/sequel-pro.xcodeproj/project.pbxproj +++ b/sequel-pro.xcodeproj/project.pbxproj @@ -224,6 +224,7 @@ BC9F0881100FCF2C00A80D32 /* SPFieldEditorController.m in Sources */ = {isa = PBXBuildFile; fileRef = BC9F0880100FCF2C00A80D32 /* SPFieldEditorController.m */; }; BCA6271C1031B9D40047E5D5 /* SPTooltip.m in Sources */ = {isa = PBXBuildFile; fileRef = BCA6271B1031B9D40047E5D5 /* SPTooltip.m */; }; BCA6F631100FA7D700E80253 /* FieldEditorSheet.xib in Resources */ = {isa = PBXBuildFile; fileRef = BCA6F62F100FA7D700E80253 /* FieldEditorSheet.xib */; }; + BCC5CC3C103EEE49007CE557 /* libz.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 17B7B593101602D200F057DE /* libz.dylib */; }; BCD0AD490FBBFC340066EA5C /* SPSQLTokenizer.l in Sources */ = {isa = PBXBuildFile; fileRef = BCD0AD480FBBFC340066EA5C /* SPSQLTokenizer.l */; }; /* End PBXBuildFile section */ @@ -644,6 +645,7 @@ 4DECC3350EC2A170008D359E /* Sparkle.framework in Frameworks */, 296DC89F0F8FD336002A3258 /* WebKit.framework in Frameworks */, 17B7B5991016038400F057DE /* MCPKit.framework in Frameworks */, + BCC5CC3C103EEE49007CE557 /* libz.dylib in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; -- cgit v1.2.3