fx.io: add basic rosetta support
This commit is contained in:
@@ -0,0 +1,486 @@
|
||||
#include "posix.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <fx/io/file.h>
|
||||
#include <fx/io/path.h>
|
||||
#include <fx/string.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/*** PRIVATE DATA *************************************************************/
|
||||
|
||||
struct fx_path_p {
|
||||
fx_string *p_pathstr;
|
||||
};
|
||||
|
||||
/*** PRIVATE FUNCTIONS ********************************************************/
|
||||
|
||||
static fx_path *path_make_absolute(const struct fx_path_p *in)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static fx_path *path_make_relative(const struct fx_path_p *in)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static fx_path *path_make_canonical(const struct fx_path_p *in)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static bool path_is_absolute(const struct fx_path_p *path)
|
||||
{
|
||||
const char *s = fx_string_get_cstr(path->p_pathstr);
|
||||
return s[0] == '/';
|
||||
}
|
||||
|
||||
static const char *path_ptr(const struct fx_path_p *path)
|
||||
{
|
||||
return fx_string_get_cstr(path->p_pathstr);
|
||||
}
|
||||
|
||||
static enum fx_status path_stat(
|
||||
const struct fx_path_p *path,
|
||||
struct fx_file_info *out)
|
||||
{
|
||||
#if 0
|
||||
struct stat st;
|
||||
int err = stat(path_ptr(path), &st);
|
||||
|
||||
if (err != 0) {
|
||||
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
|
||||
}
|
||||
|
||||
memset(out, 0x0, sizeof *out);
|
||||
|
||||
return fx_file_info_from_stat(&st, out);
|
||||
#endif
|
||||
return FX_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
static bool path_exists(const struct fx_path_p *path)
|
||||
{
|
||||
fx_file_info info;
|
||||
if (!FX_OK(path_stat(path, &info))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool path_is_file(const struct fx_path_p *path)
|
||||
{
|
||||
fx_file_info info;
|
||||
if (!FX_OK(path_stat(path, &info))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (info.attrib & FX_FILE_ATTRIB_NORMAL) != 0;
|
||||
}
|
||||
|
||||
static bool path_is_directory(const struct fx_path_p *path)
|
||||
{
|
||||
fx_file_info info;
|
||||
if (!FX_OK(path_stat(path, &info))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (info.attrib & FX_FILE_ATTRIB_DIRECTORY) != 0;
|
||||
}
|
||||
|
||||
static void append_path(struct fx_path_p *dest, const fx_string *src)
|
||||
{
|
||||
const char *src_cstr = fx_string_get_cstr(src);
|
||||
if (src_cstr[0] == '/') {
|
||||
fx_string_clear(dest->p_pathstr);
|
||||
fx_string_append_s(dest->p_pathstr, src);
|
||||
return;
|
||||
}
|
||||
|
||||
if (fx_string_get_size(dest->p_pathstr, FX_STRLEN_NORMAL) > 0
|
||||
&& fx_string_get_last_char(dest->p_pathstr) != '/'
|
||||
&& fx_string_get_first_char(src) != '/') {
|
||||
fx_string_append_c(dest->p_pathstr, '/');
|
||||
}
|
||||
|
||||
fx_string_append_s(dest->p_pathstr, src);
|
||||
}
|
||||
|
||||
static enum fx_status path_unlink(const struct fx_path_p *path)
|
||||
{
|
||||
#if 0
|
||||
int err = remove(fx_string_get_cstr(path->p_pathstr));
|
||||
return err == 0 ? FX_SUCCESS
|
||||
: fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
|
||||
#endif
|
||||
return FX_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
static enum fx_status path_get_directory(
|
||||
const struct fx_path_p *path,
|
||||
fx_path **out_dir_path)
|
||||
{
|
||||
fx_string *path_str = path->p_pathstr;
|
||||
long len = fx_string_get_size(path_str, FX_STRLEN_NORMAL);
|
||||
|
||||
const char *path_cstr = fx_string_get_cstr(path_str);
|
||||
char *sep = strrchr(path_cstr, '/');
|
||||
|
||||
if (!sep) {
|
||||
*out_dir_path = fx_path_create();
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
size_t dir_path_len = (size_t)(sep - path_cstr);
|
||||
fx_string *dir_path_s = fx_string_get_substr(path_str, 0, dir_path_len);
|
||||
|
||||
fx_path *dir_path
|
||||
= fx_path_create_from_cstr(fx_string_get_cstr(dir_path_s));
|
||||
fx_string_unref(dir_path_s);
|
||||
|
||||
*out_dir_path = dir_path;
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static enum fx_status path_get_filename(
|
||||
const struct fx_path_p *path,
|
||||
fx_string *out_name)
|
||||
{
|
||||
fx_string *path_str = path->p_pathstr;
|
||||
long len = fx_string_get_size(path_str, FX_STRLEN_NORMAL);
|
||||
|
||||
char *sep = strrchr(fx_string_get_cstr(path_str), '/');
|
||||
|
||||
if (!sep) {
|
||||
fx_string_append_s(out_name, path_str);
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
const char *filename = sep;
|
||||
while (*filename == '/' && *filename != '\0') {
|
||||
filename++;
|
||||
}
|
||||
|
||||
if (*filename == '\0') {
|
||||
fx_string_clear(out_name);
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
fx_string_append_cstr(out_name, filename);
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static size_t path_length(const struct fx_path_p *path)
|
||||
{
|
||||
return fx_string_get_size(path->p_pathstr, FX_STRLEN_NORMAL);
|
||||
}
|
||||
|
||||
/*** PUBLIC FUNCTIONS *********************************************************/
|
||||
|
||||
fx_path *fx_path_create_root()
|
||||
{
|
||||
const char *system_drive = "/";
|
||||
size_t system_drive_len = strlen(system_drive);
|
||||
|
||||
fx_path *path = fx_path_create();
|
||||
if (!path) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_path_p *p = fx_object_get_private(path, FX_TYPE_PATH);
|
||||
|
||||
fx_string_append_cstr(p->p_pathstr, system_drive);
|
||||
if (system_drive[system_drive_len - 1] != '\\') {
|
||||
fx_string_append_c(p->p_pathstr, '\\');
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
fx_path *fx_path_create_from_cstr(const char *cstr)
|
||||
{
|
||||
fx_path *path = fx_path_create();
|
||||
if (!path) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_path_p *p = fx_object_get_private(path, FX_TYPE_PATH);
|
||||
|
||||
char prev = 0;
|
||||
|
||||
for (size_t i = 0; cstr[i]; i++) {
|
||||
char c = cstr[i];
|
||||
if (c == '\\') {
|
||||
c = '/';
|
||||
}
|
||||
|
||||
if (prev == '/' && c == '/') {
|
||||
continue;
|
||||
}
|
||||
|
||||
fx_string_append_c(p->p_pathstr, c);
|
||||
prev = c;
|
||||
}
|
||||
|
||||
while (fx_string_get_last_char(p->p_pathstr) == '/') {
|
||||
fx_string_pop_back(p->p_pathstr);
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
fx_path *fx_path_get_system(fx_system_path path_type)
|
||||
{
|
||||
#if 0
|
||||
char path_buf[4096];
|
||||
const char *path_value = NULL, *default_value = NULL;
|
||||
switch (path_type) {
|
||||
case FX_PATH_ROOT:
|
||||
path_value = "/";
|
||||
break;
|
||||
case FX_PATH_CWD:
|
||||
if (getcwd(path_buf, sizeof path_buf) == NULL) {
|
||||
path_value = path_buf;
|
||||
}
|
||||
break;
|
||||
case FX_PATH_HOME:
|
||||
path_value = getenv("HOME");
|
||||
default_value = "~";
|
||||
break;
|
||||
case FX_PATH_CONFIG: {
|
||||
const char *home = getenv("HOME");
|
||||
path_value = getenv("XDG_CONFIG_HOME");
|
||||
snprintf(
|
||||
path_buf,
|
||||
sizeof path_buf,
|
||||
"%s/.config",
|
||||
home ? home : "~");
|
||||
default_value = path_buf;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (path_value) {
|
||||
return fx_path_create_from_cstr(path_value);
|
||||
}
|
||||
|
||||
if (default_value) {
|
||||
return fx_path_create_from_cstr(default_value);
|
||||
}
|
||||
#endif
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fx_path *fx_path_duplicate(const fx_path *path)
|
||||
{
|
||||
fx_path *new_path = fx_path_create();
|
||||
if (!path) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
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 = fx_string_duplicate(old_p->p_pathstr);
|
||||
if (!new_p->p_pathstr) {
|
||||
fx_path_unref(new_path);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return new_path;
|
||||
}
|
||||
|
||||
fx_path *fx_path_join_array(const fx_value *values[], size_t nr_values)
|
||||
{
|
||||
fx_path *result = fx_path_create();
|
||||
if (!result) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_path_p *result_p
|
||||
= fx_object_get_private(result, FX_TYPE_PATH);
|
||||
|
||||
fx_stringstream *tmp = fx_stringstream_create();
|
||||
fx_string *value_s = fx_string_create();
|
||||
for (size_t i = 0; i < nr_values; i++) {
|
||||
if (!values[i]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
fx_stringstream_reset(tmp);
|
||||
fx_value_to_string(values[i], tmp, NULL);
|
||||
|
||||
fx_string_clear(value_s);
|
||||
fx_string_append_cstr(value_s, fx_stringstream_get_cstr(tmp));
|
||||
append_path(result_p, value_s);
|
||||
}
|
||||
|
||||
fx_string_unref(value_s);
|
||||
fx_stringstream_unref(tmp);
|
||||
return result;
|
||||
}
|
||||
|
||||
fx_path *fx_path_join_list(size_t count, ...)
|
||||
{
|
||||
fx_path *result = fx_path_create();
|
||||
if (!result) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_path_p *result_p
|
||||
= fx_object_get_private(result, FX_TYPE_PATH);
|
||||
va_list arg;
|
||||
va_start(arg, count);
|
||||
|
||||
fx_stringstream *tmp = fx_stringstream_create();
|
||||
fx_string *value_s = fx_string_create();
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
const fx_value *value = va_arg(arg, const fx_value *);
|
||||
|
||||
fx_stringstream_reset(tmp);
|
||||
fx_value_to_string(value, tmp, NULL);
|
||||
|
||||
fx_string_clear(value_s);
|
||||
fx_string_append_cstr(value_s, fx_stringstream_get_cstr(tmp));
|
||||
append_path(result_p, value_s);
|
||||
}
|
||||
|
||||
fx_string_unref(value_s);
|
||||
fx_stringstream_unref(tmp);
|
||||
return result;
|
||||
}
|
||||
|
||||
fx_path *fx_path_make_absolute(const fx_path *in)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_make_absolute, in);
|
||||
}
|
||||
|
||||
fx_path *fx_path_make_relative(const fx_path *in)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_make_relative, in);
|
||||
}
|
||||
|
||||
fx_path *fx_path_make_canonical(const fx_path *in)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_make_canonical, in);
|
||||
}
|
||||
|
||||
bool fx_path_is_absolute(const fx_path *path)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_is_absolute, path);
|
||||
}
|
||||
|
||||
bool fx_path_exists(const fx_path *path)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_exists, path);
|
||||
}
|
||||
|
||||
bool fx_path_is_file(const fx_path *path)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_is_file, path);
|
||||
}
|
||||
|
||||
bool fx_path_is_directory(const fx_path *path)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_is_directory, path);
|
||||
}
|
||||
|
||||
enum fx_status fx_path_stat(const fx_path *path, struct fx_file_info *out)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_PATH, path_stat, path, out);
|
||||
}
|
||||
|
||||
enum fx_status fx_path_unlink(const fx_path *path)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_unlink, path);
|
||||
}
|
||||
|
||||
enum fx_status fx_path_get_directory(
|
||||
const fx_path *path,
|
||||
fx_path **out_dir_path)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_TYPE_PATH,
|
||||
path_get_directory,
|
||||
path,
|
||||
out_dir_path);
|
||||
}
|
||||
|
||||
enum fx_status fx_path_get_filename(const fx_path *path, fx_string *out_name)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_TYPE_PATH,
|
||||
path_get_filename,
|
||||
path,
|
||||
out_name);
|
||||
}
|
||||
|
||||
const char *fx_path_get_cstr(const fx_path *path)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_ptr, path);
|
||||
}
|
||||
|
||||
size_t fx_path_length(const fx_path *path)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_length, path);
|
||||
}
|
||||
|
||||
/*** VIRTUAL FUNCTIONS ********************************************************/
|
||||
|
||||
static void path_init(fx_object *obj, void *priv)
|
||||
{
|
||||
struct fx_path_p *path = priv;
|
||||
|
||||
path->p_pathstr = fx_string_create();
|
||||
if (!path->p_pathstr) {
|
||||
/* TODO return error */
|
||||
}
|
||||
}
|
||||
|
||||
void path_fini(fx_object *obj, void *priv)
|
||||
{
|
||||
struct fx_path_p *path = priv;
|
||||
|
||||
fx_string_unref(path->p_pathstr);
|
||||
}
|
||||
|
||||
static fx_status path_to_string(
|
||||
const fx_value *obj,
|
||||
fx_stream *out,
|
||||
const char *format)
|
||||
{
|
||||
struct fx_path_p *path
|
||||
= fx_object_get_private(obj->v_object, FX_TYPE_PATH);
|
||||
|
||||
return fx_stream_write_cstr(
|
||||
out,
|
||||
fx_string_get_cstr(path->p_pathstr),
|
||||
NULL);
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_path)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = path_to_string;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
FX_TYPE_CLASS_END(fx_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)
|
||||
Reference in New Issue
Block a user