1 // Copyright (C) 2007-2019 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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"
37 #include "Basics_Utils.hxx"
38 #include "Basics_DirUtils.hxx"
44 #include <sys/types.h>
53 void printStr(const std::string& theValue)
55 std::cout << "printStr: " << theValue << std::endl;
58 void GetSystemDate(int& year, int& month, int& day, int& hours, int& minutes, int& seconds)
74 struct timezone tzone;
77 status = gettimeofday( &tval, &tzone );
78 memcpy(&transfert, localtime((time_t *)&tval.tv_sec), sizeof(tm));
80 month = transfert.tm_mon + 1;
81 day = transfert.tm_mday;
82 year = transfert.tm_year + 1900;
83 hours = transfert.tm_hour;
84 minutes = transfert.tm_min ;
85 seconds = transfert.tm_sec ;
89 std::string GetUserName()
93 wchar_t* pBuff = new wchar_t[UNLEN + 1];
95 char* pBuff = new char[UNLEN + 1];
97 DWORD dwSize = UNLEN + 1;
99 GetUserName ( pBuff, &dwSize );
101 std::wstring theTmpUserName(pBuff, (int)dwSize - 1);
102 retVal = Kernel_Utils::utf8_encode_s(theTmpUserName);
104 std::string theTmpUserName(pBuff, (int)dwSize - 1);
105 retVal = theTmpUserName;
110 struct passwd *infos;
111 infos = getpwuid(getuid());
112 return std::string(infos->pw_name);
116 std::string GetNameFromPath(const std::string& thePath) {
117 if (thePath.empty()) return "";
118 int pos1 = thePath.rfind('/');
119 int pos2 = thePath.rfind('\\');
120 if(pos1 > 0) return thePath.substr(pos1+1, thePath.size());
121 if(pos2 > 0) return thePath.substr(pos2+1, thePath.size());
125 std::string GetDirFromPath(const std::string& thePath) {
126 if (thePath.empty()) return "";
128 int pos = thePath.rfind('/');
131 path = thePath.substr(0, pos+1);
134 pos = thePath.rfind('\\');
135 if(pos > 0) path = thePath.substr(0, pos+1);
138 pos = thePath.rfind('|');
139 if(pos > 0) path = thePath.substr(0, pos+1);
145 #ifdef WIN32 //Check if the only disk letter is given as path
146 if(path.size() == 2 && path[1] == ':') path +='\\';
149 for(int i = 0, len = path.size(); i<len; i++)
150 if(path[i] == '|') path[i] = '/';
155 bool Exists(const std::string thePath)
157 return Kernel_Utils::IsExists( thePath );
161 std::string divideString(const std::string& theValue, int nbChars)
163 return theValue.substr(nbChars, theValue.size());
166 std::vector<std::string> splitString(const std::string& theValue, char separator)
168 std::vector<std::string> vs;
169 if(theValue[0] == separator && theValue.size() == 1) return vs;
170 int pos = theValue.find(separator);
172 vs.push_back(theValue);
176 std::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());
183 if(!s.empty() && s[0] != separator) vs.push_back(s);
188 int main (int argc, char * argv[])
190 std::cout << "Test started " << std::endl;
192 DF_Application* appli = new DF_Application;
194 DF_Document* doc1 = appli->NewDocument("doc_1");
196 DF_Label root1 = doc1->Main();
197 DF_Label child = root1.FindChild(3, true); //0:1:3
199 DF_Container* attr1 = DF_Container::Set(child);
200 attr1->SetInt("eighteen", 18);
203 DF_Attribute* attr = NULL;
204 if(!(attr = child.FindAttribute(DF_Container::GetID()))) {
205 std::cout << "Attribute wasn't found" << std::endl;
208 attr1 = dynamic_cast<DF_Container*>(attr);
209 std::cout << "Attribute was found " << " HasID " << attr1->HasIntID("eighteen") << " value = " << attr1->GetInt("eighteen")<< std::endl;
212 DF_Container *attr2 = (DF_Container*)child.FindAttribute(DF_Container::GetID());
214 if(!attr2) cout << "Can't find the attribute" << endl;
216 std::cout << "Change find : " << attr2->GetInt("eighteen") << std::endl;
219 std::cout << "Forgetting " << child.ForgetAttribute(DF_Container::GetID()) << std::endl;
220 if(!child.FindAttribute(DF_Container::GetID())) {
221 std::cout << "Attribute wasn't found" << std::endl;
225 child = root1.NewChild(); //0:1:4
227 child.NewChild();//0:1:4:1
229 child.NewChild();//0:1:4:2
231 std::cout << "Is equal " << (child == child) << std::endl;
232 std::cout << "Is equal " << (child == root1) << std::endl;
234 child = root1.NewChild(); //0:1:5
236 child.NewChild();//0:1:5:1
237 root1.NewChild();//0:1:6
240 DF_ChildIterator CI(root1.Father(), true);
242 for(; CI.More(); CI.Next()) {
243 std::cout << CI.Value().Entry() << std::endl;
247 DF_Label L = DF_Label::Label(child, "0:1:4:1");
248 std::cout << "Found Label " << L.Entry() << std::endl;
250 std::string s("012-56");
252 int pos = s.find('-');
253 std::cout << "First part : " << s.substr(0, pos) << std::endl;
254 std::cout << "Last part : " << s.substr(pos+1, s.size()) << std::endl;
256 std::vector<std::string> vs = splitString("/dn20/salome/srn/salome2/", '/');
257 for(int i = 0; i<vs.size(); i++)
258 std::cout << vs[i] << std::endl;
260 std::cout << "Diveded str = " << divideString("abcdefg",3) << std::endl;
262 //mkdir("/dn20/salome/srn/salome2", 0x1ff);
264 //cout << "Exists " << Exists("/dn20/salome/srn/salome2") << endl;
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;
274 cout << "User : " << GetUserName() << endl;
276 int month=0,day=0,year=0,hh=0,mn=0,ss=0;
277 GetSystemDate(year, month, day, hh, mn, ss);
278 std::cout << "Date: " << year << " " << month << " " << day << " " << hh << " " << mn << " " << ss << std::endl;
280 std::string t("absd");
281 t.insert(t.begin(), 'b');
282 cout << "Result = " << t << endl;
283 char* cstr = (char*)t.c_str();
288 DF_Document* doc2 = appli->NewDocument("doc_2");
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
300 so51.FindChild(2, true);
303 DF_ChildIterator CI2(so5, true);
305 for(; CI2.More(); CI2.Next()) {
306 std::cout << " ###### Found child with entry = " << CI2.Value().Entry() << std::endl;
307 //CI2.Value().dump();
312 std::cout << "Test finished " << std::endl;