ObjFW  Diff

Differences From Artifact [857f08a203]:

To Artifact [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];
	}