mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2026-01-13 18:01:43 +01:00
* basic ctb config api for register and bit names * tests for define and definelist pass. yet to implement using them for reg, setbit, clearbit and getbit * improved autocomplete for getbit,setbit, clearbit * validate autocomplete * definelist has no put * updating help * converting char array+int in runtimeerror compiles but throws at runtime.Fixed.Tested for it. Also check if string or int before using getregisterdefinitonbyvalue to see if it threw to call the other function. because both of it can throw and we should differentiate the issues for both * removed std::vector<std::pair<string,int> to std::map<string, int> for defiitions list * Dev/define cmd tie bit to reg (#1328) * strong type * moved everythign to bit_utils class * pybindings * added tests for python * removed duplicates * removed bit names in reg * changed BitPosition to BitAddress * Using define reg/bit from python (#1344) * define_bit, define_addr in python. * setBit/clearBit takes int or addr * added example using bits * split define into 2 commands define_reg and define_bit, definelist into 2: definelist_reg and definelist_bit * allow string for register and bit names in c++ api * refactor from github comments * naming refactoring (getRegisterDefnition to retunr name and address specifically * added marker for 8 cmd tests connected to define, changed macro to static constexpr * changed bitPosition from int to uint32_t * got rid of setbitposition and setaddress, instead overloaded constructor to take in strings so that the conversion from string to bit address members, takes place within the class for easy maintainance in case type changes * Removing implicit conversions: RegisterAddresss and RegisterValue: Removed the implicit conversions. RegisterAddress: Changed member name from address_ to value_ and method as well to value(). RegisterValue: Also added | operator to be able to concatenate with uint32_t. Same in python bindings (but could not find the tests to modify * Allowed concatenation with other RegisterValue, made them all constexpr * fix a ctbConfig test * Maponstack works with integration tests, but need unit tests * tests on mapstack * fixed ctb tests and FixedString being initialized with gibberish * removing parsing from string inside the class RegisterAddress, BitAddress and RegisterValue * updated python bindings * fixed bit utils test * renaming getRegisterDefintiionAddress/Name=>getRegisterAddress/Name and similary for getBitDefinitionAddress/Name * updated python bindings * fix tests (format) * a few python tests added and python bindings corrected * replaceing str with __str__ for bit.cpp * repr reimplemented for bit.cpp * removed make with registerAddress etc * starting server for tests per session and nor module * killprocess throws if no process found-> github runs fails, changed to pkill and not throw * clean shm shouldnt raise, in ci binary not found * ignoring these tests for CI, which fail on CI because simulators are not generated in CI. This is in another PR, where it should work --------- Co-authored-by: Erik Fröjdh <erik.frojdh@gmail.com> Co-authored-by: froejdh_e <erik.frojdh@psi.ch>
100 lines
3.3 KiB
C++
100 lines
3.3 KiB
C++
// SPDX-License-Identifier: LGPL-3.0-or-other
|
|
// Copyright (C) 2021 Contributors to the SLS Detector Package
|
|
#pragma once
|
|
|
|
#include <cassert>
|
|
#include <cstdint>
|
|
#include <cstring>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace sls {
|
|
|
|
/* Implementation of a safe string copy function for setting fields in
|
|
for example the multi sls detector. It tries to copy the size of the
|
|
destination from the source, stopping on '\0'.
|
|
|
|
Warning this will truncate the source string and should be used with care.
|
|
Still this is better than strcpy and a buffer overflow...
|
|
*/
|
|
template <size_t array_size>
|
|
void strcpy_safe(char (&destination)[array_size], const char *source) {
|
|
assert(source != nullptr);
|
|
assert(array_size > strlen(source));
|
|
strncpy(destination, source, array_size - 1);
|
|
destination[array_size - 1] = '\0';
|
|
}
|
|
|
|
template <size_t array_size>
|
|
void strcpy_safe(char (&destination)[array_size], const std::string &source) {
|
|
assert(array_size > source.size());
|
|
strncpy(destination, source.c_str(), array_size - 1);
|
|
destination[array_size - 1] = '\0';
|
|
}
|
|
|
|
// Runtime-checked variant — throws if it won't fit
|
|
template <size_t array_size>
|
|
void strcpy_checked(char (&destination)[array_size], const char *source) {
|
|
if (!source)
|
|
throw std::runtime_error("Null source pointer in strcpy_checked");
|
|
|
|
size_t len = std::strlen(source);
|
|
if (len >= (array_size - 1)) {
|
|
throw std::runtime_error("String length (" + std::to_string(len) +
|
|
") should be less than " +
|
|
std::to_string(array_size - 1) + " chars");
|
|
}
|
|
std::strncpy(destination, source, array_size - 1);
|
|
destination[array_size - 1] = '\0';
|
|
}
|
|
|
|
template <size_t array_size>
|
|
void strcpy_checked(char (&destination)[array_size],
|
|
const std::string &source) {
|
|
if (source.size() >= (array_size - 1)) {
|
|
throw std::runtime_error("String length (" +
|
|
std::to_string(source.size()) +
|
|
") should be less than " +
|
|
std::to_string(array_size - 1) + " chars");
|
|
}
|
|
std::strncpy(destination, source.c_str(), array_size - 1);
|
|
destination[array_size - 1] = '\0';
|
|
}
|
|
|
|
/*
|
|
Removes all occurrences of the specified char from a c string
|
|
Templated on array size to ensure no access after buffer limits.
|
|
*/
|
|
template <size_t array_size> void removeChar(char (&str)[array_size], char ch) {
|
|
int count = 0;
|
|
for (int i = 0; str[i]; i++) {
|
|
if (str[i] != ch)
|
|
str[count++] = str[i];
|
|
if (i == array_size - 1)
|
|
break;
|
|
}
|
|
str[count] = '\0';
|
|
}
|
|
|
|
/*
|
|
Split a string using the specified delimeter and return a vector of strings.
|
|
TODO! Look into switching to absl or a string_view based implementation. Current
|
|
implementation should not be used in a performance critical place.
|
|
*/
|
|
std::vector<std::string> split(const std::string &strToSplit, char delimeter);
|
|
|
|
std::string RemoveUnit(std::string &str);
|
|
|
|
bool is_int(const std::string &s);
|
|
/** '0x200' is also an int here */
|
|
bool is_hex_or_dec_uint(const std::string &s);
|
|
|
|
bool replace_first(std::string *s, const std::string &substr,
|
|
const std::string &repl);
|
|
|
|
std::pair<std::string, uint16_t> ParseHostPort(const std::string &s);
|
|
|
|
} // namespace sls
|