genesisgamegeniecode.cc

Go to the documentation of this file.
00001 /*
00002  * Game Genie Encoder/Decoder
00003  * Copyright (C) 2004-2006,2008 emuWorks
00004  * http://games.technoplaza.net/
00005  *
00006  * This file is part of Game Genie Encoder/Decoder.
00007  *
00008  * Game Genie Encoder/Decoder is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * Game Genie Encoder/Decoder is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with Game Genie Encoder/Decoder; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00021  */
00022  
00023 // $Id: genesisgamegeniecode.cc,v 1.8 2008/12/18 03:23:30 jdratlif Exp $
00024 
00025 #include "model/genesisgamegeniecode.hh"
00026 
00027 using namespace ggencoder;
00028 
00029 const char GenesisGameGenieCode::ALPHABET[] = {
00030     'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
00031     'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S',
00032     'T', 'V', 'W', 'X', 'Y', 'Z', '0', '1',
00033     '2', '3', '4', '5', '6', '7', '8', '9'
00034 };
00035 
00036 GenesisGameGenieCode GenesisGameGenieCode::create(const QString &code)
00037     throw(InvalidGameGenieCodeException) {
00038     if (isValidCode(code)) {
00039         return GenesisGameGenieCode(code);
00040     }
00041     
00042     throw InvalidGameGenieCodeException();
00043 }
00044 
00045 const char *GenesisGameGenieCode::getAlphabet() const {
00046     return ALPHABET;
00047 }
00048 
00049 bool GenesisGameGenieCode::isValidCode(const QString &code) {
00050     GenesisGameGenieCode temp(code);
00051     
00052     const char *alphabet = temp.getAlphabet();
00053     int count = temp.getAlphabetCount();
00054     
00055     QString upper = code.toUpper();
00056     int length = upper.length();
00057     
00058     if (length != 9) {
00059         return false;
00060     }
00061     
00062     if (code[4] != '-') {
00063         return false;
00064     }
00065     
00066     for (int i = 0; i < length; i++) {
00067         if (i == 4) {
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 

Generated on Thu Dec 18 01:01:24 2008 for Game Genie Encoder/Decoder by  doxygen 1.5.4