60 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
wash 8c0a31e19b meta: update rosetta support 2026-05-30 10:09:55 +01:00
wash a20306ae64 cmake: update package finder module 2026-05-29 20:45:46 +01:00
wash 99380c7810 test: fix linux compilation 2026-05-29 20:20:31 +01:00
wash 2d396ee071 fx.reflection: fix linux compilation 2026-05-29 20:20:19 +01:00
wash 34a8ccff95 fx: fix linux compilation 2026-05-29 20:20:05 +01:00
wash d9835cea0b fx.io: fix linux compilation 2026-05-29 20:19:55 +01:00
wash 7b546f6bbe meta: update clang-format config 2026-05-29 20:04:34 +01:00
wash 65f490b900 fx.serial: toml: update array and hashtable usage 2026-05-29 20:04:15 +01:00
wash 1516eee8d1 fx.collections: hashtable: update allocate_index_for_key to handle exact key matches 2026-05-29 20:02:48 +01:00
wash 53a856794f fx.collections: move fx_datetime to fx 2026-05-29 20:02:02 +01:00
wash f879d4fdfc fx: object: add nullptr check to fx_object_is_type 2026-05-29 19:59:55 +01:00
wash e53027595e fx: double add missing fx.comparable reference 2026-05-29 19:59:30 +01:00
wash bbb26c5ee5 fx: pointer: implement hash() and compare() 2026-05-29 19:59:00 +01:00
wash e78241c051 fx: printf: adjust default floating point precision 2026-05-29 19:58:40 +01:00
wash 5877abd33e fx.reflection: update iterator semantics 2026-05-28 21:00:23 +01:00
wash b31e82aaf8 fx.io: update iterator semantics 2026-05-28 20:53:53 +01:00
wash 4c06ded99f fx.cmdline: fix namespace not having the required header subdirectory 2026-05-28 20:53:16 +01:00
wash ff0d40b324 fx.collections: update iterator semantics 2026-05-28 20:52:40 +01:00
wash 883b0b24b1 fx: improve fx_value reference management 2026-05-28 20:51:55 +01:00
wash 516f5be2d6 fx: implement fx_namemap cleanup 2026-05-28 20:50:27 +01:00
wash f79650698a fx: update fx_iterator to return pointers to values 2026-05-28 20:50:00 +01:00
114 changed files with 6856 additions and 1415 deletions
+1 -1
View File
@@ -45,7 +45,7 @@ SpacesBeforeTrailingComments: 3
TabWidth: 8 TabWidth: 8
UseTab: AlignWithSpaces UseTab: AlignWithSpaces
BreakAfterReturnType: Automatic BreakAfterReturnType: Automatic
PenaltyBreakAssignment: 1000000 PenaltyBreakAssignment: 0
PenaltyReturnTypeOnItsOwnLine: 100000000 PenaltyReturnTypeOnItsOwnLine: 100000000
PenaltyExcessCharacter: 10000000 PenaltyExcessCharacter: 10000000
PenaltyBreakOpenParenthesis: 0 PenaltyBreakOpenParenthesis: 0
+13 -3
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_MODULE_PATH} ${CMAKE_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)
@@ -59,4 +61,12 @@ foreach (assembly ${fx_assemblies})
add_subdirectory(assemblies/${assembly}) add_subdirectory(assemblies/${assembly})
endforeach (assembly) endforeach (assembly)
add_subdirectory(test) if (fx_enable_tests)
add_subdirectory(test)
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)
+44 -109
View File
@@ -49,43 +49,37 @@ if (FX_STATIC)
set(_lib_suffix "-s") set(_lib_suffix "-s")
endif () endif ()
set(supported_components Core Ds Term Cmd Io Serial Compress) set(assemblies ${FX_FIND_COMPONENTS})
set(components ${FX_FIND_COMPONENTS})
string(REPLACE ";" ", " supported_components_string_list "${supported_components}")
if (NOT components)
set(components ${supported_components})
endif ()
set(required_vars) set(required_vars)
foreach (component ${components}) if (NOT FX_INCLUDE_DIR)
if (NOT "${component}" IN_LIST supported_components) find_path(FX_INCLUDE_DIR
message(FATAL_ERROR "'${component}' is not a valid FX module.\nSupported modules: ${supported_components_string_list}") NAMES fx/misc.h ${FX_FIND_ARGS}
endif () PATH_SUFFIXES include
PATHS ${FX_SEARCH_PATHS})
endif ()
string(TOLOWER ${component} header_name) set(required_vars FX_INCLUDE_DIR)
set(lib_name ${header_name}${_lib_suffix})
if (NOT FX_${component}_INCLUDE_DIR) foreach (assembly ${assemblies})
find_path(FX_${component}_INCLUDE_DIR string(TOLOWER ${assembly} header_name)
NAMES fx/${header_name}.h ${FX_FIND_ARGS} string(REPLACE "." "_" macro_name ${assembly})
PATH_SUFFIXES include string(TOUPPER ${macro_name} macro_name)
PATHS ${FX_SEARCH_PATHS})
endif ()
if (NOT FX_${component}_LIBRARY) set(lib_name ${assembly}${_lib_suffix})
find_library(FX_${component}_LIBRARY
NAMES fx-${lib_name} ${FX_FIND_ARGS} if (NOT ${macro_name}_LIBRARY)
find_library(${macro_name}_LIBRARY
NAMES ${lib_name} ${FX_FIND_ARGS}
PATH_SUFFIXES lib PATH_SUFFIXES lib
PATHS ${FX_SEARCH_PATHS}) PATHS ${FX_SEARCH_PATHS})
else () else ()
# on Windows, ensure paths are in canonical format (forward slahes): # on Windows, ensure paths are in canonical format (forward slahes):
file(TO_CMAKE_PATH "${FX_${component}_LIBRARY}" FX_${component}_LIBRARY) file(TO_CMAKE_PATH "${${macro_name}_LIBRARY}" ${macro_name}_LIBRARY)
endif() endif()
list(APPEND required_vars FX_${component}_INCLUDE_DIR FX_${component}_LIBRARY) list(APPEND required_vars ${macro_name}_LIBRARY)
endforeach (component) endforeach (assembly)
unset(FX_FIND_ARGS) unset(FX_FIND_ARGS)
@@ -96,94 +90,35 @@ find_package_handle_standard_args(FX
if (FX_FOUND) if (FX_FOUND)
set(created_targets) set(created_targets)
foreach (component ${components}) foreach (assembly ${assemblies})
string(TOLOWER ${component} header_name) set(target_name ${assembly})
set(lib_name ${header_name}${_lib_suffix}) string(REPLACE "fx." "" target_name ${target_name})
string(SUBSTRING ${target_name} 0 1 target_name_prefix)
string(TOUPPER ${target_name_prefix} target_name_prefix)
string(SUBSTRING ${target_name} 1 -1 target_name_suffix)
set(target_name ${target_name_prefix}${target_name_suffix})
if(NOT TARGET FX::${component}) string(TOLOWER ${assembly} header_name)
add_library(FX::${component} UNKNOWN IMPORTED) string(REPLACE "." "_" macro_name ${assembly})
set_target_properties(FX::${component} PROPERTIES string(REPLACE "." "_" macro_name ${assembly})
INTERFACE_INCLUDE_DIRECTORIES "${FX_${component}_INCLUDE_DIR}") string(TOUPPER ${macro_name} macro_name)
target_compile_definitions(FX::${component} INTERFACE _CRT_SECURE_NO_WARNINGS=1)
set(lib_name ${assembly}${_lib_suffix})
if (NOT TARGET FX::${target_name})
add_library(FX::${target_name} UNKNOWN IMPORTED)
set_target_properties(FX::${target_name} PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${FX_INCLUDE_DIR}")
target_compile_definitions(FX::${target_name} INTERFACE _CRT_SECURE_NO_WARNINGS=1)
if (FX_STATIC) if (FX_STATIC)
target_compile_definitions(FX::${component} INTERFACE FX_STATIC=1) target_compile_definitions(FX::${target_name} INTERFACE FX_STATIC=1)
endif () endif ()
set_target_properties(FX::${component} PROPERTIES set_target_properties(FX::${target_name} PROPERTIES
IMPORTED_LINK_INTERFACE_LANGUAGES "C" IMPORTED_LINK_INTERFACE_LANGUAGES "C"
IMPORTED_LOCATION "${FX_${component}_LIBRARY}") IMPORTED_LOCATION "${${macro_name}_LIBRARY}")
set(created_targets ${created_targets} ${component}) set(created_targets ${created_targets} ${assembly})
endif () endif ()
endforeach (component) endforeach (assembly)
foreach (component ${created_targets})
if ("${component}" STREQUAL "Ds")
if (NOT TARGET FX::Core)
message(FATAL_ERROR "FX: Module 'Ds' depends on 'Core', which was not specified in find_package()")
endif ()
target_link_libraries(FX::Ds INTERFACE FX::Core)
endif ()
if ("${component}" STREQUAL "Term")
if (NOT TARGET FX::Core)
message(FATAL_ERROR "FX: Module 'Term' depends on 'Core', which was not specified in find_package()")
endif ()
if (NOT TARGET FX::Ds)
message(FATAL_ERROR "FX: Module 'Term' depends on 'Ds', which was not specified in find_package()")
endif ()
target_link_libraries(FX::Term INTERFACE FX::Core FX::Ds)
endif ()
if ("${component}" STREQUAL "Serial")
if (NOT TARGET FX::Core)
message(FATAL_ERROR "FX: Module 'Serial' depends on 'Core', which was not specified in find_package()")
endif ()
if (NOT TARGET FX::Ds)
message(FATAL_ERROR "FX: Module 'Serial' depends on 'Ds', which was not specified in find_package()")
endif ()
target_link_libraries(FX::Serial INTERFACE FX::Core FX::Ds)
endif ()
if ("${component}" STREQUAL "Cmd")
if (NOT TARGET FX::Core)
message(FATAL_ERROR "FX: Module 'Cmd' depends on 'Core', which was not specified in find_package()")
endif ()
if (NOT TARGET FX::Ds)
message(FATAL_ERROR "FX: Module 'Cmd' depends on 'Ds', which was not specified in find_package()")
endif ()
if (NOT TARGET FX::Term)
message(FATAL_ERROR "FX: Module 'Cmd' depends on 'Term', which was not specified in find_package()")
endif ()
target_link_libraries(FX::Cmd INTERFACE FX::Core FX::Ds FX::Term)
endif ()
if ("${component}" STREQUAL "Io")
if (NOT TARGET FX::Core)
message(FATAL_ERROR "FX: Module 'Io' depends on 'Core', which was not specified in find_package()")
endif ()
if (NOT TARGET FX::Ds)
message(FATAL_ERROR "FX: Module 'Io' depends on 'Ds', which was not specified in find_package()")
endif ()
target_link_libraries(FX::Io INTERFACE FX::Core FX::Ds)
endif ()
if ("${component}" STREQUAL "Compress")
if (NOT TARGET FX::Core)
message(FATAL_ERROR "FX: Module 'Compress' depends on 'Core', which was not specified in find_package()")
endif ()
target_link_libraries(FX::Compress INTERFACE FX::Core FX::Ds)
endif ()
endforeach (component)
endif() endif()
+2 -2
View File
@@ -19,7 +19,7 @@ function(get_platform_details)
elseif ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows") elseif ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
set(system_name "win32") set(system_name "win32")
else () else ()
message(FATAL_ERROR "Unsupported platform: ${CMAKE_SYSTEM_NAME}") string(TOLOWER "${CMAKE_SYSTEM_NAME}" system_name)
endif () endif ()
if ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64") if ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64")
@@ -27,7 +27,7 @@ function(get_platform_details)
elseif ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "arm64") elseif ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "arm64")
set(system_arch "aarch64") set(system_arch "aarch64")
else () else ()
message(FATAL_ERROR "Unsupported architecture: ${CMAKE_SYSTEM_PROCESSOR}") string(TOLOWER "${CMAKE_SYSTEM_PROCESSOR}" system_arch)
endif () endif ()
set(${arg_SYSTEM} ${system_name} PARENT_SCOPE) set(${arg_SYSTEM} ${system_name} PARENT_SCOPE)
+12 -10
View File
@@ -47,16 +47,18 @@ function(add_fx_assembly)
target_compile_definitions(${assembly_target_name} PRIVATE ${def}) target_compile_definitions(${assembly_target_name} PRIVATE ${def})
endforeach (def) endforeach (def)
foreach (ns ${arg_NAMESPACES}) if (fx_enable_tests)
file(GLOB test_sources ${fx_source_root}/${ns}/test/*.c) foreach (ns ${arg_NAMESPACES})
foreach (test_file ${test_sources}) file(GLOB test_sources ${fx_source_root}/${ns}/test/*.c)
get_filename_component(test_name ${test_file} NAME_WE) foreach (test_file ${test_sources})
set(test_name ${ns}-${test_name}) get_filename_component(test_name ${test_file} NAME_WE)
string(REPLACE "." "-" test_name ${test_name}) set(test_name ${ns}-${test_name})
add_executable(${test_name} ${test_file}) string(REPLACE "." "-" test_name ${test_name})
target_link_libraries(${test_name} ${assembly_target_name}) add_executable(${test_name} ${test_file})
endforeach (test_file) target_link_libraries(${test_name} ${assembly_target_name})
endforeach (ns) endforeach (test_file)
endforeach (ns)
endif ()
set_target_properties(${assembly_target_name} PROPERTIES set_target_properties(${assembly_target_name} PROPERTIES
FOLDER "${assembly_name}") FOLDER "${assembly_name}")
+1 -1
View File
@@ -1,6 +1,6 @@
#include "command.h" #include "command.h"
#include <fx/cmd.h> #include <fx/cmdline/cmd.h>
#include <fx/string.h> #include <fx/string.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
+1 -1
View File
@@ -1,6 +1,6 @@
#include "command.h" #include "command.h"
#include <fx/cmd.h> #include <fx/cmdline/cmd.h>
#include <fx/misc.h> #include <fx/misc.h>
#include <fx/string.h> #include <fx/string.h>
#include <fx/term/print.h> #include <fx/term/print.h>
+1 -1
View File
@@ -1,7 +1,7 @@
#include "command.h" #include "command.h"
#include <fx/bst.h> #include <fx/bst.h>
#include <fx/cmd.h> #include <fx/cmdline/cmd.h>
#include <fx/string.h> #include <fx/string.h>
#include <fx/term/print.h> #include <fx/term/print.h>
#include <fx/term/tty.h> #include <fx/term/tty.h>
+1 -1
View File
@@ -2,7 +2,7 @@
#define _FX_COMMAND_H_ #define _FX_COMMAND_H_
#include <fx/bst.h> #include <fx/bst.h>
#include <fx/cmd.h> #include <fx/cmdline/cmd.h>
#include <fx/queue.h> #include <fx/queue.h>
#include <fx/string.h> #include <fx/string.h>
+1 -1
View File
@@ -1,6 +1,6 @@
#include "command.h" #include "command.h"
#include <fx/cmd.h> #include <fx/cmdline/cmd.h>
#include <fx/string.h> #include <fx/string.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
+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 "`");
+13 -5
View File
@@ -1,4 +1,4 @@
#include <fx/cmd.h> #include <fx/cmdline/cmd.h>
#include <stdio.h> #include <stdio.h>
enum { enum {
@@ -27,16 +27,24 @@ const char *text
"great pleasure."; "great pleasure.";
static int test_command( static int test_command(
const fx_command *self, const fx_arglist *opt, const fx_array *args) const fx_command *self,
const fx_arglist *opt,
const fx_array *args)
{ {
printf("Hello, world!\n"); printf("Hello, world!\n");
fx_arglist_iterator it; fx_arglist_iterator it;
fx_arglist_iterator_begin( fx_arglist_iterator_begin(
opt, FX_COMMAND_INVALID_ID, FX_COMMAND_INVALID_ID, &it); opt,
FX_COMMAND_INVALID_ID,
FX_COMMAND_INVALID_ID,
&it);
while (fx_arglist_iterator_is_valid(&it)) { while (fx_arglist_iterator_is_valid(&it)) {
printf("opt:%u,arg:%u,i:%zu,value: %s\n", it.opt_id, printf("opt:%u,arg:%u,i:%zu,value: %s\n",
it.value->val_id, it.i, it.value->val_str); it.opt_id,
it.value->val_id,
it.i,
it.value->val_str);
fx_arglist_iterator_next(&it); fx_arglist_iterator_next(&it);
} }
+6 -8
View File
@@ -17,7 +17,7 @@ struct fx_array_p {
}; };
struct fx_array_iterator_p { struct fx_array_iterator_p {
fx_array *_a; const fx_array *_a;
struct fx_array_p *_a_p; struct fx_array_p *_a_p;
/** The index of the current value */ /** The index of the current value */
@@ -170,9 +170,7 @@ static fx_value array_get(struct fx_array_p *array, size_t at)
return FX_VALUE_EMPTY; return FX_VALUE_EMPTY;
} }
fx_value result; return fx_value_copy_return(array->ar_data[at]);
fx_value_copy(&result, &array->ar_data[at]);
return result;
} }
static size_t array_get_size(const struct fx_array_p *array) static size_t array_get_size(const struct fx_array_p *array)
@@ -373,7 +371,7 @@ static fx_status array_to_string(
/*** ITERATOR FUNCTIONS *******************************************************/ /*** ITERATOR FUNCTIONS *******************************************************/
static fx_iterator *iterable_begin(fx_object *obj) static const fx_iterator *iterable_begin(const fx_object *obj)
{ {
fx_array_iterator *it_obj = fx_object_create(FX_TYPE_ARRAY_ITERATOR); fx_array_iterator *it_obj = fx_object_create(FX_TYPE_ARRAY_ITERATOR);
struct fx_array_iterator_p *it = fx_object_get_private( struct fx_array_iterator_p *it = fx_object_get_private(
@@ -437,7 +435,7 @@ static enum fx_status iterator_erase(fx_iterator *obj)
return FX_SUCCESS; return FX_SUCCESS;
} }
static fx_value iterator_get_value(const fx_iterator *obj) static const fx_value *iterator_get_value(const fx_iterator *obj)
{ {
struct fx_array_iterator_p *it = fx_object_get_private( struct fx_array_iterator_p *it = fx_object_get_private(
obj, obj,
@@ -445,10 +443,10 @@ static fx_value iterator_get_value(const fx_iterator *obj)
struct fx_array_p *array = it->_a_p; struct fx_array_p *array = it->_a_p;
if (it->i >= array->ar_len) { if (it->i >= array->ar_len) {
return FX_VALUE_EMPTY; return NULL;
} }
return *it->value; return it->value;
} }
static enum fx_status iterator_is_valid(const fx_iterator *obj) static enum fx_status iterator_is_valid(const fx_iterator *obj)
+6 -5
View File
@@ -32,8 +32,9 @@ struct fx_hashmap_p {
struct fx_hashmap_iterator_p { struct fx_hashmap_iterator_p {
size_t i; size_t i;
fx_hashmap_item item; fx_hashmap_item item;
fx_value item_value;
fx_hashmap *_h; const fx_hashmap *_h;
struct fx_hashmap_p *_h_p; struct fx_hashmap_p *_h_p;
fx_bst_node *_cbn; fx_bst_node *_cbn;
fx_queue_entry *_cqe; fx_queue_entry *_cqe;
@@ -420,7 +421,7 @@ bool fx_hashmap_is_empty(const fx_hashmap *hashmap)
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_HASHMAP, hashmap_is_empty, hashmap); FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_HASHMAP, hashmap_is_empty, hashmap);
} }
fx_iterator *fx_hashmap_begin(fx_hashmap *hashmap) const fx_iterator *fx_hashmap_begin(const fx_hashmap *hashmap)
{ {
fx_hashmap_iterator *it_obj = fx_object_create( fx_hashmap_iterator *it_obj = fx_object_create(
FX_TYPE_HASHMAP_ITERATOR); FX_TYPE_HASHMAP_ITERATOR);
@@ -617,12 +618,13 @@ static enum fx_status iterator_erase(fx_iterator *obj)
return FX_SUCCESS; return FX_SUCCESS;
} }
static fx_value iterator_get_value(const fx_iterator *obj) static const fx_value *iterator_get_value(const fx_iterator *obj)
{ {
struct fx_hashmap_iterator_p *it = fx_object_get_private( struct fx_hashmap_iterator_p *it = fx_object_get_private(
obj, obj,
FX_TYPE_HASHMAP_ITERATOR); FX_TYPE_HASHMAP_ITERATOR);
return FX_POINTER(&it->item); it->item_value = FX_POINTER(&it->item);
return &it->item_value;
} }
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
@@ -635,7 +637,6 @@ FX_TYPE_CLASS_BEGIN(fx_hashmap)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
FX_INTERFACE_ENTRY(it_begin) = fx_hashmap_begin; FX_INTERFACE_ENTRY(it_begin) = fx_hashmap_begin;
FX_INTERFACE_ENTRY(it_cbegin) = fx_hashmap_cbegin;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_END(fx_hashmap) FX_TYPE_CLASS_END(fx_hashmap)
+196 -71
View File
@@ -1,3 +1,4 @@
#include <assert.h>
#include <fx/collections/hashtable.h> #include <fx/collections/hashtable.h>
#include <fx/misc.h> #include <fx/misc.h>
#include <fx/reflection/property.h> #include <fx/reflection/property.h>
@@ -47,8 +48,9 @@ struct fx_hashtable_p {
struct fx_hashtable_iterator_p { struct fx_hashtable_iterator_p {
size_t i; size_t i;
fx_hashtable_item *item; fx_hashtable_item *item;
fx_value item_value;
fx_hashtable *_h; const fx_hashtable *_h;
struct fx_hashtable_p *_h_p; struct fx_hashtable_p *_h_p;
}; };
@@ -66,25 +68,38 @@ 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;
while (table[index].i_item && i < table_size) { size_t index = base_index, i = 0;
bool index_found = false;
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);
int cmp = fx_value_compare(&item->i_key, v); int cmp = fx_value_compare(&item->i_key, v);
if (cmp == 0) { if (cmp == 0) {
index_found = true;
break; break;
} }
index = (index + (i * i)) % table_size; skip:
index = (base_index + (i * i)) % table_size;
i++; i++;
} }
if (!index_found) {
return FX_ERR_NO_ENTRY;
}
assert(index < table_size);
*out_index = index; *out_index = index;
return FX_SUCCESS; return FX_SUCCESS;
} }
static fx_status find_key( static fx_status allocate_index_for_key(
const fx_value *v, const fx_value *v,
struct table_item *table, struct table_item *table,
size_t table_size, size_t table_size,
@@ -96,15 +111,21 @@ static fx_status find_key(
return status; return status;
} }
bool found = false; size_t index = hash % table_size;
size_t index = hash % table_size, i = 1; bool ok = false;
while (table[index].i_item && i < table_size) {
for (size_t i = 0; i < table_size; i++) {
if (!table[index].i_item) {
ok = true;
break;
}
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);
int cmp = fx_value_compare(&item->i_key, v); int cmp = fx_value_compare(&item->i_key, v);
if (cmp == 0) { if (cmp == 0) {
found = true; ok = true;
break; break;
} }
@@ -112,10 +133,11 @@ static fx_status find_key(
i++; i++;
} }
if (!found) { if (!ok) {
return FX_ERR_NO_ENTRY; return FX_ERR_NO_SPACE;
} }
assert(index < table_size);
*out_index = index; *out_index = index;
return FX_SUCCESS; return FX_SUCCESS;
} }
@@ -126,10 +148,9 @@ static fx_status add_item_to_table(
size_t table_capacity) size_t table_capacity)
{ {
size_t index = 0; size_t index = 0;
struct fx_hashtable_item_p *item_p = fx_object_get_private( struct fx_hashtable_item_p *item_p
item, = fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
FX_TYPE_HASHTABLE_ITEM); fx_status status = allocate_index_for_key(
fx_status status = convert_key_to_index(
&item_p->i_key, &item_p->i_key,
table, table,
table_capacity, table_capacity,
@@ -231,31 +252,57 @@ static fx_status hashtable_put(
if (!FX_OK(status)) { if (!FX_OK(status)) {
return status; return status;
} }
capacity = primes[hashtable->t_max_index];
} }
size_t index = 0; size_t index = 0;
status = convert_key_to_index( do {
key, status = allocate_index_for_key(
hashtable->t_items, key,
capacity, hashtable->t_items,
&index); capacity,
if (!FX_OK(status)) { &index);
return status; if (status == FX_ERR_NO_SPACE) {
} status = resize_table(
hashtable,
hashtable->t_max_index + 1);
if (!FX_OK(status)) {
return status;
}
fx_hashtable_item *item = fx_object_create(FX_TYPE_HASHTABLE_ITEM); capacity = primes[hashtable->t_max_index];
continue;
}
if (!FX_OK(status)) {
return status;
}
break;
} while (1);
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;
} }
@@ -279,9 +326,8 @@ static const fx_value *hashtable_get(
return NULL; return NULL;
} }
const struct fx_hashtable_item_p *item_p = fx_object_get_private( const struct fx_hashtable_item_p *item_p
item, = fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
FX_TYPE_HASHTABLE_ITEM);
return &item_p->i_value; return &item_p->i_value;
} }
@@ -295,14 +341,16 @@ static bool hashtable_is_empty(const struct fx_hashtable_p *hashtable)
return hashtable->t_count == 0; return hashtable->t_count == 0;
} }
static fx_value hashtable_item_get_key(const struct fx_hashtable_item_p *item) static const fx_value *hashtable_item_get_key(
const struct fx_hashtable_item_p *item)
{ {
return item->i_key; return &item->i_key;
} }
static fx_value hashtable_item_get_value(const struct fx_hashtable_item_p *item) static const fx_value *hashtable_item_get_value(
const struct fx_hashtable_item_p *item)
{ {
return item->i_value; return &item->i_value;
} }
/*** PUBLIC FUNCTIONS *********************************************************/ /*** PUBLIC FUNCTIONS *********************************************************/
@@ -347,7 +395,7 @@ bool fx_hashtable_is_empty(const fx_hashtable *hashtable)
hashtable); hashtable);
} }
fx_value fx_hashtable_item_get_key(const fx_hashtable_item *item) const fx_value *fx_hashtable_item_get_key(const fx_hashtable_item *item)
{ {
FX_CLASS_DISPATCH_STATIC_0( FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_HASHTABLE_ITEM, FX_TYPE_HASHTABLE_ITEM,
@@ -355,7 +403,7 @@ fx_value fx_hashtable_item_get_key(const fx_hashtable_item *item)
item); item);
} }
fx_value fx_hashtable_item_get_value(const fx_hashtable_item *item) const fx_value *fx_hashtable_item_get_value(const fx_hashtable_item *item)
{ {
FX_CLASS_DISPATCH_STATIC_0( FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_HASHTABLE_ITEM, FX_TYPE_HASHTABLE_ITEM,
@@ -363,13 +411,12 @@ fx_value fx_hashtable_item_get_value(const fx_hashtable_item *item)
item); item);
} }
fx_iterator *fx_hashtable_begin(fx_hashtable *hashtable) const fx_iterator *fx_hashtable_begin(const fx_hashtable *hashtable)
{ {
fx_hashtable_iterator *it_obj = fx_object_create( fx_hashtable_iterator *it_obj
FX_TYPE_HASHTABLE_ITERATOR); = fx_object_create(FX_TYPE_HASHTABLE_ITERATOR);
struct fx_hashtable_iterator_p *it = fx_object_get_private( struct fx_hashtable_iterator_p *it
it_obj, = fx_object_get_private(it_obj, FX_TYPE_HASHTABLE_ITERATOR);
FX_TYPE_HASHTABLE_ITERATOR);
it->_h = hashtable; it->_h = hashtable;
it->_h_p = fx_object_get_private(hashtable, FX_TYPE_HASHTABLE); it->_h_p = fx_object_get_private(hashtable, FX_TYPE_HASHTABLE);
@@ -387,6 +434,7 @@ fx_iterator *fx_hashtable_begin(fx_hashtable *hashtable)
it->i++; it->i++;
} }
it->item_value = FX_VALUE_OBJECT_REF(table[it->i].i_item);
return it_obj; return it_obj;
} }
@@ -405,6 +453,30 @@ static void hashtable_init(fx_object *obj, void *priv)
static void hashtable_fini(fx_object *obj, void *priv) static void hashtable_fini(fx_object *obj, void *priv)
{ {
struct fx_hashtable_p *map = priv; struct fx_hashtable_p *map = priv;
size_t capacity = primes[map->t_max_index];
size_t nr_destroyed = 0;
for (size_t i = 0; i < capacity; i++) {
if (map->t_items[i].i_item) {
nr_destroyed++;
fx_hashtable_item_unref(map->t_items[i].i_item);
}
}
free(map->t_items);
}
static void hashtable_item_fini(fx_object *obj, void *priv)
{
struct fx_hashtable_item_p *item = priv;
fx_value_unset(&item->i_key);
fx_value_unset(&item->i_value);
}
static void hashtable_iterator_fini(fx_object *obj, void *priv)
{
struct fx_hashtable_iterator_p *it = priv;
fx_value_unset(&it->item_value);
} }
static fx_status to_string( static fx_status to_string(
@@ -412,9 +484,8 @@ static fx_status to_string(
fx_stream *out, fx_stream *out,
const char *format) const char *format)
{ {
struct fx_hashtable_p *hashtable = fx_object_get_private( struct fx_hashtable_p *hashtable
obj->v_object, = fx_object_get_private(obj->v_object, FX_TYPE_HASHTABLE);
FX_TYPE_HASHTABLE);
if (!hashtable->t_count) { if (!hashtable->t_count) {
fx_stream_write_cstr(out, "{}", NULL); fx_stream_write_cstr(out, "{}", NULL);
@@ -434,9 +505,8 @@ static fx_status to_string(
} }
nr_written++; nr_written++;
struct fx_hashtable_item_p *item_p = fx_object_get_private( struct fx_hashtable_item_p *item_p
item, = fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
FX_TYPE_HASHTABLE_ITEM);
const char *cstr = NULL; const char *cstr = NULL;
fx_value_get_cstr(&item_p->i_key, &cstr); fx_value_get_cstr(&item_p->i_key, &cstr);
@@ -476,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,
@@ -487,9 +607,8 @@ static fx_status get_key(
return FX_ERR_NOT_SUPPORTED; return FX_ERR_NOT_SUPPORTED;
} }
struct fx_hashtable_item_p *item_p = fx_object_get_private( struct fx_hashtable_item_p *item_p
item, = fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
FX_TYPE_HASHTABLE_ITEM);
fx_value_copy(out, &item_p->i_key); fx_value_copy(out, &item_p->i_key);
return FX_SUCCESS; return FX_SUCCESS;
@@ -506,9 +625,8 @@ static fx_status get_value(
return FX_ERR_NOT_SUPPORTED; return FX_ERR_NOT_SUPPORTED;
} }
struct fx_hashtable_item_p *item_p = fx_object_get_private( struct fx_hashtable_item_p *item_p
item, = fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
FX_TYPE_HASHTABLE_ITEM);
fx_value_copy(out, &item_p->i_value); fx_value_copy(out, &item_p->i_value);
return FX_SUCCESS; return FX_SUCCESS;
@@ -518,9 +636,10 @@ static fx_status get_value(
static enum fx_status iterator_move_next(const fx_iterator *obj) static enum fx_status iterator_move_next(const fx_iterator *obj)
{ {
struct fx_hashtable_iterator_p *it = fx_object_get_private( struct fx_hashtable_iterator_p *it
obj, = fx_object_get_private(obj, FX_TYPE_HASHTABLE_ITERATOR);
FX_TYPE_HASHTABLE_ITERATOR);
fx_value_unset(&it->item_value);
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];
@@ -533,14 +652,14 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
return FX_ERR_NO_DATA; return FX_ERR_NO_DATA;
} }
it->item_value = FX_VALUE_OBJECT_REF(table[it->i].i_item);
return FX_SUCCESS; return FX_SUCCESS;
} }
static enum fx_status iterator_erase(fx_iterator *obj) static enum fx_status iterator_erase(fx_iterator *obj)
{ {
struct fx_hashtable_iterator_p *it = fx_object_get_private( struct fx_hashtable_iterator_p *it
obj, = fx_object_get_private(obj, FX_TYPE_HASHTABLE_ITERATOR);
FX_TYPE_HASHTABLE_ITERATOR);
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];
@@ -562,19 +681,22 @@ static enum fx_status iterator_erase(fx_iterator *obj)
return FX_SUCCESS; return FX_SUCCESS;
} }
static fx_value iterator_get_value(const fx_iterator *obj) static const fx_value *iterator_get_value(const fx_iterator *obj)
{ {
struct fx_hashtable_iterator_p *it = fx_object_get_private( struct fx_hashtable_iterator_p *it
obj, = fx_object_get_private(obj, FX_TYPE_HASHTABLE_ITERATOR);
FX_TYPE_HASHTABLE_ITERATOR);
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 (table[it->i].i_item) { if (it->i >= capacity) {
return FX_VALUE_OBJECT(table[it->i].i_item); return NULL;
} }
return FX_VALUE_EMPTY; if (table[it->i].i_item) {
return &it->item_value;
}
return NULL;
} }
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
@@ -583,6 +705,7 @@ static 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)
@@ -619,6 +742,7 @@ FX_TYPE_DEFINITION_BEGIN(fx_hashtable_iterator)
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR); FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
FX_TYPE_CLASS(fx_hashtable_iterator_class); FX_TYPE_CLASS(fx_hashtable_iterator_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_hashtable_iterator_p); FX_TYPE_INSTANCE_PRIVATE(struct fx_hashtable_iterator_p);
FX_TYPE_INSTANCE_FINI(hashtable_iterator_fini);
FX_TYPE_DEFINITION_END(fx_hashtable_iterator) FX_TYPE_DEFINITION_END(fx_hashtable_iterator)
// ---- fx_hashtable_item DEFINITION // ---- fx_hashtable_item DEFINITION
@@ -637,4 +761,5 @@ FX_TYPE_DEFINITION_BEGIN(fx_hashtable_item)
FX_TYPE_EXTENDS(FX_TYPE_OBJECT); FX_TYPE_EXTENDS(FX_TYPE_OBJECT);
FX_TYPE_CLASS(fx_hashtable_item_class); FX_TYPE_CLASS(fx_hashtable_item_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_hashtable_item_p); FX_TYPE_INSTANCE_PRIVATE(struct fx_hashtable_item_p);
FX_TYPE_INSTANCE_FINI(hashtable_item_fini);
FX_TYPE_DEFINITION_END(fx_hashtable_item) FX_TYPE_DEFINITION_END(fx_hashtable_item)
@@ -83,7 +83,7 @@ FX_API bool fx_hashmap_has_key(
FX_API size_t fx_hashmap_get_size(const fx_hashmap *hashmap); FX_API size_t fx_hashmap_get_size(const fx_hashmap *hashmap);
FX_API bool fx_hashmap_is_empty(const fx_hashmap *hashmap); FX_API bool fx_hashmap_is_empty(const fx_hashmap *hashmap);
FX_API fx_iterator *fx_hashmap_begin(fx_hashmap *hashmap); FX_API const fx_iterator *fx_hashmap_begin(const fx_hashmap *hashmap);
FX_API const fx_iterator *fx_hashmap_cbegin(const fx_hashmap *hashmap); FX_API const fx_iterator *fx_hashmap_cbegin(const fx_hashmap *hashmap);
FX_DECLS_END; FX_DECLS_END;
@@ -45,10 +45,11 @@ FX_API const fx_value *fx_hashtable_get(
FX_API size_t fx_hashtable_get_count(const fx_hashtable *hashtable); FX_API size_t fx_hashtable_get_count(const fx_hashtable *hashtable);
FX_API bool fx_hashtable_is_empty(const fx_hashtable *hashtable); FX_API bool fx_hashtable_is_empty(const fx_hashtable *hashtable);
FX_API fx_iterator *fx_hashtable_begin(fx_hashtable *hashtable); FX_API const fx_iterator *fx_hashtable_begin(const fx_hashtable *hashtable);
FX_API fx_value fx_hashtable_item_get_key(const fx_hashtable_item *item); FX_API const fx_value *fx_hashtable_item_get_key(const fx_hashtable_item *item);
FX_API fx_value fx_hashtable_item_get_value(const fx_hashtable_item *item); FX_API const fx_value *fx_hashtable_item_get_value(
const fx_hashtable_item *item);
FX_DECLS_END; FX_DECLS_END;
+1 -2
View File
@@ -59,8 +59,7 @@ FX_API void fx_list_delete_all(fx_list *q);
FX_API void *fx_list_entry_value(const fx_list_entry *entry); FX_API void *fx_list_entry_value(const fx_list_entry *entry);
FX_API fx_iterator *fx_list_begin(fx_list *q); FX_API const fx_iterator *fx_list_begin(const fx_list *q);
FX_API const fx_iterator *fx_list_cbegin(const fx_list *q);
FX_DECLS_END; FX_DECLS_END;
+5 -24
View File
@@ -23,6 +23,7 @@ struct fx_list_iterator_p {
size_t i; size_t i;
void *item; void *item;
fx_value item_value;
fx_list_entry *entry; fx_list_entry *entry;
}; };
@@ -401,27 +402,7 @@ void *fx_list_entry_value(const struct fx_list_entry *entry)
return entry ? entry->e_data : NULL; return entry ? entry->e_data : NULL;
} }
fx_iterator *fx_list_begin(fx_list *q) const fx_iterator *fx_list_begin(const fx_list *q)
{
fx_list_iterator *it_obj = fx_object_create(FX_TYPE_LIST_ITERATOR);
struct fx_list_iterator_p *it = fx_object_get_private(
it_obj,
FX_TYPE_LIST_ITERATOR);
it->_q = q;
it->_q_p = fx_object_get_private(q, FX_TYPE_LIST);
it->_q_entry = fx_queue_first(&it->_q_p->l_queue);
it->i = 0;
it->entry = fx_unbox(struct fx_list_entry, it->_q_entry, e_entry);
if (it->entry) {
it->item = it->entry->e_data;
}
return 0;
}
const fx_iterator *fx_list_cbegin(const fx_list *q)
{ {
fx_list_iterator *it_obj = fx_object_create(FX_TYPE_LIST_ITERATOR); fx_list_iterator *it_obj = fx_object_create(FX_TYPE_LIST_ITERATOR);
struct fx_list_iterator_p *it = fx_object_get_private( struct fx_list_iterator_p *it = fx_object_get_private(
@@ -508,13 +489,14 @@ static enum fx_status iterator_erase(fx_iterator *obj)
return FX_SUCCESS; return FX_SUCCESS;
} }
static fx_value iterator_get_value(const fx_iterator *obj) static const fx_value *iterator_get_value(const fx_iterator *obj)
{ {
struct fx_list_iterator_p *it = fx_object_get_private( struct fx_list_iterator_p *it = fx_object_get_private(
obj, obj,
FX_TYPE_LIST_ITERATOR); FX_TYPE_LIST_ITERATOR);
return FX_POINTER(it->item); it->item_value = FX_POINTER(it->item);
return &it->item_value;
} }
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
@@ -527,7 +509,6 @@ FX_TYPE_CLASS_BEGIN(fx_list)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
FX_INTERFACE_ENTRY(it_begin) = fx_list_begin; FX_INTERFACE_ENTRY(it_begin) = fx_list_begin;
FX_INTERFACE_ENTRY(it_cbegin) = fx_list_cbegin;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_END(fx_list) FX_TYPE_CLASS_END(fx_list)
+5 -5
View File
@@ -19,13 +19,13 @@ int main(void)
printf("no value\n"); printf("no value\n");
} }
fx_iterator *it = fx_hashtable_begin(ht); const fx_iterator *it = fx_hashtable_begin(ht);
fx_foreach(val, it) fx_foreach(val, it)
{ {
fx_hashtable_item *item = NULL; fx_hashtable_item *item = NULL;
fx_value_get_object(&val, &item); fx_value_get_object(val, &item);
printf("item %p\n", item); printf("item %p\n", item);
fx_value key, value; const fx_value *key, *value;
key = fx_hashtable_item_get_key(item); key = fx_hashtable_item_get_key(item);
value = fx_hashtable_item_get_value(item); value = fx_hashtable_item_get_value(item);
@@ -34,10 +34,10 @@ int main(void)
fx_foreach(prop_val, prop_it) fx_foreach(prop_val, prop_it)
{ {
fx_property *prop; fx_property *prop;
fx_value_get_object(&prop_val, &prop); fx_value_get_object(prop_val, &prop);
printf("%s = ", fx_property_get_name(prop)); printf("%s = ", fx_property_get_name(prop));
fx_value value = FX_VALUE_EMPTY; fx_value value = FX_VALUE_EMPTY;
fx_property_get_value(prop, &val, &value); fx_property_get_value(prop, val, &value);
fx_value_to_string(&value, fx_stdout, NULL); fx_value_to_string(&value, fx_stdout, NULL);
printf("\n"); printf("\n");
} }
+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
+79 -92
View File
@@ -1,3 +1,5 @@
#define _POSIX_C_SOURCE 200809L
#include "misc.h" #include "misc.h"
#include "posix.h" #include "posix.h"
@@ -35,6 +37,7 @@ struct fx_directory_iterator_p {
fx_directory *root; fx_directory *root;
fx_directory_entry entry; fx_directory_entry entry;
fx_value entry_value;
}; };
/*** PRIVATE FUNCTIONS ********************************************************/ /*** PRIVATE FUNCTIONS ********************************************************/
@@ -50,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(
@@ -64,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);
} }
@@ -86,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;
} }
@@ -106,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;
} }
@@ -126,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;
} }
@@ -146,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);
} }
@@ -167,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);
} }
@@ -254,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)
{ {
@@ -262,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++;
@@ -292,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);
} }
@@ -315,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) {
@@ -323,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;
} }
@@ -331,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)
{ {
@@ -352,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;
} }
@@ -373,9 +375,7 @@ 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; return status;
} }
} }
@@ -411,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,
@@ -429,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,
@@ -438,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,
@@ -451,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(
@@ -462,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,
@@ -512,6 +507,8 @@ static void update_iterator_data(struct fx_directory_iterator_p *it)
fx_path *path = fx_path_create_from_cstr( fx_path *path = fx_path_create_from_cstr(
ent->fts_path + fx_path_length(it->_p->d_path_abs) + 1); 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.filename = ent->fts_name;
it->entry.filepath = path; it->entry.filepath = path;
@@ -559,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;
@@ -570,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,
}; };
@@ -619,12 +615,7 @@ fx_iterator *fx_directory_begin(
return it_obj; return it_obj;
} }
static fx_iterator *iterator_begin(fx_object *obj) static const fx_iterator *iterator_begin(const fx_object *obj)
{
return fx_directory_begin(obj, FX_DIRECTORY_ITERATE_PARENT_FIRST);
}
static const fx_iterator *iterator_cbegin(const fx_object *obj)
{ {
return fx_directory_begin( return fx_directory_begin(
(fx_object *)obj, (fx_object *)obj,
@@ -633,9 +624,8 @@ static const fx_iterator *iterator_cbegin(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;
} }
@@ -683,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);
@@ -698,13 +687,12 @@ static enum fx_status iterator_erase(fx_iterator *obj)
return iterator_move_next(obj); return iterator_move_next(obj);
} }
static 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 FX_POINTER(&it->entry); return &it->entry_value;
} }
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
@@ -717,7 +705,6 @@ FX_TYPE_CLASS_BEGIN(fx_directory)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
FX_INTERFACE_ENTRY(it_begin) = iterator_begin; FX_INTERFACE_ENTRY(it_begin) = iterator_begin;
FX_INTERFACE_ENTRY(it_cbegin) = iterator_cbegin;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_END(fx_directory) FX_TYPE_CLASS_END(fx_directory)
+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)
+41 -47
View File
@@ -37,6 +37,7 @@ struct fx_directory_iterator_p {
fx_directory *root; fx_directory *root;
fx_directory_entry entry; fx_directory_entry entry;
fx_value entry_value;
}; };
/*** PRIVATE FUNCTIONS ********************************************************/ /*** PRIVATE FUNCTIONS ********************************************************/
@@ -52,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(
@@ -89,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;
} }
@@ -109,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;
} }
@@ -129,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;
} }
@@ -150,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);
} }
@@ -170,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);
} }
@@ -263,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++;
@@ -297,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);
} }
@@ -510,6 +517,8 @@ static void update_iterator_data(struct fx_directory_iterator_p *it)
fx_path *path = fx_path_create_from_cstr( fx_path *path = fx_path_create_from_cstr(
ent->fts_path + fx_path_length(it->_p->d_path_abs) + 1); 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.filename = ent->fts_name;
it->entry.filepath = path; it->entry.filepath = path;
@@ -567,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,
}; };
@@ -616,12 +625,7 @@ fx_iterator *fx_directory_begin(
return it_obj; return it_obj;
} }
static fx_iterator *iterator_begin(fx_object *obj) static const fx_iterator *iterator_begin(const fx_object *obj)
{
return fx_directory_begin(obj, FX_DIRECTORY_ITERATE_PARENT_FIRST);
}
static const fx_iterator *iterator_cbegin(const fx_object *obj)
{ {
return fx_directory_begin( return fx_directory_begin(
(fx_object *)obj, (fx_object *)obj,
@@ -692,20 +696,12 @@ static enum fx_status iterator_erase(fx_iterator *obj)
return iterator_move_next(obj); return iterator_move_next(obj);
} }
static fx_iterator_value iterator_get_value(fx_iterator *obj) static const fx_value *iterator_get_value(const fx_iterator *obj)
{ {
struct fx_directory_iterator_p *it struct fx_directory_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR); = fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
return FX_ITERATOR_VALUE_PTR(&it->entry); return &it->entry_value;
}
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
{
struct fx_directory_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
return FX_ITERATOR_VALUE_CPTR(&it->entry);
} }
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
@@ -718,7 +714,6 @@ FX_TYPE_CLASS_BEGIN(fx_directory)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
FX_INTERFACE_ENTRY(it_begin) = iterator_begin; FX_INTERFACE_ENTRY(it_begin) = iterator_begin;
FX_INTERFACE_ENTRY(it_cbegin) = iterator_cbegin;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_END(fx_directory) FX_TYPE_CLASS_END(fx_directory)
@@ -741,7 +736,6 @@ FX_TYPE_CLASS_BEGIN(fx_directory_iterator)
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next; FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
FX_INTERFACE_ENTRY(it_erase) = iterator_erase; FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value; FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
FX_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR) FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_END(fx_directory_iterator) FX_TYPE_CLASS_END(fx_directory_iterator)
+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(
+106 -44
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);
} }
@@ -392,11 +447,18 @@ void path_fini(fx_object *obj, void *priv)
fx_string_unref(path->p_pathstr); fx_string_unref(path->p_pathstr);
} }
void path_to_string(const fx_object *obj, fx_stream *out) 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, FX_TYPE_PATH); struct fx_path_p *path
= fx_object_get_private(obj->v_object, FX_TYPE_PATH);
fx_stream_write_cstr(out, fx_string_get_cstr(path->p_pathstr), NULL); return fx_stream_write_cstr(
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);
}
}
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;
} }
+9 -7
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);
+64 -48
View File
@@ -71,10 +71,12 @@ struct fx_assembly_p {
struct fx_assembly_iterator_p { struct fx_assembly_iterator_p {
fx_namemap_entry *it_cur; fx_namemap_entry *it_cur;
fx_value it_value;
}; };
struct fx_assembly_type_iterator_p { struct fx_assembly_type_iterator_p {
struct type *it_item; struct type *it_item;
fx_value it_value;
}; };
/*** PRIVATE FUNCTIONS ********************************************************/ /*** PRIVATE FUNCTIONS ********************************************************/
@@ -165,11 +167,11 @@ static struct map_item *map_item_next(struct map_item *item)
struct map_bucket *bucket = item->i_entry.e_bucket; struct map_bucket *bucket = item->i_entry.e_bucket;
struct map_entry *next_item = NULL; struct map_entry *next_item = NULL;
if (bucket) { if (bucket) {
struct fx_queue_entry *q_item = fx_queue_next( struct fx_queue_entry *q_item
&item->i_entry.e_entry); = fx_queue_next(&item->i_entry.e_entry);
if (!q_item) { if (!q_item) {
struct fx_bst_node *node = fx_bst_next( struct fx_bst_node *node
&bucket->b_entry.e_node); = fx_bst_next(&bucket->b_entry.e_node);
next_item = fx_unbox(struct map_entry, node, e_node); next_item = fx_unbox(struct map_entry, node, e_node);
} else { } else {
next_item = fx_unbox(struct map_entry, q_item, e_entry); next_item = fx_unbox(struct map_entry, q_item, e_entry);
@@ -185,8 +187,8 @@ static struct map_item *map_item_next(struct map_item *item)
if (next_item->e_type == MAP_ENTRY_BUCKET) { if (next_item->e_type == MAP_ENTRY_BUCKET) {
bucket = (struct map_bucket *)next_item; bucket = (struct map_bucket *)next_item;
struct fx_queue_entry *q_item = fx_queue_first( struct fx_queue_entry *q_item
&bucket->b_items); = fx_queue_first(&bucket->b_items);
next_item = fx_unbox(struct map_entry, q_item, e_entry); next_item = fx_unbox(struct map_entry, q_item, e_entry);
} }
@@ -255,10 +257,8 @@ static void assembly_dump(struct fx_assembly_p *assembly)
fx_bst_node *cur_node = fx_bst_first(&assembly->a_types.m_entries); fx_bst_node *cur_node = fx_bst_first(&assembly->a_types.m_entries);
while (cur_node) { while (cur_node) {
struct map_entry *entry = fx_unbox( struct map_entry *entry
struct map_entry, = fx_unbox(struct map_entry, cur_node, e_node);
cur_node,
e_node);
switch (entry->e_type) { switch (entry->e_type) {
case MAP_ENTRY_ITEM: { case MAP_ENTRY_ITEM: {
struct map_item *item = (struct map_item *)entry; struct map_item *item = (struct map_item *)entry;
@@ -268,8 +268,8 @@ static void assembly_dump(struct fx_assembly_p *assembly)
case MAP_ENTRY_BUCKET: { case MAP_ENTRY_BUCKET: {
struct map_bucket *bucket = (struct map_bucket *)entry; struct map_bucket *bucket = (struct map_bucket *)entry;
fx_queue_entry *cur_qentry = fx_queue_first( fx_queue_entry *cur_qentry
&bucket->b_items); = fx_queue_first(&bucket->b_items);
while (cur_qentry) { while (cur_qentry) {
struct map_item *item = fx_unbox( struct map_item *item = fx_unbox(
struct map_item, struct map_item,
@@ -289,8 +289,8 @@ static void assembly_dump(struct fx_assembly_p *assembly)
static fx_iterator *assembly_get_types(const struct fx_assembly_p *assembly) static fx_iterator *assembly_get_types(const struct fx_assembly_p *assembly)
{ {
fx_assembly_type_iterator *it = fx_object_create( fx_assembly_type_iterator *it
fx_assembly_type_iterator_get_type()); = fx_object_create(fx_assembly_type_iterator_get_type());
if (!it) { if (!it) {
return NULL; return NULL;
} }
@@ -299,12 +299,15 @@ static fx_iterator *assembly_get_types(const struct fx_assembly_p *assembly)
it, it,
fx_assembly_type_iterator_get_type()); fx_assembly_type_iterator_get_type());
struct map_item *item = map_first_item(&assembly->a_types); struct map_item *item = map_first_item(&assembly->a_types);
if (item) { if (!item) {
it_p->it_item = fx_unbox(struct type, item, e_map_item);
} else {
fx_iterator_set_status(it, FX_ERR_NO_DATA); fx_iterator_set_status(it, FX_ERR_NO_DATA);
return it;
} }
it_p->it_item = fx_unbox(struct type, item, e_map_item);
const fx_type *ty = fx_type_get_by_id(it_p->it_item->e_type);
it_p->it_value = FX_VALUE_OBJECT_REF(ty);
return it; return it;
} }
@@ -341,20 +344,25 @@ static void assembly_get_version(
fx_iterator *fx_assembly_get_all(void) fx_iterator *fx_assembly_get_all(void)
{ {
fx_assembly_iterator *it = fx_object_create( fx_assembly_iterator *it
fx_assembly_iterator_get_type()); = fx_object_create(fx_assembly_iterator_get_type());
if (!it) { if (!it) {
return NULL; return NULL;
} }
struct fx_assembly_iterator_p *it_p = fx_object_get_private( struct fx_assembly_iterator_p *it_p
it, = fx_object_get_private(it, fx_assembly_iterator_get_type());
fx_assembly_iterator_get_type());
it_p->it_cur = fx_namemap_first(&assembly_map); it_p->it_cur = fx_namemap_first(&assembly_map);
if (!it_p->it_cur) { if (!it_p->it_cur) {
fx_iterator_set_status(it, FX_ERR_NO_DATA); fx_iterator_set_status(it, FX_ERR_NO_DATA);
return it;
} }
struct fx_assembly_p *assembly
= fx_unbox(struct fx_assembly_p, it_p->it_cur, a_entry);
it_p->it_value = FX_VALUE_OBJECT_REF(assembly->a_self);
return it; return it;
} }
@@ -365,10 +373,8 @@ const fx_assembly *fx_assembly_get_by_name(const char *name)
return NULL; return NULL;
} }
struct fx_assembly_p *assembly = fx_unbox( struct fx_assembly_p *assembly
struct fx_assembly_p, = fx_unbox(struct fx_assembly_p, entry, a_entry);
entry,
a_entry);
return assembly->a_self; return assembly->a_self;
} }
@@ -483,51 +489,61 @@ static enum fx_status type_iterator_move_next(const fx_iterator *obj)
return FX_ERR_NO_DATA; return FX_ERR_NO_DATA;
} }
fx_value_unset(&it->it_value);
struct map_item *next = map_item_next(&it->it_item->e_map_item); struct map_item *next = map_item_next(&it->it_item->e_map_item);
it->it_item = fx_unbox(struct type, next, e_map_item); it->it_item = fx_unbox(struct type, next, e_map_item);
return it->it_item ? FX_SUCCESS : FX_ERR_NO_DATA; if (!it->it_item) {
return FX_ERR_NO_DATA;
}
const fx_type *ty = fx_type_get_by_id(it->it_item->e_type);
it->it_value = FX_VALUE_OBJECT_REF(ty);
return FX_SUCCESS;
} }
static fx_value type_iterator_get_value(const fx_iterator *obj) static const fx_value *type_iterator_get_value(const fx_iterator *obj)
{ {
struct fx_assembly_type_iterator_p *it = fx_object_get_private( struct fx_assembly_type_iterator_p *it = fx_object_get_private(
obj, obj,
fx_assembly_type_iterator_get_type()); fx_assembly_type_iterator_get_type());
if (!it->it_item) { if (!it->it_item) {
return FX_VALUE_EMPTY; return NULL;
} }
const fx_type *ty = fx_type_get_by_id(it->it_item->e_type); return &it->it_value;
return FX_VALUE_OBJECT(ty);
} }
static enum fx_status assembly_iterator_move_next(const fx_iterator *obj) static enum fx_status assembly_iterator_move_next(const fx_iterator *obj)
{ {
struct fx_assembly_iterator_p *it = fx_object_get_private( struct fx_assembly_iterator_p *it
obj, = fx_object_get_private(obj, fx_assembly_iterator_get_type());
fx_assembly_iterator_get_type());
if (!it->it_cur) { if (!it->it_cur) {
return FX_ERR_NO_DATA; return FX_ERR_NO_DATA;
} }
fx_value_unset(&it->it_value);
it->it_cur = fx_namemap_next(&assembly_map, it->it_cur); it->it_cur = fx_namemap_next(&assembly_map, it->it_cur);
return it->it_cur ? FX_SUCCESS : FX_ERR_NO_DATA;
}
static fx_value assembly_iterator_get_value(const fx_iterator *obj)
{
struct fx_assembly_iterator_p *it = fx_object_get_private(
obj,
fx_assembly_iterator_get_type());
if (!it->it_cur) { if (!it->it_cur) {
return FX_VALUE_EMPTY; return FX_ERR_NO_DATA;
} }
struct fx_assembly_p *assembly = fx_unbox( struct fx_assembly_p *assembly
struct fx_assembly_p, = fx_unbox(struct fx_assembly_p, it->it_cur, a_entry);
it->it_cur,
a_entry); it->it_value = FX_VALUE_OBJECT_REF(assembly->a_self);
return FX_VALUE_OBJECT(assembly->a_self); return FX_SUCCESS;
}
static const fx_value *assembly_iterator_get_value(const fx_iterator *obj)
{
struct fx_assembly_iterator_p *it
= fx_object_get_private(obj, fx_assembly_iterator_get_type());
if (!it->it_cur) {
return NULL;
}
return &it->it_value;
} }
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
+198 -67
View File
@@ -1,3 +1,5 @@
#include <fx/type.h>
#include <fx/value-type.h>
#include <platform/callvm.h> #include <platform/callvm.h>
#include <stdlib.h> #include <stdlib.h>
@@ -11,100 +13,229 @@
} }
#endif #endif
void callvm_reset(struct callvm* vm, unsigned int max_fixed_args) void callvm_reset(struct callvm *vm, unsigned int max_fixed_args)
{ {
vm->vm_arg_int_count = 0; vm->vm_arg_int_count = 0;
vm->vm_arg_double_count = 0; vm->vm_arg_double_count = 0;
vm->vm_arg_fixed = max_fixed_args; vm->vm_arg_fixed = max_fixed_args;
vm->vm_arg_excess_count = 0; vm->vm_arg_excess_count = 0;
vm->vm_double_excess_count = 0; vm->vm_double_excess_count = 0;
} }
static void expand_excess(struct callvm* vm) static void expand_excess(struct callvm *vm)
{ {
size_t new_capacity = vm->vm_arg_excess_max * 2; size_t new_capacity = vm->vm_arg_excess_max * 2;
if (!new_capacity) { if (!new_capacity) {
new_capacity = 4; new_capacity = 4;
} }
void* buf = realloc( void *buf = realloc(
vm->vm_arg_excess, vm->vm_arg_excess,
new_capacity * sizeof *vm->vm_arg_excess); new_capacity * sizeof *vm->vm_arg_excess);
if (!buf) { if (!buf) {
return; return;
} }
vm->vm_arg_excess = buf; vm->vm_arg_excess = buf;
vm->vm_arg_excess_max = new_capacity; vm->vm_arg_excess_max = new_capacity;
} }
static void push_excess(struct callvm* vm, uintptr_t value) static void push_excess(struct callvm *vm, uintptr_t value)
{ {
if (vm->vm_arg_excess_count + 1 > vm->vm_arg_excess_max) { if (vm->vm_arg_excess_count + 1 > vm->vm_arg_excess_max) {
expand_excess(vm); expand_excess(vm);
} }
vm->vm_arg_excess[vm->vm_arg_excess_count++] = value; vm->vm_arg_excess[vm->vm_arg_excess_count++] = value;
vm->vm_arg_count++; vm->vm_arg_count++;
} }
static void push_int(struct callvm* vm, uintptr_t value) static void push_int(struct callvm *vm, uintptr_t value)
{ {
if (vm->vm_arg_int_count >= MAX_INT_ARGS) { if (vm->vm_arg_int_count >= MAX_INT_ARGS) {
push_excess(vm, value); push_excess(vm, value);
return; return;
} }
vm->vm_arg_int[vm->vm_arg_int_count++] = value; vm->vm_arg_int[vm->vm_arg_int_count++] = value;
vm->vm_arg_count++; vm->vm_arg_count++;
} }
static void push_double(struct callvm* vm, double value) static void push_double(struct callvm *vm, double value)
{ {
if (vm->vm_arg_double_count >= MAX_DOUBLE_ARGS) { if (vm->vm_arg_double_count >= MAX_DOUBLE_ARGS) {
push_excess(vm, *(uintptr_t*)&value); push_excess(vm, *(uintptr_t *)&value);
return; return;
} }
vm->vm_arg_double[vm->vm_arg_double_count++] = value; vm->vm_arg_double[vm->vm_arg_double_count++] = value;
vm->vm_arg_count++; vm->vm_arg_count++;
if (vm->vm_arg_count > vm->vm_arg_fixed) { if (vm->vm_arg_count > vm->vm_arg_fixed) {
vm->vm_double_excess_count++; vm->vm_double_excess_count++;
} }
} }
void callvm_push(struct callvm* vm, const fx_value* value) void callvm_push(struct callvm *vm, const fx_value *value)
{ {
switch (value->v_type.t_primitive) { if (!fx_type_is_value_type(value->v_type)) {
case FX_VALUE_TYPE_DOUBLE: push_int(vm, (uintptr_t)value->v_object);
push_double(vm, value->v_double); return;
break; }
default:
push_int(vm, (uintptr_t)value->v_pointer); unsigned int value_type = __fx_type_get_value_type(value->v_type);
break;
} switch (value_type) {
case __FX_VALUE_TYPE_BOOL:
push_int(vm, value->v_bool);
break;
case __FX_VALUE_TYPE_I16:
push_int(vm, value->v_i16);
break;
case __FX_VALUE_TYPE_U16:
push_int(vm, value->v_u16);
break;
case __FX_VALUE_TYPE_I32:
push_int(vm, value->v_i32);
break;
case __FX_VALUE_TYPE_U32:
push_int(vm, value->v_u32);
break;
case __FX_VALUE_TYPE_I64:
push_int(vm, value->v_i64);
break;
case __FX_VALUE_TYPE_U64:
push_int(vm, value->v_u64);
break;
case __FX_VALUE_TYPE_IPTR:
push_int(vm, value->v_iptr);
break;
case __FX_VALUE_TYPE_UPTR:
push_int(vm, value->v_uptr);
break;
case __FX_VALUE_TYPE_SBYTE:
push_int(vm, value->v_sbyte);
break;
case __FX_VALUE_TYPE_BYTE:
push_int(vm, value->v_byte);
break;
case __FX_VALUE_TYPE_SHORT:
push_int(vm, value->v_short);
break;
case __FX_VALUE_TYPE_USHORT:
push_int(vm, value->v_ushort);
break;
case __FX_VALUE_TYPE_INT:
push_int(vm, value->v_int);
break;
case __FX_VALUE_TYPE_UINT:
push_int(vm, value->v_uint);
break;
case __FX_VALUE_TYPE_LONG:
push_int(vm, value->v_long);
break;
case __FX_VALUE_TYPE_ULONG:
push_int(vm, value->v_ulong);
break;
case __FX_VALUE_TYPE_LONGLONG:
push_int(vm, value->v_longlong);
break;
case __FX_VALUE_TYPE_ULONGLONG:
push_int(vm, value->v_ulonglong);
break;
case __FX_VALUE_TYPE_SIZE:
push_int(vm, value->v_size);
break;
case __FX_VALUE_TYPE_FLOAT:
push_double(vm, value->v_float);
break;
case __FX_VALUE_TYPE_DOUBLE:
push_double(vm, value->v_double);
break;
case __FX_VALUE_TYPE_CSTR:
push_int(vm, (uintptr_t)value->v_cstr);
break;
case __FX_VALUE_TYPE_POINTER:
push_int(vm, (uintptr_t)value->v_pointer);
break;
default:
break;
}
} }
extern uintptr_t callvm_invoke_i(fx_function_impl impl, struct callvm* vm); extern uintptr_t callvm_invoke_i(fx_function_impl impl, struct callvm *vm);
extern double callvm_invoke_d(fx_function_impl impl, struct callvm* vm); extern double callvm_invoke_d(fx_function_impl impl, struct callvm *vm);
extern void callvm_invoke_v(fx_function_impl impl, struct callvm* vm); extern void callvm_invoke_v(fx_function_impl impl, struct callvm *vm);
fx_value callvm_invoke( fx_value callvm_invoke(
struct callvm* vm, struct callvm *vm,
fx_function_impl impl, fx_function_impl impl,
fx_value_type return_type) fx_type_id return_type)
{ {
switch (return_type) { if (!fx_type_is_value_type(return_type)) {
case FX_VALUE_TYPE_NONE: uintptr_t v = callvm_invoke_i(impl, vm);
callvm_invoke_v(impl, vm); fx_value result = {
break; .v_type = return_type,
case FX_VALUE_TYPE_DOUBLE: .v_object = (fx_object *)v,
return FX_VALUE_DOUBLE(callvm_invoke_d(impl, vm)); };
default: return result;
return FX_VALUE_INT(callvm_invoke_i(impl, vm)); }
}
return FX_VALUE_EMPTY; unsigned int value_type = __fx_type_get_value_type(return_type);
switch (value_type) {
case __FX_VALUE_TYPE_BOOL:
return FX_BOOL(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_I16:
return FX_I16(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_U16:
return FX_U16(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_I32:
return FX_I32(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_U32:
return FX_U32(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_I64:
return FX_I64(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_U64:
return FX_U64(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_IPTR:
return FX_IPTR(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_UPTR:
return FX_UPTR(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_SBYTE:
return FX_SBYTE(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_BYTE:
return FX_BYTE(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_SHORT:
return FX_SHORT(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_USHORT:
return FX_USHORT(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_INT:
return FX_INT(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_UINT:
return FX_UINT(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_LONG:
return FX_LONG(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_ULONG:
return FX_ULONG(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_LONGLONG:
return FX_LONGLONG(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_ULONGLONG:
return FX_ULONGLONG(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_SIZE:
return FX_SIZE(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_FLOAT:
return FX_FLOAT(callvm_invoke_d(impl, vm));
case __FX_VALUE_TYPE_DOUBLE:
return FX_DOUBLE(callvm_invoke_d(impl, vm));
case __FX_VALUE_TYPE_CSTR:
return FX_CSTR((const char *)callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_POINTER:
return FX_POINTER((void *)callvm_invoke_i(impl, vm));
default:
return FX_VALUE_EMPTY;
}
return FX_VALUE_EMPTY;
} }
@@ -32,6 +32,6 @@ extern void callvm_push(struct callvm *vm, const fx_value *value);
extern fx_value callvm_invoke( extern fx_value callvm_invoke(
struct callvm *vm, struct callvm *vm,
fx_function_impl impl, fx_function_impl impl,
fx_value_type return_type); fx_type_id return_type);
#endif #endif
+95
View File
@@ -0,0 +1,95 @@
.extern memcpy
.type memcpy, @function
.global callvm_invoke_i
.type callvm_invoke_i, @function
# %rdi = (function ptr) impl
# %rsi = (struct callvm) context
callvm_invoke_i:
# save the stack frame pointer
push %rbp
mov %rsp, %rbp
# store function pointer for later
push %r12
push %r13
push %r14
push %r15
# move our parameters out of the way
mov %rdi, %r11
mov %rsi, %r12
# calculate the amount of stack space needed for the varargs
movq 32(%r12), %r13
shl $3, %r13
# allocate the stack space
push %rsp
sub %r13, %rsp
andq $0xFFFFFFFFFFFFFFF0, %rsp # re-align the stack
# copy the excess args to the stack
mov %rsp, %rdi
mov 160(%r12), %rsi
mov %r13, %rdx
call memcpy
# Next, set up the fixed integer arguments
movq 48(%r12), %rdi # int arg 0
movq 56(%r12), %rsi # int arg 1
movq 64(%r12), %rdx # int arg 2
movq 72(%r12), %rcx # int arg 3
movq 80(%r12), %r8 # int arg 4
movq 88(%r12), %r9 # int arg 5
# Finally, set up the fixed double arguments
movq 96(%r12), %xmm0 # double arg 0
movq 104(%r12), %xmm1 # double arg 1
movq 112(%r12), %xmm2 # double arg 2
movq 120(%r12), %xmm3 # double arg 3
movq 128(%r12), %xmm4 # double arg 4
movq 136(%r12), %xmm5 # double arg 5
movq 144(%r12), %xmm6 # double arg 6
movq 152(%r12), %xmm7 # double arg 7
# set the number of vararg double parameters
# as required by the ABI
mov 168(%r12), %rax
# call the function implementation
call *%r11
# Restore the stack pointer (deallocating the varargs buffer)
mov -40(%rbp), %rsp
# Restore callee-saved registers
pop %r15
pop %r14
pop %r13
pop %r12
# restore the saved stack frame
pop %rbp
ret
.global callvm_invoke_d
.type callvm_invoke_d, @function
# %rdi = (function ptr) impl
# %rsi = (struct callvm *) context
callvm_invoke_d:
jmp callvm_invoke_i
.global callvm_invoke_v
.type callvm_invoke_v, @function
# %rdi = (function ptr) impl
# %rsi = (struct callvm *) context
callvm_invoke_v:
jmp callvm_invoke_i
+243
View File
@@ -0,0 +1,243 @@
#include <fx/type.h>
#include <fx/value-type.h>
#include <platform/callvm.h>
#include <stdlib.h>
#if 0
switch (arg->v_type.t_primitive) {
case FX_VALUE_TYPE_DOUBLE:
break;
default:
callvm_push_int(&vm, (uintptr_t)arg->v_pointer);
break;
}
#endif
void callvm_reset(struct callvm *vm, unsigned int max_fixed_args)
{
vm->vm_arg_int_count = 0;
vm->vm_arg_double_count = 0;
vm->vm_arg_fixed = max_fixed_args;
vm->vm_arg_excess_count = 0;
vm->vm_double_excess_count = 0;
}
static void expand_excess(struct callvm *vm)
{
size_t new_capacity = vm->vm_arg_excess_max * 2;
if (!new_capacity) {
new_capacity = 4;
}
void *buf = realloc(
vm->vm_arg_excess,
new_capacity * sizeof *vm->vm_arg_excess);
if (!buf) {
return;
}
vm->vm_arg_excess = buf;
vm->vm_arg_excess_max = new_capacity;
}
static void push_excess(struct callvm *vm, uintptr_t value)
{
if (vm->vm_arg_excess_count + 1 > vm->vm_arg_excess_max) {
expand_excess(vm);
}
vm->vm_arg_excess[vm->vm_arg_excess_count++] = value;
vm->vm_arg_count++;
}
static void push_int(struct callvm *vm, uintptr_t value)
{
if (vm->vm_arg_int_count >= MAX_INT_ARGS) {
push_excess(vm, value);
return;
}
vm->vm_arg_int[vm->vm_arg_int_count++] = value;
vm->vm_arg_count++;
}
static void push_double(struct callvm *vm, double value)
{
if (vm->vm_arg_double_count >= MAX_DOUBLE_ARGS) {
push_excess(vm, *(uintptr_t *)&value);
return;
}
vm->vm_arg_double[vm->vm_arg_double_count++] = value;
vm->vm_arg_count++;
if (vm->vm_arg_count > vm->vm_arg_fixed) {
vm->vm_double_excess_count++;
}
}
void callvm_push(struct callvm *vm, const fx_value *value)
{
if (!fx_type_is_value_type(value->v_type)) {
push_int(vm, (uintptr_t)value->v_object);
return;
}
unsigned int value_type = __fx_type_get_value_type(value->v_type);
switch (value_type) {
case __FX_VALUE_TYPE_BOOL:
push_int(vm, value->v_bool);
break;
case __FX_VALUE_TYPE_I16:
push_int(vm, value->v_i16);
break;
case __FX_VALUE_TYPE_U16:
push_int(vm, value->v_u16);
break;
case __FX_VALUE_TYPE_I32:
push_int(vm, value->v_i32);
break;
case __FX_VALUE_TYPE_U32:
push_int(vm, value->v_u32);
break;
case __FX_VALUE_TYPE_I64:
push_int(vm, value->v_i64);
break;
case __FX_VALUE_TYPE_U64:
push_int(vm, value->v_u64);
break;
case __FX_VALUE_TYPE_IPTR:
push_int(vm, value->v_iptr);
break;
case __FX_VALUE_TYPE_UPTR:
push_int(vm, value->v_uptr);
break;
case __FX_VALUE_TYPE_SBYTE:
push_int(vm, value->v_sbyte);
break;
case __FX_VALUE_TYPE_BYTE:
push_int(vm, value->v_byte);
break;
case __FX_VALUE_TYPE_SHORT:
push_int(vm, value->v_short);
break;
case __FX_VALUE_TYPE_USHORT:
push_int(vm, value->v_ushort);
break;
case __FX_VALUE_TYPE_INT:
push_int(vm, value->v_int);
break;
case __FX_VALUE_TYPE_UINT:
push_int(vm, value->v_uint);
break;
case __FX_VALUE_TYPE_LONG:
push_int(vm, value->v_long);
break;
case __FX_VALUE_TYPE_ULONG:
push_int(vm, value->v_ulong);
break;
case __FX_VALUE_TYPE_LONGLONG:
push_int(vm, value->v_longlong);
break;
case __FX_VALUE_TYPE_ULONGLONG:
push_int(vm, value->v_ulonglong);
break;
case __FX_VALUE_TYPE_SIZE:
push_int(vm, value->v_size);
break;
case __FX_VALUE_TYPE_FLOAT:
push_double(vm, value->v_float);
break;
case __FX_VALUE_TYPE_DOUBLE:
push_double(vm, value->v_double);
break;
case __FX_VALUE_TYPE_CSTR:
push_int(vm, (uintptr_t)value->v_cstr);
break;
case __FX_VALUE_TYPE_POINTER:
push_int(vm, (uintptr_t)value->v_pointer);
break;
default:
break;
}
}
extern uintptr_t callvm_invoke_i(fx_function_impl impl, struct callvm *vm);
extern double callvm_invoke_d(fx_function_impl impl, struct callvm *vm);
extern void callvm_invoke_v(fx_function_impl impl, struct callvm *vm);
fx_value callvm_invoke(
struct callvm *vm,
fx_function_impl impl,
fx_type_id return_type)
{
if (!fx_type_is_value_type(return_type)) {
uintptr_t v = callvm_invoke_i(impl, vm);
fx_value result = {
.v_type = return_type,
.v_object = (fx_object *)v,
};
return result;
}
unsigned int value_type = __fx_type_get_value_type(return_type);
switch (value_type) {
case __FX_VALUE_TYPE_BOOL:
return FX_BOOL(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_I16:
return FX_I16(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_U16:
return FX_U16(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_I32:
return FX_I32(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_U32:
return FX_U32(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_I64:
return FX_I64(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_U64:
return FX_U64(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_IPTR:
return FX_IPTR(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_UPTR:
return FX_UPTR(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_SBYTE:
return FX_SBYTE(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_BYTE:
return FX_BYTE(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_SHORT:
return FX_SHORT(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_USHORT:
return FX_USHORT(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_INT:
return FX_INT(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_UINT:
return FX_UINT(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_LONG:
return FX_LONG(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_ULONG:
return FX_ULONG(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_LONGLONG:
return FX_LONGLONG(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_ULONGLONG:
return FX_ULONGLONG(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_SIZE:
return FX_SIZE(callvm_invoke_i(impl, vm));
#if FX_ENABLE_FLOATING_POINT
case __FX_VALUE_TYPE_FLOAT:
return FX_FLOAT(callvm_invoke_d(impl, vm));
case __FX_VALUE_TYPE_DOUBLE:
return FX_DOUBLE(callvm_invoke_d(impl, vm));
#endif
case __FX_VALUE_TYPE_CSTR:
return FX_CSTR((const char *)callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_POINTER:
return FX_POINTER((void *)callvm_invoke_i(impl, vm));
default:
return FX_VALUE_EMPTY;
}
return FX_VALUE_EMPTY;
}
@@ -0,0 +1,37 @@
#ifndef FX_REFLECTION_DARWIN_ARM64_CALLVM_H_
#define FX_REFLECTION_DARWIN_ARM64_CALLVM_H_
#include <fx/reflection/function.h>
#include <fx/value.h>
#include <stdint.h>
#define MAX_FIXED_ARGS ((unsigned int)-1)
#define MAX_DOUBLE_ARGS 8
#define MAX_INT_ARGS 6
/* dyn-dispatch.S depends on the layout of this struct */
struct callvm {
uint64_t vm_arg_int_count;
uint64_t vm_arg_double_count;
/* any args pushed after this limit is reached will be stored in the
* excess buffer. used for calling varargs functions */
uint64_t vm_arg_count, vm_arg_fixed;
uint64_t vm_arg_excess_count;
uint64_t vm_arg_excess_max;
uintptr_t vm_arg_int[MAX_INT_ARGS];
double vm_arg_double[MAX_DOUBLE_ARGS];
uintptr_t *vm_arg_excess;
uint64_t vm_double_excess_count;
};
extern void callvm_reset(struct callvm *vm, unsigned int max_fixed_args);
extern void callvm_push(struct callvm *vm, const fx_value *value);
extern fx_value callvm_invoke(
struct callvm *vm,
fx_function_impl impl,
fx_type_id return_type);
#endif
+47 -31
View File
@@ -31,12 +31,14 @@ struct fx_type_p {
struct fx_type_function_iterator_p { struct fx_type_function_iterator_p {
struct fx_namemap *it_src; struct fx_namemap *it_src;
struct fx_namemap_entry *it_cur; struct fx_namemap_entry *it_cur;
fx_value it_value;
}; };
struct fx_type_property_iterator_p { struct fx_type_property_iterator_p {
const fx_type_info *it_cur_type; const fx_type_info *it_cur_type;
struct fx_namemap *it_src; struct fx_namemap *it_src;
struct fx_namemap_entry *it_cur; struct fx_namemap_entry *it_cur;
fx_value it_value;
}; };
/*** PRIVATE FUNCTIONS ********************************************************/ /*** PRIVATE FUNCTIONS ********************************************************/
@@ -77,8 +79,8 @@ const fx_function *type_get_function(
fx_iterator *type_get_functions(const struct fx_type_p *ty) fx_iterator *type_get_functions(const struct fx_type_p *ty)
{ {
fx_type_function_iterator *it_obj = fx_object_create( fx_type_function_iterator *it_obj
FX_REFLECTION_TYPE_TYPE_FUNCTION_ITERATOR); = fx_object_create(FX_REFLECTION_TYPE_TYPE_FUNCTION_ITERATOR);
struct fx_type_function_iterator_p *it = fx_object_get_private( struct fx_type_function_iterator_p *it = fx_object_get_private(
it_obj, it_obj,
FX_REFLECTION_TYPE_TYPE_FUNCTION_ITERATOR); FX_REFLECTION_TYPE_TYPE_FUNCTION_ITERATOR);
@@ -95,9 +97,8 @@ const fx_property *type_get_property(
{ {
struct fx_type_info *cur = ty->ty_info; struct fx_type_info *cur = ty->ty_info;
while (cur) { while (cur) {
const fx_property *prop = fx_type_info_get_property_by_name( const fx_property *prop
cur, = fx_type_info_get_property_by_name(cur, name);
name);
if (prop) { if (prop) {
return prop; return prop;
} }
@@ -114,8 +115,8 @@ const fx_property *type_get_property(
static fx_iterator *type_get_properties(const struct fx_type_p *ty) static fx_iterator *type_get_properties(const struct fx_type_p *ty)
{ {
fx_type_property_iterator *it_obj = fx_object_create( fx_type_property_iterator *it_obj
FX_REFLECTION_TYPE_TYPE_PROPERTY_ITERATOR); = fx_object_create(FX_REFLECTION_TYPE_TYPE_PROPERTY_ITERATOR);
struct fx_type_property_iterator_p *it = fx_object_get_private( struct fx_type_property_iterator_p *it = fx_object_get_private(
it_obj, it_obj,
FX_REFLECTION_TYPE_TYPE_PROPERTY_ITERATOR); FX_REFLECTION_TYPE_TYPE_PROPERTY_ITERATOR);
@@ -129,8 +130,9 @@ static fx_iterator *type_get_properties(const struct fx_type_p *ty)
break; break;
} }
const struct fx_type_info *next_type = fx_type_info_get_by_id( const struct fx_type_info *next_type
&it->it_cur_type->ty_parent_id); = fx_type_info_get_by_id(&it->it_cur_type
->ty_parent_id);
if (!next_type) { if (!next_type) {
break; break;
} }
@@ -143,6 +145,10 @@ static fx_iterator *type_get_properties(const struct fx_type_p *ty)
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA); fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
} }
struct fx_type_property *prop
= fx_unbox(struct fx_type_property, it->it_cur, p_entry);
it->it_value = FX_VALUE_OBJECT_REF(prop->p_prop);
return it_obj; return it_obj;
} }
@@ -176,9 +182,8 @@ fx_type *__fx_type_create(struct fx_type_info *type_info)
return NULL; return NULL;
} }
struct fx_type_p *p = fx_object_get_private( struct fx_type_p *p
out, = fx_object_get_private(out, FX_REFLECTION_TYPE_TYPE);
FX_REFLECTION_TYPE_TYPE);
p->ty_info = type_info; p->ty_info = type_info;
return out; return out;
@@ -300,25 +305,29 @@ static enum fx_status function_iterator_move_next(const fx_iterator *obj)
return FX_ERR_NO_DATA; return FX_ERR_NO_DATA;
} }
fx_value_unset(&it->it_value);
it->it_cur = fx_namemap_next(it->it_src, it->it_cur); it->it_cur = fx_namemap_next(it->it_src, it->it_cur);
return it->it_cur ? FX_SUCCESS : FX_ERR_NO_DATA; if (!it->it_cur) {
return FX_ERR_NO_DATA;
}
struct fx_type_function *func
= fx_unbox(struct fx_type_function, it->it_cur, f_entry);
it->it_value = FX_VALUE_OBJECT_REF(func->f_func);
return FX_SUCCESS;
} }
static fx_value function_iterator_get_value(const fx_iterator *obj) static const fx_value *function_iterator_get_value(const fx_iterator *obj)
{ {
struct fx_type_function_iterator_p *it = fx_object_get_private( struct fx_type_function_iterator_p *it = fx_object_get_private(
obj, obj,
FX_REFLECTION_TYPE_TYPE_FUNCTION_ITERATOR); FX_REFLECTION_TYPE_TYPE_FUNCTION_ITERATOR);
if (!it->it_cur) { if (!it->it_cur) {
return FX_VALUE_EMPTY; return NULL;
} }
struct fx_type_function *func = fx_unbox( return &it->it_value;
struct fx_type_function,
it->it_cur,
f_entry);
return FX_VALUE_OBJECT(func->f_func);
} }
static enum fx_status property_iterator_move_next(const fx_iterator *obj) static enum fx_status property_iterator_move_next(const fx_iterator *obj)
@@ -337,8 +346,9 @@ static enum fx_status property_iterator_move_next(const fx_iterator *obj)
return FX_ERR_NO_DATA; return FX_ERR_NO_DATA;
} }
const struct fx_type_info *next_type = fx_type_info_get_by_id( const struct fx_type_info *next_type
&it->it_cur_type->ty_parent_id); = fx_type_info_get_by_id(&it->it_cur_type
->ty_parent_id);
if (!next_type) { if (!next_type) {
return FX_ERR_NO_DATA; return FX_ERR_NO_DATA;
} }
@@ -347,24 +357,30 @@ static enum fx_status property_iterator_move_next(const fx_iterator *obj)
it->it_cur = fx_namemap_first(&it->it_cur_type->ty_properties); it->it_cur = fx_namemap_first(&it->it_cur_type->ty_properties);
} }
return it->it_cur ? FX_SUCCESS : FX_ERR_NO_DATA; if (!it->it_cur) {
return FX_ERR_NO_DATA;
}
struct fx_type_property *prop
= fx_unbox(struct fx_type_property, it->it_cur, p_entry);
it->it_value = FX_VALUE_OBJECT_REF(prop->p_prop);
return FX_SUCCESS;
} }
static fx_value property_iterator_get_value(const fx_iterator *obj) static const fx_value *property_iterator_get_value(const fx_iterator *obj)
{ {
struct fx_type_property_iterator_p *it = fx_object_get_private( struct fx_type_property_iterator_p *it = fx_object_get_private(
obj, obj,
FX_REFLECTION_TYPE_TYPE_PROPERTY_ITERATOR); FX_REFLECTION_TYPE_TYPE_PROPERTY_ITERATOR);
if (!it->it_cur) { if (!it->it_cur) {
return FX_VALUE_EMPTY; return NULL;
} }
struct fx_type_property *prop = fx_unbox( struct fx_type_property *prop
struct fx_type_property, = fx_unbox(struct fx_type_property, it->it_cur, p_entry);
it->it_cur,
p_entry);
return FX_VALUE_OBJECT(prop->p_prop); return &it->it_value;
} }
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
+2 -2
View File
@@ -5,7 +5,7 @@
static struct fx_error *bitcode_serialise( static struct fx_error *bitcode_serialise(
fx_serial_ctx *serial, fx_serial_ctx *serial,
fx_object *src, const fx_value *src,
fx_stream *dest, fx_stream *dest,
enum fx_serial_flags flags) enum fx_serial_flags flags)
{ {
@@ -15,7 +15,7 @@ static struct fx_error *bitcode_serialise(
static struct fx_error *bitcode_deserialise( static struct fx_error *bitcode_deserialise(
fx_serial_ctx *serial, fx_serial_ctx *serial,
fx_stream *src, fx_stream *src,
fx_object **dest, fx_value *dest,
enum fx_serial_flags flags) enum fx_serial_flags flags)
{ {
return FX_RESULT_ERR(NOT_SUPPORTED); return FX_RESULT_ERR(NOT_SUPPORTED);
+2 -2
View File
@@ -44,7 +44,7 @@ FX_TYPE_DEFINITION_END(fx_serial_ctx)
fx_result fx_serial_ctx_serialise( fx_result fx_serial_ctx_serialise(
fx_serial_ctx *ctx, fx_serial_ctx *ctx,
fx_object *src, const fx_value *src,
fx_stream *dest, fx_stream *dest,
enum fx_serial_flags flags) enum fx_serial_flags flags)
{ {
@@ -62,7 +62,7 @@ fx_result fx_serial_ctx_serialise(
fx_result fx_serial_ctx_deserialise( fx_result fx_serial_ctx_deserialise(
fx_serial_ctx *ctx, fx_serial_ctx *ctx,
fx_stream *src, fx_stream *src,
fx_object **dest, fx_value *dest,
enum fx_serial_flags flags) enum fx_serial_flags flags)
{ {
FX_CLASS_DISPATCH_VIRTUAL( FX_CLASS_DISPATCH_VIRTUAL(
+5 -4
View File
@@ -6,6 +6,7 @@
#include <fx/object.h> #include <fx/object.h>
#include <fx/status.h> #include <fx/status.h>
#include <fx/stream.h> #include <fx/stream.h>
#include <fx/value.h>
FX_DECLS_BEGIN; FX_DECLS_BEGIN;
@@ -21,13 +22,13 @@ FX_DECLARE_TYPE(fx_serial_ctx);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_serial_ctx) FX_TYPE_CLASS_DECLARATION_BEGIN(fx_serial_ctx)
fx_result (*s_serialise)( fx_result (*s_serialise)(
fx_serial_ctx *, fx_serial_ctx *,
fx_object *, const fx_value *,
fx_stream *, fx_stream *,
fx_serial_flags); fx_serial_flags);
fx_result (*s_deserialise)( fx_result (*s_deserialise)(
fx_serial_ctx *, fx_serial_ctx *,
fx_stream *, fx_stream *,
fx_object **, fx_value *,
fx_serial_flags); fx_serial_flags);
FX_TYPE_CLASS_DECLARATION_END(fx_serial_ctx) FX_TYPE_CLASS_DECLARATION_END(fx_serial_ctx)
@@ -39,14 +40,14 @@ FX_API fx_type_id fx_serial_ctx_get_type(void);
FX_API fx_result fx_serial_ctx_serialise( FX_API fx_result fx_serial_ctx_serialise(
fx_serial_ctx *ctx, fx_serial_ctx *ctx,
fx_object *src, const fx_value *src,
fx_stream *dest, fx_stream *dest,
fx_serial_flags flags); fx_serial_flags flags);
FX_API fx_result fx_serial_ctx_deserialise( FX_API fx_result fx_serial_ctx_deserialise(
fx_serial_ctx *ctx, fx_serial_ctx *ctx,
fx_stream *src, fx_stream *src,
fx_object **dest, fx_value *dest,
fx_serial_flags flags); fx_serial_flags flags);
FX_DECLS_END; FX_DECLS_END;
+53 -75
View File
@@ -1,24 +1,26 @@
#include <fx/bool.h> #include <fx/bool.h>
#include <fx/collections/array.h> #include <fx/collections/array.h>
#include <fx/collections/datetime.h> #include <fx/collections/hashtable.h>
#include <fx/datetime.h>
#include <fx/float.h> #include <fx/float.h>
#include <fx/int.h> #include <fx/int.h>
#include <fx/serial/ctx.h> #include <fx/serial/ctx.h>
#include <fx/serial/toml.h> #include <fx/serial/toml.h>
#include <fx/string.h> #include <fx/string.h>
#include <inttypes.h> #include <inttypes.h>
#include <math.h>
void write_tagged_value(fx_object *data); void write_tagged_value(const fx_value *data);
void write_raw_string(const fx_string *data) void write_raw_string(const fx_string *data)
{ {
fx_stream_write_cstr(fx_stdout, "\"", NULL); fx_stream_write_cstr(fx_stdout, "\"", NULL);
const fx_iterator *it = fx_iterator_cbegin(data); const fx_iterator *it = fx_iterator_begin(data);
fx_foreach(val, it) fx_foreach(val, it)
{ {
fx_wchar c; fx_wchar c;
fx_value_get_wchar(&val, &c); fx_value_get_wchar(val, &c);
if (c >= 0x10000) { if (c >= 0x10000) {
c -= 0x10000; c -= 0x10000;
long hi = 0xD800 | ((c >> 10) & 0x3FF); long hi = 0xD800 | ((c >> 10) & 0x3FF);
@@ -54,75 +56,49 @@ void write_tagged_string(fx_string *data)
fx_stream_write_cstr(fx_stdout, " }", NULL); fx_stream_write_cstr(fx_stdout, " }", NULL);
} }
void write_tagged_integer(fx_int *data) void write_tagged_integer(const fx_value *data)
{ {
#if 0
fx_stream_write_cstr( fx_stream_write_cstr(
fx_stdout, fx_stdout,
"{ \"type\": \"integer\", \"value\": \"", "{ \"type\": \"integer\", \"value\": \"",
NULL); NULL);
fx_stream_write_fmt(fx_stdout, NULL, "%lld", data->v_i64, NULL);
if (fx_int_is_inf_positive(data)) {
fx_stream_write_cstr(fx_stdout, "inf", NULL);
} else if (fx_int_is_inf_negative(data)) {
fx_stream_write_cstr(fx_stdout, "-inf", NULL);
} else if (fx_int_is_nan_positive(data)) {
fx_stream_write_cstr(fx_stdout, "nan", NULL);
} else if (fx_int_is_nan_negative(data)) {
fx_stream_write_cstr(fx_stdout, "-nan", NULL);
} else {
fx_stream_write_fmt(
fx_stdout,
NULL,
"%lld",
fx_int_get_value(data),
NULL);
}
fx_stream_write_cstr(fx_stdout, "\" }", NULL); fx_stream_write_cstr(fx_stdout, "\" }", NULL);
#endif
} }
void write_tagged_float(fx_double *data) void write_tagged_float(const fx_value *data)
{ {
#if 0
fx_stream_write_cstr( fx_stream_write_cstr(
fx_stdout, fx_stdout,
"{ \"type\": \"float\", \"value\": \"", "{ \"type\": \"float\", \"value\": \"",
NULL); NULL);
if (fx_double_is_inf_positive(data)) { double d = data->v_double;
if (d == INFINITY) {
fx_stream_write_cstr(fx_stdout, "inf", NULL); fx_stream_write_cstr(fx_stdout, "inf", NULL);
} else if (fx_double_is_inf_negative(data)) { } else if (d == -INFINITY) {
fx_stream_write_cstr(fx_stdout, "-inf", NULL); fx_stream_write_cstr(fx_stdout, "-inf", NULL);
} else if (fx_double_is_nan_positive(data)) { } else if (d == NAN) {
fx_stream_write_cstr(fx_stdout, "nan", NULL); fx_stream_write_cstr(fx_stdout, "nan", NULL);
} else if (fx_double_is_nan_negative(data)) { } else if (d == -NAN) {
fx_stream_write_cstr(fx_stdout, "-nan", NULL); fx_stream_write_cstr(fx_stdout, "-nan", NULL);
} else if (d <= 1e-9 || d >= 1e9) {
fx_stream_write_fmt(fx_stdout, NULL, "%g", d, NULL);
} else { } else {
double v = fx_double_get_value(data); fx_stream_write_fmt(fx_stdout, NULL, "%.9lf", d, NULL);
if ((v <= 0.00000001 && v > 0) || (v >= -0.00000001 && v < 0)
|| (v >= 1000000000) || (v <= -1000000000)) {
fx_stream_write_fmt(fx_stdout, NULL, "%.15e", v, NULL);
} else {
fx_stream_write_fmt(fx_stdout, NULL, "%.15f", v, NULL);
}
} }
fx_stream_write_cstr(fx_stdout, "\" }", NULL); fx_stream_write_cstr(fx_stdout, "\" }", NULL);
#endif
} }
void write_tagged_bool(fx_bool *data) void write_tagged_bool(const fx_value *data)
{ {
#if 0
fx_stream_write_fmt( fx_stream_write_fmt(
fx_stdout, fx_stdout,
NULL, NULL,
"{ \"type\": \"bool\", \"value\": \"%s\" }", "{ \"type\": \"bool\", \"value\": \"%s\" }",
fx_bool_get_value(data) ? "true" : "false", data->v_bool ? "true" : "false",
NULL); NULL);
#endif
} }
void write_tagged_datetime(fx_datetime *data) void write_tagged_datetime(fx_datetime *data)
@@ -152,75 +128,78 @@ void write_tagged_datetime(fx_datetime *data)
fx_stream_write_cstr(fx_stdout, "\", \"value\": \"", NULL); fx_stream_write_cstr(fx_stdout, "\", \"value\": \"", NULL);
fx_string *new_data = fx_string_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_string_get_cstr(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);
fx_string_unref(new_data); fx_stringstream_unref(new_data);
} }
#if 0 void write_tagged_hashtable(fx_hashtable *data)
void write_tagged_dict(fx_dict *data)
{ {
fx_stream_write_cstr(fx_stdout, "{ ", NULL); fx_stream_write_cstr(fx_stdout, "{ ", NULL);
int i = 0; int i = 0;
fx_iterator *it = fx_iterator_begin(data); const fx_iterator *it = fx_iterator_begin(data);
fx_foreach(fx_dict_item *, item, it) fx_foreach(v, it)
{ {
fx_hashtable_item *item;
fx_value_get_object(v, &item);
if (i++ > 0) { if (i++ > 0) {
fx_stream_write_cstr(fx_stdout, ", ", NULL); fx_stream_write_cstr(fx_stdout, ", ", NULL);
} }
write_raw_string(item->key); const fx_value *key = fx_hashtable_item_get_key(item);
const fx_value *value = fx_hashtable_item_get_value(item);
fx_string *key_str;
fx_value_get_object(key, &key_str);
write_raw_string(key_str);
fx_stream_write_cstr(fx_stdout, ": ", NULL); fx_stream_write_cstr(fx_stdout, ": ", NULL);
write_tagged_value(item->value); write_tagged_value(value);
} }
fx_iterator_unref(it); fx_iterator_unref(it);
fx_stream_write_cstr(fx_stdout, " }", NULL); fx_stream_write_cstr(fx_stdout, " }", NULL);
} }
#endif
void write_tagged_array(fx_array *data) void write_tagged_array(fx_array *data)
{ {
fx_stream_write_cstr(fx_stdout, "[ ", NULL); fx_stream_write_cstr(fx_stdout, "[ ", NULL);
int i = 0; int i = 0;
fx_iterator *it = fx_iterator_begin(data); const fx_iterator *it = fx_iterator_begin(data);
fx_foreach(val, it) fx_foreach(val, it)
{ {
fx_object *obj;
fx_value_get_object(&val, &obj);
if (i++ > 0) { if (i++ > 0) {
fx_stream_write_cstr(fx_stdout, ", ", NULL); fx_stream_write_cstr(fx_stdout, ", ", NULL);
} }
write_tagged_value(obj); write_tagged_value(val);
} }
fx_iterator_unref(it); fx_iterator_unref(it);
fx_stream_write_cstr(fx_stdout, " ]", NULL); fx_stream_write_cstr(fx_stdout, " ]", NULL);
} }
void write_tagged_value(fx_object *data) void write_tagged_value(const fx_value *data)
{ {
#if 0 if (fx_value_is_type(data, FX_TYPE_HASHTABLE)) {
if (fx_object_is_type(data, FX_TYPE_DICT)) { write_tagged_hashtable(data->v_object);
write_tagged_dict(data); } else if (fx_value_is_type(data, FX_TYPE_ARRAY)) {
} else write_tagged_array(data->v_object);
#endif } else if (fx_value_is_type(data, FX_TYPE_STRING)) {
if (fx_object_is_type(data, FX_TYPE_ARRAY)) { write_tagged_string(data->v_object);
write_tagged_array(data); } else if (fx_value_is_type(data, FX_TYPE_DATETIME)) {
} else if (fx_object_is_type(data, FX_TYPE_STRING)) { write_tagged_datetime(data->v_object);
write_tagged_string(data); } else if (fx_value_is_type(data, FX_TYPE_BOOL)) {
} else if (fx_object_is_type(data, FX_TYPE_DATETIME)) {
write_tagged_datetime(data);
} else if (fx_object_is_type(data, FX_TYPE_BOOL)) {
write_tagged_bool(data); write_tagged_bool(data);
} else if (fx_value_is_type(data, FX_TYPE_I64)) {
write_tagged_integer(data);
} else if (fx_value_is_type(data, FX_TYPE_DOUBLE)) {
write_tagged_float(data);
} }
} }
@@ -231,19 +210,18 @@ int main(void)
fx_serial_ctx *ctx = fx_toml_serial_ctx_create(); fx_serial_ctx *ctx = fx_toml_serial_ctx_create();
fx_object *data; fx_value data;
fx_result result = fx_serial_ctx_deserialise(ctx, src, &data, 0); fx_result result = fx_serial_ctx_deserialise(ctx, src, &data, 0);
if (fx_result_is_error(result)) { if (fx_result_is_error(result)) {
fx_throw(result); return 1;
return -1;
} }
write_tagged_value(data); write_tagged_value(&data);
fx_stream_write_char(fx_stdout, '\n'); fx_stream_write_char(fx_stdout, '\n');
fx_serial_ctx_unref(ctx); fx_serial_ctx_unref(ctx);
fx_object_unref(data); fx_value_unset(&data);
return 0; return 0;
} }
+135 -183
View File
@@ -1,8 +1,8 @@
#include <fx/bool.h> #include <fx/bool.h>
#include <fx/collections/array.h> #include <fx/collections/array.h>
#include <fx/collections/datetime.h>
#include <fx/collections/hashmap.h> #include <fx/collections/hashmap.h>
#include <fx/collections/hashtable.h> #include <fx/collections/hashtable.h>
#include <fx/datetime.h>
#include <fx/error.h> #include <fx/error.h>
#include <fx/float.h> #include <fx/float.h>
#include <fx/int.h> #include <fx/int.h>
@@ -11,6 +11,7 @@
#include <fx/status.h> #include <fx/status.h>
#include <fx/string.h> #include <fx/string.h>
#include <fx/stringstream.h> #include <fx/stringstream.h>
#include <math.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -116,9 +117,9 @@ struct ctx {
fx_stream *ctx_src; fx_stream *ctx_src;
fx_string *ctx_wordbuf; fx_string *ctx_wordbuf;
fx_string *ctx_linebuf; fx_string *ctx_linebuf;
fx_iterator *ctx_linebuf_ptr; const fx_iterator *ctx_linebuf_ptr;
fx_result ctx_result; fx_result ctx_result;
fx_hashmap *ctx_objects_flags; fx_hashtable *ctx_objects_flags;
fx_queue ctx_tokens; fx_queue ctx_tokens;
}; };
@@ -132,29 +133,18 @@ static void ctx_set_object_flags(
return; return;
} }
fx_hashmap_key key = { fx_value ptr = FX_POINTER(obj);
.key_data = obj, const fx_value *old_value
.key_size = sizeof(fx_object *), = fx_hashtable_get(ctx->ctx_objects_flags, &ptr);
.key_flags = FX_HASHMAP_KEY_F_INTVALUE,
};
const fx_hashmap_value *old_value = fx_hashmap_get(
ctx->ctx_objects_flags,
&key);
enum object_flags new_flags = 0; enum object_flags new_flags = 0;
if (old_value) { if (old_value) {
new_flags = (enum object_flags)(uintptr_t)old_value->value_data; new_flags = (enum object_flags)old_value->v_i32;
} }
new_flags |= flags; new_flags |= flags;
fx_hashmap_value value = { fx_hashtable_put(ctx->ctx_objects_flags, &ptr, &FX_I32(new_flags));
.value_data = (void *)new_flags,
.value_size = sizeof new_flags,
};
fx_hashmap_put(ctx->ctx_objects_flags, &key, &value);
} }
static void ctx_clear_object_flags( static void ctx_clear_object_flags(
@@ -166,29 +156,18 @@ static void ctx_clear_object_flags(
return; return;
} }
fx_hashmap_key key = { fx_value ptr = FX_POINTER(obj);
.key_data = obj, const fx_value *old_value
.key_size = sizeof(fx_object *), = fx_hashtable_get(ctx->ctx_objects_flags, &ptr);
.key_flags = FX_HASHMAP_KEY_F_INTVALUE,
};
const fx_hashmap_value *old_value = fx_hashmap_get(
ctx->ctx_objects_flags,
&key);
enum object_flags new_flags = 0; enum object_flags new_flags = 0;
if (old_value) { if (old_value) {
new_flags = (enum object_flags)(uintptr_t)old_value->value_data; new_flags = (enum object_flags)old_value->v_i32;
} }
new_flags &= ~mask; new_flags &= ~mask;
fx_hashmap_value value = { fx_hashtable_put(ctx->ctx_objects_flags, &ptr, &FX_I32(new_flags));
.value_data = (void *)new_flags,
.value_size = sizeof new_flags,
};
fx_hashmap_put(ctx->ctx_objects_flags, &key, &value);
} }
static enum object_flags ctx_get_object_flags(struct ctx *ctx, fx_object *obj) static enum object_flags ctx_get_object_flags(struct ctx *ctx, fx_object *obj)
@@ -197,17 +176,13 @@ static enum object_flags ctx_get_object_flags(struct ctx *ctx, fx_object *obj)
return 0; return 0;
} }
fx_hashmap_key key = { fx_value ptr = FX_POINTER(obj);
.key_data = obj, const fx_value *old_value
.key_size = sizeof(fx_object *), = fx_hashtable_get(ctx->ctx_objects_flags, &ptr);
.key_flags = FX_HASHMAP_KEY_F_INTVALUE,
};
const fx_hashmap_value *value = fx_hashmap_get( enum object_flags new_flags = 0;
ctx->ctx_objects_flags, if (old_value) {
&key); return (enum object_flags)old_value->v_i32;
if (value) {
return (enum object_flags)(uintptr_t)value->value_data;
} }
return 0; return 0;
@@ -297,7 +272,7 @@ static fx_wchar advance_char(struct ctx *ctx)
const char *s = fx_string_get_cstr(ctx->ctx_linebuf); const char *s = fx_string_get_cstr(ctx->ctx_linebuf);
fx_wchar c = fx_iterator_get_value(ctx->ctx_linebuf_ptr).v_int; fx_wchar c = fx_iterator_get_value(ctx->ctx_linebuf_ptr)->v_wchar;
if (!is_valid_char(c)) { if (!is_valid_char(c)) {
ctx->ctx_result = FX_RESULT_ERR(BAD_FORMAT); ctx->ctx_result = FX_RESULT_ERR(BAD_FORMAT);
@@ -330,7 +305,7 @@ static fx_wchar peek_char(struct ctx *ctx)
const char *s = fx_string_get_cstr(ctx->ctx_linebuf); const char *s = fx_string_get_cstr(ctx->ctx_linebuf);
fx_wchar c = fx_iterator_get_value(ctx->ctx_linebuf_ptr).v_int; fx_wchar c = fx_iterator_get_value(ctx->ctx_linebuf_ptr)->v_wchar;
if (!is_valid_char(c)) { if (!is_valid_char(c)) {
ctx->ctx_result = FX_RESULT_ERR(BAD_FORMAT); ctx->ctx_result = FX_RESULT_ERR(BAD_FORMAT);
@@ -929,7 +904,7 @@ static void split_word(struct ctx *ctx, fx_string *wordbuf)
fx_foreach(tokv, it) fx_foreach(tokv, it)
{ {
const char *tok = NULL; const char *tok = NULL;
fx_value_get_cstr(&tokv, &tok); fx_value_get_cstr(tokv, &tok);
if (i > 0) { if (i > 0) {
enqueue_token(ctx, TOK_DOT); enqueue_token(ctx, TOK_DOT);
@@ -1032,11 +1007,11 @@ static void read_word(struct ctx *ctx)
return; return;
} }
fx_iterator *it = fx_iterator_begin(wordbuf); const fx_iterator *it = fx_iterator_begin(wordbuf);
fx_foreach(cv, it) fx_foreach(cv, it)
{ {
fx_wchar c = FX_WCHAR_INVALID; fx_wchar c = FX_WCHAR_INVALID;
fx_value_get_wchar(&cv, &c); fx_value_get_wchar(cv, &c);
/* only allow ASCII numbers/letters here */ /* only allow ASCII numbers/letters here */
bool ok = isalnum(c) || c == '_' || c == '-' || c == '.'; bool ok = isalnum(c) || c == '_' || c == '-' || c == '.';
if (!ok) { if (!ok) {
@@ -1184,8 +1159,8 @@ static void read_string(struct ctx *ctx, bool squote)
} }
if (c != '\n') { if (c != '\n') {
ctx->ctx_result = FX_RESULT_ERR( ctx->ctx_result
BAD_FORMAT); = FX_RESULT_ERR(BAD_FORMAT);
fail = true; fail = true;
break; break;
} }
@@ -1230,18 +1205,16 @@ static void read_string(struct ctx *ctx, bool squote)
case 'U': case 'U':
c = read_unicode_sequence(ctx); c = read_unicode_sequence(ctx);
if (c == FX_WCHAR_INVALID) { if (c == FX_WCHAR_INVALID) {
ctx->ctx_result = FX_RESULT_ERR( ctx->ctx_result
BAD_FORMAT); = FX_RESULT_ERR(BAD_FORMAT);
fail = true; fail = true;
break; break;
} }
ctx->ctx_result = FX_OK(fx_string_append_wc( ctx->ctx_result
str, = FX_OK(fx_string_append_wc(str, c))
c)) ? FX_SUCCESS
? FX_SUCCESS : FX_RESULT_ERR(BAD_FORMAT);
: FX_RESULT_ERR(
BAD_FORMAT);
fail = !FX_OK(ctx->ctx_result); fail = !FX_OK(ctx->ctx_result);
break; break;
default: default:
@@ -1482,14 +1455,24 @@ static fx_result advance_token(struct ctx *ctx)
start: start:
c = peek_char(ctx); c = peek_char(ctx);
while (isspace(c) && c != '\n' && c != '\r') {
advance_char(ctx); if (c <= 0) {
c = peek_char(ctx); if (fx_error_get_status_code(ctx->ctx_result)
== FX_ERR_NO_DATA) {
ctx->ctx_flags |= CTX_EOF;
}
return ctx->ctx_result;
} }
if (c == -1) { if (c <= 31 && c != '\n' && c != '\r' && c != '\t') {
ctx->ctx_flags |= CTX_EOF; ctx->ctx_result = FX_RESULT_ERR(BAD_FORMAT);
return FX_RESULT_ERR(NO_DATA); return ctx->ctx_result;
}
while (fx_wchar_is_space(c) && c != '\n' && c != '\r') {
advance_char(ctx);
c = peek_char(ctx);
} }
#if 1 #if 1
@@ -1559,7 +1542,7 @@ static void ctx_cleanup(struct ctx *ctx)
} }
if (ctx->ctx_objects_flags) { if (ctx->ctx_objects_flags) {
fx_hashmap_unref(ctx->ctx_objects_flags); fx_hashtable_unref(ctx->ctx_objects_flags);
ctx->ctx_objects_flags = NULL; ctx->ctx_objects_flags = NULL;
} }
} }
@@ -1571,14 +1554,14 @@ static fx_result ctx_init(struct ctx *ctx)
ctx->ctx_linebuf = fx_string_create(); ctx->ctx_linebuf = fx_string_create();
ctx->ctx_wordbuf = fx_string_create(); ctx->ctx_wordbuf = fx_string_create();
ctx->ctx_objects_flags = fx_hashmap_create(NULL, NULL); ctx->ctx_objects_flags = fx_hashtable_create();
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
static fx_result toml_serialise( static fx_result toml_serialise(
fx_serial_ctx *serial, fx_serial_ctx *serial,
fx_object *src, const fx_value *src,
fx_stream *dest, fx_stream *dest,
enum fx_serial_flags flags) enum fx_serial_flags flags)
{ {
@@ -1672,20 +1655,20 @@ static void print_token(struct token *tok)
} }
} }
static fx_result parse_value(struct ctx *ctx, fx_object **result); static fx_result parse_value(struct ctx *ctx, fx_value *result);
static fx_result parse_key_value_pair(struct ctx *ctx, fx_hashtable *container); static fx_result parse_key_value_pair(struct ctx *ctx, fx_hashtable *container);
static fx_result parse_timestamp(struct ctx *ctx, fx_object **result) static fx_result parse_timestamp(struct ctx *ctx, fx_value *result)
{ {
struct token *tok = peek_token(ctx); struct token *tok = peek_token(ctx);
fx_datetime *dt = tok->tok_value.time; fx_datetime *dt = tok->tok_value.time;
tok->tok_value.time = NULL; tok->tok_value.time = NULL;
*result = (dt); *result = FX_VALUE_OBJECT(dt);
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
static fx_result parse_string(struct ctx *ctx, fx_object **result) static fx_result parse_string(struct ctx *ctx, fx_value *result)
{ {
struct token *tok = peek_token(ctx); struct token *tok = peek_token(ctx);
fx_string *str = fx_string_duplicate(tok->tok_str); fx_string *str = fx_string_duplicate(tok->tok_str);
@@ -1693,81 +1676,48 @@ static fx_result parse_string(struct ctx *ctx, fx_object **result)
return FX_RESULT_ERR(NO_MEMORY); return FX_RESULT_ERR(NO_MEMORY);
} }
*result = (str); *result = FX_VALUE_OBJECT(str);
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
static fx_result parse_int(struct ctx *ctx, fx_object **result) static fx_result parse_int(struct ctx *ctx, fx_value *result)
{ {
#if 0
struct token *tok = peek_token(ctx); struct token *tok = peek_token(ctx);
fx_int *val = fx_int_create(tok->tok_value.i.v); *result = FX_I64(tok->tok_value.i.v);
if (!val) {
return FX_RESULT_ERR(NO_MEMORY);
}
if (tok->tok_value.i.inf) {
if (tok->tok_value.i.v >= 0) {
fx_int_set_value_inf(val);
} else {
fx_int_set_value_inf_negative(val);
}
} else if (tok->tok_value.i.nan) {
if (tok->tok_value.i.v >= 0) {
fx_int_set_value_nan(val);
} else {
fx_int_set_value_nan_negative(val);
}
}
*result = (val);
#endif
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
static fx_result parse_float(struct ctx *ctx, fx_object **result) static fx_result parse_float(struct ctx *ctx, fx_value *result)
{ {
#if 0
struct token *tok = peek_token(ctx); struct token *tok = peek_token(ctx);
fx_double *val = fx_double_create(tok->tok_value.f.v);
if (!val) {
return FX_RESULT_ERR(NO_MEMORY);
}
if (tok->tok_value.f.inf) { if (tok->tok_value.f.inf) {
if (tok->tok_value.f.v >= 0) { if (tok->tok_value.f.v >= 0) {
fx_double_set_value_inf(val); *result = FX_DOUBLE(INFINITY);
} else { } else {
fx_double_set_value_inf_negative(val); *result = FX_DOUBLE(-INFINITY);
} }
} else if (tok->tok_value.f.nan) { } else if (tok->tok_value.f.nan) {
if (tok->tok_value.f.v >= 0) { if (tok->tok_value.f.v >= 0) {
fx_double_set_value_nan(val); *result = FX_DOUBLE(NAN);
} else { } else {
fx_double_set_value_nan_negative(val); *result = FX_DOUBLE(-NAN);
} }
} else {
*result = FX_DOUBLE(tok->tok_value.f.v);
} }
*result = (val);
#endif
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
static fx_result parse_bool(struct ctx *ctx, fx_object **result) static fx_result parse_bool(struct ctx *ctx, fx_value *result)
{ {
#if 0
struct token *tok = peek_token(ctx); struct token *tok = peek_token(ctx);
fx_bool *val = fx_bool_create(tok->tok_value.b); *result = FX_BOOL(tok->tok_value.b);
if (!val) {
return FX_RESULT_ERR(NO_MEMORY);
}
*result = (val);
#endif
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
static fx_result parse_table_inline(struct ctx *ctx, fx_object **result) static fx_result parse_table_inline(struct ctx *ctx, fx_value *result)
{ {
DISABLE_EXTENDED_LEXING(ctx); DISABLE_EXTENDED_LEXING(ctx);
@@ -1780,7 +1730,7 @@ static fx_result parse_table_inline(struct ctx *ctx, fx_object **result)
struct token *tok = peek_token(ctx); struct token *tok = peek_token(ctx);
if (tok && tok->tok_type == TOK_RIGHT_BRACE) { if (tok && tok->tok_type == TOK_RIGHT_BRACE) {
*result = (table); *result = FX_VALUE_OBJECT(table);
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
@@ -1814,7 +1764,7 @@ static fx_result parse_table_inline(struct ctx *ctx, fx_object **result)
} }
} }
*result = (table); *result = FX_VALUE_OBJECT(table);
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
@@ -1828,7 +1778,7 @@ static void skip_newlines(struct ctx *ctx)
} }
} }
static fx_result parse_array_inline(struct ctx *ctx, fx_object **result) static fx_result parse_array_inline(struct ctx *ctx, fx_value *result)
{ {
bool done = false; bool done = false;
ENABLE_EXTENDED_LEXING(ctx); ENABLE_EXTENDED_LEXING(ctx);
@@ -1865,16 +1815,15 @@ static fx_result parse_array_inline(struct ctx *ctx, fx_object **result)
break; break;
} }
fx_object *value; fx_value value;
fx_result status = parse_value(ctx, &value); fx_result status = parse_value(ctx, &value);
if (!FX_OK(status)) { if (!FX_OK(status)) {
fx_array_unref(array); fx_array_unref(array);
return status; return status;
} }
#if 0 fx_array_push_back(array, value);
fx_array_append(array, FX_RV(value)); fx_value_unset(&value);
#endif
ENABLE_EXTENDED_LEXING(ctx); ENABLE_EXTENDED_LEXING(ctx);
advance_token(ctx); advance_token(ctx);
@@ -1897,13 +1846,12 @@ static fx_result parse_array_inline(struct ctx *ctx, fx_object **result)
} }
DISABLE_EXTENDED_LEXING(ctx); DISABLE_EXTENDED_LEXING(ctx);
*result = (array); *result = FX_VALUE_OBJECT(array);
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
static fx_result parse_value(struct ctx *ctx, fx_object **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);
@@ -1948,9 +1896,13 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_hashtable *container)
} }
while (tok && tok->tok_type == TOK_DOT) { while (tok && tok->tok_type == TOK_DOT) {
const fx_value *child = fx_hashtable_get( const fx_value *child
container, = fx_hashtable_get(container, &FX_VALUE_OBJECT(key));
&FX_VALUE_OBJECT(key)); if (child && !fx_value_is_type(child, FX_TYPE_HASHTABLE)) {
fx_string_unref(key);
return FX_RESULT_ERR(BAD_FORMAT);
}
fx_hashmap *subtable = NULL; fx_hashmap *subtable = NULL;
fx_value_get_object(child, &subtable); fx_value_get_object(child, &subtable);
if (!subtable) { if (!subtable) {
@@ -1959,18 +1911,13 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_hashtable *container)
container, container,
&FX_VALUE_OBJECT(key), &FX_VALUE_OBJECT(key),
&FX_VALUE_OBJECT(subtable)); &FX_VALUE_OBJECT(subtable));
} else if (
subtable
&& !fx_object_is_type(subtable, FX_TYPE_HASHTABLE)) {
free(key);
return FX_RESULT_ERR(BAD_FORMAT);
} }
#if 1 #if 1
enum object_flags flags = ctx_get_object_flags(ctx, subtable); enum object_flags flags = ctx_get_object_flags(ctx, subtable);
if (flags if (flags
& (OBJECT_KV_END_DEFINED | OBJECT_HEADER_END_DEFINED)) { & (OBJECT_KV_END_DEFINED | OBJECT_HEADER_END_DEFINED)) {
free(key); fx_string_unref(key);
return FX_RESULT_ERR(BAD_FORMAT); return FX_RESULT_ERR(BAD_FORMAT);
} }
#endif #endif
@@ -1980,7 +1927,7 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_hashtable *container)
advance_token(ctx); advance_token(ctx);
tok = peek_token(ctx); tok = peek_token(ctx);
if (!IS_VALID_KEY_COMPONENT(tok)) { if (!IS_VALID_KEY_COMPONENT(tok)) {
free(key); fx_string_unref(key);
return FX_RESULT_ERR(BAD_FORMAT); return FX_RESULT_ERR(BAD_FORMAT);
} }
@@ -2010,7 +1957,7 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_hashtable *container)
ENABLE_EXTENDED_LEXING(ctx); ENABLE_EXTENDED_LEXING(ctx);
advance_token(ctx); advance_token(ctx);
fx_object *value = NULL; fx_value value = FX_VALUE_EMPTY;
fx_result result = parse_value(ctx, &value); fx_result result = parse_value(ctx, &value);
DISABLE_EXTENDED_LEXING(ctx); DISABLE_EXTENDED_LEXING(ctx);
@@ -2025,17 +1972,17 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_hashtable *container)
return fx_result_propagate(result); return fx_result_propagate(result);
} }
fx_hashtable_put( fx_hashtable_put(container, &FX_VALUE_OBJECT(key), &value);
container,
&FX_VALUE_OBJECT(key),
&FX_VALUE_OBJECT(value));
fx_object_unref(value);
if (fx_object_is_type(value, FX_TYPE_HASHTABLE) if (fx_value_is_type(&value, FX_TYPE_HASHTABLE)
|| fx_object_is_type(value, FX_TYPE_ARRAY)) { || fx_value_is_type(&value, FX_TYPE_ARRAY)) {
ctx_set_object_flags(ctx, value, OBJECT_KV_END_DEFINED); ctx_set_object_flags(
ctx,
value.v_object,
OBJECT_KV_END_DEFINED);
} }
fx_value_unset(&value);
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
@@ -2062,26 +2009,24 @@ static fx_result parse_table_header(
} }
while (tok && tok->tok_type == TOK_DOT) { while (tok && tok->tok_type == TOK_DOT) {
const fx_value *value = fx_hashtable_get( const fx_value *value
container, = fx_hashtable_get(container, &FX_VALUE_OBJECT(key));
&FX_VALUE_OBJECT(key));
fx_object *subtable = NULL; fx_object *subtable = NULL;
fx_value_get_object(value, &subtable); fx_value_get_object(value, &subtable);
enum object_flags flags = ctx_get_object_flags(ctx, subtable); enum object_flags flags = ctx_get_object_flags(ctx, subtable);
if (!subtable) { if (!value) {
subtable = (fx_hashtable_create()); subtable = (fx_hashtable_create());
fx_hashtable_put( fx_hashtable_put(
container, container,
&FX_VALUE_OBJECT(key), &FX_VALUE_OBJECT(key),
&FX_VALUE_OBJECT(subtable)); &FX_VALUE_OBJECT(subtable));
fx_object_unref(subtable); fx_object_unref(subtable);
} else if (fx_object_is_type(subtable, FX_TYPE_ARRAY)) { } else if (fx_value_is_type(value, FX_TYPE_ARRAY)) {
#if 0 value = fx_array_get_ref(
subtable = fx_array_at(
subtable, subtable,
fx_array_size(subtable) - 1); fx_array_get_size(subtable) - 1);
#endif fx_value_get_object(value, &subtable);
} else if (!fx_object_is_type(subtable, FX_TYPE_HASHTABLE)) { } else if (!fx_value_is_type(value, FX_TYPE_HASHTABLE)) {
return FX_RESULT_ERR(BAD_FORMAT); return FX_RESULT_ERR(BAD_FORMAT);
} }
@@ -2112,9 +2057,12 @@ static fx_result parse_table_header(
return FX_RESULT_ERR(BAD_FORMAT); return FX_RESULT_ERR(BAD_FORMAT);
} }
const fx_value *child = fx_hashtable_get( const fx_value *child
container, = fx_hashtable_get(container, &FX_VALUE_OBJECT(key));
&FX_VALUE_OBJECT(key)); if (child && !fx_value_is_type(child, FX_TYPE_HASHTABLE)) {
return FX_RESULT_ERR(BAD_FORMAT);
}
fx_hashtable *new_table = NULL; fx_hashtable *new_table = NULL;
fx_value_get_object(child, &new_table); fx_value_get_object(child, &new_table);
@@ -2122,7 +2070,7 @@ static fx_result parse_table_header(
new_table = fx_hashtable_create(); new_table = fx_hashtable_create();
if (!new_table) { if (!new_table) {
free(key); fx_string_unref(key);
return FX_RESULT_ERR(NO_MEMORY); return FX_RESULT_ERR(NO_MEMORY);
} }
@@ -2175,26 +2123,29 @@ static fx_result parse_array_header(
} }
while (tok && tok->tok_type == TOK_DOT) { while (tok && tok->tok_type == TOK_DOT) {
const fx_value *child = fx_hashtable_get( const fx_value *child
container, = fx_hashtable_get(container, &FX_VALUE_OBJECT(key));
&FX_VALUE_OBJECT(key));
fx_object *sub_dict = NULL; fx_object *sub_dict = NULL;
fx_value_get_object(child, &sub_dict); fx_value_get_object(child, &sub_dict);
if (!sub_dict) { enum object_flags flags = ctx_get_object_flags(ctx, (sub_dict));
if (flags & OBJECT_KV_END_DEFINED) {
return FX_RESULT_ERR(BAD_FORMAT);
}
if (!child) {
sub_dict = (fx_hashtable_create()); sub_dict = (fx_hashtable_create());
fx_hashtable_put( fx_hashtable_put(
container, container,
&FX_VALUE_OBJECT(key), &FX_VALUE_OBJECT(key),
&FX_VALUE_OBJECT(sub_dict)); &FX_VALUE_OBJECT(sub_dict));
fx_hashtable_unref(sub_dict); fx_hashtable_unref(sub_dict);
} else if (fx_object_is_type(sub_dict, FX_TYPE_ARRAY)) { } else if (fx_value_is_type(child, FX_TYPE_ARRAY)) {
#if 0 child = fx_array_get_ref(
sub_dict = fx_array_at(
sub_dict, sub_dict,
fx_array_size(sub_dict) - 1); fx_array_get_size(sub_dict) - 1);
#endif sub_dict = child->v_object;
} else if (!fx_object_is_type(sub_dict, FX_TYPE_HASHTABLE)) { } else if (!fx_value_is_type(child, FX_TYPE_HASHTABLE)) {
return FX_RESULT_ERR(BAD_FORMAT); return FX_RESULT_ERR(BAD_FORMAT);
} }
@@ -2219,9 +2170,12 @@ static fx_result parse_array_header(
return FX_RESULT_ERR(BAD_FORMAT); return FX_RESULT_ERR(BAD_FORMAT);
} }
const fx_value *child = fx_hashtable_get( const fx_value *child
container, = fx_hashtable_get(container, &FX_VALUE_OBJECT(key));
&FX_VALUE_OBJECT(key)); if (child && !fx_value_is_type(child, FX_TYPE_ARRAY)) {
return FX_RESULT_ERR(BAD_FORMAT);
}
fx_array *array = NULL; fx_array *array = NULL;
fx_value_get_object(child, &array); fx_value_get_object(child, &array);
@@ -2234,11 +2188,11 @@ static fx_result parse_array_header(
} else if (!fx_object_is_type(array, FX_TYPE_ARRAY)) { } else if (!fx_object_is_type(array, FX_TYPE_ARRAY)) {
return FX_RESULT_ERR(BAD_FORMAT); return FX_RESULT_ERR(BAD_FORMAT);
} }
free(key); fx_string_unref(key);
enum object_flags flags = ctx_get_object_flags(ctx, (array)); enum object_flags flags = ctx_get_object_flags(ctx, (array));
if (flags & OBJECT_KV_END_DEFINED) { if (flags & OBJECT_KV_END_DEFINED) {
return FX_RESULT_ERR(NO_MEMORY); return FX_RESULT_ERR(BAD_FORMAT);
} }
fx_hashtable *new_table = fx_hashtable_create(); fx_hashtable *new_table = fx_hashtable_create();
@@ -2246,9 +2200,7 @@ static fx_result parse_array_header(
return FX_RESULT_ERR(NO_MEMORY); return FX_RESULT_ERR(NO_MEMORY);
} }
#if 0 fx_array_push_back(array, FX_VALUE_OBJECT(new_table));
fx_array_append(array, FX_RV(new_table));
#endif
advance_token(ctx); advance_token(ctx);
*new_container = new_table; *new_container = new_table;
@@ -2330,7 +2282,7 @@ static fx_result parse_root(struct ctx *ctx, fx_hashtable **out)
static fx_result toml_deserialise( static fx_result toml_deserialise(
fx_serial_ctx *serial, fx_serial_ctx *serial,
fx_stream *src, fx_stream *src,
fx_object **dest, fx_value *dest,
enum fx_serial_flags flags) enum fx_serial_flags flags)
{ {
struct ctx ctx = {0}; struct ctx ctx = {0};
@@ -2351,7 +2303,7 @@ static fx_result toml_deserialise(
} }
if (ctx.ctx_flags & CTX_EOF) { if (ctx.ctx_flags & CTX_EOF) {
*dest = (fx_hashtable_create()); *dest = FX_VALUE_OBJECT(fx_hashtable_create());
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
@@ -2361,7 +2313,7 @@ static fx_result toml_deserialise(
return fx_result_propagate(ctx.ctx_result); return fx_result_propagate(ctx.ctx_result);
} }
*dest = (data); *dest = FX_VALUE_OBJECT(data);
#if 0 #if 0
ctx.ctx_flags ctx.ctx_flags
= CTX_ENABLE_NUMBERS | CTX_ENABLE_TIMESTAMPS | CTX_ENABLE_BOOLS; = CTX_ENABLE_NUMBERS | CTX_ENABLE_TIMESTAMPS | CTX_ENABLE_BOOLS;
+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)
+9 -1
View File
@@ -359,7 +359,7 @@ fx_status cstr_to_size(const char *in, size_t *out)
return FX_ERR_BAD_STATE; return FX_ERR_BAD_STATE;
} }
if (v > SIZE_T_MAX) { if (v > SIZE_MAX) {
return FX_ERR_BAD_STATE; return FX_ERR_BAD_STATE;
} }
@@ -369,6 +369,7 @@ fx_status cstr_to_size(const char *in, size_t *out)
fx_status cstr_to_float(const char *in, float *out) fx_status cstr_to_float(const char *in, float *out)
{ {
#if FX_ENABLE_FLOATING_POINT
char *ep; char *ep;
float v = strtof(in, &ep); float v = strtof(in, &ep);
if (*ep) { if (*ep) {
@@ -377,10 +378,14 @@ fx_status cstr_to_float(const char *in, float *out)
*out = v; *out = v;
return FX_SUCCESS; return FX_SUCCESS;
#else
return FX_ERR_NOT_SUPPORTED;
#endif
} }
fx_status cstr_to_double(const char *in, double *out) fx_status cstr_to_double(const char *in, double *out)
{ {
#if FX_ENABLE_FLOATING_POINT
char *ep; char *ep;
double v = strtod(in, &ep); double v = strtod(in, &ep);
if (*ep) { if (*ep) {
@@ -389,6 +394,9 @@ fx_status cstr_to_double(const char *in, double *out)
*out = v; *out = v;
return FX_SUCCESS; return FX_SUCCESS;
#else
return FX_ERR_NOT_SUPPORTED;
#endif
} }
fx_status cstr_to_cstr(const char *in, const char **out) fx_status cstr_to_cstr(const char *in, const char **out)
+4 -5
View File
@@ -1,4 +1,4 @@
#include <fx/collections/datetime.h> #include <fx/datetime.h>
#include <fx/stream.h> #include <fx/stream.h>
#include <fx/string.h> #include <fx/string.h>
#include <fx/value.h> #include <fx/value.h>
@@ -339,7 +339,7 @@ static enum fx_status encode_rfc3339(
dt->dt_sec); dt->dt_sec);
if (dt->dt_msec > 0) { if (dt->dt_msec > 0) {
fx_stream_write_fmt(out, NULL, ".%04ld", dt->dt_msec); fx_stream_write_fmt(out, NULL, ".%03ld", dt->dt_msec);
} }
if (!dt->dt_localtime) { if (!dt->dt_localtime) {
@@ -572,9 +572,8 @@ static fx_status _datetime_to_string(
fx_stream *out, fx_stream *out,
const char *format) const char *format)
{ {
struct fx_datetime_p *dt = fx_object_get_private( struct fx_datetime_p *dt
obj->v_object, = fx_object_get_private(obj->v_object, FX_TYPE_DATETIME);
FX_TYPE_DATETIME);
if (dt->dt_has_date) { if (dt->dt_has_date) {
fx_stream_write_fmt( fx_stream_write_fmt(
+3 -6
View File
@@ -3,7 +3,7 @@
bool fx_wchar_is_number(fx_wchar c) bool fx_wchar_is_number(fx_wchar c)
{ {
return iswdigit((wchar_t)c); return (c >= '0' && c <= '9');
} }
bool fx_wchar_is_alpha(fx_wchar c) bool fx_wchar_is_alpha(fx_wchar c)
@@ -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;
} }
+1
View File
@@ -94,6 +94,7 @@ FX_TYPE_DEFINITION_BEGIN(fx_double)
FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE); FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE);
FX_TYPE_IMPLEMENTS(FX_TYPE_CONVERTIBLE); FX_TYPE_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE); FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE);
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
FX_TYPE_NAME("fx.double"); FX_TYPE_NAME("fx.double");
FX_TYPE_CLASS(fx_double_class); FX_TYPE_CLASS(fx_double_class);
FX_TYPE_INSTANCE_PRIVATE(double); FX_TYPE_INSTANCE_PRIVATE(double);
+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
+6 -13
View File
@@ -10,15 +10,10 @@
FX_DECLS_BEGIN; FX_DECLS_BEGIN;
#define fx_foreach(var, iterator) \ #define fx_foreach(var, iterator) \
for (fx_value var = fx_iterator_get_value(iterator); \ for (const fx_value *var = fx_iterator_get_value(iterator); \
FX_OK(fx_iterator_get_status(iterator)); \ FX_OK(fx_iterator_get_status(iterator)); \
fx_iterator_move_next(iterator), \ fx_iterator_move_next(iterator), \
var = fx_iterator_get_value(iterator)) var = fx_iterator_get_value(iterator))
#define fx_foreach_ptr(type, var, iterator) \
for (type *var = (type *)fx_iterator_get_value(iterator).v_ptr; \
FX_OK(fx_iterator_get_status(iterator)); \
fx_iterator_move_next(iterator), \
var = (type *)fx_iterator_get_value(iterator).v_ptr)
#define FX_ITERATOR_VALUE_INT(v) ((fx_iterator_value) {.v_int = (v)}) #define FX_ITERATOR_VALUE_INT(v) ((fx_iterator_value) {.v_int = (v)})
#define FX_ITERATOR_VALUE_PTR(v) ((fx_iterator_value) {.v_ptr = (v)}) #define FX_ITERATOR_VALUE_PTR(v) ((fx_iterator_value) {.v_ptr = (v)})
@@ -36,12 +31,11 @@ FX_DECLARE_TYPE(fx_iterable);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_iterator) FX_TYPE_CLASS_DECLARATION_BEGIN(fx_iterator)
fx_status (*it_move_next)(const fx_iterator *); fx_status (*it_move_next)(const fx_iterator *);
fx_status (*it_erase)(fx_iterator *); fx_status (*it_erase)(fx_iterator *);
fx_value (*it_get_value)(const fx_iterator *); const fx_value *(*it_get_value)(const fx_iterator *);
FX_TYPE_CLASS_DECLARATION_END(fx_iterator) FX_TYPE_CLASS_DECLARATION_END(fx_iterator)
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_iterable) FX_TYPE_CLASS_DECLARATION_BEGIN(fx_iterable)
fx_iterator *(*it_begin)(fx_iterable *); const fx_iterator *(*it_begin)(const fx_iterable *);
const fx_iterator *(*it_cbegin)(const fx_iterable *);
FX_TYPE_CLASS_DECLARATION_END(fx_iterable) FX_TYPE_CLASS_DECLARATION_END(fx_iterable)
FX_API fx_type_id fx_iterator_get_type(void); FX_API fx_type_id fx_iterator_get_type(void);
@@ -56,15 +50,14 @@ static inline void fx_iterator_unref(const fx_iterator *p)
fx_object_unref((fx_object *)p); fx_object_unref((fx_object *)p);
} }
FX_API fx_iterator *fx_iterator_begin(fx_iterable *it); FX_API const fx_iterator *fx_iterator_begin(const fx_iterable *it);
FX_API const fx_iterator *fx_iterator_cbegin(const fx_iterable *it);
FX_API fx_status fx_iterator_get_status(const fx_iterator *it); FX_API fx_status fx_iterator_get_status(const fx_iterator *it);
FX_API fx_status FX_API fx_status
fx_iterator_set_status(const fx_iterator *it, fx_status status); fx_iterator_set_status(const fx_iterator *it, fx_status status);
FX_API fx_status fx_iterator_move_next(const fx_iterator *it); FX_API fx_status fx_iterator_move_next(const fx_iterator *it);
FX_API fx_value fx_iterator_get_value(const fx_iterator *it); FX_API const fx_value *fx_iterator_get_value(const fx_iterator *it);
FX_API fx_status fx_iterator_erase(fx_iterator *it); FX_API fx_status fx_iterator_erase(fx_iterator *it);
static inline bool fx_iterator_is_valid(const fx_iterator *it) static inline bool fx_iterator_is_valid(const fx_iterator *it)
{ {
+4 -1
View File
@@ -26,7 +26,10 @@ typedef struct fx_namemap_entry {
const char *e_name; const char *e_name;
} fx_namemap_entry; } fx_namemap_entry;
FX_API fx_status fx_namemap_cleanup(fx_namemap *map); typedef void (*fx_namemap_cleanup_callback)(fx_namemap_entry *);
FX_API fx_status
fx_namemap_cleanup(fx_namemap *map, fx_namemap_cleanup_callback callback);
FX_API fx_status FX_API fx_status
fx_namemap_put(fx_namemap *map, const char *name, fx_namemap_entry *entry); fx_namemap_put(fx_namemap *map, const char *name, fx_namemap_entry *entry);
+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);
+5
View File
@@ -45,6 +45,11 @@
#define FX_WSTR(v) __FX_VALUE_CREATE(WSTR, v_wstr, v) #define FX_WSTR(v) __FX_VALUE_CREATE(WSTR, v_wstr, v)
#define FX_CSTR(v) __FX_VALUE_CREATE(CSTR, v_cstr, v) #define FX_CSTR(v) __FX_VALUE_CREATE(CSTR, v_cstr, v)
#define FX_POINTER(v) __FX_VALUE_CREATE(POINTER, v_pointer, v) #define FX_POINTER(v) __FX_VALUE_CREATE(POINTER, v_pointer, v)
#define FX_VALUE_OBJECT_REF(v) \
((fx_value) { \
.v_type = fx_object_query_type((fx_object *)v), \
.v_object = fx_object_ref((fx_object *)(v)), \
})
#define FX_VALUE_OBJECT(v) \ #define FX_VALUE_OBJECT(v) \
((fx_value) { \ ((fx_value) { \
.v_type = fx_object_query_type((fx_object *)v), \ .v_type = fx_object_query_type((fx_object *)v), \
+31 -19
View File
@@ -23,8 +23,12 @@ static enum fx_status iterator_set_status(
/*** PUBLIC FUNCTIONS *********************************************************/ /*** PUBLIC FUNCTIONS *********************************************************/
fx_iterator *fx_iterator_begin(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,
@@ -33,23 +37,21 @@ fx_iterator *fx_iterator_begin(fx_iterable *it)
it); it);
} }
const fx_iterator *fx_iterator_cbegin(const fx_iterable *it)
{
FX_CLASS_DISPATCH_VIRTUAL_0(
fx_iterable,
FX_TYPE_ITERABLE,
NULL,
it_cbegin,
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,
@@ -59,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);
} }
@@ -74,23 +79,30 @@ enum fx_status fx_iterator_move_next(const fx_iterator *it)
return status; return status;
} }
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,
FX_VALUE_EMPTY, NULL,
it_get_value, it_get_value,
it); 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);
} }
+63 -29
View File
@@ -45,8 +45,50 @@ static struct map_bucket *map_item_convert_to_bucket(
return bucket; return bucket;
} }
enum fx_status fx_namemap_cleanup(struct fx_namemap *map) static void cleanup_bucket(
struct map_bucket *bucket,
fx_namemap_cleanup_callback callback)
{ {
fx_queue_entry *cur = fx_queue_pop_front(&bucket->b_items);
while (cur) {
struct __fx_namemap_entry *entry
= fx_unbox(struct __fx_namemap_entry, cur, e_entry);
struct fx_namemap_entry *item
= (struct fx_namemap_entry *)entry;
if (callback) {
callback(item);
}
cur = fx_queue_pop_front(&bucket->b_items);
}
free(bucket);
}
enum fx_status fx_namemap_cleanup(
struct fx_namemap *map,
fx_namemap_cleanup_callback callback)
{
fx_bst_node *cur_node = fx_bst_first(&map->m_entries);
while (cur_node) {
struct __fx_namemap_entry *entry
= fx_unbox(struct __fx_namemap_entry, cur_node, e_node);
fx_bst_delete(&map->m_entries, cur_node);
if (entry->e_flags == MAP_ENTRY_BUCKET) {
struct map_bucket *bucket = (struct map_bucket *)entry;
cleanup_bucket(bucket, callback);
} else {
struct fx_namemap_entry *item
= (struct fx_namemap_entry *)entry;
if (callback) {
callback(item);
}
}
cur_node = fx_bst_first(&map->m_entries);
}
return FX_SUCCESS; return FX_SUCCESS;
} }
@@ -137,10 +179,8 @@ fx_namemap_entry *fx_namemap_get(const fx_namemap *map, const char *name)
struct fx_namemap_entry *fx_namemap_first(const struct fx_namemap *map) struct fx_namemap_entry *fx_namemap_first(const struct fx_namemap *map)
{ {
struct fx_bst_node *first = fx_bst_first(&map->m_entries); struct fx_bst_node *first = fx_bst_first(&map->m_entries);
struct __fx_namemap_entry *entry = fx_unbox( struct __fx_namemap_entry *entry
struct __fx_namemap_entry, = fx_unbox(struct __fx_namemap_entry, first, e_node);
first,
e_node);
if (!entry) { if (!entry) {
return NULL; return NULL;
} }
@@ -158,10 +198,8 @@ struct fx_namemap_entry *fx_namemap_first(const struct fx_namemap *map)
struct fx_namemap_entry *fx_namemap_last(const struct fx_namemap *map) struct fx_namemap_entry *fx_namemap_last(const struct fx_namemap *map)
{ {
struct fx_bst_node *last = fx_bst_last(&map->m_entries); struct fx_bst_node *last = fx_bst_last(&map->m_entries);
struct __fx_namemap_entry *entry = fx_unbox( struct __fx_namemap_entry *entry
struct __fx_namemap_entry, = fx_unbox(struct __fx_namemap_entry, last, e_node);
last,
e_node);
if (entry->e_flags == MAP_ENTRY_ITEM) { if (entry->e_flags == MAP_ENTRY_ITEM) {
return (struct fx_namemap_entry *)entry; return (struct fx_namemap_entry *)entry;
} }
@@ -179,11 +217,11 @@ struct fx_namemap_entry *fx_namemap_next(
struct map_bucket *bucket = entry->e_opaque.e_bucket; struct map_bucket *bucket = entry->e_opaque.e_bucket;
struct __fx_namemap_entry *next_entry = NULL; struct __fx_namemap_entry *next_entry = NULL;
if (bucket) { if (bucket) {
struct fx_queue_entry *q_entry = fx_queue_next( struct fx_queue_entry *q_entry
&entry->e_opaque.e_entry); = fx_queue_next(&entry->e_opaque.e_entry);
if (!q_entry) { if (!q_entry) {
struct fx_bst_node *node = fx_bst_next( struct fx_bst_node *node
&bucket->b_entry.e_node); = fx_bst_next(&bucket->b_entry.e_node);
next_entry = fx_unbox( next_entry = fx_unbox(
struct __fx_namemap_entry, struct __fx_namemap_entry,
node, node,
@@ -205,12 +243,10 @@ struct fx_namemap_entry *fx_namemap_next(
if (next_entry->e_flags == MAP_ENTRY_BUCKET) { if (next_entry->e_flags == MAP_ENTRY_BUCKET) {
bucket = (struct map_bucket *)next_entry; bucket = (struct map_bucket *)next_entry;
struct fx_queue_entry *q_entry = fx_queue_first( struct fx_queue_entry *q_entry
&bucket->b_items); = fx_queue_first(&bucket->b_items);
next_entry = fx_unbox( next_entry
struct __fx_namemap_entry, = fx_unbox(struct __fx_namemap_entry, q_entry, e_entry);
q_entry,
e_entry);
} }
return (struct fx_namemap_entry *)next_entry; return (struct fx_namemap_entry *)next_entry;
@@ -223,11 +259,11 @@ struct fx_namemap_entry *fx_namemap_prev(
struct map_bucket *bucket = entry->e_opaque.e_bucket; struct map_bucket *bucket = entry->e_opaque.e_bucket;
struct __fx_namemap_entry *prev_entry = NULL; struct __fx_namemap_entry *prev_entry = NULL;
if (bucket) { if (bucket) {
struct fx_queue_entry *q_entry = fx_queue_prev( struct fx_queue_entry *q_entry
&entry->e_opaque.e_entry); = fx_queue_prev(&entry->e_opaque.e_entry);
if (!q_entry) { if (!q_entry) {
struct fx_bst_node *node = fx_bst_prev( struct fx_bst_node *node
&bucket->b_entry.e_node); = fx_bst_prev(&bucket->b_entry.e_node);
prev_entry = fx_unbox( prev_entry = fx_unbox(
struct __fx_namemap_entry, struct __fx_namemap_entry,
node, node,
@@ -249,12 +285,10 @@ struct fx_namemap_entry *fx_namemap_prev(
if (prev_entry->e_flags == MAP_ENTRY_BUCKET) { if (prev_entry->e_flags == MAP_ENTRY_BUCKET) {
bucket = (struct map_bucket *)prev_entry; bucket = (struct map_bucket *)prev_entry;
struct fx_queue_entry *q_entry = fx_queue_last( struct fx_queue_entry *q_entry
&bucket->b_items); = fx_queue_last(&bucket->b_items);
prev_entry = fx_unbox( prev_entry
struct __fx_namemap_entry, = fx_unbox(struct __fx_namemap_entry, q_entry, e_entry);
q_entry,
e_entry);
} }
return (struct fx_namemap_entry *)prev_entry; return (struct fx_namemap_entry *)prev_entry;
+49 -29
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(
@@ -41,16 +41,14 @@ fx_result fx_object_instantiate(
out->obj_type = type; out->obj_type = type;
out->obj_ref = 1; out->obj_ref = 1;
struct fx_queue_entry *entry = fx_queue_first( struct fx_queue_entry *entry
&type->ty_class_hierarchy); = fx_queue_first(&type->ty_class_hierarchy);
while (entry) { while (entry) {
struct fx_type_component *comp = fx_unbox( struct fx_type_component *comp
struct fx_type_component, = fx_unbox(struct fx_type_component, entry, c_entry);
entry,
c_entry);
const struct fx_type_info *class_info = comp->c_type; const struct fx_type_info *class_info = comp->c_type;
void *private_data = (char *)out void *private_data
+ comp->c_instance_private_data_offset; = (char *)out + comp->c_instance_private_data_offset;
if (class_info->ty_instance_init) { if (class_info->ty_instance_init) {
class_info->ty_instance_init(out, private_data); class_info->ty_instance_init(out, private_data);
@@ -90,7 +88,7 @@ fx_status fx_object_to_string(
fx_stream *out, fx_stream *out,
const char *format) const char *format)
{ {
fx_value value = FX_VALUE_OBJECT(p); fx_value value = FX_VALUE_OBJECT_REF(p);
fx_object_class *iface = fx_object_get_interface(p, FX_TYPE_OBJECT); fx_object_class *iface = fx_object_get_interface(p, FX_TYPE_OBJECT);
if (iface && iface->to_string) { if (iface && iface->to_string) {
return iface->to_string(&value, out, format); return iface->to_string(&value, out, format);
@@ -100,15 +98,35 @@ 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) {
return false;
}
if (fx_type_id_compare(&p->obj_type->ty_id, type) == 0) { if (fx_type_id_compare(&p->obj_type->ty_id, type) == 0) {
return true; return true;
} }
struct fx_type_component *comp = fx_type_get_component( struct fx_type_component *comp
&p->obj_type->ty_components, = fx_type_get_component(&p->obj_type->ty_components, type);
type);
return comp != NULL; return comp != NULL;
} }
@@ -125,9 +143,8 @@ void *fx_object_get_private(const struct _fx_object *object, fx_type_id type)
return (char *)object + object->obj_main_priv_offset; return (char *)object + object->obj_main_priv_offset;
} }
struct fx_type_component *comp = fx_type_get_component( struct fx_type_component *comp
&object->obj_type->ty_components, = fx_type_get_component(&object->obj_type->ty_components, type);
type);
if (!comp) { if (!comp) {
return NULL; return NULL;
} }
@@ -143,9 +160,8 @@ void *fx_object_get_protected(const struct _fx_object *object, fx_type_id type)
assert(object->obj_magic == FX_OBJECT_MAGIC); assert(object->obj_magic == FX_OBJECT_MAGIC);
struct fx_type_component *comp = fx_type_get_component( struct fx_type_component *comp
&object->obj_type->ty_components, = fx_type_get_component(&object->obj_type->ty_components, type);
type);
if (!comp) { if (!comp) {
return NULL; return NULL;
} }
@@ -177,9 +193,8 @@ enum fx_status fx_object_get_data(
assert(object->obj_magic == FX_OBJECT_MAGIC); assert(object->obj_magic == FX_OBJECT_MAGIC);
struct fx_type_component *comp = fx_type_get_component( struct fx_type_component *comp
&object->obj_type->ty_components, = fx_type_get_component(&object->obj_type->ty_components, type);
type);
if (!comp) { if (!comp) {
return FX_ERR_INVALID_ARGUMENT; return FX_ERR_INVALID_ARGUMENT;
} }
@@ -202,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;
@@ -218,14 +240,12 @@ void fx_object_unref(struct _fx_object *p)
struct fx_queue_entry *cur = fx_queue_last(&type->ty_class_hierarchy); struct fx_queue_entry *cur = fx_queue_last(&type->ty_class_hierarchy);
while (cur) { while (cur) {
struct fx_type_component *comp = fx_unbox( struct fx_type_component *comp
struct fx_type_component, = fx_unbox(struct fx_type_component, cur, c_entry);
cur,
c_entry);
const struct fx_type_info *class_info = comp->c_type; const struct fx_type_info *class_info = comp->c_type;
void *private_data = (char *)p void *private_data
+ comp->c_instance_private_data_offset; = (char *)p + comp->c_instance_private_data_offset;
if (class_info->ty_instance_fini) { if (class_info->ty_instance_fini) {
class_info->ty_instance_fini(p, private_data); class_info->ty_instance_fini(p, private_data);
+34
View File
@@ -1,17 +1,51 @@
#include <fx/comparable.h>
#include <fx/pointer.h> #include <fx/pointer.h>
#include <fx/value-type.h> #include <fx/value-type.h>
#include <fx/value.h>
static fx_status hash(const fx_value *v, uint64_t *out_hash)
{
*out_hash = (uint64_t)v->v_pointer;
return FX_SUCCESS;
}
static i32 compare(const fx_value *left, const fx_value *right)
{
void *left_v = left->v_pointer;
void *right_v = 0;
if (!FX_OK(fx_value_get_pointer(right, &right_v))) {
return -1;
}
uintptr_t left_p = (uintptr_t)left_v;
uintptr_t right_p = (uintptr_t)right_v;
if (left_p < right_p) {
return -1;
} else if (left_p > right_p) {
return 1;
} else {
return 0;
}
}
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_BEGIN(fx_pointer) FX_TYPE_CLASS_BEGIN(fx_pointer)
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) = NULL; FX_INTERFACE_ENTRY(to_string) = NULL;
FX_INTERFACE_ENTRY(hash) = hash;
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_comparable, FX_TYPE_COMPARABLE)
FX_INTERFACE_ENTRY(c_compare) = compare;
FX_TYPE_VTABLE_INTERFACE_END(fx_comparable, FX_TYPE_COMPARABLE)
FX_TYPE_CLASS_END(fx_pointer) FX_TYPE_CLASS_END(fx_pointer)
FX_TYPE_DEFINITION_BEGIN(fx_pointer) FX_TYPE_DEFINITION_BEGIN(fx_pointer)
__FX_VALUE_TYPE_ID(POINTER); __FX_VALUE_TYPE_ID(POINTER);
FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE); FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE);
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
FX_TYPE_NAME("fx.pointer"); FX_TYPE_NAME("fx.pointer");
FX_TYPE_CLASS(fx_pointer_class); FX_TYPE_CLASS(fx_pointer_class);
FX_TYPE_INSTANCE_PRIVATE(void *); FX_TYPE_INSTANCE_PRIVATE(void *);
+42 -47
View File
@@ -101,7 +101,7 @@
// Default precision for the floating point conversion specifiers (the C // Default precision for the floating point conversion specifiers (the C
// standard sets this at 6) // standard sets this at 6)
#ifndef PRINTF_DEFAULT_FLOAT_PRECISION #ifndef PRINTF_DEFAULT_FLOAT_PRECISION
#define PRINTF_DEFAULT_FLOAT_PRECISION 6 #define PRINTF_DEFAULT_FLOAT_PRECISION 13
#endif #endif
// Default choice of type to use for internal floating-point computations // Default choice of type to use for internal floating-point computations
@@ -623,8 +623,8 @@ static void print_integer(
} else { } else {
do { do {
const char digit = (char)(value % base); const char digit = (char)(value % base);
buf[len++] = (char)(digit < 10 buf[len++]
? '0' + digit = (char)(digit < 10 ? '0' + digit
: (flags & FLAGS_UPPERCASE : (flags & FLAGS_UPPERCASE
? 'A' ? 'A'
: 'a') : 'a')
@@ -698,15 +698,14 @@ static struct floating_point_components get_components(
number_.is_negative = get_sign_bit(number); number_.is_negative = get_sign_bit(number);
floating_point_t abs_number = (number_.is_negative) ? -number : number; floating_point_t abs_number = (number_.is_negative) ? -number : number;
number_.integral = (int_fast64_t)abs_number; number_.integral = (int_fast64_t)abs_number;
floating_point_t scaled_remainder = (abs_number floating_point_t scaled_remainder
- (floating_point_t) = (abs_number - (floating_point_t)number_.integral)
number_.integral) * powers_of_10[precision];
* powers_of_10[precision];
number_.fractional = (int_fast64_t) number_.fractional = (int_fast64_t)
scaled_remainder; // for precision == 0U, this will be 0 scaled_remainder; // for precision == 0U, this will be 0
floating_point_t remainder = scaled_remainder floating_point_t remainder
- (floating_point_t)number_.fractional; = scaled_remainder - (floating_point_t)number_.fractional;
const floating_point_t one_half = (floating_point_t)0.5; const floating_point_t one_half = (floating_point_t)0.5;
if (remainder > one_half) { if (remainder > one_half) {
@@ -779,18 +778,18 @@ static struct scaling_factor update_normalization(
result.raw_factor = sf.raw_factor * extra_multiplicative_factor; result.raw_factor = sf.raw_factor * extra_multiplicative_factor;
} else { } else {
int factor_exp2 = get_exp2(get_bit_access(sf.raw_factor)); int factor_exp2 = get_exp2(get_bit_access(sf.raw_factor));
int extra_factor_exp2 = get_exp2( int extra_factor_exp2
get_bit_access(extra_multiplicative_factor)); = get_exp2(get_bit_access(extra_multiplicative_factor));
// Divide the larger-exponent raw raw_factor by the smaller // Divide the larger-exponent raw raw_factor by the smaller
if (PRINTF_ABS(factor_exp2) > PRINTF_ABS(extra_factor_exp2)) { if (PRINTF_ABS(factor_exp2) > PRINTF_ABS(extra_factor_exp2)) {
result.multiply = false; result.multiply = false;
result.raw_factor = sf.raw_factor result.raw_factor
/ extra_multiplicative_factor; = sf.raw_factor / extra_multiplicative_factor;
} else { } else {
result.multiply = true; result.multiply = true;
result.raw_factor = extra_multiplicative_factor result.raw_factor
/ sf.raw_factor; = extra_multiplicative_factor / sf.raw_factor;
} }
} }
return result; return result;
@@ -818,27 +817,26 @@ static struct floating_point_components get_normalized_components(
return get_components(negative ? -scaled : scaled, precision); return get_components(negative ? -scaled : scaled, precision);
} }
components.integral = (int_fast64_t)scaled; components.integral = (int_fast64_t)scaled;
floating_point_t remainder = non_normalized floating_point_t remainder
- unapply_scaling( = non_normalized
(floating_point_t) - unapply_scaling(
components.integral, (floating_point_t)components.integral,
normalization); normalization);
floating_point_t prec_power_of_10 = powers_of_10[precision]; floating_point_t prec_power_of_10 = powers_of_10[precision];
struct scaling_factor account_for_precision = update_normalization( struct scaling_factor account_for_precision
normalization, = update_normalization(normalization, prec_power_of_10);
prec_power_of_10); floating_point_t scaled_remainder
floating_point_t scaled_remainder = apply_scaling( = apply_scaling(remainder, account_for_precision);
remainder,
account_for_precision);
floating_point_t rounding_threshold = 0.5; floating_point_t rounding_threshold = 0.5;
components.fractional = (int_fast64_t) components.fractional = (int_fast64_t)
scaled_remainder; // when precision == 0, the assigned value scaled_remainder; // when precision == 0, the assigned value
// should be 0 // should be 0
scaled_remainder -= (floating_point_t)components scaled_remainder
.fractional; // when precision == 0, this -= (floating_point_t)
// will not change components.fractional; // when precision == 0, this
// scaled_remainder // will not change
// scaled_remainder
components.fractional += (scaled_remainder >= rounding_threshold); components.fractional += (scaled_remainder >= rounding_threshold);
if (scaled_remainder == rounding_threshold) { if (scaled_remainder == rounding_threshold) {
@@ -960,9 +958,8 @@ static void print_decimal_number(
char *buf, char *buf,
printf_size_t len) printf_size_t len)
{ {
struct floating_point_components value_ = get_components( struct floating_point_components value_
number, = get_components(number, precision);
precision);
print_broken_up_decimal( print_broken_up_decimal(
value_, value_,
output, output,
@@ -1105,10 +1102,10 @@ static void print_exponential_number(
abs_exp10_covered_by_powers_table abs_exp10_covered_by_powers_table
= PRINTF_ABS(floored_exp10) = PRINTF_ABS(floored_exp10)
< PRINTF_MAX_PRECOMPUTED_POWER_OF_10; < PRINTF_MAX_PRECOMPUTED_POWER_OF_10;
normalization.raw_factor = abs_exp10_covered_by_powers_table normalization.raw_factor
? powers_of_10[PRINTF_ABS( = abs_exp10_covered_by_powers_table
floored_exp10)] ? powers_of_10[PRINTF_ABS(floored_exp10)]
: p10; : p10;
} }
// We now begin accounting for the widths of the two parts of our // We now begin accounting for the widths of the two parts of our
@@ -1120,9 +1117,8 @@ static void print_exponential_number(
bool fall_back_to_decimal_only_mode = false; bool fall_back_to_decimal_only_mode = false;
if (flags & FLAGS_ADAPT_EXP) { if (flags & FLAGS_ADAPT_EXP) {
int required_significant_digits = (precision == 0) int required_significant_digits
? 1 = (precision == 0) ? 1 : (int)precision;
: (int)precision;
// Should we want to fall-back to "%f" mode, and only print the // Should we want to fall-back to "%f" mode, and only print the
// decimal part? // decimal part?
fall_back_to_decimal_only_mode fall_back_to_decimal_only_mode
@@ -1133,10 +1129,10 @@ static void print_exponential_number(
// "%g" mode, "precision" is the number of _significant digits_, // "%g" mode, "precision" is the number of _significant digits_,
// and this is when we "translate" the precision value to an // and this is when we "translate" the precision value to an
// actual number of decimal digits. // actual number of decimal digits.
int precision_ = fall_back_to_decimal_only_mode int precision_
? (int)precision - 1 - floored_exp10 = fall_back_to_decimal_only_mode
: (int)precision ? (int)precision - 1 - floored_exp10
- 1; // the presence of the : (int)precision - 1; // the presence of the
// exponent ensures only // exponent ensures only
// one significant digit // one significant digit
// comes before the // comes before the
@@ -1573,9 +1569,8 @@ static inline void format_string_loop(
if (flags & FLAGS_LONG_LONG) { if (flags & FLAGS_LONG_LONG) {
#if PRINTF_SUPPORT_LONG_LONG #if PRINTF_SUPPORT_LONG_LONG
const long long value = va_arg( const long long value
args, = va_arg(args, long long);
long long);
print_integer( print_integer(
output, output,
ABS_FOR_PRINTING(value), ABS_FOR_PRINTING(value),
+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) {
+63 -82
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;
@@ -68,6 +68,7 @@ struct fx_string_iterator_p {
const char *string_value; const char *string_value;
size_t string_length; size_t string_length;
size_t string_codepoints; size_t string_codepoints;
fx_value value;
}; };
/*** PRIVATE FUNCTIONS ********************************************************/ /*** PRIVATE FUNCTIONS ********************************************************/
@@ -293,15 +294,14 @@ static fx_string *string_duplicate(const struct fx_string_p *str)
return NULL; return NULL;
} }
struct fx_string_p *new_str_p = fx_object_get_private( struct fx_string_p *new_str_p
new_str, = fx_object_get_private(new_str, FX_TYPE_STRING);
FX_TYPE_STRING);
string_change_capacity(new_str_p, str->s_len); string_change_capacity(new_str_p, str->s_len);
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;
@@ -397,9 +397,8 @@ static enum fx_status replace_utf8(
} }
size_t new_data_nr_bytes = strlen(new_data); size_t new_data_nr_bytes = strlen(new_data);
size_t new_data_nr_codepoints = fx_wchar_utf8_codepoint_count( size_t new_data_nr_codepoints
new_data, = fx_wchar_utf8_codepoint_count(new_data, new_data_nr_bytes);
new_data_nr_bytes);
if (new_data_nr_codepoints == 0) { if (new_data_nr_codepoints == 0) {
/* new_data is not a valid utf-8 string */ /* new_data is not a valid utf-8 string */
return FX_ERR_INVALID_ARGUMENT; return FX_ERR_INVALID_ARGUMENT;
@@ -417,8 +416,8 @@ static enum fx_status replace_utf8(
return status; return status;
} }
size_t new_total_bytes = str->s_len - old_data_nr_bytes size_t new_total_bytes
+ new_data_nr_bytes; = str->s_len - old_data_nr_bytes + new_data_nr_bytes;
if (new_total_bytes > str->s_max) { if (new_total_bytes > str->s_max) {
status = string_reserve(str, new_total_bytes); status = string_reserve(str, new_total_bytes);
} }
@@ -479,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;
@@ -514,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;
} }
@@ -747,9 +747,8 @@ static enum fx_status string_insert_wstr_ansi(
at = dest->s_len; at = dest->s_len;
} }
size_t utf8_encoded_size = fx_wchar_utf8_string_encoded_size( size_t utf8_encoded_size
src, = fx_wchar_utf8_string_encoded_size(src, nr_codepoints);
nr_codepoints);
if (utf8_encoded_size == 0) { if (utf8_encoded_size == 0) {
return FX_ERR_INVALID_ARGUMENT; return FX_ERR_INVALID_ARGUMENT;
} }
@@ -796,9 +795,8 @@ static enum fx_status string_insert_wstr_utf8(
codepoint_offset = dest->s_codepoints; codepoint_offset = dest->s_codepoints;
} }
size_t utf8_encoded_size = fx_wchar_utf8_string_encoded_size( size_t utf8_encoded_size
src, = fx_wchar_utf8_string_encoded_size(src, nr_codepoints);
nr_codepoints);
if (utf8_encoded_size == 0) { if (utf8_encoded_size == 0) {
return FX_ERR_INVALID_ARGUMENT; return FX_ERR_INVALID_ARGUMENT;
} }
@@ -1026,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)
@@ -1041,9 +1039,8 @@ static fx_iterator *string_tokenise(
} }
fx_string_iterator *it_obj = fx_object_create(FX_TYPE_STRING_ITERATOR); fx_string_iterator *it_obj = fx_object_create(FX_TYPE_STRING_ITERATOR);
struct fx_string_iterator_p *it = fx_object_get_private( struct fx_string_iterator_p *it
it_obj, = fx_object_get_private(it_obj, FX_TYPE_STRING_ITERATOR);
FX_TYPE_STRING_ITERATOR);
it->_m = ITERATOR_MODE_TOKENS; it->_m = ITERATOR_MODE_TOKENS;
it->_d = delims; it->_d = delims;
@@ -1152,9 +1149,8 @@ static fx_string *string_substr(
} }
fx_string *newstr = fx_string_create(); fx_string *newstr = fx_string_create();
struct fx_string_p *newstr_p = fx_object_get_private( struct fx_string_p *newstr_p
newstr, = fx_object_get_private(newstr, FX_TYPE_STRING);
FX_TYPE_STRING);
string_reserve(newstr_p, len); string_reserve(newstr_p, len);
const char *src = string_ptr(str) + start; const char *src = string_ptr(str) + start;
@@ -1304,12 +1300,10 @@ enum fx_status fx_string_insert_s(
const fx_string *src, const fx_string *src,
size_t at) size_t at)
{ {
struct fx_string_p *dest_p = fx_object_get_private( struct fx_string_p *dest_p
dest, = fx_object_get_private(dest, FX_TYPE_STRING);
FX_TYPE_STRING); const struct fx_string_p *src_p
const struct fx_string_p *src_p = fx_object_get_private( = fx_object_get_private(src, FX_TYPE_STRING);
src,
FX_TYPE_STRING);
return string_insert_s(dest_p, src_p, at); return string_insert_s(dest_p, src_p, at);
} }
@@ -1318,9 +1312,8 @@ enum fx_status fx_string_insert_cstr(
const char *src, const char *src,
size_t at) size_t at)
{ {
struct fx_string_p *dest_p = fx_object_get_private( struct fx_string_p *dest_p
dest, = fx_object_get_private(dest, FX_TYPE_STRING);
FX_TYPE_STRING);
return string_insert_cstr(dest_p, src, strlen(src), at); return string_insert_cstr(dest_p, src, strlen(src), at);
} }
@@ -1329,9 +1322,8 @@ enum fx_status fx_string_insert_wstr(
const fx_wchar *src, const fx_wchar *src,
size_t at) size_t at)
{ {
struct fx_string_p *dest_p = fx_object_get_private( struct fx_string_p *dest_p
dest, = fx_object_get_private(dest, FX_TYPE_STRING);
FX_TYPE_STRING);
return string_insert_wstr(dest_p, src, fx_wstrlen(src), at); return string_insert_wstr(dest_p, src, fx_wstrlen(src), at);
} }
@@ -1341,9 +1333,8 @@ enum fx_status fx_string_insert_cstrf(
const char *format, const char *format,
...) ...)
{ {
struct fx_string_p *dest_p = fx_object_get_private( struct fx_string_p *dest_p
dest, = fx_object_get_private(dest, FX_TYPE_STRING);
FX_TYPE_STRING);
va_list arg; va_list arg;
va_start(arg, format); va_start(arg, format);
@@ -1370,9 +1361,8 @@ enum fx_status fx_string_insert_cstrn(
enum fx_status fx_string_append_cstrf(fx_string *dest, const char *format, ...) enum fx_status fx_string_append_cstrf(fx_string *dest, const char *format, ...)
{ {
struct fx_string_p *dest_p = fx_object_get_private( struct fx_string_p *dest_p
dest, = fx_object_get_private(dest, FX_TYPE_STRING);
FX_TYPE_STRING);
va_list arg; va_list arg;
va_start(arg, format); va_start(arg, format);
@@ -1384,9 +1374,8 @@ enum fx_status fx_string_append_cstrf(fx_string *dest, const char *format, ...)
enum fx_status fx_string_prepend_cstrf(fx_string *dest, const char *format, ...) enum fx_status fx_string_prepend_cstrf(fx_string *dest, const char *format, ...)
{ {
struct fx_string_p *dest_p = fx_object_get_private( struct fx_string_p *dest_p
dest, = fx_object_get_private(dest, FX_TYPE_STRING);
FX_TYPE_STRING);
va_list arg; va_list arg;
va_start(arg, format); va_start(arg, format);
@@ -1402,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)
@@ -1539,9 +1528,8 @@ static fx_status string_to_string(
fx_stream *out, fx_stream *out,
const char *format) const char *format)
{ {
struct fx_string_p *str = fx_object_get_private( struct fx_string_p *str
obj->v_object, = fx_object_get_private(obj->v_object, FX_TYPE_STRING);
FX_TYPE_STRING);
const char *s = string_ptr(str); const char *s = string_ptr(str);
for (size_t i = 0; i < str->s_len; i++) { for (size_t i = 0; i < str->s_len; i++) {
fx_stream_write_char(out, s[i]); fx_stream_write_char(out, s[i]);
@@ -1552,9 +1540,8 @@ static fx_status string_to_string(
static fx_status string_hash(const fx_value *v, uint64_t *out_hash) static fx_status string_hash(const fx_value *v, uint64_t *out_hash)
{ {
struct fx_string_p *str = fx_object_get_private( struct fx_string_p *str
v->v_object, = fx_object_get_private(v->v_object, FX_TYPE_STRING);
FX_TYPE_STRING);
const char *s = string_ptr(str); const char *s = string_ptr(str);
*out_hash = fx_hash_cstr(s); *out_hash = fx_hash_cstr(s);
return FX_SUCCESS; return FX_SUCCESS;
@@ -1570,7 +1557,9 @@ static i32 compare(const fx_value *left, const fx_value *right)
return -1; return -1;
} }
return strcmp(left_cstr, right_cstr); int result = strcmp(left_cstr, right_cstr);
return result;
} }
static fx_status get_length( static fx_status get_length(
@@ -1586,11 +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 = fx_object_get_private( struct fx_string_iterator_p *it = priv;
obj,
FX_TYPE_STRING_ITERATOR);
if (it->_tmp) { if (it->_tmp) {
fx_string_unref(it->_tmp); fx_string_unref(it->_tmp);
} }
@@ -1598,12 +1585,11 @@ static void iterator_fini(fx_iterator *obj)
memset(it, 0x0, sizeof *it); memset(it, 0x0, sizeof *it);
} }
static fx_iterator *iterator_begin(fx_object *obj) static const fx_iterator *iterator_begin(const fx_object *obj)
{ {
fx_string_iterator *it_obj = fx_object_create(FX_TYPE_STRING_ITERATOR); fx_string_iterator *it_obj = fx_object_create(FX_TYPE_STRING_ITERATOR);
struct fx_string_iterator_p *it = fx_object_get_private( struct fx_string_iterator_p *it
it_obj, = fx_object_get_private(it_obj, FX_TYPE_STRING_ITERATOR);
FX_TYPE_STRING_ITERATOR);
struct fx_string_p *p = fx_object_get_private(obj, FX_TYPE_STRING); struct fx_string_p *p = fx_object_get_private(obj, FX_TYPE_STRING);
if (!p->s_len) { if (!p->s_len) {
@@ -1623,11 +1609,6 @@ static fx_iterator *iterator_begin(fx_object *obj)
return it_obj; return it_obj;
} }
static const fx_iterator *iterator_cbegin(const fx_object *obj)
{
return iterator_begin((fx_object *)obj);
}
static enum fx_status chars_iterator_move_next(struct fx_string_iterator_p *it) static enum fx_status chars_iterator_move_next(struct fx_string_iterator_p *it)
{ {
if (!it->_s_p) { if (!it->_s_p) {
@@ -1676,9 +1657,8 @@ static enum fx_status tokens_iterator_move_next(struct fx_string_iterator_p *it)
static enum fx_status iterator_move_next(const fx_iterator *obj) static enum fx_status iterator_move_next(const fx_iterator *obj)
{ {
struct fx_string_iterator_p *it = fx_object_get_private( struct fx_string_iterator_p *it
obj, = fx_object_get_private(obj, FX_TYPE_STRING_ITERATOR);
FX_TYPE_STRING_ITERATOR);
switch (it->_m) { switch (it->_m) {
case ITERATOR_MODE_CHARS: case ITERATOR_MODE_CHARS:
@@ -1690,21 +1670,22 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
} }
} }
static fx_value chars_iterator_get_value(struct fx_string_iterator_p *it) static fx_value *chars_iterator_get_value(struct fx_string_iterator_p *it)
{ {
return FX_U32(it->char_value); it->value = FX_WCHAR(it->char_value);
return &it->value;
} }
static fx_value tokens_iterator_get_value(struct fx_string_iterator_p *it) static fx_value *tokens_iterator_get_value(struct fx_string_iterator_p *it)
{ {
return FX_CSTR(it->string_value); it->value = FX_CSTR(it->string_value);
return &it->value;
} }
static fx_value iterator_get_value(const fx_iterator *obj) static const fx_value *iterator_get_value(const fx_iterator *obj)
{ {
struct fx_string_iterator_p *it = fx_object_get_private( struct fx_string_iterator_p *it
obj, = fx_object_get_private(obj, FX_TYPE_STRING_ITERATOR);
FX_TYPE_STRING_ITERATOR);
switch (it->_m) { switch (it->_m) {
case ITERATOR_MODE_CHARS: case ITERATOR_MODE_CHARS:
@@ -1712,7 +1693,7 @@ static fx_value iterator_get_value(const fx_iterator *obj)
case ITERATOR_MODE_TOKENS: case ITERATOR_MODE_TOKENS:
return tokens_iterator_get_value(it); return tokens_iterator_get_value(it);
default: default:
return FX_VALUE_EMPTY; return NULL;
} }
} }
@@ -1720,8 +1701,8 @@ static fx_status add(const fx_value *l, const fx_value *r, fx_value *out)
{ {
fx_string *left = l->v_object; fx_string *left = l->v_object;
fx_string *right = r->v_object; fx_string *right = r->v_object;
fx_string *result = fx_string_create_from_cstr( fx_string *result
fx_string_get_cstr(left)); = fx_string_create_from_cstr(fx_string_get_cstr(left));
if (!result) { if (!result) {
return FX_ERR_NO_MEMORY; return FX_ERR_NO_MEMORY;
} }
@@ -1775,7 +1756,6 @@ FX_TYPE_CLASS_BEGIN(fx_string)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
FX_INTERFACE_ENTRY(it_begin) = iterator_begin; FX_INTERFACE_ENTRY(it_begin) = iterator_begin;
FX_INTERFACE_ENTRY(it_cbegin) = iterator_cbegin;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE)
@@ -1860,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)

Some files were not shown because too many files have changed in this diff Show More