示例#1
0
STDLL stata_call(int argc, char *argv[]) {
	ST_uint4	j, i, from, to;
	ST_uint4 	n;
	ST_long 	nSubsets;
	ST_uint4 	nOutputVars=9; /* ES_F, varES_F, es_R, varES_R, df, Q, I2, tau2 subsetString */
	ST_uint4 	nInputVars=2;  /* ES, var*/
	ST_double	z, es, var;
	ST_retcode	rc;
	//ST_double 	version = 0.1;	
	ST_double 	version = 0.2;	
	
	char	buf[80];
	
	gsl_vector * esVector;
	gsl_vector * varVector;
	gsl_vector * metaResultsVector;
	
	//if (argc != 1) {
	if (argc != 3) {
		snprintf(buf, 80, "Plugin (ver: %g) usage: c_meta nStudies\n", version);
		SF_error(buf);
		SF_error("Please debug the calling .ado file!!!\n");
		return (-1);
	}
	n = (ST_uint4) atoi(argv[0]);
	from = (ST_uint4) atoi(argv[1]);   /* added modification */
	to = (ST_uint4) atoi(argv[2]);     /* added modification */

/*
	// debugging by printing out arguments   
	for (i=0; i< argc; i++) {
    	snprintf(buf, 80, "\narg%d=%s", i, argv[i]);
    	SF_error(buf);
 	}
	snprintf(buf, 80, "\nfrom=%d\nto=%d", from, to);
    SF_error(buf);
*/

	esVector = gsl_vector_alloc(n);
	varVector = gsl_vector_alloc(n);

	/* This will hold the meta-analysis results */
	metaResultsVector = gsl_vector_alloc(nOutputVars-1);  /* last var is string */
	
	/*nSubsets = gsl_pow_int(2,n)-1;*/
	nSubsets = 0;
	for(i=from;i<=to;i++) {
		nSubsets = nSubsets + gsl_sf_choose(n, i);
	}


	if (SF_in2()<nSubsets) {
		snprintf(buf, 80, "Plugin needs %ld observations in dataset\n", nSubsets);
		SF_error(buf);
		SF_error("Please debug the calling .ado file!!!\n");
		return (-1);
	}
	
	if (SF_nvars() != nInputVars+nOutputVars)  {
		snprintf(buf, 80, "Plugin expects %d input and %d output variables!\n",
			nInputVars, nOutputVars);
		SF_error(buf);
		SF_error("Please debug the calling .ado file!!!\n");
		return(102);
	}
    
	for(j=SF_in1(); j<= n ; j++) {
		if (SF_ifobs(j)) {
			if((rc=SF_vdata(1,j,&z))) return(rc);
			es = z;
			if((rc=SF_vdata(2,j,&z))) return(rc);
			var = z;
			if(SF_is_missing(es) | SF_is_missing(var)) {
				snprintf(buf, 80,"You have passed missing values (obs # %d) in the C plugin!\n", j);
				SF_error(buf);
				SF_error("Please debug the calling .ado file!!!\n");
				return(-1);
			} 
			else {
				/* do stuff here */
				gsl_vector_set(esVector,j-1, es);
				gsl_vector_set(varVector,j-1, var);
			}
		}
	}

	if ((rc=AllSubsetsMetaAnalysis(esVector, varVector, metaResultsVector, from, to))) return(rc); 

	gsl_vector_free(esVector);
	gsl_vector_free(varVector);
	gsl_vector_free(metaResultsVector);
	
	return(0);
}
示例#2
0
int write_data(void)
{
	int c,r,max_len ;
	max_len = 33*SF_nvars() ;	

	char query[40+max_len] ;
	char varnames[max_len] ;
	char vartypes[max_len] ;
	char buff[16] ;
	char *var = NULL ; 	
	char *type = NULL ; 	
	char *remove_comma ;	
	
	ST_retcode rc ;
	ST_double val = 0 ;

	// Create MySQL connection
	if(mysql_init(&mysql)==NULL)
	{
		SF_error("failed to initate MySQL connection\n") ;
		return 198 ;
	}
	// Connect to database stata_test
	if (!mysql_real_connect(&mysql,"127.0.0.1","user",
				"password","stata_test",3306,NULL,0))
	{
		SF_error("error connecting to database\n") ;
		return 198 ;
	}
	// Grab variable names from Stata macro myvars
	SF_macro_use("_myvars", varnames, max_len) ;
	// Build query
	strcpy(query, "INSERT INTO stata_test (") ;
	var = strtok(varnames, " ") ;	
	// Insert varnames into query
	while (var!=NULL) {
		strcat(query, var) ;
		strcat(query, ",") ;
		var = strtok(NULL, " ") ;	
	}
	remove_comma = strrchr(query, ',');
	*remove_comma = ')';	
	strcat(query, " VALUES(") ;
	// Insert values into query
	for(r=SF_in1();r<=SF_in2(); r++){
		SF_macro_use("_mytypes", vartypes, max_len) ;
		type = strtok(vartypes, " ") ;	
		if(SF_ifobs(r)) {
			for(c=1;c<=SF_nvars();c++){
				if (strstr(type, "str")) {
					if(rc = SF_sdata(c,r,buff)) return(rc) ;
				}
				else {
					if(rc = SF_vdata(c,r,&val)) return(rc) ;
					sprintf(buff, "%g", val) ;
				}
				strcat(query, "'") ;			
				strcat(query, buff) ;			
				strcat(query, "'") ;			
				strcat(query, ",") ;
				type = strtok(NULL, " ") ;
			}
			remove_comma = strrchr(query, ',') ;
			*remove_comma = ')' ;	
			strcat(query, ",(") ;
		}
	}
	remove_comma = strrchr(query, ',');
	*remove_comma = '\0';
	// Query database
	rc=mysql_real_query(&mysql,query, strlen(query)) ;
	if (rc)
	{
		SF_error("error making query\n") ;
		return 198 ;
	}
	return 0 ;
}