From f4b28ffb6a82ded6b16ab891dd1748c12b118f51 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Mon, 11 Aug 2008 02:27:21 +0000 Subject: [PATCH] --- ase/cmd/awk/awk.c | 21 +++++++++- ase/include/ase/cmn/sll.h | 8 ++++ ase/lib/cmn/sll.c | 80 ++++++++++++++++++--------------------- 3 files changed, 64 insertions(+), 45 deletions(-) diff --git a/ase/cmd/awk/awk.c b/ase/cmd/awk/awk.c index 1cb354fb..27be055d 100644 --- a/ase/cmd/awk/awk.c +++ b/ase/cmd/awk/awk.c @@ -1,5 +1,5 @@ /* - * $Id: awk.c 318 2008-08-07 11:02:08Z baconevi $ + * $Id: awk.c 321 2008-08-10 08:27:21Z baconevi $ */ #include @@ -1056,6 +1056,11 @@ static void handle_args (argc, argv) } #endif +static int dump_sf (ase_sll_t* sll, ase_sll_node_t* n, void* arg) +{ + ase_printf (ASE_T("%s\n"), n->data.ptr); +} + static int handle_args (int argc, ase_char_t* argv[], ase_sll_t* sf) { ase_cint_t c; @@ -1110,11 +1115,24 @@ static int handle_args (int argc, ase_char_t* argv[], ase_sll_t* sf) ase_size_t sz = ase_strlen(opt.arg) + 1; sz *= ASE_SIZEOF(*opt.arg); + if (ase_sll_getsize(sf) % 2) + { +wprintf (L"APPEND %S\n", opt.arg); if (ase_sll_append(sf, opt.arg, sz) == ASE_NULL) { out_of_memory (); return -1; } + } + else + { +wprintf (L"PREPEND %S\n", opt.arg); + if (ase_sll_prepend(sf, opt.arg, sz) == ASE_NULL) + { + out_of_memory (); + return -1; + } + } break; } @@ -1152,6 +1170,7 @@ static int handle_args (int argc, ase_char_t* argv[], ase_sll_t* sf) } ase_printf (ASE_T("[%d]\n"), (int)ase_sll_getsize(sf)); +ase_sll_walk (sf, dump_sf, ASE_NULL); #if 0 if (srcio->input_file == ASE_NULL) { diff --git a/ase/include/ase/cmn/sll.h b/ase/include/ase/cmn/sll.h index 061b1c20..ce524b69 100644 --- a/ase/include/ase/cmn/sll.h +++ b/ase/include/ase/cmn/sll.h @@ -119,6 +119,14 @@ ase_sll_node_t* ase_sll_gettail ( ase_sll_t* sll /* a singly linked list */ ); +/* Inserts data before a positional node given */ +ase_sll_node_t* ase_sll_insert ( + ase_sll_t* sll /* a singly linked list */, + ase_sll_node_t* pos /* a node before which a new node is inserted */, + void* dptr /* the pointer to the data */ , + ase_size_t dlen /* the length of the data in bytes */ +); + /* Traverses s singly linked list */ void ase_sll_walk (ase_sll_t* sll, ase_sll_walker_t walker, void* arg); diff --git a/ase/lib/cmn/sll.c b/ase/lib/cmn/sll.c index 6298dd7b..543c5484 100644 --- a/ase/lib/cmn/sll.c +++ b/ase/lib/cmn/sll.c @@ -98,7 +98,7 @@ void ase_sll_setfreeer (ase_sll_t* sll, ase_sll_freeer_t freeer) sll->freeer = freeer; } -static ase_sll_node_t* alloc_node (ase_sll_t* sll, void* data, ase_size_t size) +static ase_sll_node_t* alloc_node (ase_sll_t* sll, void* dptr, ase_size_t dlen) { ase_sll_node_t* n; @@ -106,51 +106,61 @@ static ase_sll_node_t* alloc_node (ase_sll_t* sll, void* data, ase_size_t size) { n = ASE_MALLOC (sll->mmgr, ASE_SIZEOF(ase_sll_node_t)); if (n == ASE_NULL) return ASE_NULL; - n->data.ptr = data; + n->data.ptr = dptr; } else if (sll->copier == ASE_SLL_COPIER_INLINE) { - n = ASE_MALLOC (sll->mmgr, ASE_SIZEOF(ase_sll_node_t) + size); + n = ASE_MALLOC (sll->mmgr, ASE_SIZEOF(ase_sll_node_t) + dlen); if (n == ASE_NULL) return ASE_NULL; - ASE_MEMCPY (n + 1, data, size); + ASE_MEMCPY (n + 1, dptr, dlen); n->data.ptr = n + 1; } else { n = ASE_MALLOC (sll->mmgr, ASE_SIZEOF(ase_sll_node_t)); if (n == ASE_NULL) return ASE_NULL; - n->data.ptr = sll->copier (sll, data, size); + n->data.ptr = sll->copier (sll, dptr, dlen); } - n->data.len = size; + n->data.len = dlen; n->next = ASE_NULL; -} - -ase_sll_node_t* ase_sll_prepend (ase_sll_t* sll, void* data, ase_size_t size) -{ - ase_sll_node_t* n = alloc_node (sll, data, size); - if (n == ASE_NULL) return ASE_NULL; - - n->next = sll->head; - - sll->head = n; - if (sll->tail == ASE_NULL) sll->tail = n; - sll->size++; return n; } -ase_sll_node_t* ase_sll_append (ase_sll_t* sll, void* data, ase_size_t size) +ase_sll_node_t* ase_sll_insert ( + ase_sll_t* sll, ase_sll_node_t* pos, void* dptr, ase_size_t dlen) { - ase_sll_node_t* n = alloc_node (sll, data, size); + ase_sll_node_t* n = alloc_node (sll, dptr, dlen); if (n == ASE_NULL) return ASE_NULL; - if (sll->tail != ASE_NULL) sll->tail->next = n; - sll->tail = n; - if (sll->head == ASE_NULL) sll->head = n; - sll->size++; + if (pos == ASE_NULL) + { + /* insert at the end */ + if (sll->head == ASE_NULL) + { + ASE_ASSERT (sll->tail == ASE_NULL); + sll->head = n; + } + else sll->tail->next = n; + sll->tail = n; + } + else + { + /* insert in front of the positional node */ + n->next = pos; + if (pos == sll->head) sll->head = n; + else + { + ase_sll_node_t* n2 = sll->head; + while (n2->next != pos) n2 = n2->next; + n2->next = n; + } + } + + sll->size++; return n; } @@ -159,27 +169,9 @@ ase_sll_node_t* ase_sll_prepend (ase_sll_t* sll, void* data, ase_size_t size) return ase_sll_insert (sll, sll->head, data, size); } - -ase_sll_node_t* ase_sll_insert ( - ase_sll_t* sll, ase_sll_node_t* pos, void* data, ase_size_t size) +ase_sll_node_t* ase_sll_append (ase_sll_t* sll, void* data, ase_size_t size) { - ase_sll_node_t* n = alloc_node (sll, data, size); - if (n == ASE_NULL) return ASE_NULL; - - if (pos == ASE_NULL) - { - /* insert at the end */ - } - else - { - /* insert in front of the positional node */ - n->next = pos->next; - pos->next = n; - - if (pos == sll->head) sll->head = n; - /* TODO: update head & tail properly */ - } - return n; + return ase_sll_insert (sll, ASE_NULL, data, size); } void ase_sll_walk (ase_sll_t* sll, ase_sll_walker_t walker, void* arg)