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 #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 }