CoreFW  Check-in [5c1d214724]

Overview
Comment:Add cfw_strdup(), as strdup() is not C99.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 5c1d2147247fe831d6a13b63c66d9a16fe8997c65f39ed6b415ee0c20594f469
User & Date: js on 2012-04-25 09:30:22
Other Links: manifest | tags
Context
2012-04-25
09:30
cfw_string_len() -> cfw_string_length(). check-in: 27a8cd7393 user: js tags: trunk
09:30
Add cfw_strdup(), as strdup() is not C99. check-in: 5c1d214724 user: js tags: trunk
09:22
ISO C forbids empty initializer braces. check-in: ceb7a22af1 user: js tags: trunk
Changes

Modified src/string.c from [0d338be9da] to [14e8b0fd1e].

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
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdlib.h>
#include <stdint.h>
#include <string.h>


#include "object.h"
#include "string.h"
#include "hash.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) {
		str->data = NULL;
		if ((str->data = strdup(cstr)) == NULL)
			return false;

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







>










>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>









|







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
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>

#include "object.h"
#include "string.h"
#include "hash.h"

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

char*
cfw_strdup(const char *s)
{
	char *copy;
	size_t len;

	len = strlen(s);

	if ((copy = malloc(len + 1)) == NULL) {
		errno = ENOMEM;
		return NULL;
	}

	memcpy(copy, s, len);
	copy[len] = 0;

	return copy;
}

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

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

		str->len = strlen(cstr);
	} else {
		str->data = NULL;
		str->len = 0;
	}
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
}

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







|







155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
}

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

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

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

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

Modified src/string.h from [2d77f73a91] to [1e1600afb1].

28
29
30
31
32
33
34

35
36
37
38
39
40
41
#define __COREFW_STRING_H__

#include "class.h"
#include "range.h"

typedef struct CFWString CFWString;
extern CFWClass *cfw_string;

extern const char* cfw_string_c(CFWString*);
extern size_t cfw_string_len(CFWString*);
extern bool cfw_string_set(CFWString*, const char*);
extern bool cfw_string_append(CFWString*, CFWString*);
extern size_t cfw_string_find(CFWString*, CFWString*, cfw_range_t);

#endif







>







28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#define __COREFW_STRING_H__

#include "class.h"
#include "range.h"

typedef struct CFWString CFWString;
extern CFWClass *cfw_string;
extern char* cfw_strdup(const char*);
extern const char* cfw_string_c(CFWString*);
extern size_t cfw_string_len(CFWString*);
extern bool cfw_string_set(CFWString*, const char*);
extern bool cfw_string_append(CFWString*, CFWString*);
extern size_t cfw_string_find(CFWString*, CFWString*, cfw_range_t);

#endif