Salome HOME
Fix bug in test on 24 cores CPU test
[modules/kernel.git] / src / DF / testDF.cxx
1 // Copyright (C) 2007-2021  CEA/DEN, EDF R&D, OPEN CASCADE
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
20 //File:    testDF.cxx
21 //Author:  Sergey RUIN
22 //
23 #include <stdio.h>
24 #include <iostream> 
25 #include <vector>
26 #include <string>
27 #include <string.h>
28
29 #include "DF_definitions.hxx"
30 #include "DF_Application.hxx"
31 #include "DF_Document.hxx"
32 #include "DF_Attribute.hxx"
33 #include "DF_Label.hxx"
34 #include "DF_Container.hxx"
35 #include "DF_ChildIterator.hxx"
36
37 #include "Basics_Utils.hxx"
38 #include "Basics_DirUtils.hxx"
39
40 #ifndef WIN32
41 #include <unistd.h>
42 #include <sys/time.h>
43 #include <sys/stat.h>
44 #include <sys/types.h>
45 #include <pwd.h>
46 #else
47 #include <time.h>
48 #include <windows.h>
49 #include <lmcons.h>
50 #endif
51
52
53 void printStr(const std::string& theValue)
54 {
55   std::cout << "printStr: " << theValue   << std::endl;
56 }
57
58 void GetSystemDate(int& year, int& month, int& day, int& hours, int& minutes, int& seconds)
59 {
60 #ifdef WIN32
61   SYSTEMTIME    st;
62
63   GetLocalTime ( &st );
64
65   month = st.wMonth;
66   day = st.wDay;
67   year = st.wYear;
68   hours = st.wHour;
69   minutes = st.wMinute;
70   seconds = st.wSecond;
71 #else
72   struct tm transfert;
73   struct timeval tval;
74   struct timezone tzone;
75   
76   gettimeofday( &tval, &tzone );
77   memcpy(&transfert, localtime((time_t *)&tval.tv_sec), sizeof(tm));
78   
79   month    = transfert.tm_mon + 1;
80   day      = transfert.tm_mday;
81   year     = transfert.tm_year + 1900;
82   hours    = transfert.tm_hour;
83   minutes  = transfert.tm_min ;
84   seconds  = transfert.tm_sec ;
85 #endif
86 }
87
88 std::string GetUserName()
89 {
90 #ifdef WIN32
91 #ifdef UNICODE
92         wchar_t*  pBuff = new wchar_t[UNLEN + 1];
93 #else
94         char*  pBuff = new char[UNLEN + 1];
95 #endif
96   DWORD  dwSize = UNLEN + 1;
97   std::string retVal;
98   GetUserName ( pBuff, &dwSize );
99 #ifdef UNICODE
100   std::wstring theTmpUserName(pBuff, (int)dwSize - 1);
101   retVal = Kernel_Utils::utf8_encode_s(theTmpUserName);
102 #else
103   std::string theTmpUserName(pBuff, (int)dwSize - 1);
104   retVal = theTmpUserName;
105 #endif
106   delete [] pBuff;
107   return retVal;
108 #else
109  struct passwd *infos;
110  infos = getpwuid(getuid()); 
111  return std::string(infos->pw_name);
112 #endif
113 }
114
115 std::string GetNameFromPath(const std::string& thePath) {
116   if (thePath.empty()) return "";
117   int pos1 = thePath.rfind('/');
118   int pos2 = thePath.rfind('\\');
119   if(pos1 > 0) return thePath.substr(pos1+1, thePath.size()); 
120   if(pos2 > 0) return thePath.substr(pos2+1, thePath.size()); 
121   return thePath;
122 }
123
124 std::string GetDirFromPath(const std::string& thePath) {
125   if (thePath.empty()) return "";
126
127   int pos = thePath.rfind('/');
128   std::string path;
129   if(pos > 0) {
130     path = thePath.substr(0, pos+1);
131   }
132   if(path.empty()) {
133     pos = thePath.rfind('\\');
134     if(pos > 0) path = thePath.substr(0, pos+1); 
135   }
136   if(path.empty()) {
137     pos = thePath.rfind('|');
138     if(pos > 0) path = thePath.substr(0, pos+1); 
139   }
140   if(path.empty()) {
141     path = thePath+"/";
142   }
143   
144 #ifdef WIN32  //Check if the only disk letter is given as path
145   if(path.size() == 2 && path[1] == ':') path +='\\';
146 #endif
147
148   for(int i = 0, len = path.size(); i<len; i++) 
149     if(path[i] == '|') path[i] = '/';
150   return path;
151 }
152
153
154 bool Exists(const std::string thePath) 
155 {
156         return Kernel_Utils::IsExists( thePath );
157 }
158
159
160 std::string divideString(const std::string& theValue, int nbChars)
161 {
162   return theValue.substr(nbChars, theValue.size());
163 }
164
165 std::vector<std::string> splitString(const std::string& theValue, char separator)
166 {
167   std::vector<std::string> vs;
168   if(theValue[0] == separator && theValue.size() == 1) return vs;
169   int pos = theValue.find(separator);
170   if(pos < 0) {
171     vs.push_back(theValue);
172     return vs;
173   }
174  
175   std::string s = theValue;
176   if(s[0] == separator) s = s.substr(1, s.size());
177   while((pos = s.find(separator)) >= 0) {
178     vs.push_back(s.substr(0, pos));
179     s = s.substr(pos+1, s.size());
180   }
181                
182   if(!s.empty() && s[0] != separator) vs.push_back(s);
183   return vs;
184 }
185
186
187 int main ()
188 {
189   std::cout << "Test started " << std::endl;
190   
191   DF_Application* appli = new DF_Application;
192   /*  
193   DF_Document* doc1 = appli->NewDocument("doc_1");
194   
195   DF_Label root1 = doc1->Main();
196   DF_Label child = root1.FindChild(3, true); //0:1:3
197   
198   DF_Container* attr1 = DF_Container::Set(child);
199   attr1->SetInt("eighteen", 18);
200
201
202   DF_Attribute* attr = NULL;
203   if(!(attr = child.FindAttribute(DF_Container::GetID()))) {
204     std::cout << "Attribute wasn't found" << std::endl;
205   }
206   else {
207     attr1 = dynamic_cast<DF_Container*>(attr);
208     std::cout << "Attribute was found " << " HasID " << attr1->HasIntID("eighteen") << " value = " << attr1->GetInt("eighteen")<< std::endl;
209   }
210   
211   DF_Container *attr2 = (DF_Container*)child.FindAttribute(DF_Container::GetID());    
212
213   if(!attr2) cout << "Can't find the attribute" << endl;
214
215   std::cout << "Change find : " << attr2->GetInt("eighteen") << std::endl;
216
217
218   std::cout << "Forgetting " << child.ForgetAttribute(DF_Container::GetID()) << std::endl;
219    if(!child.FindAttribute(DF_Container::GetID())) {
220     std::cout << "Attribute wasn't found" << std::endl;
221   }
222
223
224   child = root1.NewChild(); //0:1:4
225   
226   child.NewChild();//0:1:4:1
227     
228   child.NewChild();//0:1:4:2
229
230   std::cout << "Is equal " << (child == child)   << std::endl;
231   std::cout << "Is equal " << (child == root1)   << std::endl;
232
233   child = root1.NewChild(); //0:1:5
234
235   child.NewChild();//0:1:5:1
236   root1.NewChild();//0:1:6
237   
238
239   DF_ChildIterator CI(root1.Father(), true);
240   //root1.dump();
241   for(; CI.More(); CI.Next()) {
242     std::cout << CI.Value().Entry() << std::endl;
243     //CI.Value().dump();
244   }
245
246   DF_Label L = DF_Label::Label(child, "0:1:4:1");
247   std::cout << "Found Label " <<  L.Entry()   << std::endl;
248
249   std::string s("012-56");
250   
251   int pos = s.find('-');
252   std::cout << "First part : " << s.substr(0, pos) << std::endl;
253   std::cout << "Last part : " << s.substr(pos+1, s.size()) << std::endl;
254
255   std::vector<std::string> vs = splitString("/dn20/salome/srn/salome2/", '/');
256   for(int i = 0; i<vs.size(); i++)
257     std::cout << vs[i] << std::endl;
258
259   std::cout << "Diveded str = " << divideString("abcdefg",3) << std::endl;
260   
261   //mkdir("/dn20/salome/srn/salome2", 0x1ff);
262
263   //cout << "Exists " <<  Exists("/dn20/salome/srn/salome2") << endl;
264
265   //cout << GetDirFromPath("/dn20/salome/srn/salome2/test.hdf") << endl;
266   //cout << GetDirFromPath("D:\\salome\\srn\\salome2\\test.hdf") << endl;
267   //cout << GetDirFromPath("D:") << endl;
268   //cout << GetDirFromPath("/dn20") << endl; 
269   //cout << GetDirFromPath("..") << endl;
270   //cout << GetDirFromPath("D:\\") << endl;
271   //cout << GetDirFromPath("D:\\test.hdf") << endl;
272
273   cout << "User : " << GetUserName() << endl;
274   
275   int month=0,day=0,year=0,hh=0,mn=0,ss=0;
276   GetSystemDate(year, month, day, hh, mn, ss);
277   std::cout << "Date: " << year << " " << month << " " << day << " " << hh << " " << mn << " " << ss << std::endl;
278
279   std::string t("absd");
280   t.insert(t.begin(), 'b');
281   cout << "Result = " << t   << endl;
282   char* cstr = (char*)t.c_str();
283   printStr(cstr+1);
284  
285   */
286
287   DF_Document* doc2 = appli->NewDocument("doc_2");
288
289   DF_Label root2 = doc2->Main();
290   DF_Label sco = root2.NewChild();              //0:1:1
291   DF_Label so1 = sco.FindChild(3, true);        //0:1:1:3
292   DF_Label so5 = so1.FindChild(5, true);        //0:1:1:5
293   DF_Label so51 = so5.NewChild();               //0:1:1:5:1
294   DF_Label so511 = so51.NewChild();             //0:1:1:5:1:1
295   DF_Label so513 = so51.FindChild(3, true);     //0:1:1:5:1:3
296   DF_Label so5131 = so513.NewChild();           //0:1:1:5:1:3:1
297   
298
299   so51.FindChild(2, true);
300
301
302   DF_ChildIterator CI2(so5, true);
303   so5.dump();
304   for(; CI2.More(); CI2.Next()) {
305     std::cout << " ###### Found child with entry = " << CI2.Value().Entry() << std::endl;
306     //CI2.Value().dump();
307   }
308
309   delete appli;    
310
311   std::cout << "Test finished " << std::endl;    
312   return 0;
313 }
314