g2o
Toggle main menu visibility
Loading...
Searching...
No Matches
g2o
stuff
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
58
using namespace
::
std
;
59
60
namespace
g2o
{
61
62
std::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
70
std::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
78
std::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
90
std::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
102
std::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
114
bool
fileExists
(
const
char
* filename) {
115
struct
stat statInfo;
116
return
(stat(filename, &statInfo) == 0);
117
}
118
119
std::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
filesys_tools.h
g2o
Definition
dl_wrapper.cpp:55
g2o::getDirname
std::string getDirname(const std::string &filename)
Definition
filesys_tools.cpp:90
g2o::getFileExtension
std::string getFileExtension(const std::string &filename)
Definition
filesys_tools.cpp:62
g2o::getFilesByPattern
std::vector< std::string > getFilesByPattern(const char *pattern)
Definition
filesys_tools.cpp:119
g2o::changeFileExtension
std::string changeFileExtension(const std::string &filename, const std::string &newExt, bool stripDot)
Definition
filesys_tools.cpp:102
g2o::fileExists
bool fileExists(const char *filename)
Definition
filesys_tools.cpp:114
g2o::getBasename
std::string getBasename(const std::string &filename)
Definition
filesys_tools.cpp:78
g2o::getPureFilename
std::string getPureFilename(const std::string &filename)
Definition
filesys_tools.cpp:70
std
Definition
jet.h:876
Generated on
for g2o by
1.17.0