Commit 546831aa 546831aac55a8ba4099375834c8ff56c877ee275 by Sergey Poznyakoff

Implement mu_msgset_first and mu_msgset_last

* include/mailutils/msgset.h (mu_msgset_first, mu_msgset_last): New prototypes.
* libmailutils/msgset/Makefile.am: Add new files.
* libmailutils/msgset/first.c: New file.
* libmailutils/msgset/last.c: New file.
* libmailutils/tests/msgset.c: New options for testing the new functions.
* libmailutils/tests/msgset.at: Test new functions.
1 parent d69ca8dc
...@@ -34,7 +34,7 @@ struct mu_msgrange ...@@ -34,7 +34,7 @@ struct mu_msgrange
34 message. */ 34 message. */
35 #define MU_MSGNO_LAST 0 35 #define MU_MSGNO_LAST 0
36 36
37 #define MU_MSGSET_NUM 0 /* Message set operates on sequence numbers */ 37 #define MU_MSGSET_NUM 0 /* Message set operates on sequence numbers */
38 #define MU_MSGSET_UID 1 /* Message set operates on UIDs */ 38 #define MU_MSGSET_UID 1 /* Message set operates on UIDs */
39 39
40 #define MU_MSGSET_MODE_MASK 0x0f 40 #define MU_MSGSET_MODE_MASK 0x0f
...@@ -65,6 +65,9 @@ int mu_msgset_negate (mu_msgset_t msgset, mu_msgset_t *pnset); ...@@ -65,6 +65,9 @@ int mu_msgset_negate (mu_msgset_t msgset, mu_msgset_t *pnset);
65 65
66 int mu_msgset_count (mu_msgset_t mset, size_t *pcount); 66 int mu_msgset_count (mu_msgset_t mset, size_t *pcount);
67 int mu_msgset_is_empty (mu_msgset_t mset); 67 int mu_msgset_is_empty (mu_msgset_t mset);
68
69 int mu_msgset_first (mu_msgset_t msgset, size_t *ret);
70 int mu_msgset_last (mu_msgset_t msgset, size_t *ret);
68 71
69 typedef int (*mu_msgset_msgno_action_t) (size_t _n, void *_call_data); 72 typedef int (*mu_msgset_msgno_action_t) (size_t _n, void *_call_data);
70 typedef int (*mu_msgset_message_action_t) (size_t _n, mu_message_t _msg, 73 typedef int (*mu_msgset_message_action_t) (size_t _n, mu_message_t _msg,
......
...@@ -27,12 +27,14 @@ libmsgset_la_SOURCES = \ ...@@ -27,12 +27,14 @@ libmsgset_la_SOURCES = \
27 getitr.c\ 27 getitr.c\
28 getlist.c\ 28 getlist.c\
29 getmbox.c\ 29 getmbox.c\
30 first.c\
30 foreachnum.c\ 31 foreachnum.c\
31 foreachmsgno.c\ 32 foreachmsgno.c\
32 foreachuid.c\ 33 foreachuid.c\
33 foreachmsg.c\ 34 foreachmsg.c\
34 free.c\ 35 free.c\
35 isempty.c\ 36 isempty.c\
37 last.c\
36 locate.c\ 38 locate.c\
37 negate.c\ 39 negate.c\
38 parse.c\ 40 parse.c\
......
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2017 Free Software Foundation, Inc.
3
4 GNU Mailutils is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
8
9 GNU Mailutils 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
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18 #include <mailutils/types.h>
19 #include <mailutils/errno.h>
20 #include <mailutils/list.h>
21 #include <mailutils/msgset.h>
22 #include <mailutils/sys/msgset.h>
23
24 int
25 mu_msgset_first (mu_msgset_t msgset, size_t *ret)
26 {
27 int rc;
28 struct mu_msgrange const *range;
29
30 if (mu_msgset_is_empty (msgset))
31 return EINVAL;
32 if (!ret)
33 return MU_ERR_OUT_PTR_NULL;
34 rc = mu_msgset_aggregate (msgset);
35 if (rc)
36 return rc;
37 rc = mu_list_head (msgset->list, (void **) &range);
38 if (rc == 0)
39 *ret = range->msg_beg;
40 return rc;
41 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2017 Free Software Foundation, Inc.
3
4 GNU Mailutils is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
8
9 GNU Mailutils 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
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18 #include <mailutils/types.h>
19 #include <mailutils/errno.h>
20 #include <mailutils/list.h>
21 #include <mailutils/msgset.h>
22 #include <mailutils/sys/msgset.h>
23
24 int
25 mu_msgset_last (mu_msgset_t msgset, size_t *ret)
26 {
27 int rc;
28 struct mu_msgrange const *range;
29
30 if (mu_msgset_is_empty (msgset))
31 return EINVAL;
32 if (!ret)
33 return MU_ERR_OUT_PTR_NULL;
34 rc = mu_msgset_aggregate (msgset);
35 if (rc)
36 return rc;
37 rc = mu_list_tail (msgset->list, (void **) &range);
38 if (rc == 0)
39 *ret = range->msg_end;
40 return rc;
41 }
...@@ -171,5 +171,17 @@ MSGSET([subtract an open range with smaller left boundary], ...@@ -171,5 +171,17 @@ MSGSET([subtract an open range with smaller left boundary],
171 [3 171 [3
172 ]) 172 ])
173 173
174 MSGSET([first],
175 [msgset-first],
176 [-msgset='3,10:20' -first],
177 [3
178 ])
179
180 MSGSET([last],
181 [msgset-last],
182 [-msgset='3,10:20' -last],
183 [20
184 ])
185
174 dnl ------------------------------------------------------------------ 186 dnl ------------------------------------------------------------------
175 m4_popdef([MSGSET]) 187 m4_popdef([MSGSET])
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -73,13 +73,37 @@ parse_msgset (const char *arg) ...@@ -73,13 +73,37 @@ parse_msgset (const char *arg)
73 return msgset; 73 return msgset;
74 } 74 }
75 75
76 static void
77 print_all (mu_msgset_t msgset)
78 {
79 MU_ASSERT (mu_msgset_print (mu_strout, msgset));
80 mu_printf ("\n");
81 }
82
83 static void
84 print_first (mu_msgset_t msgset)
85 {
86 size_t n;
87 MU_ASSERT (mu_msgset_first (msgset, &n));
88 printf ("%zu\n", n);
89 }
90
91 static void
92 print_last (mu_msgset_t msgset)
93 {
94 size_t n;
95 MU_ASSERT (mu_msgset_last (msgset, &n));
96 printf ("%zu\n", n);
97 }
98
76 int 99 int
77 main (int argc, char **argv) 100 main (int argc, char **argv)
78 { 101 {
79 int i; 102 int i;
80 char *msgset_string = NULL; 103 char *msgset_string = NULL;
81 mu_msgset_t msgset; 104 mu_msgset_t msgset;
82 105 void (*print) (mu_msgset_t) = print_all;
106
83 mu_set_program_name (argv[0]); 107 mu_set_program_name (argv[0]);
84 for (i = 1; i < argc; i++) 108 for (i = 1; i < argc; i++)
85 { 109 {
...@@ -88,7 +112,7 @@ main (int argc, char **argv) ...@@ -88,7 +112,7 @@ main (int argc, char **argv)
88 if (strcmp (arg, "-h") == 0 || strcmp (arg, "-help") == 0) 112 if (strcmp (arg, "-h") == 0 || strcmp (arg, "-help") == 0)
89 { 113 {
90 mu_printf ("usage: %s [-msgset=SET] [-add=X[:Y]] [-del=X[:Y]] " 114 mu_printf ("usage: %s [-msgset=SET] [-add=X[:Y]] [-del=X[:Y]] "
91 "[-addset=SET] [-delset=SET] ...\n", 115 "[-addset=SET] [-delset=SET] [-first] [-last]...\n",
92 mu_program_name); 116 mu_program_name);
93 return 0; 117 return 0;
94 } 118 }
...@@ -142,14 +166,17 @@ main (int argc, char **argv) ...@@ -142,14 +166,17 @@ main (int argc, char **argv)
142 mu_msgset_free (tset); 166 mu_msgset_free (tset);
143 } 167 }
144 } 168 }
169 else if (strcmp (arg, "-first") == 0)
170 print = print_first;
171 else if (strcmp (arg, "-last") == 0)
172 print = print_last;
145 else 173 else
146 { 174 {
147 mu_error ("unknown option %s", arg); 175 mu_error ("unknown option %s", arg);
148 return 1; 176 return 1;
149 } 177 }
150 } 178 }
151 MU_ASSERT (mu_msgset_print (mu_strout, msgset)); 179 print (msgset);
152 mu_printf ("\n");
153 mu_msgset_free (msgset); 180 mu_msgset_free (msgset);
154 181
155 return 0; 182 return 0;
......