OpenASIP 2.2
Loading...
Searching...
No Matches
HDBRegistry.cc
Go to the documentation of this file.
1/*
2 Copyright (c) 2002-2009 Tampere University.
3
4 This file is part of TTA-Based Codesign Environment (TCE).
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23 */
24/**
25 * @file HDBRegistry.cc
26 *
27 * Implementation of HDBRegistry class.
28 *
29 * @author Pekka Jääskeläinen 2005 (pekka.jaaskelainen-no.spam-tut.fi)
30 * @note rating: red
31 */
32#include "HDBRegistry.hh"
33#include "AssocTools.hh"
34#include "CachedHDBManager.hh"
35#include "Application.hh"
36#include "Environment.hh"
37#include "FileSystem.hh"
38
39namespace HDB {
40
41HDBRegistry* HDBRegistry::instance_ = NULL;
42
43/**
44 * Constructor.
45 */
48
49/**
50 * Destructor.
51 */
55
56/**
57 * Creates and returns an instance of HDB registry.
58 *
59 * @return An instance of HDB registry.
60 */
63
64 if (instance_ == NULL) {
65 instance_ = new HDBRegistry();
66 }
67 return *instance_;
68}
69
70/**
71 * Returns the HDB at given path.
72 *
73 * In case the HDB is not found in registry, tries to open it.
74 *
75 * @param fileName File name of the HDB.
76 * @return The HDB associated with the file name.
77 * @exception Exception In case there was a problem while opening the HDB.
78 */
80HDBRegistry::hdb(const std::string fileName) {
83 return *registry_[FileSystem::absolutePathOf(fileName)];
84 }
87
88 registry_[FileSystem::absolutePathOf(fileName)] = manager;
89 return *manager;
90}
91
92/**
93 * Adds HDBManager in to the registry.
94 *
95 * Takes responsibility of deleting the given HDBManager.
96 *
97 * @param hdb The HDBManager to be added in to the registry.
98 */
99void
101
104
105 delete hdbManager;
106 hdbManager = NULL;
107 return;
108 }
109 registry_[FileSystem::absolutePathOf(hdbManager->fileName())] = hdbManager;
110}
111
112/**
113 * Returns true if the registry contains a HDBManager of the given HDB file.
114 *
115 * @param hdbFile The HDB file managed by the HDBManager.
116 * @return True if the registry contains a HDBManager of the given HDB file.
117 */
118bool
119HDBRegistry::hasHDB(const std::string& hdbFile) {
120
123 return true;
124 } else {
125 return false;
126 }
127}
128
129/**
130 * Returns the total number of stored HDBs.
131 *
132 * @return Count of stored HDBs.
133 */
134int
136
137 return registry_.size();
138}
139
140/*
141 * Searches and loads all HDB:s found in Environment paths.
142 *
143 * Stores erros caught in loading HDBs.
144 */
145void
147
148 // check every HDB path exists, and if doesn't, remove from registry
150
151 const std::vector<std::string> hdbPaths = Environment::hdbPaths();
152 std::vector<std::string>::const_iterator hdbIter = hdbPaths.begin();
153 for(; hdbIter != hdbPaths.end(); hdbIter++) {
154 if (!FileSystem::fileExists(*hdbIter) ||
155 !FileSystem::fileIsDirectory(*hdbIter) ||
156 !FileSystem::fileIsReadable(*hdbIter)) {
157 // Directory doesn't exist or can't be read.
158 continue;
159 }
160 std::vector<std::string> dir =
162 std::vector<std::string>::const_iterator dirIter = dir.begin();
163 for (; dirIter != dir.end(); dirIter++) {
164 std::string file = (*dirIter);
165 if (file.rfind(".hdb", file.size()) != std::string::npos &&
166 file.rfind(".hdb", file.size()) == (file.size() - 4)) {
168 try {
169 CachedHDBManager* manager =
172
173 registry_[FileSystem::absolutePathOf(file)] = manager;
174 } catch (IOException& e) {
175 std::string errorMessage = "Error in '" + file +
176 "': " + e.errorMessage() + ".";
177 errorMessages_.push_back(errorMessage);
178 }
179 }
180 }
181 }
182 }
183}
184
185/**
186 * Returns the HDB from given index.
187 *
188 * @return The HDB from the given index.
189 * @exception OutOfRange Is thrown if index is bigger than the HDB count.
190 */
192HDBRegistry::hdb(unsigned int index) {
193 if (index > (registry_.size() - 1)) {
194 throw OutOfRange(__FILE__, __LINE__,
195 "HDBRegistry::hdb(unsigned int)");
196 }
197 std::map<const std::string, CachedHDBManager*>::const_iterator
198 mapIterator = registry_.begin();
199 for (unsigned int counter = 0; mapIterator != registry_.end();
200 mapIterator++) {
201 if (counter == index) {
202 break;
203 } else {
204 counter++;
205 }
206 }
207 return *(*mapIterator).second;
208}
209
210/**
211 * Returns the full path of the HDB with given index.
212 *
213 * @return The HDB file path.
214 * @exception OutOfRange Is thrown if index is out of range.
215 */
216std::string
217HDBRegistry::hdbPath(unsigned index) {
218 if (index > (registry_.size() - 1)) {
219 throw OutOfRange(__FILE__, __LINE__,
220 "HDBRegistry::hdb(unsigned int)");
221 }
222 std::map<const std::string, CachedHDBManager*>::const_iterator iter =
223 registry_.begin();
224
225 for (unsigned c = 0; iter != registry_.end(); iter++) {
226 if (c == index) return (*iter).first;
227 c++;
228 }
229 assert(false);
230}
231
232/**
233 * Returns the number of error messages created during HDB loading.
234 *
235 * @return The number of error messages created during HDB loading.
236 */
237int
239
240 return errorMessages_.size();
241}
242
243/**
244 * Returns the error message from given index.
245 *
246 * @return The error message in given index.
247 * @exception OutOfRange Is thrown if index is bigger than the HDB
248 * error count.
249 */
250std::string
251HDBRegistry::hdbErrorMessage(unsigned int index) {
252 if (index > (errorMessages_.size() - 1)) {
253 throw OutOfRange(__FILE__, __LINE__,
254 "HDBRegistry::hdbErrorMessage(unsigned int)");
255 }
256 return errorMessages_[index];
257}
258
259/**
260 * Removes nonexistent HDB files from registry.
261 */
262void
264
265 std::map<const std::string, CachedHDBManager*>::iterator it;
266 for (it = registry_.begin(); it != registry_.end(); ) {
267 if (!FileSystem::fileExists(it->first)) {
268 registry_.erase(it->first);
269 ++it;
270 } else {
271 ++it;
272 }
273 }
274}
275
276}
#define assert(condition)
static bool containsKey(const ContainerType &aContainer, const KeyType &aKey)
static void deleteAllValues(ContainerType &aMap)
static std::vector< std::string > hdbPaths(bool libraryPathsOnly=false)
std::string errorMessage() const
Definition Exception.cc:123
static std::string absolutePathOf(const std::string &pathName)
static bool fileIsReadable(const std::string fileName)
static std::vector< std::string > directoryContents(const std::string &directory, const bool absolutePaths=true)
static bool fileIsDirectory(const std::string fileName)
static bool fileExists(const std::string fileName)
static CachedHDBManager & instance(const std::string &hdbFile)
std::string fileName() const
static HDBRegistry & instance()
void addHDB(CachedHDBManager *hdbManager)
CachedHDBManager & hdb(const std::string fileName)
void loadFromSearchPaths()
void removeDeadHDBPaths()
Deletes nonexistent HDB paths from registry.
std::string hdbPath(unsigned int index)
virtual ~HDBRegistry()
std::string hdbErrorMessage(unsigned int index)
HDBRegistry()
HDB registry must be created with instance() method.
std::map< const std::string, CachedHDBManager * > registry_
all opened HDBs are stored in this map
std::vector< std::string > errorMessages_
errors found during loading HDBs are strored in this vector
static HDBRegistry * instance_
Unique instance of the class.
bool hasHDB(const std::string &hdbFile)