Add cfw_strdup(), as strdup() is not C99.
This commit is contained in:
parent
a74f8e1bac
commit
feb90593b9
2 changed files with 23 additions and 2 deletions
24
src/string.c
24
src/string.c
|
@ -27,6 +27,7 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
#include "object.h"
|
#include "object.h"
|
||||||
#include "string.h"
|
#include "string.h"
|
||||||
|
@ -38,6 +39,25 @@ struct CFWString {
|
||||||
size_t len;
|
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
|
static bool
|
||||||
ctor(void *ptr, va_list args)
|
ctor(void *ptr, va_list args)
|
||||||
{
|
{
|
||||||
|
@ -46,7 +66,7 @@ ctor(void *ptr, va_list args)
|
||||||
|
|
||||||
if (cstr != NULL) {
|
if (cstr != NULL) {
|
||||||
str->data = NULL;
|
str->data = NULL;
|
||||||
if ((str->data = strdup(cstr)) == NULL)
|
if ((str->data = cfw_strdup(cstr)) == NULL)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
str->len = strlen(cstr);
|
str->len = strlen(cstr);
|
||||||
|
@ -139,7 +159,7 @@ cfw_string_set(CFWString *str, const char *cstr)
|
||||||
{
|
{
|
||||||
char *copy;
|
char *copy;
|
||||||
|
|
||||||
if ((copy = strdup(cstr)) == NULL)
|
if ((copy = cfw_strdup(cstr)) == NULL)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (str->data != NULL)
|
if (str->data != NULL)
|
||||||
|
|
|
@ -32,6 +32,7 @@
|
||||||
|
|
||||||
typedef struct CFWString CFWString;
|
typedef struct CFWString CFWString;
|
||||||
extern CFWClass *cfw_string;
|
extern CFWClass *cfw_string;
|
||||||
|
extern char* cfw_strdup(const char*);
|
||||||
extern const char* cfw_string_c(CFWString*);
|
extern const char* cfw_string_c(CFWString*);
|
||||||
extern size_t cfw_string_len(CFWString*);
|
extern size_t cfw_string_len(CFWString*);
|
||||||
extern bool cfw_string_set(CFWString*, const char*);
|
extern bool cfw_string_set(CFWString*, const char*);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue