MADARA  3.4.1
SystemCallWriteFile.cpp
Go to the documentation of this file.
1 
2 #ifndef _MADARA_NO_KARL_
3 
7 
10  : SystemCallNode(context, nodes)
11 {
12 }
13 
14 // Dtor
16 
19 {
20  return madara::knowledge::KnowledgeRecord(nodes_.size());
21 }
22 
28 {
29  // user can always change a function, and we have no control over
30  // what it does. Consequently, a function node cannot be pruned out
31  // under any situation
32  can_change = true;
33 
35 
36  for (ComponentNodes::iterator i = nodes_.begin(); i != nodes_.end(); ++i)
37  {
38  bool arg_can_change = false;
39  result = (*i)->prune(arg_can_change);
40 
41  if (!arg_can_change && dynamic_cast<LeafNode*>(*i) == 0)
42  {
43  delete *i;
44  *i = new LeafNode(*(this->logger_), result);
45  }
46  }
47 
48  if (nodes_.size() != 2)
49  {
51  "madara::expression::SystemCallWriteFile: "
52  "KARL COMPILE ERROR:"
53  "System call write_file requires 2 arguments: "
54  "a knowledge record and a file name\n");
55 
57  "madara::expression::SystemCallWriteFile: "
58  "KARL COMPILE ERROR: "
59  "System call write_file requires 2 arguments: "
60  "a knowledge record and a file name\n");
61  }
62 
63  return result;
64 }
65 
71 {
72  knowledge::KnowledgeRecord return_value;
73 
74  if (nodes_.size() == 2)
75  {
76  // copying strings wastes execution time, so we hold the
77  // knowledge::KnowledgeRecord instead of the resulting string filename.
78  knowledge::KnowledgeRecord arg1 = nodes_[0]->evaluate(settings);
79  knowledge::KnowledgeRecord arg2 = nodes_[1]->evaluate(settings);
80 
81  knowledge::KnowledgeRecord* filename = &arg1;
82  knowledge::KnowledgeRecord* contents = &arg2;
83 
85  "madara::expression::SystemCallWriteFile: "
86  "System call write_file is attempting to open %s.\n",
87  filename->to_string().c_str());
88 
89  ssize_t bytes_written = contents->to_file(filename->to_string());
90 
91  if (bytes_written <= 0)
92  {
94  "madara::expression::SystemCallWriteFile: "
95  "KARL ERROR: System call write_file could not write to %s\n",
96  filename->to_string().c_str());
97 
98  return madara::knowledge::KnowledgeRecord(bytes_written);
99  }
100  else
101  {
103  "madara::expression::SystemCallWriteFile: "
104  "System call write_file wrote %zd bytes to %s\n",
105  bytes_written, filename->to_string().c_str());
106  }
107  }
108  else
109  {
111  "madara::expression::SystemCallWriteFile: "
112  "KARL RUNTIME ERROR:"
113  "System call write_file requires 2 arguments: "
114  "a knowledge record and a file name\n");
115 
117  "madara::expression::SystemCallWriteFile: "
118  "KARL RUNTIME ERROR: "
119  "System call write_file requires 2 arguments: "
120  "a knowledge record and a file name\n");
121  }
122 
123  return return_value;
124 }
125 
126 // accept a visitor
128  madara::expression::Visitor& visitor) const
129 {
130  visitor.visit(*this);
131 }
132 
133 #endif // _MADARA_NO_KARL_
#define madara_logger_ptr_log(loggering, level,...)
Fast version of the madara::logger::log method for Logger pointers.
Definition: Logger.h:41
madara::knowledge::KnowledgeRecord KnowledgeRecord
An exception for unrecoverable KaRL compilation issues.
Definition: KarlException.h:21
Defines a node that contains a madara::knowledge::KnowledgeRecord::Integer value.
Definition: LeafNode.h:25
Interface for a MADARA system call.
virtual void accept(Visitor &visitor) const
Accepts a visitor subclassed from the Visitor class.
virtual madara::knowledge::KnowledgeRecord prune(bool &can_change)
Prunes the expression tree of unnecessary nodes.
virtual madara::knowledge::KnowledgeRecord evaluate(const madara::knowledge::KnowledgeUpdateSettings &settings)
Evaluates the expression tree.
virtual madara::knowledge::KnowledgeRecord item(void) const
Returns the value of the node.
SystemCallWriteFile(madara::knowledge::ThreadSafeContext &context, const ComponentNodes &nodes)
Constructor.
Abstract base class for all visitors to all classes that derive from ComponentNode.
Definition: Visitor.h:93
virtual void visit(const LeafNode &node)=0
Visit a LeafNode.
This class encapsulates an entry in a KnowledgeBase.
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
ssize_t to_file(const std::string &filename) const
writes the value to a file
Settings for applying knowledge updates.
This class stores variables and their values for use by any entity needing state information in a thr...
std::deque< ComponentNode * > ComponentNodes
a vector of Component Nodes