1 
2 /* "Species" - a CoreWars evolver.  Copyright (C) 2003 'Varfar'
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the Free
6  * Software Foundation; either version 1, or (at your option) any later
7  * version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 
19 #include "inst_gen_markov.hpp"
20 #include "inst_gen_markov_2.hpp"
21 #include "operand_table.hpp"
22 
23 #include <iostream>
24 #include <stdlib.h>
25 #include <string.h>
26 using namespace std;
27 
28 /******* main code ********************************************/
29 
Operand(int argc,const char ** args)30 int Operand(int argc,const char **args) {
31 	try {
32 		COperandTable *table;
33 		const char *lookup_fname;
34 		unsigned coresize;
35 		if(argc != 2 && argc < 5) {
36 			cout << "Usage to analyse rc files:" << endl
37 					<< "  build_freq_tables Operand lookup_filename CORESIZE [file1] .. [filen]" << endl
38 				<< "Usage to list contents of a lookup file:" << endl
39 					<< "  build_freq_tables Operand lookup_filename" << endl;
40 			return 1;
41 		}
42 		lookup_fname = args[1];
43 		if(argc == 2) {
44 			table = new COperandTable(lookup_fname);
45 			table->dump(cerr);
46 		} else {
47 			coresize = atoi(args[2]);
48 			if(coresize <= 0 || coresize > 0xFFFF)
49 				PANIC(MISC,"invalid coresize",NULL);
50 			table = new COperandTable(coresize);
51 			for(int i=3; i<argc; i++) {
52 				table->learn(args[i]);
53 			}
54 			table->dump(cerr);
55 			table->save(lookup_fname);
56 		}
57 	} catch(CSpeciesError *se) {
58 		cerr << se->desc() << endl;
59 		return 1;
60 	}
61 	return 0;
62 }
63 
Markov2(int argc,const char ** args)64 int Markov2(int argc,const char **args) {
65 	try {
66 		CInstGeneratorMarkov2 *markov;
67 		const char *lookup_fname;
68 		unsigned coresize, threshold;
69 		if(argc != 2 && argc < 5) {
70 			cout << "Usage to analyse rc files:" << endl
71 					<< "  build_freq_tables Markov2 lookup_filename CORESIZE THRESHOLD [file1] .. [filen]" << endl
72 				<< "Usage to list contents of a lookup file:" << endl
73 					<< "  build_freq_tables Markov2 lookup_filename" << endl;
74 			return 1;
75 		}
76 		lookup_fname = args[1];
77 		if(argc == 2) {
78 			markov = new CInstGeneratorMarkov2(lookup_fname);
79 			markov->dump(cerr);
80 		} else {
81 			coresize = atoi(args[2]);
82 			if(coresize <= 0 || coresize > 0xFFFF)
83 				PANIC(MISC,"invalid coresize",NULL);
84 			threshold = atoi(args[3]);
85 			if(threshold <= 0 || threshold > 0xFF)
86 				PANIC(MISC,"invalid threshold",NULL);
87 			markov = new CInstGeneratorMarkov2(coresize);
88 			for(int i=4; i<argc; i++) {
89 				markov->learn(args[i]);
90 			}
91 			markov->cull(threshold);
92 			markov->prepare();
93 			markov->dump(cerr);
94 			markov->save(lookup_fname);
95 		}
96 	} catch(CSpeciesError *se) {
97 		cerr << se->desc() << endl;
98 		return 1;
99 	}
100 	return 0;
101 }
102 
Markov(int argc,const char ** args)103 int Markov(int argc,const char **args) {
104 	CInstGeneratorMarkov freq;
105 	try {
106 		if(argc == 2) {
107 			freq.load(args[1]);
108 			freq.dump(cout);
109 			return 0;
110 		}
111 		if(argc < 4) {
112 			cout << "Usage to analyse rc files:" << endl
113 					<< "  build_freq_tables Markov lookup_filename CORESIZE [file1] .. [filen]" << endl
114 				<< "Usage to list contents of a lookup file:" << endl
115 					<< "  build_freq_tables Markov lookup_filename" << endl;
116 			return 1;
117 		}
118 		freq.init(atoi(args[2]));
119 		for(int i=3; i<argc; i++) {
120 			freq.append(args[i]);
121 		}
122 		freq.dump(cout);
123 		freq.save(args[1]);
124 	} catch(CSpeciesError *se) {
125 		cerr << se->desc() << endl;
126 		return 1;
127 	}
128 	return 0;
129 }
130 
main(int argc,const char ** args)131 int main(int argc,const char **args) {
132 	cout << "Species frequency lookup builder" << endl << "(c) Will \'Varfar\', 2003; this is GPL software" << endl;
133 	int ret = 1;
134 	if(argc == 1) {
135 		cout << "*** mode not specified ***" << endl;
136 		ret = 1;
137 	} else if(strcasecmp(args[1],"markov")==0) {
138 		cout << "*** Markov mode ***" << endl;
139 		ret = Markov(argc-1,&args[1]);
140 	} else if(strcasecmp(args[1],"markov2")==0) {
141 		cout << "*** Markov2 mode ***" << endl;
142 		ret = Markov2(argc-1,&args[1]);
143 	} else if(strcasecmp(args[1],"operand")==0) {
144 		cout << "*** Operand mode ***" << endl;
145 		ret = Operand(argc-1,&args[1]);
146 	} else {
147 		cout << "*** unknown mode \"" << args[1] << "\" ***" << endl;
148 		ret = 1;
149 	}
150 	if(0 != ret) {
151 		cout << "Generic usage:" << endl
152 			<< "  build_freq_tables mode" << endl
153 			<< "    where mode is one of:" << endl
154 			<< "      Markov, Markov2, Operand" << endl
155 			<< "    (each mode has mode-specific help if you specify no other arguments)" << endl;
156 	}
157 	return ret;
158 }
159