Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) SimplifyPath

 

SimplifyPath is a std::string and file I/O code snippet to remove a filename's path.

 

 

 

 

 

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: CppSimplifyPath.pro

 

#-------------------------------------------------
#
# Project created by QtCreator 2012-05-30T10:52:19
#
#-------------------------------------------------

QT       += core

QT       -= gui

TARGET = CppSimplifyPath
CONFIG   += console
CONFIG   -= app_bundle

LIBS += -lboost_regex

TEMPLATE = app


SOURCES += main.cpp

 

 

 

 

 

main.cpp

 

#include <boost/regex.hpp>

///Simplify a path
///For example,
/// /home/richel/Projects/Tools/ToolTestProFile/../../Classes/CppQtAboutDialog/qtaboutdialog.ui
///is converted to
/// /home/richel/Projects/Classes/CppQtAboutDialog/qtaboutdialog.ui
//From http://www.richelbilderbeek.nl/CppSimplifyPath.htm
const std::string SimplifyPath(const std::string& s)
{

  std::string t = s;
  while (1)
  {
    const std::string new_t = boost::regex_replace(t,boost::regex("/\\w*/../"),"/");
    if (t == new_t) return t;
    t = new_t;
  }
}

#include <cassert>

int main()
{
  assert(SimplifyPath("/home/richel/Projects/Tools/ToolTestProFile/../../Classes/CppQtAboutDialog/qtaboutdialog.ui")
    == "/home/richel/Projects/Classes/CppQtAboutDialog/qtaboutdialog.ui");
  assert(SimplifyPath("/home/richel/Projects/Tools/../Classes/CppQtAboutDialog/qtaboutdialog.ui")
    == "/home/richel/Projects/Classes/CppQtAboutDialog/qtaboutdialog.ui");
  assert(SimplifyPath("/home/richel/Projects/Classes/CppQtAboutDialog/qtaboutdialog.ui")
    == "/home/richel/Projects/Classes/CppQtAboutDialog/qtaboutdialog.ui");
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml