]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/sellist.h
Tweak copyrights again
[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    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    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 = errmsg = NULL;
74 }   
75
76 /*
77  * Destroy the list
78  */
79 inline sellist::~sellist() 
80 {
81    if (str) {
82       free(str);
83       str = 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    errmsg = NULL;
107 }
108