Пример #1
0
/*
 * strace backward or forward..this was cut and
 * pasted from strace.c and special cased to handle
 * just a single trace.  this might be relaxed later.
 */
static void edge_search_2(int direction, int is_last_iteration)
{
struct strace s_tmp;
struct strace *s_head, *s;
TimeType basetime, maxbase, sttim, fintim;
Trptr t = find_first_highlighted_trace();
int totaltraces;
#ifdef DEBUG_PRINTF
int passcount;
#endif
int whichpass;
TimeType middle=0, width;

if(!t) return;
memset(s_head = &s_tmp, 0, sizeof(struct strace));
s_head->trace = t;
s_head->value = ST_ANY;
s = s_head;

while(t)
	{
	t = find_next_highlighted_trace(t);
	if(t)
		{
		s->next = wave_alloca(sizeof(struct strace));
		memset(s = s->next, 0, sizeof(struct strace));
		s->trace = t;
		s->value = ST_ANY;
		}
	}

if(direction==STRACE_BACKWARD) /* backwards */
{
if(GLOBALS->tims.marker<0)
	{
	basetime=MAX_HISTENT_TIME;
	}
	else
	{
	basetime=GLOBALS->tims.marker;
	}
}
else /* go forwards */
{
if(GLOBALS->tims.marker<0)
	{
	basetime=GLOBALS->tims.first;
	}
	else
	{
	basetime=GLOBALS->tims.marker;
	}
}

sttim=GLOBALS->tims.first;
fintim=GLOBALS->tims.last;

for(whichpass=0;;whichpass++)
{

if(direction==STRACE_BACKWARD) /* backwards */
{
maxbase=-1;
s=s_head;
while(s)
	{
	t=s->trace;
	GLOBALS->shift_timebase=t->shift;
	if(!(t->vector))
		{
		hptr h;
		hptr *hp;
		UTimeType utt;
		TimeType  tt;

		/* h= */ bsearch_node(t->n.nd, basetime - t->shift); /* scan-build */
		hp=GLOBALS->max_compare_index;
		if((hp==&(t->n.nd->harray[1]))||(hp==&(t->n.nd->harray[0]))) return;
		if(basetime == ((*hp)->time+GLOBALS->shift_timebase)) hp--;
		h=*hp;
		s->his.h=h;
		utt=strace_adjust(h->time,GLOBALS->shift_timebase); tt=utt;
		if(tt > maxbase) maxbase=tt;
		}
		else
		{
		vptr v;
		vptr *vp;
		UTimeType utt;
		TimeType  tt;

		/* v= */ bsearch_vector(t->n.vec, basetime - t->shift); /* scan-build */
		vp=GLOBALS->vmax_compare_index;
		if((vp==&(t->n.vec->vectors[1]))||(vp==&(t->n.vec->vectors[0]))) return;
		if(basetime == ((*vp)->time+GLOBALS->shift_timebase)) vp--;
		v=*vp;
		s->his.v=v;
		utt=strace_adjust(v->time,GLOBALS->shift_timebase); tt=utt;
		if(tt > maxbase) maxbase=tt;
		}

	s=s->next;
	}
}
else /* go forward */
{
maxbase=MAX_HISTENT_TIME;
s=s_head;
while(s)
	{
	t=s->trace;
	GLOBALS->shift_timebase=t->shift;
	if(!(t->vector))
		{
		hptr h;
		UTimeType utt;
		TimeType  tt;

		h=bsearch_node(t->n.nd, basetime - t->shift);
		while(h->next && h->time==h->next->time) h=h->next;
		if((whichpass)||(GLOBALS->tims.marker>=0)) h=h->next;
		if(!h) return;
		s->his.h=h;
		utt=strace_adjust(h->time,GLOBALS->shift_timebase); tt=utt;
		if(tt < maxbase) maxbase=tt;
		}
		else
		{
		vptr v;
		UTimeType utt;
		TimeType  tt;

		v=bsearch_vector(t->n.vec, basetime - t->shift);
		while(v->next && v->time==v->next->time) v=v->next;
		if((whichpass)||(GLOBALS->tims.marker>=0)) v=v->next;
		if(!v) return;
		s->his.v=v;
		utt=strace_adjust(v->time,GLOBALS->shift_timebase); tt=utt;
		if(tt < maxbase) maxbase=tt;
		}

	s=s->next;
	}
}

s=s_head;
totaltraces=0;	/* increment when not don't care */
while(s)
	{
/* commented out, maybe will have possible future expansion later,
 * this was cut and pasted from strace.c */
#if 0
	char str[2];
#endif
	t=s->trace;
	s->search_result=0;	/* explicitly must set this */
	GLOBALS->shift_timebase=t->shift;

	if((!t->vector)&&(!(t->n.nd->extvals)))
		{
		if(strace_adjust(s->his.h->time,GLOBALS->shift_timebase)!=maxbase)
			{
			s->his.h=bsearch_node(t->n.nd, maxbase - t->shift);
			while(s->his.h->next && s->his.h->time==s->his.h->next->time) s->his.h=s->his.h->next;
			}
/* commented out, maybe will have possible future expansion later,
 * this was cut and pasted from strace.c */
#if 0
		if(t->flags&TR_INVERT)
                	{
                        str[0]=AN_STR_INV[s->his.h->v.h_val];
                        }
                        else
                        {
                        str[0]=AN_STR[s->his.h->v.h_val];
                        }
		str[1]=0x00;
#endif

		switch(s->value)
			{
			case ST_ANY:
				totaltraces++;
				s->search_result=1;
				break;

/* commented out, maybe will have possible future expansion later,
 * this was cut and pasted from strace.c */
#if 0
			case ST_DC:
				break;

			case ST_HIGH:
				totaltraces++;
				if((str[0]=='1')||(str[0]=='H')) s->search_result=1;
				break;

			case ST_RISE:
				if((str[0]=='1')||(str[0]=='H')) s->search_result=1;
				totaltraces++;
				break;

			case ST_LOW:
				totaltraces++;
				if((str[0]=='0')||(str[0]=='L')) s->search_result=1;
				break;

			case ST_FALL:
				totaltraces++;
				if((str[0]=='0')||(str[0]=='L')) s->search_result=1;
				break;

			case ST_MID:
				totaltraces++;
				if(str[0]=='Z')
 					s->search_result=1;
				break;

			case ST_X:
				totaltraces++;
				if(str[0]=='X') s->search_result=1;
				break;

			case ST_STRING:
				totaltraces++;
				if(s->string)
				if(strstr_i(s->string,str)) s->search_result=1;
				break;
#endif

			default:
				fprintf(stderr, "Internal error: st_type of %d\n",s->value);
				exit(255);
			}


		}
		else
		{
		char *chval, *chval2;
		char ch;

		if(t->vector)
			{
			if(strace_adjust(s->his.v->time,GLOBALS->shift_timebase)!=maxbase)
				{
				s->his.v=bsearch_vector(t->n.vec, maxbase - t->shift);
				while(s->his.v->next && s->his.v->time==s->his.v->next->time) s->his.v=s->his.v->next;
				}
			chval=convert_ascii(t,s->his.v);
			}
			else
			{
			if(strace_adjust(s->his.h->time,GLOBALS->shift_timebase)!=maxbase)
				{
				s->his.h=bsearch_node(t->n.nd, maxbase - t->shift);
				while(s->his.h->next && s->his.h->time==s->his.h->next->time) s->his.h=s->his.h->next;
				}
			if(s->his.h->flags&HIST_REAL)
				{
				if(!(s->his.h->flags&HIST_STRING))
					{
#ifdef WAVE_HAS_H_DOUBLE
					chval=convert_ascii_real(t, &s->his.h->v.h_double);
#else
					chval=convert_ascii_real(t, (double *)s->his.h->v.h_vector);
#endif
					}
					else
					{
					chval=convert_ascii_string((char *)s->his.h->v.h_vector);
					chval2=chval;
					while((ch=*chval2))	/* toupper() the string */
						{
						if((ch>='a')&&(ch<='z')) { *chval2= ch-('a'-'A'); }
						chval2++;
						}
					}
				}
				else
				{
				chval=convert_ascii_vec(t,s->his.h->v.h_vector);
				}
			}

		switch(s->value)
			{
			case ST_ANY:
				totaltraces++;
				s->search_result=1;
				break;

/* commented out, maybe will have possible future expansion later,
 * this was cut and pasted from strace.c */
#if 0
			case ST_DC:
				break;

			case ST_RISE:
			case ST_FALL:
				totaltraces++;
				break;

			case ST_HIGH:
				totaltraces++;
				if((chval2=chval))
				while((ch=*(chval2++)))
					{
					if(((ch>='1')&&(ch<='9'))||(ch=='h')||(ch=='H')||((ch>='A')&&(ch<='F')))
						{
						s->search_result=1;
						break;
						}
					}
				break;

			case ST_LOW:
				totaltraces++;
				if((chval2=chval))
				{
				s->search_result=1;
				while((ch=*(chval2++)))
					{
					if((ch!='0')&&(ch!='l')&&(ch!='L'))
						{
						s->search_result=0;
						break;
						}
					}
				}
				break;

			case ST_MID:
				totaltraces++;
				if((chval2=chval))
				{
				s->search_result=1;
				while((ch=*(chval2++)))
					{
					if((ch!='z')&&(ch!='Z'))
						{
						s->search_result=0;
						break;
						}
					}
				}
				break;

			case ST_X:
				totaltraces++;
				if((chval2=chval))
				{
				s->search_result=1;
				while((ch=*(chval2++)))
					{
					if((ch!='X')&&(ch!='W')&&(ch!='x')&&(ch!='w'))
						{
						s->search_result=0;
						break;
						}
					}
				}
				break;

			case ST_STRING:
				totaltraces++;
				if(s->string)
				if(strstr_i(chval, s->string)) s->search_result=1;
				break;
#endif

			default:
				fprintf(stderr, "Internal error: st_type of %d\n",s->value);
				exit(255);
			}

		free_2(chval);
		}
	s=s->next;
	}

if((maxbase<sttim)||(maxbase>fintim)) return;

#ifdef DEBUG_PRINTF
DEBUG(printf("Maxbase: "TTFormat", total traces: %d\n",maxbase, totaltraces));
s=s_head;
passcount=0;
while(s)
	{
	DEBUG(printf("\tPass: %d, Name: %s\n",s->search_result, s->trace->name));
	if(s->search_result) passcount++;
	s=s->next;
	}
#endif

if(totaltraces)
	{
	break;
	}

basetime=maxbase;
}


GLOBALS->tims.marker=maxbase;
if(is_last_iteration)
	{
	update_markertime(GLOBALS->tims.marker);

	width=(TimeType)(((gdouble)GLOBALS->wavewidth)*GLOBALS->nspx);
	if((GLOBALS->tims.marker<GLOBALS->tims.start)||(GLOBALS->tims.marker>=GLOBALS->tims.start+width))
		{
		if((GLOBALS->tims.marker<0)||(GLOBALS->tims.marker<GLOBALS->tims.first)||(GLOBALS->tims.marker>GLOBALS->tims.last))
		                {
		                if(GLOBALS->tims.end>GLOBALS->tims.last) GLOBALS->tims.end=GLOBALS->tims.last;
		                middle=(GLOBALS->tims.start/2)+(GLOBALS->tims.end/2);
		                if((GLOBALS->tims.start&1)&&(GLOBALS->tims.end&1)) middle++;
		                }
		                else
		                {
		                middle=GLOBALS->tims.marker;
		                }

		GLOBALS->tims.start=time_trunc(middle-(width/2));
		if(GLOBALS->tims.start+width>GLOBALS->tims.last) GLOBALS->tims.start=GLOBALS->tims.last-width;
		if(GLOBALS->tims.start<GLOBALS->tims.first) GLOBALS->tims.start=GLOBALS->tims.first;
		GTK_ADJUSTMENT(GLOBALS->wave_hslider)->value=GLOBALS->tims.timecache=GLOBALS->tims.start;
		}

	MaxSignalLength();
	signalarea_configure_event(GLOBALS->signalarea, NULL);
	wavearea_configure_event(GLOBALS->wavearea, NULL);
	}
}
int
main(int argc, char ** argv)
{
	int i, rc, max;
	int numbers[TEST_NUMBERS], number, *num_p;
	unsigned int pos;
	vector_type * test_vector;
	vector_type * test_vector2;

	srand(time(NULL));

	for (i = 0; i < TEST_NUMBERS; i++) {
		numbers[i] = rand() % (TEST_NUMBERS * 10) + 1;
	}

	fprintf(stdout, "[create]\n");
	rc = vector_create(&test_vector, 0, sizeof(*numbers));
	if(rc) {
		fprintf(stderr, "could not create the vector.. %s\n",
				strerror(rc));
		return rc;
	}
	test_vector->compare = intcompare;

	fprintf(stdout, "[add]\n");
	for (i = 0; i < TEST_INT_FIRST; i++) {
		rc = vector_add_value(test_vector, &numbers[i]);
		if(rc) {
			fprintf(stderr, "could not append to the vector.. %s\n",
					strerror(rc));
			goto err_free_vector;
		}
	}

	fprintf(stdout, "[values]\n");
	for (i = 0; i < TEST_INT_FIRST; i++) {
		rc = vector_copy_value(test_vector, i, &number);
		if(rc) {
			fprintf(stderr, "vector_copy_value failed.. %s\n",
					strerror(rc));
			goto err_free_vector;
		}

		if(number != numbers[i]) {
			fprintf(stderr, "vector_get supplied a wrong value\n");
			goto err_free_vector;
		}
	}

	fprintf(stdout, "[insert get]\n");
	for(i = 1; i < (TEST_INT_FIRST + 1); i++) {
		rc = vector_insert(test_vector, i + (1 * (i - 1)), &i);
		if(rc) {
			fprintf(stderr, "vector_insert failed.. %s\n",
					strerror(rc));
			goto err_free_vector;
		}

		num_p = (int *) vector_get_value(test_vector,
				i + (1 * (i - 1)));
		if(!num_p || *num_p != i) {
			fprintf(stderr, "vector_insert failed, wrong value\n");
			goto err_free_vector;
		}
	}

	fprintf(stdout, "[del]\n");
	for(i = 0; i < (TEST_INT_FIRST * 2); i++) {
		rc = vector_del_value(test_vector, 0);
		if(rc) {
			fprintf(stderr, "vector_del_value failed.. %s\n",
					strerror(rc));
			goto err_free_vector;
		}
	}

	if(test_vector->elements != 0) {
		fprintf(stderr, "number of elements doesn't count up\n");
		goto err_free_vector;
	}

	fprintf(stdout, "[add]\n");
	for (i = 0; i < TEST_INT_FIRST; i++) {
		rc = vector_add_value(test_vector, &i);
		if(rc) {
			fprintf(stderr, "could not append to the vector.. %s\n",
					strerror(rc));
			goto err_free_vector;
		}
	}

	fprintf(stdout, "[is_sorted]\n");
	rc = vector_is_sorted(test_vector);
	if(!rc) {
		fprintf(stderr, "is_sorted failed\n");
		goto err_free_vector;
	}

	fprintf(stdout, "[containes sorted]\n");
	for (i = 0; i < TEST_INT_FIRST; i++) {
		num_p = (int *) bsearch_vector(test_vector, &i, NULL);
		if(!num_p || *num_p != i) {
			fprintf(stderr, "contain_sorted failed\n");
			goto err_free_vector;
		}
	}

	fprintf(stdout, "[create]\n");
	rc = vector_create(&test_vector2, 0, sizeof(*numbers));
	if(rc) {
		fprintf(stderr, "could not create a vector.. %s\n",
				strerror(rc));
		goto err_free_vector;
	}
	test_vector2->compare = intcompare;

	fprintf(stdout, "[add]\n");
	for (i = 0, max = 0; i < TEST_INT_FIRST; i++) {
		do {
			number = rand() % TEST_INT_FIRST;
		} while (number < max);
		max = number;

		rc = vector_add_value(test_vector2, &number);
		if(rc) {
			fprintf(stderr, "could not append to the vector.. %s\n",
					strerror(rc));
			goto err_free_vector2;
		}
	}

	fprintf(stdout, "[merge]\n");
	rc = merge_sorted_vector(test_vector, test_vector2);
	if(rc) {
		fprintf(stderr, "couldn't merge the two vectors.. %s\n",
				strerror(rc));
		goto err_free_vector2;
	}

	if(!vector_is_sorted(test_vector)) {
		fprintf(stderr, "merge failed\n");
		goto err_free_vector2;
	}

	for (i = 0; i < TEST_INT_FIRST; i++) {
		num_p = bsearch_vector(test_vector, &i, &pos);
		if(!num_p) {
			fprintf(stderr, "merge failed\n");
			goto err_free_vector2;
		}

		vector_del_value(test_vector, pos);
	}

	for (i = 0; i < TEST_INT_FIRST; i++) {
		number = *((int *) vector_get_value(test_vector2, i));
		num_p = bsearch_vector(test_vector, &number, &pos);
		if(!num_p) {
			fprintf(stderr, "merge failed\n");
			goto err_free_vector2;
		}

		vector_del_value(test_vector, pos);
	}

	if(test_vector->elements != 0) {
		fprintf(stderr, "number of elements doesn't count up\n");
		goto err_free_vector2;
	}

	fprintf(stdout, "[insert_sorted]\n");
	for (i = 0; i < TEST_INT_FIRST; i++) {
		number = rand() % (TEST_INT_FIRST * 10);

		rc = vector_insert_sorted(test_vector, &number, 1);
		if(rc) {
			fprintf(stderr, "could not insert into to vector.."
					" %s\n", strerror(rc));
			goto err_free_vector2;
		}

		rc = vector_is_sorted(test_vector);
		if(!rc) {
			fprintf(stderr, "insert ended up in a unsorted vector"
					"\n");
			__output(test_vector);
			goto err_free_vector2;
		}
	}

	fprintf(stdout, "[is_sorted]\n");
	number = *((int *) vector_get_value(test_vector, TEST_INT_FIRST - 1));
	number++;
	rc = vector_insert(test_vector, 0, &number);
	if(rc) {
		fprintf(stderr, "could not insert into to vector.."
				" %s\n", strerror(rc));
		goto err_free_vector2;
	}

	rc = vector_is_sorted(test_vector);
	if(rc) {
		fprintf(stderr, "is_sorted failed"
				"\n");
		__output(test_vector);
		goto err_free_vector2;
	}

	fprintf(stdout, "[del]\n");
	max = test_vector->elements;
	for(i = 0; i < max; i++) {
		rc = vector_del_value(test_vector, 0);
		if(rc) {
			fprintf(stderr, "vector_del_value failed.. %s\n",
					strerror(rc));
			goto err_free_vector;
		}
	}

	if(test_vector->elements != 0) {
		fprintf(stderr, "number of elements doesn't count up\n");
		goto err_free_vector;
	}

	fprintf(stdout, "[add]\n");
	for (i = 0; i < TEST_INT_FIRST; i++) {
		rc = vector_add_value(test_vector, &i);
		if(rc) {
			fprintf(stderr, "could not append to the vector.. %s\n",
					strerror(rc));
			goto err_free_vector;
		}
	}

	for (i = 0; i < (TEST_INT_FIRST / 2); i++) {
		pos = test_vector->elements / 2;
		max = 10;
		rc = vector_insert(test_vector, pos, &max);
		if(rc) {
			fprintf(stderr, "could not append to the vector.. %s\n",
					strerror(rc));
			goto err_free_vector;
		}
	}

	vector_destroy(test_vector2);
	rc = vector_create(&test_vector2, 0, sizeof(number));
	if(rc) {
		fprintf(stdout, "could not create a vector.. %s\n",
				strerror(rc));
		goto err_free_vector;
	}
	test_vector2->compare = intcompare;

	rc = vector_massmove(test_vector, 5, 9, test_vector->elements - 1, NULL);
	if(rc) {
		fprintf(stdout, "massmove failed.. %s\n", strerror(rc));
		goto err_free_vector2;
	}
	__output(test_vector);

	fprintf(stdout, "[insert_sorted]\n");
	vector_destroy(test_vector2);
	rc = vector_create(&test_vector2, TEST_NUMBERS * 100, sizeof(number));
	if(rc) {
		fprintf(stdout, "could not create a vector.. %s\n",
				strerror(rc));
		goto err_free_vector;
	}
	test_vector2->compare = intcompare;

	for(i = 0; i < TEST_NUMBERS * 100; i++) {
		number = rand() % 1000;

		rc = vector_insert_sorted(test_vector2, &number, 1);
		if(rc) {
			fprintf(stderr, "could not insert into to vector.."
					" %s\n", strerror(rc));
			goto err_free_vector2;
		}

		rc = vector_is_sorted(test_vector2);
		if(!rc) {
			fprintf(stderr, "insert ended up in a unsorted vector"
					"\n");
			__output(test_vector2);
			goto err_free_vector2;
		}
	}

	vector_destroy(test_vector);
	vector_destroy(test_vector2);

	return 0;
err_free_vector2:
	vector_destroy(test_vector2);
err_free_vector:
	vector_destroy(test_vector);
	return (rc ? rc : -1);
}