]>
git.saurik.com Git - wxWidgets.git/blob - samples/forty/scorefil.cpp
aa53f764fe4a70c41b8781e6ec75c6e90e2bc593
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"
42 ScoreFile::ScoreFile(const char* appName
)
46 m_configFilename
<< "/usr/local/share/" << appName
<< ".scores";
47 if (access(m_configFilename
, F_OK
) == 0)
49 if (access(m_configFilename
, R_OK
| W_OK
) != 0)
51 // file is not r/w - use local file instead
52 m_configFilename
= wxFileConfig::GetLocalFileName(appName
);
57 int fd
= creat(m_configFilename
, 0666);
61 // failed to create file - use local file instead
62 m_configFilename
= wxFileConfig::GetLocalFileName(appName
);
66 // ensure created file has rw-rw-rw permissions
73 m_config
= new wxFileConfig( appName
, "" ); // only local
75 m_config
= new wxFileConfig( "",appName
); // only global
79 ScoreFile::~ScoreFile()
83 // ensure score file has rw-rw-rw permissions
84 // (wxFileConfig sets them to rw-------)
85 chmod(m_configFilename
, 0666);
90 void ScoreFile::GetPlayerList( wxArrayString
&list
)
92 m_config
->SetPath("/Players");
93 int length
= m_config
->GetNumberOfGroups();
95 if (length
<= 0) return;
99 if (m_config
->GetFirstGroup(player
, index
))
103 while (m_config
->GetNextGroup(player
, index
))
112 // Calculate an encrypted check number to prevent tampering with
114 long ScoreFile::CalcCheck(const char* name
, int p1
, int p2
, int p3
)
119 check
= (check
<< 1) ^ (long)*name
++;
120 check
= ((check
>> 23) ^ check
) & 0xFFFFFF;
122 check
= (check
<< 1) ^ (long)p1
;
123 check
= ((check
>> 23) ^ check
) & 0xFFFFFF;
124 check
= (check
<< 1) ^ (long)p2
;
125 check
= ((check
>> 23) ^ check
) & 0xFFFFFF;
126 check
= (check
<< 1) ^ (long)p3
;
127 check
= ((check
>> 23) ^ check
) & 0xFFFFFF;
131 wxString
ScoreFile::GetPreviousPlayer() const
134 m_config
->SetPath("/General");
135 m_config
->Read(&result
, "LastPlayer");
139 void ScoreFile::ReadPlayersScore(
146 long myWins
, myGames
, myScore
;
148 games
= wins
= score
= 0;
150 m_config
->SetPath("/Players");
151 m_config
->SetPath(player
);
152 if (m_config
->Read(&myScore
, "Score") &&
153 m_config
->Read(&myGames
, "Games") &&
154 m_config
->Read(&myWins
, "Wins") &&
155 m_config
->Read(&check
, "Check"))
157 if (check
!= CalcCheck(player
, myGames
, myWins
, myScore
))
159 wxMessageBox("Score file corrupted", "Warning",
160 wxOK
| wxICON_EXCLAMATION
);
169 WritePlayersScore(player
, wins
, games
, score
);
173 void ScoreFile::WritePlayersScore(const char* player
, int wins
, int games
, int score
)
177 m_config
->SetPath("/General");
178 m_config
->Write("LastPlayer", player
);
180 m_config
->SetPath("/Players");
181 m_config
->SetPath(player
);
182 m_config
->Write("Score", score
);
183 m_config
->Write("Games", games
);
184 m_config
->Write("Wins", wins
);
185 m_config
->Write("Check", CalcCheck(player
, games
, wins
, score
));