|
1 | 1 | # wsjcpp-safe-scripting |
2 | | -Light script language for integration to your project on C++ |
| 2 | + |
| 3 | +[](https://travis-ci.org/wsjcpp/wsjcpp-safe-scripting) [](https://github.com/wsjcpp/wsjcpp-safe-scripting/stargazers) [](https://github.com/wsjcpp/wsjcpp-safe-scripting/) [](https://github.com/wsjcpp/wsjcpp-safe-scripting/network/members) |
| 4 | + |
| 5 | +* Light script language for integration to your project |
| 6 | +* Script deny access read/write to out folders/files |
| 7 | +* Any variables or procedure is global |
| 8 | +* Used a procedure simple language |
| 9 | +* You can set variables before run script |
| 10 | +* You can get variables after run script |
| 11 | +* You can extend or disable procedures on c++ |
| 12 | +* Multiline strings |
| 13 | +* no if, for, while |
| 14 | + |
| 15 | +## Integrate to your project |
| 16 | + |
| 17 | +Just include this files: |
| 18 | + |
| 19 | +- src/wsjcpp_core/wsjcpp_core.h |
| 20 | +- src/wsjcpp_core/wsjcpp_core.cpp |
| 21 | +- src/wsjcpp_safe_scripting.h |
| 22 | +- src/wsjcpp_safe_scripting.cpp |
| 23 | + |
| 24 | +## Integrate via wsjcpp |
| 25 | + |
| 26 | +``` |
| 27 | +$ wsjcpp install https://github.com/wsjcpp/wsjcpp-safe-scripting:master |
| 28 | +``` |
| 29 | + |
| 30 | +## List of default variables and procedures |
| 31 | + |
| 32 | +Default Variables: |
| 33 | + |
| 34 | +- `rootdir` - directory when will be run script |
| 35 | +- `script_filename` - name of script |
| 36 | + |
| 37 | +Default procedures: |
| 38 | + |
| 39 | +- `exit` - will be exit from script with exitcode 0 |
| 40 | +- `error` - will be exit from script with exitcode -1 (error code) |
| 41 | +- `print_all_variables` - print to log all defined variables |
| 42 | +- `log_info [args]` - print args |
| 43 | +- `var <varname>` - define new variable |
| 44 | +- `set_value <varname1> "some"` - set string value to variable |
| 45 | +- `set_value <varname1> <varname2>` - set value to variable |
| 46 | +- `concat <varname1> [args]` - concatinate string values from all vars to <varname1> |
| 47 | +- `make_dir "dir_name"` - try create dirname |
| 48 | +- `normalize_class_name <varname1>` - will be removed characters which not in [a-zA-Z0-9] |
| 49 | +- `convert_CamelCase_to_snake_case <from> <to>` - convert from CamelCase to snake_case |
| 50 | +- `write_file <filepath> <content>` - write variable to file |
| 51 | +- `cmakelists_txt_append_wsjcpp <filepath>` - append string like 'list (APPEND WSJCPP_SOURCES "%filepath%")' to CMakeLists.txt into between '#### BEGIN_WSJCPP_APPEND' and '#### END_WSJCPP_APPEND' |
| 52 | + |
| 53 | +## Example of exec script |
| 54 | + |
| 55 | +``` |
| 56 | +#include <wsjcpp_safe_scripting.h> |
| 57 | +
|
| 58 | +... |
| 59 | +
|
| 60 | +WSJCppSafeScriptingContext scriptContext; |
| 61 | +
|
| 62 | +std::string sCurrentDirectory = WSJCppCore::getCurrentDirectory(); |
| 63 | +std::string sScriptFileName = "script_filename"; |
| 64 | +std::vector<std::string> vScriptArgs = {"Some1", "Some2"}; // script args: arg1, arg2 |
| 65 | +
|
| 66 | +// script content |
| 67 | +std::string sScriptContent = |
| 68 | + "var test1\n" |
| 69 | + "set_value test1 \"test string\"\n" |
| 70 | + "log_info test1\n" |
| 71 | + "log_info arg1\n" |
| 72 | + "log_info arg2\n"; |
| 73 | +
|
| 74 | +int nResult = scriptContext.exec( |
| 75 | + sCurrentDirectory, |
| 76 | + sScriptFileName, |
| 77 | + sScriptContent, |
| 78 | + vScriptArgs |
| 79 | +); |
| 80 | +
|
| 81 | +if (nResult == 0) { |
| 82 | + std::string sTest1 = scriptContext.getVariable("test1")->getValue(); |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +## Customizing procedures / variables |
| 87 | + |
| 88 | +### New procedures via wsjcpp |
| 89 | + |
| 90 | +``` |
| 91 | +wsjcpp generate WSJCppSafeScriptingProc YourClassName |
| 92 | +``` |
| 93 | + |
| 94 | +After this will be generated files and added to your CMakeLists.txt: |
| 95 | +``` |
| 96 | +#### BEGIN_WSJCPP_APPEND |
| 97 | +list (APPEND WSJCPP_SOURCES "src/wsjcpp_safe_scripting_proc_your_class_name.h") |
| 98 | +list (APPEND WSJCPP_SOURCES "src/wsjcpp_safe_scripting_proc_your_class_name.cpp") |
| 99 | +#### END_WSJCPP_APPEND |
| 100 | +``` |
| 101 | + |
| 102 | +Now you can write you code in this files or move to another place |
| 103 | + |
| 104 | +### New procedures copy-paste-code |
| 105 | + |
| 106 | +header `src/wsjcpp_safe_scripting_proc_your_class_name.h`: |
| 107 | +``` |
| 108 | +#ifndef WSJCPP_SAFE_SCRIPTING_PROC_YOUR_CLASS_NAME_H |
| 109 | +#define WSJCPP_SAFE_SCRIPTING_PROC_YOUR_CLASS_NAME_H |
| 110 | +
|
| 111 | +#include <wsjcpp_safe_scripting.h> |
| 112 | +
|
| 113 | +class WSJCppSafeScriptingProcYourClassName : public WSJCppSafeScriptingProc { |
| 114 | + public: |
| 115 | + WSJCppSafeScriptingProcYourClassName(); |
| 116 | + virtual bool exec(const std::vector<WSJCppSafeScriptingVariable *> &m_vArgs); |
| 117 | +}; |
| 118 | +
|
| 119 | +
|
| 120 | +#endif // WSJCPP_SAFE_SCRIPTING_PROC_YOUR_CLASS_NAME_H |
| 121 | +``` |
| 122 | + |
| 123 | +source-code `src/wsjcpp_safe_scripting_proc_your_class_name.cpp`: |
| 124 | +``` |
| 125 | +#include "wsjcpp_safe_scripting_proc_your_class_name.h" |
| 126 | +#include <wsjcpp_core.h> |
| 127 | +
|
| 128 | +// --------------------------------------------------------------------- |
| 129 | +// WSJCppSafeScriptingProcYourClassName |
| 130 | +
|
| 131 | +WSJCppSafeScriptingProcYourClassName::WSJCppSafeScriptingProcYourClassName() |
| 132 | +: WSJCppSafeScriptingProc("your_class_name") { |
| 133 | +
|
| 134 | +} |
| 135 | +
|
| 136 | +// --------------------------------------------------------------------- |
| 137 | +
|
| 138 | +bool WSJCppSafeScriptingProcYourClassName::exec(const std::vector<WSJCppSafeScriptingVariable *> &vArgs) { |
| 139 | + // you code here |
| 140 | + WSJCppLog::err(TAG, "Not implemented") |
| 141 | + return false; |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +### Add new procedures / variables to script context |
| 146 | + |
| 147 | +Example: |
| 148 | +``` |
| 149 | +WSJCppSafeScriptingContext scriptContext; |
| 150 | +
|
| 151 | +// variable |
| 152 | +WSJCppSafeScriptingVariable *pVar = new WSJCppSafeScriptingVariable(""); |
| 153 | +scriptContext.addVar(pVar); |
| 154 | +
|
| 155 | +WSJCppSafeScriptingProcYourClassName *pProc = new WSJCppSafeScriptingProcYourClassName(); |
| 156 | +scriptContext.addProc(pProc); |
| 157 | +
|
| 158 | +``` |
| 159 | + |
0 commit comments