ObjFW  Diff

Differences From Artifact [3ff03f8457]:

To Artifact [06ae24a12e]:


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
{
	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







|













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







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
{
	OFMutableString *mutableString;

	/* FIXME: Optimize */
	if (![string hasPrefix: @" "] && ![string hasPrefix: @"\t"] &&
	    ![string hasPrefix: @"\f"] && ![string hasSuffix: @" "] &&
	    ![string hasSuffix: @"\t"] && ![string hasSuffix: @"\f"] &&
	    ![string containsString: @"\""] && ![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;
}

@implementation OFINICategoryPair
- (void)dealloc
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
- (void)dealloc
{
	[_name release];
	[_lines release];

	[super dealloc];
}









































- (void)of_parseLine: (OFString *)line
{
	if (![line hasPrefix: @";"] && ![line hasPrefix: @"#"]) {
		OFINICategoryPair *pair;

		OFString *key, *value;
		size_t pos;

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

		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];
}








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



|
|
>
|
|
|
|

<
<
|
|
<
<
<

<
<
|
<
<


<

<

>

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







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
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
- (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];
}