From c31205a4616fc5828f5a093d85a66f56fe477676 Mon Sep 17 00:00:00 2001 From: CaS Date: Tue, 17 Jan 2006 15:07:23 +0000 Subject: [PATCH] Make plget a bit more flexible. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@22322 72102866-910b-0410-8b05-ffd578937521 --- Tools/pldes.1 | 3 ++- Tools/plget.m | 26 +++++++++++++++++++++----- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/Tools/pldes.1 b/Tools/pldes.1 index d79b551c4..86ac3baef 100644 --- a/Tools/pldes.1 +++ b/Tools/pldes.1 @@ -15,7 +15,7 @@ pl, pldes, plser, plmerge, plparse, pl2link \- property list tools .nf .BI "pldes " "filename(s)" .nl -.BI "plget " "key" +.BI "plget " "key" [ more keys ] .nl .BI "plser " "filename(s)" .nl @@ -48,6 +48,7 @@ representation. Reads a text representation of a dictionary in property list format as standard input, extracts the string value held in that dictionary with the specified key, and writes the result to standard output. +Multiple keys may be used to extract values from nested dictionaries. .IP "\fBplser\fR \fIfilename(s)\fR" 4 Converts a text representation of a property list to a binary serialized representation. diff --git a/Tools/plget.m b/Tools/plget.m index f69aa059a..c3ea8ce84 100644 --- a/Tools/plget.m +++ b/Tools/plget.m @@ -33,9 +33,13 @@ /**

This tool extracts a string value from a dictionary in a property list representation.
- It takes a single argument (the key to be extracted).
+ It takes one or more argument (the key to be extracted).
It expects to read the property list from STDIN.
It writes the string value (if any) on STDOUT
+ Where multiple keys are specified, they are used to extract nested + values from dictionaries within the outermost dictionary.
+ Where the resulting object exists and is not a string, + its description is written to STDOUT.

*/ int main(int argc, char** argv, char **env) @@ -44,6 +48,7 @@ main(int argc, char** argv, char **env) NSProcessInfo *proc; NSArray *args; int status = EXIT_SUCCESS; + int count; #ifdef GS_PASS_ARGUMENTS [NSProcessInfo initializeWithArguments:argv count:argc environment:env]; @@ -59,7 +64,7 @@ main(int argc, char** argv, char **env) args = [proc arguments]; - if ([args count] <= 1) + if ((count = [args count]) <= 1) { NSLog(@"plget: no key given to get."); RELEASE(pool); @@ -71,11 +76,13 @@ main(int argc, char** argv, char **env) NSData *inputData; NSString *inputString; NSDictionary *dictionary; - NSString *value; NSData *outputData; NS_DURING { + int i = 1; + id value; + fileHandle = [NSFileHandle fileHandleWithStandardInput]; inputData = [fileHandle readDataToEndOfFile]; inputString = [[NSString alloc] initWithData: inputData @@ -85,9 +92,18 @@ main(int argc, char** argv, char **env) inputString = [[NSString alloc] initWithData: inputData encoding: [NSString defaultCStringEncoding]]; } - dictionary = [inputString propertyList]; + value = [inputString propertyList]; RELEASE(inputString); - value = [dictionary objectForKey: [args objectAtIndex: 1]]; + while (i < count-1) + { + value = [(NSDictionary*)value objectForKey: + [args objectAtIndex: i++]]; + } + value = [(NSDictionary*)value objectForKey: [args objectAtIndex: i]]; + if ([value isKindOfClass: [NSString class]] == NO) + { + value = [value description]; + } outputData = [value dataUsingEncoding: [NSString defaultCStringEncoding]]; if ([outputData length] > 0)