MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
parse_mask.hpp
1 /*
2  Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #include <util/BaseString.hpp>
19 
28 template <typename T>
29 static inline int parse_mask(const char * src, T& mask)
30 {
31  int cnt = 0;
32  BaseString tmp(src);
33  Vector<BaseString> list;
34  tmp.split(list, ",");
35  for (unsigned i = 0; i<list.size(); i++)
36  {
37  list[i].trim();
38  if (list[i].empty())
39  continue;
40  char * delim = (char*)strchr(list[i].c_str(), '-');
41  unsigned first = 0;
42  unsigned last = 0;
43  if (delim == 0)
44  {
45  int res = sscanf(list[i].c_str(), "%u", &first);
46  if (res != 1)
47  {
48  return -1;
49  }
50  last = first;
51  }
52  else
53  {
54  * delim = 0;
55  delim++;
56  int res0 = sscanf(list[i].c_str(), "%u", &first);
57  if (res0 != 1)
58  {
59  return -1;
60  }
61  int res1 = sscanf(delim, "%u", &last);
62  if (res1 != 1)
63  {
64  return -1;
65  }
66  if (first > last)
67  {
68  unsigned tmp = first;
69  first = last;
70  last = tmp;
71  }
72  }
73 
74  for (unsigned j = first; j<(last+1); j++)
75  {
76  if (j > mask.max_size())
77  return -2;
78 
79  cnt++;
80  mask.set(j);
81  }
82  }
83  return cnt;
84 }
85 
86