Files
compute-runtime/runtime/helpers/string.h
Filip Hazubski 8b57d28116 clang-format: enable sorting includes
Include files are now grouped and sorted in following order:
1. Header file of the class the current file implements
2. Project files
3. Third party files
4. Standard library

Change-Id: If31af05652184169f7fee1d7ad08f1b2ed602cf0
Signed-off-by: Filip Hazubski <filip.hazubski@intel.com>
2019-02-27 11:50:07 +01:00

92 lines
1.7 KiB
C++

/*
* Copyright (C) 2017-2019 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#pragma once
#if defined(__linux__)
#include <cstring>
#include <errno.h>
#include <string>
inline int strcpy_s(char *dst, size_t dstSize, const char *src) {
if ((dst == nullptr) || (src == nullptr)) {
return -EINVAL;
}
size_t length = strlen(src);
if (dstSize <= length) {
return -ERANGE;
}
memcpy(dst, src, length);
dst[length] = '\0';
return 0;
}
inline int strncpy_s(char *dst, size_t numberOfElements, const char *src, size_t count) {
if ((dst == nullptr) || (src == nullptr)) {
return -EINVAL;
}
if (numberOfElements < count) {
return -ERANGE;
}
size_t length = strlen(src);
if (length > count) {
length = count;
}
memcpy(dst, src, length);
if (length < numberOfElements) {
numberOfElements = length;
}
dst[numberOfElements] = '\0';
return 0;
}
inline size_t strnlen_s(const char *str, size_t count) {
if (str == nullptr) {
return 0;
}
for (size_t i = 0; i < count; ++i) {
if (str[i] == '\0')
return i;
}
return count;
}
inline int memcpy_s(void *dst, size_t destSize, const void *src, size_t count) {
if ((dst == nullptr) || (src == nullptr)) {
return -EINVAL;
}
if (destSize < count) {
return -ERANGE;
}
memcpy(dst, src, count);
return 0;
}
inline int memmove_s(void *dst, size_t numberOfElements, const void *src, size_t count) {
if ((dst == nullptr) || (src == nullptr)) {
return -EINVAL;
}
if (numberOfElements < count) {
return -ERANGE;
}
memmove(dst, src, count);
return 0;
}
#endif