You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by nw...@apache.org on 2014/08/05 12:21:37 UTC
[3/8] git commit: Find files with arbitrary extensions
Find files with arbitrary extensions
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/28a012da
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/28a012da
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/28a012da
Branch: refs/heads/master
Commit: 28a012da843d3587ddbf610d3d6fd7d02d2127a4
Parents: 666be2e
Author: Nick Wellnhofer <we...@aevum.de>
Authored: Fri Aug 1 13:47:46 2014 +0200
Committer: Nick Wellnhofer <we...@aevum.de>
Committed: Tue Aug 5 11:56:01 2014 +0200
----------------------------------------------------------------------
compiler/src/CFCHierarchy.c | 58 ++++++++++++++++++++++++++--------------
1 file changed, 38 insertions(+), 20 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/28a012da/compiler/src/CFCHierarchy.c
----------------------------------------------------------------------
diff --git a/compiler/src/CFCHierarchy.c b/compiler/src/CFCHierarchy.c
index dddb121..190c01d 100644
--- a/compiler/src/CFCHierarchy.c
+++ b/compiler/src/CFCHierarchy.c
@@ -60,6 +60,12 @@ struct CFCHierarchy {
size_t num_classes;
};
+typedef struct CFCFindFilesContext {
+ const char *ext;
+ char **paths;
+ size_t num_paths;
+} CFCFindFilesContext;
+
typedef struct CFCParseParcelFilesContext {
int is_included;
} CFCParseParcelFilesContext;
@@ -302,38 +308,53 @@ S_check_prereqs(CFCHierarchy *self) {
}
static void
-S_find_cfh(const char *path, void *context) {
- char ***cfh_ptr = (char***)context;
- char **cfh_list = *cfh_ptr;
+S_find_files(const char *path, void *arg) {
// Ignore updirs and hidden files.
if (strstr(path, CHY_DIR_SEP ".") != NULL) {
return;
}
- size_t path_len = strlen(path);
- if (path_len > 4 && (strcmp((path + path_len - 4), ".cfh") == 0)) {
- size_t num_cfh = 0;
- while (cfh_list[num_cfh] != NULL) { num_cfh++; }
- size_t size = (num_cfh + 2) * sizeof(char*);
- cfh_list = (char**)REALLOCATE(cfh_list, size);
- cfh_list[num_cfh] = CFCUtil_strdup(path);
- cfh_list[num_cfh + 1] = NULL;
+
+ CFCFindFilesContext *context = (CFCFindFilesContext*)arg;
+ const char *ext = context->ext;
+ size_t path_len = strlen(path);
+ size_t ext_len = strlen(ext);
+
+ if (path_len > ext_len && (strcmp(path + path_len - ext_len, ext) == 0)) {
+ size_t num_paths = context->num_paths;
+ size_t size = (num_paths + 2) * sizeof(char*);
+ char **paths = (char**)REALLOCATE(context->paths, size);
+
+ paths[num_paths] = CFCUtil_strdup(path);
+ paths[num_paths + 1] = NULL;
+
+ context->num_paths++;
+ context->paths = paths;
}
+}
- *cfh_ptr = cfh_list;
+static void
+S_free_find_files_context(CFCFindFilesContext *context) {
+ for (int i = 0; context->paths[i] != NULL; i++) {
+ FREEMEM(context->paths[i]);
+ }
+ FREEMEM(context->paths);
}
static void
S_parse_cf_files(CFCHierarchy *self, const char *source_dir, int is_included) {
- char **all_source_paths = (char**)CALLOCATE(1, sizeof(char*));
- CFCUtil_walk(source_dir, S_find_cfh, &all_source_paths);
+ CFCFindFilesContext context;
+ context.ext = ".cfh";
+ 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; all_source_paths[i] != NULL; i++) {
+ for (int i = 0; context.paths[i] != NULL; i++) {
// Derive the name of the class that owns the module file.
- char *source_path = all_source_paths[i];
+ 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,
@@ -394,10 +415,7 @@ S_parse_cf_files(CFCHierarchy *self, const char *source_dir, int is_included) {
}
self->classes[self->num_classes] = NULL;
- for (int i = 0; all_source_paths[i] != NULL; i++) {
- FREEMEM(all_source_paths[i]);
- }
- FREEMEM(all_source_paths);
+ S_free_find_files_context(&context);
FREEMEM(path_part);
}