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