From bdb33706fe3c8a541ba3df540bbde5798c86a065 Mon Sep 17 00:00:00 2001 From: CaS Date: Sat, 10 Nov 2001 17:31:39 +0000 Subject: [PATCH] More user defaults modification. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@11359 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 23 +++++++++ Documentation/gsdoc/Base.html | 58 ++++++++++------------ Source/GSCompatibility.m | 93 +++-------------------------------- Source/GSMime.m | 4 +- Source/GSUserDefaults.h | 50 +++++++++++++++++++ Source/NSArray.m | 6 +-- Source/NSCalendarDate.m | 7 ++- Source/NSDate.m | 4 +- Source/NSDecimalNumber.m | 20 ++++---- Source/NSDictionary.m | 6 +-- Source/NSFileHandle.m | 5 ++ Source/NSGeometry.m | 1 - Source/NSLog.m | 4 +- Source/NSScanner.m | 6 +-- Source/NSString.m | 11 ++--- Source/NSUserDefaults.m | 69 +++++++++++++++++++++++--- 16 files changed, 205 insertions(+), 162 deletions(-) create mode 100644 Source/GSUserDefaults.h diff --git a/ChangeLog b/ChangeLog index 2f05eefda..27a383928 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,26 @@ +2001-11-09 Richard Frith-Macdonald + + * Documentation/gsdoc/Base.gsdoc: + * Source/GSCompatibility.m: + * Source/GSMime.m: + * Source/GSUserDefaults.h: + * Source/NSArray.m: + * Source/NSCalendarDate.m: + * Source/NSDate.m: + * Source/NSDecimalNumber.m: + * Source/NSDictionary.m: + * Source/NSFileHandle.m: + * Source/NSGeometry.m: + * Source/NSLog.m: + * Source/NSScanner.m: + * Source/NSString.m: + * Source/NSUserDefaults.m: Modify user defaults usage to cache some + defaults used internally and provide private accessors for speed. + Add default to force logging to be done via syslog since logging + to stderr within code called via JIGS from a servlet engine may + cause severe problems if the servlet engine is using descriptor 2 + for other purposes. + 2001-11-09 Richard Frith-Macdonald * Source/GSMime.m: modifications made to support HTTP continuation diff --git a/Documentation/gsdoc/Base.html b/Documentation/gsdoc/Base.html index 7888d8997..5a037af61 100644 --- a/Documentation/gsdoc/Base.html +++ b/Documentation/gsdoc/Base.html @@ -61,15 +61,32 @@

User defaults

-

- - Setting the user default GSMacOSXCompatible to - YES will cause MacOS compatible behavior to be - the default at runtime. This default may however be overridden - to provide more fine grained control of system behavior. -

-
GSMacOSXCompatibleGeometry +
GSLogSyslog +
+

+ + Setting the user default GSLogSyslog to + YES will cause log/debug output to be sent to + the syslog facility (on systems which support it), rather + than to the standard error stream. This is useful in + environments where stderr has been re-used strangely for + some reason. +

+ + +
GSMacOSXCompatible +
+

+ + Setting the user default GSMacOSXCompatible to + YES will cause MacOS compatible behavior to be + the default at runtime. This default may however be overridden + to provide more fine grained control of system behavior. +

+ + +
GSOldStyleGeometry

@@ -82,7 +99,7 @@

-
GSMacOSXCompatiblePropertyLists +
NSWriteOldStylePropertyLists

@@ -110,29 +127,6 @@

-
NSWriteOldStylePropertyLists -
-

- - Specifies whether text property-list output should be in - the default MacOS-X format (XML), or in the more human - readable (but less powerful) original OpenStep format. -

- -

- - If, GSMacOSXCompatible is YES, this default is treated - as being NO if it is not set, otherwise, this default - is treated as YES. -

- -

- - This default is NOT used if - GSMacOSXCompatiblePropertyLists is specified. -

- -

Environment variables

diff --git a/Source/GSCompatibility.m b/Source/GSCompatibility.m index 2ee08ebb0..88e80c4e7 100644 --- a/Source/GSCompatibility.m +++ b/Source/GSCompatibility.m @@ -24,6 +24,8 @@ #include #include +#include "GSUserDefaults.h" + #ifndef HAVE_RINT #include static double rint(double a) @@ -35,98 +37,19 @@ static double rint(double a) /* * Runtime MacOS-X compatibility flags. */ -static BOOL setupDone = NO; - -static BOOL MacOSXCompatible = NO; -static BOOL MacOSXCompatibleGeometry = NO; -static BOOL MacOSXCompatiblePropertyLists = NO; - -/* - * A trivial class to monitor user defaults to see how we should be - * producing strings describing geometry structures. - */ -@interface GSBaseDefaultObserver : NSObject -+ (void) defaultsChanged: (NSNotification*)aNotification; -@end -@implementation GSBaseDefaultObserver -+ (void) defaultsChanged: (NSNotification*)aNotification -{ - NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; - id def; - Class sClass = [NSString class]; - - MacOSXCompatible = [defaults boolForKey: @"GSMacOSXCompatible"]; - - def = [defaults objectForKey: @"GSMacOSXCompatibleGeometry"]; - if (def != nil && [def isKindOfClass: sClass] == YES) - { - MacOSXCompatibleGeometry = [def boolValue]; - } - else - { - MacOSXCompatibleGeometry = MacOSXCompatible; - } - def = [defaults objectForKey: @"GSMacOSXCompatiblePropertyLists"]; - if (def != nil && [def isKindOfClass: sClass] == YES) - { - MacOSXCompatiblePropertyLists = [def boolValue]; - } - else - { - def = [defaults objectForKey: @"NSWriteOldStylePropertyLists"]; - if (def != nil && [def isKindOfClass: sClass] == YES) - { - if ([def boolValue] == YES) - { - MacOSXCompatiblePropertyLists = NO; - } - else - { - MacOSXCompatiblePropertyLists = YES; - } - } - else - { - MacOSXCompatiblePropertyLists = MacOSXCompatible; - } - } -} -@end - -static void -compatibilitySetup() -{ - if (setupDone == NO) - { - setupDone = YES; - [[NSNotificationCenter defaultCenter] - addObserver: [GSBaseDefaultObserver class] - selector: @selector(defaultsChanged:) - name: NSUserDefaultsDidChangeNotification - object: nil]; - [[GSBaseDefaultObserver class] defaultsChanged: nil]; - } -} - -BOOL GSMacOSXCompatible() -{ - if (setupDone == NO) - compatibilitySetup(); - return MacOSXCompatible; -} BOOL GSMacOSXCompatibleGeometry() { - if (setupDone == NO) - compatibilitySetup(); - return MacOSXCompatibleGeometry; + if (GSUserDefaultsFlag(GSOldStyleGeometry) == YES) + return NO; + return GSUserDefaultsFlag(GSMacOSXCompatible); } BOOL GSMacOSXCompatiblePropertyLists() { - if (setupDone == NO) - compatibilitySetup(); - return MacOSXCompatiblePropertyLists; + if (GSUserDefaultsFlag(NSWriteOldStylePropertyLists) == YES) + return NO; + return GSUserDefaultsFlag(GSMacOSXCompatible); } #include diff --git a/Source/GSMime.m b/Source/GSMime.m index de6086e76..c528cad4f 100644 --- a/Source/GSMime.m +++ b/Source/GSMime.m @@ -29,7 +29,6 @@ #include #include #include -#include #include #include #include @@ -37,6 +36,7 @@ #include #include #include +#include "GSUserDefaults.h" static NSCharacterSet *specials = nil; @@ -1992,7 +1992,7 @@ parseCharacterSet(NSString *token) NSDictionary *locale; desc = [NSMutableString stringWithFormat: @"GSMimeDocument <%0x> -\n", self]; - locale = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]; + locale = GSUserDefaultsDictionaryRepresentation(); [desc appendString: [headers descriptionWithLocale: locale]]; [desc appendFormat: @"\nDocument content -\n%@", content]; return desc; diff --git a/Source/GSUserDefaults.h b/Source/GSUserDefaults.h new file mode 100644 index 000000000..7ca3b46df --- /dev/null +++ b/Source/GSUserDefaults.h @@ -0,0 +1,50 @@ +/* GSUserDefaults + Copyright (C) 2001 Free Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library 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 Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA. +*/ + +#ifndef __GSUserDefaults_h_ +#define __GSUserDefaults_h_ + +#include + +/* + * For efficiency, we save defaults information which is used by the + * base library. + */ +typedef enum { + GSMacOSXCompatible, // General behavior flag. + GSOldStyleGeometry, // Control geometry string output. + GSLogSyslog, // Force logging to go to syslog. + NSWriteOldStylePropertyLists, // Control PList output. + GSUserDefaultMaxFlag // End marker. +} GSUserDefaultFlagType; + +/* + * Get the dictionary representation. + */ +NSDictionary *GSUserDefaultsDictionaryRepresentation(); + +/* + * Get one of several potentially useful flags. + */ +BOOL GSUserDefaultsFlag(GSUserDefaultFlagType type); + +#endif /* __GSUserDefaults_h_ */ diff --git a/Source/NSArray.m b/Source/NSArray.m index 61bcdefe3..c8eae2539 100644 --- a/Source/NSArray.m +++ b/Source/NSArray.m @@ -36,11 +36,11 @@ #include #include #include -#include #include #include #include #include +#include "GSUserDefaults.h" @class NSArrayEnumerator; @class NSArrayEnumeratorReverse; @@ -834,7 +834,7 @@ static NSString *indentStrings[] = { NSDictionary *loc; NSString *desc; - loc = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]; + loc = GSUserDefaultsDictionaryRepresentation(); if (GSMacOSXCompatiblePropertyLists() == YES) { @@ -863,7 +863,7 @@ static NSString *indentStrings[] = { NSDictionary *loc; NSString *desc; - loc = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]; + loc = GSUserDefaultsDictionaryRepresentation(); if (GSMacOSXCompatiblePropertyLists() == YES) { diff --git a/Source/NSCalendarDate.m b/Source/NSCalendarDate.m index 18eb7e7e7..e477a8a68 100644 --- a/Source/NSCalendarDate.m +++ b/Source/NSCalendarDate.m @@ -32,11 +32,11 @@ #include #include #include -#include #include #include #include #include +#include "GSUserDefaults.h" // Absolute Gregorian date for NSDate reference date Jan 01 2001 // @@ -314,8 +314,7 @@ static inline int getDigits(const char *from, char *to, int limit) if (locale == nil) { - locale - = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]; + locale = GSUserDefaultsDictionaryRepresentation(); } if (fmt == nil) { @@ -1222,7 +1221,7 @@ static inline int getDigits(const char *from, char *to, int limit) int i, j, k, z; if (locale == nil) - locale = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]; + locale = GSUserDefaultsDictionaryRepresentation(); if (format == nil) format = [locale objectForKey: NSTimeDateFormatString]; diff --git a/Source/NSDate.m b/Source/NSDate.m index 07844ba02..6beb71cce 100644 --- a/Source/NSDate.m +++ b/Source/NSDate.m @@ -32,7 +32,6 @@ #include #include #include -#include #include #include #include @@ -45,6 +44,7 @@ #include #include #include +#include "GSUserDefaults.h" /* The number of seconds between 1/1/2001 and 1/1/1970 = -978307200. */ /* This number comes from: @@ -239,7 +239,7 @@ GSTimeNow() unsigned dtoIndex; if (locale == nil) - locale = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]; + locale = GSUserDefaultsDictionaryRepresentation(); ws = [NSCharacterSet whitespaceAndNewlineCharacterSet]; digits = [NSCharacterSet decimalDigitCharacterSet]; diff --git a/Source/NSDecimalNumber.m b/Source/NSDecimalNumber.m index 5de0fa27c..fc4a74521 100644 --- a/Source/NSDecimalNumber.m +++ b/Source/NSDecimalNumber.m @@ -25,7 +25,7 @@ #include #include #include -#include +#include "GSUserDefaults.h" static NSDecimalNumberHandler *handler; @@ -224,9 +224,9 @@ static NSDecimalNumber *one; return self; } -- (id)initWithMantissa:(unsigned long long)mantissa - exponent:(short)exponent - isNegative:(BOOL)flag +- (id) initWithMantissa: (unsigned long long)mantissa + exponent: (short)exponent + isNegative: (BOOL)flag { NSDecimal decimal; @@ -234,16 +234,14 @@ static NSDecimalNumber *one; return [self initWithDecimal: decimal]; } -- (id)initWithString:(NSString *)numberValue +- (id) initWithString: (NSString*)numberValue { - NSUserDefaults *defs = [NSUserDefaults standardUserDefaults]; - return [self initWithString: numberValue - locale: [defs dictionaryRepresentation]]; + locale: GSUserDefaultsDictionaryRepresentation()]; } -- (id)initWithString:(NSString *)numberValue - locale:(NSDictionary *)locale; +- (id) initWithString: (NSString*)numberValue + locale: (NSDictionary*)locale; { NSDecimal decimal; @@ -251,7 +249,7 @@ static NSDecimalNumber *one; return [self initWithDecimal: decimal]; } -- (NSString *)descriptionWithLocale:(NSDictionary *)locale +- (NSString*) descriptionWithLocale: (NSDictionary*)locale { return NSDecimalString(&data, locale); } diff --git a/Source/NSDictionary.m b/Source/NSDictionary.m index b6bb12a06..264af23b0 100644 --- a/Source/NSDictionary.m +++ b/Source/NSDictionary.m @@ -31,10 +31,10 @@ #include #include #include -#include #include #include #include +#include "GSUserDefaults.h" @implementation NSDictionary @@ -626,7 +626,7 @@ compareIt(id o1, id o2, void* context) NSDictionary *loc; NSString *desc; - loc = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]; + loc = GSUserDefaultsDictionaryRepresentation(); if (GSMacOSXCompatiblePropertyLists() == YES) { @@ -655,7 +655,7 @@ compareIt(id o1, id o2, void* context) NSDictionary *loc; NSString *desc; - loc = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]; + loc = GSUserDefaultsDictionaryRepresentation(); if (GSMacOSXCompatiblePropertyLists() == YES) { diff --git a/Source/NSFileHandle.m b/Source/NSFileHandle.m index 91063de81..0f8451e02 100644 --- a/Source/NSFileHandle.m +++ b/Source/NSFileHandle.m @@ -359,6 +359,11 @@ NSString* NSFileHandleOperationException = return nil; } +- (BOOL) useCompression +{ + return NO; +} + - (void) writeInBackgroundAndNotify: (NSData*)item forModes: (NSArray*)modes { [self subclassResponsibility: _cmd]; diff --git a/Source/NSGeometry.m b/Source/NSGeometry.m index 82cc26848..4565dceb4 100644 --- a/Source/NSGeometry.m +++ b/Source/NSGeometry.m @@ -35,7 +35,6 @@ #include #include #include -#include extern BOOL GSMacOSXCompatibleGeometry(); // Compatibility mode diff --git a/Source/NSLog.m b/Source/NSLog.m index 2f53a4b9a..e658ce977 100644 --- a/Source/NSLog.m +++ b/Source/NSLog.m @@ -39,6 +39,8 @@ #include +#include "GSUserDefaults.h" + static void _NSLog_standard_printf_handler (NSString* message) { @@ -67,7 +69,7 @@ _NSLog_standard_printf_handler (NSString* message) #ifdef HAVE_SYSLOG - if (write(2, buf, len) != len) + if (GSUserDefaultsFlag(GSLogSyslog) == YES || write(2, buf, len) != len) { int mask; diff --git a/Source/NSScanner.m b/Source/NSScanner.m index a7f45334c..76b91aa27 100644 --- a/Source/NSScanner.m +++ b/Source/NSScanner.m @@ -25,12 +25,12 @@ #include #include #include -#include #include #include #include #include #include /* FIXME: May go away once I figure out Unicode */ +#include "GSUserDefaults.h" /* BSD and Solaris have this */ #if defined(HANDLE_LLONG_MAX) && !defined(HANDLE_LONG_LONG_MAX) @@ -110,9 +110,7 @@ typedef struct { if (scanner != nil) { - NSUserDefaults *defs = [NSUserDefaults standardUserDefaults]; - - [scanner setLocale: [defs dictionaryRepresentation]]; + [scanner setLocale: GSUserDefaultsDictionaryRepresentation()]; } return scanner; } diff --git a/Source/NSString.m b/Source/NSString.m index 6da932594..c9741b05e 100644 --- a/Source/NSString.m +++ b/Source/NSString.m @@ -50,7 +50,6 @@ #include #include #include -#include #include #include #include @@ -74,6 +73,8 @@ #include +#include "GSUserDefaults.h" + @class GSString; @class GSMutableString; @class GSPlaceholderString; @@ -3097,7 +3098,7 @@ handle_printf_atsign (FILE *stream, } else { - dict = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]; + dict = GSUserDefaultsDictionaryRepresentation(); ret = AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()] initWithFormat: format locale: dict arguments: ap]); } @@ -3125,8 +3126,7 @@ handle_printf_atsign (FILE *stream, - (NSComparisonResult) localizedCompare: (NSString *)string { - NSDictionary *dict = - [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]; + NSDictionary *dict = GSUserDefaultsDictionaryRepresentation(); return [self compare: string options: 0 @@ -3136,8 +3136,7 @@ handle_printf_atsign (FILE *stream, - (NSComparisonResult) localizedCaseInsensitiveCompare: (NSString *)string { - NSDictionary *dict = - [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]; + NSDictionary *dict = GSUserDefaultsDictionaryRepresentation(); return [self compare: string options: NSCaseInsensitiveSearch diff --git a/Source/NSUserDefaults.m b/Source/NSUserDefaults.m index 05686eff2..ad4ef03af 100644 --- a/Source/NSUserDefaults.m +++ b/Source/NSUserDefaults.m @@ -47,9 +47,14 @@ #include #include +#include "GSUserDefaults.h" + /* Wait for access */ #define _MAX_COUNT 5 /* Max 10 sec. */ +/************************************************************************* + *** Class variables + *************************************************************************/ static SEL nextObjectSel; static SEL objectForKeySel; static SEL addSel; @@ -65,8 +70,31 @@ static Class NSDictionaryClass; static Class NSMutableDictionaryClass; static Class NSStringClass; +static NSUserDefaults *sharedDefaults = nil; +static NSMutableString *processName = nil; +static NSMutableArray *userLanguages = nil; static NSRecursiveLock *classLock = nil; +/* + * Caching some defaults. + */ +static BOOL flags[GSUserDefaultMaxFlag] = { 0 }; + +static void updateCache(NSUserDefaults *self) +{ + if (self == sharedDefaults) + { + flags[GSMacOSXCompatible] + = [self boolForKey: @"GSMacOSXCompatible"]; + flags[GSOldStyleGeometry] + = [self boolForKey: @"GSOldStyleGeometry"]; + flags[GSLogSyslog] + = [self boolForKey: @"GSLogSyslog"]; + flags[NSWriteOldStylePropertyLists] + = [self boolForKey: @"NSWriteOldStylePropertyLists"]; + } +} + /************************************************************************* *** Local method definitions *************************************************************************/ @@ -78,13 +106,6 @@ static NSRecursiveLock *classLock = nil; @end @implementation NSUserDefaults: NSObject -/************************************************************************* - *** Class variables - *************************************************************************/ -static NSUserDefaults *sharedDefaults = nil; -static NSMutableString *processName = nil; -static NSMutableArray *userLanguages = nil; - /************************************************************************* *** Getting the Shared Instance *************************************************************************/ @@ -1072,6 +1093,7 @@ static NSString *pathForUser(NSString *user) { RELEASE(_persDomains); _persDomains = newDict; + updateCache(self); [[NSNotificationCenter defaultCenter] postNotificationName: NSUserDefaultsDidChangeNotification object: nil]; @@ -1336,8 +1358,10 @@ static NSString *pathForUser(NSString *user) if (!_changedDomains) { _changedDomains = [[NSMutableArray alloc] initWithCapacity: 5]; + updateCache(self); [[NSNotificationCenter defaultCenter] - postNotificationName: NSUserDefaultsDidChangeNotification object: nil]; + postNotificationName: NSUserDefaultsDidChangeNotification + object: nil]; } enumerator = [_changedDomains objectEnumerator]; @@ -1363,3 +1387,32 @@ static NSString *pathForUser(NSString *user) [self synchronize]; } @end + +NSDictionary* +GSUserDefaultsDictionaryRepresentation() +{ + NSDictionary *defs; + + [classLock lock]; + if (sharedDefaults == nil) + { + [NSUserDefaults standardUserDefaults]; + } + defs = [sharedDefaults dictionaryRepresentation]; + [classLock unlock]; + return defs; +} + +/* + * Get one of several potentially useful flags. + */ +BOOL +GSUserDefaultsFlag(GSUserDefaultFlagType type) +{ + if (sharedDefaults == nil) + { + [NSUserDefaults standardUserDefaults]; + } + return flags[type]; +} +