fork(2) download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <stdexcept>
  5.  
  6. std::vector<std::string> parseCppName(std::string line)
  7. {
  8. std::vector<std::string> retVal;
  9. int level = 0;
  10. char closeChars[256];
  11.  
  12. size_t startPart = 0;
  13. for (size_t i = 0; i < line.length(); ++i)
  14. {
  15. if (line[i] == ':' && level == 0)
  16. {
  17. if (i + 1 >= line.length() || line[i + 1] != ':')
  18. throw std::runtime_error("missing :");
  19. retVal.push_back(line.substr(startPart, i - startPart));
  20. startPart = ++i + 1;
  21. }
  22. else if (line[i] == '(') {
  23. closeChars[level++] = ')';
  24. }
  25. else if (line[i] == '<') {
  26. closeChars[level++] = '>';
  27. }
  28. else if (level > 0 && line[i] == closeChars[level - 1]) {
  29. --level;
  30. }
  31. else if (line[i] == '>' || line[i] == ')') {
  32. throw std::runtime_error("Extra )>");
  33. }
  34. }
  35. if (level > 0)
  36. throw std::runtime_error("Missing )>");
  37. retVal.push_back(line.substr(startPart));
  38. return retVal;
  39. }
  40.  
  41. int main() {
  42. std::string line;
  43. while (std::getline(std::cin, line)) {
  44. try {
  45. std::vector<std::string> parts = parseCppName(line);
  46. for (size_t i = 0; i + 2 < parts.size(); ++i)
  47. std::cout << parts[i] << "@";
  48. if (parts.size() > 1)
  49. std::cout << parts[parts.size() - 2] << "@@";
  50. std::cout << parts[parts.size() - 1] << "\n";
  51. } catch (std::runtime_error& ex) {
  52. std::cout << "Error: " << ex.what() << "\nfor: " << line << "\n";
  53. }
  54. }
  55. }
  56.  
Success #stdin #stdout 0s 3068KB
stdin
Ns1::Ns2::A<int, std::vector<int, std::allocator<int> > >::A()
Ns1::Ns2::A<int, std::vector<int, std::allocator<int> > >::bar(Ns1::Ns2::A<int, std::vector<int, std::allocator<int> > > const&)
Ns1::Ns2::A<int, std::vector<int, std::allocator<int> > >::foo(std::vector<int, std::allocator<int> > const&)
Ns1::Ns2::A<int, std::vector<int, std::allocator<int> > >::~A()
stdout
Ns1@Ns2@A<int, std::vector<int, std::allocator<int> > >@@A()
Ns1@Ns2@A<int, std::vector<int, std::allocator<int> > >@@bar(Ns1::Ns2::A<int, std::vector<int, std::allocator<int> > > const&)
Ns1@Ns2@A<int, std::vector<int, std::allocator<int> > >@@foo(std::vector<int, std::allocator<int> > const&)
Ns1@Ns2@A<int, std::vector<int, std::allocator<int> > >@@~A()