Skip to content

Commit

Permalink
Make plget a bit more flexible.
Browse files Browse the repository at this point in the history
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@22322 72102866-910b-0410-8b05-ffd578937521
  • Loading branch information
rfm committed Jan 17, 2006
1 parent 841527e commit acdec21
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Tools/pldes.1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
26 changes: 21 additions & 5 deletions Tools/plget.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@

/** <p> This tool extracts a string value from a dictionary in a property
list representation.<br />
It takes a single argument (the key to be extracted).<br />
It takes one or more argument (the key to be extracted).<br />
It expects to read the property list from STDIN.<br />
It writes the string value (if any) on STDOUT<br />
Where multiple keys are specified, they are used to extract nested
values from dictionaries within the outermost dictionary.<br />
Where the resulting object exists and is not a string,
its description is written to STDOUT.
</p> */
int
main(int argc, char** argv, char **env)
Expand All @@ -44,6 +48,7 @@ It writes the string value (if any) on STDOUT<br />
NSProcessInfo *proc;
NSArray *args;
int status = EXIT_SUCCESS;
int count;

#ifdef GS_PASS_ARGUMENTS
[NSProcessInfo initializeWithArguments:argv count:argc environment:env];
Expand All @@ -59,7 +64,7 @@ It writes the string value (if any) on STDOUT<br />

args = [proc arguments];

if ([args count] <= 1)
if ((count = [args count]) <= 1)
{
NSLog(@"plget: no key given to get.");
RELEASE(pool);
Expand All @@ -71,11 +76,13 @@ It writes the string value (if any) on STDOUT<br />
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
Expand All @@ -85,9 +92,18 @@ It writes the string value (if any) on STDOUT<br />
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)
Expand Down

0 comments on commit acdec21

Please sign in to comment.