]> git.saurik.com Git - wxWidgets.git/blob - demos/forty/scorefil.cpp
removed useless defines
[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 // 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
41 ScoreFile::ScoreFile(const char* appName)
42 {
43 #if 0
44 wxString filename;
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 }
69 #endif
70
71 m_config = new wxConfig(appName, "wxWindows", appName, "", wxCONFIG_USE_LOCAL_FILE); // only local
72 }
73
74 ScoreFile::~ScoreFile()
75 {
76 delete m_config;
77 #ifdef __WXGTK__
78 // ensure score file has rw-rw-rw permissions
79 // (wxFileConfig sets them to rw-------)
80 chmod(m_configFilename, 0666);
81 #endif
82 }
83
84
85 void ScoreFile::GetPlayerList( wxArrayString &list )
86 {
87 m_config->SetPath("/Players");
88 int length = m_config->GetNumberOfGroups();
89
90 if (length <= 0) return;
91
92 wxString player;
93 long index, i = 0;
94 if (m_config->GetFirstGroup(player, index))
95 {
96 list.Add( player );
97 i++;
98 while (m_config->GetNextGroup(player, index))
99 {
100 list.Add( player );
101 i++;
102 }
103 }
104 }
105
106
107 // Calculate an encrypted check number to prevent tampering with
108 // score file
109 long ScoreFile::CalcCheck(const char* name, int p1, int p2, int p3)
110 {
111 long check = 0;
112 while (*name)
113 {
114 check = (check << 1) ^ (long)*name++;
115 check = ((check >> 23) ^ check) & 0xFFFFFF;
116 }
117 check = (check << 1) ^ (long)p1;
118 check = ((check >> 23) ^ check) & 0xFFFFFF;
119 check = (check << 1) ^ (long)p2;
120 check = ((check >> 23) ^ check) & 0xFFFFFF;
121 check = (check << 1) ^ (long)p3;
122 check = ((check >> 23) ^ check) & 0xFFFFFF;
123 return check;
124 }
125
126 wxString ScoreFile::GetPreviousPlayer() const
127 {
128 wxString result;
129 m_config->SetPath("/General");
130 m_config->Read("LastPlayer", &result);
131 return result;
132 }
133
134 void ScoreFile::ReadPlayersScore(
135 const char* player,
136 int& wins,
137 int& games,
138 int& score)
139 {
140 long check = 0;
141 long myWins = 0, myGames = 0, myScore = 0;
142
143 games = wins = score = 0;
144
145 m_config->SetPath("/Players");
146 m_config->SetPath(player);
147 if (m_config->Read("Score", &myScore, 0L) &&
148 m_config->Read("Games", &myGames, 0L) &&
149 m_config->Read("Wins", &myWins, 0L) &&
150 m_config->Read("Check", &check, 0L))
151 {
152 if (check != CalcCheck(player, myGames, myWins, myScore))
153 {
154 wxMessageBox("Score file corrupted", "Warning",
155 wxOK | wxICON_EXCLAMATION);
156 }
157 else
158 {
159 games = myGames;
160 wins = myWins;
161 score = myScore;
162 }
163 }
164 WritePlayersScore(player, wins, games, score);
165 }
166
167
168 void ScoreFile::WritePlayersScore(const char* player, int wins, int games, int score)
169 {
170 if (player)
171 {
172 m_config->SetPath("/General");
173 m_config->Write("LastPlayer", wxString(player)); // Without wxString tmp, thinks it's bool in VC++
174
175 m_config->SetPath("/Players");
176 m_config->SetPath(player);
177 m_config->Write("Score", (long)score);
178 m_config->Write("Games", (long)games);
179 m_config->Write("Wins", (long)wins);
180 m_config->Write("Check", CalcCheck(player, games, wins, score));
181 }
182 }