中国象棋控件 v2.0.13
载入中...
搜索中...
未找到
Pgn.cpp
1// 作者:康林 <kl222@126.com>
2
3#include "Pgn.h"
4#include <memory.h>
5
6CPGN::CPGN()
7 : m_Game("Chinese Chess"),
8 m_Event("?"),
9 m_Site("?"),
10 m_Date(time(nullptr)),
11 m_Round("?"),
12 m_Red("?"),
13 m_Black("?"),
14 m_Result("*"),
15 m_Format("Chinese"),
16 m_pSteps(nullptr)
17{}
18
19std::string CPGN::GetTag(const std::string &szTag)
20{
21 const auto iter = m_Tags.find(szTag);
22 if (iter != m_Tags.end())
23 return iter->second;
24 // Is it a tag from the standard tag roster?
25 if (szTag == "Event")
26 return GetEvent();
27 if (szTag == "Site")
28 return GetSite();
29 if (szTag == "Date")
30 {
31 return dateToString(GetDate());
32 }
33 if (szTag == "Round")
34 return GetRound();
35 if (szTag == "Red")
36 return GetRed();
37 if (szTag == "Black")
38 return GetBlack();
39 if (szTag == "Result")
40 return GetResult();
41 if(szTag == "Fen")
42 return GetFen();
43 if(szTag == "Format")
44 return GetFormat();
45
46 // Tag was not found.
47 return std::string();
48}
49
50int CPGN::SetTag(const std::string &szTag, const std::string &szValue)
51{
52 if (szTag == "Event")
53 SetEvent(szValue.c_str());
54 else if (szTag == "Site")
55 SetSite(szValue.c_str());
56 else if (szTag == "Date")
57 parseDate(szValue);
58 else if (szTag == "Round")
59 SetRound(szValue.c_str());
60 else if (szTag == "Red")
61 SetRed(szValue.c_str());
62 else if (szTag == "Black")
63 SetBlack(szValue.c_str());
64 else if (szTag == "Result")
65 SetResult(szValue.c_str());
66 else if(szTag == "Fen")
67 SetFen(szValue.c_str());
68 else if(szTag == "Format")
69 SetFormat(szValue.c_str());
70 else
71 m_Tags[szTag] = szValue;
72 return 0;
73}
74
75std::string CPGN::toString() const
76{
77 std::string pgn;
78
79 pgn = "[Game \"" + m_Game + "\"]\n"
80 + "[Event \"" + GetEvent() + "\"]\n"
81 + "[Site \"" + GetSite() + "\"]\n";
82
83 pgn += "[Date \"";
84 pgn += dateToString(GetDate());
85 pgn += "\"]\n";
86
87 pgn += "[Round \"" + GetRound() + "\"]\n"
88 + "[Red \"" + GetRed() + "\"]\n"
89 + "[Black \"" + GetBlack() + "\"]\n"
90 + "[Result \"" + GetResult() + "\"]\n"
91 + "[Fen \"" + GetFen() + "\"]\n"
92 + "[Format \"" + GetFormat() + "\"]\n";
93 for(const auto& item : m_Tags)
94 {
95 pgn += "[" + item.first + " \"" + item.second + "\"]\n";
96 } // for
97 pgn += "\n";
98 pgn += m_pSteps->toString();
99 pgn += "\n" + GetResult();
100 return pgn;
101}
102
103int CPGN::ParseString(const std::string szPgn)
104{
105 return 0;
106}
107
108const std::string &CPGN::GetEvent() const
109{
110 return m_Event;
111}
112
113int CPGN::SetEvent(const char *pEvent)
114{
115 m_Event = pEvent;
116 return 0;
117}
118
119const std::string& CPGN::GetSite() const
120{
121 return m_Site;
122}
123
124int CPGN::SetSite(const char *pSite)
125{
126 m_Site = pSite;
127 return 0;
128}
129
130const time_t& CPGN::GetDate() const
131{
132 return m_Date;
133}
134
135int CPGN::parseDate(const std::string &dateText)
136{
137 struct tm tm;
138
139 memset(&tm, 0, sizeof (struct tm));
140 sscanf(dateText.c_str(), "%d.%d.%d" ,
141 &(tm.tm_year),
142 &(tm.tm_mon),
143 &(tm.tm_mday));
144 tm.tm_year -= 1900;
145 tm.tm_mon --;
146 m_Date = mktime(&tm);
147 return 0;
148}
149
150std::string CPGN::dateToString(const time_t &t) const
151{
152 struct tm* t1 = localtime(&t);
153 char buf[30];
154 strftime(buf, 30, "%Y.%m.%d", t1);
155 return buf;
156}
157
158int CPGN::SetDate(const time_t &t)
159{
160 m_Date = t;
161 return 0;
162}
163
164const std::string& CPGN::GetRound() const
165{
166 return m_Round;
167}
168
169int CPGN::SetRound(const char *pRound)
170{
171 m_Round = pRound;
172 return 0;
173}
174
175const std::string& CPGN::GetRed() const
176{
177 return m_Red;
178}
179
180int CPGN::SetRed(const char *pRed)
181{
182 if(nullptr == pRed)
183 m_Red = "?";
184 else
185 m_Red = pRed;
186 return 0;
187}
188
189const std::string& CPGN::GetBlack() const
190{
191 return m_Black;
192}
193
194int CPGN::SetBlack(const char *pBlack)
195{
196 if(nullptr == pBlack)
197 m_Black = "?";
198 else
199 m_Black = pBlack;
200 return 0;
201}
202
203const std::string& CPGN::GetResult() const
204{
205 return m_Result;
206}
207
208int CPGN::SetResult(const char *pResult)
209{
210 m_Result = pResult;
211 return 0;
212}
213
214const std::string& CPGN::GetFen() const
215{
216 return m_szFEN;
217}
218
219int CPGN::SetFen(const char *pFen)
220{
221 m_szFEN = pFen;
222 return 0;
223}
224
225const std::string& CPGN::GetFormat() const
226{
227 return m_Format;
228}
229
230int CPGN::SetFormat(const char *pFormat)
231{
232 if(nullptr == pFormat)
233 m_Format = "Chinese";
234 else
235 m_Format = pFormat;
236 return 0;
237}
238
239int CPGN::SetSteps(std::shared_ptr<CChessSteps> steps)
240{
241 m_pSteps = steps;
242 return 0;
243}
std::string toString() const
Gets the game as PGN string.
Definition Pgn.cpp:75
const std::string & GetRed() const
Get the player(s) of the white pieces.
Definition Pgn.cpp:175
const std::string & GetRound() const
Get the playing round ordinal of the game.
Definition Pgn.cpp:164
const std::string & GetBlack() const
Get the player(s) of the black pieces.
Definition Pgn.cpp:189
const time_t & GetDate() const
Gets the date of the event.
Definition Pgn.cpp:130
int ParseString(const std::string szPgn)
解析 PGN
Definition Pgn.cpp:103
const std::string & GetResult() const
Gets the result of the game, if any.
Definition Pgn.cpp:203
const std::string & GetSite() const
Gets the location of the event.
Definition Pgn.cpp:119
const std::string & GetEvent() const
Gets the name of the tournament or match event.
Definition Pgn.cpp:108