T_PkgType *getTemplate(const char *key) { int n; n=index_col(dau.srm.colidx,abs(dau.srm.Aflg),key,dau.srm.tp); if(n<0) return NULL; return &dau.srm.tp[n]; }
int putitem_idx(void *buf,char *cp,T_PkgType *pkg_type,const char *key,const char *colidx,int colnum) { int k; if(pkg_type->offset<0) set_offset(pkg_type); k=index_col(colidx,colnum,key,pkg_type); if(k<0) return 0; k=put_str_one(buf,cp,pkg_type+k,0); return k; }
char *getitem_idx(char *buf,void *data,T_PkgType *pkg_type,const char *key,const char *colidx,int colnum) { int k; if(pkg_type->offset<0) set_offset(pkg_type); k=index_col(colidx,colnum,key,pkg_type); if(k<0) return 0; k=get_one_str(buf,data,pkg_type+k,0); return buf+k; }
int patt_copy_col(T_PkgType * dest,T_PkgType * src,const char *choose,char *idxcol) { int i,num; T_PkgType *tp,*dp; char buf[100]; const char *cp; int colnum; if(!src || !dest) return 0; tp=src; dp=dest; if(!choose||!*choose) { // 没有选择,全部加入 for(i=0;tp[i].type>=0;i++,dp++) { tp_cp(dp,tp,i); } tp_cp(dp,tp,i); return i; } num=0; cp=choose; colnum=set_offset(src); do { char *p; *buf=0; cp=stptok(skipblk((char *)cp),buf,sizeof(buf),",|"); //可以用的分隔符 p=buf; TRIM(p); if(!*p) continue; if(isdigit(*p)) { //数字选择,字段号范围 int ret,beg,end; end=beg=-1; ret=sscanf(buf,"%d - %d",&beg,&end); if(!ret) continue; if(ret==1) end=beg; if(end<beg) end=beg; for(ret=beg;ret<=end;ret++,dp++) { tp_cp(dp,tp,ret); num++; } } else { //字母,字段名 i=index_col(idxcol,colnum,p,src); if(src[i].type<0) continue; tp_cp(dp,tp,i); dp++; num++; } } while(*cp++); tp_cp(dp,tp,colnum); return num; }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | // Reversible Jump MCMC for Gaussian Graphical models with marginal pseudo-likelihood // for Bayesian model averaging (MA) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | void ggm_rjmcmc_mpl_ma( int *iter, int *burnin, int G[], double g_prior[], double S[], int *n, int *p, double p_links[] , int *print ) { int print_c = *print, iteration = *iter, burn_in = *burnin, copy_n = *n; int selected_edge, selected_edge_i, selected_edge_j; int nodexdim, count_mb, t, i, j, ij, counter, dim = *p, pxp = dim * dim; double log_alpha_ij; vector<double> p_links_Cpp( pxp, 0.0 ); vector<double> copyS( pxp ); memcpy( ©S[0], S, sizeof( double ) * pxp ); // Counting size of notes vector<int> size_node( dim, 0 ); for( i = 0; i < dim; i++ ) { nodexdim = i * dim; for( j = 0; j < dim; j++ ) size_node[ i ] += G[ nodexdim + j ]; } // Caclulating the log_likelihood for the current graph G vector<int>mb_node( dim ); vector<double>curr_log_mpl( dim ); vector<double> S_mb_node( pxp ); // For dynamic memory used for( i = 0; i < dim; i++ ) { if( size_node[ i ] > 0 ) { nodexdim = i * dim; count_mb = 0; for( t = 0; t < dim; t++ ) if( G[ nodexdim + t ] ) mb_node[ count_mb++ ] = t; } log_mpl( &i, &mb_node[0], &size_node[i], &curr_log_mpl[i], ©S[0], &S_mb_node[0], ©_n, &dim ); } vector<double> log_ratio_g_prior( pxp ); for( j = 1; j < dim; j++ ) for( i = 0; i < j; i++ ) { ij = j * dim + i; log_ratio_g_prior[ ij ] = log( static_cast<double>( g_prior[ ij ] / ( 1 - g_prior[ ij ] ) ) ); } // For finding the index of rates int qp = dim * ( dim - 1 ) / 2; vector<int>index_row( qp ); vector<int>index_col( qp ); counter = 0; for( j = 1; j < dim; j++ ) for( i = 0; i < j; i++ ) { ij = g_prior[ j * dim + i ]; if( ( ij != 0.0 ) or ( ij != 1.0 ) ) { index_row[ counter ] = i; index_col[ counter ] = j; counter++; } } int sub_qp = counter; // - - main loop for birth-death MCMC sampling algorithm - - - - - - - - - - - - - - - - - - - - - | GetRNGstate(); for( int i_mcmc = 0; i_mcmc < iteration; i_mcmc++ ) { if( ( i_mcmc + 1 ) % print_c == 0 ) Rprintf( " Iteration %d \n", i_mcmc + 1 ); // - - - STEP 1: selecting edge and calculating alpha - - - - - - - - - - - - - - - - - - - - - - -| // Randomly selecting one edge: NOTE qp = p * ( p - 1 ) / 2 selected_edge = static_cast<int>( unif_rand() * sub_qp ); selected_edge_i = index_row[ selected_edge ]; selected_edge_j = index_col[ selected_edge ]; // - - - STEP 1: calculating log_alpha_ij - - - - - - - - - - - - - - - - - - - - - - - - - - - - -| log_alpha_rjmcmc_ggm_mpl( &log_alpha_ij, &log_ratio_g_prior[0], &selected_edge_i, &selected_edge_j, &curr_log_mpl[0], G, &size_node[0], ©S[0], ©_n, &dim ); // - - - End of calculating log_alpha_ij - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | // Selecting an edge and updating G (graph) if( log( static_cast<double>( unif_rand() ) ) < log_alpha_ij ) { ij = selected_edge_j * dim + selected_edge_i; G[ ij ] = 1 - G[ ij ]; G[ selected_edge_i * dim + selected_edge_j ] = G[ ij ]; if( G[ ij ] ) { ++size_node[ selected_edge_i ]; ++size_node[ selected_edge_j ]; }else{ --size_node[ selected_edge_i ]; --size_node[ selected_edge_j ]; } } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | //curr_log_mpl[ i ] = log_mpl( node = i, mb_node = which( G[ i, ] == 1 ), size_node = sum( G[ i, ] ), S = S, n = n, p = p, alpha_ijl = alpha_ijl ) if( size_node[ selected_edge_i ] > 0 ) { nodexdim = selected_edge_i * dim; count_mb = 0; for( t = 0; t < dim; t++ ) if( G[ nodexdim + t ] ) mb_node[ count_mb++ ] = t; } //log_mpl_dis( &size_node[ selected_edge_i ], data, freq_data, &length_freq_data, max_range_nodes, &mb_node[0], alpha_ijl, &curr_log_mpl[ selected_edge_i ], &selected_edge_i, ©_n, &dim ); log_mpl( &selected_edge_i, &mb_node[0], &size_node[ selected_edge_i ], &curr_log_mpl[ selected_edge_i ], ©S[0], &S_mb_node[0], ©_n, &dim ); //curr_log_mpl[ j ] = log_mpl( node = j, mb_node = which( G[ j, ] == 1 ), size_node = sum( G[ j, ] ), S = S, n = n, p = p, alpha_ijl = alpha_ijl ) if( size_node[ selected_edge_j ] > 0 ) { nodexdim = selected_edge_j * dim; count_mb = 0; for( t = 0; t < dim; t++ ) if( G[ nodexdim + t ] ) mb_node[ count_mb++ ] = t; } //log_mpl_dis( &size_node[ selected_edge_j ], data, freq_data, &length_freq_data, max_range_nodes, &mb_node[0], alpha_ijl, &curr_log_mpl[ selected_edge_j ], &selected_edge_j, ©_n, &dim ); log_mpl( &selected_edge_j, &mb_node[0], &size_node[ selected_edge_j ], &curr_log_mpl[ selected_edge_j ], ©S[0], &S_mb_node[0], ©_n, &dim ); // - - - Saving result- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -| if( i_mcmc >= burn_in ) for( i = 0; i < pxp ; i++ ) p_links_Cpp[ i ] += G[ i ]; // - - - End of saving result - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -| // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | } PutRNGstate(); // - - - End of MCMC sampling algorithm - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -| memcpy( &p_links[0], &p_links_Cpp[0], sizeof( double ) * pxp ); }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | // Multiple birth-death MCMC for Gaussian Graphical models with marginal pseudo-likelihood // for maximum a posterior probability estimation (MAP) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | void ggm_bdmcmc_mpl_map_multi_update( int *iter, int *burnin, int G[], double g_prior[], double S[], int *n, int *p, int all_graphs[], double all_weights[], char *sample_graphs[], double graph_weights[], int *size_sample_g, int *counter_all_g, int *multi_update , int *print ) { int print_c = *print, multi_update_C = *multi_update; int iteration = *iter, burn_in = *burnin, copy_n = *n, count_all_g = *counter_all_g; int selected_edge_i, selected_edge_j, selected_edge_ij, size_sample_graph = *size_sample_g; int nodexdim, count_mb, t, i, j, ij, counter, dim = *p, pxp = dim * dim; double sum_weights = 0.0, weight_C, sum_rates; bool this_one; string string_g; vector<string> sample_graphs_C( iteration - burn_in ); vector<double> copyS( pxp ); memcpy( ©S[0], S, sizeof( double ) * pxp ); int qp = dim * ( dim - 1 ) / 2; vector<char> char_g( qp ); // char string_g[pp]; // Counting size of notes vector<int> size_node( dim, 0 ); for( i = 0; i < dim; i++ ) { nodexdim = i * dim; for( j = 0; j < dim; j++ ) size_node[ i ] += G[ nodexdim + j ]; } // Caclulating the log_likelihood for the current graph G vector<int>mb_node( dim ); vector<double>curr_log_mpl( dim ); vector<double> S_mb_node( pxp ); // For dynamic memory used for( i = 0; i < dim; i++ ) { if( size_node[ i ] > 0 ) { nodexdim = i * dim; count_mb = 0; for( t = 0; t < dim; t++ ) if( G[ nodexdim + t ] ) mb_node[ count_mb++ ] = t; } log_mpl( &i, &mb_node[0], &size_node[i], &curr_log_mpl[i], ©S[0], &S_mb_node[0], ©_n, &dim ); } vector<double> log_ratio_g_prior( pxp ); for( j = 1; j < dim; j++ ) for( i = 0; i < j; i++ ) { ij = j * dim + i; log_ratio_g_prior[ ij ] = log( static_cast<double>( g_prior[ ij ] / ( 1 - g_prior[ ij ] ) ) ); } // For finding the index of rates vector<int>index_row( qp ); vector<int>index_col( qp ); counter = 0; for( j = 1; j < dim; j++ ) for( i = 0; i < j; i++ ) { ij = g_prior[ j * dim + i ]; if( ( ij != 0.0 ) or ( ij != 1.0 ) ) { index_row[ counter ] = i; index_col[ counter ] = j; counter++; } } int sub_qp = counter; vector<double> rates( sub_qp ); int size_index = multi_update_C; vector<int> index_selected_edges( multi_update_C ); // - - main loop for birth-death MCMC sampling algorithm - - - - - - - - - - - - - - - - - - - - - | GetRNGstate(); for( int i_mcmc = 0; i_mcmc < iteration; i_mcmc += size_index ) { if( ( i_mcmc + 1 ) % print_c < multi_update_C ) Rprintf( " Iteration %d \n", i_mcmc + 1 ); // - - - STEP 1: calculating birth and death rates - - - - - - - - - - - - - - - - - - - - - - - - | rates_ggm_mpl( &rates[0], &log_ratio_g_prior[0], &curr_log_mpl[0], G, &index_row[0], &index_col[0], &sub_qp, &size_node[0], ©S[0], ©_n, &dim ); // Selecting multiple edges based on birth and death rates select_multi_edges( &rates[0], &index_selected_edges[0], &size_index, &sum_rates, &multi_update_C, &sub_qp ); // - - - Saving result - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | if( i_mcmc >= burn_in ) { counter = 0; for( j = 1; j < dim; j++ ) for( i = 0; i < j; i++ ) char_g[ counter++ ] = G[ j * dim + i ] + '0'; weight_C = 1.0 / sum_rates; string_g = string( char_g.begin(), char_g.end() ); all_weights[ count_all_g ] = weight_C; this_one = false; for( i = 0; i < size_sample_graph; i++ ) if( sample_graphs_C[ i ] == string_g ) { graph_weights[ i ] += all_weights[ count_all_g ]; all_graphs[ count_all_g ] = i; this_one = true; break; } if( !this_one || size_sample_graph == 0 ) { sample_graphs_C[ size_sample_graph ] = string_g; graph_weights[ size_sample_graph ] = all_weights[ count_all_g ]; all_graphs[ count_all_g ] = size_sample_graph; size_sample_graph++; } count_all_g++; sum_weights += weight_C; } // - - - End of saving result - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -| // - - - Updating graph based on selected edges - - - - - - - - - - - - - - - - - - - - - - - - - -| for ( i = 0; i < size_index; i++ ) { selected_edge_i = index_row[ index_selected_edges[ i ] ]; selected_edge_j = index_col[ index_selected_edges[ i ] ]; selected_edge_ij = selected_edge_j * dim + selected_edge_i; G[ selected_edge_ij ] = 1 - G[ selected_edge_ij ]; G[ selected_edge_i * dim + selected_edge_j ] = G[ selected_edge_ij ]; if( G[ selected_edge_ij ] ) { ++size_node[ selected_edge_i ]; ++size_node[ selected_edge_j ]; }else{ --size_node[ selected_edge_i ]; --size_node[ selected_edge_j ]; } } for ( i = 0; i < size_index; i++ ) { selected_edge_i = index_row[ index_selected_edges[ i ] ]; selected_edge_j = index_col[ index_selected_edges[ i ] ]; //curr_log_mpl[ i ] = log_mpl( node = i, mb_node = which( G[ i, ] == 1 ), size_node = sum( G[ i, ] ), S = S, n = n, p = p, alpha_ijl = alpha_ijl ) if( size_node[ selected_edge_i ] > 0 ) { nodexdim = selected_edge_i * dim; count_mb = 0; for( t = 0; t < dim; t++ ) if( G[ nodexdim + t ] ) mb_node[ count_mb++ ] = t; } //log_mpl_dis( &size_node[ selected_edge_i ], data, freq_data, &length_freq_data, max_range_nodes, &mb_node[0], alpha_ijl, &curr_log_mpl[ selected_edge_i ], &selected_edge_i, ©_n, &dim ); log_mpl( &selected_edge_i, &mb_node[0], &size_node[ selected_edge_i ], &curr_log_mpl[ selected_edge_i ], ©S[0], &S_mb_node[0], ©_n, &dim ); //curr_log_mpl[ j ] = log_mpl( node = j, mb_node = which( G[ j, ] == 1 ), size_node = sum( G[ j, ] ), S = S, n = n, p = p, alpha_ijl = alpha_ijl ) if( size_node[ selected_edge_j ] > 0 ) { nodexdim = selected_edge_j * dim; count_mb = 0; for( t = 0; t < dim; t++ ) if( G[ nodexdim + t ] ) mb_node[ count_mb++ ] = t; } //log_mpl_dis( &size_node[ selected_edge_j ], data, freq_data, &length_freq_data, max_range_nodes, &mb_node[0], alpha_ijl, &curr_log_mpl[ selected_edge_j ], &selected_edge_j, ©_n, &dim ); log_mpl( &selected_edge_j, &mb_node[0], &size_node[ selected_edge_j ], &curr_log_mpl[ selected_edge_j ], ©S[0], &S_mb_node[0], ©_n, &dim ); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | } PutRNGstate(); // - - - End of MCMC sampling algorithm - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -| #pragma omp parallel for for( i = 0; i < ( iteration - burn_in ); i++ ) { sample_graphs_C[ i ].copy( sample_graphs[ i ], qp, 0 ); sample_graphs[ i ][ qp ] = '\0'; } *size_sample_g = size_sample_graph; *counter_all_g = count_all_g; }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | // Multiple birth-death MCMC for Gaussian Graphical models with marginal pseudo-likelihood // for Bayesian model averaging (MA) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | void ggm_bdmcmc_mpl_ma_multi_update( int *iter, int *burnin, int G[], double g_prior[], double S[], int *n, int *p, double p_links[], int *multi_update, int *print ) { int print_c = *print, iteration = *iter, burn_in = *burnin, copy_n = *n; int multi_update_C = *multi_update, selected_edge_i, selected_edge_j, selected_edge_ij; int nodexdim, count_mb, t, i, j, ij, counter, dim = *p, pxp = dim * dim; double sum_weights = 0.0, weight_C, sum_rates; vector<double> p_links_Cpp( pxp, 0.0 ); vector<double> copyS( pxp ); memcpy( ©S[0], S, sizeof( double ) * pxp ); // Count size of notes vector<int> size_node( dim, 0 ); for( i = 0; i < dim; i++ ) { nodexdim = i * dim; for( j = 0; j < dim; j++ ) size_node[ i ] += G[ nodexdim + j ]; } // Caclulating the log_likelihood for the current graph G vector<int>mb_node( dim ); vector<double>curr_log_mpl( dim ); vector<double> S_mb_node( pxp ); // For dynamic memory used for( i = 0; i < dim; i++ ) { if( size_node[ i ] > 0 ) { nodexdim = i * dim; count_mb = 0; for( t = 0; t < dim; t++ ) if( G[ nodexdim + t ] ) mb_node[ count_mb++ ] = t; } log_mpl( &i, &mb_node[0], &size_node[i], &curr_log_mpl[i], ©S[0], &S_mb_node[0], ©_n, &dim ); } vector<double> log_ratio_g_prior( pxp ); for( j = 1; j < dim; j++ ) for( i = 0; i < j; i++ ) { ij = j * dim + i; log_ratio_g_prior[ ij ] = log( static_cast<double>( g_prior[ ij ] / ( 1 - g_prior[ ij ] ) ) ); } // For finding the index of rates int qp = dim * ( dim - 1 ) / 2; vector<int>index_row( qp ); vector<int>index_col( qp ); counter = 0; for( j = 1; j < dim; j++ ) for( i = 0; i < j; i++ ) { ij = g_prior[ j * dim + i ]; if( ( ij != 0.0 ) or ( ij != 1.0 ) ) { index_row[ counter ] = i; index_col[ counter ] = j; counter++; } } int sub_qp = counter; vector<double> rates( sub_qp ); int size_index = multi_update_C; vector<int> index_selected_edges( multi_update_C ); // - - main loop for birth-death MCMC sampling algorithm - - - - - - - - - - - - - - - - - - - - - | GetRNGstate(); for( int i_mcmc = 0; i_mcmc < iteration; i_mcmc += size_index ) { if( ( i_mcmc + 1 ) % print_c < multi_update_C ) Rprintf( " Iteration %d \n", i_mcmc + 1 ); // - - - STEP 1: calculating birth and death rates - - - - - - - - - - - - - - - - - - - - - - - - | rates_ggm_mpl( &rates[0], &log_ratio_g_prior[0], &curr_log_mpl[0], G, &index_row[0], &index_col[0], &sub_qp, &size_node[0], ©S[0], ©_n, &dim ); // Selecting multiple edges based on birth and death rates select_multi_edges( &rates[0], &index_selected_edges[0], &size_index, &sum_rates, &multi_update_C, &sub_qp ); // - - - Saving result- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -| if( i_mcmc >= burn_in ) { weight_C = 1.0 / sum_rates; #pragma omp parallel for for( i = 0; i < pxp ; i++ ) if( G[ i ] ) p_links_Cpp[ i ] += weight_C; sum_weights += weight_C; } // - - - End of saving result - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -| // - - - Updating graph based on selected edges - - - - - - - - - - - - - - - - - - - - - - - - - -| for ( i = 0; i < size_index; i++ ) { selected_edge_i = index_row[ index_selected_edges[ i ] ]; selected_edge_j = index_col[ index_selected_edges[ i ] ]; selected_edge_ij = selected_edge_j * dim + selected_edge_i; G[ selected_edge_ij ] = 1 - G[ selected_edge_ij ]; G[ selected_edge_i * dim + selected_edge_j ] = G[ selected_edge_ij ]; if( G[ selected_edge_ij ] ) { ++size_node[ selected_edge_i ]; ++size_node[ selected_edge_j ]; }else{ --size_node[ selected_edge_i ]; --size_node[ selected_edge_j ]; } } for ( i = 0; i < size_index; i++ ) { selected_edge_i = index_row[ index_selected_edges[ i ] ]; selected_edge_j = index_col[ index_selected_edges[ i ] ]; //curr_log_mpl[ i ] = log_mpl( node = i, mb_node = which( G[ i, ] == 1 ), size_node = sum( G[ i, ] ), S = S, n = n, p = p, alpha_ijl = alpha_ijl ) if( size_node[ selected_edge_i ] > 0 ) { nodexdim = selected_edge_i * dim; count_mb = 0; for( t = 0; t < dim; t++ ) if( G[ nodexdim + t ] ) mb_node[ count_mb++ ] = t; } //log_mpl_dis( &size_node[ selected_edge_i ], data, freq_data, &length_freq_data, max_range_nodes, &mb_node[0], alpha_ijl, &curr_log_mpl[ selected_edge_i ], &selected_edge_i, ©_n, &dim ); log_mpl( &selected_edge_i, &mb_node[0], &size_node[ selected_edge_i ], &curr_log_mpl[ selected_edge_i ], ©S[0], &S_mb_node[0], ©_n, &dim ); //curr_log_mpl[ j ] = log_mpl( node = j, mb_node = which( G[ j, ] == 1 ), size_node = sum( G[ j, ] ), S = S, n = n, p = p, alpha_ijl = alpha_ijl ) if( size_node[ selected_edge_j ] > 0 ) { nodexdim = selected_edge_j * dim; count_mb = 0; for( t = 0; t < dim; t++ ) if( G[ nodexdim + t ] ) mb_node[ count_mb++ ] = t; } //log_mpl_dis( &size_node[ selected_edge_j ], data, freq_data, &length_freq_data, max_range_nodes, &mb_node[0], alpha_ijl, &curr_log_mpl[ selected_edge_j ], &selected_edge_j, ©_n, &dim ); log_mpl( &selected_edge_j, &mb_node[0], &size_node[ selected_edge_j ], &curr_log_mpl[ selected_edge_j ], ©S[0], &S_mb_node[0], ©_n, &dim ); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | } PutRNGstate(); // - - - End of MCMC sampling algorithm - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -| #pragma omp parallel for for( i = 0; i < pxp; i++ ) p_links[ i ] = p_links_Cpp[ i ] / sum_weights; }
JSON_OBJECT stu_to_json(JSON_OBJECT json,void *data,T_PkgType * typ,const char *choose,char *colidx) { int i,colnum,n; T_PkgType *tp; char buf[100]; const char *cp; if(!json) return json; colnum=set_offset(typ); tp=typ; if(!choose||!*choose) { // 没有选择,全部加入 for(i=0;tp->type>=0;i++,tp++) { if(tp->bindtype & NOSELECT) continue; if(choose&&isnull((char *)data+tp->offset,tp->type)) continue; if(tp->type==CH_STRUCT) { JSON_OBJECT sub=json_object_new_object(); stu_to_json(sub,(char *)data+tp->offset, (T_PkgType *)tp->format,0,0); json_object_object_add(json,plain_name(tp->name),sub); continue; } add_field_to_object(json,data,tp); } return json; } cp=choose; do { char *p; *buf=0; cp=stptok(skipblk((char *)cp),buf,sizeof(buf),",|"); //可以用的分隔符 p=buf; TRIM(p); if(!*p) continue; if(isdigit(*p)) { //数字选择,字段号范围:"1-3,6-11" int ret,beg,end; end=beg=-1; ret=sscanf(buf,"%d - %d",&beg,&end); if(!ret) continue; if(ret==1) end=beg; if(end<beg) end=beg; for(ret=beg;ret<=end;ret++) { if(typ[ret].type==CH_STRUCT) { JSON_OBJECT sub=json_object_new_object(); stu_to_json(sub,(char *)data+typ[ret].offset, (T_PkgType *)typ[ret].format,0,0); json_object_object_add(json,plain_name(typ[ret].name),sub); continue; } add_field_to_object(json,data,&typ[ret]); } } else { //字母,字段名 :"colname1,colname2,..." n=index_col(colidx,colnum,p,typ); if(n<0) continue; if(typ[n].type==CH_STRUCT) { JSON_OBJECT sub=json_object_new_object(); stu_to_json(sub,(char *)data+typ[n].offset, (T_PkgType *)typ[n].format,0,0); json_object_object_add(json,plain_name(typ[n].name),sub); continue; } add_field_to_object(json,data,typ+n); } } while(*cp++); return json; }
int getTplNo(const char *colName) { return index_col(dau.srm.colidx,abs(dau.srm.Aflg),colName,dau.srm.tp); }