]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/sellist.h
Backport from BEE
[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    int64_t max;
41    int num_items;
42    char *str;
43    char *expanded;
44 public:
45    sellist();
46    ~sellist();
47    bool set_string(char *string, bool scan);
48    bool is_all() { return all; };
49    int64_t first();
50    int64_t next();
51    void begin();
52    /* size() valid only if scan enabled on string */
53    int size() const { return num_items; };
54    char *get_list() { return str; };
55    /* get the list of all jobids */
56    char *get_expanded_list();
57    /* if errmsg == NULL, no error */
58    const char *get_errmsg() { return errmsg; };
59 };
60
61 /*
62  * Initialize the list structure
63  */
64 inline sellist::sellist()
65 {
66    num_items = 0;
67    max = 99999;
68    expanded = NULL;
69    str = NULL;
70    e = NULL;
71    errmsg = NULL;
72 }
73
74 /*
75  * Destroy the list
76  */
77 inline sellist::~sellist()
78 {
79    if (str) {
80       free(str);
81       str = NULL;
82    }
83    if (expanded) {
84       free(expanded);
85       expanded = NULL;
86    }
87 }
88
89 /*
90  * Returns first item
91  *   error if returns -1 and errmsg set
92  *   end of items if returns -1 and errmsg NULL
93  */
94 inline int64_t sellist::first()
95 {
96    begin();
97    return next();
98 }
99
100 /*
101  * Reset to walk list from beginning
102  */
103 inline void sellist::begin()
104 {
105    e = str;
106    end = 0;
107    beg = 1;
108    all = false;
109    errmsg = NULL;
110 }
111
112 #endif /* __SELLIST_H_ */