Пример #1
0
static int fs_cd_file(const char *filename)
{
    struct directory d;

    if (is_cwd_zip()) {
        return fs_cd_file_zip(sf_list_tail(&fs.directories), filename);
    }

    if (strcmp(filename, "..") == 0) {
        if (sf_list_cnt(&fs.directories)) {
            sf_list_pop(&fs.directories);
        }
        return chdir("..");
    } else {
        if (directory_init(&d, filename) != SF_OK) {
            return SF_ERR;
        }
        sf_list_push(&fs.directories, &d);
        if (!d.iszip) {
            return chdir(filename);
        }
    }

    return SF_OK;
}
Пример #2
0
int fs_cd(const char *pathname)
{
    if (pathname[0] == seperator || pathname[0] == '/') {
    /* absolute path */
        struct directory d;

        sf_list_clear(&fs.directories);
        directory_init(&d, "/");
        d.name[0] = '\0';
        sf_list_push(&fs.directories, &d);
        chdir("/");
        if (pathname[1] != '\0') {
            fs_cd(pathname + 1);
        }
#ifdef __WIN32__
    } else if (pathname[1] == ':') {
        char buf[3];

        snprintf(buf, 3, "%c:", pathname[0]);
        sf_list_clear(&fs.directories);
        chdir(buf);
        if (pathname[2] != '\0') {
            fs_cd(pathname + 2);
        } else {
            fs_cd("/");
        }
#endif
    } else {
    /* relative path */
        char buf[PATH_MAX];
        char *ptr;
        char delim[2];

        strncpy(buf, pathname, PATH_MAX);

        delim[0] = seperator;
        delim[1] = '\0';

        ptr = strtok(buf, delim);
        while (ptr) {
            if (strcmp(ptr, ".") != 0) {
                if (fs_cd_file(ptr) != SF_OK) {
                    break;
                }
            }
            ptr = strtok(NULL, delim);
        }
    }
    return SF_OK;
}
Пример #3
0
static int fs_cd_file_zip(struct directory *parent, const char *filename)
{
    int isdirexist = 0;
    char buf[NAME_MAX];
    int len;
    zip_int64_t nentries, i;
    struct directory dir;

    if (strcmp(filename, "..") == 0) {
        sf_list_pop(&fs.directories);
        return SF_OK;
    }

    get_zip_cwd(buf, NAME_MAX);
    strcat(buf, filename);
    len = strlen(buf);
    buf[len++] = '/';
    buf[len] = '\0';

    nentries = zip_get_num_entries(parent->zip, 0);
    for (i = 0; i < nentries; ++i) {
        if (strncmp(zip_get_name(parent->zip, i, 0), buf, len) == 0) {
            isdirexist = 1;
            break;
        }
    }

    if (isdirexist) {
        strncpy(dir.name, filename, NAME_MAX);
        dir.iszip = 1;
        dir.isopened = 1;
        dir.zip = parent->zip;
        dir.nopens = parent->nopens + 1;
        sf_list_push(&fs.directories, &dir);
        return SF_OK;
    } else {
        sf_log(SF_LOG_ERR, "not a directory: %s", filename);
        return SF_ERR;
    }
}
Пример #4
0
void ui_toolbox_add_button(struct ui_toolbox *tb, struct ui *ui) {
    sf_list_push(&tb->buttons, &ui);
    ui_add_child((struct ui *) tb, ui, 0, 0);
    ui_toolbox_update_buttons(tb);
}
Пример #5
0
int main()
{
	char* buffer = sf_malloc(1024);
	puts("Hello.");
	sf_free(buffer);

	sf_list_ptr my_list = sf_list_alloc();

	// Test single node list

	sf_list_node_ptr node1 = sf_list_node_alloc(NULL);
	sf_list_push(my_list, node1);
	assert(my_list->first == node1);
	assert(my_list->last == node1);
	assert(node1->next == NULL);
	assert(node1->prev == NULL);

	sf_list_node_ptr popped_node = sf_list_pop(my_list);
	assert(popped_node == node1);
	assert(node1->next == NULL);
	assert(node1->prev == NULL);
	assert(my_list->first == NULL);
	assert(my_list->last == NULL);

	// Test two node list: push 2 nodes

	sf_list_node_ptr node2 = sf_list_node_alloc(NULL);
	sf_list_push(my_list, node1);
	sf_list_push(my_list, node2);
	assert(my_list->first == node1);
	assert(my_list->last == node2);
	assert(node1->prev == NULL);
	assert(node1->next == node2);
	assert(node2->prev == node1);
	assert(node2->next == NULL);

	// Pop node 2

	popped_node = sf_list_pop(my_list);
	assert(popped_node == node2);
	assert(node2->next == NULL);
	assert(node2->prev == NULL);
	assert(node1->next == NULL);
	assert(node1->prev == NULL);
	assert(my_list->first == node1);
	assert(my_list->last == node1);

	// Pop node 1

	popped_node = sf_list_pop(my_list);
	assert(popped_node == node1);
	assert(node1->next == NULL);
	assert(node1->prev == NULL);
	assert(my_list->first == NULL);
	assert(my_list->last == NULL);

	// Test three node list: push 3 nodes

	sf_list_node_ptr node3 = sf_list_node_alloc(NULL);
	sf_list_push(my_list, node1);
	sf_list_push(my_list, node2);
	sf_list_push(my_list, node3);
	assert(my_list->first == node1);
	assert(my_list->last == node3);
	assert(node1->prev == NULL);
	assert(node1->next == node2);
	assert(node2->prev == node1);
	assert(node2->next == node3);
	assert(node3->prev == node2);
	assert(node3->next == NULL);

	// Pop node 3

	popped_node = sf_list_pop(my_list);
	assert(popped_node == node3);
	assert(node3->next == NULL);
	assert(node3->prev == NULL);
	assert(node2->next == NULL);
	assert(node2->prev == node1);
	assert(node1->next == node2);
	assert(node1->prev == NULL);
	assert(my_list->first == node1);
	assert(my_list->last == node2);

	// Pop node 2

	popped_node = sf_list_pop(my_list);
	assert(popped_node == node2);
	assert(node2->next == NULL);
	assert(node2->prev == NULL);
	assert(node1->next == NULL);
	assert(node1->prev == NULL);
	assert(my_list->first == node1);
	assert(my_list->last == node1);

	// Pop node 1

	popped_node = sf_list_pop(my_list);
	assert(popped_node == node1);
	assert(node1->next == NULL);
	assert(node1->prev == NULL);
	assert(my_list->first == NULL);
	assert(my_list->last == NULL);

	// Test insert_after

	sf_list_insert_after(my_list, node1, NULL);
	assert(my_list->first == node1);
	assert(my_list->last == node1);
	assert(node1->next == NULL);
	assert(node1->prev == NULL);

	sf_list_insert_after(my_list, node2, node1);
	assert(my_list->first == node1);
	assert(my_list->last == node2);
	assert(node1->prev == NULL);
	assert(node1->next == node2);
	assert(node2->prev == node1);
	assert(node2->next == NULL);

	sf_list_insert_after(my_list, node3, node2);
	assert(my_list->first == node1);
	assert(my_list->last == node3);
	assert(node1->prev == NULL);
	assert(node1->next == node2);
	assert(node2->prev == node1);
	assert(node2->next == node3);
	assert(node3->prev == node2);
	assert(node3->next == NULL);

	sf_list_pop(my_list);
	sf_list_insert_after(my_list, node3, node1); // makes: 1, 3, 2
	assert(my_list->first == node1);
	assert(my_list->last == node2);
	assert(node1->prev == NULL);
	assert(node1->next == node3);
	assert(node3->prev == node1);
	assert(node3->next == node2);
	assert(node2->prev == node3);
	assert(node2->next == NULL);

	sf_list_pop(my_list);
	sf_list_pop(my_list);
	sf_list_pop(my_list);

	// Test remove

	sf_list_push(my_list, node1);
	sf_list_remove(my_list, node1);
	assert(my_list->first == NULL);
	assert(my_list->last == NULL);

	sf_list_push(my_list, node1);
	sf_list_push(my_list, node2);
	sf_list_remove(my_list, node2);
	assert(my_list->first == node1);
	assert(my_list->last == node1);
	assert(node1->next == NULL);
	assert(node1->prev == NULL);
	sf_list_pop(my_list);

	sf_list_push(my_list, node1);
	sf_list_push(my_list, node2);
	sf_list_remove(my_list, node1);
	assert(my_list->first == node2);
	assert(my_list->last == node2);
	assert(node2->next == NULL);
	assert(node2->prev == NULL);
	sf_list_pop(my_list);

	sf_list_push(my_list, node1);
	sf_list_push(my_list, node2);
	sf_list_push(my_list, node3);
	sf_list_remove(my_list, node1);
	assert(my_list->first == node2);
	assert(my_list->last == node3);
	assert(node2->next == node3);
	assert(node2->prev == NULL);
	assert(node3->next == NULL);
	assert(node3->prev == node2);
	sf_list_pop(my_list);
	sf_list_pop(my_list);

	sf_list_push(my_list, node1);
	sf_list_push(my_list, node2);
	sf_list_push(my_list, node3);
	sf_list_remove(my_list, node2);
	assert(my_list->first == node1);
	assert(my_list->last == node3);
	assert(node1->next == node3);
	assert(node1->prev == NULL);
	assert(node3->next == NULL);
	assert(node3->prev == node1);
	sf_list_pop(my_list);
	sf_list_pop(my_list);

	sf_list_push(my_list, node1);
	sf_list_push(my_list, node2);
	sf_list_push(my_list, node3);
	sf_list_remove(my_list, node3);
	assert(my_list->first == node1);
	assert(my_list->last == node2);
	assert(node1->next == node2);
	assert(node1->prev == NULL);
	assert(node2->next == NULL);
	assert(node2->prev == node1);
	sf_list_pop(my_list);
	sf_list_pop(my_list);

	// Clean up

	sf_list_node_free(node1);
	sf_list_node_free(node2);
	sf_list_node_free(node3);
	sf_list_free(my_list);

	sf_malloc_check();

	return 0;
}