CPN
Computational Process Networks
Namespaces | Functions | Variables
NodeLoader.cc File Reference
#include "common_priv.h"
#include <cpn/bits/NodeLoader.h>
#include <cpn/NodeFactory.h>
#include <cpn/utils/ThrowingAssert.h>
#include <cpn/utils/PathUtils.h>
#include <cpn/utils/Directory.h>
#include <cpn/threading/PthreadMutex.h>
#include <fstream>
#include <stdexcept>
#include <ltdl.h>
+ Include dependency graph for NodeLoader.cc:

Go to the source code of this file.

Namespaces

 CPN
 

Functions

void Loader (const std::string &ldfile, std::map< std::string, std::string > &data)
 
void ProcessLine (vector< string > &stack, const string &curdir, map< string, string > &data, unsigned rep)
 
void Loader (const string &ldfile, map< string, string > &data, unsigned rep)
 

Variables

static PthreadMutex lock
 

Detailed Description

Author
John Bridgman

Definition in file NodeLoader.cc.

Function Documentation

void Loader ( const std::string &  ldfile,
std::map< std::string, std::string > &  data 
)

Definition at line 314 of file NodeLoader.cc.

Referenced by CPN::NodeLoader::LoadNodeList(), and ProcessLine().

314  {
315  Loader(ldfile, data, 0);
316 }
void Loader(const std::string &ldfile, std::map< std::string, std::string > &data)
Definition: NodeLoader.cc:314

+ Here is the caller graph for this function:

void Loader ( const string &  ldfile,
map< string, string > &  data,
unsigned  rep 
)

Definition at line 198 of file NodeLoader.cc.

References ASSERT, DirName(), ProcessLine(), and RealPath().

198  {
199  ASSERT(rep < 100, "Loader include overflow");
200  ifstream f;
201  f.open(ldfile.c_str());
202  ASSERT(f.is_open(), "Failed to open %s", ldfile.c_str());
203  string curdir = DirName(RealPath(ldfile));
204  vector<char> token_stack;
205  vector<string> stack;
206  bool in_comment = false;
207  bool escape = false;
208  bool in_single_quote = false;
209  bool in_double_quote = false;
210  while (f.good()) {
211  int c = f.get();
212  if (!f.good()) {
213  break;
214  }
215  if (in_comment) {
216  if (c == '\n' || c == '\r') {
217  in_comment = false;
218  } else {
219  continue;
220  }
221  }
222  if (in_single_quote) {
223  if (c == '\'') {
224  in_single_quote = false;
225  } else {
226  token_stack.push_back(c);
227  }
228  } else if (escape) {
229  token_stack.push_back(c);
230  escape = false;
231  } else if (in_double_quote) {
232  if (c == '"') {
233  in_double_quote = false;
234  } else {
235  token_stack.push_back(c);
236  }
237  } else {
238  switch (c) {
239  case '#':
240  in_comment = true;
241  break;
242  case '\'':
243  in_single_quote = true;
244  break;
245  case '"':
246  in_double_quote = true;
247  break;
248  case '\\':
249  escape = true;
250  break;
251  case ';':
252  if (!token_stack.empty()) {
253  stack.push_back(string(&token_stack[0], token_stack.size()));
254  token_stack.clear();
255  }
256  ProcessLine(stack, curdir, data, rep);
257  break;
258  case '\n':
259  case '\r':
260  // end of line
261  case ' ':
262  case '\t':
263  case '\v':
264  case '\f':
265  // white space
266  if (!token_stack.empty()) {
267  // end the current token, otherwise nothing
268  stack.push_back(string(&token_stack[0], token_stack.size()));
269  token_stack.clear();
270  }
271  break;
272  default:
273  token_stack.push_back(c);
274  break;
275  }
276  }
277  }
278  ProcessLine(stack, curdir, data, rep);
279 }
void ProcessLine(vector< string > &stack, const string &curdir, map< string, string > &data, unsigned rep)
Definition: NodeLoader.cc:281
std::string RealPath(const std::string &path)
Definition: PathUtils.cc:29
std::string DirName(const std::string &path)
Definition: PathUtils.cc:39
#define ASSERT(exp,...)

+ Here is the call graph for this function:

void ProcessLine ( vector< string > &  stack,
const string &  curdir,
map< string, string > &  data,
unsigned  rep 
)

Definition at line 281 of file NodeLoader.cc.

References IsAbsPath(), Loader(), PathConcat(), and RealPath().

Referenced by Loader().

282  {
283  if (stack.empty()) return;
284  vector<string>::iterator itr = stack.begin();
285  if (*itr == "lib") {
286  ++itr;
287  if (itr != stack.end()) {
288  string libname = *itr;
289  if (!IsAbsPath(libname)) {
290  libname = RealPath(PathConcat(curdir, libname));
291  }
292  if (!libname.empty()) {
293  ++itr;
294  while (itr != stack.end()) {
295  data[*itr] = libname;
296  ++itr;
297  }
298  }
299  }
300  } else if (*itr == "include") {
301  ++itr;
302  while (itr != stack.end()) {
303  string inc = *itr;
304  if (!IsAbsPath(inc)) {
305  inc = RealPath(PathConcat(curdir, inc));
306  }
307  Loader(inc, data, rep + 1);
308  ++itr;
309  }
310  }
311  stack.clear();
312 }
void Loader(const std::string &ldfile, std::map< std::string, std::string > &data)
Definition: NodeLoader.cc:314
std::string RealPath(const std::string &path)
Definition: PathUtils.cc:29
std::string PathConcat(const std::string &dir, const std::string &file)
Definition: PathUtils.cc:85
bool IsAbsPath(const std::string &path)
Definition: PathUtils.cc:89

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Variable Documentation

PthreadMutex lock
static