Skip to content

Commit

Permalink
Merge pull request #16 from centreon/MON-5623-non-utf8
Browse files Browse the repository at this point in the history
enh(utf-8): check_string_utf8 implemented on text inputs.
  • Loading branch information
bouda1 committed Jun 17, 2020
2 parents 4dfb1ff + cd2c0ac commit 83e663b
Show file tree
Hide file tree
Showing 11 changed files with 560 additions and 265 deletions.
2 changes: 1 addition & 1 deletion include/com/centreon/engine/check_result.hh
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class check_result {
bool get_early_timeout() const;
void set_early_timeout(bool early_timeout);
std::string const& get_output() const;
void set_output(std::string const& output);
void set_output(std::string const& output, const bool check_encoding);
bool get_exited_ok() const;
void set_exited_ok(bool exited_ok);
bool get_reschedule_check() const;
Expand Down
47 changes: 23 additions & 24 deletions include/com/centreon/engine/commands/result.hh
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
/*
** Copyright 2011-2013 Merethis
**
** This file is part of Centreon Engine.
**
** Centreon Engine is free software: you can redistribute it and/or
** modify it under the terms of the GNU General Public License version 2
** as published by the Free Software Foundation.
**
** Centreon Engine is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Centreon Engine. If not, see
** <http://www.gnu.org/licenses/>.
*/

* Copyright 2011 - 2020 Centreon (https://www.centreon.com/)
*
* 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.
*
* For more information : contact@centreon.com
*
*/
#ifndef CCE_COMMANDS_RESULT_HH
#define CCE_COMMANDS_RESULT_HH

#include <string>

#include "com/centreon/engine/namespace.hh"
#include "com/centreon/process.hh"
#include "com/centreon/timestamp.hh"
Expand All @@ -36,22 +36,21 @@ namespace commands {
* execution time).
*/
class result {
void _internal_copy(result const& right);

public:
result();
result(result const& right);
~result() throw();
~result() noexcept;
result& operator=(result const& right);
bool operator==(result const& right) const throw();
bool operator!=(result const& right) const throw();
bool operator==(result const& right) const noexcept;
bool operator!=(result const& right) const noexcept;
uint64_t command_id;
timestamp end_time;
int exit_code;
process::status exit_status;
timestamp start_time;
std::string output;

private:
void _internal_copy(result const& right);
};
} // namespace commands

Expand Down
55 changes: 29 additions & 26 deletions include/com/centreon/engine/string.hh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <set>
#include <sstream>
#include <string>

#include "com/centreon/engine/namespace.hh"

CCE_BEGIN()
Expand All @@ -37,27 +38,27 @@ namespace string {
bool get_next_line(std::ifstream& stream, std::string& line, unsigned int& pos);

inline char const* chkstr(char const* str) noexcept {
return (str ? str : "\"NULL\"");
return str ? str : "\"NULL\"";
}

inline std::string ctime(time_t const& time) {
char buf[64];
buf[0] = 0;
if (ctime_r(&time, buf))
buf[strlen(buf) - 1] = 0;
return (buf);
return buf;
}

inline char* dup(char const* value) {
if (!value)
return (NULL);
return NULL;
char* buf(new char[strlen(value) + 1]);
return (strcpy(buf, value));
return strcpy(buf, value);
}

inline char* dup(std::string const& value) {
char* buf(new char[value.size() + 1]);
return (strcpy(buf, value.c_str()));
return strcpy(buf, value.c_str());
}

template <typename T>
Expand All @@ -67,25 +68,25 @@ inline char* dup(T value) {
std::string const& str(oss.str());
char* buf(new char[str.size() + 1]);
strcpy(buf, str.c_str());
return (buf);
return buf;
}

template <typename T>
std::string from(T value) {
std::ostringstream oss;
oss << value;
return (oss.str());
return oss.str();
}

inline char const* setstr(char*& buf, char const* value = NULL) {
delete[] buf;
return ((buf = string::dup(value)));
return (buf = string::dup(value));
}

template <typename T>
inline char const* setstr(char*& buf, T value) {
delete[] buf;
return ((buf = string::dup(value)));
return (buf = string::dup(value));
}

bool split(std::string& line, char const** key, char const** value, char delim);
Expand All @@ -102,106 +103,108 @@ void split(std::string const& data,
template <typename T>
inline bool to(char const* str, T& data) {
std::istringstream iss(str);
return ((iss >> data) && iss.eof());
return (iss >> data) && iss.eof();
}

template <>
inline bool to(char const* str, long& data) {
char* end(NULL);
errno = 0;
data = strtol(str, &end, 10);
return (!*end && !errno);
return !*end && !errno;
}

template <>
inline bool to(char const* str, unsigned long& data) {
char* end(NULL);
errno = 0;
data = strtoul(str, &end, 10);
return (!*end && !errno);
return !*end && !errno;
}

template <>
inline bool to(char const* str, bool& data) {
unsigned long tmp;
if (!to(str, tmp))
return (false);
return false;
data = static_cast<bool>(tmp);
return (true);
return true;
}

template <>
inline bool to(char const* str, double& data) {
char* end(NULL);
errno = 0;
data = strtod(str, &end);
return (!*end && !errno);
return !*end && !errno;
}

template <>
inline bool to(char const* str, float& data) {
char* end(NULL);
errno = 0;
data = strtof(str, &end);
return (!*end && !errno);
return !*end && !errno;
}

template <>
inline bool to(char const* str, int& data) {
long tmp;
if (!to(str, tmp) || tmp > std::numeric_limits<int>::max() ||
tmp < std::numeric_limits<int>::min())
return (false);
return false;
data = static_cast<int>(tmp);
return (true);
return true;
}

template <>
inline bool to(char const* str, long double& data) {
char* end(NULL);
errno = 0;
data = strtold(str, &end);
return (!*end && !errno);
return !*end && !errno;
}

template <>
inline bool to(char const* str, long long& data) {
char* end(NULL);
errno = 0;
data = strtoll(str, &end, 10);
return (!*end && !errno);
return !*end && !errno;
}

template <>
inline bool to(char const* str, unsigned int& data) {
unsigned long tmp;
if (!to(str, tmp) || tmp > std::numeric_limits<unsigned int>::max())
return (false);
return false;
data = static_cast<unsigned int>(tmp);
return (true);
return true;
}

template <>
inline bool to(char const* str, unsigned long long& data) {
char* end(NULL);
errno = 0;
data = strtoull(str, &end, 10);
return (!*end && !errno);
return !*end && !errno;
}

template <typename T, typename U>
inline bool to(char const* str, U& data) {
T tmp;
if (!to(str, tmp))
return (false);
return false;
data = static_cast<U>(tmp);
return (true);
return true;
}
std::string& trim(std::string& str) noexcept;
std::string& trim_left(std::string& str) noexcept;
std::string& trim_right(std::string& str) noexcept;
std::string extract_perfdata(std::string const& perfdata, std::string const& metric) noexcept;
std::string extract_perfdata(std::string const& perfdata,
std::string const& metric) noexcept;
std::string& remove_thresholds(std::string& perfdata) noexcept;
std::string check_string_utf8(std::string const& str) noexcept;
} // namespace string

CCE_END()
Expand Down
Loading

0 comments on commit 83e663b

Please sign in to comment.