Data.cpp 20.2 KB
Newer Older
Philip Müller's avatar
Philip Müller committed
1
/*
2 3
 *  This file is part of the mhwd - Manjaro Hardware Detection project
 *  
dec's avatar
dec committed
4 5 6
 *  mhwd - Manjaro Hardware Detection
 *  Roland Singer <roland@manjaro.org>
 *  Łukasz Matysiak <december0123@gmail.com>
Philip Müller's avatar
Philip Müller committed
7
 *  Filipe Marques <eagle.software3@gmail.com>
Philip Müller's avatar
Philip Müller committed
8
 *
dec's avatar
dec committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *  Copyright (C) 2007 Free Software Foundation, Inc.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
Philip Müller's avatar
Philip Müller committed
23 24 25
 */

#include <dirent.h>
december0123's avatar
december0123 committed
26

december0123's avatar
december0123 committed
27
#include <algorithm>
Philip Müller's avatar
Philip Müller committed
28 29 30
#include <fstream>
#include <iomanip>
#include <sstream>
december0123's avatar
december0123 committed
31 32 33 34 35
#include <string>
#include <vector>

#include "Data.hpp"

Philip Müller's avatar
Philip Müller committed
36 37
Data::Data()
{
december0123's avatar
december0123 committed
38 39
    fillDevices("PCI");
    fillDevices("USB");
Philip Müller's avatar
Philip Müller committed
40

december0123's avatar
december0123 committed
41
    updateConfigData();
Philip Müller's avatar
Philip Müller committed
42 43 44 45
}

void Data::updateInstalledConfigData()
{
december0123's avatar
december0123 committed
46
    // Clear config vectors in each device element
47
    for (auto&& PCIDevice = PCIDevices.begin();
48
            PCIDevice != PCIDevices.end(); ++PCIDevice)
december0123's avatar
december0123 committed
49
    {
december0123's avatar
december0123 committed
50
        (*PCIDevice)->installedConfigs_.clear();
december0123's avatar
december0123 committed
51 52
    }

53
    for (auto&& USBDevice = USBDevices.begin();
54
            USBDevice != USBDevices.end(); ++USBDevice)
december0123's avatar
december0123 committed
55
    {
december0123's avatar
december0123 committed
56
        (*USBDevice)->installedConfigs_.clear();
december0123's avatar
december0123 committed
57 58 59 60 61 62 63 64 65
    }

    installedPCIConfigs.clear();
    installedUSBConfigs.clear();

    // Refill data
    fillInstalledConfigs("PCI");
    fillInstalledConfigs("USB");

december0123's avatar
december0123 committed
66 67
    setMatchingConfigs(PCIDevices, installedPCIConfigs, true);
    setMatchingConfigs(USBDevices, installedUSBConfigs, true);
Philip Müller's avatar
Philip Müller committed
68 69
}

70
void Data::fillInstalledConfigs(std::string type)
Philip Müller's avatar
Philip Müller committed
71
{
december0123's avatar
december0123 committed
72
    std::vector<std::string> configPaths;
december0123's avatar
december0123 committed
73
    std::vector<std::shared_ptr<Config>>* configs;
december0123's avatar
december0123 committed
74

75
    if ("USB" == type)
december0123's avatar
december0123 committed
76 77 78 79 80 81 82 83 84 85
    {
        configs = &installedUSBConfigs;
        configPaths = getRecursiveDirectoryFileList(MHWD_USB_DATABASE_DIR, MHWD_CONFIG_NAME);
    }
    else
    {
        configs = &installedPCIConfigs;
        configPaths = getRecursiveDirectoryFileList(MHWD_PCI_DATABASE_DIR, MHWD_CONFIG_NAME);
    }

86
    for (auto&& configPath = configPaths.begin();
december0123's avatar
december0123 committed
87 88 89 90 91 92
            configPath != configPaths.end(); ++configPath)
    {
        Config *config = new Config((*configPath), type);

        if (config->readConfigFile((*configPath)))
        {
december0123's avatar
december0123 committed
93
            configs->push_back(std::shared_ptr<Config>{config});
december0123's avatar
december0123 committed
94 95 96
        }
        else
        {
december0123's avatar
december0123 committed
97
            invalidConfigs.push_back(std::shared_ptr<Config>{config});
december0123's avatar
december0123 committed
98 99
        }
    }
Philip Müller's avatar
Philip Müller committed
100 101
}

december0123's avatar
december0123 committed
102
void Data::getAllDevicesOfConfig(std::shared_ptr<Config> config, std::vector<std::shared_ptr<Device>>& foundDevices)
Philip Müller's avatar
Philip Müller committed
103
{
december0123's avatar
december0123 committed
104
    std::vector<std::shared_ptr<Device>> devices;
december0123's avatar
december0123 committed
105

106
    if ("USB" == config->type_)
december0123's avatar
december0123 committed
107 108 109 110 111 112 113 114
    {
        devices = USBDevices;
    }
    else
    {
        devices = PCIDevices;
    }

december0123's avatar
december0123 committed
115
    getAllDevicesOfConfig(devices, config, foundDevices);
Philip Müller's avatar
Philip Müller committed
116 117
}

december0123's avatar
december0123 committed
118 119 120
void Data::getAllDevicesOfConfig(const std::vector<std::shared_ptr<Device>>& devices,
        std::shared_ptr<Config> config,
        std::vector<std::shared_ptr<Device>>& foundDevices)
Philip Müller's avatar
Philip Müller committed
121
{
december0123's avatar
december0123 committed
122
    foundDevices.clear();
december0123's avatar
december0123 committed
123

124
    for (auto&& hwdID = config->hwdIDs_.begin();
december0123's avatar
december0123 committed
125 126 127 128
            hwdID != config->hwdIDs_.end(); ++hwdID)
    {
        bool foundDevice = false;
        // Check all devices
129
        for (auto&& i_device = devices.begin(); i_device != devices.end();
december0123's avatar
december0123 committed
130 131 132
                ++i_device)
        {
            // Check class ids
133 134 135
            bool found = std::find_if(hwdID->classIDs.begin(), hwdID->classIDs.end(), [i_device](const std::string& classID){
            					return (("*" == classID) || (classID == (*i_device)->classID_));
            				}) != hwdID->classIDs.end();
december0123's avatar
december0123 committed
136 137 138 139 140 141 142 143

            if (!found)
            {
                continue;
            }
            else
            {
                // Check blacklisted class ids
144 145 146
                found = std::find_if(hwdID->blacklistedClassIDs.begin(), hwdID->blacklistedClassIDs.end(), [i_device](const std::string& blacklistedClassID){
                				return (blacklistedClassID == (*i_device)->classID_);
                			}) != hwdID->blacklistedClassIDs.end();
december0123's avatar
december0123 committed
147 148 149 150 151 152 153 154

                if (found)
                {
                    continue;
                }
                else
                {
                    // Check vendor ids
155 156 157
                	found = std::find_if(hwdID->vendorIDs.begin(), hwdID->vendorIDs.end(), [i_device](const std::string& vendorID){
                	            	return (("*" == vendorID) || (vendorID == (*i_device)->vendorID_));
                	            }) != hwdID->vendorIDs.end();
december0123's avatar
december0123 committed
158 159 160 161 162 163 164 165

                    if (!found)
                    {
                        continue;
                    }
                    else
                    {
                        // Check blacklisted vendor ids
166 167 168
                        found = std::find_if(hwdID->blacklistedVendorIDs.begin(), hwdID->blacklistedVendorIDs.end(), [i_device](const std::string& blacklistedVendorID){
                        				return (blacklistedVendorID == (*i_device)->vendorID_);
                        			}) != hwdID->blacklistedVendorIDs.end();
december0123's avatar
december0123 committed
169 170 171 172 173 174 175 176

                        if (found)
                        {
                            continue;
                        }
                        else
                        {
                            // Check device ids
177 178 179
                        	found = std::find_if(hwdID->deviceIDs.begin(), hwdID->deviceIDs.end(), [i_device](const std::string& deviceID){
                        	        		return (("*" == deviceID) || (deviceID == (*i_device)->deviceID_));
                        	            }) != hwdID->deviceIDs.end();
december0123's avatar
december0123 committed
180 181 182 183 184 185 186 187

                            if (!found)
                            {
                                continue;
                            }
                            else
                            {
                                // Check blacklisted device ids
188 189 190
                                found = std::find_if(hwdID->blacklistedDeviceIDs.begin(), hwdID->blacklistedDeviceIDs.end(), [i_device](const std::string& blacklistedDeviceID){
                                				return (blacklistedDeviceID == (*i_device)->deviceID_);
                                			}) != hwdID->blacklistedDeviceIDs.end();
december0123's avatar
december0123 committed
191 192 193 194 195 196 197
                                if (found)
                                {
                                    continue;
                                }
                                else
                                {
                                    foundDevice = true;
december0123's avatar
december0123 committed
198
                                    foundDevices.push_back(*i_device);
december0123's avatar
december0123 committed
199 200 201 202 203 204 205 206 207 208
                                }
                            }
                        }
                    }
                }
            }
        }

        if (!foundDevice)
        {
december0123's avatar
december0123 committed
209
            foundDevices.clear();
december0123's avatar
december0123 committed
210 211 212
            return;
        }
    }
Philip Müller's avatar
Philip Müller committed
213 214
}

december0123's avatar
december0123 committed
215 216
std::vector<std::shared_ptr<Config>> Data::getAllDependenciesToInstall(
        std::shared_ptr<Config> config)
Philip Müller's avatar
Philip Müller committed
217
{
december0123's avatar
december0123 committed
218 219
    std::vector<std::shared_ptr<Config>> depends;
    std::vector<std::shared_ptr<Config>> installedConfigs;
december0123's avatar
december0123 committed
220 221

    // Get the right configs
222
    if ("USB" == config->type_)
december0123's avatar
december0123 committed
223 224 225 226 227 228 229 230 231
    {
        installedConfigs = installedUSBConfigs;
    }
    else
    {
        installedConfigs = installedPCIConfigs;
    }

    // Get all depends
december0123's avatar
december0123 committed
232
    getAllDependenciesToInstall(config, installedConfigs, &depends);
december0123's avatar
december0123 committed
233 234

    return depends;
Philip Müller's avatar
Philip Müller committed
235 236
}

december0123's avatar
december0123 committed
237 238 239
void Data::getAllDependenciesToInstall(std::shared_ptr<Config> config,
        std::vector<std::shared_ptr<Config>>& installedConfigs,
        std::vector<std::shared_ptr<Config>> *dependencies)
Philip Müller's avatar
Philip Müller committed
240
{
241
    for (auto&& configDependency = config->dependencies_.begin();
242
            configDependency != config->dependencies_.end(); ++configDependency)
december0123's avatar
december0123 committed
243
    {
december0123's avatar
december0123 committed
244
        auto found = std::find_if(installedConfigs.begin(), installedConfigs.end(),
Łukasz Matysiak's avatar
Łukasz Matysiak committed
245 246
                [configDependency](const std::shared_ptr<Config>& config) -> bool {
                    return (config->name_ == *configDependency);
december0123's avatar
december0123 committed
247
                });
december0123's avatar
december0123 committed
248

december0123's avatar
december0123 committed
249
        if (found != installedConfigs.end())
december0123's avatar
december0123 committed
250 251 252 253 254
        {
            continue;
        }
        else
        {
december0123's avatar
december0123 committed
255
            found = std::find_if(dependencies->begin(), dependencies->end(),
Łukasz Matysiak's avatar
Łukasz Matysiak committed
256 257
                    [configDependency](const std::shared_ptr<Config>& config) -> bool {
                        return (config->name_ == *configDependency);
december0123's avatar
december0123 committed
258 259 260
                    });

            if (found != dependencies->end())
december0123's avatar
december0123 committed
261 262 263 264 265
            {
                continue;
            }
            else
            {
december0123's avatar
december0123 committed
266
                // Add to vector and check for further subdepends...
267 268 269
                std::shared_ptr<Config> dependconfig {
                	getDatabaseConfig((*configDependency), config->type_)};
                if (nullptr == dependconfig)
december0123's avatar
december0123 committed
270 271 272 273 274
                {
                    continue;
                }
                else
                {
dec's avatar
dec committed
275
                    dependencies->emplace_back(dependconfig);
december0123's avatar
december0123 committed
276 277
                    getAllDependenciesToInstall(dependconfig, installedConfigs, dependencies);
                }
december0123's avatar
december0123 committed
278 279 280
            }
        }
    }
Philip Müller's avatar
Philip Müller committed
281 282
}

december0123's avatar
december0123 committed
283
std::shared_ptr<Config> Data::getDatabaseConfig(const std::string configName,
december0123's avatar
december0123 committed
284
        const std::string configType)
Philip Müller's avatar
Philip Müller committed
285
{
december0123's avatar
december0123 committed
286
    std::vector<std::shared_ptr<Config>> allConfigs;
december0123's avatar
december0123 committed
287 288

    // Get the right configs
289
    if ("USB" == configType)
december0123's avatar
december0123 committed
290 291 292 293 294 295 296 297
    {
        allConfigs = allUSBConfigs;
    }
    else
    {
        allConfigs = allPCIConfigs;
    }

298
    for (auto&& config = allConfigs.begin(); config != allConfigs.end();
299
            ++config)
december0123's avatar
december0123 committed
300 301 302 303 304 305 306 307
    {
        if (configName == (*config)->name_)
        {
            return (*config);
        }
    }

    return nullptr;
Philip Müller's avatar
Philip Müller committed
308 309
}

december0123's avatar
december0123 committed
310
std::vector<std::shared_ptr<Config>> Data::getAllLocalConflicts(std::shared_ptr<Config> config)
Philip Müller's avatar
Philip Müller committed
311
{
december0123's avatar
december0123 committed
312 313 314
    std::vector<std::shared_ptr<Config>> conflicts;
    std::vector<std::shared_ptr<Config>> dependencies = getAllDependenciesToInstall(config);
    std::vector<std::shared_ptr<Config>> installedConfigs;
december0123's avatar
december0123 committed
315 316

    // Get the right configs
317
    if ("USB" == config->type_)
december0123's avatar
december0123 committed
318 319 320 321 322 323 324 325
    {
        installedConfigs = installedUSBConfigs;
    }
    else
    {
        installedConfigs = installedPCIConfigs;
    }

dec's avatar
dec committed
326
    dependencies.emplace_back(config);
december0123's avatar
december0123 committed
327

328
    for (auto&& dependency = dependencies.begin();
329
            dependency != dependencies.end(); ++dependency)
december0123's avatar
december0123 committed
330
    {
331 332
        for (auto&& dependencyConflict = (*dependency)->conflicts_.begin();
                dependencyConflict != (*dependency)->conflicts_.end(); ++dependencyConflict)
december0123's avatar
december0123 committed
333
        {
334
            for (auto&& installedConfig = installedConfigs.begin();
335
                    installedConfig != installedConfigs.end(); ++installedConfig)
december0123's avatar
december0123 committed
336 337 338 339 340 341 342 343 344
            {
                if ((*dependencyConflict) != (*installedConfig)->name_)
                {
                    continue;
                }
                else
                {
                    // Check if already in vector
                    bool found = false;
345
                    for (auto&& conflict = conflicts.begin();
346
                            conflict != conflicts.end(); ++conflict)
december0123's avatar
december0123 committed
347 348 349 350 351 352 353 354 355 356 357 358 359 360
                    {
                        if ((*conflict)->name_ == (*dependencyConflict))
                        {
                            found = true;
                            break;
                        }
                    }

                    if (found)
                    {
                        continue;
                    }
                    else
                    {
dec's avatar
dec committed
361
                        conflicts.emplace_back(*installedConfig);
december0123's avatar
december0123 committed
362 363 364 365 366 367 368 369
                        break;
                    }
                }
            }
        }
    }

    return conflicts;
Philip Müller's avatar
Philip Müller committed
370 371
}

december0123's avatar
december0123 committed
372
std::vector<std::shared_ptr<Config>> Data::getAllLocalRequirements(std::shared_ptr<Config> config)
Philip Müller's avatar
Philip Müller committed
373
{
december0123's avatar
december0123 committed
374 375
    std::vector<std::shared_ptr<Config>> requirements;
    std::vector<std::shared_ptr<Config>> installedConfigs;
december0123's avatar
december0123 committed
376 377

    // Get the right configs
378
    if ("USB" == config->type_)
december0123's avatar
december0123 committed
379 380 381 382 383 384 385 386 387
    {
        installedConfigs = installedUSBConfigs;
    }
    else
    {
        installedConfigs = installedPCIConfigs;
    }

    // Check if this config is required by another installed config
388
    for (auto&& installedConfig = installedConfigs.begin();
389
            installedConfig != installedConfigs.end(); ++installedConfig)
december0123's avatar
december0123 committed
390
    {
391
        for (auto&& dependency = (*installedConfig)->dependencies_.begin();
december0123's avatar
december0123 committed
392 393 394 395 396 397 398 399 400 401
                dependency != (*installedConfig)->dependencies_.end(); dependency++)
        {
            if ((*dependency) != config->name_)
            {
                continue;
            }
            else
            {
                // Check if already in vector
                bool found = false;
402
                for (auto&& requirement = requirements.begin();
403
                        requirement != requirements.end(); ++requirement)
december0123's avatar
december0123 committed
404 405 406 407 408 409 410 411 412 413
                {
                    if ((*requirement)->name_ == (*installedConfig)->name_)
                    {
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
dec's avatar
dec committed
414
                    requirements.emplace_back(*installedConfig);
december0123's avatar
december0123 committed
415 416 417 418 419 420 421
                    break;
                }
            }
        }
    }

    return requirements;
Philip Müller's avatar
Philip Müller committed
422 423
}

424
void Data::fillDevices(std::string type)
Philip Müller's avatar
Philip Müller committed
425
{
december0123's avatar
december0123 committed
426
    hw_item hw;
december0123's avatar
december0123 committed
427
    std::vector<std::shared_ptr<Device>>* devices;
december0123's avatar
december0123 committed
428

429
    if ("USB" == type)
december0123's avatar
december0123 committed
430 431 432 433 434 435 436 437 438 439 440
    {
        hw = hw_usb;
        devices = &USBDevices;
    }
    else
    {
        hw = hw_pci;
        devices = &PCIDevices;
    }

    // Get the hardware devices
dec's avatar
dec committed
441 442
    std::unique_ptr<hd_data_t> hd_data{new hd_data_t()};
    hd_t *hd = hd_list(hd_data.get(), hw, 1, nullptr);
december0123's avatar
december0123 committed
443

dec's avatar
dec committed
444
    std::unique_ptr<Device> device;
dec's avatar
dec committed
445
    for (hd_t *hdIter = hd; hdIter; hdIter = hdIter->next)
december0123's avatar
december0123 committed
446
    {
dec's avatar
dec committed
447
        device.reset(new Device());
december0123's avatar
december0123 committed
448
        device->type_ = type;
dec's avatar
dec committed
449 450 451 452 453 454 455 456
        device->classID_ = from_Hex(hdIter->base_class.id, 2) + from_Hex(hdIter->sub_class.id, 2).toLower();
        device->vendorID_ = from_Hex(hdIter->vendor.id, 4).toLower();
        device->deviceID_ = from_Hex(hdIter->device.id, 4).toLower();
        device->className_ = from_CharArray(hdIter->base_class.name);
        device->vendorName_ = from_CharArray(hdIter->vendor.name);
        device->deviceName_ = from_CharArray(hdIter->device.name);
        device->sysfsBusID_ = from_CharArray(hdIter->sysfs_bus_id);
        device->sysfsID_ = from_CharArray(hdIter->sysfs_id);
dec's avatar
dec committed
457
        devices->emplace_back(device.release());
december0123's avatar
december0123 committed
458 459
    }

dec's avatar
dec committed
460
    hd_free_hd_list(hd);
dec's avatar
dec committed
461
    hd_free_hd_data(hd_data.get());
Philip Müller's avatar
Philip Müller committed
462 463
}

464
void Data::fillAllConfigs(std::string type)
Philip Müller's avatar
Philip Müller committed
465
{
december0123's avatar
december0123 committed
466
    std::vector<std::string> configPaths;
december0123's avatar
december0123 committed
467
    std::vector<std::shared_ptr<Config>>* configs;
december0123's avatar
december0123 committed
468

469
    if ("USB" == type)
december0123's avatar
december0123 committed
470 471 472 473 474 475 476 477 478 479
    {
        configs = &allUSBConfigs;
        configPaths = getRecursiveDirectoryFileList(MHWD_USB_CONFIG_DIR, MHWD_CONFIG_NAME);
    }
    else
    {
        configs = &allPCIConfigs;
        configPaths = getRecursiveDirectoryFileList(MHWD_PCI_CONFIG_DIR, MHWD_CONFIG_NAME);
    }

480
    for (auto&& configPath = configPaths.begin();
december0123's avatar
december0123 committed
481 482
            configPath != configPaths.end(); ++configPath)
    {
dec's avatar
dec committed
483
        std::unique_ptr<Config> config{new Config((*configPath), type)};
december0123's avatar
december0123 committed
484 485 486

        if (config->readConfigFile((*configPath)))
        {
dec's avatar
dec committed
487
            configs->emplace_back(config.release());
december0123's avatar
december0123 committed
488 489 490
        }
        else
        {
dec's avatar
dec committed
491
            invalidConfigs.emplace_back(config.release());
december0123's avatar
december0123 committed
492 493
        }
    }
Philip Müller's avatar
Philip Müller committed
494 495
}

496
std::vector<std::string> Data::getRecursiveDirectoryFileList(const std::string& directoryPath,
december0123's avatar
december0123 committed
497
        std::string onlyFilename)
Philip Müller's avatar
Philip Müller committed
498
{
december0123's avatar
december0123 committed
499
    std::vector<std::string> list;
december0123's avatar
december0123 committed
500
    struct dirent *dir = nullptr;
dec's avatar
dec committed
501
    DIR* d = opendir(directoryPath.c_str());
december0123's avatar
december0123 committed
502 503
    if (d)
    {
504
        while (nullptr != (dir = readdir(d)))
december0123's avatar
december0123 committed
505
        {
506
            std::string filename = dir->d_name;
december0123's avatar
december0123 committed
507 508
            std::string filepath = directoryPath + "/" + filename;

509
            if (("." == filename) || (".." == filename) || ("" == filename))
december0123's avatar
december0123 committed
510 511 512 513 514 515 516 517
            {
                continue;
            }
            else
            {
                struct stat filestatus;
                lstat(filepath.c_str(), &filestatus);

december0123's avatar
december0123 committed
518 519
                if (S_ISREG(filestatus.st_mode) &&
                        (onlyFilename.empty() || (onlyFilename == filename)))
december0123's avatar
december0123 committed
520 521 522 523 524 525 526 527
                {
                    list.push_back(filepath);
                }
                else if (S_ISDIR(filestatus.st_mode))
                {
                    std::vector<std::string> templist = getRecursiveDirectoryFileList(filepath,
                            onlyFilename);

528
                    for (auto&& iterator = templist.begin();
december0123's avatar
december0123 committed
529 530 531 532 533 534 535 536 537 538
                            iterator != templist.end(); iterator++)
                    {
                        list.push_back((*iterator));
                    }
                }
            }
        }

        closedir(d);
    }
december0123's avatar
december0123 committed
539
    delete dir;
december0123's avatar
december0123 committed
540
    return list;
Philip Müller's avatar
Philip Müller committed
541 542 543 544
}

Vita::string Data::getRightConfigPath(Vita::string str, Vita::string baseConfigPath)
{
december0123's avatar
december0123 committed
545
    str = str.trim();
546
    if ((str.size() <= 0) || (str.substr(0, 1) == "/"))
december0123's avatar
december0123 committed
547 548 549 550
    {
        return str;
    }
    return baseConfigPath + "/" + str;
Philip Müller's avatar
Philip Müller committed
551 552 553 554
}

std::vector<std::string> Data::splitValue(Vita::string str, Vita::string onlyEnding)
{
december0123's avatar
december0123 committed
555 556 557
    std::vector<Vita::string> work = str.toLower().explode(" ");
    std::vector<std::string> final;

558
    for (auto&& iterator = work.begin(); iterator != work.end();
december0123's avatar
december0123 committed
559 560
            iterator++)
    {
561
        if (("" != *iterator) && onlyEnding.empty())
december0123's avatar
december0123 committed
562 563 564
        {
            final.push_back(*iterator);
        }
565 566
        else if (("" != *iterator) && (Vita::string(*iterator).explode(".").back() == onlyEnding)
                && ((*iterator).size() > 5))
december0123's avatar
december0123 committed
567 568 569 570 571 572
        {
            final.push_back(Vita::string(*iterator).substr(0, (*iterator).size() - 5));
        }
    }

    return final;
Philip Müller's avatar
Philip Müller committed
573 574 575 576
}

void Data::updateConfigData()
{
december0123's avatar
december0123 committed
577
    // Clear config vectors in each device element
578
    for (auto&& PCIDevice = PCIDevices.begin();
december0123's avatar
december0123 committed
579 580
            PCIDevice != PCIDevices.end(); PCIDevice++)
    {
december0123's avatar
december0123 committed
581
        (*PCIDevice)->availableConfigs_.clear();
december0123's avatar
december0123 committed
582 583
    }

584
    for (auto&& USBDevice = USBDevices.begin();
december0123's avatar
december0123 committed
585 586
            USBDevice != USBDevices.end(); USBDevice++)
    {
december0123's avatar
december0123 committed
587
        (*USBDevice)->availableConfigs_.clear();
december0123's avatar
december0123 committed
588 589 590 591 592 593 594 595
    }
    allPCIConfigs.clear();
    allUSBConfigs.clear();

    // Refill data
    fillAllConfigs("PCI");
    fillAllConfigs("USB");

december0123's avatar
december0123 committed
596 597
    setMatchingConfigs(PCIDevices, allPCIConfigs, false);
    setMatchingConfigs(USBDevices, allUSBConfigs, false);
december0123's avatar
december0123 committed
598 599 600

    // Update also installed config data
    updateInstalledConfigData();
Philip Müller's avatar
Philip Müller committed
601 602
}

december0123's avatar
december0123 committed
603
void Data::setMatchingConfigs(const std::vector<std::shared_ptr<Device>>& devices,
604
        std::vector<std::shared_ptr<Config>>& configs, bool setAsInstalled)
Philip Müller's avatar
Philip Müller committed
605
{
606
    for (auto&& config = configs.begin(); config != configs.end();
december0123's avatar
december0123 committed
607 608 609 610
            ++config)
    {
        setMatchingConfig((*config), devices, setAsInstalled);
    }
Philip Müller's avatar
Philip Müller committed
611 612
}

613 614
void Data::setMatchingConfig(std::shared_ptr<Config> config,
		const std::vector<std::shared_ptr<Device>>& devices, bool setAsInstalled)
Philip Müller's avatar
Philip Müller committed
615
{
december0123's avatar
december0123 committed
616
    std::vector<std::shared_ptr<Device>> foundDevices;
december0123's avatar
december0123 committed
617

december0123's avatar
december0123 committed
618
    getAllDevicesOfConfig(devices, config, foundDevices);
december0123's avatar
december0123 committed
619 620

    // Set config to all matching devices
621
    for (auto&& foundDevice = foundDevices.begin();
december0123's avatar
december0123 committed
622 623 624 625
            foundDevice != foundDevices.end(); ++foundDevice)
    {
        if (setAsInstalled)
        {
december0123's avatar
december0123 committed
626
            addConfigSorted((*foundDevice)->installedConfigs_, config);
december0123's avatar
december0123 committed
627 628 629
        }
        else
        {
december0123's avatar
december0123 committed
630
            addConfigSorted((*foundDevice)->availableConfigs_, config);
december0123's avatar
december0123 committed
631 632
        }
    }
Philip Müller's avatar
Philip Müller committed
633 634
}

635 636
void Data::addConfigSorted(std::vector<std::shared_ptr<Config>>& configs,
		std::shared_ptr<Config> config)
Philip Müller's avatar
Philip Müller committed
637
{
638
    for (auto&& iterator = configs.begin();
december0123's avatar
december0123 committed
639
            iterator != configs.end(); iterator++)
december0123's avatar
december0123 committed
640 641 642 643 644 645 646
    {
        if (config->name_ == (*iterator)->name_)
        {
            return;
        }
    }

647
    for (auto&& iterator = configs.begin(); iterator != configs.end();
december0123's avatar
december0123 committed
648 649 650 651
            iterator++)
    {
        if (config->priority_ > (*iterator)->priority_)
        {
december0123's avatar
december0123 committed
652
            configs.insert(iterator, std::shared_ptr<Config>(config));
december0123's avatar
december0123 committed
653 654 655 656
            return;
        }
    }

dec's avatar
dec committed
657
    configs.emplace_back(config);
Philip Müller's avatar
Philip Müller committed
658 659
}

december0123's avatar
december0123 committed
660
Vita::string Data::from_Hex(std::uint16_t hexnum, int fill)
Philip Müller's avatar
Philip Müller committed
661
{
december0123's avatar
december0123 committed
662 663 664
    std::stringstream stream;
    stream << std::hex << std::setfill('0') << std::setw(fill) << hexnum;
    return stream.str();
Philip Müller's avatar
Philip Müller committed
665 666
}

667
std::string Data::from_CharArray(char* c)
Philip Müller's avatar
Philip Müller committed
668
{
669
    if (nullptr == c)
december0123's avatar
december0123 committed
670 671 672
    {
        return "";
    }
Philip Müller's avatar
Philip Müller committed
673

674
    return std::string(c);
Philip Müller's avatar
Philip Müller committed
675
}