/***
g++ -ftemplate-depth-128 -O0 -fno-inline -g -Wall -Wextra -Wsign-compare -Wconversion -Wpointer-arith -Wcomment -std=c++0x \
 -I/opt/local/include -DBOOST_ALL_NO_LIB=1 -DBOOST_DISABLE_THREADS \
 pathTricks.cpp -lboost_system -lboost_filesystem -o pathTricks

$sh> ./pathTricks
"/tmp/some/deep/application/folder/../configuration/instance/../instance/myfile.cfg"
"/tmp/some/deep/application/configuration/instance/myfile.cfg"
/private/tmp/some/deep/application/configuration/instance/myfile.cfg does not exists!

 ***/

#include <boost/filesystem/operations.hpp>
#include <iostream>

#ifndef _WIN32
#include <limits.h> // PATH_MAX
#include <stdlib.h> // realpath
#endif

namespace bf = boost::filesystem;

int main() {

#ifdef _WIN32
    bf::path root("c:\\some\\deep\\application\\folder");
    bf::path subdir("..\\configuration\\instance");
    bf::path cfgfile("..\\instance\\myfile.cfg");
#else // _WIN32
    bf::path root("/tmp/some/deep/application/folder");
    bf::path subdir("../configuration/instance");
    bf::path cfgfile("../instance/myfile.cfg");
#endif // _WIN32

    bf::path final(root / subdir / cfgfile);

    std::cout << final << std::endl;

    // Canonical path: An absolute path that has no elements which are symbolic
    // links, and no dot or dot dot elements.
#ifndef BOOST_FILESYSTEM_NO_DEPRECATED
    std::cout << final.normalize() << std::endl;
#else
    //FIXME: is unresolved with boost_1_48_0? ck
    // Converts p, which must exist, to an absolute path that has no symbolic
    // link, dot, or dot-dot elements.
    boost::system::error_code ec;
    std::cout << bf::canonical(final, ec) << std::endl;
#endif

#ifndef _WIN32
    char buf[PATH_MAX];
    char * ok = realpath(final.c_str(), buf);
    if (ok) {
        std::cout << buf << std::endl;
    } else {
        std::cerr << buf << " does not exists!" << std::endl;
    }
#endif

    return 0;
}

