Files
Odin/core/path/path.odin
Brendan Punsky da26e14959 Update path.odin
Fix allocator usage for `rel_current` and `rel_between`
2019-03-13 16:38:02 -04:00

201 lines
5.4 KiB
Odin

package path
import "core:strings"
import "core:unicode/utf8"
// returns everything preceding the last path element
dir :: proc(path: string, new := false, allocator := context.allocator) -> string {
if path == "" do return "";
for i := len(path) - 1; i >= 0; i -= 1 {
if path[i] == '/' || path[i] == '\\' {
if path[:i] == "" {
// path is root
return new ? strings.new_string(SEPARATOR_STRING, allocator) : SEPARATOR_STRING;
} else {
return new ? strings.new_string(path[:i], allocator) : path[:i];
}
}
}
// path doesn't contain any folder structure
return "";
}
// returns the final path element
base :: proc(path: string, new := false, allocator := context.allocator) -> string {
if path == "" do return "";
end := len(path) - 1;
for i := end; i >= 0; i -= 1 {
switch path[i] {
case '/', '\\':
if i != end {
return new ? strings.new_string(path[i+1:], allocator) : path[i+1:];
} else {
end = i; // we don't want trailing slashes
}
}
}
// path doesn't contain any folder structure, return entire path
return new ? strings.new_string(path, allocator) : path;
}
// returns the final path element, excluding the file extension if there is one
name :: proc(path: string, new := false, allocator := context.allocator) -> string {
if path == "" do return "";
dot := len(path);
end := dot - 1;
for i := end; i >= 0; i -= 1 {
switch path[i] {
case '.': dot = (dot == end ? i : dot);
case '/', '\\': return new ? strings.new_string(path[i+1:dot], allocator) : path[i+1:dot];
}
}
// path doesn't contain any folder structure or file extensions; assumed to be a valid file name
return new ? strings.new_string(path, allocator) : path;
}
// returns the file extension, if there is one
ext :: proc(path: string, new := false, allocator := context.allocator) -> string {
if path == "" do return "";
for i := len(path)-1; i >= 0; i -= 1 {
switch path[i] {
case '/', '\\': return "";
case '.': return new ? strings.new_string(path[i+1:], allocator) : path[i+1:];
}
}
// path does not include a file extension
return "";
}
rel :: proc{rel_between, rel_current};
// returns the relative path from one path to another
rel_between :: proc(from, to: string, allocator := context.allocator) -> string {
if from == "" || to == "" do return "";
from = full(from, context.temp_allocator);
to = full(to, context.temp_allocator);
from_is_dir := is_dir(from);
to_is_dir := is_dir(to);
index, slash := 0, 0;
for {
if index >= len(from) {
if index >= len(to) || (from_is_dir && index < len(to) && (to[index] == '/' || to[index] == '\\')) {
slash = index;
}
break;
}
else if index >= len(to) {
if index >= len(from) || (to_is_dir && index < len(from) && (from[index] == '/' || from[index] == '\\')) {
slash = index;
}
break;
}
lchar, skip := utf8.decode_rune_in_string(from[index:]);
rchar, _ := utf8.decode_rune_in_string(to[index:]);
if (lchar == '/' || lchar == '\\') && (rchar == '/' || lchar == '\\') {
slash = index;
}
else if lchar != rchar {
break;
}
index += skip;
}
if slash < 1 {
// there is no common path, use the absolute `to` path
return strings.new_string(to, allocator);
}
from_slashes, to_slashes := 0, 0;
if slash < len(from) {
from = from[slash+1:];
if from_is_dir {
from_slashes += 1;
}
}
else {
from = "";
}
if slash < len(to) {
to = to[slash+1:];
if to_is_dir {
to_slashes += 1;
}
}
else {
to = "";
}
for char in from {
if char == '/' || char == '\\' {
from_slashes += 1;
}
}
for char in to {
if char == '/' || char == '\\' {
to_slashes += 1;
}
}
if from_slashes == 0 {
buffer := make([]byte, 2 + len(to), allocator);
buffer[0] = '.';
buffer[1] = SEPARATOR;
copy(buffer[2:], ([]byte)(to));
return string(buffer);
}
else {
buffer := make([]byte, from_slashes*3 + len(to), allocator);
for i in 0..from_slashes-1 {
buffer[i*3+0] = '.';
buffer[i*3+1] = '.';
buffer[i*3+2] = SEPARATOR;
}
copy(buffer[from_slashes*3:], ([]byte)(to));
return string(buffer);
}
return "";
}
// returns the relative path from the current directory to another path
rel_current :: proc(to: string, allocator := context.temp_allocator) -> string {
return inline rel_between(current(context.temp_allocator), to, allocator);
}
// splits the path elements into slices of the original path string
split :: proc(s: string, allocator := context.temp_allocator) -> []string #no_bounds_check {
return inline strings.split(s, []string{"\\", "/"}, true, allocator);
}