Skip to content

Commit

Permalink
oopsie: add missing files
Browse files Browse the repository at this point in the history
  • Loading branch information
willeccles committed May 10, 2024
1 parent 7fae11c commit f40ccf3
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 0 deletions.
94 changes: 94 additions & 0 deletions src/ScpiUtil.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#ifndef ABS_SCPI_DRIVER_SRC_SCPIUTIL_H
#define ABS_SCPI_DRIVER_SRC_SCPIUTIL_H

#include <bci/abs/CommonTypes.h>

#include <ranges>
#include <span>
#include <string_view>

#include "StringUtil.h"
#include "Util.h"

namespace bci::abs::scpi {

template <std::size_t kLen>
constexpr ErrorCode SplitRespFloats(std::string_view resp,
std::span<float, kLen> out) {
resp = util::Trim(resp);

std::size_t i = 0;
for (const auto val : std::views::split(resp, ',')) {
if (i >= out.size()) {
return ErrorCode::kInvalidResponse;
}

if (auto v =
util::StrViewToFloat(std::string_view(val.begin(), val.end()))) {
out[i++] = *v;
} else {
return ErrorCode::kInvalidResponse;
}
}

if (i < out.size()) {
return ErrorCode::kInvalidResponse;
}

return ErrorCode::kSuccess;
}

template <std::size_t kLen>
constexpr ErrorCode SplitRespFloats(std::string_view resp,
std::array<float, kLen>& out) {
return SplitRespFloats(resp, std::span{out});
}

template <std::size_t kLen>
ErrorCode SplitRespMnemonics(std::string_view resp,
std::array<std::string, kLen>& out) {
resp = util::Trim(resp);

std::size_t i = 0;
for (const auto val : std::views::split(resp, ',')) {
if (i >= out.size()) {
return ErrorCode::kInvalidResponse;
}

out[i++] = std::string(val.begin(), val.end());
}

if (i < out.size()) {
return ErrorCode::kInvalidResponse;
}
}

template <std::size_t kLen>
constexpr ErrorCode SplitRespMnemonics(
std::string_view resp, std::array<std::string_view, kLen>& out) {
resp = util::Trim(resp);

std::size_t i = 0;
for (const auto val : std::views::split(resp, ',')) {
if (i >= out.size()) {
return ErrorCode::kInvalidResponse;
}

out[i++] = std::string_view(val.begin(), val.end());
}

if (i < out.size()) {
return ErrorCode::kInvalidResponse;
}
}

constexpr Result<float> ParseFloatResponse(std::string_view text) {
if (auto f = util::StrViewToFloat(text)) {
return *f;
}
return util::Err(ErrorCode::kInvalidResponse);
}

} // namespace bci::abs::scpi

#endif /* ABS_SCPI_DRIVER_SRC_SCPIUTIL_H */
50 changes: 50 additions & 0 deletions src/StringUtil.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#ifndef ABS_SCPI_DRIVER_SRC_STRINGUTIL_H
#define ABS_SCPI_DRIVER_SRC_STRINGUTIL_H

#include <algorithm>
#include <optional>
#include <string_view>
#include <system_error>

#include <fast_float/fast_float.h>

namespace bci::abs::util {

inline constexpr std::string_view kTrimChars{" \t\v\r\n\0"};

inline constexpr std::string_view Trim(std::string_view v) noexcept {
if (v.size() == 0) {
return v;
}

auto s = v.find_first_not_of(kTrimChars);
v.remove_prefix((std::min)(s, v.size()));
if (v.size() <= 1) {
return v;
}

s = v.find_last_not_of(kTrimChars);
if (s != v.npos) {
v.remove_suffix(v.size() - s - 1);
}

return v;
}

inline constexpr std::optional<float> StrViewToFloat(std::string_view sv) {
if (sv.empty()) {
return std::nullopt;
}

float f;
auto [ptr, ec] = fast_float::from_chars(sv.data(), sv.data() + sv.size(), f);
if (ec == std::errc()) {
return f;
} else {
return std::nullopt;
}
}

} // namespace bci::abs::util

#endif /* ABS_SCPI_DRIVER_SRC_STRINGUTIL_H */

0 comments on commit f40ccf3

Please sign in to comment.