39 Commits

Author SHA1 Message Date
wash 9da33eec71 build: find and link to Threads::Threads 2026-07-19 13:26:44 +01:00
wash 181a6f7c24 fx: implement mutex support for rosetta 2026-07-19 13:26:18 +01:00
wash 0fa5c7373f fx.term: add basic rosetta support 2026-07-19 13:26:01 +01:00
wash cb97001033 fx.io: add basic rosetta support 2026-07-19 13:25:53 +01:00
wash 181a6bac09 test: update tests 2026-07-12 22:15:25 +01:00
wash 24bcb18543 fx.io: update all path parameters to be fx_values 2026-07-12 22:15:14 +01:00
wash e9a1bfbde2 fx.diagnostics: process: implement environment management 2026-07-12 22:14:57 +01:00
wash 5c837abc6c fx.collections: hashtable: implement clone() 2026-07-12 22:13:36 +01:00
wash ad27b0cf73 fx.term: tty: implement get_cursor_position() on apple 2026-07-05 16:01:33 +01:00
wash dbd2135998 fx: implement byte-oriented memory stream class 2026-07-05 15:59:41 +01:00
wash 50706084eb fx: object: handle NULL in query_type() 2026-07-05 15:59:14 +01:00
wash 38699a0eb3 fx: object: add clone() virtual function 2026-07-05 15:58:50 +01:00
wash c29c687076 test: process input/output, args, and environment test 2026-06-20 15:29:32 +01:00
wash c41131eac8 fx.term: tty: implement get_cursor_position() and flush() 2026-06-20 15:28:51 +01:00
wash eb759982db fx.serial: update stringstream usage 2026-06-20 15:27:56 +01:00
wash 0ee94af705 fx.diagnostics: remove debug messages 2026-06-20 15:27:37 +01:00
wash 5b16d98e51 fx.collections: update stringstream usage 2026-06-20 15:27:16 +01:00
wash 5156971945 fx.collections: hashtable: fix handling of existing keys in put() 2026-06-20 15:27:00 +01:00
wash c887a18a10 fx.collections: hashtable: fix index calculation issues 2026-06-20 15:26:31 +01:00
wash c0f139dde6 fx.cmdline: update stringstream usage 2026-06-20 15:25:44 +01:00
wash ee11ff240f fx.io: expand path joining functionality to non-path strings 2026-06-20 15:25:26 +01:00
wash 32d4d0abf2 fx: start implementing global data cleanup 2026-06-20 15:24:23 +01:00
wash f093c0fb6e fx: implement mutex support 2026-06-20 15:24:11 +01:00
wash 9145e3bac7 fx: string: fix remove_ansi() not updating codepoints count 2026-06-20 15:23:26 +01:00
wash fcf44cc288 fx: string: make tokenise() const 2026-06-20 15:23:06 +01:00
wash 4d6f2afa5a fx: encoding: treat the entire reserved range of codepoints as control characters 2026-06-20 15:22:35 +01:00
wash 7cbd1eb162 fx: stringstream: rename ptr() to get_cstr() 2026-06-20 15:22:09 +01:00
wash f6320d64cd fx.collections: hashtable: add bounds checking to iterator 2026-06-13 14:49:44 +01:00
wash 87ff913186 fx: stringstream: make internal virtual functions static 2026-06-13 14:49:22 +01:00
wash 52c53fa57f fx: string: implement iterator destructor 2026-06-13 14:48:52 +01:00
wash 8237fae60f fx.diagnostics: add linux support for starting processes 2026-06-13 14:48:19 +01:00
wash 10cfb05287 fx.io: add linux support for iostreams and pipes 2026-06-13 14:47:46 +01:00
wash a4e797f619 meta: add fx.diagnostics namespace and assembly
the fx.diagnostics namespace includes a process class for starting and managing processes
2026-06-10 19:08:45 +01:00
wash 2512611f8f fx.io: import system-level streams and uni-directional pipes 2026-06-10 19:08:36 +01:00
wash 15784c3404 fx: stream: only return an error from read/write if no bytes were moved 2026-06-10 19:08:09 +01:00
wash 20dc844c9d fx.serial: formatting cleanup 2026-05-31 17:24:48 +01:00
wash 93ecd02b3f fx: string: handle string_duplicate not copying the null terminator 2026-05-31 17:24:34 +01:00
wash 612ba7101d fx: object: handle nullptr in object_ref and object_unref 2026-05-31 17:24:11 +01:00
wash fb864a64c9 fx: handle nullptr arguments in all iterator functions 2026-05-31 17:23:53 +01:00
73 changed files with 5455 additions and 489 deletions
+10 -2
View File
@@ -1,7 +1,10 @@
cmake_minimum_required(VERSION 3.25) cmake_minimum_required(VERSION 3.25)
project(fx C ASM) project(fx C ASM)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
find_package(Threads REQUIRED)
include (TestBigEndian) include (TestBigEndian)
include(Templates) include(Templates)
include(Platform) include(Platform)
@@ -15,7 +18,6 @@ else ()
add_compile_definitions(FX_ENDIAN=0) add_compile_definitions(FX_ENDIAN=0)
endif () endif ()
set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD 99)
set(CMAKE_C_EXTENSIONS OFF) set(CMAKE_C_EXTENSIONS OFF)
@@ -62,3 +64,9 @@ endforeach (assembly)
if (fx_enable_tests) if (fx_enable_tests)
add_subdirectory(test) add_subdirectory(test)
endif () endif ()
set_target_properties(
${fx_assemblies}
PROPERTIES
INSTALL_RPATH
"${CMAKE_INSTALL_RPATH};${CMAKE_INSTALL_PREFIX}/lib")
+4
View File
@@ -0,0 +1,4 @@
add_fx_assembly(
NAME fx.diagnostics
NAMESPACES fx.diagnostics
DEPENDENCIES fx.runtime fx.collections fx.io)
+7
View File
@@ -0,0 +1,7 @@
#include <fx/macros.h>
#include <fx/reflection/assembly.h>
FX_ASSEMBLY_BEGIN(fx_diagnostics)
FX_ASSEMBLY_NAME("fx.diagnostics");
FX_ASSEMBLY_VERSION(1, 0, 0, 0);
FX_ASSEMBLY_END(fx_diagnostics)
+2 -1
View File
@@ -1,3 +1,4 @@
add_fx_assembly( add_fx_assembly(
NAME fx.runtime NAME fx.runtime
NAMESPACES fx fx.reflection) NAMESPACES fx fx.reflection
DEPENDENCIES Threads::Threads)
+1 -1
View File
@@ -48,7 +48,7 @@ enum fx_status fx_arglist_report_missing_option(
} }
fx_err("required option `" F_YELLOW "%s" F_RESET "` was not specified.", fx_err("required option `" F_YELLOW "%s" F_RESET "` was not specified.",
fx_stringstream_ptr(opt_name)); fx_stringstream_get_cstr(opt_name));
fx_i("usage: %s", fx_string_get_cstr(opt_string)); fx_i("usage: %s", fx_string_get_cstr(opt_string));
fx_i("for more information, use `" F_YELLOW "--help" F_RESET "`"); fx_i("for more information, use `" F_YELLOW "--help" F_RESET "`");
+81 -11
View File
@@ -68,9 +68,14 @@ static fx_status convert_key_to_index(
return status; return status;
} }
size_t index = hash % table_size, i = 1; size_t base_index = hash % table_size;
size_t index = base_index, i = 0;
bool index_found = false; bool index_found = false;
while (table[index].i_item && i <= table_size) { while (i <= table_size) {
if (!table[index].i_item) {
goto skip;
}
const struct fx_hashtable_item_p *item = fx_object_get_private( const struct fx_hashtable_item_p *item = fx_object_get_private(
table[index].i_item, table[index].i_item,
FX_TYPE_HASHTABLE_ITEM); FX_TYPE_HASHTABLE_ITEM);
@@ -80,7 +85,8 @@ static fx_status convert_key_to_index(
break; break;
} }
index = (index + (i * i)) % table_size; skip:
index = (base_index + (i * i)) % table_size;
i++; i++;
} }
@@ -108,7 +114,7 @@ static fx_status allocate_index_for_key(
size_t index = hash % table_size; size_t index = hash % table_size;
bool ok = false; bool ok = false;
for (size_t i = 1; i < table_size; i++) { for (size_t i = 0; i < table_size; i++) {
if (!table[index].i_item) { if (!table[index].i_item) {
ok = true; ok = true;
break; break;
@@ -276,18 +282,27 @@ static fx_status hashtable_put(
break; break;
} while (1); } while (1);
fx_hashtable_item *item = fx_object_create(FX_TYPE_HASHTABLE_ITEM); fx_hashtable_item *item = hashtable->t_items[index].i_item;
struct fx_hashtable_item_p *item_p = NULL;
if (!item) { if (!item) {
return FX_ERR_NO_MEMORY; item = fx_object_create(FX_TYPE_HASHTABLE_ITEM);
if (!item) {
return FX_ERR_NO_MEMORY;
}
item_p = fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
fx_value_copy(&item_p->i_key, key);
hashtable->t_items[index].i_item = item;
hashtable->t_count++;
} else {
item_p = fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
fx_value_unset(&item_p->i_value);
} }
struct fx_hashtable_item_p *item_p
= fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
fx_value_copy(&item_p->i_key, key);
fx_value_copy(&item_p->i_value, value); fx_value_copy(&item_p->i_value, value);
hashtable->t_items[index].i_item = item;
hashtable->t_count++;
return FX_SUCCESS; return FX_SUCCESS;
} }
@@ -531,6 +546,56 @@ static fx_status to_string(
return FX_SUCCESS; return FX_SUCCESS;
} }
static fx_status clone(const fx_value *src_v, fx_value *dest_v)
{
fx_hashtable *src_table = NULL, *dest_table = NULL;
fx_value_get_object(src_v, &src_table);
dest_table = fx_hashtable_create();
if (!dest_table) {
return FX_ERR_NO_MEMORY;
}
struct fx_hashtable_p *src_p
= fx_object_get_private(src_table, FX_TYPE_HASHTABLE);
struct fx_hashtable_p *dest_p
= fx_object_get_private(dest_table, FX_TYPE_HASHTABLE);
dest_p->t_count = src_p->t_count;
dest_p->t_max_index = src_p->t_max_index;
size_t capacity = primes[dest_p->t_max_index];
dest_p->t_items = calloc(capacity, sizeof *dest_p->t_items);
if (!dest_p->t_items) {
fx_hashtable_unref(dest_table);
return FX_ERR_NO_MEMORY;
}
for (size_t i = 0; i < capacity; i++) {
if (!src_p->t_items[i].i_item) {
continue;
}
fx_hashtable *new_item
= fx_object_create(FX_TYPE_HASHTABLE_ITEM);
if (!new_item) {
fx_hashtable_unref(dest_table);
return FX_ERR_NO_MEMORY;
}
struct fx_hashtable_item_p *src_item_p = fx_object_get_private(
src_p->t_items[i].i_item,
FX_TYPE_HASHTABLE_ITEM);
struct fx_hashtable_item_p *new_item_p = fx_object_get_private(
new_item,
FX_TYPE_HASHTABLE_ITEM);
new_item_p->i_key = fx_value_copy_return(src_item_p->i_key);
new_item_p->i_value = fx_value_copy_return(src_item_p->i_value);
dest_p->t_items[i].i_item = new_item;
}
*dest_v = FX_VALUE_OBJECT(dest_table);
return FX_SUCCESS;
}
static fx_status get_key( static fx_status get_key(
const fx_value *item_value, const fx_value *item_value,
const fx_property *prop, const fx_property *prop,
@@ -623,6 +688,10 @@ static const fx_value *iterator_get_value(const fx_iterator *obj)
struct table_item *table = it->_h_p->t_items; struct table_item *table = it->_h_p->t_items;
size_t capacity = primes[it->_h_p->t_max_index]; size_t capacity = primes[it->_h_p->t_max_index];
if (it->i >= capacity) {
return NULL;
}
if (table[it->i].i_item) { if (table[it->i].i_item) {
return &it->item_value; return &it->item_value;
} }
@@ -636,6 +705,7 @@ static const fx_value *iterator_get_value(const fx_iterator *obj)
FX_TYPE_CLASS_BEGIN(fx_hashtable) FX_TYPE_CLASS_BEGIN(fx_hashtable)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = to_string; FX_INTERFACE_ENTRY(to_string) = to_string;
FX_INTERFACE_ENTRY(clone) = clone;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
+1 -1
View File
@@ -11,7 +11,7 @@ int main(int argc, const char **argv)
fx_stream_read_all_bytes_s(fx_stdin, dest_stream, buf, &nr_read); fx_stream_read_all_bytes_s(fx_stdin, dest_stream, buf, &nr_read);
printf("done. read %zu bytes total.\n", nr_read); printf("done. read %zu bytes total.\n", nr_read);
printf("%s\n", fx_stringstream_ptr(dest_stream)); printf("%s\n", fx_stringstream_get_cstr(dest_stream));
fx_stringstream_unref(dest_stream); fx_stringstream_unref(dest_stream);
fx_stream_buffer_unref(buf); fx_stream_buffer_unref(buf);
+1
View File
@@ -0,0 +1 @@
export_fx_namespace_details(fx.diagnostics)
@@ -0,0 +1,46 @@
#ifndef FX_DIAGNOSTICS_PROCESS_H_
#define FX_DIAGNOSTICS_PROCESS_H_
#include <fx/collections/array.h>
#include <fx/collections/hashtable.h>
#include <fx/macros.h>
#include <fx/stream.h>
FX_DECLS_BEGIN;
#define FX_DIAGNOSTICS_TYPE_PROCESS (fx_process_get_type())
FX_DECLARE_TYPE(fx_process);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_process)
FX_TYPE_CLASS_DECLARATION_END(fx_process)
typedef struct fx_process_start_info {
bool proc_redirect_stdin, proc_redirect_stdout, proc_redirect_stderr;
const char *proc_file_name;
const fx_array *proc_args;
const fx_hashtable *proc_environment;
} fx_process_start_info;
FX_API fx_type_id fx_process_get_type(void);
FX_API fx_process *fx_process_create(const fx_process_start_info *info);
FX_API fx_process *fx_process_get_self(void);
FX_API fx_status fx_process_start(fx_process *proc);
FX_API fx_status fx_process_wait(fx_process *proc, int *out_result);
FX_API fx_status fx_process_kill(fx_process *proc);
FX_API const fx_hashtable *fx_process_get_environment(const fx_process *proc);
FX_API fx_stream *fx_process_get_stdin(fx_process *proc);
FX_API fx_stream *fx_process_get_stdout(fx_process *proc);
FX_API fx_stream *fx_process_get_stderr(fx_process *proc);
FX_API void fx_process_close_stdin(fx_process *proc);
FX_API void fx_process_close_stdout(fx_process *proc);
FX_API void fx_process_close_stderr(fx_process *proc);
FX_DECLS_END;
#endif
+570
View File
@@ -0,0 +1,570 @@
#define _POSIX_C_SOURCE 200809L
#include <fx/collections/array.h>
#include <fx/diagnostics/process.h>
#include <fx/global.h>
#include <fx/io/path.h>
#include <fx/io/pipe.h>
#include <fx/io/stream.h>
#include <fx/iterator.h>
#include <fx/macros.h>
#include <fx/string.h>
#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>
static fx_process *self = NULL;
extern const char **environ;
FX_API fx_type_id fx_process_iterator_get_type();
FX_DECLARE_TYPE(fx_process_iterator);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_process_iterator)
FX_TYPE_CLASS_DECLARATION_END(fx_process_iterator)
struct fx_process_p {
fx_string *proc_exec_path;
fx_array *proc_args;
bool proc_redirect_stdin, proc_redirect_stdout, proc_redirect_stderr;
fx_stream *proc_stdin, *proc_stdout, *proc_stderr;
pid_t proc_id;
fx_hashtable *proc_environment;
};
struct fx_process_iterator_p {
};
/*** PRIVATE FUNCTIONS ********************************************************/
static bool exec_name_is_path(const char *s)
{
fx_wchar c;
while (*s) {
c = fx_wchar_utf8_codepoint_decode(s);
if (c == FX_WCHAR_INVALID) {
break;
}
if (c == '/') {
return true;
}
s += fx_wchar_utf8_codepoint_stride(s);
}
return false;
}
const fx_string *get_path(void)
{
const char *path_env = NULL;
fx_process *self = fx_process_get_self();
if (!self) {
goto fallback;
}
const fx_hashtable *env = fx_process_get_environment(self);
if (!env) {
goto fallback;
}
const fx_value *path_v = fx_hashtable_get(env, &FX_CSTR("PATH"));
if (!path_v) {
goto fallback;
}
fx_string *result = NULL;
fx_value_get_object(path_v, &result);
return result;
fallback:
path_env = getenv("PATH");
if (!path_env) {
return NULL;
}
return fx_string_create_from_cstr(path_env);
}
static fx_status find_executable(fx_string *exec_path)
{
const char *exec_path_cstr = fx_string_get_cstr(exec_path);
const fx_string *path_env = get_path();
if (!path_env) {
return FX_ERR_NO_ENTRY;
}
const char *delim[] = {":"};
size_t nr_delim = sizeof delim / sizeof delim[0];
fx_path *path = NULL;
fx_iterator *it = fx_string_tokenise(
path_env,
delim,
nr_delim,
FX_STRING_TOK_F_NORMAL);
fx_status status = FX_ERR_NO_ENTRY;
fx_foreach(tok, it)
{
path = fx_path_join_list(2, tok, &FX_VALUE_OBJECT(exec_path));
if (fx_path_is_file(path)) {
fx_string_clear(exec_path);
fx_string_append_cstr(
exec_path,
fx_path_get_cstr(path));
status = FX_SUCCESS;
break;
}
}
fx_iterator_unref(it);
fx_path_unref(path);
return status;
}
static fx_status process_start(struct fx_process_p *proc)
{
fx_iostream *in_r = NULL, *in_w = NULL;
fx_iostream *out_r = NULL, *out_w = NULL;
fx_iostream *err_r = NULL, *err_w = NULL;
fx_status status = FX_SUCCESS;
bool name_is_path
= exec_name_is_path(fx_string_get_cstr(proc->proc_exec_path));
if (!name_is_path) {
status = find_executable(proc->proc_exec_path);
}
if (!FX_OK(status)) {
return status;
}
if (proc->proc_redirect_stdin) {
status = fx_pipe_create(&in_r, &in_w);
if (!FX_OK(status)) {
goto cleanup;
}
}
if (proc->proc_redirect_stdout) {
status = fx_pipe_create(&out_r, &out_w);
if (!FX_OK(status)) {
goto cleanup;
}
}
if (proc->proc_redirect_stderr) {
status = fx_pipe_create(&err_r, &err_w);
if (!FX_OK(status)) {
goto cleanup;
}
}
pid_t pid = fork();
if (pid < 0) {
status = FX_ERR_INVALID_ARGUMENT;
goto cleanup;
}
if (pid > 0) {
proc->proc_id = pid;
proc->proc_stdin = fx_iostream_ref(in_w);
proc->proc_stdout = fx_iostream_ref(out_r);
proc->proc_stderr = fx_iostream_ref(err_r);
status = FX_SUCCESS;
goto cleanup;
}
int fd = -1;
if (proc->proc_redirect_stdin) {
fx_iostream_unref(in_w);
fd = fx_iostream_steal_os_handle(in_r);
int err = dup2(fd, 0);
close(fd);
}
if (proc->proc_redirect_stdout) {
fx_iostream_unref(out_r);
fd = fx_iostream_steal_os_handle(out_w);
dup2(fd, 1);
close(fd);
}
if (proc->proc_redirect_stderr) {
fx_iostream_unref(err_r);
fd = fx_iostream_steal_os_handle(err_w);
dup2(fd, 2);
close(fd);
}
const char **argv = NULL;
const char **envp = NULL;
const char *default_argv[] = {
fx_string_get_cstr(proc->proc_exec_path),
NULL,
};
size_t argc = 0;
size_t envc = 0;
if (proc->proc_args) {
argc = fx_array_get_size(proc->proc_args);
}
if (proc->proc_environment) {
envc = fx_hashtable_get_count(proc->proc_environment);
}
if (argc) {
argv = calloc(argc + 1, sizeof(char *));
for (size_t i = 0; i < argc; i++) {
const fx_value *arg_value
= fx_array_get_ref(proc->proc_args, i);
fx_value_get_cstr(arg_value, &argv[i]);
}
} else {
argv = default_argv;
}
if (envc) {
envp = calloc(envc + 1, sizeof(char *));
size_t i = 0;
fx_stringstream *tmp = fx_stringstream_create();
const fx_iterator *it
= fx_iterator_begin(proc->proc_environment);
fx_foreach(v, it)
{
fx_hashtable_item *item = NULL;
fx_value_get_object(v, &item);
const fx_value *name_v
= fx_hashtable_item_get_key(item);
const fx_value *value_v
= fx_hashtable_item_get_value(item);
fx_value_to_string(name_v, tmp, NULL);
fx_stream_write_char(tmp, '=');
fx_value_to_string(value_v, tmp, NULL);
envp[i++] = fx_stringstream_steal(tmp);
}
fx_iterator_unref(it);
fx_string_unref(tmp);
}
execve(fx_string_get_cstr(proc->proc_exec_path),
(char *const *)argv,
(char *const *)envp);
exit(127);
cleanup:
fx_iostream_unref(in_r);
fx_iostream_unref(in_w);
fx_iostream_unref(out_r);
fx_iostream_unref(out_w);
fx_iostream_unref(err_r);
fx_iostream_unref(err_w);
return status;
}
static fx_status process_wait(struct fx_process_p *proc, int *out_result)
{
if (proc->proc_id == 0) {
return FX_ERR_BAD_STATE;
}
int status = 0;
do {
int err = waitpid(proc->proc_id, &status, WUNTRACED);
if (err != 0) {
return FX_ERR_BAD_STATE;
}
} while (!WIFEXITED(status));
proc->proc_id = 0;
*out_result = WEXITSTATUS(status);
return FX_ERR_NOT_SUPPORTED;
}
static fx_status process_kill(struct fx_process_p *proc)
{
if (proc->proc_id == 0) {
return FX_ERR_BAD_STATE;
}
int err = kill(proc->proc_id, SIGTERM);
if (err != 0) {
return FX_ERR_BAD_STATE;
}
return FX_SUCCESS;
}
static const fx_hashtable *process_get_environment(
const struct fx_process_p *proc)
{
return proc->proc_environment;
}
static fx_stream *process_get_stdin(struct fx_process_p *proc)
{
return proc->proc_stdin;
}
static fx_stream *process_get_stdout(struct fx_process_p *proc)
{
return proc->proc_stdout;
}
static fx_stream *process_get_stderr(struct fx_process_p *proc)
{
return proc->proc_stderr;
}
static void process_close_stdin(struct fx_process_p *proc)
{
fx_iostream_unref(proc->proc_stdin);
proc->proc_stdin = NULL;
}
static void process_close_stdout(struct fx_process_p *proc)
{
fx_iostream_unref(proc->proc_stdout);
proc->proc_stdout = NULL;
}
static void process_close_stderr(struct fx_process_p *proc)
{
fx_iostream_unref(proc->proc_stderr);
proc->proc_stderr = NULL;
}
/*** PUBLIC FUNCTIONS *********************************************************/
fx_process *fx_process_create(const fx_process_start_info *info)
{
fx_process *out = fx_object_create(FX_DIAGNOSTICS_TYPE_PROCESS);
if (!out) {
return NULL;
}
struct fx_process_p *proc
= fx_object_get_private(out, FX_DIAGNOSTICS_TYPE_PROCESS);
proc->proc_exec_path = fx_string_create_from_cstr(info->proc_file_name);
if (!proc->proc_exec_path) {
fx_process_unref(out);
return NULL;
}
proc->proc_args = fx_array_ref((fx_array *)info->proc_args);
proc->proc_environment
= fx_hashtable_ref((fx_hashtable *)info->proc_environment);
proc->proc_redirect_stdin = info->proc_redirect_stdin;
proc->proc_redirect_stdout = info->proc_redirect_stdout;
proc->proc_redirect_stderr = info->proc_redirect_stderr;
return out;
}
fx_process *fx_process_get_self(void)
{
if (self) {
return self;
}
self = fx_object_create(FX_DIAGNOSTICS_TYPE_PROCESS);
if (!self) {
return NULL;
}
struct fx_process_p *proc
= fx_object_get_private(self, FX_DIAGNOSTICS_TYPE_PROCESS);
proc->proc_id = getpid();
fx_hashtable *env = fx_hashtable_create();
for (size_t i = 0; environ[i]; i++) {
char *envstr = fx_strdup(environ[i]);
const char *name = envstr;
char *sep = strchr(name, '=');
const char *value = NULL;
if (sep) {
*sep = '\0';
value = sep + 1;
}
fx_string *name_str = fx_string_create_from_cstr(name);
fx_string *value_str = fx_string_create();
if (value) {
fx_string_append_cstr(value_str, value);
}
fx_hashtable_put(
env,
&FX_VALUE_OBJECT(name_str),
&FX_VALUE_OBJECT(value_str));
fx_string_unref(name_str);
fx_string_unref(value_str);
free(envstr);
}
proc->proc_environment = env;
fx_register_global(self);
return self;
}
fx_status fx_process_start(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_start,
proc);
}
fx_status fx_process_wait(fx_process *proc, int *out_result)
{
FX_CLASS_DISPATCH_STATIC(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_wait,
proc,
out_result);
}
fx_status fx_process_kill(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_kill,
proc);
}
const fx_hashtable *fx_process_get_environment(const fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_get_environment,
proc);
}
fx_stream *fx_process_get_stdin(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_get_stdin,
proc);
}
fx_stream *fx_process_get_stdout(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_get_stdout,
proc);
}
fx_stream *fx_process_get_stderr(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_get_stderr,
proc);
}
void fx_process_close_stdin(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_V0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_close_stdin,
proc);
}
void fx_process_close_stdout(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_V0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_close_stdout,
proc);
}
void fx_process_close_stderr(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_V0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_close_stderr,
proc);
}
/*** VIRTUAL FUNCTIONS ********************************************************/
static void process_init(fx_object *obj, void *priv)
{
}
static void process_fini(fx_object *obj, void *priv)
{
struct fx_process_p *proc = priv;
fx_array_unref(proc->proc_args);
fx_iostream_unref(proc->proc_stdin);
fx_iostream_unref(proc->proc_stdout);
fx_iostream_unref(proc->proc_stderr);
fx_string_unref(proc->proc_exec_path);
fx_hashtable_unref(proc->proc_environment);
}
/*** ITERATOR DEFINITION ******************************************************/
static enum fx_status process_iterator_move_next(const fx_iterator *obj)
{
return FX_ERR_NO_DATA;
}
static const fx_value *process_iterator_get_value(const fx_iterator *obj)
{
return NULL;
}
/*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_BEGIN(fx_process)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_END(fx_process)
FX_TYPE_DEFINITION_BEGIN(fx_process)
FX_TYPE_ID(0x7334594f, 0xa1c3, 0x4715, 0xab69, 0x68a709e1b64b);
FX_TYPE_NAME("fx.diagnostics.process");
FX_TYPE_CLASS(fx_process_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_process_p);
FX_TYPE_INSTANCE_INIT(process_init);
FX_TYPE_INSTANCE_FINI(process_fini);
FX_TYPE_DEFINITION_END(fx_process)
FX_TYPE_CLASS_BEGIN(fx_process_iterator)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
FX_INTERFACE_ENTRY(it_move_next) = process_iterator_move_next;
FX_INTERFACE_ENTRY(it_get_value) = process_iterator_get_value;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_END(fx_process_iterator)
FX_TYPE_DEFINITION_BEGIN(fx_process_iterator)
FX_TYPE_ID(0x67eb13b6, 0x25d1, 0x424a, 0xb136, 0x871a07829089);
FX_TYPE_NAME("fx.diagnostics.process.iterator");
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
FX_TYPE_CLASS(fx_process_iterator_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_process_iterator_p);
FX_TYPE_DEFINITION_END(fx_process_iterator)
+491
View File
@@ -0,0 +1,491 @@
#define _POSIX_C_SOURCE 200809L
#include <fx/collections/array.h>
#include <fx/diagnostics/process.h>
#include <fx/global.h>
#include <fx/io/pipe.h>
#include <fx/io/stream.h>
#include <fx/iterator.h>
#include <fx/macros.h>
#include <fx/string.h>
#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>
static fx_process *self = NULL;
extern const char **environ;
FX_API fx_type_id fx_process_iterator_get_type();
FX_DECLARE_TYPE(fx_process_iterator);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_process_iterator)
FX_TYPE_CLASS_DECLARATION_END(fx_process_iterator)
struct fx_process_p {
fx_string *proc_exec_path;
fx_array *proc_args;
bool proc_redirect_stdin, proc_redirect_stdout, proc_redirect_stderr;
fx_stream *proc_stdin, *proc_stdout, *proc_stderr;
pid_t proc_id;
fx_hashtable *proc_environment;
};
struct fx_process_iterator_p {
};
/*** PRIVATE FUNCTIONS ********************************************************/
static fx_status process_start(struct fx_process_p *proc)
{
fx_iostream *in_r = NULL, *in_w = NULL;
fx_iostream *out_r = NULL, *out_w = NULL;
fx_iostream *err_r = NULL, *err_w = NULL;
fx_status status = FX_SUCCESS;
if (proc->proc_redirect_stdin) {
status = fx_pipe_create(&in_r, &in_w);
if (!FX_OK(status)) {
goto cleanup;
}
}
if (proc->proc_redirect_stdout) {
status = fx_pipe_create(&out_r, &out_w);
if (!FX_OK(status)) {
goto cleanup;
}
}
if (proc->proc_redirect_stderr) {
status = fx_pipe_create(&err_r, &err_w);
if (!FX_OK(status)) {
goto cleanup;
}
}
pid_t pid = fork();
if (pid < 0) {
status = FX_ERR_INVALID_ARGUMENT;
goto cleanup;
}
if (pid > 0) {
proc->proc_id = pid;
proc->proc_stdin = fx_iostream_ref(in_w);
proc->proc_stdout = fx_iostream_ref(out_r);
proc->proc_stderr = fx_iostream_ref(err_r);
status = FX_SUCCESS;
goto cleanup;
}
int fd = -1;
if (proc->proc_redirect_stdin) {
fx_iostream_unref(in_w);
fd = fx_iostream_steal_os_handle(in_r);
int err = dup2(fd, 0);
close(fd);
}
if (proc->proc_redirect_stdout) {
fx_iostream_unref(out_r);
fd = fx_iostream_steal_os_handle(out_w);
dup2(fd, 1);
close(fd);
}
if (proc->proc_redirect_stderr) {
fx_iostream_unref(err_r);
fd = fx_iostream_steal_os_handle(err_w);
dup2(fd, 2);
close(fd);
}
const char **argv = NULL;
const char **envp = NULL;
const char *default_argv[] = {
fx_string_get_cstr(proc->proc_exec_path),
NULL,
};
size_t argc = 0;
size_t envc = 0;
if (proc->proc_args) {
argc = fx_array_get_size(proc->proc_args);
}
if (proc->proc_environment) {
envc = fx_hashtable_get_count(proc->proc_environment);
}
if (argc) {
argv = calloc(argc + 1, sizeof(char *));
for (size_t i = 0; i < argc; i++) {
const fx_value *arg_value
= fx_array_get_ref(proc->proc_args, i);
fx_value_get_cstr(arg_value, &argv[i]);
}
} else {
argv = default_argv;
}
if (envc) {
envp = calloc(envc + 1, sizeof(char *));
size_t i = 0;
fx_stringstream *tmp = fx_stringstream_create();
const fx_iterator *it
= fx_iterator_begin(proc->proc_environment);
fx_foreach(v, it)
{
fx_hashtable_item *item = NULL;
fx_value_get_object(v, &item);
const fx_value *name_v
= fx_hashtable_item_get_key(item);
const fx_value *value_v
= fx_hashtable_item_get_value(item);
fx_value_to_string(name_v, tmp, NULL);
fx_stream_write_char(tmp, '=');
fx_value_to_string(value_v, tmp, NULL);
envp[i++] = fx_stringstream_steal(tmp);
}
fx_iterator_unref(it);
fx_string_unref(tmp);
}
execve(fx_string_get_cstr(proc->proc_exec_path),
(char *const *)argv,
(char *const *)envp);
exit(127);
cleanup:
fx_iostream_unref(in_r);
fx_iostream_unref(in_w);
fx_iostream_unref(out_r);
fx_iostream_unref(out_w);
fx_iostream_unref(err_r);
fx_iostream_unref(err_w);
return status;
}
static fx_status process_wait(struct fx_process_p *proc, int *out_result)
{
if (proc->proc_id == 0) {
return FX_ERR_BAD_STATE;
}
int status = 0;
do {
int err = waitpid(proc->proc_id, &status, WUNTRACED);
if (err != 0) {
return FX_ERR_BAD_STATE;
}
} while (!WIFEXITED(status));
proc->proc_id = 0;
*out_result = WEXITSTATUS(status);
return FX_ERR_NOT_SUPPORTED;
}
static fx_status process_kill(struct fx_process_p *proc)
{
if (proc->proc_id == 0) {
return FX_ERR_BAD_STATE;
}
int err = kill(proc->proc_id, SIGTERM);
if (err != 0) {
return FX_ERR_BAD_STATE;
}
return FX_SUCCESS;
}
static const fx_hashtable *process_get_environment(
const struct fx_process_p *proc)
{
return proc->proc_environment;
}
static fx_stream *process_get_stdin(struct fx_process_p *proc)
{
return proc->proc_stdin;
}
static fx_stream *process_get_stdout(struct fx_process_p *proc)
{
return proc->proc_stdout;
}
static fx_stream *process_get_stderr(struct fx_process_p *proc)
{
return proc->proc_stderr;
}
static void process_close_stdin(struct fx_process_p *proc)
{
fx_iostream_unref(proc->proc_stdin);
proc->proc_stdin = NULL;
}
static void process_close_stdout(struct fx_process_p *proc)
{
fx_iostream_unref(proc->proc_stdout);
proc->proc_stdout = NULL;
}
static void process_close_stderr(struct fx_process_p *proc)
{
fx_iostream_unref(proc->proc_stderr);
proc->proc_stderr = NULL;
}
/*** PUBLIC FUNCTIONS *********************************************************/
fx_process *fx_process_create(const fx_process_start_info *info)
{
fx_process *out = fx_object_create(FX_DIAGNOSTICS_TYPE_PROCESS);
if (!out) {
return NULL;
}
struct fx_process_p *proc
= fx_object_get_private(out, FX_DIAGNOSTICS_TYPE_PROCESS);
proc->proc_exec_path = fx_string_create_from_cstr(info->proc_file_name);
if (!proc->proc_exec_path) {
fx_process_unref(out);
return NULL;
}
if (info->proc_args_count > 0) {
proc->proc_args = fx_array_create();
if (!proc->proc_args) {
fx_process_unref(out);
return NULL;
}
}
for (size_t i = 0; i < info->proc_args_count; i++) {
fx_string *arg = fx_string_create_from_cstr(info->proc_args[i]);
if (!arg) {
fx_process_unref(out);
return NULL;
}
fx_array_push_back(proc->proc_args, FX_VALUE_OBJECT(arg));
fx_string_unref(arg);
}
proc->proc_environment
= fx_hashtable_ref((fx_hashtable *)info->proc_environment);
proc->proc_redirect_stdin = info->proc_redirect_stdin;
proc->proc_redirect_stdout = info->proc_redirect_stdout;
proc->proc_redirect_stderr = info->proc_redirect_stderr;
return out;
}
fx_process *fx_process_get_self(void)
{
if (self) {
return self;
}
self = fx_object_create(FX_DIAGNOSTICS_TYPE_PROCESS);
if (!self) {
return NULL;
}
struct fx_process_p *proc
= fx_object_get_private(self, FX_DIAGNOSTICS_TYPE_PROCESS);
proc->proc_id = getpid();
fx_hashtable *env = fx_hashtable_create();
for (size_t i = 0; environ[i]; i++) {
char *envstr = fx_strdup(environ[i]);
const char *name = envstr;
char *sep = strchr(name, '=');
const char *value = NULL;
if (sep) {
*sep = '\0';
value = sep + 1;
}
fx_string *name_str = fx_string_create_from_cstr(name);
fx_string *value_str = fx_string_create();
if (value) {
fx_string_append_cstr(value_str, value);
}
fx_hashtable_put(
env,
&FX_VALUE_OBJECT(name_str),
&FX_VALUE_OBJECT(value_str));
fx_string_unref(name_str);
fx_string_unref(value_str);
free(envstr);
}
proc->proc_environment = env;
fx_register_global(self);
return self;
}
fx_status fx_process_start(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_start,
proc);
}
fx_status fx_process_wait(fx_process *proc, int *out_result)
{
FX_CLASS_DISPATCH_STATIC(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_wait,
proc,
out_result);
}
fx_status fx_process_kill(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_kill,
proc);
}
const fx_hashtable *fx_process_get_environment(const fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_get_environment,
proc);
}
fx_stream *fx_process_get_stdin(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_get_stdin,
proc);
}
fx_stream *fx_process_get_stdout(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_get_stdout,
proc);
}
fx_stream *fx_process_get_stderr(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_get_stderr,
proc);
}
void fx_process_close_stdin(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_V0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_close_stdin,
proc);
}
void fx_process_close_stdout(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_V0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_close_stdout,
proc);
}
void fx_process_close_stderr(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_V0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_close_stderr,
proc);
}
/*** VIRTUAL FUNCTIONS ********************************************************/
static void process_init(fx_object *obj, void *priv)
{
}
static void process_fini(fx_object *obj, void *priv)
{
struct fx_process_p *proc = priv;
fx_array_unref(proc->proc_args);
fx_iostream_unref(proc->proc_stdin);
fx_iostream_unref(proc->proc_stdout);
fx_iostream_unref(proc->proc_stderr);
fx_string_unref(proc->proc_exec_path);
fx_hashtable_unref(proc->proc_environment);
}
/*** ITERATOR DEFINITION
* ******************************************************/
static enum fx_status process_iterator_move_next(const fx_iterator *obj)
{
return FX_ERR_NO_DATA;
}
static const fx_value *process_iterator_get_value(const fx_iterator *obj)
{
return NULL;
}
/*** CLASS DEFINITION
* *********************************************************/
FX_TYPE_CLASS_BEGIN(fx_process)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_END(fx_process)
FX_TYPE_DEFINITION_BEGIN(fx_process)
FX_TYPE_ID(0x7334594f, 0xa1c3, 0x4715, 0xab69, 0x68a709e1b64b);
FX_TYPE_NAME("fx.diagnostics.process");
FX_TYPE_CLASS(fx_process_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_process_p);
FX_TYPE_INSTANCE_INIT(process_init);
FX_TYPE_INSTANCE_FINI(process_fini);
FX_TYPE_DEFINITION_END(fx_process)
FX_TYPE_CLASS_BEGIN(fx_process_iterator)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
FX_INTERFACE_ENTRY(it_move_next) = process_iterator_move_next;
FX_INTERFACE_ENTRY(it_get_value) = process_iterator_get_value;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_END(fx_process_iterator)
FX_TYPE_DEFINITION_BEGIN(fx_process_iterator)
FX_TYPE_ID(0x67eb13b6, 0x25d1, 0x424a, 0xb136, 0x871a07829089);
FX_TYPE_NAME("fx.diagnostics.process.iterator");
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
FX_TYPE_CLASS(fx_process_iterator_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_process_iterator_p);
FX_TYPE_DEFINITION_END(fx_process_iterator)
+65
View File
@@ -0,0 +1,65 @@
#include <fx/diagnostics/process.h>
int main(int argc, const char **argv)
{
if (argc < 3) {
return -1;
}
const char *exec = argv[1];
const char *msg = argv[2];
const fx_value arg_values[] = {
FX_CSTR("hello"),
FX_CSTR("world"),
};
fx_array *args = fx_array_create_with_values(
arg_values,
sizeof arg_values / sizeof arg_values[0]);
fx_process *self = fx_process_get_self();
const fx_hashtable *env = fx_process_get_environment(self);
fx_process_start_info info = {
.proc_args = args,
.proc_redirect_stdout = true,
.proc_redirect_stdin = true,
.proc_file_name = exec,
.proc_environment = env,
};
fx_process *process = fx_process_create(&info);
if (!process) {
fprintf(stderr, "Process creation failed\n");
return -1;
}
fx_status status = fx_process_start(process);
if (!FX_OK(status)) {
fprintf(stderr,
"Process creation failed: %s\n",
fx_status_description(status));
return -1;
}
printf("process stdin: %s\n", msg);
fx_stream *in = fx_process_get_stdin(process);
fx_stream_write_cstr(in, msg, NULL);
fx_stream_write_char(in, '\n');
fx_process_close_stdin(process);
fx_stream *out = fx_process_get_stdout(process);
printf("process stdout:\n");
printf("---------------------\n");
while (FX_OK(fx_stream_read_line_s(out, fx_stdout))) { }
printf("---------------------\n");
int result;
fx_process_wait(process, &result);
fx_process_unref(process);
return 0;
}
+7 -12
View File
@@ -50,7 +50,7 @@ FX_API fx_type_id fx_directory_iterator_get_type(void);
FX_API fx_result fx_directory_open( FX_API fx_result fx_directory_open(
fx_directory *root, fx_directory *root,
const fx_path *path, fx_value path,
fx_directory_open_flags flags, fx_directory_open_flags flags,
fx_directory **out); fx_directory **out);
FX_API fx_result fx_directory_open_temp(fx_directory **out); FX_API fx_result fx_directory_open_temp(fx_directory **out);
@@ -60,22 +60,17 @@ 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 const char *fx_directory_get_rel_path_cstr(const fx_directory *dir);
FX_API fx_result fx_directory_delete(fx_directory *dir); FX_API fx_result fx_directory_delete(fx_directory *dir);
FX_API bool fx_directory_path_exists( FX_API bool fx_directory_path_exists(const fx_directory *root, fx_value path);
const fx_directory *root, FX_API bool fx_directory_path_is_file(const fx_directory *root, fx_value path);
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( FX_API bool fx_directory_path_is_directory(
const fx_directory *root, const fx_directory *root,
const fx_path *path); fx_value path);
FX_API fx_result fx_directory_path_stat( FX_API fx_result fx_directory_path_stat(
const fx_directory *root, const fx_directory *root,
const fx_path *path, fx_value path,
struct fx_file_info *out); struct fx_file_info *out);
FX_API fx_result fx_directory_path_unlink( FX_API fx_result
const fx_directory *root, fx_directory_path_unlink(const fx_directory *root, fx_value path);
const fx_path *path);
FX_API fx_iterator *fx_directory_begin( FX_API fx_iterator *fx_directory_begin(
fx_directory *dir, fx_directory *dir,
+2 -1
View File
@@ -5,6 +5,7 @@
#include <fx/macros.h> #include <fx/macros.h>
#include <fx/misc.h> #include <fx/misc.h>
#include <fx/stream.h> #include <fx/stream.h>
#include <fx/value.h>
FX_DECLS_BEGIN; FX_DECLS_BEGIN;
@@ -55,7 +56,7 @@ FX_API fx_type_id fx_file_get_type(void);
FX_API fx_result fx_file_open( FX_API fx_result fx_file_open(
FX_TYPE_FWDREF(fx_directory) * root, FX_TYPE_FWDREF(fx_directory) * root,
const FX_TYPE_FWDREF(fx_path) * path, fx_value path,
fx_file_mode mode, fx_file_mode mode,
fx_file **out); 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_temp(fx_file_mode mode, fx_file **out);
+11 -4
View File
@@ -18,18 +18,25 @@ FX_TYPE_CLASS_DECLARATION_END(fx_path)
#define FX_RV_PATH(cstr) FX_RV(fx_path_create_from_cstr(cstr)) #define FX_RV_PATH(cstr) FX_RV(fx_path_create_from_cstr(cstr))
typedef enum fx_system_path {
FX_PATH_ROOT = 0,
FX_PATH_CWD,
FX_PATH_HOME,
FX_PATH_CONFIG,
} fx_system_path;
struct fx_file_info; struct fx_file_info;
FX_API fx_type_id fx_path_get_type(void); FX_API fx_type_id fx_path_get_type(void);
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_path, FX_TYPE_PATH); 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_create_from_cstr(const char *path);
FX_API fx_path *fx_path_get_system(fx_system_path path_type);
FX_API fx_path *fx_path_duplicate(const fx_path *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_join_array(const fx_value *values[], size_t nr_values);
FX_API fx_path *fx_path_join_list(size_t count, ...);
FX_API fx_path *fx_path_make_absolute(const fx_path *in); 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_relative(const fx_path *in);
@@ -49,7 +56,7 @@ FX_API enum fx_status fx_path_get_filename(
const fx_path *path, const fx_path *path,
fx_string *out_name); fx_string *out_name);
FX_API const char *fx_path_ptr(const fx_path *path); FX_API const char *fx_path_get_cstr(const fx_path *path);
FX_API size_t fx_path_length(const fx_path *path); FX_API size_t fx_path_length(const fx_path *path);
FX_DECLS_END; FX_DECLS_END;
+8
View File
@@ -0,0 +1,8 @@
#ifndef FX_IO_PIPE_H_
#define FX_IO_PIPE_H_
#include <fx/io/stream.h>
FX_API fx_status fx_pipe_create(fx_iostream **reader, fx_iostream **writer);
#endif
+29
View File
@@ -0,0 +1,29 @@
#ifndef FX_IO_STREAM_H_
#define FX_IO_STREAM_H_
#include <fx/int.h>
#include <fx/macros.h>
#include <fx/stream.h>
FX_DECLS_BEGIN;
#define FX_IO_TYPE_STREAM (fx_iostream_get_type())
FX_DECLARE_TYPE(fx_iostream);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_iostream)
FX_TYPE_CLASS_DECLARATION_END(fx_iostream)
FX_API fx_type_id fx_iostream_get_type(void);
FX_API fx_iostream *fx_iostream_create(
fx_stream_mode mode,
uptr os_handle,
bool close_handle_on_release);
FX_API uptr fx_iostream_get_os_handle(const fx_iostream *stream);
FX_API uptr fx_iostream_steal_os_handle(fx_iostream *stream);
FX_DECLS_END;
#endif
+74 -82
View File
@@ -1,3 +1,5 @@
#define _POSIX_C_SOURCE 200809L
#include "misc.h" #include "misc.h"
#include "posix.h" #include "posix.h"
@@ -51,12 +53,12 @@ static const fx_path *directory_get_rel_path(const struct fx_directory_p *dir)
static const char *directory_get_path_cstr(const struct fx_directory_p *dir) static const char *directory_get_path_cstr(const struct fx_directory_p *dir)
{ {
return fx_path_ptr(dir->d_path_abs); return fx_path_get_cstr(dir->d_path_abs);
} }
static const char *directory_get_rel_path_cstr(const struct fx_directory_p *dir) static const char *directory_get_rel_path_cstr(const struct fx_directory_p *dir)
{ {
return fx_path_ptr(dir->d_path_rel); return fx_path_get_cstr(dir->d_path_rel);
} }
static fx_result directory_delete( static fx_result directory_delete(
@@ -65,9 +67,8 @@ static fx_result directory_delete(
{ {
enum fx_status status = FX_SUCCESS; enum fx_status status = FX_SUCCESS;
fx_iterator *it = fx_directory_begin( fx_iterator *it
dir, = fx_directory_begin(dir, FX_DIRECTORY_ITERATE_PARENT_LAST);
FX_DIRECTORY_ITERATE_PARENT_LAST);
while (FX_OK(fx_iterator_get_status(it))) { while (FX_OK(fx_iterator_get_status(it))) {
fx_iterator_erase(it); fx_iterator_erase(it);
} }
@@ -87,14 +88,12 @@ static fx_result directory_delete(
static bool directory_path_exists( static bool directory_path_exists(
const struct fx_directory_p *root, const struct fx_directory_p *root,
const fx_path *path) fx_value path)
{ {
const fx_path *parts[] = { fx_path *abs_path = fx_path_join_list(
root ? root->d_path_abs : NULL, 2,
path, &FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
}; &path);
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
if (!abs_path) { if (!abs_path) {
return false; return false;
} }
@@ -107,14 +106,14 @@ static bool directory_path_exists(
static bool directory_path_is_file( static bool directory_path_is_file(
const struct fx_directory_p *root, const struct fx_directory_p *root,
const fx_path *path) fx_value path)
{ {
const fx_path *parts[] = { const fx_value *parts[] = {};
root ? root->d_path_abs : NULL,
path,
};
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]); fx_path *abs_path = fx_path_join_list(
2,
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
&path);
if (!abs_path) { if (!abs_path) {
return false; return false;
} }
@@ -127,14 +126,14 @@ static bool directory_path_is_file(
static bool directory_path_is_directory( static bool directory_path_is_directory(
const struct fx_directory_p *root, const struct fx_directory_p *root,
const fx_path *path) fx_value path)
{ {
const fx_path *parts[] = { const fx_value *parts[] = {};
root ? root->d_path_abs : NULL,
path,
};
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]); fx_path *abs_path = fx_path_join_list(
2,
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
&path);
if (!abs_path) { if (!abs_path) {
return false; return false;
} }
@@ -147,15 +146,15 @@ static bool directory_path_is_directory(
static fx_result directory_path_stat( static fx_result directory_path_stat(
const struct fx_directory_p *root, const struct fx_directory_p *root,
const fx_path *path, fx_value path,
struct fx_file_info *out) struct fx_file_info *out)
{ {
const fx_path *parts[] = { const fx_value *parts[] = {};
root ? root->d_path_abs : NULL,
path,
};
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]); fx_path *abs_path = fx_path_join_list(
2,
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
&path);
if (!abs_path) { if (!abs_path) {
return FX_RESULT_ERR(NO_MEMORY); return FX_RESULT_ERR(NO_MEMORY);
} }
@@ -168,14 +167,14 @@ static fx_result directory_path_stat(
static fx_result directory_path_unlink( static fx_result directory_path_unlink(
const struct fx_directory_p *root, const struct fx_directory_p *root,
const fx_path *path) fx_value path)
{ {
const fx_path *parts[] = { const fx_value *parts[] = {};
root ? root->d_path_abs : NULL,
path,
};
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]); fx_path *abs_path = fx_path_join_list(
2,
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
&path);
if (!abs_path) { if (!abs_path) {
return FX_RESULT_ERR(NO_MEMORY); return FX_RESULT_ERR(NO_MEMORY);
} }
@@ -255,7 +254,7 @@ static fx_result create_directory_hierarchy(
static fx_result directory_open( static fx_result directory_open(
struct fx_directory_p *root, struct fx_directory_p *root,
const fx_path *path, fx_value path,
fx_directory_open_flags flags, fx_directory_open_flags flags,
fx_directory **out) fx_directory **out)
{ {
@@ -263,7 +262,10 @@ static fx_result directory_open(
int root_fd = root ? root->d_fd : AT_FDCWD; int root_fd = root ? root->d_fd : AT_FDCWD;
const char *path_cstr = fx_path_ptr(path); fx_stringstream *path_str = fx_stringstream_create();
fx_value_to_string(&path, path_str, NULL);
const char *path_cstr = fx_stringstream_get_cstr(path_str);
if (root) { if (root) {
while (*path_cstr == '/') { while (*path_cstr == '/') {
path_cstr++; path_cstr++;
@@ -293,21 +295,22 @@ static fx_result directory_open(
fx_directory *dir = fx_object_create(FX_TYPE_DIRECTORY); fx_directory *dir = fx_object_create(FX_TYPE_DIRECTORY);
fx_path *cwd = NULL; fx_path *cwd = NULL;
struct fx_directory_p *p = fx_object_get_private( struct fx_directory_p *p
dir, = fx_object_get_private(dir, FX_TYPE_DIRECTORY);
FX_TYPE_DIRECTORY);
if (!root) { if (!root) {
cwd = fx_path_create_cwd(); cwd = fx_path_get_system(FX_PATH_CWD);
} }
const fx_path *parts[] = { const fx_value *parts[] = {
root ? root->d_path_abs : cwd, &FX_VALUE_OBJECT(root ? root->d_path_abs : cwd),
path, &FX_CSTR(path_cstr),
}; };
fx_path *new_path = fx_path_join(parts, sizeof parts / sizeof parts[0]); fx_path *new_path
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
if (!new_path) { if (!new_path) {
fx_stringstream_unref(path_str);
return FX_RESULT_ERR(NO_MEMORY); return FX_RESULT_ERR(NO_MEMORY);
} }
@@ -316,7 +319,7 @@ static fx_result directory_open(
} }
p->d_path_abs = new_path; p->d_path_abs = new_path;
p->d_path_rel = fx_path_duplicate(path); p->d_path_rel = fx_path_create_from_cstr(path_cstr);
p->d_fd = fd; p->d_fd = fd;
if (flags & FX_DIRECTORY_OPEN_DELETE_ON_CLOSE) { if (flags & FX_DIRECTORY_OPEN_DELETE_ON_CLOSE) {
@@ -324,6 +327,7 @@ static fx_result directory_open(
} }
*out = dir; *out = dir;
fx_stringstream_unref(path_str);
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
@@ -332,7 +336,7 @@ static fx_result directory_open(
fx_result fx_directory_open( fx_result fx_directory_open(
fx_directory *root, fx_directory *root,
const fx_path *path, fx_value path,
fx_directory_open_flags flags, fx_directory_open_flags flags,
fx_directory **out) fx_directory **out)
{ {
@@ -353,20 +357,17 @@ fx_result fx_directory_open_temp(fx_directory **out)
while (1) { while (1) {
z__fx_io_generate_tmp_filename(name, sizeof name); z__fx_io_generate_tmp_filename(name, sizeof name);
snprintf(path, sizeof path, "/tmp/%s", name); snprintf(path, sizeof path, "/tmp/%s", name);
fx_path *rpath = fx_path_create_from_cstr(path);
fx_directory *dir = NULL; fx_directory *dir = NULL;
fx_result status = fx_directory_open( fx_result status = fx_directory_open(
FX_DIRECTORY_ROOT, FX_DIRECTORY_ROOT,
rpath, FX_CSTR(path),
FX_DIRECTORY_OPEN_CREATE, FX_DIRECTORY_OPEN_CREATE,
&dir); &dir);
struct fx_directory_p *p = fx_object_get_private( struct fx_directory_p *p
dir, = fx_object_get_private(dir, FX_TYPE_DIRECTORY);
FX_TYPE_DIRECTORY);
if (fx_error_get_status_code(status) == FX_ERR_NAME_EXISTS) { if (fx_error_get_status_code(status) == FX_ERR_NAME_EXISTS) {
fx_path_unref(rpath);
continue; continue;
} }
@@ -374,8 +375,8 @@ fx_result fx_directory_open_temp(fx_directory **out)
p->d_flags |= DIRECTORY_DELETE_ON_CLOSE; p->d_flags |= DIRECTORY_DELETE_ON_CLOSE;
} }
fx_path_unlink(rpath); *out = dir;
fx_path_unref(rpath); return status;
} }
} }
@@ -410,16 +411,15 @@ const char *fx_directory_get_rel_path_cstr(const fx_directory *dir)
fx_result fx_directory_delete(fx_directory *dir) fx_result fx_directory_delete(fx_directory *dir)
{ {
struct fx_directory_p *p = fx_object_get_private( struct fx_directory_p *p
dir, = fx_object_get_private(dir, FX_TYPE_DIRECTORY);
FX_TYPE_DIRECTORY);
p->d_flags |= DIRECTORY_DELETE_ON_CLOSE; p->d_flags |= DIRECTORY_DELETE_ON_CLOSE;
/* TODO allow object release functions to return a fx_result value */ /* TODO allow object release functions to return a fx_result value */
fx_directory_unref(dir); fx_directory_unref(dir);
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
bool fx_directory_path_exists(const fx_directory *root, const fx_path *path) bool fx_directory_path_exists(const fx_directory *root, fx_value path)
{ {
FX_CLASS_DISPATCH_STATIC( FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY, FX_TYPE_DIRECTORY,
@@ -428,7 +428,7 @@ bool fx_directory_path_exists(const fx_directory *root, const fx_path *path)
path); path);
} }
bool fx_directory_path_is_file(const fx_directory *root, const fx_path *path) bool fx_directory_path_is_file(const fx_directory *root, fx_value path)
{ {
FX_CLASS_DISPATCH_STATIC( FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY, FX_TYPE_DIRECTORY,
@@ -437,9 +437,7 @@ bool fx_directory_path_is_file(const fx_directory *root, const fx_path *path)
path); path);
} }
bool fx_directory_path_is_directory( bool fx_directory_path_is_directory(const fx_directory *root, fx_value path)
const fx_directory *root,
const fx_path *path)
{ {
FX_CLASS_DISPATCH_STATIC( FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY, FX_TYPE_DIRECTORY,
@@ -450,7 +448,7 @@ bool fx_directory_path_is_directory(
fx_result fx_directory_path_stat( fx_result fx_directory_path_stat(
const fx_directory *root, const fx_directory *root,
const fx_path *path, fx_value path,
struct fx_file_info *out) struct fx_file_info *out)
{ {
FX_CLASS_DISPATCH_STATIC( FX_CLASS_DISPATCH_STATIC(
@@ -461,9 +459,7 @@ fx_result fx_directory_path_stat(
out); out);
} }
fx_result fx_directory_path_unlink( fx_result fx_directory_path_unlink(const fx_directory *root, fx_value path)
const fx_directory *root,
const fx_path *path)
{ {
FX_CLASS_DISPATCH_STATIC( FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY, FX_TYPE_DIRECTORY,
@@ -560,9 +556,8 @@ fx_iterator *fx_directory_begin(
enum fx_directory_iterator_flags flags) enum fx_directory_iterator_flags flags)
{ {
fx_iterator *it_obj = fx_object_create(FX_TYPE_DIRECTORY_ITERATOR); fx_iterator *it_obj = fx_object_create(FX_TYPE_DIRECTORY_ITERATOR);
struct fx_directory_iterator_p *it = fx_object_get_private( struct fx_directory_iterator_p *it
it_obj, = fx_object_get_private(it_obj, FX_TYPE_DIRECTORY_ITERATOR);
FX_TYPE_DIRECTORY_ITERATOR);
it->flags = flags; it->flags = flags;
it->root = directory; it->root = directory;
@@ -571,7 +566,7 @@ fx_iterator *fx_directory_begin(
int fts_flags = FTS_COMFOLLOW | FTS_NOCHDIR; int fts_flags = FTS_COMFOLLOW | FTS_NOCHDIR;
const char *path_list[] = { const char *path_list[] = {
fx_path_ptr(it->_p->d_path_abs), fx_path_get_cstr(it->_p->d_path_abs),
NULL, NULL,
}; };
@@ -629,9 +624,8 @@ static const fx_iterator *iterator_begin(const fx_object *obj)
static enum fx_status iterator_move_next(const fx_iterator *obj) static enum fx_status iterator_move_next(const fx_iterator *obj)
{ {
struct fx_directory_iterator_p *it = fx_object_get_private( struct fx_directory_iterator_p *it
obj, = fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
FX_TYPE_DIRECTORY_ITERATOR);
if (!it || !it->fts) { if (!it || !it->fts) {
return FX_ERR_NO_DATA; return FX_ERR_NO_DATA;
} }
@@ -679,12 +673,11 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
static enum fx_status iterator_erase(fx_iterator *obj) static enum fx_status iterator_erase(fx_iterator *obj)
{ {
struct fx_directory_iterator_p *it = fx_object_get_private( struct fx_directory_iterator_p *it
obj, = fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
FX_TYPE_DIRECTORY_ITERATOR);
fx_result result = fx_directory_path_unlink( fx_result result = fx_directory_path_unlink(
it->root, it->root,
it->entry.filepath); FX_VALUE_OBJECT(it->entry.filepath));
if (fx_result_is_error(result)) { if (fx_result_is_error(result)) {
enum fx_status status = fx_error_get_status_code(result); enum fx_status status = fx_error_get_status_code(result);
fx_error_discard(result); fx_error_discard(result);
@@ -696,9 +689,8 @@ static enum fx_status iterator_erase(fx_iterator *obj)
static const fx_value *iterator_get_value(const fx_iterator *obj) static const fx_value *iterator_get_value(const fx_iterator *obj)
{ {
struct fx_directory_iterator_p *it = fx_object_get_private( struct fx_directory_iterator_p *it
obj, = fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
FX_TYPE_DIRECTORY_ITERATOR);
return &it->entry_value; return &it->entry_value;
} }
+27 -24
View File
@@ -1,3 +1,5 @@
#define _POSIX_C_SOURCE 200809L
#include "misc.h" #include "misc.h"
#include "posix.h" #include "posix.h"
@@ -84,13 +86,13 @@ static fx_result file_open_shadow(
= fx_path_create_from_cstr(fx_string_get_cstr(filename)); = fx_path_create_from_cstr(fx_string_get_cstr(filename));
fx_string_unref(filename); fx_string_unref(filename);
const fx_path *parts[] = { const fx_value *parts[] = {
dir, &FX_VALUE_OBJECT(dir),
shadow_filename, &FX_VALUE_OBJECT(shadow_filename),
}; };
fx_path *shadow_filepath fx_path *shadow_filepath
= fx_path_join(parts, sizeof parts / sizeof parts[0]); = fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
fx_path_unref(dir); fx_path_unref(dir);
fx_path_unref(shadow_filename); fx_path_unref(shadow_filename);
@@ -101,7 +103,7 @@ static fx_result file_open_shadow(
fx_file *shadow_file; fx_file *shadow_file;
fx_result status = fx_file_open( fx_result status = fx_file_open(
FX_DIRECTORY_ROOT, FX_DIRECTORY_ROOT,
shadow_filepath, FX_VALUE_OBJECT(shadow_filepath),
mode, mode,
&shadow_file); &shadow_file);
fx_path_unref(shadow_filepath); fx_path_unref(shadow_filepath);
@@ -227,12 +229,14 @@ static enum fx_status file_swap_shadow(
z__fx_io_generate_tmp_filename(tmp_name, sizeof 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); fx_path *tmp_name_p = fx_path_create_from_cstr(tmp_name);
const fx_path *parts[] = { const fx_value *parts[] = {
dir_path, &FX_VALUE_OBJECT(dir_path),
tmp_name_p, &FX_VALUE_OBJECT(tmp_name_p),
}; };
tmp_path = fx_path_join(parts, sizeof parts / sizeof parts[0]); tmp_path = fx_path_join_array(
parts,
sizeof parts / sizeof parts[0]);
fx_path_unref(tmp_name_p); fx_path_unref(tmp_name_p);
@@ -248,11 +252,15 @@ static enum fx_status file_swap_shadow(
int err; int err;
err = rename(fx_path_ptr(main_file->path), fx_path_ptr(tmp_path));
err = rename( err = rename(
fx_path_ptr(shadow_file->path), fx_path_get_cstr(main_file->path),
fx_path_ptr(main_file->path)); fx_path_get_cstr(tmp_path));
err = rename(fx_path_ptr(tmp_path), fx_path_ptr(shadow_file->path)); err = rename(
fx_path_get_cstr(shadow_file->path),
fx_path_get_cstr(main_file->path));
err = rename(
fx_path_get_cstr(tmp_path),
fx_path_get_cstr(shadow_file->path));
fx_path_unref(tmp_path); fx_path_unref(tmp_path);
@@ -406,11 +414,10 @@ static enum fx_status stream_tell(const fx_stream *stream, size_t *pos)
fx_result fx_file_open( fx_result fx_file_open(
fx_directory *root, fx_directory *root,
const fx_path *path, fx_value path,
enum fx_file_mode mode, enum fx_file_mode mode,
fx_file **out) fx_file **out)
{ {
const fx_path *file_path = path;
unsigned int flags = fx_mode_to_unix_mode(mode); unsigned int flags = fx_mode_to_unix_mode(mode);
if (flags == (unsigned int)-1) { if (flags == (unsigned int)-1) {
@@ -422,16 +429,12 @@ fx_result fx_file_open(
if (root) { if (root) {
root_path = fx_directory_get_path(root); root_path = fx_directory_get_path(root);
} else { } else {
root_path = fx_path_create_cwd(); root_path = fx_path_get_system(FX_PATH_CWD);
free_root_path = true; free_root_path = true;
} }
const fx_path *parts[] = { fx_path *abs_path
root_path, = fx_path_join_list(2, &FX_VALUE_OBJECT(root_path), &path);
file_path,
};
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
if (free_root_path) { if (free_root_path) {
fx_path_unref((fx_path *)root_path); fx_path_unref((fx_path *)root_path);
@@ -441,7 +444,7 @@ fx_result fx_file_open(
return FX_RESULT_ERR(NO_MEMORY); return FX_RESULT_ERR(NO_MEMORY);
} }
int fd = open(fx_path_ptr(abs_path), flags, 0644); int fd = open(fx_path_get_cstr(abs_path), flags, 0644);
if (fd == -1) { if (fd == -1) {
fx_path_unref(abs_path); fx_path_unref(abs_path);
return FX_RESULT_STATUS( return FX_RESULT_STATUS(
@@ -492,7 +495,7 @@ fx_result fx_file_open_temp(enum fx_file_mode mode, fx_file **out)
fx_result status = fx_file_open( fx_result status = fx_file_open(
FX_DIRECTORY_ROOT, FX_DIRECTORY_ROOT,
rpath, FX_VALUE_OBJECT(rpath),
mode | FX_FILE_CREATE_ONLY, mode | FX_FILE_CREATE_ONLY,
out); out);
+107 -54
View File
@@ -6,7 +6,6 @@
#include <fx/io/file.h> #include <fx/io/file.h>
#include <fx/io/path.h> #include <fx/io/path.h>
#include <fx/string.h> #include <fx/string.h>
#include <fx/value.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
@@ -92,22 +91,22 @@ static bool path_is_directory(const struct fx_path_p *path)
return (info.attrib & FX_FILE_ATTRIB_DIRECTORY) != 0; return (info.attrib & FX_FILE_ATTRIB_DIRECTORY) != 0;
} }
static void append_path(struct fx_path_p *dest, const struct fx_path_p *src) static void append_path(struct fx_path_p *dest, const fx_string *src)
{ {
if (path_is_absolute(src)) { const char *src_cstr = fx_string_get_cstr(src);
if (src_cstr[0] == '/') {
fx_string_clear(dest->p_pathstr); fx_string_clear(dest->p_pathstr);
fx_string_append_s(dest->p_pathstr, src->p_pathstr); fx_string_append_s(dest->p_pathstr, src);
return; return;
} }
if (fx_string_get_size(dest->p_pathstr, FX_STRLEN_NORMAL) > 0 if (fx_string_get_size(dest->p_pathstr, FX_STRLEN_NORMAL) > 0
&& fx_string_get_last_char(dest->p_pathstr) != '/' && fx_string_get_last_char(dest->p_pathstr) != '/'
&& fx_string_get_first_char(src->p_pathstr) != '/') { && fx_string_get_first_char(src) != '/') {
char s[] = {'/', 0}; fx_string_append_c(dest->p_pathstr, '/');
fx_string_append_cstr(dest->p_pathstr, s);
} }
fx_string_append_s(dest->p_pathstr, src->p_pathstr); fx_string_append_s(dest->p_pathstr, src);
} }
static enum fx_status path_unlink(const struct fx_path_p *path) static enum fx_status path_unlink(const struct fx_path_p *path)
@@ -135,8 +134,8 @@ static enum fx_status path_get_directory(
size_t dir_path_len = (size_t)(sep - path_cstr); 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_string *dir_path_s = fx_string_get_substr(path_str, 0, dir_path_len);
fx_path *dir_path = fx_path_create_from_cstr( fx_path *dir_path
fx_string_get_cstr(dir_path_s)); = fx_path_create_from_cstr(fx_string_get_cstr(dir_path_s));
fx_string_unref(dir_path_s); fx_string_unref(dir_path_s);
*out_dir_path = dir_path; *out_dir_path = dir_path;
@@ -200,33 +199,6 @@ fx_path *fx_path_create_root()
return path; return path;
} }
fx_path *fx_path_create_cwd()
{
const long buf_len = 2048;
char *buf = malloc(buf_len);
if (!buf) {
return NULL;
}
if (!getcwd(buf, buf_len)) {
free(buf);
return NULL;
}
fx_path *path = fx_path_create();
if (!path) {
free(buf);
return NULL;
}
struct fx_path_p *p = fx_object_get_private(path, FX_TYPE_PATH);
fx_string_append_cstr(p->p_pathstr, buf);
free(buf);
return path;
}
fx_path *fx_path_create_from_cstr(const char *cstr) fx_path *fx_path_create_from_cstr(const char *cstr)
{ {
fx_path *path = fx_path_create(); fx_path *path = fx_path_create();
@@ -259,6 +231,49 @@ fx_path *fx_path_create_from_cstr(const char *cstr)
return path; return path;
} }
fx_path *fx_path_get_system(fx_system_path path_type)
{
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);
}
return NULL;
}
fx_path *fx_path_duplicate(const fx_path *path) fx_path *fx_path_duplicate(const fx_path *path)
{ {
fx_path *new_path = fx_path_create(); fx_path *new_path = fx_path_create();
@@ -278,26 +293,63 @@ fx_path *fx_path_duplicate(const fx_path *path)
return new_path; return new_path;
} }
fx_path *fx_path_join(const fx_path *paths[], size_t nr_paths) fx_path *fx_path_join_array(const fx_value *values[], size_t nr_values)
{ {
fx_path *result = fx_path_create(); fx_path *result = fx_path_create();
if (!result) { if (!result) {
return NULL; return NULL;
} }
struct fx_path_p *result_p = fx_object_get_private( struct fx_path_p *result_p
result, = fx_object_get_private(result, FX_TYPE_PATH);
FX_TYPE_PATH);
for (size_t i = 0; i < nr_paths; i++) { fx_stringstream *tmp = fx_stringstream_create();
if (paths[i]) { fx_string *value_s = fx_string_create();
struct fx_path_p *path_p = fx_object_get_private( for (size_t i = 0; i < nr_values; i++) {
paths[i], if (!values[i]) {
FX_TYPE_PATH); continue;
append_path(result_p, path_p);
} }
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; return result;
} }
@@ -366,7 +418,7 @@ enum fx_status fx_path_get_filename(const fx_path *path, fx_string *out_name)
out_name); out_name);
} }
const char *fx_path_ptr(const fx_path *path) const char *fx_path_get_cstr(const fx_path *path)
{ {
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_ptr, path); FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_ptr, path);
} }
@@ -388,7 +440,7 @@ static void path_init(fx_object *obj, void *priv)
} }
} }
static void path_fini(fx_object *obj, void *priv) void path_fini(fx_object *obj, void *priv)
{ {
struct fx_path_p *path = priv; struct fx_path_p *path = priv;
@@ -400,12 +452,13 @@ static fx_status path_to_string(
fx_stream *out, fx_stream *out,
const char *format) const char *format)
{ {
struct fx_path_p *path = fx_object_get_private( struct fx_path_p *path
obj->v_object, = fx_object_get_private(obj->v_object, FX_TYPE_PATH);
FX_TYPE_PATH);
fx_stream_write_cstr(out, fx_string_get_cstr(path->p_pathstr), NULL); return fx_stream_write_cstr(
return FX_SUCCESS; out,
fx_string_get_cstr(path->p_pathstr),
NULL);
} }
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
+29
View File
@@ -0,0 +1,29 @@
#include "posix.h"
#include <errno.h>
#include <fx/io/stream.h>
#include <unistd.h>
fx_status fx_pipe_create(fx_iostream **reader, fx_iostream **writer)
{
int fds[2];
int err = pipe(fds);
if (err != 0) {
return fx_status_from_errno(errno, FX_ERR_NOT_SUPPORTED);
}
fx_iostream *r = fx_iostream_create(FX_STREAM_READ, fds[0], true);
fx_iostream *w = fx_iostream_create(FX_STREAM_WRITE, fds[1], true);
if (!r || !w) {
fx_iostream_unref(r);
fx_iostream_unref(w);
close(fds[0]);
close(fds[1]);
return FX_ERR_NO_MEMORY;
}
*reader = r;
*writer = w;
return FX_SUCCESS;
}
+149
View File
@@ -0,0 +1,149 @@
#include "posix.h"
#include <errno.h>
#include <fx/io/stream.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/*** PRIVATE DATA *************************************************************/
struct fx_iostream_p {
int s_fd;
bool s_fd_close_on_release;
};
/*** PRIVATE FUNCTIONS ********************************************************/
static uptr iostream_get_os_handle(const struct fx_iostream_p *stream)
{
return stream->s_fd;
}
static uptr iostream_steal_os_handle(struct fx_iostream_p *stream)
{
uptr out = stream->s_fd;
stream->s_fd = -1;
stream->s_fd_close_on_release = false;
return out;
}
/*** PUBLIC FUNCTIONS *********************************************************/
fx_iostream *fx_iostream_create(
fx_stream_mode mode,
uptr os_handle,
bool close_handle_on_release)
{
fx_iostream *s = fx_object_create(FX_IO_TYPE_STREAM);
if (!s) {
return NULL;
}
fx_stream_cfg *cfg = fx_object_get_protected(s, FX_TYPE_STREAM);
struct fx_iostream_p *p = fx_object_get_private(s, FX_IO_TYPE_STREAM);
cfg->s_mode = mode | Z__FX_STREAM_STATIC;
p->s_fd = (int)os_handle;
p->s_fd_close_on_release = close_handle_on_release;
return s;
}
uptr fx_iostream_get_os_handle(const fx_iostream *stream)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_IO_TYPE_STREAM,
iostream_get_os_handle,
stream);
}
uptr fx_iostream_steal_os_handle(fx_iostream *stream)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_IO_TYPE_STREAM,
iostream_steal_os_handle,
stream);
}
/*** VIRTUAL FUNCTIONS ********************************************************/
static void iostream_init(fx_object *obj, void *priv)
{
struct fx_iostream_p *stream = priv;
}
static void iostream_fini(fx_object *obj, void *priv)
{
struct fx_iostream_p *stream = priv;
if (stream->s_fd_close_on_release) {
close(stream->s_fd);
}
}
enum fx_status stream_read(
fx_stream *stream,
void *buf,
size_t count,
size_t *nr_read)
{
struct fx_iostream_p *s
= fx_object_get_private(stream, FX_IO_TYPE_STREAM);
long r = read(s->s_fd, buf, count);
if (r < 0) {
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
*nr_read = r;
return FX_SUCCESS;
}
enum fx_status stream_write(
fx_stream *stream,
const void *buf,
size_t count,
size_t *nr_written)
{
struct fx_iostream_p *s
= fx_object_get_private(stream, FX_IO_TYPE_STREAM);
long w = write(s->s_fd, buf, count);
if (w < 0) {
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
*nr_written = w;
return FX_SUCCESS;
}
/*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_BEGIN(fx_iostream)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_stream, FX_TYPE_STREAM)
FX_INTERFACE_ENTRY(s_close) = NULL;
FX_INTERFACE_ENTRY(s_seek) = NULL;
FX_INTERFACE_ENTRY(s_tell) = NULL;
FX_INTERFACE_ENTRY(s_getc) = NULL;
FX_INTERFACE_ENTRY(s_read) = stream_read;
FX_INTERFACE_ENTRY(s_write) = stream_write;
FX_INTERFACE_ENTRY(s_reserve) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_stream, FX_TYPE_STREAM)
FX_TYPE_CLASS_END(fx_iostream)
FX_TYPE_DEFINITION_BEGIN(fx_iostream)
FX_TYPE_ID(0xc0b1c3c9, 0xa9c6, 0x4910, 0x8786, 0x574b0696e7aa);
FX_TYPE_NAME("fx.io.stream");
FX_TYPE_EXTENDS(FX_TYPE_STREAM);
FX_TYPE_CLASS(fx_iostream_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_iostream_p);
FX_TYPE_INSTANCE_INIT(iostream_init);
FX_TYPE_INSTANCE_FINI(iostream_fini);
FX_TYPE_DEFINITION_END(fx_iostream)
+35 -29
View File
@@ -53,12 +53,12 @@ static const fx_path *directory_get_rel_path(const struct fx_directory_p *dir)
static const char *directory_get_path_cstr(const struct fx_directory_p *dir) static const char *directory_get_path_cstr(const struct fx_directory_p *dir)
{ {
return fx_path_ptr(dir->d_path_abs); return fx_path_get_cstr(dir->d_path_abs);
} }
static const char *directory_get_rel_path_cstr(const struct fx_directory_p *dir) static const char *directory_get_rel_path_cstr(const struct fx_directory_p *dir)
{ {
return fx_path_ptr(dir->d_path_rel); return fx_path_get_cstr(dir->d_path_rel);
} }
static fx_result directory_delete( static fx_result directory_delete(
@@ -90,12 +90,13 @@ static bool directory_path_exists(
const struct fx_directory_p *root, const struct fx_directory_p *root,
const fx_path *path) const fx_path *path)
{ {
const fx_path *parts[] = { const fx_value *parts[] = {
root ? root->d_path_abs : NULL, &FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
path, &FX_VALUE_OBJECT(path),
}; };
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]); fx_path *abs_path
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
if (!abs_path) { if (!abs_path) {
return false; return false;
} }
@@ -110,12 +111,13 @@ static bool directory_path_is_file(
const struct fx_directory_p *root, const struct fx_directory_p *root,
const fx_path *path) const fx_path *path)
{ {
const fx_path *parts[] = { const fx_value *parts[] = {
root ? root->d_path_abs : NULL, &FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
path, &FX_VALUE_OBJECT(path),
}; };
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]); fx_path *abs_path
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
if (!abs_path) { if (!abs_path) {
return false; return false;
} }
@@ -130,12 +132,13 @@ static bool directory_path_is_directory(
const struct fx_directory_p *root, const struct fx_directory_p *root,
const fx_path *path) const fx_path *path)
{ {
const fx_path *parts[] = { const fx_value *parts[] = {
root ? root->d_path_abs : NULL, &FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
path, &FX_VALUE_OBJECT(path),
}; };
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]); fx_path *abs_path
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
if (!abs_path) { if (!abs_path) {
return false; return false;
} }
@@ -151,12 +154,13 @@ static fx_result directory_path_stat(
const fx_path *path, const fx_path *path,
struct fx_file_info *out) struct fx_file_info *out)
{ {
const fx_path *parts[] = { const fx_value *parts[] = {
root ? root->d_path_abs : NULL, &FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
path, &FX_VALUE_OBJECT(path),
}; };
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]); fx_path *abs_path
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
if (!abs_path) { if (!abs_path) {
return FX_RESULT_ERR(NO_MEMORY); return FX_RESULT_ERR(NO_MEMORY);
} }
@@ -171,12 +175,13 @@ static fx_result directory_path_unlink(
const struct fx_directory_p *root, const struct fx_directory_p *root,
const fx_path *path) const fx_path *path)
{ {
const fx_path *parts[] = { const fx_value *parts[] = {
root ? root->d_path_abs : NULL, &FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
path, &FX_VALUE_OBJECT(path),
}; };
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]); fx_path *abs_path
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
if (!abs_path) { if (!abs_path) {
return FX_RESULT_ERR(NO_MEMORY); return FX_RESULT_ERR(NO_MEMORY);
} }
@@ -264,7 +269,7 @@ static fx_result directory_open(
int root_fd = root ? root->d_fd : AT_FDCWD; int root_fd = root ? root->d_fd : AT_FDCWD;
const char *path_cstr = fx_path_ptr(path); const char *path_cstr = fx_path_get_cstr(path);
if (root) { if (root) {
while (*path_cstr == '/') { while (*path_cstr == '/') {
path_cstr++; path_cstr++;
@@ -298,15 +303,16 @@ static fx_result directory_open(
= fx_object_get_private(dir, FX_TYPE_DIRECTORY); = fx_object_get_private(dir, FX_TYPE_DIRECTORY);
if (!root) { if (!root) {
cwd = fx_path_create_cwd(); cwd = fx_path_get_system(FX_PATH_CWD);
} }
const fx_path *parts[] = { const fx_value *parts[] = {
root ? root->d_path_abs : cwd, &FX_VALUE_OBJECT(root ? root->d_path_abs : cwd),
path, &FX_VALUE_OBJECT(path),
}; };
fx_path *new_path = fx_path_join(parts, sizeof parts / sizeof parts[0]); fx_path *new_path
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
if (!new_path) { if (!new_path) {
return FX_RESULT_ERR(NO_MEMORY); return FX_RESULT_ERR(NO_MEMORY);
} }
@@ -570,7 +576,7 @@ fx_iterator *fx_directory_begin(
int fts_flags = FTS_COMFOLLOW | FTS_NOCHDIR; int fts_flags = FTS_COMFOLLOW | FTS_NOCHDIR;
const char *path_list[] = { const char *path_list[] = {
fx_path_ptr(it->_p->d_path_abs), fx_path_get_cstr(it->_p->d_path_abs),
NULL, NULL,
}; };
+25 -18
View File
@@ -86,13 +86,13 @@ static fx_result file_open_shadow(
= fx_path_create_from_cstr(fx_string_get_cstr(filename)); = fx_path_create_from_cstr(fx_string_get_cstr(filename));
fx_string_unref(filename); fx_string_unref(filename);
const fx_path *parts[] = { const fx_value *parts[] = {
dir, &FX_VALUE_OBJECT(dir),
shadow_filename, &FX_VALUE_OBJECT(shadow_filename),
}; };
fx_path *shadow_filepath fx_path *shadow_filepath
= fx_path_join(parts, sizeof parts / sizeof parts[0]); = fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
fx_path_unref(dir); fx_path_unref(dir);
fx_path_unref(shadow_filename); fx_path_unref(shadow_filename);
@@ -229,12 +229,14 @@ static enum fx_status file_swap_shadow(
z__fx_io_generate_tmp_filename(tmp_name, sizeof 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); fx_path *tmp_name_p = fx_path_create_from_cstr(tmp_name);
const fx_path *parts[] = { const fx_value *parts[] = {
dir_path, &FX_VALUE_OBJECT(dir_path),
tmp_name_p, &FX_VALUE_OBJECT(tmp_name_p),
}; };
tmp_path = fx_path_join(parts, sizeof parts / sizeof parts[0]); tmp_path = fx_path_join_array(
parts,
sizeof parts / sizeof parts[0]);
fx_path_unref(tmp_name_p); fx_path_unref(tmp_name_p);
@@ -250,11 +252,15 @@ static enum fx_status file_swap_shadow(
int err; int err;
err = rename(fx_path_ptr(main_file->path), fx_path_ptr(tmp_path));
err = rename( err = rename(
fx_path_ptr(shadow_file->path), fx_path_get_cstr(main_file->path),
fx_path_ptr(main_file->path)); fx_path_get_cstr(tmp_path));
err = rename(fx_path_ptr(tmp_path), fx_path_ptr(shadow_file->path)); err = rename(
fx_path_get_cstr(shadow_file->path),
fx_path_get_cstr(main_file->path));
err = rename(
fx_path_get_cstr(tmp_path),
fx_path_get_cstr(shadow_file->path));
fx_path_unref(tmp_path); fx_path_unref(tmp_path);
@@ -424,16 +430,17 @@ fx_result fx_file_open(
if (root) { if (root) {
root_path = fx_directory_get_path(root); root_path = fx_directory_get_path(root);
} else { } else {
root_path = fx_path_create_cwd(); root_path = fx_path_get_system(FX_PATH_CWD);
free_root_path = true; free_root_path = true;
} }
const fx_path *parts[] = { const fx_value *parts[] = {
root_path, &FX_VALUE_OBJECT(root_path),
file_path, &FX_VALUE_OBJECT(file_path),
}; };
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]); fx_path *abs_path
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
if (free_root_path) { if (free_root_path) {
fx_path_unref((fx_path *)root_path); fx_path_unref((fx_path *)root_path);
@@ -443,7 +450,7 @@ fx_result fx_file_open(
return FX_RESULT_ERR(NO_MEMORY); return FX_RESULT_ERR(NO_MEMORY);
} }
int fd = open(fx_path_ptr(abs_path), flags, 0644); int fd = open(fx_path_get_cstr(abs_path), flags, 0644);
if (fd == -1) { if (fd == -1) {
fx_path_unref(abs_path); fx_path_unref(abs_path);
return FX_RESULT_STATUS( return FX_RESULT_STATUS(
+96 -41
View File
@@ -91,22 +91,22 @@ static bool path_is_directory(const struct fx_path_p *path)
return (info.attrib & FX_FILE_ATTRIB_DIRECTORY) != 0; return (info.attrib & FX_FILE_ATTRIB_DIRECTORY) != 0;
} }
static void append_path(struct fx_path_p *dest, const struct fx_path_p *src) static void append_path(struct fx_path_p *dest, const fx_string *src)
{ {
if (path_is_absolute(src)) { const char *src_cstr = fx_string_get_cstr(src);
if (src_cstr[0] == '/') {
fx_string_clear(dest->p_pathstr); fx_string_clear(dest->p_pathstr);
fx_string_append_s(dest->p_pathstr, src->p_pathstr); fx_string_append_s(dest->p_pathstr, src);
return; return;
} }
if (fx_string_get_size(dest->p_pathstr, FX_STRLEN_NORMAL) > 0 if (fx_string_get_size(dest->p_pathstr, FX_STRLEN_NORMAL) > 0
&& fx_string_get_last_char(dest->p_pathstr) != '/' && fx_string_get_last_char(dest->p_pathstr) != '/'
&& fx_string_get_first_char(src->p_pathstr) != '/') { && fx_string_get_first_char(src) != '/') {
char s[] = {'/', 0}; fx_string_append_c(dest->p_pathstr, '/');
fx_string_append_cstr(dest->p_pathstr, s);
} }
fx_string_append_s(dest->p_pathstr, src->p_pathstr); fx_string_append_s(dest->p_pathstr, src);
} }
static enum fx_status path_unlink(const struct fx_path_p *path) static enum fx_status path_unlink(const struct fx_path_p *path)
@@ -199,33 +199,6 @@ fx_path *fx_path_create_root()
return path; return path;
} }
fx_path *fx_path_create_cwd()
{
const long buf_len = 2048;
char *buf = malloc(buf_len);
if (!buf) {
return NULL;
}
if (!getcwd(buf, buf_len)) {
free(buf);
return NULL;
}
fx_path *path = fx_path_create();
if (!path) {
free(buf);
return NULL;
}
struct fx_path_p *p = fx_object_get_private(path, FX_TYPE_PATH);
fx_string_append_cstr(p->p_pathstr, buf);
free(buf);
return path;
}
fx_path *fx_path_create_from_cstr(const char *cstr) fx_path *fx_path_create_from_cstr(const char *cstr)
{ {
fx_path *path = fx_path_create(); fx_path *path = fx_path_create();
@@ -258,6 +231,49 @@ fx_path *fx_path_create_from_cstr(const char *cstr)
return path; return path;
} }
fx_path *fx_path_get_system(fx_system_path path_type)
{
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);
}
return NULL;
}
fx_path *fx_path_duplicate(const fx_path *path) fx_path *fx_path_duplicate(const fx_path *path)
{ {
fx_path *new_path = fx_path_create(); fx_path *new_path = fx_path_create();
@@ -277,7 +293,7 @@ fx_path *fx_path_duplicate(const fx_path *path)
return new_path; return new_path;
} }
fx_path *fx_path_join(const fx_path *paths[], size_t nr_paths) fx_path *fx_path_join_array(const fx_value *values[], size_t nr_values)
{ {
fx_path *result = fx_path_create(); fx_path *result = fx_path_create();
if (!result) { if (!result) {
@@ -287,14 +303,53 @@ fx_path *fx_path_join(const fx_path *paths[], size_t nr_paths)
struct fx_path_p *result_p struct fx_path_p *result_p
= fx_object_get_private(result, FX_TYPE_PATH); = fx_object_get_private(result, FX_TYPE_PATH);
for (size_t i = 0; i < nr_paths; i++) { fx_stringstream *tmp = fx_stringstream_create();
if (paths[i]) { fx_string *value_s = fx_string_create();
struct fx_path_p *path_p for (size_t i = 0; i < nr_values; i++) {
= fx_object_get_private(paths[i], FX_TYPE_PATH); if (!values[i]) {
append_path(result_p, path_p); 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; return result;
} }
@@ -363,7 +418,7 @@ enum fx_status fx_path_get_filename(const fx_path *path, fx_string *out_name)
out_name); out_name);
} }
const char *fx_path_ptr(const fx_path *path) const char *fx_path_get_cstr(const fx_path *path)
{ {
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_ptr, path); FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_ptr, path);
} }
+29
View File
@@ -0,0 +1,29 @@
#include "posix.h"
#include <errno.h>
#include <fx/io/stream.h>
#include <unistd.h>
fx_status fx_pipe_create(fx_iostream **reader, fx_iostream **writer)
{
int fds[2];
int err = pipe(fds);
if (err != 0) {
return fx_status_from_errno(errno, FX_ERR_NOT_SUPPORTED);
}
fx_iostream *r = fx_iostream_create(FX_STREAM_READ, fds[0], true);
fx_iostream *w = fx_iostream_create(FX_STREAM_WRITE, fds[1], true);
if (!r || !w) {
fx_iostream_unref(r);
fx_iostream_unref(w);
close(fds[0]);
close(fds[1]);
return FX_ERR_NO_MEMORY;
}
*reader = r;
*writer = w;
return FX_SUCCESS;
}
+149
View File
@@ -0,0 +1,149 @@
#include "posix.h"
#include <errno.h>
#include <fx/io/stream.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/*** PRIVATE DATA *************************************************************/
struct fx_iostream_p {
int s_fd;
bool s_fd_close_on_release;
};
/*** PRIVATE FUNCTIONS ********************************************************/
static uptr iostream_get_os_handle(const struct fx_iostream_p *stream)
{
return stream->s_fd;
}
static uptr iostream_steal_os_handle(struct fx_iostream_p *stream)
{
uptr out = stream->s_fd;
stream->s_fd = -1;
stream->s_fd_close_on_release = false;
return out;
}
/*** PUBLIC FUNCTIONS *********************************************************/
fx_iostream *fx_iostream_create(
fx_stream_mode mode,
uptr os_handle,
bool close_handle_on_release)
{
fx_iostream *s = fx_object_create(FX_IO_TYPE_STREAM);
if (!s) {
return NULL;
}
fx_stream_cfg *cfg = fx_object_get_protected(s, FX_TYPE_STREAM);
struct fx_iostream_p *p = fx_object_get_private(s, FX_IO_TYPE_STREAM);
cfg->s_mode = mode | Z__FX_STREAM_STATIC;
p->s_fd = (int)os_handle;
p->s_fd_close_on_release = close_handle_on_release;
return s;
}
uptr fx_iostream_get_os_handle(const fx_iostream *stream)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_IO_TYPE_STREAM,
iostream_get_os_handle,
stream);
}
uptr fx_iostream_steal_os_handle(fx_iostream *stream)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_IO_TYPE_STREAM,
iostream_steal_os_handle,
stream);
}
/*** VIRTUAL FUNCTIONS ********************************************************/
static void iostream_init(fx_object *obj, void *priv)
{
struct fx_iostream_p *stream = priv;
}
static void iostream_fini(fx_object *obj, void *priv)
{
struct fx_iostream_p *stream = priv;
if (stream->s_fd_close_on_release) {
close(stream->s_fd);
}
}
static enum fx_status stream_read(
fx_stream *stream,
void *buf,
size_t count,
size_t *nr_read)
{
struct fx_iostream_p *s
= fx_object_get_private(stream, FX_IO_TYPE_STREAM);
long r = read(s->s_fd, buf, count);
if (r < 0) {
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
*nr_read = r;
return FX_SUCCESS;
}
static enum fx_status stream_write(
fx_stream *stream,
const void *buf,
size_t count,
size_t *nr_written)
{
struct fx_iostream_p *s
= fx_object_get_private(stream, FX_IO_TYPE_STREAM);
long w = write(s->s_fd, buf, count);
if (w < 0) {
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
*nr_written = w;
return FX_SUCCESS;
}
/*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_BEGIN(fx_iostream)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_stream, FX_TYPE_STREAM)
FX_INTERFACE_ENTRY(s_close) = NULL;
FX_INTERFACE_ENTRY(s_seek) = NULL;
FX_INTERFACE_ENTRY(s_tell) = NULL;
FX_INTERFACE_ENTRY(s_getc) = NULL;
FX_INTERFACE_ENTRY(s_read) = stream_read;
FX_INTERFACE_ENTRY(s_write) = stream_write;
FX_INTERFACE_ENTRY(s_reserve) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_stream, FX_TYPE_STREAM)
FX_TYPE_CLASS_END(fx_iostream)
FX_TYPE_DEFINITION_BEGIN(fx_iostream)
FX_TYPE_ID(0xc0b1c3c9, 0xa9c6, 0x4910, 0x8786, 0x574b0696e7aa);
FX_TYPE_NAME("fx.io.stream");
FX_TYPE_EXTENDS(FX_TYPE_STREAM);
FX_TYPE_CLASS(fx_iostream_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_iostream_p);
FX_TYPE_INSTANCE_INIT(iostream_init);
FX_TYPE_INSTANCE_FINI(iostream_fini);
FX_TYPE_DEFINITION_END(fx_iostream)
+759
View File
@@ -0,0 +1,759 @@
#define _POSIX_C_SOURCE 200809L
#include "misc.h"
#include "posix.h"
#include <errno.h>
#include <fcntl.h>
#include <fx/error.h>
#include <fx/io/directory.h>
#include <fx/string.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/*** PRIVATE DATA *************************************************************/
enum directory_flags {
DIRECTORY_DELETE_ON_CLOSE = 0x01u,
};
struct fx_directory_p {
enum directory_flags d_flags;
int d_fd;
fx_path *d_path_rel;
fx_path *d_path_abs;
};
struct fx_directory_iterator_p {
struct fx_directory_p *_p;
#if 0
FTS *fts;
FTSENT *ent;
#endif
fx_directory_iterator_flags flags;
fx_directory *root;
fx_directory_entry entry;
fx_value entry_value;
};
/*** PRIVATE FUNCTIONS ********************************************************/
static const fx_path *directory_get_path(const struct fx_directory_p *dir)
{
return dir->d_path_abs;
}
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 fx_directory_p *dir)
{
return fx_path_get_cstr(dir->d_path_abs);
}
static const char *directory_get_rel_path_cstr(const struct fx_directory_p *dir)
{
return fx_path_get_cstr(dir->d_path_rel);
}
static fx_result directory_delete(
fx_directory *dir,
struct fx_directory_p *dir_p)
{
enum fx_status status = FX_SUCCESS;
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 = fx_iterator_get_status(it);
if (!FX_OK(status) && status != FX_ERR_NO_DATA) {
return FX_RESULT_STATUS(status);
}
status = fx_path_unlink(dir_p->d_path_abs);
if (!FX_OK(status)) {
return FX_RESULT_STATUS(status);
}
return FX_RESULT_SUCCESS;
}
static bool directory_path_exists(
const struct fx_directory_p *root,
fx_value path)
{
fx_path *abs_path = fx_path_join_list(
2,
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
&path);
if (!abs_path) {
return false;
}
bool result = fx_path_exists(abs_path);
fx_path_unref(abs_path);
return result;
}
static bool directory_path_is_file(
const struct fx_directory_p *root,
fx_value path)
{
const fx_value *parts[] = {};
fx_path *abs_path = fx_path_join_list(
2,
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
&path);
if (!abs_path) {
return false;
}
bool result = fx_path_is_file(abs_path);
fx_path_unref(abs_path);
return result;
}
static bool directory_path_is_directory(
const struct fx_directory_p *root,
fx_value path)
{
const fx_value *parts[] = {};
fx_path *abs_path = fx_path_join_list(
2,
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
&path);
if (!abs_path) {
return false;
}
bool result = fx_path_is_directory(abs_path);
fx_path_unref(abs_path);
return result;
}
static fx_result directory_path_stat(
const struct fx_directory_p *root,
fx_value path,
struct fx_file_info *out)
{
const fx_value *parts[] = {};
fx_path *abs_path = fx_path_join_list(
2,
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
&path);
if (!abs_path) {
return FX_RESULT_ERR(NO_MEMORY);
}
enum fx_status status = fx_path_stat(abs_path, out);
fx_path_unref(abs_path);
return FX_RESULT_STATUS(status);
}
static fx_result directory_path_unlink(
const struct fx_directory_p *root,
fx_value path)
{
const fx_value *parts[] = {};
fx_path *abs_path = fx_path_join_list(
2,
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
&path);
if (!abs_path) {
return FX_RESULT_ERR(NO_MEMORY);
}
enum fx_status status = fx_path_unlink(abs_path);
fx_path_unref(abs_path);
return FX_RESULT_STATUS(status);
}
static fx_result create_directory(struct fx_directory_p *root, const char *path)
{
#if 0
int root_fd = root ? root->d_fd : -1;
int err;
if (root_fd == -1) {
err = mkdir(path, 0755);
} else {
err = mkdirat(root_fd, path, 0755);
}
if (err == 0 || errno == EEXIST) {
return FX_RESULT_SUCCESS;
}
return fx_result_from_errno_with_subfilepath(
errno,
path,
directory_get_rel_path_cstr(root),
FX_ERR_IO_FAILURE);
#endif
return FX_RESULT_ERR(NOT_SUPPORTED);
}
static fx_result create_directory_hierarchy(
struct fx_directory_p *root,
const char *path)
{
#if 0
int root_fd = root ? root->d_fd : AT_FDCWD;
char *path_buf = fx_strdup(path);
if (!path_buf) {
return FX_RESULT_ERR(NO_MEMORY);
}
fx_result result = FX_RESULT_SUCCESS;
for (size_t i = 0; path_buf[i]; i++) {
if (path_buf[i] != '/') {
continue;
}
path_buf[i] = 0;
int err = mkdirat(root_fd, path_buf, 0755);
if (err != 0 && errno != EEXIST) {
result = fx_result_from_errno_with_subfilepath(
errno,
path_buf,
directory_get_rel_path_cstr(root),
FX_ERR_IO_FAILURE);
break;
}
path_buf[i] = '/';
}
int err = mkdirat(root_fd, path_buf, 0755);
if (err != 0 && errno != EEXIST) {
result = fx_result_from_errno_with_subfilepath(
errno,
path_buf,
directory_get_rel_path_cstr(root),
FX_ERR_IO_FAILURE);
}
free(path_buf);
return result;
#endif
return FX_RESULT_ERR(NOT_SUPPORTED);
}
static fx_result directory_open(
struct fx_directory_p *root,
fx_value path,
fx_directory_open_flags flags,
fx_directory **out)
{
#if 0
enum fx_status status = FX_SUCCESS;
int root_fd = root ? root->d_fd : AT_FDCWD;
fx_stringstream *path_str = fx_stringstream_create();
fx_value_to_string(&path, path_str, NULL);
const char *path_cstr = fx_stringstream_get_cstr(path_str);
if (root) {
while (*path_cstr == '/') {
path_cstr++;
}
}
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 & FX_DIRECTORY_OPEN_CREATE)
== FX_DIRECTORY_OPEN_CREATE) {
result = create_directory(root, path_cstr);
}
if (fx_result_is_error(result)) {
return fx_result_propagate(result);
}
int fd = openat(root_fd, path_cstr, O_DIRECTORY);
if (fd == -1) {
status = fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
return FX_RESULT_STATUS(status);
}
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 = fx_path_get_system(FX_PATH_CWD);
}
const fx_value *parts[] = {
&FX_VALUE_OBJECT(root ? root->d_path_abs : cwd),
&FX_CSTR(path_cstr),
};
fx_path *new_path
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
if (!new_path) {
fx_stringstream_unref(path_str);
return FX_RESULT_ERR(NO_MEMORY);
}
if (cwd) {
fx_path_unref(cwd);
}
p->d_path_abs = new_path;
p->d_path_rel = fx_path_create_from_cstr(path_cstr);
p->d_fd = fd;
if (flags & FX_DIRECTORY_OPEN_DELETE_ON_CLOSE) {
p->d_flags = DIRECTORY_DELETE_ON_CLOSE;
}
*out = dir;
fx_stringstream_unref(path_str);
return FX_RESULT_SUCCESS;
#endif
return FX_RESULT_ERR(NOT_SUPPORTED);
}
/*** PUBLIC FUNCTIONS *********************************************************/
fx_result fx_directory_open(
fx_directory *root,
fx_value path,
fx_directory_open_flags flags,
fx_directory **out)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY,
directory_open,
root,
path,
flags,
out);
}
fx_result fx_directory_open_temp(fx_directory **out)
{
char name[16];
char path[128];
while (1) {
z__fx_io_generate_tmp_filename(name, sizeof name);
snprintf(path, sizeof path, "/tmp/%s", name);
fx_directory *dir = NULL;
fx_result status = fx_directory_open(
FX_DIRECTORY_ROOT,
FX_CSTR(path),
FX_DIRECTORY_OPEN_CREATE,
&dir);
struct fx_directory_p *p
= fx_object_get_private(dir, FX_TYPE_DIRECTORY);
if (fx_error_get_status_code(status) == FX_ERR_NAME_EXISTS) {
continue;
}
if (dir) {
p->d_flags |= DIRECTORY_DELETE_ON_CLOSE;
}
*out = dir;
return status;
}
}
const fx_path *fx_directory_get_path(const fx_directory *dir)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DIRECTORY, directory_get_path, dir);
}
const fx_path *fx_directory_get_rel_path(const fx_directory *dir)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_DIRECTORY,
directory_get_rel_path,
dir);
}
const char *fx_directory_get_path_cstr(const fx_directory *dir)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_DIRECTORY,
directory_get_path_cstr,
dir);
}
const char *fx_directory_get_rel_path_cstr(const fx_directory *dir)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_DIRECTORY,
directory_get_rel_path_cstr,
dir);
}
fx_result fx_directory_delete(fx_directory *dir)
{
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 fx_result value */
fx_directory_unref(dir);
return FX_RESULT_SUCCESS;
}
bool fx_directory_path_exists(const fx_directory *root, fx_value path)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY,
directory_path_exists,
root,
path);
}
bool fx_directory_path_is_file(const fx_directory *root, fx_value path)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY,
directory_path_is_file,
root,
path);
}
bool fx_directory_path_is_directory(const fx_directory *root, fx_value path)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY,
directory_path_is_directory,
root,
path);
}
fx_result fx_directory_path_stat(
const fx_directory *root,
fx_value path,
struct fx_file_info *out)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY,
directory_path_stat,
root,
path,
out);
}
fx_result fx_directory_path_unlink(const fx_directory *root, fx_value path)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY,
directory_path_unlink,
root,
path);
}
/*** VIRTUAL FUNCTIONS ********************************************************/
static void directory_init(fx_object *obj, void *priv)
{
struct fx_directory_p *dir = priv;
}
static void directory_fini(fx_object *obj, void *priv)
{
struct fx_directory_p *dir = priv;
close(dir->d_fd);
if (dir->d_flags & DIRECTORY_DELETE_ON_CLOSE) {
directory_delete(obj, dir);
}
fx_path_unref(dir->d_path_abs);
}
/*** ITERATOR FUNCTIONS *******************************************************/
#if 0
static int ftsent_compare(const FTSENT **one, const FTSENT **two)
{
return (strcmp((*one)->fts_name, (*two)->fts_name));
}
#endif
static void update_iterator_data(struct fx_directory_iterator_p *it)
{
if (it->entry.filepath) {
fx_path_unref((fx_path *)it->entry.filepath);
it->entry.filepath = NULL;
}
#if 0
FTSENT *ent = it->ent;
fx_path *path = fx_path_create_from_cstr(
ent->fts_path + fx_path_length(it->_p->d_path_abs) + 1);
fx_value_unset(&it->entry_value);
it->entry_value = FX_POINTER(&it->entry);
it->entry.filename = ent->fts_name;
it->entry.filepath = path;
memset(&it->entry.info, 0x0, sizeof it->entry.info);
it->entry.info.length = ent->fts_statp->st_size;
if (S_ISREG(ent->fts_statp->st_mode)) {
it->entry.info.attrib |= FX_FILE_ATTRIB_NORMAL;
}
if (S_ISDIR(ent->fts_statp->st_mode)) {
it->entry.info.attrib |= FX_FILE_ATTRIB_DIRECTORY;
}
if (S_ISBLK(ent->fts_statp->st_mode)) {
it->entry.info.attrib |= FX_FILE_ATTRIB_BLOCK_DEVICE;
}
if (S_ISCHR(ent->fts_statp->st_mode)) {
it->entry.info.attrib |= FX_FILE_ATTRIB_CHAR_DEVICE;
}
if (S_ISLNK(ent->fts_statp->st_mode)) {
it->entry.info.attrib |= FX_FILE_ATTRIB_SYMLINK;
}
#endif
}
static void iterator_fini(fx_object *obj, void *priv)
{
struct fx_directory_iterator_p *it = priv;
if (it->entry.filepath) {
fx_path_unref((fx_path *)it->entry.filepath);
it->entry.filepath = NULL;
}
#if 0
if (it->fts) {
fts_close(it->fts);
}
#endif
}
fx_iterator *fx_directory_begin(
fx_directory *directory,
enum fx_directory_iterator_flags flags)
{
#if 0
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 = fx_object_get_private(directory, FX_TYPE_DIRECTORY);
int fts_flags = FTS_COMFOLLOW | FTS_NOCHDIR;
const char *path_list[] = {
fx_path_get_cstr(it->_p->d_path_abs),
NULL,
};
it->fts = fts_open((char *const *)path_list, fts_flags, ftsent_compare);
bool done = false;
while (!done) {
it->ent = fts_read(it->fts);
if (!it->ent) {
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
return it_obj;
}
if (it->ent->fts_level == 0) {
continue;
}
switch (it->ent->fts_info) {
case FTS_DOT:
continue;
case FTS_F:
done = true;
break;
case FTS_D:
if (it->flags & FX_DIRECTORY_ITERATE_PARENT_LAST) {
continue;
}
done = true;
break;
case FTS_DP:
if (it->flags & FX_DIRECTORY_ITERATE_PARENT_FIRST) {
continue;
}
done = true;
break;
default:
done = true;
break;
}
}
update_iterator_data(it);
return it_obj;
#endif
return NULL;
}
static const fx_iterator *iterator_begin(const fx_object *obj)
{
return fx_directory_begin(
(fx_object *)obj,
FX_DIRECTORY_ITERATE_PARENT_FIRST);
}
static enum fx_status iterator_move_next(const fx_iterator *obj)
{
#if 0
struct fx_directory_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
if (!it || !it->fts) {
return FX_ERR_NO_DATA;
}
bool done = false;
while (!done) {
it->ent = fts_read(it->fts);
if (!it->ent) {
return FX_ERR_NO_DATA;
}
if (it->ent->fts_level == 0) {
continue;
}
switch (it->ent->fts_info) {
case FTS_DOT:
continue;
case FTS_F:
done = true;
break;
case FTS_D:
if (it->flags & FX_DIRECTORY_ITERATE_PARENT_LAST) {
continue;
}
done = true;
break;
case FTS_DP:
if (it->flags & FX_DIRECTORY_ITERATE_PARENT_FIRST) {
continue;
}
done = true;
break;
default:
done = true;
break;
}
}
update_iterator_data(it);
return FX_SUCCESS;
#endif
return FX_ERR_NOT_SUPPORTED;
}
static enum fx_status iterator_erase(fx_iterator *obj)
{
struct fx_directory_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
fx_result result = fx_directory_path_unlink(
it->root,
FX_VALUE_OBJECT(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 const fx_value *iterator_get_value(const fx_iterator *obj)
{
struct fx_directory_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
return &it->entry_value;
}
/*** CLASS DEFINITION *********************************************************/
// ---- fx_directory DEFINITION
FX_TYPE_CLASS_BEGIN(fx_directory)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
FX_INTERFACE_ENTRY(it_begin) = iterator_begin;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_END(fx_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)
// ---- fx_directory_iterator DEFINITION
FX_TYPE_CLASS_BEGIN(fx_directory_iterator)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_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_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_END(fx_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)
+653
View File
@@ -0,0 +1,653 @@
#define _POSIX_C_SOURCE 200809L
#include "misc.h"
#include "posix.h"
#include <errno.h>
#include <fcntl.h>
#include <fx/io/directory.h>
#include <fx/io/file.h>
#include <fx/io/path.h>
#include <fx/random.h>
#include <fx/string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define CHECK_FLAG(v, f) (((v) & (f)) == (f))
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 fx_file_p {
enum fx_file_mode mode;
int fd;
fx_path *path;
};
/*** PRIVATE FUNCTIONS ********************************************************/
static unsigned int fx_mode_to_unix_mode(enum fx_file_mode mode)
{
unsigned int result = 0;
if (CHECK_FLAG(mode, FX_FILE_READ_WRITE)) {
result |= O_RDWR;
} else if (CHECK_FLAG(mode, FX_FILE_READ_ONLY)) {
result |= O_RDONLY;
} else if (CHECK_FLAG(mode, FX_FILE_WRITE_ONLY)) {
result |= O_WRONLY;
} else {
return (unsigned int)-1;
}
if (CHECK_FLAG(mode, FX_FILE_TRUNCATE)) {
result |= O_TRUNC;
}
if (CHECK_FLAG(mode, FX_FILE_CREATE)) {
result |= O_CREAT;
}
if (CHECK_FLAG(mode, FX_FILE_CREATE_ONLY)) {
result |= O_EXCL;
}
return result;
}
static fx_result file_open_shadow(
struct fx_file_p *original,
enum fx_file_mode mode,
fx_file **out)
{
mode |= FX_FILE_SHADOW | FX_FILE_DELETE_ON_CLOSE | FX_FILE_CREATE;
fx_path *dir;
fx_path_get_directory(original->path, &dir);
fx_string *filename = fx_string_create();
fx_path_get_filename(original->path, filename);
fx_string_prepend_cstr(filename, ".~");
fx_path *shadow_filename
= fx_path_create_from_cstr(fx_string_get_cstr(filename));
fx_string_unref(filename);
const fx_value *parts[] = {
&FX_VALUE_OBJECT(dir),
&FX_VALUE_OBJECT(shadow_filename),
};
fx_path *shadow_filepath
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
fx_path_unref(dir);
fx_path_unref(shadow_filename);
if (fx_path_exists(shadow_filepath)) {
fx_path_unlink(shadow_filepath);
}
fx_file *shadow_file;
fx_result status = fx_file_open(
FX_DIRECTORY_ROOT,
FX_VALUE_OBJECT(shadow_filepath),
mode,
&shadow_file);
fx_path_unref(shadow_filepath);
if (fx_result_is_error(status)) {
return status;
}
*out = shadow_file;
return FX_RESULT_SUCCESS;
}
static const fx_path *file_path(const struct fx_file_p *file)
{
return file->path;
}
static enum fx_status file_stat(
struct fx_file_p *file,
struct fx_file_info *out)
{
#if 0
struct stat st;
int err = fstat(file->fd, &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 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 fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
off_t len = lseek(file->fd, 0, SEEK_END);
if (len == (off_t)-1) {
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
cur = lseek(file->fd, cur, SEEK_SET);
if (cur == (off_t)-1) {
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
*out_len = (size_t)len;
return FX_SUCCESS;
}
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 fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
*out_pos = (size_t)cur;
return FX_SUCCESS;
}
static enum fx_status file_resize(struct fx_file_p *file, size_t len)
{
#if 0
int err = ftruncate(file->fd, len);
if (err == 0) {
return FX_SUCCESS;
}
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
#endif
return FX_ERR_NOT_SUPPORTED;
}
static enum fx_status file_seek(
struct fx_file_p *file,
long long offset,
enum fx_seek_basis basis)
{
int whence;
switch (basis) {
case FX_SEEK_BEGINNING:
whence = SEEK_SET;
break;
case FX_SEEK_CURRENT:
whence = SEEK_CUR;
break;
case FX_SEEK_END:
whence = SEEK_END;
break;
default:
return FX_ERR_INVALID_ARGUMENT;
}
int err = lseek(file->fd, offset, whence);
if (err == (off_t)-1) {
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
return FX_SUCCESS;
}
static enum fx_status file_swap_shadow(
struct fx_file_p *main_file,
struct fx_file_p *shadow_file)
{
if (main_file->mode & FX_FILE_SHADOW) {
return FX_ERR_NOT_SUPPORTED;
}
if (!(shadow_file->mode & FX_FILE_SHADOW)) {
return FX_ERR_NOT_SUPPORTED;
}
fx_path *dir_path;
fx_path_get_directory(main_file->path, &dir_path);
fx_path *tmp_path = NULL;
while (1) {
char tmp_name[16];
z__fx_io_generate_tmp_filename(tmp_name, sizeof tmp_name);
fx_path *tmp_name_p = fx_path_create_from_cstr(tmp_name);
const fx_value *parts[] = {
&FX_VALUE_OBJECT(dir_path),
&FX_VALUE_OBJECT(tmp_name_p),
};
tmp_path = fx_path_join_array(
parts,
sizeof parts / sizeof parts[0]);
fx_path_unref(tmp_name_p);
if (!fx_path_exists(tmp_path)) {
break;
}
fx_path_unref(tmp_path);
tmp_path = NULL;
}
fx_path_unref(dir_path);
int err;
#if 0
err = rename(
fx_path_get_cstr(main_file->path),
fx_path_get_cstr(tmp_path));
err = rename(
fx_path_get_cstr(shadow_file->path),
fx_path_get_cstr(main_file->path));
err = rename(
fx_path_get_cstr(tmp_path),
fx_path_get_cstr(shadow_file->path));
#endif
fx_path_unref(tmp_path);
int fd = main_file->fd;
main_file->fd = shadow_file->fd;
shadow_file->fd = fd;
return FX_SUCCESS;
}
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 != FX_OFFSET_CURRENT) {
lseek(file->fd, offset, SEEK_SET);
}
long r = read(file->fd, buf, len);
enum fx_status status = FX_SUCCESS;
if (r < 0) {
status = fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
*nr_read = r;
return status;
}
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 != FX_OFFSET_CURRENT) {
lseek(file->fd, offset, SEEK_SET);
}
long w = write(file->fd, buf, len);
enum fx_status status = FX_SUCCESS;
if (w < 0) {
status = fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
*nr_written = w;
return status;
}
/*** STREAM FUNCTIONS *********************************************************/
static enum fx_status stream_close(fx_stream *stream)
{
return FX_SUCCESS;
}
static enum fx_status stream_getc(fx_stream *stream, int *out)
{
struct fx_file_p *file = fx_object_get_private(stream, FX_TYPE_FILE);
char c;
size_t nr_read = 0;
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 FX_ERR_NO_DATA;
}
*out = c;
return FX_SUCCESS;
}
static enum fx_status stream_read(
fx_stream *stream,
void *buf,
size_t max,
size_t *nr_read)
{
struct fx_file_p *file = fx_object_get_private(stream, FX_TYPE_FILE);
enum fx_status status
= file_read(file, FX_OFFSET_CURRENT, max, buf, nr_read);
return status;
}
static enum fx_status stream_write(
fx_stream *stream,
const void *buf,
size_t count,
size_t *nr_written)
{
struct fx_file_p *file = fx_object_get_private(stream, FX_TYPE_FILE);
enum fx_status status
= file_write(file, FX_OFFSET_CURRENT, count, buf, nr_written);
return status;
}
static enum fx_status stream_seek(
fx_stream *stream,
long long offset,
fx_stream_seek_origin origin)
{
fx_seek_basis basis;
switch (origin) {
case FX_STREAM_SEEK_START:
basis = FX_SEEK_BEGINNING;
break;
case FX_STREAM_SEEK_CURRENT:
basis = FX_SEEK_CURRENT;
break;
case FX_STREAM_SEEK_END:
basis = FX_SEEK_END;
break;
default:
return FX_ERR_INVALID_ARGUMENT;
}
struct fx_file_p *file = fx_object_get_private(stream, FX_TYPE_FILE);
return file_seek(file, offset, basis);
}
static enum fx_status stream_tell(const fx_stream *stream, size_t *pos)
{
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 fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
*pos = v;
return FX_SUCCESS;
}
/*** PUBLIC FUNCTIONS *********************************************************/
fx_result fx_file_open(
fx_directory *root,
fx_value path,
enum fx_file_mode mode,
fx_file **out)
{
unsigned int flags = fx_mode_to_unix_mode(mode);
if (flags == (unsigned int)-1) {
return FX_RESULT_ERR(INVALID_ARGUMENT);
}
const fx_path *root_path = NULL;
bool free_root_path = false;
if (root) {
root_path = fx_directory_get_path(root);
} else {
root_path = fx_path_get_system(FX_PATH_CWD);
free_root_path = true;
}
fx_path *abs_path
= fx_path_join_list(2, &FX_VALUE_OBJECT(root_path), &path);
if (free_root_path) {
fx_path_unref((fx_path *)root_path);
}
if (!abs_path) {
return FX_RESULT_ERR(NO_MEMORY);
}
int fd = open(fx_path_get_cstr(abs_path), flags);
if (fd == -1) {
fx_path_unref(abs_path);
return FX_RESULT_STATUS(
fx_status_from_errno(errno, FX_ERR_IO_FAILURE));
}
fx_file *file = fx_object_create(FX_TYPE_FILE);
if (!file) {
close(fd);
fx_path_unref(abs_path);
return FX_RESULT_ERR(NO_MEMORY);
}
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 & FX_FILE_READ_ONLY) {
cfg->s_mode |= FX_STREAM_READ;
}
if (mode & FX_FILE_WRITE_ONLY) {
cfg->s_mode |= FX_STREAM_WRITE;
}
if (mode & FX_FILE_BINARY) {
cfg->s_mode |= FX_STREAM_BINARY;
}
p->fd = fd;
p->path = abs_path;
p->mode = mode;
*out = file;
return FX_RESULT_SUCCESS;
}
fx_result fx_file_open_temp(enum fx_file_mode mode, fx_file **out)
{
mode |= FX_FILE_DELETE_ON_CLOSE;
char name[16];
char path[128];
while (1) {
z__fx_io_generate_tmp_filename(name, sizeof name);
snprintf(path, sizeof path, "/tmp/%s", name);
fx_path *rpath = fx_path_create_from_cstr(path);
fx_result status = fx_file_open(
FX_DIRECTORY_ROOT,
FX_VALUE_OBJECT(rpath),
mode | FX_FILE_CREATE_ONLY,
out);
if (fx_error_get_status_code(status) == FX_ERR_NAME_EXISTS) {
fx_path_unref(rpath);
continue;
}
fx_path_unlink(rpath);
fx_path_unref(rpath);
return status;
}
}
fx_result fx_file_open_shadow(
fx_file *original,
enum fx_file_mode mode,
fx_file **out)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_FILE,
file_open_shadow,
original,
mode,
out);
}
const fx_path *fx_file_path(const fx_file *file)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_FILE, file_path, file);
}
enum fx_status fx_file_stat(fx_file *file, struct fx_file_info *out)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_stat, file, out);
}
enum fx_status fx_file_size(fx_file *file, size_t *out_len)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_size, file, out_len);
}
enum fx_status fx_file_cursor(fx_file *file, size_t *out_pos)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_cursor, file, out_pos);
}
enum fx_status fx_file_resize(fx_file *file, size_t len)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_resize, file, len);
}
enum fx_status fx_file_seek(
fx_file *file,
long long offset,
enum fx_seek_basis basis)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_seek, file, offset, basis);
}
enum fx_status fx_file_swap_shadow(fx_file *main_file, fx_file *shadow_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 fx_status fx_file_read(
fx_file *file,
size_t offset,
size_t len,
void *buf,
size_t *nr_read)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_FILE,
file_read,
file,
offset,
len,
buf,
nr_read);
}
enum fx_status fx_file_write(
fx_file *file,
size_t offset,
size_t len,
const void *buf,
size_t *nr_written)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_FILE,
file_write,
file,
offset,
len,
buf,
nr_written);
}
/*** VIRTUAL FUNCTIONS ********************************************************/
static void file_init(fx_object *obj, void *priv)
{
struct fx_file_p *file = priv;
}
static void file_fini(fx_object *obj, void *priv)
{
struct fx_file_p *file = priv;
close(file->fd);
if (file->mode & FX_FILE_DELETE_ON_CLOSE) {
fx_path_unlink(file->path);
}
fx_path_unref(file->path);
}
/*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_BEGIN(fx_file)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_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_VTABLE_INTERFACE_END(fx_stream, FX_TYPE_STREAM)
FX_TYPE_CLASS_END(fx_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)
+21
View File
@@ -0,0 +1,21 @@
#include "misc.h"
#include <fx/random.h>
void z__fx_io_generate_tmp_filename(char *out, size_t len)
{
static const char *alphabet
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
"01234567"
"89+=-_.";
static const size_t alphabet_len = 67;
fx_random_ctx *ctx = fx_random_global_ctx();
for (size_t i = 0; i < len; i++) {
int v = fx_random_next_int64(ctx) % alphabet_len;
out[i] = alphabet[v];
}
out[len - 1] = 0;
}
+8
View File
@@ -0,0 +1,8 @@
#ifndef _IO_DARWIN_MISC_H_
#define _IO_DARWIN_MISC_H_
#include <stddef.h>
extern void z__fx_io_generate_tmp_filename(char *out, size_t len);
#endif
+486
View File
@@ -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)
+32
View File
@@ -0,0 +1,32 @@
#include "posix.h"
#include <errno.h>
#include <fx/io/stream.h>
#include <unistd.h>
fx_status fx_pipe_create(fx_iostream **reader, fx_iostream **writer)
{
#if 0
int fds[2];
int err = pipe(fds);
if (err != 0) {
return fx_status_from_errno(errno, FX_ERR_NOT_SUPPORTED);
}
fx_iostream *r = fx_iostream_create(FX_STREAM_READ, fds[0], true);
fx_iostream *w = fx_iostream_create(FX_STREAM_WRITE, fds[1], true);
if (!r || !w) {
fx_iostream_unref(r);
fx_iostream_unref(w);
close(fds[0]);
close(fds[1]);
return FX_ERR_NO_MEMORY;
}
*reader = r;
*writer = w;
return FX_SUCCESS;
#endif
return FX_ERR_NOT_SUPPORTED;
}
+152
View File
@@ -0,0 +1,152 @@
#include <errno.h>
#include <fx/error.h>
#include <fx/io/file.h>
#include <fx/status.h>
enum fx_status fx_status_from_errno(int error, enum fx_status default_value)
{
switch (error) {
case 0:
return FX_SUCCESS;
case ENOENT:
return FX_ERR_NO_ENTRY;
case EEXIST:
return FX_ERR_NAME_EXISTS;
case ENOMEM:
return FX_ERR_NO_MEMORY;
case EINVAL:
return FX_ERR_INVALID_ARGUMENT;
case EIO:
return FX_ERR_IO_FAILURE;
case EISDIR:
return FX_ERR_IS_DIRECTORY;
case ENOTDIR:
return FX_ERR_NOT_DIRECTORY;
case EPERM:
case EACCES:
return FX_ERR_PERMISSION_DENIED;
case ENOTSUP:
case ENOSYS:
return FX_ERR_NOT_SUPPORTED;
default:
return 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 FX_RESULT_SUCCESS;
case ENOENT:
return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_NO_ENTRY,
"Path @i{%s} does not exist",
path);
case ENOTDIR:
return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_NOT_DIRECTORY,
"Path @i{%s} is not a directory",
path);
case EISDIR:
return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_IS_DIRECTORY,
"Path @i{%s} is a directory",
path);
case EPERM:
case EACCES:
return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_PERMISSION_DENIED,
"Permission denied while accessing path @i{%s}",
path);
default:
return FX_RESULT_STATUS(
fx_status_from_errno(error, default_value));
}
return FX_RESULT_SUCCESS;
}
fx_result fx_result_from_errno_with_subfilepath(
int error,
const char *path,
const char *dir_path,
enum fx_status default_value)
{
if (!dir_path) {
return fx_result_propagate(fx_result_from_errno_with_filepath(
error,
path,
default_value));
}
switch (error) {
case 0:
return FX_RESULT_SUCCESS;
case ENOENT:
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 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 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 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 FX_RESULT_STATUS(
fx_status_from_errno(error, default_value));
}
return FX_RESULT_SUCCESS;
}
#if 0
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 |= FX_FILE_ATTRIB_NORMAL;
}
if (S_ISDIR(st->st_mode)) {
out->attrib |= FX_FILE_ATTRIB_DIRECTORY;
}
if (S_ISBLK(st->st_mode)) {
out->attrib |= FX_FILE_ATTRIB_BLOCK_DEVICE;
}
if (S_ISCHR(st->st_mode)) {
out->attrib |= FX_FILE_ATTRIB_CHAR_DEVICE;
}
if (S_ISLNK(st->st_mode)) {
out->attrib |= FX_FILE_ATTRIB_SYMLINK;
}
return FX_SUCCESS;
}
#endif
+26
View File
@@ -0,0 +1,26 @@
#ifndef _IO_DARWIN_POSIX_H_
#define _IO_DARWIN_POSIX_H_
#include <fx/error.h>
#include <fx/status.h>
struct stat;
struct fx_file_info;
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 fx_status default_value);
extern enum fx_status fx_file_info_from_stat(
const struct stat *in,
struct fx_file_info *out);
#endif
+149
View File
@@ -0,0 +1,149 @@
#include "posix.h"
#include <errno.h>
#include <fx/io/stream.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/*** PRIVATE DATA *************************************************************/
struct fx_iostream_p {
int s_fd;
bool s_fd_close_on_release;
};
/*** PRIVATE FUNCTIONS ********************************************************/
static uptr iostream_get_os_handle(const struct fx_iostream_p *stream)
{
return stream->s_fd;
}
static uptr iostream_steal_os_handle(struct fx_iostream_p *stream)
{
uptr out = stream->s_fd;
stream->s_fd = -1;
stream->s_fd_close_on_release = false;
return out;
}
/*** PUBLIC FUNCTIONS *********************************************************/
fx_iostream *fx_iostream_create(
fx_stream_mode mode,
uptr os_handle,
bool close_handle_on_release)
{
fx_iostream *s = fx_object_create(FX_IO_TYPE_STREAM);
if (!s) {
return NULL;
}
fx_stream_cfg *cfg = fx_object_get_protected(s, FX_TYPE_STREAM);
struct fx_iostream_p *p = fx_object_get_private(s, FX_IO_TYPE_STREAM);
cfg->s_mode = mode | Z__FX_STREAM_STATIC;
p->s_fd = (int)os_handle;
p->s_fd_close_on_release = close_handle_on_release;
return s;
}
uptr fx_iostream_get_os_handle(const fx_iostream *stream)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_IO_TYPE_STREAM,
iostream_get_os_handle,
stream);
}
uptr fx_iostream_steal_os_handle(fx_iostream *stream)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_IO_TYPE_STREAM,
iostream_steal_os_handle,
stream);
}
/*** VIRTUAL FUNCTIONS ********************************************************/
static void iostream_init(fx_object *obj, void *priv)
{
struct fx_iostream_p *stream = priv;
}
static void iostream_fini(fx_object *obj, void *priv)
{
struct fx_iostream_p *stream = priv;
if (stream->s_fd_close_on_release) {
close(stream->s_fd);
}
}
static enum fx_status stream_read(
fx_stream *stream,
void *buf,
size_t count,
size_t *nr_read)
{
struct fx_iostream_p *s
= fx_object_get_private(stream, FX_IO_TYPE_STREAM);
long r = read(s->s_fd, buf, count);
if (r < 0) {
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
*nr_read = r;
return FX_SUCCESS;
}
static enum fx_status stream_write(
fx_stream *stream,
const void *buf,
size_t count,
size_t *nr_written)
{
struct fx_iostream_p *s
= fx_object_get_private(stream, FX_IO_TYPE_STREAM);
long w = write(s->s_fd, buf, count);
if (w < 0) {
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
*nr_written = w;
return FX_SUCCESS;
}
/*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_BEGIN(fx_iostream)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_stream, FX_TYPE_STREAM)
FX_INTERFACE_ENTRY(s_close) = NULL;
FX_INTERFACE_ENTRY(s_seek) = NULL;
FX_INTERFACE_ENTRY(s_tell) = NULL;
FX_INTERFACE_ENTRY(s_getc) = NULL;
FX_INTERFACE_ENTRY(s_read) = stream_read;
FX_INTERFACE_ENTRY(s_write) = stream_write;
FX_INTERFACE_ENTRY(s_reserve) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_stream, FX_TYPE_STREAM)
FX_TYPE_CLASS_END(fx_iostream)
FX_TYPE_DEFINITION_BEGIN(fx_iostream)
FX_TYPE_ID(0xc0b1c3c9, 0xa9c6, 0x4910, 0x8786, 0x574b0696e7aa);
FX_TYPE_NAME("fx.io.stream");
FX_TYPE_EXTENDS(FX_TYPE_STREAM);
FX_TYPE_CLASS(fx_iostream_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_iostream_p);
FX_TYPE_INSTANCE_INIT(iostream_init);
FX_TYPE_INSTANCE_FINI(iostream_fini);
FX_TYPE_DEFINITION_END(fx_iostream)
+3 -3
View File
@@ -60,7 +60,7 @@ enum fx_status fx_directory_open(
} }
HANDLE dir_handle = CreateFileA( HANDLE dir_handle = CreateFileA(
fx_path_ptr(new_path), GENERIC_READ, fx_path_get_cstr(new_path), GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, INVALID_HANDLE_VALUE); OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, INVALID_HANDLE_VALUE);
@@ -197,7 +197,7 @@ static bool move_into_directory(struct fx_directory_iterator *it, const char *di
state = push_iteration_state(it->_z); state = push_iteration_state(it->_z);
state->search_path = dir_path; state->search_path = dir_path;
state->search = FindFirstFileA(fx_path_ptr(search_path), &state->data); state->search = FindFirstFileA(fx_path_get_cstr(search_path), &state->data);
fx_path_release(search_path); fx_path_release(search_path);
fx_path_release(wildcard); fx_path_release(wildcard);
@@ -224,7 +224,7 @@ static bool move_into_directory(struct fx_directory_iterator *it, const char *di
static bool move_to_first_item( static bool move_to_first_item(
struct fx_directory_iterator *it, const struct fx_path *root_dir) struct fx_directory_iterator *it, const struct fx_path *root_dir)
{ {
bool has_results = move_into_directory(it, fx_path_ptr(root_dir)); bool has_results = move_into_directory(it, fx_path_get_cstr(root_dir));
if (!has_results) { if (!has_results) {
return false; return false;
+4 -4
View File
@@ -205,13 +205,13 @@ bool fx_path_is_absolute(const struct fx_path *path)
bool fx_path_exists(const struct fx_path *path) bool fx_path_exists(const struct fx_path *path)
{ {
DWORD attrib = GetFileAttributesA(fx_path_ptr(path)); DWORD attrib = GetFileAttributesA(fx_path_get_cstr(path));
return attrib != INVALID_FILE_ATTRIBUTES; return attrib != INVALID_FILE_ATTRIBUTES;
} }
bool fx_path_is_file(const struct fx_path *path) bool fx_path_is_file(const struct fx_path *path)
{ {
DWORD attrib = GetFileAttributesA(fx_path_ptr(path)); DWORD attrib = GetFileAttributesA(fx_path_get_cstr(path));
if (attrib == INVALID_FILE_ATTRIBUTES) { if (attrib == INVALID_FILE_ATTRIBUTES) {
return false; return false;
} }
@@ -221,7 +221,7 @@ bool fx_path_is_file(const struct fx_path *path)
bool fx_path_is_directory(const struct fx_path *path) bool fx_path_is_directory(const struct fx_path *path)
{ {
DWORD attrib = GetFileAttributesA(fx_path_ptr(path)); DWORD attrib = GetFileAttributesA(fx_path_get_cstr(path));
if (attrib == INVALID_FILE_ATTRIBUTES) { if (attrib == INVALID_FILE_ATTRIBUTES) {
return false; return false;
} }
@@ -229,7 +229,7 @@ bool fx_path_is_directory(const struct fx_path *path)
return (attrib & FILE_ATTRIBUTE_DIRECTORY) != 0; return (attrib & FILE_ATTRIBUTE_DIRECTORY) != 0;
} }
const char *fx_path_ptr(const struct fx_path *path) const char *fx_path_get_cstr(const struct fx_path *path)
{ {
return fx_string_get_cstr(path->pathstr); return fx_string_get_cstr(path->pathstr);
} }
+4 -1
View File
@@ -11,7 +11,10 @@ int main(int argc, const char **argv)
fx_directory *dir; fx_directory *dir;
fx_result result = fx_directory_open( fx_result result = fx_directory_open(
NULL, FX_RV_PATH(path), FX_DIRECTORY_OPEN_CREATE_INTERMEDIATE, &dir); NULL,
FX_CSTR(path),
FX_DIRECTORY_OPEN_CREATE_INTERMEDIATE,
&dir);
if (fx_result_is_error(result)) { if (fx_result_is_error(result)) {
fx_throw(result); fx_throw(result);
return -1; return -1;
+23
View File
@@ -0,0 +1,23 @@
#include <fx/io/pipe.h>
#include <stdio.h>
int main(void)
{
fx_iostream *read = NULL, *write = NULL;
fx_status status = fx_pipe_create(&read, &write);
if (!FX_OK(status)) {
fprintf(stderr,
"failed to create pipe: %s\n",
fx_status_description(status));
return -1;
}
fx_stream_write_cstr(write, "Hello, world!\n", NULL);
char buf[128];
fx_stream_read_line(read, buf, sizeof buf);
printf("read from pipe: %s\n", buf);
return 0;
}
+1 -1
View File
@@ -9,7 +9,7 @@ int main(int argc, const char **argv)
const char *path = argv[1]; const char *path = argv[1];
fx_directory *dir; fx_directory *dir;
fx_result result = fx_directory_open(NULL, FX_RV_PATH(path), 0, &dir); fx_result result = fx_directory_open(NULL, FX_CSTR(path), 0, &dir);
if (fx_result_is_error(result)) { if (fx_result_is_error(result)) {
fx_throw(result); fx_throw(result);
return -1; return -1;
+1 -3
View File
@@ -6,10 +6,9 @@
int main(int argc, const char **argv) int main(int argc, const char **argv)
{ {
fx_file *dest; fx_file *dest;
fx_path *path = fx_path_create_from_cstr("data.txt");
fx_result result = fx_file_open( fx_result result = fx_file_open(
NULL, NULL,
path, FX_CSTR("data.txt"),
FX_FILE_WRITE_ONLY | FX_FILE_CREATE, FX_FILE_WRITE_ONLY | FX_FILE_CREATE,
&dest); &dest);
if (fx_result_is_error(result)) { if (fx_result_is_error(result)) {
@@ -23,7 +22,6 @@ int main(int argc, const char **argv)
printf("done. read %zu bytes total.\n", nr_read); printf("done. read %zu bytes total.\n", nr_read);
fx_path_unref(path);
fx_file_unref(dest); fx_file_unref(dest);
return 0; return 0;
} }
+8 -6
View File
@@ -12,22 +12,24 @@ int main(int argc, const char **argv)
} }
fx_directory *dir = NULL; fx_directory *dir = NULL;
fx_path *path = fx_path_create_from_cstr(argv[1]); fx_result result = fx_directory_open(
fx_result result = fx_directory_open(FX_DIRECTORY_ROOT, path, 0, &dir); FX_DIRECTORY_ROOT,
FX_CSTR(argv[1]),
0,
&dir);
if (fx_result_is_error(result)) { if (fx_result_is_error(result)) {
fx_throw(result); fx_throw(result);
return -1; return -1;
} }
fx_iterator *it = fx_directory_begin( fx_iterator *it
dir, = fx_directory_begin(dir, FX_DIRECTORY_ITERATE_PARENT_FIRST);
FX_DIRECTORY_ITERATE_PARENT_FIRST);
fx_foreach(val, it) fx_foreach(val, it)
{ {
fx_directory_entry *entry = NULL; fx_directory_entry *entry = NULL;
fx_value_get_pointer(val, (void **)&entry); fx_value_get_pointer(val, (void **)&entry);
printf("%s\n", fx_path_ptr(entry->filepath)); printf("%s\n", fx_path_get_cstr(entry->filepath));
} }
fx_iterator_unref(it); fx_iterator_unref(it);
+1 -1
View File
@@ -130,7 +130,7 @@ void write_tagged_datetime(fx_datetime *data)
fx_stringstream *new_data = fx_stringstream_create(); fx_stringstream *new_data = fx_stringstream_create();
fx_datetime_to_string(data, FX_DATETIME_FORMAT_RFC3339, new_data); fx_datetime_to_string(data, FX_DATETIME_FORMAT_RFC3339, new_data);
fx_stream_write_cstr(fx_stdout, fx_stringstream_ptr(new_data), NULL); fx_stream_write_cstr(fx_stdout, fx_stringstream_get_cstr(new_data), NULL);
fx_stream_write_cstr(fx_stdout, "\" }", NULL); fx_stream_write_cstr(fx_stdout, "\" }", NULL);
-1
View File
@@ -1852,7 +1852,6 @@ static fx_result parse_array_inline(struct ctx *ctx, fx_value *result)
static fx_result parse_value(struct ctx *ctx, fx_value *result) static fx_result parse_value(struct ctx *ctx, fx_value *result)
{ {
struct token *tok = peek_token(ctx); struct token *tok = peek_token(ctx);
if (!tok) { if (!tok) {
return FX_RESULT_ERR(BAD_FORMAT); return FX_RESULT_ERR(BAD_FORMAT);
+6
View File
@@ -50,6 +50,11 @@ enum {
FX_KEY_BACKSPACE, FX_KEY_BACKSPACE,
FX_KEY_RETURN, FX_KEY_RETURN,
/* within this range, we further reserve a sub-range. applications can
* define their own special keycode values within this range */
FX_KEY_RESERVED_START = 0xFFF00,
FX_KEY_RESERVED_END = 0xFFFFD,
FX_MOD_CTRL = 0x10000000, FX_MOD_CTRL = 0x10000000,
FX_KEY_EOF = 0xFFFFFFFF, FX_KEY_EOF = 0xFFFFFFFF,
@@ -153,6 +158,7 @@ FX_API void fx_tty_set_vmode(
struct fx_tty *tty, struct fx_tty *tty,
const struct fx_tty_vmode *vmode); const struct fx_tty_vmode *vmode);
FX_API void fx_tty_reset_vmode(struct fx_tty *tty); FX_API void fx_tty_reset_vmode(struct fx_tty *tty);
FX_API void fx_tty_flush(struct fx_tty *tty);
FX_API enum fx_status fx_tty_get_dimensions( FX_API enum fx_status fx_tty_get_dimensions(
struct fx_tty *tty, struct fx_tty *tty,
+70 -27
View File
@@ -1,6 +1,8 @@
#define _POSIX_C_SOURCE 200809L
#include "../../tty.h" #include "../../tty.h"
#include <fx/encoding.h> #include <ctype.h>
#include <fx/term/tty.h> #include <fx/term/tty.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -102,11 +104,11 @@ static void init_tty(struct fx_tty *tty, FILE *in, FILE *out)
tcgetattr(fd, &tty->t_ios_default); tcgetattr(fd, &tty->t_ios_default);
memcpy(&tty->t_ios_raw, &tty->t_ios_default, sizeof tty->t_ios_raw); memcpy(&tty->t_ios_raw, &tty->t_ios_default, sizeof tty->t_ios_raw);
setvbuf(out, NULL, _IOLBF, 4096);
tty->t_ios_raw.c_iflag &= ~(BRKINT | INPCK | ISTRIP | ICRNL | IXON); tty->t_ios_raw.c_iflag &= ~(ICRNL | IXON);
tty->t_ios_raw.c_oflag &= ~(OPOST); tty->t_ios_raw.c_oflag &= ~(OPOST);
tty->t_ios_raw.c_cflag |= (CS8); tty->t_ios_raw.c_lflag &= ~(ECHO | ICANON | IEXTEN);
tty->t_ios_raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
tty->t_flags |= FX_TTY_INIT; tty->t_flags |= FX_TTY_INIT;
} }
@@ -207,6 +209,11 @@ void fx_tty_reset_vmode(struct fx_tty *tty)
tty->t_vmode.v_attrib = FX_TTY_ATTRIB_NORMAL; tty->t_vmode.v_attrib = FX_TTY_ATTRIB_NORMAL;
} }
void fx_tty_flush(struct fx_tty *tty)
{
fflush(tty->t_out);
}
static int compare_colour( static int compare_colour(
const struct fx_tty_colour *a, const struct fx_tty_colour *a,
const struct fx_tty_colour *b) const struct fx_tty_colour *b)
@@ -408,24 +415,6 @@ void fx_tty_set_vmode(struct fx_tty *tty, const struct fx_tty_vmode *vmode)
memcpy(&tty->t_vmode, vmode, sizeof *vmode); memcpy(&tty->t_vmode, vmode, sizeof *vmode);
} }
static fx_keycode read_utf8(int fd, char header, int len)
{
char s[4] = {header, 0, 0, 0};
for (int i = 1; i < len; i++) {
char c;
int v = read(fd, &c, 1);
if (v != 1) {
return FX_KEY_EOF;
}
s[i] = c;
}
return fx_wchar_utf8_codepoint_decode(s);
}
fx_keycode fx_tty_read_key(struct fx_tty *tty) fx_keycode fx_tty_read_key(struct fx_tty *tty)
{ {
char c; char c;
@@ -450,11 +439,6 @@ fx_keycode fx_tty_read_key(struct fx_tty *tty)
return FX_TTY_CTRL_KEY(c + 'a' - 1); return FX_TTY_CTRL_KEY(c + 'a' - 1);
} }
int utf8_len = fx_wchar_utf8_header_decode(c);
if (utf8_len > 1) {
return read_utf8(fd, c, utf8_len);
}
if (c != 0x1b) { if (c != 0x1b) {
return c; return c;
} }
@@ -590,3 +574,62 @@ enum fx_status fx_tty_get_dimensions(
return 0; return 0;
} }
enum fx_status fx_tty_get_cursor_position(
struct fx_tty *tty,
unsigned int *w,
unsigned int *h)
{
if (!(tty->t_flags & FX_TTY_INTERACTIVE)) {
return -1;
}
int in_fd = fileno(tty->t_in);
int out_fd = fileno(tty->t_out);
write(out_fd, "\x1b[6n", 4);
char c;
read(in_fd, &c, 1);
read(in_fd, &c, 1);
char s[16] = {0};
for (int i = 0; i < sizeof s - 1; i++) {
read(in_fd, &c, 1);
if (!isdigit(c)) {
break;
}
s[i] = c;
s[i + 1] = 0;
}
if (w) {
if (s[0]) {
*w = atoi(s);
} else {
*w = 0;
}
}
s[0] = 0;
for (int i = 0; i < sizeof s - 1; i++) {
read(in_fd, &c, 1);
if (!isdigit(c)) {
break;
}
s[i] = c;
s[i + 1] = 0;
}
if (h) {
if (s[0]) {
*h = atoi(s);
} else {
*h = 0;
}
}
return 0;
}
+95 -14
View File
@@ -2,6 +2,7 @@
#include "../../tty.h" #include "../../tty.h"
#include <ctype.h>
#include <fx/term/tty.h> #include <fx/term/tty.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -103,6 +104,7 @@ static void init_tty(struct fx_tty *tty, FILE *in, FILE *out)
tcgetattr(fd, &tty->t_ios_default); tcgetattr(fd, &tty->t_ios_default);
memcpy(&tty->t_ios_raw, &tty->t_ios_default, sizeof tty->t_ios_raw); memcpy(&tty->t_ios_raw, &tty->t_ios_default, sizeof tty->t_ios_raw);
setvbuf(out, NULL, _IOLBF, 4096);
tty->t_ios_raw.c_iflag &= ~(ICRNL | IXON); tty->t_ios_raw.c_iflag &= ~(ICRNL | IXON);
tty->t_ios_raw.c_oflag &= ~(OPOST); tty->t_ios_raw.c_oflag &= ~(OPOST);
@@ -207,8 +209,14 @@ void fx_tty_reset_vmode(struct fx_tty *tty)
tty->t_vmode.v_attrib = FX_TTY_ATTRIB_NORMAL; tty->t_vmode.v_attrib = FX_TTY_ATTRIB_NORMAL;
} }
void fx_tty_flush(struct fx_tty *tty)
{
fflush(tty->t_out);
}
static int compare_colour( static int compare_colour(
const struct fx_tty_colour *a, const struct fx_tty_colour *b) const struct fx_tty_colour *a,
const struct fx_tty_colour *b)
{ {
if (a->c_mode != b->c_mode) { if (a->c_mode != b->c_mode) {
return -1; return -1;
@@ -245,7 +253,9 @@ static int compare_colour(
return 0; return 0;
} }
static int compare_vmode(const struct fx_tty_vmode *a, const struct fx_tty_vmode *b) static int compare_vmode(
const struct fx_tty_vmode *a,
const struct fx_tty_vmode *b)
{ {
if (a->v_attrib != b->v_attrib) { if (a->v_attrib != b->v_attrib) {
return -1; return -1;
@@ -273,45 +283,48 @@ static void put_ansi_attrib(struct fx_tty *tty, const char *s)
} }
static void set_attrib( static void set_attrib(
struct fx_tty *tty, enum fx_tty_attrib old, enum fx_tty_attrib new) struct fx_tty *tty,
enum fx_tty_attrib old,
enum fx_tty_attrib new)
{ {
if (old == new) { if (old == new) {
return; return;
} }
/* Bold ON */ /* Bold ON */
if (!(old & FX_TTY_ATTRIB_BOLD) && new &FX_TTY_ATTRIB_BOLD) { if (!(old & FX_TTY_ATTRIB_BOLD) && new & FX_TTY_ATTRIB_BOLD) {
put_ansi_attrib(tty, ANSI_BOLD_ON); put_ansi_attrib(tty, ANSI_BOLD_ON);
} }
/* Bold OFF */ /* Bold OFF */
if (old & FX_TTY_ATTRIB_BOLD && !(new &FX_TTY_ATTRIB_BOLD)) { if (old & FX_TTY_ATTRIB_BOLD && !(new & FX_TTY_ATTRIB_BOLD)) {
put_ansi_attrib(tty, ANSI_BOLD_OFF); put_ansi_attrib(tty, ANSI_BOLD_OFF);
} }
/* Underline ON */ /* Underline ON */
if (!(old & FX_TTY_ATTRIB_UNDERLINE) && new &FX_TTY_ATTRIB_UNDERLINE) { if (!(old & FX_TTY_ATTRIB_UNDERLINE) && new & FX_TTY_ATTRIB_UNDERLINE) {
put_ansi_attrib(tty, ANSI_UNDERLINE_ON); put_ansi_attrib(tty, ANSI_UNDERLINE_ON);
} }
/* Underline OFF */ /* Underline OFF */
if (old & FX_TTY_ATTRIB_UNDERLINE && !(new &FX_TTY_ATTRIB_UNDERLINE)) { if (old & FX_TTY_ATTRIB_UNDERLINE && !(new & FX_TTY_ATTRIB_UNDERLINE)) {
put_ansi_attrib(tty, ANSI_UNDERLINE_OFF); put_ansi_attrib(tty, ANSI_UNDERLINE_OFF);
} }
/* Italic ON */ /* Italic ON */
if (!(old & FX_TTY_ATTRIB_ITALIC) && new &FX_TTY_ATTRIB_ITALIC) { if (!(old & FX_TTY_ATTRIB_ITALIC) && new & FX_TTY_ATTRIB_ITALIC) {
put_ansi_attrib(tty, ANSI_ITALIC_ON); put_ansi_attrib(tty, ANSI_ITALIC_ON);
} }
/* Italic OFF */ /* Italic OFF */
if (old & FX_TTY_ATTRIB_ITALIC && !(new &FX_TTY_ATTRIB_ITALIC)) { if (old & FX_TTY_ATTRIB_ITALIC && !(new & FX_TTY_ATTRIB_ITALIC)) {
put_ansi_attrib(tty, ANSI_ITALIC_OFF); put_ansi_attrib(tty, ANSI_ITALIC_OFF);
} }
} }
static void set_fg( static void set_fg(
struct fx_tty *tty, const struct fx_tty_colour *old, struct fx_tty *tty,
const struct fx_tty_colour *old,
const struct fx_tty_colour *new) const struct fx_tty_colour *new)
{ {
if (compare_colour(old, new) == 0) { if (compare_colour(old, new) == 0) {
@@ -347,7 +360,8 @@ static void set_fg(
} }
static void set_bg( static void set_bg(
struct fx_tty *tty, const struct fx_tty_colour *old, struct fx_tty *tty,
const struct fx_tty_colour *old,
const struct fx_tty_colour *new) const struct fx_tty_colour *new)
{ {
if (compare_colour(old, new) == 0) { if (compare_colour(old, new) == 0) {
@@ -460,7 +474,10 @@ fx_keycode fx_tty_read_key(struct fx_tty *tty)
return c; return c;
} }
void fx_tty_move_cursor_x(struct fx_tty *tty, enum fx_tty_position_base base, int pos) void fx_tty_move_cursor_x(
struct fx_tty *tty,
enum fx_tty_position_base base,
int pos)
{ {
if (base == FX_TTY_POS_CURSOR && pos < 0 && pos >= -4) { if (base == FX_TTY_POS_CURSOR && pos < 0 && pos >= -4) {
for (int i = 0; i > pos; i--) { for (int i = 0; i > pos; i--) {
@@ -489,7 +506,10 @@ void fx_tty_move_cursor_x(struct fx_tty *tty, enum fx_tty_position_base base, in
} }
} }
void fx_tty_move_cursor_y(struct fx_tty *tty, enum fx_tty_position_base base, int pos) void fx_tty_move_cursor_y(
struct fx_tty *tty,
enum fx_tty_position_base base,
int pos)
{ {
if (base == FX_TTY_POS_START) { if (base == FX_TTY_POS_START) {
/* we don't need this functionality right now */ /* we don't need this functionality right now */
@@ -530,7 +550,9 @@ void fx_tty_clear(struct fx_tty *tty, enum fx_tty_clear_mode mode)
} }
enum fx_status fx_tty_get_dimensions( enum fx_status fx_tty_get_dimensions(
struct fx_tty *tty, unsigned int *w, unsigned int *h) struct fx_tty *tty,
unsigned int *w,
unsigned int *h)
{ {
if (!(tty->t_flags & FX_TTY_INTERACTIVE)) { if (!(tty->t_flags & FX_TTY_INTERACTIVE)) {
return -1; return -1;
@@ -552,3 +574,62 @@ enum fx_status fx_tty_get_dimensions(
return 0; return 0;
} }
enum fx_status fx_tty_get_cursor_position(
struct fx_tty *tty,
unsigned int *w,
unsigned int *h)
{
if (!(tty->t_flags & FX_TTY_INTERACTIVE)) {
return -1;
}
int in_fd = fileno(tty->t_in);
int out_fd = fileno(tty->t_out);
write(out_fd, "\x1b[6n", 4);
char c;
read(in_fd, &c, 1);
read(in_fd, &c, 1);
char s[16] = {0};
for (int i = 0; i < sizeof s - 1; i++) {
read(in_fd, &c, 1);
if (!isdigit(c)) {
break;
}
s[i] = c;
s[i + 1] = 0;
}
if (w) {
if (s[0]) {
*w = atoi(s);
} else {
*w = 0;
}
}
s[0] = 0;
for (int i = 0; i < sizeof s - 1; i++) {
read(in_fd, &c, 1);
if (!isdigit(c)) {
break;
}
s[i] = c;
s[i + 1] = 0;
}
if (h) {
if (s[0]) {
*h = atoi(s);
} else {
*h = 0;
}
}
return 0;
}
+30
View File
@@ -0,0 +1,30 @@
#define _POSIX_C_SOURCE 200809L
#include "../../print.h"
#include <stdio.h>
#include <string.h>
int z__fx_stream_is_tty(FILE *fp)
{
return 0;
}
int z__fx_stream_dimensions(FILE *fp, unsigned int *w, unsigned int *h)
{
return -1;
}
int z__fx_stream_cursorpos(
FILE *in,
FILE *out,
unsigned int *x,
unsigned int *y)
{
return -1;
}
int z__fx_stream_set_modifier(FILE *fp, enum z__fx_stream_modifier mod)
{
return -1;
}
+229
View File
@@ -0,0 +1,229 @@
#define _POSIX_C_SOURCE 200809L
#include "../../tty.h"
#include <ctype.h>
#include <fx/term/tty.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ANSI_BOLD_ON "1"
#define ANSI_BOLD_OFF "22"
#define ANSI_ITALIC_ON "3"
#define ANSI_ITALIC_OFF "23"
#define ANSI_UNDERLINE_ON "4"
#define ANSI_UNDERLINE_OFF "24"
#define ANSI_DEFAULTCOLOUR_FG "39"
#define ANSI_DEFAULTCOLOUR_BG "49"
#define ANSI_256COLOUR_FG "38;5"
#define ANSI_256COLOUR_BG "48;5"
#define ANSI_TRUECOLOUR_FG "38;2"
#define ANSI_TRUECOLOUR_BG "48;2"
enum tty_flags {
FX_TTY_INIT = 0x01u,
FX_TTY_INTERACTIVE = 0x02u,
};
struct fx_tty {
FILE *t_in, *t_out;
enum tty_flags t_flags;
struct fx_tty_vmode t_vmode;
unsigned char t_mcount;
struct tty_format_buf t_format_buf;
};
static struct fx_tty std = {0};
static struct fx_tty err = {0};
static const char *ansi_colour16_fg[] = {
[FX_TTY_COLOUR16_BLACK] = "30",
[FX_TTY_COLOUR16_RED] = "31",
[FX_TTY_COLOUR16_GREEN] = "32",
[FX_TTY_COLOUR16_YELLOW] = "33",
[FX_TTY_COLOUR16_FX] = "34",
[FX_TTY_COLOUR16_MAGENTA] = "35",
[FX_TTY_COLOUR16_CYAN] = "36",
[FX_TTY_COLOUR16_WHITE] = "37",
[FX_TTY_COLOUR16_BRIGHT_BLACK] = "90",
[FX_TTY_COLOUR16_BRIGHT_RED] = "91",
[FX_TTY_COLOUR16_BRIGHT_GREEN] = "92",
[FX_TTY_COLOUR16_BRIGHT_YELLOW] = "93",
[FX_TTY_COLOUR16_BRIGHT_FX] = "94",
[FX_TTY_COLOUR16_BRIGHT_MAGENTA] = "95",
[FX_TTY_COLOUR16_BRIGHT_CYAN] = "96",
[FX_TTY_COLOUR16_BRIGHT_WHITE] = "97",
};
static const char *ansi_colour16_bg[] = {
[FX_TTY_COLOUR16_BLACK] = "40",
[FX_TTY_COLOUR16_RED] = "41",
[FX_TTY_COLOUR16_GREEN] = "42",
[FX_TTY_COLOUR16_YELLOW] = "43",
[FX_TTY_COLOUR16_FX] = "44",
[FX_TTY_COLOUR16_MAGENTA] = "45",
[FX_TTY_COLOUR16_CYAN] = "46",
[FX_TTY_COLOUR16_WHITE] = "47",
[FX_TTY_COLOUR16_BRIGHT_BLACK] = "100",
[FX_TTY_COLOUR16_BRIGHT_RED] = "101",
[FX_TTY_COLOUR16_BRIGHT_GREEN] = "102",
[FX_TTY_COLOUR16_BRIGHT_YELLOW] = "103",
[FX_TTY_COLOUR16_BRIGHT_FX] = "104",
[FX_TTY_COLOUR16_BRIGHT_MAGENTA] = "105",
[FX_TTY_COLOUR16_BRIGHT_CYAN] = "106",
[FX_TTY_COLOUR16_BRIGHT_WHITE] = "107",
};
static void init_tty(struct fx_tty *tty, FILE *in, FILE *out)
{
}
struct fx_tty *z__fx_tty_get_std(void)
{
if (!(std.t_flags & FX_TTY_INIT)) {
init_tty(&std, stdin, stdout);
}
return &std;
}
struct fx_tty *z__fx_tty_get_err(void)
{
if (!(err.t_flags & FX_TTY_INIT)) {
init_tty(&err, NULL, stderr);
}
return &err;
}
struct tty_format_buf *z__fx_tty_get_format_buf(struct fx_tty *tty)
{
return &tty->t_format_buf;
}
void z__fx_tty_putc(struct fx_tty *tty, char c)
{
fputc(c, tty->t_out);
}
bool fx_tty_is_interactive(const struct fx_tty *tty)
{
return (tty->t_flags & FX_TTY_INTERACTIVE) == FX_TTY_INTERACTIVE;
}
void fx_tty_set_mode(struct fx_tty *tty, enum fx_tty_mode mode)
{
}
void fx_tty_reset_vmode(struct fx_tty *tty)
{
}
void fx_tty_flush(struct fx_tty *tty)
{
}
static int compare_colour(
const struct fx_tty_colour *a,
const struct fx_tty_colour *b)
{
if (a->c_mode != b->c_mode) {
return -1;
}
switch (a->c_mode) {
case FX_TTY_COLOUR_16:
if (a->c_16.value != b->c_16.value) {
return -1;
}
break;
case FX_TTY_COLOUR_256:
if (a->c_256.value != b->c_256.value) {
return -1;
}
break;
case FX_TTY_COLOUR_TRUE:
if (a->c_true.r != b->c_true.r) {
return -1;
}
if (a->c_true.g != b->c_true.g) {
return -1;
}
if (a->c_true.b != b->c_true.b) {
return -1;
}
break;
default:
break;
}
return 0;
}
static int compare_vmode(
const struct fx_tty_vmode *a,
const struct fx_tty_vmode *b)
{
if (a->v_attrib != b->v_attrib) {
return -1;
}
if (compare_colour(&a->v_fg, &b->v_fg) != 0) {
return -1;
}
if (compare_colour(&a->v_bg, &b->v_bg) != 0) {
return -1;
}
return 0;
}
void fx_tty_set_vmode(struct fx_tty *tty, const struct fx_tty_vmode *vmode)
{
}
fx_keycode fx_tty_read_key(struct fx_tty *tty)
{
return FX_KEY_EOF;
}
void fx_tty_move_cursor_x(
struct fx_tty *tty,
enum fx_tty_position_base base,
int pos)
{
}
void fx_tty_move_cursor_y(
struct fx_tty *tty,
enum fx_tty_position_base base,
int pos)
{
}
void fx_tty_clear(struct fx_tty *tty, enum fx_tty_clear_mode mode)
{
}
enum fx_status fx_tty_get_dimensions(
struct fx_tty *tty,
unsigned int *w,
unsigned int *h)
{
return -1;
}
enum fx_status fx_tty_get_cursor_position(
struct fx_tty *tty,
unsigned int *w,
unsigned int *h)
{
return -1;
}
+283
View File
@@ -0,0 +1,283 @@
#include <fx/bytestream.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#define DEFAULT_CAPACITY 128
/*** PRIVATE DATA *************************************************************/
struct fx_bytestream_p {
unsigned char *s_buf;
size_t s_ptr;
size_t s_len;
size_t s_max;
unsigned char s_alloc;
};
/*** PRIVATE FUNCTIONS ********************************************************/
static enum fx_status __gets(
struct fx_bytestream_p *s,
unsigned char *buf,
size_t len,
size_t *nr_read)
{
size_t available = s->s_len - s->s_ptr;
size_t to_copy = len;
if (available < to_copy) {
to_copy = available;
}
memcpy(buf, s->s_buf + s->s_ptr, to_copy);
s->s_ptr += to_copy;
if (s->s_ptr == s->s_len) {
s->s_ptr = s->s_len = 0;
}
if (nr_read) {
*nr_read = to_copy;
}
return FX_SUCCESS;
}
static enum fx_status __puts(
struct fx_bytestream_p *s,
const unsigned char *buf,
size_t len,
size_t *nr_written)
{
size_t available = s->s_max - s->s_len;
size_t to_copy = len;
if (to_copy > available && s->s_alloc == 1) {
unsigned char *new_buf
= realloc(s->s_buf, s->s_len + to_copy + 1);
if (!new_buf) {
return FX_ERR_NO_MEMORY;
}
s->s_buf = new_buf;
s->s_max = s->s_len + to_copy;
available = s->s_max - s->s_len;
}
if (available < to_copy) {
to_copy = available;
}
memcpy(s->s_buf + s->s_len, buf, to_copy);
s->s_buf[s->s_len + to_copy] = 0;
/* increment the length by the full byte length, even if only a
* portion was copied */
s->s_len += len;
if (nr_written) {
*nr_written = to_copy;
}
return FX_SUCCESS;
}
static enum fx_status bytestream_reset(struct fx_bytestream_p *s)
{
s->s_len = 0;
if (s->s_buf) {
*s->s_buf = 0;
}
return FX_SUCCESS;
}
static size_t bytestream_get_length(const struct fx_bytestream_p *strv)
{
return strv->s_len;
}
static const unsigned char *bytestream_get_ptr(const struct fx_bytestream_p *s)
{
return s->s_buf;
}
static unsigned char *bytestream_steal(struct fx_bytestream_p *s)
{
unsigned char *out = s->s_buf;
memset(s, 0x0, sizeof *s);
s->s_alloc = 1;
return out;
}
/*** PUBLIC FUNCTIONS *********************************************************/
fx_bytestream *fx_bytestream_create_view(unsigned char *buf, size_t max)
{
fx_bytestream *s = fx_object_create(FX_TYPE_BYTESTREAM);
if (!s) {
return NULL;
}
fx_stream_cfg *cfg = fx_object_get_protected(s, FX_TYPE_STREAM);
struct fx_bytestream_p *p
= fx_object_get_private(s, FX_TYPE_BYTESTREAM);
cfg->s_mode = FX_STREAM_READ | FX_STREAM_WRITE | FX_STREAM_BINARY
| Z__FX_STREAM_STATIC;
p->s_buf = buf;
p->s_max = max;
p->s_len = max;
p->s_alloc = 0;
return s;
}
fx_bytestream *fx_bytestream_create_readonly_view(
const unsigned char *buf,
size_t max)
{
fx_bytestream *s = fx_object_create(FX_TYPE_BYTESTREAM);
if (!s) {
return NULL;
}
fx_stream_cfg *cfg = fx_object_get_protected(s, FX_TYPE_STREAM);
struct fx_bytestream_p *p
= fx_object_get_private(s, FX_TYPE_BYTESTREAM);
cfg->s_mode = FX_STREAM_READ | FX_STREAM_BINARY | Z__FX_STREAM_STATIC;
p->s_buf = (unsigned char *)buf;
p->s_max = max;
p->s_len = max;
p->s_alloc = 0;
return s;
}
fx_bytestream *fx_bytestream_create(void)
{
fx_bytestream *s = fx_object_create(FX_TYPE_BYTESTREAM);
if (!s) {
return NULL;
}
fx_stream_cfg *cfg = fx_object_get_protected(s, FX_TYPE_STREAM);
struct fx_bytestream_p *p
= fx_object_get_private(s, FX_TYPE_BYTESTREAM);
cfg->s_mode = FX_STREAM_READ | FX_STREAM_WRITE | FX_STREAM_BINARY
| Z__FX_STREAM_STATIC;
p->s_buf = malloc(DEFAULT_CAPACITY + 1);
if (!p->s_buf) {
fx_bytestream_unref(s);
return NULL;
}
memset(p->s_buf, 0x0, DEFAULT_CAPACITY + 1);
p->s_max = DEFAULT_CAPACITY;
p->s_len = 0;
p->s_alloc = 1;
return s;
}
size_t fx_bytestream_get_length(const fx_bytestream *strv)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_BYTESTREAM,
bytestream_get_length,
strv);
}
enum fx_status fx_bytestream_reset(fx_bytestream *strv)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_BYTESTREAM, bytestream_reset, strv);
}
const unsigned char *fx_bytestream_get_ptr(const fx_bytestream *s)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_BYTESTREAM, bytestream_get_ptr, s);
}
unsigned char *fx_bytestream_steal(fx_bytestream *s)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_BYTESTREAM, bytestream_steal, s);
}
/*** VIRTUAL FUNCTIONS ********************************************************/
static void bytestream_init(fx_object *obj, void *priv)
{
struct fx_bytestream_p *stream = priv;
}
static void bytestream_fini(fx_object *obj, void *priv)
{
struct fx_bytestream_p *stream = priv;
if (stream->s_alloc && stream->s_buf) {
free(stream->s_buf);
}
}
static enum fx_status stream_read(
fx_stream *stream,
void *buf,
size_t count,
size_t *nr_read)
{
struct fx_bytestream_p *s
= fx_object_get_private(stream, FX_TYPE_BYTESTREAM);
enum fx_status status = __gets(s, buf, count, nr_read);
return status;
}
static enum fx_status stream_write(
fx_stream *stream,
const void *buf,
size_t count,
size_t *nr_written)
{
struct fx_bytestream_p *s
= fx_object_get_private(stream, FX_TYPE_BYTESTREAM);
enum fx_status status
= __puts(s, (const unsigned char *)buf, count, nr_written);
return status;
}
/*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_BEGIN(fx_bytestream)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_stream, FX_TYPE_STREAM)
FX_INTERFACE_ENTRY(s_close) = NULL;
FX_INTERFACE_ENTRY(s_seek) = NULL;
FX_INTERFACE_ENTRY(s_tell) = NULL;
FX_INTERFACE_ENTRY(s_getc) = NULL;
FX_INTERFACE_ENTRY(s_read) = stream_read;
FX_INTERFACE_ENTRY(s_write) = stream_write;
FX_INTERFACE_ENTRY(s_reserve) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_stream, FX_TYPE_STREAM)
FX_TYPE_CLASS_END(fx_bytestream)
FX_TYPE_DEFINITION_BEGIN(fx_bytestream)
FX_TYPE_ID(0xdcb7fb3a, 0xc476, 0x4c0b, 0xad5a, 0x039d1190a5b7);
FX_TYPE_EXTENDS(FX_TYPE_STREAM);
FX_TYPE_CLASS(fx_bytestream_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_bytestream_p);
FX_TYPE_INSTANCE_INIT(bytestream_init);
FX_TYPE_INSTANCE_FINI(bytestream_fini);
FX_TYPE_DEFINITION_END(fx_bytestream)
+2 -5
View File
@@ -1217,11 +1217,8 @@ static bool wchar_is_control(fx_wchar c)
return true; return true;
} }
if (c == 0xFEFF) { /* treat the reserved range as control characters */
return true; if (c >= 0xF0000 && c <= 0xFFFFD) {
}
if (c >= 0xFFF9 && c <= 0xFFFB) {
return true; return true;
} }
+39
View File
@@ -0,0 +1,39 @@
#include <fx/global.h>
#include <fx/thread.h>
#include <stdlib.h>
#include <string.h>
static fx_queue globals = FX_QUEUE_INIT;
static fx_mutex globals_lock = FX_MUTEX_INIT;
struct global {
fx_object *g_object;
fx_queue_entry g_entry;
};
void fx_cleanup_global(void)
{
fx_queue_entry *cur = fx_queue_first(&globals);
while (cur) {
struct global *global = fx_unbox(struct global, cur, g_entry);
cur = fx_queue_next(cur);
fx_object_unref(global->g_object);
free(global);
}
}
void fx_register_global(fx_object *obj)
{
struct global *global = malloc(sizeof *global);
if (!global) {
return;
}
memset(global, 0x0, sizeof *global);
global->g_object = fx_object_ref(obj);
fx_mutex_lock(&globals_lock);
fx_queue_push_back(&globals, &global->g_entry);
fx_mutex_unlock(&globals_lock);
}
+37
View File
@@ -0,0 +1,37 @@
#ifndef FX_CORE_BYTESTREAM_H_
#define FX_CORE_BYTESTREAM_H_
#include <fx/macros.h>
#include <fx/misc.h>
#include <fx/status.h>
#include <fx/stream.h>
#include <stddef.h>
FX_DECLS_BEGIN;
#define FX_TYPE_BYTESTREAM (fx_bytestream_get_type())
FX_DECLARE_TYPE(fx_bytestream);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_bytestream)
FX_TYPE_CLASS_DECLARATION_END(fx_bytestream)
FX_API fx_type_id fx_bytestream_get_type(void);
FX_API fx_bytestream *fx_bytestream_create(void);
FX_API fx_bytestream *fx_bytestream_create_view(unsigned char *buf, size_t max);
FX_API fx_bytestream *fx_bytestream_create_readonly_view(
const unsigned char *buf,
size_t max);
FX_API fx_status fx_bytestream_reset(fx_bytestream *strv);
FX_API const unsigned char *fx_bytestream_get_ptr(const fx_bytestream *strv);
FX_API unsigned char *fx_bytestream_steal(fx_bytestream *strv);
FX_API size_t fx_bytestream_get_length(const fx_bytestream *strv);
FX_API size_t fx_bytestream_get_available(const fx_bytestream *strv);
FX_DECLS_END;
#endif
+10
View File
@@ -0,0 +1,10 @@
#ifndef FX_GLOBAL_H_
#define FX_GLOBAL_H_
#include <fx/misc.h>
#include <fx/object.h>
FX_API void fx_register_global(fx_object *obj);
FX_API void fx_cleanup_global(void);
#endif
+2
View File
@@ -22,6 +22,7 @@ typedef struct _fx_object_class {
FX_TYPE_FWDREF(fx_stream) *, FX_TYPE_FWDREF(fx_stream) *,
const char *); const char *);
fx_status (*hash)(const struct fx_value *, uint64_t *); fx_status (*hash)(const struct fx_value *, uint64_t *);
fx_status (*clone)(const struct fx_value *, struct fx_value *);
} fx_object_class; } fx_object_class;
FX_API fx_type_id fx_object_get_type(void); FX_API fx_type_id fx_object_get_type(void);
@@ -46,6 +47,7 @@ FX_API fx_status fx_object_to_string(
const fx_object *p, const fx_object *p,
FX_TYPE_FWDREF(fx_stream) * out, FX_TYPE_FWDREF(fx_stream) * out,
const char *format); const char *format);
FX_API fx_status fx_object_clone(const fx_object *src, fx_object **dest);
FX_API bool fx_object_is_type(const fx_object *p, fx_type_id type); FX_API bool fx_object_is_type(const fx_object *p, fx_type_id type);
#endif #endif
+1 -1
View File
@@ -95,7 +95,7 @@ fx_string_insert_cstrf(fx_string *dest, size_t at, const char *format, ...);
FX_API void fx_string_clear(fx_string *str); FX_API void fx_string_clear(fx_string *str);
FX_API fx_iterator *fx_string_tokenise( FX_API fx_iterator *fx_string_tokenise(
fx_string *str, const fx_string *str,
const char *delims[], const char *delims[],
size_t nr_delims, size_t nr_delims,
fx_string_tokenise_flags flags); fx_string_tokenise_flags flags);
+1 -1
View File
@@ -27,7 +27,7 @@ FX_API fx_status fx_stringstream_reset(fx_stringstream *strv);
FX_API fx_status FX_API fx_status
fx_stringstream_reset_with_buffer(fx_stringstream *strv, char *buf, size_t max); fx_stringstream_reset_with_buffer(fx_stringstream *strv, char *buf, size_t max);
FX_API const char *fx_stringstream_ptr(const fx_stringstream *strv); FX_API const char *fx_stringstream_get_cstr(const fx_stringstream *strv);
FX_API char *fx_stringstream_steal(fx_stringstream *strv); FX_API char *fx_stringstream_steal(fx_stringstream *strv);
FX_API size_t fx_stringstream_get_length(const fx_stringstream *strv); FX_API size_t fx_stringstream_get_length(const fx_stringstream *strv);
+28 -6
View File
@@ -25,6 +25,10 @@ static enum fx_status iterator_set_status(
const fx_iterator *fx_iterator_begin(const fx_iterable *it) const fx_iterator *fx_iterator_begin(const fx_iterable *it)
{ {
if (!it) {
return NULL;
}
FX_CLASS_DISPATCH_VIRTUAL_0( FX_CLASS_DISPATCH_VIRTUAL_0(
fx_iterable, fx_iterable,
FX_TYPE_ITERABLE, FX_TYPE_ITERABLE,
@@ -35,11 +39,19 @@ const fx_iterator *fx_iterator_begin(const fx_iterable *it)
enum fx_status fx_iterator_get_status(const fx_iterator *it) enum fx_status fx_iterator_get_status(const fx_iterator *it)
{ {
if (!it) {
return FX_ERR_NO_DATA;
}
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ITERATOR, iterator_get_status, it); FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ITERATOR, iterator_get_status, it);
} }
enum fx_status fx_iterator_set_status(const fx_iterator *it, fx_status status) enum fx_status fx_iterator_set_status(const fx_iterator *it, fx_status status)
{ {
if (!it) {
return FX_ERR_NO_DATA;
}
FX_CLASS_DISPATCH_STATIC( FX_CLASS_DISPATCH_STATIC(
FX_TYPE_ITERATOR, FX_TYPE_ITERATOR,
iterator_set_status, iterator_set_status,
@@ -49,11 +61,14 @@ enum fx_status fx_iterator_set_status(const fx_iterator *it, fx_status status)
enum fx_status fx_iterator_move_next(const fx_iterator *it) enum fx_status fx_iterator_move_next(const fx_iterator *it)
{ {
if (!it) {
return FX_ERR_NO_DATA;
}
enum fx_status status = FX_ERR_NOT_SUPPORTED; enum fx_status status = FX_ERR_NOT_SUPPORTED;
fx_iterator_class *iface = fx_object_get_interface( fx_iterator_class *iface
it, = fx_object_get_interface(it, FX_TYPE_ITERATOR);
FX_TYPE_ITERATOR);
if (iface && iface->it_move_next) { if (iface && iface->it_move_next) {
status = iface->it_move_next(it); status = iface->it_move_next(it);
} }
@@ -66,6 +81,10 @@ enum fx_status fx_iterator_move_next(const fx_iterator *it)
const fx_value *fx_iterator_get_value(const fx_iterator *it) const fx_value *fx_iterator_get_value(const fx_iterator *it)
{ {
if (!it) {
return NULL;
}
FX_CLASS_DISPATCH_VIRTUAL_0( FX_CLASS_DISPATCH_VIRTUAL_0(
fx_iterator, fx_iterator,
FX_TYPE_ITERATOR, FX_TYPE_ITERATOR,
@@ -76,11 +95,14 @@ const fx_value *fx_iterator_get_value(const fx_iterator *it)
fx_status fx_iterator_erase(fx_iterator *it) fx_status fx_iterator_erase(fx_iterator *it)
{ {
if (!it) {
return FX_ERR_NO_DATA;
}
enum fx_status status = FX_ERR_NOT_SUPPORTED; enum fx_status status = FX_ERR_NOT_SUPPORTED;
fx_iterator_class *iface = fx_object_get_interface( fx_iterator_class *iface
it, = fx_object_get_interface(it, FX_TYPE_ITERATOR);
FX_TYPE_ITERATOR);
if (iface && iface->it_erase) { if (iface && iface->it_erase) {
status = iface->it_erase(it); status = iface->it_erase(it);
} }
+26 -2
View File
@@ -23,7 +23,7 @@ FX_TYPE_DEFINITION_END(fx_object)
fx_type_id fx_object_query_type(const struct _fx_object *obj) fx_type_id fx_object_query_type(const struct _fx_object *obj)
{ {
return &obj->obj_type->ty_id; return obj ? &obj->obj_type->ty_id : NULL;
} }
fx_result fx_object_instantiate( fx_result fx_object_instantiate(
@@ -98,6 +98,23 @@ fx_status fx_object_to_string(
return FX_SUCCESS; return FX_SUCCESS;
} }
fx_status fx_object_clone(const fx_object *src, fx_object **dest)
{
fx_object_class *iface = fx_object_get_interface(src, FX_TYPE_OBJECT);
fx_value dest_v = FX_VALUE_EMPTY;
if (!iface || !iface->clone) {
return FX_ERR_NOT_SUPPORTED;
}
fx_status status = iface->clone(&FX_VALUE_OBJECT(src), &dest_v);
if (!FX_OK(status)) {
return status;
}
fx_value_get_object(&dest_v, dest);
return status;
}
bool fx_object_is_type(const struct _fx_object *p, fx_type_id type) bool fx_object_is_type(const struct _fx_object *p, fx_type_id type)
{ {
if (!p) { if (!p) {
@@ -200,12 +217,19 @@ enum fx_status fx_object_get_data(
struct _fx_object *fx_object_ref(struct _fx_object *p) struct _fx_object *fx_object_ref(struct _fx_object *p)
{ {
p->obj_ref++; if (p) {
p->obj_ref++;
}
return p; return p;
} }
void fx_object_unref(struct _fx_object *p) void fx_object_unref(struct _fx_object *p)
{ {
if (!p) {
return;
}
if (p->obj_ref > 1) { if (p->obj_ref > 1) {
p->obj_ref--; p->obj_ref--;
return; return;
+18 -25
View File
@@ -642,9 +642,8 @@ static fx_stream *init_stdio_stream(FILE *fp, fx_stream_mode mode)
return NULL; return NULL;
} }
struct fx_stdio_stream_p *p = fx_object_get_private( struct fx_stdio_stream_p *p
stream, = fx_object_get_private(stream, FX_TYPE_STDIO_STREAM);
FX_TYPE_STDIO_STREAM);
fx_stream_cfg *cfg = fx_object_get_protected(stream, FX_TYPE_STREAM); fx_stream_cfg *cfg = fx_object_get_protected(stream, FX_TYPE_STREAM);
p->s_fp = fp; p->s_fp = fp;
cfg->s_mode = mode; cfg->s_mode = mode;
@@ -681,8 +680,8 @@ static enum fx_status stream_push_indent(struct stream_data *stream, int indent)
stream->s_private->s_istack_size += 4; stream->s_private->s_istack_size += 4;
} }
int cur_indent = stream->s_private int cur_indent
->s_istack[stream->s_private->s_istack_ptr]; = stream->s_private->s_istack[stream->s_private->s_istack_ptr];
stream->s_private->s_istack[++stream->s_private->s_istack_ptr] stream->s_private->s_istack[++stream->s_private->s_istack_ptr]
= cur_indent + indent; = cur_indent + indent;
@@ -750,9 +749,8 @@ fx_stream_buffer *fx_stream_buffer_create_dynamic(size_t buffer_size)
return NULL; return NULL;
} }
struct fx_stream_buffer_p *p = fx_object_get_private( struct fx_stream_buffer_p *p
buffer, = fx_object_get_private(buffer, FX_TYPE_STREAM_BUFFER);
FX_TYPE_STREAM_BUFFER);
p->p_buf = malloc(buffer_size); p->p_buf = malloc(buffer_size);
if (!p->p_buf) { if (!p->p_buf) {
@@ -773,9 +771,8 @@ fx_stream_buffer *fx_stream_buffer_create(void *buf, size_t len)
return NULL; return NULL;
} }
struct fx_stream_buffer_p *p = fx_object_get_private( struct fx_stream_buffer_p *p
buffer, = fx_object_get_private(buffer, FX_TYPE_STREAM_BUFFER);
FX_TYPE_STREAM_BUFFER);
p->p_buf = buf; p->p_buf = buf;
p->p_buf_len = len; p->p_buf_len = len;
@@ -1130,15 +1127,14 @@ static enum fx_status stdio_read(
size_t max, size_t max,
size_t *nr_read) size_t *nr_read)
{ {
struct fx_stdio_stream_p *p = fx_object_get_private( struct fx_stdio_stream_p *p
stream, = fx_object_get_private(stream, FX_TYPE_STDIO_STREAM);
FX_TYPE_STDIO_STREAM);
enum fx_status status = FX_SUCCESS; enum fx_status status = FX_SUCCESS;
size_t count = fread(out, 1, max, p->s_fp); size_t count = fread(out, 1, max, p->s_fp);
if (ferror(p->s_fp)) { if (count == 0 && ferror(p->s_fp)) {
status = FX_ERR_IO_FAILURE; status = FX_ERR_IO_FAILURE;
} }
@@ -1152,14 +1148,13 @@ static enum fx_status stdio_write(
size_t count, size_t count,
size_t *nr_written) size_t *nr_written)
{ {
struct fx_stdio_stream_p *p = fx_object_get_private( struct fx_stdio_stream_p *p
stream, = fx_object_get_private(stream, FX_TYPE_STDIO_STREAM);
FX_TYPE_STDIO_STREAM);
enum fx_status status = FX_SUCCESS; enum fx_status status = FX_SUCCESS;
size_t w = fwrite(data, 1, count, p->s_fp); size_t w = fwrite(data, 1, count, p->s_fp);
if (ferror(p->s_fp)) { if (count == 0 && ferror(p->s_fp)) {
status = FX_ERR_IO_FAILURE; status = FX_ERR_IO_FAILURE;
} }
@@ -1172,9 +1167,8 @@ static enum fx_status stdio_seek(
long long offset, long long offset,
fx_stream_seek_origin origin) fx_stream_seek_origin origin)
{ {
struct fx_stdio_stream_p *p = fx_object_get_private( struct fx_stdio_stream_p *p
stream, = fx_object_get_private(stream, FX_TYPE_STDIO_STREAM);
FX_TYPE_STDIO_STREAM);
int whence = 0; int whence = 0;
switch (origin) { switch (origin) {
@@ -1201,9 +1195,8 @@ static enum fx_status stdio_seek(
static enum fx_status stdio_tell(const fx_stream *stream, size_t *cursor) static enum fx_status stdio_tell(const fx_stream *stream, size_t *cursor)
{ {
struct fx_stdio_stream_p *p = fx_object_get_private( struct fx_stdio_stream_p *p
stream, = fx_object_get_private(stream, FX_TYPE_STDIO_STREAM);
FX_TYPE_STDIO_STREAM);
long pos = ftell(p->s_fp); long pos = ftell(p->s_fp);
if (pos == -1L) { if (pos == -1L) {
+10 -9
View File
@@ -56,7 +56,7 @@ struct fx_string_p {
struct fx_string_iterator_p { struct fx_string_iterator_p {
int _m, _f; int _m, _f;
fx_string *_tmp; fx_string *_tmp;
struct fx_string_p *_s_p, *_tmp_p; const struct fx_string_p *_s_p, *_tmp_p;
const char **_d; const char **_d;
size_t _nd, _ds; size_t _nd, _ds;
@@ -301,7 +301,7 @@ static fx_string *string_duplicate(const struct fx_string_p *str)
const char *src = string_ptr(str); const char *src = string_ptr(str);
char *dst = string_ptr(new_str_p); char *dst = string_ptr(new_str_p);
memcpy(dst, src, str->s_len); memcpy(dst, src, str->s_len + 1);
new_str_p->s_len = str->s_len; new_str_p->s_len = str->s_len;
new_str_p->s_codepoints = str->s_codepoints; new_str_p->s_codepoints = str->s_codepoints;
@@ -478,7 +478,7 @@ static fx_status string_replace_all_with_stringstream(
size_t new_len = fx_stringstream_get_length(new_data); size_t new_len = fx_stringstream_get_length(new_data);
string_reserve(str, new_len); string_reserve(str, new_len);
char *dest = string_ptr(str); char *dest = string_ptr(str);
memcpy(dest, fx_stringstream_ptr(new_data), new_len); memcpy(dest, fx_stringstream_get_cstr(new_data), new_len);
dest[new_len] = '\0'; dest[new_len] = '\0';
str->s_len = new_len; str->s_len = new_len;
@@ -513,7 +513,8 @@ static enum fx_status remove_ansi(
memmove(removal_start, excess_src, excess_length); memmove(removal_start, excess_src, excess_length);
s[new_str_len] = '\0'; s[new_str_len] = '\0';
str->s_len = new_str_len; str->s_len -= length;
str->s_codepoints -= length;
return FX_SUCCESS; return FX_SUCCESS;
} }
@@ -1023,7 +1024,7 @@ static enum fx_status find_next_token(struct fx_string_iterator_p *it)
} }
static fx_iterator *string_tokenise( static fx_iterator *string_tokenise(
struct fx_string_p *str, const struct fx_string_p *str,
const char *delims[], const char *delims[],
size_t nr_delims, size_t nr_delims,
fx_string_tokenise_flags flags) fx_string_tokenise_flags flags)
@@ -1390,7 +1391,7 @@ void fx_string_clear(fx_string *str)
} }
fx_iterator *fx_string_tokenise( fx_iterator *fx_string_tokenise(
fx_string *str, const fx_string *str,
const char *delims[], const char *delims[],
size_t nr_delims, size_t nr_delims,
fx_string_tokenise_flags flags) fx_string_tokenise_flags flags)
@@ -1574,10 +1575,9 @@ static fx_status get_length(
/*** ITERATOR FUNCTIONS *******************************************************/ /*** ITERATOR FUNCTIONS *******************************************************/
static void iterator_fini(fx_iterator *obj) static void iterator_fini(fx_object *obj, void *priv)
{ {
struct fx_string_iterator_p *it struct fx_string_iterator_p *it = priv;
= fx_object_get_private(obj, FX_TYPE_STRING_ITERATOR);
if (it->_tmp) { if (it->_tmp) {
fx_string_unref(it->_tmp); fx_string_unref(it->_tmp);
} }
@@ -1840,4 +1840,5 @@ FX_TYPE_DEFINITION_BEGIN(fx_string_iterator)
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR); FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
FX_TYPE_CLASS(fx_string_iterator_class); FX_TYPE_CLASS(fx_string_iterator_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_string_iterator_p); FX_TYPE_INSTANCE_PRIVATE(struct fx_string_iterator_p);
FX_TYPE_INSTANCE_FINI(iterator_fini);
FX_TYPE_DEFINITION_END(fx_string_iterator) FX_TYPE_DEFINITION_END(fx_string_iterator)
+19 -21
View File
@@ -141,7 +141,7 @@ static size_t stringstream_get_length(const struct fx_stringstream_p *strv)
return strv->ss_len; return strv->ss_len;
} }
static const char *stringstream_ptr(const struct fx_stringstream_p *ss) static const char *stringstream_get_cstr(const struct fx_stringstream_p *ss)
{ {
return ss->ss_buf; return ss->ss_buf;
} }
@@ -167,9 +167,8 @@ fx_stringstream *fx_stringstream_create_with_buffer(char *buf, size_t max)
} }
fx_stream_cfg *cfg = fx_object_get_protected(s, FX_TYPE_STREAM); fx_stream_cfg *cfg = fx_object_get_protected(s, FX_TYPE_STREAM);
struct fx_stringstream_p *p = fx_object_get_private( struct fx_stringstream_p *p
s, = fx_object_get_private(s, FX_TYPE_STRINGSTREAM);
FX_TYPE_STRINGSTREAM);
cfg->s_mode = FX_STREAM_READ | FX_STREAM_WRITE | Z__FX_STREAM_STATIC; cfg->s_mode = FX_STREAM_READ | FX_STREAM_WRITE | Z__FX_STREAM_STATIC;
@@ -189,9 +188,8 @@ fx_stringstream *fx_stringstream_create(void)
} }
fx_stream_cfg *cfg = fx_object_get_protected(s, FX_TYPE_STREAM); fx_stream_cfg *cfg = fx_object_get_protected(s, FX_TYPE_STREAM);
struct fx_stringstream_p *p = fx_object_get_private( struct fx_stringstream_p *p
s, = fx_object_get_private(s, FX_TYPE_STRINGSTREAM);
FX_TYPE_STRINGSTREAM);
cfg->s_mode = FX_STREAM_READ | FX_STREAM_WRITE | Z__FX_STREAM_STATIC; cfg->s_mode = FX_STREAM_READ | FX_STREAM_WRITE | Z__FX_STREAM_STATIC;
@@ -238,9 +236,12 @@ enum fx_status fx_stringstream_reset_with_buffer(
max); max);
} }
const char *fx_stringstream_ptr(const fx_stringstream *ss) const char *fx_stringstream_get_cstr(const fx_stringstream *ss)
{ {
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_STRINGSTREAM, stringstream_ptr, ss); FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_STRINGSTREAM,
stringstream_get_cstr,
ss);
} }
char *fx_stringstream_steal(fx_stringstream *ss) char *fx_stringstream_steal(fx_stringstream *ss)
@@ -268,41 +269,38 @@ static void stringstream_fini(fx_object *obj, void *priv)
} }
} }
enum fx_status stream_getc(fx_stream *stream, fx_wchar *c) static enum fx_status stream_getc(fx_stream *stream, fx_wchar *c)
{ {
struct fx_stringstream_p *s = fx_object_get_private( struct fx_stringstream_p *s
stream, = fx_object_get_private(stream, FX_TYPE_STRINGSTREAM);
FX_TYPE_STRINGSTREAM);
enum fx_status status = __getc(s, c); enum fx_status status = __getc(s, c);
return status; return status;
} }
enum fx_status stream_read( static enum fx_status stream_read(
fx_stream *stream, fx_stream *stream,
void *buf, void *buf,
size_t count, size_t count,
size_t *nr_read) size_t *nr_read)
{ {
struct fx_stringstream_p *s = fx_object_get_private( struct fx_stringstream_p *s
stream, = fx_object_get_private(stream, FX_TYPE_STRINGSTREAM);
FX_TYPE_STRINGSTREAM);
enum fx_status status = __gets(s, buf, count, nr_read); enum fx_status status = __gets(s, buf, count, nr_read);
return status; return status;
} }
enum fx_status stream_write( static enum fx_status stream_write(
fx_stream *stream, fx_stream *stream,
const void *buf, const void *buf,
size_t count, size_t count,
size_t *nr_written) size_t *nr_written)
{ {
struct fx_stringstream_p *s = fx_object_get_private( struct fx_stringstream_p *s
stream, = fx_object_get_private(stream, FX_TYPE_STRINGSTREAM);
FX_TYPE_STRINGSTREAM);
enum fx_status status = __puts(s, (const char *)buf, count, nr_written); enum fx_status status = __puts(s, (const char *)buf, count, nr_written);
+17
View File
@@ -0,0 +1,17 @@
#include <fx/thread.h>
#include <pthread.h>
bool fx_mutex_lock(fx_mutex *mut)
{
return pthread_mutex_lock(mut) == 0;
}
bool fx_mutex_trylock(fx_mutex *mut)
{
return pthread_mutex_trylock(mut) == 0;
}
bool fx_mutex_unlock(fx_mutex *mut)
{
return pthread_mutex_trylock(mut) == 0;
}
+17
View File
@@ -0,0 +1,17 @@
#include <fx/thread.h>
#include <pthread.h>
bool fx_mutex_lock(fx_mutex *mut)
{
return pthread_mutex_lock(mut) == 0;
}
bool fx_mutex_trylock(fx_mutex *mut)
{
return pthread_mutex_trylock(mut) == 0;
}
bool fx_mutex_unlock(fx_mutex *mut)
{
return pthread_mutex_trylock(mut) == 0;
}
+17
View File
@@ -0,0 +1,17 @@
#include <fx/thread.h>
#include <pthread.h>
bool fx_mutex_lock(fx_mutex *mut)
{
return pthread_mutex_lock(mut) == 0;
}
bool fx_mutex_trylock(fx_mutex *mut)
{
return pthread_mutex_trylock(mut) == 0;
}
bool fx_mutex_unlock(fx_mutex *mut)
{
return pthread_mutex_trylock(mut) == 0;
}
+78 -31
View File
@@ -5,6 +5,7 @@
#include <fx/bst.h> #include <fx/bst.h>
#include <fx/endian.h> #include <fx/endian.h>
#include <fx/global.h>
#include <fx/int.h> #include <fx/int.h>
#include <fx/object.h> #include <fx/object.h>
#include <fx/reflection/function.h> #include <fx/reflection/function.h>
@@ -16,6 +17,7 @@
static struct fx_bst type_idmap = FX_BST_INIT; static struct fx_bst type_idmap = FX_BST_INIT;
static struct fx_namemap type_namemap = FX_NAMEMAP_INIT; static struct fx_namemap type_namemap = FX_NAMEMAP_INIT;
static fx_once runtime_cleanup_init = FX_ONCE_INIT;
static union fx_type_id zero_id = {0}; static union fx_type_id zero_id = {0};
struct type_init_ctx { struct type_init_ctx {
@@ -23,6 +25,37 @@ struct type_init_ctx {
size_t ctx_instance_offset; size_t ctx_instance_offset;
}; };
static fx_result fx_type_destroy(struct fx_type_info *info);
static void type_namemap_cleanup(struct fx_namemap_entry *entry)
{
struct fx_type_info *ty
= fx_unbox(struct fx_type_info, entry, ty_namemap_entry);
fx_type_destroy(ty);
}
static void function_namemap_cleanup(struct fx_namemap_entry *entry)
{
struct fx_type_function *func
= fx_unbox(struct fx_type_function, entry, f_entry);
fx_function_unref(func->f_func);
free(func);
}
static void property_namemap_cleanup(struct fx_namemap_entry *entry)
{
struct fx_type_property *prop
= fx_unbox(struct fx_type_property, entry, p_entry);
fx_property_unref(prop->p_prop);
free(prop);
}
static void runtime_cleanup(void)
{
fx_cleanup_global();
// fx_namemap_cleanup(&type_namemap, type_namemap_cleanup);
}
static inline int type_info_compare( static inline int type_info_compare(
const struct fx_type_info *a, const struct fx_type_info *a,
const struct fx_type_info *b) const struct fx_type_info *b)
@@ -56,10 +89,8 @@ static struct fx_type_info *get_type(
{ {
fx_bst_node *cur = tree->bst_root; fx_bst_node *cur = tree->bst_root;
while (cur) { while (cur) {
struct fx_type_info *cur_node = fx_unbox( struct fx_type_info *cur_node
struct fx_type_info, = fx_unbox(struct fx_type_info, cur, ty_idmap_node);
cur,
ty_idmap_node);
int cmp = fx_type_id_compare(key, &cur_node->ty_id); int cmp = fx_type_id_compare(key, &cur_node->ty_id);
if (cmp > 0) { if (cmp > 0) {
@@ -80,10 +111,8 @@ struct fx_type_component *fx_type_get_component(
{ {
fx_bst_node *cur = tree->bst_root; fx_bst_node *cur = tree->bst_root;
while (cur) { while (cur) {
struct fx_type_component *cur_node = fx_unbox( struct fx_type_component *cur_node
struct fx_type_component, = fx_unbox(struct fx_type_component, cur, c_node);
cur,
c_node);
int cmp = fx_type_id_compare(key, &cur_node->c_type->ty_id); int cmp = fx_type_id_compare(key, &cur_node->c_type->ty_id);
if (cmp > 0) { if (cmp > 0) {
@@ -151,16 +180,14 @@ static fx_result locate_interface(
struct fx_type_info *dest, struct fx_type_info *dest,
struct type_init_ctx *init_ctx) struct type_init_ctx *init_ctx)
{ {
struct fx_type_component *interface_comp = fx_type_get_component( struct fx_type_component *interface_comp
&dest->ty_components, = fx_type_get_component(&dest->ty_components, interface_id);
interface_id);
if (interface_comp) { if (interface_comp) {
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
struct fx_type_info *interface_reg = get_type( struct fx_type_info *interface_reg
&type_idmap, = get_type(&type_idmap, interface_id);
interface_id);
if (!interface_reg) { if (!interface_reg) {
return FX_RESULT_ERR(NO_ENTRY); return FX_RESULT_ERR(NO_ENTRY);
} }
@@ -227,9 +254,8 @@ static fx_result find_type_components(struct fx_type_info *type)
} }
while (1) { while (1) {
struct fx_type_info *dep_class = get_type( struct fx_type_info *dep_class
&type_idmap, = get_type(&type_idmap, current_id);
current_id);
if (!dep_class) { if (!dep_class) {
return FX_RESULT_ERR(NO_ENTRY); return FX_RESULT_ERR(NO_ENTRY);
} }
@@ -302,6 +328,10 @@ static bool type_has_base_class(struct fx_type_info *info)
fx_result fx_type_register(struct fx_type_info *info) fx_result fx_type_register(struct fx_type_info *info)
{ {
if (fx_init_once(&runtime_cleanup_init)) {
atexit(runtime_cleanup);
}
if (!type_has_base_class(info)) { if (!type_has_base_class(info)) {
fx_type_id_copy(FX_TYPE_OBJECT, &info->ty_parent_id); fx_type_id_copy(FX_TYPE_OBJECT, &info->ty_parent_id);
} }
@@ -340,12 +370,34 @@ fx_result fx_type_register(struct fx_type_info *info)
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
static fx_result fx_type_destroy(struct fx_type_info *info)
{
info->ty_category = FX_TYPE_CLASS;
union fx_type_id metatype = {0};
fx_bst_node *cur = fx_bst_first(&info->ty_components);
while (cur) {
struct fx_type_component *component
= fx_unbox(struct fx_type_component, cur, c_node);
cur = fx_bst_next(cur);
free(component);
}
fx_namemap_cleanup(&info->ty_properties, property_namemap_cleanup);
fx_namemap_cleanup(&info->ty_functions, function_namemap_cleanup);
fx_type_unref(info->ty_metatype);
return FX_RESULT_SUCCESS;
}
fx_result fx_type_add_function(fx_type_info *info, struct _fx_object *func) fx_result fx_type_add_function(fx_type_info *info, struct _fx_object *func)
{ {
const char *name = fx_function_get_name(func); const char *name = fx_function_get_name(func);
struct fx_namemap_entry *entry = fx_namemap_get( struct fx_namemap_entry *entry
&info->ty_functions, = fx_namemap_get(&info->ty_functions, name);
name);
if (entry) { if (entry) {
return FX_RESULT_ERR(NAME_EXISTS); return FX_RESULT_ERR(NAME_EXISTS);
} }
@@ -366,9 +418,8 @@ fx_result fx_type_add_function(fx_type_info *info, struct _fx_object *func)
fx_result fx_type_add_property(fx_type_info *info, struct _fx_object *prop) fx_result fx_type_add_property(fx_type_info *info, struct _fx_object *prop)
{ {
const char *name = fx_property_get_name(prop); const char *name = fx_property_get_name(prop);
struct fx_namemap_entry *entry = fx_namemap_get( struct fx_namemap_entry *entry
&info->ty_functions, = fx_namemap_get(&info->ty_functions, name);
name);
if (entry) { if (entry) {
return FX_RESULT_ERR(NAME_EXISTS); return FX_RESULT_ERR(NAME_EXISTS);
} }
@@ -410,10 +461,8 @@ fx_function *fx_type_info_get_function_by_name(
return NULL; return NULL;
} }
struct fx_type_function *func = fx_unbox( struct fx_type_function *func
struct fx_type_function, = fx_unbox(struct fx_type_function, entry, f_entry);
entry,
f_entry);
return func->f_func; return func->f_func;
} }
@@ -426,9 +475,7 @@ fx_function *fx_type_info_get_property_by_name(
return NULL; return NULL;
} }
struct fx_type_property *prop = fx_unbox( struct fx_type_property *prop
struct fx_type_property, = fx_unbox(struct fx_type_property, entry, p_entry);
entry,
p_entry);
return prop->p_prop; return prop->p_prop;
} }
+1 -1
View File
@@ -181,7 +181,7 @@ static fx_status value_change_string(fx_value *in, fx_value *out)
} }
fx_string *result fx_string *result
= fx_string_create_from_cstr(fx_stringstream_ptr(strm)); = fx_string_create_from_cstr(fx_stringstream_get_cstr(strm));
fx_stringstream_unref(strm); fx_stringstream_unref(strm);
if (!result) { if (!result) {
+3 -3
View File
@@ -11,11 +11,11 @@ int main(int argc, const char **argv)
return -1; return -1;
} }
const char *path_cstr = argv[1]; const char *path = argv[1];
fx_path *path = fx_path_create_from_cstr(path_cstr);
fx_file *src = NULL; fx_file *src = NULL;
fx_result result = fx_file_open(NULL, path, FX_FILE_READ_ONLY, &src); fx_result result
= fx_file_open(NULL, FX_CSTR(path), FX_FILE_READ_ONLY, &src);
if (fx_result_is_error(result)) { if (fx_result_is_error(result)) {
fx_throw(result); fx_throw(result);
return -1; return -1;
+3 -3
View File
@@ -11,11 +11,11 @@ int main(int argc, const char **argv)
return -1; return -1;
} }
const char *path_cstr = argv[1]; const char *path = argv[1];
fx_path *path = fx_path_create_from_cstr(path_cstr);
fx_file *src = NULL; fx_file *src = NULL;
fx_result result = fx_file_open(NULL, path, FX_FILE_READ_ONLY, &src); fx_result result
= fx_file_open(NULL, FX_CSTR(path), FX_FILE_READ_ONLY, &src);
if (fx_result_is_error(result)) { if (fx_result_is_error(result)) {
fx_throw(result); fx_throw(result);
return -1; return -1;
+20 -33
View File
@@ -1,40 +1,27 @@
#include <stdarg.h> #include <fx/diagnostics/process.h>
#include <stdio.h> #include <stdio.h>
static int another_function(int a, double b, int c) int main(int argc, const char **argv)
{ {
printf("a=%d, b=%lf, c=%d\n", a, b, c); printf("%d args:\n", argc);
return 2;
}
static int test_function(int a, int b, int c, ...) for (int i = 0; i < argc; i++) {
{ printf("%s\n", argv[i]);
va_list args; }
va_start(args, c);
int d = va_arg(args, int); printf("environment:\n");
int e = va_arg(args, int); fx_process *self = fx_process_get_self();
int f = va_arg(args, int); const fx_hashtable *env = fx_process_get_environment(self);
int g = va_arg(args, int); fx_object_to_string(env, fx_stdout, NULL);
int h = va_arg(args, int); printf("\n");
int i = va_arg(args, int);
int j = va_arg(args, int); printf("read from stdin...\n");
printf("a=%d, b=%d, c=%d, d=%d, e=%d, f=%d, g=%d, h=%d, i=%d, j=%d\n", char line[128];
a, if (fgets(line, sizeof line, stdin)) {
b, printf("read: %s\n", line);
c, } else {
d, printf("read failed\n");
e, }
f,
g,
h,
i,
j);
return a + b + c + d + e;
}
int main(void)
{
test_function(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
another_function(1, 2.5, 5);
return 0; return 0;
} }
+3 -3
View File
@@ -9,11 +9,11 @@ int main(int argc, const char **argv)
return -1; return -1;
} }
const char *path_cstr = argv[1]; const char *path = argv[1];
fx_path *path = fx_path_create_from_cstr(path_cstr);
fx_file *src = NULL; fx_file *src = NULL;
fx_result result = fx_file_open(NULL, path, FX_FILE_READ_ONLY, &src); fx_result result
= fx_file_open(NULL, FX_CSTR(path), FX_FILE_READ_ONLY, &src);
if (fx_result_is_error(result)) { if (fx_result_is_error(result)) {
fx_throw(result); fx_throw(result);
return -1; return -1;
+3 -3
View File
@@ -10,11 +10,11 @@ int main(int argc, const char **argv)
return -1; return -1;
} }
const char *path_cstr = argv[1]; const char *path = argv[1];
fx_path *path = fx_path_create_from_cstr(path_cstr);
fx_file *src = NULL; fx_file *src = NULL;
fx_result result = fx_file_open(NULL, path, FX_FILE_READ_ONLY, &src); fx_result result
= fx_file_open(NULL, FX_CSTR(path), FX_FILE_READ_ONLY, &src);
if (fx_result_is_error(result)) { if (fx_result_is_error(result)) {
fx_throw(result); fx_throw(result);
return -1; return -1;