g2o
filesys_tools.cpp
Go to the documentation of this file.
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 /***************************************************************************
28  * filesysTools.cpp
29  *
30  * Fr 02 Mär 2007 23:14:08 CET
31  * Copyright 2007 Rainer Kümmerle
32  * Email rk@raikue.net
33  ****************************************************************************/
34 #include "filesys_tools.h"
35 
36 #include <sys/stat.h>
37 #include <ctime>
38 #include <sys/types.h>
39 #include <cstdio>
40 #include <iostream>
41 
42 #ifdef WINDOWS
43 #include <windows.h>
44 #include <winbase.h>
45 #endif
46 
47 #if (defined (UNIX) || defined(CYGWIN)) && !defined(ANDROID)
48 #include <wordexp.h>
49 #endif
50 
51 #ifdef __APPLE__
52 //#include <chrono>
53 //#include <thread>
54 #endif
55 
56 using namespace ::std;
57 
58 namespace g2o {
59 
60 std::string getFileExtension(const std::string& filename)
61 {
62  std::string::size_type lastDot = filename.find_last_of('.');
63  if (lastDot != std::string::npos)
64  return filename.substr(lastDot + 1);
65  else
66  return "";
67 }
68 
69 std::string getPureFilename(const std::string& filename)
70 {
71  std::string::size_type lastDot = filename.find_last_of('.');
72  if (lastDot != std::string::npos)
73  return filename.substr(0, lastDot);
74  else
75  return filename;
76 }
77 
78 std::string getBasename(const std::string& filename)
79 {
80 #ifdef WINDOWS
81  std::string::size_type lastSlash = filename.find_last_of('\\');
82 #else
83  std::string::size_type lastSlash = filename.find_last_of('/');
84 #endif
85  if (lastSlash != std::string::npos)
86  return filename.substr(lastSlash + 1);
87  else
88  return filename;
89 }
90 
91 std::string getDirname(const std::string& filename)
92 {
93 #ifdef WINDOWS
94  std::string::size_type lastSlash = filename.find_last_of('\\');
95 #else
96  std::string::size_type lastSlash = filename.find_last_of('/');
97 #endif
98  if (lastSlash != std::string::npos)
99  return filename.substr(0, lastSlash);
100  else
101  return "";
102 }
103 
104 std::string changeFileExtension(const std::string& filename, const std::string& newExt, bool stripDot)
105 {
106  std::string::size_type lastDot = filename.find_last_of('.');
107  if (lastDot != std::string::npos) {
108  if (stripDot)
109  return filename.substr(0, lastDot) + newExt;
110  else
111  return filename.substr(0, lastDot + 1) + newExt;
112  } else
113  return filename;
114 }
115 
116 bool fileExists(const char* filename)
117 {
118  struct stat statInfo;
119  return (stat(filename, &statInfo) == 0);
120 }
121 
122 std::vector<std::string> getFilesByPattern(const char* pattern)
123 {
124  std::vector<std::string> result;
125 
126 #ifdef WINDOWS
127 
128  HANDLE hFind;
129  WIN32_FIND_DATA FData;
130  if ((hFind = FindFirstFile(pattern, &FData)) != INVALID_HANDLE_VALUE) {
131  do {
132  result.push_back(FData.cFileName);
133  } while (FindNextFile(hFind, &FData));
134  FindClose(hFind);
135  }
136 
137 #elif (defined (UNIX) || defined (CYGWIN)) && !defined(ANDROID)
138 
139  wordexp_t p;
140  wordexp(pattern, &p, 0);
141 
142  // For some reason, wordexp sometimes fails on an APPLE machine to
143  // return anything; therefore, run it several times until we do find
144  // something - or give up
145 #ifdef __APPLE__
146  for (int k = 0; (k < 100) && (p.we_wordc == 0); k++) {
147  //chrono::milliseconds duration(20);
148  //this_thread::sleep_for(duration);
149  wordexp(pattern, &p, WRDE_APPEND);
150  }
151 #endif
152 
153  result.reserve(p.we_wordc);
154  for (size_t i = 0; i < p.we_wordc; ++i)
155  result.push_back(p.we_wordv[i]);
156 
157  wordfree(&p);
158 
159 #endif
160 
161  return result;
162 }
163 
164 }
std::string changeFileExtension(const std::string &filename, const std::string &newExt, bool stripDot)
std::string getFileExtension(const std::string &filename)
std::string getBasename(const std::string &filename)
std::string getDirname(const std::string &filename)
std::string getPureFilename(const std::string &filename)
std::vector< std::string > getFilesByPattern(const char *pattern)
bool fileExists(const char *filename)