]> git.saurik.com Git - wxWidgets.git/blob - demos/forty/scorefil.cpp
Regenerate the msvc makefiles after TARGET_CPU option added
[wxWidgets.git] / demos / forty / scorefil.cpp
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
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
34 ScoreFile::ScoreFile(const wxString& appName)
35 {
36 #if 0
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 }
62 #endif
63
64 m_config = new wxConfig(appName, _T("wxWidgets"), appName, wxEmptyString,
65 wxCONFIG_USE_LOCAL_FILE); // only local
66 }
67
68 ScoreFile::~ScoreFile()
69 {
70 delete m_config;
71 #ifdef __WXGTK__
72 // ensure score file has rw-rw-rw permissions
73 // (wxFileConfig sets them to rw-------)
74 chmod(m_configFilename, 0666);
75 #endif
76 }
77
78
79 void ScoreFile::GetPlayerList( wxArrayString &list )
80 {
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 }
96 }
97
98
99 // Calculate an encrypted check number to prevent tampering with
100 // score file
101 long ScoreFile::CalcCheck(const wxString& name, int p1, int p2, int p3)
102 {
103 long check = 0;
104 size_t i, max = name.length();
105
106 for(i = 0; i < max; ++i )
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;
117 return check;
118 }
119
120 wxString ScoreFile::GetPreviousPlayer() const
121 {
122 wxString result;
123 m_config->SetPath(_T("/General"));
124 m_config->Read(_T("LastPlayer"), &result);
125 return result;
126 }
127
128 void ScoreFile::ReadPlayersScore(
129 const wxString& player,
130 int& wins,
131 int& games,
132 int& score)
133 {
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"),
149 wxOK | wxICON_EXCLAMATION);
150 }
151 else
152 {
153 games = myGames;
154 wins = myWins;
155 score = myScore;
156 }
157 }
158 WritePlayersScore(player, wins, games, score);
159 }
160
161
162 void ScoreFile::WritePlayersScore(const wxString& player, int wins, int games, int score)
163 {
164 if (player)
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 }
176 }