dthelp: Change to ANSI function definitions
[oweals/cde.git] / cde / programs / dtmail / dtmail / MsgHndArray.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*
24  *+SNOTICE
25  *
26  *      $TOG: MsgHndArray.C /main/6 1998/09/02 15:54:28 mgreess $
27  *
28  *      RESTRICTED CONFIDENTIAL INFORMATION:
29  *      
30  *      The information in this document is subject to special
31  *      restrictions in a confidential disclosure agreement between
32  *      HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
33  *      document outside HP, IBM, Sun, USL, SCO, or Univel without
34  *      Sun's specific written approval.  This document and all copies
35  *      and derivative works thereof must be returned or destroyed at
36  *      Sun's request.
37  *
38  *      Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
39  *
40  *+ENOTICE
41  */
42
43 #include "MsgHndArray.hh"
44 #include "MemUtils.hh"
45
46 MsgStruct*
47 MsgHndArray::at(int indx)
48 {
49     return(_contents[indx]);
50 }
51
52 void
53 MsgHndArray::clear()
54 {
55     memset(_contents, 0, sizeof(MsgStruct *)*_size);
56 }
57
58 int
59 MsgHndArray::length()
60 {
61     return(_length);
62 }
63
64
65 int
66 MsgHndArray::indexof(MsgStruct *a_msg_struct)
67 {
68     int tmp;
69
70     for (tmp = 0; tmp < _length; tmp++)
71       {
72           if ((_contents[tmp]->sessionNumber == a_msg_struct->sessionNumber) &&
73               (_contents[tmp]->message_handle == a_msg_struct->message_handle))
74             {
75                 return(tmp);
76             }
77       }
78     return(-1);
79 }
80
81 int
82 MsgHndArray::indexof(DtMailMessageHandle a_message_handle)
83 {
84     int tmp;
85
86     for (tmp = 0; tmp < _length; tmp++)
87       {
88           if (_contents[tmp]->message_handle == a_message_handle)
89             {
90                 return(tmp);
91             }
92       }
93     return(-1);
94 }
95     
96 void
97 MsgHndArray::remove_entry(MsgStruct *ms)
98 {
99     for (int i=0; i<_length; i++)
100     {
101         if (ms == _contents[i]) 
102         {
103             remove_entry(i);
104             return;
105         }
106     }
107 }
108
109
110 void
111 MsgHndArray::remove_entry(int position)
112 {
113     int i;
114     FORCE_SEGV_DECL(MsgStruct, tmpMS);
115
116     if ((position < 0) || (position >= _length)) return;
117
118     tmpMS = _contents[position];
119
120     for (i=position; i<(_length-1); i++)
121       _contents[i] = _contents[i+1];
122
123     _contents[_length] = NULL;
124     _length -= 1;
125     
126 }
127
128
129 int
130 MsgHndArray::insert(
131     MsgStruct *a_msg_struct
132 )
133 {
134     int i, j, orig_size, return_val;
135     int sess_num;
136     Boolean found;
137     FORCE_SEGV_DECL(MsgStruct, tmpMS1);
138     FORCE_SEGV_DECL(MsgStruct, tmpMS2);
139
140     found = FALSE;
141     sess_num = a_msg_struct->sessionNumber;
142
143     for(i = 0; i < _length; i++) {
144         tmpMS1 = _contents[i];
145         if (tmpMS1->sessionNumber > sess_num) {
146             _contents[i] = a_msg_struct;
147             return_val = i;
148             found = TRUE;
149             break;
150         }
151     }
152     if (found == TRUE) {
153         _length++;
154
155     // If we hit size, then grow by 25% to allow more entries.
156     // Zero out the added portion.
157
158         if (_length == _size) {
159             orig_size = _size;
160             _size += (_size >> 2);
161             _contents = (MsgStruct **)realloc(_contents, 
162                                               _size * sizeof(MsgStruct *));
163             memset(_contents+orig_size, 0, sizeof(MsgStruct *)*_size);
164         }
165
166         for (j = i + 1; j < _length; j++) {
167             tmpMS2 = _contents[j];
168             _contents[j] = tmpMS1;
169             tmpMS1 = tmpMS2;
170         }
171     }
172      else {
173          _contents[_length] = a_msg_struct;
174          return_val = _length;
175          _length++;
176      }
177     return(return_val);
178
179 }
180
181 void
182 MsgHndArray::append(
183     MsgStruct *a_msg_struct
184 )
185
186 {
187
188     _contents[_length] = a_msg_struct;
189     _length++;
190
191     // If we hit size, then grow by 25% to allow more entries.
192
193     if (_length == _size) {
194         _size += (_size >> 2);
195         _contents = (MsgStruct **)realloc(_contents, 
196                                          _size * sizeof(MsgStruct *));
197     }
198
199 }
200
201 void
202 MsgHndArray::mark_for_delete(int indx)
203 {
204     _contents[indx]->is_deleted = TRUE;
205 }
206
207 void
208 MsgHndArray::compact(
209     int start_pos
210 )
211 {
212     FORCE_SEGV_DECL(MsgStruct, tmpMS);
213     int i;
214
215     if ((_length <= 0) || (start_pos < 0) || (start_pos >= _length))
216         return;
217
218     for (i = _length - 1; i >= start_pos; i--) {
219         tmpMS = _contents[i];
220         if (tmpMS->is_deleted) {
221             tmpMS->is_deleted = FALSE;
222             remove_entry(i);
223             compact(i);
224         }
225     }
226 }
227
228     
229 //
230 // Replace the MsgStruct at 'position' with the passed MsgStruct.
231 // The MsgStruct previously located at 'position' is *not* destroyed.
232 //
233 void
234 MsgHndArray::replace(
235         int position,
236         MsgStruct *a_msg_struct
237 )
238 {
239     if (position < 0 || position >= _length) {
240         return;
241     }
242
243     _contents[position] = a_msg_struct;
244
245     return;
246 }