slice.c
2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* 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/list.h>
#include <mailutils/sys/list.h>
struct slice_info
{
size_t cur;
size_t *posv;
size_t posc;
size_t idx;
int (*dup_item) (void **, void *, void *);
void *dup_data;
int err;
};
static int
_slice_mapper (void **itmv, size_t itmc, void *call_data)
{
struct slice_info *si = call_data;
size_t cur = si->cur++;
while (1)
{
if (cur < si->posv[si->idx])
return MU_LIST_MAP_SKIP;
if (si->idx + 1 < si->posc && cur > si->posv[si->idx + 1])
{
si->idx += 2;
if (si->idx >= si->posc)
return MU_LIST_MAP_STOP|MU_LIST_MAP_SKIP;
}
else
break;
}
if (si->dup_item && itmv[0])
{
void *p;
int rc = si->dup_item (&p, itmv[0], si->dup_data);
if (rc)
{
si->err = rc;
return MU_LIST_MAP_STOP|MU_LIST_MAP_SKIP;
}
itmv[0] = p;
}
return MU_LIST_MAP_OK;
}
static int
poscmp (const void *a, const void *b)
{
size_t posa = *(size_t*)a;
size_t posb = *(size_t*)b;
if (posa < posb)
return -1;
else if (posa > posb)
return 1;
return 0;
}
int
mu_list_slice_dup (mu_list_t *pdest, mu_list_t list, size_t *posv, size_t posc,
int (*dup_item) (void **, void *, void *),
void *dup_data)
{
int rc;
struct slice_info si;
mu_list_t dest;
si.cur = 0;
si.idx = 0;
si.posv = posv;
si.posc = posc;
si.dup_item = dup_item;
si.dup_data = dup_data;
si.err = 0;
qsort (posv, posc, sizeof (posv[0]), poscmp);
rc = mu_list_map (list, _slice_mapper, &si, 1, &dest);
if (rc == 0)
{
if (si.err)
{
mu_list_destroy (&dest);
rc = si.err;
}
else
{
if (dup_item)
mu_list_set_destroy_item (dest, list->destroy_item);
*pdest = dest;
}
}
return rc;
}
int
mu_list_slice (mu_list_t *pdest, mu_list_t list, size_t *posv, size_t posc)
{
return mu_list_slice_dup (pdest, list, posv, posc, NULL, NULL);
}