filesys_tools.cpp
Go to the documentation of this file.
00001 // g2o - General Graph Optimization
00002 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
00003 //
00004 // g2o is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU Lesser General Public License as published
00006 // by the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // g2o is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU Lesser General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU Lesser General Public License
00015 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 /***************************************************************************
00018  *            filesysTools.cpp
00019  *
00020  *  Fr 02 Mär 2007 23:14:08 CET
00021  *  Copyright 2007 Rainer Kümmerle
00022  *  Email rk@raikue.net
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   // For some reason, wordexp sometimes fails on an APPLE machine to
00129   // return anything; therefore, run it several times until we do find
00130   // something - or give up
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 }


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:31:08