Skip to content

Instantly share code, notes, and snippets.

@eklitzke
Created April 12, 2012 01:30
Show Gist options
  • Select an option

  • Save eklitzke/2364110 to your computer and use it in GitHub Desktop.

Select an option

Save eklitzke/2364110 to your computer and use it in GitHub Desktop.
#include <assert.h>
#include <ctype.h>
#include <cxxabi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
enum { BUF_SIZE = 1000 };
int main(int argc, char **argv) {
char *buffer = new char[BUF_SIZE];
char *mangle_buf = new char[BUF_SIZE];
size_t mangle_len = BUF_SIZE;
std::vector<std::string> line;
while (true) {
buffer = fgets(buffer, BUF_SIZE, stdin);
if (buffer == NULL) {
break;
}
size_t remaining = strlen(buffer) - 1;
char *word_end = buffer + remaining;
while (remaining > 0) {
char *w = static_cast<char *>(memrchr(buffer, ' ', remaining));
if (w == NULL) {
break;
}
std::string word(w + 1, word_end - w);
if (word.length() >= 2 &&
word[0] == '_' &&
word[1] == 'Z') {
int status;
size_t trim = 0;
printf("word is \"%s\"\n", word.c_str());
fflush(stdout);
while (isspace(word[word.length() - trim - 1])) {
trim++;
}
std::string stripped = word.substr(0, word.length() - trim);
size_t atsign = stripped.find('@');
if (atsign != std::string::npos) {
stripped = stripped.substr(0, atsign);
}
printf("stripped is \"%s\"\n", stripped.c_str());
fflush(stdout);
char *realname = abi::__cxa_demangle(stripped.c_str(), mangle_buf, &mangle_len, &status);
assert(status == 0);
if (status == 0) {
word = realname;
printf("%s\n", word.c_str());
}
}
remaining -= word.length();
word_end = w;
line.push_back(word);
}
std::string out;
std::vector<std::string>::reverse_iterator it;
for (it = line.rbegin(); it != line.rend(); ++it) {
out += *it;
}
printf("%s", out.c_str());
}
delete[] buffer;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment