2017-02-14 14:39:16 +08:00
|
|
|
/* guiddatabase.cpp
|
2022-08-28 18:47:01 +08:00
|
|
|
|
|
|
|
Copyright (c) 2017, LongSoft. All rights reserved.
|
|
|
|
This program and the accompanying materials
|
|
|
|
are licensed and made available under the terms and conditions of the BSD License
|
|
|
|
which accompanies this distribution. The full text of the license may be found at
|
|
|
|
http://opensource.org/licenses/bsd-license.php
|
|
|
|
|
|
|
|
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
|
|
|
|
WITHWARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
|
|
|
*/
|
2017-02-14 14:39:16 +08:00
|
|
|
|
|
|
|
#include "guiddatabase.h"
|
|
|
|
#include "ubytearray.h"
|
2018-10-08 17:58:12 +08:00
|
|
|
#include "ffs.h"
|
2017-02-14 14:39:16 +08:00
|
|
|
|
2018-08-02 10:37:09 +08:00
|
|
|
#include <fstream>
|
|
|
|
#include <string>
|
2019-01-08 00:26:31 +08:00
|
|
|
|
|
|
|
#if defined(U_ENABLE_GUID_DATABASE_SUPPORT)
|
|
|
|
#include <sstream>
|
2018-08-02 10:37:09 +08:00
|
|
|
#include <vector>
|
2018-10-08 17:58:12 +08:00
|
|
|
#include <cstdio>
|
2017-02-14 14:39:16 +08:00
|
|
|
|
2019-01-08 00:26:31 +08:00
|
|
|
static GuidDatabase gLocalGuidDatabase;
|
2017-02-14 14:39:16 +08:00
|
|
|
|
2018-08-02 10:37:09 +08:00
|
|
|
#ifdef QT_CORE_LIB
|
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
#include <QTextStream>
|
|
|
|
|
|
|
|
// This is required to be able to read Qt-embedded paths
|
|
|
|
|
|
|
|
static std::string readGuidDatabase(const UString &path) {
|
|
|
|
QFile guids(path);
|
|
|
|
if (guids.open(QFile::ReadOnly | QFile::Text))
|
|
|
|
return QTextStream(&guids).readAll().toStdString();
|
|
|
|
return std::string {};
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
static std::string readGuidDatabase(const UString &path) {
|
|
|
|
std::ifstream guids(path.toLocal8Bit());
|
|
|
|
std::stringstream ret;
|
|
|
|
if (ret)
|
|
|
|
ret << guids.rdbuf();
|
|
|
|
return ret.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-02-14 14:39:16 +08:00
|
|
|
void initGuidDatabase(const UString & path, UINT32* numEntries)
|
|
|
|
{
|
2019-01-08 00:26:31 +08:00
|
|
|
gLocalGuidDatabase.clear();
|
2022-08-28 18:47:01 +08:00
|
|
|
|
2018-08-02 10:37:09 +08:00
|
|
|
std::stringstream file(readGuidDatabase(path));
|
2022-08-28 18:47:01 +08:00
|
|
|
|
2018-08-02 10:37:09 +08:00
|
|
|
while (!file.eof()) {
|
|
|
|
std::string line;
|
|
|
|
std::getline(file, line);
|
2022-08-28 18:47:01 +08:00
|
|
|
|
2018-08-02 10:37:09 +08:00
|
|
|
// Use sharp symbol as commentary
|
|
|
|
if (line.size() == 0 || line[0] == '#')
|
2017-02-14 14:39:16 +08:00
|
|
|
continue;
|
2022-08-28 18:47:01 +08:00
|
|
|
|
2021-10-07 23:51:39 +08:00
|
|
|
// GUID and name are comma-separated
|
2018-08-02 10:37:09 +08:00
|
|
|
std::vector<UString> lineParts;
|
|
|
|
std::string::size_type prev = 0, curr = 0;
|
|
|
|
while ((curr = line.find(',', curr)) != std::string::npos) {
|
|
|
|
std::string substring( line.substr(prev, curr-prev) );
|
|
|
|
lineParts.push_back(UString(substring.c_str()));
|
|
|
|
prev = ++curr;
|
|
|
|
}
|
|
|
|
lineParts.push_back(UString(line.substr(prev, curr-prev).c_str()));
|
2022-08-28 18:47:01 +08:00
|
|
|
|
2018-08-02 10:37:09 +08:00
|
|
|
if (lineParts.size() < 2)
|
|
|
|
continue;
|
2022-08-28 18:47:01 +08:00
|
|
|
|
2018-08-02 10:37:09 +08:00
|
|
|
EFI_GUID guid;
|
2018-10-08 17:58:12 +08:00
|
|
|
if (!ustringToGuid(lineParts[0], guid))
|
2017-02-14 14:39:16 +08:00
|
|
|
continue;
|
2022-08-28 18:47:01 +08:00
|
|
|
|
2019-01-08 00:26:31 +08:00
|
|
|
gLocalGuidDatabase[guid] = lineParts[1];
|
2017-02-14 14:39:16 +08:00
|
|
|
}
|
2022-08-28 18:47:01 +08:00
|
|
|
|
2017-02-14 14:39:16 +08:00
|
|
|
if (numEntries)
|
2019-01-08 00:26:31 +08:00
|
|
|
*numEntries = (UINT32)gLocalGuidDatabase.size();
|
2017-02-14 14:39:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
UString guidDatabaseLookup(const EFI_GUID & guid)
|
|
|
|
{
|
2019-01-08 00:26:31 +08:00
|
|
|
return gLocalGuidDatabase[guid];
|
2017-02-14 14:39:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
void initGuidDatabase(const UString & path, UINT32* numEntries)
|
|
|
|
{
|
|
|
|
U_UNUSED_PARAMETER(path);
|
|
|
|
if (numEntries)
|
|
|
|
*numEntries = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
UString guidDatabaseLookup(const EFI_GUID & guid)
|
|
|
|
{
|
2021-10-07 23:51:39 +08:00
|
|
|
U_UNUSED_PARAMETER(guid);
|
2017-02-14 14:39:16 +08:00
|
|
|
return UString();
|
|
|
|
}
|
|
|
|
#endif
|
2019-01-08 00:26:31 +08:00
|
|
|
|
|
|
|
GuidDatabase guidDatabaseFromTreeRecursive(TreeModel * model, const UModelIndex index)
|
|
|
|
{
|
|
|
|
GuidDatabase db;
|
2022-08-28 18:47:01 +08:00
|
|
|
|
2019-01-08 00:26:31 +08:00
|
|
|
if (!index.isValid())
|
|
|
|
return db;
|
2022-08-28 18:47:01 +08:00
|
|
|
|
2019-01-08 00:26:31 +08:00
|
|
|
for (int i = 0; i < model->rowCount(index); i++) {
|
2021-04-04 17:09:23 +08:00
|
|
|
GuidDatabase tmpDb = guidDatabaseFromTreeRecursive(model, index.model()->index(i, index.column(), index));
|
2022-08-28 18:47:01 +08:00
|
|
|
|
2019-01-08 00:26:31 +08:00
|
|
|
db.insert(tmpDb.begin(), tmpDb.end());
|
|
|
|
}
|
2022-08-28 18:47:01 +08:00
|
|
|
|
2019-01-08 00:26:31 +08:00
|
|
|
if (model->type(index) == Types::File && !model->text(index).isEmpty())
|
|
|
|
db[readUnaligned((const EFI_GUID*)model->header(index).left(16).constData())] = model->text(index);
|
2022-08-28 18:47:01 +08:00
|
|
|
|
2019-01-08 00:26:31 +08:00
|
|
|
return db;
|
|
|
|
}
|
|
|
|
|
2019-01-08 04:01:04 +08:00
|
|
|
USTATUS guidDatabaseExportToFile(const UString & outPath, GuidDatabase & db)
|
2019-01-08 00:26:31 +08:00
|
|
|
{
|
|
|
|
std::ofstream outputFile(outPath.toLocal8Bit(), std::ios::out | std::ios::trunc);
|
|
|
|
if (!outputFile)
|
|
|
|
return U_FILE_OPEN;
|
|
|
|
for (GuidDatabase::iterator it = db.begin(); it != db.end(); it++) {
|
2023-03-12 23:49:19 +08:00
|
|
|
std::string guid(guidToUString (it->first, false).toLocal8Bit());
|
2019-01-08 00:26:31 +08:00
|
|
|
std::string name(it->second.toLocal8Bit());
|
|
|
|
outputFile << guid << ',' << name << '\n';
|
|
|
|
}
|
2022-08-28 18:47:01 +08:00
|
|
|
|
2019-01-08 00:26:31 +08:00
|
|
|
return U_SUCCESS;
|
|
|
|
}
|