LCOV - code coverage report
Current view: top level - snapdev - string_pathinfo.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 5 9 55.6 %
Date: 2021-08-22 18:14:51 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2011-2021  Made to Order Software Corp.  All Rights Reserved
       2             : //
       3             : // https://snapwebsites.org/project/snapdev
       4             : // contact@m2osw.com
       5             : //
       6             : // This program is free software; you can redistribute it and/or modify
       7             : // it under the terms of the GNU General Public License as published by
       8             : // the Free Software Foundation; either version 2 of the License, or
       9             : // (at your option) any later version.
      10             : //
      11             : // This program is distributed in the hope that it will be useful,
      12             : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             : // GNU General Public License for more details.
      15             : //
      16             : // You should have received a copy of the GNU General Public License along
      17             : // with this program; if not, write to the Free Software Foundation, Inc.,
      18             : // 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
      19             : #pragma once
      20             : 
      21             : #include "snapdev/reverse_cstring.h"
      22             : 
      23             : #include <string>
      24             : 
      25             : namespace snap
      26             : {
      27             : 
      28             : 
      29             : /** \brief Retrieve the basename of a path.
      30             :  *
      31             :  * This function retrieves the basename of a path. You can also remove the
      32             :  * suffix (often called a file extension) and a prefix.
      33             :  *
      34             :  * \code
      35             :  *     // the following returns true
      36             :  *     snap::string_pathinfo_basename(
      37             :  *                "/usr/share/snapwebsites/in.basename.txt"
      38             :  *              , ".txt"
      39             :  *              , "in.") == "basename"
      40             :  * \endcode
      41             :  *
      42             :  * \param[in] path  The path from which basename gets retrieved.
      43             :  * \param[in] suffix  If the path ends with that suffix, remove it.
      44             :  * \param[in] prefix  If the path starts with that prefix, remove it.
      45             :  *
      46             :  * \return The basename of \p path.
      47             :  */
      48             : template < class StringT >
      49             : StringT string_pathinfo_basename(StringT const & path
      50             :                                , typename std::decay<StringT>::type const & suffix = ""
      51             :                                , typename std::decay<StringT>::type const & prefix = "")
      52             : {
      53             :     // ignore path if present
      54             :     //
      55             :     typename StringT::size_type pos(path.find_last_of('/'));
      56             :     if(pos == StringT::npos)
      57             :     {
      58             :         // if no '/' in string, the entire name is a basename
      59             :         //
      60             :         pos = 0;
      61             :     }
      62             :     else
      63             :     {
      64             :         ++pos;      // skip the actual '/'
      65             :     }
      66             : 
      67             :     // ignore prefix if present
      68             :     //
      69             :     if(prefix.length() <= path.length() - pos
      70             :     && path.compare(pos, prefix.length(), prefix) == 0)
      71             :     {
      72             :         pos += prefix.length();
      73             :     }
      74             : 
      75             :     // if the path ends with suffix, then return the path without it
      76             :     //
      77             :     if(suffix.length() <= path.length() - pos
      78             :     && path.compare(path.length() - suffix.length(), suffix.length(), suffix) == 0)
      79             :     {
      80             :         return path.substr(pos, path.length() - pos - suffix.length());
      81             :     }
      82             : 
      83             :     // no suffix in this case
      84             :     //
      85             :     return path.substr(pos);
      86             : }
      87             : 
      88             : 
      89             : /** \brief Retrieve the directory name of a path.
      90             :  *
      91             :  * This function retrieves the directory name of a path. The returned path
      92             :  * is the empty string if the input does not include any '/'.
      93             :  *
      94             :  * \code
      95             :  *     // the following returns true
      96             :  *     snap::string_pathinfo_dirname(
      97             :  *         "/usr/share/snapwebsites/in.filename.txt");
      98             :  *             == "/usr/share/snapwebsites";
      99             :  * \endcode
     100             :  *
     101             :  * \param[in] path  The path from which basename gets retrieved.
     102             :  *
     103             :  * \return The directory name of \p path.
     104             :  */
     105             : template < class StringT >
     106           2 : StringT string_pathinfo_dirname(StringT const & path)
     107             : {
     108           2 :     typename StringT::size_type pos(path.find_last_of('/'));
     109           2 :     if(pos == StringT::npos)
     110             :     {
     111           0 :         return StringT();
     112             :     }
     113           2 :     else if(pos == 0)
     114             :     {
     115           0 :         if(path[0] == '/')
     116             :         {
     117           0 :             return StringT("/");
     118             :         }
     119           0 :         return StringT(".");
     120             :     }
     121             :     else
     122             :     {
     123           2 :         return path.substr(0, pos);
     124             :     }
     125             : }
     126             : 
     127             : } // namespace snap
     128             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13