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


if(!strcmp(value,"true")) *((bool*)(pconfig-value))=true;
else *((bool*)(pconfig-value))=false;
memset(currentkey, 0, sizeof(currentkey));
break;
case FLOAT_TYPE_NAME:
*((float*)(pconfig-value))=atof(value);
memset(currentkey, 0, sizeof(currentkey));
break;
case STRUCT_TYPE_NAME:
case MAP_TYPE_NAME:
case LIST_TYPE_NAME:
memset(currentkey, 0, sizeof(currentkey));
strncpy(currentkey,value,strlen(value));
break;
default:
break;
}
break;
}
//continue;
}else{
if(!strcmp(value,pconfig-key)){
strncpy(currentkey,pconfig-key,strlen(pconfig-key));
break;
}
}
pconfig++;
}
}
int Load_YAML_Config( char *yaml_file, KeyValue *(configs[]) )
{
struct stat filecheck;
yaml_parser_t parser;
yaml_event_t event;
bool done = 0;
unsigned char type = 0;
unsigned char sub_type = 0;
if (stat(yaml_file, filecheck) != false )
{
printf("[%s, line %d] Cannot open configuration file '%s'! %s", __FILE__, __LINE__, yaml_file, strerror(errno) );
return -1;
}
FILE *fh = fopen(yaml_file, "r");
if (!yaml_parser_initialize(parser))
{
printf("[%s, line %d] Failed to initialize the libyaml parser. Abort!", __FILE__, __LINE__);
return -1;
}
if (fh == NULL)
{
printf("[%s, line %d] Failed to open the configuration file '%s' Abort!", __FILE__, __LINE__, yaml_file);
return -1;
}
memset(currentkey, 0, sizeof(currentkey));
/* Set input file */
yaml_parser_set_input_file(parser, fh);
while(!done)
{
if (!yaml_parser_parse(parser, event))
{
/* Useful YAML vars: parser.context_mark.line+1, parser.context_mark.column+1, parser.problem, parser.problem_mark.line+1, parser.problem_mark.column+1 */
printf( "[%s, line %d] libyam parse error at line %ld in '%s'", __FILE__, __LINE__, parser.problem_mark.line+1, yaml_file);
}
if ( event.type == YAML_DOCUMENT_START_EVENT )
{
//yaml file first line is version
//%YAML 1.1
//---
yaml_version_directive_t *ver = event.data.document_start.version_directive;
if ( ver == NULL )
{
printf( "[%s, line %d] Invalid configuration file. Configuration must start with "%%YAML 1.1"", __FILE__, __LINE__);
}
int major = ver-major;
int minor = ver-minor;
if (! (major == YAML_VERSION_MAJORminor == YAML_VERSION_MINOR) )
{
【go语言yaml配置 go语言 ui】 printf( "[%s, line %d] Configuration has a invalid YAML version. Must be 1.1 or above", __FILE__, __LINE__);
return -1;
}
}
else if ( event.type == YAML_STREAM_END_EVENT )
{
done = true;
}
else if ( event.type == YAML_MAPPING_END_EVENT )
{
sub_type = 0;
}
else if ( event.type == YAML_SCALAR_EVENT )
{
getvalue(event,configs);
}
}
return 0;
}
int main(int argc, char *argv[]){
pKeyValue pconfig=webrtcconfig[0];
Load_YAML_Config("../../etc/kvmagent.yml",pconfig);
printConfig(config);
freeConfig(config);
}
YAML 配置文件语言YAML 是专门用来写配置文件的语言,非常简洁和强大 , 远比 JSON 格式方便 。YAML 语言的设计目标,就是方便人类读写 。它实质上是一种通用的数据串行化格式 。它的基本语法规则如下:
对象的一组键值对,使用冒号结构表示
一组连词线开头的行,构成一个数组
数据结构的子成员是一个数组 , 则可以在该项下面缩进一个空格
对象和数组可以结合使用,形成复合结构
纯量是最基本的、不可再分的值 。以下数据类型都属于 JavaScript 的纯量

推荐阅读