From: Eric S. Raymond Date: Fri, 19 Sep 2003 12:15:46 +0000 (+0000) Subject: Feature analysis is now done through a method table. X-Git-Tag: 2.1~7 X-Git-Url: http://thyrsus.com/gitweb/?a=commitdiff_plain;h=439e66d3c4e1f54b83310df0a6bca58410369871;p=comparator.git Feature analysis is now done through a method table. --- diff --git a/linebyline.c b/linebyline.c index b3302f3..7b212b4 100644 --- a/linebyline.c +++ b/linebyline.c @@ -260,6 +260,15 @@ void analyzer_free(const char *text) free(text); } +/* our method table */ +struct analyzer_t linebyline = +{ + init: analyzer_init, + mode: analyzer_mode, + get: analyzer_get, + free: analyzer_free, +}; + #ifdef TEST int main(int argc, char *argv[]) { diff --git a/main.c b/main.c index 913f760..037b15d 100644 --- a/main.c +++ b/main.c @@ -522,7 +522,7 @@ main(int argc, char *argv[]) usage(); report_time(NULL); - analyzer_init(); + linebyline.init(); /* special case if user gave exactly one tree */ if (!compile_only && argcount == 1) diff --git a/shred.h b/shred.h index 8fb366a..c40f7f0 100644 --- a/shred.h +++ b/shred.h @@ -39,26 +39,34 @@ struct hash_t }; #define SORTHASHCMP(s, t) hash_compare((s)->hash.hash, (t)->hash.hash) -struct filehdr_t +struct filehdr_t /* file/attributes structure describing input source */ { char *name; linenum_t length; struct filehdr_t *next; }; -struct sorthash_t +struct sorthash_t /* in-core hash with pointer to input source */ { struct hash_t hash; struct filehdr_t *file; }; -typedef struct +typedef struct /* structure describing an input feature */ { char *text; int flags; } feature_t; +struct analyzer_t /* structure describing a feature analyzer */ +{ + void (*init)(void); + void (*mode)(int); + feature_t *(*get)(const struct filehdr_t *, FILE *, linenum_t *); + void (*free)(const char *); +}; + /* control bits, meant to be set at startup */ extern int remove_braces; extern int remove_whitespace; @@ -81,12 +89,8 @@ extern int shredfile(struct filehdr_t *, void (*hook)(struct hash_t, struct filehdr_t *)); extern void sort_hashes(struct sorthash_t *hashlist, int hashcount); -/* linebyline.c functions */ -extern void analyzer_init(void); -extern void analyzer_mode(int); -extern feature_t *analyzer_get(const struct filehdr_t *, - FILE *, linenum_t *linenumber); -extern void analyzer_free(const char *); +/* linebyline.c feature analyzer */ +struct analyzer_t linebyline; /* shredcompare.c functions */ extern int merge_compare(struct sorthash_t *obarray, int hashcount); diff --git a/shredtree.c b/shredtree.c index 16821d0..647397d 100644 --- a/shredtree.c +++ b/shredtree.c @@ -130,17 +130,17 @@ int shredfile(struct filehdr_t *file, /* deduce what filtering type we should use */ #define endswith(suff) !strcmp(suff,file->name+strlen(file->name)-strlen(suff)) - analyzer_mode(0); + linebyline.mode(0); if (endswith(".c") || endswith(".h")) - analyzer_mode(C_CODE); + linebyline.mode(C_CODE); else if (endswith(".sh")) - analyzer_mode(SHELL_CODE); + linebyline.mode(SHELL_CODE); #undef endswith display = (shred *)calloc(sizeof(shred), shredsize); linenumber = accepted = 0; - while ((feature = analyzer_get(file, fp, &linenumber))) + while ((feature = linebyline.get(file, fp, &linenumber))) { accepted++; @@ -154,7 +154,7 @@ int shredfile(struct filehdr_t *file, hook(emit_chunk(display, linenumber), file); /* shreds in progress are shifted down */ - analyzer_free(display[0].feature); + linebyline.free(display[0].feature); for (i=1; i < shredsize; i++) display[i-1] = display[i]; display[shredsize-1].feature = NULL;