/***************************************************************************//**
 *
 * \file rosa/config/project_path.hpp
 *
 * \author  David Juhasz (david.juhasz@tuwien.ac.at)
 *
 * \date 2017
 *
 * \brief Facility for compile-time manipulation of paths.
 *
 ******************************************************************************/

#ifndef ROSA_CONFIG_PROJECT_PATH_HPP
#define ROSA_CONFIG_PROJECT_PATH_HPP

#include "rosa/config/rosa_config.h"

#include <cstdlib>

namespace rosa {

/// Nested namespace with implementation of the provided features,
/// consider it private.
namespace {

/// Tells the index of the project-relative part of an absolute path.
///
/// \param path absolute path to check
/// \param project the absolute path of the project
/// \param index number of leading characters already checked and found
///        matching
///
/// \return index of the `project`-relative part of `path`; `0` if `path` is not
///         under `project`
constexpr size_t
project_relative_path_index_impl(const char *const path,
                                 const char *const project = ROSA_SRC_DIR,
                                 const size_t index = 0) {
  return project[index] == '\0'
             ? index // Found it.
             : (path[index] == '\0' || project[index] != path[index])
                   ? 0 // Path is not under project.
                   : project_relative_path_index_impl(
                         path, project,
                         index + 1); // Continue searching...
}

} // End namespace

/// Tells the index of the project-relative part of an absolute path.
///
/// \param path absolute path to check
///
/// \return index of the project-relative part of `path`; `0` if `path` is not
///         under the RoSA siource directory.
constexpr size_t project_relative_path_index(const char * const path) {
  return project_relative_path_index_impl(path);
}

} // End namespace rosa

#endif // ROSA_CONFIG_PROJECT_PATH_HPP

