Memory leak fix and tidyup

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@20360 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
CaS 2004-11-19 09:41:34 +00:00
parent 1ddff14944
commit 5e95fdf285
3 changed files with 131 additions and 110 deletions

View file

@ -29,134 +29,139 @@
#import <Foundation/Foundation.h>
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 <serialized_file>} {-output <serialized_file>}");
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 <serialized_file>} {-output <serialized_file>}");
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.
}