g2o
Loading...
Searching...
No Matches
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// clang-format off
37#ifdef WINDOWS
38#include <windows.h>
39#endif
40// clang-format on
41
42#include <sys/stat.h>
43#include <sys/types.h>
44
45#include <cstdio>
46#include <ctime>
47#include <iostream>
48
49#if (defined(UNIX) || defined(CYGWIN)) && !defined(ANDROID)
50#include <wordexp.h>
51#endif
52
53#ifdef __APPLE__
54// #include <chrono>
55// #include <thread>
56#endif
57
58using namespace ::std;
59
60namespace g2o {
61
62std::string getFileExtension(const std::string& filename) {
63 std::string::size_type lastDot = filename.find_last_of('.');
64 if (lastDot != std::string::npos)
65 return filename.substr(lastDot + 1);
66 else
67 return "";
68}
69
70std::string getPureFilename(const std::string& filename) {
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
78std::string getBasename(const std::string& filename) {
79#ifdef WINDOWS
80 std::string::size_type lastSlash = filename.find_last_of('\\');
81#else
82 std::string::size_type lastSlash = filename.find_last_of('/');
83#endif
84 if (lastSlash != std::string::npos)
85 return filename.substr(lastSlash + 1);
86 else
87 return filename;
88}
89
90std::string getDirname(const std::string& filename) {
91#ifdef WINDOWS
92 std::string::size_type lastSlash = filename.find_last_of('\\');
93#else
94 std::string::size_type lastSlash = filename.find_last_of('/');
95#endif
96 if (lastSlash != std::string::npos)
97 return filename.substr(0, lastSlash);
98 else
99 return "";
100}
101
102std::string changeFileExtension(const std::string& filename,
103 const std::string& newExt, bool stripDot) {
104 std::string::size_type lastDot = filename.find_last_of('.');
105 if (lastDot != std::string::npos) {
106 if (stripDot)
107 return filename.substr(0, lastDot) + newExt;
108 else
109 return filename.substr(0, lastDot + 1) + newExt;
110 } else
111 return filename;
112}
113
114bool fileExists(const char* filename) {
115 struct stat statInfo;
116 return (stat(filename, &statInfo) == 0);
117}
118
119std::vector<std::string> getFilesByPattern(const char* pattern) {
120 std::vector<std::string> result;
121
122#ifdef WINDOWS
123
124 HANDLE hFind;
125 WIN32_FIND_DATA FData;
126 if ((hFind = FindFirstFile(pattern, &FData)) != INVALID_HANDLE_VALUE) {
127 do {
128 result.push_back(FData.cFileName);
129 } while (FindNextFile(hFind, &FData));
130 FindClose(hFind);
131 }
132
133#elif (defined(UNIX) || defined(CYGWIN)) && !defined(ANDROID)
134
135 wordexp_t p;
136 wordexp(pattern, &p, 0);
137
138 // For some reason, wordexp sometimes fails on an APPLE machine to
139 // return anything; therefore, run it several times until we do find
140 // something - or give up
141#ifdef __APPLE__
142 for (int k = 0; (k < 100) && (p.we_wordc == 0); k++) {
143 // chrono::milliseconds duration(20);
144 // this_thread::sleep_for(duration);
145 wordexp(pattern, &p, WRDE_APPEND);
146 }
147#endif
148
149 result.reserve(p.we_wordc);
150 for (size_t i = 0; i < p.we_wordc; ++i) result.push_back(p.we_wordv[i]);
151
152 wordfree(&p);
153
154#endif
155
156 return result;
157}
158
159} // namespace g2o
std::string changeFileExtension(const std::string &filename, const std::string &newExt, bool stripDot)
std::vector< std::string > getFilesByPattern(const char *pattern)
std::string getFileExtension(const std::string &filename)
bool fileExists(const char *filename)
std::string getPureFilename(const std::string &filename)
std::string getBasename(const std::string &filename)
std::string getDirname(const std::string &filename)
Definition jet.h:876