meta: rename to fx

This commit is contained in:
2026-03-16 10:35:43 +00:00
parent 84df46489a
commit e9d0e323f0
233 changed files with 12875 additions and 12869 deletions

View File

@@ -1,3 +1,3 @@
include(../cmake/Templates.cmake)
add_bluelib_module(NAME io DEPENDENCIES core ds)
add_fx_module(NAME io DEPENDENCIES core ds)

View File

@@ -1,75 +0,0 @@
#ifndef BLUE_IO_DIRECTORY_H_
#define BLUE_IO_DIRECTORY_H_
#include <blue/core/error.h>
#include <blue/core/iterator.h>
#include <blue/core/macros.h>
#include <blue/core/misc.h>
#include <blue/io/file.h>
#include <blue/io/path.h>
#define B_DIRECTORY_ROOT ((b_directory *)NULL)
B_DECLS_BEGIN;
struct b_directory_p;
#define B_TYPE_DIRECTORY (b_directory_get_type())
#define B_TYPE_DIRECTORY_ITERATOR (b_directory_iterator_get_type())
B_DECLARE_TYPE(b_directory);
B_DECLARE_TYPE(b_directory_iterator);
B_TYPE_CLASS_DECLARATION_BEGIN(b_directory)
B_TYPE_CLASS_DECLARATION_END(b_directory)
B_TYPE_CLASS_DECLARATION_BEGIN(b_directory_iterator)
B_TYPE_CLASS_DECLARATION_END(b_directory_iterator)
struct z__b_directory_iterator;
typedef enum b_directory_iterator_flags {
B_DIRECTORY_ITERATE_PARENT_FIRST = 0x01u,
B_DIRECTORY_ITERATE_PARENT_LAST = 0x02u,
} b_directory_iterator_flags;
typedef enum b_directory_open_flags {
B_DIRECTORY_OPEN_CREATE = 0x01u,
B_DIRECTORY_OPEN_CREATE_INTERMEDIATE = 0x03u,
B_DIRECTORY_OPEN_DELETE_ON_CLOSE = 0x04u,
} b_directory_open_flags;
typedef struct b_directory_entry {
const b_path *filepath;
char *filename;
b_file_info info;
} b_directory_entry;
BLUE_API b_type b_directory_get_type(void);
BLUE_API b_type b_directory_iterator_get_type(void);
BLUE_API b_result b_directory_open(
b_directory *root, const b_path *path, b_directory_open_flags flags,
b_directory **out);
BLUE_API b_result b_directory_open_temp(b_directory **out);
BLUE_API const b_path *b_directory_get_path(const b_directory *dir);
BLUE_API const b_path *b_directory_get_rel_path(const b_directory *dir);
BLUE_API const char *b_directory_get_path_cstr(const b_directory *dir);
BLUE_API const char *b_directory_get_rel_path_cstr(const b_directory *dir);
BLUE_API b_result b_directory_delete(b_directory *dir);
BLUE_API bool b_directory_path_exists(const b_directory *root, const b_path *path);
BLUE_API bool b_directory_path_is_file(const b_directory *root, const b_path *path);
BLUE_API bool b_directory_path_is_directory(
const b_directory *root, const b_path *path);
BLUE_API b_result b_directory_path_stat(
const b_directory *root, const b_path *path, struct b_file_info *out);
BLUE_API b_result b_directory_path_unlink(
const b_directory *root, const b_path *path);
BLUE_API b_iterator *b_directory_begin(
b_directory *dir, b_directory_iterator_flags flags);
B_DECLS_END;
#endif

View File

@@ -1,80 +0,0 @@
#ifndef BLUE_IO_FILE_H_
#define BLUE_IO_FILE_H_
#include <blue/core/error.h>
#include <blue/core/macros.h>
#include <blue/core/misc.h>
#include <blue/core/stream.h>
B_DECLS_BEGIN;
#define B_TYPE_FILE (b_file_get_type())
B_DECLARE_TYPE(b_file);
B_TYPE_CLASS_DECLARATION_BEGIN(b_file)
B_TYPE_CLASS_DECLARATION_END(b_file)
#define B_OFFSET_CURRENT ((size_t)-1)
typedef enum b_seek_basis {
B_SEEK_BEGINNING,
B_SEEK_CURRENT,
B_SEEK_END
} b_seek_basis;
typedef enum b_file_attribute {
B_FILE_ATTRIB_NORMAL = 0x01u,
B_FILE_ATTRIB_DIRECTORY = 0x02u,
B_FILE_ATTRIB_BLOCK_DEVICE = 0x04u,
B_FILE_ATTRIB_CHAR_DEVICE = 0x08u,
B_FILE_ATTRIB_SYMLINK = 0x80u,
} b_file_attribute;
typedef enum b_file_mode {
B_FILE_READ_ONLY = 0x01u,
B_FILE_WRITE_ONLY = 0x02u,
B_FILE_READ_WRITE = B_FILE_READ_ONLY | B_FILE_WRITE_ONLY,
B_FILE_TRUNCATE = 0x04u,
B_FILE_CREATE = 0x08u,
B_FILE_CREATE_ONLY = 0x10u | B_FILE_CREATE,
B_FILE_APPEND = 0x20u,
B_FILE_BINARY = 0x40u,
B_FILE_DELETE_ON_CLOSE = 0x80u,
B_FILE_SHADOW = 0x100u,
} b_file_mode;
typedef struct b_file_info {
b_file_attribute attrib;
b_file_mode mode;
size_t length;
} b_file_info;
BLUE_API b_type b_file_get_type(void);
BLUE_API b_result b_file_open(
B_TYPE_FWDREF(b_directory) * root, const B_TYPE_FWDREF(b_path) * path,
b_file_mode mode, b_file **out);
BLUE_API b_result b_file_open_temp(b_file_mode mode, b_file **out);
BLUE_API b_result b_file_open_shadow(
b_file *original, b_file_mode mode, b_file **out);
BLUE_API b_status b_file_stat(b_file *file, b_file_info *out);
BLUE_API b_status b_file_size(b_file *file, size_t *out_len);
BLUE_API b_status b_file_cursor(b_file *file, size_t *out_pos);
BLUE_API b_status b_file_resize(b_file *file, size_t len);
BLUE_API b_status b_file_seek(b_file *file, long long offset, b_seek_basis basis);
BLUE_API const B_TYPE_FWDREF(b_path) * b_file_path(const b_file *file);
BLUE_API b_status b_file_swap_shadow(b_file *main_file, b_file *shadow_file);
BLUE_API b_status b_file_read(
b_file *file, size_t offset, size_t len, void *buf, size_t *nr_read);
BLUE_API b_status b_file_write(
b_file *file, size_t offset, size_t len, const void *buf,
size_t *nr_written);
B_DECLS_END;
#endif

View File

@@ -1,54 +0,0 @@
#ifndef BLUE_IO_PATH_H_
#define BLUE_IO_PATH_H_
#include <blue/core/macros.h>
#include <blue/core/misc.h>
#include <blue/core/status.h>
#include <blue/ds/string.h>
#include <stddef.h>
B_DECLS_BEGIN;
#define B_TYPE_PATH (b_path_get_type())
B_DECLARE_TYPE(b_path);
B_TYPE_CLASS_DECLARATION_BEGIN(b_path)
B_TYPE_CLASS_DECLARATION_END(b_path)
#define B_RV_PATH(cstr) B_RV(b_path_create_from_cstr(cstr))
struct b_file_info;
BLUE_API b_type b_path_get_type(void);
B_TYPE_DEFAULT_CONSTRUCTOR(b_path, B_TYPE_PATH);
BLUE_API b_path *b_path_create_root();
BLUE_API b_path *b_path_create_cwd();
BLUE_API b_path *b_path_create_from_cstr(const char *path);
BLUE_API b_path *b_path_duplicate(const b_path *path);
BLUE_API b_path *b_path_join(const b_path *paths[], size_t nr_paths);
BLUE_API b_path *b_path_make_absolute(const b_path *in);
BLUE_API b_path *b_path_make_relative(const b_path *in);
BLUE_API b_path *b_path_make_canonical(const b_path *in);
BLUE_API bool b_path_is_absolute(const b_path *path);
BLUE_API bool b_path_exists(const b_path *path);
BLUE_API bool b_path_is_file(const b_path *path);
BLUE_API bool b_path_is_directory(const b_path *path);
BLUE_API b_status b_path_stat(const b_path *path, struct b_file_info *out);
BLUE_API b_status b_path_unlink(const b_path *path);
BLUE_API enum b_status b_path_get_directory(
const b_path *path, b_path **out_dir_path);
BLUE_API enum b_status b_path_get_filename(const b_path *path, b_string *out_name);
BLUE_API const char *b_path_ptr(const b_path *path);
BLUE_API size_t b_path_length(const b_path *path);
B_DECLS_END;
#endif

View File

@@ -0,0 +1,75 @@
#ifndef FX_IO_DIRECTORY_H_
#define FX_IO_DIRECTORY_H_
#include <fx/core/error.h>
#include <fx/core/iterator.h>
#include <fx/core/macros.h>
#include <fx/core/misc.h>
#include <fx/io/file.h>
#include <fx/io/path.h>
#define FX_DIRECTORY_ROOT ((fx_directory *)NULL)
FX_DECLS_BEGIN;
struct fx_directory_p;
#define FX_TYPE_DIRECTORY (fx_directory_get_type())
#define FX_TYPE_DIRECTORY_ITERATOR (fx_directory_iterator_get_type())
FX_DECLARE_TYPE(fx_directory);
FX_DECLARE_TYPE(fx_directory_iterator);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_directory)
FX_TYPE_CLASS_DECLARATION_END(fx_directory)
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_directory_iterator)
FX_TYPE_CLASS_DECLARATION_END(fx_directory_iterator)
struct z__fx_directory_iterator;
typedef enum fx_directory_iterator_flags {
FX_DIRECTORY_ITERATE_PARENT_FIRST = 0x01u,
FX_DIRECTORY_ITERATE_PARENT_LAST = 0x02u,
} fx_directory_iterator_flags;
typedef enum fx_directory_open_flags {
FX_DIRECTORY_OPEN_CREATE = 0x01u,
FX_DIRECTORY_OPEN_CREATE_INTERMEDIATE = 0x03u,
FX_DIRECTORY_OPEN_DELETE_ON_CLOSE = 0x04u,
} fx_directory_open_flags;
typedef struct fx_directory_entry {
const fx_path *filepath;
char *filename;
fx_file_info info;
} fx_directory_entry;
FX_API fx_type fx_directory_get_type(void);
FX_API fx_type fx_directory_iterator_get_type(void);
FX_API fx_result fx_directory_open(
fx_directory *root, const fx_path *path, fx_directory_open_flags flags,
fx_directory **out);
FX_API fx_result fx_directory_open_temp(fx_directory **out);
FX_API const fx_path *fx_directory_get_path(const fx_directory *dir);
FX_API const fx_path *fx_directory_get_rel_path(const fx_directory *dir);
FX_API const char *fx_directory_get_path_cstr(const fx_directory *dir);
FX_API const char *fx_directory_get_rel_path_cstr(const fx_directory *dir);
FX_API fx_result fx_directory_delete(fx_directory *dir);
FX_API bool fx_directory_path_exists(const fx_directory *root, const fx_path *path);
FX_API bool fx_directory_path_is_file(const fx_directory *root, const fx_path *path);
FX_API bool fx_directory_path_is_directory(
const fx_directory *root, const fx_path *path);
FX_API fx_result fx_directory_path_stat(
const fx_directory *root, const fx_path *path, struct fx_file_info *out);
FX_API fx_result fx_directory_path_unlink(
const fx_directory *root, const fx_path *path);
FX_API fx_iterator *fx_directory_begin(
fx_directory *dir, fx_directory_iterator_flags flags);
FX_DECLS_END;
#endif

80
io/include/fx/io/file.h Normal file
View File

@@ -0,0 +1,80 @@
#ifndef FX_IO_FILE_H_
#define FX_IO_FILE_H_
#include <fx/core/error.h>
#include <fx/core/macros.h>
#include <fx/core/misc.h>
#include <fx/core/stream.h>
FX_DECLS_BEGIN;
#define FX_TYPE_FILE (fx_file_get_type())
FX_DECLARE_TYPE(fx_file);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_file)
FX_TYPE_CLASS_DECLARATION_END(fx_file)
#define FX_OFFSET_CURRENT ((size_t)-1)
typedef enum fx_seek_basis {
FX_SEEK_BEGINNING,
FX_SEEK_CURRENT,
FX_SEEK_END
} fx_seek_basis;
typedef enum fx_file_attribute {
FX_FILE_ATTRIB_NORMAL = 0x01u,
FX_FILE_ATTRIB_DIRECTORY = 0x02u,
FX_FILE_ATTRIB_BLOCK_DEVICE = 0x04u,
FX_FILE_ATTRIB_CHAR_DEVICE = 0x08u,
FX_FILE_ATTRIB_SYMLINK = 0x80u,
} fx_file_attribute;
typedef enum fx_file_mode {
FX_FILE_READ_ONLY = 0x01u,
FX_FILE_WRITE_ONLY = 0x02u,
FX_FILE_READ_WRITE = FX_FILE_READ_ONLY | FX_FILE_WRITE_ONLY,
FX_FILE_TRUNCATE = 0x04u,
FX_FILE_CREATE = 0x08u,
FX_FILE_CREATE_ONLY = 0x10u | FX_FILE_CREATE,
FX_FILE_APPEND = 0x20u,
FX_FILE_BINARY = 0x40u,
FX_FILE_DELETE_ON_CLOSE = 0x80u,
FX_FILE_SHADOW = 0x100u,
} fx_file_mode;
typedef struct fx_file_info {
fx_file_attribute attrib;
fx_file_mode mode;
size_t length;
} fx_file_info;
FX_API fx_type fx_file_get_type(void);
FX_API fx_result fx_file_open(
FX_TYPE_FWDREF(fx_directory) * root, const FX_TYPE_FWDREF(fx_path) * path,
fx_file_mode mode, fx_file **out);
FX_API fx_result fx_file_open_temp(fx_file_mode mode, fx_file **out);
FX_API fx_result fx_file_open_shadow(
fx_file *original, fx_file_mode mode, fx_file **out);
FX_API fx_status fx_file_stat(fx_file *file, fx_file_info *out);
FX_API fx_status fx_file_size(fx_file *file, size_t *out_len);
FX_API fx_status fx_file_cursor(fx_file *file, size_t *out_pos);
FX_API fx_status fx_file_resize(fx_file *file, size_t len);
FX_API fx_status fx_file_seek(fx_file *file, long long offset, fx_seek_basis basis);
FX_API const FX_TYPE_FWDREF(fx_path) * fx_file_path(const fx_file *file);
FX_API fx_status fx_file_swap_shadow(fx_file *main_file, fx_file *shadow_file);
FX_API fx_status fx_file_read(
fx_file *file, size_t offset, size_t len, void *buf, size_t *nr_read);
FX_API fx_status fx_file_write(
fx_file *file, size_t offset, size_t len, const void *buf,
size_t *nr_written);
FX_DECLS_END;
#endif

54
io/include/fx/io/path.h Normal file
View File

@@ -0,0 +1,54 @@
#ifndef FX_IO_PATH_H_
#define FX_IO_PATH_H_
#include <fx/core/macros.h>
#include <fx/core/misc.h>
#include <fx/core/status.h>
#include <fx/ds/string.h>
#include <stddef.h>
FX_DECLS_BEGIN;
#define FX_TYPE_PATH (fx_path_get_type())
FX_DECLARE_TYPE(fx_path);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_path)
FX_TYPE_CLASS_DECLARATION_END(fx_path)
#define FX_RV_PATH(cstr) FX_RV(fx_path_create_from_cstr(cstr))
struct fx_file_info;
FX_API fx_type fx_path_get_type(void);
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_path, FX_TYPE_PATH);
FX_API fx_path *fx_path_create_root();
FX_API fx_path *fx_path_create_cwd();
FX_API fx_path *fx_path_create_from_cstr(const char *path);
FX_API fx_path *fx_path_duplicate(const fx_path *path);
FX_API fx_path *fx_path_join(const fx_path *paths[], size_t nr_paths);
FX_API fx_path *fx_path_make_absolute(const fx_path *in);
FX_API fx_path *fx_path_make_relative(const fx_path *in);
FX_API fx_path *fx_path_make_canonical(const fx_path *in);
FX_API bool fx_path_is_absolute(const fx_path *path);
FX_API bool fx_path_exists(const fx_path *path);
FX_API bool fx_path_is_file(const fx_path *path);
FX_API bool fx_path_is_directory(const fx_path *path);
FX_API fx_status fx_path_stat(const fx_path *path, struct fx_file_info *out);
FX_API fx_status fx_path_unlink(const fx_path *path);
FX_API enum fx_status fx_path_get_directory(
const fx_path *path, fx_path **out_dir_path);
FX_API enum fx_status fx_path_get_filename(const fx_path *path, fx_string *out_name);
FX_API const char *fx_path_ptr(const fx_path *path);
FX_API size_t fx_path_length(const fx_path *path);
FX_DECLS_END;
#endif

View File

@@ -1,9 +1,9 @@
#include "misc.h"
#include "posix.h"
#include <blue/core/error.h>
#include <blue/ds/string.h>
#include <blue/io/directory.h>
#include <fx/core/error.h>
#include <fx/ds/string.h>
#include <fx/io/directory.h>
#include <errno.h>
#include <fcntl.h>
#include <fts.h>
@@ -18,165 +18,165 @@ enum directory_flags {
DIRECTORY_DELETE_ON_CLOSE = 0x01u,
};
struct b_directory_p {
struct fx_directory_p {
enum directory_flags d_flags;
int d_fd;
b_path *d_path_rel;
b_path *d_path_abs;
fx_path *d_path_rel;
fx_path *d_path_abs;
};
struct b_directory_iterator_p {
struct b_directory_p *_p;
struct fx_directory_iterator_p {
struct fx_directory_p *_p;
FTS *fts;
FTSENT *ent;
b_directory_iterator_flags flags;
b_directory *root;
fx_directory_iterator_flags flags;
fx_directory *root;
b_directory_entry entry;
fx_directory_entry entry;
};
/*** PRIVATE FUNCTIONS ********************************************************/
static const b_path *directory_get_path(const struct b_directory_p *dir)
static const fx_path *directory_get_path(const struct fx_directory_p *dir)
{
return dir->d_path_abs;
}
static const b_path *directory_get_rel_path(const struct b_directory_p *dir)
static const fx_path *directory_get_rel_path(const struct fx_directory_p *dir)
{
return dir->d_path_rel;
}
static const char *directory_get_path_cstr(const struct b_directory_p *dir)
static const char *directory_get_path_cstr(const struct fx_directory_p *dir)
{
return b_path_ptr(dir->d_path_abs);
return fx_path_ptr(dir->d_path_abs);
}
static const char *directory_get_rel_path_cstr(const struct b_directory_p *dir)
static const char *directory_get_rel_path_cstr(const struct fx_directory_p *dir)
{
return b_path_ptr(dir->d_path_rel);
return fx_path_ptr(dir->d_path_rel);
}
static b_result directory_delete(b_directory *dir, struct b_directory_p *dir_p)
static fx_result directory_delete(fx_directory *dir, struct fx_directory_p *dir_p)
{
enum b_status status = B_SUCCESS;
enum fx_status status = FX_SUCCESS;
b_iterator *it = b_directory_begin(dir, B_DIRECTORY_ITERATE_PARENT_LAST);
while (B_OK(b_iterator_get_status(it))) {
b_iterator_erase(it);
fx_iterator *it = fx_directory_begin(dir, FX_DIRECTORY_ITERATE_PARENT_LAST);
while (FX_OK(fx_iterator_get_status(it))) {
fx_iterator_erase(it);
}
status = b_iterator_get_status(it);
if (!B_OK(status) && status != B_ERR_NO_DATA) {
return B_RESULT_STATUS(status);
status = fx_iterator_get_status(it);
if (!FX_OK(status) && status != FX_ERR_NO_DATA) {
return FX_RESULT_STATUS(status);
}
status = b_path_unlink(dir_p->d_path_abs);
if (!B_OK(status)) {
return B_RESULT_STATUS(status);
status = fx_path_unlink(dir_p->d_path_abs);
if (!FX_OK(status)) {
return FX_RESULT_STATUS(status);
}
return B_RESULT_SUCCESS;
return FX_RESULT_SUCCESS;
}
static bool directory_path_exists(
const struct b_directory_p *root, const b_path *path)
const struct fx_directory_p *root, const fx_path *path)
{
const b_path *parts[] = {
const fx_path *parts[] = {
root ? root->d_path_abs : NULL,
path,
};
b_path *abs_path = b_path_join(parts, sizeof parts / sizeof parts[0]);
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
if (!abs_path) {
return false;
}
bool result = b_path_exists(abs_path);
b_path_unref(abs_path);
bool result = fx_path_exists(abs_path);
fx_path_unref(abs_path);
return result;
}
static bool directory_path_is_file(
const struct b_directory_p *root, const b_path *path)
const struct fx_directory_p *root, const fx_path *path)
{
const b_path *parts[] = {
const fx_path *parts[] = {
root ? root->d_path_abs : NULL,
path,
};
b_path *abs_path = b_path_join(parts, sizeof parts / sizeof parts[0]);
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
if (!abs_path) {
return false;
}
bool result = b_path_is_file(abs_path);
b_path_unref(abs_path);
bool result = fx_path_is_file(abs_path);
fx_path_unref(abs_path);
return result;
}
static bool directory_path_is_directory(
const struct b_directory_p *root, const b_path *path)
const struct fx_directory_p *root, const fx_path *path)
{
const b_path *parts[] = {
const fx_path *parts[] = {
root ? root->d_path_abs : NULL,
path,
};
b_path *abs_path = b_path_join(parts, sizeof parts / sizeof parts[0]);
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
if (!abs_path) {
return false;
}
bool result = b_path_is_directory(abs_path);
b_path_unref(abs_path);
bool result = fx_path_is_directory(abs_path);
fx_path_unref(abs_path);
return result;
}
static b_result directory_path_stat(
const struct b_directory_p *root, const b_path *path,
struct b_file_info *out)
static fx_result directory_path_stat(
const struct fx_directory_p *root, const fx_path *path,
struct fx_file_info *out)
{
const b_path *parts[] = {
const fx_path *parts[] = {
root ? root->d_path_abs : NULL,
path,
};
b_path *abs_path = b_path_join(parts, sizeof parts / sizeof parts[0]);
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
if (!abs_path) {
return B_RESULT_ERR(NO_MEMORY);
return FX_RESULT_ERR(NO_MEMORY);
}
enum b_status status = b_path_stat(abs_path, out);
b_path_unref(abs_path);
enum fx_status status = fx_path_stat(abs_path, out);
fx_path_unref(abs_path);
return B_RESULT_STATUS(status);
return FX_RESULT_STATUS(status);
}
static b_result directory_path_unlink(
const struct b_directory_p *root, const b_path *path)
static fx_result directory_path_unlink(
const struct fx_directory_p *root, const fx_path *path)
{
const b_path *parts[] = {
const fx_path *parts[] = {
root ? root->d_path_abs : NULL,
path,
};
b_path *abs_path = b_path_join(parts, sizeof parts / sizeof parts[0]);
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
if (!abs_path) {
return B_RESULT_ERR(NO_MEMORY);
return FX_RESULT_ERR(NO_MEMORY);
}
enum b_status status = b_path_unlink(abs_path);
b_path_unref(abs_path);
enum fx_status status = fx_path_unlink(abs_path);
fx_path_unref(abs_path);
return B_RESULT_STATUS(status);
return FX_RESULT_STATUS(status);
}
static b_result create_directory(struct b_directory_p *root, const char *path)
static fx_result create_directory(struct fx_directory_p *root, const char *path)
{
int root_fd = root ? root->d_fd : -1;
int err;
@@ -188,24 +188,24 @@ static b_result create_directory(struct b_directory_p *root, const char *path)
}
if (err == 0 || errno == EEXIST) {
return B_RESULT_SUCCESS;
return FX_RESULT_SUCCESS;
}
return b_result_from_errno_with_subfilepath(
errno, path, directory_get_rel_path_cstr(root), B_ERR_IO_FAILURE);
return fx_result_from_errno_with_subfilepath(
errno, path, directory_get_rel_path_cstr(root), FX_ERR_IO_FAILURE);
}
static b_result create_directory_hierarchy(
struct b_directory_p *root, const char *path)
static fx_result create_directory_hierarchy(
struct fx_directory_p *root, const char *path)
{
int root_fd = root ? root->d_fd : AT_FDCWD;
char *path_buf = b_strdup(path);
char *path_buf = fx_strdup(path);
if (!path_buf) {
return B_RESULT_ERR(NO_MEMORY);
return FX_RESULT_ERR(NO_MEMORY);
}
b_result result = B_RESULT_SUCCESS;
fx_result result = FX_RESULT_SUCCESS;
for (size_t i = 0; path_buf[i]; i++) {
if (path_buf[i] != '/') {
continue;
@@ -215,9 +215,9 @@ static b_result create_directory_hierarchy(
int err = mkdirat(root_fd, path_buf, 0755);
if (err != 0 && errno != EEXIST) {
result = b_result_from_errno_with_subfilepath(
result = fx_result_from_errno_with_subfilepath(
errno, path_buf, directory_get_rel_path_cstr(root),
B_ERR_IO_FAILURE);
FX_ERR_IO_FAILURE);
break;
}
@@ -226,112 +226,112 @@ static b_result create_directory_hierarchy(
int err = mkdirat(root_fd, path_buf, 0755);
if (err != 0 && errno != EEXIST) {
result = b_result_from_errno_with_subfilepath(
result = fx_result_from_errno_with_subfilepath(
errno, path_buf, directory_get_rel_path_cstr(root),
B_ERR_IO_FAILURE);
FX_ERR_IO_FAILURE);
}
free(path_buf);
return result;
}
static b_result directory_open(
struct b_directory_p *root, const b_path *path,
b_directory_open_flags flags, b_directory **out)
static fx_result directory_open(
struct fx_directory_p *root, const fx_path *path,
fx_directory_open_flags flags, fx_directory **out)
{
enum b_status status = B_SUCCESS;
enum fx_status status = FX_SUCCESS;
int root_fd = root ? root->d_fd : AT_FDCWD;
const char *path_cstr = b_path_ptr(path);
const char *path_cstr = fx_path_ptr(path);
if (root) {
while (*path_cstr == '/') {
path_cstr++;
}
}
b_result result = B_RESULT_SUCCESS;
if ((flags & B_DIRECTORY_OPEN_CREATE_INTERMEDIATE)
== B_DIRECTORY_OPEN_CREATE_INTERMEDIATE) {
fx_result result = FX_RESULT_SUCCESS;
if ((flags & FX_DIRECTORY_OPEN_CREATE_INTERMEDIATE)
== FX_DIRECTORY_OPEN_CREATE_INTERMEDIATE) {
result = create_directory_hierarchy(root, path_cstr);
} else if ((flags & B_DIRECTORY_OPEN_CREATE) == B_DIRECTORY_OPEN_CREATE) {
} else if ((flags & FX_DIRECTORY_OPEN_CREATE) == FX_DIRECTORY_OPEN_CREATE) {
result = create_directory(root, path_cstr);
}
if (b_result_is_error(result)) {
return b_result_propagate(result);
if (fx_result_is_error(result)) {
return fx_result_propagate(result);
}
int fd = openat(root_fd, path_cstr, O_DIRECTORY);
if (fd == -1) {
status = b_status_from_errno(errno, B_ERR_IO_FAILURE);
return B_RESULT_STATUS(status);
status = fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
return FX_RESULT_STATUS(status);
}
b_directory *dir = b_object_create(B_TYPE_DIRECTORY);
b_path *cwd = NULL;
struct b_directory_p *p = b_object_get_private(dir, B_TYPE_DIRECTORY);
fx_directory *dir = fx_object_create(FX_TYPE_DIRECTORY);
fx_path *cwd = NULL;
struct fx_directory_p *p = fx_object_get_private(dir, FX_TYPE_DIRECTORY);
if (!root) {
cwd = b_path_create_cwd();
cwd = fx_path_create_cwd();
}
const b_path *parts[] = {
const fx_path *parts[] = {
root ? root->d_path_abs : cwd,
path,
};
b_path *new_path = b_path_join(parts, sizeof parts / sizeof parts[0]);
fx_path *new_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
if (!new_path) {
return B_RESULT_ERR(NO_MEMORY);
return FX_RESULT_ERR(NO_MEMORY);
}
if (cwd) {
b_path_unref(cwd);
fx_path_unref(cwd);
}
p->d_path_abs = new_path;
p->d_path_rel = b_path_duplicate(path);
p->d_path_rel = fx_path_duplicate(path);
p->d_fd = fd;
if (flags & B_DIRECTORY_OPEN_DELETE_ON_CLOSE) {
if (flags & FX_DIRECTORY_OPEN_DELETE_ON_CLOSE) {
p->d_flags = DIRECTORY_DELETE_ON_CLOSE;
}
*out = dir;
return B_RESULT_SUCCESS;
return FX_RESULT_SUCCESS;
}
/*** PUBLIC FUNCTIONS *********************************************************/
b_result b_directory_open(
b_directory *root, const b_path *path, b_directory_open_flags flags,
b_directory **out)
fx_result fx_directory_open(
fx_directory *root, const fx_path *path, fx_directory_open_flags flags,
fx_directory **out)
{
B_CLASS_DISPATCH_STATIC(
B_TYPE_DIRECTORY, directory_open, root, path, flags, out);
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY, directory_open, root, path, flags, out);
}
b_result b_directory_open_temp(b_directory **out)
fx_result fx_directory_open_temp(fx_directory **out)
{
char name[16];
char path[128];
while (1) {
z__b_io_generate_tmp_filename(name, sizeof name);
z__fx_io_generate_tmp_filename(name, sizeof name);
snprintf(path, sizeof path, "/tmp/%s", name);
b_path *rpath = b_path_create_from_cstr(path);
fx_path *rpath = fx_path_create_from_cstr(path);
b_directory *dir = NULL;
b_result status = b_directory_open(
B_DIRECTORY_ROOT, rpath, B_DIRECTORY_OPEN_CREATE, &dir);
struct b_directory_p *p
= b_object_get_private(dir, B_TYPE_DIRECTORY);
fx_directory *dir = NULL;
fx_result status = fx_directory_open(
FX_DIRECTORY_ROOT, rpath, FX_DIRECTORY_OPEN_CREATE, &dir);
struct fx_directory_p *p
= fx_object_get_private(dir, FX_TYPE_DIRECTORY);
if (b_error_get_status_code(status) == B_ERR_NAME_EXISTS) {
b_path_unref(rpath);
if (fx_error_get_status_code(status) == FX_ERR_NAME_EXISTS) {
fx_path_unref(rpath);
continue;
}
@@ -339,82 +339,82 @@ b_result b_directory_open_temp(b_directory **out)
p->d_flags |= DIRECTORY_DELETE_ON_CLOSE;
}
b_path_unlink(rpath);
b_path_unref(rpath);
fx_path_unlink(rpath);
fx_path_unref(rpath);
return status;
}
}
const b_path *b_directory_get_path(const b_directory *dir)
const fx_path *fx_directory_get_path(const fx_directory *dir)
{
B_CLASS_DISPATCH_STATIC_0(B_TYPE_DIRECTORY, directory_get_path, dir);
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DIRECTORY, directory_get_path, dir);
}
const b_path *b_directory_get_rel_path(const b_directory *dir)
const fx_path *fx_directory_get_rel_path(const fx_directory *dir)
{
B_CLASS_DISPATCH_STATIC_0(B_TYPE_DIRECTORY, directory_get_rel_path, dir);
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DIRECTORY, directory_get_rel_path, dir);
}
const char *b_directory_get_path_cstr(const b_directory *dir)
const char *fx_directory_get_path_cstr(const fx_directory *dir)
{
B_CLASS_DISPATCH_STATIC_0(B_TYPE_DIRECTORY, directory_get_path_cstr, dir);
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DIRECTORY, directory_get_path_cstr, dir);
}
const char *b_directory_get_rel_path_cstr(const b_directory *dir)
const char *fx_directory_get_rel_path_cstr(const fx_directory *dir)
{
B_CLASS_DISPATCH_STATIC_0(
B_TYPE_DIRECTORY, directory_get_rel_path_cstr, dir);
FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_DIRECTORY, directory_get_rel_path_cstr, dir);
}
b_result b_directory_delete(b_directory *dir)
fx_result fx_directory_delete(fx_directory *dir)
{
struct b_directory_p *p = b_object_get_private(dir, B_TYPE_DIRECTORY);
struct fx_directory_p *p = fx_object_get_private(dir, FX_TYPE_DIRECTORY);
p->d_flags |= DIRECTORY_DELETE_ON_CLOSE;
/* TODO allow object release functions to return a b_result value */
b_directory_unref(dir);
return B_RESULT_SUCCESS;
/* TODO allow object release functions to return a fx_result value */
fx_directory_unref(dir);
return FX_RESULT_SUCCESS;
}
bool b_directory_path_exists(const b_directory *root, const b_path *path)
bool fx_directory_path_exists(const fx_directory *root, const fx_path *path)
{
B_CLASS_DISPATCH_STATIC(B_TYPE_DIRECTORY, directory_path_exists, root, path);
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DIRECTORY, directory_path_exists, root, path);
}
bool b_directory_path_is_file(const b_directory *root, const b_path *path)
bool fx_directory_path_is_file(const fx_directory *root, const fx_path *path)
{
B_CLASS_DISPATCH_STATIC(
B_TYPE_DIRECTORY, directory_path_is_file, root, path);
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY, directory_path_is_file, root, path);
}
bool b_directory_path_is_directory(const b_directory *root, const b_path *path)
bool fx_directory_path_is_directory(const fx_directory *root, const fx_path *path)
{
B_CLASS_DISPATCH_STATIC(
B_TYPE_DIRECTORY, directory_path_is_directory, root, path);
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY, directory_path_is_directory, root, path);
}
b_result b_directory_path_stat(
const b_directory *root, const b_path *path, struct b_file_info *out)
fx_result fx_directory_path_stat(
const fx_directory *root, const fx_path *path, struct fx_file_info *out)
{
B_CLASS_DISPATCH_STATIC(
B_TYPE_DIRECTORY, directory_path_stat, root, path, out);
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY, directory_path_stat, root, path, out);
}
b_result b_directory_path_unlink(const b_directory *root, const b_path *path)
fx_result fx_directory_path_unlink(const fx_directory *root, const fx_path *path)
{
B_CLASS_DISPATCH_STATIC(B_TYPE_DIRECTORY, directory_path_unlink, root, path);
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DIRECTORY, directory_path_unlink, root, path);
}
/*** VIRTUAL FUNCTIONS ********************************************************/
static void directory_init(b_object *obj, void *priv)
static void directory_init(fx_object *obj, void *priv)
{
struct b_directory_p *dir = priv;
struct fx_directory_p *dir = priv;
}
static void directory_fini(b_object *obj, void *priv)
static void directory_fini(fx_object *obj, void *priv)
{
struct b_directory_p *dir = priv;
struct fx_directory_p *dir = priv;
close(dir->d_fd);
@@ -422,7 +422,7 @@ static void directory_fini(b_object *obj, void *priv)
directory_delete(obj, dir);
}
b_path_unref(dir->d_path_abs);
fx_path_unref(dir->d_path_abs);
}
/*** ITERATOR FUNCTIONS *******************************************************/
@@ -432,17 +432,17 @@ static int ftsent_compare(const FTSENT **one, const FTSENT **two)
return (strcmp((*one)->fts_name, (*two)->fts_name));
}
static void update_iterator_data(struct b_directory_iterator_p *it)
static void update_iterator_data(struct fx_directory_iterator_p *it)
{
if (it->entry.filepath) {
b_path_unref((b_path *)it->entry.filepath);
fx_path_unref((fx_path *)it->entry.filepath);
it->entry.filepath = NULL;
}
FTSENT *ent = it->ent;
b_path *path = b_path_create_from_cstr(
ent->fts_path + b_path_length(it->_p->d_path_abs) + 1);
fx_path *path = fx_path_create_from_cstr(
ent->fts_path + fx_path_length(it->_p->d_path_abs) + 1);
it->entry.filename = ent->fts_name;
it->entry.filepath = path;
@@ -452,32 +452,32 @@ static void update_iterator_data(struct b_directory_iterator_p *it)
it->entry.info.length = ent->fts_statp->st_size;
if (S_ISREG(ent->fts_statp->st_mode)) {
it->entry.info.attrib |= B_FILE_ATTRIB_NORMAL;
it->entry.info.attrib |= FX_FILE_ATTRIB_NORMAL;
}
if (S_ISDIR(ent->fts_statp->st_mode)) {
it->entry.info.attrib |= B_FILE_ATTRIB_DIRECTORY;
it->entry.info.attrib |= FX_FILE_ATTRIB_DIRECTORY;
}
if (S_ISBLK(ent->fts_statp->st_mode)) {
it->entry.info.attrib |= B_FILE_ATTRIB_BLOCK_DEVICE;
it->entry.info.attrib |= FX_FILE_ATTRIB_BLOCK_DEVICE;
}
if (S_ISCHR(ent->fts_statp->st_mode)) {
it->entry.info.attrib |= B_FILE_ATTRIB_CHAR_DEVICE;
it->entry.info.attrib |= FX_FILE_ATTRIB_CHAR_DEVICE;
}
if (S_ISLNK(ent->fts_statp->st_mode)) {
it->entry.info.attrib |= B_FILE_ATTRIB_SYMLINK;
it->entry.info.attrib |= FX_FILE_ATTRIB_SYMLINK;
}
}
static void iterator_fini(b_object *obj, void *priv)
static void iterator_fini(fx_object *obj, void *priv)
{
struct b_directory_iterator_p *it = priv;
struct fx_directory_iterator_p *it = priv;
if (it->entry.filepath) {
b_path_unref((b_path *)it->entry.filepath);
fx_path_unref((fx_path *)it->entry.filepath);
it->entry.filepath = NULL;
}
@@ -486,21 +486,21 @@ static void iterator_fini(b_object *obj, void *priv)
}
}
b_iterator *b_directory_begin(
b_directory *directory, enum b_directory_iterator_flags flags)
fx_iterator *fx_directory_begin(
fx_directory *directory, enum fx_directory_iterator_flags flags)
{
b_iterator *it_obj = b_object_create(B_TYPE_DIRECTORY_ITERATOR);
struct b_directory_iterator_p *it
= b_object_get_private(it_obj, B_TYPE_DIRECTORY_ITERATOR);
fx_iterator *it_obj = fx_object_create(FX_TYPE_DIRECTORY_ITERATOR);
struct fx_directory_iterator_p *it
= fx_object_get_private(it_obj, FX_TYPE_DIRECTORY_ITERATOR);
it->flags = flags;
it->root = directory;
it->_p = b_object_get_private(directory, B_TYPE_DIRECTORY);
it->_p = fx_object_get_private(directory, FX_TYPE_DIRECTORY);
int fts_flags = FTS_COMFOLLOW | FTS_NOCHDIR;
const char *path_list[] = {
b_path_ptr(it->_p->d_path_abs),
fx_path_ptr(it->_p->d_path_abs),
NULL,
};
@@ -512,7 +512,7 @@ b_iterator *b_directory_begin(
it->ent = fts_read(it->fts);
if (!it->ent) {
b_iterator_set_status(it_obj, B_ERR_NO_DATA);
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
return it_obj;
}
@@ -527,14 +527,14 @@ b_iterator *b_directory_begin(
done = true;
break;
case FTS_D:
if (it->flags & B_DIRECTORY_ITERATE_PARENT_LAST) {
if (it->flags & FX_DIRECTORY_ITERATE_PARENT_LAST) {
continue;
}
done = true;
break;
case FTS_DP:
if (it->flags & B_DIRECTORY_ITERATE_PARENT_FIRST) {
if (it->flags & FX_DIRECTORY_ITERATE_PARENT_FIRST) {
continue;
}
done = true;
@@ -549,22 +549,22 @@ b_iterator *b_directory_begin(
return it_obj;
}
static b_iterator *iterator_begin(b_object *obj)
static fx_iterator *iterator_begin(fx_object *obj)
{
return b_directory_begin(obj, B_DIRECTORY_ITERATE_PARENT_FIRST);
return fx_directory_begin(obj, FX_DIRECTORY_ITERATE_PARENT_FIRST);
}
static const b_iterator *iterator_cbegin(const b_object *obj)
static const fx_iterator *iterator_cbegin(const fx_object *obj)
{
return b_directory_begin((b_object *)obj, B_DIRECTORY_ITERATE_PARENT_FIRST);
return fx_directory_begin((fx_object *)obj, FX_DIRECTORY_ITERATE_PARENT_FIRST);
}
static enum b_status iterator_move_next(const b_iterator *obj)
static enum fx_status iterator_move_next(const fx_iterator *obj)
{
struct b_directory_iterator_p *it
= b_object_get_private(obj, B_TYPE_DIRECTORY_ITERATOR);
struct fx_directory_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
if (!it || !it->fts) {
return B_ERR_NO_DATA;
return FX_ERR_NO_DATA;
}
bool done = false;
@@ -573,7 +573,7 @@ static enum b_status iterator_move_next(const b_iterator *obj)
it->ent = fts_read(it->fts);
if (!it->ent) {
return B_ERR_NO_DATA;
return FX_ERR_NO_DATA;
}
if (it->ent->fts_level == 0) {
@@ -587,13 +587,13 @@ static enum b_status iterator_move_next(const b_iterator *obj)
done = true;
break;
case FTS_D:
if (it->flags & B_DIRECTORY_ITERATE_PARENT_LAST) {
if (it->flags & FX_DIRECTORY_ITERATE_PARENT_LAST) {
continue;
}
done = true;
break;
case FTS_DP:
if (it->flags & B_DIRECTORY_ITERATE_PARENT_FIRST) {
if (it->flags & FX_DIRECTORY_ITERATE_PARENT_FIRST) {
continue;
}
done = true;
@@ -605,79 +605,79 @@ static enum b_status iterator_move_next(const b_iterator *obj)
}
update_iterator_data(it);
return B_SUCCESS;
return FX_SUCCESS;
}
static enum b_status iterator_erase(b_iterator *obj)
static enum fx_status iterator_erase(fx_iterator *obj)
{
struct b_directory_iterator_p *it
= b_object_get_private(obj, B_TYPE_DIRECTORY_ITERATOR);
b_result result = b_directory_path_unlink(it->root, it->entry.filepath);
if (b_result_is_error(result)) {
enum b_status status = b_error_get_status_code(result);
b_error_discard(result);
struct fx_directory_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
fx_result result = fx_directory_path_unlink(it->root, it->entry.filepath);
if (fx_result_is_error(result)) {
enum fx_status status = fx_error_get_status_code(result);
fx_error_discard(result);
return status;
}
return iterator_move_next(obj);
}
static b_iterator_value iterator_get_value(b_iterator *obj)
static fx_iterator_value iterator_get_value(fx_iterator *obj)
{
struct b_directory_iterator_p *it
= b_object_get_private(obj, B_TYPE_DIRECTORY_ITERATOR);
struct fx_directory_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
return B_ITERATOR_VALUE_PTR(&it->entry);
return FX_ITERATOR_VALUE_PTR(&it->entry);
}
static const b_iterator_value iterator_get_cvalue(const b_iterator *obj)
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
{
struct b_directory_iterator_p *it
= b_object_get_private(obj, B_TYPE_DIRECTORY_ITERATOR);
struct fx_directory_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
return B_ITERATOR_VALUE_CPTR(&it->entry);
return FX_ITERATOR_VALUE_CPTR(&it->entry);
}
/*** CLASS DEFINITION *********************************************************/
// ---- b_directory DEFINITION
B_TYPE_CLASS_DEFINITION_BEGIN(b_directory)
B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT)
B_INTERFACE_ENTRY(to_string) = NULL;
B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT)
// ---- fx_directory DEFINITION
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_directory)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
B_TYPE_CLASS_INTERFACE_BEGIN(b_iterable, B_TYPE_ITERABLE)
B_INTERFACE_ENTRY(it_begin) = iterator_begin;
B_INTERFACE_ENTRY(it_cbegin) = iterator_cbegin;
B_TYPE_CLASS_INTERFACE_END(b_iterable, B_TYPE_ITERABLE)
B_TYPE_CLASS_DEFINITION_END(b_directory)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
FX_INTERFACE_ENTRY(it_begin) = iterator_begin;
FX_INTERFACE_ENTRY(it_cbegin) = iterator_cbegin;
FX_TYPE_CLASS_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_DEFINITION_END(fx_directory)
B_TYPE_DEFINITION_BEGIN(b_directory)
B_TYPE_ID(0x10d36546, 0x7f96, 0x464b, 0xbc4d, 0xe504b283fa45);
B_TYPE_CLASS(b_directory_class);
B_TYPE_IMPLEMENTS(B_TYPE_ITERABLE);
B_TYPE_INSTANCE_PRIVATE(struct b_directory_p);
B_TYPE_INSTANCE_INIT(directory_init);
B_TYPE_INSTANCE_FINI(directory_fini);
B_TYPE_DEFINITION_END(b_directory)
FX_TYPE_DEFINITION_BEGIN(fx_directory)
FX_TYPE_ID(0x10d36546, 0x7f96, 0x464b, 0xbc4d, 0xe504b283fa45);
FX_TYPE_CLASS(fx_directory_class);
FX_TYPE_IMPLEMENTS(FX_TYPE_ITERABLE);
FX_TYPE_INSTANCE_PRIVATE(struct fx_directory_p);
FX_TYPE_INSTANCE_INIT(directory_init);
FX_TYPE_INSTANCE_FINI(directory_fini);
FX_TYPE_DEFINITION_END(fx_directory)
// ---- b_directory_iterator DEFINITION
B_TYPE_CLASS_DEFINITION_BEGIN(b_directory_iterator)
B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT)
B_INTERFACE_ENTRY(to_string) = NULL;
B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT)
// ---- fx_directory_iterator DEFINITION
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_directory_iterator)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
B_TYPE_CLASS_INTERFACE_BEGIN(b_iterator, B_TYPE_ITERATOR)
B_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
B_INTERFACE_ENTRY(it_erase) = iterator_erase;
B_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
B_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue;
B_TYPE_CLASS_INTERFACE_END(b_iterator, B_TYPE_ITERATOR)
B_TYPE_CLASS_DEFINITION_END(b_directory_iterator)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
FX_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue;
FX_TYPE_CLASS_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_DEFINITION_END(fx_directory_iterator)
B_TYPE_DEFINITION_BEGIN(b_directory_iterator)
B_TYPE_ID(0xc707fce6, 0xc895, 0x4925, 0x8700, 0xa60641dee0cc);
B_TYPE_EXTENDS(B_TYPE_ITERATOR);
B_TYPE_CLASS(b_directory_iterator_class);
B_TYPE_INSTANCE_PRIVATE(struct b_directory_iterator_p);
B_TYPE_DEFINITION_END(b_directory_iterator)
FX_TYPE_DEFINITION_BEGIN(fx_directory_iterator)
FX_TYPE_ID(0xc707fce6, 0xc895, 0x4925, 0x8700, 0xa60641dee0cc);
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
FX_TYPE_CLASS(fx_directory_iterator_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_directory_iterator_p);
FX_TYPE_DEFINITION_END(fx_directory_iterator)

View File

@@ -1,11 +1,11 @@
#include "misc.h"
#include "posix.h"
#include <blue/core/random.h>
#include <blue/ds/string.h>
#include <blue/io/directory.h>
#include <blue/io/file.h>
#include <blue/io/path.h>
#include <fx/core/random.h>
#include <fx/ds/string.h>
#include <fx/io/directory.h>
#include <fx/io/file.h>
#include <fx/io/path.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
@@ -16,270 +16,270 @@
#define CHECK_FLAG(v, f) (((v) & (f)) == (f))
static enum b_status stream_close(b_stream *);
static enum b_status stream_getc(b_stream *, int *);
static enum b_status stream_read(b_stream *, void *, size_t, size_t *);
static enum b_status stream_write(b_stream *, const void *, size_t, size_t *);
static enum b_status stream_seek(b_stream *, long long, b_stream_seek_origin);
static enum b_status stream_tell(const b_stream *, size_t *);
static enum fx_status stream_close(fx_stream *);
static enum fx_status stream_getc(fx_stream *, int *);
static enum fx_status stream_read(fx_stream *, void *, size_t, size_t *);
static enum fx_status stream_write(fx_stream *, const void *, size_t, size_t *);
static enum fx_status stream_seek(fx_stream *, long long, fx_stream_seek_origin);
static enum fx_status stream_tell(const fx_stream *, size_t *);
/*** PRIVATE DATA *************************************************************/
struct b_file_p {
enum b_file_mode mode;
struct fx_file_p {
enum fx_file_mode mode;
int fd;
b_path *path;
fx_path *path;
};
/*** PRIVATE FUNCTIONS ********************************************************/
static unsigned int b_mode_to_unix_mode(enum b_file_mode mode)
static unsigned int fx_mode_to_unix_mode(enum fx_file_mode mode)
{
unsigned int result = 0;
if (CHECK_FLAG(mode, B_FILE_READ_WRITE)) {
if (CHECK_FLAG(mode, FX_FILE_READ_WRITE)) {
result |= O_RDWR;
} else if (CHECK_FLAG(mode, B_FILE_READ_ONLY)) {
} else if (CHECK_FLAG(mode, FX_FILE_READ_ONLY)) {
result |= O_RDONLY;
} else if (CHECK_FLAG(mode, B_FILE_WRITE_ONLY)) {
} else if (CHECK_FLAG(mode, FX_FILE_WRITE_ONLY)) {
result |= O_WRONLY;
} else {
return (unsigned int)-1;
}
if (CHECK_FLAG(mode, B_FILE_TRUNCATE)) {
if (CHECK_FLAG(mode, FX_FILE_TRUNCATE)) {
result |= O_TRUNC;
}
if (CHECK_FLAG(mode, B_FILE_CREATE)) {
if (CHECK_FLAG(mode, FX_FILE_CREATE)) {
result |= O_CREAT;
}
if (CHECK_FLAG(mode, B_FILE_CREATE_ONLY)) {
if (CHECK_FLAG(mode, FX_FILE_CREATE_ONLY)) {
result |= O_EXCL;
}
return result;
}
static b_result file_open_shadow(
struct b_file_p *original, enum b_file_mode mode, b_file **out)
static fx_result file_open_shadow(
struct fx_file_p *original, enum fx_file_mode mode, fx_file **out)
{
mode |= B_FILE_SHADOW | B_FILE_DELETE_ON_CLOSE | B_FILE_CREATE;
mode |= FX_FILE_SHADOW | FX_FILE_DELETE_ON_CLOSE | FX_FILE_CREATE;
b_path *dir;
b_path_get_directory(original->path, &dir);
fx_path *dir;
fx_path_get_directory(original->path, &dir);
b_string *filename = b_string_create();
b_path_get_filename(original->path, filename);
fx_string *filename = fx_string_create();
fx_path_get_filename(original->path, filename);
b_string_prepend_cstr(filename, ".~");
fx_string_prepend_cstr(filename, ".~");
b_path *shadow_filename = b_path_create_from_cstr(b_string_ptr(filename));
b_string_unref(filename);
fx_path *shadow_filename = fx_path_create_from_cstr(fx_string_ptr(filename));
fx_string_unref(filename);
const b_path *parts[] = {
const fx_path *parts[] = {
dir,
shadow_filename,
};
b_path *shadow_filepath
= b_path_join(parts, sizeof parts / sizeof parts[0]);
b_path_unref(dir);
b_path_unref(shadow_filename);
fx_path *shadow_filepath
= fx_path_join(parts, sizeof parts / sizeof parts[0]);
fx_path_unref(dir);
fx_path_unref(shadow_filename);
if (b_path_exists(shadow_filepath)) {
b_path_unlink(shadow_filepath);
if (fx_path_exists(shadow_filepath)) {
fx_path_unlink(shadow_filepath);
}
b_file *shadow_file;
b_result status = b_file_open(
B_DIRECTORY_ROOT, shadow_filepath, mode, &shadow_file);
b_path_unref(shadow_filepath);
fx_file *shadow_file;
fx_result status = fx_file_open(
FX_DIRECTORY_ROOT, shadow_filepath, mode, &shadow_file);
fx_path_unref(shadow_filepath);
if (b_result_is_error(status)) {
if (fx_result_is_error(status)) {
return status;
}
*out = shadow_file;
return B_RESULT_SUCCESS;
return FX_RESULT_SUCCESS;
}
static const b_path *file_path(const struct b_file_p *file)
static const fx_path *file_path(const struct fx_file_p *file)
{
return file->path;
}
static enum b_status file_stat(struct b_file_p *file, struct b_file_info *out)
static enum fx_status file_stat(struct fx_file_p *file, struct fx_file_info *out)
{
struct stat st;
int err = fstat(file->fd, &st);
if (err != 0) {
return b_status_from_errno(errno, B_ERR_IO_FAILURE);
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
memset(out, 0x0, sizeof *out);
return b_file_info_from_stat(&st, out);
return fx_file_info_from_stat(&st, out);
}
static enum b_status file_size(struct b_file_p *file, size_t *out_len)
static enum fx_status file_size(struct fx_file_p *file, size_t *out_len)
{
off_t cur = lseek(file->fd, 0, SEEK_CUR);
if (cur == (off_t)-1) {
return b_status_from_errno(errno, B_ERR_IO_FAILURE);
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
off_t len = lseek(file->fd, 0, SEEK_END);
if (len == (off_t)-1) {
return b_status_from_errno(errno, B_ERR_IO_FAILURE);
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
cur = lseek(file->fd, cur, SEEK_SET);
if (cur == (off_t)-1) {
return b_status_from_errno(errno, B_ERR_IO_FAILURE);
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
*out_len = (size_t)len;
return B_SUCCESS;
return FX_SUCCESS;
}
static enum b_status file_cursor(struct b_file_p *file, size_t *out_pos)
static enum fx_status file_cursor(struct fx_file_p *file, size_t *out_pos)
{
off_t cur = lseek(file->fd, 0, SEEK_CUR);
if (cur == (off_t)-1) {
return b_status_from_errno(errno, B_ERR_IO_FAILURE);
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
*out_pos = (size_t)cur;
return B_SUCCESS;
return FX_SUCCESS;
}
static enum b_status file_resize(struct b_file_p *file, size_t len)
static enum fx_status file_resize(struct fx_file_p *file, size_t len)
{
int err = ftruncate(file->fd, len);
if (err == 0) {
return B_SUCCESS;
return FX_SUCCESS;
}
return b_status_from_errno(errno, B_ERR_IO_FAILURE);
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
static enum b_status file_seek(
struct b_file_p *file, long long offset, enum b_seek_basis basis)
static enum fx_status file_seek(
struct fx_file_p *file, long long offset, enum fx_seek_basis basis)
{
int whence;
switch (basis) {
case B_SEEK_BEGINNING:
case FX_SEEK_BEGINNING:
whence = SEEK_SET;
break;
case B_SEEK_CURRENT:
case FX_SEEK_CURRENT:
whence = SEEK_CUR;
break;
case B_SEEK_END:
case FX_SEEK_END:
whence = SEEK_END;
break;
default:
return B_ERR_INVALID_ARGUMENT;
return FX_ERR_INVALID_ARGUMENT;
}
int err = lseek(file->fd, offset, whence);
if (err == (off_t)-1) {
return b_status_from_errno(errno, B_ERR_IO_FAILURE);
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
return B_SUCCESS;
return FX_SUCCESS;
}
static enum b_status file_swap_shadow(
struct b_file_p *main_file, struct b_file_p *shadow_file)
static enum fx_status file_swap_shadow(
struct fx_file_p *main_file, struct fx_file_p *shadow_file)
{
if (main_file->mode & B_FILE_SHADOW) {
return B_ERR_NOT_SUPPORTED;
if (main_file->mode & FX_FILE_SHADOW) {
return FX_ERR_NOT_SUPPORTED;
}
if (!(shadow_file->mode & B_FILE_SHADOW)) {
return B_ERR_NOT_SUPPORTED;
if (!(shadow_file->mode & FX_FILE_SHADOW)) {
return FX_ERR_NOT_SUPPORTED;
}
b_path *dir_path;
b_path_get_directory(main_file->path, &dir_path);
fx_path *dir_path;
fx_path_get_directory(main_file->path, &dir_path);
b_path *tmp_path = NULL;
fx_path *tmp_path = NULL;
while (1) {
char tmp_name[16];
z__b_io_generate_tmp_filename(tmp_name, sizeof tmp_name);
b_path *tmp_name_p = b_path_create_from_cstr(tmp_name);
z__fx_io_generate_tmp_filename(tmp_name, sizeof tmp_name);
fx_path *tmp_name_p = fx_path_create_from_cstr(tmp_name);
const b_path *parts[] = {
const fx_path *parts[] = {
dir_path,
tmp_name_p,
};
tmp_path = b_path_join(parts, sizeof parts / sizeof parts[0]);
tmp_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
b_path_unref(tmp_name_p);
fx_path_unref(tmp_name_p);
if (!b_path_exists(tmp_path)) {
if (!fx_path_exists(tmp_path)) {
break;
}
b_path_unref(tmp_path);
fx_path_unref(tmp_path);
tmp_path = NULL;
}
b_path_unref(dir_path);
fx_path_unref(dir_path);
int err;
err = rename(b_path_ptr(main_file->path), b_path_ptr(tmp_path));
err = rename(b_path_ptr(shadow_file->path), b_path_ptr(main_file->path));
err = rename(b_path_ptr(tmp_path), b_path_ptr(shadow_file->path));
err = rename(fx_path_ptr(main_file->path), fx_path_ptr(tmp_path));
err = rename(fx_path_ptr(shadow_file->path), fx_path_ptr(main_file->path));
err = rename(fx_path_ptr(tmp_path), fx_path_ptr(shadow_file->path));
b_path_unref(tmp_path);
fx_path_unref(tmp_path);
int fd = main_file->fd;
main_file->fd = shadow_file->fd;
shadow_file->fd = fd;
return B_SUCCESS;
return FX_SUCCESS;
}
static enum b_status file_read(
struct b_file_p *file, size_t offset, size_t len, void *buf, size_t *nr_read)
static enum fx_status file_read(
struct fx_file_p *file, size_t offset, size_t len, void *buf, size_t *nr_read)
{
if (offset != B_OFFSET_CURRENT) {
if (offset != FX_OFFSET_CURRENT) {
lseek(file->fd, offset, SEEK_SET);
}
long r = read(file->fd, buf, len);
enum b_status status = B_SUCCESS;
enum fx_status status = FX_SUCCESS;
if (r < 0) {
status = b_status_from_errno(errno, B_ERR_IO_FAILURE);
status = fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
*nr_read = r;
return status;
}
static enum b_status file_write(
struct b_file_p *file, size_t offset, size_t len, const void *buf,
static enum fx_status file_write(
struct fx_file_p *file, size_t offset, size_t len, const void *buf,
size_t *nr_written)
{
if (offset != B_OFFSET_CURRENT) {
if (offset != FX_OFFSET_CURRENT) {
lseek(file->fd, offset, SEEK_SET);
}
long w = write(file->fd, buf, len);
enum b_status status = B_SUCCESS;
enum fx_status status = FX_SUCCESS;
if (w < 0) {
status = b_status_from_errno(errno, B_ERR_IO_FAILURE);
status = fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
*nr_written = w;
@@ -288,151 +288,151 @@ static enum b_status file_write(
/*** STREAM FUNCTIONS *********************************************************/
static enum b_status stream_close(b_stream *stream)
static enum fx_status stream_close(fx_stream *stream)
{
return B_SUCCESS;
return FX_SUCCESS;
}
static enum b_status stream_getc(b_stream *stream, int *out)
static enum fx_status stream_getc(fx_stream *stream, int *out)
{
struct b_file_p *file = b_object_get_private(stream, B_TYPE_FILE);
struct fx_file_p *file = fx_object_get_private(stream, FX_TYPE_FILE);
char c;
size_t nr_read = 0;
enum b_status status
= file_read(file, B_OFFSET_CURRENT, sizeof c, &c, &nr_read);
if (status != B_SUCCESS) {
enum fx_status status
= file_read(file, FX_OFFSET_CURRENT, sizeof c, &c, &nr_read);
if (status != FX_SUCCESS) {
return status;
}
if (nr_read == 0) {
return B_ERR_NO_DATA;
return FX_ERR_NO_DATA;
}
*out = c;
return B_SUCCESS;
return FX_SUCCESS;
}
static enum b_status stream_read(
b_stream *stream, void *buf, size_t max, size_t *nr_read)
static enum fx_status stream_read(
fx_stream *stream, void *buf, size_t max, size_t *nr_read)
{
struct b_file_p *file = b_object_get_private(stream, B_TYPE_FILE);
struct fx_file_p *file = fx_object_get_private(stream, FX_TYPE_FILE);
enum b_status status
= file_read(file, B_OFFSET_CURRENT, max, buf, nr_read);
enum fx_status status
= file_read(file, FX_OFFSET_CURRENT, max, buf, nr_read);
return status;
}
static enum b_status stream_write(
b_stream *stream, const void *buf, size_t count, size_t *nr_written)
static enum fx_status stream_write(
fx_stream *stream, const void *buf, size_t count, size_t *nr_written)
{
struct b_file_p *file = b_object_get_private(stream, B_TYPE_FILE);
struct fx_file_p *file = fx_object_get_private(stream, FX_TYPE_FILE);
enum b_status status
= file_write(file, B_OFFSET_CURRENT, count, buf, nr_written);
enum fx_status status
= file_write(file, FX_OFFSET_CURRENT, count, buf, nr_written);
return status;
}
static enum b_status stream_seek(
b_stream *stream, long long offset, b_stream_seek_origin origin)
static enum fx_status stream_seek(
fx_stream *stream, long long offset, fx_stream_seek_origin origin)
{
b_seek_basis basis;
fx_seek_basis basis;
switch (origin) {
case B_STREAM_SEEK_START:
basis = B_SEEK_BEGINNING;
case FX_STREAM_SEEK_START:
basis = FX_SEEK_BEGINNING;
break;
case B_STREAM_SEEK_CURRENT:
basis = B_SEEK_CURRENT;
case FX_STREAM_SEEK_CURRENT:
basis = FX_SEEK_CURRENT;
break;
case B_STREAM_SEEK_END:
basis = B_SEEK_END;
case FX_STREAM_SEEK_END:
basis = FX_SEEK_END;
break;
default:
return B_ERR_INVALID_ARGUMENT;
return FX_ERR_INVALID_ARGUMENT;
}
struct b_file_p *file = b_object_get_private(stream, B_TYPE_FILE);
struct fx_file_p *file = fx_object_get_private(stream, FX_TYPE_FILE);
return file_seek(file, offset, basis);
}
static enum b_status stream_tell(const b_stream *stream, size_t *pos)
static enum fx_status stream_tell(const fx_stream *stream, size_t *pos)
{
const struct b_file_p *file = b_object_get_private(stream, B_TYPE_FILE);
const struct fx_file_p *file = fx_object_get_private(stream, FX_TYPE_FILE);
off_t v = lseek(file->fd, 0, SEEK_CUR);
if (v == (off_t)-1) {
return b_status_from_errno(errno, B_ERR_IO_FAILURE);
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
*pos = v;
return B_SUCCESS;
return FX_SUCCESS;
}
/*** PUBLIC FUNCTIONS *********************************************************/
b_result b_file_open(
b_directory *root, const b_path *path, enum b_file_mode mode, b_file **out)
fx_result fx_file_open(
fx_directory *root, const fx_path *path, enum fx_file_mode mode, fx_file **out)
{
const b_path *file_path = path;
unsigned int flags = b_mode_to_unix_mode(mode);
const fx_path *file_path = path;
unsigned int flags = fx_mode_to_unix_mode(mode);
if (flags == (unsigned int)-1) {
return B_RESULT_ERR(INVALID_ARGUMENT);
return FX_RESULT_ERR(INVALID_ARGUMENT);
}
const b_path *root_path = NULL;
const fx_path *root_path = NULL;
bool free_root_path = false;
if (root) {
root_path = b_directory_get_path(root);
root_path = fx_directory_get_path(root);
} else {
root_path = b_path_create_cwd();
root_path = fx_path_create_cwd();
free_root_path = true;
}
const b_path *parts[] = {
const fx_path *parts[] = {
root_path,
file_path,
};
b_path *abs_path = b_path_join(parts, sizeof parts / sizeof parts[0]);
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
if (free_root_path) {
b_path_unref((b_path *)root_path);
fx_path_unref((fx_path *)root_path);
}
if (!abs_path) {
return B_RESULT_ERR(NO_MEMORY);
return FX_RESULT_ERR(NO_MEMORY);
}
int fd = open(b_path_ptr(abs_path), flags, 0644);
int fd = open(fx_path_ptr(abs_path), flags, 0644);
if (fd == -1) {
b_path_unref(abs_path);
return B_RESULT_STATUS(
b_status_from_errno(errno, B_ERR_IO_FAILURE));
fx_path_unref(abs_path);
return FX_RESULT_STATUS(
fx_status_from_errno(errno, FX_ERR_IO_FAILURE));
}
b_file *file = b_object_create(B_TYPE_FILE);
fx_file *file = fx_object_create(FX_TYPE_FILE);
if (!file) {
close(fd);
b_path_unref(abs_path);
return B_RESULT_ERR(NO_MEMORY);
fx_path_unref(abs_path);
return FX_RESULT_ERR(NO_MEMORY);
}
struct b_file_p *p = b_object_get_private(file, B_TYPE_FILE);
b_stream_cfg *cfg = b_object_get_protected(file, B_TYPE_STREAM);
struct fx_file_p *p = fx_object_get_private(file, FX_TYPE_FILE);
fx_stream_cfg *cfg = fx_object_get_protected(file, FX_TYPE_STREAM);
if (mode & B_FILE_READ_ONLY) {
cfg->s_mode |= B_STREAM_READ;
if (mode & FX_FILE_READ_ONLY) {
cfg->s_mode |= FX_STREAM_READ;
}
if (mode & B_FILE_WRITE_ONLY) {
cfg->s_mode |= B_STREAM_WRITE;
if (mode & FX_FILE_WRITE_ONLY) {
cfg->s_mode |= FX_STREAM_WRITE;
}
if (mode & B_FILE_BINARY) {
cfg->s_mode |= B_STREAM_BINARY;
if (mode & FX_FILE_BINARY) {
cfg->s_mode |= FX_STREAM_BINARY;
}
p->fd = fd;
@@ -440,133 +440,133 @@ b_result b_file_open(
p->mode = mode;
*out = file;
return B_RESULT_SUCCESS;
return FX_RESULT_SUCCESS;
}
b_result b_file_open_temp(enum b_file_mode mode, b_file **out)
fx_result fx_file_open_temp(enum fx_file_mode mode, fx_file **out)
{
mode |= B_FILE_DELETE_ON_CLOSE;
mode |= FX_FILE_DELETE_ON_CLOSE;
char name[16];
char path[128];
while (1) {
z__b_io_generate_tmp_filename(name, sizeof name);
z__fx_io_generate_tmp_filename(name, sizeof name);
snprintf(path, sizeof path, "/tmp/%s", name);
b_path *rpath = b_path_create_from_cstr(path);
fx_path *rpath = fx_path_create_from_cstr(path);
b_result status = b_file_open(
B_DIRECTORY_ROOT, rpath, mode | B_FILE_CREATE_ONLY, out);
fx_result status = fx_file_open(
FX_DIRECTORY_ROOT, rpath, mode | FX_FILE_CREATE_ONLY, out);
if (b_error_get_status_code(status) == B_ERR_NAME_EXISTS) {
b_path_unref(rpath);
if (fx_error_get_status_code(status) == FX_ERR_NAME_EXISTS) {
fx_path_unref(rpath);
continue;
}
b_path_unlink(rpath);
b_path_unref(rpath);
fx_path_unlink(rpath);
fx_path_unref(rpath);
return status;
}
}
b_result b_file_open_shadow(b_file *original, enum b_file_mode mode, b_file **out)
fx_result fx_file_open_shadow(fx_file *original, enum fx_file_mode mode, fx_file **out)
{
B_CLASS_DISPATCH_STATIC(B_TYPE_FILE, file_open_shadow, original, mode, out);
FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_open_shadow, original, mode, out);
}
const b_path *b_file_path(const b_file *file)
const fx_path *fx_file_path(const fx_file *file)
{
B_CLASS_DISPATCH_STATIC_0(B_TYPE_FILE, file_path, file);
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_FILE, file_path, file);
}
enum b_status b_file_stat(b_file *file, struct b_file_info *out)
enum fx_status fx_file_stat(fx_file *file, struct fx_file_info *out)
{
B_CLASS_DISPATCH_STATIC(B_TYPE_FILE, file_stat, file, out);
FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_stat, file, out);
}
enum b_status b_file_size(b_file *file, size_t *out_len)
enum fx_status fx_file_size(fx_file *file, size_t *out_len)
{
B_CLASS_DISPATCH_STATIC(B_TYPE_FILE, file_size, file, out_len);
FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_size, file, out_len);
}
enum b_status b_file_cursor(b_file *file, size_t *out_pos)
enum fx_status fx_file_cursor(fx_file *file, size_t *out_pos)
{
B_CLASS_DISPATCH_STATIC(B_TYPE_FILE, file_cursor, file, out_pos);
FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_cursor, file, out_pos);
}
enum b_status b_file_resize(b_file *file, size_t len)
enum fx_status fx_file_resize(fx_file *file, size_t len)
{
B_CLASS_DISPATCH_STATIC(B_TYPE_FILE, file_resize, file, len);
FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_resize, file, len);
}
enum b_status b_file_seek(b_file *file, long long offset, enum b_seek_basis basis)
enum fx_status fx_file_seek(fx_file *file, long long offset, enum fx_seek_basis basis)
{
B_CLASS_DISPATCH_STATIC(B_TYPE_FILE, file_seek, file, offset, basis);
FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_seek, file, offset, basis);
}
enum b_status b_file_swap_shadow(b_file *main_file, b_file *shadow_file)
enum fx_status fx_file_swap_shadow(fx_file *main_file, fx_file *shadow_file)
{
struct b_file_p *main_p = b_object_get_private(main_file, B_TYPE_FILE);
struct b_file_p *shadow_p = b_object_get_private(main_file, B_TYPE_FILE);
struct fx_file_p *main_p = fx_object_get_private(main_file, FX_TYPE_FILE);
struct fx_file_p *shadow_p = fx_object_get_private(main_file, FX_TYPE_FILE);
return file_swap_shadow(main_p, shadow_p);
}
enum b_status b_file_read(
b_file *file, size_t offset, size_t len, void *buf, size_t *nr_read)
enum fx_status fx_file_read(
fx_file *file, size_t offset, size_t len, void *buf, size_t *nr_read)
{
B_CLASS_DISPATCH_STATIC(
B_TYPE_FILE, file_read, file, offset, len, buf, nr_read);
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_FILE, file_read, file, offset, len, buf, nr_read);
}
enum b_status b_file_write(
b_file *file, size_t offset, size_t len, const void *buf, size_t *nr_written)
enum fx_status fx_file_write(
fx_file *file, size_t offset, size_t len, const void *buf, size_t *nr_written)
{
B_CLASS_DISPATCH_STATIC(
B_TYPE_FILE, file_write, file, offset, len, buf, nr_written);
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_FILE, file_write, file, offset, len, buf, nr_written);
}
/*** VIRTUAL FUNCTIONS ********************************************************/
static void file_init(b_object *obj, void *priv)
static void file_init(fx_object *obj, void *priv)
{
struct b_file_p *file = priv;
struct fx_file_p *file = priv;
}
static void file_fini(b_object *obj, void *priv)
static void file_fini(fx_object *obj, void *priv)
{
struct b_file_p *file = priv;
struct fx_file_p *file = priv;
close(file->fd);
if (file->mode & B_FILE_DELETE_ON_CLOSE) {
b_path_unlink(file->path);
if (file->mode & FX_FILE_DELETE_ON_CLOSE) {
fx_path_unlink(file->path);
}
b_path_unref(file->path);
fx_path_unref(file->path);
}
/*** CLASS DEFINITION *********************************************************/
B_TYPE_CLASS_DEFINITION_BEGIN(b_file)
B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT)
B_INTERFACE_ENTRY(to_string) = NULL;
B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT)
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_file)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
B_TYPE_CLASS_INTERFACE_BEGIN(b_stream, B_TYPE_STREAM)
B_INTERFACE_ENTRY(s_close) = stream_close;
B_INTERFACE_ENTRY(s_getc) = stream_getc;
B_INTERFACE_ENTRY(s_read) = stream_read;
B_INTERFACE_ENTRY(s_write) = stream_write;
B_INTERFACE_ENTRY(s_seek) = stream_seek;
B_INTERFACE_ENTRY(s_tell) = stream_tell;
B_TYPE_CLASS_INTERFACE_END(b_stream, B_TYPE_STREAM)
B_TYPE_CLASS_DEFINITION_END(b_file)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_stream, FX_TYPE_STREAM)
FX_INTERFACE_ENTRY(s_close) = stream_close;
FX_INTERFACE_ENTRY(s_getc) = stream_getc;
FX_INTERFACE_ENTRY(s_read) = stream_read;
FX_INTERFACE_ENTRY(s_write) = stream_write;
FX_INTERFACE_ENTRY(s_seek) = stream_seek;
FX_INTERFACE_ENTRY(s_tell) = stream_tell;
FX_TYPE_CLASS_INTERFACE_END(fx_stream, FX_TYPE_STREAM)
FX_TYPE_CLASS_DEFINITION_END(fx_file)
B_TYPE_DEFINITION_BEGIN(b_file)
B_TYPE_ID(0x495a73f6, 0xb8c3, 0x4e17, 0xb5f4, 0x6fc321f67c7b);
B_TYPE_EXTENDS(B_TYPE_STREAM);
B_TYPE_CLASS(b_file_class);
B_TYPE_INSTANCE_PRIVATE(struct b_file_p);
B_TYPE_INSTANCE_INIT(file_init);
B_TYPE_INSTANCE_FINI(file_fini);
B_TYPE_DEFINITION_END(b_file)
FX_TYPE_DEFINITION_BEGIN(fx_file)
FX_TYPE_ID(0x495a73f6, 0xb8c3, 0x4e17, 0xb5f4, 0x6fc321f67c7b);
FX_TYPE_EXTENDS(FX_TYPE_STREAM);
FX_TYPE_CLASS(fx_file_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_file_p);
FX_TYPE_INSTANCE_INIT(file_init);
FX_TYPE_INSTANCE_FINI(file_fini);
FX_TYPE_DEFINITION_END(fx_file)

View File

@@ -1,8 +1,8 @@
#include "misc.h"
#include <blue/core/random.h>
#include <fx/core/random.h>
void z__b_io_generate_tmp_filename(char *out, size_t len)
void z__fx_io_generate_tmp_filename(char *out, size_t len)
{
static const char *alphabet
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
@@ -10,10 +10,10 @@ void z__b_io_generate_tmp_filename(char *out, size_t len)
"89+=-_.";
static const size_t alphabet_len = 67;
b_random_ctx *ctx = b_random_global_ctx();
fx_random_ctx *ctx = fx_random_global_ctx();
for (size_t i = 0; i < len; i++) {
int v = b_random_next_int64(ctx) % alphabet_len;
int v = fx_random_next_int64(ctx) % alphabet_len;
out[i] = alphabet[v];
}

View File

@@ -3,6 +3,6 @@
#include <stddef.h>
extern void z__b_io_generate_tmp_filename(char *out, size_t len);
extern void z__fx_io_generate_tmp_filename(char *out, size_t len);
#endif

View File

@@ -1,8 +1,8 @@
#include "posix.h"
#include <blue/ds/string.h>
#include <blue/io/file.h>
#include <blue/io/path.h>
#include <fx/ds/string.h>
#include <fx/io/file.h>
#include <fx/io/path.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
@@ -13,142 +13,142 @@
/*** PRIVATE DATA *************************************************************/
struct b_path_p {
b_string *p_pathstr;
struct fx_path_p {
fx_string *p_pathstr;
};
/*** PRIVATE FUNCTIONS ********************************************************/
static b_path *path_make_absolute(const struct b_path_p *in)
static fx_path *path_make_absolute(const struct fx_path_p *in)
{
return NULL;
}
static b_path *path_make_relative(const struct b_path_p *in)
static fx_path *path_make_relative(const struct fx_path_p *in)
{
return NULL;
}
static b_path *path_make_canonical(const struct b_path_p *in)
static fx_path *path_make_canonical(const struct fx_path_p *in)
{
return NULL;
}
static bool path_is_absolute(const struct b_path_p *path)
static bool path_is_absolute(const struct fx_path_p *path)
{
const char *s = b_string_ptr(path->p_pathstr);
const char *s = fx_string_ptr(path->p_pathstr);
return s[0] == '/';
}
static const char *path_ptr(const struct b_path_p *path)
static const char *path_ptr(const struct fx_path_p *path)
{
return b_string_ptr(path->p_pathstr);
return fx_string_ptr(path->p_pathstr);
}
static enum b_status path_stat(const struct b_path_p *path, struct b_file_info *out)
static enum fx_status path_stat(const struct fx_path_p *path, struct fx_file_info *out)
{
struct stat st;
int err = stat(path_ptr(path), &st);
if (err != 0) {
return b_status_from_errno(errno, B_ERR_IO_FAILURE);
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
memset(out, 0x0, sizeof *out);
return b_file_info_from_stat(&st, out);
return fx_file_info_from_stat(&st, out);
}
static bool path_exists(const struct b_path_p *path)
static bool path_exists(const struct fx_path_p *path)
{
b_file_info info;
if (!B_OK(path_stat(path, &info))) {
fx_file_info info;
if (!FX_OK(path_stat(path, &info))) {
return false;
}
return true;
}
static bool path_is_file(const struct b_path_p *path)
static bool path_is_file(const struct fx_path_p *path)
{
b_file_info info;
if (!B_OK(path_stat(path, &info))) {
fx_file_info info;
if (!FX_OK(path_stat(path, &info))) {
return false;
}
return (info.attrib & B_FILE_ATTRIB_NORMAL) != 0;
return (info.attrib & FX_FILE_ATTRIB_NORMAL) != 0;
}
static bool path_is_directory(const struct b_path_p *path)
static bool path_is_directory(const struct fx_path_p *path)
{
b_file_info info;
if (!B_OK(path_stat(path, &info))) {
fx_file_info info;
if (!FX_OK(path_stat(path, &info))) {
return false;
}
return (info.attrib & B_FILE_ATTRIB_DIRECTORY) != 0;
return (info.attrib & FX_FILE_ATTRIB_DIRECTORY) != 0;
}
static void append_path(struct b_path_p *dest, const struct b_path_p *src)
static void append_path(struct fx_path_p *dest, const struct fx_path_p *src)
{
if (path_is_absolute(src)) {
b_string_clear(dest->p_pathstr);
b_string_append_s(dest->p_pathstr, src->p_pathstr);
fx_string_clear(dest->p_pathstr);
fx_string_append_s(dest->p_pathstr, src->p_pathstr);
return;
}
if (b_string_get_size(dest->p_pathstr, B_STRLEN_NORMAL) > 0
&& b_string_back(dest->p_pathstr) != '/'
&& b_string_front(src->p_pathstr) != '/') {
if (fx_string_get_size(dest->p_pathstr, FX_STRLEN_NORMAL) > 0
&& fx_string_back(dest->p_pathstr) != '/'
&& fx_string_front(src->p_pathstr) != '/') {
char s[] = {'/', 0};
b_string_append_cstr(dest->p_pathstr, s);
fx_string_append_cstr(dest->p_pathstr, s);
}
b_string_append_s(dest->p_pathstr, src->p_pathstr);
fx_string_append_s(dest->p_pathstr, src->p_pathstr);
}
static enum b_status path_unlink(const struct b_path_p *path)
static enum fx_status path_unlink(const struct fx_path_p *path)
{
int err = remove(b_string_ptr(path->p_pathstr));
return err == 0 ? B_SUCCESS : b_status_from_errno(errno, B_ERR_IO_FAILURE);
int err = remove(fx_string_ptr(path->p_pathstr));
return err == 0 ? FX_SUCCESS : fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
static enum b_status path_get_directory(
const struct b_path_p *path, b_path **out_dir_path)
static enum fx_status path_get_directory(
const struct fx_path_p *path, fx_path **out_dir_path)
{
b_string *path_str = path->p_pathstr;
long len = b_string_get_size(path_str, B_STRLEN_NORMAL);
fx_string *path_str = path->p_pathstr;
long len = fx_string_get_size(path_str, FX_STRLEN_NORMAL);
const char *path_cstr = b_string_ptr(path_str);
const char *path_cstr = fx_string_ptr(path_str);
char *sep = strrchr(path_cstr, '/');
if (!sep) {
*out_dir_path = b_path_create();
return B_SUCCESS;
*out_dir_path = fx_path_create();
return FX_SUCCESS;
}
size_t dir_path_len = (size_t)(sep - path_cstr);
b_string *dir_path_s = b_string_substr(path_str, 0, dir_path_len);
fx_string *dir_path_s = fx_string_substr(path_str, 0, dir_path_len);
b_path *dir_path = b_path_create_from_cstr(b_string_ptr(dir_path_s));
b_string_unref(dir_path_s);
fx_path *dir_path = fx_path_create_from_cstr(fx_string_ptr(dir_path_s));
fx_string_unref(dir_path_s);
*out_dir_path = dir_path;
return B_SUCCESS;
return FX_SUCCESS;
}
static enum b_status path_get_filename(
const struct b_path_p *path, b_string *out_name)
static enum fx_status path_get_filename(
const struct fx_path_p *path, fx_string *out_name)
{
b_string *path_str = path->p_pathstr;
long len = b_string_get_size(path_str, B_STRLEN_NORMAL);
fx_string *path_str = path->p_pathstr;
long len = fx_string_get_size(path_str, FX_STRLEN_NORMAL);
char *sep = strrchr(b_string_ptr(path_str), '/');
char *sep = strrchr(fx_string_ptr(path_str), '/');
if (!sep) {
b_string_append_s(out_name, path_str);
return B_SUCCESS;
fx_string_append_s(out_name, path_str);
return FX_SUCCESS;
}
const char *filename = sep;
@@ -157,43 +157,43 @@ static enum b_status path_get_filename(
}
if (*filename == '\0') {
b_string_clear(out_name);
return B_SUCCESS;
fx_string_clear(out_name);
return FX_SUCCESS;
}
b_string_append_cstr(out_name, filename);
fx_string_append_cstr(out_name, filename);
return B_SUCCESS;
return FX_SUCCESS;
}
static size_t path_length(const struct b_path_p *path)
static size_t path_length(const struct fx_path_p *path)
{
return b_string_get_size(path->p_pathstr, B_STRLEN_NORMAL);
return fx_string_get_size(path->p_pathstr, FX_STRLEN_NORMAL);
}
/*** PUBLIC FUNCTIONS *********************************************************/
b_path *b_path_create_root()
fx_path *fx_path_create_root()
{
const char *system_drive = "/";
size_t system_drive_len = strlen(system_drive);
b_path *path = b_path_create();
fx_path *path = fx_path_create();
if (!path) {
return NULL;
}
struct b_path_p *p = b_object_get_private(path, B_TYPE_PATH);
struct fx_path_p *p = fx_object_get_private(path, FX_TYPE_PATH);
b_string_append_cstr(p->p_pathstr, system_drive);
fx_string_append_cstr(p->p_pathstr, system_drive);
if (system_drive[system_drive_len - 1] != '\\') {
b_string_append_c(p->p_pathstr, '\\');
fx_string_append_c(p->p_pathstr, '\\');
}
return path;
}
b_path *b_path_create_cwd()
fx_path *fx_path_create_cwd()
{
const long buf_len = 2048;
char *buf = malloc(buf_len);
@@ -206,28 +206,28 @@ b_path *b_path_create_cwd()
return NULL;
}
b_path *path = b_path_create();
fx_path *path = fx_path_create();
if (!path) {
free(buf);
return NULL;
}
struct b_path_p *p = b_object_get_private(path, B_TYPE_PATH);
struct fx_path_p *p = fx_object_get_private(path, FX_TYPE_PATH);
b_string_append_cstr(p->p_pathstr, buf);
fx_string_append_cstr(p->p_pathstr, buf);
free(buf);
return path;
}
b_path *b_path_create_from_cstr(const char *cstr)
fx_path *fx_path_create_from_cstr(const char *cstr)
{
b_path *path = b_path_create();
fx_path *path = fx_path_create();
if (!path) {
return NULL;
}
struct b_path_p *p = b_object_get_private(path, B_TYPE_PATH);
struct fx_path_p *p = fx_object_get_private(path, FX_TYPE_PATH);
char prev = 0;
@@ -241,49 +241,49 @@ b_path *b_path_create_from_cstr(const char *cstr)
continue;
}
b_string_append_c(p->p_pathstr, c);
fx_string_append_c(p->p_pathstr, c);
prev = c;
}
while (b_string_back(p->p_pathstr) == '/') {
b_string_pop_back(p->p_pathstr);
while (fx_string_back(p->p_pathstr) == '/') {
fx_string_pop_back(p->p_pathstr);
}
return path;
}
b_path *b_path_duplicate(const b_path *path)
fx_path *fx_path_duplicate(const fx_path *path)
{
b_path *new_path = b_path_create();
fx_path *new_path = fx_path_create();
if (!path) {
return NULL;
}
struct b_path_p *old_p = b_object_get_private(path, B_TYPE_PATH);
struct b_path_p *new_p = b_object_get_private(new_path, B_TYPE_PATH);
struct fx_path_p *old_p = fx_object_get_private(path, FX_TYPE_PATH);
struct fx_path_p *new_p = fx_object_get_private(new_path, FX_TYPE_PATH);
new_p->p_pathstr = b_string_duplicate(old_p->p_pathstr);
new_p->p_pathstr = fx_string_duplicate(old_p->p_pathstr);
if (!new_p->p_pathstr) {
b_path_unref(new_path);
fx_path_unref(new_path);
return NULL;
}
return new_path;
}
b_path *b_path_join(const b_path *paths[], size_t nr_paths)
fx_path *fx_path_join(const fx_path *paths[], size_t nr_paths)
{
b_path *result = b_path_create();
fx_path *result = fx_path_create();
if (!result) {
return NULL;
}
struct b_path_p *result_p = b_object_get_private(result, B_TYPE_PATH);
struct fx_path_p *result_p = fx_object_get_private(result, FX_TYPE_PATH);
for (size_t i = 0; i < nr_paths; i++) {
if (paths[i]) {
struct b_path_p *path_p
= b_object_get_private(paths[i], B_TYPE_PATH);
struct fx_path_p *path_p
= fx_object_get_private(paths[i], FX_TYPE_PATH);
append_path(result_p, path_p);
}
}
@@ -291,109 +291,109 @@ b_path *b_path_join(const b_path *paths[], size_t nr_paths)
return result;
}
b_path *b_path_make_absolute(const b_path *in)
fx_path *fx_path_make_absolute(const fx_path *in)
{
B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_make_absolute, in);
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_make_absolute, in);
}
b_path *b_path_make_relative(const b_path *in)
fx_path *fx_path_make_relative(const fx_path *in)
{
B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_make_relative, in);
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_make_relative, in);
}
b_path *b_path_make_canonical(const b_path *in)
fx_path *fx_path_make_canonical(const fx_path *in)
{
B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_make_canonical, in);
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_make_canonical, in);
}
bool b_path_is_absolute(const b_path *path)
bool fx_path_is_absolute(const fx_path *path)
{
B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_is_absolute, path);
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_is_absolute, path);
}
bool b_path_exists(const b_path *path)
bool fx_path_exists(const fx_path *path)
{
B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_exists, path);
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_exists, path);
}
bool b_path_is_file(const b_path *path)
bool fx_path_is_file(const fx_path *path)
{
B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_is_file, path);
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_is_file, path);
}
bool b_path_is_directory(const b_path *path)
bool fx_path_is_directory(const fx_path *path)
{
B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_is_directory, path);
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_is_directory, path);
}
enum b_status b_path_stat(const b_path *path, struct b_file_info *out)
enum fx_status fx_path_stat(const fx_path *path, struct fx_file_info *out)
{
B_CLASS_DISPATCH_STATIC(B_TYPE_PATH, path_stat, path, out);
FX_CLASS_DISPATCH_STATIC(FX_TYPE_PATH, path_stat, path, out);
}
enum b_status b_path_unlink(const b_path *path)
enum fx_status fx_path_unlink(const fx_path *path)
{
B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_unlink, path);
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_unlink, path);
}
enum b_status b_path_get_directory(const b_path *path, b_path **out_dir_path)
enum fx_status fx_path_get_directory(const fx_path *path, fx_path **out_dir_path)
{
B_CLASS_DISPATCH_STATIC(B_TYPE_PATH, path_get_directory, path, out_dir_path);
FX_CLASS_DISPATCH_STATIC(FX_TYPE_PATH, path_get_directory, path, out_dir_path);
}
enum b_status b_path_get_filename(const b_path *path, b_string *out_name)
enum fx_status fx_path_get_filename(const fx_path *path, fx_string *out_name)
{
B_CLASS_DISPATCH_STATIC(B_TYPE_PATH, path_get_filename, path, out_name);
FX_CLASS_DISPATCH_STATIC(FX_TYPE_PATH, path_get_filename, path, out_name);
}
const char *b_path_ptr(const b_path *path)
const char *fx_path_ptr(const fx_path *path)
{
B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_ptr, path);
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_ptr, path);
}
size_t b_path_length(const b_path *path)
size_t fx_path_length(const fx_path *path)
{
B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_length, path);
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_length, path);
}
/*** VIRTUAL FUNCTIONS ********************************************************/
static void path_init(b_object *obj, void *priv)
static void path_init(fx_object *obj, void *priv)
{
struct b_path_p *path = priv;
struct fx_path_p *path = priv;
path->p_pathstr = b_string_create();
path->p_pathstr = fx_string_create();
if (!path->p_pathstr) {
/* TODO return error */
}
}
void path_fini(b_object *obj, void *priv)
void path_fini(fx_object *obj, void *priv)
{
struct b_path_p *path = priv;
struct fx_path_p *path = priv;
b_string_unref(path->p_pathstr);
fx_string_unref(path->p_pathstr);
}
void path_to_string(const b_object *obj, b_stream *out)
void path_to_string(const fx_object *obj, fx_stream *out)
{
struct b_path_p *path = b_object_get_private(obj, B_TYPE_PATH);
struct fx_path_p *path = fx_object_get_private(obj, FX_TYPE_PATH);
b_stream_write_string(out, b_string_ptr(path->p_pathstr), NULL);
fx_stream_write_string(out, fx_string_ptr(path->p_pathstr), NULL);
}
/*** CLASS DEFINITION *********************************************************/
B_TYPE_CLASS_DEFINITION_BEGIN(b_path)
B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT)
B_INTERFACE_ENTRY(to_string) = path_to_string;
B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT)
B_TYPE_CLASS_DEFINITION_END(b_path)
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_path)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = path_to_string;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_DEFINITION_END(fx_path)
B_TYPE_DEFINITION_BEGIN(b_path)
B_TYPE_ID(0x56dc32eb, 0xea96, 0x46ed, 0x85d3, 0x760fa4ad61f4);
B_TYPE_CLASS(b_path_class);
B_TYPE_INSTANCE_PRIVATE(struct b_path_p);
B_TYPE_INSTANCE_INIT(path_init);
B_TYPE_INSTANCE_FINI(path_fini);
B_TYPE_DEFINITION_END(b_path)
FX_TYPE_DEFINITION_BEGIN(fx_path)
FX_TYPE_ID(0x56dc32eb, 0xea96, 0x46ed, 0x85d3, 0x760fa4ad61f4);
FX_TYPE_CLASS(fx_path_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_path_p);
FX_TYPE_INSTANCE_INIT(path_init);
FX_TYPE_INSTANCE_FINI(path_fini);
FX_TYPE_DEFINITION_END(fx_path)

View File

@@ -1,131 +1,131 @@
#include <blue/core/error.h>
#include <blue/core/status.h>
#include <blue/io/file.h>
#include <fx/core/error.h>
#include <fx/core/status.h>
#include <fx/io/file.h>
#include <errno.h>
#include <sys/stat.h>
enum b_status b_status_from_errno(int error, enum b_status default_value)
enum fx_status fx_status_from_errno(int error, enum fx_status default_value)
{
switch (error) {
case 0:
return B_SUCCESS;
return FX_SUCCESS;
case ENOENT:
return B_ERR_NO_ENTRY;
return FX_ERR_NO_ENTRY;
case EEXIST:
return B_ERR_NAME_EXISTS;
return FX_ERR_NAME_EXISTS;
case ENOMEM:
return B_ERR_NO_MEMORY;
return FX_ERR_NO_MEMORY;
case EINVAL:
return B_ERR_INVALID_ARGUMENT;
return FX_ERR_INVALID_ARGUMENT;
case EIO:
return B_ERR_IO_FAILURE;
return FX_ERR_IO_FAILURE;
case EISDIR:
return B_ERR_IS_DIRECTORY;
return FX_ERR_IS_DIRECTORY;
case ENOTDIR:
return B_ERR_NOT_DIRECTORY;
return FX_ERR_NOT_DIRECTORY;
case EPERM:
case EACCES:
return B_ERR_PERMISSION_DENIED;
return FX_ERR_PERMISSION_DENIED;
case ENOTSUP:
case ENOSYS:
return B_ERR_NOT_SUPPORTED;
return FX_ERR_NOT_SUPPORTED;
default:
return default_value;
}
}
b_result b_result_from_errno_with_filepath(
int error, const char *path, enum b_status default_value)
fx_result fx_result_from_errno_with_filepath(
int error, const char *path, enum fx_status default_value)
{
switch (error) {
case 0:
return B_RESULT_SUCCESS;
return FX_RESULT_SUCCESS;
case ENOENT:
return B_RESULT_STATUS_WITH_STRING(
B_ERR_NO_ENTRY, "Path @i{%s} does not exist", path);
return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_NO_ENTRY, "Path @i{%s} does not exist", path);
case ENOTDIR:
return B_RESULT_STATUS_WITH_STRING(
B_ERR_NOT_DIRECTORY, "Path @i{%s} is not a directory",
return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_NOT_DIRECTORY, "Path @i{%s} is not a directory",
path);
case EISDIR:
return B_RESULT_STATUS_WITH_STRING(
B_ERR_IS_DIRECTORY, "Path @i{%s} is a directory", path);
return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_IS_DIRECTORY, "Path @i{%s} is a directory", path);
case EPERM:
case EACCES:
return B_RESULT_STATUS_WITH_STRING(
B_ERR_PERMISSION_DENIED,
return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_PERMISSION_DENIED,
"Permission denied while accessing path @i{%s}", path);
default:
return B_RESULT_STATUS(b_status_from_errno(error, default_value));
return FX_RESULT_STATUS(fx_status_from_errno(error, default_value));
}
return B_RESULT_SUCCESS;
return FX_RESULT_SUCCESS;
}
b_result b_result_from_errno_with_subfilepath(
fx_result fx_result_from_errno_with_subfilepath(
int error, const char *path, const char *dir_path,
enum b_status default_value)
enum fx_status default_value)
{
if (!dir_path) {
return b_result_propagate(b_result_from_errno_with_filepath(
return fx_result_propagate(fx_result_from_errno_with_filepath(
error, path, default_value));
}
switch (error) {
case 0:
return B_RESULT_SUCCESS;
return FX_RESULT_SUCCESS;
case ENOENT:
return B_RESULT_STATUS_WITH_STRING(
B_ERR_NO_ENTRY,
return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_NO_ENTRY,
"Path @i{%s} in directory @i{%s} does not exist", path,
dir_path);
case ENOTDIR:
return B_RESULT_STATUS_WITH_STRING(
B_ERR_NOT_DIRECTORY,
return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_NOT_DIRECTORY,
"Path @i{%s} in directory @i{%s} is not a directory",
path, dir_path);
case EISDIR:
return B_RESULT_STATUS_WITH_STRING(
B_ERR_IS_DIRECTORY,
return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_IS_DIRECTORY,
"Path @i{%s} in directory @i{%s} is a directory", path,
dir_path);
case EPERM:
case EACCES:
return B_RESULT_STATUS_WITH_STRING(
B_ERR_PERMISSION_DENIED,
return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_PERMISSION_DENIED,
"Permission denied while accessing path @i{%s} in "
"directory @i{%s}",
path, dir_path);
default:
return B_RESULT_STATUS(b_status_from_errno(error, default_value));
return FX_RESULT_STATUS(fx_status_from_errno(error, default_value));
}
return B_RESULT_SUCCESS;
return FX_RESULT_SUCCESS;
}
enum b_status b_file_info_from_stat(const struct stat *st, struct b_file_info *out)
enum fx_status fx_file_info_from_stat(const struct stat *st, struct fx_file_info *out)
{
out->length = st->st_size;
if (S_ISREG(st->st_mode)) {
out->attrib |= B_FILE_ATTRIB_NORMAL;
out->attrib |= FX_FILE_ATTRIB_NORMAL;
}
if (S_ISDIR(st->st_mode)) {
out->attrib |= B_FILE_ATTRIB_DIRECTORY;
out->attrib |= FX_FILE_ATTRIB_DIRECTORY;
}
if (S_ISBLK(st->st_mode)) {
out->attrib |= B_FILE_ATTRIB_BLOCK_DEVICE;
out->attrib |= FX_FILE_ATTRIB_BLOCK_DEVICE;
}
if (S_ISCHR(st->st_mode)) {
out->attrib |= B_FILE_ATTRIB_CHAR_DEVICE;
out->attrib |= FX_FILE_ATTRIB_CHAR_DEVICE;
}
if (S_ISLNK(st->st_mode)) {
out->attrib |= B_FILE_ATTRIB_SYMLINK;
out->attrib |= FX_FILE_ATTRIB_SYMLINK;
}
return B_SUCCESS;
return FX_SUCCESS;
}

View File

@@ -1,19 +1,19 @@
#ifndef _IO_DARWIN_POSIX_H_
#define _IO_DARWIN_POSIX_H_
#include <blue/core/error.h>
#include <blue/core/status.h>
#include <fx/core/error.h>
#include <fx/core/status.h>
struct stat;
struct b_file_info;
struct fx_file_info;
extern enum b_status b_status_from_errno(int error, enum b_status default_value);
extern b_result b_result_from_errno_with_filepath(
int error, const char *path, enum b_status default_value);
extern b_result b_result_from_errno_with_subfilepath(
extern enum fx_status fx_status_from_errno(int error, enum fx_status default_value);
extern fx_result fx_result_from_errno_with_filepath(
int error, const char *path, enum fx_status default_value);
extern fx_result fx_result_from_errno_with_subfilepath(
int error, const char *path, const char *dir_path,
enum b_status default_value);
extern enum b_status b_file_info_from_stat(
const struct stat *in, struct b_file_info *out);
enum fx_status default_value);
extern enum fx_status fx_file_info_from_stat(
const struct stat *in, struct fx_file_info *out);
#endif

View File

@@ -1,99 +1,99 @@
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <blue/io/directory.h>
#include <fx/io/directory.h>
#include <stdlib.h>
#include <string.h>
struct b_directory {
struct b_dsref base;
struct fx_directory {
struct fx_dsref base;
HANDLE handle;
struct b_path *abs_path;
struct fx_path *abs_path;
};
struct z__b_directory_iterator {
b_queue state_stack;
struct z__fx_directory_iterator {
fx_queue state_stack;
};
struct iteration_state {
const b_path *search_path;
b_queue_entry entry;
const fx_path *search_path;
fx_queue_entry entry;
HANDLE search;
WIN32_FIND_DATAA data;
bool child_search_complete;
};
static void directory_release(struct b_dsref *obj);
static void directory_release(struct fx_dsref *obj);
static struct b_dsref_type directory_type = {
static struct fx_dsref_type directory_type = {
.t_name = "corelib::directory",
.t_flags = B_DSREF_FUNDAMENTAL,
.t_id = B_DSREF_TYPE_PATH,
.t_instance_size = sizeof(struct b_directory),
.t_flags = FX_DSREF_FUNDAMENTAL,
.t_id = FX_DSREF_TYPE_PATH,
.t_instance_size = sizeof(struct fx_directory),
.t_release = directory_release,
};
static enum b_status status_from_win32_error(int error, enum b_status default_value)
static enum fx_status status_from_win32_error(int error, enum fx_status default_value)
{
switch (error) {
case ERROR_FILE_NOT_FOUND:
return B_ERR_NO_ENTRY;
return FX_ERR_NO_ENTRY;
default:
return default_value;
}
}
enum b_status b_directory_open(
struct b_directory *root, const struct b_path *path,
struct b_directory **out)
enum fx_status fx_directory_open(
struct fx_directory *root, const struct fx_path *path,
struct fx_directory **out)
{
enum b_status status = B_SUCCESS;
enum fx_status status = FX_SUCCESS;
const b_path *parts[] = {
const fx_path *parts[] = {
root ? root->abs_path : NULL,
path,
};
b_path *new_path = b_path_join(parts, sizeof parts / sizeof *parts);
fx_path *new_path = fx_path_join(parts, sizeof parts / sizeof *parts);
if (!new_path) {
return B_ERR_NO_MEMORY;
return FX_ERR_NO_MEMORY;
}
HANDLE dir_handle = CreateFileA(
b_path_ptr(new_path), GENERIC_READ,
fx_path_ptr(new_path), GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, INVALID_HANDLE_VALUE);
if (dir_handle == INVALID_HANDLE_VALUE) {
status = status_from_win32_error(GetLastError(), B_ERR_IO_FAILURE);
status = status_from_win32_error(GetLastError(), FX_ERR_IO_FAILURE);
b_path_release(new_path);
fx_path_release(new_path);
return status;
}
BY_HANDLE_FILE_INFORMATION dir_info;
if (!GetFileInformationByHandle(dir_handle, &dir_info)) {
status = status_from_win32_error(GetLastError(), B_ERR_IO_FAILURE);
status = status_from_win32_error(GetLastError(), FX_ERR_IO_FAILURE);
CloseHandle(dir_handle);
b_path_release(new_path);
fx_path_release(new_path);
return status;
}
if (!(dir_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
CloseHandle(dir_handle);
b_path_release(new_path);
return B_ERR_NOT_DIRECTORY;
fx_path_release(new_path);
return FX_ERR_NOT_DIRECTORY;
}
struct b_directory *dir
= (struct b_directory *)b_dsref_type_instantiate(&directory_type);
struct fx_directory *dir
= (struct fx_directory *)fx_dsref_type_instantiate(&directory_type);
if (!path) {
CloseHandle(dir_handle);
b_path_release(new_path);
return B_ERR_NO_MEMORY;
fx_path_release(new_path);
return FX_ERR_NO_MEMORY;
}
dir->abs_path = new_path;
@@ -101,23 +101,23 @@ enum b_status b_directory_open(
*out = dir;
return B_SUCCESS;
return FX_SUCCESS;
}
static struct iteration_state *get_iteration_state(
struct z__b_directory_iterator *it)
struct z__fx_directory_iterator *it)
{
b_queue_entry *last = b_queue_last(&it->state_stack);
fx_queue_entry *last = fx_queue_last(&it->state_stack);
if (!last) {
return NULL;
}
return b_unbox(struct iteration_state, last, entry);
return fx_unbox(struct iteration_state, last, entry);
}
static struct iteration_state *push_iteration_state(
struct z__b_directory_iterator *it)
struct z__fx_directory_iterator *it)
{
struct iteration_state *state = malloc(sizeof *state);
if (!state) {
@@ -126,11 +126,11 @@ static struct iteration_state *push_iteration_state(
memset(state, 0x0, sizeof *state);
b_queue_push_back(&it->state_stack, &state->entry);
fx_queue_push_back(&it->state_stack, &state->entry);
return state;
}
static void pop_iteration_state(struct z__b_directory_iterator *it)
static void pop_iteration_state(struct z__fx_directory_iterator *it)
{
struct iteration_state *state = get_iteration_state(it);
@@ -138,14 +138,14 @@ static void pop_iteration_state(struct z__b_directory_iterator *it)
return;
}
b_queue_pop_back(&it->state_stack);
fx_queue_pop_back(&it->state_stack);
FindClose(state->search);
free(state);
}
static void cleanup_iterator(struct b_directory_iterator *it)
static void cleanup_iterator(struct fx_directory_iterator *it)
{
while (!b_queue_empty(&it->_z->state_stack)) {
while (!fx_queue_empty(&it->_z->state_stack)) {
pop_iteration_state(it->_z);
}
@@ -153,10 +153,10 @@ static void cleanup_iterator(struct b_directory_iterator *it)
it->_z = NULL;
}
static void update_iterator_data(struct b_directory_iterator *it)
static void update_iterator_data(struct fx_directory_iterator *it)
{
if (it->filepath) {
b_path_release(B_PATH(it->filepath));
fx_path_release(FX_PATH(it->filepath));
it->filepath = NULL;
}
@@ -164,44 +164,44 @@ static void update_iterator_data(struct b_directory_iterator *it)
if (state) {
it->filename = state->data.cFileName;
struct b_path *filename = b_path_create_from_cstr(it->filename);
struct fx_path *filename = fx_path_create_from_cstr(it->filename);
const struct b_path *parts[] = {
const struct fx_path *parts[] = {
state->search_path,
filename,
};
it->filepath = b_path_join(parts, sizeof parts / sizeof parts[0]);
it->filepath = fx_path_join(parts, sizeof parts / sizeof parts[0]);
}
}
static bool move_into_directory(struct b_directory_iterator *it, const char *dir_name)
static bool move_into_directory(struct fx_directory_iterator *it, const char *dir_name)
{
struct iteration_state *state = get_iteration_state(it->_z);
struct b_path *dir_name_p = b_path_create_from_cstr(dir_name);
struct b_path *wildcard = b_path_create_from_cstr("*");
struct fx_path *dir_name_p = fx_path_create_from_cstr(dir_name);
struct fx_path *wildcard = fx_path_create_from_cstr("*");
const struct b_path *parts[] = {
const struct fx_path *parts[] = {
state ? state->search_path : NULL,
dir_name_p,
};
struct b_path *dir_path = b_path_join(parts, sizeof parts / sizeof *parts);
struct fx_path *dir_path = fx_path_join(parts, sizeof parts / sizeof *parts);
parts[0] = dir_path;
parts[1] = wildcard;
struct b_path *search_path
= b_path_join(parts, sizeof parts / sizeof *parts);
struct fx_path *search_path
= fx_path_join(parts, sizeof parts / sizeof *parts);
state = push_iteration_state(it->_z);
state->search_path = dir_path;
state->search = FindFirstFileA(b_path_ptr(search_path), &state->data);
state->search = FindFirstFileA(fx_path_ptr(search_path), &state->data);
b_path_release(search_path);
b_path_release(wildcard);
b_path_release(dir_name_p);
fx_path_release(search_path);
fx_path_release(wildcard);
fx_path_release(dir_name_p);
if (state->search == INVALID_HANDLE_VALUE) {
pop_iteration_state(it->_z);
@@ -222,15 +222,15 @@ static bool move_into_directory(struct b_directory_iterator *it, const char *dir
}
static bool move_to_first_item(
struct b_directory_iterator *it, const struct b_path *root_dir)
struct fx_directory_iterator *it, const struct fx_path *root_dir)
{
bool has_results = move_into_directory(it, b_path_ptr(root_dir));
bool has_results = move_into_directory(it, fx_path_ptr(root_dir));
if (!has_results) {
return false;
}
if (it->flags & B_DIRECTORY_ITERATE_PARENT_FIRST) {
if (it->flags & FX_DIRECTORY_ITERATE_PARENT_FIRST) {
return true;
}
@@ -252,14 +252,14 @@ static bool move_to_first_item(
return true;
}
static bool move_to_next_item(struct b_directory_iterator *it)
static bool move_to_next_item(struct fx_directory_iterator *it)
{
while (true) {
struct iteration_state *state = get_iteration_state(it->_z);
if (!state->child_search_complete
&& (state->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
&& (it->flags & B_DIRECTORY_ITERATE_PARENT_FIRST)) {
&& (it->flags & FX_DIRECTORY_ITERATE_PARENT_FIRST)) {
if (move_into_directory(it, state->data.cFileName)) {
return true;
}
@@ -271,7 +271,7 @@ static bool move_to_next_item(struct b_directory_iterator *it)
pop_iteration_state(it->_z);
state = get_iteration_state(it->_z);
if (it->flags & B_DIRECTORY_ITERATE_PARENT_FIRST
if (it->flags & FX_DIRECTORY_ITERATE_PARENT_FIRST
&& state != NULL) {
state->child_search_complete = true;
continue;
@@ -286,7 +286,7 @@ static bool move_to_next_item(struct b_directory_iterator *it)
return true;
}
if (it->flags & B_DIRECTORY_ITERATE_PARENT_FIRST) {
if (it->flags & FX_DIRECTORY_ITERATE_PARENT_FIRST) {
return true;
}
@@ -311,17 +311,17 @@ static bool move_to_next_item(struct b_directory_iterator *it)
return true;
}
int b_directory_iterator_begin(
struct b_directory *directory, struct b_directory_iterator *it,
enum b_directory_iterator_flags flags)
int fx_directory_iterator_begin(
struct fx_directory *directory, struct fx_directory_iterator *it,
enum fx_directory_iterator_flags flags)
{
if (b_directory_iterator_is_valid(it)) {
if (fx_directory_iterator_is_valid(it)) {
cleanup_iterator(it);
}
it->flags = flags;
struct z__b_directory_iterator *it_data = malloc(sizeof *it_data);
struct z__fx_directory_iterator *it_data = malloc(sizeof *it_data);
memset(it_data, 0x0, sizeof *it_data);
it->_z = it_data;
@@ -332,7 +332,7 @@ int b_directory_iterator_begin(
return 0;
}
bool b_directory_iterator_next(struct b_directory_iterator *it)
bool fx_directory_iterator_next(struct fx_directory_iterator *it)
{
if (!it->_z) {
return false;
@@ -343,12 +343,12 @@ bool b_directory_iterator_next(struct b_directory_iterator *it)
return result;
}
enum b_status b_directory_iterator_erase(struct b_directory_iterator *it)
enum fx_status fx_directory_iterator_erase(struct fx_directory_iterator *it)
{
return B_SUCCESS;
return FX_SUCCESS;
}
bool b_directory_iterator_is_valid(const struct b_directory_iterator *it)
bool fx_directory_iterator_is_valid(const struct fx_directory_iterator *it)
{
if (!it->_z) {
return false;
@@ -361,6 +361,6 @@ bool b_directory_iterator_is_valid(const struct b_directory_iterator *it)
return true;
}
static void directory_release(struct b_dsref *obj)
static void directory_release(struct fx_dsref *obj)
{
}

View File

@@ -1,48 +1,48 @@
#define WIN32_LEAN_AND_MEAN
#include <blue/core/stringstream.h>
#include <blue/io/path.h>
#include <blue/ds/string.h>
#include <fx/core/stringstream.h>
#include <fx/io/path.h>
#include <fx/ds/string.h>
#include <Windows.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct b_path {
struct b_dsref base;
struct b_string *pathstr;
struct fx_path {
struct fx_dsref base;
struct fx_string *pathstr;
};
static void path_release(struct b_dsref *obj);
static void path_to_string(struct b_dsref *obj, struct b_stringstream *out);
static void path_release(struct fx_dsref *obj);
static void path_to_string(struct fx_dsref *obj, struct fx_stringstream *out);
static struct b_dsref_type path_type = {
static struct fx_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_flags = FX_DSREF_FUNDAMENTAL,
.t_id = FX_DSREF_TYPE_PATH,
.t_instance_size = sizeof(struct fx_path),
.t_release = path_release,
.t_to_string = path_to_string,
};
struct b_path *b_path_create()
struct fx_path *fx_path_create()
{
struct b_path *path
= (struct b_path *)b_dsref_type_instantiate(&path_type);
struct fx_path *path
= (struct fx_path *)fx_dsref_type_instantiate(&path_type);
if (!path) {
return NULL;
}
path->pathstr = b_string_create();
path->pathstr = fx_string_create();
if (!path->pathstr) {
b_path_release(path);
fx_path_release(path);
return NULL;
}
return path;
}
struct b_path *b_path_create_root()
struct fx_path *fx_path_create_root()
{
const char *system_drive = getenv("SystemDrive");
if (!system_drive) {
@@ -52,20 +52,20 @@ struct b_path *b_path_create_root()
size_t system_drive_len = strlen(system_drive);
struct b_path *path = b_path_create();
struct fx_path *path = fx_path_create();
if (!path) {
return NULL;
}
b_string_append_cstr(path->pathstr, system_drive);
fx_string_append_cstr(path->pathstr, system_drive);
if (system_drive[system_drive_len - 1] != '\\') {
b_string_append_cstr(path->pathstr, "\\");
fx_string_append_cstr(path->pathstr, "\\");
}
return path;
}
struct b_path *b_path_create_cwd()
struct fx_path *fx_path_create_cwd()
{
DWORD cwd_len = GetCurrentDirectory(0, NULL);
if (cwd_len == 0) {
@@ -83,7 +83,7 @@ struct b_path *b_path_create_cwd()
return NULL;
}
struct b_path *path = b_path_create();
struct fx_path *path = fx_path_create();
if (!path) {
return NULL;
}
@@ -91,16 +91,16 @@ struct b_path *b_path_create_cwd()
for (DWORD i = 0; i < cwd_len; i++) {
TCHAR c = cwd_buf[i];
char s[] = {(c >= 128 ? '?' : (char)c), 0};
b_string_append_cstr(path->pathstr, s);
fx_string_append_cstr(path->pathstr, s);
}
free(cwd_buf);
return path;
}
struct b_path *b_path_create_from_cstr(const char *cstr)
struct fx_path *fx_path_create_from_cstr(const char *cstr)
{
struct b_path *path = b_path_create();
struct fx_path *path = fx_path_create();
if (!path) {
return NULL;
}
@@ -119,38 +119,38 @@ struct b_path *b_path_create_from_cstr(const char *cstr)
char s[] = {c, 0};
b_string_append_cstr(path->pathstr, s);
fx_string_append_cstr(path->pathstr, s);
prev = c;
}
while (b_string_back(path->pathstr) == '/') {
b_string_pop_back(path->pathstr);
while (fx_string_back(path->pathstr) == '/') {
fx_string_pop_back(path->pathstr);
}
return path;
}
static void append_path(struct b_path *dest, const struct b_path *src)
static void append_path(struct fx_path *dest, const struct fx_path *src)
{
if (b_path_is_absolute(src)) {
b_string_clear(dest->pathstr);
b_string_append_s(dest->pathstr, src->pathstr);
if (fx_path_is_absolute(src)) {
fx_string_clear(dest->pathstr);
fx_string_append_s(dest->pathstr, src->pathstr);
return;
}
if (b_string_back(dest->pathstr) != '/'
&& b_string_front(src->pathstr) != '/') {
if (fx_string_back(dest->pathstr) != '/'
&& fx_string_front(src->pathstr) != '/') {
char s[] = {'/', 0};
b_string_append_cstr(dest->pathstr, s);
fx_string_append_cstr(dest->pathstr, s);
}
b_string_append_s(dest->pathstr, src->pathstr);
fx_string_append_s(dest->pathstr, src->pathstr);
}
struct b_path *b_path_join(
const b_path *paths[], size_t nr_paths)
struct fx_path *fx_path_join(
const fx_path *paths[], size_t nr_paths)
{
struct b_path *result = b_path_create();
struct fx_path *result = fx_path_create();
if (!result) {
return NULL;
}
@@ -164,24 +164,24 @@ struct b_path *b_path_join(
return result;
}
struct b_path *b_path_make_absolute(const struct b_path *in)
struct fx_path *fx_path_make_absolute(const struct fx_path *in)
{
return NULL;
}
struct b_path *b_path_make_relative(const struct b_path *in)
struct fx_path *fx_path_make_relative(const struct fx_path *in)
{
return NULL;
}
struct b_path *b_path_make_canonical(const struct b_path *in)
struct fx_path *fx_path_make_canonical(const struct fx_path *in)
{
return NULL;
}
bool b_path_is_absolute(const struct b_path *path)
bool fx_path_is_absolute(const struct fx_path *path)
{
const char *s = b_string_ptr(path->pathstr);
const char *s = fx_string_ptr(path->pathstr);
size_t i;
for (i = 0; s[i]; i++) {
@@ -203,15 +203,15 @@ bool b_path_is_absolute(const struct b_path *path)
return true;
}
bool b_path_exists(const struct b_path *path)
bool fx_path_exists(const struct fx_path *path)
{
DWORD attrib = GetFileAttributesA(b_path_ptr(path));
DWORD attrib = GetFileAttributesA(fx_path_ptr(path));
return attrib != INVALID_FILE_ATTRIBUTES;
}
bool b_path_is_file(const struct b_path *path)
bool fx_path_is_file(const struct fx_path *path)
{
DWORD attrib = GetFileAttributesA(b_path_ptr(path));
DWORD attrib = GetFileAttributesA(fx_path_ptr(path));
if (attrib == INVALID_FILE_ATTRIBUTES) {
return false;
}
@@ -219,9 +219,9 @@ bool b_path_is_file(const struct b_path *path)
return (attrib & FILE_ATTRIBUTE_NORMAL) != 0;
}
bool b_path_is_directory(const struct b_path *path)
bool fx_path_is_directory(const struct fx_path *path)
{
DWORD attrib = GetFileAttributesA(b_path_ptr(path));
DWORD attrib = GetFileAttributesA(fx_path_ptr(path));
if (attrib == INVALID_FILE_ATTRIBUTES) {
return false;
}
@@ -229,20 +229,20 @@ bool b_path_is_directory(const struct b_path *path)
return (attrib & FILE_ATTRIBUTE_DIRECTORY) != 0;
}
const char *b_path_ptr(const struct b_path *path)
const char *fx_path_ptr(const struct fx_path *path)
{
return b_string_ptr(path->pathstr);
return fx_string_ptr(path->pathstr);
}
void path_release(struct b_dsref* obj)
void path_release(struct fx_dsref* obj)
{
struct b_path *path = B_PATH(obj);
b_string_release(path->pathstr);
struct fx_path *path = FX_PATH(obj);
fx_string_release(path->pathstr);
}
void path_to_string(struct b_dsref* obj, struct b_stringstream* out)
void path_to_string(struct fx_dsref* obj, struct fx_stringstream* out)
{
struct b_path *path = (struct b_path *)obj;
struct fx_path *path = (struct fx_path *)obj;
b_stringstream_add(out, b_string_ptr(path->pathstr));
fx_stringstream_add(out, fx_string_ptr(path->pathstr));
}