]>
git.saurik.com Git - wxWidgets.git/blob - samples/forty/scorefil.cpp
9025430eab1ee27250d673b9cce7c4db7d1ebaeb
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
72 m_configFilename
= wxFileConfig::GetGlobalFileName(appName
);
74 m_config
= new wxFileConfig(m_configFilename
);
77 ScoreFile::~ScoreFile()
81 // ensure score file has rw-rw-rw permissions
82 // (wxFileConfig sets them to rw-------)
83 chmod(m_configFilename
, 0666);
88 void ScoreFile::GetPlayerList(wxString
** list
, int& length
)
90 m_config
->SetPath("/Players");
91 length
= m_config
->GetNumberOfGroups();
99 *list
= new wxString
[length
];
103 if (m_config
->GetFirstGroup(player
, index
))
105 (*list
)[i
++] = player
;
106 while (m_config
->GetNextGroup(player
, index
))
108 (*list
)[i
++] = player
;
114 // Calculate an encrypted check number to prevent tampering with
116 long ScoreFile::CalcCheck(const char* name
, int p1
, int p2
, int p3
)
121 check
= (check
<< 1) ^ (long)*name
++;
122 check
= ((check
>> 23) ^ check
) & 0xFFFFFF;
124 check
= (check
<< 1) ^ (long)p1
;
125 check
= ((check
>> 23) ^ check
) & 0xFFFFFF;
126 check
= (check
<< 1) ^ (long)p2
;
127 check
= ((check
>> 23) ^ check
) & 0xFFFFFF;
128 check
= (check
<< 1) ^ (long)p3
;
129 check
= ((check
>> 23) ^ check
) & 0xFFFFFF;
133 wxString
ScoreFile::GetPreviousPlayer() const
136 m_config
->SetPath("/General");
137 m_config
->Read(&result
, "LastPlayer");
141 void ScoreFile::ReadPlayersScore(
148 long myWins
, myGames
, myScore
;
150 games
= wins
= score
= 0;
152 m_config
->SetPath("/Players");
153 m_config
->SetPath(player
);
154 if (m_config
->Read(&myScore
, "Score") &&
155 m_config
->Read(&myGames
, "Games") &&
156 m_config
->Read(&myWins
, "Wins") &&
157 m_config
->Read(&check
, "Check"))
159 if (check
!= CalcCheck(player
, myGames
, myWins
, myScore
))
161 wxMessageBox("Score file corrupted", "Warning",
162 wxOK
| wxICON_EXCLAMATION
);
171 WritePlayersScore(player
, wins
, games
, score
);
175 void ScoreFile::WritePlayersScore(const char* player
, int wins
, int games
, int score
)
179 m_config
->SetPath("/General");
180 m_config
->Write("LastPlayer", player
);
182 m_config
->SetPath("/Players");
183 m_config
->SetPath(player
);
184 m_config
->Write("Score", score
);
185 m_config
->Write("Games", games
);
186 m_config
->Write("Wins", wins
);
187 m_config
->Write("Check", CalcCheck(player
, games
, wins
, score
));