Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "filesys_tools.h"
00025
00026 #include <sys/stat.h>
00027 #include <ctime>
00028 #include <sys/types.h>
00029 #include <cstdio>
00030 #include <iostream>
00031
00032 #ifdef WINDOWS
00033 #include <Windows.h>
00034 #include <WinBase.h>
00035 #endif
00036
00037 #if defined (UNIX) || defined(CYGWIN)
00038 #include <wordexp.h>
00039 #endif
00040
00041 using namespace ::std;
00042
00043
00044 namespace g2o {
00045
00046 std::string getFileExtension(const std::string& filename)
00047 {
00048 std::string::size_type lastDot = filename.find_last_of('.');
00049 if (lastDot != std::string::npos)
00050 return filename.substr(lastDot + 1);
00051 else
00052 return "";
00053 }
00054
00055 std::string getPureFilename(const std::string& filename)
00056 {
00057 std::string::size_type lastDot = filename.find_last_of('.');
00058 if (lastDot != std::string::npos)
00059 return filename.substr(0, lastDot);
00060 else
00061 return filename;
00062 }
00063
00064 std::string getBasename(const std::string& filename)
00065 {
00066 #ifdef WINDOWS
00067 std::string::size_type lastSlash = filename.find_last_of('\\');
00068 #else
00069 std::string::size_type lastSlash = filename.find_last_of('/');
00070 #endif
00071 if (lastSlash != std::string::npos)
00072 return filename.substr(lastSlash + 1);
00073 else
00074 return filename;
00075 }
00076
00077 std::string getDirname(const std::string& filename)
00078 {
00079 #ifdef WINDOWS
00080 std::string::size_type lastSlash = filename.find_last_of('\\');
00081 #else
00082 std::string::size_type lastSlash = filename.find_last_of('/');
00083 #endif
00084 if (lastSlash != std::string::npos)
00085 return filename.substr(0, lastSlash);
00086 else
00087 return "";
00088 }
00089
00090 std::string changeFileExtension(const std::string& filename, const std::string& newExt, bool stripDot)
00091 {
00092 std::string::size_type lastDot = filename.find_last_of('.');
00093 if (lastDot != std::string::npos) {
00094 if (stripDot)
00095 return filename.substr(0, lastDot) + newExt;
00096 else
00097 return filename.substr(0, lastDot + 1) + newExt;
00098 } else
00099 return filename;
00100 }
00101
00102 bool fileExists(const char* filename)
00103 {
00104 struct stat statInfo;
00105 return (stat(filename, &statInfo) == 0);
00106 }
00107
00108 std::vector<std::string> getFilesByPattern(const char* pattern)
00109 {
00110 std::vector<std::string> result;
00111
00112 #ifdef WINDOWS
00113
00114 HANDLE hFind;
00115 WIN32_FIND_DATA FData;
00116 if ((hFind = FindFirstFile(pattern, &FData)) != INVALID_HANDLE_VALUE) {
00117 do {
00118 result.push_back(FData.cFileName);
00119 } while (FindNextFile(hFind, &FData));
00120 FindClose(hFind);
00121 }
00122
00123 #elif defined (UNIX) || defined (CYGWIN)
00124
00125 wordexp_t p;
00126 wordexp(pattern, &p, 0);
00127
00128
00129
00130
00131 #ifdef __APPLE__
00132 for (int k = 0; (k < 5) && (p.we_wordc == 0); k++) {
00133 wordexp(pattern, &p, WRDE_APPEND);
00134 }
00135 #endif
00136
00137 result.reserve(p.we_wordc);
00138 for (size_t i = 0; i < p.we_wordc; ++i)
00139 result.push_back(p.we_wordv[i]);
00140
00141 wordfree(&p);
00142
00143 #endif
00144
00145 return result;
00146 }
00147
00148 }