Exemple #1
0
//read config from file
int NDInstanceBase::ReadConfig(const char *configname) 
{
	int ret;
	ndxml *xmlroot ;
	ndxml_root xmlfile;

	ret = ndxml_load(config_file, &xmlfile) ;
	if(0!=ret) {
		nd_logfatal("load xml from file\n") ;
		return -1;
	}

	xmlroot = ndxml_getnode(&xmlfile,"root") ;
	if(!xmlroot) {		
		ndxml_destroy(&xmlfile);
		nd_logfatal("xml read error : root-node can not found \n") ;
		return -1;
	}

	if(-1==read_config(xmlroot, configname, &m_config) ) {
		ndxml_destroy(&xmlfile);
		nd_logfatal("xml config error %s node \n", configname) ;
		return -1;
	}

	ndxml_destroy(&xmlfile);
    
	return 0;

}
Exemple #2
0
int read_remote_proxy(char *file, struct nd_proxy_info *proxy_info )
{
	int ret;
	ndxml  *xml_node, *xml_read ;
	ndxml_root xmlroot;

	nd_assert(file) ;
	ret = ndxml_load(file, &xmlroot) ;
	if(0!=ret) {
		return -1;
	}
	xml_node = ndxml_getnode(&xmlroot,"server_config") ;
	if(!xml_node) {		
		ndxml_destroy(&xmlroot);
		return -1;
	}

	//read proxy
	xml_read = ndxml_refsub(xml_node, "proxy") ;
	if(xml_read) {

		ndxml *xmltmp = ndxml_refsub(xml_read, "proxy_type") ;
		if (!xmltmp) 	goto CFG_EXIT ;		
		proxy_info->proxy_type = ndxml_getval_int(xmltmp);
		if(0==proxy_info->proxy_type )goto CFG_EXIT ;

		xmltmp = ndxml_refsub(xml_read, "proxy_host") ;
		if (!xmltmp) 	goto CFG_EXIT ;		
		ndxml_getval_buf(xmltmp, proxy_info->proxy_host, sizeof(proxy_info->proxy_host)) ;


		xmltmp = ndxml_refsub(xml_read, "proxy_port") ;
		if (!xmltmp) 	goto CFG_EXIT ;		
		proxy_info->proxy_port = ndxml_getval_int(xmltmp) ;

		xmltmp = ndxml_refsub(xml_read, "proxy_user") ;
		if (xmltmp)
			ndxml_getval_buf(xmltmp, proxy_info->user, sizeof(proxy_info->user)) ;

		xmltmp = ndxml_refsub(xml_read, "proxy_password") ;
		if (xmltmp) 
			ndxml_getval_buf(xmltmp, proxy_info->password, sizeof(proxy_info->password)) ;
	}

CFG_EXIT:
	ndxml_destroy(&xmlroot);
	return 0;

}
Exemple #3
0
int read_dbconfig(const char *fileName, const char *dbCfgname ,struct nd_db_config *db_cfg)
{
	//ND_TRACE_FUNC() ;
	
	int ret = -1;
	ndxml *xmlroot, *xmlsub, *xmlnode ;
	ndxml_root xmlfile;
	
#define GET_VAL(name)                       \
	xmlnode = ndxml_refsub(xmlsub, #name) ; \
	if (!xmlnode){                          \
		ret = -1;                               \
		goto READ_EXIT ;                        \
	}                                       \
	strncpy(db_cfg->db_##name, ndxml_getval(xmlnode), sizeof(db_cfg->db_##name) )
	
	
	if ( 0 != ndxml_load(fileName , &xmlfile ) ){
		nd_logfatal("read file %s error %s\n", fileName, nd_last_error() ) ;
		return -1;
	}
	
	xmlroot = ndxml_getnode( &xmlfile, "root" ) ;
	if ( !xmlroot )   {
		goto READ_EXIT;
	}
	//read connect config
	xmlsub = ndxml_refsub( xmlroot, dbCfgname ) ;
	if ( !xmlsub )   {
		goto READ_EXIT;
	}
	
	GET_VAL( host ) ;
	GET_VAL( database ) ;
	GET_VAL( user ) ;
	GET_VAL( password) ;
	ret = 0 ;
	
	
READ_EXIT:
	ndxml_destroy( &xmlfile );
	return ret ;
}
Exemple #4
0
int set_xml_value(char *file, char *node_name_list, char *attr_name, char*attr_val, char *node_val)
{
    int ret;
    ndxml_root xmlfile;
    
    ndxml *xnode = 0 ;
    
    char *p = node_name_list;
    char name[128] ;
    
    ret = ndxml_load(file, &xmlfile) ;
    if(0!=ret) {
        fprintf(stderr,"load xml from %s\n", file) ;
        return -1;
    }
    
    p = (char*)ndstr_nstr_end(p, name, '.', sizeof(name)) ;
    
    if (IS_NUMERALS(name[0])) {
		xnode = ndxml_getnodei(&xmlfile, ndstr_atoi_hex(name));
    }
    else {
        xnode = ndxml_getnode(&xmlfile, name) ;
    }
    if (!xnode) {
        fprintf(stderr,"read xml-node %s error \n", name) ;
    }
    
    bool success = true ;
    while (p && *p) {
        
        if (*p == '.') {
            ++p ;
            if (!*p) {
                break ;
            }
        }
        
        p = (char*) ndstr_nstr_end(p, name, '.', sizeof(name)) ;
        
        if (IS_NUMERALS(name[0])) {
			xnode = ndxml_refsubi(xnode, ndstr_atoi_hex(name));
        }
        else {
            xnode = ndxml_refsub(xnode, name) ;
        }
        if (!xnode) {
            fprintf(stderr,"read xml-node %s error \n", name) ;
            success = false ;
            break ;
        }
        
        
    }
    if (success) {
        success = false;
        if (attr_name && attr_name[0] && attr_val && attr_val[0]) {
            ndxml_setattrval(xnode,attr_name, attr_val ) ;
            success = true ;
        }
        if (node_val && node_val[0]) {
            ndxml_setval(xnode, node_val) ;
            success = true ;
        }
        if (success) {
            ndxml_save(&xmlfile,file) ;
        }
    }
    
    ndxml_destroy(&xmlfile);
    return success ? 0: -1 ;
}