io_utils.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 test {
  14. bool fileExists(const std::string& file_path) {
  15. struct stat statbuf;
  16. return(stat(file_path.c_str(), &statbuf) == 0);
  17. }
  18. std::string readFile(const std::string& file_path) {
  19. std::ifstream ifs;
  20. ifs.open(file_path.c_str(), std::ifstream::in);
  21. if (!ifs.good()) {
  22. return (std::string());
  23. }
  24. std::string buf;
  25. std::ostringstream output;
  26. while (!ifs.eof() && ifs.good()) {
  27. ifs >> buf;
  28. output << buf;
  29. }
  30. ifs.close();
  31. return (output.str());
  32. }
  33. std::string decommentJSONfile(const std::string& input_file) {
  34. using namespace std;
  35. ifstream f(input_file);
  36. if (!f.is_open()) {
  37. isc_throw(isc::BadValue, "can't open input file for reading: " + input_file);
  38. }
  39. string outfile;
  40. size_t last_slash = input_file.find_last_of("/");
  41. if (last_slash != string::npos) {
  42. outfile = input_file.substr(last_slash + 1);
  43. } else {
  44. outfile = input_file;
  45. }
  46. outfile += "-decommented";
  47. ofstream out(outfile);
  48. if (!out.is_open()) {
  49. isc_throw(isc::BadValue, "can't open output file for writing: " + input_file);
  50. }
  51. bool in_comment = false;
  52. string line;
  53. while (std::getline(f, line)) {
  54. // First, let's get rid of the # comments
  55. size_t hash_pos = line.find("#");
  56. if (hash_pos != string::npos) {
  57. line = line.substr(0, hash_pos);
  58. }
  59. // Second, let's get rid of the // comments
  60. // at the beginning or after a control character.
  61. size_t dblslash_pos = line.find("//");
  62. if ((dblslash_pos != string::npos) &&
  63. ((dblslash_pos == 0) ||
  64. ((unsigned) line[dblslash_pos - 1] <= 32))) {
  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. // single 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::test namespace
  98. }; // end of isc namespace