ObjFW  Diff

Differences From Artifact [0e47b76592]:

To Artifact [5ced3a325d]:


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
 */

#include "config.h"

#import "OFINICategory.h"
#import "OFINICategory+Private.h"
#import "OFArray.h"

#import "OFString.h"
#import "OFStream.h"

#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"

@interface OFINICategoryPair: OFObject
{
@public
	OFString *_key, *_value;
}
@end

@interface OFINICategoryComment: OFObject
{
@public
	OFString *_comment;
}
@end



static OFString *
escapeString(OFString *string)
{
	OFMutableString *mutableString;

	/* FIXME: Optimize */
	if (![string hasPrefix: @" "] && ![string hasPrefix: @"\t"] &&
	    ![string hasPrefix: @"\f"] && ![string hasSuffix: @" "] &&
	    ![string hasSuffix: @"\t"] && ![string hasSuffix: @"\f"] &&
	    ![string containsString: @"\""])

		return string;

	mutableString = [[string mutableCopy] autorelease];

	[mutableString replaceOccurrencesOfString: @"\\" withString: @"\\\\"];
	[mutableString replaceOccurrencesOfString: @"\f" withString: @"\\f"];
	[mutableString replaceOccurrencesOfString: @"\r" withString: @"\\r"];
	[mutableString replaceOccurrencesOfString: @"\n" withString: @"\\n"];
	[mutableString replaceOccurrencesOfString: @"\"" withString: @"\\\""];

	[mutableString insertString: @"\"" atIndex: 0];
	[mutableString appendString: @"\""];

	[mutableString makeImmutable];

	return mutableString;
}

static OFString *
unescapeString(OFString *string)
{
	OFMutableString *mutableString;

	if (![string hasPrefix: @"\""] || ![string hasSuffix: @"\""])
		return string;

	string = [string substringWithRange: OFMakeRange(1, string.length - 2)];
	mutableString = [[string mutableCopy] autorelease];

	[mutableString replaceOccurrencesOfString: @"\\f" withString: @"\f"];
	[mutableString replaceOccurrencesOfString: @"\\r" withString: @"\r"];
	[mutableString replaceOccurrencesOfString: @"\\n" withString: @"\n"];
	[mutableString replaceOccurrencesOfString: @"\\\"" withString: @"\""];
	[mutableString replaceOccurrencesOfString: @"\\\\" withString: @"\\"];

	[mutableString makeImmutable];

	return mutableString;
}

@implementation OFINICategoryPair
- (void)dealloc







>
|
|


















>
>





<
|
<
|
|
>













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







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
 */

#include "config.h"

#import "OFINICategory.h"
#import "OFINICategory+Private.h"
#import "OFArray.h"
#import "OFCharacterSet.h"
#import "OFStream.h"
#import "OFString.h"

#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"

@interface OFINICategoryPair: OFObject
{
@public
	OFString *_key, *_value;
}
@end

@interface OFINICategoryComment: OFObject
{
@public
	OFString *_comment;
}
@end

static OFCharacterSet *needsEscapeCharacterSet;

static OFString *
escapeString(OFString *string)
{
	OFMutableString *mutableString;


	if (![string hasPrefix: @" "] && ![string hasSuffix: @" "] &&

	    ![string hasPrefix: @"\t"] && ![string hasSuffix: @"\t"] &&
	    [string indexOfCharacterFromSet: needsEscapeCharacterSet] ==
	    OFNotFound)
		return string;

	mutableString = [[string mutableCopy] autorelease];

	[mutableString replaceOccurrencesOfString: @"\\" withString: @"\\\\"];
	[mutableString replaceOccurrencesOfString: @"\f" withString: @"\\f"];
	[mutableString replaceOccurrencesOfString: @"\r" withString: @"\\r"];
	[mutableString replaceOccurrencesOfString: @"\n" withString: @"\\n"];
	[mutableString replaceOccurrencesOfString: @"\"" withString: @"\\\""];

	[mutableString insertString: @"\"" atIndex: 0];
	[mutableString appendString: @"\""];























	[mutableString makeImmutable];

	return mutableString;
}

@implementation OFINICategoryPair
- (void)dealloc
119
120
121
122
123
124
125









126
127
128
129
130
131
132
{
	return [[_comment copy] autorelease];
}
@end

@implementation OFINICategory
@synthesize name = _name;










- (instancetype)of_initWithName: (OFString *)name OF_DIRECT
{
	self = [super init];

	@try {
		_name = [name copy];







>
>
>
>
>
>
>
>
>







99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
{
	return [[_comment copy] autorelease];
}
@end

@implementation OFINICategory
@synthesize name = _name;

+ (void)initialize
{
	if (self != [OFINICategory class])
		return;

	needsEscapeCharacterSet = [[OFCharacterSet alloc]
	    initWithCharactersInString: @"\r\n\f\"\\="];
}

- (instancetype)of_initWithName: (OFString *)name OF_DIRECT
{
	self = [super init];

	@try {
		_name = [name copy];
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
- (void)dealloc
{
	[_name release];
	[_lines release];

	[super dealloc];
}

- (void)of_parseLine: (OFString *)line

{

	if (![line hasPrefix: @";"]) {

		OFINICategoryPair *pair =
		    [[[OFINICategoryPair alloc] init] autorelease];




		OFString *key, *value;


		size_t pos;

		if ((pos = [line rangeOfString: @"="].location) == OFNotFound)


			@throw [OFInvalidFormatException exception];


		key = unescapeString([line substringToIndex: pos]
		    .stringByDeletingEnclosingWhitespaces);
		value = unescapeString([line substringFromIndex: pos + 1]

		    .stringByDeletingEnclosingWhitespaces);

		pair->_key = [key copy];









		pair->_value = [value copy];









		[_lines addObject: pair];


	} else {

		OFINICategoryComment *comment =
		    [[[OFINICategoryComment alloc] init] autorelease];




		comment->_comment = [line copy];


























































		[_lines addObject: comment];
	}

}

- (OFString *)stringValueForKey: (OFString *)key
{
	return [self stringValueForKey: key defaultValue: nil];
}









|
>

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

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


>
>
>
|
|
>
>
>
>
>

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







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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
- (void)dealloc
{
	[_name release];
	[_lines release];

	[super dealloc];
}

static void
parseQuoted(const char **cString, const char **start, size_t *length)
{
	bool inEscape = false;

	(*cString)++;
	*start = *cString;

	while (**cString != '\0') {
		if (inEscape)
			inEscape = false;
		else {
			if (**cString == '\\')
				inEscape = true;
			else if (**cString == '"')
				break;
		}
		(*cString)++;
	}
	if (**cString == '\0')
		@throw [OFInvalidFormatException exception];

	*length = *cString - *start;

	(*cString)++;

	while (OFASCIIIsSpace(**cString))
		(*cString)++;
}

static void
unescapeMutableString(OFMutableString *string)
{
	[string replaceOccurrencesOfString: @"\\f" withString: @"\f"];
	[string replaceOccurrencesOfString: @"\\r" withString: @"\r"];
	[string replaceOccurrencesOfString: @"\\n" withString: @"\n"];
	[string replaceOccurrencesOfString: @"\\\"" withString: @"\""];
	[string replaceOccurrencesOfString: @"\\\\" withString: @"\\"];
}

- (void)of_parseLine: (OFString *)line
{
	void *pool = objc_autoreleasePoolPush();
	const char *cString = line.UTF8String;
	bool keyIsQuoted = false, valueIsQuoted = false;
	const char *keyStart, *valueStart;
	size_t keyLength, valueLength;
	OFMutableString *key, *value;
	OFINICategoryPair *pair;

	while (OFASCIIIsSpace(*cString))
		cString++;

	if (*cString == ';' || *cString == '#') {
		OFINICategoryComment *comment =
		    [[[OFINICategoryComment alloc] init] autorelease];
		comment->_comment = [line copy];
		[_lines addObject: comment];
		return;
	}

	if (*cString == '"') {
		keyIsQuoted = true;
		parseQuoted(&cString, &keyStart, &keyLength);
	} else {
		keyStart = cString;

		while (*cString != '=' && *cString != '\0')
			cString++;

		keyLength = cString - keyStart;
	}

	if (*cString != '=')
		@throw [OFInvalidFormatException exception];
	cString++;

	while (OFASCIIIsSpace(*cString))
		cString++;

	if (*cString == '"') {
		valueIsQuoted = true;
		parseQuoted(&cString, &valueStart, &valueLength);
	} else {
		valueStart = cString;

		while (*cString != '\0')
			cString++;

		valueLength = cString - valueStart;
	}

	while (*cString != '\0') {
		if (!OFASCIIIsSpace(*cString))
			@throw [OFInvalidFormatException exception];

		cString++;
	}

	key = [OFMutableString stringWithUTF8String: keyStart
					     length: keyLength];
	value = [OFMutableString stringWithUTF8String: valueStart
					       length: valueLength];

	if (keyIsQuoted)
		unescapeMutableString(key);
	else
		[key deleteEnclosingWhitespaces];
	if (valueIsQuoted)
		unescapeMutableString(value);
	else
		[value deleteEnclosingWhitespaces];

	[key makeImmutable];
	[value makeImmutable];

	pair = [[[OFINICategoryPair alloc] init] autorelease];
	pair->_key = [key copy];
	pair->_value = [value copy];
	[_lines addObject: pair];

	objc_autoreleasePoolPop(pool);
}

- (OFString *)stringValueForKey: (OFString *)key
{
	return [self stringValueForKey: key defaultValue: nil];
}

475
476
477
478
479
480
481

482
483
484
485

486
487
488
489
490
491
492
- (bool)of_writeToStream: (OFStream *)stream
		encoding: (OFStringEncoding)encoding
		   first: (bool)first
{
	if (_lines.count == 0)
		return false;


	if (first)
		[stream writeFormat: @"[%@]\r\n", _name];
	else
		[stream writeFormat: @"\r\n[%@]\r\n", _name];


	for (id line in _lines) {
		if ([line isKindOfClass: [OFINICategoryComment class]]) {
			OFINICategoryComment *comment = line;
			[stream writeFormat: @"%@\r\n", comment->_comment];
		} else if ([line isKindOfClass: [OFINICategoryPair class]]) {
			OFINICategoryPair *pair = line;







>
|
|
|
|
>







558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
- (bool)of_writeToStream: (OFStream *)stream
		encoding: (OFStringEncoding)encoding
		   first: (bool)first
{
	if (_lines.count == 0)
		return false;

	if (_name.length > 0) {
		if (first)
			[stream writeFormat: @"[%@]\r\n", _name];
		else
			[stream writeFormat: @"\r\n[%@]\r\n", _name];
	}

	for (id line in _lines) {
		if ([line isKindOfClass: [OFINICategoryComment class]]) {
			OFINICategoryComment *comment = line;
			[stream writeFormat: @"%@\r\n", comment->_comment];
		} else if ([line isKindOfClass: [OFINICategoryPair class]]) {
			OFINICategoryPair *pair = line;