示例#1
0
int main()
{
	map *m;
	string *k;
	string *v;
	string *q;
	map_iterator *n;
	pair *t;

	m = map_init(stcmp);
	q = string_init_cstring("");
	k = string_init();
	string_read_line(k);

	while(string_cmp(k, q))
	{
		v = string_init();
		string_read_line(v);
		map_add(m, k, v);

		k = string_init();
		string_read_line(k);
	}

	k = string_free(k);

	/* Iterate through map. */
	for(n = map_begin(m); n; n = map_next(n))
	{
		t = n->data;
		string_print(t->first);
		printf(" => ");
		string_println(t->second);
	}

	/* Free map. */
	for(n = map_begin(m); n; n = map_next(n))
	{
		t = n->data;
		string_free(t->first);
		string_free(t->second);
	}

	string_free(q);
	m = map_free(m);

	return 0;
}
示例#2
0
int main()
{
	string *s = string_init();
	string *o;
	string *subs;
	string *q = string_init_cstring("quit");
	
	string_read_line(s);
	
	while(string_cmp(s, q))
	{
		string_print(s);
		
		string_reset(s);
		string_read_line(s);
	}

	string_append(s, q);
	o = string_cat(s, q);
	string_print(s);
	string_print(o);
	string_append_cstring(o, "hello");
	string_append_char(o, 'J');
	string_print(o);
	subs = string_substring(o, 5, 6);

	printf("--\n");
	printf("%c\n", string_get(s, 5));
	printf("%d\n", string_index(o, 'o'));
	string_print(subs);
	printf("--\n");

	o = string_free(o);
	s = string_free(s);
	q = string_free(q);
	subs = string_free(subs);

	return 0;
}
示例#3
0
文件: ui.c 项目: abulmo/edax-reversi
/**
 * @brief Get an event
 *
 * Wait for an event from the standard input.
 *
 * @param ui User Interface.
 */
static void ui_read_input(UI *ui)
{
	char *buffer;
	char cmd[8];
	Event *event = ui->event;
	Play *play = ui->play;

	buffer = string_read_line(stdin);

	if (buffer == NULL) {
		if (ferror(stdin)) {
			if (errno == EINTR) clearerr(stdin);
			else fatal_error("stdin is broken\n");
		} else if (feof(stdin)) {
			buffer = string_duplicate("eof");
		}
		event->loop = false;
	} else {
		if (ui->type == UI_GTP) gtp_preprocess(buffer);
		parse_word(buffer, cmd, 5);
		string_to_lowercase(cmd);
		if (strcmp(cmd, "stop") == 0) {
			event_clear_messages(event);
			info("<stop>\n");
			play_stop(play);
			if (ui->type == UI_GGS) play_stop(play + 1);
		} else if (ui->type == UI_NBOARD && strcmp(cmd, "ping") == 0) {
			play_stop(play);
		} else if (ui->type == UI_XBOARD && strcmp(cmd, "?") == 0) {
			play_stop(play);
		} else {
			if (strcmp(cmd, "quit") == 0 || strcmp(cmd, "q") == 0) {
				event_clear_messages(event);
				play_stop(play);
				if (ui->type == UI_GGS) play_stop(play + 1);
				event->loop = false;
			}
		}
	}
	if (buffer) {
		lock(event);
			event_add_message(event, buffer);
			condition_signal(event);
		unlock(event);
	}
}