如何读取 Javascript 文件
How to read a Javascript file
我有一个文件/path/to/my_js_functions.js,其中包含一个Javascript函数my_js_functions((等函数。
如何在C++中将函数myJsFunction
读取为字符串?
我正在寻找一种方法来获取整个函数,并且仅获取该函数,然后将其包含在char *jsFunction
中。
function myJsFunction(stringParam) {
return stringParam // The function returns a stringParam from the parameter
}
function anotherJsFunction(stringParam) {
return stringParam // Another function
}
提前谢谢大家。
使用 fstream,我会逐行读取并检查每行是否包含序列 myJsFunction。如果一行确实包含此序列,则开始聚合到字符串,直到到达下一个函数(在到达下一个关键字"function"或类似内容后停止(。请注意,使用}
作为关键字可能不起作用,因为函数可能有多个右大括号。
另一种可能的解决方案可能包括通过注意当换行符后面紧跟非空格时,新函数开始来识别函数的结尾,假设文件中的代码被格式化,其中任何范围较低的内容都被正确选项卡化。
为此,您需要读取javascript代码文件并对其进行解析。使用一些解析器库来做到这一点是非常高度的,比如腰果,esprima-cpp。我以前从未使用过它,所以我不能对此发表评论。
但这里有一些解析器的快速代码。您可以从此构建开始,以使其更加健壮。
主.cpp
#include <fstream>
#include <iostream>
#include <streambuf>
#include <string>
#include <vector>
std::string getFunction(const std::string &fileData, const std::string &name) {
std::string ret;
std::size_t start = 0;
while (true) {
const auto fNameStart = fileData.find(name, start);
if (fNameStart != std::string::npos) {
auto fStart = fileData.find_last_not_of(" ",fNameStart-1);
if(fStart == std::string::npos){
ret = "No Function Defination";
break;
}
else {
fStart = fStart-7;
if(fileData.substr(fStart,8) == "function"){
int openBraceCount = 0, closeBraceCount = 0;
std::size_t fEnd = fNameStart + name.size();
fEnd = fileData.find_first_of("{}", fEnd);
while (fEnd != std::string::npos) {
if (fileData.at(fEnd) == '{') {
openBraceCount++;
} else {
closeBraceCount++;
}
if (openBraceCount == closeBraceCount) {
ret = fileData.substr(fStart, fEnd - fStart+1);
break;
}
fEnd++;
fEnd = fileData.find_first_of("{}", fEnd);
}
if(!ret.empty()){
break;
}
else if(openBraceCount != closeBraceCount){
ret = "Function Parse Error";
break;
}
}
else{
start = fNameStart + name.size();
}
}
} else {
ret = "No Function Defination";
break;
}
}
return ret;
}
int main(int argc, char **argv) {
const std::string jsPath = "module.js";
const std::vector<std::string> vecFuncNames{"funcA", "funcB", "funcC",
"funcD", "funcE"};
std::ifstream fs(jsPath);
if (fs.is_open()) {
std::string fileData((std::istreambuf_iterator<char>(fs)),
std::istreambuf_iterator<char>());
for (auto &name : vecFuncNames) {
std::cout << name << "n" << getFunction(fileData, name) << std::endl;
}
}
return 0;
}
模块.js
function funcA ( ){
funcC(); console.log(" Hello");funcB();
}function funcC(){funcB();console.log("Hello");funcA();}
function funcB(a, b, c){
funcA(); setTimeout(function(){ alert("Hello"); }, 3000);funcC();
}
funcD();
function funcE(){{{{}}}
你可以简单地这样做
例如/path/code.js 是你的代码存储的路径
你的代码.js
function myJsFunction(stringParam) {
return stringParam // The function returns a stringParam from the parameter
}
function anotherJsFunction(stringParam) {
return stringParam // Another function
}
module.exports = {
myJsFunction,
anotherJsFunction
}
这是您用来读取您编写的函数的文件
索引.js
const code = require('/path/code.js');
console.log(代码(,它将在您的代码.js文件中显示您的整个代码
如果要使其成为字符串,可以使用这样的语法。在下面的代码中添加它到你的索引.js文件,它将使你的代码成为字符串版本。
String(code.myJsFunction)
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 文本文件中的单词链表
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 如何读取 Javascript 文件
- 有没有一种方法可以将数据从javascript发送到exe文件
- 如何使jse文件执行编码.exe并静默添加启动?Javascript将文件添加到启动?
- JavaScript "header"文件来存储一些数据
- 如何运行 JavaScript 文件 - V8
- 从Emscripten生成的JavaScript文件中使用C API
- C ++ tiny-js 从外部文件执行 JavaScript 函数
- 如何在Acrobat JavaScript中加载DLL文件
- IE扩展-正在注入Javascript文件
- 读/写文件导致错误。(javaScript & C++)
- 在QWebview中使用javascript访问文件系统
- 将 emacs 用于大型多文件 JavaScript 项目
- 我可以保存一个文件吗?该文件将由服务器端的php代码生成,只使用javascript/C/C++,而不打开浏览器
- 用C++创建一个文件Javascript对象