WsParser_VS/ArgsParser/argsparser.cpp

165 lines
4.1 KiB
C++
Raw Normal View History

2024-06-22 10:53:51 +00:00
#include "argsparser.h"
using namespace args_parse;
2024-09-24 10:43:10 +00:00
using namespace std;
2024-06-22 10:53:51 +00:00
2024-10-01 08:52:05 +00:00
__ArgvPackImpls::__ArgvPackImpls(const QString& means, ParamType t) : means_store(means), type_store(t) { }
2024-06-22 10:53:51 +00:00
2024-10-01 08:52:05 +00:00
// ͨ<><CDA8> IArgvPack <20>̳<EFBFBD>
ParamType __ArgvPackImpls::paramType() const {
2024-09-24 10:43:10 +00:00
return type_store;
}
2024-06-22 10:53:51 +00:00
2024-10-01 08:52:05 +00:00
QString __ArgvPackImpls::means() const {
2024-09-24 10:43:10 +00:00
return means_store;
}
2024-06-22 10:53:51 +00:00
2024-10-01 08:52:05 +00:00
void __ArgvPackImpls::setValue(const QString& v) {
2024-06-22 10:53:51 +00:00
this->value_store = v;
}
2024-10-01 08:52:05 +00:00
QString __ArgvPackImpls::value() const {
2024-09-24 10:43:10 +00:00
return value_store;
}
2024-06-22 10:53:51 +00:00
2024-10-01 08:52:05 +00:00
FloatKeyValue::FloatKeyValue(const QString& key, const QString& means, bool optional) :
__FloatArgvImpl(key, means, optional) { }
2024-06-22 10:53:51 +00:00
2024-10-01 08:52:05 +00:00
QString __FloatArgvImpl::bindKey() const {
2024-09-24 10:43:10 +00:00
return key_name;
}
2024-06-22 10:53:51 +00:00
2024-10-01 08:52:05 +00:00
bool __FloatArgvImpl::optional() const {
2024-06-22 10:53:51 +00:00
return optional_value;
}
2024-10-01 08:52:05 +00:00
int FloatKeyValue::matchLenth() const {
2024-06-22 10:53:51 +00:00
return 2;
}
2024-10-01 08:52:05 +00:00
bool FloatKeyValue::parse(const QList<QString> args, int start) {
2024-06-22 10:53:51 +00:00
auto args_t = args[start];
auto args_v = args[start + 1];
2024-09-24 10:43:10 +00:00
if (args_t == bindKey())
2024-06-22 10:53:51 +00:00
setValue(args_v);
return args_t == bindKey();
}
2024-10-01 08:52:05 +00:00
IndexParam::IndexParam(const QString& means) : __ArgvPackImpls(means, ParamType::IndexParam) { }
2024-06-22 10:53:51 +00:00
2024-09-24 10:43:10 +00:00
int IndexParam::matchLenth() const {
2024-06-22 10:53:51 +00:00
return 1;
}
2024-09-24 10:43:10 +00:00
bool IndexParam::parse(const QList<QString> args, int start) {
2024-06-22 10:53:51 +00:00
setValue(args[start]);
return true;
}
2024-10-01 08:52:05 +00:00
__FloatArgvImpl::__FloatArgvImpl(const QString& key, const QString& means, bool optional)
: __ArgvPackImpls(means, ParamType::FloatParam), key_name(key), optional_value(optional) { }
2024-06-22 10:53:51 +00:00
2024-09-24 10:43:10 +00:00
FloatOption::FloatOption(const QString& key, const QString& means, bool opt)
2024-10-01 08:52:05 +00:00
: __FloatArgvImpl(key, means, opt) {
2024-09-24 10:43:10 +00:00
setValue(u8"0");
}
2024-06-22 10:53:51 +00:00
2024-09-24 10:43:10 +00:00
int FloatOption::matchLenth() const {
return 1;
}
2024-06-22 10:53:51 +00:00
bool FloatOption::parse(const QList<QString> args, int start) {
auto args_t = args[start];
setValue(QString::number(args_t == bindKey()));
return args_t == bindKey();
}
namespace args_parse {
class MatchMode {
private:
2024-10-01 08:52:05 +00:00
QList<shared_ptr<IArgvPack>> args_mode;
2024-06-22 10:53:51 +00:00
int code_store;
public:
2024-10-01 08:52:05 +00:00
explicit MatchMode(const QList<shared_ptr<IArgvPack>> mode, int mode_code)
2024-09-24 10:43:10 +00:00
:args_mode(mode), code_store(mode_code) { }
/**
* @brief <EFBFBD><EFBFBD>ȡģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
int modeCode()const {
return code_store;
}
/**
* @brief <EFBFBD><EFBFBD>ȡģʽƥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return ģʽƥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
2024-10-01 08:52:05 +00:00
QList<shared_ptr<IArgvPack>> result() const {
2024-09-24 10:43:10 +00:00
return args_mode;
}
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
bool parse(const QList<QString>& args, int argv_index, int parse_index) {
if (argv_index >= args.size())
2024-06-22 10:53:51 +00:00
return true;
2024-09-24 10:43:10 +00:00
// <20><>ȡģʽƥ<CABD>䵥Ԫ
2024-06-22 10:53:51 +00:00
auto parse_unit = args_mode[parse_index];
switch (parse_unit->paramType()) {
2024-09-24 10:43:10 +00:00
case ParamType::IndexParam:// <20>̶<EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5>
{
parse_unit->parse(args, argv_index);
// <20><><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>Ϊֹ
return parse(args, argv_index + parse_unit->matchLenth(), parse_index + 1);
}break;
case ParamType::FloatParam:// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5>
{
2024-10-01 08:52:05 +00:00
QList<shared_ptr<__FloatArgvImpl>> float_parsers;
2024-09-24 10:43:10 +00:00
for (auto& unit : args_mode) {
if (unit->paramType() == ParamType::FloatParam)
2024-10-01 08:52:05 +00:00
float_parsers << dynamic_pointer_cast<__FloatArgvImpl>(unit);
2024-09-24 10:43:10 +00:00
}
for (auto& unit : float_parsers) {
if (unit->matchLenth() + argv_index > args.size())
continue;
auto result = unit->parse(args, argv_index);
if (result)
if (parse(args, argv_index + unit->matchLenth(), parse_index + 1))
return true;
else if (!result && unit->optional())
if (parse(args, argv_index, parse_index + 1))
return true;
}
}break;
2024-06-22 10:53:51 +00:00
}
return false;
}
};
}
2024-10-01 08:52:05 +00:00
void ArgsParser::loadMode(int mode_code, const QList<shared_ptr<IArgvPack>>& args_model) {
2024-09-24 10:43:10 +00:00
this->match_modes.append(make_shared<MatchMode>(args_model, mode_code));
2024-06-22 10:53:51 +00:00
}
2024-10-01 08:52:05 +00:00
tuple<int, QList<shared_ptr<IArgvPack>>> ArgsParser::parse(int argc, char* argv[]) {
2024-09-24 10:43:10 +00:00
// <20>ۺϲ<DBBA><CFB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-06-22 10:53:51 +00:00
QList<QString> args_list;
for (int idx = 0; idx < argc; idx++) {
auto argv_str = QString::fromLocal8Bit(argv[idx]);
args_list.append(argv_str);
}
2024-09-24 10:43:10 +00:00
// ö<><C3B6>ģʽƥ<CABD><C6A5>
2024-06-22 10:53:51 +00:00
for (auto& minst : this->match_modes) {
if (minst->parse(args_list, 0, 0))
2024-10-01 08:52:05 +00:00
return tuple<int, QList<shared_ptr<IArgvPack>>>(minst->modeCode(), minst->result());
2024-06-22 10:53:51 +00:00
}
2024-10-01 08:52:05 +00:00
return tuple<int, QList<shared_ptr<IArgvPack>>>(0, QList<shared_ptr<IArgvPack>>());
2024-06-22 10:53:51 +00:00
}