Update to C11, added parsing and replacing functions

master
Avril 6 years ago
parent e944b75a4f
commit 888caed9bf
No known key found for this signature in database
GPG Key ID: DDDD9B6759158726

@ -1,7 +1,7 @@
SOURCE:= src/*.c
TEST_SOURCE:= src/test/*.c
INCLUDE:= include/
CFLAGS:= -Wall -g --std=gnu99
CFLAGS:= -Wall -g --std=c11
OBJ:= obj
BUILD:= build
LFLAGS:= -L./$(BUILD) -lse

@ -13,6 +13,8 @@ typedef struct {
typedef struct cons {
union {
void* ptr;
struct cons* car_l;
char* car_s;
car_t car;
};
struct cons* cdr;
@ -41,5 +43,8 @@ enum _se_type se_typeof(list_t* cons); //Get type of cons's car value.
list_t* se_new(char* from, int dupe); //Create new cons.
#define se_newd(f) se_new((f), 1)
list_t* se_last(list_t* list); //Get last non-nil cons.
car_t se_make(const char* fmt); //Parse "input" as string and "(input)" as sexpr and return as car_t. (NULL returns NIL).
void se_rplaca(list_t* cons, car_t car); //Replace car of cons with `car'
void se_rplacd(list_t* cons, list_t* cdr); //Replace cdr of cons with 'cdr'
#endif /* _LIBSE_H */

@ -5,6 +5,15 @@
#include <libse.h>
#include <internal/exstring.h>
static char* strdup(const char* str)
{
register size_t size = strlen(str);
char* ret = malloc(size+1);
memcpy(ret, str, size);
ret[size] = 0;
return ret;
}
static void _addcar(list_t* out, exString* token, int nil, int ucase)
{
if(ucase)
@ -304,3 +313,60 @@ list_t* se_new(char* str, int dupe)
{
return _se_new(str, SE_STRING, dupe); //We don't need the list part.
}
car_t se_make(const char* fmt)
{
if(!fmt)
{
car_t c;
memset(&c, 0, sizeof(car_t));
return c;
}
while(*fmt && iws(*fmt))
{
fmt+=1;
}
if(!*fmt) return se_make(NULL);
else if(*fmt == '(') {
car_t c;
list_t* ret = se_parse(fmt, SETP_DEFAULT);
memset(&c,0,sizeof(car_t));
c.ptr = ret;
c.alloced= 1;
c.size = sizeof(list_t);
c.sexpr = 1;
return c;
}
else {
car_t c;
memset(&c,0,sizeof(car_t));
c.ptr = strdup(fmt);
c.alloced=1;
c.size = strlen((char*)c.ptr);
return c;
}
}
void se_rplaca(list_t* cons, car_t car)
{
if(cons->car.alloced && cons->ptr)
{
switch(se_typeof(cons))
{
case SE_LIST:
se_free(cons->ptr); break;
case SE_STRING:
free(cons->ptr); break;
default: break;
}
}
memcpy(&cons->car, &car, sizeof(car_t));
}
void se_rplacd(list_t* cons, list_t* cdr)
{
if(cons->cdr)
se_free(cons->cdr);
cons->cdr = cdr;
}

@ -25,12 +25,12 @@ void test_exs()
exs_nappendc(test, 'A', 6);
exs_appendf(test, "\nCurrent size: %d -> %d", strlen(EXS(test)), exs_realsize(test));
exs_appendf(test, "\nCurrent size: %ld -> %ld", strlen(EXS(test)), exs_realsize(test));
exs_appendf(test, "AOISDJOAIJS%s", "sokdok");
printf("%s\n", EXS(test));
printf("strlen() %d, ->currentSize %d\n", strlen(EXS(test)), exs_realsize(test));
printf("strlen() %ld, ->currentSize %ld\n", strlen(EXS(test)), exs_realsize(test));
exs_free(test);
}
@ -60,6 +60,10 @@ void test_parse()
}
//printf("Parse okay.\n");
se_rplaca(list, se_make("test1"));
se_rplaca(list->cdr, se_make("(test)"));
se_rplacd(list->cdr->car_l, se_make("(list)").ptr);
se_print(NULL, list);
printf("\n");
// printf("(car list) -> %s\n", (char*)list->ptr);

Loading…
Cancel
Save