• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 
16 #include "audio_common_napi.h"
17 #include "media_log.h"
18 
19 namespace OHOS {
20 namespace AudioStandard {
GetStringArgument(napi_env env,napi_value value)21 std::string AudioCommonNapi::GetStringArgument(napi_env env, napi_value value)
22 {
23     std::string strValue = "";
24     size_t bufLength = 0;
25     napi_status status = napi_get_value_string_utf8(env, value, nullptr, 0, &bufLength);
26     if (status == napi_ok && bufLength > 0 && bufLength < PATH_MAX) {
27         char *buffer = (char *)malloc((bufLength + 1) * sizeof(char));
28         CHECK_AND_RETURN_RET_LOG(buffer != nullptr, strValue, "no memory");
29         status = napi_get_value_string_utf8(env, value, buffer, bufLength + 1, &bufLength);
30         if (status == napi_ok) {
31             MEDIA_DEBUG_LOG("argument = %{public}s", buffer);
32             strValue = buffer;
33         }
34         free(buffer);
35         buffer = nullptr;
36     }
37     return strValue;
38 }
39 }  // namespace AudioStandard
40 }  // namespace OHOS
41