321 lines
6.1 KiB
C
321 lines
6.1 KiB
C
#include "posix.h"
|
|
|
|
#include <blue/core/stringstream.h>
|
|
#include <blue/io/file.h>
|
|
#include <blue/io/path.h>
|
|
#include <blue/ds/string.h>
|
|
#include <ctype.h>
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
|
|
struct b_path {
|
|
struct b_dsref base;
|
|
struct b_string *pathstr;
|
|
};
|
|
|
|
static void path_release(struct b_dsref *obj);
|
|
static void path_to_string(struct b_dsref *obj, struct b_stream *out);
|
|
|
|
static struct b_dsref_type path_type = {
|
|
.t_name = "corelib::path",
|
|
.t_flags = B_DSREF_FUNDAMENTAL,
|
|
.t_id = B_DSREF_TYPE_PATH,
|
|
.t_instance_size = sizeof(struct b_path),
|
|
.t_release = path_release,
|
|
.t_to_string = path_to_string,
|
|
};
|
|
|
|
struct b_path *b_path_create()
|
|
{
|
|
struct b_path *path
|
|
= (struct b_path *)b_dsref_type_instantiate(&path_type);
|
|
if (!path) {
|
|
return NULL;
|
|
}
|
|
|
|
path->pathstr = b_string_create();
|
|
if (!path->pathstr) {
|
|
b_path_release(path);
|
|
return NULL;
|
|
}
|
|
|
|
return path;
|
|
}
|
|
|
|
struct b_path *b_path_create_root()
|
|
{
|
|
const char *system_drive = "/";
|
|
size_t system_drive_len = strlen(system_drive);
|
|
|
|
struct b_path *path = b_path_create();
|
|
if (!path) {
|
|
return NULL;
|
|
}
|
|
|
|
b_string_append_cstr(path->pathstr, system_drive);
|
|
if (system_drive[system_drive_len - 1] != '\\') {
|
|
b_string_append_cstr(path->pathstr, "\\");
|
|
}
|
|
|
|
return path;
|
|
}
|
|
|
|
struct b_path *b_path_create_cwd()
|
|
{
|
|
const long buf_len = 2048;
|
|
char *buf = malloc(buf_len);
|
|
if (!buf) {
|
|
return NULL;
|
|
}
|
|
|
|
if (!getcwd(buf, buf_len)) {
|
|
free(buf);
|
|
return NULL;
|
|
}
|
|
|
|
struct b_path *path = b_path_create();
|
|
if (!path) {
|
|
free(buf);
|
|
return NULL;
|
|
}
|
|
|
|
b_string_append_cstr(path->pathstr, buf);
|
|
free(buf);
|
|
|
|
return path;
|
|
}
|
|
|
|
struct b_path *b_path_create_from_cstr(const char *cstr)
|
|
{
|
|
struct b_path *path = b_path_create();
|
|
if (!path) {
|
|
return NULL;
|
|
}
|
|
|
|
char prev = 0;
|
|
|
|
for (size_t i = 0; cstr[i]; i++) {
|
|
char c = cstr[i];
|
|
if (c == '\\') {
|
|
c = '/';
|
|
}
|
|
|
|
if (prev == '/' && c == '/') {
|
|
continue;
|
|
}
|
|
|
|
char s[] = {c, 0};
|
|
b_string_append_cstr(path->pathstr, s);
|
|
prev = c;
|
|
}
|
|
|
|
while (b_string_back(path->pathstr) == '/') {
|
|
b_string_pop_back(path->pathstr);
|
|
}
|
|
|
|
return path;
|
|
}
|
|
|
|
struct b_path *b_path_duplicate(const struct b_path *path)
|
|
{
|
|
struct b_path *new_path = b_path_create();
|
|
if (!path) {
|
|
return NULL;
|
|
}
|
|
|
|
new_path->pathstr = b_string_duplicate(path->pathstr);
|
|
if (!new_path->pathstr) {
|
|
b_path_release(new_path);
|
|
return NULL;
|
|
}
|
|
|
|
return new_path;
|
|
}
|
|
|
|
static void append_path(struct b_path *dest, const struct b_path *src)
|
|
{
|
|
if (b_path_is_absolute(src)) {
|
|
b_string_clear(dest->pathstr);
|
|
b_string_append_s(dest->pathstr, src->pathstr);
|
|
return;
|
|
}
|
|
|
|
if (b_string_get_size(dest->pathstr, B_STRLEN_NORMAL) > 0
|
|
&& b_string_back(dest->pathstr) != '/'
|
|
&& b_string_front(src->pathstr) != '/') {
|
|
char s[] = {'/', 0};
|
|
b_string_append_cstr(dest->pathstr, s);
|
|
}
|
|
|
|
b_string_append_s(dest->pathstr, src->pathstr);
|
|
}
|
|
|
|
struct b_path *b_path_join(const b_path *paths[], size_t nr_paths)
|
|
{
|
|
struct b_path *result = b_path_create();
|
|
if (!result) {
|
|
return NULL;
|
|
}
|
|
|
|
for (size_t i = 0; i < nr_paths; i++) {
|
|
if (paths[i]) {
|
|
append_path(result, paths[i]);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
struct b_path *b_path_make_absolute(const struct b_path *in)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
struct b_path *b_path_make_relative(const struct b_path *in)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
struct b_path *b_path_make_canonical(const struct b_path *in)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
bool b_path_is_absolute(const struct b_path *path)
|
|
{
|
|
const char *s = b_string_ptr(path->pathstr);
|
|
return s[0] == '/';
|
|
}
|
|
|
|
bool b_path_exists(const struct b_path *path)
|
|
{
|
|
b_file_info info;
|
|
if (!B_OK(b_path_stat(path, &info))) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool b_path_is_file(const struct b_path *path)
|
|
{
|
|
b_file_info info;
|
|
if (!B_OK(b_path_stat(path, &info))) {
|
|
return false;
|
|
}
|
|
|
|
return (info.attrib & B_FILE_ATTRIB_NORMAL) != 0;
|
|
}
|
|
|
|
bool b_path_is_directory(const struct b_path *path)
|
|
{
|
|
b_file_info info;
|
|
if (!B_OK(b_path_stat(path, &info))) {
|
|
return false;
|
|
}
|
|
|
|
return (info.attrib & B_FILE_ATTRIB_DIRECTORY) != 0;
|
|
}
|
|
|
|
enum b_status b_path_stat(const b_path *path, struct b_file_info *out)
|
|
{
|
|
struct stat st;
|
|
int err = stat(b_path_ptr(path), &st);
|
|
|
|
if (err != 0) {
|
|
return b_status_from_errno(errno, B_ERR_IO_FAILURE);
|
|
}
|
|
|
|
memset(out, 0x0, sizeof *out);
|
|
|
|
return b_file_info_from_stat(&st, out);
|
|
}
|
|
|
|
enum b_status b_path_unlink(const b_path *path)
|
|
{
|
|
int err = remove(b_string_ptr(path->pathstr));
|
|
return err == 0 ? B_SUCCESS : b_status_from_errno(errno, B_ERR_IO_FAILURE);
|
|
}
|
|
|
|
enum b_status b_path_get_directory(
|
|
const struct b_path *path, struct b_path **out_dir_path)
|
|
{
|
|
struct b_string *path_str = path->pathstr;
|
|
long len = b_string_get_size(path_str, B_STRLEN_NORMAL);
|
|
|
|
const char *path_cstr = b_string_ptr(path_str);
|
|
char *sep = strrchr(path_cstr, '/');
|
|
|
|
if (!sep) {
|
|
*out_dir_path = b_path_create();
|
|
return B_SUCCESS;
|
|
}
|
|
|
|
size_t dir_path_len = (size_t)(sep - path_cstr);
|
|
struct b_string *dir_path_s = b_string_substr(path_str, 0, dir_path_len);
|
|
|
|
struct b_path *dir_path
|
|
= b_path_create_from_cstr(b_string_ptr(dir_path_s));
|
|
b_string_release(dir_path_s);
|
|
|
|
*out_dir_path = dir_path;
|
|
|
|
return B_SUCCESS;
|
|
}
|
|
|
|
enum b_status b_path_get_filename(
|
|
const struct b_path *path, struct b_string *out_name)
|
|
{
|
|
struct b_string *path_str = path->pathstr;
|
|
long len = b_string_get_size(path_str, B_STRLEN_NORMAL);
|
|
|
|
char *sep = strrchr(b_string_ptr(path_str), '/');
|
|
|
|
if (!sep) {
|
|
b_string_append_s(out_name, path_str);
|
|
return B_SUCCESS;
|
|
}
|
|
|
|
const char *filename = sep;
|
|
while (*filename == '/' && *filename != '\0') {
|
|
filename++;
|
|
}
|
|
|
|
if (*filename == '\0') {
|
|
b_string_clear(out_name);
|
|
return B_SUCCESS;
|
|
}
|
|
|
|
b_string_append_cstr(out_name, filename);
|
|
|
|
return B_SUCCESS;
|
|
}
|
|
|
|
const char *b_path_ptr(const struct b_path *path)
|
|
{
|
|
return b_string_ptr(path->pathstr);
|
|
}
|
|
|
|
size_t b_path_length(const struct b_path *path)
|
|
{
|
|
return b_string_get_size(path->pathstr, B_STRLEN_NORMAL);
|
|
}
|
|
|
|
void path_release(struct b_dsref *obj)
|
|
{
|
|
struct b_path *path = (struct b_path *)obj;
|
|
|
|
b_string_release(path->pathstr);
|
|
}
|
|
|
|
void path_to_string(struct b_dsref *obj, struct b_stream *out)
|
|
{
|
|
struct b_path *path = (struct b_path *)obj;
|
|
|
|
b_stream_write_string(out, b_string_ptr(path->pathstr), NULL);
|
|
}
|