ObjFW  Check-in [7694d37135]

Overview
Comment:OFString: Add -[getCString:maxLength:encoding:].
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 7694d37135cc00865fedc1ab6e7072b0f323ec2878376ee8b0a9457f163edc1f
User & Date: js on 2013-01-16 14:27:07
Other Links: manifest | tags
Context
2013-01-16
14:31
OFRunLoop: Make ivar "running" volatile. check-in: b96e676256 user: js tags: trunk
14:27
OFString: Add -[getCString:maxLength:encoding:]. check-in: 7694d37135 user: js tags: trunk
2013-01-15
02:33
-[OFString cStringWithEncoding:]: Add Windows-1252 check-in: 7a14ffd2cb user: js tags: trunk
Changes

Modified src/OFString.h from [b1831ea39c] to [6da3527557].

558
559
560
561
562
563
564















565
566
567
568
569
570
571
 * @param URL The URL to the contents for the string
 * @param encoding The encoding to assume
 * @return An initialized OFString
 */
- initWithContentsOfURL: (OFURL*)URL
	       encoding: (of_string_encoding_t)encoding;
















/*!
 * @brief Returns the OFString as a C string in the specified encoding.
 *
 * The result is valid until the autorelease pool is released. If you want to
 * use the result outside the scope of the current autorelease pool, you have to
 * copy it.
 *







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







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
 * @param URL The URL to the contents for the string
 * @param encoding The encoding to assume
 * @return An initialized OFString
 */
- initWithContentsOfURL: (OFURL*)URL
	       encoding: (of_string_encoding_t)encoding;

/*!
 * @brief Writes the OFString into the specified C string with the specified
 *	  encoding.
 *
 * @param cString The C string to write into
 * @param maxLength The maximum number of bytes to write into the C string,
 *		    including the terminating zero
 * @param encoding The encoding to use for writing into the C string
 * @return The number of bytes written into the C string, without the
 *	   terminating zero
 */
- (size_t)getCString: (char*)cString
	   maxLength: (size_t)maxLength
	    encoding: (of_string_encoding_t)encoding;

/*!
 * @brief Returns the OFString as a C string in the specified encoding.
 *
 * The result is valid until the autorelease pool is released. If you want to
 * use the result outside the scope of the current autorelease pool, you have to
 * copy it.
 *

Modified src/OFString.m from [857f08a203] to [0f2f5910d1].

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

#include "config.h"

#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

#include <assert.h>

#include <sys/stat.h>

#import "OFString.h"
#import "OFString_UTF8.h"
#import "OFArray.h"
#import "OFDictionary.h"
#import "OFFile.h"







<
<







16
17
18
19
20
21
22


23
24
25
26
27
28
29

#include "config.h"

#include <stdarg.h>
#include <stdlib.h>
#include <string.h>



#include <sys/stat.h>

#import "OFString.h"
#import "OFString_UTF8.h"
#import "OFArray.h"
#import "OFDictionary.h"
#import "OFFile.h"
985
986
987
988
989
990
991


992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010








1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021

1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055


1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069


1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083


1084
1085
1086
1087
1088
1089
1090
		[self release];
		@throw e;
	}

	return self;
}



- (const char*)cStringWithEncoding: (of_string_encoding_t)encoding
{
	const of_unichar_t *characters = [self characters];
	size_t i, length = [self length];
	OFObject *object = [[[OFObject alloc] init] autorelease];
	char *cString;

	switch (encoding) {
	case OF_STRING_ENCODING_UTF_8:;
		size_t cStringLength, j = 0;

		cString = [object allocMemoryWithSize: (length * 4) + 1];
		cStringLength = length;

		for (i = 0; i < length; i++) {
			char buffer[4];
			size_t len = of_string_utf8_encode(characters[i],
			    buffer);









			switch (len) {
			case 1:
				cString[j++] = buffer[0];
				break;
			case 2:
				cStringLength++;

				memcpy(cString + j, buffer, 2);
				j += 2;

				break;

			case 3:
				cStringLength += 2;

				memcpy(cString + j, buffer, 3);
				j += 3;

				break;
			case 4:
				cStringLength += 3;

				memcpy(cString + j, buffer, 4);
				j += 4;

				break;
			default:
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];
			}
		}

		cString[j] = '\0';

		assert(j == cStringLength);

		@try {
			cString = [object resizeMemory: cString
						  size: cStringLength + 1];
		} @catch (OFOutOfMemoryException *e) {
			/* We don't care, as we only tried to make it smaller */
		}

		break;
	case OF_STRING_ENCODING_ASCII:
		cString = [object allocMemoryWithSize: length + 1];



		for (i = 0; i < length; i++) {
			if OF_UNLIKELY (characters[i] > 0x80)
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];

			cString[i] = (char)characters[i];
		}

		cString[i] = '\0';

		break;
	case OF_STRING_ENCODING_ISO_8859_1:
		cString = [object allocMemoryWithSize: length + 1];



		for (i = 0; i < length; i++) {
			if OF_UNLIKELY (characters[i] > 0xFF)
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];

			cString[i] = (uint8_t)characters[i];
		}

		cString[i] = '\0';

		break;
	case OF_STRING_ENCODING_ISO_8859_15:
		cString = [object allocMemoryWithSize: length + 1];



		for (i = 0; i < length; i++) {
			of_unichar_t c = characters[i];

			switch (c) {
			case 0xA4:
			case 0xA6:







>
>
|



<
<



|
<
<
<






>
>
>
>
>
>
>
>



<
<
<

<
<
<

>

<
<
<
<
<
<

<
<
|
|










<
|
<
<
<
<
<
<
<
<

|
>
>











|

|
>
>











|

|
>
>







983
984
985
986
987
988
989
990
991
992
993
994
995


996
997
998
999



1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016



1017



1018
1019
1020






1021


1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033

1034








1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
		[self release];
		@throw e;
	}

	return self;
}

- (size_t)getCString: (char*)cString
	   maxLength: (size_t)maxLength
	    encoding: (of_string_encoding_t)encoding
{
	const of_unichar_t *characters = [self characters];
	size_t i, length = [self length];



	switch (encoding) {
	case OF_STRING_ENCODING_UTF_8:;
		size_t j = 0;




		for (i = 0; i < length; i++) {
			char buffer[4];
			size_t len = of_string_utf8_encode(characters[i],
			    buffer);

			/*
			 * Check for one more than the current index, as we
			 * need one for the terminating zero.
			 */
			if (j + len >= maxLength)
				@throw [OFOutOfRangeException
				    exceptionWithClass: [self class]];

			switch (len) {
			case 1:
				cString[j++] = buffer[0];







				break;
			case 2:
			case 3:






			case 4:


				memcpy(cString + j, buffer, len);
				j += len;

				break;
			default:
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];
			}
		}

		cString[j] = '\0';


		return j;








	case OF_STRING_ENCODING_ASCII:
		if (length + 1 > maxLength)
			@throw [OFOutOfRangeException
			    exceptionWithClass: [self class]];

		for (i = 0; i < length; i++) {
			if OF_UNLIKELY (characters[i] > 0x80)
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];

			cString[i] = (char)characters[i];
		}

		cString[i] = '\0';

		return length;
	case OF_STRING_ENCODING_ISO_8859_1:
		if (length + 1 > maxLength)
			@throw [OFOutOfRangeException
			    exceptionWithClass: [self class]];

		for (i = 0; i < length; i++) {
			if OF_UNLIKELY (characters[i] > 0xFF)
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];

			cString[i] = (uint8_t)characters[i];
		}

		cString[i] = '\0';

		return length;
	case OF_STRING_ENCODING_ISO_8859_15:
		if (length + 1 > maxLength)
			@throw [OFOutOfRangeException
			    exceptionWithClass: [self class]];

		for (i = 0; i < length; i++) {
			of_unichar_t c = characters[i];

			switch (c) {
			case 0xA4:
			case 0xA6:
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139


1140
1141
1142
1143
1144
1145
1146
				}
			} else
				cString[i] = (uint8_t)c;
		}

		cString[i] = '\0';

		break;
	case OF_STRING_ENCODING_WINDOWS_1252:
		cString = [object allocMemoryWithSize: length + 1];



		for (i = 0; i < length; i++) {
			of_unichar_t c = characters[i];

			if OF_UNLIKELY (c >= 0x80 && c <= 0x9F)
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];







|

|
>
>







1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
				}
			} else
				cString[i] = (uint8_t)c;
		}

		cString[i] = '\0';

		return length;
	case OF_STRING_ENCODING_WINDOWS_1252:
		if (length + 1 > maxLength)
			@throw [OFOutOfRangeException
			    exceptionWithClass: [self class]];

		for (i = 0; i < length; i++) {
			of_unichar_t c = characters[i];

			if OF_UNLIKELY (c >= 0x80 && c <= 0x9F)
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];
1234
1235
1236
1237
1238
1239
1240










































1241
1242
1243
1244
1245
1246
1247
				}
			} else
				cString[i] = (uint8_t)c;
		}

		cString[i] = '\0';











































		break;
	default:
		@throw [OFNotImplementedException
		    exceptionWithClass: [self class]
			      selector: _cmd];
	}








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







1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
				}
			} else
				cString[i] = (uint8_t)c;
		}

		cString[i] = '\0';

		return length;
	default:
		@throw [OFNotImplementedException
		    exceptionWithClass: [self class]
			      selector: _cmd];
	}
}

- (const char*)cStringWithEncoding: (of_string_encoding_t)encoding
{
	OFObject *object = [[[OFObject alloc] init] autorelease];
	size_t length = [self length];
	char *cString;

	switch (encoding) {
	case OF_STRING_ENCODING_UTF_8:;
		size_t cStringLength;

		cString = [object allocMemoryWithSize: (length * 4) + 1];

		cStringLength = [self getCString: cString
				       maxLength: (length * 4) + 1
					encoding: OF_STRING_ENCODING_UTF_8];

		@try {
			cString = [object resizeMemory: cString
						  size: cStringLength + 1];
		} @catch (OFOutOfMemoryException *e) {
			/* We don't care, as we only tried to make it smaller */
		}

		break;
	case OF_STRING_ENCODING_ASCII:
	case OF_STRING_ENCODING_ISO_8859_1:
	case OF_STRING_ENCODING_ISO_8859_15:
	case OF_STRING_ENCODING_WINDOWS_1252:
		cString = [object allocMemoryWithSize: length + 1];

		[self getCString: cString
		       maxLength: length + 1
			encoding: encoding];

		break;
	default:
		@throw [OFNotImplementedException
		    exceptionWithClass: [self class]
			      selector: _cmd];
	}

Modified src/OFString_UTF8.m from [5a1fe9d9fc] to [17c27fdf19].

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
		s = &s_store;

		s->cString = [self allocMemoryWithSize: (length * 4) + 1];
		s->length = length;

		for (i = 0; i < length; i++) {
			char buffer[4];



			switch (of_string_utf8_encode(characters[i], buffer)) {
			case 1:
				s->cString[j++] = buffer[0];
				break;
			case 2:
				s->isUTF8 = YES;

				memcpy(s->cString + j, buffer, 2);
				j += 2;

				break;
			case 3:
				s->isUTF8 = YES;

				memcpy(s->cString + j, buffer, 3);
				j += 3;

				break;
			case 4:
				s->isUTF8 = YES;

				memcpy(s->cString + j, buffer, 4);
				j += 4;

				break;
			default:
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];
			}
		}







>
>

|




<
<
<
<
<
<

<
<
<
<
<
<



|
|







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
		s = &s_store;

		s->cString = [self allocMemoryWithSize: (length * 4) + 1];
		s->length = length;

		for (i = 0; i < length; i++) {
			char buffer[4];
			size_t len = of_string_utf8_encode(characters[i],
			    buffer);

			switch (len) {
			case 1:
				s->cString[j++] = buffer[0];
				break;
			case 2:






			case 3:






			case 4:
				s->isUTF8 = YES;

				memcpy(s->cString + j, buffer, len);
				j += len;

				break;
			default:
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];
			}
		}
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
			string++;
			length--;
		} else if (byteOrder != OF_BYTE_ORDER_NATIVE)
			swap = YES;

		s = &s_store;

		s->cStringLength = length;
		s->cString = [self allocMemoryWithSize: (length * 4) + 1];
		s->length = length;

		for (i = 0; i < length; i++) {
			char buffer[4];
			of_unichar_t character =
			    (swap ? OF_BSWAP16(string[i]) : string[i]);
			size_t characterLen;

			/* Missing high surrogate */
			if ((character & 0xFC00) == 0xDC00)
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];

			if ((character & 0xFC00) == 0xD800) {







<







|







474
475
476
477
478
479
480

481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
			string++;
			length--;
		} else if (byteOrder != OF_BYTE_ORDER_NATIVE)
			swap = YES;

		s = &s_store;


		s->cString = [self allocMemoryWithSize: (length * 4) + 1];
		s->length = length;

		for (i = 0; i < length; i++) {
			char buffer[4];
			of_unichar_t character =
			    (swap ? OF_BSWAP16(string[i]) : string[i]);
			size_t len;

			/* Missing high surrogate */
			if ((character & 0xFC00) == 0xDC00)
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];

			if ((character & 0xFC00) == 0xD800) {
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
					@throw [OFInvalidEncodingException
					    exceptionWithClass: [self class]];

				character = (((character & 0x3FF) << 10) |
				    (nextCharacter & 0x3FF)) + 0x10000;

				i++;
				s->cStringLength--;
				s->length--;
			}

			characterLen = of_string_utf8_encode(character, buffer);

			switch (characterLen) {
			case 1:
				s->cString[j++] = buffer[0];
				break;
			case 2:
				s->isUTF8 = YES;
				s->cStringLength++;

				memcpy(s->cString + j, buffer, 2);
				j += 2;

				break;
			case 3:
				s->isUTF8 = YES;
				s->cStringLength += 2;

				memcpy(s->cString + j, buffer, 3);
				j += 3;

				break;
			case 4:
				s->isUTF8 = YES;
				s->cStringLength += 3;

				memcpy(s->cString + j, buffer, 4);
				j += 4;

				break;
			default:
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];
			}
		}

		s->cString[j] = '\0';


		@try {
			s->cString = [self resizeMemory: s->cString
						   size: s->cStringLength + 1];
		} @catch (OFOutOfMemoryException *e) {
			/* We don't care, as we only tried to make it smaller */
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}







<



|

|




<
<
<
<
<
<
<

<
<
<
<
<
<
<


<

|
|









>



|







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
					@throw [OFInvalidEncodingException
					    exceptionWithClass: [self class]];

				character = (((character & 0x3FF) << 10) |
				    (nextCharacter & 0x3FF)) + 0x10000;

				i++;

				s->length--;
			}

			len = of_string_utf8_encode(character, buffer);

			switch (len) {
			case 1:
				s->cString[j++] = buffer[0];
				break;
			case 2:







			case 3:







			case 4:
				s->isUTF8 = YES;


				memcpy(s->cString + j, buffer, len);
				j += len;

				break;
			default:
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];
			}
		}

		s->cString[j] = '\0';
		s->cStringLength = j;

		@try {
			s->cString = [self resizeMemory: s->cString
						   size: j + 1];
		} @catch (OFOutOfMemoryException *e) {
			/* We don't care, as we only tried to make it smaller */
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}
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
		s = &s_store;

		s->cString = [self allocMemoryWithSize: (length * 4) + 1];
		s->length = length;

		for (i = 0; i < length; i++) {
			char buffer[4];
			size_t characterLen = of_string_utf8_encode(
			    (swap ? OF_BSWAP32(characters[i]) : characters[i]),
			    buffer);

			switch (characterLen) {
			case 1:
				s->cString[j++] = buffer[0];
				break;
			case 2:
				s->isUTF8 = YES;

				memcpy(s->cString + j, buffer, 2);
				j += 2;

				break;
			case 3:
				s->isUTF8 = YES;

				memcpy(s->cString + j, buffer, 3);
				j += 3;

				break;
			case 4:
				s->isUTF8 = YES;

				memcpy(s->cString + j, buffer, 4);
				j += 4;

				break;
			default:
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];
			}
		}







|



|




<
<
<
<
<
<

<
<
<
<
<
<



|
|







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
		s = &s_store;

		s->cString = [self allocMemoryWithSize: (length * 4) + 1];
		s->length = length;

		for (i = 0; i < length; i++) {
			char buffer[4];
			size_t len = of_string_utf8_encode(
			    (swap ? OF_BSWAP32(characters[i]) : characters[i]),
			    buffer);

			switch (len) {
			case 1:
				s->cString[j++] = buffer[0];
				break;
			case 2:






			case 3:






			case 4:
				s->isUTF8 = YES;

				memcpy(s->cString + j, buffer, len);
				j += len;

				break;
			default:
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];
			}
		}
771
772
773
774
775
776
777

























778
779
780
781
782
783
784
785
786
787
788

789
790
791
792
793
794
795
- (void)dealloc
{
	if (s != NULL && s->freeWhenDone != NULL)
		free(s->freeWhenDone);

	[super dealloc];
}


























- (const char*)cStringWithEncoding: (of_string_encoding_t)encoding
{
	switch (encoding) {
	case OF_STRING_ENCODING_UTF_8:
		return s->cString;
	case OF_STRING_ENCODING_ASCII:
		if (s->isUTF8)
			@throw [OFInvalidEncodingException
			    exceptionWithClass: [self class]];


		return s->cString;
	default:
		return [super cStringWithEncoding: encoding];
	}
}

- (const char*)UTF8String







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




<
<




|
>







733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768


769
770
771
772
773
774
775
776
777
778
779
780
781
- (void)dealloc
{
	if (s != NULL && s->freeWhenDone != NULL)
		free(s->freeWhenDone);

	[super dealloc];
}

- (size_t)getCString: (char*)cString
	   maxLength: (size_t)maxLength
	    encoding: (of_string_encoding_t)encoding
{
	switch (encoding) {
	case OF_STRING_ENCODING_ASCII:
		if (s->isUTF8)
			@throw [OFInvalidEncodingException
			    exceptionWithClass: [self class]];
		/* intentional fall-through */
	case OF_STRING_ENCODING_UTF_8:
		if (s->cStringLength + 1 > maxLength)
			@throw [OFOutOfRangeException
			    exceptionWithClass: [self class]];

		memcpy(cString, s->cString, s->cStringLength + 1);

		return s->cStringLength;
	default:
		return [super getCString: cString
			       maxLength: maxLength
				encoding: encoding];
	}
}

- (const char*)cStringWithEncoding: (of_string_encoding_t)encoding
{
	switch (encoding) {


	case OF_STRING_ENCODING_ASCII:
		if (s->isUTF8)
			@throw [OFInvalidEncodingException
			    exceptionWithClass: [self class]];
		/* intentional fall-through */
	case OF_STRING_ENCODING_UTF_8:
		return s->cString;
	default:
		return [super cStringWithEncoding: encoding];
	}
}

- (const char*)UTF8String