go语言yaml配置 go语言 ui( 二 )


clientid: "kvm_test"
writelog: false
writetodisk: false
outputfile: "./receivedMessages.txt"
hearttime: 30
#ifndef __CONFIG_H__
#define __CONFIG_H__
#ifdef __cplusplus
extern "C" {
#endif
/************************/
/* Minimum YAML version */
/************************/
#define YAML_VERSION_MAJOR 1
#define YAML_VERSION_MINOR 1
#define STRUCT_TYPE_NAME 100
#define INT_TYPE_NAME 101
#define STRING_TYPE_NAME 102
#define BOOL_TYPE_NAME 103
#define FLOAT_TYPE_NAME 104
#define MAP_TYPE_NAME 105
#define LIST_TYPE_NAME 106
typedef struct{
char *key;
void *value;
int valuetype;
char *parent;
}KeyValue,*pKeyValue;
#ifdef __cplusplus
}
#endif
#endif
#include
#include
#include
#include
#include
#include
#include
#include "config.h"
typedef struct {
char *SUBTOPIC; //string `yaml:"subtopic" mapstructure:"subtopic"` //"topic1"
char *PUBTOPIC; //string `yaml:"pubtopic" mapstructure:"pubtopic"`
int QOS; //byte `yaml:"qos" mapstructure:"qos"` //1
char *SERVERADDRESS; //string `yaml:"serveraddress" mapstructure:"serveraddress"` //= "tcp://mosquitto:1883"
char *CLIENTID; //string `yaml:"clientid" mapstructure:"clientid"` //= "mqtt_subscriber"
int HEARTTIME; //int `yaml:"hearttime" mapstructure:"hearttime"`
// CommandLocalPath string `yam:"commanlocalpath"`
}mqttSection,*pmqttSection;
typedef struct {
mqttSection Mqtt;// `yaml:"mqtt" mapstructure:"mqtt"`
// KVM kvmSection `yaml:"kvm" mapstructure:"kvm"`
}ConfigT;
ConfigT config;
static KeyValue webrtcconfig[]={
{"mqtt",config,STRUCT_TYPE_NAME,NULL},
{"subtopic",(config.Mqtt.SUBTOPIC),STRING_TYPE_NAME,"mqtt"},
{"pubtopic",(config.Mqtt.PUBTOPIC),STRING_TYPE_NAME,"mqtt"},
{"qos",(config.Mqtt.QOS),INT_TYPE_NAME,"mqtt"},
{"serveraddress",(config.Mqtt.SERVERADDRESS),STRING_TYPE_NAME,"mqtt"},
{"clientid",(config.Mqtt.CLIENTID),STRING_TYPE_NAME,"mqtt"},
{"hearttime",(config.Mqtt.HEARTTIME),INT_TYPE_NAME,"mqtt"},
{NULL,NULL,0,NULL},
};
int printConfig(ConfigT * pconfig){
if(pconfig==NULL) return -1;
printf("mqtt:r ");
if(pconfig-Mqtt.SUBTOPIC!=NULL) {printf("subtopic: %sr ",pconfig-Mqtt.SUBTOPIC); }
if(pconfig-Mqtt.SUBTOPIC!=NULL) {printf("pubtopic: %sr ",pconfig-Mqtt.PUBTOPIC); }
printf("qos: %dr ",config.Mqtt.QOS);
if(pconfig-Mqtt.SERVERADDRESS!=NULL) {printf("serveraddress: %sr ",pconfig-Mqtt.SERVERADDRESS); }
if(pconfig-Mqtt.CLIENTID!=NULL) {printf("clientid: %sr ",pconfig-Mqtt.CLIENTID); }
printf("hearttime: %dr ",config.Mqtt.HEARTTIME);
}
int freeConfig(ConfigT * pconfig){
if(pconfig==NULL) return -1;
if(pconfig-Mqtt.SERVERADDRESS!=NULL) {free(pconfig-Mqtt.SERVERADDRESS); }
if(pconfig-Mqtt.CLIENTID!=NULL) {free(pconfig-Mqtt.CLIENTID); }
if(pconfig-Mqtt.SUBTOPIC!=NULL) {free(pconfig-Mqtt.SUBTOPIC); }
}
char currentkey[100];
void getvalue(yaml_event_t event,pKeyValue *ppconfigs){
char *value = https://www.04ip.com/post/(char *)event.data.scalar.value;
pKeyValue pconfig=*ppconfigs;
char *pstringname;
while(pconfig-key!=NULL){
if(currentkey[0]!=0){
if(!strcmp(currentkey,pconfig-key))
{
switch(pconfig-valuetype){
case STRING_TYPE_NAME:
pstringname=strdup(value);
printf("get string value %sr ",pstringname);
*((char**)pconfig-value)=pstringname;
memset(currentkey, 0, sizeof(currentkey));
break;
case INT_TYPE_NAME:
*((int*)(pconfig-value))=atoi(value);
memset(currentkey, 0, sizeof(currentkey));
break;
case BOOL_TYPE_NAME:

推荐阅读