1 /*
2  * Copyright (c) 2021-2023 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 #ifndef WEBGL_UTIL_H
17 #define WEBGL_UTIL_H
18 
19 #include <vector>
20 #include <string>
21 #include <cerrno>
22 #include <cstdlib>
23 #include "log.h"
24 #include "util/object_manager.h"
25 #include "napi/n_exporter.h"
26 #include "context/webgl_rendering_context_basic_base.h"
27 #include "context/webgl_context_attributes.h"
28 
29 namespace OHOS {
30 namespace Rosen {
31 class Util {
32 public:
33     static void SplitString(const std::string& str, std::vector<std::string>& vec, const std::string& pattern);
34     static bool GetContextInfo(napi_env env, napi_value thisVar,
35         std::string &contextId, std::vector<std::string> &info);
36     static WebGLRenderingContextBasicBase *GetContextObject(napi_env env, napi_value thisVar);
37 
StringToInt(const std::string& value)38     static inline int32_t StringToInt(const std::string& value)
39     {
40         errno = 0;
41         char* pEnd = nullptr;
42         int64_t result = std::strtol(value.c_str(), &pEnd, 10);
43         if (pEnd == value.c_str() || (result < INT_MIN || result > INT_MAX) || errno == ERANGE) {
44             return 0;
45         } else {
46             return result;
47         }
48     }
49 };
50 } // namespace Rosen
51 } // namespace OHOS
52 #endif // WEBGL_UTIL_H
53