Salome HOME
Removed includes and libraries of OCC
[modules/kernel.git] / src / DF / testDF.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 //File:    testDF.cxx
21 //Author:  Sergey RUIN
22
23 #include <stdio.h>
24 #include <iostream.h> 
25 #include <vector>
26 #include <string>
27
28 #include "DF_definitions.hxx"
29 #include "DF_Application.hxx"
30 #include "DF_Document.hxx"
31 #include "DF_Attribute.hxx"
32 #include "DF_Label.hxx"
33 #include "DF_Container.hxx"
34 #include "DF_ChildIterator.hxx"
35
36 #ifndef WNT
37 #include <sys/time.h>
38 #include <sys/stat.h>
39 #include <sys/types.h>
40 #include <pwd.h>
41 #else
42 #include <time.h>
43 #include <lmcons.h>
44 #endif
45
46 using namespace std;
47
48 void printStr(const string& theValue)
49 {
50   cout << "printStr: " << theValue   << endl;
51 }
52
53 void GetSystemDate(int& year, int& month, int& day, int& hours, int& minutes, int& seconds)
54 {
55 #ifdef WNT
56   SYSTEMTIME    st;
57
58   GetLocalTime ( &st );
59
60   month = st.wMonth;
61   day = st.wDay;
62   year = st.wYear;
63   hours = st.wHour;
64   minutes = st.wMinute;
65   seconds = st.wSecond;
66 #else
67   struct tm transfert;
68   struct timeval tval;
69   struct timezone tzone;
70   int status;
71
72   status = gettimeofday( &tval, &tzone );
73   memcpy(&transfert, localtime((time_t *)&tval.tv_sec), sizeof(tm));
74   
75   month    = transfert.tm_mon + 1;
76   day      = transfert.tm_mday;
77   year     = transfert.tm_year + 1900;
78   hours    = transfert.tm_hour;
79   minutes  = transfert.tm_min ;
80   seconds  = transfert.tm_sec ;
81 #endif
82 }
83
84 string GetUserName()
85 {
86 #ifdef WNT
87   char*  pBuff = new char[UNLEN + 1];
88   DWORD  dwSize = UNLEN + 1;
89   string retVal;
90   GetUserName ( pBuff, &dwSize );
91   string theTmpUserName(pBuff,(int)dwSize -1 );
92   retVal = theTmpUserName;
93   delete [] pBuff;
94   return retVal;
95 #else
96  struct passwd *infos;
97  infos = getpwuid(getuid()); 
98  return string(infos->pw_name);
99 #endif
100 }
101
102 string GetNameFromPath(const string& thePath) {
103   if (thePath.empty()) return "";
104   int pos1 = thePath.rfind('/');
105   int pos2 = thePath.rfind('\\');
106   if(pos1 > 0) return thePath.substr(pos1+1, thePath.size()); 
107   if(pos2 > 0) return thePath.substr(pos2+1, thePath.size()); 
108   return thePath;
109 }
110
111 string GetDirFromPath(const string& thePath) {
112   if (thePath.empty()) return "";
113
114   int pos = thePath.rfind('/');
115   string path;
116   if(pos > 0) {
117     path = thePath.substr(0, pos+1);
118   }
119   if(path.empty()) {
120     pos = thePath.rfind('\\');
121     if(pos > 0) path = thePath.substr(0, pos+1); 
122   }
123   if(path.empty()) {
124     pos = thePath.rfind('|');
125     if(pos > 0) path = thePath.substr(0, pos+1); 
126   }
127   if(path.empty()) {
128     path = thePath+"/";
129   }
130   
131 #ifdef WNT  //Check if the only disk letter is given as path
132   if(path.size() == 2 && path[1] == ":") path +='\\';
133 #endif
134
135   for(int i = 0, len = path.size(); i<len; i++) 
136     if(path[i] == '|') path[i] = '/';
137   return path;
138 }
139
140
141 bool Exists(const string thePath) 
142 {
143 #ifdef WNT 
144   if (  GetFileAttributes (  thePath.c_str()  ) == 0xFFFFFFFF  ) { 
145     if (  GetLastError () != ERROR_FILE_NOT_FOUND  ) {
146       return false;
147     }
148   }
149 #else 
150   int status = access ( thePath.c_str() , F_OK ); 
151   if (status != 0) return false;
152 #endif
153   return true;
154 }
155
156
157 string divideString(const string& theValue, int nbChars)
158 {
159   return theValue.substr(nbChars, theValue.size());
160 }
161
162 vector<string> splitString(const string& theValue, char separator)
163 {
164   vector<string> vs;
165   if(theValue[0] == separator && theValue.size() == 1) return vs;
166   int pos = theValue.find(separator);
167   if(pos < 0) {
168     vs.push_back(theValue);
169     return vs;
170   }
171  
172   string s = theValue;
173   if(s[0] == separator) s = s.substr(1, s.size());
174   while((pos = s.find(separator)) >= 0) {
175     vs.push_back(s.substr(0, pos));
176     s = s.substr(pos+1, s.size());
177   }
178                
179   if(!s.empty() && s[0] != separator) vs.push_back(s);
180   return vs;
181 }
182
183
184 int main (int argc, char * argv[])
185 {
186   cout << "Test started " << endl;
187   
188   DF_Application* appli = new DF_Application;
189   DF_Document* doc1 = appli->NewDocument("doc_1");
190   
191   /*  
192   DF_Label root1 = doc1->Main();
193   DF_Label child = root1.FindChild(3, true); //0:1:3
194   
195   DF_Container* attr1 = DF_Container::Set(child);
196   attr1->SetInt("eighteen", 18);
197
198
199   DF_Attribute* attr = NULL;
200   if(!(attr = child.FindAttribute(DF_Container::GetID()))) {
201     cout << "Attribute wasn't found" << endl;
202   }
203   else {
204     attr1 = dynamic_cast<DF_Container*>(attr);
205     cout << "Attribute was found " << " HasID " << attr1->HasIntID("eighteen") << " value = " << attr1->GetInt("eighteen")<< endl;
206   }
207   
208   DF_Container *attr2 = (DF_Container*)child.FindAttribute(DF_Container::GetID());    
209
210   if(!attr2) cout << "Can't find the attribute" << endl;
211
212   cout << "Change find : " << attr2->GetInt("eighteen") << endl;
213
214
215   cout << "Forgetting " << child.ForgetAttribute(DF_Container::GetID()) << endl;
216    if(!child.FindAttribute(DF_Container::GetID())) {
217     cout << "Attribute wasn't found" << endl;
218   }
219
220
221   child = root1.NewChild(); //0:1:4
222   
223   child.NewChild();//0:1:4:1
224     
225   child.NewChild();//0:1:4:2
226
227   cout << "Is equal " << (child == child)   << endl;
228   cout << "Is equal " << (child == root1)   << endl;
229
230   child = root1.NewChild(); //0:1:5
231
232   child.NewChild();//0:1:5:1
233   root1.NewChild();//0:1:6
234   
235
236   DF_ChildIterator CI(root1.Father(), true);
237   //root1.dump();
238   for(; CI.More(); CI.Next()) {
239     cout << CI.Value().Entry() << endl;
240     //CI.Value().dump();
241   }
242
243   DF_Label L = DF_Label::Label(child, "0:1:4:1");
244   cout << "Found Label " <<  L.Entry()   << endl;
245
246   std::string s("012-56");
247   
248   int pos = s.find('-');
249   cout << "Fisrt part : " << s.substr(0, pos) << endl;
250   cout << "Last part : " << s.substr(pos+1, s.size()) << endl;
251
252   vector<string> vs = splitString("/dn20/salome/srn/salome2/", '/');
253   for(int i = 0; i<vs.size(); i++)
254     cout << vs[i] << endl;
255
256   cout << "Diveded str = " << divideString("abcdefg",3) << endl;
257   
258   //mkdir("/dn20/salome/srn/salome2", 0x1ff);
259
260   //cout << "Exists " <<  Exists("/dn20/salome/srn/salome2") << endl;
261
262   //cout << GetDirFromPath("/dn20/salome/srn/salome2/test.hdf") << endl;
263   //cout << GetDirFromPath("D:\\salome\\srn\\salome2\\test.hdf") << endl;
264   //cout << GetDirFromPath("D:") << endl;
265   //cout << GetDirFromPath("/dn20") << endl; 
266   //cout << GetDirFromPath("..") << endl;
267   //cout << GetDirFromPath("D:\\") << endl;
268   //cout << GetDirFromPath("D:\\test.hdf") << endl;
269
270   cout << "User : " << GetUserName() << endl;
271   
272   int month=0,day=0,year=0,hh=0,mn=0,ss=0;
273   GetSystemDate(year, month, day, hh, mn, ss);
274   cout << "Date: " << year << " " << month << " " << day << " " << hh << " " << mn << " " << ss << endl;
275
276   string t("absd");
277   t.insert(t.begin(), 'b');
278   cout << "Result = " << t   << endl;
279   char* cstr = (char*)t.c_str();
280   printStr(cstr+1);
281  
282   */
283
284   DF_Document* doc2 = appli->NewDocument("doc_2");
285
286   DF_Label root2 = doc2->Main();
287   DF_Label sco = root2.NewChild();              //0:1:1
288   DF_Label so1 = sco.FindChild(3, true);        //0:1:1:3
289   DF_Label so5 = so1.FindChild(5, true);        //0:1:1:5
290   DF_Label so51 = so5.NewChild();               //0:1:1:5:1
291   DF_Label so511 = so51.NewChild();             //0:1:1:5:1:1
292   DF_Label so513 = so51.FindChild(3, true);     //0:1:1:5:1:3
293   DF_Label so5131 = so513.NewChild();           //0:1:1:5:1:3:1
294   
295
296   so51.FindChild(2, true);
297
298
299   DF_ChildIterator CI2(so5, true);
300   so5.dump();
301   for(; CI2.More(); CI2.Next()) {
302     cout << " ###### Found child with entry = " << CI2.Value().Entry() << endl;
303     //CI2.Value().dump();
304   }
305
306   delete appli;    
307
308   cout << "Test finished " << endl;    
309   return 0;
310 }
311