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