This commit is contained in:
hyung-hwan 2008-08-11 02:27:21 +00:00
parent 9b0309eacc
commit f4b28ffb6a
3 changed files with 64 additions and 45 deletions

View File

@ -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 <ase/awk/awk.h> #include <ase/awk/awk.h>
@ -1056,6 +1056,11 @@ static void handle_args (argc, argv)
} }
#endif #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) static int handle_args (int argc, ase_char_t* argv[], ase_sll_t* sf)
{ {
ase_cint_t c; 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; ase_size_t sz = ase_strlen(opt.arg) + 1;
sz *= ASE_SIZEOF(*opt.arg); 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) if (ase_sll_append(sf, opt.arg, sz) == ASE_NULL)
{ {
out_of_memory (); out_of_memory ();
return -1; 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; 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_printf (ASE_T("[%d]\n"), (int)ase_sll_getsize(sf));
ase_sll_walk (sf, dump_sf, ASE_NULL);
#if 0 #if 0
if (srcio->input_file == ASE_NULL) if (srcio->input_file == ASE_NULL)
{ {

View File

@ -119,6 +119,14 @@ ase_sll_node_t* ase_sll_gettail (
ase_sll_t* sll /* a singly linked list */ 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 */ /* Traverses s singly linked list */
void ase_sll_walk (ase_sll_t* sll, ase_sll_walker_t walker, void* arg); void ase_sll_walk (ase_sll_t* sll, ase_sll_walker_t walker, void* arg);

View File

@ -98,7 +98,7 @@ void ase_sll_setfreeer (ase_sll_t* sll, ase_sll_freeer_t freeer)
sll->freeer = 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; 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)); n = ASE_MALLOC (sll->mmgr, ASE_SIZEOF(ase_sll_node_t));
if (n == ASE_NULL) return ASE_NULL; if (n == ASE_NULL) return ASE_NULL;
n->data.ptr = data; n->data.ptr = dptr;
} }
else if (sll->copier == ASE_SLL_COPIER_INLINE) 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; if (n == ASE_NULL) return ASE_NULL;
ASE_MEMCPY (n + 1, data, size); ASE_MEMCPY (n + 1, dptr, dlen);
n->data.ptr = n + 1; n->data.ptr = n + 1;
} }
else else
{ {
n = ASE_MALLOC (sll->mmgr, ASE_SIZEOF(ase_sll_node_t)); n = ASE_MALLOC (sll->mmgr, ASE_SIZEOF(ase_sll_node_t));
if (n == ASE_NULL) return ASE_NULL; 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; 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; 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 (n == ASE_NULL) return ASE_NULL;
if (sll->tail != ASE_NULL) sll->tail->next = n; if (pos == ASE_NULL)
sll->tail = n; {
if (sll->head == ASE_NULL) sll->head = n; /* insert at the end */
sll->size++; 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; 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); return ase_sll_insert (sll, sll->head, data, size);
} }
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* data, ase_size_t size)
{ {
ase_sll_node_t* n = alloc_node (sll, data, size); return ase_sll_insert (sll, ASE_NULL, 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;
} }
void ase_sll_walk (ase_sll_t* sll, ase_sll_walker_t walker, void* arg) void ase_sll_walk (ase_sll_t* sll, ase_sll_walker_t walker, void* arg)