OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
load-file.cc
Go to the documentation of this file.
1 /*
2  * load-file.cc
3  *
4  * Utility helper function -- load scheme code from a file
5  * Copyright (c) 2008 Linas Vepstas <linasvepstas@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License v3 as
9  * published by the Free Software Foundation and including the exceptions
10  * at http://opencog.org/wiki/Licenses
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program; if not, write to:
19  * Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #ifdef HAVE_GUILE
24 
25 #include <errno.h>
26 #include <stdio.h>
27 #include <string.h>
28 
29 #include <boost/filesystem/operations.hpp>
30 
32 #include <opencog/util/Config.h>
33 #include <opencog/util/files.h>
34 #include <opencog/util/Logger.h>
35 #include <opencog/util/misc.h>
36 
37 namespace opencog {
38 
46 int load_scm_file (AtomSpace& as, const std::string& filename)
47 {
48  SchemeEval evaluator(&as);
49 
50  evaluator.begin_eval();
51 
52  std::string load_exp("(load \"");
53  load_exp += filename + "\")";
54  evaluator.eval_expr(load_exp.c_str());
55 
56  std::string rv = evaluator.poll_result();
57  if (evaluator.eval_error()) {
58  printf("Error: %s\n", rv.c_str());
59  return 1;
60  }
61 
62  return 0;
63 }
64 
70 int load_scm_file_relative (AtomSpace& as, const std::string& filename,
71  std::vector<std::string> search_paths)
72 {
73  if (search_paths.empty()) {
74  // Sometimes paths are given without the "opencog" part.
75  // Also check the build directory for autogen'ed files.
76  // XXX This is fairly tacky/broken, and needs a better fix.
77  for (auto p : DEFAULT_MODULE_PATHS) {
78  search_paths.push_back(p);
79  search_paths.push_back(p + "/examples/rule-engine");
80  search_paths.push_back(p + "/opencog");
81  search_paths.push_back(p + "/build");
82  search_paths.push_back(p + "/build/opencog");
83  }
84  }
85 
86  int rc = 2;
87  for (const std::string& search_path : search_paths) {
88  boost::filesystem::path modulePath(search_path);
89  modulePath /= filename;
90  logger().debug("Searching path %s", modulePath.string().c_str());
91  if (boost::filesystem::exists(modulePath)) {
92  rc = load_scm_file(as, modulePath.string());
93  if (0 == rc) {
94  logger().info("Loaded %s", modulePath.string().c_str());
95  break;
96  }
97  }
98  }
99 
100  if (rc)
101  {
102  logger().warn("Failed to load file %s: %d %s",
103  filename.c_str(), rc, strerror(rc));
104  }
105  return rc;
106 }
107 
113  std::vector<std::string> search_paths)
114 {
115  // Load scheme modules specified in the config file
116  std::vector<std::string> scm_modules;
117  tokenize(config()["SCM_PRELOAD"], std::back_inserter(scm_modules), ", ");
118 
119  for (const std::string& scm_module : scm_modules)
120  load_scm_file_relative(atomSpace, scm_module, search_paths);
121 }
122 
123 }
124 #endif /* HAVE_GUILE */
void eval_expr(const std::string &)
Definition: SchemeEval.cc:490
std::string poll_result()
Definition: SchemeEval.cc:523
virtual bool eval_error(void)
Definition: GenericEval.h:75
void load_scm_files_from_config(AtomSpace &atomSpace, std::vector< std::string > search_paths)
Definition: load-file.cc:112
int load_scm_file(AtomSpace &as, const std::string &filename)
Definition: load-file.cc:46
int load_scm_file_relative(AtomSpace &as, const std::string &filename, std::vector< std::string > search_paths)
Definition: load-file.cc:70