Skip to content

Commit

Permalink
Fix Windows warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanel committed Apr 4, 2024
1 parent 1234a5f commit ecdf8f8
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/http-request/src/curlhandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ CurlHandle::CurlHandle(BestURLPicker bestURLPicker, AbstractMetricGateway *pMetr
std::chrono::duration_cast<milliseconds>(permanentCurlOptions.timeout()).count());
}

#ifdef CCT_MSVC
#ifdef _WIN32
// https://stackoverflow.com/questions/37551409/configure-curl-to-use-default-system-cert-store-on-windows
CurlSetLogIfError(curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA);
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/objects/include/exchangeconfigparser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class TopLevelOption {
}

vector<ValueType> ret;
ret.reserve(optValIt->size());
ret.reserve(static_cast<vector<ValueType>::size_type>(optValIt->size()));
for (const auto& val : *optValIt) {
ret.emplace_back(val.get<std::string_view>());
}
Expand Down
4 changes: 2 additions & 2 deletions src/objects/src/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ int File::write(const json& data, Writer::Mode mode) const {
if (data.empty()) {
static constexpr std::string_view kEmptyJsonStr = "{}";
fileOfStream << kEmptyJsonStr << '\n';
return kEmptyJsonStr.length() + 1;
return static_cast<int>(kEmptyJsonStr.length()) + 1;
}
const int indent = mode == Writer::Mode::FromStart ? 2 : -1;
string outStr = data.dump(indent);
fileOfStream << outStr << '\n';
return outStr.length() + 1;
return static_cast<int>(outStr.length()) + 1;
} catch (const std::exception& e) {
if (_ifError == IfError::kThrow) {
throw e;
Expand Down
3 changes: 2 additions & 1 deletion src/objects/src/logginginfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ LoggingInfo::LoggingInfo(WithLoggersCreation withLoggersCreation, std::string_vi

const json &activityTrackingPart = generalConfigJsonLogPart["activityTracking"];
const json &commandTypes = activityTrackingPart["commandTypes"];
_trackedCommandTypes.reserve(commandTypes.size());

_trackedCommandTypes.reserve(static_cast<decltype(_trackedCommandTypes)::size_type>(commandTypes.size()));
std::ranges::transform(
commandTypes, std::inserter(_trackedCommandTypes, _trackedCommandTypes.end()),
[](const json &elem) { return CoincenterCommandTypeFromString(elem.get<std::string_view>()); });
Expand Down
3 changes: 2 additions & 1 deletion src/tech/include/durationstring.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "timedef.hpp"

namespace cct {

/// Parse given string representation of a duration and return the duration.
/// Amounts and units may be separated by spaces. For example:
/// "1h45min" is allowed, as well as "1h 45min" and "1 h 45 min "
Expand All @@ -16,4 +17,4 @@ Duration ParseDuration(std::string_view durationStr);
/// "1y6mon" instead of "1y 6mon" will be returned
string DurationToString(Duration dur);

} // namespace cct
} // namespace cct
8 changes: 4 additions & 4 deletions src/tech/include/gethostname.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#pragma once

#include "cct_config.hpp"
#include "cct_string.hpp"

namespace cct {

class HostNameGetter {
public:
#ifdef CCT_MSVC
#ifdef _WIN32
HostNameGetter();
#else
HostNameGetter() noexcept = default;
Expand All @@ -17,14 +17,14 @@ class HostNameGetter {
HostNameGetter(HostNameGetter &&) = delete;
HostNameGetter &operator=(HostNameGetter &&) = delete;

#ifdef CCT_MSVC
#ifdef _WIN32
~HostNameGetter();
#else
~HostNameGetter() = default;
#endif

/// Safe version of gethostname, working in POSIX and Windows with similar behavior.
string getHostName() const;
[[nodiscard]] string getHostName() const;
};

} // namespace cct
3 changes: 2 additions & 1 deletion src/tech/include/simpletable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <compare>
#include <cstdint>
#include <ostream>
#include <span>
#include <string_view>
#include <type_traits>
#include <utility>
Expand Down Expand Up @@ -48,6 +47,8 @@ namespace table {

/// Cell in a SimpleTable on a single line.
/// Can currently hold only 4 types of values: a string, a string_view, a int64_t and a bool.
/// Note: if you pass a const char * or a string_view, a string_view will be stored in the cell.
/// Make sure that the lifetime of the data it points to extends the lifetime of this cell.
class CellLine {
public:
#ifdef CCT_MSVC
Expand Down
10 changes: 5 additions & 5 deletions src/tech/src/durationstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

#include <algorithm>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <string_view>
#include <type_traits>
#include <utility>

#include "cct_cctype.hpp"
Expand Down Expand Up @@ -39,10 +39,10 @@ Duration ParseDuration(std::string_view durationStr) {
"Cannot parse time duration. Accepted time units are 'y (years), mon (months), w (weeks), d (days), h (hours), "
"min (minutes), s (seconds), ms (milliseconds) and us (microseconds)'";

const std::size_t sz = durationStr.size();
const auto sz = durationStr.size();
Duration ret{};
for (std::size_t charPos = 0; charPos < sz;) {
const std::size_t intFirst = charPos;
for (std::remove_const_t<decltype(sz)> charPos = 0; charPos < sz;) {
const auto intFirst = charPos;

while (charPos < sz && isdigit(durationStr[charPos])) {
++charPos;
Expand All @@ -56,7 +56,7 @@ Duration ParseDuration(std::string_view durationStr) {
while (charPos < sz && isspace(durationStr[charPos])) {
++charPos;
}
const std::size_t unitFirst = charPos;
const auto unitFirst = charPos;
while (charPos < sz && islower(durationStr[charPos])) {
++charPos;
}
Expand Down
10 changes: 4 additions & 6 deletions src/tech/src/gethostname.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#include "gethostname.hpp"

// NOLINTNEXTLINE(misc-include-cleaner)
#include "cct_config.hpp"
#include "cct_exception.hpp"
#include "cct_string.hpp"
#ifdef CCT_MSVC
#ifdef _WIN32
#include <Winsock2.h>
#include <winsock.h>
#else
Expand All @@ -14,7 +12,7 @@
#endif

namespace cct {
#ifdef CCT_MSVC
#ifdef _WIN32
HostNameGetter::HostNameGetter() {
WSADATA wsaData;
WORD wVersionRequested = MAKEWORD(2, 2);
Expand All @@ -36,7 +34,7 @@ string HostNameGetter::getHostName() const {
do {
auto errorCode = ::gethostname(hostname.data(), hostname.size() - 1U);
if (errorCode != 0) {
#ifdef CCT_MSVC
#ifdef _WIN32
// In Windows, too small buffer returns an error WSAEFAULT
if (WSAGetLastError() == WSAEFAULT) {
#else
Expand All @@ -49,7 +47,7 @@ string HostNameGetter::getHostName() const {
}
throw exception("Error {} in gethostname", errorCode);
}
#ifndef CCT_MSVC
#ifndef _WIN32
if (hostname.back() != '\0') {
// In POSIX, if the null-terminated hostname is too large to fit, then the name is truncated, and no error is
// returned, meaning that last char has necessarily been written to
Expand Down
2 changes: 1 addition & 1 deletion src/tech/src/simpletable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ CellLine::size_type CellLine::width() const {
if constexpr (std::is_same_v<T, string_type> || std::is_same_v<T, std::string_view>) {
return val.length();
} else if constexpr (std::is_same_v<T, bool>) {
return val ? kBoolValueTrue.length() : kBoolValueFalse.length();
return static_cast<CellLine::size_type>(val ? kBoolValueTrue.length() : kBoolValueFalse.length());
} else if constexpr (std::is_integral_v<T>) {
return nchars(val);
} else {
Expand Down
2 changes: 0 additions & 2 deletions src/tech/test/cct_exception_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ TEST(CctExceptionTest, MoveStringConstructorLong) {
"I can test if the exception stores it correctly.");
}

#ifndef CCT_MSVC
TEST(CctExceptionTest, Format) {
EXPECT_STREQ(exception("Unknown currency code {}. Please enter a real one.", "BABYDOGE").what(),
"Unknown currency code BABYDOGE. Please enter a real one.");
}
#endif

} // namespace cct

0 comments on commit ecdf8f8

Please sign in to comment.