diff options
-rw-r--r-- | Source/SPEncodingPopupAccessory.m | 33 | ||||
-rw-r--r-- | Source/SPFileHandle.m | 69 |
2 files changed, 65 insertions, 37 deletions
diff --git a/Source/SPEncodingPopupAccessory.m b/Source/SPEncodingPopupAccessory.m index f9c34238..3179b1fe 100644 --- a/Source/SPEncodingPopupAccessory.m +++ b/Source/SPEncodingPopupAccessory.m @@ -29,13 +29,17 @@ + (NSView *)encodingAccessory:(NSUInteger)encoding includeDefaultEntry:(BOOL)includeDefaultItem encodingPopUp:(NSPopUpButton **)popup { SPEncodingPopupAccessory *owner = [[[SPEncodingPopupAccessory alloc] init] autorelease]; + // Rather than caching, load the accessory view everytime, as it might appear in multiple panels simultaneously. if (![NSBundle loadNibNamed:@"EncodingPopupView" owner:owner]) { NSLog(@"Failed to load EncodingPopupView.nib"); return nil; } + if (popup) *popup = owner->encodingPopUp; + [[self class] setupPopUp:owner->encodingPopUp selectedEncoding:encoding withDefaultEntry:includeDefaultItem]; + return [owner->encodingAccessoryView autorelease]; } @@ -55,17 +59,16 @@ static int encodingCompare(const void *firstPtr, const void *secondPtr) if (first == second) return 0; if (macEncodingForFirst == kCFStringEncodingUnicode || macEncodingForSecond == kCFStringEncodingUnicode) { - if (macEncodingForSecond == macEncodingForFirst) + if (macEncodingForSecond == macEncodingForFirst) { // Both Unicode; compare second order return (first > second) ? 1 : -1; + } + // First is Unicode return (macEncodingForFirst == kCFStringEncodingUnicode) ? -1 : 1; } - if ((macEncodingForFirst > macEncodingForSecond) || ((macEncodingForFirst == macEncodingForSecond) && (first > second))) - return 1; - - return -1; + return ((macEncodingForFirst > macEncodingForSecond) || ((macEncodingForFirst == macEncodingForSecond) && (first > second))) ? 1 : -1; } /** @@ -80,18 +83,29 @@ static int encodingCompare(const void *firstPtr, const void *secondPtr) const CFStringEncoding *cfEncodings = CFStringGetListOfAvailableEncodings(); CFStringEncoding *tmp; NSInteger cnt, num = 0; + while (cfEncodings[num] != kCFStringEncodingInvalidId) num++; + tmp = malloc(sizeof(CFStringEncoding) * num); + memcpy(tmp, cfEncodings, sizeof(CFStringEncoding) * num); + qsort(tmp, num, sizeof(CFStringEncoding), encodingCompare); + allEncodings = [[NSMutableArray alloc] init]; - for (cnt = 0; cnt < num; cnt++) { + + for (cnt = 0; cnt < num; cnt++) + { NSStringEncoding nsEncoding = CFStringConvertEncodingToNSStringEncoding(tmp[cnt]); - if (nsEncoding && [NSString localizedNameOfStringEncoding:nsEncoding]) + + if (nsEncoding && [NSString localizedNameOfStringEncoding:nsEncoding]) { [allEncodings addObject:[NSNumber numberWithUnsignedInteger:nsEncoding]]; + } } + free(tmp); } + return allEncodings; } @@ -114,11 +128,14 @@ static int encodingCompare(const void *firstPtr, const void *secondPtr) numEncodings = [encs count]; // Fill with encodings - for (cnt = 0; cnt < numEncodings; cnt++) { + for (cnt = 0; cnt < numEncodings; cnt++) + { NSStringEncoding enc = [[encs objectAtIndex:cnt] unsignedIntegerValue]; + [popup addItemWithTitle:[NSString localizedNameOfStringEncoding:enc]]; [[popup lastItem] setTag:enc]; [[popup lastItem] setEnabled:YES]; + if (enc == selectedEncoding) itemToSelect = [popup numberOfItems] - 1; } diff --git a/Source/SPFileHandle.m b/Source/SPFileHandle.m index 41c15b09..3410a7e5 100644 --- a/Source/SPFileHandle.m +++ b/Source/SPFileHandle.m @@ -32,7 +32,7 @@ // waits until some has been written out. This can affect speed and memory usage. #define SPFH_MAX_WRITE_BUFFER_SIZE 1048576 -@interface SPFileHandle (PrivateAPI) +@interface SPFileHandle () - (void)_writeBufferToData; @@ -41,7 +41,6 @@ @implementation SPFileHandle #pragma mark - -#pragma mark Setup and teardown /** * Initialises and returns a SPFileHandle with a specified file (FILE, gzFile or BZFILE). @@ -146,19 +145,6 @@ return self; } -/** - * Dealloc. - */ -- (void)dealloc -{ - [self closeFile]; - if (processingThread) [processingThread release]; - free(wrappedFilePath); - [buffer release]; - pthread_mutex_destroy(&bufferLock); - [super dealloc]; -} - #pragma mark - #pragma mark Class methods @@ -210,22 +196,22 @@ */ - (NSMutableData *)readDataOfLength:(NSUInteger)length { - long theDataLength = 0; - void *theData = malloc(length); + long dataLength = 0; + void *data = malloc(length); if (useCompression) { if (compressionFormat == SPGzipCompression) { - theDataLength = gzread(wrappedFile, theData, (unsigned)length); + dataLength = gzread(wrappedFile, data, (unsigned)length); } else if (compressionFormat == SPBzip2Compression) { - theDataLength = BZ2_bzread(wrappedFile, theData, (int)length); + dataLength = BZ2_bzread(wrappedFile, data, (int)length); } } else { - theDataLength = fread(theData, 1, length, wrappedFile); + dataLength = fread(data, 1, length, wrappedFile); } - return [NSMutableData dataWithBytesNoCopy:theData length:theDataLength freeWhenDone:YES]; + return [NSMutableData dataWithBytesNoCopy:data length:dataLength freeWhenDone:YES]; } /** @@ -316,11 +302,13 @@ } // If the buffer is large, wait for some to be written out - while (bufferDataLength > SPFH_MAX_WRITE_BUFFER_SIZE) { + while (bufferDataLength > SPFH_MAX_WRITE_BUFFER_SIZE) + { pthread_mutex_unlock(&bufferLock); usleep(100); pthread_mutex_lock(&bufferLock); } + pthread_mutex_unlock(&bufferLock); } @@ -330,11 +318,14 @@ - (void)synchronizeFile { pthread_mutex_lock(&bufferLock); - while (!allDataWritten) { + + while (!allDataWritten) + { pthread_mutex_unlock(&bufferLock); usleep(100); pthread_mutex_lock(&bufferLock); } + pthread_mutex_unlock(&bufferLock); } @@ -389,10 +380,6 @@ return compressionFormat; } -@end - -@implementation SPFileHandle (PrivateAPI) - /** * A method to be called on a background thread, allowing write data to build * up in a buffer and write to disk in chunks as the buffer fills. This allows @@ -407,6 +394,7 @@ // Check whether any data in the buffer needs to be written out - using thread locks for safety pthread_mutex_lock(&bufferLock); + if (!bufferDataLength) { pthread_mutex_unlock(&bufferLock); usleep(1000); @@ -415,8 +403,10 @@ // Copy the data into a local buffer NSData *dataToBeWritten = [buffer copy]; + [buffer setLength:0]; bufferDataLength = 0; + pthread_mutex_unlock(&bufferLock); // Write out the data @@ -441,17 +431,19 @@ // Restore data to the buffer if it wasn't written out pthread_mutex_lock(&bufferLock); + if (bufferLengthWrittenOut < (NSInteger)[dataToBeWritten length]) { if ([buffer length]) { long dataLengthToRestore = [dataToBeWritten length] - bufferLengthWrittenOut; [buffer replaceBytesInRange:NSMakeRange(0, 0) withBytes:[[dataToBeWritten subdataWithRange:NSMakeRange(bufferLengthWrittenOut, dataLengthToRestore)] bytes] length:dataLengthToRestore]; bufferDataLength += dataLengthToRestore; } - + } // Otherwise, mark all data as written if it has been - allows synching to hard disk. - } else if (![buffer length]) { + else if (![buffer length]) { allDataWritten = YES; } + pthread_mutex_unlock(&bufferLock); [dataToBeWritten release]; @@ -460,4 +452,23 @@ [writePool drain]; } +#pragma mark - + +/** + * Dealloc. + */ +- (void)dealloc +{ + [self closeFile]; + + if (processingThread) [processingThread release]; + + free(wrappedFilePath); + [buffer release]; + + pthread_mutex_destroy(&bufferLock); + + [super dealloc]; +} + @end |