00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "genesisgamegeniecode.hh"
00026 #include "../exceptions/invalidgamegeniecode.hh"
00027
00028 using namespace emuWorks;
00029
00030 const char GenesisGameGenieCode::ALPHABET[] = {
00031 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
00032 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S',
00033 'T', 'V', 'W', 'X', 'Y', 'Z', '0', '1',
00034 '2', '3', '4', '5', '6', '7', '8', '9'
00035 };
00036
00037 GenesisGameGenieCode GenesisGameGenieCode::create(QString &code) {
00038 if (isValidCode(code)) {
00039 return GenesisGameGenieCode(code);
00040 }
00041
00042 throw InvalidGameGenieCodeException();
00043 }
00044
00045 bool GenesisGameGenieCode::isValidCode(QString &code) {
00046 GenesisGameGenieCode temp(code);
00047
00048 const char *alphabet = temp.getAlphabet();
00049 int count = temp.getAlphabetCount();
00050
00051 QString upper = code.toUpper();
00052 int length = upper.length();
00053
00054 if (length != 9) {
00055 return false;
00056 }
00057
00058 if (code[4] != '-') {
00059 return false;
00060 }
00061
00062 for (int i = 0; i < length; i++) {
00063 if (i == 4) {
00064 continue;
00065 }
00066
00067 bool found = false;
00068 QChar ch = code[i];
00069 char letter = ch.toAscii();
00070
00071 for (int j = 0; j < count; j++) {
00072 if (letter == alphabet[j]) {
00073 found = true;
00074 break;
00075 }
00076 }
00077
00078 if (!found) {
00079 return false;
00080 }
00081 }
00082
00083 return true;
00084 }
00085
00086 const char *GenesisGameGenieCode::getAlphabet() {
00087 return ALPHABET;
00088 }
00089