1/* $XConsortium: mlist.c,v 2.10 91/01/06 21:08:51 rws Exp $" */
2
3/*
4 *			  COPYRIGHT 1987
5 *		   DIGITAL EQUIPMENT CORPORATION
6 *		       MAYNARD, MASSACHUSETTS
7 *			ALL RIGHTS RESERVED.
8 *
9 * THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE AND
10 * SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT CORPORATION.
11 * DIGITAL MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE FOR
12 * ANY PURPOSE.  IT IS SUPPLIED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
13 *
14 * IF THE SOFTWARE IS MODIFIED IN A MANNER CREATING DERIVATIVE COPYRIGHT
15 * RIGHTS, APPROPRIATE LEGENDS MAY BE PLACED ON THE DERIVATIVE WORK IN
16 * ADDITION TO THAT SET FORTH ABOVE.
17 *
18 *
19 * Permission to use, copy, modify, and distribute this software and its
20 * documentation for any purpose and without fee is hereby granted, provided
21 * that the above copyright notice appear in all copies and that both that
22 * copyright notice and this permission notice appear in supporting
23 * documentation, and that the name of Digital Equipment Corporation not be
24 * used in advertising or publicity pertaining to distribution of the software
25 * without specific, written prior permission.
26 */
27/* $XFree86$ */
28
29/* mlist.c -- functions to deal with message lists. */
30
31#include "xmh.h"
32
33
34/* Create a message list containing no messages. */
35
36MsgList MakeNullMsgList(void)
37{
38    MsgList mlist;
39    mlist = XtNew(MsgListRec);
40    mlist->nummsgs = 0;
41    mlist->msglist = XtNew(Msg);
42    mlist->msglist[0] = NULL;
43    return mlist;
44}
45
46
47/* Append a message to the given message list. */
48
49void AppendMsgList(MsgList mlist, Msg msg)
50{
51    mlist->nummsgs++;
52    mlist->msglist = XtReallocArray(mlist->msglist,
53			  (unsigned) (mlist->nummsgs + 1), sizeof(Msg));
54    mlist->msglist[mlist->nummsgs - 1] = msg;
55    mlist->msglist[mlist->nummsgs] = NULL;
56}
57
58
59
60/* Delete a message from a message list. */
61
62void DeleteMsgFromMsgList(MsgList mlist, Msg msg)
63{
64    int i;
65    for (i=0 ; i<mlist->nummsgs ; i++) {
66	if (mlist->msglist[i] == msg) {
67	    mlist->nummsgs--;
68	    for (; i<mlist->nummsgs ; i++)
69		mlist->msglist[i] = mlist->msglist[i+1];
70	    return;
71	}
72    }
73}
74
75
76
77/* Create a new messages list containing only the one given message. */
78
79MsgList MakeSingleMsgList(Msg msg)
80{
81    MsgList result;
82    result = MakeNullMsgList();
83    AppendMsgList(result, msg);
84    return result;
85}
86
87
88/* We're done with this message list; free it's storage. */
89
90void FreeMsgList(MsgList mlist)
91{
92    XtFree((char *) mlist->msglist);
93    XtFree((char *) mlist);
94}
95
96
97
98/* Parse the given string into a message list.  The string contains mh-style
99   message numbers.  This routine assumes those messages numbers refer to
100   messages in the given toc. */
101
102MsgList StringToMsgList(Toc toc, char *str)
103{
104    MsgList mlist;
105    char *ptr;
106    int first, second, i;
107    Msg msg;
108    mlist = MakeNullMsgList();
109    while (*str) {
110        while (*str == ' ')
111            str++;
112        first = second = atoi(str);
113        str++;
114        for (ptr = str; *ptr >= '0' && *ptr <= '9'; ptr++) ;
115        if (*ptr == '-')
116            second = atoi(ptr + 1);
117        if (first > 0) {
118            for (i = first; i <= second; i++) {
119		msg = TocMsgFromId(toc, i);
120                if (msg) AppendMsgList(mlist, msg);
121	    }
122	}
123        str = ptr;
124    }
125    return mlist;
126}
127