xref: /aosp_15_r20/frameworks/base/libs/androidfw/include_pathutils/androidfw/PathUtils.h (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <utils/String8.h>
20 
21 /* This library contains path manipulation functions that are used only by androidfw and aapt.
22  * When it's possible, migrate all uses to std::filesystem::path.
23  */
24 
25 namespace android {
26 
27 /**
28  * Get just the filename component.
29  *
30  * DEPRECATED: use std::filesystem::path::filename
31  *
32  * "/tmp/foo/bar.c" --> "bar.c"
33  */
34 String8 getPathLeaf(const String8& str);
35 
36 /**
37  * Remove the last (file name) component, leaving just the directory
38  * name.
39  *
40  * DEPRECATED: use std::filesystem::path::parent_path
41  *
42  * "/tmp/foo/bar.c" --> "/tmp/foo"
43  * "/tmp" --> "" // ????? shouldn't this be "/" ???? XXX
44  * "bar.c" --> ""
45  */
46 String8 getPathDir(const String8& str);
47 
48 /**
49  * Return the filename extension.  This is the last '.' and any number
50  * of characters that follow it.  The '.' is included in case we
51  * decide to expand our definition of what constitutes an extension.
52  *
53  * DEPRECATED: use std::filesystem::path::extension
54  *
55  * "/tmp/foo/bar.c" --> ".c"
56  * "/tmp" --> ""
57  * "/tmp/foo.bar/baz" --> ""
58  * "foo.jpeg" --> ".jpeg"
59  * "foo." --> ""
60  */
61 String8 getPathExtension(const String8& str);
62 
63 /**
64  * Return the path without the extension.  Rules for what constitutes
65  * an extension are described in the comment for getPathExtension().
66  *
67  * DEPRECATED: use std::filesystem::path::stem and std::filesystem::path::parent_path
68  *
69  * "/tmp/foo/bar.c" --> "/tmp/foo/bar"
70  */
71 String8 getBasePath(const String8& str);
72 
73 /**
74  * Add a component to the pathname.  We guarantee that there is
75  * exactly one path separator between the old path and the new.
76  * If there is no existing name, we just copy the new name in.
77  *
78  * DEPRECATED: use std::filesystem::path::operator/=
79  *
80  * If leaf is a fully qualified path (i.e. starts with '/', it
81  * replaces whatever was there before.
82  */
83 String8& appendPath(String8& str, const char* leaf);
appendPath(String8 & str,const String8 & leaf)84 inline String8& appendPath(String8& str, const String8& leaf) {
85     return appendPath(str, leaf.c_str());
86 }
87 
88 /**
89  * Like appendPath(), but does not affect this string.  Returns a new one instead.
90  *
91  * DEPRECATED: use std::filesystem::operator/
92  */
appendPathCopy(String8 str,const char * leaf)93 inline String8 appendPathCopy(String8 str, const char* leaf) { return appendPath(str, leaf); }
appendPathCopy(String8 str,const String8 & leaf)94 inline String8 appendPathCopy(String8 str, const String8& leaf) {
95     return appendPath(str, leaf.c_str());
96 }
97 
98 } // namespace android
99