# HG changeset patch # User John Tsiombikas # Date 1414970440 -7200 # Node ID 7211fa8db425b58b6e45f9051722faf18a836af9 # Parent 0a45dfe93e6639c3090646fbf09f3fade9234bde added filters diff -r 0a45dfe93e66 -r 7211fa8db425 src/mtg.c --- a/src/mtg.c Sun Nov 02 11:07:45 2014 +0200 +++ b/src/mtg.c Mon Nov 03 01:20:40 2014 +0200 @@ -195,11 +195,6 @@ return bits > 1; } -static void dbgprint(struct rbnode *node, void *cls) -{ - fprintf(stderr, "%s\n", (char*)rb_node_key(node)); -} - static int load_editions(void) { FILE *fp; @@ -222,13 +217,11 @@ rb_insert(editions, strdup(code), strdup(name)); } - rb_foreach(editions, dbgprint, 0); - fclose(fp); return 0; } -static unsigned int parse_multicolor(const char *str) +unsigned int mtg_parse_multicolor(const char *str) { int i; unsigned int mask = 0; @@ -259,12 +252,12 @@ } cstr[i] = 0; - if((res = parse_multicolor(colstr))) { + if((res = mtg_parse_multicolor(colstr))) { return res; } if(strcmp(cstr, "*") == 0) { - if((res = parse_multicolor(notes))) { + if((res = mtg_parse_multicolor(notes))) { return res; } return MTG_COL_MULTI; diff -r 0a45dfe93e66 -r 7211fa8db425 src/mtg.h --- a/src/mtg.h Sun Nov 02 11:07:45 2014 +0200 +++ b/src/mtg.h Mon Nov 03 01:20:40 2014 +0200 @@ -47,6 +47,7 @@ const char *mtg_edition(const char *edcode); int mtg_is_multicolor(unsigned int color); +unsigned int mtg_parse_multicolor(const char *str); #endif /* MTG_H_ */ diff -r 0a45dfe93e66 -r 7211fa8db425 src/mtglist.c --- a/src/mtglist.c Sun Nov 02 11:07:45 2014 +0200 +++ b/src/mtglist.c Mon Nov 03 01:20:40 2014 +0200 @@ -1,40 +1,164 @@ #include #include #include +#include #include "mtg.h" +static void print_usage(const char *argv0); +static const char *strstr_lower(const char *str, const char *sub); + +int opt_list; +int opt_count; +unsigned int opt_color_filter; +int opt_color_filter_excl; +enum mtg_card_type opt_type_filter = MTG_TYPE_UNKNOWN; +const char *opt_ed_filter; + int main(int argc, char **argv) { const struct mtg_card *card; - const char *fname = "cardlist"; - int ncards; - - if(argv[1]) fname = argv[1]; + int i, ncards, num_done = 0; if(mtg_init() == -1) { return 1; } - if(mtg_load_cards(fname) == -1) { - mtg_destroy(); + for(i=1; icolor) { + continue; + } + } else { + if(!(card_mask & card->color)) { + continue; + } + } + + if(opt_type_filter != MTG_TYPE_UNKNOWN && opt_type_filter != card->type) { + continue; + } + + if(opt_ed_filter && !strstr_lower(card->edition, opt_ed_filter)) { + continue; + } + + if(opt_list) { + printf("%3d ", card->count); + printf("%-11s ", mtg_color_str(card->color)); + printf("%-32s ", card->name); + printf("%-12s ", mtg_type_str(card->type)); + printf("%s\n", card->edition); + } + + ncards += card->count; + } + + if(opt_count) { + printf("Total cards: %d\n", ncards); + } + + mtg_destroy(); + num_done++; + } + } + + if(num_done == 0) { + fprintf(stderr, "you must specify a cardlist\n"); return 1; } - ncards = 0; + return 0; +} - mtg_iter_begin(); - while((card = mtg_iter_next())) { - printf("%3d ", card->count); - printf("%-11s ", mtg_color_str(card->color)); - printf("%-32s ", card->name); - printf("%-12s ", mtg_type_str(card->type)); - printf("%s\n", card->edition); +static void print_usage(const char *argv0) +{ + printf("Usage: %s [options] \n", argv0); + printf("Options:\n"); + printf(" -c match specified color (eg: \"red\", \"red/green\")\n"); + printf(" use ! to exclude multi (eg: \"red!\" doesn't match \"red/black\")\n"); + printf(" -t match specified card type (creature, instant, etc)\n"); + printf(" -e match specified edition (substring matching: \"5th\", \"5\",\n"); + printf(" and \"5th edition\" all match the 5th edition)\n"); + printf(" -l print list of cards matching filters (or all)\n"); + printf(" -n print a count of matched cards\n"); +} - ncards += card->count; +static const char *strstr_lower(const char *str, const char *sub) +{ + int i, sublen = strlen(sub); + + while(*str) { + for(i=0; i