123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- // Copyright (C) 2015,2017 Internet Systems Consortium, Inc. ("ISC")
- //
- // This Source Code Form is subject to the terms of the Mozilla Public
- // License, v. 2.0. If a copy of the MPL was not distributed with this
- // file, You can obtain one at http://mozilla.org/MPL/2.0/.
- #include <exceptions/exceptions.h>
- #include <testutils/io_utils.h>
- #include <gtest/gtest.h>
- #include <fstream>
- #include <sstream>
- #include <string>
- namespace isc {
- namespace dhcp {
- namespace test {
- bool fileExists(const std::string& file_path) {
- std::ifstream fs(file_path.c_str());
- const bool file_exists = fs.good();
- fs.close();
- return (file_exists);
- }
- std::string readFile(const std::string& file_path) {
- std::ifstream ifs;
- ifs.open(file_path.c_str(), std::ifstream::in);
- if (!ifs.good()) {
- return (std::string());
- }
- std::string buf;
- std::ostringstream output;
- while (!ifs.eof() && ifs.good()) {
- ifs >> buf;
- output << buf;
- }
- ifs.close();
- return (output.str());
- }
- std::string decommentJSONfile(const std::string& input_file) {
- using namespace std;
- ifstream f(input_file);
- if (!f.is_open()) {
- isc_throw(isc::BadValue, "can't open input file for reading: " + input_file);
- }
- string outfile;
- size_t last_slash = input_file.find_last_of("/");
- if (last_slash != string::npos) {
- outfile = input_file.substr(last_slash + 1);
- } else {
- outfile = input_file;
- }
- outfile += "-decommented";
- ofstream out(outfile);
- if (!out.is_open()) {
- isc_throw(isc::BadValue, "can't open output file for writing: " + input_file);
- }
- bool in_comment = false;
- string line;
- while (std::getline(f, line)) {
- // First, let's get rid of the # comments
- size_t hash_pos = line.find("#");
- if (hash_pos != string::npos) {
- line = line.substr(0, hash_pos);
- }
- // Second, let's get rid of the // comments
- size_t dblslash_pos = line.find("//");
- if (dblslash_pos != string::npos) {
- line = line.substr(0, dblslash_pos);
- }
- // Now the tricky part: c comments.
- size_t begin_pos = line.find("/*");
- size_t end_pos = line.find("*/");
- if (in_comment && end_pos == string::npos) {
- // we continue through multiline comment
- line = "";
- } else {
- if (begin_pos != string::npos) {
- in_comment = true;
- if (end_pos != string::npos) {
- // sigle line comment. Let's get rid of the content in between
- line = line.replace(begin_pos, end_pos + 2, end_pos + 2 - begin_pos, ' ');
- in_comment = false;
- } else {
- line = line.substr(0, begin_pos);
- }
- } else {
- if (in_comment && end_pos != string::npos) {
- line = line.replace(0, end_pos +2 , end_pos + 2, ' ');
- in_comment = false;
- }
- }
- }
- // Finally, write the line to the output file.
- out << line << endl;
- }
- f.close();
- out.close();
- return (outfile);
- }
- }; // end of isc::dhcp::test namespace
- }; // end of isc::dhcp namespace
- }; // end of isc namespace
|