OSDN Git Service

Bump version.
[lamexp/LameXP.git] / src / Global_Tools.cpp
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.
10 //
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.
15 //
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.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "Global.h"
24
25 //LameXP includes
26 #include "LockedFile.h"
27
28 //Qt includes
29 #include <QApplication>
30 #include <QHash>
31 #include <QReadWriteLock>
32 #include <QString>
33 #include <QStringList>
34 #include <QFileInfo>
35 #include <QPair>
36
37 //MUtils
38 #include <MUtils/Global.h>
39 #include <MUtils/Exception.h>
40
41 //CRT
42 #include <stdint.h>
43
44 ///////////////////////////////////////////////////////////////////////////////
45 // GLOBAL VARS
46 ///////////////////////////////////////////////////////////////////////////////
47
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;
52
53 //Tool registry
54 static QScopedPointer<tool_hash_t> g_lamexp_tools_data;
55 static QReadWriteLock              g_lamexp_tools_lock;
56
57 //Null String
58 static const QString g_null_string;
59
60 //UINT_MAX
61 static const quint32 g_max_uint32 = UINT32_MAX;
62
63 //Helper Macro
64 #define MAKE_ENTRY(LOCK_FILE,VER,TAG) \
65         qMakePair((LOCK_FILE),qMakePair((VER),(TAG)))
66
67 ///////////////////////////////////////////////////////////////////////////////
68 // GLOBAL FUNCTIONS
69 ///////////////////////////////////////////////////////////////////////////////
70
71 /*
72  * Clean-up *all* registered tools
73  */
74 static void lamexp_tools_clean_up(void)
75 {
76         QWriteLocker writeLock(&g_lamexp_tools_lock);
77
78         if(!g_lamexp_tools_data.isNull())
79         {
80                 const QStringList keys = g_lamexp_tools_data->keys();
81                 for(QStringList::ConstIterator iter = keys.constBegin(); iter != keys.constEnd(); iter++)
82                 {
83                         tool_data_t currentTool = (*g_lamexp_tools_data)[*iter];
84                         MUTILS_DELETE(currentTool.first);
85                 }
86                 g_lamexp_tools_data->clear();
87         }
88 }
89
90 /*
91  * Register tool
92  */
93 void lamexp_tools_register(const QString &toolName, LockedFile *const file, const quint32 &version, const QString &tag)
94 {
95         QWriteLocker writeLock(&g_lamexp_tools_lock);
96         
97         if(!file)
98         {
99                 MUTILS_THROW("lamexp_register_tool: Tool file must not be NULL!");
100         }
101
102         if(g_lamexp_tools_data.isNull())
103         {
104                 g_lamexp_tools_data.reset(new tool_hash_t());
105                 atexit(lamexp_tools_clean_up);
106         }
107
108         const QString key = toolName.simplified().toLower();
109         if(g_lamexp_tools_data->contains(key))
110         {
111                 MUTILS_THROW("lamexp_register_tool: Tool is already registered!");
112         }
113
114         g_lamexp_tools_data->insert(key, MAKE_ENTRY(file, version, tag));
115 }
116
117 /*
118  * Check for tool
119  */
120 bool lamexp_tools_check(const QString &toolName)
121 {
122         QReadLocker readLock(&g_lamexp_tools_lock);
123
124         if(!g_lamexp_tools_data.isNull())
125         {
126                 const QString key = toolName.simplified().toLower();
127                 return g_lamexp_tools_data->contains(key);
128         }
129
130         return false;
131 }
132
133 /*
134  * Lookup tool path
135  */
136 const QString &lamexp_tools_lookup(const QString &toolName)
137 {
138         QReadLocker readLock(&g_lamexp_tools_lock);
139
140         if(!g_lamexp_tools_data.isNull())
141         {
142                 const QString key = toolName.simplified().toLower();
143                 if(g_lamexp_tools_data->contains(key))
144                 {
145                         return (*g_lamexp_tools_data)[key].first->filePath();
146                 }
147         }
148
149         return g_null_string;
150 }
151
152 /*
153  * Lookup tool version
154  */
155 const quint32 &lamexp_tools_version(const QString &toolName, QString *const tagOut)
156 {
157         QReadLocker readLock(&g_lamexp_tools_lock);
158
159         if(!g_lamexp_tools_data.isNull())
160         {
161                 const QString key = toolName.simplified().toLower();
162                 if(g_lamexp_tools_data->contains(key))
163                 {
164                         const tool_info_t &info = (*g_lamexp_tools_data)[key].second;
165                         if(tagOut)
166                         {
167                                 *tagOut = info.second;
168                         }
169                         return info.first;
170                 }
171         }
172
173         if(tagOut)
174         {
175                 tagOut->clear();
176         }
177         return g_max_uint32;
178 }