]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/sellist.h
c9716a0eea7a99fc610a2f4768a85d6227ecb9dc
[bacula/bacula] / bacula / src / lib / sellist.h
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2011-2014 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from many
7    others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    Bacula® is a registered trademark of Kern Sibbald.
15 */
16 /*
17  *  Kern Sibbald, January  MMXII
18  *
19  *  Selection list. A string of integers separated by commas
20  *   representing items selected. Ranges of the form nn-mm
21  *   are also permitted.
22  */
23
24 #ifndef __SELLIST_H_
25 #define __SELLIST_H_
26
27 /*
28  * Loop var through each member of list
29  */
30 #define foreach_sellist(var, list) \
31         for((var)=(list)->first(); (var)>=0; (var)=(list)->next() )
32
33
34 class sellist : public SMARTALLOC {
35    const char *errmsg;
36    char *p, *e, *h;
37    char esave, hsave;
38    bool all;
39    int64_t beg, end;
40    int num_items;
41    char *str;
42    char *expanded;
43 public:
44    sellist();
45    ~sellist();
46    bool set_string(char *string, bool scan);
47    bool is_all() { return all; };
48    int64_t first();
49    int64_t next();
50    void begin();
51    /* size() valid only if scan enabled on string */
52    int size() const { return num_items; };
53    char *get_list() { return str; };
54    /* get the list of all jobids */
55    char *get_expanded_list();
56    /* if errmsg == NULL, no error */
57    const char *get_errmsg() { return errmsg; };
58 };
59
60 /*
61  * Initialize the list structure
62  */
63 inline sellist::sellist()
64 {
65    num_items = 0;
66    expanded = NULL;
67    str = NULL;
68    e = NULL;
69    errmsg = NULL;
70 }
71
72 /*
73  * Destroy the list
74  */
75 inline sellist::~sellist()
76 {
77    if (str) {
78       free(str);
79       str = NULL;
80    }
81    if (expanded) {
82       free(expanded);
83       expanded = NULL;
84    }
85 }
86
87 /*
88  * Returns first item
89  *   error if returns -1 and errmsg set
90  *   end of items if returns -1 and errmsg NULL
91  */
92 inline int64_t sellist::first()
93 {
94    begin();
95    return next();
96 }
97
98 /*
99  * Reset to walk list from beginning
100  */
101 inline void sellist::begin()
102 {
103    e = str;
104    end = 0;
105    beg = 1;
106    all = false;
107    errmsg = NULL;
108 }
109
110 #endif /* __SELLIST_H_ */