diff --git a/demo/ecma119_tree.c b/demo/ecma119_tree.c index 5d1af58..b787321 100644 --- a/demo/ecma119_tree.c +++ b/demo/ecma119_tree.c @@ -95,17 +95,17 @@ int main(int argc, char **argv) ecma119->iso_level = 1; /* create low level tree */ - result = ecma119_tree_create(ecma119, (IsoNode*)iso_image_get_root(image), &tree); + result = ecma119_tree_create(ecma119, (IsoNode*)iso_image_get_root(image)); if (result < 0) { printf ("Error creating ecma-119 tree: %d\n", result); return 1; } printf("================= ECMA-119 TREE =================\n"); - print_dir(tree, 0); + print_dir(ecma119->root, 0); printf("\n\n"); - ecma119_node_free(tree); + ecma119_node_free(ecma119->root); iso_image_unref(image); return 0; } diff --git a/src/ecma119_tree.c b/src/ecma119_tree.c index c10a1ed..26fbdf1 100644 --- a/src/ecma119_tree.c +++ b/src/ecma119_tree.c @@ -238,18 +238,49 @@ int create_tree(Ecma119Image *image, IsoNode *iso, Ecma119Node **tree, return ISO_SUCCESS; } -int ecma119_tree_create(Ecma119Image *img, IsoNode *iso, Ecma119Node **tree) +/** + * Compare the iso name of two ECMA-119 nodes + */ +static +int cmp_node_name(const void *f1, const void *f2) +{ + Ecma119Node *f = *((Ecma119Node**)f1); + Ecma119Node *g = *((Ecma119Node**)f2); + return strcmp(f->iso_name, g->iso_name); +} + +/** + * Sorts a the children of each directory in the ECMA-119 tree represented + * by \p root, acording to the order specified in ECMA-119, section 9.3. + */ +static +void sort_tree(Ecma119Node *root) +{ + size_t i; + + qsort(root->info.dir.children, root->info.dir.nchildren, + sizeof(void*), cmp_node_name); + for (i = 0; i < root->info.dir.nchildren; i++) { + if (root->info.dir.children[i]->type == ECMA119_DIR) + sort_tree(root->info.dir.children[i]); + } +} + +int ecma119_tree_create(Ecma119Image *img, IsoNode *iso) { int ret; - ret = create_tree(img, iso, tree, 1, 0); + Ecma119Node *root; + + ret = create_tree(img, iso, &root, 1, 0); if (ret < 0) { return ret; } + img->root = root; + sort_tree(root); /* * TODO * - reparent if RR - * - sort files in dir * - mangle names */ diff --git a/src/ecma119_tree.h b/src/ecma119_tree.h index 4230e29..67b3961 100644 --- a/src/ecma119_tree.h +++ b/src/ecma119_tree.h @@ -58,7 +58,7 @@ struct ecma119_node /** * */ -int ecma119_tree_create(Ecma119Image *img, IsoNode *iso, Ecma119Node **tree); +int ecma119_tree_create(Ecma119Image *img, IsoNode *iso); /** * Free an Ecma119Node, and its children if node is a dir