]> git.saurik.com Git - wxWidgets.git/blame - demos/forty/scorefil.cpp
Fix a crash in wxExecute() in wxMSW too.
[wxWidgets.git] / demos / forty / scorefil.cpp
CommitLineData
63cafd27
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: scorefil.cpp
3// Purpose: Forty Thieves patience game
4// Author: Chris Breeze
5// Modified by:
6// Created: 21/07/97
7// RCS-ID: $Id$
8// Copyright: (c) 1993-1998 Chris Breeze
2a21ac15 9// Licence: wxWindows licence
63cafd27
JS
10/////////////////////////////////////////////////////////////////////////////
11
63cafd27
JS
12// For compilers that support precompilation, includes "wx/wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16#pragma hdrstop
17#endif
18
19#ifndef WX_PRECOMP
20#include "wx/wx.h"
21#endif
22
63cafd27
JS
23#include "wx/textfile.h"
24#include "wx/config.h"
25#include "wx/fileconf.h"
26
27#include "scorefil.h"
28
f37c24e0 29ScoreFile::ScoreFile(const wxString& appName)
63cafd27 30{
9a83f860 31 m_config = new wxConfig(appName, wxT("wxWidgets"), appName, wxEmptyString,
f37c24e0 32 wxCONFIG_USE_LOCAL_FILE); // only local
63cafd27
JS
33}
34
35ScoreFile::~ScoreFile()
36{
2a21ac15 37 delete m_config;
63cafd27
JS
38}
39
40
54ff4a70 41void ScoreFile::GetPlayerList( wxArrayString &list )
63cafd27 42{
9a83f860 43 m_config->SetPath(wxT("/Players"));
2a21ac15
DS
44 int length = m_config->GetNumberOfGroups();
45
46 if (length <= 0) return;
47
48 wxString player;
49 long index;
50 if (m_config->GetFirstGroup(player, index))
51 {
52 list.Add( player );
53 while (m_config->GetNextGroup(player, index))
54 {
55 list.Add( player );
56 }
57 }
63cafd27
JS
58}
59
60
61// Calculate an encrypted check number to prevent tampering with
62// score file
f37c24e0 63long ScoreFile::CalcCheck(const wxString& name, int p1, int p2, int p3)
63cafd27
JS
64{
65 long check = 0;
f37c24e0
MB
66 size_t i, max = name.length();
67
68 for(i = 0; i < max; ++i )
2a21ac15
DS
69 {
70 check = (check << 1) ^ (long)name[i];
71 check = ((check >> 23) ^ check) & 0xFFFFFF;
72 }
73 check = (check << 1) ^ (long)p1;
74 check = ((check >> 23) ^ check) & 0xFFFFFF;
75 check = (check << 1) ^ (long)p2;
76 check = ((check >> 23) ^ check) & 0xFFFFFF;
77 check = (check << 1) ^ (long)p3;
78 check = ((check >> 23) ^ check) & 0xFFFFFF;
63cafd27
JS
79 return check;
80}
81
82wxString ScoreFile::GetPreviousPlayer() const
83{
2a21ac15 84 wxString result;
9a83f860
VZ
85 m_config->SetPath(wxT("/General"));
86 m_config->Read(wxT("LastPlayer"), &result);
2a21ac15 87 return result;
63cafd27
JS
88}
89
90void ScoreFile::ReadPlayersScore(
2a21ac15
DS
91 const wxString& player,
92 int& wins,
93 int& games,
94 int& score)
63cafd27 95{
2a21ac15
DS
96 long check = 0;
97 long myWins = 0, myGames = 0, myScore = 0;
98
99 games = wins = score = 0;
100
9a83f860 101 m_config->SetPath(wxT("/Players"));
2a21ac15 102 m_config->SetPath(player);
9a83f860
VZ
103 if (m_config->Read(wxT("Score"), &myScore, 0L) &&
104 m_config->Read(wxT("Games"), &myGames, 0L) &&
105 m_config->Read(wxT("Wins"), &myWins, 0L) &&
106 m_config->Read(wxT("Check"), &check, 0L))
2a21ac15
DS
107 {
108 if (check != CalcCheck(player, myGames, myWins, myScore))
109 {
9a83f860 110 wxMessageBox(wxT("Score file corrupted"), wxT("Warning"),
f37c24e0 111 wxOK | wxICON_EXCLAMATION);
2a21ac15
DS
112 }
113 else
114 {
115 games = myGames;
116 wins = myWins;
117 score = myScore;
118 }
119 }
120 WritePlayersScore(player, wins, games, score);
63cafd27
JS
121}
122
123
f37c24e0 124void ScoreFile::WritePlayersScore(const wxString& player, int wins, int games, int score)
63cafd27 125{
bd0b594d 126 if (!player.empty())
2a21ac15 127 {
9a83f860
VZ
128 m_config->SetPath(wxT("/General"));
129 m_config->Write(wxT("LastPlayer"), wxString(player)); // Without wxString tmp, thinks it's bool in VC++
2a21ac15 130
9a83f860 131 m_config->SetPath(wxT("/Players"));
2a21ac15 132 m_config->SetPath(player);
9a83f860
VZ
133 m_config->Write(wxT("Score"), (long)score);
134 m_config->Write(wxT("Games"), (long)games);
135 m_config->Write(wxT("Wins"), (long)wins);
136 m_config->Write(wxT("Check"), CalcCheck(player, games, wins, score));
2a21ac15 137 }
63cafd27 138}