ObjFW  Check-in [dab4c26020]

Overview
Comment:OFINISection: Allow values in global section
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: dab4c260207a5f86b9febd4cc5220fa5eddbff12edaeffbd391b4d262f4dfff0
User & Date: js on 2024-08-24 10:25:56
Other Links: manifest | tags
Context
2024-08-24
10:39
OFINIFile: Allow # for comments check-in: 16ebc2d82a user: js tags: trunk
10:32
OFINICategory: Allow values in global category check-in: e2f6eea34e user: js tags: 1.1
10:25
OFINISection: Allow values in global section check-in: dab4c26020 user: js tags: trunk
10:14
Rename OFINICategory to OFINISection check-in: 0fead8a915 user: js tags: trunk
Changes

Modified src/OFINIFile.h from [56fd049fb8] to [e05af431e8].

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
{
	OFINISection *_prologue;
	OFMutableArray OF_GENERIC(OFINISection *) *_sections;
}

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







<







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(OFINISection *) *_sections;
}

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

Modified src/OFINIFile.m from [408c66dded] to [6a07a21f7e].

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 = [[OFINISection alloc] of_initWithName: nil];
		_sections = [[OFMutableArray alloc] init];


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

	return self;
}

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

	[super dealloc];
}

- (OFArray OF_GENERIC(OFINISection *) *)categories
{







<
|
>












<







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
}

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

	@try {

		_sections = [[OFMutableArray alloc] initWithObject:
		    [[[OFINISection alloc] of_initWithName: @""] autorelease]];

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

	return self;
}

- (void)dealloc
{

	[_sections release];

	[super dealloc];
}

- (OFArray OF_GENERIC(OFINISection *) *)categories
{
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
	return [self sectionForName: name];
}

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

	if (encoding == OFStringEncodingAutodetect)
		encoding = OFStringEncodingUTF8;

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







|







125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
	return [self sectionForName: name];
}

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

	if (encoding == OFStringEncodingAutodetect)
		encoding = OFStringEncodingUTF8;

	@try {
		file = [OFIRIHandler openItemAtIRI: IRI mode: @"r"];
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
			OFString *sectionName;

			if (![line hasSuffix: @"]"])
				@throw [OFInvalidFormatException exception];

			sectionName = [line substringWithRange:
			    OFMakeRange(1, line.length - 2)];



			section = [[[OFINISection alloc]
			    of_initWithName: sectionName] autorelease];
			[_sections addObject: section];
		} else



			[section 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 (OFINISection *section in _sections)
		if ([section of_writeToStream: file
				     encoding: encoding
					first: first])
			first = false;

	objc_autoreleasePoolPop(pool);







>
>
>



|
>
>
>

>
















<
<
<







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
			OFString *sectionName;

			if (![line hasSuffix: @"]"])
				@throw [OFInvalidFormatException exception];

			sectionName = [line substringWithRange:
			    OFMakeRange(1, line.length - 2)];
			if (sectionName.length == 0)
				@throw [OFInvalidFormatException exception];

			section = [[[OFINISection alloc]
			    of_initWithName: sectionName] autorelease];
			[_sections addObject: section];
		} else {
			if (section == nil)
				section = [self sectionForName: @""];

			[section 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 (OFINISection *section in _sections)
		if ([section of_writeToStream: file
				     encoding: encoding
					first: first])
			first = false;

	objc_autoreleasePoolPop(pool);

Modified src/OFINISection+Private.h from [eb63ba043a] to [97d51eca37].

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 OFINISection ()
- (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







|
<







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 OFINISection ()
- (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

Modified src/OFINISection.m from [80d783f695] to [8aabd85aa9].

155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
- (void)of_parseLine: (OFString *)line
{
	if (![line hasPrefix: @";"]) {
		OFINISectionPair *pair;
		OFString *key, *value;
		size_t pos;

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

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

		if ((pos = [line rangeOfString: @"="].location) == OFNotFound)
			@throw [OFInvalidFormatException exception];

		key = unescapeString([line substringToIndex: pos]
		    .stringByDeletingEnclosingWhitespaces);







<
<
<







155
156
157
158
159
160
161



162
163
164
165
166
167
168
- (void)of_parseLine: (OFString *)line
{
	if (![line hasPrefix: @";"]) {
		OFINISectionPair *pair;
		OFString *key, *value;
		size_t pos;




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

		if ((pos = [line rangeOfString: @"="].location) == OFNotFound)
			@throw [OFInvalidFormatException exception];

		key = unescapeString([line substringToIndex: pos]
		    .stringByDeletingEnclosingWhitespaces);
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
- (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) {







|







476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
- (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) {

Modified tests/OFINIFileTests.m from [0bacd762e4] to [08c72cb447].

107
108
109
110
111
112
113
114

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

- (void)testWriteToIRIEncoding
{
	OFString *expectedOutput = @"; Comment before sections\r\n"

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







|
>







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

- (void)testWriteToIRIEncoding
{
	OFString *expectedOutput = @"; Comment in global section\r\n"
	    @"global=yes\r\n"
	    @"\r\n"
	    @"[tests]\r\n"
	    @"foo=baz\r\n"
	    @"foobar=baz\r\n"
	    @";comment\r\n"
	    @"new=new\r\n"
	    @"\r\n"
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)testPairOutsideOfSectionRejected
{
	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







<
<
<
<
<
<
<
<
<
<
<

173
174
175
176
177
178
179











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











@end

Modified tests/testfile.ini from [3660cb00b0] to [f46e5f3bc4].

1

2
3
4
5
6
7
8
; Comment before sections


[tests]
foo = bar
foobar=baz
;comment

[foobar]
|
>







1
2
3
4
5
6
7
8
9
; Comment in global section
global=yes

[tests]
foo = bar
foobar=baz
;comment

[foobar]