]> Pileus Git - ~andy/fetchmail/blob - uid_db.h
Update <sq> Albanian translation to fetchmail-6.4.3-rc2
[~andy/fetchmail] / uid_db.h
1 /*
2   POP3 UID database
3
4         Copyright (c) 2010 MAD Partners, Ltd. (rweikusat@mssgmbh.com)
5
6         This file is being published in accordance with the GPLv2 terms
7         contained in the COPYING file being part of the fetchmail
8         6.3.17 release, including the OpenSSL exemption.
9 */
10 #ifndef fetchmail_uid_db_h
11 #define fetchmail_uid_db_h
12
13 /* If ffs() is missing on, for instance, Solaris, make sure that config.h or fetchmail.h
14  * contains #define __EXTENSIONS__ - autoconf should set it in config.h. */
15 #include "config.h"
16
17 /*  includes */
18 #include <stddef.h>
19
20 /*  types */
21 struct uid_db_record {
22     char *id;
23     size_t id_len;
24
25     /*
26       num       -       message number assigned by server
27       status    -       message status (eg seen, deleted, ...)
28       pos       -       position in record list
29     */
30     unsigned num, status, pos;
31
32     struct uid_db_record *next;
33 };
34
35 struct num_ndx {
36     /*
37       Used to find uid records by message number.
38
39       pos_0_value       -       highest message number
40       end_value         -       lowest known message number
41
42       Grows downwards because the fastuidl-code may record
43       message numbers in non-ascending order but the
44       lookup array should ideally only be large enough to
45       store pointers to interesting ('new') messages.
46     */
47     struct uid_db_record **records;
48     unsigned pos_0_value, end_value;
49 };
50
51 struct uid_db
52 {
53     struct pat_node *pat_root;
54
55     struct uid_db_record **records;
56     unsigned records_max, records_next;
57
58     struct num_ndx num_ndx;
59 };
60
61 typedef int uid_db_traversal_routine(struct uid_db_record *, void *);
62
63 /*  routines */
64 /**  initialization/ finalization */
65 void init_uid_db(struct uid_db *db);
66
67 void free_uid_db(struct uid_db *db);
68
69 static inline void clear_uid_db(struct uid_db *db)
70 {
71     free_uid_db(db);
72     init_uid_db(db);
73 }
74
75 /**  message number index handling */
76 static inline unsigned uid_db_num_ofs(struct num_ndx const *num_ndx, unsigned num)
77 {
78     return num_ndx->pos_0_value - num;
79 }
80
81 void set_uid_db_num(struct uid_db *db, struct uid_db_record *rec,
82                     unsigned num);
83
84 static inline void set_uid_db_num_pos_0(struct uid_db *db, unsigned num)
85 {
86     db->num_ndx.pos_0_value = num;
87     db->num_ndx.end_value = num + 1;
88 }
89
90 void reset_uid_db_nums(struct uid_db *db);
91
92 /**  various uidl db manipulatiors */
93 struct uid_db_record *uid_db_insert(struct uid_db *db,
94                                     char const *id, unsigned status);
95
96 void swap_uid_db_data(struct uid_db *db_0, struct uid_db *db_1);
97
98 /**  search routines */
99 struct uid_db_record *find_uid_by_id(struct uid_db *db, char const *id);
100
101 static inline struct uid_db_record *
102 find_uid_by_num(struct uid_db *db, unsigned num)
103 {
104     struct num_ndx *num_ndx;
105
106     num_ndx = &db->num_ndx;
107     return num >= num_ndx->end_value ?
108         num_ndx->records[uid_db_num_ofs(num_ndx, num)] : NULL;
109 }
110
111 static inline struct uid_db_record *
112 find_uid_by_pos(struct uid_db *db, unsigned pos)
113 {
114     return pos < db->records_next ? db->records[pos] : NULL;
115 }
116
117 static inline struct uid_db_record *
118 first_uid_in_db(struct uid_db *db, char const *id)
119 {
120     return find_uid_by_id(db, id);
121 }
122
123 struct uid_db_record *last_uid_in_db(struct uid_db *db, char const *id);
124
125 /**  various accessors */
126 static inline unsigned uid_db_n_records(struct uid_db const *db)
127 {
128     return db->records_next;
129 }
130
131 /*
132   Traverses the struct uid_db records array in insert order,
133   invoking the subroutine pointed to by r with a pointer to
134   each record and the arg pointer as arguments. If the return
135   value of that is non-zero, traverse_uid_db immediately returns
136   with this value. Otherwise, zero is returned after the last
137   record was visited.
138
139   The uid_db_traversal_routine must not modify the uid_db during
140   traversal.
141 */
142 int traverse_uid_db(struct uid_db *db,
143                     uid_db_traversal_routine *r, void *arg);
144
145 #endif