00001
00002 #include "FileUtil.hh"
00003
00004
00005 #include <iostream>
00006 #include <stdio.h>
00007 #include <fstream>
00008
00009 using std::ifstream;
00010 using std::ios;
00011
00012 namespace slic
00013 {
00014
00015 int FileUtil::removeFile(const char* filename)
00016 {
00017 int ret = remove( filename );
00018 return ret;
00019 }
00020
00021 int FileUtil::removeFile(const std::string& filename)
00022 {
00023 return FileUtil::removeFile( filename.c_str() );
00024 }
00025
00026 bool FileUtil::fileExists(const char* filename)
00027 {
00028 bool exists=true;
00029
00030 ifstream fin;
00031 fin.open(filename, ios::in);
00032
00033
00034 if( fin.fail() ) {
00035 exists=false;
00036 }
00037 fin.close();
00038 return exists;
00039 }
00040
00041 bool FileUtil::fileExists(const std::string& filename)
00042 {
00043 return FileUtil::fileExists( filename.c_str() );
00044 }
00045
00046 std::string FileUtil::basename(std::string fn)
00047 {
00048 static std::string delim = "/";
00049
00050 std::string filename = fn;
00051
00052
00053 int basename_pos = filename.rfind(delim);
00054
00055
00056 #if defined(__WIN32__) || defined(__CYGWIN__) || defined(__CYGWIN32__)
00057 if ( basename_pos == -1 ) {
00058 basename_pos = filename.rfind("\\");
00059 }
00060 #endif
00061
00062
00063 if ( basename_pos != -1 ) {
00064 filename.erase(filename.begin(),
00065 filename.begin() + basename_pos + 1);
00066 }
00067
00068 return filename;
00069 }
00070
00071 std::string FileUtil::extension(std::string fn)
00072 {
00073 std::string::size_type idx = fn.find_last_of(".");
00074
00075 std::string ext;
00076 if ( idx != std::string::npos ) {
00077 ext = fn.substr(idx + 1, fn.size() - idx);
00078 }
00079 else {
00080 ext = "";
00081 }
00082
00083 return ext;
00084 }
00085
00086 std::string FileUtil::removeExtension(std::string fn)
00087 {
00088 std::string::size_type idx = fn.find_last_of(".");
00089
00090 std::string fname;
00091 if ( idx != std::string::npos ) {
00092 fname = fn.substr(0, idx);
00093 }
00094 else {
00095 fname = fn;
00096 }
00097 return fname;
00098 }
00099 }