MADARA  3.2.3
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  "madara::expression::SystemCallRandInt: "
59  "KARL COMPILE ERROR:"
60  "System call read_file"
61  " requires at least a filename to read, e.g."
62  " #read_file (filename), #read_file (filename, 'text')."
63  " Second argument is to force a file type when the filename"
64  " does not end with .txt, .xml, .jpg, etc. Can be 'text',"
65  " 'jpeg', 'xml'\n");
66 
67  throw KarlException ("madara::expression::SystemCallRandInt: "
68  "KARL COMPILE ERROR: "
69  "System call read_file"
70  " requires at least a filename to read, e.g."
71  " #read_file (filename), #read_file (filename, 'text')."
72  " Second argument is to force a file type when the filename"
73  " does not end with .txt, .xml, .jpg, etc. Can be 'text',"
74  " 'jpeg', 'xml'\n");
75  }
76 
77  return result;
78 }
79 
85 {
86  knowledge::KnowledgeRecord return_value;
87 
88  if (nodes_.size () > 0)
89  {
90  // copying strings wastes execution time, so we hold the knowledge::KnowledgeRecord
91  // instead of the resulting string filename.
92  knowledge::KnowledgeRecord filename_eval = nodes_[0]->evaluate (settings);
93  uint32_t read_as_type_uint (0);
94  (void)read_as_type_uint;
95 
97  "madara::expression::SystemCallReadFile: "
98  "System call read_file is attempting to open %s.\n",
99  filename_eval.to_string ().c_str ());
100 
101  if (nodes_.size () == 2)
102  {
103  knowledge::KnowledgeRecord read_as_type = nodes_[1]->evaluate (settings);
104  if (read_as_type.type () == knowledge::KnowledgeRecord::INTEGER)
105  {
106  read_as_type_uint = (uint32_t) read_as_type.to_integer ();
107  }
108  else if (read_as_type.is_string_type ())
109  {
110  std::string type = read_as_type.to_string ();
111  if (type == "text")
112  {
113  read_as_type_uint = knowledge::KnowledgeRecord::TEXT_FILE;
114  }
115  else if (type == "jpeg")
116  {
117  read_as_type_uint = knowledge::KnowledgeRecord::IMAGE_JPEG;
118  }
119  else if (type == "xml")
120  {
121  read_as_type_uint = knowledge::KnowledgeRecord::XML;
122  }
123  }
124  }
125 
126  if (0 != return_value.read_file (filename_eval.to_string ()))
127  {
129  "madara::expression::SystemCallReadFile: "
130  "KARL RUNTIME WARNING: System call read_file could not open %s.\n",
131  filename_eval.to_string ().c_str ());
132  }
133  }
134  else
135  {
137  "madara::expression::SystemCallReadFile: "
138  "KARL COMPILE ERROR:"
139  "System call read_file"
140  " requires at least a filename to read, e.g."
141  " #read_file (filename), #read_file (filename, 'text')."
142  " Second argument is to force a file type when the filename"
143  " does not end with .txt, .xml, .jpg, etc. Can be 'text',"
144  " 'jpeg', 'xml'\n");
145 
146  throw KarlException ("madara::expression::SystemCallReadFile: "
147  "KARL COMPILE ERROR: "
148  "System call read_file"
149  " requires at least a filename to read, e.g."
150  " #read_file (filename), #read_file (filename, 'text')."
151  " Second argument is to force a file type when the filename"
152  " does not end with .txt, .xml, .jpg, etc. Can be 'text',"
153  " 'jpeg', 'xml'\n");
154  }
155 
156  return return_value;
157 }
158 
159 // accept a visitor
160 void
162  madara::expression::Visitor &visitor) const
163 {
164  visitor.visit (*this);
165 }
166 
167 #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:97
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
static struct madara::knowledge::tags::string_t string
#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
Integer to_integer(void) const
converts the value to an integer.
An exception for unrecoverable KaRL compilation issues.
Definition: KarlException.h:18
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.