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); ...@@ -86,6 +86,11 @@ int mu_list_count (mu_list_t _list, size_t *_pcount);
86 86
87 /* Get _indexth element from the list and store it in _pitem. */ 87 /* Get _indexth element from the list and store it in _pitem. */
88 int mu_list_get (mu_list_t _list, size_t _index, void **_pitem); 88 int mu_list_get (mu_list_t _list, size_t _index, void **_pitem);
89 /* Retrieve first element of _list */
90 int mu_list_head (mu_list_t _list, void **_pitem);
91 /* Retrieve last element of _list */
92 int mu_list_tail (mu_list_t _list, void **_pitem);
93
89 /* Store at most _count first elements from _list in the _array. Unless 94 /* Store at most _count first elements from _list in the _array. Unless
90 _pcount is NULL, fill it with the actual number of elements copied. */ 95 _pcount is NULL, fill it with the actual number of elements copied. */
91 int mu_list_to_array (mu_list_t _list, void **_array, size_t _count, 96 int mu_list_to_array (mu_list_t _list, void **_array, size_t _count,
......
...@@ -29,6 +29,7 @@ liblist_la_SOURCES = \ ...@@ -29,6 +29,7 @@ liblist_la_SOURCES = \
29 get.c\ 29 get.c\
30 getcomp.c\ 30 getcomp.c\
31 gmap.c\ 31 gmap.c\
32 head.c\
32 insert.c\ 33 insert.c\
33 intersect.c\ 34 intersect.c\
34 iterator.c\ 35 iterator.c\
...@@ -41,7 +42,8 @@ liblist_la_SOURCES = \ ...@@ -41,7 +42,8 @@ liblist_la_SOURCES = \
41 setcomp.c\ 42 setcomp.c\
42 setdestr.c\ 43 setdestr.c\
43 slice.c\ 44 slice.c\
44 slice2.c 45 slice2.c\
46 tail.c
45 47
46 INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils 48 INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils
47 49
......
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2011 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 3 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General
15 Public License along with this library. If not, see
16 <http://www.gnu.org/licenses/>. */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21 #include <stdlib.h>
22 #include <mailutils/sys/list.h>
23 #include <mailutils/errno.h>
24
25 int
26 mu_list_head (mu_list_t list, void **pitem)
27 {
28 if (list == NULL)
29 return EINVAL;
30 if (pitem == NULL)
31 return MU_ERR_OUT_PTR_NULL;
32 if (!list->head.next)
33 return MU_ERR_NOENT;
34 *pitem = list->head.next->item;
35 return 0;
36 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2011 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 3 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General
15 Public License along with this library. If not, see
16 <http://www.gnu.org/licenses/>. */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21 #include <stdlib.h>
22 #include <mailutils/sys/list.h>
23 #include <mailutils/errno.h>
24
25 int
26 mu_list_tail (mu_list_t list, void **pitem)
27 {
28 if (list == NULL)
29 return EINVAL;
30 if (pitem == NULL)
31 return MU_ERR_OUT_PTR_NULL;
32 if (!list->head.prev)
33 return MU_ERR_NOENT;
34 *pitem = list->head.prev->item;
35 return 0;
36 }
...@@ -103,6 +103,18 @@ TESTLIST([get],[], ...@@ -103,6 +103,18 @@ TESTLIST([get],[],
103 [fire 103 [fire
104 ]) 104 ])
105 105
106 TESTLIST([head],[],
107 [add en to tre fire fem
108 head],
109 [en
110 ])
111
112 TESTLIST([tail],[],
113 [add en to tre fire fem
114 tail],
115 [fem
116 ])
117
106 118
107 # ------------------------------------------------------------ 119 # ------------------------------------------------------------
108 # Iterators 120 # Iterators
......
...@@ -685,6 +685,42 @@ slice (mu_list_t *plist, int argc, char **argv) ...@@ -685,6 +685,42 @@ slice (mu_list_t *plist, int argc, char **argv)
685 } 685 }
686 686
687 void 687 void
688 head (size_t argc, mu_list_t list)
689 {
690 int rc;
691 const char *text;
692
693 if (argc != 1)
694 {
695 fprintf (stderr, "head ?\n");
696 return;
697 }
698 rc = mu_list_head (list, (void**) &text);
699 if (rc)
700 mu_diag_funcall (MU_DIAG_ERROR, "mu_list_head", NULL, rc);
701 else
702 printf ("%s\n", text);
703 }
704
705 void
706 tail (size_t argc, mu_list_t list)
707 {
708 int rc;
709 const char *text;
710
711 if (argc != 1)
712 {
713 fprintf (stderr, "head ?\n");
714 return;
715 }
716 rc = mu_list_tail (list, (void**) &text);
717 if (rc)
718 mu_diag_funcall (MU_DIAG_ERROR, "mu_list_tail", NULL, rc);
719 else
720 printf ("%s\n", text);
721 }
722
723 void
688 help () 724 help ()
689 { 725 {
690 printf ("count\n"); 726 printf ("count\n");
...@@ -708,6 +744,8 @@ help () ...@@ -708,6 +744,8 @@ help ()
708 printf ("quit\n"); 744 printf ("quit\n");
709 printf ("iter num\n"); 745 printf ("iter num\n");
710 printf ("help\n"); 746 printf ("help\n");
747 printf ("head\n");
748 printf ("tail\n");
711 printf ("NUMBER\n"); 749 printf ("NUMBER\n");
712 } 750 }
713 751
...@@ -819,6 +857,10 @@ shell (mu_list_t list) ...@@ -819,6 +857,10 @@ shell (mu_list_t list)
819 find (itr[num], ws.ws_wordv[1]); 857 find (itr[num], ws.ws_wordv[1]);
820 else if (strcmp (ws.ws_wordv[0], "help") == 0) 858 else if (strcmp (ws.ws_wordv[0], "help") == 0)
821 help (); 859 help ();
860 else if (strcmp (ws.ws_wordv[0], "head") == 0)
861 head (ws.ws_wordc, list);
862 else if (strcmp (ws.ws_wordv[0], "tail") == 0)
863 tail (ws.ws_wordc, list);
822 else if (ws.ws_wordc == 1) 864 else if (ws.ws_wordc == 1)
823 { 865 {
824 char *p; 866 char *p;
......