]> diplodocus.org Git - nmh/blob - sbr/folder_realloc.c
Fix test for inlineonly.
[nmh] / sbr / folder_realloc.c
1
2 /*
3 * folder_realloc.c -- realloc a folder/msgs structure
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/utils.h>
12
13 /*
14 * Reallocate some of the space in the folder
15 * structure (currently just message status array).
16 *
17 * Return pointer to new folder structure.
18 * If error, return NULL.
19 */
20
21 struct msgs *
22 folder_realloc (struct msgs *mp, int lo, int hi)
23 {
24 int msgnum;
25
26 /* sanity checks */
27 if (lo < 1)
28 adios (NULL, "BUG: called folder_realloc with lo (%d) < 1", lo);
29 if (hi < 1)
30 adios (NULL, "BUG: called folder_realloc with hi (%d) < 1", hi);
31 if (mp->nummsg > 0 && lo > mp->lowmsg)
32 adios (NULL, "BUG: called folder_realloc with lo (%d) > mp->lowmsg (%d)",
33 lo, mp->lowmsg);
34 if (mp->nummsg > 0 && hi < mp->hghmsg)
35 adios (NULL, "BUG: called folder_realloc with hi (%d) < mp->hghmsg (%d)",
36 hi, mp->hghmsg);
37
38 /* Check if we really need to reallocate anything */
39 if (lo == mp->lowoff && hi == mp->hghoff)
40 return mp;
41
42 if (lo == mp->lowoff) {
43 bvector_t *v;
44 size_t old_size = mp->num_msgstats;
45 size_t new_size = hi - lo + 1;
46 size_t i;
47
48 /*
49 * We are just extending (or shrinking) the end of message
50 * status array. So we don't have to move anything and can
51 * just realloc the message status array.
52 */
53
54 for (i = old_size, v = &mp->msgstats[old_size-1]; i > new_size;
55 --i, --v) {
56 bvector_free (*v);
57 }
58 mp->num_msgstats = MSGSTATNUM (lo, hi);
59 mp->msgstats =
60 mh_xrealloc (mp->msgstats, MSGSTATSIZE(mp));
61 for (i = old_size, v = &mp->msgstats[old_size]; i < new_size;
62 ++i, ++v) {
63 *v = bvector_create (0);
64 }
65 } else {
66 /*
67 * We are changing the offset of the message status
68 * array. So we will need to shift everything.
69 */
70 bvector_t *tmpstats, *t;
71 size_t i;
72
73 /* first allocate the new message status space */
74 mp->num_msgstats = MSGSTATNUM (lo, hi);
75 tmpstats = mh_xmalloc (MSGSTATSIZE(mp));
76 for (i = 0, t = tmpstats; i < mp->num_msgstats; ++i, ++t) {
77 *t = bvector_create (0);
78 }
79
80 /* then copy messages status array with shift */
81 if (mp->nummsg > 0) {
82 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++)
83 bvector_copy (tmpstats[msgnum - lo], msgstat (mp, msgnum));
84 }
85 free(mp->msgstats);
86 mp->msgstats = tmpstats;
87 }
88
89 mp->lowoff = lo;
90 mp->hghoff = hi;
91
92 /*
93 * Clear all the flags for entries outside
94 * the current message range for this folder.
95 */
96 if (mp->nummsg > 0) {
97 for (msgnum = mp->lowoff; msgnum < mp->lowmsg; msgnum++)
98 clear_msg_flags (mp, msgnum);
99 for (msgnum = mp->hghmsg + 1; msgnum <= mp->hghoff; msgnum++)
100 clear_msg_flags (mp, msgnum);
101 } else {
102 /* no messages, so clear entire range */
103 for (msgnum = mp->lowoff; msgnum <= mp->hghoff; msgnum++)
104 clear_msg_flags (mp, msgnum);
105 }
106
107 return mp;
108 }