Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Wave: a Standard conformant C++ preprocessor library

, 10 Jan 2004
Describes a free and fully Standard conformant C++ preprocessor library
wave_preprocessor_demo.zip
wave_preprocessor_demo1.zip
wave_preprocessor_src.zip
wave
doc
theme
bkd.gif
bkd2.gif
bullet.gif
l_arr.gif
l_arr_disabled.gif
r_arr.gif
r_arr_disabled.gif
uc.gif
u_arr.gif
wave.gif
test
boost-build.jam
cpp_tokens
Jamfile.v2
list_includes
project-root.jam
wave
Jamfile
Jamfile.v2
runtests.sh
test_files
wave
cpplexer
re2clex
cpp.re
test
lextest.in
lextest.output
run_tests.sh
test_lexer.in
test_lexer.output
slex
test
lextest.in
lextest.re2c.output
lextest.slex.output
run_tests.sh
grammars
idllexer
re2clex
idl.re
util
wave_preprocessor_src1.zip
bkd.gif
bkd2.gif
bullet.gif
l_arr.gif
l_arr_disabled.gif
r_arr.gif
r_arr_disabled.gif
u_arr.gif
uc.gif
wave.gif
boost-build.jam
Jamfile.v2
project-root.jam
Jamfile
Jamfile.v2
runtests.sh
cpp.re
lextest.in
lextest.output
run_tests.sh
test_lexer.in
test_lexer.output
lextest.in
lextest.re2c.output
lextest.slex.output
run_tests.sh
/*=============================================================================
    Wave: A Standard compliant C++ preprocessor

    Copyright (c) 2001-2004 Hartmut Kaiser
    http://spirit.sourceforge.net/

    Use, modification and distribution is subject to the Boost Software
    License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
    http://www.boost.org/LICENSE_1_0.txt)

    See Copyright.txt for full acknowledgements.
=============================================================================*/

#if !defined(CPP_INCLUDE_PATHES_HPP_AF620DA4_B3D2_4221_AD91_8A1ABFFB6944_INCLUDED)
#define CPP_INCLUDE_PATHES_HPP_AF620DA4_B3D2_4221_AD91_8A1ABFFB6944_INCLUDED

#include <string>
#include <list>
#include <set>

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

///////////////////////////////////////////////////////////////////////////////
namespace wave {
namespace util {

///////////////////////////////////////////////////////////////////////////////
//
//  include_pathes - controlling the include path search order
//
//  General notes:
//
//      Any directories specified with the 'add_include_path()' function before 
//      the function 'set_sys_include_delimiter()' is called are searched only 
//      for the case of '#include�"file"' directives, they are not searched for 
//      '#include�<file>' directives. If additional directories are specified 
//      with the 'add_include_path()' function after a call to the function 
//      'set_sys_include_delimiter()', these directories are searched for all 
//      '#include' directives. 
//
//      In addition, a call to the function 'set_sys_include_delimiter()' 
//      inhibits the use of the current directory as the first search directory 
//      for '#include�"file"' directives. Therefore, the current directory is 
//      searched only if it is requested explicitly with a call to the function
//      'add_include_path(".")'. 
//
//      Calling both functions, the 'set_sys_include_delimiter()' and 
//      'add_include_path(".")' allows you to control precisely which 
//      directories are searched before the current one and which are searched 
//      after.
//
///////////////////////////////////////////////////////////////////////////////
class include_pathes
{
    typedef std::list<boost::filesystem::path> include_list_t;
    typedef std::set<std::string> pragma_once_set_t;
    
public:
    include_pathes()
    :   was_sys_include_path(false),
        current_dir(boost::filesystem::initial_path())
    {}
    
    bool add_include_path(char const *path_, bool is_system = false)
    {
        return add_include_path(path_, (is_system || was_sys_include_path) ? 
            system_include_pathes : user_include_pathes);
    }
    void set_sys_include_delimiter() { was_sys_include_path = true; }
    bool find_include_file (std::string &s, bool is_system) const;
    void set_current_directory(char const *path_);

    void init_initial_path() { boost::filesystem::initial_path(); }
    
protected:
    bool find_include_file (std::string &s, include_list_t const &pathes) const;
    bool add_include_path(char const *path_, include_list_t &pathes_);

private:
    include_list_t user_include_pathes;
    include_list_t system_include_pathes;
    bool was_sys_include_path;          // saw a set_sys_include_delimiter()
    boost::filesystem::path current_dir;

#if defined(WAVE_SUPPORT_PRAGMA_ONCE)
public:
    bool has_pragma_once(std::string const &filename)
    {
        return pragma_once_files.find(filename) != pragma_once_files.end();
    }
    bool add_pragma_once_header(std::string const &filename)
    {
        return pragma_once_files.insert(filename).second;
    }

private:
    pragma_once_set_t pragma_once_files;
#endif // defined(WAVE_SUPPORT_PRAGMA_ONCE)
};

///////////////////////////////////////////////////////////////////////////////
//  Add an include path to one of the search lists (user include path or system 
//  include path).
inline
bool include_pathes::add_include_path (
    char const *path_, include_list_t &pathes_)
{
    namespace fs = boost::filesystem;
    if (path_) {
    fs::path newpath = fs::complete(fs::path(path_, fs::native), current_dir);

        if (!fs::exists(newpath) || !fs::is_directory(newpath)) {
        // the given path does not form a name of a valid file system directory
        // item
            return false;
        }

        pathes_.push_back (newpath);
        return true;
    }
    return false;
}

///////////////////////////////////////////////////////////////////////////////
//  Find an include file by traversing the list of include directories
inline
bool include_pathes::find_include_file (std::string &s, 
    include_list_t const &pathes) const
{
    namespace fs = boost::filesystem;
    typedef include_list_t::const_iterator const_include_list_iter_t;
    
    const_include_list_iter_t include_pathes_end = pathes.end();
    for (const_include_list_iter_t it = pathes.begin(); 
         it != include_pathes_end; ++it)
    {
        fs::path currpath (*it);
        currpath /= s;      // append filename

        if (fs::exists(currpath)) {
        // found the required file
            s = currpath.string();
            return true;
        }
    }
    return false;
}

///////////////////////////////////////////////////////////////////////////////
//  Find an include file by searching the user and system includes in the 
//  correct sequence (as it was configured by the user of the driver program)
inline
bool include_pathes::find_include_file (std::string &s, bool is_system) const
{
    namespace fs = boost::filesystem;
    
// if not system include (<...>), then search current directory first
    if (!is_system) {
        if (!was_sys_include_path) {  // set_sys_include_delimiter() not called
        // first look in the current directory
            fs::path currpath = current_dir;
            currpath /= s;
            if (fs::exists(currpath)) {
                s = currpath.string();    // found in local directory
                return true;
            }   

        // iterate all user include file directories to find the file
            return find_include_file(s, user_include_pathes);
        }

    // iterate all user include file directories to find the file
        if (find_include_file(s, user_include_pathes))
            return true;

    // if nothing found, fall through
    // ...
    }

// iterate all system include file directories to find the file
    return find_include_file (s, system_include_pathes);
}

///////////////////////////////////////////////////////////////////////////////
//  Set current directory from a given file name

inline
void include_pathes::set_current_directory(char const *path_)
{
    namespace fs = boost::filesystem;
    
    fs::path filename = fs::complete(fs::path(path_, fs::native), current_dir);
    if (fs::exists(filename) && fs::is_directory(filename)) 
        current_dir = filename;
    else
        current_dir = filename.branch_path();
}

///////////////////////////////////////////////////////////////////////////////
}   // namespace util
}   // namespace wave

#endif // !defined(CPP_INCLUDE_PATHES_HPP_AF620DA4_B3D2_4221_AD91_8A1ABFFB6944_INCLUDED)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Hartmut Kaiser

United States United States
Actively involved in Boost and the development of the Spirit parser construction framework.

| Advertise | Privacy | Mobile
Web03 | 2.8.140821.2 | Last Updated 11 Jan 2004
Article Copyright 2003 by Hartmut Kaiser
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid