]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/sellist.c
First cut selection list
[bacula/bacula] / bacula / src / lib / sellist.c
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 #include "bacula.h"
37 #include "sellist.h"
38
39 /*
40  * Returns next item
41  *   error if returns -1 and errmsg set
42  *   end of items if returns -1 and errmsg NULL
43  */
44 int64_t sellist::next()
45 {
46    if (e == NULL) {
47       goto bail_out;
48    }
49    errmsg = NULL;
50    if (beg <= end) {
51       return beg++;
52    }
53    /*
54     * As we walk the list, we set EOF in
55     *   the end of the next item to ease scanning,
56     *   but save and then restore the character.
57     */
58    for (p=e; p && *p; p=e) {
59       /* Check for list */
60       e = strchr(p, ',');
61       if (e) {
62          esave = *e;
63          *e++ = 0;
64       } else {
65          esave = 0;
66       }
67       /* Check for range */
68       h = strchr(p, '-');             /* range? */
69       if (h == p) {
70          errmsg = _("Negative numbers not permitted.\n");
71          goto bail_out;
72       }
73       if (h) {
74          hsave = *h;
75          *h++ = 0;
76          if (!is_an_integer(h)) {
77             errmsg = _("Range end is not integer.\n");
78             goto bail_out;
79          }
80          skip_spaces(&p);
81          if (!is_an_integer(p)) {
82             errmsg = _("Range start is not an integer.\n");
83             goto bail_out;
84          }
85          beg = str_to_int64(p);
86          end = str_to_int64(h);
87          if (end < beg) {
88             errmsg = _("Range end not bigger than start.\n");
89             goto bail_out;
90          }
91       } else {
92          hsave = 0;
93          skip_spaces(&p);
94          if (!is_an_integer(p)) {
95             errmsg = _("Input value is not an integer.\n");
96             goto bail_out;
97          }
98          beg = end = str_to_int64(p);
99       }
100       if (esave) {
101          *(e-1) = esave;
102       }
103       if (hsave) {
104          *(h-1) = hsave;
105       }
106       if (beg <= 0 || end <= 0) {
107          errmsg = _("Selection items must be be greater than zero.\n");
108          goto bail_out;
109       }
110       if (end > max) {
111          errmsg = _("Selection item too large.\n");
112          goto bail_out;
113       }
114       if (beg <= end) {
115          return beg++;
116       }
117    }
118    /* End of items */
119    errmsg = NULL;      /* No error */
120    return -1;
121
122 bail_out:
123    return -1;          /* Error, errmsg set */
124 }
125
126
127 /*
128  * Set selection string and optionally scan it
129  *   returns false on error in string
130  *   returns true if OK
131  */
132 bool sellist::set_string(char *string, bool scan=true)
133 {
134    /*
135     * Copy string, because we write into it,
136     *  then scan through it once to find any
137     *  errors.
138     */
139    if (str) {
140       free(str);
141    }
142    e = str = bstrdup(string);
143    end = 0;
144    beg = 1;
145    num_items = 0;
146    if (scan) {
147       while (next() >= 0) {
148          num_items++;
149       }
150       if (get_errmsg()) {
151          return false;
152       }
153       e = str;
154       end = 0;
155       beg = 1;
156    }
157    return true;
158 }
159
160 #ifdef TEST_PROGRAM
161 int main(int argc, char **argv, char **env)
162 {
163    char *msg;
164    sellist sl;
165    int i;
166
167    if (!argv[1]) {
168       msg = _("No input string given.\n");
169       goto bail_out;
170    }
171    Dmsg1(000, "argv[1]=%s\n", argv[1]);
172
173    strip_trailing_junk(argv[1]);
174    sl.set_string(argv[1]);
175    if ((msg = sl.get_errmsg())) {
176       goto bail_out;
177    }
178    while ((i=sl.next()) >= 0) {
179       Dmsg1(000, "%d\n", i);
180    }
181    if ((msg = sl.get_errmsg())) {
182       goto bail_out;
183    }
184    printf("\nPass 2\n");
185    sl.set_string(argv[1]);
186    while ((i=sl.next()) >= 0) {
187       Dmsg1(000, "%d\n", i);
188    }
189    if ((msg = sl.get_errmsg())) {
190       goto bail_out;
191    }
192    return 0;
193
194 bail_out:
195    Dmsg1(000, "Error: %s\n", msg);
196    return 1;
197
198 }
199 #endif /* TEST_PROGRAM */