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
16#include "ecmascript/js_file_path.h"
17
18namespace panda::ecmascript {
19std::string JSFilePath::GetBaseName(const std::string &filePath)
20{
21    auto beginPos = filePath.find_last_of("/");
22    if (beginPos != std::string::npos) {
23        beginPos++;
24    } else {
25        beginPos = 0;
26    }
27
28    return filePath.substr(beginPos);
29}
30
31std::string JSFilePath::GetFileName(const std::string &filePath)
32{
33    auto beginPos = filePath.find_last_of('/');
34    if (beginPos != std::string::npos) {
35        beginPos++;
36    } else {
37        beginPos = 0;
38    }
39    auto endPos = filePath.find_last_of('.');
40    if (endPos != std::string::npos) {
41        return filePath.substr(beginPos, endPos - beginPos);
42    }
43
44    return filePath.substr(beginPos);
45}
46
47std::string JSFilePath::GetFileExtension(const std::string &filePath)
48{
49    auto beginPos = filePath.find_last_of('.');
50    if (beginPos != std::string::npos) {
51        return filePath.substr(beginPos);
52    }
53
54    return "";
55}
56
57std::string JSFilePath::GetHapName(const JSPandaFile *jsPandaFile)
58{
59    std::string jsPandaFileDesc = jsPandaFile->GetJSPandaFileDesc().c_str();
60    if (jsPandaFileDesc.find(JSPandaFile::MERGE_ABC_NAME) == std::string::npos) {
61        return "";
62    }
63
64    auto found = jsPandaFileDesc.find(JSPandaFile::BUNDLE_INSTALL_PATH);
65    if (found == std::string::npos) {
66        return "";
67    }
68    std::string subPath = jsPandaFileDesc.substr(sizeof(JSPandaFile::BUNDLE_INSTALL_PATH) - 1);
69    auto endPos = subPath.find("/");
70    if (endPos == std::string::npos) {
71        return "";
72    }
73
74    return subPath.substr(0, endPos);
75}
76
77}  // namespace panda::ecmascript
78