1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
#include "common/printing.h"
#include "common/jsonconfig.h"
#include "common/strutil.h"
#include "detection/weather/weather.h"
#include "modules/weather/weather.h"
bool ffPrintWeather(FFWeatherOptions* options) {
FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate();
const char* error = ffDetectWeather(options, &result);
if (error) {
ffPrintError(FF_WEATHER_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
return false;
}
if (options->moduleArgs.outputFormat.length == 0) {
ffPrintLogoAndKey(FF_WEATHER_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
ffStrbufPutTo(&result, stdout);
} else {
FF_PRINT_FORMAT_CHECKED(FF_WEATHER_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
FF_ARG(result, "result"),
}));
}
return true;
}
void ffParseWeatherJsonObject(FFWeatherOptions* options, yyjson_val* module) {
yyjson_val *key, *val;
size_t idx, max;
yyjson_obj_foreach (module, idx, max, key, val) {
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
continue;
}
if (unsafe_yyjson_equals_str(key, "location")) {
ffStrbufSetJsonVal(&options->location, val);
continue;
}
if (unsafe_yyjson_equals_str(key, "outputFormat")) {
ffStrbufSetJsonVal(&options->outputFormat, val);
continue;
}
if (unsafe_yyjson_equals_str(key, "timeout")) {
options->timeout = (uint32_t) yyjson_get_uint(val);
continue;
}
ffPrintError(FF_WEATHER_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
}
}
void ffGenerateWeatherJsonConfig(FFWeatherOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
yyjson_mut_obj_add_strbuf(doc, module, "location", &options->location);
yyjson_mut_obj_add_strbuf(doc, module, "outputFormat", &options->outputFormat);
yyjson_mut_obj_add_uint(doc, module, "timeout", options->timeout);
}
bool ffGenerateWeatherJsonResult(FFWeatherOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate();
const char* error = ffDetectWeather(options, &result);
if (error) {
yyjson_mut_obj_add_str(doc, module, "error", error);
return false;
}
yyjson_mut_obj_add_strbuf(doc, module, "result", &result);
return true;
}
void ffInitWeatherOptions(FFWeatherOptions* options) {
ffOptionInitModuleArg(&options->moduleArgs, "");
ffStrbufInit(&options->location);
ffStrbufInitStatic(&options->outputFormat, "%t+-+%C+(%l)");
options->timeout = 0;
}
void ffDestroyWeatherOptions(FFWeatherOptions* options) {
ffOptionDestroyModuleArg(&options->moduleArgs);
ffStrbufDestroy(&options->outputFormat);
}
FFModuleBaseInfo ffWeatherModuleInfo = {
.name = FF_WEATHER_MODULE_NAME,
.description = "Print weather information",
.initOptions = (void*) ffInitWeatherOptions,
.destroyOptions = (void*) ffDestroyWeatherOptions,
.parseJsonObject = (void*) ffParseWeatherJsonObject,
.printModule = (void*) ffPrintWeather,
.generateJsonResult = (void*) ffGenerateWeatherJsonResult,
.generateJsonConfig = (void*) ffGenerateWeatherJsonConfig,
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
{ "Weather result", "result" },
}))
};
|