Salome HOME
1st step.
[modules/med.git] / src / MEDCalculator / MEDCalculatorDBRangeSelection.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library 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 GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (CEA/DEN)
20
21 #include "MEDCalculatorDBRangeSelection.hxx"
22
23 #include <string>
24 #include <limits>
25 #include <sstream>
26
27 using namespace MEDCoupling;
28
29 const char MEDCalculatorDBRangeSelection::ALL_ELTS[]=":";
30
31 MEDCalculatorDBRangeSelection::MEDCalculatorDBRangeSelection(const char *v) throw(INTERP_KERNEL::Exception)
32 try
33   {
34     setValue(v);
35   }
36  catch(INTERP_KERNEL::Exception& e)
37    {
38      throw e;
39    }
40
41 MEDCalculatorDBRangeSelection::MEDCalculatorDBRangeSelection(int v)
42 {
43   setValue(v);
44 }
45
46 MEDCalculatorDBRangeSelection::MEDCalculatorDBRangeSelection()
47 {
48   setValue(ALL_ELTS);
49 }
50
51 void MEDCalculatorDBRangeSelection::setPyStart(int val)
52 {
53   _start=TraducePyVal(val);
54 }
55
56 void MEDCalculatorDBRangeSelection::setPyEnd(int val)
57 {
58   _end=TraducePyVal(val);
59 }
60
61 std::vector<int> MEDCalculatorDBRangeSelection::getIds(int lgth) const throw(INTERP_KERNEL::Exception)
62 {
63   if(_start>=lgth || _start<0)
64     {
65       std::ostringstream oss;
66       oss << "RangeSelection::getIds : Specified range is outside possible value : " << lgth << " ! ";
67       throw INTERP_KERNEL::Exception(oss.str().c_str());
68     }
69   int trueEnd=_end;
70   if(_end<0)
71     trueEnd=lgth+_end;
72   if(_end==std::numeric_limits<int>::max())
73     trueEnd=lgth;
74   if(trueEnd>lgth)
75     throw INTERP_KERNEL::Exception("RangeSelection::getIds : end specficied is higher than length !");
76   if(_start>trueEnd)
77     throw INTERP_KERNEL::Exception("RangeSelection::getIds : begin of range after end !");
78   std::vector<int> ret(trueEnd-_start);
79   int j=0;
80   for(int i=_start;i<trueEnd;i++,j++)
81     ret[j]=i;
82   return ret;
83 }
84
85 int MEDCalculatorDBRangeSelection::getSize(int lgth) const throw(INTERP_KERNEL::Exception)
86 {
87   return getIds(lgth).size();
88 }
89
90 bool MEDCalculatorDBRangeSelection::isAll() const
91 {
92   return _start==0 && _end==std::numeric_limits<int>::max();
93 }
94
95 void MEDCalculatorDBRangeSelection::setAll()
96 {
97   _start=0;
98   _end=std::numeric_limits<int>::max();
99 }
100
101 MEDCalculatorDBRangeSelection& MEDCalculatorDBRangeSelection::operator=(const char *v) throw(INTERP_KERNEL::Exception)
102 {
103   setValue(v);
104   return *this;
105 }
106
107 MEDCalculatorDBRangeSelection& MEDCalculatorDBRangeSelection::operator=(int v) throw(INTERP_KERNEL::Exception)
108 {
109   setValue(v);
110   return *this;
111 }
112
113 void MEDCalculatorDBRangeSelection::setValue(const char *v) throw(INTERP_KERNEL::Exception)
114 {
115   try
116   {
117     std::string s(v);
118     std::size_t pos=s.find_first_of(SEPARATOR);
119     if(pos!=std::string::npos)
120       {
121         std::string s1=s.substr(0,pos);
122         std::string s2=s.substr(pos+1);
123         std::size_t pos2=s2.find_first_of(SEPARATOR);
124         if(pos2!=std::string::npos)
125           throw INTERP_KERNEL::Exception("RangeSelection constructor : Only one ':' supported !");
126         if(s1.empty())
127           _start=0;
128         else
129           {
130             std::istringstream iss(s1);
131             iss.exceptions(std::istream::failbit | std::istream::badbit);
132             iss >> _start;
133             if(!iss.eof())
134               throw INTERP_KERNEL::Exception("Invalid 1st part of ':' !");
135           }
136         //
137         if(s2.empty())
138           _end=std::numeric_limits<int>::max();
139         else
140           {
141             std::istringstream iss(s2);
142             iss.exceptions(std::istream::failbit | std::istream::badbit);
143             iss >> _end;
144             if(!iss.eof())
145               throw INTERP_KERNEL::Exception("Invalid 2nd part of ':' !");
146           }
147         if(_end>0)
148           if(_start>_end)
149             throw INTERP_KERNEL::Exception("RangeSelection constructor : begin of range after end !");
150       }
151   }
152  catch(INTERP_KERNEL::Exception& e)
153    {
154      throw e;
155    }
156  catch(std::istream::failure& e)
157    {
158      throw INTERP_KERNEL::Exception("RangeSelection constructor : impossible to analyze one side of expr ':' !");
159    }
160 }
161
162 void MEDCalculatorDBRangeSelection::setValue(int v) throw(INTERP_KERNEL::Exception)
163 {
164   _start=v;
165   _end=v+1;
166 }
167
168 int MEDCalculatorDBRangeSelection::TraducePyVal(int val)
169 {
170   return val;
171 }