From 68e0fe10f057729f494af0ee7afa681dd7154675 Mon Sep 17 00:00:00 2001 From: Bibiko Date: Tue, 23 Jun 2009 09:06:35 +0000 Subject: =?UTF-8?q?=E2=80=A2=20moved=20dataToHex=20as=20dataToFormattedHex?= =?UTF-8?q?String=20from=20TableContent=20to=20SPDataAdditions=20in=20orde?= =?UTF-8?q?r=20to=20you=20that=20method=20in=20different=20classes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/SPDataAdditions.h | 2 +- Source/SPDataAdditions.m | 86 ++++++++++++++++++++++++++++++++++++++++++++ Source/TableContent.h | 1 - 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) -- cgit v1.2.3