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