ObjFW  Check-in [bf02f0ef25]

Overview
Comment:New string API, string class completely rewritten.
One class for all string types now.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: bf02f0ef25bc5ad1cf2979fecad149b1e9d911340330723cc6846d53634a5dfd
User & Date: js on 2008-11-23 06:11:59
Other Links: manifest | tags
Context
2008-11-26
14:40
Support for wide C strings for OFXMLFactory. check-in: cd99b982ac user: js tags: trunk
2008-11-23
06:11
New string API, string class completely rewritten.
One class for all string types now.
check-in: bf02f0ef25 user: js tags: trunk
2008-11-19
18:27
Implement reverse for OF(Wide)CString & rename wcString -> wCString. check-in: cebd6fbbfc user: js tags: trunk
Changes

Modified src/Makefile from [b348bda782] to [d1706d71f5].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
1
2
3
4
5



6
7
8
9
10
11
12

13
14
15
16
17
18
19
20
21
22
23
24





-
-
-







-












LIB = ${LIB_PREFIX}objfw${LIB_SUFFIX}
LIB_MAJOR = 1
LIB_MINOR = 0

SRCS = OFArray.m		\
       OFConstCString.m		\
       OFCString.m		\
       OFConstWideCString.m	\
       OFExceptions.m		\
       OFHashes.m		\
       OFFile.m			\
       OFList.m			\
       OFListObject.m		\
       OFObject.m		\
       OFString.m		\
       OFWideCString.m		\
       OFXMLFactory.m

INCLUDES = ${SRCS:.m=.h}	\
	   OFMacros.h

include ../buildsys.mk

CPPFLAGS += -I..
OBJCFLAGS += ${LIB_CFLAGS}
LD = ${OBJC}
LDFLAGS += ${LIB_LDFLAGS}
LIBS += -lobjc

Deleted src/OFCString.h version [4014e47d9b].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
64
65
66
67
68
69
70
71
72
73
74
75
76












































































-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
/*
 * Copyright (c) 2008
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import <stddef.h>

#import "OFString.h"

/**
 * An OFString using a C string as internal storage.
 */
@interface OFCString: OFString
{
	char   *string;
	size_t length;
}

/**
 * Initializes an already allocated OFCString.
 * 
 * \param str A C string to initialize the OFCString with
 * \return An initialized OFCString
 */
- initAsCString: (char*)str;

/**
 * \return The OFCString as a C string
 */
- (char*)cString;

/**
 * \return The length of the OFCString
 */
- (size_t)length;

/**
 * Clones the OFCString, creating a new one.
 * 
 * \return A copy of the OFCString
 */
- (OFString*)clone;

/**
 * Compares the OFCString to another OFString.
 *
 * \param str An OFString in a compatible type to compare with
 * \return An integer which is the result of the comparison, see strcmp
 */
- (int)compareTo: (OFString*)str;

/**
 * Append another OFString to the OFCString.
 *
 * \param str An OFString in a compatible type to append
 */
- append: (OFString*)str;

/**
 * Append a C string to the OFCString.
 *
 * \param str A C string to append
 */
- appendCString: (const char*)str;

/**
 * Reverse the OFCString.
 */
- reverse;
@end

Deleted src/OFCString.m version [745d2596c4].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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































































































-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
/*
 * Copyright (c) 2008
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "config.h"

#import <stdlib.h>
#import <string.h>

#import "OFCString.h"
#import "OFExceptions.h"

@implementation OFCString
- initAsCString: (char*)str
{
	if ((self = [super init])) {
		if (str == NULL) {
			length = 0;
			string = NULL;
		} else {
			length = strlen(str);
			string = [self getMemWithSize: length + 1];
			memcpy(string, str, length + 1);
		}
	}
	return self;
}

- (char*)cString
{
	return string;
}

- (size_t)length
{
	return length;
}

- (OFString*)clone
{
	return [OFString newAsCString: string];
}

- (int)compareTo: (OFString*)str
{
	return strcmp(string, [str cString]);
}

- append: (OFString*)str
{
	return [self appendCString: [str cString]];
}

- appendCString: (const char*)str
{
	char   *newstr;
	size_t newlen, strlength;

	if (string == NULL) 
		return [self setTo: [OFString newAsCString: (char*)str]];

	strlength = strlen(str);
	newlen = length + strlength;

	newstr = [self resizeMem: string
			  toSize: newlen + 1];

	memcpy(newstr + length, str, strlength + 1);

	length = newlen;
	string = newstr;

	return self;
}

- reverse
{
	size_t i, j, len = length / 2;

	for (i = 0, j = length - 1; i < len; i++, j--) {
		string[i] ^= string[j];
		string[j] ^= string[i];
		string[i] ^= string[j];
	}

	return self;
}
@end

Deleted src/OFConstCString.h version [594c3b2544].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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























































-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
/*
 * Copyright (c) 2008
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "OFString.h"

/**
 * An OFString using a constant C string as internal storage.
 */
@interface OFConstCString: OFString
{
	const char *string;
	size_t	   length;
}

/**
 * Initializes an already allocated OFConstCString.
 * 
 * \param str A constant C string to initialize the OFConstCString with
 * \return An initialized OFConstCString
 */
- initAsConstCString: (const char*)str;

/**
 * \return The OFConstCString as a constant C strin
 */
- (const char*)cString;

/**
 * \return The length of the OFConstCString
 */
- (size_t)length;

/**
 * Clones the OFConstCString, creating a new one.
 * 
 * \return A copy of the OFConstCString
 */
- (OFString*)clone;

/**
 * Compares the OFConstCString to another OFString.
 *
 * \param str An OFString in a compatible type to compare with
 * \return An integer which is the result of the comparison, see strcmp
 */
- (int)compareTo: (OFString*)str;
@end

Deleted src/OFConstCString.m version [cab2a5a929].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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



















































-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
/*
 * Copyright (c) 2008
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "config.h"

#import <string.h>
#import "OFConstCString.h"

@implementation OFConstCString
- initAsConstCString: (const char*)str
{
	if ((self = [super init])) {
		if (str == NULL) {
			length = 0;
			string = NULL;
		} else {
			length = strlen(str);
			string = str;
		}
	}
	return self;
}

- (const char*)cString
{
	return string;
}

- (size_t)length
{
	return length;
}

- (OFString*)clone
{
	return [OFString newAsConstCString: string];
}

- (int)compareTo: (OFString*)str
{
	return strcmp(string, [str cString]);
}
@end

Deleted src/OFConstWideCString.h version [441911b239].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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


























































-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
/*
 * Copyright (c) 2008
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import <wchar.h>
#import <stddef.h>
#import "OFString.h"

/**
 * An OFString using a constant wide C string as internal storage.
 */
@interface OFConstWideCString: OFString
{
	const wchar_t *string;
	size_t	      length;
}

/**
 * Initializes an already allocated OFConstWideCString.
 * 
 * \param str A constant wide C string to initialize the OFConstWideCString
 * 	  with
 * \return An initialized OFConstWideCString
 */
- initAsConstWideCString: (const wchar_t*)str;

/**
 * \return The OFConstWideCString as a constant wide C string
 */
- (const wchar_t*)wCString;

/**
 * \return The length of the OFConstWideCString
 */
- (size_t)length;

/**
 * Clones the OFConstWideCString, creating a new one.
 * 
 * \return A copy of the OFConstWideCString
 */
- (OFString*)clone;

/**
 * Compares the OFConstWideCString to another OFString.
 *
 * \param str An OFString in a compatible type to compare with
 * \return An integer which is the result of the comparison, see wcscmp
 */
- (int)compareTo: (OFString*)str;
@end

Deleted src/OFConstWideCString.m version [1a558eeed6].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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



















































-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
/*
 * Copyright (c) 2008
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "config.h"

#import <wchar.h>
#import "OFConstWideCString.h"

@implementation OFConstWideCString
- initAsConstWideCString: (const wchar_t*)str
{
	if ((self = [super init])) {
		if (str == NULL) {
			length = 0;
			string = NULL;
		} else {
			length = wcslen(str);
			string = str;
		}
	}
	return self;
}

- (const wchar_t*)wCString
{
	return string;
}

- (size_t)length
{
	return length;
}

- (OFString*)clone
{
	return [OFString newAsConstWideCString: string];
}

- (int)compareTo: (OFString*)str
{
	return wcscmp(string, [str wCString]);
}
@end

Modified src/OFExceptions.h from [9d8efd0a46] to [7c1fcb25b2].

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
88
89
90
91
92
93
94







































95
96
97
98
99
100
101







-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-








/**
 * \return The size of the memoory that couldn't be allocated
 */
- (size_t)requestedSize;
@end

/**
 * An OFException indicating the requested selector is not implemented.
 */
@interface OFNotImplementedException: OFException
{
	SEL selector;
}

/**
 * Creates a new not impemented exception.
 *
 * \param obj The object which caused the exception
 * \param sel A selector for the function not implemented
 * \return A new not implemented exception
 */
+ newWithObject: (id)obj
    andSelector: (SEL)sel;

/**
 * Initializes an already allocated not implemented exception.
 *
 * \param obj The object which caused the exception
 * \param sel A selector for the function not implemented
 * \return An initialized not implemented exception
 */
- initWithObject: (id)obj
     andSelector: (SEL)sel;

/**
 * \return An error message for the exception as a C String
 */
- (char*)cString;

/**
 * \return The requested selector that is not implemented
 */
- (SEL)selector;
@end

/**
 * An OFException indicating the given memory is not part of the object.
 */
@interface OFMemNotPartOfObjException: OFException
{
	void *pointer;
}

Modified src/OFExceptions.m from [aaeb7dcecc] to [3ae2ac34e6].

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
92
93
94
95
96
97
98


































99
100
101
102
103
104
105







-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-







	return string;
}

- (size_t)requestedSize
{
	return req_size;
}
@end

@implementation OFNotImplementedException
+ newWithObject: (id)obj
    andSelector: (SEL)sel
{
	return [[OFNotImplementedException alloc] initWithObject: obj
						     andSelector: sel];
}

- initWithObject: (id)obj
     andSelector: (SEL)sel
{
	if ((self = [super initWithObject: obj]))
		selector = sel;

	return self;
}

- (char*)cString
{
	if (string != NULL)
		return string;

	asprintf(&string, "ERROR: Requested selector %s not implemented in "
	    "%s!\n", SEL_NAME(selector), [object name]);

	return string;
}

- (SEL)selector
{
	return selector;
}
@end

@implementation OFMemNotPartOfObjException
+ newWithObject: (id)obj
     andPointer: (void*)ptr
{
	return [[OFMemNotPartOfObjException alloc] initWithObject: obj

Modified src/OFMacros.h from [3fdd6eaada] to [d433d98b76].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1
2
3
4
5
6
7
8
9
10
11






12
13
14
15
16
17
18











-
-
-
-
-
-







/*
 * Copyright (c) 2008
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

/* Return is only to make the compiler happy - it is never reached */
#define OF_NOT_IMPLEMENTED(ret)						\
	[[OFNotImplementedException newWithObject: self			\
				      andSelector: _cmd] raise];	\
	return ret;

#ifdef OF_BIG_ENDIAN
static inline void
OF_BSWAP_V(uint8_t *buf, size_t len)
{
	uint32_t t;

	while (len--) {

Modified src/OFString.h from [d4d814f095] to [3ff6ad57f0].

11
12
13
14
15
16
17
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
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
11
12
13
14
15
16
17

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







-
+

-
+
+
+
+
+
+

+
+
+
+
+
-
-
-
+
+
+
+
+
+

-
+


+
+
-
+
-
-
+

-
+


+
+
+
+
+
+
+
+
+
-
-
+
+

-
+


+
+
-
-
+
+

-
+


-
+
+




-
+

-
+






-
-
-
-
-
-
-







+
+
+
+
+
+
+
+



-
-
+
+

-
+




-
+








#import <wchar.h>
#import <stddef.h>

#import "OFObject.h"

/**
 * The OFString class can store and modify string of different types.
 * A class for storing and modifying strings.
 */
@interface OFString: OFObject {}
@interface OFString: OFObject
{
	wchar_t	*string;
	size_t  length;
}

/**
 * Creates a new OFString.
 * 
 * \return An initialized OFString
 */
+ new;
 * \param str A constant C string from which the new OFConstCString will be
 *	  created
 * \return A new OFConstCString

/**
 * Creates a new OFString from a C string.
 * 
 * \param str A C string to initialize the OFString with
 * \return A new OFString
 */
+ newAsConstCString: (const char*)str;
+ newFromCString: (const char*)str;

/**
 * Creates a new OFString from a wide C string.
 * 
 * \param str A constant wide C string from which the new OFConstCString will be
 * \param str A wide C string to initialize the OFString with
 *	  created
 * \return A new OFConstWideCString
 * \return A new OFString
 */
+ newAsConstWideCString: (const wchar_t*)str;
+ newFromWideCString: (const wchar_t*)str;

/**
 * Initializes an already allocated OFString.
 *
 * \return An initialized OFString
 */
- init;

/**
 * Initializes an already allocated OFString from a C string.
 * 
 * \param str A C string from which the new OFConstCString will be created
 * \return A new OFCString
 * \param str A C string to initialize the OFString with
 * \return An initialized OFString
 */
+ newAsCString: (char*)str;
- initFromCString: (const char*)str;

/**
 * Initializes an already allocated OFString from a wide C string.
 * 
 * \param str A wide C string from which the new OFConstCString will be created
 * \return A new OFWideCString
 * \param str A wide C string to initialize the OFString with
 * \return An initialized OFString
 */
+ newAsWideCString: (wchar_t*)str;
- initFromWideCString: (const wchar_t*)str;

/**
 * \return The OFString as a C-type string of the type it was created as
 * \return The OFString as a C string, if possible (if not, returns NULL).
 *         If not needed anymore, it is usefull to call freeMem:.
 */
- (char*)cString;

/**
 * \return The OFString as a C-type wide string of the type it was created as
 * \return The OFString as a wide C string
 */
- (wchar_t*)wCString;
- (wchar_t*)wideCString;

/**
 * \return The length of the OFString
 */
- (size_t)length;

/**
 * Sets the OFString to the specified OFString.
 *
 * \param str The OFString to set the current OFString to
 */
- (OFString*)setTo: (OFString*)str;

/**
 * Clones the OFString, creating a new one.
 * 
 * \return A copy of the OFString
 */
- (OFString*)clone;

/**
 * Frees the OFString and sets it to the specified OFString.
 *
 * \param str An OFString to set the OFString to.
 * \return The new OFString
 */
- (OFString*)setTo: (OFString*)str;

/**
 * Compares the OFString to another OFString.
 *
 * \param str An OFString in a compatible type to compare with
 * \return An integer which is the result of the comparison, see strcmp
 * \param str An OFString to compare with
 * \return An integer which is the result of the comparison, see wcscmp
 */
- (int)compareTo: (OFString*)str;
- (int)compare: (OFString*)str;

/**
 * Append another OFString to the OFString.
 *
 * \param str An OFString in a compatible type to append
 * \param str An OFString to append
 */
- append: (OFString*)str;

/**
 * Append a C string to the OFString.
 *
 * \param str A C string to append

Modified src/OFString.m from [fbb622def6] to [cffef7d468].

9
10
11
12
13
14
15

16
17
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
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
9
10
11
12
13
14
15
16
17
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
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







+


-
-
-
-

-


+
+
+
+
+
-
+

-
+


-
+

+
+
-
-
+
+
+
+
+
+
+

+
+
+
-
+

+
+
+
+
+
+
+
+
-
-
+
+

+
+
+
+
+
+
+
+
+
+
+
+
+
-
+

+
+
+
+
+
+
+
+
+
+
+
-
+
+




+
+
-
-
+
+
+
+
+

+
+
+
+
+
+
+
+
+
+
+
-
+

-
+




+
+
-
+
+
+
+





-
+
-


-
+

-
-
-
+
-
-
-




-
+




+
+
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+


+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+




+
-
-
+
+
+
+
+
+
+
+
+

 * the packaging of this file.
 */

#import "config.h"

#import <stdlib.h>
#import <string.h>
#import <wchar.h>

#import "OFString.h"
#import "OFConstCString.h"
#import "OFConstWideCString.h"
#import "OFCString.h"
#import "OFWideCString.h"
#import "OFExceptions.h"
#import "OFMacros.h"

@implementation OFString
+ new
{
	return [[OFString alloc] init];
}

+ newAsConstCString: (const char*)str
+ newFromCString: (const char*)str
{
	return [[OFConstCString alloc] initAsConstCString: str];
	return [[OFString alloc] initFromCString: str];
}

+ newAsConstWideCString: (const wchar_t*)str
+ newFromWideCString: (const wchar_t*)str
{
	return [[OFString alloc] initFromWideCString: str];
}
	return [[OFConstWideCString alloc] initAsConstWideCString: str];
}

- init
{
	if ((self = [super init])) {
		length = 0;
		string = NULL;
	}

	return self;
}

+ newAsCString: (char*)str
- initFromCString: (const char*)str
{
	if ((self = [super init])) {
		if (str == NULL) {
			length = 0;
			string = NULL;
		} else {
			if ((length = mbstowcs(NULL, str, 0)) == (size_t)-1) {
				/* FIXME: Throw exception */
				[super free];
	return [[OFCString alloc] initAsCString: str];
}
				return nil;
			}

			string = [self getMemForNItems: length + 1
						ofSize: sizeof(wchar_t)];

			if (mbstowcs(string, str, length) != length) {
				[super free];
				return nil;
			}
		}
	}

	return self;
}

+ newAsWideCString: (wchar_t*)str
- initFromWideCString: (const wchar_t*)str
{
	if ((self = [super init])) {
		if (str == NULL) {
			length = 0;
			string = NULL;
		} else {
			length = wcslen(str);
			string = [self getMemForNItems: length + 1
						ofSize: sizeof(wchar_t)];
			wmemcpy(string, str, length + 1);
		}
	}
	return [[OFWideCString alloc] initAsWideCString: str];

	return self;
}

- (char*)cString
{
	char *str;
	size_t len;
	OF_NOT_IMPLEMENTED(NULL)
}

	if ((len = wcstombs(NULL, string, 0)) == (size_t)-1) {
		/* FIXME: Throw exception */
		return NULL;
	}

	str = [self getMemWithSize: len];

	if (wcstombs(str, string, len) != len) {
		/* FIXME: Throw exception */
		[self freeMem: str];
		return NULL;
	}

	return str;
}

- (wchar_t*)wCString
- (wchar_t*)wideCString
{
	OF_NOT_IMPLEMENTED(NULL)
	return string;
}

- (size_t)length
{
	return length;
}
	OF_NOT_IMPLEMENTED(0)

- (OFString*)clone
{
	return [OFString newFromWideCString: string];
}

- (OFString*)setTo: (OFString*)str
{
	[self free];
	self = [str clone];
	return (self = [str clone]);
	return self;
}

- (OFString*)clone
- (int)compare: (OFString*)str
{
	OF_NOT_IMPLEMENTED(nil)
}

	return wcscmp(string, [str wideCString]);
- (int)compareTo: (OFString*)str
{
	OF_NOT_IMPLEMENTED(0)
}

- append: (OFString*)str
{
	OF_NOT_IMPLEMENTED(nil)
	return [self appendWideCString: [str wideCString]];
}

- appendCString: (const char*)str
{
	wchar_t	*newstr, *tmpstr;
	size_t	newlen, strlength;
	OF_NOT_IMPLEMENTED(nil)
}


	if (string == NULL) 
		return [self setTo: [OFString newFromCString: str]];

	if ((strlength = mbstowcs(NULL, str, 0)) == (size_t)-1) {
		/* FIXME: Throw exception */
		return nil;
	} 

	tmpstr = [self getMemForNItems: strlength + 1
				ofSize: sizeof(wchar_t)];

	if (mbstowcs(tmpstr, str, strlength) != strlength) {
		/* FIXME: Throw exception */
		[self freeMem: tmpstr];
		return nil;
	}

	newlen = length + strlength;
	newstr = [self resizeMem: string
			toNItems: newlen + 1
			  ofSize: sizeof(wchar_t)];

	wmemcpy(newstr + length, tmpstr, strlength + 1);

	length = newlen;
	string = newstr;

	[self freeMem: tmpstr];

	return self;
}

- appendWideCString: (const wchar_t*)str
{
	wchar_t	*newstr;
	size_t	newlen, strlength;
	OF_NOT_IMPLEMENTED(nil)

	if (string == NULL) 
		return [self setTo: [OFString newFromWideCString: str]];

	strlength = wcslen(str);
	newlen = length + strlength;

	newstr = [self resizeMem: string
			toNItems: newlen + 1
			  ofSize: sizeof(wchar_t)];

	wmemcpy(newstr + length, str, strlength + 1);

	length = newlen;
	string = newstr;

	return self;
}

- reverse
{
	size_t i, j, len = length / 2;
	OF_NOT_IMPLEMENTED(nil)
}

	for (i = 0, j = length - 1; i < len; i++, j--) {
		string[i] ^= string[j];
		string[j] ^= string[i];
		string[i] ^= string[j];
	}

	return self;
}
@end

Deleted src/OFWideCString.h version [3ceaa14475].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77













































































-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
/*
 * Copyright (c) 2008
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import <wchar.h>
#import <stddef.h>

#import "OFString.h"

/**
 * An OFString using a wide C string as internal storage.
 */
@interface OFWideCString: OFString
{
	wchar_t	*string;
	size_t  length;
}

/**
 * Initializes an already allocated OFWideCString.
 * 
 * \param str A wide C string to initialize the OFWideCString with
 * \return An initialized OFWideCString
 */
- initAsWideCString: (wchar_t*)str;

/**
 * \return The OFWideCString as a wide C string
 */
- (wchar_t*)wCString;

/**
 * \return The length of the OFWideCString
 */
- (size_t)length;

/**
 * Clones the OFWideCString, creating a new one.
 * 
 * \return A copy of the OFWideCString
 */
- (OFString*)clone;

/**
 * Compares the OFWideCString to another OFString.
 *
 * \param str An OFString in a compatible type to compare with
 * \return An integer which is the result of the comparison, see wcscmp
 */
- (int)compareTo: (OFString*)str;

/**
 * Append another OFString to the OFWideCString.
 *
 * \param str An OFString in a compatible type to append
 */
- append: (OFString*)str;

/**
 * Append a wide C string to the OFWideCString.
 *
 * \param str A wide C string to append
 */
- appendWideCString: (const wchar_t*)str;

/**
 * Reverse the OFWideCString.
 */
- reverse;
@end

Deleted src/OFWideCString.m version [979098a330].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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


































































































-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
/*
 * Copyright (c) 2008
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "config.h"

#import <stdlib.h>
#import <string.h>
#import <wchar.h>

#import "OFWideCString.h"
#import "OFExceptions.h"

@implementation OFWideCString
- initAsWideCString: (wchar_t*)str
{
	if ((self = [super init])) {
		if (str == NULL) {
			length = 0;
			string = NULL;
		} else {
			length = wcslen(str);
			string = [self getMemWithSize: (length + 1) *
							   sizeof(wchar_t)];
			wmemcpy(string, str, length + 1);
		}
	}
	return self;
}

- (wchar_t*)wCString
{
	return string;
}

- (size_t)length
{
	return length;
}

- (OFString*)clone
{
	return [OFString newAsWideCString: string];
}

- (int)compareTo: (OFString*)str
{
	return wcscmp(string, [str wCString]);
}

- append: (OFString*)str
{
	return [self appendWideCString: [str wCString]];
}

- appendWideCString: (const wchar_t*)str
{
	wchar_t	*newstr;
	size_t	newlen, strlength;

	if (string == NULL) 
		return [self setTo: [OFString
					newAsWideCString: (wchar_t*)str]];

	strlength = wcslen(str);
	newlen = length + strlength;

	newstr = [self resizeMem: string
			  toSize: (newlen + 1) * sizeof(wchar_t)];

	wmemcpy(newstr + length, str, strlength + 1);

	length = newlen;
	string = newstr;

	return self;
}

- reverse
{
	size_t i, j, len = length / 2;

	for (i = 0, j = length - 1; i < len; i++, j--) {
		string[i] ^= string[j];
		string[j] ^= string[i];
		string[i] ^= string[j];
	}

	return self;
}
@end

Modified tests/Makefile from [18ee2d1c94] to [06e32c1877].

1






2
3

1
2
3
4
5
6
7
8
-
+
+
+
+
+
+


SUBDIRS = OFObject OFArray OFHashes OFString OFList OFWideString OFXMLFactory
SUBDIRS = OFObject	\
	  OFArray	\
	  OFHashes	\
	  OFString	\
	  OFList	\
	  OFXMLFactory

include ../buildsys.mk

Modified tests/OFList/OFList.m from [913aa88c30] to [cbcfa7ab84].

1
2
3
4
5
6
7
8
9
10
11

12
13
14
15
16
17
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
64
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
64
65











+








-
-
-
-
+
+
+
+











-
-
-
+
+
+


-
+






-
+






-
+










/*
 * Copyright (c) 2008
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import <wchar.h>
#import <stdio.h>
#import <string.h>

#import "OFString.h"
#import "OFList.h"

/* TODO: Do real checks */
 
const char *strings[] = {
	"First String Object",
	"Second String Object",
	"Third String Object"
const wchar_t *strings[] = {
	L"First String Object",
	L"Second String Object",
	L"Third String Object"
};

int
main()
{
	size_t	     i;
	OFList	     *list;
	OFListObject *iter;

	list = [OFList new];
 
	[list addNew: [OFString newAsConstCString: strings[0]]];
	[list addNew: [OFString newAsConstCString: strings[1]]];
	[list addNew: [OFString newAsConstCString: strings[2]]];
	[list addNew: [OFString newFromWideCString: strings[0]]];
	[list addNew: [OFString newFromWideCString: strings[1]]];
	[list addNew: [OFString newFromWideCString: strings[2]]];
 
	for (iter = [list first], i = 0; iter != nil; iter = [iter next], i++)
		if (!strcmp([(OFString*)[iter data] cString], strings[i]))
		if (!wcscmp([(OFString*)[iter data] wideCString], strings[i]))
			printf("Element %zu is expected element. GOOD!\n", i);
		else {
			printf("Element %zu is not expected element!\n", i);
			return 1;
		}

	if (!strcmp([(OFString*)[[list first] data] cString], strings[0]))
	if (!wcscmp([(OFString*)[[list first] data] wideCString], strings[0]))
		puts("First element is expected element. GOOD!");
	else {
		puts("First element is not expected element!");
		return 1;
	}

	if (!strcmp([(OFString*)[[list last] data] cString], strings[2]))
	if (!wcscmp([(OFString*)[[list last] data] wideCString], strings[2]))
		puts("Last element is expected element. GOOD!");
	else {
		puts("Last element is not expected element!");
		return 1;
	}
 
	[list freeIncludingData];

	return 0;
}

Modified tests/OFString/OFString.m from [97aa03dbaf] to [e5dacf305a].

15
16
17
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
15
16
17
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







-
-
+
+

-
+

-


-
-
-
+






+
+
+
-
+


-
+







#import "OFString.h"

/* TODO: Do real checks */

int
main()
{
	OFString *s1 = [OFString newAsCString: "test"];
	OFString *s2 = [OFString newAsCString: ""];
	OFString *s1 = [OFString newFromCString: "test"];
	OFString *s2 = [OFString newFromCString: ""];
	OFString *s3;
	OFString *s4 = [OFString newAsConstCString: NULL];
	OFString *s4 = [OFString new];

	[s2 appendCString: "123"];
	s3 = [s1 clone];

	[s4 setTo: s2];

	if (![s1 compareTo: s3])
	if (![s1 compare: s3])
		puts("s1 and s3 match! GOOD!");
	else {
		puts("s1 and s3 don't match!");
		return 1;
	}

	[s2 appendCString: "123"];
	[s4 setTo: s2];

	if (![s2 compareTo: s4])
	if (![s2 compare: s4])
		puts("s2 and s4 match! GOOD!");
	else {
		puts("s1 and s3 don't match!");
		puts("s2 and s4 don't match!");
		return 1;
	}

	if (!strcmp([[s1 append: s2] cString], "test123"))
		puts("s1 appended with s2 is the expected string! GOOD!");
	else {
		puts("s1 appended with s2 is not the expected string!");

Deleted tests/OFWideString/Makefile version [3581da7fe1].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20




















-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
PROG_NOINST = ofwidestring
SRCS = OFWideString.m

include ../../buildsys.mk

CPPFLAGS += -I../../src
LIBS += -lobjc -L../../src -lobjfw

.PHONY: run

all: run
run: ${PROG_NOINST}
	rm -f libobjfw.so.1 libobjfw.so.1.0 libobjfw.dylib
	ln -s ../../src/libobjfw.so libobjfw.so.1
	ln -s ../../src/libobjfw.so libobjfw.so.1.0
	ln -s ../../src/libobjfw.dylib libobjfw.dylib
	LD_LIBRARY_PATH=. \
	DYLD_LIBRARY_PATH=. \
	./${PROG_NOINST}
	rm -f libobjfw.so.1 libobjfw.so.1.0 libobjfw.dylib

Deleted tests/OFWideString/OFWideString.m version [1851e3e09d].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
64
65
66
67
68
69
70
71
72








































































-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
/*
 * Copyright (c) 2008
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import <wchar.h>

#import "OFString.h"

/* TODO: Do real checks */

int
main()
{
	OFString *s1 = [OFString newAsWideCString: L"test"];
	OFString *s2 = [OFString newAsWideCString: L""];
	OFString *s3;
	OFString *s4 = [OFString newAsConstWideCString: NULL];

	[s2 appendWideCString: L"123"];
	s3 = [s1 clone];

	[s4 setTo: s2];

	if (![s1 compareTo: s3])
		puts("s1 and s3 match! GOOD!");
	else {
		puts("s1 and s3 don't match!");
		return 1;
	}

	if (![s2 compareTo: s4])
		puts("s2 and s4 match! GOOD!");
	else {
		puts("s1 and s3 don't match!");
		return 1;
	}

	if (!wcscmp([[s1 append: s2] wCString], L"test123"))
		puts("s1 appended with s2 is the expected string! GOOD!");
	else {
		puts("s1 appended with s2 is not the expected string!");
		return 1;
	}

	if (wcslen([s1 wCString]) == [s1 length] && [s1 length] == 7)
		puts("s1 has the expected length. GOOD!");
	else {
		puts("s1 does not have the expected length!");
		return 1;
	}

	if (!wcscmp([[s1 reverse] wCString], L"321tset"))
		puts("Reversed s1 is expected string! GOOD!");
	else {
		puts("Reversed s1 is NOT the expected string!");
		return 1;
	}

	[s1 free];
	[s2 free];
	[s3 free];
	[s4 free];

	return 0;
}