ObjFW  Diff

Differences From Artifact [fbb622def6]:

To Artifact [cffef7d468]:


9
10
11
12
13
14
15

16
17
18
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
62
63
64

65
66
67
68

69
70
71
72

73
74
75
76
77
78
79
80

81
82
83
84


85
86
87

































88
89


90

















91
92
93
94

95
96









97
9
10
11
12
13
14
15
16
17
18




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
62
63
64
65
66
67
68
69
70
71
72

73
74
75
76
77
78
79
80
81
82
83
84
85

86
87
88
89
90
91
92
93


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110

111
112

113
114
115
116
117
118
119

120
121
122
123
124
125
126
127
128

129

130
131

132
133



134



135
136
137
138

139
140
141
142
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182

183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204


205
206
207
208
209
210
211
212
213
214







+


-
-
-
-

-


+
+
+
+
+
-
+

-
+


-
+

+
+
-
-
+
+
+
+
+
+
+

+
+
+
-
+

+
+
+
+
+
+
+
+
-
-
+
+

+
+
+
+
+
+
+
+
+
+
+
+
+
-
+

+
+
+
+
+
+
+
+
+
+
+
-
+
+




+
+
-
-
+
+
+
+
+

+
+
+
+
+
+
+
+
+
+
+
-
+

-
+




+
+
-
+
+
+
+





-
+
-


-
+

-
-
-
+
-
-
-




-
+




+
+
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+


+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+




+
-
-
+
+
+
+
+
+
+
+
+

 * the packaging of this file.
 */

#import "config.h"

#import <stdlib.h>
#import <string.h>
#import <wchar.h>

#import "OFString.h"
#import "OFConstCString.h"
#import "OFConstWideCString.h"
#import "OFCString.h"
#import "OFWideCString.h"
#import "OFExceptions.h"
#import "OFMacros.h"

@implementation OFString
+ new
{
	return [[OFString alloc] init];
}

+ newAsConstCString: (const char*)str
+ newFromCString: (const char*)str
{
	return [[OFConstCString alloc] initAsConstCString: str];
	return [[OFString alloc] initFromCString: str];
}

+ newAsConstWideCString: (const wchar_t*)str
+ newFromWideCString: (const wchar_t*)str
{
	return [[OFString alloc] initFromWideCString: str];
}
	return [[OFConstWideCString alloc] initAsConstWideCString: str];
}

- init
{
	if ((self = [super init])) {
		length = 0;
		string = NULL;
	}

	return self;
}

+ newAsCString: (char*)str
- initFromCString: (const char*)str
{
	if ((self = [super init])) {
		if (str == NULL) {
			length = 0;
			string = NULL;
		} else {
			if ((length = mbstowcs(NULL, str, 0)) == (size_t)-1) {
				/* FIXME: Throw exception */
				[super free];
	return [[OFCString alloc] initAsCString: str];
}
				return nil;
			}

			string = [self getMemForNItems: length + 1
						ofSize: sizeof(wchar_t)];

			if (mbstowcs(string, str, length) != length) {
				[super free];
				return nil;
			}
		}
	}

	return self;
}

+ newAsWideCString: (wchar_t*)str
- initFromWideCString: (const wchar_t*)str
{
	if ((self = [super init])) {
		if (str == NULL) {
			length = 0;
			string = NULL;
		} else {
			length = wcslen(str);
			string = [self getMemForNItems: length + 1
						ofSize: sizeof(wchar_t)];
			wmemcpy(string, str, length + 1);
		}
	}
	return [[OFWideCString alloc] initAsWideCString: str];

	return self;
}

- (char*)cString
{
	char *str;
	size_t len;
	OF_NOT_IMPLEMENTED(NULL)
}

	if ((len = wcstombs(NULL, string, 0)) == (size_t)-1) {
		/* FIXME: Throw exception */
		return NULL;
	}

	str = [self getMemWithSize: len];

	if (wcstombs(str, string, len) != len) {
		/* FIXME: Throw exception */
		[self freeMem: str];
		return NULL;
	}

	return str;
}

- (wchar_t*)wCString
- (wchar_t*)wideCString
{
	OF_NOT_IMPLEMENTED(NULL)
	return string;
}

- (size_t)length
{
	return length;
}
	OF_NOT_IMPLEMENTED(0)

- (OFString*)clone
{
	return [OFString newFromWideCString: string];
}

- (OFString*)setTo: (OFString*)str
{
	[self free];
	self = [str clone];
	return (self = [str clone]);
	return self;
}

- (OFString*)clone
- (int)compare: (OFString*)str
{
	OF_NOT_IMPLEMENTED(nil)
}

	return wcscmp(string, [str wideCString]);
- (int)compareTo: (OFString*)str
{
	OF_NOT_IMPLEMENTED(0)
}

- append: (OFString*)str
{
	OF_NOT_IMPLEMENTED(nil)
	return [self appendWideCString: [str wideCString]];
}

- appendCString: (const char*)str
{
	wchar_t	*newstr, *tmpstr;
	size_t	newlen, strlength;
	OF_NOT_IMPLEMENTED(nil)
}


	if (string == NULL) 
		return [self setTo: [OFString newFromCString: str]];

	if ((strlength = mbstowcs(NULL, str, 0)) == (size_t)-1) {
		/* FIXME: Throw exception */
		return nil;
	} 

	tmpstr = [self getMemForNItems: strlength + 1
				ofSize: sizeof(wchar_t)];

	if (mbstowcs(tmpstr, str, strlength) != strlength) {
		/* FIXME: Throw exception */
		[self freeMem: tmpstr];
		return nil;
	}

	newlen = length + strlength;
	newstr = [self resizeMem: string
			toNItems: newlen + 1
			  ofSize: sizeof(wchar_t)];

	wmemcpy(newstr + length, tmpstr, strlength + 1);

	length = newlen;
	string = newstr;

	[self freeMem: tmpstr];

	return self;
}

- appendWideCString: (const wchar_t*)str
{
	wchar_t	*newstr;
	size_t	newlen, strlength;
	OF_NOT_IMPLEMENTED(nil)

	if (string == NULL) 
		return [self setTo: [OFString newFromWideCString: str]];

	strlength = wcslen(str);
	newlen = length + strlength;

	newstr = [self resizeMem: string
			toNItems: newlen + 1
			  ofSize: sizeof(wchar_t)];

	wmemcpy(newstr + length, str, strlength + 1);

	length = newlen;
	string = newstr;

	return self;
}

- reverse
{
	size_t i, j, len = length / 2;
	OF_NOT_IMPLEMENTED(nil)
}

	for (i = 0, j = length - 1; i < len; i++, j--) {
		string[i] ^= string[j];
		string[j] ^= string[i];
		string[i] ^= string[j];
	}

	return self;
}
@end