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