Visual Servoing Platform  version 3.1.0
testIoTools.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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  * Authors:
35  * Souriya Trinh
36  *
37  *****************************************************************************/
38 
45 #include <iostream>
46 #include <stdio.h>
47 #include <string.h>
48 #include <visp3/core/vpIoTools.h>
49 
50 int main(int argc, const char **argv)
51 {
52  const char c = vpIoTools::separator;
53  if (c == '\\') {
54  std::cout << "The directory separator character is '" << c << "' (Windows platform)." << std::endl;
55  } else {
56  std::cout << "The directory separator character is '" << c << "' (Unix like platform)." << std::endl;
57  }
58 
59 #if defined(_WIN32)
60  std::string pathname = "C:\\Program Files (x86)\\Java\\jre7";
61 #else
62  std::string pathname = "/usr/bin/java";
63 #endif
64 
65  std::cout << "Parent of " << pathname << " is " << vpIoTools::getParent(pathname) << std::endl;
66  std::cout << "Name of " << pathname << " is " << vpIoTools::getName(pathname) << std::endl;
67 
68  if (argc == 3 && std::string(argv[1]) == std::string("-i")) {
69  std::cout << "Parent of " << argv[2] << " is " << vpIoTools::getParent(argv[2]) << std::endl;
70  std::cout << "Name of " << argv[2] << " is " << vpIoTools::getName(argv[2]) << std::endl;
71  }
72 
73  std::string windowsPathnameStyle = "\\usr\\bin\\java";
74  std::cout << "Parent of " << windowsPathnameStyle << " is " << vpIoTools::getParent(windowsPathnameStyle)
75  << std::endl;
76  std::cout << "Name of " << windowsPathnameStyle << " is " << vpIoTools::getName(windowsPathnameStyle) << std::endl;
77 
78  std::string parent = "/usr/toto/", child = "\\blabla\\java";
79  std::cout << "parent=" << vpIoTools::path(parent) << " ; child=" << vpIoTools::path(child) << std::endl;
80  std::cout << "Create file path from parent=" << parent << " and child=" << child << " is "
81  << vpIoTools::createFilePath(parent, child) << std::endl;
82 
83  std::string expandPath = "~/Documents/fictional directory/fictional file";
84  std::cout << "Path for " << expandPath << " is " << vpIoTools::path(expandPath) << std::endl;
85 
86  std::cout << "Test get name with an empty pathname=" << vpIoTools::getName("") << std::endl;
87  std::cout << "Get parent with an empty pathname=" << vpIoTools::getParent("") << std::endl;
88  std::cout << "Get parent with a filename=" << vpIoTools::getParent("my_file.txt") << std::endl;
89  expandPath = "~/Documents/fictional dir/fictional file.txt";
90  std::cout << "Get name with a unix expand pathname " << expandPath << "=" << vpIoTools::getName(expandPath)
91  << std::endl;
92  std::cout << "Get parent with a unix expand pathname " << expandPath << "=" << vpIoTools::getParent(expandPath)
93  << std::endl;
94 
95  pathname = "c:/dir";
96  std::cout << "pathname=" << vpIoTools::splitDrive(pathname).first << " ; " << vpIoTools::splitDrive(pathname).second
97  << std::endl;
98 
99  std::cout << "isAbsolutePath of " << pathname << "=" << vpIoTools::isAbsolutePathname(pathname) << std::endl;
100 
101  pathname = "c:/dir/fictional directory/fictional file.txt";
102  std::cout << "isAbsolutePath of " << pathname << "=" << vpIoTools::isAbsolutePathname(pathname) << std::endl;
103 
104  pathname = "/home/user/Documents/fictional directory/fictional file.txt";
105  std::cout << "isAbsolutePath of " << pathname << "=" << vpIoTools::isAbsolutePathname(pathname) << std::endl;
106 
107  pathname = "~/Documents/fictional directory/fictional file.txt";
108  std::cout << "isAbsolutePath of " << pathname << "=" << vpIoTools::isAbsolutePathname(pathname) << std::endl;
109 
110  pathname = "fictional directory/fictional file.txt";
111  std::cout << "isAbsolutePath of " << pathname << "=" << vpIoTools::isAbsolutePathname(pathname) << std::endl;
112 
113  // Test vpIoTools::splitDrive
114  unsigned int nbFail = 0, nbOk = 0;
115 #if defined(_WIN32)
116  if (strcmp(vpIoTools::splitDrive("c:\\foo\\bar").first.c_str(), "c:") == 0) {
117  nbOk++;
118  } else {
119  nbFail++;
120  std::cout << "Fail=" << vpIoTools::splitDrive("c:\\foo\\bar").first << " should be=c:" << std::endl;
121  }
122  if (strcmp(vpIoTools::splitDrive("c:\\foo\\bar").second.c_str(), "\\foo\\bar") == 0) {
123  nbOk++;
124  } else {
125  nbFail++;
126  std::cout << "Fail=" << vpIoTools::splitDrive("c:\\foo\\bar").second << " should be=\\foo\\bar" << std::endl;
127  }
128 
129  if (strcmp(vpIoTools::splitDrive("c:/foo/bar").first.c_str(), "c:") == 0) {
130  nbOk++;
131  } else {
132  nbFail++;
133  std::cout << "Fail=" << vpIoTools::splitDrive("c:/foo/bar").first << " should be=c:" << std::endl;
134  }
135  if (strcmp(vpIoTools::splitDrive("c:/foo/bar").second.c_str(), "/foo/bar") == 0) {
136  nbOk++;
137  } else {
138  nbFail++;
139  std::cout << "Fail=" << vpIoTools::splitDrive("c:/foo/bar").second << " should be=/foo/bar" << std::endl;
140  }
141 
142  if (strcmp(vpIoTools::splitDrive("\\\\conky\\mountpoint\\foo\\bar").first.c_str(), "\\\\conky\\mountpoint") == 0) {
143  nbOk++;
144  } else {
145  nbFail++;
146  std::cout << "Fail=" << vpIoTools::splitDrive("\\\\conky\\mountpoint\\foo\\bar").first
147  << " should be=\\\\conky\\mountpoint" << std::endl;
148  }
149  if (strcmp(vpIoTools::splitDrive("\\\\conky\\mountpoint\\foo\\bar").second.c_str(), "\\foo\\bar") == 0) {
150  nbOk++;
151  } else {
152  nbFail++;
153  std::cout << "Fail=" << vpIoTools::splitDrive("\\\\conky\\mountpoint\\foo\\bar").second << " should be=\\foo\\bar"
154  << std::endl;
155  }
156 
157  if (strcmp(vpIoTools::splitDrive("//conky/mountpoint/foo/bar").first.c_str(), "//conky/mountpoint") == 0) {
158  nbOk++;
159  } else {
160  nbFail++;
161  std::cout << "Fail=" << vpIoTools::splitDrive("//conky/mountpoint/foo/bar").first << " should be=//conky/mountpoint"
162  << std::endl;
163  }
164  if (strcmp(vpIoTools::splitDrive("//conky/mountpoint/foo/bar").second.c_str(), "/foo/bar") == 0) {
165  nbOk++;
166  } else {
167  nbFail++;
168  std::cout << "Fail=" << vpIoTools::splitDrive("//conky/mountpoint/foo/bar").second << " should be=/foo/bar"
169  << std::endl;
170  }
171 
172  if (strcmp(vpIoTools::splitDrive("\\\\\\conky\\mountpoint\\foo\\bar").first.c_str(), "") == 0) {
173  nbOk++;
174  } else {
175  nbFail++;
176  std::cout << "Fail=" << vpIoTools::splitDrive("\\\\\\conky\\mountpoint\\foo\\bar").first
177  << " should be=" << std::endl;
178  }
179  if (strcmp(vpIoTools::splitDrive("\\\\\\conky\\mountpoint\\foo\\bar").second.c_str(),
180  "\\\\\\conky\\mountpoint\\foo\\bar") == 0) {
181  nbOk++;
182  } else {
183  nbFail++;
184  std::cout << "Fail=" << vpIoTools::splitDrive("\\\\\\conky\\mountpoint\\foo\\bar").second
185  << " should be=\\\\\\conky\\mountpoint\\foo\\bar" << std::endl;
186  }
187 
188  if (strcmp(vpIoTools::splitDrive("///conky/mountpoint/foo/bar").first.c_str(), "") == 0) {
189  nbOk++;
190  } else {
191  nbFail++;
192  std::cout << "Fail=" << vpIoTools::splitDrive("///conky/mountpoint/foo/bar").first << " should be=" << std::endl;
193  }
194  if (strcmp(vpIoTools::splitDrive("///conky/mountpoint/foo/bar").second.c_str(), "///conky/mountpoint/foo/bar") == 0) {
195  nbOk++;
196  } else {
197  nbFail++;
198  std::cout << "Fail=" << vpIoTools::splitDrive("///conky/mountpoint/foo/bar").second
199  << " should be=///conky/mountpoint/foo/bar" << std::endl;
200  }
201 
202  if (strcmp(vpIoTools::splitDrive("\\\\conky\\\\mountpoint\\foo\\bar").first.c_str(), "") == 0) {
203  nbOk++;
204  } else {
205  nbFail++;
206  std::cout << "Fail=" << vpIoTools::splitDrive("\\\\conky\\\\mountpoint\\foo\\bar").first
207  << " should be=" << std::endl;
208  }
209  if (strcmp(vpIoTools::splitDrive("\\\\conky\\\\mountpoint\\foo\\bar").second.c_str(),
210  "\\\\conky\\\\mountpoint\\foo\\bar") == 0) {
211  nbOk++;
212  } else {
213  nbFail++;
214  std::cout << "Fail=" << vpIoTools::splitDrive("\\\\conky\\\\mountpoint\\foo\\bar").second
215  << " should be=\\\\conky\\\\mountpoint\\foo\\bar" << std::endl;
216  }
217 
218  if (strcmp(vpIoTools::splitDrive("//conky//mountpoint/foo/bar").first.c_str(), "") == 0) {
219  nbOk++;
220  } else {
221  nbFail++;
222  std::cout << "Fail=" << vpIoTools::splitDrive("//conky//mountpoint/foo/bar").first << " should be=" << std::endl;
223  }
224  if (strcmp(vpIoTools::splitDrive("//conky//mountpoint/foo/bar").second.c_str(), "//conky//mountpoint/foo/bar") == 0) {
225  nbOk++;
226  } else {
227  nbFail++;
228  std::cout << "Fail=" << vpIoTools::splitDrive("//conky//mountpoint/foo/bar").second
229  << " should be=//conky//mountpoint/foo/bar" << std::endl;
230  }
231 
232  std::cout << "Test vpIoTools::splitDrive (Win32) - passed: " << nbOk << "/" << (nbOk + nbFail) << std::endl;
233 
234  if (nbFail) {
235  std::cerr << "Failed test: vpIoTools::splitDrive (Win32)" << std::endl;
236  return EXIT_FAILURE;
237  }
238 #endif
239 
240 // Test vpIoTools::getFileExtension
241 #if defined(_WIN32)
242  nbFail = 0;
243  nbOk = 0;
244 
245  if (strcmp(vpIoTools::getFileExtension("foo.ext").c_str(), ".ext") == 0) {
246  nbOk++;
247  } else {
248  nbFail++;
249  std::cout << "Fail=" << vpIoTools::getFileExtension("foo.ext") << " should be=.ext" << std::endl;
250  }
251 
252  if (strcmp(vpIoTools::getFileExtension("/foo/foo.ext").c_str(), ".ext") == 0) {
253  nbOk++;
254  } else {
255  nbFail++;
256  std::cout << "Fail=" << vpIoTools::getFileExtension("/foo/foo.ext") << " should be=.ext" << std::endl;
257  }
258 
259  if (strcmp(vpIoTools::getFileExtension(".ext").c_str(), "") == 0) {
260  nbOk++;
261  } else {
262  nbFail++;
263  std::cout << "Fail=" << vpIoTools::getFileExtension(".ext") << " should be=" << std::endl;
264  }
265 
266  if (strcmp(vpIoTools::getFileExtension("\\foo.ext\\foo").c_str(), "") == 0) {
267  nbOk++;
268  } else {
269  nbFail++;
270  std::cout << "Fail=" << vpIoTools::getFileExtension("\\foo.ext\\foo") << " should be=" << std::endl;
271  }
272 
273  if (strcmp(vpIoTools::getFileExtension("foo.ext\\").c_str(), "") == 0) {
274  nbOk++;
275  } else {
276  nbFail++;
277  std::cout << "Fail=" << vpIoTools::getFileExtension("foo.ext\\") << " should be=" << std::endl;
278  }
279 
280  if (strcmp(vpIoTools::getFileExtension("").c_str(), "") == 0) {
281  nbOk++;
282  } else {
283  nbFail++;
284  std::cout << "Fail=" << vpIoTools::getFileExtension("") << " should be=" << std::endl;
285  }
286 
287  if (strcmp(vpIoTools::getFileExtension("foo.bar.ext").c_str(), ".ext") == 0) {
288  nbOk++;
289  } else {
290  nbFail++;
291  std::cout << "Fail=" << vpIoTools::getFileExtension("foo.bar.ext") << " should be=.ext" << std::endl;
292  }
293 
294  if (strcmp(vpIoTools::getFileExtension("xx/foo.bar.ext").c_str(), ".ext") == 0) {
295  nbOk++;
296  } else {
297  nbFail++;
298  std::cout << "Fail=" << vpIoTools::getFileExtension("xx/foo.bar.ext") << " should be=.ext" << std::endl;
299  }
300 
301  if (strcmp(vpIoTools::getFileExtension("xx\\foo.bar.ext").c_str(), ".ext") == 0) {
302  nbOk++;
303  } else {
304  nbFail++;
305  std::cout << "Fail=" << vpIoTools::getFileExtension("xx\\foo.bar.ext") << " should be=.ext" << std::endl;
306  }
307 
308  if (strcmp(vpIoTools::getFileExtension("c:a/b\\c.d").c_str(), ".d") == 0) {
309  nbOk++;
310  } else {
311  nbFail++;
312  std::cout << "Fail=" << vpIoTools::getFileExtension("c:a/b\\c.d") << " should be=.d" << std::endl;
313  }
314 
315  std::cout << "Test vpIoTools::getFileExtension (WIN32 platform) - passed: " << nbOk << "/" << (nbOk + nbFail)
316  << std::endl;
317 
318  if (nbFail) {
319  std::cerr << "Failed test: vpIoTools::getFileExtension (WIN32 platform)" << std::endl;
320  return EXIT_FAILURE;
321  }
322 #else
323  nbFail = 0;
324  nbOk = 0;
325 
326  if (strcmp(vpIoTools::getFileExtension("foo.bar").c_str(), ".bar") == 0) {
327  nbOk++;
328  } else {
329  nbFail++;
330  std::cout << "Fail=" << vpIoTools::getFileExtension("foo.bar") << " should be=.bar" << std::endl;
331  }
332 
333  if (strcmp(vpIoTools::getFileExtension("foo.boo.bar").c_str(), ".bar") == 0) {
334  nbOk++;
335  } else {
336  nbFail++;
337  std::cout << "Fail=" << vpIoTools::getFileExtension("foo.boo.bar") << " should be=.bar" << std::endl;
338  }
339 
340  if (strcmp(vpIoTools::getFileExtension("foo.boo.biff.bar").c_str(), ".bar") == 0) {
341  nbOk++;
342  } else {
343  nbFail++;
344  std::cout << "Fail=" << vpIoTools::getFileExtension("foo.boo.biff.bar") << " should be=.bar" << std::endl;
345  }
346 
347  if (strcmp(vpIoTools::getFileExtension(".csh.rc").c_str(), ".rc") == 0) {
348  nbOk++;
349  } else {
350  nbFail++;
351  std::cout << "Fail=" << vpIoTools::getFileExtension(".csh.rc") << " should be=.rc" << std::endl;
352  }
353 
354  if (strcmp(vpIoTools::getFileExtension("nodots").c_str(), "") == 0) {
355  nbOk++;
356  } else {
357  nbFail++;
358  std::cout << "Fail=" << vpIoTools::getFileExtension("nodots") << " should be=" << std::endl;
359  }
360 
361  if (strcmp(vpIoTools::getFileExtension(".cshrc").c_str(), "") == 0) {
362  nbOk++;
363  } else {
364  nbFail++;
365  std::cout << "Fail=" << vpIoTools::getFileExtension(".cshrc") << " should be=" << std::endl;
366  }
367 
368  if (strcmp(vpIoTools::getFileExtension("...manydots").c_str(), "") == 0) {
369  nbOk++;
370  } else {
371  nbFail++;
372  std::cout << "Fail=" << vpIoTools::getFileExtension("...manydots") << " should be=" << std::endl;
373  }
374 
375  if (strcmp(vpIoTools::getFileExtension("...manydots.ext").c_str(), ".ext") == 0) {
376  nbOk++;
377  } else {
378  nbFail++;
379  std::cout << "Fail=" << vpIoTools::getFileExtension("...manydots.ext") << " should be=.ext" << std::endl;
380  }
381 
382  if (strcmp(vpIoTools::getFileExtension(".").c_str(), "") == 0) {
383  nbOk++;
384  } else {
385  nbFail++;
386  std::cout << "Fail=" << vpIoTools::getFileExtension(".") << " should be=" << std::endl;
387  }
388 
389  if (strcmp(vpIoTools::getFileExtension("..").c_str(), "") == 0) {
390  nbOk++;
391  } else {
392  nbFail++;
393  std::cout << "Fail=" << vpIoTools::getFileExtension("..") << " should be=" << std::endl;
394  }
395 
396  if (strcmp(vpIoTools::getFileExtension("........").c_str(), "") == 0) {
397  nbOk++;
398  } else {
399  nbFail++;
400  std::cout << "Fail=" << vpIoTools::getFileExtension("........") << " should be=" << std::endl;
401  }
402 
403  if (strcmp(vpIoTools::getFileExtension("").c_str(), "") == 0) {
404  nbOk++;
405  } else {
406  nbFail++;
407  std::cout << "Fail=" << vpIoTools::getFileExtension("") << " should be=" << std::endl;
408  }
409 
410  std::cout << "Test vpIoTools::getFileExtension (Unix-like platform) - passed: " << nbOk << "/" << (nbOk + nbFail)
411  << std::endl;
412 #endif
413 
414  // Test makeDirectory()
415  try {
416  std::string username, directory_filename;
417  vpIoTools::getUserName(username);
418 #if defined(_WIN32)
419  directory_filename = "C:/temp/" + username + "/test_directory1/test directory 2/";
420 #elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
421  directory_filename = "/tmp/" + username + "/test_directory1/test directory 2/";
422 #endif
423  vpIoTools::makeDirectory(directory_filename);
424  vpIoTools::makeDirectory(directory_filename);
425  std::cout << "Create directories: " << directory_filename
426  << " ; check: " << vpIoTools::checkDirectory(directory_filename) << std::endl;
427 
428 #if defined(_WIN32)
429  directory_filename = "C:/temp/" + username + "/test_directory1/test directory 3";
430 #elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
431  directory_filename = "/tmp/" + username + "/test_directory1/test directory 3";
432 #endif
433  vpIoTools::makeDirectory(directory_filename);
434  std::cout << "Create directories: " << directory_filename
435  << " ; check: " << vpIoTools::checkDirectory(directory_filename) << std::endl;
436 
437 #if defined(_WIN32)
438  directory_filename = "C:\\temp/" + username + "\\test_directory1\\test directory 4";
439 #elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
440  directory_filename = "/tmp\\" + username + "\\test_directory1\\test directory 4";
441 #endif
442  vpIoTools::makeDirectory(directory_filename);
443  vpIoTools::makeDirectory(directory_filename);
444  std::cout << "Create directories: " << directory_filename
445  << " ; check: " << vpIoTools::checkDirectory(directory_filename) << std::endl;
446 
447 #if defined(_WIN32)
448  directory_filename = "C:\\temp/" + username + "\\test_directory1\\test directory 5 . dir/test directory 6";
449 #elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
450  directory_filename = "/tmp\\" + username + "\\test_directory1\\test directory 5 . dir/test directory 6";
451 #endif
452  vpIoTools::makeDirectory(directory_filename);
453  std::cout << "Create directories: " << directory_filename
454  << " ; check: " << vpIoTools::checkDirectory(directory_filename) << std::endl;
455  } catch (const vpException &e) {
456  std::cerr << "Exception: " << e.what() << std::endl;
457  return EXIT_FAILURE;
458  }
459 
460  // Get the user login name
461  std::string username = "";
462  vpIoTools::getUserName(username);
463  std::ofstream dummy_file;
464 
465 // Test isSamePathname()
466 #if defined(_WIN32)
467  std::string path1 = "tmp/test/file.txt";
468  std::string path2 = "tmp/test/../test/file.txt";
469 
470  nbOk = 0;
471  nbFail = 0;
472  bool res;
473 
474  res = vpIoTools::isSamePathname(path1, path2); // True
475  std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
476  nbOk = res ? nbOk + 1 : nbOk;
477  nbFail = res ? nbFail : nbFail + 1;
478 
479  path1 = ".\\tmp/test/file.txt";
480  res = vpIoTools::isSamePathname(path1, path2); // True
481  std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
482  nbOk = res ? nbOk + 1 : nbOk;
483  nbFail = res ? nbFail : nbFail + 1;
484 
485  path1 = ".\\tmp/test\\../fake dir/..\\test\\file.txt";
486  res = vpIoTools::isSamePathname(path1, path2); // True
487  std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
488  nbOk = res ? nbOk + 1 : nbOk;
489  nbFail = res ? nbFail : nbFail + 1;
490 
491  path2 = "/tmp/test/../test/file.txt";
492  res = vpIoTools::isSamePathname(path1, path2); // False
493  std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
494  nbOk = res ? nbOk : nbOk + 1;
495  nbFail = res ? nbFail + 1 : nbFail;
496 
497  std::cout << "Test vpIoTools::isSamePathname (WIN32 platform) - passed: " << nbOk << "/" << (nbOk + nbFail)
498  << std::endl;
499  if (nbFail) {
500  std::cerr << "Failed test: vpIoTools::isSamePathname (WIN32 platform)" << std::endl;
501  return EXIT_FAILURE;
502  }
503 #else
504  // realpath requires not fake path, so we create dummy file and directories
505 
506  vpIoTools::makeDirectory("/tmp/" + username + "/test");
507  vpIoTools::makeDirectory("/tmp/" + username + "/dummy dir");
508 
509  std::string path1 = "/tmp/" + username + "/test/file.txt";
510  std::string path2 = "/tmp/" + username + "/test/../test/file.txt";
511  dummy_file.open(path1.c_str());
512  if (!dummy_file.is_open()) {
513  return EXIT_SUCCESS;
514  }
515  dummy_file.close();
516 
517  nbOk = 0;
518  nbFail = 0;
519  bool res;
520 
521  res = vpIoTools::isSamePathname(path1, path2); // True
522  std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
523  nbOk = res ? nbOk + 1 : nbOk;
524  nbFail = res ? nbFail : nbFail + 1;
525 
526  path1 = "\\tmp/" + username + "/./test/file.txt";
527  res = vpIoTools::isSamePathname(path1, path2); // True
528  std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
529  nbOk = res ? nbOk + 1 : nbOk;
530  nbFail = res ? nbFail : nbFail + 1;
531 
532  path1 = "\\tmp/" + username + "/test\\../dummy dir/..\\test\\file.txt";
533  res = vpIoTools::isSamePathname(path1, path2); // True
534  std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
535  nbOk = res ? nbOk + 1 : nbOk;
536  nbFail = res ? nbFail : nbFail + 1;
537 
538  path2 = "/tmp/" + username + "/test/../test";
539  res = vpIoTools::isSamePathname(path1, path2); // False
540  std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
541  nbOk = res ? nbOk : nbOk + 1;
542  nbFail = res ? nbFail + 1 : nbFail;
543 
544  path1 = "/tmp/" + username + "/test/";
545  res = vpIoTools::isSamePathname(path1, path2); // True
546  std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
547  nbOk = res ? nbOk + 1 : nbOk;
548  nbFail = res ? nbFail : nbFail + 1;
549 
550  std::cout << "Test vpIoTools::isSamePathname (Unix platform) - passed: " << nbOk << "/" << (nbOk + nbFail)
551  << std::endl;
552  if (nbFail) {
553  std::cerr << "Failed test: vpIoTools::isSamePathname (Unix platform)" << std::endl;
554  return EXIT_FAILURE;
555  }
556 #endif
557 
558  // Test checkFilename()
559  vpIoTools::makeDirectory("/tmp/" + username + "/directory (1) with ' quote and spaces");
560  path1 = "/tmp/" + username +
561  "/directory (1) with ' quote and spaces/file with ' quote (1) and "
562  "spaces.txt";
563  dummy_file.open(path1.c_str());
564  if (!dummy_file.is_open()) {
565  return EXIT_SUCCESS;
566  }
567  dummy_file.close();
568 
569  if (!vpIoTools::checkFilename(path1)) {
570  std::cerr << "Problem with checkFilename(" << path1 << ")!" << std::endl;
571  return EXIT_FAILURE;
572  }
573  std::cout << "Test vpIoTools::checkFilename() is ok." << std::endl;
574 
575  std::cout << std::endl << "End" << std::endl;
576  return EXIT_SUCCESS;
577 }
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:367
static bool isAbsolutePathname(const std::string &pathname)
Definition: vpIoTools.cpp:1476
static std::string getFileExtension(const std::string &pathname, const bool checkFile=false)
Definition: vpIoTools.cpp:1257
static const char separator
Definition: vpIoTools.h:186
error that can be emited by ViSP classes.
Definition: vpException.h:71
static std::string path(const char *pathname)
Definition: vpIoTools.cpp:839
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1371
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:495
static bool checkFilename(const char *filename)
Definition: vpIoTools.cpp:573
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1435
static std::pair< std::string, std::string > splitDrive(const std::string &pathname)
Definition: vpIoTools.cpp:1520
static bool isSamePathname(const std::string &pathname1, const std::string &pathname2)
Definition: vpIoTools.cpp:1500
static std::string getUserName()
Definition: vpIoTools.cpp:198
static std::string getName(const std::string &pathname)
Definition: vpIoTools.cpp:1336
const char * what() const