1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "string_help.h"
16 #include <cstdio>
17 #include <string>
18 #include <memory.h>
19 #include <vector>
20 namespace SysTuning {
21 namespace base {
22 #define UNUSED(expr) \
23 do { \
24 static_cast<void>(expr); \
25 } while (0)
memcpy_s(void * dest,uint32_t destSize,const void * src,size_t srcSize)26 int memcpy_s(void* dest, uint32_t destSize, const void* src, size_t srcSize)
27 {
28 if (srcSize > destSize || src == nullptr || dest == nullptr) {
29 return -1;
30 } else {
31 if (!memcpy(dest, src, srcSize)) {
32 printf("memcpy fail\n");
33 return -1;
34 }
35 }
36 return 0;
37 }
38 #if !is_mingw
sscanf_s(const char * buffer,const char * format,...)39 int sscanf_s(const char* buffer, const char* format, ...)
40 {
41 va_list ap;
42 __builtin_va_start(ap, format);
43 int ret = scanf(buffer, format, ap);
44 __builtin_va_end(ap);
45 return ret;
46 }
47
strncpy_s(char * strDest,size_t destMax,const char * strSrc,size_t count)48 int strncpy_s(char* strDest, size_t destMax, const char* strSrc, size_t count)
49 {
50 return memcpy_s(strDest, destMax, strSrc, count);
51 }
52 #endif
memset_s(void * dest,size_t destSize,int ch,size_t n)53 void* memset_s(void* dest, size_t destSize, int ch, size_t n)
54 {
55 UNUSED(destSize);
56 UNUSED(ch);
57 return memset(dest, 0, n);
58 }
59
snprintf_s(char * strDest,size_t destMax,size_t count,const char * format,...)60 int snprintf_s(char* strDest, size_t destMax, size_t count, const char* format, ...)
61 {
62 UNUSED(count);
63 int ret;
64 va_list ap;
65 __builtin_va_start(ap, format);
66 ret = vsnprintf(strDest, destMax, format, ap);
67 __builtin_va_end(ap);
68 return ret;
69 }
70
sprintf_s(char * strDest,size_t destMax,const char * format,...)71 int sprintf_s(char* strDest, size_t destMax, const char* format, ...)
72 {
73 UNUSED(destMax);
74 va_list ap;
75 __builtin_va_start(ap, format);
76 int ret = sprintf(strDest, format, ap);
77 __builtin_va_end(ap);
78 return ret;
79 }
80 } // namespace base
81 } // namespace SysTuning
82