Skip to content

Commit 5f94315

Browse files
committed
vcap/pipewire: Simplify param parsing
1 parent 570b692 commit 5f94315

1 file changed

Lines changed: 34 additions & 40 deletions

File tree

src/video_capture/pipewire.cpp

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
#include "pipewire_common.hpp"
6868
#include "pixfmt_conv.h"
6969
#include "utils/misc.h"
70+
#include "utils/string_view_utils.hpp"
7071

7172
#define MOD_NAME "[PW vcap] "
7273

@@ -479,51 +480,44 @@ static void show_generic_help(){
479480

480481

481482
static int parse_params(const struct vidcap_params *params, vcap_pw_state *s) {
482-
if(const char *fmt = vidcap_params_get_fmt(params)) {
483-
std::istringstream params_stream(fmt);
484-
485-
std::string param;
486-
while (std::getline(params_stream, param, ':')) {
487-
if (param == "help") {
488-
if(s->mode == vcap_pw_state::Mode::Screen_capture)
489-
show_screen_help();
490-
else
491-
show_generic_help();
492-
return VIDCAP_INIT_NOERR;
493-
}
483+
std::string_view cfg;
484+
if(auto fmt = vidcap_params_get_fmt(params)){
485+
cfg = fmt;
486+
}
487+
488+
while(!cfg.empty()){
489+
auto tok = tokenize(cfg, ':', '"');
494490

495-
if (param == "cursor") {
496-
s->user_options.show_cursor = true;
497-
} else if (param == "nocrop") {
498-
s->user_options.crop = false;
499-
} else {
500-
auto split_index = param.find('=');
501-
if(split_index != std::string::npos && split_index != 0){
502-
std::string name = param.substr(0, split_index);
503-
std::string value = param.substr(split_index + 1);
504-
505-
if (name == "fps" || name == "FPS"){
506-
std::istringstream is(value);
507-
is >> s->user_options.fps;
508-
continue;
509-
}
510-
511-
if(name == "restore"){
512-
s->user_options.restore_file = std::move(value);
513-
continue;
514-
}
515-
516-
if(name =="target"){
517-
s->user_options.target = std::move(value);
518-
continue;
519-
}
520-
}
521-
522-
LOG(LOG_LEVEL_ERROR) << MOD_NAME "invalid option: \"" << param << "\"\n";
491+
const auto key = tokenize(tok, '=');
492+
const auto val = tokenize(tok, '=');
493+
494+
if (key == "help") {
495+
if(s->mode == vcap_pw_state::Mode::Screen_capture)
496+
show_screen_help();
497+
else
498+
show_generic_help();
499+
return VIDCAP_INIT_NOERR;
500+
}
501+
502+
if (key == "cursor") {
503+
s->user_options.show_cursor = true;
504+
} else if (key == "nocrop") {
505+
s->user_options.crop = false;
506+
} else if (key == "fps" || key == "FPS"){
507+
if(!parse_num(val, s->user_options.fps)){
508+
log_msg(LOG_LEVEL_FATAL, MOD_NAME "Failed to parse fps\n");
523509
return VIDCAP_INIT_FAIL;
524510
}
511+
} else if(key == "restore"){
512+
s->user_options.restore_file = val;
513+
} else if(key == "target"){
514+
s->user_options.target = val;
515+
} else{
516+
log_msg(LOG_LEVEL_FATAL, MOD_NAME "Unknown parameter %s\n", std::string(key).c_str());
517+
return VIDCAP_INIT_FAIL;
525518
}
526519
}
520+
527521
return VIDCAP_INIT_OK;
528522
}
529523

0 commit comments

Comments
 (0)