Helper function to extract path parts
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/692f8ad1 Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/692f8ad1 Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/692f8ad1 Branch: refs/heads/master Commit: 692f8ad186ee0dc50cb4b4937845fad8bfc87714 Parents: b055f09 Author: Nick Wellnhofer <[email protected]> Authored: Wed Jul 8 13:59:25 2015 +0200 Committer: Nick Wellnhofer <[email protected]> Committed: Sat Jul 11 14:54:59 2015 +0200 ---------------------------------------------------------------------- compiler/src/CFCHierarchy.c | 77 +++++++++++++++++++-------------------- compiler/src/CFCTestParcel.c | 6 +-- 2 files changed, 40 insertions(+), 43 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/692f8ad1/compiler/src/CFCHierarchy.c ---------------------------------------------------------------------- diff --git a/compiler/src/CFCHierarchy.c b/compiler/src/CFCHierarchy.c index ea4f8d7..a9bda94 100644 --- a/compiler/src/CFCHierarchy.c +++ b/compiler/src/CFCHierarchy.c @@ -81,6 +81,9 @@ S_parse_cf_files(CFCHierarchy *self, const char *source_dir, int is_included); static void S_find_files(const char *path, void *arg); +static char* +S_extract_path_part(const char *path, const char *dir, const char *ext); + static void S_connect_classes(CFCHierarchy *self); @@ -251,26 +254,10 @@ S_parse_parcel_files(const char *source_dir, int is_included) { context.num_paths = 0; CFCUtil_walk(source_dir, S_find_files, &context); - size_t source_dir_len = strlen(source_dir); - // Parse .cfp files and register the parcels they define. for (int i = 0; context.paths[i] != NULL; i++) { const char *path = context.paths[i]; - - if (strncmp(path, source_dir, source_dir_len) != 0) { - CFCUtil_die("'%s' doesn't start with '%s'", path, source_dir); - } - const char *path_part = path + source_dir_len; - while (*path_part == CHY_DIR_SEP_CHAR) { - ++path_part; - } - - // Ignore hidden files. - if (path_part[0] == '.' - || strstr(path_part, CHY_DIR_SEP ".") != NULL) { - continue; - } - + char *path_part = S_extract_path_part(path, source_dir, ".cfp"); CFCFileSpec *file_spec = CFCFileSpec_new(source_dir, path_part, is_included); CFCParcel *parcel = CFCParcel_new_from_file(path, file_spec); @@ -293,6 +280,7 @@ S_parse_parcel_files(const char *source_dir, int is_included) { } CFCBase_decref((CFCBase*)parcel); CFCBase_decref((CFCBase*)file_spec); + FREEMEM(path_part); } CFCUtil_free_string_array(context.paths); @@ -328,33 +316,12 @@ S_parse_cf_files(CFCHierarchy *self, const char *source_dir, int is_included) { context.paths = (char**)CALLOCATE(1, sizeof(char*)); context.num_paths = 0; CFCUtil_walk(source_dir, S_find_files, &context); - size_t source_dir_len = strlen(source_dir); - char *path_part = NULL; - size_t path_part_max = 0; // Process any file that has at least one class declaration. for (int i = 0; context.paths[i] != NULL; i++) { // Derive the name of the class that owns the module file. char *source_path = context.paths[i]; - size_t source_path_len = strlen(source_path); - if (strncmp(source_path, source_dir, source_dir_len) != 0) { - CFCUtil_die("'%s' doesn't start with '%s'", source_path, - source_dir); - } - size_t path_part_len = source_path_len - - source_dir_len - - strlen(".cfh"); - if (path_part_max < path_part_len + 1) { - path_part_max = path_part_len + 1; - path_part = (char*)REALLOCATE(path_part, path_part_max); - } - const char *src = source_path + source_dir_len; - while (*src == CHY_DIR_SEP_CHAR) { - ++src; - --path_part_len; - } - memcpy(path_part, src, path_part_len); - path_part[path_part_len] = '\0'; + char *path_part = S_extract_path_part(source_path, source_dir, ".cfh"); // Ignore hidden files. if (path_part[0] == '.' @@ -397,11 +364,11 @@ S_parse_cf_files(CFCHierarchy *self, const char *source_dir, int is_included) { CFCBase_decref((CFCBase*)file); CFCBase_decref((CFCBase*)file_spec); + FREEMEM(path_part); } self->classes[self->num_classes] = NULL; CFCUtil_free_string_array(context.paths); - FREEMEM(path_part); } static void @@ -424,6 +391,36 @@ S_find_files(const char *path, void *arg) { } } +static char* +S_extract_path_part(const char *path, const char *dir, const char *ext) { + size_t path_len = strlen(path); + size_t dir_len = strlen(dir); + size_t ext_len = strlen(ext); + + if (path_len <= dir_len + ext_len) { + CFCUtil_die("Unexpected path '%s'", path); + } + if (strncmp(path, dir, dir_len) != 0) { + CFCUtil_die("'%s' doesn't start with '%s'", path, dir); + } + if (strcmp(path + path_len - ext_len, ext) != 0) { + CFCUtil_die("'%s' doesn't end with '%s'", path, ext); + } + + const char *src = path + dir_len; + size_t path_part_len = path_len - (dir_len + ext_len); + while (path_part_len && *src == CHY_DIR_SEP_CHAR) { + ++src; + --path_part_len; + } + + char *path_part = (char*)MALLOCATE(path_part_len + 1); + memcpy(path_part, src, path_part_len); + path_part[path_part_len] = '\0'; + + return path_part; +} + static void S_connect_classes(CFCHierarchy *self) { // Wrangle the classes into hierarchies and figure out inheritance. http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/692f8ad1/compiler/src/CFCTestParcel.c ---------------------------------------------------------------------- diff --git a/compiler/src/CFCTestParcel.c b/compiler/src/CFCTestParcel.c index 8d57b0c..eec782a 100644 --- a/compiler/src/CFCTestParcel.c +++ b/compiler/src/CFCTestParcel.c @@ -86,7 +86,7 @@ S_run_parcel_tests(CFCTest *test) { } { - CFCFileSpec *file_spec = CFCFileSpec_new(".", "Parcel.cfp", true); + CFCFileSpec *file_spec = CFCFileSpec_new(".", "Parcel", true); CFCParcel *parcel = CFCParcel_new("Foo", NULL, NULL, file_spec); OK(test, CFCParcel_included(parcel), "included"); CFCBase_decref((CFCBase*)parcel); @@ -163,13 +163,13 @@ S_run_parcel_tests(CFCTest *test) { } { - CFCFileSpec *foo_file_spec = CFCFileSpec_new(".", "Foo.cfp", true); + CFCFileSpec *foo_file_spec = CFCFileSpec_new(".", "Foo", true); CFCParcel *foo = CFCParcel_new("Foo", NULL, NULL, foo_file_spec); CFCParcel_register(foo); CFCVersion *cfish_version = CFCVersion_new("v0.8.7"); CFCFileSpec *cfish_file_spec - = CFCFileSpec_new(".", "Clownfish.cfp", true); + = CFCFileSpec_new(".", "Clownfish", true); CFCParcel *cfish = CFCParcel_new("Clownfish", NULL, cfish_version, cfish_file_spec); CFCParcel_register(cfish);
