/*******************************************************************************
 *
 * File:     project_path.hpp
 *
 * Contents: Facility for compile-time manipulation of paths.
 *
 * Copyright 2017
 *
 * Author: David Juhasz (david.juhasz@tuwien.ac.at)
 *
 ******************************************************************************/

#ifndef ROSA_CONFIG_PROJECT_PATH_HPP
#define ROSA_CONFIG_PROJECT_PATH_HPP

#include "rosa/config/rosa_config.h"

namespace rosa {

// Nested namespace containing the implementation of the provided features,
// not supposed to be used directly.
namespace impl {

// Implementation of the static search for the project-relative part of an
// absolute path.
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 impl

// Staticaly finds the index of the project-relative part of a absolute path.
// Returns 0 if the absolute path is not located under the project directory.
constexpr size_t project_relative_path_index(const char * const path) {
  return impl::project_relative_path_index_impl(path);
}

} // End namespace rosa

#endif // ROSA_CONFIG_PROJECT_PATH_HPP

