Fixed regression in 027f4d519d70df873c105c4235a9e5a996f2544a: If no existing file...
[LameXP.git] / src / Global_Tools.cpp
blob799bd68e17f435db5c813a52d5c9414d67a348c3
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2015 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 #include "Global.h"
25 //LameXP includes
26 #include "LockedFile.h"
28 //Qt includes
29 #include <QApplication>
30 #include <QHash>
31 #include <QReadWriteLock>
32 #include <QString>
33 #include <QStringList>
34 #include <QFileInfo>
35 #include <QPair>
37 //MUtils
38 #include <MUtils/Global.h>
39 #include <MUtils/Exception.h>
41 //CRT
42 #include <stdint.h>
44 ///////////////////////////////////////////////////////////////////////////////
45 // GLOBAL VARS
46 ///////////////////////////////////////////////////////////////////////////////
48 //Typedef
49 typedef QPair<quint32,QString> tool_info_t;
50 typedef QPair<LockedFile*, tool_info_t> tool_data_t;
51 typedef QHash<QString, tool_data_t> tool_hash_t;
53 //Tool registry
54 static QScopedPointer<tool_hash_t> g_lamexp_tools_data;
55 static QReadWriteLock g_lamexp_tools_lock;
57 //Null String
58 static const QString g_null_string;
60 //UINT_MAX
61 static const quint32 g_max_uint32 = UINT32_MAX;
63 //Helper Macro
64 #define MAKE_ENTRY(LOCK_FILE,VER,TAG) \
65 qMakePair((LOCK_FILE),qMakePair((VER),(TAG)))
67 ///////////////////////////////////////////////////////////////////////////////
68 // GLOBAL FUNCTIONS
69 ///////////////////////////////////////////////////////////////////////////////
72 * Clean-up *all* registered tools
74 static void lamexp_tools_clean_up(void)
76 QWriteLocker writeLock(&g_lamexp_tools_lock);
78 if(!g_lamexp_tools_data.isNull())
80 const QStringList keys = g_lamexp_tools_data->keys();
81 for(QStringList::ConstIterator iter = keys.constBegin(); iter != keys.constEnd(); iter++)
83 tool_data_t currentTool = (*g_lamexp_tools_data)[*iter];
84 MUTILS_DELETE(currentTool.first);
86 g_lamexp_tools_data->clear();
91 * Register tool
93 void lamexp_tools_register(const QString &toolName, LockedFile *const file, const quint32 &version, const QString &tag)
95 QWriteLocker writeLock(&g_lamexp_tools_lock);
97 if(g_lamexp_tools_data.isNull())
99 g_lamexp_tools_data.reset(new tool_hash_t());
100 atexit(lamexp_tools_clean_up);
103 const QString key = toolName.simplified().toLower();
104 if(g_lamexp_tools_data->contains(key))
106 MUTILS_THROW("lamexp_register_tool: Tool is already registered!");
109 g_lamexp_tools_data->insert(key, MAKE_ENTRY(file, version, tag));
113 * Check for tool
115 bool lamexp_tools_check(const QString &toolName)
117 QReadLocker readLock(&g_lamexp_tools_lock);
119 if(!g_lamexp_tools_data.isNull())
121 const QString key = toolName.simplified().toLower();
122 return g_lamexp_tools_data->contains(key);
125 return false;
129 * Lookup tool path
131 const QString &lamexp_tools_lookup(const QString &toolName)
133 QReadLocker readLock(&g_lamexp_tools_lock);
135 if(!g_lamexp_tools_data.isNull())
137 const QString key = toolName.simplified().toLower();
138 if(g_lamexp_tools_data->contains(key))
140 return (*g_lamexp_tools_data)[key].first->filePath();
144 return g_null_string;
148 * Lookup tool version
150 const quint32 &lamexp_tools_version(const QString &toolName, QString *const tagOut)
152 QReadLocker readLock(&g_lamexp_tools_lock);
154 if(!g_lamexp_tools_data.isNull())
156 const QString key = toolName.simplified().toLower();
157 if(g_lamexp_tools_data->contains(key))
159 const tool_info_t &info = (*g_lamexp_tools_data)[key].second;
160 if(tagOut)
162 *tagOut = info.second;
164 return info.first;
168 if(tagOut)
170 tagOut->clear();
172 return g_max_uint32;