UFile.cpp
Go to the documentation of this file.
00001 /*
00002 *  utilite is a cross-platform library with
00003 *  useful utilities for fast and small developing.
00004 *  Copyright (C) 2010  Mathieu Labbe
00005 *
00006 *  utilite is free library: you can redistribute it and/or modify
00007 *  it under the terms of the GNU Lesser General Public License as published by
00008 *  the Free Software Foundation, either version 3 of the License, or
00009 *  (at your option) any later version.
00010 *
00011 *  utilite is distributed in the hope that it will be useful,
00012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 *  GNU Lesser General Public License for more details.
00015 *
00016 *  You should have received a copy of the GNU Lesser General Public License
00017 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 #include "rtabmap/utilite/UFile.h"
00021 
00022 #include <fstream>
00023 #include "rtabmap/utilite/UStl.h"
00024 
00025 bool UFile::exists(const std::string &filePath)
00026 {
00027     bool fileExists = false;
00028     std::ifstream in(filePath.c_str(), std::ios::in);
00029     if (in.is_open())
00030     {
00031         fileExists = true;
00032         in.close();   
00033     }
00034     return fileExists;
00035 }
00036 
00037 long UFile::length(const std::string &filePath)
00038 {
00039     long fileSize = 0;
00040     FILE* fp = 0;
00041 #ifdef _MSC_VER
00042     fopen_s(&fp, filePath.c_str(), "rb");
00043 #else
00044     fp = fopen(filePath.c_str(), "rb");
00045 #endif
00046     if(fp == NULL)
00047     {
00048         return 0;
00049     }
00050 
00051     fseek(fp , 0 , SEEK_END);
00052     fileSize = ftell(fp);
00053     fclose(fp);
00054 
00055     return fileSize;
00056 }
00057 
00058 int UFile::erase(const std::string &filePath)
00059 {
00060     return std::remove(filePath.c_str());
00061 }
00062 
00063 int UFile::rename(const std::string &oldFilePath,
00064                      const std::string &newFilePath)
00065 {
00066     return std::rename(oldFilePath.c_str(), newFilePath.c_str());
00067 }
00068 
00069 std::string UFile::getName(const std::string & filePath)
00070 {
00071         std::string fullPath = filePath;
00072         std::string name;
00073         for(int i=(int)fullPath.size()-1; i>=0; --i)
00074         {
00075                 if(fullPath[i] == '/' || fullPath[i] == '\\')
00076                 {
00077                         break;
00078                 }
00079                 else
00080                 {
00081                         name.insert(name.begin(), fullPath[i]);
00082                 }
00083         }
00084         return name;
00085 }
00086 
00087 std::string UFile::getExtension(const std::string &filePath)
00088 {
00089         std::list<std::string> list = uSplit(filePath, '.');
00090         if(list.size())
00091         {
00092                 return list.back();
00093         }
00094         return "";
00095 }
00096 
00097 void UFile::copy(const std::string & from, const std::string & to)
00098 {
00099         std::ifstream src(from.c_str());
00100         std::ofstream dst(to.c_str());
00101 
00102         dst << src.rdbuf();
00103 }


rtabmap
Author(s): Mathieu Labbe
autogenerated on Sat Jul 23 2016 11:44:28