00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "common.h"
00022
00023 #ifdef MARSYAS_WIN32
00024 #include <io.h>
00025 #endif
00026
00027 #include "FileName.h"
00028
00029 using std::ostringstream;
00030 using std::max;
00031
00032 using namespace Marsyas;
00033
00034 FileName::FileName()
00035 {
00036 }
00037
00038 FileName::FileName(mrs_string filename)
00039 {
00040 filename_ = filename;
00041
00042
00043
00044 #if (defined(MARSYAS_WIN32))
00045 if (isDir ())
00046 removeLastSlash ();
00047 #endif
00048 }
00049
00050
00051 FileName::~FileName()
00052 {
00053 }
00054
00055
00056
00057
00058
00059 mrs_string
00060 FileName::fullname()
00061 {
00062 return filename_;
00063 }
00064
00065
00066 mrs_string
00067 FileName::name()
00068 {
00069 mrs_string name;
00070 size_t loc = getLastSlashPos ();
00071
00072 if (loc != mrs_string::npos)
00073 name = filename_.substr(loc+1, filename_.length()-1);
00074 else
00075 name = filename_;
00076
00077 return name;
00078
00079 }
00080
00081 mrs_string
00082 FileName::nameNoExt()
00083 {
00084 mrs_string str = name();
00085 size_t loc;
00086
00087 loc = str.rfind(".", str.length()-1);
00088 return str.substr(0,loc);
00089 }
00090
00091 mrs_string
00092 FileName::ext()
00093 {
00094 size_t loc;
00095 loc = filename_.rfind(".", filename_.length()-1);
00096 return filename_.substr(loc+1, filename_.length()-1);
00097 }
00098
00099 mrs_string
00100 FileName::path()
00101 {
00102
00103 mrs_string name;
00104 size_t loc = getLastSlashPos ();
00105
00106
00107 if (loc != mrs_string::npos)
00108 name = filename_.substr(0, loc+1);
00109 else
00110 name = "";
00111
00112 return name;
00113
00114 }
00115
00116 mrs_bool
00117 FileName::isDir ()
00118 {
00119
00120 #if (defined(MARSYAS_WIN32))
00121 const DWORD attr = GetFileAttributes (filename_.c_str ());
00122
00123 return (attr != 0xffffffff)
00124 && ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0);
00125 #else
00126 MRSWARN("isDir only implemented on Windows");
00127 return false;
00128 #endif
00129
00130
00131
00132
00133 }
00134
00135 std::vector<mrs_string>
00136 FileName::getFilesInDir (mrs_string wildcard)
00137 {
00138 std::vector<mrs_string> result;
00139
00140 #ifdef MARSYAS_WIN32
00141 struct _finddata_t CurrentFile;
00142 long hFile;
00143 mrs_string search4;
00144
00145 search4 = filename_ + "/" + wildcard;
00146
00147
00148 if( (hFile = _findfirst( search4.c_str (), &CurrentFile )) == -1L )
00149 return result;
00150 else
00151 {
00152
00153 result.push_back (filename_ + "/" + CurrentFile.name);
00154
00155
00156 while( _findnext( hFile, &CurrentFile ) == 0 )
00157 {
00158
00159 result.push_back (filename_ + "/" + CurrentFile.name);
00160 }
00161
00162
00163 _findclose( hFile );
00164 }
00165 #else
00166 MRSWARN("getFilesInDir only works on Windows");
00167 #endif
00168 return result;
00169 }
00170 size_t
00171 FileName::getLastSlashPos ()
00172 {
00173 size_t loc;
00174
00175 #ifdef MARSYAS_WIN32
00176 size_t loc2 = filename_.rfind("/", filename_.length()-1);
00177 loc = filename_.rfind("\\", filename_.length()-1);
00178 if (loc2 != mrs_string::npos)
00179 loc = max (loc, loc2);
00180 #else
00181 loc = filename_.rfind("/", filename_.length()-1);
00182 #endif
00183 return loc;
00184 }
00185
00186 void
00187 FileName::removeLastSlash ()
00188 {
00189 size_t loc = getLastSlashPos ();
00190
00191 if (loc == filename_.length()-1)
00192 filename_ = filename_.substr(0, loc);
00193 }
00194
00195