diff -urN v8/Makefile v9/Makefile --- v8/Makefile 1970-01-01 09:00:00.000000000 +0900 +++ v9/Makefile 2018-09-06 02:13:01.000000000 +0900 @@ -0,0 +1,16 @@ +CC = gcc +CFLAGS += -Wall +CFLAGS += -g + +OBJS = ezyaml.o objs.o + +all: ezyaml + +ezyaml: $(OBJS) + gcc $(CFLAGS) -o $@ $(OBJS) + +clean: + rm -f *.o *~ *.pyc + rm -rf __pycache__ + +# EOF diff -urN v8/ezyaml.c v9/ezyaml.c --- v8/ezyaml.c 1970-01-01 09:00:00.000000000 +0900 +++ v9/ezyaml.c 2018-09-06 02:13:01.000000000 +0900 @@ -0,0 +1,331 @@ +#include "objs.h" + +static struct obj * +lines_dump_sub(struct obj *str) +{ + fprintf( stderr, "\"%s\"\n", (char *)obj_buf(str) ); + return NULL; +} + +void +lines_dump(struct obj *lines) +{ + list_map(lines, lines_dump_sub); +} + +/**/ + +char * +slice_tail(char *s, int i) +{ + return strlen(s) >= i ? s + i : ""; +} + +/**/ + +int +is_qts(char c) +{ + return c == '"' || c == '\''; +} + +struct obj * +parse_append(struct obj *buf, int c) +{ + char c1 = c; + return buf_append(buf, &c1, 1); +} + +struct obj * +parse(char *s) +{ + struct obj *lst = list_new(); + struct obj *buf = buf_new(1024); + + int in_esc = 0; + int qt = 0; + + while(*s){ + int c = *s++; + + if(!in_esc && c == '\\'){ + in_esc = 1; + continue; + } + if(in_esc){ + in_esc = 0; + buf = parse_append(buf, c); + continue; + } + if(qt){ + if( is_qts(c) ){ + buf = parse_append(buf, c); + buf = parse_append(buf, '\0'); + list_append( lst, str_new( obj_buf(buf) ) ); + buf->buf_n = 0; + qt = 0; + continue; + } + }else if( is_qts(c) ){ + qt = c; + if(buf->buf_n > 0){ + buf = parse_append(buf, '\0'); + list_append( lst, str_new( obj_buf(buf) ) ); + buf->buf_n = 0; + } + } + buf = parse_append(buf, c); + } + + if(buf->buf_n > 0){ + buf = parse_append(buf, '\0'); + list_append( lst, str_new( obj_buf(buf) ) ); + buf->buf_n = 0; + } + obj_del(buf); + + return lst; +} + +int +find_idx(char *s, char c) +{ + int idx = 0; + struct obj *lst = parse(s); + while( list_len(lst) > 0 ){ + struct obj *str = list_pop_top(lst); + s = obj_buf(str); + if( !is_qts(*s) && strchr(s, c) ){ + return idx + ( strchr(s, c) - s ); + } + idx += strlen(s); + } + return -1; +} + +int +find_colon_idx(char *s) +{ + return find_idx(s, ':'); +} + +#define TYP_OTHER (-1) + +int +get_kind(char *s) +{ + if( strncmp(s, "- ", 2) == 0 ){ + return OBJ_TYP_LIST; + } + if( find_colon_idx(s) >= 0 ){ + return OBJ_TYP_DICT; + } + return TYP_OTHER; +} + +struct obj * +cut_tail_spc(struct obj *str) +{ + char *s = obj_buf(str); + while(1){ + char *t = s + strlen(s) - 1; + if(*t != ' ' && *t != '\t'){ + str->buf_n = strlen(s) + 1; + break; + } + *t = '\0'; + } + return NULL; +} + +int +top_spc_num(char *s) +{ + return *s == ' ' ? 1 + top_spc_num(s+1) : 0; +} + +void +get_idt_kind(char *s, int *idt, int *kind) +{ + *idt = top_spc_num(s); + *kind = get_kind( slice_tail(s, *idt) ); +} + +struct obj * +get_dict(int idt, struct obj *lines) +{ + // ... + return NULL; +} + +struct obj * +get_list(int idt, struct obj *lines) +{ + struct obj *get(int idt, int kind, struct obj *lines); + + int idt_ = idt + 2; + char *s = lines_top_str(lines); + int kind_ = get_kind( slice_tail(s, idt_) ); + struct obj *v = get(idt_, kind_, lines); + struct obj *lst = list_new(); + + list_append(lst, v); + + if( list_len(lines) ){ + char *s = lines_top_str(lines); + int idt_, kind_; + get_idt_kind(s, &idt_, &kind_); + if(kind_ == OBJ_TYP_LIST && idt_ == idt){ + struct obj *add = get_list(idt, lines); + while( list_len(add) ){ + list_append( lst, list_pop_top(add) ); + } + obj_del(add); + } + } + return lst; +} + +struct obj * +get_value(char *s) +{ + // ... + return NULL; +} + +struct obj * +get_cont_str(int idt, struct obj *lines) +{ + if( list_len(lines) ){ + char *s = lines_top_str(lines); + int idt_, kind_; + get_idt_kind(s, &idt_, &kind_); + if(kind_ == TYP_OTHER && idt_ == idt){ /* ! */ + struct obj *v = get_value( slice_tail(s, idt) ); + int typ = v->typ; + if( ( typ == OBJ_TYP_STR && strlen( obj_buf(v) ) ) || + ( typ == OBJ_TYP_INT || typ == OBJ_TYP_DOUBLE ) ){ + ; + struct obj *str = list_pop_top(lines); + char *s = slice_tail( obj_buf(str), idt ); + struct obj *add = get_cont_str(idt, lines); + struct obj *ret = str_new(" "); + str_append(ret, s); + str_append( ret, obj_buf(add) ); + + obj_del(str); + obj_del(add); + return ret; + } + + } + } + return str_new(""); +} + +struct obj * +get_other(int idt, struct obj *lines) +{ + char *s = lines_top_str(lines); + struct obj *v = get_value( slice_tail(s, idt) ); + if(v->typ == OBJ_TYP_STR){ + struct obj *add = get_cont_str(idt+2, lines); + v = str_append( v, obj_buf(add) ); + obj_del(add); + } + return v; +} + +struct obj * +get(int idt, int kind, struct obj *lines) +{ + if(kind == OBJ_TYP_DICT){ + return get_dict(idt, lines); + } + if(kind == OBJ_TYP_LIST){ + return get_list(idt, lines); + } + return get_other(idt, lines); +} + +struct obj * +cut_comment(struct obj *str) +{ + char *s = obj_buf(str); + char *t = strchr(s, '#'); + if(t){ + *t = '\0'; + str->buf_n = strlen(s) + 1; + } + return NULL; +} + +int +is_not_empty(struct obj *str) +{ + char *s = obj_buf(str); + return *s != '\0'; +} + +struct obj * +load(struct obj *str) +{ + struct obj *obj, *objs; + struct obj *lines = lines_new( obj_buf(str) ); + struct obj *lst_trash = list_new(); + + list_map(lines, cut_comment); + list_map(lines, cut_tail_spc); + list_filter(lines, is_not_empty, lst_trash); + list_del(lst_trash); + + lines_dump(lines); + + objs = list_new(); +#if 0 + while( list_len(lines) ){ + int idt, kind; + get_idt_kind( obj_buf( list_top(lines) ), &idt, &kind ); + obj = get(idt, kind, lines); + list_append(objs, obj); + } +#endif + + obj_del(lines); + + obj = objs; + if(list_len(objs) == 1){ + obj = list_pop_top(objs); + obj_del(objs); + } + return obj; +} + +struct obj * +dump(struct obj *obj) +{ + return str_new("foo: bar\n"); /* ... */ +} + +int +main(int ac, char **av) +{ + struct obj *obj; + struct obj *str = str_read(stdin); + char *s; + + obj = load(str); + obj_del(str); + + str = dump(obj); + s = obj_buf(str); + fwrite( s, 1, strlen(s), stdout ); + obj_del(str); + + /* obj --> stderr ... */ + + obj_del(obj); + return 0; +} + +/* EOF */ diff -urN v8/ezyaml.py v9/ezyaml.py --- v8/ezyaml.py 2018-08-31 22:54:29.000000000 +0900 +++ v9/ezyaml.py 2018-09-06 02:13:01.000000000 +0900 @@ -182,7 +182,7 @@ lines = s.strip().split('\n') lines = list( map(cut_comment, lines) ) lines = list( map(cut_tail_spc, lines) ) - liess = list( filter(lambda s: s, lines) ) + lines = list( filter(lambda s: s, lines) ) objs = [] while lines: diff -urN v8/objs.c v9/objs.c --- v8/objs.c 1970-01-01 09:00:00.000000000 +0900 +++ v9/objs.c 2018-09-06 02:13:01.000000000 +0900 @@ -0,0 +1,510 @@ +#include "objs.h" + +struct obj * +objs_tail(struct objs *objs) +{ + return objs->top ? objs->top->prev : NULL; +} + +void +objs_append(struct objs *objs, struct obj *obj) +{ + obj->next = objs->top; + obj->prev = objs_tail(objs); + + if(obj->next){ + obj->next->prev = obj; + }else{ + obj->next = obj; + } + + if(obj->prev){ + obj->prev->next = obj; + }else{ + obj->prev = obj; + } + + if(objs->top == NULL){ + objs->top = obj; + } + + objs->n++; +} + +void +objs_insert(struct objs *objs, struct obj *obj) +{ + objs_append(objs, obj); + objs->top = obj; +} + +void +objs_del_obj(struct objs *objs, struct obj *obj) +{ + if(obj){ + if(obj->prev != obj){ + obj->prev->next = obj->next; + } + if(obj->next != obj){ + obj->next->prev = obj->prev; + } + if(objs->top == obj){ + objs->top = (obj->next != obj) ? obj->next : NULL; + } + objs->n--; + obj->next = obj->prev = NULL; + /* not free obj */ + } +} + +void +objs_del_obj_all(struct objs *objs) +{ + while(objs->n > 0){ + struct obj *obj = objs->top; + objs_del_obj(objs, obj); + obj_del(obj); /* free */ + } +} + +struct obj * +objs_pop_top(struct objs *objs) +{ + struct obj *obj = objs->top; + objs_del_obj(objs, obj); + return obj; +} + +struct obj * +objs_pop_tail(struct objs *objs) +{ + struct obj *obj = objs_tail(objs); + objs_del_obj(objs, obj); + return obj; +} + +int +objs_sum_alc_n(struct objs *objs) +{ + struct obj *obj = objs->top; + int sum = 0, n = objs->n, i; + + for(i=0; ialc_n; + obj = obj->next; + } + return sum; +} + +void +objs_copy_to_obj_arr(struct objs *objs, struct obj *arr) +{ + /* + struct obj arr[ objs_sum_alc_n(objs) ]; + */ + + struct objs dsts = OBJS_INIT; + + struct obj *obj = objs->top; + int sum = 0, n = objs->n, i; + + for(i=0; ialc_n; + obj = obj->next; + } +} + +/**/ + +int +obj_typ_sz(int typ) +{ + switch( OBJ_TYP_BASE(typ) ){ + case OBJ_TYP_INT: return sizeof(int); + case OBJ_TYP_DOUBLE: return sizeof(double); + case OBJ_TYP_PTR: return sizeof(void *); + case OBJ_TYP_OBJS: return sizeof(struct objs); + case OBJ_TYP_BUF: /* ! */ + default: + break; + } + return 0; +} + +int +obj_req_sz(int typ, void *vp, int buf_sz) +{ + int sz; + + switch(typ){ + case OBJ_TYP_BUF: + return buf_sz; + case OBJ_TYP_STR: + if(!vp){ + return buf_sz; + } + sz = strlen(vp) + 1; + return buf_sz > sz ? buf_sz : sz; + default: + break; + } + return obj_typ_sz(typ); +} + +int +obj_alc_n(int req_sz) +{ + struct obj *obj; + int osz = sizeof(*obj); + int add = req_sz - sizeof(obj->any); + return ( osz + (add > 0 ? add : 0) + osz - 1 ) / osz; +} + +void +obj_init(struct obj *obj, int typ, void *vp, int buf_sz) +{ + int req_sz = obj_req_sz(typ, vp, buf_sz); + int alc_n = obj_alc_n(req_sz); + + obj->prev = obj->next = NULL; + obj->alc_n = alc_n; + obj->typ = typ; + obj->buf_sz = OBJ_TYP_BASE(typ) == OBJ_TYP_BUF ? req_sz : 0; + obj->buf_n = 0; + if(vp && req_sz > 0){ + memcpy(&obj->any, vp, req_sz); + obj->buf_n = req_sz; + } +} + +void +obj_init_obj(struct obj *dst, struct obj *src) +{ + obj_init(dst, src->typ, &src->any, src->buf_sz); +} + +void +obj_fini(struct obj *obj) +{ + if( (obj->typ & OBJ_TYP_MASK) == OBJ_TYP_OBJS ){ + objs_del_obj_all(&obj->any.objs); + } +} + +struct obj * +obj_new(int typ, void *vp, int buf_sz) +{ + int req_sz = obj_req_sz(typ, vp, buf_sz); + int alc_n = obj_alc_n(req_sz); + struct obj *obj = malloc( sizeof(*obj) * alc_n ); + + obj_init(obj, typ, vp, buf_sz); + + return obj; +} + +void +obj_del(struct obj *obj) +{ + obj_fini(obj); + free(obj); +} + +void * +obj_buf(struct obj *obj) +{ + return obj->any.buf; +} + +/**/ + +struct obj * +buf_new(int buf_sz) +{ + return obj_new(OBJ_TYP_BUF, NULL, buf_sz); +} + +struct obj * +buf_append(struct obj *buf, void *vp, int sz) +{ + char *dst; + int asz = buf->buf_n + sz; + + if(asz > buf->buf_sz){ + struct obj *bak = buf; + int rsz = bak->buf_sz * 2; + while(asz > rsz){ + rsz *= 2; + } + buf = buf_new(rsz); + memcpy( obj_buf(buf), obj_buf(bak), bak->buf_n ); + buf->buf_n = bak->buf_n; + obj_del(bak); /* ! */ + } + + dst = obj_buf(buf); + dst += buf->buf_n; + memcpy(dst, vp, sz); + buf->buf_n += sz; + return buf; +} + +struct obj * +buf_append_str(struct obj *buf, char *s) +{ + return buf_append( buf, s, strlen(s)+1 ); +} + +struct obj * +buf_read(FILE *fp, int add_sz) +{ + char buf[4096]; + struct obj bufs, *obj; + int n; + + bufs_init(&bufs); + while((n = fread(buf, 1, sizeof(buf), fp)) > 0){ + bufs_append(&bufs, buf, n); + } + if(add_sz > 0){ + bufs_append(&bufs, NULL, add_sz); + } + obj = bufs_join(&bufs); + obj->buf_n -= add_sz; + bufs_fini(&bufs); + return obj; +} + +/**/ + +struct obj * +str_new(char *s) +{ + return obj_new(OBJ_TYP_STR, s, 0); +} + +struct obj * +str_append(struct obj *str, char *s) +{ + str->typ = OBJ_TYP_BUF; /* ! */ + str = buf_append_str(str, s); + str->typ = OBJ_TYP_STR; /* ! */ + return str; +} + +struct obj * +str_read(FILE *fp) +{ + struct obj *str = buf_read(fp, 1); + char c = '\0'; + buf_append(str, &c, 1); + str->typ = OBJ_TYP_STR; /* ! */ + return str; +} + +/**/ + +void +list_init(struct obj *obj) +{ + struct objs objs = OBJS_INIT; + obj_init(obj, OBJ_TYP_LIST, &objs, 0); +} + +void +list_fini(struct obj *lst) +{ + obj_fini(lst); +} + +struct obj * +list_new(void) +{ + struct objs objs = OBJS_INIT; + return obj_new(OBJ_TYP_LIST, &objs, 0); +} + +void +list_del(struct obj *lst) +{ + obj_del(lst); +} + +struct objs * +list_objs(struct obj *lst) +{ + return &lst->any.objs; +} + +void +list_insert(struct obj *lst, struct obj *obj) +{ + objs_insert( list_objs(lst), obj ); +} + +void +list_append(struct obj *lst, struct obj *obj) +{ + objs_append( list_objs(lst), obj ); +} + +void +list_del_obj(struct obj *lst, struct obj *obj) +{ + objs_del_obj( list_objs(lst), obj ); + /* not free obj */ +} + +struct obj * +list_pop_top(struct obj *lst) +{ + return objs_pop_top( list_objs(lst) ); +} + +struct obj * +list_pop_tail(struct obj *lst) +{ + return objs_pop_tail( list_objs(lst) ); +} + +int +list_len(struct obj *lst) +{ + return list_objs(lst)->n; +} + +struct obj * +list_top(struct obj *lst) +{ + return list_objs(lst)->top; +} + +struct obj * +list_tail(struct obj *lst) +{ + return objs_tail( list_objs(lst) ); +} + +struct obj * +list_map(struct obj *lst, struct obj *(*fp)(struct obj *e)) +{ + struct obj *rlst = list_new(); + struct obj *e = list_top(lst); + int n = list_len(lst), i; + + for(i=0; inext; + } + + if( list_len(rlst) == 0 ){ + obj_del(rlst); + rlst = NULL; + } + return rlst; +} + +void +list_filter(struct obj *lst, int (*fp)(struct obj *e), struct obj *lst_false) +{ + struct obj *e = list_top(lst); + int n = list_len(lst), i; + + for(i=0; inext; + if( !(*fp)(e) ){ + list_del_obj(lst, e); + if(lst_false){ + list_append(lst_false, e); + } + } + e = next; + } +} + +/**/ + +void +bufs_init(struct obj *bufs) +{ + list_init(bufs); +} + +void +bufs_append(struct obj *bufs, void *buf, int buf_sz) +{ + list_append( bufs, obj_new(OBJ_TYP_BUF, buf, buf_sz) ); +} + +int +bufs_sum_sz(struct obj *bufs) +{ + struct obj *obj = list_top(bufs); + int sum = 0, n = list_len(bufs), i; + + for(i=0; ibuf_sz; + obj = obj->next; + } + return sum; +} + +struct obj * +bufs_join(struct obj *bufs) +{ + int sz = bufs_sum_sz(bufs); + char buf[sz], *dp = buf; + + struct obj *obj = list_top(bufs); + int n = list_len(bufs), i; + + for(i=0; ibuf_sz ); + dp += obj->buf_sz; + obj = obj->next; + } + + return obj_new(OBJ_TYP_BUF, buf, sz); + /* not free bufs */ +} + +void +bufs_fini(struct obj *bufs) +{ + while( list_len(bufs) > 0 ){ + obj_del( list_pop_top(bufs) ); + } +} + +/**/ + +struct obj * +lines_new(char *s) +{ + struct obj *lines = list_new(); + + while(s && *s){ + char *t = strchr(s, '\n'); + if(t){ + *t++ = '\0'; + } + list_append( lines, str_new(s) ); + s = t; + } + return lines; +} + +char * +lines_top_str(struct obj *lines) +{ + return list_len(lines) ? obj_buf( list_top(lines) ) : NULL; +} + +/* EOF */ diff -urN v8/objs.h v9/objs.h --- v8/objs.h 1970-01-01 09:00:00.000000000 +0900 +++ v9/objs.h 2018-09-06 02:13:01.000000000 +0900 @@ -0,0 +1,153 @@ +#ifndef __OBJS_H__ +#define __OBJS_H__ 1 + +#include +#include +#include + +struct objs{ + struct obj *top; + int n; +}; + +#define OBJS_INIT { NULL, 0 } + +struct obj *objs_tail(struct objs *objs); +void objs_insert(struct objs *objs, struct obj *obj); +void objs_append(struct objs *objs, struct obj *obj); +void objs_del_obj(struct objs *objs, struct obj *obj); +void objs_del_obj_all(struct objs *objs); + +struct obj *objs_pop_top(struct objs *objs); +struct obj *objs_pop_tail(struct objs *objs); + +int objs_sum_alc_n(struct objs *objs); +void objs_copy_to_obj_arr(struct objs *objs, struct obj *arr); + +/**/ + +struct obj{ + struct obj *prev, *next; + int alc_n; + int typ; + int buf_sz; + int buf_n; + union{ + int i; + double d; + void *p; + struct objs objs; + char buf[1]; + }any; +}; + +#define OBJ_TYP_INT 0 +#define OBJ_TYP_DOUBLE 1 +#define OBJ_TYP_PTR 2 +#define OBJ_TYP_OBJS 3 +#define OBJ_TYP_BUF 4 + +#define OBJ_TYP_EXP_SFT 8 +#define OBJ_TYP_EXP(i) ( (i) << OBJ_TYP_EXP_SFT ) +#define OBJ_TYP_BOOL ( OBJ_TYP_EXP(1) | OBJ_TYP_INT ) +#define OBJ_TYP_LIST ( OBJ_TYP_EXP(1) | OBJ_TYP_OBJS ) +#define OBJ_TYP_DICT ( OBJ_TYP_EXP(2) | OBJ_TYP_OBJS ) +#define OBJ_TYP_STR ( OBJ_TYP_EXP(1) | OBJ_TYP_BUF ) + +#define OBJ_TYP_MASK ( (1<alc_n ]; + +obj_init_obj(obj, src); +obj_del(src); + : +*/ + +/* + +struct objs srcs = OBJS_INIT; + : + +struct obj arr[ objs_sum_alc_n(&srcs) ]; +struct objs dsts = { arr, srsc->n }; + +objs_copy_to_obj_arr(dsts, arr); + : +*/ + +struct obj *buf_new(int buf_sz); +struct obj *buf_append(struct obj *buf, void *vp, int sz); +struct obj *buf_append_str(struct obj *buf, char *s); +struct obj *buf_read(FILE *fp, int add_sz); + +struct obj *str_new(char *s); +struct obj *str_append(struct obj *str, char *s); +struct obj *str_read(FILE *fp); + +void list_init(struct obj *obj); +void list_fini(struct obj *lst); +struct obj *list_new(void); +void list_del(struct obj *lst); + +struct objs *list_objs(struct obj *lst); + +void list_insert(struct obj *lst, struct obj *obj); +void list_append(struct obj *lst, struct obj *obj); +void list_del_obj(struct obj *lst, struct obj *obj); +struct obj *list_pop_top(struct obj *lst); +struct obj *list_pop_tail(struct obj *lst); + +int list_len(struct obj *lst); +struct obj *list_top(struct obj *lst); +struct obj *list_tail(struct obj *lst); + +struct obj *list_map(struct obj *lst, struct obj *(*fp)(struct obj *e)); +void list_filter(struct obj *lst, int (*fp)(struct obj *e), struct obj *lst_false); + +void bufs_init(struct obj *bufs); +void bufs_append(struct obj *bufs, void *buf, int buf_sz); +int bufs_sum_sz(struct obj *bufs); +struct obj *bufs_join(struct obj *bufs); +void bufs_fini(struct obj *bufs); + +struct obj *lines_new(char *s); +char *lines_top_str(struct obj *lines); + +#endif