Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

directory_utils Class Reference

Directory and path utilities. More...

#include <playlist.h>

List of all members.

Static Public Member Functions

static string join_file_to_directory (const string directory, const string &file)
 Join the file to the directory.
static string get_directory_from_file (const string &file)
 Obtain the directory from the file.
static string get_absolute_path_to_file (const string &directory, const string &file)
 Get absolute path to file.
static string get_relative_path_to_file (const string &directory, const string &file)
 Get relative path to file.
static string expand_directory (const string directory)
 Expand directories with ~ as the first item.


Detailed Description

Directory and path utilities.

Definition at line 108 of file playlist.h.


Member Function Documentation

string directory_utils::expand_directory const string  directory  )  [static]
 

Expand directories with ~ as the first item.

Definition at line 235 of file playlist.cc.

References StringUtils::split().

Referenced by PlayList::GetProjectDirectory().

00236 {
00237     string output;
00238     vector <string> items;
00239     StringUtils::split( directory, "/", items );
00240     vector< string >::iterator item = items.begin( );
00241 
00242     if ( item != items.end( ) && *item == "~" )
00243     {
00244         output = getenv( "HOME" );
00245         item ++;
00246     }
00247 
00248     for ( ; item != items.end( ); item ++ )
00249         output += "/" + *item;
00250 
00251     return output;
00252 }

string directory_utils::get_absolute_path_to_file const string &  directory,
const string &  file
[static]
 

Get absolute path to file.

If file has an absolute path, then you will get a cleaned up version of that path and file returned (ie: /'directory'/'sub'/..///'file' becomes /'directory'/'file').

If file is relative (ie: not starting with a /) and directory is absolute (ie: starting with a /) then you will receive the appended output.

If neither is absolute, then cwd is pre-pended to directory and file is appended and a cleaned up file spec is returned.

Definition at line 190 of file playlist.cc.

References join_file_to_directory().

Referenced by fillMap(), and PlayList::LoadMediaObject().

00191 {
00192     return join_file_to_directory( directory, file );
00193 }

string directory_utils::get_directory_from_file const string &  file  )  [static]
 

Obtain the directory from the file.

A value of 'file' that actually corresponds to a directory will result in the parent directory being returned ie: /'parent'/'directory' returns /'parent' which is probably not what you want...

Definition at line 173 of file playlist.cc.

References join_file_to_directory().

Referenced by KinoCommon::getFileToOpen(), PlayList::GetPlayList(), PlayList::GetProjectDirectory(), PlayList::InsertPlayList(), PlayList::LoadPlayList(), KinoCommon::loadPlayList(), and PlayList::SavePlayList().

00174 {
00175     return join_file_to_directory( "", file + "/.." );
00176 }

string directory_utils::get_relative_path_to_file const string &  directory,
const string &  file
[static]
 

Get relative path to file.

Given a directory of /same-path/different-path and file of /same-path/blah, the output should be ../blah.

Definition at line 201 of file playlist.cc.

References StringUtils::join(), join_file_to_directory(), and StringUtils::split().

Referenced by relativeMap().

00202 {
00203     string output = "";
00204     string absolute = join_file_to_directory( directory, file );
00205     vector < string > directory_items;
00206     vector < string > absolute_items;
00207     StringUtils::split( absolute, "/", absolute_items );
00208     StringUtils::split( directory, "/", directory_items );
00209 
00210     vector < string >::iterator directory_item;
00211     vector < string >::iterator absolute_item;
00212 
00213     // While they're both the same, remove from both
00214     for ( directory_item = directory_items.begin(), absolute_item = absolute_items.begin();
00215             directory_item != directory_items.end() && absolute_item != absolute_items.end() && *directory_item == *absolute_item; )
00216     {
00217         directory_items.erase( directory_item );
00218         absolute_items.erase( absolute_item );
00219         directory_item = directory_items.begin();
00220         absolute_item = absolute_items.begin();
00221     }
00222 
00223     // For each item left in the directory_items, output a ../
00224     for ( directory_item = directory_items.begin(); directory_item != directory_items.end() ; directory_item ++ )
00225         output += "../";
00226 
00227     // Now simply join what's left in absolute to output and return
00228     output += StringUtils::join( absolute_items, "/" );
00229     return output;
00230 }

string directory_utils::join_file_to_directory const string  directory,
const string &  file
[static]
 

Join the file to the directory.

It is assumed that if the input file is not an absolute path (ie: starting with a /) then the full path is relative to the current working directory. So 'file' would return /'cwd' and 'relative/file' would be /'cwd + relative'.

Note that .. directory specs in the file input are normalised to the full path minus the .. and erroneously formed paths (containing double slashes for example) are also corrected in the output.

Definition at line 121 of file playlist.cc.

References StringUtils::join(), and StringUtils::split().

Referenced by get_absolute_path_to_file(), get_directory_from_file(), get_relative_path_to_file(), and PlayList::GetProjectDirectory().

00122 {
00123     vector <string> items;
00124 
00125     // Determine if the file is a full file spec or not
00126     if ( file[ 0 ] != '/' && directory[ 0 ] != '/' )
00127     {
00128         char path[ PATH_MAX ];
00129         getcwd( path, PATH_MAX );
00130         StringUtils::split( path, "/", items );
00131     }
00132 
00133     // Now add the directory if file is not absolute
00134     if ( file[ 0 ] != '/' )
00135         StringUtils::split( directory, "/", items );
00136 
00137     // Split the file and append to the directory info
00138     StringUtils::split( file, "/", items );
00139 
00140     // iterate through the items vector
00141     for ( vector< string >::iterator item = items.begin( ); item != items.end( ); )
00142     {
00143         if ( *item == ".." )
00144         {
00145             if ( item == items.begin( ) )
00146             {
00147                 items.erase( item );
00148                 item = items.begin();
00149             }
00150             else
00151             {
00152                 items.erase( -- item + 1 );
00153                 items.erase( -- item + 1 );
00154                 item ++;
00155             }
00156         }
00157         else
00158         {
00159             item ++;
00160         }
00161     }
00162 
00163     return "/" + StringUtils::join( items, "/" );
00164 }


The documentation for this class was generated from the following files:
Generated on Sun Mar 11 22:12:47 2007 for Kino by  doxygen 1.4.2