OpcUaCanOpen
CANopen OPC-UA server
exprtk_simple_example_18.cpp
Go to the documentation of this file.
1 /*
2  **************************************************************
3  * C++ Mathematical Expression Toolkit Library *
4  * *
5  * Simple Example 18 *
6  * Author: Arash Partow (1999-2019) *
7  * URL: http://www.partow.net/programming/exprtk/index.html *
8  * *
9  * Copyright notice: *
10  * Free use of the Mathematical Expression Toolkit Library is *
11  * permitted under the guidelines and in accordance with the *
12  * most current version of the MIT License. *
13  * http://www.opensource.org/licenses/MIT *
14  * *
15  **************************************************************
16 */
17 
18 
19 #include <cstdio>
20 #include <string>
21 
22 #include "exprtk.hpp"
23 
24 
25 template <typename T>
26 void file_io()
27 {
31 
32  std::string fileio_program =
33  " var file_name := 'file.txt'; "
34  " var stream := null; "
35  " "
36  " if (stream := open(file_name,'w')) "
37  " println('Successfully opened file: ' + file_name); "
38  " else "
39  " { "
40  " println('Failed to open file: ' + file_name); "
41  " return [false]; "
42  " } "
43  " "
44  " var s := 'Hello world...\n'; "
45  " "
46  " for (var i := 0; i < 10; i += 1) "
47  " { "
48  " write(stream,s); "
49  " } "
50  " "
51  " if (close(stream)) "
52  " println('Sucessfully closed file: ' + file_name); "
53  " else "
54  " { "
55  " println('Failed to close file: ' + file_name); "
56  " return [false]; "
57  " } ";
58 
59  exprtk::rtl::io::file::package<T> fileio_package;
61 
62  symbol_table_t symbol_table;
63  symbol_table.add_function("println",println);
64  symbol_table.add_package (fileio_package );
65 
66  expression_t expression;
67  expression.register_symbol_table(symbol_table);
68 
70  parser.compile(fileio_program,expression);
71 
72  printf("Result %10.3f\n",expression.value());
73 }
74 
75 int main()
76 {
77  file_io<double>();
78  return 0;
79 }