ObjFW  Check-in [9555835784]

Overview
Comment:OFINICategory: Add support for arrays
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 95558357846131e4aeed8cb9e3eb2f4212eaeab5b8c5a3b8f837173f57cab3b0
User & Date: js on 2014-06-16 18:35:42
Other Links: manifest | tags
Context
2014-06-18
12:54
Add OFSettings check-in: 1333634935 user: js tags: trunk
2014-06-16
18:35
OFINICategory: Add support for arrays check-in: 9555835784 user: js tags: trunk
15:06
Make return type of -[OFArray objects] const check-in: 68d32a92c1 user: js tags: trunk
Changes

Modified src/OFINICategory.h from [c458e5b7fa] to [3d136493c4].

13
14
15
16
17
18
19

20
21
22
23
24
25
26
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#import "OFObject.h"

@class OFString;

@class OFMutableArray;

/*!
 * @class OFINICategory OFINICategory.h ObjFW/OFINICategory.h
 *
 * @brief A class for representing a category of an INI file.
 */







>







13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#import "OFObject.h"

@class OFString;
@class OFArray;
@class OFMutableArray;

/*!
 * @class OFINICategory OFINICategory.h ObjFW/OFINICategory.h
 *
 * @brief A class for representing a category of an INI file.
 */
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
 * @return The name of the INI category
 */
- (OFString*)name;

/*!
 * @brief Returns the string value for the specified key, or nil if it does not
 *	  exist.



 *
 * @param key The key for which the string value should be returned
 * @return The string value for the specified key, or nil if it does not exist
 */
- (OFString*)stringForKey: (OFString*)key;

/*!
 * @brief Returns the string value for the specified key or the specified
 *	  default value if it does not exist.



 *
 * @param key The key for which the string value should be returned
 * @param defaultValue The value to return if the key does not exist
 * @return The string value for the specified key or the specified default
 *	   value if it does not exist
 */
- (OFString*)stringForKey: (OFString*)key
	     defaultValue: (OFString*)defaultValue;

/*!
 * @brief Returns the integer value for the specified key or the specified
 *	  default value if it does not exist.



 *
 * @param key The key for which the integer value should be returned
 * @param defaultValue The value to return if the key does not exist
 * @return The integer value for the specified key or the specified default
 *	   value if it does not exist
 */
- (intmax_t)integerForKey: (OFString*)key
	     defaultValue: (intmax_t)defaultValue;

/*!
 * @brief Returns the bool value for the specified key or the specified default
 *	  value if it does not exist.



 *
 * @param key The key for which the bool value should be returned
 * @param defaultValue The value to return if the key does not exist
 * @return The bool value for the specified key or the specified default value
 *	   if it does not exist
 */
- (bool)boolForKey: (OFString*)key
      defaultValue: (bool)defaultValue;

/*!
 * @brief Returns the float value for the specified key or the specified
 *	  default value if it does not exist.



 *
 * @param key The key for which the float value should be returned
 * @param defaultValue The value to return if the key does not exist
 * @return The float value for the specified key or the specified default value
 *	   if it does not exist
 */
- (float)floatForKey: (OFString*)key
	defaultValue: (float)defaultValue;

/*!
 * @brief Returns the double value for the specified key or the specified
 *	  default value if it does not exist.



 *
 * @param key The key for which the double value should be returned
 * @param defaultValue The value to return if the key does not exist
 * @return The double value for the specified key or the specified default
 *	   value if it does not exist
 */
- (double)doubleForKey: (OFString*)key
	  defaultValue: (double)defaultValue;

/*!













 * @brief Sets the value of the specified key to the specified string.



 *
 * @param string The string to which the value of the key should be set
 * @param key The key for which the new value should be set
 */
- (void)setString: (OFString*)string
	   forKey: (OFString*)key;

/*!
 * @brief Sets the value of the specified key to the specified integer.



 *
 * @param integer The integer to which the value of the key should be set
 * @param key The key for which the new value should be set
 */
- (void)setInteger: (intmax_t)integer
	    forKey: (OFString*)key;

/*!
 * @brief Sets the value of the specified key to the specified bool.



 *
 * @param bool_ The bool to which the value of the key should be set
 * @param key The key for which the new value should be set
 */
- (void)setBool: (bool)bool_
	 forKey: (OFString*)key;

/*!
 * @brief Sets the value of the specified key to the specified float.



 *
 * @param float_ The float to which the value of the key should be set
 * @param key The key for which the new value should be set
 */
- (void)setFloat: (float)float_
	  forKey: (OFString*)key;

/*!
 * @brief Sets the value of the specified key to the specified double.



 *
 * @param double_ The double to which the value of the key should be set
 * @param key The key for which the new value should be set
 */
- (void)setDouble: (double)double_
	   forKey: (OFString*)key;

/*!















 * @brief Removes the value for the specified key



 *
 * @param key The key of the value to remove
 */
- (void)removeValueForKey: (OFString*)key;
@end







>
>
>









>
>
>












>
>
>












>
>
>












>
>
>












>
>
>










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

>
>
>









>
>
>









>
>
>









>
>
>









>
>
>








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

>
>
>





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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
 * @return The name of the INI category
 */
- (OFString*)name;

/*!
 * @brief Returns the string value for the specified key, or nil if it does not
 *	  exist.
 *
 * If the specified key is a multi-key (see @ref arrayForKey:), the value of
 * the first key/value pair found is returned.
 *
 * @param key The key for which the string value should be returned
 * @return The string value for the specified key, or nil if it does not exist
 */
- (OFString*)stringForKey: (OFString*)key;

/*!
 * @brief Returns the string value for the specified key or the specified
 *	  default value if it does not exist.
 *
 * If the specified key is a multi-key (see @ref arrayForKey:), the value of
 * the first key/value pair found is returned.
 *
 * @param key The key for which the string value should be returned
 * @param defaultValue The value to return if the key does not exist
 * @return The string value for the specified key or the specified default
 *	   value if it does not exist
 */
- (OFString*)stringForKey: (OFString*)key
	     defaultValue: (OFString*)defaultValue;

/*!
 * @brief Returns the integer value for the specified key or the specified
 *	  default value if it does not exist.
 *
 * If the specified key is a multi-key (see @ref arrayForKey:), the value of
 * the first key/value pair found is returned.
 *
 * @param key The key for which the integer value should be returned
 * @param defaultValue The value to return if the key does not exist
 * @return The integer value for the specified key or the specified default
 *	   value if it does not exist
 */
- (intmax_t)integerForKey: (OFString*)key
	     defaultValue: (intmax_t)defaultValue;

/*!
 * @brief Returns the bool value for the specified key or the specified default
 *	  value if it does not exist.
 *
 * If the specified key is a multi-key (see @ref arrayForKey:), the value of
 * the first key/value pair found is returned.
 *
 * @param key The key for which the bool value should be returned
 * @param defaultValue The value to return if the key does not exist
 * @return The bool value for the specified key or the specified default value
 *	   if it does not exist
 */
- (bool)boolForKey: (OFString*)key
      defaultValue: (bool)defaultValue;

/*!
 * @brief Returns the float value for the specified key or the specified
 *	  default value if it does not exist.
 *
 * If the specified key is a multi-key (see @ref arrayForKey:), the value of
 * the first key/value pair found is returned.
 *
 * @param key The key for which the float value should be returned
 * @param defaultValue The value to return if the key does not exist
 * @return The float value for the specified key or the specified default value
 *	   if it does not exist
 */
- (float)floatForKey: (OFString*)key
	defaultValue: (float)defaultValue;

/*!
 * @brief Returns the double value for the specified key or the specified
 *	  default value if it does not exist.
 *
 * If the specified key is a multi-key (see @ref arrayForKey:), the value of
 * the first key/value pair found is returned.
 *
 * @param key The key for which the double value should be returned
 * @param defaultValue The value to return if the key does not exist
 * @return The double value for the specified key or the specified default
 *	   value if it does not exist
 */
- (double)doubleForKey: (OFString*)key
	  defaultValue: (double)defaultValue;

/*!
 * @brief Returns an array of string values for the specified multi-key, or an
 *	  empty array if the key does not exist.
 *
 * A multi-key is a key which exists several times in the same category. Each
 * occurrence of the key/value pair adds the respective value to the array.
 *
 * @param key The multi-key for which the array should be returned
 * @return The array for the specified key, or an empty array if it does not
 *	   exist
 */
- (OFArray*)arrayForKey: (OFString*)key;

/*!
 * @brief Sets the value of the specified key to the specified string.
 *
 * If the specified key is a multi-key (see @ref arrayForKey:), the value of
 * the first key/value pair found is changed.
 *
 * @param string The string to which the value of the key should be set
 * @param key The key for which the new value should be set
 */
- (void)setString: (OFString*)string
	   forKey: (OFString*)key;

/*!
 * @brief Sets the value of the specified key to the specified integer.
 *
 * If the specified key is a multi-key (see @ref arrayForKey:), the value of
 * the first key/value pair found is changed.
 *
 * @param integer The integer to which the value of the key should be set
 * @param key The key for which the new value should be set
 */
- (void)setInteger: (intmax_t)integer
	    forKey: (OFString*)key;

/*!
 * @brief Sets the value of the specified key to the specified bool.
 *
 * If the specified key is a multi-key (see @ref arrayForKey:), the value of
 * the first key/value pair found is changed.
 *
 * @param bool_ The bool to which the value of the key should be set
 * @param key The key for which the new value should be set
 */
- (void)setBool: (bool)bool_
	 forKey: (OFString*)key;

/*!
 * @brief Sets the value of the specified key to the specified float.
 *
 * If the specified key is a multi-key (see @ref arrayForKey:), the value of
 * the first key/value pair found is changed.
 *
 * @param float_ The float to which the value of the key should be set
 * @param key The key for which the new value should be set
 */
- (void)setFloat: (float)float_
	  forKey: (OFString*)key;

/*!
 * @brief Sets the value of the specified key to the specified double.
 *
 * If the specified key is a multi-key (see @ref arrayForKey:), the value of
 * the first key/value pair found is changed.
 *
 * @param double_ The double to which the value of the key should be set
 * @param key The key for which the new value should be set
 */
- (void)setDouble: (double)double_
	   forKey: (OFString*)key;

/*!
 * @brief Sets the specified multi-key to the specified array of strings.
 *
 * It replaces the first occurrence of the multi-key with several key/value
 * pairs and removes all following occurrences. If the multi-key does not exist
 * yet, it is appended to the section.
 *
 * See also @ref arrayForKey: for more information about multi-keys.
 *
 * @param array The array of strings to which the multi-key should be set
 * @param key The multi-key for which the new values should be set
 */
- (void)setArray: (OFArray*)array
	  forKey: (OFString*)key;

/*!
 * @brief Removes the value for the specified key
 *
 * If the specified key is a multi-key (see @ref arrayForKey:), all key/value
 * pairs matching the specified key are removed.
 *
 * @param key The key of the value to remove
 */
- (void)removeValueForKey: (OFString*)key;
@end

Modified src/OFINICategory.m from [077a8d208b] to [3582bcd6d7].

302
303
304
305
306
307
308


























309
310
311
312
313
314
315
	else
		ret = defaultValue;

	objc_autoreleasePoolPop(pool);

	return ret;
}



























- (void)setString: (OFString*)string
	   forKey: (OFString*)key
{
	void *pool = objc_autoreleasePoolPush();
	OFEnumerator *enumerator = [_lines objectEnumerator];
	OFINICategory_Pair *pair;







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







302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
	else
		ret = defaultValue;

	objc_autoreleasePoolPop(pool);

	return ret;
}

- (OFArray*)arrayForKey: (OFString*)key
{
	OFMutableArray *ret = [OFMutableArray array];
	void *pool = objc_autoreleasePoolPush();
	OFEnumerator *enumerator = [_lines objectEnumerator];
	id line;

	while ((line = [enumerator nextObject]) != nil) {
		OFINICategory_Pair *pair;

		if (![line isKindOfClass: [OFINICategory_Pair class]])
			continue;

		pair = line;

		if ([pair->_key isEqual: key])
			[ret addObject: [[pair->_value copy] autorelease]];
	}

	objc_autoreleasePoolPop(pool);

	[ret makeImmutable];

	return ret;
}

- (void)setString: (OFString*)string
	   forKey: (OFString*)key
{
	void *pool = objc_autoreleasePoolPush();
	OFEnumerator *enumerator = [_lines objectEnumerator];
	OFINICategory_Pair *pair;
375
376
377
378
379
380
381
382

383
384
385
386



387

388



389
390





391
392
393














394

395






































396
397
398
399
400
401
402
403


404


405
406
407
408
409
410
411
412
413
	void *pool = objc_autoreleasePoolPush();

	[self setString: [OFString stringWithFormat: @"%g", double_]
		 forKey: key];

	objc_autoreleasePoolPop(pool);
}


- (void)removeValueForKey: (OFString*)key
{
	void *pool = objc_autoreleasePoolPush();
	OFEnumerator *enumerator = [_lines objectEnumerator];



	size_t i;

	id line;




	i = 0;





	while ((line = [enumerator nextObject]) != nil) {
		OFINICategory_Pair *pair;















		if (![line isKindOfClass: [OFINICategory_Pair class]]) {

			i++;






































			continue;
		}

		pair = line;

		if ([pair->_key isEqual: key]) {
			[_lines removeObjectAtIndex: i];
			break;


		}



		i++;
	}

	objc_autoreleasePoolPop(pool);
}

- (bool)OF_writeToStream: (OFStream*)stream
		encoding: (of_string_encoding_t)encoding








>
|

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


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

|
<
|



|
>
>
|
>
>
|
<







401
402
403
404
405
406
407
408
409
410
411
412
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489

490
491
492
493
494
495
496
497
498
499
500

501
502
503
504
505
506
507
	void *pool = objc_autoreleasePoolPush();

	[self setString: [OFString stringWithFormat: @"%g", double_]
		 forKey: key];

	objc_autoreleasePoolPop(pool);
}

- (void)setArray: (OFArray*)array
	  forKey: (OFString*)key
{
	void *pool;
	OFEnumerator *enumerator;
	OFMutableArray *pairs;
	id object;
	id const *lines;
	size_t i, count;
	bool replaced;

	if ([array count] == 0) {
		[self removeValueForKey: key];
		return;
	}

	pool = objc_autoreleasePoolPush();

	enumerator = [array objectEnumerator];
	pairs = [OFMutableArray arrayWithCapacity: [array count]];

	while ((object = [enumerator nextObject]) != nil) {
		OFINICategory_Pair *pair;

		pair = [[[OFINICategory_Pair alloc] init] autorelease];
		pair->_key = [key copy];
		pair->_value = [object copy];

		[pairs addObject: pair];
	}

	lines = [_lines objects];
	count = [_lines count];
	replaced = false;

	for (i = 0; i < count; i++) {
		OFINICategory_Pair *pair;

		if (![lines[i] isKindOfClass: [OFINICategory_Pair class]])
			continue;

		pair = lines[i];

		if ([pair->_key isEqual: key]) {
			[_lines removeObjectAtIndex: i];

			if (!replaced) {
				[_lines insertObjectsFromArray: pairs
						       atIndex: i];

				replaced = true;
				/* Continue after inserted pairs */
				i += [array count] - 1;
			} else
				i--;	/* Continue at same position */

			lines = [_lines objects];
			count = [_lines count];

			continue;
		}
	}

	if (!replaced)
		[_lines addObjectsFromArray: pairs];

	objc_autoreleasePoolPop(pool);
}

- (void)removeValueForKey: (OFString*)key
{
	void *pool = objc_autoreleasePoolPush();
	id const *lines = [_lines objects];
	size_t i, count = [_lines count];

	for (i = 0; i < count; i++) {
		OFINICategory_Pair *pair;

		if (![lines[i] isKindOfClass: [OFINICategory_Pair class]])
			continue;


		pair = lines[i];

		if ([pair->_key isEqual: key]) {
			[_lines removeObjectAtIndex: i];

			lines = [_lines objects];
			count = [_lines count];

			i--;	/* Continue at same position */
			continue;
		}

	}

	objc_autoreleasePoolPop(pool);
}

- (bool)OF_writeToStream: (OFStream*)stream
		encoding: (of_string_encoding_t)encoding

Modified tests/OFINIFileTests.m from [4b57243305] to [c2abc78adb].

15
16
17
18
19
20
21

22
23
24
25
26
27
28
 */

#include "config.h"

#import "OFINIFile.h"
#import "OFINICategory.h"
#import "OFString.h"

#import "OFFile.h"
#import "OFAutoreleasePool.h"

#import "TestsAppDelegate.h"

#ifdef _WIN32
# define NL @"\r\n"







>







15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 */

#include "config.h"

#import "OFINIFile.h"
#import "OFINICategory.h"
#import "OFString.h"
#import "OFArray.h"
#import "OFFile.h"
#import "OFAutoreleasePool.h"

#import "TestsAppDelegate.h"

#ifdef _WIN32
# define NL @"\r\n"
49
50
51
52
53
54
55


56
57
58

59
60
61
62
63
64
65
	    @"qux2=\"a\\f\"" NL
	    @"qux3=a\fb" NL
	    NL
	    @"[types]" NL
	    @"integer=16" NL
	    @"bool=false" NL
	    @"float=0.25" NL


	    @"double=0.75" NL;
	OFINIFile *file;
	OFINICategory *tests, *foobar, *types;


	TEST(@"+[fileWithPath:encoding:]",
	    (file = [OFINIFile fileWithPath: @"testfile.ini"
				   encoding: OF_STRING_ENCODING_CODEPAGE_437]))

	tests = [file categoryForName: @"tests"];
	foobar = [file categoryForName: @"foobar"];







>
>



>







50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
	    @"qux2=\"a\\f\"" NL
	    @"qux3=a\fb" NL
	    NL
	    @"[types]" NL
	    @"integer=16" NL
	    @"bool=false" NL
	    @"float=0.25" NL
	    @"array1=foo" NL
	    @"array1=bar" NL
	    @"double=0.75" NL;
	OFINIFile *file;
	OFINICategory *tests, *foobar, *types;
	OFArray *array;

	TEST(@"+[fileWithPath:encoding:]",
	    (file = [OFINIFile fileWithPath: @"testfile.ini"
				   encoding: OF_STRING_ENCODING_CODEPAGE_437]))

	tests = [file categoryForName: @"tests"];
	foobar = [file categoryForName: @"foobar"];
104
105
106
107
108
109
110
111










112
113

114
115
116
117
118
119
120

	TEST(@"-[doubleForKey:defaultValue:]",
	    [types doubleForKey: @"double"
		   defaultValue: 3] == 0.25)

	TEST(@"-[setDouble:forKey:]", R([types setDouble: 0.75
						  forKey: @"double"]))











	TEST(@"-[removeValueForKey:]",
	    R([foobar removeValueForKey: @"quxqux "]))


	module = @"OFINIFile";

	/* FIXME: Find a way to write files on Nintendo DS */
#ifndef OF_NINTENDO_DS
	TEST(@"-[writeToFile:encoding:]",
	    R([file writeToFile: @"tmpfile.ini"








>
>
>
>
>
>
>
>
>
>

|
>







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

	TEST(@"-[doubleForKey:defaultValue:]",
	    [types doubleForKey: @"double"
		   defaultValue: 3] == 0.25)

	TEST(@"-[setDouble:forKey:]", R([types setDouble: 0.75
						  forKey: @"double"]))

	array = [OFArray arrayWithObjects: @"1", @"2", nil];
	TEST(@"-[arrayForKey:]",
	    [[types arrayForKey: @"array1"] isEqual: array] &&
	    [[types arrayForKey: @"array2"] isEqual: array] &&
	    [[types arrayForKey: @"array3"] isEqual: [OFArray array]])

	array = [OFArray arrayWithObjects: @"foo", @"bar", nil];
	TEST(@"-[setArray:forKey:]", R([types setArray: array
						forKey: @"array1"]))

	TEST(@"-[removeValueForKey:]",
	    R([foobar removeValueForKey: @"quxqux "]) &&
	    R([types removeValueForKey: @"array2"]))

	module = @"OFINIFile";

	/* FIXME: Find a way to write files on Nintendo DS */
#ifndef OF_NINTENDO_DS
	TEST(@"-[writeToFile:encoding:]",
	    R([file writeToFile: @"tmpfile.ini"

Modified tests/testfile.ini from [a2b6991eb2] to [828829461b].

10
11
12
13
14
15
16


17


quxquxqux="hello\"w”rld"
qux2="a\f"

[types]
integer = 0x20
bool = true
float = 0.5


double = 0.25









>
>

>
>
10
11
12
13
14
15
16
17
18
19
20
21
quxquxqux="hello\"w”rld"
qux2="a\f"

[types]
integer = 0x20
bool = true
float = 0.5
array1 = 1
array2 = 1
double = 0.25
array1 = 2
array2 = 2