giGii

preprocessor

Mar 31st, 2023
922
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.11 KB | None | 0 0
  1. #include <cassert>
  2. #include <filesystem>
  3. #include <fstream>
  4. #include <iostream>
  5. #include <regex>
  6. #include <sstream>
  7. #include <string>
  8. #include <string_view>
  9. #include <vector>
  10.  
  11. using namespace std;
  12. using filesystem::path;
  13.  
  14. path operator""_p(const char* data, std::size_t sz) {
  15.     return path(data, data + sz);
  16. }
  17.  
  18. // напишите эту функцию
  19. bool Preprocess(const path& in_file, const path& out_file, const vector<path>& include_directories);
  20.  
  21. string GetFileContents(string file) {
  22.     ifstream stream(file);
  23.  
  24.     // конструируем string по двум итераторам
  25.     return {(istreambuf_iterator<char>(stream)), istreambuf_iterator<char>()};
  26. }
  27.  
  28. void Test() {
  29.     error_code err;
  30.     filesystem::remove_all("sources"_p, err);
  31.     filesystem::create_directories("sources"_p / "include2"_p / "lib"_p, err);
  32.     filesystem::create_directories("sources"_p / "include1"_p, err);
  33.     filesystem::create_directories("sources"_p / "dir1"_p / "subdir"_p, err);
  34.  
  35.     {
  36.         ofstream file("sources/a.cpp");
  37.         file << "// this comment before include\n"
  38.                 "#include \"dir1/b.h\"\n"
  39.                 "// text between b.h and c.h\n"
  40.                 "#include \"dir1/d.h\"\n"
  41.                 "\n"
  42.                 "int SayHello() {\n"
  43.                 "    cout << \"hello, world!\" << endl;\n"
  44.                 "#   include<dummy.txt>\n"
  45.                 "}\n"sv;
  46.     }
  47.     {
  48.         ofstream file("sources/dir1/b.h");
  49.         file << "// text from b.h before include\n"
  50.                 "#include \"subdir/c.h\"\n"
  51.                 "// text from b.h after include"sv;
  52.     }
  53.     {
  54.         ofstream file("sources/dir1/subdir/c.h");
  55.         file << "// text from c.h before include\n"
  56.                 "#include <std1.h>\n"
  57.                 "// text from c.h after include\n"sv;
  58.     }
  59.     {
  60.         ofstream file("sources/dir1/d.h");
  61.         file << "// text from d.h before include\n"
  62.                 "#include \"lib/std2.h\"\n"
  63.                 "// text from d.h after include\n"sv;
  64.     }
  65.     {
  66.         ofstream file("sources/include1/std1.h");
  67.         file << "// std1\n"sv;
  68.     }
  69.     {
  70.         ofstream file("sources/include2/lib/std2.h");
  71.         file << "// std2\n"sv;
  72.     }
  73.  
  74.     assert((!Preprocess("sources"_p / "a.cpp"_p, "sources"_p / "a.in"_p,
  75.                                   {"sources"_p / "include1"_p,"sources"_p / "include2"_p})));
  76.  
  77.     ostringstream test_out;
  78.     test_out << "// this comment before include\n"
  79.                 "// text from b.h before include\n"
  80.                 "// text from c.h before include\n"
  81.                 "// std1\n"
  82.                 "// text from c.h after include\n"
  83.                 "// text from b.h after include\n"
  84.                 "// text between b.h and c.h\n"
  85.                 "// text from d.h before include\n"
  86.                 "// std2\n"
  87.                 "// text from d.h after include\n"
  88.                 "\n"
  89.                 "int SayHello() {\n"
  90.                 "    cout << \"hello, world!\" << endl;\n"sv;
  91.  
  92.     assert(GetFileContents("sources/a.in"s) == test_out.str());
  93. }
  94.  
  95. int main() {
  96.     Test();
  97. }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment