Filename.cpp 2.64 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015 Hugo Beauzée-Luyssen, Videolabs
 *
 * Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

23 24 25 26
#if HAVE_CONFIG_H
# include "config.h"
#endif

27
#include "utils/Filename.h"
28

29 30 31
namespace medialibrary
{

32 33 34 35 36 37 38 39 40
namespace utils
{

namespace file
{

std::string extension( const std::string& fileName )
{
    auto pos = fileName.find_last_of( '.' );
41
    if ( pos == std::string::npos )
42 43 44 45
        return {};
    return fileName.substr( pos + 1 );
}

46 47 48 49 50 51 52 53
std::string directory( const std::string& filePath )
{
    auto pos = filePath.find_last_of( '/' );
    if ( pos == std::string::npos )
        return {};
    return filePath.substr( 0, pos + 1 );
}

54 55 56 57 58 59 60 61
std::string parentDirectory( const std::string& path )
{
    auto pos = path.find_last_of( '/' );
    if ( pos == path.length() - 1 )
        pos = path.find_last_of( '/', pos - 1 );
    return path.substr( 0, pos + 1 );
}

62 63 64 65 66 67 68 69
std::string fileName(const std::string& filePath)
{
    auto pos = filePath.find_last_of( '/' );
    if ( pos == std::string::npos )
        return filePath;
    return filePath.substr( pos + 1 );
}

70 71 72 73 74 75 76 77 78 79 80
std::string firstFolder( const std::string& path )
{
    size_t offset = 0;
    while ( path[offset] == '/' )
        offset++;
    auto pos = path.find_first_of( '/', offset );
    if ( pos == std::string::npos )
        return {};
    return path.substr( offset, pos - offset );
}

81
std::string removePath( const std::string& fullPath, const std::string& toRemove )
82
{
83 84 85 86
    if ( toRemove.length() == 0 )
        return fullPath;
    auto pos = fullPath.find( toRemove ) + toRemove.length();
    while ( fullPath[pos] == '/' )
87
        pos++;
88
    if ( pos >= fullPath.length() )
89
        return {};
90
    return fullPath.substr( pos );
91 92
}

93 94 95
}

}
96 97

}