diff options
author | Bibiko <bibiko@eva.mpg.de> | 2009-06-23 09:06:35 +0000 |
---|---|---|
committer | Bibiko <bibiko@eva.mpg.de> | 2009-06-23 09:06:35 +0000 |
commit | 68e0fe10f057729f494af0ee7afa681dd7154675 (patch) | |
tree | 0b4a1c26a4050629ca8aff537611b17f6a4bd203 | |
parent | 74a0f3eed47cd086f689a543e07c74f5c38f6248 (diff) | |
download | sequelpro-68e0fe10f057729f494af0ee7afa681dd7154675.tar.gz sequelpro-68e0fe10f057729f494af0ee7afa681dd7154675.tar.bz2 sequelpro-68e0fe10f057729f494af0ee7afa681dd7154675.zip |
• moved dataToHex as dataToFormattedHexString from TableContent to SPDataAdditions in order to you that method in different classes
-rw-r--r-- | Source/SPDataAdditions.h | 2 | ||||
-rw-r--r-- | Source/SPDataAdditions.m | 86 | ||||
-rw-r--r-- | Source/TableContent.h | 1 | ||||
-rw-r--r-- | Source/TableContent.m | 92 |
4 files changed, 90 insertions, 91 deletions
diff --git a/Source/SPDataAdditions.h b/Source/SPDataAdditions.h index 5f51213e..c1c0b389 100644 --- a/Source/SPDataAdditions.h +++ b/Source/SPDataAdditions.h @@ -28,6 +28,6 @@ @interface NSData (SPDataAdditions) - (NSString *) base64EncodingWithLineLength:(unsigned int)lineLength; - +- (NSString *) dataToFormattedHexString; @end diff --git a/Source/SPDataAdditions.m b/Source/SPDataAdditions.m index 166288e5..ec91f55c 100644 --- a/Source/SPDataAdditions.m +++ b/Source/SPDataAdditions.m @@ -100,6 +100,92 @@ static char base64encodingTable[64] = { return base64; } +- (NSString *)dataToFormattedHexString +/* + returns the hex representation of the given data + */ +{ + unsigned i; + unsigned totalLength = [self length]; + int bytesPerLine = 16; + NSMutableString *retVal = [NSMutableString string]; + char *nodisplay = "\t\n\r\f"; + + // get the length of the longest location + int longest = [(NSString *)[NSString stringWithFormat:@"%X", totalLength - ( totalLength % bytesPerLine )] length]; + + for ( i = 0; i < totalLength; i += bytesPerLine ) { + int j; + NSMutableString *hex = [[NSMutableString alloc] initWithCapacity:(3 * bytesPerLine - 1)]; + NSMutableString *location = [[NSMutableString alloc] initWithCapacity:(longest + 2)]; + NSMutableString *chars = [[NSMutableString alloc] init]; + unsigned char *buffer; + int buffLength = bytesPerLine; + + // add hex value of location + [location appendString:[NSString stringWithFormat:@"%X", i]]; + + // pad it + while( longest > [location length] ) { + [location insertString:@"0" atIndex:0]; + } + + // get the chars from the NSData obj + if ( i + buffLength >= totalLength ) { + buffLength = totalLength - i; + } + buffer = (unsigned char*) malloc( sizeof( unsigned char ) * buffLength ); + NSRange range = { i, buffLength }; + [self getBytes:buffer range:range]; + + // build the hex string + for ( j = 0; j < buffLength; j++ ) { + unsigned char byte = *(buffer + j); + if ( byte < 16 ) { + [hex appendString:@"0"]; + } + [hex appendString:[NSString stringWithFormat:@"%X", byte]]; + [hex appendString:@" "]; + + // if the char is undisplayable, replace it with "." + unsigned char current; + int count = 0; + while ( ( current = *(nodisplay + count++) ) > 0 ) { + if ( current == byte ) { + *(buffer + j) = '.'; + break; + } + } + } + + // add padding to missing hex values. + for ( j = 0; j < bytesPerLine - buffLength; j++ ) { + [hex appendString:@" "]; + } + + // remove extra ghost characters + [chars appendString:[NSString stringWithCString:(char *)buffer]]; + if ( [chars length] > bytesPerLine ) { + [chars deleteCharactersInRange:NSMakeRange( bytesPerLine, [chars length] - bytesPerLine )]; + } + + // build line + [retVal appendString:location]; + [retVal appendString:@" "]; + [retVal appendString:hex]; + [retVal appendString:@" "]; + [retVal appendString:chars]; + [retVal appendString:@"\n"]; + + // clean up + [hex release]; + [chars release]; + [location release]; + free( buffer ); + } + + return retVal; +} @end diff --git a/Source/TableContent.h b/Source/TableContent.h index dcb527ca..7e020792 100644 --- a/Source/TableContent.h +++ b/Source/TableContent.h @@ -104,7 +104,6 @@ - (void)processUpdatedImageData:(NSData *)data; - (IBAction)dropImage:(id)sender; - (void)textViewDidChangeSelection:(NSNotification *)aNotification; -- (NSString *)dataToHex:(NSData *)data; //getter methods - (NSArray *)currentResult; diff --git a/Source/TableContent.m b/Source/TableContent.m index 3d723c1b..e6f0d085 100644 --- a/Source/TableContent.m +++ b/Source/TableContent.m @@ -890,7 +890,7 @@ else [editTextView setString:@""]; - [hexTextView setString:[self dataToHex:editData]]; + [hexTextView setString:[editData dataToFormattedHexString]]; // If the image cell now contains a valid image, select the image view if (image) { @@ -1125,7 +1125,7 @@ // Set the string contents and hex representation if(contents) [editTextView setString:contents]; - [hexTextView setString:[self dataToHex:editData]]; + [hexTextView setString:[editData dataToFormattedHexString]]; [contents release]; editSheetWillBeInitialized = NO; @@ -1169,92 +1169,6 @@ } -- (NSString *)dataToHex:(NSData *)data -/* - returns the hex representation of the given data - */ -{ - unsigned i; - unsigned totalLength = [data length]; - int bytesPerLine = 16; - NSMutableString *retVal = [NSMutableString string]; - char *nodisplay = "\t\n\r\f"; - - // get the length of the longest location - int longest = [(NSString *)[NSString stringWithFormat:@"%X", totalLength - ( totalLength % bytesPerLine )] length]; - - for ( i = 0; i < totalLength; i += bytesPerLine ) { - int j; - NSMutableString *hex = [[NSMutableString alloc] initWithCapacity:(3 * bytesPerLine - 1)]; - NSMutableString *location = [[NSMutableString alloc] initWithCapacity:(longest + 2)]; - NSMutableString *chars = [[NSMutableString alloc] init]; - unsigned char *buffer; - int buffLength = bytesPerLine; - - // add hex value of location - [location appendString:[NSString stringWithFormat:@"%X", i]]; - - // pad it - while( longest > [location length] ) { - [location insertString:@"0" atIndex:0]; - } - - // get the chars from the NSData obj - if ( i + buffLength >= totalLength ) { - buffLength = totalLength - i; - } - buffer = (unsigned char*) malloc( sizeof( unsigned char ) * buffLength ); - NSRange range = { i, buffLength }; - [data getBytes:buffer range:range]; - - // build the hex string - for ( j = 0; j < buffLength; j++ ) { - unsigned char byte = *(buffer + j); - if ( byte < 16 ) { - [hex appendString:@"0"]; - } - [hex appendString:[NSString stringWithFormat:@"%X", byte]]; - [hex appendString:@" "]; - - // if the char is undisplayable, replace it with "." - unsigned char current; - int count = 0; - while ( ( current = *(nodisplay + count++) ) > 0 ) { - if ( current == byte ) { - *(buffer + j) = '.'; - break; - } - } - } - - // add padding to missing hex values. - for ( j = 0; j < bytesPerLine - buffLength; j++ ) { - [hex appendString:@" "]; - } - - // remove extra ghost characters - [chars appendString:[NSString stringWithCString:(char *)buffer]]; - if ( [chars length] > bytesPerLine ) { - [chars deleteCharactersInRange:NSMakeRange( bytesPerLine, [chars length] - bytesPerLine )]; - } - - // build line - [retVal appendString:location]; - [retVal appendString:@" "]; - [retVal appendString:hex]; - [retVal appendString:@" "]; - [retVal appendString:chars]; - [retVal appendString:@"\n"]; - - // clean up - [hex release]; - [chars release]; - [location release]; - free( buffer ); - } - - return retVal; -} //getter methods - (NSArray *)currentDataResult @@ -2350,7 +2264,7 @@ objectValueForTableColumn:(NSTableColumn *)aTableColumn if ( [theValue isKindOfClass:[NSData class]] ) { image = [[[NSImage alloc] initWithData:theValue] autorelease]; - [hexTextView setString:[self dataToHex:theValue]]; + [hexTextView setString:[theValue dataToFormattedHexString]]; stringValue = [[NSString alloc] initWithData:theValue encoding:[mySQLConnection encoding]]; if (stringValue == nil) |