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