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
message. */
#define MU_MSGNO_LAST 0
#define MU_MSGSET_NUM 0 /* Message set operates on sequence numbers */
#define MU_MSGSET_NUM 0 /* Message set operates on sequence numbers */
#define MU_MSGSET_UID 1 /* Message set operates on UIDs */
#define MU_MSGSET_MODE_MASK 0x0f
......@@ -65,6 +65,9 @@ int mu_msgset_negate (mu_msgset_t msgset, mu_msgset_t *pnset);
int mu_msgset_count (mu_msgset_t mset, size_t *pcount);
int mu_msgset_is_empty (mu_msgset_t mset);
int mu_msgset_first (mu_msgset_t msgset, size_t *ret);
int mu_msgset_last (mu_msgset_t msgset, size_t *ret);
typedef int (*mu_msgset_msgno_action_t) (size_t _n, void *_call_data);
typedef int (*mu_msgset_message_action_t) (size_t _n, mu_message_t _msg,
......
......@@ -27,12 +27,14 @@ libmsgset_la_SOURCES = \
getitr.c\
getlist.c\
getmbox.c\
first.c\
foreachnum.c\
foreachmsgno.c\
foreachuid.c\
foreachmsg.c\
free.c\
isempty.c\
last.c\
locate.c\
negate.c\
parse.c\
......
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2017 Free Software Foundation, Inc.
GNU Mailutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Mailutils 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
#include <mailutils/types.h>
#include <mailutils/errno.h>
#include <mailutils/list.h>
#include <mailutils/msgset.h>
#include <mailutils/sys/msgset.h>
int
mu_msgset_first (mu_msgset_t msgset, size_t *ret)
{
int rc;
struct mu_msgrange const *range;
if (mu_msgset_is_empty (msgset))
return EINVAL;
if (!ret)
return MU_ERR_OUT_PTR_NULL;
rc = mu_msgset_aggregate (msgset);
if (rc)
return rc;
rc = mu_list_head (msgset->list, (void **) &range);
if (rc == 0)
*ret = range->msg_beg;
return rc;
}
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2017 Free Software Foundation, Inc.
GNU Mailutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Mailutils 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
#include <mailutils/types.h>
#include <mailutils/errno.h>
#include <mailutils/list.h>
#include <mailutils/msgset.h>
#include <mailutils/sys/msgset.h>
int
mu_msgset_last (mu_msgset_t msgset, size_t *ret)
{
int rc;
struct mu_msgrange const *range;
if (mu_msgset_is_empty (msgset))
return EINVAL;
if (!ret)
return MU_ERR_OUT_PTR_NULL;
rc = mu_msgset_aggregate (msgset);
if (rc)
return rc;
rc = mu_list_tail (msgset->list, (void **) &range);
if (rc == 0)
*ret = range->msg_end;
return rc;
}
......@@ -171,5 +171,17 @@ MSGSET([subtract an open range with smaller left boundary],
[3
])
MSGSET([first],
[msgset-first],
[-msgset='3,10:20' -first],
[3
])
MSGSET([last],
[msgset-last],
[-msgset='3,10:20' -last],
[20
])
dnl ------------------------------------------------------------------
m4_popdef([MSGSET])
\ No newline at end of file
......
......@@ -73,13 +73,37 @@ parse_msgset (const char *arg)
return msgset;
}
static void
print_all (mu_msgset_t msgset)
{
MU_ASSERT (mu_msgset_print (mu_strout, msgset));
mu_printf ("\n");
}
static void
print_first (mu_msgset_t msgset)
{
size_t n;
MU_ASSERT (mu_msgset_first (msgset, &n));
printf ("%zu\n", n);
}
static void
print_last (mu_msgset_t msgset)
{
size_t n;
MU_ASSERT (mu_msgset_last (msgset, &n));
printf ("%zu\n", n);
}
int
main (int argc, char **argv)
{
int i;
char *msgset_string = NULL;
mu_msgset_t msgset;
void (*print) (mu_msgset_t) = print_all;
mu_set_program_name (argv[0]);
for (i = 1; i < argc; i++)
{
......@@ -88,7 +112,7 @@ main (int argc, char **argv)
if (strcmp (arg, "-h") == 0 || strcmp (arg, "-help") == 0)
{
mu_printf ("usage: %s [-msgset=SET] [-add=X[:Y]] [-del=X[:Y]] "
"[-addset=SET] [-delset=SET] ...\n",
"[-addset=SET] [-delset=SET] [-first] [-last]...\n",
mu_program_name);
return 0;
}
......@@ -142,14 +166,17 @@ main (int argc, char **argv)
mu_msgset_free (tset);
}
}
else if (strcmp (arg, "-first") == 0)
print = print_first;
else if (strcmp (arg, "-last") == 0)
print = print_last;
else
{
mu_error ("unknown option %s", arg);
return 1;
}
}
MU_ASSERT (mu_msgset_print (mu_strout, msgset));
mu_printf ("\n");
print (msgset);
mu_msgset_free (msgset);
return 0;
......