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 #include "utilite/UFile.h"
00023
00024 #include <fstream>
00025 #include "utilite/UStl.h"
00026
00027 bool UFile::exists(const std::string &filePath)
00028 {
00029 bool fileExists = false;
00030 std::ifstream in(filePath.c_str(), std::ios::in);
00031 if (in.is_open())
00032 {
00033 fileExists = true;
00034 in.close();
00035 }
00036 return fileExists;
00037 }
00038
00039 long UFile::length(const std::string &filePath)
00040 {
00041 long fileSize = 0;
00042 FILE* fp = 0;
00043 #ifdef _MSC_VER
00044 fopen_s(&fp, filePath.c_str(), "rb");
00045 #else
00046 fp = fopen(filePath.c_str(), "rb");
00047 #endif
00048 if(fp == NULL)
00049 {
00050 return 0;
00051 }
00052
00053 fseek(fp , 0 , SEEK_END);
00054 fileSize = ftell(fp);
00055 fclose(fp);
00056
00057 return fileSize;
00058 }
00059
00060 int UFile::erase(const std::string &filePath)
00061 {
00062 return remove(filePath.c_str());
00063 }
00064
00065 int UFile::rename(const std::string &oldFilePath,
00066 const std::string &newFilePath)
00067 {
00068 return rename(oldFilePath.c_str(), newFilePath.c_str());
00069 }
00070
00071 std::string UFile::getName(const std::string & filePath)
00072 {
00073 std::string fullPath = filePath;
00074 std::string name;
00075 for(int i=fullPath.size()-1; i>=0; --i)
00076 {
00077 if(fullPath[i] == '/' || fullPath[i] == '\\')
00078 {
00079 break;
00080 }
00081 else
00082 {
00083 name.insert(name.begin(), fullPath[i]);
00084 }
00085 }
00086 return name;
00087 }
00088
00089 std::string UFile::getExtension(const std::string &filePath)
00090 {
00091 std::list<std::string> list = uSplit(filePath, '.');
00092 if(list.size())
00093 {
00094 return list.back();
00095 }
00096 return "";
00097 }