]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/sellist.h
First cut selection list
[bacula/bacula] / bacula / src / lib / sellist.h
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2011-2011 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    int size() const;
58    char *get_errmsg();
59 };
60
61 /*
62  * Initialize the list structure
63  */
64 inline sellist::sellist()
65 {
66    num_items = 0;
67    max = 99999;
68    str = NULL;
69    e = 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 }
82
83 /*
84  * Return list size, but only if scan enabled on string
85  */
86 inline int sellist::size() const
87 {
88    return num_items;
89 }
90
91 /*
92  * Return error message or NULL if none
93  */
94 inline char *sellist::get_errmsg()
95 {
96    return errmsg;
97 }
98
99 /*
100  * Returns first item
101  *   error if returns -1 and errmsg set
102  *   end of items if returns -1 and errmsg NULL
103  */
104 inline int64_t sellist::first()
105 {
106    e = str;
107    end = 0;
108    beg = 1;
109    errmsg = NULL;
110    return next();
111 }