LILAC
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
engine_read.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2014, Sam Schetterer, Nathan Kutz, University of Washington
3 Authors: Sam Schetterer
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9 
10 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
11 
12 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
13 
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 
16 */
17 #include "engine/engineimp.h"
18 #include "graph.h"
19 #include "engine/input.h"
20 #include "eval_lisp.h"
21 
22 //passed a string (type name params)
23 void engineimp::read(std::istream& fstr){
24  _read(fstr);
25  sort_pp();
26 }
27 void engineimp::_read(std::istream& fstr){
28  const std::string delim=" ";
29  const std::string comment="!";
30  const char command = '#';
31  std::string token;
32  std::string curline;
33  int line = 0;
34  while(fstr.good()){
35  //get current line
36  std::getline(fstr, curline);
37  line++;
38  //remove comments
39  size_t cpos = curline.find(comment);
40  if(cpos != std::string::npos){
41  curline.erase(cpos, std::string::npos);
42  }
43 
44  //trim leading and trailing whitespace
45  trim(curline);
46  //skip rest of loop if entire string deleted
47  if(curline.empty()){
48  continue;
49  }
50  //if the first character is a !, then a system command is being executed
51  if(curline[0] == command){
52  curline.erase(0, 1);
53  execute_command(curline);
54  continue;
55  }
56 
57  item_wrapper curit = eval_lisp(curline, std::string(), inputs, this);
58  values[curit->name()] = item_wrapper(curit);
59  }
60  //get the current line from the string
61 }
62 
67  std::list<item_wrapper> post_order;
68  for(const auto& val : values){
69  post_order.push_back(val.second);
70  }
71  graph gg;
72  auto sorted(gg.sort(post_order));
73  for(auto val:sorted){
74  val->postprocess(inputs[val->name()]);
75  }
76 }
77 
79  trim(inval);
80  std::string command;
81 #include <list>
82  ltoken(command, inval);
83  if(command == "include"){
84  //filename is the rest of the line
85  std::string fname = curdir;
86  fname.append(inval);
87  std::ifstream fstr(fname.c_str());
88  if(fstr.peek()==std::ifstream::traits_type::eof()){
89  fname.append("Empty/non-existant file passed as configuration parameter");
90  err(fname, "engineimp::engineimp(std::string)", "engine/engine.cpp", FATAL_ERROR);
91  }
92  _read(fstr);
93  }
94  else if(command=="cd"){
95  curdir.append(inval);
96  trim(curdir, "/");
97  curdir.push_back('/');
98  }
99  else if(command=="setdir"){
100  curdir=inval;
101  }
102 }
103