mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-07 10:30:41 +02:00
added string_utils and strcpy_safe
This commit is contained in:
parent
89d9204e1c
commit
1ca8bf204a
@ -6,6 +6,7 @@ set(SOURCES
|
||||
slsDetector/slsDetector.cpp
|
||||
../slsSupportLib/include/ClientInterface.cpp
|
||||
../slsSupportLib/include/utilities.cpp
|
||||
../slsSupportLib/src/string_utils.cpp
|
||||
)
|
||||
|
||||
set(HEADERS
|
||||
@ -46,6 +47,7 @@ set(PUBLICHEADERS
|
||||
${PROJECT_SOURCE_DIR}/slsSupportLib/include/sls_detector_exceptions.h
|
||||
${PROJECT_SOURCE_DIR}/slsSupportLib/include/utilities.h
|
||||
${PROJECT_SOURCE_DIR}/slsSupportLib/include/container_utils.h
|
||||
${PROJECT_SOURCE_DIR}/slsSupportLib/include/string_utils.h
|
||||
sharedMemory/SharedMemory.h
|
||||
slsDetector/slsDetector.h
|
||||
slsDetector/slsDetectorUsers.h
|
||||
|
@ -9,11 +9,13 @@
|
||||
#include "sls_detector_exceptions.h"
|
||||
#include "utilities.h"
|
||||
|
||||
#include "string_utils.h"
|
||||
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <rapidjson/document.h> //json header in zmq stream
|
||||
#include <sstream>
|
||||
#include <string.h>
|
||||
#include <cstring>
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/shm.h>
|
||||
#include <sys/types.h>
|
||||
@ -391,15 +393,11 @@ void multiSlsDetector::updateUserdetails() {
|
||||
memset(thisMultiDetector->lastUser, 0, SHORT_STRING_LENGTH);
|
||||
memset(thisMultiDetector->lastDate, 0, SHORT_STRING_LENGTH);
|
||||
try {
|
||||
strncpy(thisMultiDetector->lastUser, exec("whoami").c_str(),
|
||||
SHORT_STRING_LENGTH - 1);
|
||||
thisMultiDetector->lastUser[SHORT_STRING_LENGTH - 1] = 0;
|
||||
strncpy(thisMultiDetector->lastDate, exec("date").c_str(),
|
||||
DATE_LENGTH - 1);
|
||||
thisMultiDetector->lastDate[DATE_LENGTH - 1] = 0;
|
||||
sls::strcpy_safe(thisMultiDetector->lastUser, exec("whoami").c_str());
|
||||
sls::strcpy_safe(thisMultiDetector->lastDate, exec("date").c_str());
|
||||
} catch (...) {
|
||||
strcpy(thisMultiDetector->lastUser, "errorreading");
|
||||
strcpy(thisMultiDetector->lastDate, "errorreading");
|
||||
sls::strcpy_safe(thisMultiDetector->lastUser, "errorreading");
|
||||
sls::strcpy_safe(thisMultiDetector->lastDate, "errorreading");
|
||||
}
|
||||
}
|
||||
|
||||
@ -3116,7 +3114,7 @@ int multiSlsDetector::setCTBPattern(std::string fname, int detPos) {
|
||||
int addr = 0;
|
||||
|
||||
FILE *fd = fopen(fname.c_str(), "r");
|
||||
if (fd <= 0) {
|
||||
if (fd == nullptr) {
|
||||
FILE_LOG(logERROR) << "Could not open file";
|
||||
setErrorMask(getErrorMask() | MULTI_OTHER_ERROR);
|
||||
return -1;
|
||||
|
@ -98,6 +98,8 @@ T minusOneIfDifferent(const std::vector<T>& container)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//TODO!(Erik)Should try to move away from using this in the slsDetectorPackage
|
||||
inline
|
||||
std::string concatenateIfDifferent(std::vector<std::string> container)
|
||||
|
17
slsSupportLib/include/string_utils.h
Normal file
17
slsSupportLib/include/string_utils.h
Normal file
@ -0,0 +1,17 @@
|
||||
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 would truncate the source string and should be used with care.
|
||||
Still this is better than strcpy...
|
||||
*/
|
||||
template <size_t array_size>
|
||||
void strcpy_safe(char (&destination)[array_size], const char *source) {
|
||||
strncpy(destination, source, array_size);
|
||||
destination[array_size - 1] = '\0';
|
||||
}
|
||||
|
||||
|
||||
}; // namespace sls
|
9
slsSupportLib/src/string_utils.cpp
Normal file
9
slsSupportLib/src/string_utils.cpp
Normal file
@ -0,0 +1,9 @@
|
||||
|
||||
// #include <cstring>
|
||||
// namespace sls{
|
||||
|
||||
// void strcpy_safe(char *dst, const char *src, size_t size){
|
||||
// strncpy(dst, src, size);
|
||||
// }
|
||||
|
||||
// };
|
@ -16,7 +16,9 @@ if(USE_TESTS)
|
||||
set(LOCAL_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
|
||||
set(TEST_SOURCES
|
||||
${LOCAL_TEST_DIR}/test-container_utils.cpp
|
||||
${LOCAL_TEST_DIR}/test-string_utils.cpp
|
||||
${LOCAL_TEST_DIR}/test-MySocketTCP.cpp
|
||||
|
||||
#${LOCAL_TEST_DIR}/test-multiDetector.cpp
|
||||
${LOCAL_TEST_DIR}/test.cpp
|
||||
# PARENT_SCOPE
|
||||
|
34
tests/src/test-string_utils.cpp
Normal file
34
tests/src/test-string_utils.cpp
Normal file
@ -0,0 +1,34 @@
|
||||
#include "MySocketTCP.h"
|
||||
#include "catch.hpp"
|
||||
// #include "multiSlsDetector.h"
|
||||
#include "logger.h"
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#include "string_utils.h"
|
||||
|
||||
#define VERBOSE
|
||||
|
||||
TEST_CASE("copy a string") {
|
||||
|
||||
char src[10] = "hej";
|
||||
REQUIRE(src[3]=='\0');
|
||||
|
||||
char dst[20];
|
||||
|
||||
sls::strcpy_safe(dst, src);
|
||||
REQUIRE(dst[0]=='h');
|
||||
REQUIRE(dst[1]=='e');
|
||||
REQUIRE(dst[2]=='j');
|
||||
REQUIRE(dst[3]=='\0');
|
||||
|
||||
}
|
||||
|
||||
TEST_CASE("copy a long string"){
|
||||
auto src = "some very very long sting that does not fit";
|
||||
char dst[3];
|
||||
sls::strcpy_safe(dst, src);
|
||||
REQUIRE(dst[0]=='s');
|
||||
REQUIRE(dst[1]=='o');
|
||||
REQUIRE(dst[3]=='\0');
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user