]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/sellist.h
Tweak version date
[bacula/bacula] / bacula / src / lib / sellist.h
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2011-2012 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula(R) is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  *  Kern Sibbald, January  MMXII
30  *
31  *  Selection list. A string of integers separated by commas
32  *   representing items selected. Ranges of the form nn-mm
33  *   are also permitted.
34  */
35
36 /*
37  * Loop var through each member of list
38  */
39 #define foreach_sellist(var, list) \
40         for((var)=(list)->first(); (var)>=0; (var)=(list)->next() )
41
42
43 class sellist : public SMARTALLOC {
44    const char *errmsg;
45    char *p, *e, *h;
46    char esave, hsave;
47    int64_t beg, end;
48    int64_t max;
49    int num_items;
50    char *str;
51 public:
52    sellist();
53    ~sellist();
54    bool set_string(char *string, bool scan);
55    int64_t first();
56    int64_t next();
57    void begin();
58    /* size() valid only if scan enabled on string */
59    int size() const { return num_items; };
60    char *get_list() { return str; };
61    /* if errmsg == NULL, no error */
62    const char *get_errmsg() { return errmsg; };
63 };
64
65 /*
66  * Initialize the list structure
67  */
68 inline sellist::sellist()
69 {
70    num_items = 0;
71    max = 99999;
72    str = NULL;
73    e = NULL;
74    errmsg = NULL;
75 }   
76
77 /*
78  * Destroy the list
79  */
80 inline sellist::~sellist() 
81 {
82    if (str) {
83       free(str);
84       str = NULL;
85    }
86 }
87
88 /*
89  * Returns first item
90  *   error if returns -1 and errmsg set
91  *   end of items if returns -1 and errmsg NULL
92  */
93 inline int64_t sellist::first()
94 {
95    begin();
96    return next();
97 }
98
99 /*
100  * Reset to walk list from beginning
101  */
102 inline void sellist::begin()
103 {
104    e = str;
105    end = 0;
106    beg = 1;
107    errmsg = NULL;
108 }