CoreFW  Diff

Differences From Artifact [12e11ffa26]:

To Artifact [58c7761d81]:


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







-
+










-
+




-
+











-
-
+
+

















-
+











-
+





-
+





-
+

-
+
















-
-
+
+

-
+










-
+


-
+


-
+







#include <string.h>

#include "cfwobject.h"
#include "cfwstring.h"

struct CFWString {
	CFWObject obj;
	char *cstr;
	char *data;
	size_t len;
};

static bool
ctor(void *ptr, va_list args)
{
	CFWString *str = ptr;
	const char *cstr = va_arg(args, const char*);

	if (cstr != NULL) {
		if ((str->cstr = strdup(cstr)) == NULL)
		if ((str->data = strdup(cstr)) == NULL)
			return false;

		str->len = strlen(cstr);
	} else {
		str->cstr = NULL;
		str->data = NULL;
		str->len = 0;
	}

	return true;
}

static void
dtor(void *ptr)
{
	CFWString *str = ptr;

	if (str->cstr != NULL)
		free(str->cstr);
	if (str->data != NULL)
		free(str->data);
}

static bool
equal(void *ptr1, void *ptr2)
{
	CFWObject *obj2 = ptr2;
	CFWString *str1, *str2;

	if (obj2->cls != cfw_string)
		return false;

	str1 = ptr1;
	str2 = ptr2;

	if (str1->len != str2->len)
		return false;

	return !strcmp(str1->cstr, str2->cstr);
	return !memcmp(str1->data, str2->data, str1->len);
}

static void*
copy(void *ptr)
{
	CFWString *str = ptr;
	CFWString *new;

	if ((new = cfw_new(cfw_string)) == NULL)
		return NULL;

	if ((new->cstr = malloc(str->len + 1)) == NULL) {
	if ((new->data = malloc(str->len + 1)) == NULL) {
		cfw_unref(new);
		return NULL;
	}
	new->len = str->len;

	memcpy(new->cstr, str->cstr, str->len + 1);
	memcpy(new->data, str->data, str->len + 1);

	return new;
}

const char*
cfw_string_c(CFWString *string)
cfw_string_c(CFWString *str)
{
	return string->cstr;
	return str->data;
}

size_t
cfw_string_len(CFWString *string)
{
	return string->len;
}

bool
cfw_string_set(CFWString *str, const char *cstr)
{
	char *copy;

	if ((copy = strdup(cstr)) == NULL)
		return false;

	if (str->cstr != NULL)
		free(str->cstr);
	if (str->data != NULL)
		free(str->data);

	str->cstr = copy;
	str->data = copy;
	str->len = strlen(copy);

	return true;
}

bool
cfw_string_append(CFWString *str, CFWString *append)
{
	char *new;

	if ((new = realloc(str->cstr, str->len + append->len + 1)) == NULL)
	if ((new = realloc(str->data, str->len + append->len + 1)) == NULL)
		return false;

	memcpy(new + str->len, append->cstr, append->len);
	memcpy(new + str->len, append->data, append->len);
	new[str->len + append->len] = 0;

	str->cstr = new;
	str->data = new;
	str->len += append->len;

	return true;
}

static CFWClass class = {
	.name = "CFWString",