Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/FileName.cpp
Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2005 George Tzanetakis <gtzan@cs.uvic.ca>
00003 **
00004 ** This program is free software; you can redistribute it and/or modify
00005 ** it under the terms of the GNU General Public License as published by
00006 ** the Free Software Foundation; either version 2 of the License, or
00007 ** (at your option) any later version.
00008 **
00009 ** This program is distributed in the hope that it will be useful,
00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 ** GNU General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU General Public License
00015 ** along with this program; if not, write to the Free Software
00016 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #include <marsyas/common_source.h>
00020 
00021 #ifdef _WIN32
00022 # define MARSYAS_WINDOWS
00023 #endif
00024 
00025 #ifdef MARSYAS_WINDOWS
00026 # include <windows.h>
00027 # include <io.h>
00028 #endif
00029 
00030 #include <marsyas/FileName.h>
00031 
00032 #include <algorithm>
00033 #include <string>
00034 #include <cstdlib>
00035 
00036 using std::ostringstream;
00037 using std::max;
00038 using std::min;
00039 using std::string;
00040 
00041 using namespace Marsyas;
00042 
00043 FileName::FileName()
00044 {
00045 }
00046 
00047 FileName::FileName(mrs_string filename)
00048 {
00049   filename_ = filename;
00050 
00051 #ifdef MARSYAS_WINDOWS
00052   std::replace(filename_.begin(), filename_.end(), '\\', '/');
00053 
00054   if (isDir ())
00055     removeLastSlash ();
00056 #endif
00057 }
00058 
00059 FileName::~FileName()
00060 {
00061 }
00062 
00063 mrs_string
00064 FileName::fullname()
00065 {
00066   return filename_;
00067 }
00068 
00069 
00070 mrs_string
00071 FileName::name()
00072 {
00073   mrs_string name;
00074   size_t loc = getLastSlashPos ();
00075 
00076   if (loc != mrs_string::npos)
00077     name = filename_.substr(loc+1);
00078   else
00079     name = filename_; // file in current directory
00080 
00081   return name;
00082 
00083 }
00084 
00085 mrs_string
00086 FileName::nameNoExt()
00087 {
00088   mrs_string str = name();
00089   size_t dot_position = str.rfind('.');
00090   return str.substr(0, dot_position);
00091 }
00092 
00093 mrs_string
00094 FileName::ext()
00095 {
00096   size_t loc;
00097   loc = filename_.rfind('.');
00098   return filename_.substr(loc+1);
00099 }
00100 
00101 mrs_string
00102 FileName::path()
00103 {
00104   mrs_string name;
00105   size_t last_slash_pos = getLastSlashPos ();
00106 
00107   if (last_slash_pos != mrs_string::npos)
00108     name = filename_.substr(0, last_slash_pos+1);
00109   else
00110     name = ""; // file in current directory no path
00111 
00112   return name;
00113 
00114 }
00115 
00116 mrs_bool
00117 FileName::isDir ()
00118 {
00119 
00120 #ifdef MARSYAS_WINDOWS
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_WINDOWS
00141   struct _finddata_t          CurrentFile;
00142   long                        hFile;
00143   mrs_string                  search4;
00144 
00145   search4   = filename_ + "/" + wildcard;
00146 
00147   // find first file
00148   if( (hFile = (long)_findfirst( search4.c_str (), &CurrentFile )) == -1L )
00149     return result;
00150   else
00151   {
00152     // file found, add it to the list
00153     result.push_back (filename_ + "/" + CurrentFile.name);
00154 
00155     // Find the rest of the files
00156     while( _findnext( hFile, &CurrentFile ) == 0 )
00157     {
00158       // file found, add it to the list
00159       result.push_back (filename_ + "/" + CurrentFile.name);
00160     }
00161 
00162     // has to be called at the end
00163     _findclose( hFile );
00164   }
00165 #else
00166   (void) wildcard;
00167   MRSWARN("getFilesInDir only works on Windows");
00168 #endif
00169   return result;
00170 }
00171 size_t
00172 FileName::getLastSlashPos ()
00173 {
00174   return filename_.rfind('/');
00175 }
00176 
00177 void
00178 FileName::removeLastSlash ()
00179 {
00180   size_t last_slash_pos = getLastSlashPos ();
00181   if (last_slash_pos == filename_.length()-1)
00182     filename_ = filename_.substr(0, last_slash_pos);
00183 }
00184 
00185 bool FileName::isAbsolute()
00186 {
00187   if (filename_.empty())
00188     return false;
00189   // Try UNIX style:
00190   if (filename_[0] == '/')
00191     return true;
00192 #if defined(MARSYAS_LINUX) || defined(MARSYAS_MACOSX)
00193   if (filename_[0] == '~')
00194     return true;
00195 #endif
00196 #ifdef MARSYAS_WINDOWS
00197   // Try Windows style:
00198   if (filename_.find(':') != string::npos)
00199     return true;
00200 #endif
00201   return false;
00202 }
00203 
00204 FileName & FileName::append(const string & element)
00205 {
00206   if (element.empty())
00207     return *this;
00208 
00209   if (filename_.empty())
00210   {
00211     filename_ = element;
00212   }
00213   else
00214   {
00215     if (filename_[filename_.size()-1] != '/' &&
00216         element[0] != '/')
00217     {
00218       filename_.push_back('/');
00219     }
00220     filename_.append(element);
00221   }
00222   return *this;
00223 }
00224 
00225 FileName FileName::userHomeDir()
00226 {
00227 #if defined(MARSYAS_LINUX) || defined(MARSYAS_MACOSX)
00228   return FileName( std::getenv("HOME") );
00229 #else
00230   MRSERR("FileName::userHomeDir not implemented for this system.");
00231   return FileName();
00232 #endif
00233 }
00234 
00235 FileName FileName::userAppDataDir()
00236 {
00237 #if defined(MARSYAS_LINUX)
00238   return userHomeDir().append(".local/share");
00239 #elif defined(MARSYAS_MACOSX)
00240   return userHomeDir().append("Library/Application Support");
00241 #else
00242   MRSERR("FileName::userAppDataDir not implemented for this system.");
00243   return FileName();
00244 #endif
00245 }