Back to index

salome-med  6.5.0
MEDCalculatorDBRangeSelection.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include "MEDCalculatorDBRangeSelection.hxx"
00021 
00022 #include <string>
00023 #include <limits>
00024 #include <sstream>
00025 
00026 using namespace ParaMEDMEM;
00027 
00028 const char MEDCalculatorDBRangeSelection::ALL_ELTS[]=":";
00029 
00030 MEDCalculatorDBRangeSelection::MEDCalculatorDBRangeSelection(const char *v) throw(INTERP_KERNEL::Exception)
00031 try
00032   {
00033     setValue(v);
00034   }
00035  catch(INTERP_KERNEL::Exception& e)
00036    {
00037      throw e;
00038    }
00039 
00040 MEDCalculatorDBRangeSelection::MEDCalculatorDBRangeSelection(int v)
00041 {
00042   setValue(v);
00043 }
00044 
00045 MEDCalculatorDBRangeSelection::MEDCalculatorDBRangeSelection()
00046 {
00047   setValue(ALL_ELTS);
00048 }
00049 
00050 void MEDCalculatorDBRangeSelection::setPyStart(int val)
00051 {
00052   _start=TraducePyVal(val);
00053 }
00054 
00055 void MEDCalculatorDBRangeSelection::setPyEnd(int val)
00056 {
00057   _end=TraducePyVal(val);
00058 }
00059 
00060 std::vector<int> MEDCalculatorDBRangeSelection::getIds(int lgth) const throw(INTERP_KERNEL::Exception)
00061 {
00062   if(_start>=lgth || _start<0)
00063     {
00064       std::ostringstream oss;
00065       oss << "RangeSelection::getIds : Specified range is outside possible value : " << lgth << " ! ";
00066       throw INTERP_KERNEL::Exception(oss.str().c_str());
00067     }
00068   int trueEnd=_end;
00069   if(_end<0)
00070     trueEnd=lgth+_end;
00071   if(_end==std::numeric_limits<int>::max())
00072     trueEnd=lgth;
00073   if(trueEnd>lgth)
00074     throw INTERP_KERNEL::Exception("RangeSelection::getIds : end specficied is higher than length !");
00075   if(_start>trueEnd)
00076     throw INTERP_KERNEL::Exception("RangeSelection::getIds : begin of range after end !");
00077   std::vector<int> ret(trueEnd-_start);
00078   int j=0;
00079   for(int i=_start;i<trueEnd;i++,j++)
00080     ret[j]=i;
00081   return ret;
00082 }
00083 
00084 int MEDCalculatorDBRangeSelection::getSize(int lgth) const throw(INTERP_KERNEL::Exception)
00085 {
00086   return getIds(lgth).size();
00087 }
00088 
00089 bool MEDCalculatorDBRangeSelection::isAll() const
00090 {
00091   return _start==0 && _end==std::numeric_limits<int>::max();
00092 }
00093 
00094 void MEDCalculatorDBRangeSelection::setAll()
00095 {
00096   _start=0;
00097   _end=std::numeric_limits<int>::max();
00098 }
00099 
00100 MEDCalculatorDBRangeSelection& MEDCalculatorDBRangeSelection::operator=(const char *v) throw(INTERP_KERNEL::Exception)
00101 {
00102   setValue(v);
00103   return *this;
00104 }
00105 
00106 MEDCalculatorDBRangeSelection& MEDCalculatorDBRangeSelection::operator=(int v) throw(INTERP_KERNEL::Exception)
00107 {
00108   setValue(v);
00109   return *this;
00110 }
00111 
00112 void MEDCalculatorDBRangeSelection::setValue(const char *v) throw(INTERP_KERNEL::Exception)
00113 {
00114   try
00115   {
00116     std::string s(v);
00117     std::size_t pos=s.find_first_of(SEPARATOR);
00118     if(pos!=std::string::npos)
00119       {
00120         std::string s1=s.substr(0,pos);
00121         std::string s2=s.substr(pos+1);
00122         std::size_t pos2=s2.find_first_of(SEPARATOR);
00123         if(pos2!=std::string::npos)
00124           throw INTERP_KERNEL::Exception("RangeSelection constructor : Only one ':' supported !");
00125         if(s1.empty())
00126           _start=0;
00127         else
00128           {
00129             std::istringstream iss(s1);
00130             iss.exceptions(std::istream::failbit | std::istream::badbit);
00131             iss >> _start;
00132             if(!iss.eof())
00133               throw INTERP_KERNEL::Exception("Invalid 1st part of ':' !");
00134           }
00135         //
00136         if(s2.empty())
00137           _end=std::numeric_limits<int>::max();
00138         else
00139           {
00140             std::istringstream iss(s2);
00141             iss.exceptions(std::istream::failbit | std::istream::badbit);
00142             iss >> _end;
00143             if(!iss.eof())
00144               throw INTERP_KERNEL::Exception("Invalid 2nd part of ':' !");
00145           }
00146         if(_end>0)
00147           if(_start>_end)
00148             throw INTERP_KERNEL::Exception("RangeSelection constructor : begin of range after end !");
00149       }
00150   }
00151  catch(INTERP_KERNEL::Exception& e)
00152    {
00153      throw e;
00154    }
00155  catch(std::istream::failure& e)
00156    {
00157      throw INTERP_KERNEL::Exception("RangeSelection constructor : impossible to analyze one side of expr ':' !");
00158    }
00159 }
00160 
00161 void MEDCalculatorDBRangeSelection::setValue(int v) throw(INTERP_KERNEL::Exception)
00162 {
00163   _start=v;
00164   _end=v+1;
00165 }
00166 
00167 int MEDCalculatorDBRangeSelection::TraducePyVal(int val)
00168 {
00169   return val;
00170 }