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