ObjFW  Check-in [904d0639c4]

Overview
Comment:OFINIFile: Allow comments before first category
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 904d0639c46d2f90ac3a8ee330bf531548ceebd1d2cd10ce9a570f7c9a8299f9
User & Date: js on 2024-08-24 09:20:58
Other Links: manifest | tags
Context
2024-08-24
10:14
Rename OFINICategory to OFINISection check-in: 0fead8a915 user: js tags: trunk
09:22
OFINIFile: Allow comments before first category check-in: b1eae58a7a user: js tags: 1.1
09:20
OFINIFile: Allow comments before first category check-in: 904d0639c4 user: js tags: trunk
2024-08-23
23:29
README.md: Fix missing word check-in: 2fe7ea274e user: js tags: trunk
Changes

Modified src/OFINICategory+Private.h from [9fe10387de] to [f683fe929a].

22
23
24
25
26
27
28
29

30
31
32
33
34
35
36

OF_ASSUME_NONNULL_BEGIN

@class OFStream;

OF_DIRECT_MEMBERS
@interface OFINICategory ()
- (instancetype)of_initWithName: (OFString *)name OF_METHOD_FAMILY(init);

- (void)of_parseLine: (OFString *)line;
- (bool)of_writeToStream: (OFStream *)stream
		encoding: (OFStringEncoding)encoding
		   first: (bool)first;
@end

OF_ASSUME_NONNULL_END







|
>







22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

OF_ASSUME_NONNULL_BEGIN

@class OFStream;

OF_DIRECT_MEMBERS
@interface OFINICategory ()
- (instancetype)of_initWithName: (nullable OFString *)name
    OF_METHOD_FAMILY(init);
- (void)of_parseLine: (OFString *)line;
- (bool)of_writeToStream: (OFStream *)stream
		encoding: (OFStringEncoding)encoding
		   first: (bool)first;
@end

OF_ASSUME_NONNULL_END

Modified src/OFINICategory.m from [0e47b76592] to [8affd22517].

151
152
153
154
155
156
157
158
159
160
161
162





163
164
165
166
167
168
169

	[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);







|
<



>
>
>
>
>







151
152
153
154
155
156
157
158

159
160
161
162
163
164
165
166
167
168
169
170
171
172
173

	[super dealloc];
}

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

		OFString *key, *value;
		size_t pos;

		if (_name == nil)
			@throw [OFInvalidFormatException exception];

		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);
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;







>
|
|
|
|
>







479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
- (bool)of_writeToStream: (OFStream *)stream
		encoding: (OFStringEncoding)encoding
		   first: (bool)first
{
	if (_lines.count == 0)
		return false;

	if (_name != nil) {
		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;

Modified src/OFINIFile.h from [bd21f45448] to [44ccbfc4a0].

30
31
32
33
34
35
36

37
38
39
40
41
42
43
 * @class OFINIFile OFINIFile.h ObjFW/ObjFW.h
 *
 * @brief A class for reading, creating and modifying INI files.
 */
OF_SUBCLASSING_RESTRICTED
@interface OFINIFile: OFObject
{

	OFMutableArray OF_GENERIC(OFINICategory *) *_categories;
}

/**
 * @brief All categories in the INI file.
 */
@property (readonly, nonatomic) OFArray OF_GENERIC(OFINICategory *) *categories;







>







30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
 * @class OFINIFile OFINIFile.h ObjFW/ObjFW.h
 *
 * @brief A class for reading, creating and modifying INI files.
 */
OF_SUBCLASSING_RESTRICTED
@interface OFINIFile: OFObject
{
	OFINICategory *_prologue;
	OFMutableArray OF_GENERIC(OFINICategory *) *_categories;
}

/**
 * @brief All categories in the INI file.
 */
@property (readonly, nonatomic) OFArray OF_GENERIC(OFINICategory *) *categories;

Modified src/OFINIFile.m from [8879da27e2] to [e87f1f698c].

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
}

- (instancetype)initWithIRI: (OFIRI *)IRI encoding: (OFStringEncoding)encoding
{
	self = [super init];

	@try {

		_categories = [[OFMutableArray alloc] init];

		[self of_parseIRI: IRI encoding: encoding];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{

	[_categories release];

	[super dealloc];
}

- (OFINICategory *)categoryForName: (OFString *)name
{







>













>







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
}

- (instancetype)initWithIRI: (OFIRI *)IRI encoding: (OFStringEncoding)encoding
{
	self = [super init];

	@try {
		_prologue = [[OFINICategory alloc] of_initWithName: nil];
		_categories = [[OFMutableArray alloc] init];

		[self of_parseIRI: IRI encoding: encoding];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[_prologue release];
	[_categories release];

	[super dealloc];
}

- (OFINICategory *)categoryForName: (OFString *)name
{
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
	return category;
}

- (void)of_parseIRI: (OFIRI *)IRI encoding: (OFStringEncoding)encoding
{
	void *pool = objc_autoreleasePoolPush();
	OFStream *file;
	OFINICategory *category = nil;
	OFString *line;

	if (encoding == OFStringEncodingAutodetect)
		encoding = OFStringEncodingUTF8;

	@try {
		file = [OFIRIHandler openItemAtIRI: IRI mode: @"r"];







|







116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
	return category;
}

- (void)of_parseIRI: (OFIRI *)IRI encoding: (OFStringEncoding)encoding
{
	void *pool = objc_autoreleasePoolPush();
	OFStream *file;
	OFINICategory *category = _prologue;
	OFString *line;

	if (encoding == OFStringEncodingAutodetect)
		encoding = OFStringEncodingUTF8;

	@try {
		file = [OFIRIHandler openItemAtIRI: IRI mode: @"r"];
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
				@throw [OFInvalidFormatException exception];

			categoryName = [line substringWithRange:
			    OFMakeRange(1, line.length - 2)];
			category = [[[OFINICategory alloc]
			    of_initWithName: categoryName] autorelease];
			[_categories addObject: category];
		} else {
			if (category == nil)
				@throw [OFInvalidFormatException exception];

			[category of_parseLine: line];
		}
	}

	objc_autoreleasePoolPop(pool);
}

- (void)writeToIRI: (OFIRI *)IRI
{
	[self writeToIRI: IRI encoding: OFStringEncodingUTF8];
}

- (void)writeToIRI: (OFIRI *)IRI encoding: (OFStringEncoding)encoding
{
	void *pool = objc_autoreleasePoolPush();
	OFStream *file = [OFIRIHandler openItemAtIRI: IRI mode: @"w"];
	bool first = true;




	for (OFINICategory *category in _categories)
		if ([category of_writeToStream: file
				      encoding: encoding
					 first: first])
			first = false;








|
<
<
<

<















>
>
>







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
				@throw [OFInvalidFormatException exception];

			categoryName = [line substringWithRange:
			    OFMakeRange(1, line.length - 2)];
			category = [[[OFINICategory alloc]
			    of_initWithName: categoryName] autorelease];
			[_categories addObject: category];
		} else



			[category of_parseLine: line];

	}

	objc_autoreleasePoolPop(pool);
}

- (void)writeToIRI: (OFIRI *)IRI
{
	[self writeToIRI: IRI encoding: OFStringEncodingUTF8];
}

- (void)writeToIRI: (OFIRI *)IRI encoding: (OFStringEncoding)encoding
{
	void *pool = objc_autoreleasePoolPush();
	OFStream *file = [OFIRIHandler openItemAtIRI: IRI mode: @"w"];
	bool first = true;

	if ([_prologue of_writeToStream: file encoding: encoding first: true])
		first = false;

	for (OFINICategory *category in _categories)
		if ([category of_writeToStream: file
				      encoding: encoding
					 first: first])
			first = false;

Modified tests/OFINIFileTests.m from [97d500cefe] to [6b63270541].

17
18
19
20
21
22
23


24
25
26
27
28
29
30
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#import "ObjFW.h"
#import "ObjFWTest.h"



@interface OFINIFileTests: OTTestCase
{
	OFINIFile *_file;
}
@end








>
>







17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#import "ObjFW.h"
#import "ObjFWTest.h"

#import "OFEmbeddedIRIHandler.h"

@interface OFINIFileTests: OTTestCase
{
	OFINIFile *_file;
}
@end

105
106
107
108
109
110
111
112


113
114
115
116
117
118
119
	OTAssertEqualObjects([types arrayValueForKey: @"array2"], array);
	OTAssertEqualObjects([types arrayValueForKey: @"array3"],
	    [OFArray array]);
}

- (void)testWriteToIRIEncoding
{
	OFString *expectedOutput = @"[tests]\r\n"


	    @"foo=baz\r\n"
	    @"foobar=baz\r\n"
	    @";comment\r\n"
	    @"new=new\r\n"
	    @"\r\n"
	    @"[foobar]\r\n"
	    @";foobarcomment\r\n"







|
>
>







107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
	OTAssertEqualObjects([types arrayValueForKey: @"array2"], array);
	OTAssertEqualObjects([types arrayValueForKey: @"array3"],
	    [OFArray array]);
}

- (void)testWriteToIRIEncoding
{
	OFString *expectedOutput = @"; Comment before categories\r\n"
	    @"\r\n"
	    @"[tests]\r\n"
	    @"foo=baz\r\n"
	    @"foobar=baz\r\n"
	    @";comment\r\n"
	    @"new=new\r\n"
	    @"\r\n"
	    @"[foobar]\r\n"
	    @";foobarcomment\r\n"
168
169
170
171
172
173
174











175
	} @finally {
		[[OFFileManager defaultManager] removeItemAtIRI: writeIRI];
	}
#else
	(void)expectedOutput;
#endif
}











@end







>
>
>
>
>
>
>
>
>
>
>

172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
	} @finally {
		[[OFFileManager defaultManager] removeItemAtIRI: writeIRI];
	}
#else
	(void)expectedOutput;
#endif
}

- (void)testValuePairOutsideOfCategoryRejected
{
	OFIRI *IRI = [OFIRI IRIWithString: @"embedded:testfile_broken.ini"];

	OFRegisterEmbeddedFile(@"testfile_broken.ini",
	    (const uint8_t *)"; comment\r\na=b", 14);

	OTAssertThrowsSpecific([OFINIFile fileWithIRI: IRI],
	    OFInvalidFormatException);
}
@end

Modified tests/testfile.ini from [9e2ef2dc1a] to [e974f469b7].



1
2
3
4
5
6
7


[tests]
foo = bar
foobar=baz
;comment

[foobar]
;foobarcomment
>
>







1
2
3
4
5
6
7
8
9
; Comment before categories

[tests]
foo = bar
foobar=baz
;comment

[foobar]
;foobarcomment