• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #ifndef HISYSEVENT_STRING_UTIL
16 #define HISYSEVENT_STRING_UTIL
17 
18 #include <string>
19 #include <vector>
20 
21 namespace OHOS {
22 namespace HiviewDFX {
23 namespace StringUtil {
24 int CopyCString(char* dst, const std::string& src, size_t len);
25 int CreateCString(char** dst, const std::string& src, size_t len = 1024); // default maxLen is 1024
26 int ConvertCString(const std::string& str, char** sp, size_t len = 8 * 1024); // default maxLen is 8*1024
27 int ConvertCStringVec(const std::vector<std::string>& vec, char*** strs, size_t& len);
28 void MemsetSafe(void* dest, size_t destSize);
29 
DeletePointer(T ** p)30 template <typename T> void DeletePointer(T** p)
31 {
32     if (p == nullptr || *p == nullptr) {
33         return;
34     }
35     delete[] *p;
36     *p = nullptr;
37 }
38 
DeletePointers(T *** ps,size_t len)39 template <typename T> void DeletePointers(T*** ps, size_t len)
40 {
41     if (ps == nullptr || *ps == nullptr) {
42         return;
43     }
44     auto realPs = *ps;
45     for (size_t i = 0; i < len; i++) {
46         DeletePointer<T>(&realPs[i]);
47     }
48     delete[] realPs;
49     realPs = nullptr;
50 }
51 } // namespace StringUtil
52 } // namespace HiviewDFX
53 } // namespace OHOS
54 #endif // HISYSEVENT_STRING_UTIL
55