OSDN Git Service

Merge branch 'master' of github.com:lordmulder/LameXP
[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(g_lamexp_tools_data.isNull())
98         {
99                 g_lamexp_tools_data.reset(new tool_hash_t());
100                 atexit(lamexp_tools_clean_up);
101         }
102
103         const QString key = toolName.simplified().toLower();
104         if(g_lamexp_tools_data->contains(key))
105         {
106                 MUTILS_THROW("lamexp_register_tool: Tool is already registered!");
107         }
108
109         g_lamexp_tools_data->insert(key, MAKE_ENTRY(file, version, tag));
110 }
111
112 /*
113  * Check for tool
114  */
115 bool lamexp_tools_check(const QString &toolName)
116 {
117         QReadLocker readLock(&g_lamexp_tools_lock);
118
119         if(!g_lamexp_tools_data.isNull())
120         {
121                 const QString key = toolName.simplified().toLower();
122                 return g_lamexp_tools_data->contains(key);
123         }
124
125         return false;
126 }
127
128 /*
129  * Lookup tool path
130  */
131 const QString &lamexp_tools_lookup(const QString &toolName)
132 {
133         QReadLocker readLock(&g_lamexp_tools_lock);
134
135         if(!g_lamexp_tools_data.isNull())
136         {
137                 const QString key = toolName.simplified().toLower();
138                 if(g_lamexp_tools_data->contains(key))
139                 {
140                         return (*g_lamexp_tools_data)[key].first->filePath();
141                 }
142         }
143
144         return g_null_string;
145 }
146
147 /*
148  * Lookup tool version
149  */
150 const quint32 &lamexp_tools_version(const QString &toolName, QString *const tagOut)
151 {
152         QReadLocker readLock(&g_lamexp_tools_lock);
153
154         if(!g_lamexp_tools_data.isNull())
155         {
156                 const QString key = toolName.simplified().toLower();
157                 if(g_lamexp_tools_data->contains(key))
158                 {
159                         const tool_info_t &info = (*g_lamexp_tools_data)[key].second;
160                         if(tagOut)
161                         {
162                                 *tagOut = info.second;
163                         }
164                         return info.first;
165                 }
166         }
167
168         if(tagOut)
169         {
170                 tagOut->clear();
171         }
172         return g_max_uint32;
173 }