Scid  4.6.5
dbasepool.cpp
Go to the documentation of this file.
1 /*
2 # Copyright (C) 2015 Fulvio Benini
3 
4 * This file is part of Scid (Shane's Chess Information Database).
5 *
6 * Scid is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation.
9 *
10 * Scid is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with Scid. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include "dbasepool.h"
20 #include "ui.h"
21 #include "scidbase.h"
22 
23 
24 
25 //Current database
26 scidBaseT* db = NULL;
27 
28 namespace {
29 // MAX_BASES is the maximum number of databases that can be open,
30 // including the clipbase database.
31 const int MAX_BASES = 9;
32 const int CLIPBASE_NUM = MAX_BASES - 1;
33 
34 scidBaseT* dbList = NULL; // array of database slots.
35 }
36 
38  dbList = new scidBaseT[MAX_BASES];
39 
40  dbList[CLIPBASE_NUM].Open(ICodecDatabase::MEMORY, FMODE_Memory, "<clipbase>");
41  dbList[CLIPBASE_NUM].setExtraInfo("type", "2");
42 
43  DBasePool::switchCurrent(&(dbList[CLIPBASE_NUM]));
44 }
45 
47  ASSERT(dbList != NULL);
48  delete[] dbList;
49 }
50 
51 int DBasePool::find(const char* filename) {
52  for (int i = 0, n = MAX_BASES; i < n; i++) {
53  if (dbList[i].inUse && dbList[i].getFileName() == filename)
54  return i + 1;
55  }
56  return 0;
57 }
58 
59 scidBaseT* DBasePool::getBase(int baseHandle) {
60  if (baseHandle < 1 || baseHandle > MAX_BASES) return 0;
61  scidBaseT* res = &(dbList[baseHandle - 1]);
62  return res->inUse ? res : 0;
63 }
64 
66  return CLIPBASE_NUM + 1;
67 }
68 
70  for (int i = 0, n = MAX_BASES; i < n; i++) {
71  if (!dbList[i].inUse) { return &(dbList[i]); }
72  }
73  return 0;
74 }
75 
76 std::vector<int> DBasePool::getHandles() {
77  std::vector<int> res;
78  for (int i = 0, n = MAX_BASES; i < n; i++) {
79  if (dbList[i].inUse) res.push_back(i + 1);
80  }
81  return res;
82 }
83 
85  static int currentBase = 0;
86  if (dbase != 0) {
87  for (int i = 0; i < MAX_BASES; i++) {
88  if ((dbList + i) == dbase) {
89  currentBase = i;
90  db = dbase;
91  break;
92  }
93  }
94  }
95  return currentBase + 1;
96 }
97 
int switchCurrent(scidBaseT *dbase=0)
switchCurrent() - DEPRECATED.
Definition: dbasepool.cpp:84
scidBaseT * db
Definition: dbasepool.cpp:26
void init()
init() - initialize the pool of databases.
Definition: dbasepool.cpp:37
#define ASSERT(f)
Definition: common.h:67
scidBaseT * getBase(int baseHandle)
getBase() - get a database from the pool.
Definition: dbasepool.cpp:59
bool inUse
Definition: scidbase.h:282
int find(const char *filename)
find() - search for a database.
Definition: dbasepool.cpp:51
void closeAll()
closeAll() - close all the databases in the pool.
Definition: dbasepool.cpp:46
scidBaseT * getFreeSlot()
getFreeSlot() - search for a free database slot.
Definition: dbasepool.cpp:69
int getClipBase()
getClipBase() - return the handle of the clipbase
Definition: dbasepool.cpp:65
std::vector< int > getHandles()
getHandles() - get the handles of opened databases.
Definition: dbasepool.cpp:76
const int MAX_BASES
Definition: tkscid.cpp:58
errorT Open(ICodecDatabase::Codec dbtype, fileModeT mode, const char *filename=0, const Progress &progress=Progress())
Definition: scidbase.cpp:89