summaryrefslogtreecommitdiff
path: root/src/manager/initial-values/xml-utils.cpp
blob: 5e10b4322cd12ec11ed6a57fec44c784f2ab4b63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 *  Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License
 *
 *
 * @file        parser.cpp
 * @author      Maciej Karpiuk (m.karpiuk2@samsung.com)
 * @version     1.0
 * @brief       XML parser class implementation.
 */

#include <string>
#include <sstream>
#include <algorithm>
#include <xml-utils.h>

namespace {
const char *const WHITESPACE       = " \n\r\t\v";
const char *const LINE_WHITESPACE  = " \r\t\v";

std::string trim_left(const std::string &s, const char *whitespaces)
{
	size_t startpos = s.find_first_not_of(whitespaces);
	return (startpos == std::string::npos) ? "" : s.substr(startpos);
}

std::string trim_right(const std::string &s, const char *whitespaces)
{
	size_t endpos = s.find_last_not_of(whitespaces);
	return (endpos == std::string::npos) ? "" : s.substr(0, endpos + 1);
}

std::string trim(const std::string &s, const char *whitespaces)
{
	return trim_right(trim_left(s, whitespaces), whitespaces);
}

}

namespace CKM {
namespace XML {

template <typename T>
T removeChars(const T &input, const char *what)
{
	T out(input);
	auto endit = std::remove_if(out.begin(), out.end(),
	[what](char c) {
		for (const char *ptr = what; *ptr; ++ptr)
			if (*ptr == c)
				return true;

		return false;
	});

	out.erase(endit, out.end());
	return out;
}

RawBuffer removeWhiteChars(const RawBuffer &buffer)
{
	return removeChars(buffer, WHITESPACE);
}

std::string trimEachLine(const std::string &input)
{
	std::stringstream ss(input);
	std::stringstream output;
	std::string line;

	while (std::getline(ss, line, '\n')) {
		auto afterTrim = ::trim(line, LINE_WHITESPACE);

		if (!afterTrim.empty())
			output << afterTrim << std::endl;
	}

	return output.str();
}

std::string trim(const std::string &s)
{
	return removeChars(s, WHITESPACE);
}

} // namespace XML
} // namespace CKM