2011-07-27 20:56:53 +02:00
|
|
|
/* plreader.m - this file is part of SOGo
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Inverse inc
|
|
|
|
*
|
|
|
|
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
|
|
|
|
*
|
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* A format-agnostic property list dumper.
|
|
|
|
Usage: plreader [filename] */
|
|
|
|
|
|
|
|
#import <Foundation/NSAutoreleasePool.h>
|
|
|
|
#import <Foundation/NSArray.h>
|
|
|
|
#import <Foundation/NSData.h>
|
|
|
|
#import <Foundation/NSDictionary.h>
|
|
|
|
#import <Foundation/NSProcessInfo.h>
|
|
|
|
#import <Foundation/NSPropertyList.h>
|
2011-08-12 16:28:36 +02:00
|
|
|
#import <Foundation/NSString.h>
|
|
|
|
#import <Foundation/NSValue.h>
|
2011-07-27 20:56:53 +02:00
|
|
|
#import <NGExtensions/NSNull+misc.h>
|
|
|
|
|
|
|
|
const char *indentationStep = " ";
|
|
|
|
|
|
|
|
@interface NSObject (plext)
|
|
|
|
|
|
|
|
- (void) displayWithIndentation: (NSInteger) anInt;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSObject (plext)
|
|
|
|
|
|
|
|
- (void) _outputIndentation: (NSInteger) anInt
|
|
|
|
{
|
|
|
|
NSInteger i;
|
|
|
|
|
|
|
|
for (i = 0; i < anInt; i++)
|
|
|
|
printf ("%s", indentationStep);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) displayWithIndentation: (NSInteger) anInt
|
|
|
|
{
|
2011-08-12 16:28:36 +02:00
|
|
|
printf ("(%s) %s",
|
2011-07-27 20:56:53 +02:00
|
|
|
[NSStringFromClass (isa) UTF8String],
|
|
|
|
[[self description] UTF8String]);
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSDictionary (plext)
|
|
|
|
|
|
|
|
- (void) displayKey: (NSString *) key
|
|
|
|
withIndentation: (NSInteger) anInt
|
|
|
|
{
|
|
|
|
[self _outputIndentation: anInt];
|
2011-08-12 07:31:29 +02:00
|
|
|
|
2011-08-12 16:28:36 +02:00
|
|
|
printf ("%s ", [[key description] UTF8String]);
|
|
|
|
if ([key isKindOfClass: [NSValue class]])
|
|
|
|
printf ("(%s: 0x%.8x) ", [(NSValue *) key objCType], [key intValue]);
|
|
|
|
|
|
|
|
printf ("= ");
|
2011-07-27 20:56:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) displayWithIndentation: (NSInteger) anInt
|
|
|
|
{
|
|
|
|
NSUInteger i, max;
|
|
|
|
NSArray *keys;
|
|
|
|
NSInteger subIndent;
|
|
|
|
NSString *key;
|
|
|
|
|
|
|
|
keys = [self allKeys];
|
|
|
|
max = [keys count];
|
|
|
|
|
2011-07-29 16:16:53 +02:00
|
|
|
printf ("{ (%ld) items\n", (long) max);
|
2011-07-27 20:56:53 +02:00
|
|
|
|
|
|
|
subIndent = anInt + 1;
|
|
|
|
|
|
|
|
for (i = 0; i < max; i++)
|
|
|
|
{
|
|
|
|
key = [keys objectAtIndex: i];
|
|
|
|
[self displayKey: key withIndentation: subIndent];
|
2011-08-12 16:28:36 +02:00
|
|
|
[[self objectForKey: key] displayWithIndentation: subIndent];
|
|
|
|
if (i < (max - 1))
|
|
|
|
printf (",");
|
|
|
|
printf ("\n");
|
2011-07-27 20:56:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[self _outputIndentation: anInt];
|
2011-08-12 16:28:36 +02:00
|
|
|
printf ("}");
|
2011-07-27 20:56:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSArray (plext)
|
|
|
|
|
|
|
|
- (void) displayCount: (NSUInteger) count
|
|
|
|
withIndentation: (NSInteger) anInt
|
|
|
|
{
|
|
|
|
[self _outputIndentation: anInt];
|
2011-08-12 16:28:36 +02:00
|
|
|
printf ("%lu = ", (unsigned long) count);
|
2011-07-27 20:56:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) displayWithIndentation: (NSInteger) anInt
|
|
|
|
{
|
|
|
|
NSUInteger i, max;
|
|
|
|
NSInteger subIndent;
|
|
|
|
|
|
|
|
max = [self count];
|
|
|
|
|
2011-07-29 16:16:53 +02:00
|
|
|
printf ("[ (%ld) items\n", (long) max);
|
2011-07-27 20:56:53 +02:00
|
|
|
|
|
|
|
subIndent = anInt + 1;
|
|
|
|
|
|
|
|
for (i = 0; i < max; i++)
|
|
|
|
{
|
|
|
|
[self displayCount: i withIndentation: subIndent];
|
2011-08-12 16:28:36 +02:00
|
|
|
[[self objectAtIndex: i] displayWithIndentation: subIndent];
|
|
|
|
if (i < (max - 1))
|
|
|
|
printf (",");
|
|
|
|
printf ("\n");
|
2011-07-27 20:56:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[self _outputIndentation: anInt];
|
2011-08-12 16:28:36 +02:00
|
|
|
printf ("]");
|
2011-07-27 20:56:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
static void
|
|
|
|
PLReaderDumpPListFile (NSString *filename)
|
|
|
|
{
|
|
|
|
NSData *content;
|
|
|
|
NSDictionary *d;
|
|
|
|
NSPropertyListFormat format;
|
|
|
|
NSString *error = nil;
|
|
|
|
const char *formatName;
|
|
|
|
|
|
|
|
content = [NSData dataWithContentsOfFile: filename];
|
|
|
|
d = [NSPropertyListSerialization propertyListFromData: content
|
|
|
|
mutabilityOption: NSPropertyListImmutable
|
|
|
|
format: &format
|
|
|
|
errorDescription: &error];
|
2011-10-22 01:44:29 +02:00
|
|
|
if (d)
|
2011-07-27 20:56:53 +02:00
|
|
|
{
|
|
|
|
switch (format)
|
|
|
|
{
|
|
|
|
case NSPropertyListOpenStepFormat:
|
|
|
|
formatName = "OpenStep";
|
|
|
|
break;
|
|
|
|
case NSPropertyListXMLFormat_v1_0:
|
|
|
|
formatName = "XML";
|
|
|
|
break;
|
|
|
|
case NSPropertyListBinaryFormat_v1_0:
|
|
|
|
formatName = "Binary";
|
|
|
|
break;
|
|
|
|
case NSPropertyListGNUstepFormat:
|
|
|
|
formatName = "GNUstep";
|
|
|
|
break;
|
|
|
|
case NSPropertyListGNUstepBinaryFormat:
|
|
|
|
formatName = "GNUstep binary";
|
|
|
|
break;
|
|
|
|
default: formatName = "unknown";
|
|
|
|
}
|
|
|
|
|
|
|
|
printf ("File format is: %s\n", formatName);
|
|
|
|
[d displayWithIndentation: 0];
|
2011-08-12 16:28:36 +02:00
|
|
|
printf ("\n");
|
2011-07-27 20:56:53 +02:00
|
|
|
}
|
2011-10-22 01:44:29 +02:00
|
|
|
else
|
|
|
|
printf ("an error occurred: %s\n", [error UTF8String]);
|
2011-07-27 20:56:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
NSAutoreleasePool *p;
|
|
|
|
NSProcessInfo *pi;
|
|
|
|
NSArray *arguments;
|
|
|
|
|
|
|
|
p = [NSAutoreleasePool new];
|
|
|
|
pi = [NSProcessInfo processInfo];
|
|
|
|
arguments = [pi arguments];
|
|
|
|
if ([arguments count] > 1)
|
|
|
|
PLReaderDumpPListFile ([arguments objectAtIndex: 1]);
|
|
|
|
[p release];
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|