1/**
2 * Copyright (c) 2023-2024 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 ES2PANDA_UTIL_PATH_H
17#define ES2PANDA_UTIL_PATH_H
18
19#include <cstdio>
20#include <string>
21#include "util/ustring.h"
22
23namespace ark::es2panda::util {
24const char PATH_DELIMITER =
25#ifdef _WIN32
26    '\\';
27#else
28    '/';
29#endif
30
31class Path {
32public:
33    Path();
34    Path(const std::string &absolutePath, ArenaAllocator *allocator);
35    Path(const std::string &relativePath, const std::string &basePath, ArenaAllocator *allocator);
36    Path(const util::StringView &absolutePath, ArenaAllocator *allocator);
37    Path(const util::StringView &relativePath, const util::StringView &basePath, ArenaAllocator *allocator);
38    bool IsRelative();
39    bool IsAbsolute();
40    const util::StringView &GetPath() const;
41    const util::StringView &GetAbsolutePath() const;
42    const util::StringView &GetExtension() const;
43    const util::StringView &GetFileName() const;
44    const util::StringView &GetParentFolder() const;
45    const util::StringView &GetAbsoluteParentFolder() const;
46    constexpr static char GetPathDelimiter()
47    {
48        return PATH_DELIMITER;
49    }
50
51private:
52    ArenaAllocator *allocator_ {};
53    bool isRelative_ {};
54    util::StringView path_ {};
55    util::StringView basePath_ {};
56    util::StringView absolutePath_ {};
57    util::StringView fileName_ {};
58    util::StringView fileExtension_ {};
59    util::StringView parentFolder_ {};
60    util::StringView absoluteParentFolder_ {};
61
62    void Initializer(const std::string &absolutePath, ArenaAllocator *allocator);
63    void InitializeBasePath(std::string basePath);
64    void InitializeAbsolutePath();
65    void InitializeParentFolder();
66    void InitializeAbsoluteParentFolder();
67    void InitializeFileName();
68    void InitializeFileExtension();
69};
70}  // namespace ark::es2panda::util
71
72#endif
73