dora_ssr/dora/
path.rs

1/* Copyright (c) 2016-2025 Li Jin <dragon-fly@qq.com>
2
3Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
7THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
8
9extern "C" {
10	fn path_get_ext(path: i64) -> i64;
11	fn path_get_path(path: i64) -> i64;
12	fn path_get_name(path: i64) -> i64;
13	fn path_get_filename(path: i64) -> i64;
14	fn path_get_relative(path: i64, target: i64) -> i64;
15	fn path_replace_ext(path: i64, new_ext: i64) -> i64;
16	fn path_replace_filename(path: i64, new_file: i64) -> i64;
17	fn path_concat(paths: i64) -> i64;
18}
19/// Helper struct for file path operations.
20pub struct Path { }
21impl Path {
22	/// Extracts the file extension from a given file path.
23	///
24	/// # Example
25	///
26	/// Input: "/a/b/c.TXT" Output: "txt"
27	///
28	/// # Arguments
29	///
30	/// * `path` - The input file path.
31	///
32	/// # Returns
33	///
34	/// * `String` - The extension of the input file.
35	pub fn get_ext(path: &str) -> String {
36		unsafe { return crate::dora::to_string(path_get_ext(crate::dora::from_string(path))); }
37	}
38	/// Extracts the parent path from a given file path.
39	///
40	/// # Example
41	///
42	/// Input: "/a/b/c.TXT" Output: "/a/b"
43	///
44	/// # Arguments
45	///
46	/// * `path` - The input file path.
47	///
48	/// # Returns
49	///
50	/// * `String` - The parent path of the input file.
51	pub fn get_path(path: &str) -> String {
52		unsafe { return crate::dora::to_string(path_get_path(crate::dora::from_string(path))); }
53	}
54	/// Extracts the file name without extension from a given file path.
55	///
56	/// # Example
57	///
58	/// Input: "/a/b/c.TXT" Output: "c"
59	///
60	/// # Arguments
61	///
62	/// * `path` - The input file path.
63	///
64	/// # Returns
65	///
66	/// * `String` - The name of the input file without extension.
67	pub fn get_name(path: &str) -> String {
68		unsafe { return crate::dora::to_string(path_get_name(crate::dora::from_string(path))); }
69	}
70	/// Extracts the file name from a given file path.
71	///
72	/// # Example
73	///
74	/// Input: "/a/b/c.TXT" Output: "c.TXT"
75	///
76	/// # Arguments
77	///
78	/// * `path` - The input file path.
79	///
80	/// # Returns
81	///
82	/// * `String` - The name of the input file.
83	pub fn get_filename(path: &str) -> String {
84		unsafe { return crate::dora::to_string(path_get_filename(crate::dora::from_string(path))); }
85	}
86	/// Computes the relative path from the target file to the input file.
87	///
88	/// # Example
89	///
90	/// Input: "/a/b/c.TXT", base: "/a" Output: "b/c.TXT"
91	///
92	/// # Arguments
93	///
94	/// * `path` - The input file path.
95	/// * `base` - The target file path.
96	///
97	/// # Returns
98	///
99	/// * `String` - The relative path from the input file to the target file.
100	pub fn get_relative(path: &str, target: &str) -> String {
101		unsafe { return crate::dora::to_string(path_get_relative(crate::dora::from_string(path), crate::dora::from_string(target))); }
102	}
103	/// Changes the file extension in a given file path.
104	///
105	/// # Example
106	///
107	/// Input: "/a/b/c.TXT", "lua" Output: "/a/b/c.lua"
108	///
109	/// # Arguments
110	///
111	/// * `path` - The input file path.
112	/// * `new_ext` - The new file extension to replace the old one.
113	///
114	/// # Returns
115	///
116	/// * `String` - The new file path.
117	pub fn replace_ext(path: &str, new_ext: &str) -> String {
118		unsafe { return crate::dora::to_string(path_replace_ext(crate::dora::from_string(path), crate::dora::from_string(new_ext))); }
119	}
120	/// Changes the filename in a given file path.
121	///
122	/// # Example
123	///
124	/// Input: "/a/b/c.TXT", "d" Output: "/a/b/d.TXT"
125	///
126	/// # Arguments
127	///
128	/// * `path` - The input file path.
129	/// * `new_file` - The new filename to replace the old one.
130	///
131	/// # Returns
132	///
133	/// * `String` - The new file path.
134	pub fn replace_filename(path: &str, new_file: &str) -> String {
135		unsafe { return crate::dora::to_string(path_replace_filename(crate::dora::from_string(path), crate::dora::from_string(new_file))); }
136	}
137	/// Joins the given segments into a new file path.
138	///
139	/// # Example
140	///
141	/// Input: "a", "b", "c.TXT" Output: "a/b/c.TXT"
142	///
143	/// # Arguments
144	///
145	/// * `segments` - The segments to be joined as a new file path.
146	///
147	/// # Returns
148	///
149	/// * `String` - The new file path.
150	pub fn concat(paths: &Vec<&str>) -> String {
151		unsafe { return crate::dora::to_string(path_concat(crate::dora::Vector::from_str(paths))); }
152	}
153}