Visual Servoing Platform  version 3.4.0
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 << std::endl;
466 
467  if (! vpIoTools::checkDirectory(directory_filename)) {
468  std::cerr << "Error: " << directory_filename << " is not a directory" << std::endl;
469  return EXIT_FAILURE;
470  }
471 
472 #if defined(_WIN32)
473  directory_filename = tmp_dir + "/test_directory1/test directory 3";
474 #elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
475  directory_filename = tmp_dir + "/test_directory1/test directory 3";
476 #endif
477  vpIoTools::makeDirectory(directory_filename);
478  std::cout << "Create directories: " << directory_filename << std::endl;
479 
480  if (! vpIoTools::checkDirectory(directory_filename)) {
481  std::cerr << "Error: " << directory_filename << " is not a directory" << std::endl;
482  return EXIT_FAILURE;
483  }
484 
485 #if defined(_WIN32)
486  directory_filename = "C:\\temp/" + username + "\\test_directory1\\test directory 4";
487 #elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
488  directory_filename = "/tmp\\" + username + "\\test_directory1\\test directory 4";
489 #endif
490  vpIoTools::makeDirectory(directory_filename);
491  vpIoTools::makeDirectory(directory_filename);
492  std::cout << "Create directories: " << directory_filename << std::endl;
493 
494  if (! vpIoTools::checkDirectory(directory_filename)) {
495  std::cerr << "Error: " << directory_filename << " is not a directory" << std::endl;
496  return EXIT_FAILURE;
497  }
498 
499 #if defined(_WIN32)
500  directory_filename = "C:\\temp/" + username + "\\test_directory1\\test directory 5 . dir/test directory 6";
501 #elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
502  directory_filename = "/tmp\\" + username + "\\test_directory1\\test directory 5 . dir/test directory 6";
503 #endif
504  vpIoTools::makeDirectory(directory_filename);
505  std::cout << "Create directories: " << directory_filename << std::endl;
506 
507  if (! vpIoTools::checkDirectory(directory_filename)) {
508  std::cerr << "Error: " << directory_filename << " is not a directory" << std::endl;
509  return EXIT_FAILURE;
510  }
511 
512  //Delete test directory
513  if (!vpIoTools::remove(tmp_dir + "/test_directory1")) {
514  std::cerr << "Error: cannot remove directory: " << tmp_dir << "/test_directory1" << std::endl;
515  return EXIT_FAILURE;
516  }
517  } catch (const vpException &e) {
518  std::cerr << "Exception: " << e.what() << std::endl;
519  return EXIT_FAILURE;
520  }
521 
522  // Test FIFO only implemented on unix like OS
523 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
524  try {
525  std::string username, fifo_file;
526  vpIoTools::getUserName(username);
527  std::string fifo_tmp_dir = "/tmp/" + username + "/fifo_test_directory";
528 
529  if (!vpIoTools::checkDirectory(fifo_tmp_dir)) {
530  vpIoTools::makeDirectory(fifo_tmp_dir);
531  }
532 
533  // Test 1
534  fifo_file = fifo_tmp_dir + "/" + "fifo_testfile";
535 
536  vpIoTools::makeFifo(fifo_file);
537  std::cout << "Create fifo file: " << fifo_file << std::endl;
538 
539  if (! vpIoTools::checkFifo(fifo_file)) {
540  std::cerr << "Error: file " << fifo_file << " is not a fifo file as expected" << std::endl;
541  return EXIT_FAILURE;
542  }
543 
544  // Delete test file and directory
545  if (!vpIoTools::remove(fifo_file)) {
546  std::cerr << "Error: cannot remove fifo: " << fifo_file << std::endl;
547  return EXIT_FAILURE;
548  }
549  if (!vpIoTools::remove(fifo_tmp_dir)) {
550  std::cerr << "Error: cannot remove directory: " << fifo_tmp_dir << std::endl;
551  return EXIT_FAILURE;
552  }
553 
554  } catch (const vpException &e) {
555  std::cerr << "Catch an exception: " << e.what() << std::endl;
556  return EXIT_FAILURE;
557  }
558 #endif
559 
560  // Test makeTempDirectory()
561 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
562  try {
563  std::string username, directory_filename_tmp;
564  vpIoTools::getUserName(username);
565  std::string tmp_dir = "/tmp/" + username;
566 
567  // Test 1
568  directory_filename_tmp = tmp_dir + "/" + "vpIoTools_test_XXXXXX";
569 
570  std::string converted_dirname_tmp = vpIoTools::makeTempDirectory(directory_filename_tmp);
571 
572  std::cout << "Create temp directory: " << converted_dirname_tmp << std::endl;
573 
574  if (! vpIoTools::checkDirectory(converted_dirname_tmp)) {
575  std::cerr << "Error: " << converted_dirname_tmp << " is not a tmp directory" << std::endl;
576  return EXIT_FAILURE;
577  }
578 
579  // Delete test directory
580  if (!vpIoTools::remove(converted_dirname_tmp)) {
581  std::cerr << "Error: cannot remove temp directory: " << converted_dirname_tmp << std::endl;
582  return EXIT_FAILURE;
583  }
584 
585  // Test 2
586  vpIoTools::makeDirectory(tmp_dir);
587  converted_dirname_tmp = vpIoTools::makeTempDirectory(tmp_dir);
588 
589  std::cout << "Create temp directory: " << converted_dirname_tmp << std::endl;
590 
591  if (! vpIoTools::checkDirectory(converted_dirname_tmp)) {
592  std::cerr << "Error: " << converted_dirname_tmp << " is not a temp directory" << std::endl;
593  return EXIT_FAILURE;
594  }
595 
596  // Delete test directory
597  if (!vpIoTools::remove(converted_dirname_tmp)) {
598  std::cerr << "Cannot remove directory: " << converted_dirname_tmp << std::endl;
599  return EXIT_FAILURE;
600  }
601 
602  } catch (const vpException &e) {
603  std::cerr << "Catch an exception: " << e.what() << std::endl;
604  return EXIT_FAILURE;
605  }
606 #endif
607 
608  // Get the user login name
609  std::string username = vpIoTools::getUserName();
610  std::ofstream dummy_file;
611 
612 // Test isSamePathname()
613 #if defined(_WIN32)
614  std::string path1 = "tmp/test/file.txt";
615  std::string path2 = "tmp/test/../test/file.txt";
616 
617  nbOk = 0;
618  nbFail = 0;
619  bool res;
620 
621  res = vpIoTools::isSamePathname(path1, path2); // True
622  std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
623  nbOk = res ? nbOk + 1 : nbOk;
624  nbFail = res ? nbFail : nbFail + 1;
625 
626  path1 = ".\\tmp/test/file.txt";
627  res = vpIoTools::isSamePathname(path1, path2); // True
628  std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
629  nbOk = res ? nbOk + 1 : nbOk;
630  nbFail = res ? nbFail : nbFail + 1;
631 
632  path1 = ".\\tmp/test\\../fake dir/..\\test\\file.txt";
633  res = vpIoTools::isSamePathname(path1, path2); // True
634  std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
635  nbOk = res ? nbOk + 1 : nbOk;
636  nbFail = res ? nbFail : nbFail + 1;
637 
638  path2 = "/tmp/test/../test/file.txt";
639  res = vpIoTools::isSamePathname(path1, path2); // False
640  std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
641  nbOk = res ? nbOk : nbOk + 1;
642  nbFail = res ? nbFail + 1 : nbFail;
643 
644  std::cout << "Test vpIoTools::isSamePathname (WIN32 platform) - passed: " << nbOk << "/" << (nbOk + nbFail)
645  << std::endl;
646  if (nbFail) {
647  std::cerr << "Failed test: vpIoTools::isSamePathname (WIN32 platform)" << std::endl;
648  return EXIT_FAILURE;
649  }
650 #else
651  // realpath requires not fake path, so we create dummy file and directories
652 
653  vpIoTools::makeDirectory("/tmp/" + username + "/test");
654  vpIoTools::makeDirectory("/tmp/" + username + "/dummy dir");
655 
656  std::string path1 = "/tmp/" + username + "/test/file.txt";
657  std::string path2 = "/tmp/" + username + "/test/../test/file.txt";
658  dummy_file.open(path1.c_str());
659  if (!dummy_file.is_open()) {
660  return EXIT_SUCCESS;
661  }
662  dummy_file.close();
663 
664  nbOk = 0;
665  nbFail = 0;
666  bool res;
667 
668  res = vpIoTools::isSamePathname(path1, path2); // True
669  std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
670  nbOk = res ? nbOk + 1 : nbOk;
671  nbFail = res ? nbFail : nbFail + 1;
672 
673  path1 = "\\tmp/" + username + "/./test/file.txt";
674  res = vpIoTools::isSamePathname(path1, path2); // True
675  std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
676  nbOk = res ? nbOk + 1 : nbOk;
677  nbFail = res ? nbFail : nbFail + 1;
678 
679  path1 = "\\tmp/" + username + "/test\\../dummy dir/..\\test\\file.txt";
680  res = vpIoTools::isSamePathname(path1, path2); // True
681  std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
682  nbOk = res ? nbOk + 1 : nbOk;
683  nbFail = res ? nbFail : nbFail + 1;
684 
685  path2 = "/tmp/" + username + "/test/../test";
686  res = vpIoTools::isSamePathname(path1, path2); // False
687  std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
688  nbOk = res ? nbOk : nbOk + 1;
689  nbFail = res ? nbFail + 1 : nbFail;
690 
691  path1 = "/tmp/" + username + "/test/";
692  res = vpIoTools::isSamePathname(path1, path2); // True
693  std::cout << "vpIoTools::isSamePathname(" << path1 << ", " << path2 << ")? " << res << std::endl;
694  nbOk = res ? nbOk + 1 : nbOk;
695  nbFail = res ? nbFail : nbFail + 1;
696 
697  std::cout << "Test vpIoTools::isSamePathname (Unix platform) - passed: " << nbOk << "/" << (nbOk + nbFail)
698  << std::endl;
699 
700  //Delete test directory
701  if (!vpIoTools::remove("/tmp/" + username + "/test")) {
702  std::cerr << "Cannot remove directory: " << "/tmp/" << username << "/test" << std::endl;
703  }
704  if (!vpIoTools::remove("/tmp/" + username + "/dummy dir")) {
705  std::cerr << "Cannot remove directory: " << "/tmp/" << username << "/dummy dir" << std::endl;
706  }
707 
708  if (nbFail) {
709  std::cerr << "Failed test: vpIoTools::isSamePathname (Unix platform)" << std::endl;
710  return EXIT_FAILURE;
711  }
712 #endif
713 
714  // Test checkFilename()
715  vpIoTools::makeDirectory("/tmp/" + username + "/directory (1) with ' quote and spaces");
716  path1 = "/tmp/" + username +
717  "/directory (1) with ' quote and spaces/file with ' quote (1) and "
718  "spaces.txt";
719  dummy_file.open(path1.c_str());
720  if (!dummy_file.is_open()) {
721  return EXIT_SUCCESS;
722  }
723  dummy_file.close();
724 
725  if (!vpIoTools::checkFilename(path1)) {
726  std::cerr << "Problem with checkFilename(" << path1 << ")!" << std::endl;
727  return EXIT_FAILURE;
728  }
729  std::cout << "Test vpIoTools::checkFilename() is ok." << std::endl;
730 
731  //Delete test directory
732  if (!vpIoTools::remove("/tmp/" + username + "/directory (1) with ' quote and spaces")) {
733  std::cerr << "Cannot remove directory: " << "/tmp/" << username << "/directory (1) with ' quote and spaces" << std::endl;
734  }
735 
736  // Test endianness
737  {
738  std::string filename_endianness = vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "endianness/test_endianness_little_endian.bin");
739  std::ifstream file_endianness(filename_endianness.c_str(), std::ios::in | std::ios::binary);
740  if (file_endianness.is_open()) {
741  checkReadBinaryValue<short>(file_endianness, std::numeric_limits<short>::min());
742  checkReadBinaryValue<short>(file_endianness, std::numeric_limits<short>::max());
743 
744  checkReadBinaryValue<unsigned short>(file_endianness, std::numeric_limits<unsigned short>::min());
745  checkReadBinaryValue<unsigned short>(file_endianness, std::numeric_limits<unsigned short>::max());
746 
747  checkReadBinaryValue<int>(file_endianness, std::numeric_limits<int>::min());
748  checkReadBinaryValue<int>(file_endianness, std::numeric_limits<int>::max());
749 
750  checkReadBinaryValue<unsigned int>(file_endianness, std::numeric_limits<unsigned int>::min());
751  checkReadBinaryValue<unsigned int>(file_endianness, std::numeric_limits<unsigned int>::max());
752 
753  checkReadBinaryValue<float>(file_endianness, -std::numeric_limits<float>::max());
754  checkReadBinaryValue<float>(file_endianness, std::numeric_limits<float>::max());
755 
756  checkReadBinaryValue<double>(file_endianness, -std::numeric_limits<double>::max());
757  checkReadBinaryValue<double>(file_endianness, std::numeric_limits<double>::max());
758 
759  std::cout << "Test endianness is ok." << std::endl;
760  } else {
761  std::cout << "Cannot open file: " << filename_endianness << std::endl;
762  }
763  }
764 
765  std::cout << std::endl << "Test succeed" << std::endl;
766  return EXIT_SUCCESS;
767 }
Used to indicate that a value is not in the allowed range.
Definition: vpException.h:97
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:482
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1202
static bool isAbsolutePathname(const std::string &pathname)
Definition: vpIoTools.cpp:1487
static void readBinaryValueLE(std::ifstream &file, int16_t &short_value)
Definition: vpIoTools.cpp:1769
static bool equal(double x, double y, double s=0.001)
Definition: vpMath.h:293
static const char separator
Definition: vpIoTools.h:185
error that can be emited by ViSP classes.
Definition: vpException.h:71
static std::string getFileExtension(const std::string &pathname, bool checkFile=false)
Definition: vpIoTools.cpp:1267
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1382
static bool checkFifo(const std::string &filename)
Definition: vpIoTools.cpp:392
static std::string path(const std::string &pathname)
Definition: vpIoTools.cpp:841
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:332
const char * what() const
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1446
static std::pair< std::string, std::string > splitDrive(const std::string &pathname)
Definition: vpIoTools.cpp:1531
static bool isSamePathname(const std::string &pathname1, const std::string &pathname2)
Definition: vpIoTools.cpp:1511
static void makeFifo(const std::string &dirname)
Definition: vpIoTools.cpp:533
static std::string getUserName()
Definition: vpIoTools.cpp:228
static std::string getName(const std::string &pathname)
Definition: vpIoTools.cpp:1347
static bool remove(const std::string &filename)
Definition: vpIoTools.cpp:765
static std::string makeTempDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:581
static bool checkFilename(const std::string &filename)
Definition: vpIoTools.cpp:640