Exemple #1
0
/* Return integer/real/string in pointer */
Value create_const_value(Lang* context, AstElement* element)
{
    STD_ASSERT(element->is_constant);

    switch (element->type)
    {
    case INTEGER:
        /* Got number */
        return (Integer)strtol64(element->output.c_str(), NULL, 10, true);

    case REAL:
        /* Got real */
        return (Real)atof(element->output.c_str());

    case STRING:
        /* Got string */
        return element->output.ptr();

    default:
        /* Impossible to here */
        STD_ASSERT(0);
        return 0;
    }
}
int edview_search_name(edview *xx, int dir, int strand, char *value)
{
    tg_rec rec, *rp, cnum = -1, best_rec;
    int best_pos, best_off;
    int nr, i;
    rangec_t *(*ifunc)(GapIO *io, contig_iterator *ci);
    int start, end, cstart;
    contig_iterator *iter;
    contig_t *c;

    /* Check for #num where num is a sequence record in this contig */
    if (*value == '#') {
	char *endp;
	int64_t v = strtol64(value+1, &endp, 10);

	rec = v;

	if (*endp == '\0' && cache_exists(xx->io, GT_Seq, rec)) {
	    sequence_get_clipped_position(xx->io, rec, &cnum,
					  &start, NULL, &cstart, NULL, NULL);
	    if (cnum == xx->cnum) {
		edSetCursorPos(xx, GT_Seq, rec, cstart - start, 1);
		return 0;
	    }
	}
    }

    /* Find all hits matching this name */
    rp = sequence_index_query_all(xx->io, value, 1, &nr);

    /* Also get an position-based iterator */
    c = cache_search(xx->io, GT_Contig, xx->cnum);
    if (dir) {
	start    = xx->cursor_apos + 1;
	end      = c->end;
	ifunc    = contig_iter_next;
	best_pos = end + 1;
	best_off = 0;
    } else {
	start    = c->start;
	end      = xx->cursor_apos - 1;
	ifunc    = contig_iter_prev;
	best_pos = start - 1;
	best_off = 0;
    }

    iter = contig_iter_new_by_type(xx->io, xx->cnum, 1,
				   dir == 1 ? CITER_FIRST : CITER_LAST,
				   start-1, end+1, GRANGE_FLAG_ISSEQ);
    if (!iter)
	return -1;

    /*
     * The iterator also finds overlapping objects, not just ones beyond this
     * point. That's fine if we're on the consensus as we probably want to
     * jump to the first seq-name overlapping this point.
     *
     * However if we're on a sequence already, we want the first one
     * after or before that sequence. So we skip along iterator until we're
     * at the current record.
     */
    if (xx->cursor_type == GT_Seq) {
	rangec_t *r;
	while ((r = ifunc(xx->io, iter))) {
	    if (r->rec == xx->cursor_rec)
		break;
	}
    }
				   

    /* Alternate between the by-name and by-position scan */
    best_rec = -1;
    for (i = 0; i < nr; i++) {
	int start, end;
	rangec_t *r;

	/* From name index */
	rec = rp[i++];
	sequence_get_clipped_position(xx->io, rec, &cnum, &start, &end,
				      &cstart, NULL, NULL);
	if (cnum == xx->cnum) {
	    if ((dir  && best_pos > cstart && cstart > xx->cursor_apos) ||
		(!dir && best_pos < cstart && cstart < xx->cursor_apos)) {
		best_pos = cstart;
		best_off = cstart - start;
		best_rec = rec;
	    }
	}

	/* From iterator */
	if ((r = ifunc(xx->io, iter))) {
	    seq_t *s;
	    if (NULL == (s = cache_search(xx->io, GT_Seq, r->rec))) {
		/* No match */
		best_rec = -1;
		break;
	    }
	    
	    if (strncmp(s->name, value, strlen(value)) == 0) {
		/* prefix match */
		puts("Found by pos iterator");
		best_rec = r->rec;
		break;
	    }
	} else {
	    /* End of contig - bail out early */
	    best_rec = -1;
	    break;
	}
    }

    contig_iter_del(iter);
    if (rp)
	free(rp);
    
    if (best_rec != -1) {
	edSetCursorPos(xx, GT_Seq, best_rec, best_off, 1);
	return 0;
    }

    return -1;
}