Back to index

salome-med  6.5.0
MEDLoaderBase.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 "MEDLoaderBase.hxx"
00021 #include "InterpKernelException.hxx"
00022 
00023 #include <sstream>
00024 #include <fstream>
00025 #include <cstring>
00026 #include <iostream>
00027 
00028 const char MEDLoaderBase::WHITE_SPACES[]=" \n";
00029 
00030 int MEDLoaderBase::getStatusOfFile(const char *fileName)
00031 {
00032   std::ifstream ifs;
00033   ifs.open(fileName);
00034   if((ifs.rdstate() & std::ifstream::failbit)!=0)
00035     {
00036       ifs.close();
00037       return NOT_EXIST;
00038     }
00039   std::ofstream ofs(fileName,std::ios_base::app);
00040   if((ofs.rdstate() & std::ofstream::failbit)!=0)
00041     {
00042       return EXIST_RDONLY;
00043     }
00044   return EXIST_RW;
00045 }
00046 
00047 char *MEDLoaderBase::buildEmptyString(int lgth)
00048 {
00049   char *ret=new char[lgth+1];
00050   std::fill(ret,ret+lgth,' ');
00051   ret[lgth]='\0';
00052   return ret;
00053 }
00054 
00055 std::string MEDLoaderBase::buildUnionUnit(const char *name, int nameLgth, const char *unit, int unitLgth)
00056 {
00057   std::string ret(buildStringFromFortran(name,nameLgth));
00058   std::string unitCpp(buildStringFromFortran(unit,unitLgth));
00059   if(unitCpp[0]=='\0')
00060     return ret;
00061   ret+=" [";
00062   ret+=unitCpp;
00063   ret+="]";
00064   return ret;
00065 }
00066 
00067 void MEDLoaderBase::splitIntoNameAndUnit(const std::string& s, std::string& name, std::string& unit)
00068 {
00069   std::string::size_type f1=s.find_first_of('[');
00070   std::string::size_type f2=s.find_last_of(']');
00071   if(f1!=std::string::npos && f2!=std::string::npos)
00072     {
00073       if(f1<f2)
00074         {
00075           name=s.substr(0,f1);
00076           unit=s.substr(f1+1,f2-f1-1);
00077           strip(name);
00078           strip(unit);
00079           return;
00080         }
00081     }
00082   name=s;
00083   unit="";
00084 }
00085 
00086 void MEDLoaderBase::strip(std::string& s)
00087 {
00088   std::string::size_type f1=s.find_first_not_of(' ');
00089   if(f1==std::string::npos)
00090     {
00091       s="";
00092       return ;
00093     }
00094   std::string::size_type f2=s.find_last_not_of(' ');
00095   s=s.substr(f1,f2-f1+1);
00096 }
00097 
00104 void MEDLoaderBase::safeStrCpy(const char *src, int maxLgth, char *dest, int behaviour) throw(INTERP_KERNEL::Exception)
00105 {
00106   if((int)strlen(src)>maxLgth)
00107     {
00108       if(behaviour==0 || behaviour>1)
00109         {
00110           std::ostringstream oss; oss << "A string : \"" << src << "\" has been detected to be too long for MED File ( > " << maxLgth << ") !";
00111           throw INTERP_KERNEL::Exception(oss.str().c_str());
00112         }
00113       else if(behaviour==1)
00114         {
00115           std::string s=zipString(src,maxLgth);
00116           std::cerr << "A string : \"" << src << "\" has been detected to be too long for MED File ( > " << maxLgth << ") : ";
00117           std::cerr << "zipping to : " << s << "\n";
00118           strcpy(dest,s.c_str());
00119           return ;
00120         }
00121     }
00122   strcpy(dest,src);
00123 }
00124 
00129 void MEDLoaderBase::safeStrCpy2(const char *src, int maxLgth, char *dest, int behaviour) throw(INTERP_KERNEL::Exception)
00130 {
00131   if((int)strlen(src)>maxLgth)
00132     {
00133       if(behaviour==0 || behaviour>1)
00134         {
00135           std::ostringstream oss; oss << "A string : \"" << src << "\" has been detected to be too long for MED File ( > " << maxLgth << ") !";
00136           throw INTERP_KERNEL::Exception(oss.str().c_str());
00137         }
00138       else if(behaviour==1)
00139         {
00140           std::string s=zipString(src,maxLgth);
00141           std::cerr << "A string : \"" << src << "\" has been detected to be too long for MED File ( > " << maxLgth << ") : ";
00142           std::cerr << "zipping to : " << s << "\n";
00143           strcpy(dest,s.c_str());
00144           return ;
00145         }
00146     }
00147   int n=strlen(src);
00148   strncpy(dest,src,n);
00149 }
00150 
00151 std::string MEDLoaderBase::buildStringFromFortran(const char *expr, int lgth)
00152 {
00153   std::string ret(expr,lgth);
00154   std::string whiteSpaces(WHITE_SPACES);
00155   std::size_t lgthReal=strlen(ret.c_str());
00156   std::string ret2=ret.substr(0,lgthReal);
00157   std::size_t found=ret2.find_last_not_of(whiteSpaces);
00158   if (found!=std::string::npos)
00159     ret2.erase(found+1);
00160   else
00161     ret2.clear();//ret is all whitespace
00162   return ret2;
00163 }
00164 
00169 std::string MEDLoaderBase::zipString(const char *src, int sizeToRespect)
00170 {
00171   std::string s(src);
00172   strip(s);
00173   if((int)s.length()<=sizeToRespect)
00174     return s;
00175   s=src;
00176   zipEqualConsChar(s,3);
00177   if((int)s.length()<=sizeToRespect)
00178     return s;
00179   s=src;
00180   zipEqualConsChar(s,2);
00181   if((int)s.length()<=sizeToRespect)
00182     return s;
00183   s=src;
00184   return s.substr(0,sizeToRespect);
00185 }
00186 
00192 void MEDLoaderBase::zipEqualConsChar(std::string& s, int minConsSmChar)
00193 {
00194   for(std::string::iterator it=s.begin();it!=s.end();it++)
00195     {
00196       char tmp=*it;
00197       int sz=1;
00198       for(std::string::iterator it2=it+1;it2!=s.end() && *it2==tmp;it2++)
00199         sz++;
00200       if(sz>=minConsSmChar)
00201         s.erase(it+1,it+sz);
00202     }
00203 }
00204