diff --git a/ChangeLog b/ChangeLog index efbe95733..e45bd0578 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2004-11-19 Richard Frith-Macdonald + + * Tools/pl.m: Make code conform to gnu coding standards. + * Source/NSRunLoop.m: ([runMode:beforeDate:]) add autorelease pool + to retain old behavior where anything inside a runloop is + automatically in the scope of an autorelease pool, and will not + be leaked by autoreleasing. Fixes obscure problems with the last + limitDateForMode changes. + 2004-11-18 Gregory John Casamento * Tools/pl.m: Added a tool I wrote a while back which is an diff --git a/Source/NSRunLoop.m b/Source/NSRunLoop.m index 9d1b2f820..2d4532478 100644 --- a/Source/NSRunLoop.m +++ b/Source/NSRunLoop.m @@ -1724,7 +1724,7 @@ static void setPollfd(int fd, int event, GSRunLoopCtxt *ctxt) */ - (NSDate*) limitDateForMode: (NSString*)mode { - extern NSTimer *GSHousekeeper(void); + extern NSTimer *GSHousekeeper(void); GSRunLoopCtxt *context = NSMapGet(_contextMap, mode); NSDate *when = nil; @@ -1742,7 +1742,8 @@ static void setPollfd(int fd, int event, GSRunLoopCtxt *ctxt) while (GSIArrayCount(timers) != 0) { - NSTimer *min_timer = GSIArrayItemAtIndex(timers, 0).obj; + NSTimer *min_timer = GSIArrayItemAtIndex(timers, 0).obj; + if (timerInvalidated(min_timer) == YES) { GSIArrayRemoveItemAtIndex(timers, 0); @@ -1750,9 +1751,10 @@ static void setPollfd(int fd, int event, GSRunLoopCtxt *ctxt) continue; } - if (!when) - when = [timerDate(min_timer) copy]; - + if (when == nil) + { + when = [timerDate(min_timer) copy]; + } if ([timerDate(min_timer) timeIntervalSinceNow] > 0) { break; @@ -1849,13 +1851,14 @@ static void setPollfd(int fd, int event, GSRunLoopCtxt *ctxt) * that the housekeeper timer does not keep the runloop * active. It's a special case set up in NSThread.m */ - if (min_watcher == nil && when) + if (min_watcher == nil && when != nil) { unsigned count = GSIArrayCount(timers); while (count-- > 1) { NSTimer *tmp = GSIArrayItemAtIndex(timers, 0).obj; + if (timerInvalidated(tmp) == YES) { GSIArrayRemoveItemAtIndex(timers, count); @@ -1863,7 +1866,7 @@ static void setPollfd(int fd, int event, GSRunLoopCtxt *ctxt) } if (GSIArrayCount(timers) == 1) { - DESTROY(when); + DESTROY(when); } } @@ -1885,7 +1888,7 @@ static void setPollfd(int fd, int event, GSRunLoopCtxt *ctxt) * watcher (or leave it as the date of the earliest timer if that is * before the watchers limit). */ - if (when) + if (when != nil) { if (min_watcher != nil && [min_watcher->_date compare: when] == NSOrderedAscending) @@ -1894,7 +1897,9 @@ static void setPollfd(int fd, int event, GSRunLoopCtxt *ctxt) when = min_watcher->_date; } else - AUTORELEASE(when); + { + AUTORELEASE(when); + } } else if (min_watcher != nil) { @@ -2030,7 +2035,8 @@ static void setPollfd(int fd, int event, GSRunLoopCtxt *ctxt) */ - (BOOL) runMode: (NSString*)mode beforeDate: (NSDate*)date { - id d; + CREATE_AUTORELEASE_POOL(arp); + NSDate *d; NSAssert(mode != nil, NSInvalidArgumentException); @@ -2046,6 +2052,7 @@ static void setPollfd(int fd, int event, GSRunLoopCtxt *ctxt) { GSNotifyASAP(); } + RELEASE(arp); return NO; } @@ -2064,7 +2071,7 @@ static void setPollfd(int fd, int event, GSRunLoopCtxt *ctxt) [self acceptInputForMode: mode beforeDate: d]; RELEASE(d); - + RELEASE(arp); return YES; } diff --git a/Tools/pl.m b/Tools/pl.m index 6dcea92ab..c34356df5 100644 --- a/Tools/pl.m +++ b/Tools/pl.m @@ -29,134 +29,139 @@ #import -void create_output( id propertyList ) +void create_output(id propertyList) { - NSFileHandle *fileHandle = nil; - NSProcessInfo *processInfo = [NSProcessInfo processInfo]; - NSArray *arguments = [processInfo arguments]; - int outputIndex = 0; + NSFileHandle *fileHandle = nil; + NSProcessInfo *processInfo = [NSProcessInfo processInfo]; + NSArray *arguments = [processInfo arguments]; + int outputIndex = 0; - // insert your code here - outputIndex = [arguments indexOfObject: @"-output"]; - if( outputIndex == NSNotFound ) + // insert your code here + outputIndex = [arguments indexOfObject: @"-output"]; + if (outputIndex == NSNotFound) { - const char *buffer = [[propertyList description] cString]; - NSData *outputData = [NSData dataWithBytes: buffer length: strlen(buffer)]; - - // setup the file handle. - fileHandle = [NSFileHandle fileHandleWithStandardOutput]; - // Send the data to stdout - [fileHandle writeData: outputData]; - puts("\n"); + const char *buffer = [[propertyList description] cString]; + NSData *outputData; + + outputData = [NSData dataWithBytes: buffer length: strlen(buffer)]; + // setup the file handle. + fileHandle = [NSFileHandle fileHandleWithStandardOutput]; + // Send the data to stdout + [fileHandle writeData: outputData]; + puts("\n"); } - else + else { - NSData *serializedData = nil; - NSFileManager *fileManager = [NSFileManager defaultManager]; + NSData *serializedData = nil; + NSFileManager *fileManager = [NSFileManager defaultManager]; - // Write in the serialized plist. - serializedData = [NSSerializer serializePropertyList: propertyList]; - [fileManager createFileAtPath: [arguments objectAtIndex: outputIndex+1] - contents: serializedData - attributes: nil]; + // Write in the serialized plist. + serializedData = [NSSerializer serializePropertyList: propertyList]; + [fileManager createFileAtPath: [arguments objectAtIndex: outputIndex+1] + contents: serializedData + attributes: nil]; } } -id process_plist( NSData *inputData ) +id process_plist(NSData *inputData) { - id propertyList = nil; - NSString *string = nil; + id propertyList = nil; + NSString *string = nil; - // Initialize a string with the contents of the file. - string = [NSString stringWithCString: (char *)[inputData bytes]]; + // Initialize a string with the contents of the file. + string = [NSString stringWithCString: (char *)[inputData bytes]]; - // Convert the string into a property list. If there is a parsing error - // the property list interpreter will throw an exception. - NS_DURING - propertyList = [string propertyList]; - NS_HANDLER - NSLog([localException description]); - NS_ENDHANDLER - - // return the results - return propertyList; + // Convert the string into a property list. If there is a parsing error + // the property list interpreter will throw an exception. + NS_DURING + propertyList = [string propertyList]; + NS_HANDLER + NSLog([localException description]); + NS_ENDHANDLER + + // return the results + return propertyList; } NSData *read_input() { - NSData *inputData = nil; - NSFileHandle *fileHandle = nil; - NSProcessInfo *processInfo = [NSProcessInfo processInfo]; - NSArray *arguments = [processInfo arguments]; - int inputIndex = 0; + NSData *inputData = nil; + NSFileHandle *fileHandle = nil; + NSProcessInfo *processInfo = [NSProcessInfo processInfo]; + NSArray *arguments = [processInfo arguments]; + int inputIndex = 0; - // insert your code here - inputIndex = [arguments indexOfObject: @"-input"]; - if( inputIndex == NSNotFound ) + // insert your code here + inputIndex = [arguments indexOfObject: @"-input"]; + if (inputIndex == NSNotFound) { - // setup the file handle. - fileHandle = [NSFileHandle fileHandleWithStandardInput]; - // Read in the input from the file. - inputData = [fileHandle readDataToEndOfFile]; + // setup the file handle. + fileHandle = [NSFileHandle fileHandleWithStandardInput]; + // Read in the input from the file. + inputData = [fileHandle readDataToEndOfFile]; } - else + else { - NSData *serializedData = nil; - id propertyList = nil; - char *buffer = 0; + NSData *serializedData = nil; + id propertyList = nil; + char *buffer = 0; - // set up the file handle. - fileHandle = [NSFileHandle fileHandleForReadingAtPath: [arguments objectAtIndex: inputIndex+1]]; - // read in the serialized plist. - serializedData = [fileHandle readDataToEndOfFile]; - [fileHandle closeFile]; - propertyList = [NSDeserializer deserializePropertyListFromData: serializedData - mutableContainers: NO]; - if( propertyList != nil ) - { - buffer = (char *)[[propertyList description] cString]; - inputData = [NSData dataWithBytes: buffer length: strlen(buffer)]; - } - else - { - NSLog(@"%@ is not a serialized property list.", [arguments objectAtIndex: inputIndex+1]); - } + // set up the file handle. + fileHandle = [NSFileHandle fileHandleForReadingAtPath: + [arguments objectAtIndex: inputIndex+1]]; + // read in the serialized plist. + serializedData = [fileHandle readDataToEndOfFile]; + [fileHandle closeFile]; + propertyList = [NSDeserializer deserializePropertyListFromData: + serializedData mutableContainers: NO]; + if (propertyList != nil) + { + buffer = (char *)[[propertyList description] cString]; + inputData = [NSData dataWithBytes: buffer length: strlen(buffer)]; + } + else + { + NSLog(@"%@ is not a serialized property list.", + [arguments objectAtIndex: inputIndex+1]); + } } - return inputData; + return inputData; } int main (int argc, const char *argv[]) { - NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; - NSData *inputData = nil; - id propertyList = nil; - - // put your code here. - if(argc == 1 || argc == 3|| argc == 5) + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + NSData *inputData = nil; + id propertyList = nil; + + // put your code here. + if (argc == 1 || argc == 3|| argc == 5) { - inputData = read_input(); - if( inputData != nil ) - { - // If the input data was sucessfully read... - propertyList = process_plist( inputData ); - if( propertyList != nil ) - { - // If the property list was okay... - create_output( propertyList ); - } - } + inputData = read_input(); + if (inputData != nil) + { + // If the input data was sucessfully read... + propertyList = process_plist( inputData ); + if (propertyList != nil) + { + // If the property list was okay... + create_output( propertyList ); + } + } } - else + else { - puts("pl {-input } {-output }"); - puts(" - Reads an ASCII property list from standard in, or a serialized one"); - puts(" if -input is specified."); - puts(" - Writes an ASCII propert list to standard out, or a serialized one"); - puts(" if -output is specified."); + puts("pl {-input } {-output }"); + puts( + " - Reads an ASCII property list from standard in, or a serialized one"); + puts(" if -input is specified."); + puts( + " - Writes an ASCII propert list to standard out, or a serialized one"); + puts(" if -output is specified."); } - - [pool release]; - exit(0); // insure the process exit status is 0 - return 0; // ...and make main fit the ANSI spec. + + [pool release]; + exit(0); // insure the process exit status is 0 + return 0; // ...and make main fit the ANSI spec. }