Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
testIoTools.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Test functions in vpIoTools.
33  *
34  *****************************************************************************/
41 #include <iostream>
42 #include <stdio.h>
43 #include <string.h>
44 #include <visp3/core/vpIoTools.h>
45 
46 namespace
47 {
48 template <typename T>
49 void checkReadBinaryValue(std::ifstream &file, const T checkValue)
50 {
51  T value = (T)10;
52  vpIoTools::readBinaryValueLE(file, value);
53  if (value != checkValue) {
54  std::stringstream ss;
55  ss << "Read: " << value << " ; Expected: " << checkValue;
56  throw vpException(vpException::badValue, ss.str());
57  }
58 }
59 
60 template <>
61 void checkReadBinaryValue<float>(std::ifstream &file, const float checkValue)
62 {
63  float value = 10.0f;
64  vpIoTools::readBinaryValueLE(file, value);
65  if (!vpMath::equal(value, checkValue, std::numeric_limits<float>::epsilon())) {
66  std::stringstream ss;
67  ss << "Read: " << value << " ; Expected: " << checkValue;
68  throw vpException(vpException::badValue, ss.str());
69  }
70 }
71 
72 template <>
73 void checkReadBinaryValue<double>(std::ifstream &file, const double checkValue)
74 {
75  double value = 10.0;
76  vpIoTools::readBinaryValueLE(file, value);
77  if (!vpMath::equal(value, checkValue, std::numeric_limits<double>::epsilon())) {
78  std::stringstream ss;
79  ss << "Read: " << value << " ; Expected: " << checkValue;
80  throw vpException(vpException::badValue, ss.str());
81  }
82 }
83 }
84 
85 int main(int argc, const char **argv)
86 {
87  const char c = vpIoTools::separator;
88  if (c == '\\') {
89  std::cout << "The directory separator character is '" << c << "' (Windows platform)." << std::endl;
90  } else {
91  std::cout << "The directory separator character is '" << c << "' (Unix like platform)." << std::endl;
92  }
93 
94 #if defined(_WIN32)
95  std::string pathname = "C:\\Program Files (x86)\\Java\\jre7";
96 #else
97  std::string pathname = "/usr/bin/java";
98 #endif
99 
100  std::cout << "Parent of " << pathname << " is " << vpIoTools::getParent(pathname) << std::endl;
101  std::cout << "Name of " << pathname << " is " << vpIoTools::getName(pathname) << std::endl;
102 
103  if (argc == 3 && std::string(argv[1]) == std::string("-i")) {
104  std::cout << "Parent of " << argv[2] << " is " << vpIoTools::getParent(argv[2]) << std::endl;
105  std::cout << "Name of " << argv[2] << " is " << vpIoTools::getName(argv[2]) << std::endl;
106  }
107 
108  std::string windowsPathnameStyle = "\\usr\\bin\\java";
109  std::cout << "Parent of " << windowsPathnameStyle << " is " << vpIoTools::getParent(windowsPathnameStyle)
110  << std::endl;
111  std::cout << "Name of " << windowsPathnameStyle << " is " << vpIoTools::getName(windowsPathnameStyle) << std::endl;
112 
113  std::string parent = "/usr/toto/", child = "\\blabla\\java";
114  std::cout << "parent=" << vpIoTools::path(parent) << " ; child=" << vpIoTools::path(child) << std::endl;
115  std::cout << "Create file path from parent=" << parent << " and child=" << child << " is "
116  << vpIoTools::createFilePath(parent, child) << std::endl;
117 
118  std::string expandPath = "~/Documents/fictional directory/fictional file";
119  std::cout << "Path for " << expandPath << " is " << vpIoTools::path(expandPath) << std::endl;
120 
121  std::cout << "Test get name with an empty pathname=" << vpIoTools::getName("") << std::endl;
122  std::cout << "Get parent with an empty pathname=" << vpIoTools::getParent("") << std::endl;
123  std::cout << "Get parent with a filename=" << vpIoTools::getParent("my_file.txt") << std::endl;
124  expandPath = "~/Documents/fictional dir/fictional file.txt";
125  std::cout << "Get name with a unix expand pathname " << expandPath << "=" << vpIoTools::getName(expandPath)
126  << std::endl;
127  std::cout << "Get parent with a unix expand pathname " << expandPath << "=" << vpIoTools::getParent(expandPath)
128  << std::endl;
129 
130  pathname = "c:/dir";
131  std::cout << "pathname=" << vpIoTools::splitDrive(pathname).first << " ; " << vpIoTools::splitDrive(pathname).second
132  << std::endl;
133 
134  std::cout << "isAbsolutePath of " << pathname << "=" << vpIoTools::isAbsolutePathname(pathname) << std::endl;
135 
136  pathname = "c:/dir/fictional directory/fictional file.txt";
137  std::cout << "isAbsolutePath of " << pathname << "=" << vpIoTools::isAbsolutePathname(pathname) << std::endl;
138 
139  pathname = "/home/user/Documents/fictional directory/fictional file.txt";
140  std::cout << "isAbsolutePath of " << pathname << "=" << vpIoTools::isAbsolutePathname(pathname) << std::endl;
141 
142  pathname = "~/Documents/fictional directory/fictional file.txt";
143  std::cout << "isAbsolutePath of " << pathname << "=" << vpIoTools::isAbsolutePathname(pathname) << std::endl;
144 
145  pathname = "fictional directory/fictional file.txt";
146  std::cout << "isAbsolutePath of " << pathname << "=" << vpIoTools::isAbsolutePathname(pathname) << std::endl;
147 
148  // Test vpIoTools::splitDrive
149  unsigned int nbFail = 0, nbOk = 0;
150 #if defined(_WIN32)
151  if (strcmp(vpIoTools::splitDrive("c:\\foo\\bar").first.c_str(), "c:") == 0) {
152  nbOk++;
153  } else {
154  nbFail++;
155  std::cout << "Fail=" << vpIoTools::splitDrive("c:\\foo\\bar").first << " should be=c:" << std::endl;
156  }
157  if (strcmp(vpIoTools::splitDrive("c:\\foo\\bar").second.c_str(), "\\foo\\bar") == 0) {
158  nbOk++;
159  } else {
160  nbFail++;
161  std::cout << "Fail=" << vpIoTools::splitDrive("c:\\foo\\bar").second << " should be=\\foo\\bar" << std::endl;
162  }
163 
164  if (strcmp(vpIoTools::splitDrive("c:/foo/bar").first.c_str(), "c:") == 0) {
165  nbOk++;
166  } else {
167  nbFail++;
168  std::cout << "Fail=" << vpIoTools::splitDrive("c:/foo/bar").first << " should be=c:" << std::endl;
169  }
170  if (strcmp(vpIoTools::splitDrive("c:/foo/bar").second.c_str(), "/foo/bar") == 0) {
171  nbOk++;
172  } else {
173  nbFail++;
174  std::cout << "Fail=" << vpIoTools::splitDrive("c:/foo/bar").second << " should be=/foo/bar" << std::endl;
175  }
176 
177  if (strcmp(vpIoTools::splitDrive("\\\\conky\\mountpoint\\foo\\bar").first.c_str(), "\\\\conky\\mountpoint") == 0) {
178  nbOk++;
179  } else {
180  nbFail++;
181  std::cout << "Fail=" << vpIoTools::splitDrive("\\\\conky\\mountpoint\\foo\\bar").first
182  << " should be=\\\\conky\\mountpoint" << std::endl;
183  }
184  if (strcmp(vpIoTools::splitDrive("\\\\conky\\mountpoint\\foo\\bar").second.c_str(), "\\foo\\bar") == 0) {
185  nbOk++;
186  } else {
187  nbFail++;
188  std::cout << "Fail=" << vpIoTools::splitDrive("\\\\conky\\mountpoint\\foo\\bar").second << " should be=\\foo\\bar"
189  << std::endl;
190  }
191 
192  if (strcmp(vpIoTools::splitDrive("//conky/mountpoint/foo/bar").first.c_str(), "//conky/mountpoint") == 0) {
193  nbOk++;
194  } else {
195  nbFail++;
196  std::cout << "Fail=" << vpIoTools::splitDrive("//conky/mountpoint/foo/bar").first << " should be=//conky/mountpoint"
197  << std::endl;
198  }
199  if (strcmp(vpIoTools::splitDrive("//conky/mountpoint/foo/bar").second.c_str(), "/foo/bar") == 0) {
200  nbOk++;
201  } else {
202  nbFail++;
203  std::cout << "Fail=" << vpIoTools::splitDrive("//conky/mountpoint/foo/bar").second << " should be=/foo/bar"
204  << std::endl;
205  }
206 
207  if (strcmp(vpIoTools::splitDrive("\\\\\\conky\\mountpoint\\foo\\bar").first.c_str(), "") == 0) {
208  nbOk++;
209  } else {
210  nbFail++;
211  std::cout << "Fail=" << vpIoTools::splitDrive("\\\\\\conky\\mountpoint\\foo\\bar").first
212  << " should be=" << std::endl;
213  }
214  if (strcmp(vpIoTools::splitDrive("\\\\\\conky\\mountpoint\\foo\\bar").second.c_str(),
215  "\\\\\\conky\\mountpoint\\foo\\bar") == 0) {
216  nbOk++;
217  } else {
218  nbFail++;
219  std::cout << "Fail=" << vpIoTools::splitDrive("\\\\\\conky\\mountpoint\\foo\\bar").second
220  << " should be=\\\\\\conky\\mountpoint\\foo\\bar" << std::endl;
221  }
222 
223  if (strcmp(vpIoTools::splitDrive("///conky/mountpoint/foo/bar").first.c_str(), "") == 0) {
224  nbOk++;
225  } else {
226  nbFail++;
227  std::cout << "Fail=" << vpIoTools::splitDrive("///conky/mountpoint/foo/bar").first << " should be=" << std::endl;
228  }
229  if (strcmp(vpIoTools::splitDrive("///conky/mountpoint/foo/bar").second.c_str(), "///conky/mountpoint/foo/bar") == 0) {
230  nbOk++;
231  } else {
232  nbFail++;
233  std::cout << "Fail=" << vpIoTools::splitDrive("///conky/mountpoint/foo/bar").second
234  << " should be=///conky/mountpoint/foo/bar" << std::endl;
235  }
236 
237  if (strcmp(vpIoTools::splitDrive("\\\\conky\\\\mountpoint\\foo\\bar").first.c_str(), "") == 0) {
238  nbOk++;
239  } else {
240  nbFail++;
241  std::cout << "Fail=" << vpIoTools::splitDrive("\\\\conky\\\\mountpoint\\foo\\bar").first
242  << " should be=" << std::endl;
243  }
244  if (strcmp(vpIoTools::splitDrive("\\\\conky\\\\mountpoint\\foo\\bar").second.c_str(),
245  "\\\\conky\\\\mountpoint\\foo\\bar") == 0) {
246  nbOk++;
247  } else {
248  nbFail++;
249  std::cout << "Fail=" << vpIoTools::splitDrive("\\\\conky\\\\mountpoint\\foo\\bar").second
250  << " should be=\\\\conky\\\\mountpoint\\foo\\bar" << std::endl;
251  }
252 
253  if (strcmp(vpIoTools::splitDrive("//conky//mountpoint/foo/bar").first.c_str(), "") == 0) {
254  nbOk++;
255  } else {
256  nbFail++;
257  std::cout << "Fail=" << vpIoTools::splitDrive("//conky//mountpoint/foo/bar").first << " should be=" << std::endl;
258  }
259  if (strcmp(vpIoTools::splitDrive("//conky//mountpoint/foo/bar").second.c_str(), "//conky//mountpoint/foo/bar") == 0) {
260  nbOk++;
261  } else {
262  nbFail++;
263  std::cout << "Fail=" << vpIoTools::splitDrive("//conky//mountpoint/foo/bar").second
264  << " should be=//conky//mountpoint/foo/bar" << std::endl;
265  }
266 
267  std::cout << "Test vpIoTools::splitDrive (Win32) - passed: " << nbOk << "/" << (nbOk + nbFail) << std::endl;
268 
269  if (nbFail) {
270  std::cerr << "Failed test: vpIoTools::splitDrive (Win32)" << std::endl;
271  return EXIT_FAILURE;
272  }
273 #endif
274 
275 // Test vpIoTools::getFileExtension
276 #if defined(_WIN32)
277  nbFail = 0;
278  nbOk = 0;
279 
280  if (strcmp(vpIoTools::getFileExtension("foo.ext").c_str(), ".ext") == 0) {
281  nbOk++;
282  } else {
283  nbFail++;
284  std::cout << "Fail=" << vpIoTools::getFileExtension("foo.ext") << " should be=.ext" << std::endl;
285  }
286 
287  if (strcmp(vpIoTools::getFileExtension("/foo/foo.ext").c_str(), ".ext") == 0) {
288  nbOk++;
289  } else {
290  nbFail++;
291  std::cout << "Fail=" << vpIoTools::getFileExtension("/foo/foo.ext") << " should be=.ext" << std::endl;
292  }
293 
294  if (strcmp(vpIoTools::getFileExtension(".ext").c_str(), "") == 0) {
295  nbOk++;
296  } else {
297  nbFail++;
298  std::cout << "Fail=" << vpIoTools::getFileExtension(".ext") << " should be=" << std::endl;
299  }
300 
301  if (strcmp(vpIoTools::getFileExtension("\\foo.ext\\foo").c_str(), "") == 0) {
302  nbOk++;
303  } else {
304  nbFail++;
305  std::cout << "Fail=" << vpIoTools::getFileExtension("\\foo.ext\\foo") << " should be=" << std::endl;
306  }
307 
308  if (strcmp(vpIoTools::getFileExtension("foo.ext\\").c_str(), "") == 0) {
309  nbOk++;
310  } else {
311  nbFail++;
312  std::cout << "Fail=" << vpIoTools::getFileExtension("foo.ext\\") << " should be=" << std::endl;
313  }
314 
315  if (strcmp(vpIoTools::getFileExtension("").c_str(), "") == 0) {
316  nbOk++;
317  } else {
318  nbFail++;
319  std::cout << "Fail=" << vpIoTools::getFileExtension("") << " should be=" << std::endl;
320  }
321 
322  if (strcmp(vpIoTools::getFileExtension("foo.bar.ext").c_str(), ".ext") == 0) {
323  nbOk++;
324  } else {
325  nbFail++;
326  std::cout << "Fail=" << vpIoTools::getFileExtension("foo.bar.ext") << " should be=.ext" << std::endl;
327  }
328 
329  if (strcmp(vpIoTools::getFileExtension("xx/foo.bar.ext").c_str(), ".ext") == 0) {
330  nbOk++;
331  } else {
332  nbFail++;
333  std::cout << "Fail=" << vpIoTools::getFileExtension("xx/foo.bar.ext") << " should be=.ext" << std::endl;
334  }
335 
336  if (strcmp(vpIoTools::getFileExtension("xx\\foo.bar.ext").c_str(), ".ext") == 0) {
337  nbOk++;
338  } else {
339  nbFail++;
340  std::cout << "Fail=" << vpIoTools::getFileExtension("xx\\foo.bar.ext") << " should be=.ext" << std::endl;
341  }
342 
343  if (strcmp(vpIoTools::getFileExtension("c:a/b\\c.d").c_str(), ".d") == 0) {
344  nbOk++;
345  } else {
346  nbFail++;
347  std::cout << "Fail=" << vpIoTools::getFileExtension("c:a/b\\c.d") << " should be=.d" << std::endl;
348  }
349 
350  std::cout << "Test vpIoTools::getFileExtension (WIN32 platform) - passed: " << nbOk << "/" << (nbOk + nbFail)
351  << std::endl;
352 
353  if (nbFail) {
354  std::cerr << "Failed test: vpIoTools::getFileExtension (WIN32 platform)" << std::endl;
355  return EXIT_FAILURE;
356  }
357 #else
358  nbFail = 0;
359  nbOk = 0;
360 
361  if (strcmp(vpIoTools::getFileExtension("foo.bar").c_str(), ".bar") == 0) {
362  nbOk++;
363  } else {
364  nbFail++;
365  std::cout << "Fail=" << vpIoTools::getFileExtension("foo.bar") << " should be=.bar" << std::endl;
366  }
367 
368  if (strcmp(vpIoTools::getFileExtension("foo.boo.bar").c_str(), ".bar") == 0) {
369  nbOk++;
370  } else {
371  nbFail++;
372  std::cout << "Fail=" << vpIoTools::getFileExtension("foo.boo.bar") << " should be=.bar" << std::endl;
373  }
374 
375  if (strcmp(vpIoTools::getFileExtension("foo.boo.biff.bar").c_str(), ".bar") == 0) {
376  nbOk++;
377  } else {
378  nbFail++;
379  std::cout << "Fail=" << vpIoTools::getFileExtension("foo.boo.biff.bar") << " should be=.bar" << std::endl;
380  }
381 
382  if (strcmp(vpIoTools::getFileExtension(".csh.rc").c_str(), ".rc") == 0) {
383  nbOk++;
384  } else {
385  nbFail++;
386  std::cout << "Fail=" << vpIoTools::getFileExtension(".csh.rc") << " should be=.rc" << std::endl;
387  }
388 
389  if (strcmp(vpIoTools::getFileExtension("nodots").c_str(), "") == 0) {
390  nbOk++;
391  } else {
392  nbFail++;
393  std::cout << "Fail=" << vpIoTools::getFileExtension("nodots") << " should be=" << std::endl;
394  }
395 
396  if (strcmp(vpIoTools::getFileExtension(".cshrc").c_str(), "") == 0) {
397  nbOk++;
398  } else {
399  nbFail++;
400  std::cout << "Fail=" << vpIoTools::getFileExtension(".cshrc") << " should be=" << std::endl;
401  }
402 
403  if (strcmp(vpIoTools::getFileExtension("...manydots").c_str(), "") == 0) {
404  nbOk++;
405  } else {
406  nbFail++;
407  std::cout << "Fail=" << vpIoTools::getFileExtension("...manydots") << " should be=" << std::endl;
408  }
409 
410  if (strcmp(vpIoTools::getFileExtension("...manydots.ext").c_str(), ".ext") == 0) {
411  nbOk++;
412  } else {
413  nbFail++;
414  std::cout << "Fail=" << vpIoTools::getFileExtension("...manydots.ext") << " should be=.ext" << std::endl;
415  }
416 
417  if (strcmp(vpIoTools::getFileExtension(".").c_str(), "") == 0) {
418  nbOk++;
419  } else {
420  nbFail++;
421  std::cout << "Fail=" << vpIoTools::getFileExtension(".") << " should be=" << std::endl;
422  }
423 
424  if (strcmp(vpIoTools::getFileExtension("..").c_str(), "") == 0) {
425  nbOk++;
426  } else {
427  nbFail++;
428  std::cout << "Fail=" << vpIoTools::getFileExtension("..") << " should be=" << std::endl;
429  }
430 
431  if (strcmp(vpIoTools::getFileExtension("........").c_str(), "") == 0) {
432  nbOk++;
433  } else {
434  nbFail++;
435  std::cout << "Fail=" << vpIoTools::getFileExtension("........") << " should be=" << std::endl;
436  }
437 
438  if (strcmp(vpIoTools::getFileExtension("").c_str(), "") == 0) {
439  nbOk++;
440  } else {
441  nbFail++;
442  std::cout << "Fail=" << vpIoTools::getFileExtension("") << " should be=" << std::endl;
443  }
444 
445  std::cout << "Test vpIoTools::getFileExtension (Unix-like platform) - passed: " << nbOk << "/" << (nbOk + nbFail)
446  << std::endl;
447 #endif
448 
449  // Test makeDirectory()
450  try {
451  std::string username, directory_filename;
452  vpIoTools::getUserName(username);
453 #if defined(_WIN32)
454  std::string tmp_dir = "C:/temp/" + username;
455 #elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
456  std::string tmp_dir = "/tmp/" + username;
457 #endif
458 #if defined(_WIN32)
459  directory_filename = tmp_dir + "/test_directory1/test directory 2/";
460 #elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
461  directory_filename = tmp_dir + "/test_directory1/test directory 2/";
462 #endif
463  vpIoTools::makeDirectory(directory_filename);
464  vpIoTools::makeDirectory(directory_filename);
465  std::cout << "Create directories: " << directory_filename
466  << " ; check: " << vpIoTools::checkDirectory(directory_filename) << std::endl;
467 
468 #if defined(_WIN32)
469  directory_filename = tmp_dir + "/test_directory1/test directory 3";
470 #elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
471  directory_filename = tmp_dir + "/test_directory1/test directory 3";
472 #endif
473  vpIoTools::makeDirectory(directory_filename);
474  std::cout << "Create directories: " << directory_filename
475  << " ; check: " << vpIoTools::checkDirectory(directory_filename) << std::endl;
476 
477 #if defined(_WIN32)
478  directory_filename = "C:\\temp/" + username + "\\test_directory1\\test directory 4";
479 #elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
480  directory_filename = "/tmp\\" + username + "\\test_directory1\\test directory 4";
481 #endif
482  vpIoTools::makeDirectory(directory_filename);
483  vpIoTools::makeDirectory(directory_filename);
484  std::cout << "Create directories: " << directory_filename
485  << " ; check: " << vpIoTools::checkDirectory(directory_filename) << std::endl;
486 
487 #if defined(_WIN32)
488  directory_filename = "C:\\temp/" + username + "\\test_directory1\\test directory 5 . dir/test directory 6";
489 #elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
490  directory_filename = "/tmp\\" + username + "\\test_directory1\\test directory 5 . dir/test directory 6";
491 #endif
492  vpIoTools::makeDirectory(directory_filename);
493  std::cout << "Create directories: " << directory_filename
494  << " ; check: " << vpIoTools::checkDirectory(directory_filename) << std::endl;
495 
496  //Delete test directory
497  if (!vpIoTools::remove(tmp_dir + "/test_directory1")) {
498  std::cerr << "Cannot remove directory: " << tmp_dir << "/test_directory1" << std::endl;
499  }
500  } catch (const vpException &e) {
501  std::cerr << "Exception: " << e.what() << std::endl;
502  return EXIT_FAILURE;
503  }
504 
505  // Get the user login name
506  std::string username = "";
507  vpIoTools::getUserName(username);
508  std::ofstream dummy_file;
509 
510 // Test isSamePathname()
511 #if defined(_WIN32)
512  std::string path1 = "tmp/test/file.txt";
513  std::string path2 = "tmp/test/../test/file.txt";
514 
515  nbOk = 0;
516  nbFail = 0;
517  bool res;
518 
519  res = vpIoTools::isSamePathname(path1, path2); // True
520  std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
521  nbOk = res ? nbOk + 1 : nbOk;
522  nbFail = res ? nbFail : nbFail + 1;
523 
524  path1 = ".\\tmp/test/file.txt";
525  res = vpIoTools::isSamePathname(path1, path2); // True
526  std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
527  nbOk = res ? nbOk + 1 : nbOk;
528  nbFail = res ? nbFail : nbFail + 1;
529 
530  path1 = ".\\tmp/test\\../fake dir/..\\test\\file.txt";
531  res = vpIoTools::isSamePathname(path1, path2); // True
532  std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
533  nbOk = res ? nbOk + 1 : nbOk;
534  nbFail = res ? nbFail : nbFail + 1;
535 
536  path2 = "/tmp/test/../test/file.txt";
537  res = vpIoTools::isSamePathname(path1, path2); // False
538  std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
539  nbOk = res ? nbOk : nbOk + 1;
540  nbFail = res ? nbFail + 1 : nbFail;
541 
542  std::cout << "Test vpIoTools::isSamePathname (WIN32 platform) - passed: " << nbOk << "/" << (nbOk + nbFail)
543  << std::endl;
544  if (nbFail) {
545  std::cerr << "Failed test: vpIoTools::isSamePathname (WIN32 platform)" << std::endl;
546  return EXIT_FAILURE;
547  }
548 #else
549  // realpath requires not fake path, so we create dummy file and directories
550 
551  vpIoTools::makeDirectory("/tmp/" + username + "/test");
552  vpIoTools::makeDirectory("/tmp/" + username + "/dummy dir");
553 
554  std::string path1 = "/tmp/" + username + "/test/file.txt";
555  std::string path2 = "/tmp/" + username + "/test/../test/file.txt";
556  dummy_file.open(path1.c_str());
557  if (!dummy_file.is_open()) {
558  return EXIT_SUCCESS;
559  }
560  dummy_file.close();
561 
562  nbOk = 0;
563  nbFail = 0;
564  bool res;
565 
566  res = vpIoTools::isSamePathname(path1, path2); // True
567  std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
568  nbOk = res ? nbOk + 1 : nbOk;
569  nbFail = res ? nbFail : nbFail + 1;
570 
571  path1 = "\\tmp/" + username + "/./test/file.txt";
572  res = vpIoTools::isSamePathname(path1, path2); // True
573  std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
574  nbOk = res ? nbOk + 1 : nbOk;
575  nbFail = res ? nbFail : nbFail + 1;
576 
577  path1 = "\\tmp/" + username + "/test\\../dummy dir/..\\test\\file.txt";
578  res = vpIoTools::isSamePathname(path1, path2); // True
579  std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
580  nbOk = res ? nbOk + 1 : nbOk;
581  nbFail = res ? nbFail : nbFail + 1;
582 
583  path2 = "/tmp/" + username + "/test/../test";
584  res = vpIoTools::isSamePathname(path1, path2); // False
585  std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
586  nbOk = res ? nbOk : nbOk + 1;
587  nbFail = res ? nbFail + 1 : nbFail;
588 
589  path1 = "/tmp/" + username + "/test/";
590  res = vpIoTools::isSamePathname(path1, path2); // True
591  std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
592  nbOk = res ? nbOk + 1 : nbOk;
593  nbFail = res ? nbFail : nbFail + 1;
594 
595  std::cout << "Test vpIoTools::isSamePathname (Unix platform) - passed: " << nbOk << "/" << (nbOk + nbFail)
596  << std::endl;
597 
598  //Delete test directory
599  if (!vpIoTools::remove("/tmp/" + username + "/test")) {
600  std::cerr << "Cannot remove directory: " << "/tmp/" << username << "/test" << std::endl;
601  }
602  if (!vpIoTools::remove("/tmp/" + username + "/dummy dir")) {
603  std::cerr << "Cannot remove directory: " << "/tmp/" << username << "/dummy dir" << std::endl;
604  }
605 
606  if (nbFail) {
607  std::cerr << "Failed test: vpIoTools::isSamePathname (Unix platform)" << std::endl;
608  return EXIT_FAILURE;
609  }
610 #endif
611 
612  // Test checkFilename()
613  vpIoTools::makeDirectory("/tmp/" + username + "/directory (1) with ' quote and spaces");
614  path1 = "/tmp/" + username +
615  "/directory (1) with ' quote and spaces/file with ' quote (1) and "
616  "spaces.txt";
617  dummy_file.open(path1.c_str());
618  if (!dummy_file.is_open()) {
619  return EXIT_SUCCESS;
620  }
621  dummy_file.close();
622 
623  if (!vpIoTools::checkFilename(path1)) {
624  std::cerr << "Problem with checkFilename(" << path1 << ")!" << std::endl;
625  return EXIT_FAILURE;
626  }
627  std::cout << "Test vpIoTools::checkFilename() is ok." << std::endl;
628 
629  //Delete test directory
630  if (!vpIoTools::remove("/tmp/" + username + "/directory (1) with ' quote and spaces")) {
631  std::cerr << "Cannot remove directory: " << "/tmp/" << username << "/directory (1) with ' quote and spaces" << std::endl;
632  }
633 
634  // Test endianness
635  {
636  std::string filename_endianness = vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "endianness/test_endianness_little_endian.bin");
637  std::ifstream file_endianness(filename_endianness.c_str(), std::ios::in | std::ios::binary);
638  if (file_endianness.is_open()) {
639  checkReadBinaryValue<short>(file_endianness, std::numeric_limits<short>::min());
640  checkReadBinaryValue<short>(file_endianness, std::numeric_limits<short>::max());
641 
642  checkReadBinaryValue<unsigned short>(file_endianness, std::numeric_limits<unsigned short>::min());
643  checkReadBinaryValue<unsigned short>(file_endianness, std::numeric_limits<unsigned short>::max());
644 
645  checkReadBinaryValue<int>(file_endianness, std::numeric_limits<int>::min());
646  checkReadBinaryValue<int>(file_endianness, std::numeric_limits<int>::max());
647 
648  checkReadBinaryValue<unsigned int>(file_endianness, std::numeric_limits<unsigned int>::min());
649  checkReadBinaryValue<unsigned int>(file_endianness, std::numeric_limits<unsigned int>::max());
650 
651  checkReadBinaryValue<float>(file_endianness, -std::numeric_limits<float>::max());
652  checkReadBinaryValue<float>(file_endianness, std::numeric_limits<float>::max());
653 
654  checkReadBinaryValue<double>(file_endianness, -std::numeric_limits<double>::max());
655  checkReadBinaryValue<double>(file_endianness, std::numeric_limits<double>::max());
656 
657  std::cout << "Test endianness is ok." << std::endl;
658  } else {
659  std::cout << "Cannot open file: " << filename_endianness << std::endl;
660  }
661  }
662 
663  std::cout << std::endl << "End" << std::endl;
664  return EXIT_SUCCESS;
665 }
Used to indicate that a value is not in the allowed range.
Definition: vpException.h:97
static bool remove(const char *filename)
Definition: vpIoTools.cpp:837
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:467
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1316
static bool isAbsolutePathname(const std::string &pathname)
Definition: vpIoTools.cpp:1582
static std::string getFileExtension(const std::string &pathname, const bool checkFile=false)
Definition: vpIoTools.cpp:1363
static void readBinaryValueLE(std::ifstream &file, int16_t &short_value)
Definition: vpIoTools.cpp:1864
static bool equal(double x, double y, double s=0.001)
Definition: vpMath.h:290
static const char separator
Definition: vpIoTools.h:187
error that can be emited by ViSP classes.
Definition: vpException.h:71
static std::string path(const char *pathname)
Definition: vpIoTools.cpp:941
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1477
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:597
static bool checkFilename(const char *filename)
Definition: vpIoTools.cpp:675
const char * what() const
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1541
static std::pair< std::string, std::string > splitDrive(const std::string &pathname)
Definition: vpIoTools.cpp:1626
static bool isSamePathname(const std::string &pathname1, const std::string &pathname2)
Definition: vpIoTools.cpp:1606
static std::string getUserName()
Definition: vpIoTools.cpp:298
static std::string getName(const std::string &pathname)
Definition: vpIoTools.cpp:1442