UFile.cpp
Go to the documentation of this file.
00001 // Taken from UtiLite library r185 [www.utilite.googlecode.com]
00002 
00003 /*
00004 *  utilite is a cross-platform library with
00005 *  useful utilities for fast and small developing.
00006 *  Copyright (C) 2010  Mathieu Labbe
00007 *
00008 *  utilite is free library: you can redistribute it and/or modify
00009 *  it under the terms of the GNU Lesser General Public License as published by
00010 *  the Free Software Foundation, either version 3 of the License, or
00011 *  (at your option) any later version.
00012 *
00013 *  utilite is distributed in the hope that it will be useful,
00014 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 *  GNU Lesser General Public License for more details.
00017 *
00018 *  You should have received a copy of the GNU Lesser General Public License
00019 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
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 }


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Fri Feb 12 2016 01:18:17