check_conversion_defs.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Boost.Units - A C++ library for zero-overhead dimensional analysis and
  2. // unit/quantity manipulation and conversion
  3. //
  4. // Copyright (C) 2003-2008 Matthias Christian Schabel
  5. // Copyright (C) 2007-2008 Steven Watanabe
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See
  8. // accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #include <iterator>
  11. #include <string>
  12. #include <utility>
  13. #include <set>
  14. #include <map>
  15. #include <iostream>
  16. #include <boost/filesystem.hpp>
  17. #include <boost/filesystem/fstream.hpp>
  18. #include <boost/regex.hpp>
  19. namespace filesystem = boost::filesystem;
  20. //struct stop {
  21. // stop() { char c; std::cin >> c; }
  22. //} stop_;
  23. boost::regex whitespace("\\s*");
  24. boost::regex blank_line("\\A(?://.*$|\\s)*");
  25. boost::regex include_guard("#ifndef (\\w+)\n#define \\1\n");
  26. boost::regex base_unit("(\\w*_base_unit)(?:;| :)");
  27. std::pair<std::string, std::string> get_base_unit_and_include_guard(const filesystem::path& path) {
  28. filesystem::ifstream in(path);
  29. std::string contents(std::istreambuf_iterator<char>(in.rdbuf()), std::istreambuf_iterator<char>());
  30. in.close();
  31. boost::smatch include_guard_match;
  32. boost::regex_search(contents, include_guard_match, include_guard);
  33. boost::smatch base_unit_match;
  34. boost::regex_search(contents, base_unit_match, base_unit);
  35. std::cout << "creating map entry: " << base_unit_match[1].str() << " -> "<< include_guard_match[1].str() << std::endl;
  36. return(std::make_pair(base_unit_match[1].str(), include_guard_match[1].str()));
  37. }
  38. int main() {
  39. std::cout << "In main" << std::endl;
  40. std::map<std::string, std::string> include_guards;
  41. for(filesystem::directory_iterator begin(filesystem::path("../../../boost/units/systems/base_units")), end; begin != end; ++begin) {
  42. if(begin->status().type() == filesystem::regular_file) {
  43. std::cout << "reading file: " << begin->path() << std::endl;
  44. include_guards.insert(get_base_unit_and_include_guard(begin->path()));
  45. }
  46. }
  47. std::cout << "reading conversions file" << std::endl;
  48. filesystem::ifstream conversions_file(filesystem::path("../../../boost/units/systems/base_units/detail/conversions.hpp"));
  49. std::string line;
  50. int line_count = 0;
  51. boost::smatch match;
  52. std::set<std::string> conversion_guards;
  53. try {
  54. boost::regex include_guard_regex("#if defined\\((\\w+)\\) && defined\\((\\w+)\\) &&\\\\");
  55. std::cout << __LINE__ << std::endl;
  56. boost::regex conversion_guard_regex(" !defined\\((\\w+)\\)");
  57. std::cout << __LINE__ << std::endl;
  58. boost::regex set_conversion_guard(" #define (\\w+)");
  59. std::cout << __LINE__ << std::endl;
  60. boost::regex include_conversion(" #include <boost/units/conversion.hpp>");
  61. std::cout << __LINE__ << std::endl;
  62. boost::regex include_absolute(" #include <boost/units/absolute.hpp>");
  63. std::cout << __LINE__ << std::endl;
  64. boost::regex define_conversion_factor(" BOOST_UNITS_DEFINE_CONVERSION_FACTOR\\(boost::units::(\\w+_base_unit), boost::units::(\\w+_base_unit), \\w+, (?:[\\d\\.e\\-/ ]*|one\\(\\))\\);");
  65. std::cout << __LINE__ << std::endl;
  66. boost::regex define_conversion_offset(" BOOST_UNITS_DEFINE_CONVERSION_OFFSET\\(boost::units::(\\w+_base_unit), boost::units::(\\w+_base_unit), \\w+, [\\d\\.e+\\* \\-/]*\\);");
  67. std::cout << __LINE__ << std::endl;
  68. boost::regex endif("#endif");
  69. std::cout << __LINE__ << std::endl;
  70. while(std::getline(conversions_file, line)) {
  71. ++line_count;
  72. std::cout << "on line: " << line_count << std::endl;
  73. if(boost::regex_match(line, match, blank_line)) {
  74. continue;
  75. } else if(boost::regex_match(line, match, include_guard_regex)) {
  76. std::string guard1, guard2, unit1, unit2, conversion_guard;
  77. bool uses_absolute = false;
  78. guard1 = match[1].str();
  79. guard2 = match[2].str();
  80. if(!std::getline(conversions_file, line)) { std::cerr << "unexpected end of file." << std::endl; return(1); } ++line_count;
  81. if(!boost::regex_match(line, match, conversion_guard_regex)) { std::cerr << "error on line: " << line_count << std::endl; return(1); }
  82. conversion_guard = match[1].str();
  83. if(!conversion_guards.insert(conversion_guard).second){ std::cerr << "error on line: " << line_count << std::endl; return(1); }
  84. if(!std::getline(conversions_file, line)) { std::cerr << "unexpected end of file." << std::endl; return(1); } ++line_count;
  85. if(!boost::regex_match(line, match, set_conversion_guard)) { std::cerr << "error on line: " << line_count << std::endl; return(1); }
  86. std::cout << __LINE__ << std::endl;
  87. if(match[1].str() != conversion_guard) { std::cerr << "error on line: " << line_count << std::endl; return(1); }
  88. if(!std::getline(conversions_file, line)) { std::cerr << "unexpected end of file." << std::endl; return(1); } ++line_count;
  89. if(!boost::regex_match(line, match, include_conversion)) { std::cerr << "error on line: " << line_count << std::endl; return(1); }
  90. std::cout << __LINE__ << std::endl;
  91. if(!std::getline(conversions_file, line)) { std::cerr << "unexpected end of file." << std::endl; return(1); } ++line_count;
  92. if(boost::regex_match(line, match, include_absolute)) {
  93. uses_absolute = true;
  94. if(!std::getline(conversions_file, line)) { std::cerr << "unexpected end of file." << std::endl; return(1); } ++line_count;
  95. }
  96. std::cout << __LINE__ << std::endl;
  97. if(!boost::regex_match(line, match, define_conversion_factor)) { std::cerr << "error on line: " << line_count << std::endl; return(1); }
  98. std::cout << __LINE__ << ": " << line << std::endl;
  99. unit1 = match[1].str();
  100. unit2 = match[2].str();
  101. if(!((include_guards[unit1] == guard1 && include_guards[unit2] == guard2) ||
  102. (include_guards[unit1] == guard2 && include_guards[unit2] == guard1))) {
  103. std::cerr << "guard1: " << guard1 << std::endl;
  104. std::cerr << "guard2: " << guard2 << std::endl;
  105. std::cerr << "unit1: " << unit1 << std::endl;
  106. std::cerr << "unit2: " << unit2 << std::endl;
  107. std::cerr << "include_guards[unit1]: " << include_guards[unit1] << std::endl;
  108. std::cerr << "include_guards[unit2]: " << include_guards[unit2] << std::endl;
  109. { std::cerr << "error on line: " << line_count << std::endl; return(1); }
  110. }
  111. std::cout << __LINE__ << std::endl;
  112. if(!std::getline(conversions_file, line)) { std::cerr << "unexpected end of file." << std::endl; return(1); } ++line_count;
  113. std::cout << __LINE__ << std::endl;
  114. if(boost::regex_match(line, match, define_conversion_offset)) {
  115. if(!uses_absolute) { std::cerr << "error on line: " << line_count << std::endl; return(1); }
  116. std::cout << __LINE__ << std::endl;
  117. if(match[1].str() != unit1 || match[2].str() != unit2) { std::cerr << "error on line: " << line_count << std::endl; return(1); }
  118. if(!std::getline(conversions_file, line)) { std::cerr << "unexpected end of file." << std::endl; return(1); } ++line_count;
  119. } else {
  120. if(uses_absolute) { std::cerr << "error on line: " << line_count << std::endl; return(1); }
  121. }
  122. std::cout << __LINE__ << std::endl;
  123. if(!boost::regex_match(line, match, endif)) { std::cerr << "error on line: " << line_count << std::endl; return(1); }
  124. }
  125. }
  126. } catch(std::exception& e) {
  127. std::cerr << e.what() << std::endl;
  128. return(1);
  129. }
  130. }