ObjFW  Check-in [995f217b65]

Overview
Comment:OFINIFile: Properly escape newlines
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 995f217b6507ebd116a580d93cda981945c8ef01a8a6ae37f41feddfec643029
User & Date: js on 2024-09-06 22:27:46
Other Links: manifest | tags
Context
2024-09-08
17:02
Add -[attributesOfItemAtIRI:] for embedded files check-in: 3f3746be03 user: js tags: trunk
2024-09-06
22:28
OFINIFile: Properly escape newlines check-in: 35df8d869a user: js tags: 1.1
22:27
OFINIFile: Properly escape newlines check-in: 995f217b65 user: js tags: trunk
2024-09-04
23:30
OFMemoryBarrier: Use sync on PowerPC check-in: 259abc30ce user: js tags: trunk
Changes

Modified src/OFINISection.m from [0b76cf6ad8] to [1775218a4e].

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

#include "config.h"

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

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

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

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

@interface OFINISectionComment: 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: @"\""] && ![string containsString: @"="])

		return string;

	mutableString = [[string mutableCopy] autorelease];

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







>
|
|


















>
>





<
|
<
|
|
>







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

#include "config.h"

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

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

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

@interface OFINISectionComment: 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"];
97
98
99
100
101
102
103









104
105
106
107
108
109
110
{
	return [[_comment copy] autorelease];
}
@end

@implementation OFINISection
@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 OFINISection
@synthesize name = _name;

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

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

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

	@try {
		_name = [name copy];

Modified tests/OFINIFileTests.m from [7041b472fb] to [f3bbd53585].

120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
	    @";comment\r\n"
	    @"new=new\r\n"
	    @"\r\n"
	    @"[foobar]\r\n"
	    @"#foobarcomment\r\n"
	    @"qux=\" asd\"\r\n"
	    @"quxquxqux=\"hello\\\"wörld\"\r\n"
	    @"qux2=\"a\\f\"\r\n"
	    @"\"asd=asd\"=foobar\r\n"
	    @"qux3=a\fb\r\n"
	    @"\r\n"
	    @"[types]\r\n"
	    @"integer=16\r\n"
	    @"bool=false\r\n"
	    @"float=0.25\r\n"
	    @"array1=foo\r\n"
	    @"array1=bar\r\n"







|

|







120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
	    @";comment\r\n"
	    @"new=new\r\n"
	    @"\r\n"
	    @"[foobar]\r\n"
	    @"#foobarcomment\r\n"
	    @"qux=\" asd\"\r\n"
	    @"quxquxqux=\"hello\\\"wörld\"\r\n"
	    @"qux2=\"a\\n\"\r\n"
	    @"\"asd=asd\"=foobar\r\n"
	    @"qux3=\"a\\fb\"\r\n"
	    @"\r\n"
	    @"[types]\r\n"
	    @"integer=16\r\n"
	    @"bool=false\r\n"
	    @"float=0.25\r\n"
	    @"array1=foo\r\n"
	    @"array1=bar\r\n"

Modified tests/testfile.ini from [96cb01d476] to [d185c6cbaf].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
; Comment in global section
global=yes

[tests]
foo = bar
foobar=baz
;comment

[foobar]
#foobarcomment
qux=" asd"
"quxqux " = asd
quxquxqux="hello\"wörld"
qux2="a\f"
"asd=asd"=foobar

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













|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
; Comment in global section
global=yes

[tests]
foo = bar
foobar=baz
;comment

[foobar]
#foobarcomment
qux=" asd"
"quxqux " = asd
quxquxqux="hello\"wörld"
qux2="a\n"
"asd=asd"=foobar

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