Program Listing for File utils.hpp

Return to documentation for file (include/email/utils.hpp)

// Copyright 2020-2021 Christophe Bedard
//
// 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.

#ifndef EMAIL__UTILS_HPP_
#define EMAIL__UTILS_HPP_

#include <memory>
#include <optional>  // NOLINT cpplint mistakes <optional> for a C system header
#include <stdexcept>
#include <string>
#include <vector>

#include "yaml-cpp/yaml.h"

#include "email/visibility_control.hpp"

namespace email
{
namespace utils
{


template<typename ... Args>
std::string
string_format(const std::string & format, Args... args)
{
  size_t size = snprintf(nullptr, 0, format.c_str(), args ...) + 1;
  if (size <= 0) {  // LCOV_EXCL_LINE
    throw std::runtime_error("Error during formatting.");  // LCOV_EXCL_LINE
  }
  std::unique_ptr<char[]> buf(new char[size]);
  snprintf(buf.get(), size, format.c_str(), args ...);
  return std::string(buf.get(), buf.get() + size - 1);
}


EMAIL_PUBLIC
std::string
get_env_var(const std::string & env_var);


EMAIL_PUBLIC
std::string
get_env_var_or_default(const std::string & env_var, const std::string & default_value);


EMAIL_PUBLIC
std::optional<std::string>
read_file(const std::string & path);


EMAIL_PUBLIC
std::vector<std::string>
split_email_list(const std::string & list, const bool has_space_after_comma = false);


EMAIL_PUBLIC
std::vector<std::string>
split_email_list(const YAML::Node & node);


EMAIL_PUBLIC
std::string
yaml_to_string(const YAML::Node & node);


EMAIL_PUBLIC
std::string
full_url(const std::string & protocol, const std::string & host, const int port);


EMAIL_PUBLIC
std::optional<uint32_t>
optional_stoul(const std::string & str);


EMAIL_PUBLIC
std::optional<int>
optional_stoi(const std::string & str);


EMAIL_PUBLIC
std::optional<int64_t>
optional_stoll(const std::string & str);


EMAIL_PUBLIC
void
thread_get_name(char * name, size_t len);


EMAIL_PUBLIC
void
thread_set_name(const char * name);


EMAIL_PUBLIC
void
thread_append_name(const char * suffix);

}  // namespace utils
}  // namespace email

#endif  // EMAIL__UTILS_HPP_