Newer
Older
/*
* Data.cpp
*
* Created on: 28 sie 2014
* Author: dec
*/
#include "Data.hpp"
#include <dirent.h>
#include <fstream>
#include <iomanip>
#include <sstream>
Data::Data()
{
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
for (auto PCIDevice : PCIDevices)
{
delete PCIDevice;
PCIDevice = nullptr;
}
for (auto USBDevice : USBDevices)
{
delete USBDevice;
USBDevice = nullptr;
}
for (auto installedPCIConfig : installedPCIConfigs)
{
delete installedPCIConfig;
installedPCIConfig = nullptr;
}
for (auto installedUSBConfig : installedUSBConfigs)
{
delete installedUSBConfig;
installedUSBConfig = nullptr;
}
for (auto PCIConfig : allPCIConfigs)
{
delete PCIConfig;
PCIConfig = nullptr;
}
for (auto USBConfig : allUSBConfigs)
{
delete USBConfig;
USBConfig = nullptr;
}
for (auto invalidConfig : invalidConfigs)
{
delete invalidConfig;
invalidConfig = nullptr;
}
PCIDevices.clear();
USBDevices.clear();
installedPCIConfigs.clear();
installedUSBConfigs.clear();
allUSBConfigs.clear();
allPCIConfigs.clear();
invalidConfigs.clear();
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Clear config vectors in each device element
for (std::vector<Device*>::iterator PCIDevice = PCIDevices.begin();
PCIDevice != PCIDevices.end(); PCIDevice++)
{
(*PCIDevice)->installedConfigs.clear();
}
for (std::vector<Device*>::iterator USBDevice = USBDevices.begin();
USBDevice != USBDevices.end(); USBDevice++)
{
(*USBDevice)->installedConfigs.clear();
}
// Clear installed config vectors
for (auto PCIConfig : installedPCIConfigs)
{
delete PCIConfig;
PCIConfig = nullptr;
}
for (auto USBConfig : installedUSBConfigs)
{
delete USBConfig;
USBConfig = nullptr;
}
installedPCIConfigs.clear();
installedUSBConfigs.clear();
// Refill data
fillInstalledConfigs("PCI");
fillInstalledConfigs("USB");
setMatchingConfigs(&PCIDevices, &installedPCIConfigs, true);
setMatchingConfigs(&USBDevices, &installedUSBConfigs, true);
void Data::fillInstalledConfigs(std::string type)
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
std::vector<std::string> configPaths;
std::vector<Config*>* configs;
if (type == "USB")
{
configs = &installedUSBConfigs;
configPaths = getRecursiveDirectoryFileList(MHWD_USB_DATABASE_DIR, MHWD_CONFIG_NAME);
}
else
{
configs = &installedPCIConfigs;
configPaths = getRecursiveDirectoryFileList(MHWD_PCI_DATABASE_DIR, MHWD_CONFIG_NAME);
}
for (std::vector<std::string>::const_iterator configPath = configPaths.begin();
configPath != configPaths.end(); ++configPath)
{
Config *config = new Config((*configPath), type);
if (config->readConfigFile((*configPath)))
{
configs->push_back(config);
}
else
{
invalidConfigs.push_back(config);
}
}
}
void Data::getAllDevicesOfConfig(Config *config, std::vector<Device*>* foundDevices)
{
std::vector<Device*> devices;
if (config->type_ == "USB")
{
devices = USBDevices;
}
else
{
devices = PCIDevices;
}
getAllDevicesOfConfig(&devices, config, foundDevices);
}
void Data::getAllDevicesOfConfig(std::vector<Device*>* devices, Config *config,
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
foundDevices->clear();
for (std::vector<Config::HardwareID>::const_iterator hwdID = config->hwdIDs_.begin();
hwdID != config->hwdIDs_.end(); ++hwdID)
{
bool foundDevice = false;
// Check all devices
for (std::vector<Device*>::iterator i_device = devices->begin(); i_device != devices->end();
++i_device)
{
bool found = false;
// Check class ids
for (std::vector<std::string>::const_iterator classID = hwdID->classIDs.begin();
classID != hwdID->classIDs.end(); ++classID)
{
if (*classID == "*" || *classID == (*i_device)->classID)
{
found = true;
break;
}
}
if (!found)
{
continue;
}
else
{
// Check blacklisted class ids
found = false;
for (std::vector<std::string>::const_iterator blacklistedClassID =
(*hwdID).blacklistedClassIDs.begin();
blacklistedClassID != (*hwdID).blacklistedClassIDs.end(); ++blacklistedClassID)
{
if (*blacklistedClassID == (*i_device)->classID)
{
found = true;
break;
}
}
if (found)
{
continue;
}
else
{
// Check vendor ids
found = false;
for (std::vector<std::string>::const_iterator vendorID = hwdID->vendorIDs.begin();
vendorID != hwdID->vendorIDs.end(); ++vendorID)
{
if (*vendorID == "*" || *vendorID == (*i_device)->vendorID)
{
found = true;
break;
}
}
if (!found)
{
continue;
}
else
{
// Check blacklisted vendor ids
found = false;
for (std::vector<std::string>::const_iterator blacklistedVendorID =
hwdID->blacklistedVendorIDs.begin();
blacklistedVendorID != hwdID->blacklistedVendorIDs.end(); ++blacklistedVendorID)
{
if (*blacklistedVendorID == (*i_device)->vendorID)
{
found = true;
break;
}
}
if (found)
{
continue;
}
else
{
// Check device ids
found = false;
for (std::vector<std::string>::const_iterator deviceID = hwdID->deviceIDs.begin();
deviceID != hwdID->deviceIDs.end(); ++deviceID)
{
if (*deviceID == "*" || *deviceID == (*i_device)->deviceID)
{
found = true;
break;
}
}
if (!found)
{
continue;
}
else
{
// Check blacklisted device ids
found = false;
for (std::vector<std::string>::const_iterator blacklistedDeviceID =
hwdID->blacklistedDeviceIDs.begin();
blacklistedDeviceID != hwdID->blacklistedDeviceIDs.end(); ++blacklistedDeviceID)
{
if (*blacklistedDeviceID == (*i_device)->deviceID)
{
found = true;
break;
}
}
if (found)
{
continue;
}
else
{
foundDevice = true;
foundDevices->push_back((*i_device));
}
}
}
}
}
}
}
if (!foundDevice)
{
foundDevices->clear();
return;
}
}
}
std::vector<Config*> Data::getAllDependenciesToInstall(Config *config)
{
std::vector<Config*> depends;
std::vector<Config*> installedConfigs;
// Get the right configs
if (config->type_ == "USB")
{
installedConfigs = installedUSBConfigs;
}
else
{
installedConfigs = installedPCIConfigs;
}
// Get all depends
getAllDependenciesToInstall(config, &installedConfigs, &depends);
return depends;
}
void Data::getAllDependenciesToInstall(Config *config,
std::vector<Config*>* installedConfigs, std::vector<Config*> *dependencies)
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
for (std::vector<std::string>::const_iterator configDependency = config->dependencies_.begin();
configDependency != config->dependencies_.end(); configDependency++)
{
bool found = false;
for (std::vector<Config*>::const_iterator installedConfig = installedConfigs->begin();
installedConfig != installedConfigs->end(); installedConfig++)
{
if ((*configDependency) == (*installedConfig)->name_)
{
found = true;
break;
}
}
if (found)
continue;
// Check if already in vector
for (std::vector<Config*>::const_iterator dependency = dependencies->begin();
dependency != dependencies->end(); dependency++)
{
if ((*dependency)->name_ == (*configDependency))
{
found = true;
break;
}
}
if (found)
{
continue;
}
else
{
// Add to vector and check for further subdepends...
Config *dependconfig = getDatabaseConfig((*configDependency), config->type_);
if (dependconfig == nullptr)
{
continue;
}
else
{
dependencies->push_back(dependconfig);
getAllDependenciesToInstall(dependconfig, installedConfigs, dependencies);
}
}
}
}
Config* Data::getDatabaseConfig(const std::string configName,
std::vector<Config*> allConfigs;
// Get the right configs
if (configType == "USB")
{
allConfigs = allUSBConfigs;
}
else
{
allConfigs = allPCIConfigs;
}
for (std::vector<Config*>::iterator config = allConfigs.begin(); config != allConfigs.end();
config++)
{
if (configName == (*config)->name_)
{
return (*config);
}
}
return nullptr;
}
std::vector<Config*> Data::getAllLocalConflicts(Config *config)
{
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
std::vector<Config*> conflicts;
std::vector<Config*> dependencies = getAllDependenciesToInstall(config);
std::vector<Config*> installedConfigs;
// Get the right configs
if (config->type_ == "USB")
{
installedConfigs = installedUSBConfigs;
}
else
{
installedConfigs = installedPCIConfigs;
}
dependencies.push_back(config);
for (std::vector<Config*>::const_iterator dependency = dependencies.begin();
dependency != dependencies.end(); dependency++)
{
for (std::vector<std::string>::const_iterator dependencyConflict =
(*dependency)->conflicts_.begin();
dependencyConflict != (*dependency)->conflicts_.end(); dependencyConflict++)
{
for (std::vector<Config*>::const_iterator installedConfig = installedConfigs.begin();
installedConfig != installedConfigs.end(); installedConfig++)
{
if ((*dependencyConflict) != (*installedConfig)->name_)
{
continue;
}
else
{
// Check if already in vector
bool found = false;
for (std::vector<Config*>::const_iterator conflict = conflicts.begin();
conflict != conflicts.end(); conflict++)
{
if ((*conflict)->name_ == (*dependencyConflict))
{
found = true;
break;
}
}
if (found)
{
continue;
}
else
{
conflicts.push_back((*installedConfig));
break;
}
}
}
}
}
return conflicts;
}
std::vector<Config*> Data::getAllLocalRequirements(Config *config)
{
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
std::vector<Config*> requirements;
std::vector<Config*> installedConfigs;
// Get the right configs
if (config->type_ == "USB")
{
installedConfigs = installedUSBConfigs;
}
else
{
installedConfigs = installedPCIConfigs;
}
// Check if this config is required by another installed config
for (std::vector<Config*>::const_iterator installedConfig = installedConfigs.begin();
installedConfig != installedConfigs.end(); installedConfig++)
{
for (std::vector<std::string>::const_iterator dependency =
(*installedConfig)->dependencies_.begin();
dependency != (*installedConfig)->dependencies_.end(); dependency++)
{
if ((*dependency) != config->name_)
{
continue;
}
else
{
// Check if already in vector
bool found = false;
for (std::vector<Config*>::const_iterator requirement = requirements.begin();
requirement != requirements.end(); requirement++)
{
if ((*requirement)->name_ == (*installedConfig)->name_)
{
found = true;
break;
}
}
if (!found)
{
requirements.push_back((*installedConfig));
break;
}
}
}
}
return requirements;
void Data::fillDevices(std::string type)
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
hw_item hw;
std::vector<Device*>* devices;
if (type == "USB")
{
hw = hw_usb;
devices = &USBDevices;
}
else
{
hw = hw_pci;
devices = &PCIDevices;
}
// Get the hardware devices
hd_data_t *hd_data = (hd_data_t*) calloc(1, sizeof *hd_data);
hd_t *hd;
hd = hd_list(hd_data, hw, 1, nullptr);
hd_t *beginningOfhd = hd;
Device *device;
for (; hd; hd = hd->next)
{
device = new Device();
device->type = type;
device->classID = from_Hex(hd->base_class.id, 2) + from_Hex(hd->sub_class.id, 2).toLower();
device->vendorID = from_Hex(hd->vendor.id, 4).toLower();
device->deviceID = from_Hex(hd->device.id, 4).toLower();
device->className = from_CharArray(hd->base_class.name);
device->vendorName = from_CharArray(hd->vendor.name);
device->deviceName = from_CharArray(hd->device.name);
device->sysfsBusID = from_CharArray(hd->sysfs_bus_id);
device->sysfsID = from_CharArray(hd->sysfs_id);
devices->push_back(device);
}
hd_free_hd_list(beginningOfhd);
hd_free_hd_data(hd_data);
free(hd_data);
void Data::fillAllConfigs(std::string type)
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
std::vector<std::string> configPaths;
std::vector<Config*>* configs;
if (type == "USB")
{
configs = &allUSBConfigs;
configPaths = getRecursiveDirectoryFileList(MHWD_USB_CONFIG_DIR, MHWD_CONFIG_NAME);
}
else
{
configs = &allPCIConfigs;
configPaths = getRecursiveDirectoryFileList(MHWD_PCI_CONFIG_DIR, MHWD_CONFIG_NAME);
}
for (std::vector<std::string>::const_iterator configPath = configPaths.begin();
configPath != configPaths.end(); ++configPath)
{
Config *config = new Config((*configPath), type);
if (config->readConfigFile((*configPath)))
{
configs->push_back(config);
}
else
{
invalidConfigs.push_back(config);
}
}
bool Data::fillConfig(Config *config, std::string configPath, std::string type)
config->type_ = type;
config->priority_ = 0;
config->freedriver_ = true;
config->basePath_ = configPath.substr(0, configPath.find_last_of('/'));
config->configPath_ = configPath;
// Add new HardwareIDs group to vector if vector is empty
if (config->hwdIDs_.empty())
{
Config::HardwareID hwdID;
config->hwdIDs_.push_back(hwdID);
}
return readConfigFile(config, config->configPath_);
}
std::vector<std::string> Data::getRecursiveDirectoryFileList(const std::string directoryPath,
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
std::vector<std::string> list;
struct dirent *dir;
DIR *d = opendir(directoryPath.c_str());
if (d)
{
while ((dir = readdir(d)) != nullptr)
{
std::string filename = std::string(dir->d_name);
std::string filepath = directoryPath + "/" + filename;
if (filename == "." || filename == ".." || filename == "")
{
continue;
}
else
{
struct stat filestatus;
lstat(filepath.c_str(), &filestatus);
if (S_ISREG(filestatus.st_mode) && (onlyFilename.empty() || onlyFilename == filename))
{
list.push_back(filepath);
}
else if (S_ISDIR(filestatus.st_mode))
{
std::vector<std::string> templist = getRecursiveDirectoryFileList(filepath,
onlyFilename);
for (std::vector<std::string>::const_iterator iterator = templist.begin();
iterator != templist.end(); iterator++)
{
list.push_back((*iterator));
}
}
}
}
closedir(d);
}
return list;
}
bool Data::readConfigFile(Config *config, std::string configPath)
{
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
std::ifstream file(configPath.c_str(), std::ios::in);
if (!file.is_open())
{
return false;
}
else
{
Vita::string line;
Vita::string key;
Vita::string value;
std::vector<Vita::string> parts;
while (!file.eof())
{
getline(file, line);
size_t pos = line.find_first_of('#');
if (pos != std::string::npos)
{
line.erase(pos);
}
if (line.trim().empty())
{
continue;
}
else
{
parts = line.explode("=");
key = parts.front().trim().toLower();
value = parts.back().trim("\"").trim();
// Read in extern file
if ((value.size() > 1) && (value.substr(0, 1) == ">"))
{
std::ifstream file(
getRightConfigPath(value.substr(1), config->basePath_).c_str(),
std::ios::in);
if (!file.is_open())
{
return false;
}
else
{
Vita::string line;
value.clear();
while (!file.eof())
{
getline(file, line);
size_t pos = line.find_first_of('#');
if (pos != std::string::npos)
{
line.erase(pos);
}
if (line.trim().empty())
{
continue;
}
else
{
value += " " + line.trim();
}
}
value = value.trim();
// remove all multiple spaces
while (value.find(" ") != std::string::npos)
{
value = value.replace(" ", " ");
}
}
}
if (key == "include")
{
readConfigFile(config, getRightConfigPath(value, config->basePath_));
}
else if (key == "name")
{
config->name_ = value.toLower();
}
else if (key == "version")
{
config->version_ = value;
}
else if (key == "info")
{
config->info_ = value;
}
else if (key == "priority")
{
config->priority_ = value.convert<int>();
}
else if (key == "freedriver")
{
value = value.toLower();
if (value == "false")
{
config->freedriver_ = false;
}
else if (value == "true")
{
config->freedriver_ = true;
}
}
else if (key == "classids")
{
// Add new HardwareIDs group to vector if vector is not empty
if (!config->hwdIDs_.back().classIDs.empty())
{
Config::HardwareID hwdID;
config->hwdIDs_.push_back(hwdID);
}
config->hwdIDs_.back().classIDs = splitValue(value);
}
else if (key == "vendorids")
{
// Add new HardwareIDs group to vector if vector is not empty
if (!config->hwdIDs_.back().vendorIDs.empty())
{
Config::HardwareID hwdID;
config->hwdIDs_.push_back(hwdID);
}
config->hwdIDs_.back().vendorIDs = splitValue(value);
}
else if (key == "deviceids")
{
// Add new HardwareIDs group to vector if vector is not empty
if (!config->hwdIDs_.back().deviceIDs.empty())
{
Config::HardwareID hwdID;
config->hwdIDs_.push_back(hwdID);
}
config->hwdIDs_.back().deviceIDs = splitValue(value);
}
else if (key == "blacklistedclassids")
{
config->hwdIDs_.back().blacklistedClassIDs = splitValue(value);
}
else if (key == "blacklistedvendorids")
{
config->hwdIDs_.back().blacklistedVendorIDs = splitValue(value);
}
else if (key == "blacklisteddeviceids")
{
config->hwdIDs_.back().blacklistedDeviceIDs = splitValue(value);
}
else if (key == "mhwddepends")
{
config->dependencies_ = splitValue(value);
}
else if (key == "mhwdconflicts")
{
config->conflicts_ = splitValue(value);
}
}
}
// Append * to all empty vectors
for (Config::HardwareID& hwdID : config->hwdIDs_)
{
if (hwdID.classIDs.empty())
{
hwdID.classIDs.push_back("*");
}
if (hwdID.vendorIDs.empty())
{
hwdID.vendorIDs.push_back("*");
}
if (hwdID.deviceIDs.empty())
{
hwdID.deviceIDs.push_back("*");
}
}
if (config->name_.empty())
{
return false;
}
else
{
return true;
}
}
}
Vita::string Data::getRightConfigPath(Vita::string str, Vita::string baseConfigPath)
{
if (str.size() <= 0 || str.substr(0, 1) == "/")
{
return str;
}
}
std::vector<std::string> Data::splitValue(Vita::string str, Vita::string onlyEnding)
{
std::vector<Vita::string> work = str.toLower().explode(" ");
std::vector<std::string> final;
for (std::vector<Vita::string>::const_iterator iterator = work.begin(); iterator != work.end();
iterator++)
{
if (*iterator != "" && onlyEnding.empty())
{
final.push_back(*iterator);
}
else if (*iterator != "" && Vita::string(*iterator).explode(".").back() == onlyEnding
&& (*iterator).size() > 5)
{
final.push_back(Vita::string(*iterator).substr(0, (*iterator).size() - 5));
}
}
return final;
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
// Clear config vectors in each device element
for (std::vector<Device*>::iterator PCIDevice = PCIDevices.begin();
PCIDevice != PCIDevices.end(); PCIDevice++)
{
(*PCIDevice)->availableConfigs.clear();
}
for (std::vector<Device*>::iterator USBDevice = USBDevices.begin();
USBDevice != USBDevices.end(); USBDevice++)
{
(*USBDevice)->availableConfigs.clear();
}
// Clear installed config vectors
for (auto PCIConfig : allPCIConfigs)
{
delete PCIConfig;
PCIConfig = nullptr;
}
for (auto USBConfig : allUSBConfigs)
{
delete USBConfig;
USBConfig = nullptr;
}
allPCIConfigs.clear();
allUSBConfigs.clear();
// Refill data
fillAllConfigs("PCI");
fillAllConfigs("USB");
setMatchingConfigs(&PCIDevices, &allPCIConfigs, false);
setMatchingConfigs(&USBDevices, &allUSBConfigs, false);
// Update also installed config data
updateInstalledConfigData();
}
void Data::setMatchingConfigs(std::vector<Device*>* devices, std::vector<Config*>* configs,
for (std::vector<Config*>::iterator config = configs->begin(); config != configs->end();
++config)
{
setMatchingConfig((*config), devices, setAsInstalled);
}
}
void Data::setMatchingConfig(Config* config, std::vector<Device*>* devices, bool setAsInstalled)
{
std::vector<Device*> foundDevices;
getAllDevicesOfConfig(devices, config, &foundDevices);
// Set config to all matching devices
for (std::vector<Device*>::iterator foundDevice = foundDevices.begin();
foundDevice != foundDevices.end(); ++foundDevice)
{
if (setAsInstalled)
{
addConfigSorted(&(*foundDevice)->installedConfigs, config);
}
else
{
addConfigSorted(&(*foundDevice)->availableConfigs, config);
}
}
}
void Data::addConfigSorted(std::vector<Config*>* configs, Config* config)
{
for (std::vector<Config*>::const_iterator iterator = configs->begin();
iterator != configs->end(); iterator++)
{
if (config->name_ == (*iterator)->name_)
{
return;
}
}
for (std::vector<Config*>::iterator iterator = configs->begin(); iterator != configs->end();
iterator++)
{
if (config->priority_ > (*iterator)->priority_)
{
configs->insert(iterator, config);
return;
}
}
configs->push_back(config);
}
Vita::string Data::from_Hex(uint16_t hexnum, int fill)
{
std::stringstream stream;
stream << std::hex << std::setfill('0') << std::setw(fill) << hexnum;
return stream.str();