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