MADARA  3.1.8
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  "KARL ERROR: System call write_file requires 2 arguments: "
59  "a knowledge record and a file name\n");
60  }
61 
62  return result;
63 }
64 
70 {
71  knowledge::KnowledgeRecord return_value;
72 
73  if (nodes_.size () == 2)
74  {
75  // copying strings wastes execution time, so we hold the knowledge::KnowledgeRecord
76  // instead of the resulting string filename.
77  knowledge::KnowledgeRecord arg1 = nodes_[0]->evaluate (settings);
78  knowledge::KnowledgeRecord arg2 = nodes_[1]->evaluate (settings);
79 
80  knowledge::KnowledgeRecord * filename = &arg1;
81  knowledge::KnowledgeRecord * contents = &arg2;
82 
84  "System call write_file is attempting to open %s.\n",
85  filename->to_string ().c_str ());
86 
87  ssize_t bytes_written = contents->to_file (filename->to_string ());
88 
89  if (bytes_written <= 0)
90  {
92  "KARL ERROR: System call write_file could not write to %s\n",
93  filename->to_string ().c_str ());
94 
95  return madara::knowledge::KnowledgeRecord (bytes_written);
96  }
97  else
98  {
100  "System call write_file wrote %zd bytes to %s\n",
101  bytes_written, filename->to_string ().c_str ());
102  }
103  }
104  else
105  {
107  "KARL ERROR: System call write_file requires 2 arguments: "
108  "a knowledge record and a file name\n");
109  }
110 
111  return return_value;
112 }
113 
114 // accept a visitor
115 void
117  madara::expression::Visitor &visitor) const
118 {
119  visitor.visit (*this);
120 }
121 
122 #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:96
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:90
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.
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.