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 "gbgggamegeniecode.hh"
00026 #include "../exceptions/invalidgamegeniecode.hh"
00027
00028 using namespace emuWorks;
00029
00030 const char GBGGGameGenieCode::ALPHABET[] = {
00031 '0', '1', '2', '3', '4', '5', '6', '7',
00032 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
00033 };
00034
00035 GBGGGameGenieCode GBGGGameGenieCode::create(QString &code) {
00036 if (isValidCode(code)) {
00037 return GBGGGameGenieCode(code);
00038 }
00039
00040 throw InvalidGameGenieCodeException();
00041 }
00042
00043 bool GBGGGameGenieCode::isValidCode(QString &code) {
00044 GBGGGameGenieCode temp(code);
00045
00046 const char *alphabet = temp.getAlphabet();
00047 int count = temp.getAlphabetCount();
00048
00049 QString upper = code.toUpper();
00050 int length = upper.length();
00051
00052 if ((length != 7) && (length != 11)) {
00053 return false;
00054 }
00055
00056 if (code[3] != '-') {
00057 return false;
00058 }
00059
00060 if (length == 11) {
00061 if (code[7] != '-') {
00062 return false;
00063 }
00064 }
00065
00066 for (int i = 0; i < length; i++) {
00067 if ((i == 3) || (i == 7)) {
00068 continue;
00069 }
00070
00071 bool found = false;
00072 QChar ch = code[i];
00073 char letter = ch.toAscii();
00074
00075 for (int j = 0; j < count; j++) {
00076 if (letter == alphabet[j]) {
00077 found = true;
00078 break;
00079 }
00080 }
00081
00082 if (!found) {
00083 return false;
00084 }
00085 }
00086
00087 return true;
00088 }
00089
00090 const char *GBGGGameGenieCode::getAlphabet() {
00091 return ALPHABET;
00092 }
00093