ObjFW  Check-in [35df8d869a]

Overview
Comment:OFINIFile: Properly escape newlines
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | 1.1
Files: files | file ages | folders
SHA3-256: 35df8d869a9d528dbe09c7bf2c80ab21ffce64fb633cc85a74a07e197bfd54a5
User & Date: js on 2024-09-06 22:28:54
Other Links: branch diff | manifest | tags
Context
2024-09-21
23:01
Update misc/keys.asc Leaf check-in: 76bda9647d user: js tags: 1.1
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: 5b00004344 user: js tags: 1.1
Changes

Modified src/OFINICategory.m from [06ae24a12e] to [5ced3a325d].

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 "OFINICategory.h"
#import "OFINICategory+Private.h"
#import "OFArray.h"

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

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

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

@interface OFINICategoryComment: 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 "OFINICategory.h"
#import "OFINICategory+Private.h"
#import "OFArray.h"
#import "OFCharacterSet.h"
#import "OFStream.h"
#import "OFString.h"

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

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

@interface OFINICategoryComment: 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 OFINICategory
@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 OFINICategory
@synthesize name = _name;

+ (void)initialize
{
	if (self != [OFINICategory 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 [80cce0e553] to [65527d83fd].

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 [f3a28cbc21] to [5fdaa160af].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
; Comment in global category
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 category
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