source: messagelist.c @ 5df4b9a

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 5df4b9a was 0c8ab5e, checked in by Alejandro R. Sedeño <asedeno@mit.edu>, 18 years ago
Binary search to find a message with a specific id.
  • Property mode set to 100644
File size: 2.3 KB
Line 
1#include "owl.h"
2#include <stdlib.h>
3#include <string.h>
4
5static const char fileIdent[] = "$Id$";
6
7int owl_messagelist_create(owl_messagelist *ml)
8{
9  owl_list_create(&(ml->list));
10  return(0);
11}
12
13int owl_messagelist_get_size(owl_messagelist *ml)
14{
15  return(owl_list_get_size(&(ml->list)));
16}
17
18void *owl_messagelist_get_element(owl_messagelist *ml, int n)
19{
20  return(owl_list_get_element(&(ml->list), n));
21}
22
23owl_message *owl_messagelist_get_by_id(owl_messagelist *ml, int target_id)
24{
25  /* return the message with id == 'id'.  If it doesn't exist return NULL. */
26  int first, last, mid, msg_id;
27  owl_message *m;
28
29  first = 0;
30  last = owl_list_get_size(&(ml->list)) - 1;
31  while (first <= last) {
32    mid = (first + last) / 2;
33    m = owl_list_get_element(&(ml->list), mid);
34    msg_id = owl_message_get_id(m);
35    if (msg_id == target_id) {
36      return(m);
37    } else if (msg_id < target_id) {
38      first = mid + 1;
39    } else {
40      last = mid - 1;
41    }
42  }
43  return(NULL);
44}
45
46int owl_messagelist_append_element(owl_messagelist *ml, void *element)
47{
48  return(owl_list_append_element(&(ml->list), element));
49}
50
51/* do we really still want this? */
52int owl_messagelist_delete_element(owl_messagelist *ml, int n)
53{
54  /* mark a message as deleted */
55  owl_message_mark_delete(owl_list_get_element(&(ml->list), n));
56  return(0);
57}
58
59int owl_messagelist_undelete_element(owl_messagelist *ml, int n)
60{
61  /* mark a message as deleted */
62  owl_message_unmark_delete(owl_list_get_element(&(ml->list), n));
63  return(0);
64}
65
66int owl_messagelist_expunge(owl_messagelist *ml)
67{
68  /* expunge deleted messages */
69  int i, j;
70  owl_list newlist;
71  owl_message *m;
72
73  owl_list_create(&newlist);
74  /*create a new list without messages marked as deleted */
75  j=owl_list_get_size(&(ml->list));
76  for (i=0; i<j; i++) {
77    m=owl_list_get_element(&(ml->list), i);
78    if (owl_message_is_delete(m)) {
79      owl_message_free(m);
80    } else {
81      owl_list_append_element(&newlist, m);
82    }
83  }
84
85  /* free the old list */
86  owl_list_free_simple(&(ml->list));
87
88  /* copy the new list to the old list */
89  memcpy(&(ml->list), &newlist, sizeof(owl_list));
90
91  return(0);
92}
93
94void owl_messagelist_invalidate_formats(owl_messagelist *ml)
95{
96  int i, j;
97  owl_message *m;
98
99  j=owl_list_get_size(&(ml->list));
100  for (i=0; i<j; i++) {
101    m=owl_list_get_element(&(ml->list), i);
102    owl_message_invalidate_format(m);
103  }
104}
Note: See TracBrowser for help on using the repository browser.