lisp-take-1/string/new.c

99 lines
1 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
#include <string.h>
#include <debug.h>
#include <memory/smalloc.h>
#include "struct.h"
#include "new.h"
struct string* new_string(
const uint8_t* str,
size_t olen)
{
ENTER;
struct string* this = smalloc(sizeof(*this));
uint8_t* copy = (uint8_t*) strndup((char*) str, olen);
size_t len = strlen((void*) copy);
this->data = copy;
this->n = len;
this->refcount = 1;
EXIT;
return this;
}
struct string* new_string_from_format(
const char* fmt __attribute((unused)), ...)
{
ENTER;
TODO;
EXIT;
/* return this;*/
}
struct string* new_string_from_vargs(
const char* fmt, va_list va)
{
ENTER;
char* buffer = NULL;
int ret = vasprintf(&buffer, fmt, va);
if (ret < 0)
{
TODO;
}
struct string* new = new_string((uint8_t*) buffer, (size_t) ret);
free(buffer);
EXIT;
return new;
}