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;
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)
return false;
str->len = strlen(cstr);
} else {
str->cstr = NULL;
str->len = 0;
}
return true;
}
static void
dtor(void *ptr)
{
CFWString *str = ptr;
if (str->cstr != NULL)
free(str->cstr);
}
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);
}
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) {
cfw_unref(new);
return NULL;
}
new->len = str->len;
memcpy(new->cstr, str->cstr, str->len + 1);
return new;
}
const char*
cfw_string_c(CFWString *string)
{
return string->cstr;
}
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);
str->cstr = 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)
return false;
memcpy(new + str->len, append->cstr, append->len);
new[str->len + append->len] = 0;
str->cstr = new;
str->len += append->len;
return true;
}
static CFWClass class = {
.name = "CFWString",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 *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->data = strdup(cstr)) == NULL)
return false;
str->len = strlen(cstr);
} else {
str->data = NULL;
str->len = 0;
}
return true;
}
static void
dtor(void *ptr)
{
CFWString *str = ptr;
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 !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->data = malloc(str->len + 1)) == NULL) {
cfw_unref(new);
return NULL;
}
new->len = str->len;
memcpy(new->data, str->data, str->len + 1);
return new;
}
const char*
cfw_string_c(CFWString *str)
{
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->data != NULL)
free(str->data);
str->data = copy;
str->len = strlen(copy);
return true;
}
bool
cfw_string_append(CFWString *str, CFWString *append)
{
char *new;
if ((new = realloc(str->data, str->len + append->len + 1)) == NULL)
return false;
memcpy(new + str->len, append->data, append->len);
new[str->len + append->len] = 0;
str->data = new;
str->len += append->len;
return true;
}
static CFWClass class = {
.name = "CFWString",
|