trivial format fixes

This commit is contained in:
Richard Frith-Macdonald 2020-11-07 16:02:15 +00:00
parent 7ecb170800
commit 5dbd0a7da5
2 changed files with 174 additions and 174 deletions

View file

@ -1,47 +1,47 @@
/** Permit handling JSON as plists, and writing Objective-C literals. /** Permit handling JSON as plists, and writing Objective-C literals.
Copyright (C) 2020 Free Software Foundation, Inc. Copyright (C) 2020 Free Software Foundation, Inc.
Written by: Mingye Wang Written by: Mingye Wang
Created: feb 2020 Created: feb 2020
This file is part of the GNUstep Objective-C Library. This file is part of the GNUstep Objective-C Library.
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version. version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details. Library General Public License for more details.
You should have received a copy of the GNU Lesser General Public You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02111 USA. Boston, MA 02111 USA.
*/ */
#import "Foundation/NSPropertyList.h" #import "Foundation/NSPropertyList.h"
/** Extra types supported by plutil. */ /** Extra types supported by plutil. */
enum _PLUExtentedFormats enum _PLUExtentedFormats
{ {
NSPropertyListJSONFormat = NSPropertyListBinaryFormat_v1_0 + 100, NSPropertyListJSONFormat = NSPropertyListBinaryFormat_v1_0 + 100,
/** https://clang.llvm.org/docs/ObjectiveCLiterals.html */ /** https://clang.llvm.org/docs/ObjectiveCLiterals.html */
NSPropertyListObjectiveCFormat, NSPropertyListObjectiveCFormat,
/** https://docs.swift.org/swift-book/ReferenceManual/zzSummaryOfTheGrammar.html */ /** https://docs.swift.org/swift-book/ReferenceManual/zzSummaryOfTheGrammar.html */
NSPropertyListSwiftFormat, NSPropertyListSwiftFormat,
}; };
@interface NSPropertyListSerialization (PLUtilAdditions) @interface NSPropertyListSerialization (PLUtilAdditions)
+ (NSData *)_pdataFromPropertyList:(id)aPropertyList + (NSData *) _pdataFromPropertyList: (id)aPropertyList
format:(NSPropertyListFormat)aFormat format: (NSPropertyListFormat)aFormat
errorDescription:(NSString **)anErrorString; errorDescription: (NSString **)anErrorString;
+ (id)_ppropertyListWithData:(NSData *)data + (id) _ppropertyListWithData: (NSData *)data
options:(NSPropertyListReadOptions)anOption options: (NSPropertyListReadOptions)anOption
format:(NSPropertyListFormat *)aFormat format: (NSPropertyListFormat *)aFormat
error:(out NSError **)error; error: (out NSError **)error;
+ (void)load; + (void) load;
@end @end

View file

@ -1,127 +1,127 @@
/** Permit handling JSON as plists, and writing Objective-C literals. /** Permit handling JSON as plists, and writing Objective-C literals.
Copyright (C) 2020 Free Software Foundation, Inc. Copyright (C) 2020 Free Software Foundation, Inc.
Written by: Mingye Wang Written by: Mingye Wang
Created: feb 2020 Created: feb 2020
This file is part of the GNUstep Objective-C Library. This file is part of the GNUstep Objective-C Library.
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version. version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details. Library General Public License for more details.
You should have received a copy of the GNU Lesser General Public You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02111 USA. Boston, MA 02111 USA.
*/ */
#import "NSPropertyList+PLUtil.h" #import "NSPropertyList+PLUtil.h"
#import "GNUstepBase/GSObjCRuntime.h" #import "GNUstepBase/GSObjCRuntime.h"
#import "Foundation/NSData.h" #import "Foundation/NSData.h"
#import "Foundation/NSUserDefaults.h" #import "Foundation/NSUserDefaults.h"
#import "Foundation/NSJSONSerialization.h" #import "Foundation/NSJSONSerialization.h"
static IMP originalRead = 0; static IMP originalRead = 0;
static IMP originalWrite = 0; static IMP originalWrite = 0;
@implementation NSPropertyListSerialization (PLUtilAdditions) @implementation NSPropertyListSerialization (PLUtilAdditions)
+ (NSData*) _pdataFromPropertyList: (id)aPropertyList + (NSData*) _pdataFromPropertyList: (id)aPropertyList
format: (NSPropertyListFormat)aFormat format: (NSPropertyListFormat)aFormat
errorDescription: (NSString **)anErrorString errorDescription: (NSString **)anErrorString
{ {
NSError *myError = nil; NSError *myError = nil;
NSData *dest; NSData *dest;
NSDictionary *loc; NSDictionary *loc;
loc = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]; loc = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
switch (aFormat) switch (aFormat)
{ {
case NSPropertyListJSONFormat: case NSPropertyListJSONFormat:
dest = [NSJSONSerialization dest = [NSJSONSerialization
dataWithJSONObject: aPropertyList dataWithJSONObject: aPropertyList
options: loc != nil ? NSJSONWritingPrettyPrinted : 0 options: loc != nil ? NSJSONWritingPrettyPrinted : 0
error: &myError]; error: &myError];
if (myError != nil && anErrorString != NULL) if (myError != nil && anErrorString != NULL)
{ {
*anErrorString = [myError description]; *anErrorString = [myError description];
} }
return dest; return dest;
case NSPropertyListObjectiveCFormat: case NSPropertyListObjectiveCFormat:
case NSPropertyListSwiftFormat: case NSPropertyListSwiftFormat:
*anErrorString = @"Not implemented"; *anErrorString = @"Not implemented";
return nil; return nil;
default: default:
return (*originalWrite)(self, _cmd, aPropertyList, return (*originalWrite)(self, _cmd, aPropertyList,
aFormat, anErrorString); aFormat, anErrorString);
} }
} }
+ (id) _ppropertyListWithData: (NSData *)data + (id) _ppropertyListWithData: (NSData *)data
options: (NSPropertyListReadOptions)anOption options: (NSPropertyListReadOptions)anOption
format: (NSPropertyListFormat *)aFormat format: (NSPropertyListFormat *)aFormat
error: (out NSError **)error; error: (out NSError **)error;
{ {
NSError *myError = nil; NSError *myError = nil;
NSPropertyListFormat format; NSPropertyListFormat format;
NSJSONReadingOptions jsonOptions = NSJSONReadingAllowFragments; NSJSONReadingOptions jsonOptions = NSJSONReadingAllowFragments;
id prop; id prop;
prop = (*originalRead)(self, _cmd, data, anOption, &format, &myError); prop = (*originalRead)(self, _cmd, data, anOption, &format, &myError);
if (nil == prop) if (nil == prop)
{ {
if (format == NSPropertyListOpenStepFormat if (format == NSPropertyListOpenStepFormat
|| format == NSPropertyListGNUstepFormat) || format == NSPropertyListGNUstepFormat)
// rescue as json when we know it is not anything else // rescue as json when we know it is not anything else
{ {
switch (anOption) switch (anOption)
{ {
case NSPropertyListMutableContainersAndLeaves: case NSPropertyListMutableContainersAndLeaves:
jsonOptions |= NSJSONReadingMutableLeaves; jsonOptions |= NSJSONReadingMutableLeaves;
/* FALLTHROUGH */ /* FALLTHROUGH */
case NSPropertyListMutableContainers: case NSPropertyListMutableContainers:
jsonOptions |= NSJSONReadingMutableContainers; jsonOptions |= NSJSONReadingMutableContainers;
} }
format = NSPropertyListJSONFormat; format = NSPropertyListJSONFormat;
prop = [NSJSONSerialization JSONObjectWithData: data prop = [NSJSONSerialization JSONObjectWithData: data
options: jsonOptions options: jsonOptions
error: &myError]; error: &myError];
} }
} }
if (error != NULL) if (error != NULL)
{ {
*error = myError; *error = myError;
} }
if (aFormat != NULL) if (aFormat != NULL)
{ {
*aFormat = format; *aFormat = format;
} }
return prop; return prop;
} }
+ (void) load + (void) load
{ {
Method replacementRead; Method replacementRead;
Method replacementWrite; Method replacementWrite;
replacementRead = class_getClassMethod(self, replacementRead = class_getClassMethod(self,
@selector(_ppropertyListWithData:options:format:error:)); @selector(_ppropertyListWithData:options:format:error:));
replacementWrite = class_getClassMethod(self, replacementWrite = class_getClassMethod(self,
@selector(_pdataFromPropertyList:format:errorDescription:)); @selector(_pdataFromPropertyList:format:errorDescription:));
originalRead = class_replaceMethod(object_getClass(self), originalRead = class_replaceMethod(object_getClass(self),
@selector(propertyListWithData:options:format:error:), @selector(propertyListWithData:options:format:error:),
method_getImplementation(replacementRead), method_getImplementation(replacementRead),
method_getTypeEncoding(replacementRead)); method_getTypeEncoding(replacementRead));
originalWrite = class_replaceMethod(object_getClass(self), originalWrite = class_replaceMethod(object_getClass(self),
@selector(dataFromPropertyList:format:errorDescription:), @selector(dataFromPropertyList:format:errorDescription:),
method_getImplementation(replacementWrite), method_getImplementation(replacementWrite),
method_getTypeEncoding(replacementWrite)); method_getTypeEncoding(replacementWrite));
} }
@end @end