mtglist

view src/mtglist.c @ 1:7211fa8db425

added filters
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 03 Nov 2014 01:20:40 +0200
parents 0a45dfe93e66
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include "mtg.h"
7 static void print_usage(const char *argv0);
8 static const char *strstr_lower(const char *str, const char *sub);
10 int opt_list;
11 int opt_count;
12 unsigned int opt_color_filter;
13 int opt_color_filter_excl;
14 enum mtg_card_type opt_type_filter = MTG_TYPE_UNKNOWN;
15 const char *opt_ed_filter;
17 int main(int argc, char **argv)
18 {
19 const struct mtg_card *card;
20 int i, ncards, num_done = 0;
22 if(mtg_init() == -1) {
23 return 1;
24 }
26 for(i=1; i<argc; i++) {
27 if(argv[i][0] == '-') {
28 if(argv[i][2] != 0) {
29 fprintf(stderr, "invalid option: %s\n", argv[i]);
30 return 1;
31 }
32 switch(argv[i][1]) {
33 case 'l':
34 opt_list = 1;
35 break;
37 case 'n':
38 opt_count = 1;
39 break;
41 case 'c':
42 {
43 unsigned int c = mtg_parse_multicolor(argv[++i]);
44 if(mtg_is_multicolor(c)) {
45 opt_color_filter = c;
46 opt_color_filter_excl = 1;
47 } else {
48 opt_color_filter |= c;
49 opt_color_filter_excl = 0;
50 }
51 if(strstr(argv[i], "!")) {
52 opt_color_filter_excl = 1;
53 }
54 }
55 break;
57 case 't':
58 if((opt_type_filter = mtg_str_type(argv[++i])) == MTG_TYPE_UNKNOWN) {
59 fprintf(stderr, "invalid type: %s\n", argv[i]);
60 return 1;
61 }
62 break;
64 case 'e':
65 opt_ed_filter = argv[++i];
66 break;
68 case 'h':
69 print_usage(argv[0]);
70 return 0;
72 default:
73 fprintf(stderr, "invalid option: %s\n", argv[i]);
74 return 1;
75 }
76 } else {
77 unsigned int card_mask = opt_color_filter ? opt_color_filter : 0xffff;
79 if(mtg_load_cards(argv[i]) == -1) {
80 mtg_destroy();
81 return 1;
82 }
84 ncards = 0;
86 mtg_iter_begin();
87 while((card = mtg_iter_next())) {
88 if(opt_color_filter_excl) {
89 if(card_mask != card->color) {
90 continue;
91 }
92 } else {
93 if(!(card_mask & card->color)) {
94 continue;
95 }
96 }
98 if(opt_type_filter != MTG_TYPE_UNKNOWN && opt_type_filter != card->type) {
99 continue;
100 }
102 if(opt_ed_filter && !strstr_lower(card->edition, opt_ed_filter)) {
103 continue;
104 }
106 if(opt_list) {
107 printf("%3d ", card->count);
108 printf("%-11s ", mtg_color_str(card->color));
109 printf("%-32s ", card->name);
110 printf("%-12s ", mtg_type_str(card->type));
111 printf("%s\n", card->edition);
112 }
114 ncards += card->count;
115 }
117 if(opt_count) {
118 printf("Total cards: %d\n", ncards);
119 }
121 mtg_destroy();
122 num_done++;
123 }
124 }
126 if(num_done == 0) {
127 fprintf(stderr, "you must specify a cardlist\n");
128 return 1;
129 }
131 return 0;
132 }
134 static void print_usage(const char *argv0)
135 {
136 printf("Usage: %s [options] <cardlist>\n", argv0);
137 printf("Options:\n");
138 printf(" -c <color> match specified color (eg: \"red\", \"red/green\")\n");
139 printf(" use ! to exclude multi (eg: \"red!\" doesn't match \"red/black\")\n");
140 printf(" -t <type> match specified card type (creature, instant, etc)\n");
141 printf(" -e <edition> match specified edition (substring matching: \"5th\", \"5\",\n");
142 printf(" and \"5th edition\" all match the 5th edition)\n");
143 printf(" -l print list of cards matching filters (or all)\n");
144 printf(" -n print a count of matched cards\n");
145 }
147 static const char *strstr_lower(const char *str, const char *sub)
148 {
149 int i, sublen = strlen(sub);
151 while(*str) {
152 for(i=0; i<sublen; i++) {
153 if(tolower(str[i]) != tolower(sub[i])) {
154 break;
155 }
156 }
157 if(i == sublen) {
158 return str;
159 }
160 ++str;
161 }
163 return 0;
164 }