MADARA  3.1.8
SystemCallReadFile.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 () == 0 || nodes_.size () > 2)
56  {
58  "KARL COMPILE ERROR: System call read_file"
59  " requires at least a filename to read, e.g."
60  " #read_file (filename), #read_file (filename, 'text')."
61  " Second argument is to force a file type when the filename"
62  " does not end with .txt, .xml, .jpg, etc. Can be 'text',"
63  " 'jpeg', 'xml'\n");
64  }
65 
66  return result;
67 }
68 
74 {
75  knowledge::KnowledgeRecord return_value;
76 
77  if (nodes_.size () > 0)
78  {
79  // copying strings wastes execution time, so we hold the knowledge::KnowledgeRecord
80  // instead of the resulting string filename.
81  knowledge::KnowledgeRecord filename_eval = nodes_[0]->evaluate (settings);
82  uint32_t read_as_type_uint (0);
83  (void)read_as_type_uint;
84 
86  "System call read_file is attempting to open %s.\n",
87  filename_eval.to_string ().c_str ());
88 
89  if (nodes_.size () == 2)
90  {
91  knowledge::KnowledgeRecord read_as_type = nodes_[1]->evaluate (settings);
92  if (read_as_type.type () == knowledge::KnowledgeRecord::INTEGER)
93  {
94  read_as_type_uint = (uint32_t) read_as_type.to_integer ();
95  }
96  else if (read_as_type.is_string_type ())
97  {
98  std::string type = read_as_type.to_string ();
99  if (type == "text")
100  {
101  read_as_type_uint = knowledge::KnowledgeRecord::TEXT_FILE;
102  }
103  else if (type == "jpeg")
104  {
105  read_as_type_uint = knowledge::KnowledgeRecord::IMAGE_JPEG;
106  }
107  else if (type == "xml")
108  {
109  read_as_type_uint = knowledge::KnowledgeRecord::XML;
110  }
111  }
112  }
113 
114  if (0 != return_value.read_file (filename_eval.to_string ()))
115  {
117  "KARL RUNTIME ERROR: System call read_file could not open %s.\n",
118  filename_eval.to_string ().c_str ());
119  }
120  }
121  else
122  {
124  "KARL RUNTIME ERROR: System call read_file"
125  " requires at least a filename to read, e.g."
126  " #read_file (filename), #read_file (filename, 'text')."
127  " Second argument is to force a file type when the filename"
128  " does not end with .txt, .xml, .jpg, etc. Can be 'text',"
129  " 'jpeg', 'xml'");
130  }
131 
132  return return_value;
133 }
134 
135 // accept a visitor
136 void
138  madara::expression::Visitor &visitor) const
139 {
140  visitor.visit (*this);
141 }
142 
143 #endif // _MADARA_NO_KARL_
This class encapsulates an entry in a KnowledgeBase.
int32_t type(void) const
returns the size of the value
virtual madara::knowledge::KnowledgeRecord item(void) const
Returns the value of the node.
madara::knowledge::KnowledgeRecord KnowledgeRecord
std::deque< ComponentNode * > ComponentNodes
a vector of Component Nodes
virtual madara::knowledge::KnowledgeRecord prune(bool &can_change)
Prunes the expression tree of unnecessary nodes.
logger::Logger * logger_
handle the context
Definition: ComponentNode.h:96
virtual ~SystemCallReadFile(void)
Destructor.
This class stores variables and their values for use by any entity needing state information in a thr...
Defines a node that contains a madara::knowledge::KnowledgeRecord::Integer value. ...
Definition: LeafNode.h:23
#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
Integer to_integer(void) const
converts the value to an integer
static constexpr struct madara::knowledge::tags::string_t string
Interface for a MADARA system call.
SystemCallReadFile(madara::knowledge::ThreadSafeContext &context, const ComponentNodes &nodes)
Constructor.
Settings for applying knowledge updates.
virtual void visit(const LeafNode &node)=0
Visit a LeafNode.
int read_file(const std::string &filename, uint32_t read_as_type=0)
reads a file and sets the type appropriately according to the extension
virtual madara::knowledge::KnowledgeRecord evaluate(const madara::knowledge::KnowledgeUpdateSettings &settings)
Evaluates the expression tree.
virtual void accept(Visitor &visitor) const
Accepts a visitor subclassed from the Visitor class.
bool is_string_type(void) const
returns true if the record is a string type (STRING, XML, TEXT_FILE)
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.