Skip to content

Instantly share code, notes, and snippets.

@yumetodo
Last active March 2, 2016 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yumetodo/b7d55812a719f8bc81cc to your computer and use it in GitHub Desktop.
Save yumetodo/b7d55812a719f8bc81cc to your computer and use it in GitHub Desktop.
for dumpbin.exe's output
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <cctype>
#include <unordered_map>
#include <vector>
#include <chrono>
namespace detail {
struct string_include {
std::string key;
};
bool operator| (const std::string& str, const string_include& info) {
return std::string::npos != str.find(info.key);
}
}
detail::string_include include(const std::string& key) { return{ key }; }
bool is_instruction(const std::string& str) {
try {
std::size_t not_space_count = 0;
return std::all_of(str.begin(), str.end(), [&not_space_count](const char& c) -> bool {
if (std::isspace(c)) return true;
++not_space_count;
if ('C' == c) return false;
return std::isalpha(c) || ('0' <= c && c <= '9');
}) && not_space_count;
}
catch (const std::exception&) {
std::cerr << "err in function is_instruction(), " << std::endl;
throw;
}
}
int main() {
using std::cout;
using std::endl;
using std::flush;
using std::string;
namespace ch = std::chrono;
try {
const auto process_begin = ch::high_resolution_clock::now();
cout << "loading file..." << flush;
std::ifstream in("Don't_push.asm");
std::ofstream out("Don't_push.asm.analyse.txt");
std::unordered_map<string, std::size_t> instruction_count;
cout
<< "done." << std::endl
<< "analysing..."
<< flush;
std::size_t l = 0;
for (string buf; std::getline(in, buf); ++l) {
try {
if (51 < buf.size() && ' ' == buf[0] && ' ' == buf[1]) {
string instruction = buf.substr(39, 12);
if (is_instruction(instruction)) {
try {
const auto pos = instruction.find_first_of(' ');
if (string::npos != pos) instruction.erase(pos);
}
catch (const std::exception& er) {
std::cerr << er.what() << "code line." << __LINE__ << " file line." << l << endl;
return -1;
}
if (!instruction.empty()) {
try {
if (instruction_count.count(instruction)) {
++instruction_count[instruction];
}
else {
instruction_count[instruction] = 1U;
}
}
catch (const std::exception& er) {
std::cerr << er.what() << "code line." << __LINE__ << " file line." << l << endl;
return -1;
}
}
}
}
else if (buf | include("RAW DATA")) {
break;//ignore RAW DATA and Summary
}
}
catch (const std::exception& er) {
std::cerr << er.what() << "code line." << __LINE__ << " file line." << l << endl;
return -1;
}
}
cout << "done." << endl;
using elem_t = std::pair<string, std::size_t>;
std::vector<elem_t> result;
result.reserve(instruction_count.size());
for (auto&& n : instruction_count) result.push_back(n);
std::sort(result.begin(), result.end(), [](const elem_t& l, const elem_t& r) {
return r.second < l.second || (r.second == l.second && l.first < r.first);
});
cout << "writing result..." << flush;
std::size_t instruction_name_max_len = 0;
for (auto&& n : result) instruction_name_max_len = std::max(instruction_name_max_len, n.first.size());
for (auto&& n : result) out << n.first << string(instruction_name_max_len + 1 - n.first.size(), ' ') << " : " << n.second << endl;
cout << "done." << endl;
const auto process_end = ch::high_resolution_clock::now();
const auto t = process_end - process_begin;
using ch::duration_cast;
cout << "file:" << l << " lines. time" << t.count() << " [ns] (" << duration_cast<ch::milliseconds>(t).count() << " [ms])" << endl;
std::system("pause");
}
catch (const std::exception& er) {
std::cerr << er.what() << endl;
return -1;
}
return 0;
}
@yumetodo
Copy link
Author

yumetodo commented Mar 2, 2016

Copyright (C) 2016 yumetodo

Distributed under the Boost Software License, Version 1.0.
(See http://www.boost.org/LICENSE_1_0.txt)

@yumetodo
Copy link
Author

yumetodo commented Mar 2, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment