diff -urN v26/Makefile v27/Makefile --- v26/Makefile 2018-09-14 21:50:19.000000000 +0900 +++ v27/Makefile 2018-09-15 17:42:25.000000000 +0900 @@ -4,20 +4,29 @@ CFLAGS += $(DBG) OBJS = ezyaml.o objs.o chain.o mem.o +LIB = libezyaml.a TARG = ezyaml all: $(TARG) mem.c: chain.h objs.c: chain.h mem.h -ezyaml.c: objs.h mem.h - -ezyaml: $(OBJS) - gcc $(CFLAGS) -o $@ $(OBJS) +ezyaml.h: objs.h +ezyaml.c: ezyaml.h mem.h +ezyaml_main.c: mem.h + +ezyaml: ezyaml_main.o $(LIB) + gcc $(CFLAGS) -o $@ ezyaml_main.o $(LIB) + +lib: $(LIB) + +$(LIB): $(OBJS) + rm -rf $(LIB) + ar cq $@ $(OBJS) clean: rm -f *.o *~ *.pyc rm -rf __pycache__ - rm -f $(TARG) + rm -f $(TARG) $(LIB) # EOF diff -urN v26/ezyaml.c v27/ezyaml.c --- v26/ezyaml.c 2018-09-14 22:25:48.000000000 +0900 +++ v27/ezyaml.c 2018-09-15 17:45:43.000000000 +0900 @@ -1,7 +1,7 @@ -#include "objs.h" +#include "ezyaml.h" #include "mem.h" -void +static void err(char *t, char *s) { fprintf(stderr, "err %s \"%s\"\n", t, s); @@ -10,7 +10,7 @@ /**/ -char * +static char * slice_tail(char *s, int i) { return strlen(s) >= i ? s + i : ""; @@ -18,16 +18,16 @@ /**/ -int +static int is_qts(char c) { return c == '"' || c == '\''; } -char *kwd_true[] = { "True", "true", "TRUE", "ON", "On", "On", NULL }; -char *kwd_false[] = { "False", "false", "FALSE", "OFF", "Off", "off", NULL }; +static char *kwd_true[] = { "True", "true", "TRUE", "ON", "On", "On", NULL }; +static char *kwd_false[] = { "False", "false", "FALSE", "OFF", "Off", "off", NULL }; -int +static int is_any_str(char *s, char **ss) { while(*ss){ @@ -39,14 +39,14 @@ return 0; } -void +static void parse_append(struct obj *buf, int c) { char c1 = c; buf_append(buf, &c1, 1); } -void +static void parse_buf_to_lst(struct obj *lst, struct obj *buf) { parse_append(buf, '\0'); @@ -54,7 +54,7 @@ buf->n = 0; } -struct obj * +static struct obj * parse(char *s) { struct obj *lst = list_new(); @@ -99,7 +99,7 @@ return lst; } -int +static int find_idx(char *s, char *t) { int idx = 0; @@ -126,7 +126,7 @@ return -1; } -int +static int find_colon_idx(char *s) { int n; @@ -140,7 +140,7 @@ #define TYP_OTHER (-1) -int +static int get_kind(char *s) { if( strncmp(s, "- ", 2) == 0 ){ @@ -152,34 +152,34 @@ return TYP_OTHER; } -struct obj * +static struct obj * cut_tail_spc(struct obj *str) { str_trunc(str); return NULL; } -int +static int top_spc_num(char *s) { return *s == ' ' ? 1 + top_spc_num(s+1) : 0; } -void +static 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); -struct obj *get_list(int idt, struct obj *lines); -struct obj *get_other(int idt, struct obj *lines); -struct obj *get_value(char *s); -struct obj *get(int idt, int kind, struct obj *lines); -char *get_cont_str(int idt, struct obj *lines); +static struct obj *get_dict(int idt, struct obj *lines); +static struct obj *get_list(int idt, struct obj *lines); +static struct obj *get_other(int idt, struct obj *lines); +static struct obj *get_value(char *s); +static struct obj *get(int idt, int kind, struct obj *lines); +static char *get_cont_str(int idt, struct obj *lines); -struct obj * +static struct obj * get_dict_v(int idt, struct obj *lines) { char *s = lines_top_str(lines); @@ -211,7 +211,7 @@ return obj_new(OBJ_TYP_PTR, &vp); } -struct obj * +static struct obj * get_dict(int idt, struct obj *lines) { char *s = lines_pop_str(lines); @@ -240,8 +240,7 @@ } }else if(v->typ == OBJ_TYP_STR){ char *add = get_cont_str(idt+2, lines); - str_append(v, add); - free(add); + str_append_free(v, add); } dict_add(dic, k, v); @@ -260,7 +259,7 @@ return dic; } -struct obj * +static struct obj * get_list(int idt, struct obj *lines) { int idt_ = idt + 2; @@ -286,7 +285,7 @@ return lst; } -struct obj * +static struct obj * get_value(char *s) { int i, n; @@ -339,7 +338,7 @@ return str_new_n(s, n); } -char * +static char * get_cont_str(int idt, struct obj *lines) { if( list_len(lines) ){ @@ -364,21 +363,20 @@ return strdup(""); } -struct obj * +static struct obj * get_other(int idt, struct obj *lines) { char *s = lines_pop_str(lines); struct obj *v = get_value( slice_tail(s, idt) ); if(v->typ == OBJ_TYP_STR){ char *add = get_cont_str(idt+2, lines); - str_append(v, add); - free(add); + str_append_free(v, add); } free(s); return v; } -struct obj * +static struct obj * get(int idt, int kind, struct obj *lines) { if(kind == OBJ_TYP_DICT){ @@ -390,7 +388,7 @@ return get_other(idt, lines); } -struct obj * +static struct obj * cut_comment(struct obj *str) { char *s = obj_ptr(str); @@ -401,7 +399,7 @@ return NULL; } -int +static int is_not_empty(struct obj *str) { char *s = obj_ptr(str); @@ -409,7 +407,7 @@ } struct obj * -load(struct obj *str) +ezyaml_load(struct obj *str) { struct obj *obj, *objs; struct obj *lines = lines_new( obj_ptr(str) ); @@ -441,12 +439,12 @@ /**/ -struct obj *dump_list(struct obj *lst, int idt); -struct obj *dump_value(struct obj *obj, int from_dic); -struct obj *dump_other(struct obj *obj, int idt, int from_dic); -struct obj *dump_obj(struct obj *obj, int idt); +static struct obj *dump_list(struct obj *lst, int idt); +static struct obj *dump_value(struct obj *obj, int from_dic); +static struct obj *dump_other(struct obj *obj, int idt, int from_dic); +static struct obj *dump_obj(struct obj *obj, int idt); -struct obj * +static struct obj * wrap_line(struct obj *str, int idt) { struct obj *lst; @@ -487,7 +485,7 @@ return str; } -struct obj * +static struct obj * dump_dict(struct obj *dic, int idt) { struct obj *lines = list_new(); @@ -510,8 +508,7 @@ }else{ str_append(str, " "); tmp = dump_value(v, 'r'); - str_append( str, obj_ptr(tmp) ); - obj_del(tmp); + str_append_free( str, str_pop_del(tmp) ); str = wrap_line(str, idt); } list_append(lines, str); @@ -524,7 +521,7 @@ return str; } -struct obj * +static struct obj * dump_list(struct obj *lst, int idt) { struct obj *lines = list_new(); @@ -552,7 +549,7 @@ return str; } -int +static int is_not_same_str(char *s, struct obj *obj) { struct obj *v = get_value(s); @@ -561,7 +558,7 @@ return r; } -int +static int need_quote(char *s, struct obj *obj, int from_dic) { char h, t; @@ -584,7 +581,7 @@ return 0; } -struct obj * +static struct obj * dump_value(struct obj *obj, int from_dic) { if(obj->typ == OBJ_TYP_STR){ @@ -637,19 +634,18 @@ return str_new(""); /* ! */ } -struct obj * +static struct obj * dump_other(struct obj *obj, int idt, int from_dic) { struct obj *str = dump_value(obj, from_dic); char *s = cp_char_n(' ', idt); - s = cp_add( s, strdup( obj_ptr(str) ) ); - obj_del(str); + s = cp_add( s, str_pop_del(str) ); str = wrap_line( str_new(s), idt); free(s); return str; } -struct obj * +static struct obj * dump_obj(struct obj *obj, int idt) { int t = obj->typ; @@ -663,35 +659,11 @@ } struct obj * -dump(struct obj *obj) +ezyaml_dump(struct obj *obj) { struct obj *str = dump_obj(obj, 0); str_append(str, "\n"); return str; } -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_ptr(str); - fwrite( s, 1, strlen(s), stdout ); - obj_del(str); - - /* obj --> stderr ... */ - - obj_del(obj); - - mem_show(); - - return 0; -} - /* EOF */ diff -urN v26/ezyaml.h v27/ezyaml.h --- v26/ezyaml.h 1970-01-01 09:00:00.000000000 +0900 +++ v27/ezyaml.h 2018-09-15 12:39:56.000000000 +0900 @@ -0,0 +1,9 @@ +#ifndef __EZYAML_H__ +#define __EZYAML_H__ 1 + +#include "objs.h" + +struct obj *ezyaml_load(struct obj *str); +struct obj *ezyaml_dump(struct obj *obj); + +#endif diff -urN v26/ezyaml_main.c v27/ezyaml_main.c --- v26/ezyaml_main.c 1970-01-01 09:00:00.000000000 +0900 +++ v27/ezyaml_main.c 2018-09-15 12:39:56.000000000 +0900 @@ -0,0 +1,25 @@ +#include "ezyaml.h" +#include "mem.h" + +int +main(int ac, char **av) +{ + char *s; + struct obj *str = str_read(stdin); + struct obj *obj = ezyaml_load(str); + + obj_del(str); + str = ezyaml_dump(obj); + + s = obj_ptr(str); + fwrite( s, 1, strlen(s), stdout ); + obj_del(str); + + obj_del(obj); + + mem_show(); + + return 0; +} + +/* EOF */ diff -urN v26/objs.c v27/objs.c --- v26/objs.c 2018-09-14 21:53:22.000000000 +0900 +++ v27/objs.c 2018-09-15 17:45:43.000000000 +0900 @@ -350,6 +350,42 @@ return r; } +static int +slice_clip(int pos, int n) +{ + if(pos < 0){ + pos += n; + } + pos = pos < 0 ? 0 : pos; + pos = pos > n ? n : pos; + return pos; +} + + +char * +cp_slice(char *s, int head, int tail) +{ + /* free s */ + /* tail == 0 is tail == strlen(s) */ + int n; + char *r; + + if(!s){ + return NULL; + } + n = strlen(s); + + head = slice_clip(head, n); + tail = slice_clip(tail, n); + if(tail == 0){ + tail = n; + } + + r = head < tail ? strndup(s + head, tail - head ) : strdup(""); + free(s); + return r; +} + /**/ struct obj * @@ -389,9 +425,16 @@ } void +str_append_free(struct obj *str, char *s) +{ + obj_ptr(str) = cp_add( obj_ptr(str), s ); + /* free s */ +} + +void str_append(struct obj *str, char *s) { - obj_ptr(str) = cp_add( obj_ptr(str), strdup(s) ); + str_append_free( str, strdup(s) ); } void @@ -420,6 +463,15 @@ str_free_set( str, strdup(t) ); } +char * +str_pop_del(struct obj *str) +{ + char *s = obj_ptr(str); + obj_ptr(str) = NULL; + obj_del(str); + return s; +} + /**/ struct obj * @@ -615,9 +667,7 @@ char *s = NULL; struct obj *str = list_pop_top(lines); if(str){ - s = obj_ptr(str); - obj_ptr(str) = NULL; - obj_del(str); + s = str_pop_del(str); } return s; } diff -urN v26/objs.h v27/objs.h --- v26/objs.h 2018-09-14 05:09:22.000000000 +0900 +++ v27/objs.h 2018-09-15 17:45:43.000000000 +0900 @@ -71,14 +71,17 @@ char *cp_add(char *a, char *b); char *cp_replace(char *s, char *from, char *to); char *cp_char_n(char v, int n); +char *cp_slice(char *s, int head, int tail); struct obj *str_new(char *s); struct obj *str_new_n(char *s, int n); struct obj *str_read(FILE *fp); void str_free_set(struct obj *str, char *s); +void str_append_free(struct obj *str, char *s); void str_append(struct obj *str, char *s); void str_trunc(struct obj *str); void str_strip(struct obj *str); +char *str_pop_del(struct obj *str); struct obj *list_new(void); void list_del(struct obj *lst);