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 "error.hpp"
20
21 #include <fstream>
22
23 namespace { // anon
24
25 const char
26 *CSpeciesError_CODES[CSpeciesError::CODE_LAST+1] = {
27 "MISC",
28 "MARS",
29 "BAD_SPECIES_STATE",
30 "BAD_OPCODE",
31 "NEG_OPCODEFREQ_HINT",
32 "ZERO_OPCODEFREQ_HINT_SUM",
33 "BAD_CRand",
34 "BAD_FILE",
35 "EVOLVING_SPECIES_CANNOT_ADD_BENCHMARK_WARRIOR",
36 "BENCHMARK_SPECIES_CANNOT_ADD_EVOLVING_CWarrior",
37 "CANNOT_OPEN_GENERATION_FILE",
38 "CWarrior_NOT_IN_GENERATION_FILE",
39 "GENERATION_FILE_ERROR",
40 "MARS_ALLOC_ERROR",
41 "CANNOT_OPEN_INI",
42 "BAD_GEN_FILE",
43 "BAD_RESUMEFILE",
44 "BAD_CODE"
45 };
46 } // anon namespace
47
string_append(std::string & s,unsigned int i)48 void string_append(std::string &s,unsigned int i) { // very crude, ought to be a way to sort this out
49 const char *number = "0123456789";
50 unsigned int x = s.size();
51 if(0 == i) { // special case?
52 s.insert(x,1,'0');
53 } else while(i > 0) {
54 s.insert(x,1,number[i % 10]);
55 i /= 10;
56 }
57 }
58
file_exists(std::string & s)59 bool file_exists(std::string &s) {
60 using namespace std;
61 fstream file(s.c_str(),ios::in);
62 if(file.is_open()) {
63 file.close();
64 return true;
65 }
66 return false;
67 }
68
69 /***** CSpeciesError class implementation ************/
70
CSpeciesError(const CSpeciesError::CODE code,const char * s1,const char * s2)71 CSpeciesError::CSpeciesError(const CSpeciesError::CODE code,const char *s1,const char *s2) {
72 _code = code;
73 _desc = "CSpeciesError <";
74 _desc += code_desc(_code);
75 _desc += "> ";
76 if(0 != s1)
77 _desc += s1;
78 if(0 != s2) {
79 if(0 != s1)
80 _desc += ": ";
81 _desc += s2;
82 }
83 }
84
code_desc(const CSpeciesError::CODE code)85 const char *CSpeciesError::code_desc(const CSpeciesError::CODE code) {
86 if((0 <= code) && (code < CODE_LAST))
87 return CSpeciesError_CODES[code];
88 return CSpeciesError_CODES[CSpeciesError::CODE_LAST]; // bad
89 }
90
91