acl/app/wizard_demo/pkv/proto/redis_parser.cpp

94 lines
2.0 KiB
C++
Raw Normal View History

2023-07-19 19:07:53 +08:00
//
// Created by shuxin <20><><EFBFBD><EFBFBD>zheng on 2023/7/19.
//
#include "stdafx.h"
#include "redis_parser.h"
namespace pkv {
redis_parser::redis_parser() {
curr_ = nullptr;
2023-07-20 19:24:18 +08:00
dbuf_ = new (1) acl::dbuf_pool();
2023-07-20 22:56:34 +08:00
curr_ = std::make_shared<redis_object>(dbuf_);
2023-07-19 19:07:53 +08:00
}
2023-07-20 19:24:18 +08:00
redis_parser::~redis_parser() {
dbuf_->destroy();
}
2023-07-19 19:07:53 +08:00
2023-07-20 19:24:18 +08:00
const char* redis_parser::update(const char* data, size_t& len) {
2023-07-19 19:07:53 +08:00
while (len > 0) {
2023-07-20 19:24:18 +08:00
data = curr_->update(data, len);
2023-07-19 19:07:53 +08:00
if (curr_->finish()) {
objs_.emplace_back(curr_);
2023-07-20 22:56:34 +08:00
curr_ = std::make_shared<redis_object>(dbuf_);
2023-07-20 19:24:18 +08:00
} else if (curr_->failed()) {
break;
2023-07-19 19:07:53 +08:00
}
}
2023-07-20 19:24:18 +08:00
return data;
}
2023-07-20 22:56:34 +08:00
bool redis_parser::to_string(acl::string& out) const {
for (const auto& obj : objs_) {
2023-07-20 19:24:18 +08:00
if (!obj->to_string(out)) {
return false;
}
}
return true;
}
2023-07-20 22:56:34 +08:00
//////////////////////////////////////////////////////////////////////////////
2023-07-20 19:24:18 +08:00
bool test_redis_parse(const char* filepath) {
acl::string buf;
if (!acl::ifstream::load(filepath, buf)) {
printf("load %s error %s\r\n", filepath, acl::last_serror());
return false;
}
redis_parser parser;
const char* data = buf.c_str();
size_t len = buf.size();
const char* left = parser.update(data, len);
if (len > 0) {
printf(">>>parse failed<<<\r\n");
printf("%s\r\n", left);
return false;
}
printf(">>>parse success<<<\r\n");
acl::string out;
if (!parser.to_string(out)) {
printf(">>>build failed<<<\r\n");
return false;
}
if (out != buf) {
printf(">>>build failed<<<\r\n");
printf("output:\r\n|%s|\r\n", out.c_str());
printf("input:\r\n|%s|\r\n", buf.c_str());
acl::string filetmp(filepath);
filetmp += ".tmp";
acl::ofstream fp;
if (fp.open_trunc(filetmp)) {
fp.write(out);
}
return false;
}
printf(">>>build successfully<<<\r\n");
printf("%s\r\n", out.c_str());
return true;
2023-07-19 19:07:53 +08:00
}
} // namespace pkv