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