]>
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 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
28 #include "wx/textfile.h"
29 #include "wx/config.h"
30 #include "wx/fileconf.h"
34 ScoreFile::ScoreFile(const wxString
& appName
)
38 m_configFilename
<< "/usr/local/share/" << appName
<< ".scores";
39 if (access(m_configFilename
, F_OK
) == 0)
41 if (access(m_configFilename
, R_OK
| W_OK
) != 0)
43 // file is not r/w - use local file instead
44 m_configFilename
= wxFileConfig::GetLocalFileName(appName
);
49 int fd
= creat(m_configFilename
, 0666);
53 // failed to create file - use local file instead
54 m_configFilename
= wxFileConfig::GetLocalFileName(appName
);
58 // ensure created file has rw-rw-rw permissions
64 m_config
= new wxConfig(appName
, _T("wxWidgets"), appName
, wxEmptyString
,
65 wxCONFIG_USE_LOCAL_FILE
); // only local
68 ScoreFile::~ScoreFile()
72 // ensure score file has rw-rw-rw permissions
73 // (wxFileConfig sets them to rw-------)
74 chmod(m_configFilename
, 0666);
79 void ScoreFile::GetPlayerList( wxArrayString
&list
)
81 m_config
->SetPath(_T("/Players"));
82 int length
= m_config
->GetNumberOfGroups();
84 if (length
<= 0) return;
88 if (m_config
->GetFirstGroup(player
, index
))
91 while (m_config
->GetNextGroup(player
, index
))
99 // Calculate an encrypted check number to prevent tampering with
101 long ScoreFile::CalcCheck(const wxString
& name
, int p1
, int p2
, int p3
)
104 size_t i
, max
= name
.length();
106 for(i
= 0; i
< max
; ++i
)
108 check
= (check
<< 1) ^ (long)name
[i
];
109 check
= ((check
>> 23) ^ check
) & 0xFFFFFF;
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;
120 wxString
ScoreFile::GetPreviousPlayer() const
123 m_config
->SetPath(_T("/General"));
124 m_config
->Read(_T("LastPlayer"), &result
);
128 void ScoreFile::ReadPlayersScore(
129 const wxString
& player
,
135 long myWins
= 0, myGames
= 0, myScore
= 0;
137 games
= wins
= score
= 0;
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))
146 if (check
!= CalcCheck(player
, myGames
, myWins
, myScore
))
148 wxMessageBox(_T("Score file corrupted"), _T("Warning"),
149 wxOK
| wxICON_EXCLAMATION
);
158 WritePlayersScore(player
, wins
, games
, score
);
162 void ScoreFile::WritePlayersScore(const wxString
& player
, int wins
, int games
, int score
)
166 m_config
->SetPath(_T("/General"));
167 m_config
->Write(_T("LastPlayer"), wxString(player
)); // Without wxString tmp, thinks it's bool in VC++
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
));