mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-23 00:41:02 +00:00
plutil: get other formats in
This commit is contained in:
parent
319fc47772
commit
ce7ff5709b
4 changed files with 200 additions and 15 deletions
|
@ -97,7 +97,7 @@ plget_OBJC_FILES = plget.m
|
|||
plser_OBJC_FILES = plser.m
|
||||
plmerge_OBJC_FILES = plmerge.m
|
||||
plparse_OBJC_FILES = plparse.m
|
||||
plutil_OBJC_FILES = plutil.m
|
||||
plutil_OBJC_FILES = NSPropertyList+PLUtil.m plutil.m
|
||||
sfparse_OBJC_FILES = sfparse.m
|
||||
pl2link_OBJC_FILES = pl2link.m
|
||||
locale_alias_OBJC_FILES = locale_alias.m
|
||||
|
|
47
Tools/NSPropertyList+PLUtil.h
Normal file
47
Tools/NSPropertyList+PLUtil.h
Normal file
|
@ -0,0 +1,47 @@
|
|||
/** Permit handling JSON as plists, and writing Objective-C literals.
|
||||
Copyright (C) 2020 Free Software Foundation, Inc.
|
||||
|
||||
Written by: Mingye Wang
|
||||
Created: feb 2020
|
||||
|
||||
This file is part of the GNUstep Objective-C Library.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library 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
|
||||
Library General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free
|
||||
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
Boston, MA 02111 USA.
|
||||
|
||||
*/
|
||||
|
||||
#import "Foundation/NSPropertyList.h"
|
||||
|
||||
/** Extra types supported by plutil. */
|
||||
enum _PLUExtentedFormats
|
||||
{
|
||||
NSPropertyListJSONFormat = NSPropertyListBinaryFormat_v1_0 + 100,
|
||||
/** https://clang.llvm.org/docs/ObjectiveCLiterals.html */
|
||||
NSPropertyListObjectiveCFormat,
|
||||
/** https://docs.swift.org/swift-book/ReferenceManual/zzSummaryOfTheGrammar.html */
|
||||
NSPropertyListSwiftFormat,
|
||||
};
|
||||
|
||||
@interface NSPropertyListSerialization (PLUtilAdditions)
|
||||
+ (NSData *)_pdataFromPropertyList:(id)aPropertyList
|
||||
format:(NSPropertyListFormat)aFormat
|
||||
errorDescription:(NSString **)anErrorString;
|
||||
+ (id)_ppropertyListWithData:(NSData *)data
|
||||
options:(NSPropertyListReadOptions)anOption
|
||||
format:(NSPropertyListFormat *)aFormat
|
||||
error:(out NSError **)error;
|
||||
+ (void)load;
|
||||
@end
|
137
Tools/NSPropertyList+PLUtil.m
Normal file
137
Tools/NSPropertyList+PLUtil.m
Normal file
|
@ -0,0 +1,137 @@
|
|||
/** Permit handling JSON as plists, and writing Objective-C literals.
|
||||
Copyright (C) 2020 Free Software Foundation, Inc.
|
||||
|
||||
Written by: Mingye Wang
|
||||
Created: feb 2020
|
||||
|
||||
This file is part of the GNUstep Objective-C Library.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library 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
|
||||
Library General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free
|
||||
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
Boston, MA 02111 USA.
|
||||
|
||||
*/
|
||||
#import "NSPropertyList+PLUtil.h"
|
||||
#import "GNUstepBase/GSObjCRuntime.h"
|
||||
#import "Foundation/NSJSONSerialization.h"
|
||||
|
||||
static IMP originalRead = 0;
|
||||
static IMP originalWrite = 0;
|
||||
|
||||
// The whole PAppend and indent table thing again. Later?
|
||||
static void
|
||||
OAppend(id obj, NSDictionary *loc, unsigned lev, unsigned step,
|
||||
NSPropertyListFormat x, NSMutableData *dest)
|
||||
{
|
||||
// Generate declaration w/ type
|
||||
if (lev == 0)
|
||||
;
|
||||
|
||||
// Main loop
|
||||
|
||||
// Add semicolon
|
||||
if (lev == 0 && x == NSPropertyListObjectiveCFormat)
|
||||
[dest appendBytes:";" length:1];
|
||||
[dest appendBytes:"\n" length:1];
|
||||
}
|
||||
|
||||
@implementation NSPropertyListSerialization (PLUtilAdditions)
|
||||
+ (NSData *)_pdataFromPropertyList:(id)aPropertyList
|
||||
format:(NSPropertyListFormat)aFormat
|
||||
errorDescription:(NSString **)anErrorString
|
||||
{
|
||||
NSError * myError = nil;
|
||||
NSData * dest;
|
||||
NSDictionary *loc;
|
||||
switch (aFormat)
|
||||
{
|
||||
case NSPropertyListJSONFormat:
|
||||
dest = [NSJSONSerialization dataWithJSONObject:aPropertyList
|
||||
options:NSJSONWritingPrettyPrinted
|
||||
error:&myError];
|
||||
if (myError != nil && anErrorString != NULL)
|
||||
{
|
||||
*anErrorString = [myError description];
|
||||
}
|
||||
return dest;
|
||||
case NSPropertyListObjectiveCFormat:
|
||||
case NSPropertyListSwiftFormat:
|
||||
loc = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
|
||||
dest = [NSMutableData dataWithCapacity:1024];
|
||||
OAppend(aPropertyList, loc, 0, 2, aFormat, dest);
|
||||
return dest;
|
||||
default:
|
||||
return (*originalImp)(self, _cmd, aPropertyList, aFormat, anErrorString);
|
||||
}
|
||||
}
|
||||
|
||||
+ (id)_ppropertyListWithData:(NSData *)data
|
||||
options:(NSPropertyListReadOptions)anOption
|
||||
format:(NSPropertyListFormat *)aFormat
|
||||
error:(out NSError **)error;
|
||||
{
|
||||
NSError * myError = nil;
|
||||
NSPropertyListFormat format;
|
||||
NSJSONReadingOptions jsonOptions = NSJSONReadingAllowFragments;
|
||||
id prop = (*originalImp)(self, _cmd, data, anOption, &format, &myError);
|
||||
if (prop == nil)
|
||||
if (format == NSPropertyListOpenStepFormat
|
||||
|| format == NSPropertyListGNUstepFormat)
|
||||
// rescue as json when we know it is not anything else
|
||||
{
|
||||
switch (anOption)
|
||||
{
|
||||
case NSPropertyListMutableContainersAndLeaves:
|
||||
jsonOptions |= NSJSONReadingMutableLeaves;
|
||||
/* FALLTHROUGH */
|
||||
case NSPropertyListMutableContainers:
|
||||
jsonOptions |= NSJSONReadingMutableContainers;
|
||||
}
|
||||
format = NSPropertyListJSONFormat;
|
||||
prop = [NSJSONSerialization JSONObjectWithData:data
|
||||
options:jsonOptions
|
||||
error:&myError];
|
||||
}
|
||||
if (error != nil)
|
||||
*error = *myError;
|
||||
if (*aFormat != nil)
|
||||
*aFormat = format;
|
||||
return prop;
|
||||
}
|
||||
|
||||
+ (void)load
|
||||
{
|
||||
Method replacementRead;
|
||||
Method replacementWrite;
|
||||
|
||||
replacementRead
|
||||
= class_getClassMethod(self, @selector
|
||||
(_ppropertyListWithData:options:format:error:));
|
||||
replacementWrite
|
||||
= class_getClassMethod(self, @selector
|
||||
(_pdataFromPropertyList:format:errorDescription:));
|
||||
|
||||
originalRead
|
||||
= class_replaceMethod(object_getClass(self),
|
||||
@selector(propertyListWithData:options:format:error:),
|
||||
method_getImplementation(replacementRead),
|
||||
method_getTypeEncoding(replacementRead));
|
||||
originalWrite
|
||||
= class_replaceMethod(object_getClass(self),
|
||||
@selector(dataFromPropertyList:
|
||||
format:errorDescription:),
|
||||
method_getImplementation(replacementWrite),
|
||||
method_getTypeEncoding(replacementWrite));
|
||||
}
|
||||
@end
|
|
@ -32,6 +32,7 @@
|
|||
#import "Foundation/NSString.h"
|
||||
#import "Foundation/NSUserDefaults.h"
|
||||
#import "Foundation/NSValue.h"
|
||||
#import "NSPropertyList+PLutil.h"
|
||||
|
||||
// From NSPropertyList.m
|
||||
extern void
|
||||
|
@ -296,6 +297,7 @@ plFormatFromName(NSString *name)
|
|||
NINT(NSPropertyListOpenStepFormat), @"openstep",
|
||||
NINT(NSPropertyListGNUstepFormat), @"gnustep",
|
||||
NINT(NSPropertyListGNUstepBinaryFormat), @"gsbinary",
|
||||
NINT(NSPropertyListJSONFormat), @"json",
|
||||
SELFMAP(NSPropertyListOpenStepFormat),
|
||||
SELFMAP(NSPropertyListXMLFormat_v1_0),
|
||||
SELFMAP(NSPropertyListBinaryFormat_v1_0),
|
||||
|
@ -406,7 +408,8 @@ print_help(FILE *f)
|
|||
GSPrintf(f, @"Property list utility\n");
|
||||
GSPrintf(f, @"Usage: plutil [command] [options] file\n\n");
|
||||
GSPrintf(f, @"Accepted commands:\n");
|
||||
GSPrintf(f, @"-p\tPrints the plists in a human-readable form (GNUstep ASCII).");
|
||||
GSPrintf(
|
||||
f, @" -p\tPrints the plists in a human-readable form (GNUstep ASCII).\n");
|
||||
GSPrintf(f, @"Accepted options:\n");
|
||||
}
|
||||
|
||||
|
@ -465,19 +468,17 @@ main(int argc, char **argv, char **env)
|
|||
|
||||
// Parse the COMMAND.
|
||||
// Maps number of args to commands.
|
||||
commands = [NSDictionary
|
||||
dictionaryWithObjectsAndKeys:NARRAY(NINT(ACTION_PRINT), NINT(0)), @"-p",
|
||||
NARRAY(NINT(ACTION_LINT), NINT(0)), @"-lint",
|
||||
NARRAY(NINT(ACTION_CONVERT), NINT(1)),
|
||||
@"-convert",
|
||||
NARRAY(NINT(ACTION_INSERT), NINT(3)),
|
||||
@"-insert",
|
||||
NARRAY(NINT(ACTION_REPLACE), NINT(3)),
|
||||
@"-replace",
|
||||
NARRAY(NINT(ACTION_REMOVE), NINT(1)),
|
||||
@"-remove",
|
||||
NARRAY(NINT(ACTION_EXTRACT), NINT(2)),
|
||||
@"-extract", nil];
|
||||
// clang-format off
|
||||
commands = [NSDictionary dictionaryWithObjectsAndKeys:
|
||||
NARRAY(NINT(ACTION_PRINT), NINT(0)), @"-p",
|
||||
NARRAY(NINT(ACTION_LINT), NINT(0)), @"-lint",
|
||||
NARRAY(NINT(ACTION_CONVERT), NINT(1)), @"-convert",
|
||||
NARRAY(NINT(ACTION_INSERT), NINT(3)), @"-insert",
|
||||
NARRAY(NINT(ACTION_REPLACE), NINT(3)), @"-replace",
|
||||
NARRAY(NINT(ACTION_REMOVE), NINT(1)), @"-remove",
|
||||
NARRAY(NINT(ACTION_EXTRACT), NINT(2)), @"-extract",
|
||||
nil];
|
||||
// clang-format on
|
||||
NS_DURING
|
||||
{
|
||||
NSData * fileData;
|
||||
|
|
Loading…
Reference in a new issue