Skip to content

Instantly share code, notes, and snippets.

@jbenner-radham
Created December 19, 2013 15:15
Show Gist options
  • Save jbenner-radham/8040730 to your computer and use it in GitHub Desktop.
Save jbenner-radham/8040730 to your computer and use it in GitHub Desktop.
Recursive directory code to "expand" Windows drivers that don't want to inject (e.g. Nvidia and AMD video drivers) using Boost (primarily for "filesystem".)
#include <cstdlib> // printf
#include <iostream>
#include <string>
#include <boost/filesystem.hpp>
#include <boost/system/error_code.hpp>
// Included due to the MinGW bug which inhibits std::to_string() from being invoked.
// e.g. lexical_cast<string>(example_numeric_var)
#include <boost/lexical_cast.hpp>
using namespace boost;
using namespace std;
// g++ -std=c++1y -static <filename> -I C:\lib\boost C:\lib\boost\stage\lib\libboost_filesystem-mgw48-1_55.a C:\lib\boost\stage\lib\libboost_system-mgw48-1_55.a
// ^ Referenced from: http://wiki.codeblocks.org/index.php?title=BoostWindowsQuickRef
#define EXPAND_DIR "__expand__"
#define MAX_NUM_DIR 500
int main(int argc, char* argv[])
{
filesystem::path p;
auto init_path = filesystem::initial_path();
int num_of_dirs = 0;
if (argc < 2) {
p = filesystem::current_path();
} else {
p = (argv[1]);
}
for (filesystem::recursive_directory_iterator iter(p), end; iter != end; ++iter) {
auto path_obj = iter->path();
if (filesystem::is_directory(path_obj)) {
if (path_obj.filename() == EXPAND_DIR) {
continue;
}
cout << "I'm a path! @ " << path_obj << " - " << path_obj.filename() << endl;
++num_of_dirs;
} else if (filesystem::is_regular_file(path_obj)) {
if (path_obj.has_extension() && path_obj.extension() == ".inf") {
cout << "Found an inf in: " << path_obj.parent_path() << endl;
auto expand_path = path_obj.parent_path() /= EXPAND_DIR;
// In this context it changes the operating directory
// e.g. "Establishes the postcondition, as if by POSIX chdir()."
filesystem::current_path(path_obj.parent_path());
filesystem::create_directory(expand_path);
stringstream cmd;
cmd << "expand.exe " << "*.*" << " " << expand_path;
cout << "Command is: " << cmd.str() << endl;
// Invoke with the "std" namespace due to namespace conflicts with Boost.
int sys_code = std::system(cmd.str().c_str());
printf("Expand exit code is: %d\n", sys_code);
continue;
}
}
if (num_of_dirs >= MAX_NUM_DIR) {
break;
}
}
printf("Found %d directories fo rizzle!\n", num_of_dirs);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment