Ejemplo n.º 1
0
int main(void)
{
	printf("初始化LinkedList及其正反两个方向的迭代器...");
	al = list_create(string, LinkedList, NULL);
	fwd = list_iterator(al, Forward);
	bwd = list_iterator(al, Reverse);
	printf("Ok!\n");
	printf("TEST1:空列表时的查询\n");
	show();
	cont();
	printf("TEST2: 用append添加一个元素\n");
	appends(1);
	show();
	cont();
	printf("TEST3: 用remove_at删除一个元素\n");
	printf("删除了%zu个元素\n", list_remove_at(al, 0));
	show();
	cont();
	printf("TEST4: 用insert从头部开始连续添加18个元素\n");
	inserts(18, 0);
	show();
	int pos = 0;
	int from = -1;
	while ((pos = list_search(al, from, Forward, "South Korea", string, 11)) != -1) {
		printf("正向搜索所有韩国: %d\n", pos);
		from = pos + 1;
	}
	from = -1;
	while ((pos = list_search(al, from, Reverse, "Brazil", string, 6)) != -1) {
		printf("反向搜索所有巴西: %d\n", pos);
		from = pos - 1;
	}
	cont();
	printf("TEST5: 用remove删除所有Brazil\n");
	list_remove(al, "Brazil", string, 6);
	show();
	cont();
	printf("TEST6: 用removeall删除所有元素\n");
	list_removeall(al);
	show();
	cont();
	printf("TEST7: 用push连续添加12个元素后进行递增快速排序\n");
	int i, j;
	for (i = 0; i < 12; i++) {
		j = rand() % 16;
		list_push(al, nations[j], string, strlen(nations[j]));
	}
	printf("排序前:\n");
	type();
	printf("排序后:\n");
	list_qsort(al, Asc);
	type();
	printf("二分搜索找韩国: %d\n", list_bi_search(al, "South Korea", string, strlen("South Korea")));
	printf("二分搜索找中国: %d\n", list_bi_search(al, "中华人民共和国", string, strlen("中华人民共和国")));
	cont();
	printf("TEST8: 用enqueue连续添加12个元素后进行递减插入排序\n");
	for (i = 0; i < 12; i++) {
		j = rand() % 16;
		list_enqueue(al, nations[j], string, strlen(nations[j]));
	}
	printf("排序前:\n");
	type();
	printf("排序后:\n");
	list_isort(al, Desc);
	type();
	printf("二分搜索找日本: %d\n", list_bi_search(al, "Japan", string, 5));
	printf("二分搜索找台湾: %d\n", list_bi_search(al, "中华民国", string, strlen("中华民国")));
	printf("反向排列所有元素\n");
	list_reverse(al);
	type();
	printf("二分搜索找韩国: %d\n", list_bi_search(al, "South Korea", string, strlen("South Korea")));
	printf("二分搜索找中国: %d\n", list_bi_search(al, "中华人民共和国", string, strlen("中华人民共和国")));
	printf("二分搜索找日本: %d\n", list_bi_search(al, "Japan", string, 5));
	printf("二分搜索找台湾: %d\n", list_bi_search(al, "中华民国", string, strlen("中华民国")));
	cont();
	printf("TEST9: 用迭代器迭代删除所有元素\n");
	Iterator delit = list_iterator(al, Forward);
	Element ele = NULL;
	while ((ele = it_next(delit))) {
		it_remove(delit);
		printf("删除元素:\"%s\"\n", POINTOF(ele, char));
		free(ele);
	}
	type();
	cont();
	printf("TEST10: 模拟堆栈\n");
	printf("连续PUSH三次:\n");
	j = rand() % 16;
	list_push(al, nations[j], string, strlen(nations[j]));
	type();
	j = rand() % 16;
	list_push(al, nations[j], string, strlen(nations[j]));
	type();
	j = rand() % 16;
	list_push(al, nations[j], string, strlen(nations[j]));
	type();
	printf("用stacktop读取栈顶元素:");
	ele = list_stacktop(al);
	printf(" \"%s\"\n", POINTOF(ele, char));
	free(ele);
	printf("用pop弹空堆栈:\n");
	ele = list_pop(al);
	printf("\"%s\"\n", POINTOF(ele, char));
	free(ele);
	ele = list_pop(al);
	printf("\"%s\"\n", POINTOF(ele, char));
	free(ele);
	ele = list_pop(al);
	printf("\"%s\"\n", POINTOF(ele, char));
	free(ele);
	type();
	cont();
	printf("TEST11: 模拟队列\n");
	printf("连续enqueue三次:\n");
	j = rand() % 16;
	list_enqueue(al, nations[j], string, strlen(nations[j]));
	type();
	j = rand() % 16;
	list_enqueue(al, nations[j], string, strlen(nations[j]));
	type();
	j = rand() % 16;
	list_enqueue(al, nations[j], string, strlen(nations[j]));
	type();
	printf("用queuehead读取栈顶元素:");
	ele = list_queuehead(al);
	printf(" \"%s\"\n", POINTOF(ele, char));
	free(ele);
	printf("用dequeue全部出队:\n");
	ele = list_dequeue(al);
	printf("\"%s\"\n", POINTOF(ele, char));
	free(ele);
	ele = list_dequeue(al);
	printf("\"%s\"\n", POINTOF(ele, char));
	free(ele);
	ele = list_dequeue(al);
	printf("\"%s\"\n", POINTOF(ele, char));
	free(ele);
	type();
	cont();
	printf("TEST12: 两个列表相加\n");
	Container list2 = list_create(string, LinkedList, NULL);
	appends(9);
	printf("原列表:\n");
	type();
	printf("加上一个空列表:\n");
	list_plus(al, list2);
	type();
	printf("加上一个有9个元素的列表:\n");
	for (i = 0; i < 9; i++) {
		j = rand() % 16;
		list_append(list2, nations[j], string, strlen(nations[j]));
	}
	list_plus(al, list2);
	type();
	cont();
	printf("再减去这个列表:\n");
	list_minus(al, list2);
	type();
	printf("再减去一个空列表:\n");
	Container empty = list_create(string, LinkedList, NULL);
	list_minus(al, empty);
	type();
	cont();
	printf("添加到18个元素后再进行retain操作,类似取交集\n");
	appends(18);
	printf("原列表:\n");
	type();
	printf("list2:\n");
	for (i = 0; i < 9; i++) {
		ele = list_get(list2, i);
		printf("%s, ", POINTOF(ele, char));
		free(ele);
	}
	printf("\n");
	list_retain(al, list2);
	printf("retain后:\n");
	type();
	printf("retain一个空列表:\n");
	list_retain(al, empty);
	type();
	printf("FIN: 销毁列表和迭代器...");
	it_destroy(fwd);
	it_destroy(bwd);
	list_destroy(al);
	list_destroy(list2);
	list_destroy(empty);
	printf("Ok!\n");
	return 0;
}
Ejemplo n.º 2
0
bool load_main_config(const char *file, bool is_active) {
	input_init();

	char *path;
	if (file != NULL) {
		path = strdup(file);
	} else {
		path = get_config_path();
	}

	struct sway_config *old_config = config;
	config = calloc(1, sizeof(struct sway_config));
	if (!config) {
		sway_abort("Unable to allocate config");
	}

	config_defaults(config);
	if (is_active) {
		sway_log(L_DEBUG, "Performing configuration file reload");
		config->reloading = true;
		config->active = true;
	}

	config->current_config = path;
	list_add(config->config_chain, path);

	config->reading = true;
	
	// Read security configs
	bool success = true;
	DIR *dir = opendir(SYSCONFDIR "/sway/security.d");
	if (!dir) {
		sway_log(L_ERROR, "%s does not exist, sway will have no security configuration"
				" and will probably be broken", SYSCONFDIR "/sway/security.d");
	} else {
		list_t *secconfigs = create_list();
		char *base = SYSCONFDIR "/sway/security.d/";
		struct dirent *ent = readdir(dir);
		struct stat s;
		while (ent != NULL) {
			char *_path = malloc(strlen(ent->d_name) + strlen(base) + 1);
			strcpy(_path, base);
			strcat(_path, ent->d_name);
			lstat(_path, &s);
			if (S_ISREG(s.st_mode)) {
				list_add(secconfigs, _path);
			}
			ent = readdir(dir);
		}
		closedir(dir);

		list_qsort(secconfigs, qstrcmp);
		for (int i = 0; i < secconfigs->length; ++i) {
			char *_path = secconfigs->items[i];
			if (stat(_path, &s) || s.st_uid != 0 || s.st_gid != 0 || (((s.st_mode & 0777) != 0644) && (s.st_mode & 0777) != 0444)) {
				sway_log(L_ERROR, "Refusing to load %s - it must be owned by root and mode 644 or 444", _path);
				success = false;
			} else {
				success = success && load_config(_path, config);
			}
		}

		free_flat_list(secconfigs);
	}

	success = success && load_config(path, config);

	if (is_active) {
		config->reloading = false;
	}

	if (old_config) {
		free_config(old_config);
	}
	config->reading = false;

	if (success) {
		update_active_bar_modifiers();
	}

	return success;
}
Ejemplo n.º 3
0
int list_sort_func(struct linked_list *list, void *cmp_func){
    list_qsort(list, cmp_func);
    return 0;
}