Scid  4.7.0
indexentry.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 1999-2002 Shane Hudson
3 * Copyright (c) 2006-2009 Pascal Georges
4 * Copyright (C) 2014-2017 Fulvio Benini
5 
6 * This file is part of Scid (Shane's Chess Information Database).
7 *
8 * Scid is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation.
11 *
12 * Scid is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Scid. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifndef SCID_INDEXENTRY_H
22 #define SCID_INDEXENTRY_H
23 
24 #include "common.h"
25 #include "date.h"
26 #include "matsig.h"
27 #include "namebase.h"
28 
29 // Length is encoded as 17bits uint
30 #define MAX_GAME_LENGTH 131072
31 
32 // HPSIG_SIZE = size of HomePawnData array in an IndexEntry.
33 // It is nine bytes: the first byte contains the number of valid entries
34 // in the array, and the next 8 bytes contain up to 16 half-byte entries.
35 const uint HPSIG_SIZE = 9;
36 
37 const eloT MAX_ELO = 4000; // Since we store Elo Ratings in 12 bits
38 
39 const byte CUSTOM_FLAG_MASK[] = { 1, 1 << 1, 1 << 2, 1 << 3, 1 << 4, 1 << 5 };
40 
41 // Total on-disk size per index entry: currently 47 bytes.
44 
45 
46 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
47 // Class IndexEntry: one of these per game in the index file.
48 //
49 // It contains more than just the location of the game data in the main
50 // data file. For fast searching, it also store some other important
51 // values: players, event, site, date, result, eco, gamelength.
52 //
53 // It takes 56 bytes.
54 class IndexEntry {
55  uint64_t offset_ : 46; // Start of gamefile record for this game.
56  uint64_t gameDataSize_ : 18; // Length of gamefile record for this game.
57 
58  uint64_t storedLineCode_ : 8;
59  uint64_t whiteID_ : 28;
60  uint64_t blackID_ : 28;
61 
62  uint32_t eventID_ : 28;
63  uint32_t whiteEloType_ : 4;
64 
65  uint32_t siteID_ : 28;
66  uint32_t blackEloType_ : 4;
67 
68  uint32_t roundID_ : 28;
69  uint32_t result_ : 4;
70 
71  uint32_t whiteElo_ : 12;
72  uint32_t date_ : 20;
73 
74  uint32_t blackElo_ : 12;
75  uint32_t eventDate_ : 20;
76 
77  uint32_t numHalfMoves_ : 10;
78  uint32_t flags_ : 22;
79 
80  uint32_t finalMatSig_ : 24; // material of the final position in the game
81  uint32_t nVariations_ : 4;
82  uint32_t nComments_ : 4;
83 
84  uint16_t ECOcode_;
85 
86  uint8_t nNags_ : 4;
87 
88  byte HomePawnData [HPSIG_SIZE]; // homePawnSig data.
89 
90 public:
91  void Init() { std::memset(this, 0, sizeof(IndexEntry)); }
92 
93  template <class T> errorT Write (T* file, versionT version) const;
94 
95  // get functions
96  uint64_t GetOffset() const { return offset_; }
97  uint32_t GetLength() const { return gameDataSize_; }
98  idNumberT GetWhite() const { return whiteID_; }
99  eloT GetWhiteElo() const { return whiteElo_; }
100  byte GetWhiteRatingType() const { return whiteEloType_; }
101  idNumberT GetBlack() const { return blackID_; }
102  eloT GetBlackElo() const { return blackElo_; }
103  byte GetBlackRatingType() const { return blackEloType_; }
104  idNumberT GetEvent() const { return eventID_; }
105  idNumberT GetSite() const { return siteID_; }
106  idNumberT GetRound() const { return roundID_; }
107  dateT GetDate() const { return date_; }
108  dateT GetEventDate() const { return eventDate_; }
109  resultT GetResult() const { return result_; }
110  uint GetVariationCount() const { return DecodeCount(nVariations_); }
111  uint GetCommentCount() const { return DecodeCount(nComments_); }
112  uint GetNagCount() const { return DecodeCount(nNags_); }
113  uint16_t GetNumHalfMoves() const { return numHalfMoves_; }
114  matSigT GetFinalMatSig() const { return finalMatSig_; }
115  byte GetStoredLineCode() const { return storedLineCode_; }
116  ecoT GetEcoCode() const { return ECOcode_; }
117  bool GetFlag(uint32_t mask) const { return (flags_ & mask) == mask; }
118  const byte* GetHomePawnData() const { return HomePawnData; }
119  byte* GetHomePawnData() { return HomePawnData; }
120 
121  // set functions, assert that the value is not truncated.
122  void SetOffset(uint64_t offset) {
123  offset_ = offset;
124  ASSERT(GetOffset() == offset);
125  }
126  void SetLength(size_t length) {
127  gameDataSize_ = length;
128  ASSERT(GetLength() == length);
129  }
130  void SetWhite(idNumberT id) {
131  whiteID_ = id;
132  ASSERT(GetWhite() == id);
133  }
134  void SetWhiteElo(eloT elo) {
135  whiteElo_ = elo;
136  ASSERT(GetWhiteElo() == elo);
137  }
139  whiteEloType_ = b;
140  ASSERT(GetWhiteRatingType() == b);
141  }
142  void SetBlack(idNumberT id) {
143  blackID_ = id;
144  ASSERT(GetBlack() == id);
145  }
146  void SetBlackElo(eloT elo) {
147  blackElo_ = elo;
148  ASSERT(GetBlackElo() == elo);
149  }
151  blackEloType_ = b;
152  ASSERT(GetBlackRatingType() == b);
153  }
154  void SetEvent(idNumberT id) {
155  eventID_ = id;
156  ASSERT(GetEvent() == id);
157  }
158  void SetSite(idNumberT id) {
159  siteID_ = id;
160  ASSERT(GetSite() == id);
161  }
162  void SetRound(idNumberT id) {
163  roundID_ = id;
164  ASSERT(GetRound() == id);
165  }
167  date_ = date;
168  ASSERT(GetDate() == date);
169  }
170  void SetEventDate(dateT edate) {
171  eventDate_ = edate;
172  ASSERT(GetEventDate() == edate);
173  }
174  void SetResult(resultT res) {
175  result_ = res;
176  ASSERT(GetResult() == res);
177  }
178  void SetVariationCount(unsigned x) { nVariations_ = EncodeCount(x); }
179  void SetCommentCount(unsigned x) { nComments_ = EncodeCount(x); }
180  void SetNagCount(unsigned x) { nNags_ = EncodeCount(x); }
181  void SetRawVariationCount(unsigned x) {
182  nVariations_ = x;
183  ASSERT(x == nVariations_);
184  }
185  void SetRawCommentCount(unsigned x) {
186  nComments_ = x;
187  ASSERT(x == nComments_);
188  }
189  void SetRawNagCount(unsigned x) {
190  nNags_ = x;
191  ASSERT(x == nNags_);
192  }
194  numHalfMoves_ = b;
195  ASSERT(GetNumHalfMoves() == b);
196  }
198  finalMatSig_ = ms;
199  ASSERT(GetFinalMatSig() == ms);
200  }
202  storedLineCode_ = b;
203  ASSERT(GetStoredLineCode() == b);
204  }
205  void SetEcoCode(ecoT eco) {
206  ECOcode_ = eco;
207  ASSERT(GetEcoCode() == eco);
208  }
209  void SetFlag(uint32_t flagMask, bool set) {
210  if (set)
211  flags_ |= flagMask;
212  else
213  flags_ &= ~flagMask;
214  }
215 
216  // Handy functions that do not directly access member vars.
217  uint GetYear () const { return date_GetYear (GetDate()); }
218  uint GetMonth() const { return date_GetMonth (GetDate()); }
219  uint GetDay () const { return date_GetDay (GetDate()); }
220 
221  void SetPlayer(colorT col, idNumberT id) {
222  return (col == BLACK) ? SetBlack(id) : SetWhite(id);
223  }
224 
225  const char* GetWhiteName (const NameBase* nb) const {
226  return nb->GetName (NAME_PLAYER, GetWhite());
227  }
228  const char* GetBlackName (const NameBase* nb) const {
229  return nb->GetName (NAME_PLAYER, GetBlack());
230  }
231  const char* GetEventName (const NameBase* nb) const {
232  return nb->GetName (NAME_EVENT, GetEvent());
233  }
234  const char* GetSiteName (const NameBase* nb) const {
235  return nb->GetName (NAME_SITE, GetSite());
236  }
237  const char* GetRoundName (const NameBase* nb) const {
238  return nb->GetName (NAME_ROUND, GetRound());
239  }
240  eloT GetWhiteElo (const NameBase* nb) const {
241  eloT r = GetWhiteElo();
242  if (r == 0 && nb != 0) return nb->GetElo (GetWhite());
243  return r;
244  }
245  eloT GetBlackElo (const NameBase* nb) const {
246  eloT r = GetBlackElo();
247  if (r == 0 && nb != 0) return nb->GetElo (GetBlack());
248  return r;
249  }
250  byte GetRating(const NameBase* nb) const;
251 
252  bool GetStartFlag () const { return GetFlag(1 << IDX_FLAG_START); }
253  bool GetPromotionsFlag () const { return GetFlag(1 << IDX_FLAG_PROMO); }
254  bool GetUnderPromoFlag() const { return GetFlag(1 << IDX_FLAG_UPROMO); }
255  bool GetCommentsFlag () const { return (GetCommentCount() > 0); }
256  bool GetVariationsFlag () const { return (GetVariationCount() > 0); }
257  bool GetNagsFlag () const { return (GetNagCount() > 0); }
258  bool GetDeleteFlag () const { return GetFlag(1 << IDX_FLAG_DELETE); }
259 
260  static uint CharToFlag (char ch);
261  static uint32_t CharToFlagMask (char flag);
262  static uint32_t StrToFlagMask (const char* flags);
263  uint GetFlagStr(char* dest, const char* flags) const;
264 
265  void SetStartFlag (bool b) { SetFlag(1 << IDX_FLAG_START, b); }
266  void SetPromotionsFlag (bool b) { SetFlag(1 << IDX_FLAG_PROMO, b); }
267  void SetUnderPromoFlag (bool b) { SetFlag(1 << IDX_FLAG_UPROMO, b); }
268  void SetDeleteFlag (bool b) { SetFlag(1 << IDX_FLAG_DELETE, b); }
269  void clearFlags() { return SetFlag(IDX_MASK_ALLFLAGS, false); }
270 
271  enum {
272  // IndexEntry Flag types:
273  IDX_FLAG_START = 0, // Game has own start position.
274  IDX_FLAG_PROMO = 1, // Game contains promotion(s).
275  IDX_FLAG_UPROMO = 2, // Game contains promotion(s).
276  IDX_FLAG_DELETE = 3, // Game marked for deletion.
277  IDX_FLAG_WHITE_OP = 4, // White openings flag.
278  IDX_FLAG_BLACK_OP = 5, // Black openings flag.
279  IDX_FLAG_MIDDLEGAME = 6, // Middlegames flag.
280  IDX_FLAG_ENDGAME = 7, // Endgames flag.
281  IDX_FLAG_NOVELTY = 8, // Novelty flag.
282  IDX_FLAG_PAWN = 9, // Pawn structure flag.
283  IDX_FLAG_TACTICS = 10, // Tactics flag.
284  IDX_FLAG_KSIDE = 11, // Kingside play flag.
285  IDX_FLAG_QSIDE = 12, // Queenside play flag.
286  IDX_FLAG_BRILLIANCY = 13, // Brilliancy or good play.
287  IDX_FLAG_BLUNDER = 14, // Blunder or bad play.
288  IDX_FLAG_USER = 15, // User-defined flag.
289  IDX_FLAG_CUSTOM1 = 16, // Custom flag.
290  IDX_FLAG_CUSTOM2 = 17, // Custom flag.
291  IDX_FLAG_CUSTOM3 = 18, // Custom flag.
292  IDX_FLAG_CUSTOM4 = 19, // Custom flag.
293  IDX_FLAG_CUSTOM5 = 20, // Custom flag.
294  IDX_FLAG_CUSTOM6 = 21, // Custom flag.
296  };
297  static const uint32_t IDX_MASK_ALLFLAGS = 0xFFFFFFFF;
298 
299 private:
300  static uint EncodeCount (uint x) {
301  if (x <= 10) { return x; }
302  if (x <= 12) { return 10; }
303  if (x <= 17) { return 11; } // 11 indicates 15 (13-17)
304  if (x <= 24) { return 12; } // 12 indicates 20 (18-24)
305  if (x <= 34) { return 13; } // 13 indicates 30 (25-34)
306  if (x <= 44) { return 14; } // 14 indicates 40 (35-44)
307  return 15; // 15 indicates 50 or more
308  }
309  static uint DecodeCount (uint x) {
310  static uint countCodes[16] = {0,1,2,3,4,5,6,7,8,9,10,15,20,30,40,50};
311  return countCodes[x & 15];
312  }
313 };
314 
315 
316 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
317 // IndexEntry::Write():
318 // Writes a single index entry to an open index file.
319 // INDEX_ENTRY_SIZE must be updated
320 template <class T> errorT IndexEntry::Write(T* file, versionT version) const {
321  if (version < 400) // Cannot write old-version index files:
322  return ERROR_FileVersion;
323 
324  char buf[INDEX_ENTRY_SIZE];
325  char* buf_it = buf;
326  auto WriteOneByte = [&buf_it](uint8_t v) { *buf_it++ = v; };
327  auto WriteTwoBytes = [&WriteOneByte](uint16_t v) {
328  WriteOneByte(static_cast<uint8_t>(v >> 8));
329  WriteOneByte(static_cast<uint8_t>(v));
330  };
331  auto WriteFourBytes = [&WriteTwoBytes](uint32_t v) {
332  WriteTwoBytes(static_cast<uint16_t>(v >> 16));
333  WriteTwoBytes(static_cast<uint16_t>(v));
334  };
335 
336  const IndexEntry* ie = this;
337 
338  ASSERT(ie->GetOffset() < (1ULL << 32));
339  WriteFourBytes(static_cast<uint32_t>(ie->GetOffset()));
340 
341  ASSERT(ie->GetLength() < (1ULL << 17));
342  WriteTwoBytes(static_cast<uint16_t>(ie->GetLength()));
343  uint8_t len_flags = static_cast<uint8_t>(ie->GetLength() >> 9) & 0x80;
344  len_flags |= static_cast<uint8_t>(ie->flags_ >> 16) & 0x3F;
345  WriteOneByte(len_flags);
346  WriteTwoBytes(static_cast<uint16_t>(ie->flags_));
347 
348  // WhiteID and BlackID are 20-bit values, EventID and SiteID are
349  // 19-bit values, and RoundID is an 18-bit value.
350  // WhiteID high 4 bits = bits 4-7 of WhiteBlack_High.
351  // BlackID high 4 bits = bits 0-3 of WhiteBlack_High.
352  // EventID high 3 bits = bits 5-7 of EventSiteRnd_high.
353  // SiteID high 3 bits = bits 2-4 of EventSiteRnd_high.
354  // RoundID high 2 bits = bits 0-1 of EventSiteRnd_high.
355  ASSERT(std::max(ie->GetWhite(), ie->GetBlack()) < (1ULL << 20));
356  uint32_t WhiteID_Low = ie->GetWhite();
357  uint32_t BlackID_Low = ie->GetBlack();
358  uint32_t WhiteBlack_High = (WhiteID_Low & 0x0F0000) >> 12;
359  WhiteBlack_High |= (BlackID_Low & 0x0F0000) >> 16;
360  WriteOneByte(static_cast<uint8_t>(WhiteBlack_High));
361  WriteTwoBytes(static_cast<uint16_t>(WhiteID_Low));
362  WriteTwoBytes(static_cast<uint16_t>(BlackID_Low));
363 
364  ASSERT(std::max(ie->GetEvent(), ie->GetSite()) < (1ULL << 19));
365  ASSERT(ie->GetRound() < (1ULL << 18));
366  uint32_t EventID_Low = ie->GetEvent();
367  uint32_t SiteID_Low = ie->GetSite();
368  uint32_t RoundID_Low = ie->GetRound();
369  uint32_t EventSiteRnd_High = (EventID_Low & 0x070000) >> 11;
370  EventSiteRnd_High |= (SiteID_Low & 0x070000) >> 14;
371  EventSiteRnd_High |= (RoundID_Low & 0x030000) >> 16;
372  WriteOneByte(static_cast<uint8_t>(EventSiteRnd_High));
373  WriteTwoBytes(static_cast<uint16_t>(EventID_Low));
374  WriteTwoBytes(static_cast<uint16_t>(SiteID_Low));
375  WriteTwoBytes(static_cast<uint16_t>(RoundID_Low));
376 
377  uint16_t varCounts = ie->nVariations_ & 0x0F;
378  varCounts |= static_cast<uint16_t>(ie->nComments_ & 0x0F) << 4;
379  varCounts |= static_cast<uint16_t>(ie->nNags_ & 0x0F) << 8;
380  varCounts |= static_cast<uint16_t>(ie->GetResult() & 0x0F) << 12;
381  WriteTwoBytes(varCounts);
382 
383  WriteTwoBytes(ie->GetEcoCode());
384 
385  // Due to a compact encoding format, the EventDate
386  // must be within a few years of the Date.
387  uint32_t date = ie->GetDate() & 0xFFFFF;
388  uint32_t edate = ie->GetEventDate();
389  uint32_t eyear = date_GetYear(edate);
390  uint32_t dyear = date_GetYear(date);
391  if ((eyear + 3) < dyear || eyear > (dyear + 3)) {
392  edate = ZERO_DATE;
393  } else {
394  eyear = (eyear + 4 - dyear) & 7;
395  edate = (eyear << 9) | (date_GetMonth(edate) << 5) | date_GetDay(edate);
396  }
397  WriteFourBytes((edate << 20) | date);
398 
399  // Elo ratings and rating types: 2 bytes each.
400  uint16_t wElo = std::min(MAX_ELO, ie->GetWhiteElo());
401  wElo |= static_cast<uint16_t>(ie->GetWhiteRatingType()) << 12;
402  uint16_t bElo = std::min(MAX_ELO, ie->GetBlackElo());
403  bElo |= static_cast<uint16_t>(ie->GetBlackRatingType()) << 12;
404  WriteTwoBytes(wElo);
405  WriteTwoBytes(bElo);
406 
407  ASSERT(ie->GetFinalMatSig() < (1ULL << 24));
408  ASSERT(ie->GetStoredLineCode() < (1ULL << 8));
409  uint32_t FinalMatSig = ie->GetFinalMatSig();
410  FinalMatSig |= static_cast<uint32_t>(ie->GetStoredLineCode()) << 24;
411  WriteFourBytes(FinalMatSig);
412 
413  // The first byte of HomePawnData has high bits of the NumHalfMoves
414  // counter in its top two bits:
415  uint16_t nMoves = ie->GetNumHalfMoves();
416  ASSERT(nMoves < (1ULL << 10));
417  WriteOneByte(static_cast<uint8_t>(nMoves));
418  uint8_t pawnData0 = static_cast<uint8_t>(nMoves >> 8) << 6;
419 
420  // Write the 9-byte homePawnData array:
421  const byte* pb = ie->GetHomePawnData();
422  pawnData0 |= *pb & 0x3F;
423  WriteOneByte(pawnData0);
424  std::copy(pb + 1, pb + HPSIG_SIZE, buf_it);
425 
426  return file->sputn(buf, INDEX_ENTRY_SIZE) == INDEX_ENTRY_SIZE
427  ? OK
428  : ERROR_FileWrite;
429 }
430 
431 inline byte IndexEntry::GetRating(const NameBase* nb) const {
432  eloT welo = GetWhiteElo();
433  eloT belo = GetBlackElo();
434  if (welo == 0) { welo = nb->GetElo (GetWhite()); }
435  if (belo == 0) { belo = nb->GetElo (GetBlack()); }
436  int rating = static_cast<int>(welo + belo) / 140;
437 
438  // Bonus for comments or Nags
439  if (GetCommentCount() > 2 || GetNagCount() > 2) {
440  if (rating < 21) { // Missing elo
441  rating = 40;
442  } else {
443  rating += 6;
444  }
445  }
446 
447  // Early draw penalty
448  if (GetResult() == RESULT_Draw) {
449  uint moves = GetNumHalfMoves();
450  if (moves < 80) {
451  rating -= 3;
452  if (moves < 60) {
453  rating -= 2;
454  if (moves < 40) rating -= 2;
455  }
456  }
457  }
458 
459  if (rating < 0) return 0;
460  else return static_cast<byte> (rating);
461 }
462 
463 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
464 // IndexEntry::CharToFlag():
465 // Returns the flag number corresponding to the given character.
466 inline uint
468 {
469  uint flag = 0;
470  switch (toupper(ch)) {
471  case 'D': flag = IDX_FLAG_DELETE; break;
472  case 'W': flag = IDX_FLAG_WHITE_OP; break;
473  case 'B': flag = IDX_FLAG_BLACK_OP; break;
474  case 'M': flag = IDX_FLAG_MIDDLEGAME; break;
475  case 'E': flag = IDX_FLAG_ENDGAME; break;
476  case 'N': flag = IDX_FLAG_NOVELTY; break;
477  case 'P': flag = IDX_FLAG_PAWN; break;
478  case 'T': flag = IDX_FLAG_TACTICS; break;
479  case 'K': flag = IDX_FLAG_KSIDE; break;
480  case 'Q': flag = IDX_FLAG_QSIDE; break;
481  case '!': flag = IDX_FLAG_BRILLIANCY; break;
482  case '?': flag = IDX_FLAG_BLUNDER; break;
483  case 'U': flag = IDX_FLAG_USER; break;
484  case '1': flag = IDX_FLAG_CUSTOM1; break;
485  case '2': flag = IDX_FLAG_CUSTOM2; break;
486  case '3': flag = IDX_FLAG_CUSTOM3; break;
487  case '4': flag = IDX_FLAG_CUSTOM4; break;
488  case '5': flag = IDX_FLAG_CUSTOM5; break;
489  case '6': flag = IDX_FLAG_CUSTOM6; break;
490  }
491  return flag;
492 }
493 
494 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
495 // IndexEntry::CharToFlagMask():
496 // Transform a char in a mask that can be used with GetFlag() and SetFlag()
497 inline uint32_t IndexEntry::CharToFlagMask(char flag)
498 {
499  switch (toupper(flag)) {
500  case 'S': return 1 << IDX_FLAG_START;
501  case 'X': return 1 << IDX_FLAG_PROMO;
502  case 'Y': return 1 << IDX_FLAG_UPROMO;
503  case 'D': return 1 << IDX_FLAG_DELETE;
504  case 'W': return 1 << IDX_FLAG_WHITE_OP;
505  case 'B': return 1 << IDX_FLAG_BLACK_OP;
506  case 'M': return 1 << IDX_FLAG_MIDDLEGAME;
507  case 'E': return 1 << IDX_FLAG_ENDGAME;
508  case 'N': return 1 << IDX_FLAG_NOVELTY;
509  case 'P': return 1 << IDX_FLAG_PAWN;
510  case 'T': return 1 << IDX_FLAG_TACTICS;
511  case 'K': return 1 << IDX_FLAG_KSIDE;
512  case 'Q': return 1 << IDX_FLAG_QSIDE;
513  case '!': return 1 << IDX_FLAG_BRILLIANCY;
514  case '?': return 1 << IDX_FLAG_BLUNDER;
515  case 'U': return 1 << IDX_FLAG_USER;
516  case '1': return 1 << IDX_FLAG_CUSTOM1;
517  case '2': return 1 << IDX_FLAG_CUSTOM2;
518  case '3': return 1 << IDX_FLAG_CUSTOM3;
519  case '4': return 1 << IDX_FLAG_CUSTOM4;
520  case '5': return 1 << IDX_FLAG_CUSTOM5;
521  case '6': return 1 << IDX_FLAG_CUSTOM6;
522  }
523 
524  ASSERT(0);
525  return 0;
526 }
527 
528 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
529 // IndexEntry::StrToFlagMask():
530 // Transform a string in a mask that can be used with GetFlag() and SetFlag()
531 inline uint32_t IndexEntry::StrToFlagMask(const char* flags)
532 {
533  if (flags == 0) return 0;
534 
535  uint32_t res = 0;
536  while (*flags != 0) {
537  res |= IndexEntry::CharToFlagMask(*(flags++));
538  }
539  return res;
540 }
541 
542 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
543 // IndexEntry::GetFlagStr():
544 // Fills in the provided flag string with information on the
545 // user-settable flags set for this game.
546 // Returns the number of specified flags that are turned on.
547 inline uint
548 IndexEntry::GetFlagStr(char* dest, const char* flags) const
549 {
550  if (flags == NULL) { flags = "DWBMENPTKQ!?U123456"; }
551  uint count = 0;
552  while (*flags != 0) {
553  uint32_t mask = CharToFlagMask(*flags);
554  ASSERT(mask != 0);
555  if (GetFlag(mask)) {
556  *dest++ = *flags;
557  count++;
558  }
559  flags++;
560  }
561  *dest = 0;
562  return count;
563 }
564 
565 #endif
void SetNumHalfMoves(ushort b)
Definition: indexentry.h:193
void SetBlack(idNumberT id)
Definition: indexentry.h:142
unsigned char byte
Definition: common.h:89
uint date_GetYear(dateT date)
Definition: date.h:52
Definition: indexentry.h:284
Definition: indexentry.h:277
void SetCommentCount(unsigned x)
Definition: indexentry.h:179
byte resultT
Definition: common.h:187
static const uint32_t IDX_MASK_ALLFLAGS
Definition: indexentry.h:297
bool GetStartFlag() const
Definition: indexentry.h:252
dateT GetDate() const
Definition: indexentry.h:107
Definition: indexentry.h:291
const uint INDEX_ENTRY_SIZE
Definition: indexentry.h:42
Definition: indexentry.h:289
const char * GetBlackName(const NameBase *nb) const
Definition: indexentry.h:228
const byte * GetHomePawnData() const
Definition: indexentry.h:118
Definition: indexentry.h:276
Definition: indexentry.h:282
const errorT OK
Definition: error.h:23
Definition: indexentry.h:283
unsigned short versionT
Definition: common.h:38
const uint HPSIG_SIZE
Definition: indexentry.h:35
resultT GetResult() const
Definition: indexentry.h:109
static uint32_t StrToFlagMask(const char *flags)
Definition: indexentry.h:531
uint max(int a, int b)
Definition: crosstab.cpp:237
void SetDeleteFlag(bool b)
Definition: indexentry.h:268
Definition: indexentry.h:295
idNumberT GetSite() const
Definition: indexentry.h:105
const errorT ERROR_FileWrite
Definition: error.h:32
void SetBlackRatingType(byte b)
Definition: indexentry.h:150
uint dateT
Definition: common.h:147
#define ASSERT(f)
Definition: common.h:59
const char * GetWhiteName(const NameBase *nb) const
Definition: indexentry.h:225
const char * GetEventName(const NameBase *nb) const
Definition: indexentry.h:231
void SetStartFlag(bool b)
Definition: indexentry.h:265
uint32_t matSigT
Definition: matsig.h:25
idNumberT GetEvent() const
Definition: indexentry.h:104
void SetWhite(idNumberT id)
Definition: indexentry.h:130
const colorT BLACK
Definition: common.h:208
void SetUnderPromoFlag(bool b)
Definition: indexentry.h:267
Definition: indexentry.h:294
Definition: indexentry.h:292
Definition: indexentry.h:275
const eloT MAX_ELO
Definition: indexentry.h:37
void SetRound(idNumberT id)
Definition: indexentry.h:162
static uint CharToFlag(char ch)
Definition: indexentry.h:467
matSigT GetFinalMatSig() const
Definition: indexentry.h:114
uint16_t GetNumHalfMoves() const
Definition: indexentry.h:113
const char * GetName(nameT nt, idNumberT id) const
Retrieve a name.
Definition: namebase.h:162
void SetNagCount(unsigned x)
Definition: indexentry.h:180
void SetStoredLineCode(byte b)
Definition: indexentry.h:201
void SetEcoCode(ecoT eco)
Definition: indexentry.h:205
const resultT RESULT_Draw
Definition: common.h:192
uint32_t idNumberT
Definition: common.h:152
const dateT ZERO_DATE
Definition: date.h:35
eloT GetWhiteElo() const
Definition: indexentry.h:99
static uint32_t CharToFlagMask(char flag)
Definition: indexentry.h:497
uint GetNagCount() const
Definition: indexentry.h:112
const byte CUSTOM_FLAG_MASK[]
Definition: indexentry.h:39
Definition: indexentry.h:293
Definition: indexentry.h:288
bool GetFlag(uint32_t mask) const
Definition: indexentry.h:117
This class stores the database&#39;s names (players, events, sites and rounds).
Definition: namebase.h:33
uint GetFlagStr(char *dest, const char *flags) const
Definition: indexentry.h:548
void SetRawNagCount(unsigned x)
Definition: indexentry.h:189
Definition: indexentry.h:280
void SetRawCommentCount(unsigned x)
Definition: indexentry.h:185
void SetDate(dateT date)
Definition: indexentry.h:166
uint32_t uint
Definition: common.h:91
idNumberT GetRound() const
Definition: indexentry.h:106
idNumberT GetWhite() const
Definition: indexentry.h:98
void SetSite(idNumberT id)
Definition: indexentry.h:158
bool GetDeleteFlag() const
Definition: indexentry.h:258
Definition: indexentry.h:281
void Init()
Definition: indexentry.h:91
void SetLength(size_t length)
Definition: indexentry.h:126
void SetVariationCount(unsigned x)
Definition: indexentry.h:178
uint GetDay() const
Definition: indexentry.h:219
eloT GetWhiteElo(const NameBase *nb) const
Definition: indexentry.h:240
byte GetStoredLineCode() const
Definition: indexentry.h:115
eloT GetBlackElo() const
Definition: indexentry.h:102
errorT Write(T *file, versionT version) const
Definition: indexentry.h:320
ushort eloT
Definition: common.h:164
uint GetCommentCount() const
Definition: indexentry.h:111
byte GetRating(const NameBase *nb) const
Definition: indexentry.h:431
byte GetWhiteRatingType() const
Definition: indexentry.h:100
void SetEventDate(dateT edate)
Definition: indexentry.h:170
ushort ecoT
Definition: common.h:165
ecoT GetEcoCode() const
Definition: indexentry.h:116
Definition: indexentry.h:287
idNumberT GetBlack() const
Definition: indexentry.h:101
unsigned short errorT
Definition: error.h:20
const errorT ERROR_FileVersion
Definition: error.h:39
void SetPromotionsFlag(bool b)
Definition: indexentry.h:266
eloT GetElo(idNumberT id) const
Definition: namebase.h:128
uint16_t ushort
Definition: common.h:90
void SetWhiteElo(eloT elo)
Definition: indexentry.h:134
bool GetVariationsFlag() const
Definition: indexentry.h:256
void SetFinalMatSig(matSigT ms)
Definition: indexentry.h:197
void SetResult(resultT res)
Definition: indexentry.h:174
uint GetMonth() const
Definition: indexentry.h:218
const char * GetSiteName(const NameBase *nb) const
Definition: indexentry.h:234
bool GetCommentsFlag() const
Definition: indexentry.h:255
void SetPlayer(colorT col, idNumberT id)
Definition: indexentry.h:221
uint GetYear() const
Definition: indexentry.h:217
void SetEvent(idNumberT id)
Definition: indexentry.h:154
Definition: indexentry.h:286
Definition: indexentry.h:274
byte colorT
Definition: common.h:104
void clearFlags()
Definition: indexentry.h:269
void SetFlag(uint32_t flagMask, bool set)
Definition: indexentry.h:209
uint32_t GetLength() const
Definition: indexentry.h:97
bool GetUnderPromoFlag() const
Definition: indexentry.h:254
void SetBlackElo(eloT elo)
Definition: indexentry.h:146
Definition: indexentry.h:290
const uint OLD_INDEX_ENTRY_SIZE
Definition: indexentry.h:43
Definition: indexentry.h:278
Definition: indexentry.h:285
byte GetBlackRatingType() const
Definition: indexentry.h:103
uint64_t GetOffset() const
Definition: indexentry.h:96
eloT GetBlackElo(const NameBase *nb) const
Definition: indexentry.h:245
bool GetNagsFlag() const
Definition: indexentry.h:257
const char * GetRoundName(const NameBase *nb) const
Definition: indexentry.h:237
void SetWhiteRatingType(byte b)
Definition: indexentry.h:138
void SetOffset(uint64_t offset)
Definition: indexentry.h:122
Definition: indexentry.h:273
uint date_GetMonth(dateT date)
Definition: date.h:61
void SetRawVariationCount(unsigned x)
Definition: indexentry.h:181
bool GetPromotionsFlag() const
Definition: indexentry.h:253
Definition: indexentry.h:54
byte * GetHomePawnData()
Definition: indexentry.h:119
uint GetVariationCount() const
Definition: indexentry.h:110
Definition: indexentry.h:279
dateT GetEventDate() const
Definition: indexentry.h:108
uint date_GetDay(dateT date)
Definition: date.h:70