From f9cd032a5fe7f1dbdbf73d6bf47ef6febbcdffff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Tue, 5 Sep 2023 16:22:37 +0800 Subject: [PATCH] LM: add JSON format support --- src/modules/lm/lm.c | 31 +++++++++++++++++++++++++++++++ src/modules/lm/lm.h | 1 + 2 files changed, 32 insertions(+) diff --git a/src/modules/lm/lm.c b/src/modules/lm/lm.c index 0cd896a53..f84a422a3 100644 --- a/src/modules/lm/lm.c +++ b/src/modules/lm/lm.c @@ -86,3 +86,34 @@ void ffParseLMJsonObject(FFLMOptions* options, yyjson_val* module) ffPrintError(FF_LM_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key); } } + +void ffGenerateLMJson(FF_MAYBE_UNUSED FFLMOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +{ + FFLMResult result; + ffStrbufInit(&result.service); + ffStrbufInit(&result.type); + ffStrbufInit(&result.version); + const char* error = ffDetectLM(&result); + + if(error) + { + yyjson_mut_obj_add_str(doc, module, "error", error); + goto exit; + } + + if(result.service.length == 0) + { + yyjson_mut_obj_add_str(doc, module, "error", "No LM service found"); + goto exit; + } + + yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result"); + yyjson_mut_obj_add_strbuf(doc, obj, "service", &result.service); + yyjson_mut_obj_add_strbuf(doc, obj, "type", &result.type); + yyjson_mut_obj_add_strbuf(doc, obj, "version", &result.version); + +exit: + ffStrbufDestroy(&result.service); + ffStrbufDestroy(&result.type); + ffStrbufDestroy(&result.version); +} diff --git a/src/modules/lm/lm.h b/src/modules/lm/lm.h index 079f90e5f..72509adcb 100644 --- a/src/modules/lm/lm.h +++ b/src/modules/lm/lm.h @@ -9,3 +9,4 @@ void ffInitLMOptions(FFLMOptions* options); bool ffParseLMCommandOptions(FFLMOptions* options, const char* key, const char* value); void ffDestroyLMOptions(FFLMOptions* options); void ffParseLMJsonObject(FFLMOptions* options, yyjson_val* module); +void ffGenerateLMJson(FFLMOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module);