ObjFW  Diff

Differences From Artifact [2546d1519d]:

To Artifact [6583af04e3]:


298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
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
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
+ stringWithContentsOfFile: (OFString*)path
		  encoding: (enum of_string_encoding)encoding
{
	return [[[self alloc] initWithContentsOfFile: path
					    encoding: encoding] autorelease];
}

- init
{
	[super init];

	string = NULL;

	return self;
}

- initWithCString: (const char*)str
{
	return [self initWithCString: str
			    encoding: OF_STRING_ENCODING_UTF_8
			      length: strlen(str)];
}

- initWithCString: (const char*)str
	 encoding: (enum of_string_encoding)encoding
{
	return [self initWithCString: str
			    encoding: encoding
			      length: strlen(str)];
}

- initWithCString: (const char*)str
	 encoding: (enum of_string_encoding)encoding
	   length: (size_t)len
{
	size_t i, j;

	self = [super init];



	length = len;

	@try {
		string = [self allocMemoryWithSize: length + 1];
	} @catch (OFException *e) {
		/*
		 * We can't use [super dealloc] on OS X here.
		 * Compiler bug? Anyway, [self dealloc] will do here as we
		 * don't reimplement dealloc.
		 */
		[self dealloc];
		@throw e;
	}

	switch (encoding) {
	case OF_STRING_ENCODING_UTF_8:
		switch (of_string_check_utf8(str, length)) {
		case 1:
			isUTF8 = YES;
			break;
		case -1:;
			/*
			 * We can't use [super dealloc] on OS X here.
			 * Compiler bug? Anyway, [self dealloc] will do here as
			 * we don't reimplement dealloc.
			 */
			Class c = isa;
			[self dealloc];
			@throw [OFInvalidEncodingException newWithClass: c];

		}

		memcpy(string, str, length);
		string[length] = 0;

		break;

	case OF_STRING_ENCODING_ISO_8859_1:
	case OF_STRING_ENCODING_ISO_8859_15:
	case OF_STRING_ENCODING_WINDOWS_1252:
		for (i = j = 0; i < len; i++) {



			if (!(str[i] & 0x80))
				string[j++] = str[i];
			else {
				char buf[4];
				of_unichar_t chr;
				size_t chr_bytes;

				switch (encoding) {
				case OF_STRING_ENCODING_ISO_8859_1:
					chr = (uint8_t)str[i];
					break;
				case OF_STRING_ENCODING_ISO_8859_15:
					chr = of_iso_8859_15[(uint8_t)str[i]];
					break;
				case OF_STRING_ENCODING_WINDOWS_1252:
					chr = of_windows_1252[(uint8_t)str[i]];
					break;
				default:;
					/*
					 * We can't use [super dealloc] on OS X
					 * here. Compiler bug? Anyway,
					 * [self dealloc] will do here as we
					 * don't reimplement dealloc.
					 */
					Class c = isa;
					[self dealloc];
					@throw [OFInvalidEncodingException
					    newWithClass: c];










				}


				if (chr == 0xFFFD) {










					/*
					 * We can't use [super dealloc] on OS X

					 * here. Compiler bug? Anyway,


					 * [self dealloc] will do here as we


					 * don't reimplement dealloc.

					 */
					Class c = isa;
					[self dealloc];

					@throw [OFInvalidEncodingException
					    newWithClass: c];
				}

				isUTF8 = YES;
				chr_bytes = of_string_unicode_to_utf8(chr, buf);

				if (chr_bytes == 0) {
					/*
					 * We can't use [super dealloc] on OS X
					 * here. Compiler bug? Anyway,
					 * [self dealloc] will do here as we
					 * don't reimplement dealloc.
					 */
					Class c = isa;
					[self dealloc];
					@throw [OFInvalidEncodingException
					    newWithClass: c];
				}

				length += chr_bytes - 1;
				@try {
					string = [self resizeMemory: string
							     toSize: length +
								     1];
				} @catch (OFException *e) {
					/*
					 * We can't use [super dealloc] on OS X
					 * here. Compiler bug? Anyway,
					 * [self dealloc] will do here as we
					 * don't reimplement dealloc.
					 */
					[self dealloc];
					@throw e;
				}

				memcpy(string + j, buf, chr_bytes);
				j += chr_bytes;
			}
		}

		string[length] = 0;

		break;
	default:;
		/*
		 * We can't use [super dealloc] on OS X here.
		 * Compiler bug? Anyway, [self dealloc] will do here as we
		 * don't reimplement dealloc.
		 */
		Class c = isa;
		[self dealloc];
		@throw [OFInvalidEncodingException newWithClass: c];
	}

	return self;
}

- initWithCString: (const char*)str
	   length: (size_t)len







<
<
<
<
<
<
<
<
<



















<
<


>
>
|

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

|
|

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

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

|
<
<
<
<
<
<
<
<
|
|
|
<
|
<
|
|
<
<
<
<
<
<
<
<
<
<
|
<
|
|
|
|
<

|
<
<
<
<
<
<
<
<
|
|







298
299
300
301
302
303
304









305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323


324
325
326
327
328
329

330







331
332


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

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
429
430
431
432
+ stringWithContentsOfFile: (OFString*)path
		  encoding: (enum of_string_encoding)encoding
{
	return [[[self alloc] initWithContentsOfFile: path
					    encoding: encoding] autorelease];
}










- initWithCString: (const char*)str
{
	return [self initWithCString: str
			    encoding: OF_STRING_ENCODING_UTF_8
			      length: strlen(str)];
}

- initWithCString: (const char*)str
	 encoding: (enum of_string_encoding)encoding
{
	return [self initWithCString: str
			    encoding: encoding
			      length: strlen(str)];
}

- initWithCString: (const char*)str
	 encoding: (enum of_string_encoding)encoding
	   length: (size_t)len
{


	self = [super init];

	@try {
		size_t i, j;
		const uint16_t *table;


		string = [self allocMemoryWithSize: len + 1];







		length = len;



		if (encoding == OF_STRING_ENCODING_UTF_8) {
			switch (of_string_check_utf8(str, length)) {
			case 1:
				isUTF8 = YES;
				break;
			case -1:







				@throw [OFInvalidEncodingException
				    newWithClass: isa];
			}

			memcpy(string, str, length);
			string[length] = 0;

			return self;
		}

		if (encoding == OF_STRING_ENCODING_ISO_8859_1) {

			for (i = j = 0; i < len; i++) {
				char buf[4];
				size_t bytes;

				if (!(str[i] & 0x80)) {
					string[j++] = str[i];
					continue;



				}


				isUTF8 = YES;


				bytes = of_string_unicode_to_utf8(


				    (uint8_t)str[i], buf);








				if (bytes == 0)

					@throw [OFInvalidEncodingException
					    newWithClass: isa];

				length += bytes - 1;
				string = [self resizeMemory: string
						     toSize: length + 1];

				memcpy(string + j, buf, bytes);
				j += bytes;
			}

			string[length] = 0;

			return self;
		}

		switch (encoding) {
		case OF_STRING_ENCODING_ISO_8859_15:
			table = of_iso_8859_15;
			break;
		case OF_STRING_ENCODING_WINDOWS_1252:
			table = of_windows_1252;
			break;
		default:
			@throw [OFInvalidEncodingException newWithClass: isa];
		}


		for (i = j = 0; i < len; i++) {
			char buf[4];
			of_unichar_t chr;
			size_t chr_bytes;

			if (!(str[i] & 0x80)) {
				string[j++] = str[i];
				continue;
			}

			chr = table[(uint8_t)str[i]];

			if (chr == 0xFFFD)
				@throw [OFInvalidEncodingException
				    newWithClass: isa];


			isUTF8 = YES;
			chr_bytes = of_string_unicode_to_utf8(chr, buf);

			if (chr_bytes == 0)








				@throw [OFInvalidEncodingException
				    newWithClass: isa];


			length += chr_bytes - 1;

			string = [self resizeMemory: string
					     toSize: length + 1];












			memcpy(string + j, buf, chr_bytes);
			j += chr_bytes;
		}


		string[length] = 0;
	} @catch (id e) {








		[self release];
		@throw e;
	}

	return self;
}

- initWithCString: (const char*)str
	   length: (size_t)len
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531


























532
533
534



535
536
537
538
539
540
541

	return ret;
}

- initWithFormat: (OFString*)fmt
       arguments: (va_list)args
{
	int t;

	self = [super init];

	if (fmt == nil) {
		Class c = isa;
		[super dealloc];
		@throw [OFInvalidArgumentException newWithClass: c
						       selector: _cmd];
	}

	if ((t = vasprintf(&string, [fmt cString], args)) == -1) {
		Class c = isa;
		[super dealloc];

		/*
		 * This is only the most likely error to happen. Unfortunately,
		 * there is no good way to check what really happened.
		 */
		@throw [OFOutOfMemoryException newWithClass: c];
	}
	length = t;

	switch (of_string_check_utf8(string, length)) {
	case 1:
		isUTF8 = YES;
		break;
	case -1:;
		Class c = isa;
		free(string);
		[super dealloc];
		@throw [OFInvalidEncodingException newWithClass: c];
	}

	@try {


























		[self addMemoryToPool: string];
	} @catch (OFException *e) {
		free(string);



		@throw e;
	}

	return self;
}

- initWithPath: (OFString*)first, ...







<
<


<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
>
>
>







448
449
450
451
452
453
454


455
456






























457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496

	return ret;
}

- initWithFormat: (OFString*)fmt
       arguments: (va_list)args
{


	self = [super init];































	@try {
		int t;

		if (fmt == nil)
			@throw [OFInvalidArgumentException newWithClass: isa
							       selector: _cmd];

		if ((t = vasprintf(&string, [fmt cString], args)) == -1)
			/*
			 * This is only the most likely error to happen.
			 * Unfortunately, there is no good way to check what
			 * really happened.
			 */
			@throw [OFOutOfMemoryException newWithClass: isa];

		@try {
			length = t;

			switch (of_string_check_utf8(string, length)) {
			case 1:
				isUTF8 = YES;
				break;
			case -1:
				@throw [OFInvalidEncodingException
				    newWithClass: isa];
			}

			[self addMemoryToPool: string];
		} @catch (id e) {
			free(string);
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithPath: (OFString*)first, ...
550
551
552
553
554
555
556



557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575

576
577
578
579
580
581

582
583
584
585
586
587
588

589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610




611
612
613
614
615
616
617
618


619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636

637
638
639
640
641
642
643
644
645
646
647



648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663




664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698

699




700
701
702
703
704
705
706

	return ret;
}

- initWithPath: (OFString*)first
     arguments: (va_list)args
{



	OFString *component;
	size_t len, i;
	va_list args2;
	Class c;

	self = [super init];

	len = [first cStringLength];
	switch (of_string_check_utf8([first cString], len)) {
	case 1:
		isUTF8 = YES;
		break;
	case -1:
		c = isa;
		[self dealloc];
		@throw [OFInvalidEncodingException newWithClass: c];
	}
	length += len;


	va_copy(args2, args);
	while ((component = va_arg(args2, OFString*)) != nil) {
		len = [component cStringLength];
		length += 1 + len;

		switch (of_string_check_utf8([component cString], len)) {

		case 1:
			isUTF8 = YES;
			break;
		case -1:
			c = isa;
			[self dealloc];
			@throw [OFInvalidEncodingException newWithClass: c];

		}
	}

	@try {
		string = [self allocMemoryWithSize: length + 1];
	} @catch (OFException *e) {
		[self dealloc];
		@throw e;
	}

	len = [first cStringLength];
	memcpy(string, [first cString], len);
	i = len;

	while ((component = va_arg(args, OFString*)) != nil) {
		len = [component cStringLength];
		string[i] = OF_PATH_DELIM;
		memcpy(string + i + 1, [component cString], len);
		i += len + 1;
	}

	string[i] = '\0';





	return self;
}

- initWithString: (OFString*)str
{
	self = [super init];



	string = (char*)[str cString];
	length = [str cStringLength];

	switch (of_string_check_utf8(string, length)) {
	case 1:
		isUTF8 = YES;
		break;
	case -1:;
		Class c = isa;
		[self dealloc];
		@throw [OFInvalidEncodingException newWithClass: c];
	}

	if ((string = strdup(string)) == NULL) {
		Class c = isa;
		[self dealloc];
		@throw [OFOutOfMemoryException newWithClass: c
						       size: length + 1];

	}

	@try {
		[self addMemoryToPool: string];
	} @catch (OFException *e) {
		/*
		 * We can't use [super dealloc] on OS X here.
		 * Compiler bug? Anyway, [self dealloc] will do here as we
		 * don't reimplement dealloc.
		 */
		free(string);



		[self dealloc];
		@throw e;
	}

	return self;
}

- initWithContentsOfFile: (OFString*)path
{
	return [self initWithContentsOfFile: path
				   encoding: OF_STRING_ENCODING_UTF_8];
}

- initWithContentsOfFile: (OFString*)path
		encoding: (enum of_string_encoding)encoding
{




	OFFile *file = nil;
	char *tmp;
	struct stat s;

	if (stat([path cString], &s) == -1) {
		Class c = isa;
		[super dealloc];
		@throw [OFInitializationFailedException newWithClass: c];
	}

	if ((tmp = malloc(s.st_size)) == NULL) {
		Class c = isa;
		[super dealloc];
		@throw [OFOutOfMemoryException newWithClass: c
						       size: s.st_size];
	}

	@try {
		file = [[OFFile alloc] initWithPath: path
					       mode: @"rb"];
		[file readExactlyNBytes: s.st_size
			     intoBuffer: tmp];
	} @catch (OFException *e) {
		free(tmp);
		[super dealloc];
		@throw e;
	} @finally {
		[file release];
	}

	@try {
		self = [self initWithCString: tmp
				    encoding: encoding
				      length: s.st_size];
	} @finally {

		free(tmp);




	}

	return self;
}

- (const char*)cString
{







>
>
>
|
|
|
<

|

<
|
|
|
|
|
<
<
|
|
<

>
|
|
|
|

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

<

<
<
<
<

|
|
|

|
|
|
|
|
|

|
>
>
>
>








>
>
|
|

|
|
|
|
|
<
<
|
|

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















>
>
>
>
|
|
|

|
<
<
|
<
<
<
<
<
|
<
|
|
<
|
|


<
<
<
<
<
<
|
<
<



|
>
|
>
>
>
>







505
506
507
508
509
510
511
512
513
514
515
516
517

518
519
520

521
522
523
524
525


526
527

528
529
530
531
532
533
534
535
536
537
538
539
540


541
542
543
544
545

546




547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581


582
583
584
585


586
587
588
589

590
591
592





593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621


622





623

624
625

626
627
628
629






630


631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647

	return ret;
}

- initWithPath: (OFString*)first
     arguments: (va_list)args
{
	self = [super init];

	@try {
		OFString *component;
		size_t len, i;
		va_list args2;


		length = [first cStringLength];


		switch (of_string_check_utf8([first cString], length)) {
		case 1:
			isUTF8 = YES;
			break;
		case -1:


			@throw [OFInvalidEncodingException newWithClass: isa];
		}


		/* Calculate length */
		va_copy(args2, args);
		while ((component = va_arg(args2, OFString*)) != nil) {
			len = [component cStringLength];
			length += 1 + len;

			switch (of_string_check_utf8([component cString],
			    len)) {
			case 1:
				isUTF8 = YES;
				break;
			case -1:


				@throw [OFInvalidEncodingException
				    newWithClass: isa];
			}
		}


		string = [self allocMemoryWithSize: length + 1];





		len = [first cStringLength];
		memcpy(string, [first cString], len);
		i = len;

		while ((component = va_arg(args, OFString*)) != nil) {
			len = [component cStringLength];
			string[i] = OF_PATH_DELIM;
			memcpy(string + i + 1, [component cString], len);
			i += len + 1;
		}

		string[i] = '\0';
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithString: (OFString*)str
{
	self = [super init];

	@try {
		/* We have no -[dealloc], so this is ok */
		string = (char*)[str cString];
		length = [str cStringLength];

		switch (of_string_check_utf8(string, length)) {
		case 1:
			isUTF8 = YES;
			break;
		case -1:;


			@throw [OFInvalidEncodingException newWithClass: isa];
		}

		if ((string = strdup(string)) == NULL)


			@throw [OFOutOfMemoryException newWithClass: isa
							       size: length +
								     1];


		@try {
			[self addMemoryToPool: string];
		} @catch (id e) {





			free(string);
			@throw e;
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithContentsOfFile: (OFString*)path
{
	return [self initWithContentsOfFile: path
				   encoding: OF_STRING_ENCODING_UTF_8];
}

- initWithContentsOfFile: (OFString*)path
		encoding: (enum of_string_encoding)encoding
{
	self = [super init];

	@try {
		OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
		OFFile *file;
		char *tmp;
		struct stat s;

		if (stat([path cString], &s) == -1)


			@throw [OFInitializationFailedException





			    newWithClass: isa];


		tmp = [self allocMemoryWithSize: s.st_size];

		file = [OFFile fileWithPath: path
				       mode: @"rb"];
		[file readExactlyNBytes: s.st_size
			     intoBuffer: tmp];









		self = [self initWithCString: tmp
				    encoding: encoding
				      length: s.st_size];

		[self freeMemory: tmp];

		[pool release];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (const char*)cString
{