fork download
  1. #define OPTION_1
  2.  
  3. #ifdef OPTION_1
  4.  
  5. #include <algorithm>
  6. #include <array>
  7. #include <iostream>
  8. #include <string>
  9. #include <string.h>
  10.  
  11. enum {
  12. EXTENSIONTAG_PCX,
  13. EXTENSIONTAG_MNG,
  14. EXTENSIONTAG_SMK
  15. };
  16.  
  17. int main()
  18. {
  19. constexpr std::array<std::pair<const char*, int>, 4> extensions {{
  20. {"pcx", EXTENSIONTAG_PCX},
  21. {"mng", EXTENSIONTAG_MNG},
  22. {"png", EXTENSIONTAG_MNG},
  23. {"smk", EXTENSIONTAG_SMK}
  24. }};
  25.  
  26. for(const std::string file_extension : {"pcx", "mng", "png", "smk", "evil"}) {
  27. try {
  28. const auto found = std::find_if(extensions.cbegin(), extensions.cend(), [&file_extension](const std::pair<const char*, int>& p) {
  29. return strcmp(file_extension.c_str(), p.first) == 0;
  30. });
  31.  
  32. if(found != extensions.cend()) {
  33. //bn_int_tag_set(&extensiontag, found->second);
  34. std::cout << "extension: " << found->first << ", tag: " << found->second << '\n';
  35. } else {
  36. throw std::runtime_error("Unknown file extension (" + file_extension + ")");
  37. }
  38. } catch (const std::exception& e) {
  39. std::cerr << e.what() << '\n';
  40. }
  41. }
  42. }
  43.  
  44. #else
  45.  
  46. #include <algorithm>
  47. #include <map>
  48. #include <iostream>
  49. #include <string>
  50. #include <string.h>
  51.  
  52. enum {
  53. EXTENSIONTAG_PCX,
  54. EXTENSIONTAG_MNG,
  55. EXTENSIONTAG_SMK
  56. };
  57.  
  58. int main()
  59. {
  60. auto cmp = [](const char* lhs, const char* rhs) { return strcmp(lhs, rhs) < 0; };
  61.  
  62. const std::map<const char*, int, decltype(cmp)> extensions {{
  63. {"pcx", EXTENSIONTAG_PCX},
  64. {"mng", EXTENSIONTAG_MNG},
  65. {"png", EXTENSIONTAG_MNG},
  66. {"smk", EXTENSIONTAG_SMK}
  67. }, cmp};
  68.  
  69. for(const std::string file_extension : {"pcx", "mng", "png", "smk", "evil"}) {
  70. try {
  71. // if custom exception is not needed
  72. //bn_int_tag_set(&extensiontag, extensions.at(file_extension.c_str()));
  73.  
  74. // else
  75. const auto found = extensions.find(file_extension.c_str());
  76. if(found != extensions.cend()) {
  77. //bn_int_tag_set(&extensiontag, found->second);
  78. std::cout << "extension: " << found->first << ", tag: " << found->second << '\n';
  79. } else {
  80. throw std::runtime_error("Unknown file extension (" + file_extension + ")");
  81. }
  82.  
  83. } catch (const std::exception& e) {
  84. std::cerr << e.what() << '\n';
  85. }
  86. }
  87. }
  88.  
  89. #endif
  90.  
Success #stdin #stdout #stderr 0s 3460KB
stdin
Standard input is empty
stdout
extension: pcx, tag: 0
extension: mng, tag: 1
extension: png, tag: 1
extension: smk, tag: 2
stderr
Unknown file extension (evil)