Coverage Report

Created: 2024-11-21 21:13

/root/doris/be/src/util/path_util.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#include "util/path_util.h"
19
20
// Use the POSIX version of dirname(3). See `man 3 dirname`
21
#include <libgen.h>
22
23
#include "gutil/strings/split.h"
24
#include "gutil/strings/strip.h"
25
26
using std::string;
27
using std::vector;
28
using strings::SkipEmpty;
29
using strings::Split;
30
31
namespace doris {
32
namespace path_util {
33
34
370
std::string join_path_segments(const string& a, const string& b) {
35
370
    if (a.empty()) {
36
1
        return b;
37
369
    } else if (b.empty()) {
38
1
        return a;
39
368
    } else {
40
368
        return StripSuffixString(a, "/") + "/" + StripPrefixString(b, "/");
41
368
    }
42
370
}
43
44
// strdup use malloc to obtain memory for the new string, it should be freed with free.
45
// but std::unique_ptr use delete to free memory by default, so it should specify free memory using free
46
47
18
std::string dir_name(const string& path) {
48
18
    std::vector<char> path_copy(path.c_str(), path.c_str() + path.size() + 1);
49
18
    return dirname(&path_copy[0]);
50
18
}
51
52
51
std::string base_name(const string& path) {
53
51
    std::vector<char> path_copy(path.c_str(), path.c_str() + path.size() + 1);
54
51
    return basename(&path_copy[0]);
55
51
}
56
57
39
std::string file_extension(const string& path) {
58
39
    string file_name = base_name(path);
59
39
    if (file_name == "." || file_name == "..") {
60
3
        return "";
61
3
    }
62
63
36
    string::size_type pos = file_name.rfind(".");
64
36
    return pos == string::npos ? "" : file_name.substr(pos);
65
39
}
66
67
} // namespace path_util
68
} // namespace doris