1/*
2 * Copyright (c) 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#include "tooling/client/manager/breakpoint_manager.h"
17
18#include "common/log_wrapper.h"
19#include "tooling/utils/utils.h"
20#include "tooling/client/session/session.h"
21
22using PtJson = panda::ecmascript::tooling::PtJson;
23using Result = panda::ecmascript::tooling::Result;
24namespace OHOS::ArkCompiler::Toolchain {
25void BreakPointManager::Createbreaklocation(const std::unique_ptr<PtJson> json)
26{
27    if (json == nullptr) {
28        LOGE("arkdb: json parse error");
29        return;
30    }
31
32    if (!json->IsObject()) {
33        LOGE("arkdb: json parse format error");
34        json->ReleaseRoot();
35        return;
36    }
37    Result ret;
38    std::unique_ptr<PtJson> result;
39    ret = json->GetObject("result", &result);
40    if (ret != Result::SUCCESS) {
41        LOGE("arkdb: find result error");
42        return;
43    }
44    std::string breakpointId;
45    ret = result->GetString("breakpointId", &breakpointId);
46    if (ret == Result::SUCCESS) {
47        Breaklocation breaklocation;
48        breaklocation.breakpointId = breakpointId;
49        std::vector<std::string> breaksplitstring;
50        breaksplitstring = Utils::SplitString(breakpointId, ":");
51        breaklocation.lineNumber = breaksplitstring[1]; // 1: linenumber
52        breaklocation.columnNumber = breaksplitstring[2]; // 2: columnnumber
53        breaklocation.url = breaksplitstring[3]; // 3: url
54        breaklist_.push_back(breaklocation);
55    } else {
56        LOGE("arkdb: find breakpointId error");
57        return;
58    }
59}
60
61void BreakPointManager::Show()
62{
63    size_t size = breaklist_.size();
64    for (size_t i = 0; i < size; i++) {
65        std::cout << (i + 1) << ':' << " url:" << breaklist_[i].url;
66        std::cout << " lineNumber:" << (std::atoi(breaklist_[i].lineNumber.c_str()) + 1)
67            << " columnNumber:" << breaklist_[i].columnNumber << std::endl;
68    }
69}
70
71void BreakPointManager::Deletebreaklist(unsigned int num)
72{
73    std::vector<Breaklocation>::iterator it = breaklist_.begin() + num - 1;
74    breaklist_.erase(it);
75}
76
77std::vector<Breaklocation> BreakPointManager::Getbreaklist() const
78{
79    return breaklist_;
80}
81}