ViSP
testIoTools.cpp
1 /****************************************************************************
2  *
3  * $Id: testIoTools.cpp 5210 2015-01-26 10:51:11Z strinh $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Test functions in vpIoTools.
36  *
37  * Authors:
38  * Souriya Trinh
39  *
40  *****************************************************************************/
41 
50 #include <stdio.h>
51 #include <string.h>
52 #include <iostream>
53 #include <visp/vpIoTools.h>
54 
55 
56 int
57 main(int argc, const char ** argv)
58 {
59  const char c = vpIoTools::separator;
60  if(c == '\\')
61  {
62  std::cout << "The directory separator character is '" << c << "' (Windows platform)." << std::endl;
63  }
64  else {
65  std::cout << "The directory separator character is '" << c << "' (Unix like platform)." << std::endl;
66  }
67 
68 
69  std::string pathname = "";
70 #if defined(_WIN32)
71  pathname = "C:\\Program Files (x86)\\Java\\jre7";
72 #else
73  pathname = "/usr/bin/java";
74 #endif
75 
76  std::cout << "Parent of " << pathname << " is " << vpIoTools::getParent(pathname) << std::endl;
77  std::cout << "Name of " << pathname << " is " << vpIoTools::getName(pathname) << std::endl;
78 
79 
80  if(argc == 3 && std::string(argv[1]) == std::string("-i"))
81  {
82  std::cout << "Parent of " << argv[2] << " is " << vpIoTools::getParent(argv[2]) << std::endl;
83  std::cout << "Name of " << argv[2] << " is " << vpIoTools::getName(argv[2]) << std::endl;
84  }
85 
86  std::string windowsPathnameStyle = "\\usr\\bin\\java";
87  std::cout << "Parent of " << windowsPathnameStyle << " is " << vpIoTools::getParent(windowsPathnameStyle) << std::endl;
88  std::cout << "Name of " << windowsPathnameStyle << " is " << vpIoTools::getName(windowsPathnameStyle) << std::endl;
89 
90  std::string parent = "/usr/toto/", child = "\\blabla\\java";
91  std::cout << "parent=" << vpIoTools::path(parent) << " ; child=" << vpIoTools::path(child) << std::endl;
92  std::cout << "Create file path from parent=" << parent << " and child=" << child << " is "
93  << vpIoTools::createFilePath(parent, child) << std::endl;
94 
95  std::string expandPath = "~/Documents/fictional directory/fictional file";
96  std::cout << "Path for " << expandPath << " is " << vpIoTools::path(expandPath) << std::endl;
97 
98  std::cout << "Test get name with an empty pathname=" << vpIoTools::getName("") << std::endl;
99  std::cout << "Get parent with an empty pathname=" << vpIoTools::getParent("") << std::endl;
100  std::cout << "Get parent with a filename=" << vpIoTools::getParent("my_file.txt") << std::endl;
101  expandPath = "~/Documents/fictional dir/fictional file.txt";
102  std::cout << "Get name with a unix expand pathname " << expandPath << "=" << vpIoTools::getName(expandPath) << std::endl;
103  std::cout << "Get parent with a unix expand pathname " << expandPath << "=" << vpIoTools::getParent(expandPath) << std::endl;
104 
105 
106  pathname = "c:/dir";
107  std::cout << "pathname=" << vpIoTools::splitDrive(pathname).first << " ; " << vpIoTools::splitDrive(pathname).second << std::endl;
108 
109  std::cout << "isAbsolutePath of " << pathname << "=" << vpIoTools::isAbsolutePathname(pathname) << std::endl;
110 
111  pathname = "c:/dir/fictional directory/fictional file.txt";
112  std::cout << "isAbsolutePath of " << pathname << "=" << vpIoTools::isAbsolutePathname(pathname) << std::endl;
113 
114  pathname = "/home/user/Documents/fictional directory/fictional file.txt";
115  std::cout << "isAbsolutePath of " << pathname << "=" << vpIoTools::isAbsolutePathname(pathname) << std::endl;
116 
117  pathname = "~/Documents/fictional directory/fictional file.txt";
118  std::cout << "isAbsolutePath of " << pathname << "=" << vpIoTools::isAbsolutePathname(pathname) << std::endl;
119 
120  pathname = "fictional directory/fictional file.txt";
121  std::cout << "isAbsolutePath of " << pathname << "=" << vpIoTools::isAbsolutePathname(pathname) << std::endl;
122 
123 
124  //Test vpIoTools::splitDrive
125  unsigned int nbFail = 0, nbOk = 0;
126 #if defined(_WIN32)
127  if(strcmp(vpIoTools::splitDrive("c:\\foo\\bar").first.c_str(), "c:") == 0) {
128  nbOk++;
129  }
130  else {
131  nbFail++;
132  std::cout << "Fail=" << vpIoTools::splitDrive("c:\\foo\\bar").first << " should be=c:" << std::endl;
133  }
134  if(strcmp(vpIoTools::splitDrive("c:\\foo\\bar").second.c_str(), "\\foo\\bar") == 0) {
135  nbOk++;
136  }
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("c:/foo/bar").first.c_str(), "c:") == 0) {
143  nbOk++;
144  }
145  else {
146  nbFail++;
147  std::cout << "Fail=" << vpIoTools::splitDrive("c:/foo/bar").first << " should be=c:" << std::endl;
148  }
149  if(strcmp(vpIoTools::splitDrive("c:/foo/bar").second.c_str(), "/foo/bar") == 0) {
150  nbOk++;
151  }
152  else {
153  nbFail++;
154  std::cout << "Fail=" << vpIoTools::splitDrive("c:/foo/bar").second << " should be=/foo/bar" << std::endl;
155  }
156 
157  if(strcmp(vpIoTools::splitDrive("\\\\conky\\mountpoint\\foo\\bar").first.c_str(), "\\\\conky\\mountpoint") == 0) {
158  nbOk++;
159  }
160  else {
161  nbFail++;
162  std::cout << "Fail=" << vpIoTools::splitDrive("\\\\conky\\mountpoint\\foo\\bar").first << " should be=\\\\conky\\mountpoint" << std::endl;
163  }
164  if(strcmp(vpIoTools::splitDrive("\\\\conky\\mountpoint\\foo\\bar").second.c_str(), "\\foo\\bar") == 0) {
165  nbOk++;
166  }
167  else {
168  nbFail++;
169  std::cout << "Fail=" << vpIoTools::splitDrive("\\\\conky\\mountpoint\\foo\\bar").second << " should be=\\foo\\bar" << std::endl;
170  }
171 
172  if(strcmp(vpIoTools::splitDrive("//conky/mountpoint/foo/bar").first.c_str(), "//conky/mountpoint") == 0) {
173  nbOk++;
174  }
175  else {
176  nbFail++;
177  std::cout << "Fail=" << vpIoTools::splitDrive("//conky/mountpoint/foo/bar").first << " should be=//conky/mountpoint" << std::endl;
178  }
179  if(strcmp(vpIoTools::splitDrive("//conky/mountpoint/foo/bar").second.c_str(), "/foo/bar") == 0) {
180  nbOk++;
181  }
182  else {
183  nbFail++;
184  std::cout << "Fail=" << vpIoTools::splitDrive("//conky/mountpoint/foo/bar").second << " should be=/foo/bar" << std::endl;
185  }
186 
187  if(strcmp(vpIoTools::splitDrive("\\\\\\conky\\mountpoint\\foo\\bar").first.c_str(), "") == 0) {
188  nbOk++;
189  }
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(),
195  "\\\\\\conky\\mountpoint\\foo\\bar") == 0) {
196  nbOk++;
197  }
198  else {
199  nbFail++;
200  std::cout << "Fail=" << vpIoTools::splitDrive("\\\\\\conky\\mountpoint\\foo\\bar").second << " should be=\\\\\\conky\\mountpoint\\foo\\bar" << std::endl;
201  }
202 
203  if(strcmp(vpIoTools::splitDrive("///conky/mountpoint/foo/bar").first.c_str(), "") == 0) {
204  nbOk++;
205  }
206  else {
207  nbFail++;
208  std::cout << "Fail=" << vpIoTools::splitDrive("///conky/mountpoint/foo/bar").first << " should be=" << std::endl;
209  }
210  if(strcmp(vpIoTools::splitDrive("///conky/mountpoint/foo/bar").second.c_str(), "///conky/mountpoint/foo/bar") == 0) {
211  nbOk++;
212  }
213  else {
214  nbFail++;
215  std::cout << "Fail=" << vpIoTools::splitDrive("///conky/mountpoint/foo/bar").second << " 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  }
221  else {
222  nbFail++;
223  std::cout << "Fail=" << vpIoTools::splitDrive("\\\\conky\\\\mountpoint\\foo\\bar").first << " should be=" << std::endl;
224  }
225  if(strcmp(vpIoTools::splitDrive("\\\\conky\\\\mountpoint\\foo\\bar").second.c_str(),
226  "\\\\conky\\\\mountpoint\\foo\\bar") == 0) {
227  nbOk++;
228  }
229  else {
230  nbFail++;
231  std::cout << "Fail=" << vpIoTools::splitDrive("\\\\conky\\\\mountpoint\\foo\\bar").second << " should be=\\\\conky\\\\mountpoint\\foo\\bar" << std::endl;
232  }
233 
234  if(strcmp(vpIoTools::splitDrive("//conky//mountpoint/foo/bar").first.c_str(), "") == 0) {
235  nbOk++;
236  }
237  else {
238  nbFail++;
239  std::cout << "Fail=" << vpIoTools::splitDrive("//conky//mountpoint/foo/bar").first << " should be=" << std::endl;
240  }
241  if(strcmp(vpIoTools::splitDrive("//conky//mountpoint/foo/bar").second.c_str(), "//conky//mountpoint/foo/bar") == 0) {
242  nbOk++;
243  }
244  else {
245  nbFail++;
246  std::cout << "Fail=" << vpIoTools::splitDrive("//conky//mountpoint/foo/bar").second << " should be=//conky//mountpoint/foo/bar" << std::endl;
247  }
248 
249  std::cout << "Test vpIoTools::splitDrive (Win32) - passed: " << nbOk << "/" << (nbOk+nbFail) << std::endl;
250 #endif
251 
252 
253  //Test vpIoTools::getFileExtension
254 #if defined(_WIN32)
255  nbFail = 0;
256  nbOk = 0;
257 
258  if(strcmp(vpIoTools::getFileExtension("foo.ext").c_str(), ".ext") == 0) {
259  nbOk++;
260  }
261  else {
262  nbFail++;
263  std::cout << "Fail=" << vpIoTools::getFileExtension("foo.ext") << " should be=.ext" << std::endl;
264  }
265 
266  if(strcmp(vpIoTools::getFileExtension("/foo/foo.ext").c_str(), ".ext") == 0) {
267  nbOk++;
268  }
269  else {
270  nbFail++;
271  std::cout << "Fail=" << vpIoTools::getFileExtension("/foo/foo.ext") << " should be=.ext" << std::endl;
272  }
273 
274  if(strcmp(vpIoTools::getFileExtension(".ext").c_str(), "") == 0) {
275  nbOk++;
276  }
277  else {
278  nbFail++;
279  std::cout << "Fail=" << vpIoTools::getFileExtension(".ext") << " should be=" << std::endl;
280  }
281 
282  if(strcmp(vpIoTools::getFileExtension("\\foo.ext\\foo").c_str(), "") == 0) {
283  nbOk++;
284  }
285  else {
286  nbFail++;
287  std::cout << "Fail=" << vpIoTools::getFileExtension("\\foo.ext\\foo") << " should be=" << std::endl;
288  }
289 
290  if(strcmp(vpIoTools::getFileExtension("foo.ext\\").c_str(), "") == 0) {
291  nbOk++;
292  }
293  else {
294  nbFail++;
295  std::cout << "Fail=" << vpIoTools::getFileExtension("foo.ext\\") << " should be=" << std::endl;
296  }
297 
298  if(strcmp(vpIoTools::getFileExtension("").c_str(), "") == 0) {
299  nbOk++;
300  }
301  else {
302  nbFail++;
303  std::cout << "Fail=" << vpIoTools::getFileExtension("") << " should be=" << std::endl;
304  }
305 
306  if(strcmp(vpIoTools::getFileExtension("foo.bar.ext").c_str(), ".ext") == 0) {
307  nbOk++;
308  }
309  else {
310  nbFail++;
311  std::cout << "Fail=" << vpIoTools::getFileExtension("foo.bar.ext") << " should be=.ext" << std::endl;
312  }
313 
314  if(strcmp(vpIoTools::getFileExtension("xx/foo.bar.ext").c_str(), ".ext") == 0) {
315  nbOk++;
316  }
317  else {
318  nbFail++;
319  std::cout << "Fail=" << vpIoTools::getFileExtension("xx/foo.bar.ext") << " should be=.ext" << std::endl;
320  }
321 
322  if(strcmp(vpIoTools::getFileExtension("xx\\foo.bar.ext").c_str(), ".ext") == 0) {
323  nbOk++;
324  }
325  else {
326  nbFail++;
327  std::cout << "Fail=" << vpIoTools::getFileExtension("xx\\foo.bar.ext") << " should be=.ext" << std::endl;
328  }
329 
330  if(strcmp(vpIoTools::getFileExtension("c:a/b\\c.d").c_str(), ".d") == 0) {
331  nbOk++;
332  }
333  else {
334  nbFail++;
335  std::cout << "Fail=" << vpIoTools::getFileExtension("c:a/b\\c.d") << " should be=.d" << std::endl;
336  }
337 
338  std::cout << "Test vpIoTools::getFileExtension (WIN32 platform) - passed: " << nbOk << "/" << (nbOk+nbFail) << std::endl;
339 #else
340  nbFail = 0;
341  nbOk = 0;
342 
343  if(strcmp(vpIoTools::getFileExtension("foo.bar").c_str(), ".bar") == 0) {
344  nbOk++;
345  }
346  else {
347  nbFail++;
348  std::cout << "Fail=" << vpIoTools::getFileExtension("foo.bar") << " should be=.bar" << std::endl;
349  }
350 
351  if(strcmp(vpIoTools::getFileExtension("foo.boo.bar").c_str(), ".bar") == 0) {
352  nbOk++;
353  }
354  else {
355  nbFail++;
356  std::cout << "Fail=" << vpIoTools::getFileExtension("foo.boo.bar") << " should be=.bar" << std::endl;
357  }
358 
359  if(strcmp(vpIoTools::getFileExtension("foo.boo.biff.bar").c_str(), ".bar") == 0) {
360  nbOk++;
361  }
362  else {
363  nbFail++;
364  std::cout << "Fail=" << vpIoTools::getFileExtension("foo.boo.biff.bar") << " should be=.bar" << std::endl;
365  }
366 
367  if(strcmp(vpIoTools::getFileExtension(".csh.rc").c_str(), ".rc") == 0) {
368  nbOk++;
369  }
370  else {
371  nbFail++;
372  std::cout << "Fail=" << vpIoTools::getFileExtension(".csh.rc") << " should be=.rc" << std::endl;
373  }
374 
375  if(strcmp(vpIoTools::getFileExtension("nodots").c_str(), "") == 0) {
376  nbOk++;
377  }
378  else {
379  nbFail++;
380  std::cout << "Fail=" << vpIoTools::getFileExtension("nodots") << " should be=" << std::endl;
381  }
382 
383  if(strcmp(vpIoTools::getFileExtension(".cshrc").c_str(), "") == 0) {
384  nbOk++;
385  }
386  else {
387  nbFail++;
388  std::cout << "Fail=" << vpIoTools::getFileExtension(".cshrc") << " should be=" << std::endl;
389  }
390 
391  if(strcmp(vpIoTools::getFileExtension("...manydots").c_str(), "") == 0) {
392  nbOk++;
393  }
394  else {
395  nbFail++;
396  std::cout << "Fail=" << vpIoTools::getFileExtension("...manydots") << " should be=" << std::endl;
397  }
398 
399  if(strcmp(vpIoTools::getFileExtension("...manydots.ext").c_str(), ".ext") == 0) {
400  nbOk++;
401  }
402  else {
403  nbFail++;
404  std::cout << "Fail=" << vpIoTools::getFileExtension("...manydots.ext") << " should be=.ext" << std::endl;
405  }
406 
407  if(strcmp(vpIoTools::getFileExtension(".").c_str(), "") == 0) {
408  nbOk++;
409  }
410  else {
411  nbFail++;
412  std::cout << "Fail=" << vpIoTools::getFileExtension(".") << " should be=" << std::endl;
413  }
414 
415  if(strcmp(vpIoTools::getFileExtension("..").c_str(), "") == 0) {
416  nbOk++;
417  }
418  else {
419  nbFail++;
420  std::cout << "Fail=" << vpIoTools::getFileExtension("..") << " should be=" << std::endl;
421  }
422 
423  if(strcmp(vpIoTools::getFileExtension("........").c_str(), "") == 0) {
424  nbOk++;
425  }
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  }
434  else {
435  nbFail++;
436  std::cout << "Fail=" << vpIoTools::getFileExtension("") << " should be=" << std::endl;
437  }
438 
439  std::cout << "Test vpIoTools::getFileExtension (Unix-like platform) - passed: " << nbOk << "/" << (nbOk+nbFail) << std::endl;
440 #endif
441 
442 
443  std::cout << std::endl << "End" << std::endl;
444 
445  return 0;
446 }
static bool isAbsolutePathname(const std::string &pathname)
Definition: vpIoTools.cpp:1291
static std::string getFileExtension(const std::string &pathname, const bool checkFile=false)
Definition: vpIoTools.cpp:1105
static const char separator
Definition: vpIoTools.h:184
static std::string path(const char *pathname)
Definition: vpIoTools.cpp:695
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1220
static std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1245
static std::pair< std::string, std::string > splitDrive(const std::string &pathname)
Definition: vpIoTools.cpp:1315
static std::string getName(const std::string &pathname)
Definition: vpIoTools.cpp:1185