Guest User

Untitled

a guest
Dec 11th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. //---------------------------------------------------------------
  2. // File: GetLogicalDrivers.cpp
  3. // Description: Enumerate all logical drivers in the computer.
  4. // Author: Caio Rodrigues
  5. //--------------------------------------------------------------
  6.  
  7. #include <iostream>
  8. #include <string>
  9. #include <sstream>
  10. #include <deque>
  11. #include <vector>
  12. #include <functional>
  13. #include <Windows.h>
  14.  
  15. using DriverEnumerator = std::function<auto (std::string) -> void>;
  16. auto EnumerateLogicalDriver(DriverEnumerator Consumer) -> void;
  17.  
  18. template<template<class, class> class Container = std::vector>
  19. auto LogicalDriverList()
  20. -> Container<std::string, std::allocator<std::string>>;
  21.  
  22. int main(){
  23. std::puts("Logical Drivers or Disks found in the current installation");
  24. std::puts("------------------------------------------------");
  25.  
  26. EnumerateLogicalDriver(
  27. [](const std::string& name){
  28. std::cout << "Driver = " << name << std::endl;
  29. });
  30.  
  31. auto driverList1 = LogicalDriverList<std::deque>();
  32. std::cout << " *=> Driver list in STL container std::deque = ";
  33. for(const auto& d: driverList1){ std::cout << d << ", "; }
  34. std::cout << std::endl;;
  35.  
  36. auto driverList2 = LogicalDriverList<std::vector>();
  37. std::cout << " *=> Driver list in STL container std::vector = ";
  38. for(const auto& d: driverList2){ std::cout << d << ", "; }
  39. std::cout << std::endl;;
  40.  
  41. return 0;
  42. }
  43.  
  44. //---------------------------------------------------//
  45.  
  46. using DriverEnumerator = std::function<auto (std::string) -> void>;
  47.  
  48. auto EnumerateLogicalDriver(DriverEnumerator Consumer) -> void
  49. {
  50. size_t size = ::GetLogicalDriveStringsA(0, nullptr);
  51. std::string buffer(size, 0x00);
  52. ::GetLogicalDriveStringsA(buffer.size(), &buffer[0]);
  53. std::stringstream ss{buffer};
  54. while(std::getline(ss, buffer, '\0') && !buffer.empty())
  55. Consumer(buffer);
  56. }
  57.  
  58. // Remember: Template always in header files
  59. // The user can choose the type of container used to return
  60. // the computer drivers.
  61. template<template<class, class> class Container = std::vector>
  62. auto LogicalDriverList()
  63. -> Container<std::string, std::allocator<std::string>>
  64. {
  65. Container<std::string, std::allocator<std::string>> list{};
  66. EnumerateLogicalDriver(
  67. [&](const std::string& name){
  68. list.push_back(name);
  69. });
  70. return list;
  71. }
Add Comment
Please, Sign In to add comment