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