]>
git.saurik.com Git - wxWidgets.git/blob - demos/forty/scorefil.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Forty Thieves patience game
4 // Author: Chris Breeze
8 // Copyright: (c) 1993-1998 Chris Breeze
9 // Licence: wxWindows licence
10 //---------------------------------------------------------------------------
11 // Last modified: 14th May 1998 - ported to wxWindows 2.0
12 /////////////////////////////////////////////////////////////////////////////
15 #pragma implementation
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
35 #include "wx/textfile.h"
36 #include "wx/config.h"
37 #include "wx/fileconf.h"
41 ScoreFile::ScoreFile(const wxString
& appName
)
45 m_configFilename
<< "/usr/local/share/" << appName
<< ".scores";
46 if (access(m_configFilename
, F_OK
) == 0)
48 if (access(m_configFilename
, R_OK
| W_OK
) != 0)
50 // file is not r/w - use local file instead
51 m_configFilename
= wxFileConfig::GetLocalFileName(appName
);
56 int fd
= creat(m_configFilename
, 0666);
60 // failed to create file - use local file instead
61 m_configFilename
= wxFileConfig::GetLocalFileName(appName
);
65 // ensure created file has rw-rw-rw permissions
71 m_config
= new wxConfig(appName
, _T("wxWindows"), appName
, _T(""),
72 wxCONFIG_USE_LOCAL_FILE
); // only local
75 ScoreFile::~ScoreFile()
79 // ensure score file has rw-rw-rw permissions
80 // (wxFileConfig sets them to rw-------)
81 chmod(m_configFilename
, 0666);
86 void ScoreFile::GetPlayerList( wxArrayString
&list
)
88 m_config
->SetPath(_T("/Players"));
89 int length
= m_config
->GetNumberOfGroups();
91 if (length
<= 0) return;
95 if (m_config
->GetFirstGroup(player
, index
))
98 while (m_config
->GetNextGroup(player
, index
))
106 // Calculate an encrypted check number to prevent tampering with
108 long ScoreFile::CalcCheck(const wxString
& name
, int p1
, int p2
, int p3
)
111 size_t i
, max
= name
.length();
113 for(i
= 0; i
< max
; ++i
)
115 check
= (check
<< 1) ^ (long)name
[i
];
116 check
= ((check
>> 23) ^ check
) & 0xFFFFFF;
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;
127 wxString
ScoreFile::GetPreviousPlayer() const
130 m_config
->SetPath(_T("/General"));
131 m_config
->Read(_T("LastPlayer"), &result
);
135 void ScoreFile::ReadPlayersScore(
136 const wxString
& player
,
142 long myWins
= 0, myGames
= 0, myScore
= 0;
144 games
= wins
= score
= 0;
146 m_config
->SetPath(_T("/Players"));
147 m_config
->SetPath(player
);
148 if (m_config
->Read(_T("Score"), &myScore
, 0L) &&
149 m_config
->Read(_T("Games"), &myGames
, 0L) &&
150 m_config
->Read(_T("Wins"), &myWins
, 0L) &&
151 m_config
->Read(_T("Check"), &check
, 0L))
153 if (check
!= CalcCheck(player
, myGames
, myWins
, myScore
))
155 wxMessageBox(_T("Score file corrupted"), _T("Warning"),
156 wxOK
| wxICON_EXCLAMATION
);
165 WritePlayersScore(player
, wins
, games
, score
);
169 void ScoreFile::WritePlayersScore(const wxString
& player
, int wins
, int games
, int score
)
173 m_config
->SetPath(_T("/General"));
174 m_config
->Write(_T("LastPlayer"), wxString(player
)); // Without wxString tmp, thinks it's bool in VC++
176 m_config
->SetPath(_T("/Players"));
177 m_config
->SetPath(player
);
178 m_config
->Write(_T("Score"), (long)score
);
179 m_config
->Write(_T("Games"), (long)games
);
180 m_config
->Write(_T("Wins"), (long)wins
);
181 m_config
->Write(_T("Check"), CalcCheck(player
, games
, wins
, score
));