ObjFW  Check-in [e7a8f3d462]

Overview
Comment:OFLocale: Support automatic initialization
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: e7a8f3d462600b033a23af9cf62c5917bce7964a4639370a088eec3489660fe0
User & Date: js on 2024-04-02 02:22:10
Other Links: manifest | tags
Context
2024-04-02
03:21
Only align OFVector4D where necessary check-in: 1af54eb2c2 user: js tags: trunk
02:22
OFLocale: Support automatic initialization check-in: e7a8f3d462 user: js tags: trunk
01:33
OFTarArchive: Fix a memory leak check-in: ea9079b62b user: js tags: trunk
Changes

Modified src/OFApplication.m from [b344510e2d] to [3193393a96].

113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
	OFSocketDeinit();
#endif
}

int
OFApplicationMain(int *argc, char **argv[], id <OFApplicationDelegate> delegate)
{
	[[OFLocale alloc] init];

	app = [[OFApplication alloc] of_init];

#ifdef OF_WINDOWS
	if ([OFSystemInfo isWindowsNT]) {
		wchar_t **wargv, **wenvp;
		int wargc, si = 0;







|







113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
	OFSocketDeinit();
#endif
}

int
OFApplicationMain(int *argc, char **argv[], id <OFApplicationDelegate> delegate)
{
	[OFLocale currentLocale];

	app = [[OFApplication alloc] of_init];

#ifdef OF_WINDOWS
	if ([OFSystemInfo isWindowsNT]) {
		wchar_t **wargv, **wenvp;
		int wargc, si = 0;

Modified src/OFLocale.h from [5b6f1c91ee] to [1cbc31b491].

91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
 * @brief The decimal separator of the locale.
 */
@property (readonly, nonatomic) OFString *decimalSeparator;

/**
 * @brief Returns the current OFLocale.
 *
 * @warning If you don't use @ref OFApplication, this might be `nil`! In this
 *	    case, you need to manually allocate an instance and call
 *	    @ref init once.
 *
 * @return The current OFLocale instance
 */
+ (nullable OFLocale *)currentLocale;

/**
 * @brief Returns the language code of the locale.







|
<
|







91
92
93
94
95
96
97
98

99
100
101
102
103
104
105
106
 * @brief The decimal separator of the locale.
 */
@property (readonly, nonatomic) OFString *decimalSeparator;

/**
 * @brief Returns the current OFLocale.
 *
 * @note If you don't use @ref OFApplication, you need to call this as early as

 *	 possible to initialize the locale!
 *
 * @return The current OFLocale instance
 */
+ (nullable OFLocale *)currentLocale;

/**
 * @brief Returns the language code of the locale.
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160

161
162
163
164
165
166
167
/**
 * @brief Adds a directory to scan for localizations.
 *
 * @param IRI The IRI to the directory to scan for localizations
 */
+ (void)addLocalizationDirectoryIRI: (OFIRI *)IRI;

/**
 * @brief Initializes the current OFLocale.
 *
 * @warning This sets the locale via `setlocale()`!
 *
 * @warning You should never call this yourself, except if you do not use
 *	    @ref OFApplication. In this case, you need to allocate exactly one
 *	    instance of OFLocale, which will become the current locale, and
 *	    call this method.
 */
- (instancetype)init;


/**
 * @brief Adds a directory to scan for localizations.
 *
 * @param IRI The IRI to the directory to scan for localizations
 */
- (void)addLocalizationDirectoryIRI: (OFIRI *)IRI;







<
<
<
<
<
<
<
<
<
<
|
>







142
143
144
145
146
147
148










149
150
151
152
153
154
155
156
157
/**
 * @brief Adds a directory to scan for localizations.
 *
 * @param IRI The IRI to the directory to scan for localizations
 */
+ (void)addLocalizationDirectoryIRI: (OFIRI *)IRI;











- (instancetype)init OF_DEPRECATED(ObjFW, 1, 1, "Manually creating an OFLocale "
    "is no longer necessary. Use +[OFLocale currentLocale] instead.");

/**
 * @brief Adds a directory to scan for localizations.
 *
 * @param IRI The IRI to the directory to scan for localizations
 */
- (void)addLocalizationDirectoryIRI: (OFIRI *)IRI;

Modified src/OFLocale.m from [8178789b63] to [86d73ce338].

19
20
21
22
23
24
25


26
27
28
29
30
31
32
33
34
35
36
37
38
39





40
41






42
43
44
45
46
47
48

#import "OFLocale.h"
#import "OFArray.h"
#import "OFDictionary.h"
#import "OFIRI.h"
#import "OFNumber.h"
#import "OFString.h"



#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"
#import "OFOpenItemFailedException.h"

#ifdef OF_AMIGAOS
# define Class IntuitionClass
# include <proto/dos.h>
# include <proto/exec.h>
# include <proto/locale.h>
# undef Class
#endif






static OFLocale *currentLocale = nil;
static OFDictionary *operatorPrecedences = nil;







#ifndef OF_AMIGAOS
static void
parseLocale(char *locale, OFStringEncoding *encoding,
    OFString **languageCode, OFString **countryCode)
{
	locale = OFStrDup(locale);







>
>














>
>
>
>
>


>
>
>
>
>
>







19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

#import "OFLocale.h"
#import "OFArray.h"
#import "OFDictionary.h"
#import "OFIRI.h"
#import "OFNumber.h"
#import "OFString.h"

#import "OFOnce.h"

#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"
#import "OFOpenItemFailedException.h"

#ifdef OF_AMIGAOS
# define Class IntuitionClass
# include <proto/dos.h>
# include <proto/exec.h>
# include <proto/locale.h>
# undef Class
#endif

@interface OFLocale ()
- (instancetype)of_init OF_METHOD_FAMILY(init);
@end

static OFOnceControl initLocaleControl = OFOnceControlInitValue;
static OFLocale *currentLocale = nil;
static OFDictionary *operatorPrecedences = nil;

static void
initLocale(void)
{
	currentLocale = [[OFLocale alloc] of_init];
}

#ifndef OF_AMIGAOS
static void
parseLocale(char *locale, OFStringEncoding *encoding,
    OFString **languageCode, OFString **countryCode)
{
	locale = OFStrDup(locale);
308
309
310
311
312
313
314

315
316
317
318


319
320
321
322
323
324
325

@implementation OFLocale
@synthesize languageCode = _languageCode, countryCode = _countryCode;
@synthesize encoding = _encoding, decimalSeparator = _decimalSeparator;

+ (void)initialize
{

	OFNumber *one, *two, *three, *four;

	if (self != [OFLocale class])
		return;



	/* 1 is also used to denote a unary operator. */
	one = [OFNumber numberWithUnsignedInt: 1];
	two = [OFNumber numberWithUnsignedInt: 2];
	three = [OFNumber numberWithUnsignedInt: 3];
	four = [OFNumber numberWithUnsignedInt: 4];








>




>
>







321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341

@implementation OFLocale
@synthesize languageCode = _languageCode, countryCode = _countryCode;
@synthesize encoding = _encoding, decimalSeparator = _decimalSeparator;

+ (void)initialize
{
	void *pool;
	OFNumber *one, *two, *three, *four;

	if (self != [OFLocale class])
		return;

	pool = objc_autoreleasePoolPush();

	/* 1 is also used to denote a unary operator. */
	one = [OFNumber numberWithUnsignedInt: 1];
	two = [OFNumber numberWithUnsignedInt: 2];
	three = [OFNumber numberWithUnsignedInt: 3];
	four = [OFNumber numberWithUnsignedInt: 4];

333
334
335
336
337
338
339


340
341
342
343


344
345
346
347
348


349
350
351
352
353


354
355
356
357
358


359
360
361
362
363


364
365
366
367
368
369
370
371
372
373














374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
	    @"+", two,
	    @"%", two,
	    @"&&", three,
	    @"||", four,
	    @"!", one,
	    @"is_real", one,
	    nil];


}

+ (OFLocale *)currentLocale
{


	return currentLocale;
}

+ (OFString *)languageCode
{


	return currentLocale.languageCode;
}

+ (OFString *)countryCode
{


	return currentLocale.countryCode;
}

+ (OFStringEncoding)encoding
{


	return currentLocale.encoding;
}

+ (OFString *)decimalSeparator
{


	return currentLocale.decimalSeparator;
}

+ (void)addLocalizationDirectoryIRI: (OFIRI *)IRI
{
	[currentLocale addLocalizationDirectoryIRI: IRI];
}

- (instancetype)init
{














	self = [super init];

	@try {
#ifndef OF_AMIGAOS
		char *locale, *messagesLocale = NULL;

		if (currentLocale != nil)
			@throw [OFInitializationFailedException
			    exceptionWithClass: self.class];

# ifdef OF_MSDOS
		_encoding = OFStringEncodingCodepage437;
# else
		_encoding = OFStringEncodingUTF8;
# endif
		_decimalSeparator = @".";
		_localizedStrings = [[OFMutableArray alloc] init];







>
>




>
>





>
>





>
>





>
>





>
>










>
>
>
>
>
>
>
>
>
>
>
>
>
>






<
<
<
<







349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421




422
423
424
425
426
427
428
	    @"+", two,
	    @"%", two,
	    @"&&", three,
	    @"||", four,
	    @"!", one,
	    @"is_real", one,
	    nil];

	objc_autoreleasePoolPop(pool);
}

+ (OFLocale *)currentLocale
{
	OFOnce(&initLocaleControl, initLocale);

	return currentLocale;
}

+ (OFString *)languageCode
{
	OFOnce(&initLocaleControl, initLocale);

	return currentLocale.languageCode;
}

+ (OFString *)countryCode
{
	OFOnce(&initLocaleControl, initLocale);

	return currentLocale.countryCode;
}

+ (OFStringEncoding)encoding
{
	OFOnce(&initLocaleControl, initLocale);

	return currentLocale.encoding;
}

+ (OFString *)decimalSeparator
{
	OFOnce(&initLocaleControl, initLocale);

	return currentLocale.decimalSeparator;
}

+ (void)addLocalizationDirectoryIRI: (OFIRI *)IRI
{
	[currentLocale addLocalizationDirectoryIRI: IRI];
}

- (instancetype)init
{
	/*
	 * In the past, applications not using OFApplication were required to
	 * create an instance of OFLocale manually. This is no longer needed
	 * and +[currentLocale] creates the singleton. However, in order to not
	 * break old applications, this method needs to just return the
	 * singleton now.
	 */
	[self release];

	return [OFLocale currentLocale];
}

- (instancetype)of_init
{
	self = [super init];

	@try {
#ifndef OF_AMIGAOS
		char *locale, *messagesLocale = NULL;





# ifdef OF_MSDOS
		_encoding = OFStringEncodingCodepage437;
# else
		_encoding = OFStringEncodingUTF8;
# endif
		_decimalSeparator = @".";
		_localizedStrings = [[OFMutableArray alloc] init];
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
		objc_autoreleasePoolPop(pool);
#endif
	} @catch (id e) {
		[self release];
		@throw e;
	}

	currentLocale = self;

	return self;
}

- (void)dealloc
{
	[_languageCode release];
	[_countryCode release];
	[_decimalSeparator release];
	[_localizedStrings release];

	[super dealloc];
}

- (void)addLocalizationDirectoryIRI: (OFIRI *)IRI
{
	void *pool;
	OFIRI *mapIRI, *localizationIRI;
	OFString *languageCode, *countryCode, *localizationFile;
	OFDictionary *map;







<
<



<
<
<
<
<
<
|
<
<







518
519
520
521
522
523
524


525
526
527






528


529
530
531
532
533
534
535
		objc_autoreleasePoolPop(pool);
#endif
	} @catch (id e) {
		[self release];
		@throw e;
	}



	return self;
}







OF_SINGLETON_METHODS



- (void)addLocalizationDirectoryIRI: (OFIRI *)IRI
{
	void *pool;
	OFIRI *mapIRI, *localizationIRI;
	OFString *languageCode, *countryCode, *localizationFile;
	OFDictionary *map;