Commit bbc46f0c bbc46f0ca2ec67b0c20355ceba3b9e5349032b47 by Sergey Poznyakoff

list: new operations: head and tail.

* include/mailutils/list.h (mu_list_head, mu_list_tail): New prototypes.
* libmailutils/list/head.c: New file.
* libmailutils/list/tail.c: New file.
* libmailutils/list/Makefile.am (liblist_la_SOURCES): Add new files.
* libmailutils/tests/listop.c (head,tail): New commands.
* libmailutils/tests/list.at: Test head and tail.
1 parent 9a419c50
......@@ -86,6 +86,11 @@ int mu_list_count (mu_list_t _list, size_t *_pcount);
/* Get _indexth element from the list and store it in _pitem. */
int mu_list_get (mu_list_t _list, size_t _index, void **_pitem);
/* Retrieve first element of _list */
int mu_list_head (mu_list_t _list, void **_pitem);
/* Retrieve last element of _list */
int mu_list_tail (mu_list_t _list, void **_pitem);
/* Store at most _count first elements from _list in the _array. Unless
_pcount is NULL, fill it with the actual number of elements copied. */
int mu_list_to_array (mu_list_t _list, void **_array, size_t _count,
......
......@@ -29,6 +29,7 @@ liblist_la_SOURCES = \
get.c\
getcomp.c\
gmap.c\
head.c\
insert.c\
intersect.c\
iterator.c\
......@@ -41,7 +42,8 @@ liblist_la_SOURCES = \
setcomp.c\
setdestr.c\
slice.c\
slice2.c
slice2.c\
tail.c
INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils
......
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2011 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <mailutils/sys/list.h>
#include <mailutils/errno.h>
int
mu_list_head (mu_list_t list, void **pitem)
{
if (list == NULL)
return EINVAL;
if (pitem == NULL)
return MU_ERR_OUT_PTR_NULL;
if (!list->head.next)
return MU_ERR_NOENT;
*pitem = list->head.next->item;
return 0;
}
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2011 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <mailutils/sys/list.h>
#include <mailutils/errno.h>
int
mu_list_tail (mu_list_t list, void **pitem)
{
if (list == NULL)
return EINVAL;
if (pitem == NULL)
return MU_ERR_OUT_PTR_NULL;
if (!list->head.prev)
return MU_ERR_NOENT;
*pitem = list->head.prev->item;
return 0;
}
......@@ -103,6 +103,18 @@ TESTLIST([get],[],
[fire
])
TESTLIST([head],[],
[add en to tre fire fem
head],
[en
])
TESTLIST([tail],[],
[add en to tre fire fem
tail],
[fem
])
# ------------------------------------------------------------
# Iterators
......
......@@ -685,6 +685,42 @@ slice (mu_list_t *plist, int argc, char **argv)
}
void
head (size_t argc, mu_list_t list)
{
int rc;
const char *text;
if (argc != 1)
{
fprintf (stderr, "head ?\n");
return;
}
rc = mu_list_head (list, (void**) &text);
if (rc)
mu_diag_funcall (MU_DIAG_ERROR, "mu_list_head", NULL, rc);
else
printf ("%s\n", text);
}
void
tail (size_t argc, mu_list_t list)
{
int rc;
const char *text;
if (argc != 1)
{
fprintf (stderr, "head ?\n");
return;
}
rc = mu_list_tail (list, (void**) &text);
if (rc)
mu_diag_funcall (MU_DIAG_ERROR, "mu_list_tail", NULL, rc);
else
printf ("%s\n", text);
}
void
help ()
{
printf ("count\n");
......@@ -708,6 +744,8 @@ help ()
printf ("quit\n");
printf ("iter num\n");
printf ("help\n");
printf ("head\n");
printf ("tail\n");
printf ("NUMBER\n");
}
......@@ -819,6 +857,10 @@ shell (mu_list_t list)
find (itr[num], ws.ws_wordv[1]);
else if (strcmp (ws.ws_wordv[0], "help") == 0)
help ();
else if (strcmp (ws.ws_wordv[0], "head") == 0)
head (ws.ws_wordc, list);
else if (strcmp (ws.ws_wordv[0], "tail") == 0)
tail (ws.ws_wordc, list);
else if (ws.ws_wordc == 1)
{
char *p;
......