io_utils.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright (C) 2015,2017 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <exceptions/exceptions.h>
  7. #include <testutils/io_utils.h>
  8. #include <gtest/gtest.h>
  9. #include <fstream>
  10. #include <sstream>
  11. #include <string>
  12. namespace isc {
  13. namespace dhcp {
  14. namespace test {
  15. bool fileExists(const std::string& file_path) {
  16. std::ifstream fs(file_path.c_str());
  17. const bool file_exists = fs.good();
  18. fs.close();
  19. return (file_exists);
  20. }
  21. std::string readFile(const std::string& file_path) {
  22. std::ifstream ifs;
  23. ifs.open(file_path.c_str(), std::ifstream::in);
  24. if (!ifs.good()) {
  25. return (std::string());
  26. }
  27. std::string buf;
  28. std::ostringstream output;
  29. while (!ifs.eof() && ifs.good()) {
  30. ifs >> buf;
  31. output << buf;
  32. }
  33. ifs.close();
  34. return (output.str());
  35. }
  36. std::string decommentJSONfile(const std::string& input_file) {
  37. using namespace std;
  38. ifstream f(input_file);
  39. if (!f.is_open()) {
  40. isc_throw(isc::BadValue, "can't open input file for reading: " + input_file);
  41. }
  42. string outfile;
  43. size_t last_slash = input_file.find_last_of("/");
  44. if (last_slash != string::npos) {
  45. outfile = input_file.substr(last_slash + 1);
  46. } else {
  47. outfile = input_file;
  48. }
  49. outfile += "-decommented";
  50. ofstream out(outfile);
  51. if (!out.is_open()) {
  52. isc_throw(isc::BadValue, "can't open output file for writing: " + input_file);
  53. }
  54. bool in_comment = false;
  55. string line;
  56. while (std::getline(f, line)) {
  57. // First, let's get rid of the # comments
  58. size_t hash_pos = line.find("#");
  59. if (hash_pos != string::npos) {
  60. line = line.substr(0, hash_pos);
  61. }
  62. // Second, let's get rid of the // comments
  63. size_t dblslash_pos = line.find("//");
  64. if (dblslash_pos != string::npos) {
  65. line = line.substr(0, dblslash_pos);
  66. }
  67. // Now the tricky part: c comments.
  68. size_t begin_pos = line.find("/*");
  69. size_t end_pos = line.find("*/");
  70. if (in_comment && end_pos == string::npos) {
  71. // we continue through multiline comment
  72. line = "";
  73. } else {
  74. if (begin_pos != string::npos) {
  75. in_comment = true;
  76. if (end_pos != string::npos) {
  77. // sigle line comment. Let's get rid of the content in between
  78. line = line.replace(begin_pos, end_pos + 2, end_pos + 2 - begin_pos, ' ');
  79. in_comment = false;
  80. } else {
  81. line = line.substr(0, begin_pos);
  82. }
  83. } else {
  84. if (in_comment && end_pos != string::npos) {
  85. line = line.replace(0, end_pos +2 , end_pos + 2, ' ');
  86. in_comment = false;
  87. }
  88. }
  89. }
  90. // Finally, write the line to the output file.
  91. out << line << endl;
  92. }
  93. f.close();
  94. out.close();
  95. return (outfile);
  96. }
  97. }; // end of isc::dhcp::test namespace
  98. }; // end of isc::dhcp namespace
  99. }; // end of isc namespace