Ejemplo n.º 1
0
main()
{
  void *qd, *map_info ;

  char *all_in ;
  char *fam_in ; 
  char *gen_in ;
  char *spec_in ; 
  char *mode_in ;
  char *mode_str ;

  char *herb_in, *coll_in, *loc_in, *cult_in ;

  char query_str[1024] ;

  char fam_name_html[80] ;
  char genus_name_html[80] ;
  char spec_name_html[80] ;

  int i ;
  int num_hits ;
  int request_level ;
  int item_num ;
  int first ;

  char header_string[1024] ;
  char *title_string ;

  int redblue_colorsteps[] = {162, 54, 0, 0, 0} ;
  int green_colorsteps[] = {255, 255, 217, 140, 86} ;

  return_header("text/html") ;

  printf("<script>") ;
  printf("if (top.display_mode.substring(top.display_mode.length-4,top.display_mode.length) == 'icty')") ;
  printf("top.change_mode(top.display_mode);") ;
  printf("</script>") ;

  decode_query_string(9, "all", &all_in, "fam", &fam_in, "gen", &gen_in,
		      "spec", &spec_in, "mode", &mode_in, 
		      "herb", &herb_in, "coll", &coll_in,
		      "cult", &cult_in, "loc", &loc_in) ;

  if (nonempty(mode_in) && !strcmp(mode_in, "taxa"))
    mode_str = "qqtax" ;
  else mode_str = "qqspc" ;

  if (nonempty(all_in))
    request_level = 0 ;
  else if (nonempty(fam_in)) {
    if (nonempty(gen_in)) {
      if (nonempty(spec_in)) {
	char *s ;
	while ((s = strchr(spec_in, '-')) != NULL)
	  strcpy(s, s+1) ;
	
	request_level = 3 ;
      }
      else request_level = 2 ;
    }
    else request_level = 1 ;
  }

  if (nonempty(mode_str))
    sprintf(query_str, "%s&", mode_str) ;
  else sprintf(query_str, "") ;
  
  switch(request_level) {
  case 3:
    sprintf(query_str+strlen(query_str), 
	    "qqfam%s&qqgen%s&qqspe%s&", fam_in, gen_in, spec_in) ;
    break ;
  case 2:
    sprintf(query_str+strlen(query_str), 
	    "qqfam%s&qqgen%s&", fam_in, gen_in) ;
    break ;
  case 1:
    sprintf(query_str+strlen(query_str), 
	    "qqfam%s&", fam_in) ;
    break ;
  case 0:
    /* don't need to do anything */
    break ;
  }
  
  if (nonempty(cult_in)) 
    sprintf(query_str+strlen(query_str), "qqcul%s&", cult_in) ;

  if (nonempty(loc_in)) {
    char *t ;
    sprintf(query_str+strlen(query_str), "(") ;
    t = loc_in ;
    while (*t != '\0') {
      sprintf(query_str+strlen(query_str), "qqloc%c%c%c", t[0],t[1],t[2]) ; 
      t+=3 ;
      if (*t != '\0') 
	sprintf(query_str+strlen(query_str), "|") ;
    }
    sprintf(query_str+strlen(query_str), ")&") ;
  }
  
  if (nonempty(herb_in)) {
    char *temp, *tok ;

    temp = strdup(herb_in) ;
    tok = strtok(temp, " ") ;
    sprintf(query_str+strlen(query_str), "(") ;
    do {
      sprintf(query_str+strlen(query_str), "qqhrb%s", tok) ;
      tok = strtok(NULL, " ") ;
      if (tok != NULL) sprintf(query_str+strlen(query_str), "|") ;
    } while (tok != NULL) ;
    sprintf(query_str+strlen(query_str), ")&") ;
    free(temp) ;
  }

  *strrchr(query_str, '&') = '\0' ;
  
  mg_bool_query(query_str, HSB_DATA_PATH, "tracyspecs_map", 
		&qd) ;
  
  num_hits = mg_get_num_returned(qd) ;


  if (num_hits == 0) {
    printf("<h1>No specimens match criteria</h1>") ;
    printf("Sorry! The database does not contain any items ") ;
    printf("meeting the criteria you have selected.\n") ;
  }
  else {
    map_info = map_initialize(HSB_MAP_PATH, "tx", GRADED_VALUE);
    map_assign_graded_colors(map_info, 5, redblue_colorsteps,
			     green_colorsteps, redblue_colorsteps) ;

    if (nonempty(loc_in)) {
      char loc[4], *t ;
      map_disable_all_regions(map_info) ;
      t = loc_in ;
      while (*t != '\0') {
	strncpy(loc, t, 3) ;
	loc[3] = '\0' ;
	map_enable_region_by_name(map_info, "ut_abbr", loc) ;
	t += 3 ;
      }
    }
    
    first = 1 ;
    do {
      item_num = mg_get_doc_num(qd) ;
      if (request_level > 0 && first) {
	/* grab one of the returned items to get all the names */
	void *line_producer = NULL;
	mg_setup_doc_line_producer(qd, item_num, &line_producer) ;
	while (mg_dlp_more_lines(line_producer)) {
	  char s[256] ;
	  mg_dlp_next_line(line_producer, s) ;
	  if (s[0] != '\0') {
	    
	    if (!strncmp(s, "qqfah", 5)) {
	      strcpy(fam_name_html, s+6) ;
	    }
	    else if (!strncmp(s, "qqgeh", 5)) {
	      strcpy(genus_name_html, s+6) ;
	    }
	    else if (!strncmp(s, "qqsph", 5)) {
	      strcpy(spec_name_html, s+6) ;
	    }
	  }
	}
	first = 0 ;
      }
      add_to_county_bins(map_info, item_num) ;
    } while (mg_goto_next_doc(qd)) ;

    printf("<body bgcolor=\"#ffffff\">") ;

    printf("<h1>") ;
    if (!strcmp(mode_str, "qqtax"))
      printf("Species") ;
    else printf("Specimen") ;
    printf(" Distribution Map</h1>\n") ;

    printf("<b>Selection criteria:</b>:<br>") ;

    if (nonempty(cult_in)) 
      printf("Mapping only items taken from %s material <br>\n",
	     !strcmp(cult_in, "yes") ? "cultivated" : "non-cultivated") ;

    printf("Herbaria: <b>") ;
    if (nonempty(herb_in)) {
      printf("%s", herb_in) ;
    }
    else printf("all") ;
    printf("</b><br>\n") ;
    
    printf("Items: <b>") ;
    switch (request_level) {
    case 0:
      printf("all") ;
      break ;
    case 1:
      printf("family %s", fam_name_html) ;
      break ;
    case 2:
      printf("genus %s", genus_name_html) ;
      break ;
    case 3:
      printf("species %s", spec_name_html) ;
      break ;
    }
    printf("</b><p>\n") ;

    printf("<font size=+1>") ;

    printf("<b>Color Key (number of %s county) :<br>\n",
	   !strcmp(mode_str, "qqtax") ? "species in" : "specimens from") ;

    map_output_graded_color_key(map_info) ;
    printf("</b>\n") ;
    
    printf("<form method=get action=\"%s/specsfrommap.html\">", 
	   HSB_MAIN_URL) ;

    map_output_form_elem(map_info, NULL, NULL, NULL) ;

    printf("</form>\n") ;
    printf("</font>") ;
  }
}
Ejemplo n.º 2
0
int main(int argc, char *argv[]) 
{
  char *all_in, *group_in, *subclass_in, *order_in ;
  char *family_in, *genus_in, *species_in, *tri_in, *show_infras ;
  char *loc_in, *x_in, *y_in ;
  request_level_t request_level ;
  char name_html[128], *name_plain ;
  char qstr[512] ;
  void *qd ;
  taxon *root ;
  taxon *group_p, *subc_p, *ord_p, *fam_p, *gen_p, *spec_p, *tri_p, *quad_p ;
  int has_orders, has_subclasses ;
  int is_ext = !strcmp(argv[0], "b98_list_ext") ? 1 : 0 ;

  /* get CGI vars */
  decode_query_string(11, "all", &all_in,
		      "group", &group_in, "subclass", &subclass_in,
		      "order", &order_in, "family", &family_in,
		      "genus", &genus_in, "species", &species_in,
		      "tri", &tri_in,
		      "loc", &loc_in, "x", &x_in, "y", &y_in) ;

  /* process mouse click */
  
  if (nonempty(x_in)) {
    loc_in = map_convert_xy_to_name(atoi(x_in), atoi(y_in),
				    MAP_DIR, MAP_NAME, "abbr") ;
    if (!nonempty(loc_in)) return_nothing() ;
  }

  /* determine "request level" of request */

  if (nonempty(all_in))
    request_level = ALL ;
  else if (nonempty(group_in))
    request_level = GROUP ;
  else if (nonempty(subclass_in))
    request_level = SUBCLASS ;
  else if (nonempty(order_in))
    request_level = ORDER ;
  else if (nonempty(family_in))
    request_level = FAMILY ;
  else if (nonempty(genus_in)) {
    if (nonempty(species_in)) 
      if (nonempty(tri_in))
	request_level = TRI ;
      else request_level = SPECIES ;
    else request_level = GENUS ;
  }
  else request_level = ERROR ;

  /* build MG query string */

  switch(request_level) {
  case ALL:
    sprintf(qstr, "qqall") ; break;
  case GROUP:
    sprintf(qstr, "qqgrp%s", group_in) ; break ;
  case SUBCLASS:
    sprintf(qstr, "qqsbc%s", subclass_in) ; break ;
  case ORDER:
    sprintf(qstr, "qqord%s", order_in) ; break ;
  case FAMILY:
    sprintf(qstr, "qqfam%s", family_in) ; break ;
  case GENUS:
    sprintf(qstr, "qqgen%s", genus_in) ; break ;
  case SPECIES: case TRI:
    /* eliminate dashes in species names */
    {
      char *temp_s, *dash_loc ;
      temp_s = strdup(species_in) ;
      if ((dash_loc = strchr(temp_s, '-')) != NULL)
	strcpy(dash_loc, dash_loc+1) ;
      sprintf(qstr, "qqgen%s&qqspe%s", genus_in, temp_s) ;
      free(temp_s) ;
      if (request_level == TRI) {
	temp_s = strdup(tri_in) ;
	if ((dash_loc = strchr(temp_s, '-')) != NULL)
	  strcpy(dash_loc, dash_loc+1) ;
	sprintf(qstr+strlen(qstr), "&qqtri%s", temp_s) ;
	free(temp_s) ;
      }
    } break ;
  }

  if (nonempty(loc_in))
    sprintf(qstr+strlen(qstr), "&qqloc%s", loc_in) ;

  /* execute MG query */
  mg_bool_query(qstr, MG_COLL_DIR, MG_COLL_NAME, &qd) ;

  /* process MG query results */

  if (mg_get_num_returned(qd) == 0) return_nothing() ;

  root = NULL ;

  {
    char s[256] ;
    void *lp=NULL ;
    int docnum ;
    
    do {
      clear_name_vars() ;
      docnum = mg_get_doc_num(qd) ;
      mg_setup_doc_line_producer(qd, docnum, &lp) ;
      while (mg_dlp_more_lines(lp)) {
	if (!strncmp(s, "qqgrp", 5))
	  strcpy(group_out, s+5) ;
	else if (!strncmp(s, "qqsbc", 5))
	  strcpy(subclass_out, s+5) ;
	else if (!strncmp(s, "qqord", 5))
	  strcpy(order_out, s+5) ;
	else if (!strncmp(s, "qqfam", 5))
	  strcpy(family_out, s+5) ;
	else if (!strncmp(s, "qqgen", 5))
	  strcpy(genus_out, s+5) ; 
	else if (!strncmp(s, "qqhyb", 5))
	  hybrid = (!strncmp(s+5, "yes", 3) ? 1 : 0) ;
	else if (!strncmp(s, "qqspr", 5))
	  strcpy(species_out, s+5) ;
	else if (!strncmp(s, "qqtrs", 5))
	  strcpy(trirank_out, s+5) ;
	else if (!strncmp(s, "qqtrr", 5))
	  strcpy(tri_out, s+5) ;
	else if (!strncmp(s, "qqqus", 5))
	  strcpy(quadrank_out, s+5) ;
	else if (!strncmp(s, "qqqur", 5))
	  strcpy(quad_out, s+5) ;
	mg_dlp_next_line(lp, s) ;
      }
      switch (group_out[0]) {
      case 'P': 
	group_p = insert_taxon_item(&root, "1Pteridophytes", NULL, 1) ;
	break ;
      case 'G': 
	group_p = insert_taxon_item(&root, "2Gymnosperms", NULL, 1) ;
	break ;
      case 'M': 
	group_p = insert_taxon_item(&root, "3Monocots", NULL, 1) ;
	break ;
      case 'D': 
	group_p = insert_taxon_item(&root, "4Dicots", NULL, 1) ;
	break ;
      }
      subc_p = insert_taxon_child(group_p, subclass_out, NULL, 1) ;
      ord_p = insert_taxon_child(subc_p, order_out, NULL, 1) ;
      fam_p = insert_taxon_child(ord_p, family_out, NULL, 1) ;
      gen_p = insert_taxon_child(fam_p, genus_out, NULL, 1) ;
      spec_p = insert_taxon_child(gen_p, species_out, 
				  new_int(hybrid), 1) ;
      if (nonempty(tri_out)) {
	tri_p = insert_taxon_child(spec_p, tri_out, 
				   new_int(!strcmp(trirank_out, "var") ?
					   1 : 0), 1) ;
	if (nonempty(quad_out)) {
	  quad_p = insert_taxon_child(tri_p, quad_out, 
				      new_int(!strcmp(quadrank_out, "var") ?
						      1 : 0), 1) ;
	}
      }
    } while (mg_goto_next_doc(qd)) ;
  }

  /* generate HTML page */

  /* HTTP header */
  return_header("text/html") ;

  build_name(name_html, request_level, 0) ;
  name_plain = strip_tags(name_html) ;

  puts("<html>") ;
  puts("<head>") ;
  printf("<title>BONAP Distribution Data: ") ;
  if (request_level == SPECIES || request_level == TRI)
    printf("subspecies/varieties ") ;
  else printf("taxa ") ;
  printf("of %s", name_plain) ;
  if (nonempty(loc_in))
    printf(" in %s", 
	   map_convert_region_name(MAP_DIR, MAP_NAME, 
				   "abbr", "name", loc_in)) ;
  else
    puts(" in the US") ;
  puts("</title>") ;
  puts("</head>") ;
  puts("<body bgcolor=\"#ffffff\">") ;

  if (is_ext) ext_header() ;

  printf("<h1>") ;
  if (request_level == SPECIES || request_level == TRI)
    printf("Subspecies/varieties ") ;
  else printf("Taxa ") ;
  printf("of %s", name_html) ;
  if (nonempty(loc_in))
    printf(" in %s", 
	   map_convert_region_name(MAP_DIR, MAP_NAME, 
				   "abbr", "name", loc_in)) ;
  else
    puts(" in the US") ;
  printf("</h1>") ;

  if (request_level > GROUP) {
    if (request_level <= GENUS) name_html[0] = toupper(name_html[0]) ;
    printf("%s is a member of the %s group",
	   name_html,
	   (root->name)+1) ;
    if (request_level > SUBCLASS) 
      if (strcmp(root->nextlevel->name, "none")) 
	printf(", subclass %s", root->nextlevel->name) ;
    if (request_level > ORDER)
      if (strcmp(root->nextlevel->nextlevel->name, "none"))
	printf(", order %s", root->nextlevel->nextlevel->name) ;
    if (request_level > FAMILY)
      printf(", family %s", root->nextlevel->nextlevel->nextlevel->name) ;
    /* Don't really need to mention genus because it's obvious
       from the species name */
    /*
    if (request_level > GENUS)
      printf(", genus <i>%s</i>",
	     root->nextlevel->nextlevel->nextlevel->nextlevel->name) ;
	     */
    puts(".<p>") ;
  }

  if (request_level <= ALL) 
    puts("<ul>") ;
  group_p = root ;
  while (group_p != NULL) {
    if (request_level <= ALL)
      printf("%s<br>\n", (group_p->name)+1) ;

    subc_p = group_p->nextlevel ;
    has_subclasses = (strcmp(subc_p->name, "none") != 0) ;

    if (request_level <= GROUP)
      if (has_subclasses) puts("<ul>") ;
    while (subc_p != NULL) {
      strcpy(subclass_out, subc_p->name) ;
      if (request_level <= GROUP && has_subclasses) {
	build_name(name_html, SUBCLASS, 1) ;
	printf("<b>%s</b>", name_html) ;

	puts("<br>") ;
      }
      ord_p = subc_p->nextlevel ;
      has_orders = (strcmp(ord_p->name, "none") != 0) ;
      if (request_level <= SUBCLASS)
	if (has_orders) puts("<ul>") ;
      while (ord_p != NULL) {
	strcpy(order_out, ord_p->name) ;
	if (request_level <= SUBCLASS && has_orders) {
	  build_name(name_html, ORDER, 1) ;
	  printf("<b>%s</b><br>\n", name_html) ;
	}
	fam_p = ord_p->nextlevel ;
	if (request_level <= ORDER)
	  puts("<ul>") ;
	while (fam_p != NULL) {
	  strcpy(family_out, fam_p->name) ;
	  if (request_level <= ORDER) {
	    build_name(name_html, FAMILY, 1) ;
	    printf("<b>%s</b><br>\n", name_html) ;
	  }
	  gen_p = fam_p->nextlevel ;
	  if (request_level <= FAMILY)
	    puts("<ul>") ;
	  while (gen_p != NULL) {
	    strcpy(genus_out, gen_p->name) ;
	    if (request_level <= FAMILY) {
	      build_name(name_html, GENUS, 1) ;
	      printf("<b>%s</b> ", name_html) ;
	      printf("<a href=\"%s?colldir=%s&collname=%s&"
		     "query=%s\">(checklist entries)</a>",
		     RULED_HTML_QUERY_URL,
		     CHECKLIST_COLL_DIR, CHECKLIST_COLL_NAME,
		     gen_p->name) ;
	      printf(" <a href=\"%s?genus=%s\">(map)</a>",
		     is_ext ? B98_MAP_EXT_URL : B98_MAP_URL,
		     genus_out) ;
	      puts("<br>") ;
	    }
	    spec_p = gen_p->nextlevel ;
	    if (request_level <= GENUS)
	      puts("<ul>") ;
	    while (spec_p != NULL) {
	      strcpy(species_out, spec_p->name) ;
	      hybrid = *((int *)(spec_p->other_data)) ;
	      if (request_level <= GENUS) {
		build_name(name_html, SPECIES, 1) ;
		printf("<b>%s</b>", name_html) ;
		printf(" <a href=\"%s?colldir=%s&collname=%s&"
		       "query=%s%%26%s\">(checklist entry)</a>",
		       RULED_HTML_QUERY_URL,
		       CHECKLIST_COLL_DIR, CHECKLIST_COLL_NAME,
		       gen_p->name, spec_p->name) ;
		printf(" <a href=\"%s?genus=%s&species=%s\">(species "
		       "map)</a>",
		       is_ext ? B98_MAP_EXT_URL : B98_MAP_URL,
		       genus_out, species_out) ;
		if (spec_p->nextlevel != NULL) 
		  printf(" <a href=\"%s?genus=%s&species=%s&show_infras=yes\">"
			 "(infras map)</a>",
			 is_ext ? B98_MAP_EXT_URL : B98_MAP_URL,
			 genus_out, species_out) ;
		printf("<br>\n") ;
	      }
	      if (spec_p->nextlevel != NULL) puts("<ul>") ;
	      tri_p = spec_p->nextlevel ;
	      while (tri_p != NULL) {
		strcpy(tri_out, tri_p->name) ;
		strcpy(trirank_out,
		       *((int *)(tri_p->other_data)) ? "var" : "ssp") ;
		build_name(name_html, TRI, 1) ;
		printf("<b>%s</b>", name_html) ;
		printf(" <a href=\"%s?genus=%s&species=%s&tri=%s\">"
		       "(infra map)</a>",
		       is_ext ? B98_MAP_EXT_URL : B98_MAP_URL,
		       genus_out, species_out, tri_out) ;
		if (tri_p->nextlevel != NULL) 
		  printf(" <a href=\"%s?genus=%s&species=%s&tri=%s"
			 "&show_infras=yes\">(subinfras map)</a>",
			 is_ext ? B98_MAP_EXT_URL : B98_MAP_URL,
			 genus_out, species_out, tri_out) ;
		puts("<br>") ;
		if (tri_p->nextlevel != NULL) puts("<ul>") ;
		quad_p = tri_p->nextlevel ;
		while (quad_p != NULL) {
		  strcpy(quad_out, quad_p->name) ;
		  strcpy(quadrank_out,
			 *((int *)(quad_p->other_data)) ? "var" : "ssp") ;
		  build_name(name_html, QUAD, 1) ;
		  printf("<b>%s</b>", name_html) ;
		  printf(" <a href=\"%s?genus=%s&species=%s&tri=%s&quad=%s\">"
			 "(subinfra map)</a>",
			 is_ext ? B98_MAP_EXT_URL : B98_MAP_URL,
			 genus_out, species_out, tri_out, quad_out) ;
		  puts("<br>") ;
		  step_taxon(quad_p) ;
		}
		if (tri_p->nextlevel != NULL) puts("</ul>") ;
		step_taxon(tri_p) ;
	      }
	      if (spec_p->nextlevel != NULL) puts("</ul>") ;
	      step_taxon(spec_p) ;
	    }
	    if (request_level <= GENUS)
	      puts("</ul>") ;
	    step_taxon(gen_p) ;
	  }
	  if (request_level <= FAMILY)
	    puts("</ul>") ;
	  step_taxon(fam_p) ;
	}
	if (request_level <= ORDER)
	  puts("</ul>") ;
	step_taxon(ord_p) ;
      }
      if (request_level <= SUBCLASS)
	if (has_orders) puts("</ul>") ;
      step_taxon(subc_p) ;
    }
    if (request_level <= GROUP)
      if (has_subclasses) puts("</ul>") ;
    step_taxon(group_p) ;
  }

  if (request_level <= ALL)
    puts("</ul>") ;

  puts("</body>"
       "</html>") ;
}
Ejemplo n.º 3
0
main()
{
  char *fam_in, *gen_in, *spec_in, *nkcode_in, *tkcode_in, *x_in, *y_in ;
  char *ncode_in, *tcode_in, *gcode_in, *fcode_in, *ntype_in ;
  char *all_in, *tri_in, *quad_in, *cutoff_in ;
  int need_name ;
  char name_html[128] ;
  int i, cutoff_level ;
  void *qd ;
  int num_hits ;
  char *loc_in=NULL, *county_in=NULL ;
  int request_level ;
  char qstr[256] ;

  if (!mg_db_available("/bwg/FLORA/tracy/mgdata","tracyspecs")) {
    return_header("text/html") ;
    puts("<title>Sorry!</title>") ;
    puts("<h1>Sorry!</h1>") ;
    puts("The database of specimens used to generate lists, which is ") ;
    puts("required to process ") ;
    puts("your query, is currently being rebuilt. Please try again ") ;
    puts("in a little while.") ;
    exit(0) ;
  }

  decode_query_string(14, "ncode", &ncode_in,  /* bonap name code */ 
		      "tcode", &tcode_in,  /* bonap taxon code */
		      "gcode", &gcode_in,  /* ncode of genus */
		      "fcode", &fcode_in,  /* ncode of family */
		      "ntype", &ntype_in,  /* native/bonap_n/bonap_t */
		      "all", &all_in,
		      "fam", &fam_in, 
		      "gen", &gen_in,
		      "spec", &spec_in, 
		      "infra", &tri_in,
		      "infra2", &quad_in,
		      "cutoff", &cutoff_in, /* clip level of map */
		      "x", &x_in, "y", &y_in) ;
  
  if (nonempty(x_in)) {
    loc_in = map_convert_xy_to_name(atoi(x_in), atoi(y_in),
				    "/herbaria/FLORA/cgi/maps","tx",
				    "ut_abbr") ;
    if (loc_in == NULL) {
      return_nothing() ;
      exit() ;
    }
    county_in = map_convert_xy_to_name(atoi(x_in), atoi(y_in),
				    "/herbaria/FLORA/cgi/maps","tx",
				    "cname") ;
  }

  need_name = 0 ;

  if (nonempty(ncode_in) || nonempty(tcode_in) ||
      nonempty(gcode_in) || nonempty(fcode_in)) {
    char *code, *prefix ;
    if (nonempty(ncode_in)) {
      code = ncode_in ;
      prefix = "qqbnn" ;
    }
    else if (nonempty(tcode_in)) {
      code = tcode_in ; 
      prefix = "qqbtn" ;
    }
    else if (nonempty(gcode_in)){
      code = gcode_in ;
      prefix = "qqgco" ;
    }
    else if (nonempty(fcode_in)) {
      code = fcode_in ;
      prefix = "qqfco" ;
    }
    get_bonap_info_by_code(code, name_html, &request_level) ;
    strcpy(qstr, prefix) ;
    for (i=0 ; code[i] != '\0' ; i++) 
      sprintf(qstr+strlen(qstr), "%c", code[i]+'A'-'0') ;
  }
  else /* no bonap code, doing it by name */ {
    /* determine request level */
    if (nonempty(all_in)) {
      request_level = 0 ;
      sprintf(qstr, "qqspc") ;
    }
    else if (nonempty(fam_in)) {
      request_level = 1 ;
      sprintf(qstr, "qqfam%s", fam_in) ;
    }
    else if (nonempty(gen_in)) {
      if (nonempty(spec_in)) {
	if (nonempty(tri_in)) {
	  if (nonempty(quad_in)) {
	    request_level = 5 ;
	    sprintf(qstr, "qqgen%s&qqspe%s&qqtri%s&qqqua%s",
		    fam_in, gen_in, tri_in, quad_in) ;
	  }
	  else {
	    request_level = 4 ;
	    sprintf(qstr, "qqgen%s&qqspe%s&qqtri%s",
		    fam_in, gen_in, tri_in) ;
	  }
	}
	else {
	  request_level = 3 ;
	  sprintf(qstr, "qqgen%s&qqspe%s", gen_in, spec_in) ;
	}
      }
      else {
	request_level = 2 ;
	sprintf(qstr, "qqgen%s", gen_in) ;
      }
    }
    else {
      return_header("text/html") ;
      puts("<title>Sorry!</title>") ;
      puts("<h1>Sorry!</h1>") ;
      puts("The URL you requested is malformed. When querying by name,") ;
      puts("you must provide one of the following:") ;
      puts("<ul>") ;
      puts("<li>A request for all specimens") ;
      puts("<li>A family") ;
      puts("<li>A genus") ;
      puts("<li>A genus and species") ;
      puts("<li>A genus, species, and tri-level infra") ;
      puts("<li>A genus, species, tri- and quad-level infras") ;
      puts("</ul>") ;
      puts("Please check the URL structure and try again.") ;
      exit(1) ;
    }

    if (request_level > 0) {
      if (nonempty(ntype_in) && strcmp(ntype_in, "native")) {
	char ncode[10], tcode[10], *prefix, *code ;
	get_bonap_info_by_qstr(qstr, name_html, ncode, tcode) ;
	prefix = !strcmp(ntype_in, "bonap_n") ? "qqbnn" : "qqbtn" ;
	code = !strcmp(ntype_in, "bonap_n") ? ncode : tcode ;
	switch(request_level) {
	case 1: 
	  sprintf(qstr, "qqfco%s", tcode) ; break ;
	case 2:
	  sprintf(qstr, "qqgco%s", tcode) ; break ;
	case 3:
	  sprintf(qstr, "(%s%s|qqsco%s", prefix, code, tcode) ; break ;
	case 4:
	  sprintf(qstr, "(%s%s|qqtco%s", prefix, code, tcode) ; break ;
	case 5:
	  sprintf(qstr, "%s%s", prefix, code) ; break ;
	}
      }
      else need_name = 1 ;
    }
  }

  /* after all this drill "name_html" should contain the html form
     of the thing being queried for or need_name != 0 */

  if (nonempty(cutoff_in)) {
    if (!strcmp(cutoff_in, "fam") && request_level <= 1) {
      cutoff_level = 1 ;
      sprintf(qstr+strlen(qstr), "&qqfrp") ;
    }
    else if (!strcmp(cutoff_in, "gen") && request_level <= 2) {
      cutoff_level = 2 ;
      sprintf(qstr+strlen(qstr), "&qqgrp") ;
    }
    else if (!strcmp(cutoff_in, "spec") && request_level <= 3) {
      sprintf(qstr+strlen(qstr), "&qqtax") ;
      cutoff_level = 3 ;
    }
    else {
      return_header("text/html") ;
      puts("<title>Sorry!</title>") ;
      puts("<h1>Sorry!</h1>") ;
      puts("The URL you requested is malformed. When supplying a cutoff,") ;
      puts("the cutoff must be at a taxonomic level less than or equal to ") ;
      puts("the level of the query (for example, you cannot have a cutoff ") ;
      puts("of \"family\" when listing a genus). Please check the URL ") ;
      puts("and try again.") ;
      exit(1) ;
    }
  }
  else cutoff_level = 0 ;
  
  if (nonempty(loc_in)) 
    sprintf(qstr+strlen(qstr), "&qqloc%s", loc_in) ;

  mg_bool_query(qstr,"/bwg/FLORA/tracy/mgdata", "tracyspecs", 
		&qd) ;

  num_hits = mg_get_num_returned(qd) ;

  if (num_hits == 0) {
    if (nonempty(loc_in)) return_nothing() ;
    else {
      return_header("text/html") ;
      puts("<title>Sorry!</title>") ;
      puts("<h1>Sorry!</h1>") ;
      puts("There are no items in the database which corresponded to your ") ;
      puts("query.") ;
    }
  }
  else {
    void *dlp=NULL ;
    char s[256] ;
    char stored_fam[80], stored_gen[80], stored_spec[80] ;
    char stored_tri[80], stored_quad[80] ;
    int new_fam, new_gen, new_spec ;
    int printing_genera, printing_species, printing_specimens ;

    if (need_name) {
      void *lp=NULL ;
      char tri_html[80], quad_html[80] ;
      mg_setup_doc_line_producer(qd, mg_get_doc_num(qd), &lp) ;
      while (mg_dlp_more_lines(lp)) {
	char s[256] ;
	mg_dlp_next_line(lp, s) ;
	if (request_level == 1) {
	  if (!strncmp(s, "qqfah ", 6))
	    strcpy(name_html, s+6) ;
	}
	else if (request_level == 2) {
	  if (!strncmp(s, "qqgeh ", 6))
	    strcpy(name_html, s+6) ;
	}
	else {
	  if (!strncmp(s, "qqsph ", 6))
	    strcpy(name_html, s+6) ;
	  else if (!strncmp(s, "qqiah ", 6))
	    strcpy(tri_html, s+6) ;
	  else if (!strncmp(s, "qqibh ", 6))
	    strcpy(quad_html, s+6) ;
	}
      }
      if (request_level >= 4)
	sprintf(name_html+strlen(name_html), " %s", tri_html) ;
      if (request_level == 5)
	sprintf(name_html+strlen(name_html), " %s", quad_html) ;
    }

    if (cutoff_level != 0) {
      printing_specimens = 0 ;
      stored_fam[0] = stored_gen[0] = stored_spec[0] = '\0' ;
      printing_genera = (cutoff_level >= 2) ;
      printing_species = (cutoff_level == 3) ;
    }
    else {
      printing_specimens = 1 ;
      printing_genera=printing_species = 0 ;
    }

    /* header stuff */

    return_header("text/html") ;
    printf("<body bgcolor=\"#ffffff\">\n") ;

    puts("<title>Flora of Texas Consortium - Texas Plant") ;
    puts("Distributions</title>") ;

    puts("<h1>Flora of Texas Consortium - Texas Plant Distributions</h1>") ;

    puts("<b>Currently listing: ") ;
    switch (request_level) {
    case 0: 
      switch (cutoff_level) {
      case 0: puts("all specimens") ; break ;
      case 1: puts("all families") ; break ;
      case 2: puts("all genera") ; break ;
      case 3: puts("all species") ; break ;
      }
      break ;
    case 1:
      switch (cutoff_level) {
      case 0: puts("specimens of family") ; break ;
      case 2: puts("genera of family") ; break ;
      case 3: puts("species of family") ; break ;
      }
      break ;
    case 2: 
      switch(cutoff_level) {
      case 0: puts("specimens of genus") ; break ;
      case 3: puts("species of genus") ; break ;
      }
      break ;
    default:
      puts("specimens of ") ; break ;
    }
    if (request_level > 0) puts(name_html) ;
    if (nonempty(county_in)) printf(" from %s County", county_in) ;
    puts("</b><p>") ;
    if (nonempty(ntype_in) && !strcmp(ntype_in, "bonap_t")) {
      puts("Items whose names are defined as synonymous to ") ;
      puts("the name requested are included in this list.<p>") ;
    }

    if (printing_genera) puts("<ul>") ;
    if (printing_species) puts("<ul>") ;

    do {
      char auth_name[80], herb_abbr[5], acc_num[16] ;
      char coll_name[80] ;
      char spec_loc[4] ;
      int cultivated ;

      auth_name[0] = '\0' ;
      coll_name[0] = '\0' ;
      stored_tri[0] = stored_quad[0] = '\0' ;

      mg_setup_doc_line_producer(qd, mg_get_doc_num(qd), &dlp) ;
      while (mg_dlp_more_lines(dlp)) {
	mg_dlp_next_line(dlp, s) ;

	if (!strncmp(s, "qqfah", 5)) {
	  if (strcmp(s+6, stored_fam)) {
	    new_fam=1 ;
	    strcpy(stored_fam, s+6) ;
	  }
	  else new_fam = 0 ;
	}

	if (!strncmp(s, "qqgeh", 5)) {
	  if (strcmp(s+6, stored_gen)) {
	    new_gen=1 ;
	    strcpy(stored_gen, s+6) ;
	  }
	  else new_gen = 0 ;
	}

	if (!strncmp(s, "qqsph", 5)) {
	  if (strcmp(s+6, stored_spec)) {
	    new_spec=1 ;
	    strcpy(stored_spec, s+6) ;
	  }
	  else new_spec = 0 ;
	}

	if (!strncmp(s, "qqiah", 5)) 
	  strcpy(stored_tri, s+6) ;
	if (!strncmp(s, "qqibh", 5))
	  strcpy(stored_quad, s+6) ;

	if (printing_specimens) {
	  if (!strncmp(s, "qqaut", 5)) 
	    strcpy(auth_name, s+6) ;
	  else if (!strncmp(s, "qqhrb", 5)) 
	    strcpy(herb_abbr, s+5) ;
	  else if (!strncmp(s, "qqacc", 5)) {
	    strcpy(acc_num, s+5) ;
	    for (i=0 ; acc_num[i] != '\0' ; i++) acc_num[i] += '0'-'A' ;
	  }
	  else if (!strncmp(s, "qqcnm", 5)) 
	    strcpy(coll_name, s+6) ;
	  else if (!strncmp(s, "qqcul", 5)) 
	    cultivated = !strncmp(s+5, "yes", 3) ;
	  else if (!nonempty(loc_in) && !strncmp(s, "qqloc", 5))
	    strcpy(spec_loc, s+5) ;
	}

      }

      if (printing_specimens) {
	printf("%s %s ", herb_abbr, acc_num, auth_name) ;
	if (nonempty(auth_name)) printf(" %s", auth_name) ;
	else {
	  fputs(stored_spec, stdout) ;
	  if (nonempty(stored_tri))
	    printf(" %s", stored_tri) ;
	  if (nonempty(stored_quad))
	    printf(" %s", stored_quad) ;
	}

	printf(", collected by %s", coll_name) ;
	if (cultivated) printf(" from cultivated material") ;
	if (!nonempty(loc_in))
	  printf(", %s county ", 
		 map_convert_region_name("/bwg/FLORA/cgi/maps", "tx",
					 "ut_abbr", "cname", spec_loc)) ;
	puts("<br>") ;
      }
      else {
	if (new_fam) {
	  if (printing_specimens) puts("</ul>") ;
	  if (printing_species) puts("</ul>") ;
	  if (printing_genera) puts("</ul>") ;

	  printf("Family %s<br>", stored_fam) ;
	}
	if (printing_genera && new_gen) {
	  if (new_fam)
	    puts("<ul>") ;
	  else {
	    if (printing_specimens) puts("</ul>") ;
	    if (printing_species) puts("</ul>") ;
	  }
	  printf("Genus %s", stored_gen) ;
	  puts("<br>") ;
	}
	if (printing_species && new_spec) {
	  if (new_gen) printf("<ul>") ;
	  else {
	    if (printing_specimens) puts("</ul>") ;
	  }
	  printf("%s<br>", stored_spec) ;
	}
      }

    } while (mg_goto_next_doc(qd)) ;
    if (printing_specimens) puts("</ul>") ;
    if (printing_species) puts("</ul>") ;
    if (printing_genera) puts("</ul>") ;

    /* footer stuff */

    puts("<hr>") ;
    puts("This page was constructed by a program written by ") ;
    puts("<a href=\"mailto:[email protected]\">") ;
    puts("Erich Schneider</a> of the ") ;
    puts("<a href=\"http://www.csdl.tamu.edu\">") ;
    puts("Center for the Study of Digital Libraries</a> ") ;
    puts("working for the ") ;
    puts("<a href=\"http://www.csdl.tamu.edu/FLORA/tamuherb.htm\">") ;
    puts("Texas A&amp;M Bioinformatics Working Group</a>.") ;
  }
}
Ejemplo n.º 4
0
int main(void) {

  char *request_method = NULL;
  char *size_of_query_string = NULL;
  char *query_string = NULL;
  char *decoded_query_string = NULL;
  blocking_struct_t *bs = NULL;
  query_str_t *qs = NULL;
  char filepath[MAX_PATH_LENGTH];
  struct stat64 file_stats;
  struct stat fdtfile_stats;
  char *fdt_buf = NULL;
  unsigned int fdt_length;
  unsigned fdt_nbytes;
  FILE *fdt_fp;
  fdt_t *fdt = NULL;
  file_t *file = NULL;

  int fec_ratio = 0;
  
  char *buf = NULL;
  unsigned long long nbytes;
  unsigned long long pos;
  int fp;

  trans_block_t *tr_block = NULL;
  trans_unit_t *tr_unit = NULL;

  qs_missing_block_t *ms_block = NULL;
  qs_missing_symbol_t *ms_symbol = NULL;

  int i;
  char *uri_path = NULL;

  char *repair_conf_file = NULL;
  repair_arguments_t ra;
  int retval;
  FILE *ptm_repair_file;

  unsigned int sbn = 0;

  repair_conf_file = getenv("RepairConfFile");

  if(repair_conf_file == NULL) {
    return -1;
  }

  if(repair_conf_file[strlen(repair_conf_file)-1] == '\r') {
    repair_conf_file[strlen(repair_conf_file)-1] = '\0';
  }
  
  retval = parse_repair_conf_file(repair_conf_file, &ra);
    
  if(retval == -1) {
    return -1;
  }

  retval = parse_flute_conf_file(&ra);
    
  if(retval == -1) {
    return -1;
  }

  if(stat(ra.fdt_file, &fdtfile_stats) == -1) {
    return -1;
  }

  fdt_length = fdtfile_stats.st_size;

  /* Allocate memory for buf, to read fdt file to it */
  if(!(fdt_buf = (char*)calloc((fdt_length + 1), sizeof(char)))) {
    return -1;
  }

  if((fdt_fp = fopen(ra.fdt_file, "rb")) == NULL) {
    free(fdt_buf);
    return -1;
  }

  fdt_nbytes = fread(fdt_buf, 1, fdt_length, fdt_fp);

  if(fdt_nbytes <= 0) {
    fclose(fdt_fp);
    free(fdt_buf);
    return -1;
  }

  fdt = decode_fdt_payload(fdt_buf);
  free(fdt_buf);
  fclose(fdt_fp);

  request_method = getenv("REQUEST_METHOD");

  if(strcmp(request_method, "GET") == 0) {
    query_string = getenv("QUERY_STRING");

    if(query_string == NULL) {
      FreeFDT(fdt);
      return -1;
    }
  }
  else {
      size_of_query_string = getenv("CONTENT_LENGTH");

      if(!(query_string = (char*)calloc((atoi(size_of_query_string) + 1), sizeof(char)))) {
	FreeFDT(fdt);
	return -1;
      }

      read(0, query_string, atoi(size_of_query_string)); /* 0 is stdin */
  }

  decoded_query_string = decode_query_string(query_string);
  
  if(decoded_query_string == NULL) {

    if(strcmp(request_method, "POST") == 0) {
      free(query_string);
    }
    FreeFDT(fdt);
    return -1;
  }

  /* parse query string */
  qs = parse_query_string(decoded_query_string);
  
  if(qs == NULL) {
    free(decoded_query_string);

    if(strcmp(request_method, "POST") == 0) {
      free(query_string);
    }

    FreeFDT(fdt);
    return -1;
  }

  /* Find file struct */

  file = fdt->file_list;

  while(file != NULL) {
    uri_path = get_uri_host_and_path(qs->fileURI);

    if(strstr(file->location, uri_path) != NULL) {
      break;
    }

    file = file->next;
  }

  if(file == NULL) {

    free(decoded_query_string);

    if(strcmp(request_method, "POST") == 0) {
      free(query_string);
    }

    FreeFDT(fdt);
    free_query_str(qs);
    return -1;
  }

  if(strcmp(ra.repair_method, "PTM") == 0){

    if((ptm_repair_file = fopen(ra.requested_blocks_file, "a")) == NULL) {
      free(decoded_query_string);

      if(strcmp(request_method, "POST") == 0) {
	free(query_string);
      }

      free_query_str(qs);
      FreeFDT(fdt);
      return -1;
    }

    ms_block = qs->block_list;

    if(ms_block == NULL) {
      fprintf(ptm_repair_file, "%llu:*\n", file->toi);
    }
    else {
      while(ms_block != NULL) {
	fprintf(ptm_repair_file, "%llu:%i\n", file->toi, ms_block->sbn);
	ms_block = ms_block->next;
      }
    }
    
    free(decoded_query_string);

    if(strcmp(request_method, "POST") == 0) {
      free(query_string);
    }

    free_query_str(qs);
    FreeFDT(fdt);
    fclose(ptm_repair_file);
    
    printf("%s%c%c", "Location:", 13, 10);
    printf("%c%c", 13, 10);
  }
  else {
    memset(filepath, 0, MAX_PATH_LENGTH);
    
    if(ra.base_dir != NULL) {
      if(!(strcmp(ra.base_dir, "") == 0)) {
	strcpy(filepath, ra.base_dir);
	strcat(filepath, "/");
      }
    } 
    strcat(filepath, get_uri_host_and_path(qs->fileURI));

    if(file->encoding != NULL) {
#ifdef USE_ZLIB
      if(strcmp(file->encoding, "gzip") == 0) {
	strcat(filepath, GZ_SUFFIX);
      }
#endif
    }

    /* use stat to get file size */
    
    if(stat64(filepath, &file_stats) == -1) {
      free(decoded_query_string);

      if(strcmp(request_method, "POST") == 0) {
	free(query_string);
      }

      free_query_str(qs);
      FreeFDT(fdt);
      return -1;

    }
    
    if(file_stats.st_size == 0) {
      free(decoded_query_string);

      if(strcmp(request_method, "POST") == 0) {
	free(query_string);
      }

      free_query_str(qs);
      FreeFDT(fdt);
      return -1;
    }
    
    /* calculate blocking structure */

    bs = compute_blocking_structure((unsigned long long)file_stats.st_size, file->max_sb_len, file->es_len);
       
    /* Allocate memory for buf */
    if(!(buf = (char*)calloc((unsigned int)(file->es_len * file->max_sb_len), sizeof(char)))) {
      free(decoded_query_string);

      if(strcmp(request_method, "POST") == 0) {
	free(query_string);
      }

      free_query_str(qs);
      free(bs);
      FreeFDT(fdt);
      return -1;
    }
    
    /* File to repair */
    if((fp = open64(filepath, 0, 0)) < 0) {
      free(decoded_query_string);

      if(strcmp(request_method, "POST") == 0) {
	free(query_string);
      }

      free_query_str(qs);
      free(bs);
      free(buf);
      FreeFDT(fdt);
      return -1;
    }
        
    /* http response message */    
    /* http response message headers*/

    printf("%s%c%c", "Content-Type: application/simpleSymbolContainer", 13, 10);
    printf("%s%c%c", "Content-Transfer-Encoding: binary", 13, 10);
    printf("%c%c", 13, 10);
    
    ms_block = qs->block_list;

    if(ms_block == NULL) {
     
      while(sbn < bs->N) {
	
	if(sbn < bs->I) {
          nbytes = file->es_len * (bs->A_large);
        }
        else {
          nbytes = file->es_len * (bs->A_small);
        }
	
	memset(buf, 0, (file->es_len * file->max_sb_len));
        nbytes = read(fp, buf, (unsigned int)nbytes);
	
	if(nbytes < 0) {
	  free(decoded_query_string);

	  if(strcmp(request_method, "POST") == 0) {
	    free(query_string);
	  }

          free_query_str(qs);
          free(buf);
          free(bs);
          close(fp);
          FreeFDT(fdt);
	  return -1;
	}
	
	/* all could use null_fec_encode_src_block() functions, because FEC symbols are not transmitted */
	
        if(file->fec_enc_id == COM_NO_C_FEC_ENC_ID) {
          tr_block = null_fec_encode_src_block(buf, nbytes, sbn, file->es_len);
        }
        else if(file->fec_enc_id == SIMPLE_XOR_FEC_ENC_ID) {
          tr_block = xor_fec_encode_src_block(buf, nbytes, sbn, file->es_len);
        }
        else if(((file->fec_enc_id == SB_SYS_FEC_ENC_ID) && (file->fec_inst_id == REED_SOL_FEC_INST_ID))) {
          tr_block = rs_fec_encode_src_block(buf, nbytes, sbn, file->es_len, fec_ratio, file->max_sb_len);
        }
        else {
          free(decoded_query_string);

	  if(strcmp(request_method, "POST") == 0) {
	    free(query_string);
	  }

          free_query_str(qs);
          free(buf);
          free(bs);
          close(fp);
          FreeFDT(fdt);
          return -1;
        }
	
	if(tr_block == NULL) {
          free(decoded_query_string);

	  if(strcmp(request_method, "POST") == 0) {
	    free(query_string);
	  }

          free_query_str(qs);
          free(buf);
          free(bs);
          close(fp);
          FreeFDT(fdt);
          return -1;
        }
	
	tr_unit = tr_block->unit_list;
	
	add_length_indicator(tr_block->k);

	if(file->fec_enc_id == COM_NO_C_FEC_ENC_ID) {
	  add_fec_plid_0_130((unsigned short)tr_block->sbn, (unsigned short)tr_unit->esi);
	}
	else if(file->fec_enc_id == SIMPLE_XOR_FEC_ENC_ID) {
	  add_fec_plid_128(tr_block->sbn, tr_unit->esi);
	}
	else if(((file->fec_enc_id == SB_SYS_FEC_ENC_ID) && (file->fec_inst_id == REED_SOL_FEC_INST_ID))) {
	  add_fec_plid_129(tr_block->sbn, (unsigned int)tr_block->k, (unsigned int)tr_unit->esi);
	}
	
	while(1) {
	  
	  for(i = 0; i < tr_unit->len; i++) {
	    printf("%c", tr_unit->data[i]);
	  }
	  
	  if(tr_unit->esi == (tr_block->k - 1)) { /* no FEC symbols */
	    break;
	  }
	  
	  tr_unit++;
	}

	tr_unit = tr_block->unit_list;

        while(1) {
          free(tr_unit->data);

          if(tr_unit->esi == (tr_block->n - 1)) {
            break;
          }

          tr_unit++;
        }

        free(tr_block->unit_list);
        free(tr_block);

        sbn++;
      }
    }
    else {
    
      while(ms_block != NULL) {
	
	/* Set place where to read */
	
	if(ms_block->sbn < bs->I) {
	  pos = (unsigned long long)ms_block->sbn * (unsigned long long)bs->A_large * (unsigned long long)file->es_len;
	}
	else {
	  pos = ( ( ( (unsigned long long)bs->I * (unsigned long long)bs->A_large ) + 
		    ( (unsigned long long)ms_block->sbn - (unsigned long long)bs->I ) * (unsigned long long)bs->A_small ) * (unsigned long long)file->es_len );
	}

	/* set correct position */
	
	if(lseek64(fp, pos, SEEK_SET) == -1) {
	  free(decoded_query_string);

	  if(strcmp(request_method, "POST") == 0) {
	    free(query_string);
	  }

	  free_query_str(qs);
	  free(buf);
	  free(bs);
	  close(fp);
	  FreeFDT(fdt);
	  return -1;
	}
	
	if(ms_block->sbn < bs->I) {
	  nbytes = file->es_len * (bs->A_large);
	}
	else {
	  nbytes = file->es_len * (bs->A_small);
	}
	
	memset(buf, 0, (file->es_len * file->max_sb_len));
	nbytes = read(fp, buf, (unsigned int)nbytes);
	
	/* all could use null_fec_encode_src_block() functions, because FEC symbols are not transmitted */
	
	if(file->fec_enc_id == COM_NO_C_FEC_ENC_ID) {
	  tr_block = null_fec_encode_src_block(buf, nbytes, ms_block->sbn, file->es_len);
	}
	else if(file->fec_enc_id == SIMPLE_XOR_FEC_ENC_ID) {
	  tr_block = xor_fec_encode_src_block(buf, nbytes, ms_block->sbn, file->es_len);
	}
	else if(((file->fec_enc_id == SB_SYS_FEC_ENC_ID) && (file->fec_inst_id == REED_SOL_FEC_INST_ID))) {
	  tr_block = rs_fec_encode_src_block(buf, nbytes, ms_block->sbn, file->es_len, fec_ratio, file->max_sb_len);
	}
	else {
	  free(decoded_query_string);

	  if(strcmp(request_method, "POST") == 0) {
	    free(query_string);
	  }

	  free_query_str(qs);
	  free(buf);
	  free(bs);
	  close(fp);
	  FreeFDT(fdt);
	  return -1; 
	}
	
	if(tr_block == NULL) {
	  free(decoded_query_string);

	  if(strcmp(request_method, "POST") == 0) {
	    free(query_string);
	  }

	  free_query_str(qs);
	  free(buf);
	  free(bs);
	  close(fp);
	  FreeFDT(fdt);
	  return -1;
	}
	
	ms_symbol = ms_block->es_list;
	
	if(ms_symbol == NULL) {
	  tr_unit = tr_block->unit_list;
	  
	  add_length_indicator(tr_block->k);
	  
	  if(file->fec_enc_id == COM_NO_C_FEC_ENC_ID) {
	    add_fec_plid_0_130((unsigned short)ms_block->sbn, (unsigned short)tr_unit->esi);
	  }
	  else if(file->fec_enc_id == SIMPLE_XOR_FEC_ENC_ID) {
	    add_fec_plid_128(ms_block->sbn, tr_unit->esi);
	  }
	  else if(((file->fec_enc_id == SB_SYS_FEC_ENC_ID) && (file->fec_inst_id == REED_SOL_FEC_INST_ID))) {
	    add_fec_plid_129(ms_block->sbn, (unsigned int)tr_block->k, (unsigned int)tr_unit->esi);
	  }
	  
	  while(1) {
	    
	    for(i = 0; i < tr_unit->len; i++) {
	      printf("%c", tr_unit->data[i]);
	    }
	    
	    if(tr_unit->esi == (tr_block->k - 1)) { /* no FEC symbols */
	      break;
	    }
	    
	    tr_unit++;
	  }
	}
	else {
	  
	  while(ms_symbol != NULL) {
	    
	    tr_unit = tr_block->unit_list;
	    
	    while(1) {
	      if(tr_unit->esi == ms_symbol->esi) {
		add_length_indicator(1);
		
		if(file->fec_enc_id == COM_NO_C_FEC_ENC_ID) {
		  add_fec_plid_0_130((unsigned short)ms_block->sbn, (unsigned short)ms_symbol->esi);
		}
		else if(file->fec_enc_id == SIMPLE_XOR_FEC_ENC_ID) {
		  add_fec_plid_128(ms_block->sbn, ms_symbol->esi);
		}
		else if(((file->fec_enc_id == SB_SYS_FEC_ENC_ID) && (file->fec_inst_id == REED_SOL_FEC_INST_ID))) {
		  add_fec_plid_129(ms_block->sbn, (unsigned short)tr_block->k,
				   (unsigned short)ms_symbol->esi);
		}
		
		for(i = 0; i < tr_unit->len; i++) {
		  printf("%c", tr_unit->data[i]);
		}
		
		break;
	      }
	      
	      if(tr_unit->esi == (tr_block->k - 1)) { /* no FEC symbols */
		break;
	      }
	      
	      tr_unit++;
	    }
	    ms_symbol = ms_symbol->next;
	  }
	}
	
	tr_unit = tr_block->unit_list;
	
	while(1) {
	  free(tr_unit->data);
	  
	  if(tr_unit->esi == (tr_block->n - 1)) {
	    break;
	  }
	  
	  tr_unit++;
	} 
	
	free(tr_block->unit_list);
	free(tr_block);
	
	ms_block = ms_block->next;
      }
    }

    free(decoded_query_string);

    if(strcmp(request_method, "POST") == 0) {
      free(query_string);
    }

    free_query_str(qs);
    free(bs);
    free(buf);
    close(fp);        
    FreeFDT(fdt);
  }

  return 0;
}
Ejemplo n.º 5
0
main()
{
  char *rle_fname, *colors_in, *array_in, *s, rle_fname_full[80] ;
  char *scale_in, *top_in, *bottom_in, *left_in, *right_in ;
  int num_colors, *r_level, *g_level, *b_level ;
  int *r_array, *g_array, *b_array ;
  int i, nregs, step ;

  /* hex digits here are number plus 'A' */

  decode_query_string(8,"fname", &rle_fname, "colors", &colors_in, 
		      "array",&array_in, "scale", &scale_in,
		      "top", &top_in, "bot", &bottom_in,
		      "left", &left_in, "right", &right_in) ;

  num_colors = ((int)strlen(colors_in) / 6)+1 ;
  r_level = (int *)malloc(sizeof(int) * num_colors) ;
  g_level = (int *)malloc(sizeof(int) * num_colors) ;
  b_level = (int *)malloc(sizeof(int) * num_colors) ;

  r_level[0] = g_level[0] = b_level[0] = 255 ;

  for (s=colors_in, i=1 ; *s != '\0' ; i++, s+=6) {
    r_level[i] = hexdigit(s[0])*16 + hexdigit(s[1]) ;
    g_level[i] = hexdigit(s[2])*16 + hexdigit(s[3]) ;
    b_level[i] = hexdigit(s[4])*16 + hexdigit(s[5]) ;
  }

  nregs = strlen(array_in) ;
  r_array = (int *)malloc(sizeof(int) * nregs) ;
  g_array = (int *)malloc(sizeof(int) * nregs) ;
  b_array = (int *)malloc(sizeof(int) * nregs) ;

  for (i=0 ; i < nregs ; i++) {
    if (array_in[i] >= '0' && array_in[i] <= '9') 
      step = array_in[i] - '0' ;
    else if (array_in[i] >= 'a' && array_in[i] <= 'z') 
      step = array_in[i] - 'a' + 10 ;
    else if (array_in[i] >= 'A' && array_in[i] <= 'Z') 
      step = array_in[i] - 'Z' + 36 ;
    else step = 0 ;
    r_array[i] = r_level[step] ;
    g_array[i] = g_level[step] ;
    b_array[i] = b_level[step] ;
  }

  printf("Content-type: image/gif\n\n") ;
  fflush(stdout) ;

  if (nonempty(scale_in) ||
      (nonempty(top_in) && nonempty(bottom_in) &&
       nonempty(left_in) && nonempty(right_in))) {
    void *image ;
    image = rle22mem(rle_fname, nregs, r_array, g_array, b_array) ;

    if (nonempty(top_in)) 
      rle2clip_mem(image, atoi(left_in), atoi(right_in), 
		   atoi(top_in), atoi(bottom_in)) ;

    if (nonempty(scale_in)) 
      rle2scale_mem(image, atof(scale_in)) ;

    rle2output_mem(image, 0) ;
  }
  else
    rle22gif(rle_fname, nregs, 0, r_array, g_array, b_array) ;
 
}
Ejemplo n.º 6
0
main()
{
  char *mode ;
//  char date[100];
  int i ;
  char new_mode[20] ;
  char *final_mode ;
//  FILE* datein;
//  datein = fopen("updated","r");
//  fgets(date,99,datein);
//  fclose(datein);
  decode_query_string(1,"mode", &mode) ;
  return_header("text/html") ;

  final_mode = mode + strlen(mode)-4 ;

  printf("<b><a target=_top href=\"http://www.texasflora.org\">Digital Flora of Texas</a> Herbarium Specimen Browser-");
  if (!strcmp(final_mode, "main")) 
    printf("Main Display") ;
  else if (!strcmp(final_mode, "mcty")) 
    printf("Visual County Filter") ;
  else if (!strcmp(final_mode, "sfor")) 
    printf("Get by Accession Number") ;
  else if (!strcmp(final_mode, "sing")) 
    printf("Single Item Display") ;
  else if (!strcmp(final_mode, "slst")) 
    printf("Specimen List") ;
  else if (!strcmp(final_mode, "imap")) 
    printf("Item Map") ;
  else if (!strcmp(final_mode, "icty")) 
    printf("Single County Specimen List") ;
	printf(" mode<br>");
//  printf(" mode <br>Last updated: %s</b><br>",date) ;

  printf("<form>") ;
  printf("<input type=button value=\"Help\" onClick=\"top.popup_help();\">") ;
  printf("<input type=button value=\"Restart\" onClick=\"top.restart();\">") ;
  printf("<input type=button value=\"DataFiles\" onClick=\"top.popup_herbariadata();\">");
  printf("<input type=button value=\"stats\" onClick=\"top.popup_stats();\">");
  printf("<input type=button value=\"Contributors\" onClick=\"top.popup_cons();\">");
  /*
     printf("<input type=button value=\"Settings\" onClick=\"top.display_settings();\">") ;
     */

  if (strlen(mode) > 4)
    printf(" <b>Back to: </b>") ;

  i = 0 ; 
  while (i+4 < strlen(mode)) {
    strncpy(new_mode, mode, i+4) ;
    new_mode[i+4] = '\0' ;
    if (!strncmp(mode+i, "main", 4))
      button("Main Display", new_mode) ;
    else if (!strncmp(mode+i, "sfor", 4))
      button("Get by Acc. Num.", new_mode) ;
    else if (!strncmp(mode+i, "imap", 4))
      button("Item map", new_mode) ;
    else if (!strncmp(mode+i, "slst", 4))
      button("Specimen List", new_mode) ;
    else if (!strncmp(mode+i, "icty", 4))
      button("Specimens in County", new_mode) ;
    i += 4 ;
  }

  if (!strcmp(final_mode, "main") ||
      !strcmp(final_mode, "slst") ||
      !strcmp(final_mode, "imap"))
    printf(" <b>Also available: </b>") ;

  if (!strcmp(final_mode, "main")) {
    button("Get by Acc. Num.", "mainsfor") ;
    button("Visual County Filter", "mainmcty") ;
  }
  else if (!strcmp(final_mode, "slst")) {
    button("Item Map", "mainimap") ;
    button("Visual County Filter", "mainslstmcty") ;
  }
  else if (!strcmp(final_mode, "imap")) {
    button("Specimen List", "mainslst") ;
    button("Visual County Filter", "mainimapmcty") ;
  }
  printf("</form>") ;
}
Ejemplo n.º 7
0
main()
{
  char *set_id, *sets_list ;

  decode_query_string(2, "set_id", &set_id, "sets_list", &sets_list) ;

  return_header("text/html") ;

  printf("<title>Voting system: post a new issue (%s)</title>",
	 set_id_to_name(sets_list, set_id)) ;
  printf("<h1>Voting system: post a new issue (%s)</h1>",
	 set_id_to_name(sets_list, set_id)) ;

  printf("For more detailed explanations of the various form items ") ;
  printf("on this page, follow the help links near the items. <p>") ;

  printf("<form method=post action=\"/FLORA/cgi/voting_post\">") ;
  printf("<input type=hidden name=\"sets_list\" value=\"%s\">", sets_list) ;
  printf("<input type=hidden name=\"set_id\" value=\"%s\">", set_id) ;


  printf("Your voting username: <input type=text name=\"name\"> ") ;
  printf("Your password: <input type=password name=\"password\">") ;
  printf("<A HREF=\"/FLORA/voting/voting_user_password_help.html\">(help)</A> <BR>") ;

  printf("Do you want your name listed as the proposer of this issue?") ;
  printf("<select size=1 name=\"anon_post\">") ;
  printf("<option value=0>Yes") ;
  printf("<option value=1>No") ;
  printf("</select> ") ;
  printf("<A HREF=\"/FLORA/voting/voting_options_help.html\">(help)</A> <BR>") ;

  printf("Ballot type:") ;
  printf("<select size=1 name=\"secret_ballot\">") ;
  printf("<option value=1>Secret ballot") ;
  printf("<option value=0>Accountable ballot") ;
  printf("</select>") ;
  printf("<A HREF=\"/FLORA/voting/voting_options_help.html\">(help)</A> <BR>") ;

  printf("Should partial vote counts be visible before the end of the voting period?") ;
  printf("<select size=1 name=\"constant_update\">") ;
  printf("<option value=0>No") ;
  printf("<option value=1>Yes") ;
  printf("</select>") ;
  printf("<A HREF=\"/FLORA/voting/voting_options_help.html\">(help)</A> <BR>") ;

  printf("Should people be able to change their votes during the voting period?") ;
  printf("<select size=1 name=\"can_change_vote\">") ;
  printf("<option value=0>No") ;
  printf("<option value=1>Yes") ;
  printf("</select>") ;
  printf("<A HREF=\"/FLORA/voting/voting_options_help.html\">(help)</A> <BR>") ;

  printf("One-line description: <A HREF=\"/FLORA/voting/voting_shortdesc_help.html\">(help)</A> <BR>") ;
  printf("<input type=text name=\"shortdesc\" size=80> <BR>") ;

  printf("Full description: <A HREF=\"/FLORA/voting/voting_longdesc_help.html\">(help)</A> <BR>") ;
  printf("<textarea name=\"longdesc\" rows=4 cols=80>") ;
  printf("</textarea> <BR>") ;

  printf("Choices (one per line): <A HREF=\"/FLORA/voting/voting_choices_help.html\">(help)</A> <BR>") ;
  printf("<textarea name=\"choices\" rows=4 cols=80>") ;
  printf("</textarea> <BR>") ;

  printf("How many choices can be selected? ") ;
  printf("<input type=text size=4 name=multiple_choice value=\"1\"> ") ;
  printf("<A HREF=\"/FLORA/voting/voting_num_choices_help.html\">(help)</A><BR>") ;

  printf("Vote counting date (votes must be cast before this day) - ") ;
  printf("month: <select size=1 name=\"countmo\">") ;
  printf("<option value=0>January") ;
  printf("<option value=1>February") ;
  printf("<option value=2>March") ;
  printf("<option value=3>April") ;
  printf("<option value=4>May") ;
  printf("<option value=5>June") ;
  printf("<option value=6>July") ;
  printf("<option value=7>August") ;
  printf("<option value=8>September") ;
  printf("<option value=9>October") ;
  printf("<option value=10>November") ;
  printf("<option value=11>December") ;
  printf("</select>") ;
  printf("day: <input type=text name=\"countday\" size=3> ") ;
  printf("year: <input type=text size=5 name=\"countyr\" value=\"1996\"> ") ;
  printf("<A HREF=\"/FLORA/voting/voting_date_help.html\">(help)</A> <BR>") ;

  printf("<input type=submit value=\"Post issue to voting system\">") ;
  printf("<input type=reset value=\"Clear form\">") ;
  printf("</form>") ;

}
Ejemplo n.º 8
0
main() 
{
  char *name, *password, *short_desc, *long_desc ;
  char *choices, *day_str, *month_str, *year_str ;
  char *anon_post_str, *secret_ballot_str, *constant_update_str ;
  char *can_change_vote_str, *multiple_choice_str ;

  decode_query_string(15, "set_id", &set_id, "sets_list", &sets_list,
		      "name", &name, "password", &password,
		      "shortdesc", &short_desc, "longdesc", &long_desc,
		      "choices", &choices, "countday", &day_str,
		      "countmo", &month_str, "countyr", &year_str,
		      "anon_post", &anon_post_str,
		      "secret_ballot", &secret_ballot_str,
		      "constant_update", &constant_update_str,
		      "can_change_vote", &can_change_vote_str,
		      "multiple_choice", &multiple_choice_str) ;

  strip_trailing_spaces(name) ;
  strip_trailing_spaces(short_desc) ;
  strip_trailing_spaces(long_desc) ;
  strip_trailing_spaces(choices) ;

  if (!nonempty(name) || !nonempty(password) || !nonempty(short_desc) ||
      !nonempty(long_desc) || !nonempty(choices) || 
      !nonempty(day_str) || !nonempty(month_str) || !nonempty(year_str)) {
    return_nothing() ;
  }
  else {
    return_header("text/html") ;

    if (!verify_password(set_id, name, password)) {
      printf("<title>Sorry! Invalid user name/password pair</title>\n") ;
      printf("<h1>Sorry! Invalid user name/password pair</h1>\n") ;
      printf("The password you entered did not match the user name ") ;
      printf("you entered.\n") ;
    }
    else if (!date_ok(day_str, month_str, year_str)) {
      printf("<title>Sorry! Invalid date</title>\n") ;
      printf("<h1>Sorry! Invalid date</h1>\n") ;
      printf("The date you entered made no sense (for example, ") ;
      printf("you entered a day number that was invalid for the ") ;
      printf("month you chose) or was not a future date.\n") ;
    }
    else {
      char id_str[64], fname[FILENAME_MAX] ;
      struct tm count_time ;
      long count_time_val ;
      int num_choices, i ;
      char *new_choices ;
      FILE *fp ;

      if (!strcmp(anon_post_str, "0"))
	sprintf(id_str, "%s.%d", name, (long)time(NULL)) ;
      else
	sprintf(id_str, "anonymous.%d", (long)time(NULL)) ;

      count_time.tm_sec = 1 ;
      count_time.tm_min = 0 ;
      count_time.tm_hour = 0 ;
      count_time.tm_mday = atoi(day_str) ;
      count_time.tm_mon = atoi(month_str) ;
      count_time.tm_year = atoi(year_str)-1900 ;
      count_time.tm_isdst = -1 ;
      count_time_val = (long)mktime(&count_time) ;

      /* remove "blank" choice lines */

      {
	char *s, *t ;
	new_choices = (char *)malloc(sizeof(char) * (strlen(choices)+1)) ;
	s = choices ;
	t = new_choices ;
	while (*s != '\0') {
	  while (isspace(*s)) s++ ;
	  while (*s != '\n' && *s != '\0') {
	    *t = *s ; t++ ; s++ ;
	  }
	  *t = *s ; 
	  if (*s != '\0') {
	    s++ ; t++ ;
	  }
	}
	if (new_choices[(int)strlen(new_choices)-1] == '\n')
	  new_choices[(int)strlen(new_choices)-1] = '\0' ;
      }

      for (i=0, num_choices=1 ; i < (int)strlen(new_choices) ; i++)
	if (new_choices[i] == '\n') num_choices++ ;

      sprintf(fname, "%s/%s.issues", file_prefix, set_id) ;
      fp = fopen(fname, "a") ;
      lock_file(fp) ;
      fprintf(fp, "%s\n%d\n%d\n", 
	      id_str, count_time_val, num_choices) ;
      fprintf(fp, "%s\n", multiple_choice_str) ;
      fprintf(fp, "%d\n",
	      atoi(secret_ballot_str) << 3 |
	      atoi(anon_post_str) << 2 |
	      atoi(can_change_vote_str) << 1 |
	      atoi(constant_update_str) << 0) ;
      fprintf(fp, "%s\n", short_desc) ;
      fclose(fp) ;

      sprintf(fname, "%s/%s.descriptions", file_prefix, set_id) ;
      fp = fopen(fname, "a") ;
      lock_file(fp) ;
      fprintf(fp, "BEGIN %s\n", id_str) ;
      fprintf(fp, "%s\n", long_desc) ;
      fprintf(fp, "END %s\n", id_str) ;
      fclose(fp) ;

      sprintf(fname, "%s/%s.choices", file_prefix, set_id) ;
      fp = fopen(fname, "a") ;
      lock_file(fp) ;
      fprintf(fp, "BEGIN %s\n", id_str) ;
      fprintf(fp, "%s\n", new_choices) ;
      fprintf(fp, "END %s\n", id_str) ;
      fclose(fp) ;

      printf("<title>Issue added</title>\n") ;
      printf("<h1>Issue added</h1>\n") ;

      printf("Your new issue has been added to the voting system. ") ;
      printf("You may now go to: <ul>") ;
      printf("<li>The overall ") ;
      printf("<a href=\"/FLORA/cgi/voting_list?sets_list=%s&set_id=%s\">issue list</a>",
	     sets_list, set_id) ;
      printf("<li>The new issue's ") ;
      printf("<a href=\"/FLORA/cgi/voting_issue?set_id=%s&issue_id=%s\">issue page</a>",
	     set_id, id_str) ;
      printf("</ul>") ;
      printf("or use your browser's \"back\" function to return to ") ;
      printf("where you came from.") ;
      send_mail(name, anon_post_str, count_time_val, short_desc, id_str) ;
    }
  }
}
Ejemplo n.º 9
0
main()
{
  char *ncode_in, *tcode_in, *gcode_in, *fcode_in ;
  char *ntype_in, *all_in, *fam_in, *gen_in, *spec_in, *tri_in, *quad_in ;
  char *cutoff_in, *nonbonap_in ;
  int need_name, request_level, cutoff_level ;
  char name_html[128], *name_plain ;
  char qstr[256] ;
  int i ;
  void *qd ;
  long num_hits ;

  if (!mg_db_available("/bwg/FLORA/tracy/mgdata", "tracyspecs_map")) {
    return_header("text/html") ;
    puts("<title>Sorry!</title>") ;
    puts("<h1>Sorry!</h1>") ;
    puts("The database of specimens used to draw maps, which is ") ;
    puts("required to process ") ;
    puts("your query, is currently being rebuilt. Please try again ") ;
    puts("in a little while.") ;
    exit(0) ;
  }

  decode_query_string(12, "ncode", &ncode_in,  /* bonap name code */ 
		      "tcode", &tcode_in,  /* bonap taxon code */
		      "gcode", &gcode_in,  /* ncode of genus */
		      "fcode", &fcode_in,  /* ncode of family */
		      "ntype", &ntype_in,  /* native/bonap_n/bonap_t */
		      "all", &all_in,
		      "fam", &fam_in, 
		      "gen", &gen_in,
		      "spec", &spec_in, 
		      "infra", &tri_in,
		      "infra2", &quad_in,
		      "cutoff", &cutoff_in /* clip level of map */) ;

  need_name = 0 ;

  if (nonempty(ncode_in) || nonempty(tcode_in) ||
      nonempty(gcode_in) || nonempty(fcode_in)) {
    char *code, *prefix ;
    if (nonempty(ncode_in)) {
      code = ncode_in ;
      prefix = "qqbnn" ;
    }
    else if (nonempty(tcode_in)) {
      code = tcode_in ; 
      prefix = "qqbtn" ;
    }
    else if (nonempty(gcode_in)){
      code = gcode_in ;
      prefix = "qqgco" ;
    }
    else if (nonempty(fcode_in)) {
      code = fcode_in ;
      prefix = "qqfco" ;
    }
    get_bonap_info_by_code(code, name_html, &request_level) ;
    strcpy(qstr, prefix) ;
    for (i=0 ; code[i] != '\0' ; i++) 
      sprintf(qstr+strlen(qstr), "%c", code[i]+'A'-'0') ;
  }
  else /* no bonap code, doing it by name */ {
    /* determine request level */
    if (nonempty(all_in)) {
      request_level = 0 ;
      sprintf(qstr, "qqspc") ;
    }
    else if (nonempty(fam_in)) {
      request_level = 1 ;
      sprintf(qstr, "qqfam%s", fam_in) ;
    }
    else if (nonempty(gen_in)) {
      if (nonempty(spec_in)) {
	if (nonempty(tri_in)) {
	  if (nonempty(quad_in)) {
	    request_level = 5 ;
	    sprintf(qstr, "qqgen%s&qqspe%s&qqtri%s&qqqua%s",
		    fam_in, gen_in, tri_in, quad_in) ;
	  }
	  else {
	    request_level = 4 ;
	    sprintf(qstr, "qqgen%s&qqspe%s&qqtri%s",
		    fam_in, gen_in, tri_in) ;
	  }
	}
	else {
	  request_level = 3 ;
	  sprintf(qstr, "qqgen%s&qqspe%s", gen_in, spec_in) ;
	}
      }
      else {
	request_level = 2 ;
	sprintf(qstr, "qqgen%s", gen_in) ;
      }
    }
    else { 
      return_header("text/html") ;
      puts("<title>Sorry!</title>") ;
      puts("<h1>Sorry!</h1>") ;
      puts("The URL you requested is malformed. When querying by name,") ;
      puts("you must provide one of the following:") ;
      puts("<ul>") ;
      puts("<li>A request for all specimens") ;
      puts("<li>A family") ;
      puts("<li>A genus") ;
      puts("<li>A genus and species") ;
      puts("<li>A genus, species, and tri-level infra") ;
      puts("<li>A genus, species, tri- and quad-level infras") ;
      puts("</ul>") ;
      puts("Please check the URL structure and try again.") ;
      exit(1) ;
    }

    if (request_level > 0) {
      if (nonempty(ntype_in) && strcmp(ntype_in, "native")) {
	char ncode[10], tcode[10], *prefix, *code ;
	get_bonap_info_by_qstr(qstr, name_html, ncode, tcode) ;
	prefix = !strcmp(ntype_in, "bonap_n") ? "qqbnn" : "qqbtn" ;
	code = !strcmp(ntype_in, "bonap_n") ? ncode : tcode ;
	switch(request_level) {
	case 1: 
	  sprintf(qstr, "qqfco%s", tcode) ; break ;
	case 2:
	  sprintf(qstr, "qqgco%s", tcode) ; break ;
	case 3:
	  sprintf(qstr, "(%s%s|qqsco%s", prefix, code, tcode) ; break ;
	case 4:
	  sprintf(qstr, "(%s%s|qqtco%s", prefix, code, tcode) ; break ;
	case 5:
	  sprintf(qstr, "%s%s", prefix, code) ; break ;
	}
      }
      else need_name = 1 ;
    }
  }

  /* after all this drill "name_html" should contain the html form
     of the thing being queried for or need_name != 0 */

  if (nonempty(cutoff_in)) {
    if (!strcmp(cutoff_in, "fam") && request_level <= 1) {
      cutoff_level = 1 ;
      sprintf(qstr+strlen(qstr), "&qqfrp") ;
    }
    else if (!strcmp(cutoff_in, "gen") && request_level <= 2) {
      cutoff_level = 2 ;
      sprintf(qstr+strlen(qstr), "&qqgrp") ;
    }
    else if (!strcmp(cutoff_in, "spec") && request_level <= 3) {
      sprintf(qstr+strlen(qstr), "&qqtax") ;
      cutoff_level = 3 ;
    }
    else {
      return_header("text/html") ;
      puts("<title>Sorry!</title>") ;
      puts("<h1>Sorry!</h1>") ;
      puts("The URL you requested is malformed. When supplying a cutoff,") ;
      puts("the cutoff must be at a taxonomic level less than or equal to ") ;
      puts("the level of the query (for example, you cannot have a cutoff ") ;
      puts("of \"family\" when mapping a genus). Please check the URL ") ;
      puts("and try again.") ;
      exit(1) ;
    }
  }
  else cutoff_level = 0 ;
  
  mg_bool_query(qstr,"/bwg/FLORA/tracy/mgdata", "tracyspecs_map", 
		&qd) ;

  num_hits = mg_get_num_returned(qd) ;

  return_header("text/html") ;
  printf("<body bgcolor=\"#ffffff\">\n") ;

  if (num_hits == 0) {
    puts("<title>Sorry!</title>") ;
    puts("<h1>Sorry!</h1>") ;
    puts("There are no items in the database which corresponded to your ") ;
    puts("query.") ;
  }
  else {
    int first ;
    int rbs[] = {162, 54, 0, 0, 0} ;
    int gs[] = {255, 255, 217, 140, 86} ;
    int single_rbs[]={0} ;
    int single_gs[]={217} ;

    void *map_info ;
    
    map_info = map_initialize("/bwg/FLORA/cgi/maps","tx",
			      GRADED_VALUE) ;

    if (cutoff_level != 0 && cutoff_level == request_level)
      map_assign_graded_colors(map_info, 1, 
			       single_rbs, single_gs, single_rbs) ;
    else
      map_assign_graded_colors(map_info, 5, rbs, gs, rbs) ;

    if (need_name) {
      void *lp=NULL ;
      char tri_html[80], quad_html[80] ;
      mg_setup_doc_line_producer(qd, mg_get_doc_num(qd), &lp) ;
      while (mg_dlp_more_lines(lp)) {
	char s[256] ;
	mg_dlp_next_line(lp, s) ;
	if (request_level == 1) {
	  if (!strncmp(s, "qqfah ", 6))
	    strcpy(name_html, s+6) ;
	}
	else if (request_level == 2) {
	  if (!strncmp(s, "qqgeh ", 6))
	    strcpy(name_html, s+6) ;
	}
	else {
	  if (!strncmp(s, "qqsph ", 6))
	    strcpy(name_html, s+6) ;
	  else if (!strncmp(s, "qqiah ", 6))
	    strcpy(tri_html, s+6) ;
	  else if (!strncmp(s, "qqibh ", 6))
	    strcpy(quad_html, s+6) ;
	}
      }
      if (request_level >= 4)
	sprintf(name_html+strlen(name_html), " %s", tri_html) ;
      if (request_level == 5)
	sprintf(name_html+strlen(name_html), " %s", quad_html) ;
    }

    do {
      add_to_county_bins(map_info, mg_get_doc_num(qd)) ;
    } while (mg_goto_next_doc(qd)) ;

    printf("<title>Flora of Texas Consortium - Texas Plant Distributions</title>\n");
	   
    printf("<h1>Flora of Texas Consortium - Texas Plant Distributions</h1>\n") ;

    printf("<b>Currently mapping: ") ;
    switch(request_level) {
    case 0: /* all */
      switch (cutoff_level) {
      case 0: printf("all specimens") ; break ;
      case 1: printf("all families") ; break ;
      case 2: printf("all genera") ; break ;
      case 3: printf("all species") ; break ;
      }
      break ;
    case 1: /* family */
      switch (cutoff_level) {
      case 0: printf("specimens of family ") ; break ;
      case 1: printf("distribution of family ") ; break ;
      case 2: printf("genera of family ") ; break ;
      case 3: printf("species of family ") ; break ;
      }
      break ;
    case 2: /* genus */
      switch (cutoff_level) {
      case 0: printf("specimens of genus ") ; break ;
      case 2: printf("distribution of genus ") ; break ;
      case 3: printf("species of genus ") ; break ;
      }
      break ;
    case 3: /* species */
      switch (cutoff_level) {
      case 0: printf("specimens of ") ; break ;
      case 3: printf("distribution of ") ; break ;
      }
      break ;
    case 4: /* tri-rank */
    case 5: /* quad-rank */
      printf("speciemens of ") ;
      break ;
    }

    if (request_level > 0)
      printf("%s", name_html) ;
    printf("</b><p>\n") ;
    if (nonempty(ntype_in) && !strcmp(ntype_in, "bonap_t")) {
      puts("Items whose names are defined as synonymous to ") ;
      puts("the name requested are included in this map.<p>") ;
    }

    if (cutoff_level > 0 && request_level == cutoff_level) {
      map_output_img(map_info, 0, NULL, NULL, NULL) ;
    }
    else {
      puts("Click on a colored county to get a list of mapped items ") ;
      puts("present in that county. <p>") ;

      printf("<b>Color Key (number of ") ;
      switch(cutoff_level) {
      case 0: printf("specimens") ; break ;
      case 1: printf("families") ; break ;
      case 2: printf("genera") ; break ;
      case 3: printf("species") ; break ;
      }
      printf(" per county):<p>") ;
      map_output_graded_color_key(map_info) ;
      printf("</b><p>\n") ;

      printf("<form method=get action=\"/FLORA/cgi/ftc_hsbdb_list1\">") ;
      map_output_form_elem(map_info, NULL, NULL, NULL) ;
      if (nonempty(cutoff_in)) 
	printf("<input type=hidden name=cutoff value=\"%s\">", cutoff_in) ;
      if (nonempty(ntype_in))
	printf("<input type=hidden name=ntype value=\"%s\">", ntype_in) ;
      if (nonempty(fcode_in))
	printf("<input type=hidden name=fcode value=\"%s\">", fcode_in) ;
      if (nonempty(gcode_in))
	printf("<input type=hidden name=gcode value=\"%s\">", gcode_in) ;
      if (nonempty(ncode_in))
	printf("<input type=hidden name=ncode value=\"%s\">", ncode_in) ;
      if (nonempty(tcode_in))
	printf("<input type=hidden name=tcode value=\"%s\">", tcode_in) ;
      if (nonempty(all_in))
	printf("<input type=hidden name=all value=\"%s\">", all_in) ;
      if (nonempty(fam_in))
	printf("<input type=hidden name=fam value=\"%s\">", fam_in) ;
      if (nonempty(gen_in))
	printf("<input type=hidden name=gen value=\"%s\">", gen_in) ;
      if (nonempty(spec_in))
	printf("<input type=hidden name=spec value=\"%s\">", spec_in) ;
      if (nonempty(tri_in))
	printf("<input type=hidden name=infra value=\"%s\">", tri_in) ;
      if (nonempty(quad_in))
	printf("<input type=hidden name=infra2 value=\"%s\">", quad_in) ;

      printf("</form>") ;
    }

    puts("<hr>") ;
    puts("This page was constructed by a program written by ") ;
    puts("<a href=\"mailto:[email protected]\">") ;
    puts("Erich Schneider</a> of the ") ;
    puts("<a href=\"http://www.csdl.tamu.edu\">") ;
    puts("Center for the Study of Digital Libraries</a> ") ;
    puts("working for the ") ;
    puts("<a href=\"http://www.csdl.tamu.edu/FLORA/tamuherb.htm\">") ;
    puts("Texas A&amp;M Bioinformatics Working Group</a>.") ;
  }
}
Ejemplo n.º 10
0
main()
{
  void *qd ;
  char command[256] ;
  int cur_fam_cutoff ;
  int cur_gen_cutoff ;
  int cur_tax_cutoff ;
  int total_families ;
  int total_genera ;
  int total_taxa ;
  int total_specimens ;
  int genera_in_family ;
  int taxa_in_family ;
  int specimens_in_family ;
  int taxa_in_genus ;
  int specimens_in_genus ;
  int specimens_in_taxon ;
  int item_num ;
  taxon *new_fam_ptr ;
  taxon *new_gen_ptr ;
  taxon *tax_ptr ;
  taxon **fam_array, **gen_array, **spec_array ;
  char *cur_fam_name ;
  char *cur_gen_name ;
  char *cur_tax_name ;
  int cur_tax_hybrid ;
  int cur_gen_hybrid ;
  char *loc_in ;
  char *herb_in ;
  char *items_in ;
  char *coll_in ;
  char *cult_in ;
  char *query_save = NULL ;
  FILE *fp ;
  taxon *query_root ;
  taxon *fam_root ;
  taxon *fam_ptr ;
  taxon *gen_ptr ;
  taxon *spec_ptr ;
  family_info *this_family_info ;
  genus_info *this_genus_info ; 
  taxon_info *this_taxon_info ;
  char *fam_out ;
  char *gen_out ;
  char *spec_out ;
  int new_taxon, new_genus, new_family ;
  int getting_everything = 0 ;
  int i,j,k ;
  int restrictions ;

  /* the CGI input to the program has two vars:
     "loc", a list of UT Texas county codes concatenated together w/o 
     delimieters
     "items", a list of the form:
     family;genus;genus;genus;/;family;genus;genus;/;family;genus...
     which indicates what items are to be "expanded"
     */

  decode_query_string(6, "loc", &loc_in, "items", &items_in, 
		      "herb", &herb_in,
		      "sort", &do_sort, 
		      "coll", &coll_in,
		      "cult", &cult_in) ;

  restrictions = 0 ;
  if (nonempty(loc_in)) restrictions++ ;
  if (nonempty(herb_in)) restrictions++ ;
  if (nonempty(coll_in)) restrictions++ ;
  if (nonempty(cult_in)) restrictions++ ;

  /* parse the "items" value and create a taxon tree in query_root */

  query_root = NULL ;
  if (nonempty(items_in)) {
    char *item ;
    int new_family = 1 ;

    query_save = strdup(items_in) ;
    item = strtok(items_in, ";") ;
    while (item != NULL) {
      if (item[0] == '/')
	new_family = 1 ;
      else if (new_family) {
	fam_ptr = insert_taxon_item(&query_root, item, NULL, NULL) ;
	new_family = 0 ;
      }
      else {
	insert_taxon_item(&(fam_ptr->nextlevel), item, NULL, NULL) ;
      }
      item = strtok(NULL, ";") ;
    }
  }

  /* The query.
     We want to get the numbers of the relevant records.
     If "loc" hasn't been set, we want all of them, and they're
     stored in a file. Otherwise we have to query MG. */

  {
    int i ;
    char qstr[2048] ;

    if (restrictions==0) sprintf(qstr, "qqspc") ;

    if (nonempty(cult_in)) {
      sprintf(qstr+strlen(qstr), "qqcul%s", cult_in) ;
      if (restrictions > 1) {
	sprintf(qstr+strlen(qstr), "&") ;
	restrictions-- ;
      }
    }

    if (nonempty(loc_in)) {
      sprintf(qstr+strlen(qstr), "(") ;
      for (i=0 ; loc_in[i] != '\0' ; i+=3) {
	sprintf(qstr+strlen(qstr), 
		"qqloc%c%c%c", loc_in[i], loc_in[i+1], loc_in[i+2]) ;
	if (loc_in[i+3] != '\0') 
	  sprintf(qstr+strlen(qstr), "|") ;
      }
      sprintf(qstr+strlen(qstr), ")") ;
      if (restrictions > 1) {
	sprintf(qstr+strlen(qstr), "&") ;
	restrictions-- ;
      }
    }

    if (nonempty(herb_in)) {
      char *temp, *tok ;

      temp = strdup(herb_in) ;
      tok = strtok(temp, " ") ;
      sprintf(qstr+strlen(qstr), "(") ;
      do {
	sprintf(qstr+strlen(qstr), "qqhrb%s", tok) ;
	tok = strtok(NULL, " ") ;
	if (tok != NULL) sprintf(qstr+strlen(qstr), "|") ;
      } while (tok != NULL) ;
      sprintf(qstr+strlen(qstr), ")") ;
      free(temp) ;
      if (restrictions > 1) {
	sprintf(qstr+strlen(qstr), "&") ;
	restrictions-- ;
      }
    }

    if (nonempty(coll_in)) {
      int first, count ;
      char num_str[8] ;
      char s[80] ;
      { 
	char fname[FILENAME_MAX] ;
	sprintf(fname, "%s/collector_list", HSB_DATA_PATH) ;
	fp = fopen(fname, "r") ;
      }
      count = 0 ;
      first = 1 ;
      sprintf(qstr+strlen(qstr), "(") ;
      while (!feof(fp)) {
	fgets(s, 80, fp) ;
	if (strstr(s, coll_in) != NULL) {
	  sprintf(num_str, "%d", count) ;
	  for (i=0 ; num_str[i] != '\0' ; i++) num_str[i] += 'A' - '0' ;
	  if (!first) sprintf(qstr+strlen(qstr), "|") ;
	  first = 0 ;
	  sprintf(qstr+strlen(qstr), "qqcnu%s", num_str) ;
	}
	count++ ;
      }
      sprintf(qstr+strlen(qstr), ")") ;
      fclose(fp) ;
    }

    mg_bool_query(qstr, HSB_DATA_PATH, "tracyspecs", &qd) ;
  }

  /* parse the returned output */

  /* these files have the format
     name1  x1
     name2  x2
     name3  x3 ...

     and indicate what names are associated with what record #s.
     1 to x1 have name1, x1+1 to x2 have name2, etc. */

  {
    char fname[FILENAME_MAX] ;
    sprintf(fname, "%s/family_ranges", HSB_DATA_PATH) ;
    fam_fp = fopen(fname, "r") ;
    sprintf(fname, "%s/genus_ranges", HSB_DATA_PATH) ;
    gen_fp = fopen(fname, "r") ;
    sprintf(fname, "%s/species_ranges", HSB_DATA_PATH) ;
    tax_fp = fopen(fname, "r") ;
  }

  /* in the cutoff variables, we keep track of the record number which,
     when we exceed it, will cause us to change family/genus/species.
     They are set at 0 initially so that we immediately load the
     names from the files */

  cur_fam_cutoff = cur_gen_cutoff = cur_tax_cutoff = 0 ;

  /* we use the taxon tree stored in query_root to tell us what to expand */

  fam_ptr = query_root ;
  gen_ptr = (fam_ptr == NULL ? NULL : fam_ptr->nextlevel) ;

  fam_root = NULL ;

  total_families = 0 ;
  total_genera = 0 ;
  total_taxa = 0 ;
  total_specimens = 0 ;

  if (mg_get_num_returned(qd) > 0) {
    do {
      item_num = mg_get_doc_num(qd) ;

      if (item_num <= cur_fam_cutoff) {

	if (item_num <= cur_gen_cutoff) {

	  if (item_num <= cur_tax_cutoff) 
	    /* do nothing */ ;
	  else {
	    new_taxon = 1 ;

	    /* if the current family and genus are both being expanded,
	       put a record for the last taxon in the tree (the family and
	       genus have already been added) */
	       
	    if (tax_ptr_name_match(fam_ptr, cur_fam_name) &&
		tax_ptr_name_match(gen_ptr, cur_gen_name)) {
	      new_fam_ptr = 
		insert_taxon_item(&fam_root, cur_fam_name, NULL, NULL) ;
	      new_gen_ptr = 
		insert_taxon_item(&(new_fam_ptr->nextlevel), cur_gen_name,
				  NULL, hybrid_cmp) ;
	      this_taxon_info = 
		new_taxon_info(cur_tax_hybrid, specimens_in_taxon) ;
	      insert_taxon_item(&(new_gen_ptr->nextlevel), cur_tax_name,
				this_taxon_info, hybrid_cmp) ;
	    }

	    get_next_taxon(item_num, &cur_tax_cutoff, 
			   &cur_tax_hybrid, &cur_tax_name) ;
	  }
	}
	else {
	  new_genus = 1 ;
	  new_taxon = 1 ;

	  /* if the current family is being expanded, put a
	     record for the last genus in the tree (the family
	     has already been added) */

	  if (tax_ptr_name_match(fam_ptr, cur_fam_name)) {
	    new_fam_ptr = 
	      insert_taxon_item(&fam_root, cur_fam_name, NULL, NULL) ;

	    this_genus_info = 
	      new_genus_info(cur_gen_hybrid, taxa_in_genus, 
			     specimens_in_genus) ;
	    new_gen_ptr = 
	      insert_taxon_item(&(new_fam_ptr->nextlevel), cur_gen_name,
				this_genus_info, hybrid_cmp) ;

	    /* if, additionally, the last genus was being expanded,
	       put an entry for the last taxon of it in the tree */

	    if (tax_ptr_name_match(gen_ptr, cur_gen_name)) {
	      this_taxon_info = 
		new_taxon_info(cur_tax_hybrid, specimens_in_taxon) ;
	      insert_taxon_item(&(new_gen_ptr->nextlevel), cur_tax_name,
				this_taxon_info, hybrid_cmp) ;
	      gen_ptr = gen_ptr->next ;
	    }
	  }

	  get_next_taxon(item_num, &cur_tax_cutoff, 
			 &cur_tax_hybrid, &cur_tax_name) ;

	  get_next_genus(item_num, &cur_gen_cutoff,
			 &cur_gen_hybrid, &cur_gen_name) ;
	}
      }
      else {
	new_family = 1 ;
	new_genus = 1 ;
	new_taxon = 1 ;

	if (cur_fam_cutoff != 0) { /* i.e. not the first item */

	  /* we always put an entry for the last family in the tree */

	  this_family_info = 
	    new_family_info(genera_in_family, taxa_in_family, 
			    specimens_in_family) ;
	  new_fam_ptr =
	    insert_taxon_item(&fam_root, cur_fam_name, this_family_info,
			      hybrid_cmp) ;

	  /* if we were expanding the last family, put a record for
	     the last genus in the tree */

	  if (tax_ptr_name_match(fam_ptr, cur_fam_name)) {
	    this_genus_info = 
	      new_genus_info(cur_gen_hybrid, taxa_in_genus, 
			     specimens_in_genus) ;

	    new_gen_ptr = 
	      insert_taxon_item(&(new_fam_ptr->nextlevel), cur_gen_name,
				this_genus_info, hybrid_cmp) ;

	    /* if we were expanded the last genus, put a record for the
	       last taxon in the tree */

	    if (tax_ptr_name_match(gen_ptr, cur_gen_name)) {
	      this_taxon_info =
		new_taxon_info(cur_tax_hybrid, specimens_in_taxon) ;
	      insert_taxon_item(&(new_gen_ptr->nextlevel), cur_tax_name,
				this_taxon_info, hybrid_cmp) ;
	    }

	    fam_ptr = fam_ptr->next ;
	    gen_ptr = (fam_ptr == NULL ? NULL : fam_ptr->nextlevel) ;
	  }

	}

	get_next_taxon(item_num, &cur_tax_cutoff,
		       &cur_tax_hybrid, &cur_tax_name) ;
	get_next_genus(item_num, &cur_gen_cutoff,
		       &cur_gen_hybrid, &cur_gen_name) ;
	get_next_family(item_num, &cur_fam_cutoff, &cur_fam_name) ;
      }


      if (new_family) {
	total_families++ ;
	specimens_in_family = 0 ;
	taxa_in_family = 0 ;
	genera_in_family = 0 ;
	new_family = 0 ;
      }

      if (new_genus) {
	total_genera++ ;
	genera_in_family++ ;
	specimens_in_genus = 0 ;
	taxa_in_genus = 0 ;    
	new_genus = 0 ;
      }

      if (new_taxon) {
	total_taxa++ ;
	taxa_in_family++ ;
	taxa_in_genus++ ;
	specimens_in_taxon = 0 ;
	new_taxon = 0 ;
      }

      total_specimens++ ;
      specimens_in_family++ ;
      specimens_in_genus++ ;
      specimens_in_taxon++ ;
    } while (mg_goto_next_doc(qd)) ;

    /* need to put the info for the last family examined in the tree */

    if (total_specimens > 0) {
      this_family_info = 
	new_family_info(genera_in_family, taxa_in_family, 
			specimens_in_family) ;
      new_fam_ptr =
	insert_taxon_item(&fam_root, cur_fam_name, this_family_info,
			  NULL) ;

      /* if we were expanding the last family, put it in too */
  
      if (tax_ptr_name_match(fam_ptr, cur_fam_name)) {
	this_genus_info = 
	  new_genus_info(cur_gen_hybrid, taxa_in_genus, 
			 specimens_in_genus) ;
	new_gen_ptr = 
	  insert_taxon_item(&(new_fam_ptr->nextlevel), cur_gen_name,
			    this_genus_info, hybrid_cmp) ;

	/* and if we were expanding the last genus, it goes in as well */
    
	if (tax_ptr_name_match(gen_ptr, cur_gen_name)) {
	  this_taxon_info =
	    new_taxon_info(cur_tax_hybrid, specimens_in_taxon) ;
	  insert_taxon_item(&(new_gen_ptr->nextlevel), cur_tax_name,
			    this_taxon_info, hybrid_cmp) ;
	}
      }
    }
  }

  fclose(fam_fp) ; fclose(gen_fp) ; fclose(tax_fp) ;

  return_header("text/html") ;

  printf("<script>\n") ;
  if (query_save != NULL) 
    printf("top.items='%s';\n", query_save) ;
  else printf("top.items='';\n") ;
  printf("</script>\n") ;

  if (nonempty(loc_in) || nonempty(herb_in) || nonempty(cult_in)) {

    printf("<b>Selection criteria</b>:<br>\n") ;

    if (nonempty(cult_in)) 
      printf("Showing only specimens taken from %s material<br>\n",
	     !strcmp(cult_in, "yes") ? "cultivated" : "non-cultivated") ;
    
    printf("Counties: <b>") ;
    if (nonempty(loc_in)) {
      int i, j ;
      char cur_loc[4] ;
      char *cname ;
      for (i=0 ; loc_in[i] != '\0' ; i+=3) {
	strncpy(cur_loc, loc_in+i, 3) ;
	cur_loc[3] = '\0' ;
	cname = map_convert_region_name(HSB_MAP_PATH, "tx", "ut_abbr", "cname",
					cur_loc) ;
	printf("%s%s", cname, loc_in[i+3] != '\0' ? ", " : "") ;
      }
    }
    else printf("all") ;
    printf("</b><br>\n") ;

    printf("Herbaria: <b>") ;
    if (nonempty(herb_in)) 
      printf("%s", herb_in) ;
    else printf("all") ;
    printf("</b><br>") ;
    printf("<p>\n") ;
  }
  /*
     if (nonempty(coll_in))
     printf("Collector string: %s<br>\n", coll_in) ;
     */

  if (total_specimens > 0) {
    printf("%d %s, %d %s, %d %s, %d %s ",
	   total_families, family_str(total_families),
	   total_genera, genus_str(total_genera),
	   total_taxa, taxon_str(total_taxa),
	   total_specimens, specimen_str(total_specimens)) ;

    printf("<a href=\"javascript:top.show_specs('all=yes');\">") ;
    printf("(specimens)</a> ") ;

    printf("<a href=\"javascript:top.do_map('all=yes');\">") ;
    printf("(map)</a><br>\n") ;

    pack_taxon_array(fam_root, &fam_array, &total_families) ;

    if (nonempty(do_sort))
      qsort(fam_array, total_families, sizeof(taxon *), 
	    (int (*)(const void *, const void *))fam_compare) ;

    for (i=0 ; i< total_families ; i++) {
      char fam_print_str[256], gen_print_str[256], tax_print_str[256] ;
      int num_genera, num_taxa, num_specs ;

      fam_ptr = fam_array[i] ;

      num_genera = ((family_info *)(fam_ptr->other_data))->num_genera ;
      num_taxa = ((family_info *)(fam_ptr->other_data))->num_taxa ;
      num_specs = ((family_info *)(fam_ptr->other_data))->num_specs ;

      printf("<a name=\"fam%s\"></a>", fam_ptr->name) ;

      if (!tax_ptr_name_match(fam_ptr, "xxx")) 
	printf("Family ") ;
      dump_items_anchor(query_root, fam_ptr->name, NULL) ;
      if (!tax_ptr_name_match(fam_ptr, "xxx"))
	printf("<b>%s</b></a>", fam_ptr->name) ;
      else printf("No family listed</a>") ;

      printf(" (%d %s, %d %s, %d %s) ", 
	     num_genera, genus_str(num_genera),
	     num_taxa, taxon_str(num_taxa),
	     num_specs, specimen_str(num_specs)) ;

      dump_show_specs_anchor(fam_ptr->name, NULL, NULL) ;
      printf("(%s)</a>", specimen_str(num_specs)) ;

      putchar(' ') ;
      dump_do_map_anchor(fam_ptr->name, NULL, NULL) ;
      printf("(map)</a>") ;

      printf("<br>\n") ;

      pack_taxon_array(fam_ptr->nextlevel, &gen_array, &genera_in_family) ;

      if (nonempty(do_sort))
	qsort(gen_array, genera_in_family, sizeof(taxon *), 
	      (int (*)(const void *, const void *))gen_compare) ;

      for (j=0 ; j < genera_in_family ; j++) {
	gen_ptr = gen_array[j] ;
	num_taxa = ((genus_info *)(gen_ptr->other_data))->num_taxa ;
	num_specs = ((genus_info *)(gen_ptr->other_data))->num_specs ;

	printf("<spacer type=horizontal size=20>") ;
	printf("<a name=\"fam%s_gen%s\"></a>", fam_ptr->name, gen_ptr->name);

	if (!tax_ptr_name_match(gen_ptr, "xxx"))
	  printf("Genus ") ;
	dump_items_anchor(query_root, fam_ptr->name, gen_ptr->name) ;
	if (!tax_ptr_name_match(gen_ptr, "xxx"))
	  printf("%s<i>%s</i></a>",
		 ((genus_info *)(gen_ptr->other_data))->hybrid ? "X " : "",
		 gen_ptr->name) ;
	else 
	  printf("No genus listed</a>") ;

	printf(" (%d %s, %d %s) ",
	       num_taxa, taxon_str(num_taxa),
	       num_specs, specimen_str(num_specs)) ;

	dump_show_specs_anchor(fam_ptr->name, gen_ptr->name, NULL) ;
	printf("(%s)</a>", specimen_str(num_specs)) ;

	putchar(' ') ;
	dump_do_map_anchor(fam_ptr->name, gen_ptr->name, NULL) ;
	printf("(map)</a>") ;

	printf("<br>\n") ;

	pack_taxon_array(gen_ptr->nextlevel, &spec_array, &taxa_in_genus) ;

	if (nonempty(do_sort))
	  qsort(spec_array, taxa_in_genus, sizeof(taxon *), 
		(int (*)(const void *, const void *))taxon_compare) ;

	for (k=0 ; k < taxa_in_genus ; k++) {

	  spec_ptr = spec_array[k] ;

	  printf("<spacer type=horizontal size=40>") ;

	  num_specs = ((taxon_info *)(spec_ptr->other_data))->num_specs ;
	
	  if (!tax_ptr_name_match(spec_ptr, "xxx")) {
	    if (((genus_info *)(gen_ptr->other_data))->hybrid) {
	      if (((taxon_info *)(spec_ptr->other_data))->hybrid) 
		sprintf(tax_print_str, "X <i>%s</i> X <i>%s</i>",
			gen_ptr->name, spec_ptr->name) ;
	      else sprintf(tax_print_str, "X <i>%s %s</i>",
			   gen_ptr->name, spec_ptr->name) ;

	    }
	    else {
	      if (((taxon_info *)(spec_ptr->other_data))->hybrid) 
		sprintf(tax_print_str, "<i>%s</i> X <i>%s</i>",
			gen_ptr->name, spec_ptr->name) ;
	      else sprintf(tax_print_str, "<i>%s %s</i>",
			   gen_ptr->name, spec_ptr->name) ;
	    }
	  }
	  else sprintf(tax_print_str, "No species listed") ;

	  printf("%s (%d %s) ", tax_print_str, 
		 num_specs, specimen_str(num_specs)) ;
	
	  dump_show_specs_anchor(fam_ptr->name, gen_ptr->name,
				 spec_ptr->name) ;
	  printf("(%s)</a>", specimen_str(num_specs)) ;

	  putchar(' ') ;
	  dump_do_map_anchor(fam_ptr->name, gen_ptr->name, spec_ptr->name) ;
	  printf("(map)</a>") ;
	  printf("<br>\n") ;
	}
	free(spec_array) ;
      }
      free(gen_array) ;
    }
    free(fam_array) ;
  }
  else
    printf("No specimens meet these specifications.") ;
}
Ejemplo n.º 11
0
main()
{
  char *id_str, fname[FILENAME_MAX] ;
  FILE *fp ;
  int done ;
  char *begin_str, *end_str ;
  issue_desc_t issue ;

  decode_query_string(2, "set_id", &set_id, "issue_id", &id_str) ;

  get_issue(set_id, id_str, &issue) ;

  begin_str = (char *)malloc(sizeof(char) * (strlen(id_str)+8)) ;
  sprintf(begin_str, "BEGIN %s\n", id_str) ;
  end_str = (char *)malloc(sizeof(char) * (strlen(id_str)+6)) ;
  sprintf(end_str, "END %s\n", id_str) ;

  no_cache() ;
  return_header("text/html") ;

  if (difftime(issue.expire_time, time(NULL)) > 0) {
    char s[80] ;
    int val_count ;

    /* issue is still open */

    printf("<title>Vote on issue: %s</title>\n", issue.short_desc) ;
    printf("<h1>Vote on issue: %s</h1>\n", issue.short_desc) ;

    if (issue.secret_ballot) 
      printf("<b>This vote is using a secret ballot.</b><br>") ;
    else {
      printf("<b>This vote is NOT using a secret ballot. ") ;
      printf("Your name and vote will be known to everybody!</b><br>") ;
    }

    if (issue.constant_update) {
      printf("<b>Partial voting results will be constantly updated ") ;
      printf("and visible on this page.</b><br>") ;
    }

    if (issue.can_change_vote) {
      printf("<b>You can change your vote at any time before the ") ;
      printf("counting date by voting again.</b><br>") ;
    }
    else {
      printf("<b>You cannot change your vote once it is made.</b><br>") ;
    }

    printf("<form method=post action=\"/FLORA/cgi/voting_cast\">") ;
    printf("<input type=hidden name=\"set_id\" value=\"%s\">",
	   set_id) ;
    printf("<input type=hidden name=\"issue_id\" value=\"%s\">",
	   id_str) ;
    printf("Your voting username: <input type=text name=\"name\"> ") ;
    printf("Your password: <input type=password name=\"password\"><br>") ;

    printf("<b>Description of issue:</b>") ;
    printf("<pre>") ;
    sprintf(fname, "%s/%s.descriptions", file_prefix, set_id) ;
    fp = fopen(fname, "r") ;
    do {
      fgets(s, 80, fp) ;
    } while (strcmp(s, begin_str)) ;
    while (strcmp(s, end_str)) {
      fgets(s, 80, fp) ;
      if (strcmp(s, end_str))
	dump_line_with_links(s) ;
    }
    fclose(fp) ;
    printf("</pre>") ;
    if (issue.multiple_choice > 1)
      printf("<b>Select up to %d choices:</b>", issue.multiple_choice) ;
    else 
      printf("<b>Select a choice:</b>") ;
    printf("<pre>") ;
    sprintf(fname, "%s/%s.choices", file_prefix, set_id) ;
    fp = fopen(fname, "r") ;
    do {
      fgets(s, 80, fp) ;
    } while (strcmp(s, begin_str)) ;
    val_count = 1 ;
    while (strcmp(s, end_str)) {
      fgets(s, 80, fp) ;
      if (strcmp(s, end_str)) {
	printf("<input type=%s name=choice value=%d> ", 
	       issue.multiple_choice > 1 ? "checkbox" : "radio",
	       val_count++) ;
	dump_line_with_links(s) ;
      }
    }
    fclose(fp) ;
    printf("<input type=%s name=choice value=%d> Abstain\n", 
	   issue.multiple_choice > 1 ? "checkbox" : "radio", 
	   val_count) ;
    printf("</pre>\n") ;
    printf("<input type=submit value=\"Cast your vote\">") ;
    printf("</form>") ;

    if (issue.constant_update) {
      printf("<b>Current voting results:</b>\n") ;
      dump_issue_results(issue) ;
    }
  }
  else {
    char s[80] ;

    printf("<title>Results of vote on issue: %s</title>\n", 
	   issue.short_desc) ;
    printf("<h1>Results of vote on issue: %s</h1>\n", issue.short_desc) ;

    printf("<b>Description of issue:</b>") ;
    printf("<pre>") ;
    sprintf(fname, "%s/%s.descriptions", file_prefix, set_id) ;
    fp = fopen(fname, "r") ;
    do {
      fgets(s, 80, fp) ;
    } while (strcmp(s, begin_str)) ;
    while (strcmp(s, end_str)) {
      fgets(s, 80, fp) ;
      if (strcmp(s, end_str))
	dump_line_with_links(s) ;
    }
    fclose(fp) ;
    printf("</pre>") ;
    printf("<b>Voting results:</b>\n") ;

    dump_issue_results(issue) ;

  }
}