First commit
[anna.git] / source / core / util / MultiRangeExpression.cpp
1 // ANNA - Anna is Not 'N' Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 //     * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //     * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 //     * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 // Local
38 #include <anna/core/util/MultiRangeExpression.hpp>
39
40 #include <anna/core/util/Tokenizer.hpp>
41 #include <anna/core/functions.hpp>
42
43 // Standard
44 #include <limits.h>
45
46
47 //using namespace anna;
48
49
50 // private
51
52 //------------------------------------------------------------------------------
53 //---------------------------------------------- MultiRangeExpression::refresh()
54 //------------------------------------------------------------------------------
55 void anna::MultiRangeExpression::refresh(void) throw() {
56   anna::Tokenizer ranges, borders;
57   anna::Tokenizer::const_iterator ranges_it, borders_it;
58   std::string range;
59   unsigned int min, max;
60   a_data.clear();
61
62   if(a_literal == "") return;
63
64   ranges.apply(a_literal, ",");
65
66   for(ranges_it = ranges.begin(); ranges_it != ranges.end(); ranges_it ++) {
67     range = anna::Tokenizer::data(ranges_it);
68     borders.apply(range, "-");
69     borders_it = borders.begin();
70
71     if(borders_it != borders.end()) {
72       min = atoi(anna::Tokenizer::data(borders_it));
73       max = min;
74       borders_it++;
75
76       if(borders_it != borders.end()) {
77         max = atoi(anna::Tokenizer::data(borders_it));
78       }
79
80       // Update a_data:
81       for(register unsigned int k = min; k <= max; k++) {
82         a_data[k] = 0;
83
84         if(k == UINT_MAX/* overflow */) break;
85       }
86     }
87   }
88 }
89
90
91 //------------------------------------------------------------------------------
92 //----------------------------------- MultiRangeExpression::getExpandedLiteral()
93 //------------------------------------------------------------------------------
94 std::string anna::MultiRangeExpression::getExpandedLiteral(void) const throw() {
95   std::string result;
96   std::map < unsigned int, int/*dummy*/ >::const_iterator it;
97   std::map < unsigned int, int/*dummy*/ >::const_iterator it_min(a_data.begin());
98   std::map < unsigned int, int/*dummy*/ >::const_iterator it_max(a_data.end());
99
100   for(it = it_min; it != it_max; it++) {
101     result += anna::functions::asString((*it).first);
102     result += ",";
103   }
104
105   int pos = result.size();
106
107   if(pos) result.erase(pos - 1);
108
109   return (result);
110 }
111