1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License.
5800b99b8Sopenharmony_ci * You may obtain a copy of the License at
6800b99b8Sopenharmony_ci *
7800b99b8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8800b99b8Sopenharmony_ci *
9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and
13800b99b8Sopenharmony_ci * limitations under the License.
14800b99b8Sopenharmony_ci */
15800b99b8Sopenharmony_ci
16800b99b8Sopenharmony_ci#ifndef STRING_PRINTF_H
17800b99b8Sopenharmony_ci#define STRING_PRINTF_H
18800b99b8Sopenharmony_ci
19800b99b8Sopenharmony_ci#include <cstdint>
20800b99b8Sopenharmony_ci#include <cstdio>
21800b99b8Sopenharmony_ci#include <string>
22800b99b8Sopenharmony_ci#include <securec.h>
23800b99b8Sopenharmony_ci
24800b99b8Sopenharmony_cinamespace OHOS {
25800b99b8Sopenharmony_cinamespace HiviewDFX {
26800b99b8Sopenharmony_cinamespace {
27800b99b8Sopenharmony_ciconst int STRING_BUF_LEN = 4096;
28800b99b8Sopenharmony_ci}
29800b99b8Sopenharmony_ci
30800b99b8Sopenharmony_cistatic int BufferAppendV(char *buf, int size, const char *fmt, va_list ap)
31800b99b8Sopenharmony_ci{
32800b99b8Sopenharmony_ci    if (buf == nullptr || size <= 0) {
33800b99b8Sopenharmony_ci        return -1;
34800b99b8Sopenharmony_ci    }
35800b99b8Sopenharmony_ci    int ret = vsnprintf_s(buf, size, size - 1, fmt, ap);
36800b99b8Sopenharmony_ci    return ret;
37800b99b8Sopenharmony_ci}
38800b99b8Sopenharmony_ci
39800b99b8Sopenharmony_cistatic bool StringAppendV(std::string& dst, const char* fmt, va_list ap)
40800b99b8Sopenharmony_ci{
41800b99b8Sopenharmony_ci    char buffer[STRING_BUF_LEN] = {0};
42800b99b8Sopenharmony_ci    va_list bakAp;
43800b99b8Sopenharmony_ci    va_copy(bakAp, ap);
44800b99b8Sopenharmony_ci    int ret = BufferAppendV(buffer, sizeof(buffer), fmt, bakAp);
45800b99b8Sopenharmony_ci    va_end(bakAp);
46800b99b8Sopenharmony_ci    if (ret > 0) {
47800b99b8Sopenharmony_ci        dst.append(buffer, ret);
48800b99b8Sopenharmony_ci    }
49800b99b8Sopenharmony_ci    return ret != -1;
50800b99b8Sopenharmony_ci}
51800b99b8Sopenharmony_ci
52800b99b8Sopenharmony_ciinline int BufferPrintf(char *buf, size_t size, const char *fmt, ...)
53800b99b8Sopenharmony_ci{
54800b99b8Sopenharmony_ci    va_list ap;
55800b99b8Sopenharmony_ci    va_start(ap, fmt);
56800b99b8Sopenharmony_ci    int ret = BufferAppendV(buf, size, fmt, ap);
57800b99b8Sopenharmony_ci    va_end(ap);
58800b99b8Sopenharmony_ci    return ret;
59800b99b8Sopenharmony_ci}
60800b99b8Sopenharmony_ci
61800b99b8Sopenharmony_ciinline std::string StringPrintf(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
62800b99b8Sopenharmony_ci
63800b99b8Sopenharmony_ciinline std::string StringPrintf(const char *fmt, ...)
64800b99b8Sopenharmony_ci{
65800b99b8Sopenharmony_ci    if (fmt == nullptr) {
66800b99b8Sopenharmony_ci        return "";
67800b99b8Sopenharmony_ci    }
68800b99b8Sopenharmony_ci    std::string dst;
69800b99b8Sopenharmony_ci    va_list ap;
70800b99b8Sopenharmony_ci    va_start(ap, fmt);
71800b99b8Sopenharmony_ci    StringAppendV(dst, fmt, ap);
72800b99b8Sopenharmony_ci    va_end(ap);
73800b99b8Sopenharmony_ci    return dst;
74800b99b8Sopenharmony_ci}
75800b99b8Sopenharmony_ci
76800b99b8Sopenharmony_ciinline void StringAppendF(std::string& dst, const char* fmt, ...)
77800b99b8Sopenharmony_ci{
78800b99b8Sopenharmony_ci    va_list ap;
79800b99b8Sopenharmony_ci    va_start(ap, fmt);
80800b99b8Sopenharmony_ci    StringAppendV(dst, fmt, ap);
81800b99b8Sopenharmony_ci    va_end(ap);
82800b99b8Sopenharmony_ci}
83800b99b8Sopenharmony_ci} // namespace HiviewDFX
84800b99b8Sopenharmony_ci} // namespace OHOS
85800b99b8Sopenharmony_ci#endif
86