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 #include <iterator>
25 #include <iostream>
26 #include <core/exception.h>
27 #include <eclipseclass.h>
28 
29 using namespace boost::filesystem;
30 using namespace fawkes;
31 
32 /** @class EclipsePath
33  * Class to determine the location of ECLiPSe-clp programs.
34  * Given a filename the complete path to that file will be specified.
35  * Paths can contain variables, which will be transformed to the string
36  * if matched by a regex.
37  * @author Gesche Gierse
38  * @author Tim Niemueller
39  */
40 
41 
42 EclipsePath* EclipsePath::m_instance = NULL;
43 
44 
45 /** Constructor. */
46 EclipsePath::EclipsePath()
47 {
48 }
49 
50 /** Create the initial EclipsePath object.
51  * Already supplies regexes for BASEDIR, CONFDIR and FAWKES_BASEDIR
52  */
53 void
55 {
56  if (m_instance) return;
57 
58  m_instance = new EclipsePath();
59  m_instance->add_regex(boost::regex("@BASEDIR@"), BASEDIR);
60  m_instance->add_regex(boost::regex("@CONFDIR@"), CONFDIR);
61  m_instance->add_regex(boost::regex("@FAWKESDIR@"), FAWKES_BASEDIR);
62 }
63 
64 /** Get the EclipsePath instance.
65  * @return the instance
66  */
68 {
69  create_initial_object();
70  return m_instance;
71 }
72 
73 
74 
75 /** Add a new path.
76  * @param path The path to be added.
77  */
78 void
79 EclipsePath::add_path(std::string path)
80 {
81  paths.push_back(path);
82 }
83 
84 
85 /** Add a new path and apply regexes to all paths.
86  * @param path The path to be added.
87  */
88 void
89 EclipsePath::add_path_check(std::string path)
90 {
91  instance()->add_path(path);
92  instance()->apply_regexes();
93 }
94 
95 /** Locate a file by filename
96  * @param filename the searched filename
97  * @return path to the file
98  */
99 std::string
100 EclipsePath::locate_file(std::string filename)
101 {
102  if (paths.empty()){
103  return "";
104  }
105  //std::cout << "locate file: " << filename << '\n';
106  for (std::vector<std::string>::iterator it = paths.begin(); it != paths.end(); ++it){
107  path p (*it);
108  p /= filename;
109  //std::cout << "locate file: created path for:" << p.native() << '\n' ;
110  try {
111  if (exists(p)) {
112  //std::cout << "found file " << filename << " at:" << '\n';
113 #ifdef BOOST_FILESYSTEM_VERSION
114  return p.native();
115 #else
116  return p.string();
117 #endif
118  }
119  }
120  catch (const filesystem_error& ex)
121  {
122  throw Exception( "Filesystem error" );
123  }
124 
125  }
126  return "";
127 }
128 
129 /** Apply the regexes to all paths.
130  */
131 void
133 {
134  int i;
135  std::vector<std::string>::iterator it;
136  for (i = 0, it = paths.begin(); it != paths.end(); ++it, i++){
137  for (std::map<boost::regex,std::string>::iterator re=regexes.begin(); re!=regexes.end(); ++re){
138  std::string result = boost::regex_replace(*it, re->first, re->second);
139  //std::cout << "path: " << paths[i] << '\n';
140  paths[i]=result;
141  //std::cout << "applying: " << re->first << "=>" << re->second << "\nregex result:" << result << '\n';
142 
143  }
144  }
145 }
146 
147 /** Debug method to print all path to the command line.
148  */
149 void
151 {
152  for ( std::vector<std::string>::iterator it = paths.begin(); it != paths.end(); ++it){
153  //std::cout << *it << '\n';
154  }
155 }
156 
157 /** Add a regex. To apply the regex to all paths use
158  * apply_regexes().
159  * @param re the regex to be matched
160  * @param str the string by which each instanstance of the regex will be replaced
161  */
162 void
163 EclipsePath::add_regex(boost::regex re, std::string str)
164 {
165  regexes.insert( std::pair<boost::regex,std::string>(re, str) );
166 }
167 
168 /** Wrapper method for external ECLiPSe-clp.
169  * Returns the path to a file.
170  * locate_file(+filename,-result)
171  */
172 int
173 p_locate_file(...)
174 {
175  char* filename;
176  if ( EC_succeed != EC_arg(1).is_string ( &filename ) )
177  {
178  printf( "p_locate_file(): no filename given\n" );
179  }
180  std::string p = EclipsePath::instance()->locate_file(filename);
181  if (EC_succeed != EC_arg(2).unify( EC_word(p.c_str()) ) ){
182  printf( "p_locate_file(): could not bind return valie\n" );
183  return EC_fail;
184  }
185  return p.empty() ? EC_fail : EC_succeed;
186 }
void apply_regexes()
Apply the regexes to all paths.
void add_regex(boost::regex re, std::string str)
Add a regex.
static EclipsePath * instance()
Get the EclipsePath instance.
Fawkes library namespace.
void print_all_paths()
Debug method to print all path to the command line.
Class to determine the location of ECLiPSe-clp programs.
Definition: eclipse_path.h:31
Base class for exceptions in Fawkes.
Definition: exception.h:36
static void create_initial_object()
Create the initial EclipsePath object.
void add_path_check(std::string path)
Add a new path and apply regexes to all paths.
std::string locate_file(std::string filename)
Locate a file by filename.
void add_path(std::string path)
Add a new path.