Back to index

salome-kernel  6.5.0
testDF.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
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 //File:    testDF.cxx
00021 //Author:  Sergey RUIN
00022 //
00023 #include <stdio.h>
00024 #include <iostream> 
00025 #include <vector>
00026 #include <string>
00027 #include <string.h>
00028 
00029 #include "DF_definitions.hxx"
00030 #include "DF_Application.hxx"
00031 #include "DF_Document.hxx"
00032 #include "DF_Attribute.hxx"
00033 #include "DF_Label.hxx"
00034 #include "DF_Container.hxx"
00035 #include "DF_ChildIterator.hxx"
00036 
00037 #ifndef WIN32
00038 #include <sys/time.h>
00039 #include <sys/stat.h>
00040 #include <sys/types.h>
00041 #include <pwd.h>
00042 #else
00043 #include <time.h>
00044 #include <windows.h>
00045 #include <lmcons.h>
00046 #endif
00047 
00048 
00049 void printStr(const std::string& theValue)
00050 {
00051   std::cout << "printStr: " << theValue   << std::endl;
00052 }
00053 
00054 void GetSystemDate(int& year, int& month, int& day, int& hours, int& minutes, int& seconds)
00055 {
00056 #ifdef WIN32
00057   SYSTEMTIME    st;
00058 
00059   GetLocalTime ( &st );
00060 
00061   month = st.wMonth;
00062   day = st.wDay;
00063   year = st.wYear;
00064   hours = st.wHour;
00065   minutes = st.wMinute;
00066   seconds = st.wSecond;
00067 #else
00068   struct tm transfert;
00069   struct timeval tval;
00070   struct timezone tzone;
00071   int status;
00072 
00073   status = gettimeofday( &tval, &tzone );
00074   memcpy(&transfert, localtime((time_t *)&tval.tv_sec), sizeof(tm));
00075   
00076   month    = transfert.tm_mon + 1;
00077   day      = transfert.tm_mday;
00078   year     = transfert.tm_year + 1900;
00079   hours    = transfert.tm_hour;
00080   minutes  = transfert.tm_min ;
00081   seconds  = transfert.tm_sec ;
00082 #endif
00083 }
00084 
00085 std::string GetUserName()
00086 {
00087 #ifdef WIN32
00088   char*  pBuff = new char[UNLEN + 1];
00089   DWORD  dwSize = UNLEN + 1;
00090   std::string retVal;
00091   GetUserName ( pBuff, &dwSize );
00092   std::string theTmpUserName(pBuff,(int)dwSize -1 );
00093   retVal = theTmpUserName;
00094   delete [] pBuff;
00095   return retVal;
00096 #else
00097  struct passwd *infos;
00098  infos = getpwuid(getuid()); 
00099  return std::string(infos->pw_name);
00100 #endif
00101 }
00102 
00103 std::string GetNameFromPath(const std::string& thePath) {
00104   if (thePath.empty()) return "";
00105   int pos1 = thePath.rfind('/');
00106   int pos2 = thePath.rfind('\\');
00107   if(pos1 > 0) return thePath.substr(pos1+1, thePath.size()); 
00108   if(pos2 > 0) return thePath.substr(pos2+1, thePath.size()); 
00109   return thePath;
00110 }
00111 
00112 std::string GetDirFromPath(const std::string& thePath) {
00113   if (thePath.empty()) return "";
00114 
00115   int pos = thePath.rfind('/');
00116   std::string path;
00117   if(pos > 0) {
00118     path = thePath.substr(0, pos+1);
00119   }
00120   if(path.empty()) {
00121     pos = thePath.rfind('\\');
00122     if(pos > 0) path = thePath.substr(0, pos+1); 
00123   }
00124   if(path.empty()) {
00125     pos = thePath.rfind('|');
00126     if(pos > 0) path = thePath.substr(0, pos+1); 
00127   }
00128   if(path.empty()) {
00129     path = thePath+"/";
00130   }
00131   
00132 #ifdef WIN32  //Check if the only disk letter is given as path
00133   if(path.size() == 2 && path[1] == ':') path +='\\';
00134 #endif
00135 
00136   for(int i = 0, len = path.size(); i<len; i++) 
00137     if(path[i] == '|') path[i] = '/';
00138   return path;
00139 }
00140 
00141 
00142 bool Exists(const std::string thePath) 
00143 {
00144 #ifdef WIN32 
00145   if (  GetFileAttributes (  thePath.c_str()  ) == 0xFFFFFFFF  ) { 
00146     if (  GetLastError () != ERROR_FILE_NOT_FOUND  ) {
00147       return false;
00148     }
00149   }
00150 #else 
00151   int status = access ( thePath.c_str() , F_OK ); 
00152   if (status != 0) return false;
00153 #endif
00154   return true;
00155 }
00156 
00157 
00158 std::string divideString(const std::string& theValue, int nbChars)
00159 {
00160   return theValue.substr(nbChars, theValue.size());
00161 }
00162 
00163 std::vector<std::string> splitString(const std::string& theValue, char separator)
00164 {
00165   std::vector<std::string> vs;
00166   if(theValue[0] == separator && theValue.size() == 1) return vs;
00167   int pos = theValue.find(separator);
00168   if(pos < 0) {
00169     vs.push_back(theValue);
00170     return vs;
00171   }
00172  
00173   std::string s = theValue;
00174   if(s[0] == separator) s = s.substr(1, s.size());
00175   while((pos = s.find(separator)) >= 0) {
00176     vs.push_back(s.substr(0, pos));
00177     s = s.substr(pos+1, s.size());
00178   }
00179                
00180   if(!s.empty() && s[0] != separator) vs.push_back(s);
00181   return vs;
00182 }
00183 
00184 
00185 int main (int argc, char * argv[])
00186 {
00187   std::cout << "Test started " << std::endl;
00188   
00189   DF_Application* appli = new DF_Application;
00190   /*  
00191   DF_Document* doc1 = appli->NewDocument("doc_1");
00192   
00193   DF_Label root1 = doc1->Main();
00194   DF_Label child = root1.FindChild(3, true); //0:1:3
00195   
00196   DF_Container* attr1 = DF_Container::Set(child);
00197   attr1->SetInt("eighteen", 18);
00198 
00199 
00200   DF_Attribute* attr = NULL;
00201   if(!(attr = child.FindAttribute(DF_Container::GetID()))) {
00202     std::cout << "Attribute wasn't found" << std::endl;
00203   }
00204   else {
00205     attr1 = dynamic_cast<DF_Container*>(attr);
00206     std::cout << "Attribute was found " << " HasID " << attr1->HasIntID("eighteen") << " value = " << attr1->GetInt("eighteen")<< std::endl;
00207   }
00208   
00209   DF_Container *attr2 = (DF_Container*)child.FindAttribute(DF_Container::GetID());    
00210 
00211   if(!attr2) cout << "Can't find the attribute" << endl;
00212 
00213   std::cout << "Change find : " << attr2->GetInt("eighteen") << std::endl;
00214 
00215 
00216   std::cout << "Forgetting " << child.ForgetAttribute(DF_Container::GetID()) << std::endl;
00217    if(!child.FindAttribute(DF_Container::GetID())) {
00218     std::cout << "Attribute wasn't found" << std::endl;
00219   }
00220 
00221 
00222   child = root1.NewChild(); //0:1:4
00223   
00224   child.NewChild();//0:1:4:1
00225     
00226   child.NewChild();//0:1:4:2
00227 
00228   std::cout << "Is equal " << (child == child)   << std::endl;
00229   std::cout << "Is equal " << (child == root1)   << std::endl;
00230 
00231   child = root1.NewChild(); //0:1:5
00232 
00233   child.NewChild();//0:1:5:1
00234   root1.NewChild();//0:1:6
00235   
00236 
00237   DF_ChildIterator CI(root1.Father(), true);
00238   //root1.dump();
00239   for(; CI.More(); CI.Next()) {
00240     std::cout << CI.Value().Entry() << std::endl;
00241     //CI.Value().dump();
00242   }
00243 
00244   DF_Label L = DF_Label::Label(child, "0:1:4:1");
00245   std::cout << "Found Label " <<  L.Entry()   << std::endl;
00246 
00247   std::string s("012-56");
00248   
00249   int pos = s.find('-');
00250   std::cout << "Fisrt part : " << s.substr(0, pos) << std::endl;
00251   std::cout << "Last part : " << s.substr(pos+1, s.size()) << std::endl;
00252 
00253   std::vector<std::string> vs = splitString("/dn20/salome/srn/salome2/", '/');
00254   for(int i = 0; i<vs.size(); i++)
00255     std::cout << vs[i] << std::endl;
00256 
00257   std::cout << "Diveded str = " << divideString("abcdefg",3) << std::endl;
00258   
00259   //mkdir("/dn20/salome/srn/salome2", 0x1ff);
00260 
00261   //cout << "Exists " <<  Exists("/dn20/salome/srn/salome2") << endl;
00262 
00263   //cout << GetDirFromPath("/dn20/salome/srn/salome2/test.hdf") << endl;
00264   //cout << GetDirFromPath("D:\\salome\\srn\\salome2\\test.hdf") << endl;
00265   //cout << GetDirFromPath("D:") << endl;
00266   //cout << GetDirFromPath("/dn20") << endl; 
00267   //cout << GetDirFromPath("..") << endl;
00268   //cout << GetDirFromPath("D:\\") << endl;
00269   //cout << GetDirFromPath("D:\\test.hdf") << endl;
00270 
00271   cout << "User : " << GetUserName() << endl;
00272   
00273   int month=0,day=0,year=0,hh=0,mn=0,ss=0;
00274   GetSystemDate(year, month, day, hh, mn, ss);
00275   std::cout << "Date: " << year << " " << month << " " << day << " " << hh << " " << mn << " " << ss << std::endl;
00276 
00277   std::string t("absd");
00278   t.insert(t.begin(), 'b');
00279   cout << "Result = " << t   << endl;
00280   char* cstr = (char*)t.c_str();
00281   printStr(cstr+1);
00282  
00283   */
00284 
00285   DF_Document* doc2 = appli->NewDocument("doc_2");
00286 
00287   DF_Label root2 = doc2->Main();
00288   DF_Label sco = root2.NewChild();              //0:1:1
00289   DF_Label so1 = sco.FindChild(3, true);        //0:1:1:3
00290   DF_Label so5 = so1.FindChild(5, true);        //0:1:1:5
00291   DF_Label so51 = so5.NewChild();               //0:1:1:5:1
00292   DF_Label so511 = so51.NewChild();             //0:1:1:5:1:1
00293   DF_Label so513 = so51.FindChild(3, true);     //0:1:1:5:1:3
00294   DF_Label so5131 = so513.NewChild();           //0:1:1:5:1:3:1
00295   
00296 
00297   so51.FindChild(2, true);
00298 
00299 
00300   DF_ChildIterator CI2(so5, true);
00301   so5.dump();
00302   for(; CI2.More(); CI2.Next()) {
00303     std::cout << " ###### Found child with entry = " << CI2.Value().Entry() << std::endl;
00304     //CI2.Value().dump();
00305   }
00306 
00307   delete appli;    
00308 
00309   std::cout << "Test finished " << std::endl;    
00310   return 0;
00311 }
00312