Fawkes API Fawkes Development Version
eclipse_path.cpp
1
2/***************************************************************************
3 * eclipse_path.cpp - Eclipse-CLP path externals
4 *
5 * Created: Thu Feb 27 15:21:35 2014
6 * Copyright 2014 Gesche Gierse
7 * 2014 Tim Niemueller
8 ****************************************************************************/
9
10/* This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Library General Public License for more details.
19 *
20 * Read the full text in the LICENSE.GPL file in the doc directory.
21 */
22
23#include "eclipse_path.h"
24
25#include <core/exception.h>
26
27#include <eclipseclass.h>
28#include <iostream>
29#include <iterator>
30
31using namespace boost::filesystem;
32using namespace fawkes;
33
34/** @class EclipsePath
35 * Class to determine the location of ECLiPSe-clp programs.
36 * Given a filename the complete path to that file will be specified.
37 * Paths can contain variables, which will be transformed to the string
38 * if matched by a regex.
39 * @author Gesche Gierse
40 * @author Tim Niemueller
41 */
42
43EclipsePath *EclipsePath::m_instance = NULL;
44
45/** Constructor. */
46EclipsePath::EclipsePath()
47{
48}
49
50/** Create the initial EclipsePath object.
51 * Already supplies regexes for BASEDIR, CONFDIR and FAWKES_BASEDIR
52 */
53void
55{
56 if (m_instance)
57 return;
58
59 m_instance = new EclipsePath();
60 m_instance->add_regex(boost::regex("@BASEDIR@"), BASEDIR);
61 m_instance->add_regex(boost::regex("@CONFDIR@"), CONFDIR);
62 m_instance->add_regex(boost::regex("@FAWKESDIR@"), FAWKES_BASEDIR);
63}
64
65/** Get the EclipsePath instance.
66 * @return the instance
67 */
70{
72 return m_instance;
73}
74
75/** Add a new path.
76 * @param path The path to be added.
77 */
78void
79EclipsePath::add_path(const std::string &path)
80{
81 paths.push_back(path);
82}
83
84/** Add a new path and apply regexes to all paths.
85 * @param path The path to be added.
86 */
87void
88EclipsePath::add_path_check(const std::string &path)
89{
90 instance()->add_path(path);
92}
93
94/** Locate a file by filename
95 * @param filename the searched filename
96 * @return path to the file
97 */
98std::string
99EclipsePath::locate_file(const std::string &filename)
100{
101 if (paths.empty()) {
102 return "";
103 }
104 //std::cout << "locate file: " << filename << '\n';
105 for (std::vector<std::string>::iterator it = paths.begin(); it != paths.end(); ++it) {
106 path p(*it);
107 p /= filename;
108 //std::cout << "locate file: created path for:" << p.native() << '\n' ;
109 try {
110 if (exists(p)) {
111 //std::cout << "found file " << filename << " at:" << '\n';
112#ifdef BOOST_FILESYSTEM_VERSION
113 return p.native();
114#else
115 return p.string();
116#endif
117 }
118 } catch (const filesystem_error &ex) {
119 throw Exception("Filesystem error");
120 }
121 }
122 return "";
123}
124
125/** Apply the regexes to all paths.
126 */
127void
129{
130 int i;
131 std::vector<std::string>::iterator it;
132 for (i = 0, it = paths.begin(); it != paths.end(); ++it, i++) {
133 for (std::map<boost::regex, std::string>::iterator re = regexes.begin(); re != regexes.end();
134 ++re) {
135 std::string result = boost::regex_replace(*it, re->first, re->second);
136 //std::cout << "path: " << paths[i] << '\n';
137 paths[i] = result;
138 //std::cout << "applying: " << re->first << "=>" << re->second << "\nregex result:" << result << '\n';
139 }
140 }
141}
142
143/** Debug method to print all path to the command line.
144 */
145void
147{
148 for (std::vector<std::string>::iterator it = paths.begin(); it != paths.end(); ++it) {
149 //std::cout << *it << '\n';
150 }
151}
152
153/** Add a regex. To apply the regex to all paths use
154 * apply_regexes().
155 * @param re the regex to be matched
156 * @param str the string by which each instanstance of the regex will be replaced
157 */
158void
159EclipsePath::add_regex(boost::regex re, const std::string &str)
160{
161 regexes.insert(std::make_pair(re, str));
162}
163
164/** Wrapper method for external ECLiPSe-clp.
165 * Returns the path to a file.
166 * locate_file(+filename,-result)
167 */
168int
169p_locate_file(...)
170{
171 char *filename;
172 if (EC_succeed != EC_arg(1).is_string(&filename)) {
173 printf("p_locate_file(): no filename given\n");
174 }
175 std::string p = EclipsePath::instance()->locate_file(filename);
176 if (EC_succeed != EC_arg(2).unify(EC_word(p.c_str()))) {
177 printf("p_locate_file(): could not bind return valie\n");
178 return EC_fail;
179 }
180 return p.empty() ? EC_fail : EC_succeed;
181}
Class to determine the location of ECLiPSe-clp programs.
Definition: eclipse_path.h:32
void add_path(const std::string &path)
Add a new path.
static EclipsePath * instance()
Get the EclipsePath instance.
std::map< boost::regex, std::string > regexes
regexes and strings they should be replaced with
Definition: eclipse_path.h:52
std::string locate_file(const std::string &filename)
Locate a file by filename.
static void create_initial_object()
Create the initial EclipsePath object.
void add_path_check(const std::string &path)
Add a new path and apply regexes to all paths.
void print_all_paths()
Debug method to print all path to the command line.
void apply_regexes()
Apply the regexes to all paths.
void add_regex(boost::regex re, const std::string &str)
Add a regex.
std::vector< std::string > paths
all paths known
Definition: eclipse_path.h:50
Base class for exceptions in Fawkes.
Definition: exception.h:36
Fawkes library namespace.