]> git.saurik.com Git - wxWidgets.git/blob - samples/forty/scorefil.cpp
old makefiles removed
[wxWidgets.git] / samples / 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
42 ScoreFile::ScoreFile(const char* appName)
43 {
44 #if 0
45 wxString filename;
46 m_configFilename << "/usr/local/share/" << appName << ".scores";
47 if (access(m_configFilename, F_OK) == 0)
48 {
49 if (access(m_configFilename, R_OK | W_OK) != 0)
50 {
51 // file is not r/w - use local file instead
52 m_configFilename = wxFileConfig::GetLocalFileName(appName);
53 }
54 }
55 else
56 {
57 int fd = creat(m_configFilename, 0666);
58
59 if (fd < 0)
60 {
61 // failed to create file - use local file instead
62 m_configFilename = wxFileConfig::GetLocalFileName(appName);
63 }
64 else
65 {
66 // ensure created file has rw-rw-rw permissions
67 close(fd);
68 }
69 }
70 #endif
71
72 m_config = new wxConfig(appName, "wxWindows", appName, "", wxCONFIG_USE_LOCAL_FILE); // only local
73 }
74
75 ScoreFile::~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
86 void ScoreFile::GetPlayerList( wxArrayString &list )
87 {
88 m_config->SetPath("/Players");
89 int length = m_config->GetNumberOfGroups();
90
91 if (length <= 0) return;
92
93 wxString player;
94 long index, i = 0;
95 if (m_config->GetFirstGroup(player, index))
96 {
97 list.Add( player );
98 i++;
99 while (m_config->GetNextGroup(player, index))
100 {
101 list.Add( player );
102 i++;
103 }
104 }
105 }
106
107
108 // Calculate an encrypted check number to prevent tampering with
109 // score file
110 long ScoreFile::CalcCheck(const char* name, int p1, int p2, int p3)
111 {
112 long check = 0;
113 while (*name)
114 {
115 check = (check << 1) ^ (long)*name++;
116 check = ((check >> 23) ^ check) & 0xFFFFFF;
117 }
118 check = (check << 1) ^ (long)p1;
119 check = ((check >> 23) ^ check) & 0xFFFFFF;
120 check = (check << 1) ^ (long)p2;
121 check = ((check >> 23) ^ check) & 0xFFFFFF;
122 check = (check << 1) ^ (long)p3;
123 check = ((check >> 23) ^ check) & 0xFFFFFF;
124 return check;
125 }
126
127 wxString ScoreFile::GetPreviousPlayer() const
128 {
129 wxString result;
130 m_config->SetPath("/General");
131 m_config->Read("LastPlayer", &result);
132 return result;
133 }
134
135 void ScoreFile::ReadPlayersScore(
136 const char* player,
137 int& wins,
138 int& games,
139 int& score)
140 {
141 long check;
142 long myWins, myGames, myScore;
143
144 games = wins = score = 0;
145
146 m_config->SetPath("/Players");
147 m_config->SetPath(player);
148 if (m_config->Read("Score", &myScore, 0L) &&
149 m_config->Read("Games", &myGames, 0L) &&
150 m_config->Read("Wins", &myWins, 0L) &&
151 m_config->Read("Check", &check, 0L))
152 {
153 if (check != CalcCheck(player, myGames, myWins, myScore))
154 {
155 wxMessageBox("Score file corrupted", "Warning",
156 wxOK | wxICON_EXCLAMATION);
157 }
158 else
159 {
160 games = myGames;
161 wins = myWins;
162 score = myScore;
163 }
164 }
165 WritePlayersScore(player, wins, games, score);
166 }
167
168
169 void ScoreFile::WritePlayersScore(const char* player, int wins, int games, int score)
170 {
171 if (player)
172 {
173 m_config->SetPath("/General");
174 m_config->Write("LastPlayer", player);
175
176 m_config->SetPath("/Players");
177 m_config->SetPath(player);
178 m_config->Write("Score", (long)score);
179 m_config->Write("Games", (long)games);
180 m_config->Write("Wins", (long)wins);
181 m_config->Write("Check", CalcCheck(player, games, wins, score));
182 }
183 }