]> git.saurik.com Git - wxWidgets.git/blame - demos/forty/scorefil.cpp
Don't create uneeded variables.
[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
23#ifdef __WXGTK__
24#include <unistd.h>
25#include <sys/stat.h>
26#include <fcntl.h>
27#endif
28#include "wx/textfile.h"
29#include "wx/config.h"
30#include "wx/fileconf.h"
31
32#include "scorefil.h"
33
f37c24e0 34ScoreFile::ScoreFile(const wxString& appName)
63cafd27 35{
18244936 36#if 0
2a21ac15
DS
37 wxString filename;
38 m_configFilename << "/usr/local/share/" << appName << ".scores";
39 if (access(m_configFilename, F_OK) == 0)
40 {
41 if (access(m_configFilename, R_OK | W_OK) != 0)
42 {
43 // file is not r/w - use local file instead
44 m_configFilename = wxFileConfig::GetLocalFileName(appName);
45 }
46 }
47 else
48 {
49 int fd = creat(m_configFilename, 0666);
50
51 if (fd < 0)
52 {
53 // failed to create file - use local file instead
54 m_configFilename = wxFileConfig::GetLocalFileName(appName);
55 }
56 else
57 {
58 // ensure created file has rw-rw-rw permissions
59 close(fd);
60 }
61 }
54ff4a70
RR
62#endif
63
2a21ac15 64 m_config = new wxConfig(appName, _T("wxWidgets"), appName, wxEmptyString,
f37c24e0 65 wxCONFIG_USE_LOCAL_FILE); // only local
63cafd27
JS
66}
67
68ScoreFile::~ScoreFile()
69{
2a21ac15 70 delete m_config;
63cafd27 71#ifdef __WXGTK__
2a21ac15
DS
72 // ensure score file has rw-rw-rw permissions
73 // (wxFileConfig sets them to rw-------)
74 chmod(m_configFilename, 0666);
63cafd27
JS
75#endif
76}
77
78
54ff4a70 79void ScoreFile::GetPlayerList( wxArrayString &list )
63cafd27 80{
2a21ac15
DS
81 m_config->SetPath(_T("/Players"));
82 int length = m_config->GetNumberOfGroups();
83
84 if (length <= 0) return;
85
86 wxString player;
87 long index;
88 if (m_config->GetFirstGroup(player, index))
89 {
90 list.Add( player );
91 while (m_config->GetNextGroup(player, index))
92 {
93 list.Add( player );
94 }
95 }
63cafd27
JS
96}
97
98
99// Calculate an encrypted check number to prevent tampering with
100// score file
f37c24e0 101long ScoreFile::CalcCheck(const wxString& name, int p1, int p2, int p3)
63cafd27
JS
102{
103 long check = 0;
f37c24e0
MB
104 size_t i, max = name.length();
105
106 for(i = 0; i < max; ++i )
2a21ac15
DS
107 {
108 check = (check << 1) ^ (long)name[i];
109 check = ((check >> 23) ^ check) & 0xFFFFFF;
110 }
111 check = (check << 1) ^ (long)p1;
112 check = ((check >> 23) ^ check) & 0xFFFFFF;
113 check = (check << 1) ^ (long)p2;
114 check = ((check >> 23) ^ check) & 0xFFFFFF;
115 check = (check << 1) ^ (long)p3;
116 check = ((check >> 23) ^ check) & 0xFFFFFF;
63cafd27
JS
117 return check;
118}
119
120wxString ScoreFile::GetPreviousPlayer() const
121{
2a21ac15
DS
122 wxString result;
123 m_config->SetPath(_T("/General"));
124 m_config->Read(_T("LastPlayer"), &result);
125 return result;
63cafd27
JS
126}
127
128void ScoreFile::ReadPlayersScore(
2a21ac15
DS
129 const wxString& player,
130 int& wins,
131 int& games,
132 int& score)
63cafd27 133{
2a21ac15
DS
134 long check = 0;
135 long myWins = 0, myGames = 0, myScore = 0;
136
137 games = wins = score = 0;
138
139 m_config->SetPath(_T("/Players"));
140 m_config->SetPath(player);
141 if (m_config->Read(_T("Score"), &myScore, 0L) &&
142 m_config->Read(_T("Games"), &myGames, 0L) &&
143 m_config->Read(_T("Wins"), &myWins, 0L) &&
144 m_config->Read(_T("Check"), &check, 0L))
145 {
146 if (check != CalcCheck(player, myGames, myWins, myScore))
147 {
148 wxMessageBox(_T("Score file corrupted"), _T("Warning"),
f37c24e0 149 wxOK | wxICON_EXCLAMATION);
2a21ac15
DS
150 }
151 else
152 {
153 games = myGames;
154 wins = myWins;
155 score = myScore;
156 }
157 }
158 WritePlayersScore(player, wins, games, score);
63cafd27
JS
159}
160
161
f37c24e0 162void ScoreFile::WritePlayersScore(const wxString& player, int wins, int games, int score)
63cafd27
JS
163{
164 if (player)
2a21ac15
DS
165 {
166 m_config->SetPath(_T("/General"));
167 m_config->Write(_T("LastPlayer"), wxString(player)); // Without wxString tmp, thinks it's bool in VC++
168
169 m_config->SetPath(_T("/Players"));
170 m_config->SetPath(player);
171 m_config->Write(_T("Score"), (long)score);
172 m_config->Write(_T("Games"), (long)games);
173 m_config->Write(_T("Wins"), (long)wins);
174 m_config->Write(_T("Check"), CalcCheck(player, games, wins, score));
175 }
63cafd27 176}