1 /*
2    Bacula(R) - The Network Backup Solution
3 
4    Copyright (C) 2000-2020 Kern Sibbald
5 
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many 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    This notice must be preserved when any source code is
15    conveyed and/or propagated.
16 
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*
20  *  Kern Sibbald, January  MMXII
21  *
22  *  Selection list. A string of integers separated by commas
23  *   representing items selected. Ranges of the form nn-mm
24  *   are also permitted.
25  */
26 
27 #ifndef __SELLIST_H_
28 #define __SELLIST_H_
29 
30 /*
31  * Loop var through each member of list
32  */
33 #define foreach_sellist(var, list) \
34         for((var)=(list)->first(); (var)>=0; (var)=(list)->next() )
35 
36 
37 class sellist : public SMARTALLOC {
38    const char *errmsg;
39    char *p, *e, *h;
40    char esave, hsave;
41    bool all;
42    int64_t beg, end;
43    int num_items;
44    char *str;
45    char *expanded;
46 public:
47    sellist();
48    ~sellist();
49    bool set_string(const char *string, bool scan);
is_all()50    bool is_all() { return all; };
51    int64_t first();
52    int64_t next();
53    void begin();
54    /* size() valid only if scan enabled on string */
size()55    int size() const { return num_items; };
get_list()56    char *get_list() { return str; };
57    /* get the list of all jobids */
58    char *get_expanded_list();
59    /* if errmsg == NULL, no error */
get_errmsg()60    const char *get_errmsg() { return errmsg; };
61 };
62 
63 /*
64  * Initialize the list structure
65  */
sellist()66 inline sellist::sellist()
67 {
68    num_items = 0;
69    expanded = NULL;
70    str = NULL;
71    e = NULL;
72    errmsg = NULL;
73 }
74 
75 /*
76  * Destroy the list
77  */
~sellist()78 inline sellist::~sellist()
79 {
80    if (str) {
81       free(str);
82       str = NULL;
83    }
84    if (expanded) {
85       free(expanded);
86       expanded = NULL;
87    }
88 }
89 
90 /*
91  * Returns first item
92  *   error if returns -1 and errmsg set
93  *   end of items if returns -1 and errmsg NULL
94  */
first()95 inline int64_t sellist::first()
96 {
97    begin();
98    return next();
99 }
100 
101 /*
102  * Reset to walk list from beginning
103  */
begin()104 inline void sellist::begin()
105 {
106    e = str;
107    end = 0;
108    beg = 1;
109    all = false;
110    errmsg = NULL;
111 }
112 
113 #endif /* __SELLIST_H_ */
114