Ejemplo n.º 1
0
static void run() {
	solutions = alist_new();
	
	gettimeofday(&startTime, NULL);
	
	gamestate_t *gs = gamestate_new(5, coord_new(3, 2));
	search(gs, alist_new());
	
	gettimeofday(&endTime, NULL);
	
	printf("Games played:    %6ld\n", gamesPlayed);
	printf("Solutions found: %6d\n", solutions->size);
	printf("Time elapsed:    %6ldms\n", diff_usec(startTime, endTime) / 1000);
}
Ejemplo n.º 2
0
alist_t *coord_possible_moves(coord_t *c, int rowCount) {
	alist_t *moves = alist_new();
	
	// upward (needs at least 2 rows above)
	if (c->row >= 3) {
		
		// up-left
		if (c->hole >= 3) {
			alist_add(moves, move_new(
							   c,
							   coord_new(c->row - 1, c->hole - 1),
							   coord_new(c->row - 2, c->hole - 2)));
		}
		
		// up-right
		if (c->row - c->hole >= 2) {
			alist_add(moves, move_new(
							   c,
							   coord_new(c->row - 1, c->hole),
							   coord_new(c->row - 2, c->hole)));
		}
	}
	
	// leftward (needs at least 2 pegs to the left)
	if (c->hole >= 3) {
		alist_add(moves, move_new(
						   c,
						   coord_new(c->row, c->hole - 1),
						   coord_new(c->row, c->hole - 2)));
	}
	
	// rightward (needs at least 2 holes to the right)
	if (c->row - c->hole >= 2) {
		alist_add(moves, move_new(
						   c,
						   coord_new(c->row, c->hole + 1),
						   coord_new(c->row, c->hole + 2)));
	}
	
	// downward (needs at least 2 rows below)
	if (rowCount - c->row >= 2) {
		
		// down-left (always possible when there are at least 2 rows below)
		alist_add(moves, move_new(
						   c,
						   coord_new(c->row + 1, c->hole),
						   coord_new(c->row + 2, c->hole)));
		
		// down-right (always possible when there are at least 2 rows below)
		alist_add(moves, move_new(
						   c,
						   coord_new(c->row + 1, c->hole + 1),
						   coord_new(c->row + 2, c->hole + 2)));
	}
	
	return moves;
}
Ejemplo n.º 3
0
alist alist_copy(alist src)
{
	alist al;
	aentry ae;
	assert(src != NULL);
	al = alist_new();
	for (ae = src->head; ae != NULL; ae = ae->next)
		alist_append(al, ae->p);
	return al;
}
Ejemplo n.º 4
0
void particles_init(struct particles *em, int particles_max)
{
	if(particles_max > PARTICLES_MAX) {
		particles_error("Number of particles (%d) > PARTICLES_MAX (%d)\n",
				particles_max, PARTICLES_MAX);
		return;
	}

	em->particles_count = 0;
	em->particles_max = particles_max;
	em->sprites = animatedsprites_create();
	em->particles = alist_new(particles_max);

	for(int i=0; i<particles_max; i++) {
		em->mem[i].dead = 1;
	}
}
Ejemplo n.º 5
0
/*
 * Format expected is one addres per line, at the start of each line.
 */
alist_t *
load_http(char *url)
{
	int fd, len, left, port, endhdr, removed, linenum = 0;
	char *s, *t, *u, buffer[LOAD_BUFSIZE], *myurl;
	alist_t *a, *rtop, *rbot;
	size_t avail;
	int error;

	/*
	 * More than this would just be absurd.
	 */
	if (strlen(url) > MAX_URL_LEN) {
		fprintf(stderr, "load_http has a URL > %d bytes?!\n",
			MAX_URL_LEN);
		return NULL;
	}

	fd = -1;
	rtop = NULL;
	rbot = NULL;

	avail = sizeof(buffer);
	error = snprintf(buffer, avail, "GET %s HTTP/1.0\r\n", url);

	/*
	 * error is always less then avail due to the constraint on
	 * the url length above.
	 */
	avail -= error;

	myurl = strdup(url);
	if (myurl == NULL)
		goto done;

	s = myurl + 7;			/* http:// */
	t = strchr(s, '/');
	if (t == NULL) {
		fprintf(stderr, "load_http has a malformed URL '%s'\n", url);
		free(myurl);
		return NULL;
	}
	*t++ = '\0';

	/*
	 * 10 is the length of 'Host: \r\n\r\n' below.
	 */
	if (strlen(s) + strlen(buffer) + 10 > sizeof(buffer)) {
		fprintf(stderr, "load_http has a malformed URL '%s'\n", url);
		free(myurl);
		return NULL;
	}

	u = strchr(s, '@');
	if (u != NULL)
		s = u + 1;		/* AUTH */

	error = snprintf(buffer + strlen(buffer), avail, "Host: %s\r\n\r\n", s);
	if (error >= avail) {
		fprintf(stderr, "URL is too large: %s\n", url);
		goto done;
	}

	u = strchr(s, ':');
	if (u != NULL) {
		*u++ = '\0';
		port = atoi(u);
		if (port < 0 || port > 65535)
			goto done;
	} else {
		port = 80;
	}


	fd = connecttcp(s, port);
	if (fd == -1)
		goto done;


	len = strlen(buffer);
	if (write(fd, buffer, len) != len)
		goto done;

	s = buffer;
	endhdr = 0;
	left = sizeof(buffer) - 1;

	while ((len = read(fd, s, left)) > 0) {
		s[len] = '\0';
		left -= len;
		s += len;

		if (endhdr >= 0) {
			if (endhdr == 0) {
				t = strchr(buffer, ' ');
				if (t == NULL)
					continue;
				t++;
				if (*t != '2')
					break;
			}

			u = buffer;
			while ((t = strchr(u, '\r')) != NULL) {
				if (t == u) {
					if (*(t + 1) == '\n') {
						u = t + 2;
						endhdr = -1;
						break;
					} else
						t++;
				} else if (*(t + 1) == '\n') {
					endhdr++;
					u = t + 2;
				} else
					u = t + 1;
			}
			if (endhdr >= 0)
				continue;
			removed = (u - buffer) + 1;
			memmove(buffer, u, (sizeof(buffer) - left) - removed);
			s -= removed;
			left += removed;
		}

		do {
			t = strchr(buffer, '\n');
			if (t == NULL)
				break;

			linenum++;
			*t = '\0';

			/*
			 * Remove comment and continue to the next line if
			 * the comment is at the start of the line.
			 */
			u = strchr(buffer, '#');
			if (u != NULL) {
				*u = '\0';
				if (u == buffer)
					continue;
			}

			/*
			 * Trim off tailing white spaces, will include \r
			 */
			for (u = t - 1; (u >= buffer) && ISSPACE(*u); u--)
				*u = '\0';

			a = alist_new(AF_UNSPEC, buffer);
			if (a != NULL) {
				if (rbot != NULL)
					rbot->al_next = a;
				else
					rtop = a;
				rbot = a;
			} else {
				fprintf(stderr,
					"%s:%d unrecognised content:%s\n",
					url, linenum, buffer);
			}

			t++;
			removed = t - buffer;
			memmove(buffer, t, sizeof(buffer) - left - removed);
			s -= removed;
			left += removed;

		} while (1);
	}

done:
	if (myurl != NULL)
		free(myurl);
	if (fd != -1)
		close(fd);
	return rtop;
}
Ejemplo n.º 6
0
int main(void)
{
	alist al;
	char *t1 = "def", *t2 = "abc", *t3 = "xyz";
	char *s;

	al = alist_new();
	assert(alist_count(al) == 0);
	assert(alist_current(al) == NULL);
	assert(alist_current_idx(al) == -1);

	alist_append(al, t1);
	assert(alist_count(al) == 1);
	assert(alist_current(al) == t1);
	assert(alist_current_idx(al) == 0);

	alist_append(al, t2);
	assert(alist_count(al) == 2);
	assert(alist_current(al) == t2);
	assert(alist_current_idx(al) == 1);

	s = alist_first(al);
	assert(s == t1);
	assert(alist_current(al) == t1);
	assert(alist_current_idx(al) == 0);

	s = alist_next(al);
	assert(s == t2);
	assert(alist_current(al) == t2);
	assert(alist_current_idx(al) == 1);

	s = alist_next(al);
	assert(s == NULL);
	assert(alist_current(al) == NULL);
	assert(alist_current_idx(al) == -1);

	alist_prepend(al, t3);
	assert(alist_count(al) == 3);
	assert(alist_current(al) == t3);
	assert(alist_current_idx(al) == 0);

	printf("elements:\n");
	for (s = alist_first(al); s != NULL; s = alist_next(al))
		printf("element %d: %s\n", alist_current_idx(al), s);

	alist_sort(al, sorter);
	printf("sorted elements:\n");
	for (s = alist_first(al); s != NULL; s = alist_next(al))
		printf("element %d: %s\n", alist_current_idx(al), s)
;	assert(alist_at(al, 0) == t2);
	assert(alist_at(al, 1) == t1);
	assert(alist_at(al, 2) == t3);

	alist_clear(al);
	assert(alist_count(al) == 0);
	assert(alist_current(al) == NULL);
	assert(alist_current_idx(al) == -1);

	alist_insert(al, 5, t1);
	assert(alist_count(al) == 1);
	assert(alist_current(al) == t1);
	assert(alist_current_idx(al) == 0);

	alist_insert(al, 0, t2);
	assert(alist_count(al) == 2);
	assert(alist_current(al) == t2);
	assert(alist_current_idx(al) == 0);

	alist_insert(al, 1, t3);
	assert(alist_count(al) == 3);
	assert(alist_at(al, 0) == t2);
	assert(alist_at(al, 1) == t3);
	assert(alist_at(al, 2) == t1);
	assert(alist_current(al) == t3);
	assert(alist_current_idx(al) == 1);

	alist_delete(al);
	printf("alist test successful.\n");

	return 0;
}
Ejemplo n.º 7
0
/*
 * Format expected is one addres per line, at the start of each line.
 */
alist_t *
load_http(char *url)
{
	char *s, *t, *u, buffer[1044], *myurl;
	alist_t *a, *rtop, *rbot;
	struct sockaddr_in sin;
	struct hostent *host;
	size_t avail;
	int fd, len, left, port, endhdr, removed;
	int error;

	/*
	 * More than this would just be absurd.
	 */
	if (strlen(url) > 512) {
		fprintf(stderr, "load_http has a URL > 512 bytes?!\n");
		return NULL;
	}

	fd = -1;
	rtop = NULL;
	rbot = NULL;

	avail = sizeof(buffer);
	error = snprintf(buffer, avail, "GET %s HTTP/1.0\r\n", url);

	/*
	 * error is always less then avail due to the constraint on
	 * the url length above.
	 */
	avail -= error;

	myurl = strdup(url);
	if (myurl == NULL)
		goto done;

	s = myurl + 7;			/* http:// */
	t = strchr(s, '/');
	if (t == NULL) {
		fprintf(stderr, "load_http has a malformed URL '%s'\n", url);
		free(myurl);
		return NULL;
	}
	*t++ = '\0';

	u = strchr(s, '@');
	if (u != NULL)
		s = u + 1;		/* AUTH */

	error = snprintf(buffer + strlen(buffer), avail, "Host: %s\r\n\r\n", s);
	if (error >= avail) {
		fprintf(stderr, "URL is too large: %s\n", url);
		goto done;
	}

	u = strchr(s, ':');
	if (u != NULL) {
		*u++ = '\0';
		port = atoi(u);
		if (port < 0 || port > 65535)
			goto done;
	} else {
		port = 80;
	}

	memset(&sin, 0, sizeof(sin));
	sin.sin_family = AF_INET;
	sin.sin_port = htons(port);

	if (isdigit(*s)) {
		if (inet_aton(s, &sin.sin_addr) == -1) {
			goto done;
		}
	} else {
		host = gethostbyname(s);
		if (host == NULL)
			goto done;
		memcpy(&sin.sin_addr, host->h_addr_list[0],
		       sizeof(sin.sin_addr));
	}

	fd = socket(AF_INET, SOCK_STREAM, 0);
	if (fd == -1)
		goto done;

	if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) == -1)
		goto done;

	len = strlen(buffer);
	if (write(fd, buffer, len) != len)
		goto done;

	s = buffer;
	endhdr = 0;
	left = sizeof(buffer) - 1;

	while ((len = read(fd, s, left)) > 0) {
		s[len] = '\0';
		left -= len;
		s += len;

		if (endhdr >= 0) {
			if (endhdr == 0) {
				t = strchr(buffer, ' ');
				if (t == NULL)
					continue;
				t++;
				if (*t != '2')
					break;
			}

			u = buffer;
			while ((t = strchr(u, '\r')) != NULL) {
				if (t == u) {
					if (*(t + 1) == '\n') {
						u = t + 2;
						endhdr = -1;
						break;
					} else
						t++;
				} else if (*(t + 1) == '\n') {
					endhdr++;
					u = t + 2;
				} else
					u = t + 1;
			}
			if (endhdr >= 0)
				continue;
			removed = (u - buffer) + 1;
			memmove(buffer, u, (sizeof(buffer) - left) - removed);
			s -= removed;
			left += removed;
		}

		do {
			t = strchr(buffer, '\n');
			if (t == NULL)
				break;

			*t++ = '\0';
			for (u = buffer; isdigit(*u) || (*u == '.'); u++)
				;
			if (*u == '/') {
				char *slash;

				slash = u;
				u++;
				while (isdigit(*u))
					u++;
				if (!isspace(*u) && *u)
					u = slash;
			}
			*u = '\0';

			a = alist_new(4, buffer);
			if (a != NULL) {
				if (rbot != NULL)
					rbot->al_next = a;
				else
					rtop = a;
				rbot = a;
			}

			removed = t - buffer;
			memmove(buffer, t, sizeof(buffer) - left - removed);
			s -= removed;
			left += removed;

		} while (1);
	}

done:
	if (myurl != NULL)
		free(myurl);
	if (fd != -1)
		close(fd);
	return rtop;
}
Ejemplo n.º 8
0
alist_t *
load_file(char *filename)
{
	alist_t *a, *rtop, *rbot;
	char *s, line[1024], *t;
	int linenum, not;
	FILE *fp;

	fp = fopen(filename + 7, "r");
	if (fp == NULL) {
		fprintf(stderr, "load_file cannot open '%s'\n", filename);
		return NULL;
	}       

	a = NULL;
	rtop = NULL;
	rbot = NULL;
	linenum = 0;    
		
	while (fgets(line, sizeof(line) - 1, fp)) {
		line[sizeof(line) - 1] = '\0';
		linenum++;
		/*
		 * Hunt for CR/LF.  If no LF, stop processing.
		 */
		s = strchr(line, '\n');
		if (s == NULL) {
			fprintf(stderr, "%d:%s: line too long\n", linenum, filename);
			fclose(fp);
			alist_free(rtop);
			return NULL;
		}

		*s = '\0';
		s = strchr(line, '\r');
		if (s != NULL)
			*s = '\0';
		for (t = line; isspace(*t); t++)
			;
		if (*t == '!') {
			not = 1;
			t++;
		} else
			not = 0;

		/*
		 * Remove comment markers
		 */
		for (s = t; *s; s++) {
			if (*s == '#')
				*s = '\0';
		}
		if (!*t)
			continue;
		/*
		 * Trim off tailing white spaces
		 */
		s = strlen(t) + t - 1;
		while (isspace(*s))
			*s-- = '\0';

		if (isdigit(*t)) {
			a = alist_new(4, t);
			a->al_not = not;
			if (rbot != NULL)
				rbot->al_next = a;
			else
				rtop = a;
			rbot = a;
		} else {
			fprintf(stderr, "%s: unrecognised content line %d\n",
				filename, linenum);
		}
	}
	fclose(fp);

	return rtop;
}
Ejemplo n.º 9
0
stack_t* stack_new(size_t data_size) {
  return alist_new(data_size);
}