Kea 2.5.8
io_utils.cc
Go to the documentation of this file.
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
7#include <config.h>
8
10#include <testutils/io_utils.h>
11#include <gtest/gtest.h>
12#include <fstream>
13#include <sstream>
14#include <string>
15
16namespace isc {
17namespace test {
18
19bool fileExists(const std::string& file_path) {
20 struct stat statbuf;
21 return(stat(file_path.c_str(), &statbuf) == 0);
22}
23
24std::string readFile(const std::string& file_path) {
25 std::ifstream ifs;
26 ifs.open(file_path.c_str(), std::ifstream::in);
27 if (!ifs.good()) {
28 return (std::string());
29 }
30 std::string buf;
31 std::ostringstream output;
32 while (!ifs.eof() && ifs.good()) {
33 ifs >> buf;
34 output << buf;
35 }
36 ifs.close();
37
38 return (output.str());
39}
40
41std::string decommentJSONfile(const std::string& input_file) {
42
43 using namespace std;
44
45 ifstream f(input_file);
46 if (!f.is_open()) {
47 isc_throw(isc::BadValue, "can't open input file for reading: " + input_file);
48 }
49
50 string outfile;
51 size_t last_slash = input_file.find_last_of("/");
52 if (last_slash != string::npos) {
53 outfile = input_file.substr(last_slash + 1);
54 } else {
55 outfile = input_file;
56 }
57 outfile += "-decommented";
58
59 ofstream out(outfile);
60 if (!out.is_open()) {
61 isc_throw(isc::BadValue, "can't open output file for writing: " + input_file);
62 }
63
64 bool in_comment = false;
65 string line;
66 while (std::getline(f, line)) {
67 // First, let's get rid of the # comments
68 size_t hash_pos = line.find("#");
69 if (hash_pos != string::npos) {
70 line = line.substr(0, hash_pos);
71 }
72
73 // Second, let's get rid of the // comments
74 // at the beginning or after a control character.
75 size_t dblslash_pos = line.find("//");
76 if ((dblslash_pos != string::npos) &&
77 ((dblslash_pos == 0) ||
78 ((unsigned) line[dblslash_pos - 1] <= 32))) {
79 line = line.substr(0, dblslash_pos);
80 }
81
82 // Now the tricky part: c comments.
83 size_t begin_pos = line.find("/*");
84 size_t end_pos = line.find("*/");
85 if (in_comment && end_pos == string::npos) {
86 // we continue through multiline comment
87 line = "";
88 } else {
89
90 if (begin_pos != string::npos) {
91 in_comment = true;
92 if (end_pos != string::npos) {
93 // single line comment. Let's get rid of the content in between
94 line = line.replace(begin_pos, end_pos + 2, end_pos + 2 - begin_pos, ' ');
95 in_comment = false;
96 } else {
97 line = line.substr(0, begin_pos);
98 }
99 } else {
100 if (in_comment && end_pos != string::npos) {
101 line = line.replace(0, end_pos +2 , end_pos + 2, ' ');
102 in_comment = false;
103 }
104 }
105 }
106
107 // Finally, write the line to the output file.
108 out << line << endl;
109 }
110 f.close();
111 out.close();
112
113 return (outfile);
114}
115
116}; // end of isc::test namespace
117}; // end of isc namespace
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
std::string decommentJSONfile(const std::string &input_file)
Removes comments from a JSON file.
Definition: io_utils.cc:41
bool fileExists(const std::string &file_path)
Checks if specified file exists.
Definition: io_utils.cc:19
std::string readFile(const std::string &file_path)
Reads contents of the specified file.
Definition: io_utils.cc:24
Defines the logger used by the top-level component of kea-lfc.