Compare commits
88 Commits
5ead9118b3
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 9da33eec71 | |||
| 181a6f7c24 | |||
| 0fa5c7373f | |||
| cb97001033 | |||
| 181a6bac09 | |||
| 24bcb18543 | |||
| e9a1bfbde2 | |||
| 5c837abc6c | |||
| ad27b0cf73 | |||
| dbd2135998 | |||
| 50706084eb | |||
| 38699a0eb3 | |||
| c29c687076 | |||
| c41131eac8 | |||
| eb759982db | |||
| 0ee94af705 | |||
| 5b16d98e51 | |||
| 5156971945 | |||
| c887a18a10 | |||
| c0f139dde6 | |||
| ee11ff240f | |||
| 32d4d0abf2 | |||
| f093c0fb6e | |||
| 9145e3bac7 | |||
| fcf44cc288 | |||
| 4d6f2afa5a | |||
| 7cbd1eb162 | |||
| f6320d64cd | |||
| 87ff913186 | |||
| 52c53fa57f | |||
| 8237fae60f | |||
| 10cfb05287 | |||
| a4e797f619 | |||
| 2512611f8f | |||
| 15784c3404 | |||
| 20dc844c9d | |||
| 93ecd02b3f | |||
| 612ba7101d | |||
| fb864a64c9 | |||
| 8c0a31e19b | |||
| a20306ae64 | |||
| 99380c7810 | |||
| 2d396ee071 | |||
| 34a8ccff95 | |||
| d9835cea0b | |||
| 7b546f6bbe | |||
| 65f490b900 | |||
| 1516eee8d1 | |||
| 53a856794f | |||
| f879d4fdfc | |||
| e53027595e | |||
| bbb26c5ee5 | |||
| e78241c051 | |||
| 5877abd33e | |||
| b31e82aaf8 | |||
| 4c06ded99f | |||
| ff0d40b324 | |||
| 883b0b24b1 | |||
| 516f5be2d6 | |||
| f79650698a | |||
| 01cbcf712a | |||
| 5e3439bc4a | |||
| 56e1fae273 | |||
| c5eac75a0b | |||
| aea3f99859 | |||
| cca52c788a | |||
| 74fbd94f95 | |||
| 97fe00d4a1 | |||
| 0be82c63fa | |||
| 2df76a76c4 | |||
| 9d4aab49ac | |||
| d84d56681f | |||
| 6efe473c11 | |||
| 5d0aa577d6 | |||
| f9b456a99f | |||
| 673bf07cf6 | |||
| 82bb20c6af | |||
| 12b04eea8d | |||
| 0f1dfc0b07 | |||
| 13332145c2 | |||
| 7cdad180da | |||
| 554a30fa1a | |||
| 5058bb164a | |||
| 79a8041aac | |||
| bc902047d3 | |||
| 61182e0153 | |||
| f1258489d1 | |||
| 65a7a025c5 |
+1
-1
@@ -45,7 +45,7 @@ SpacesBeforeTrailingComments: 3
|
||||
TabWidth: 8
|
||||
UseTab: AlignWithSpaces
|
||||
BreakAfterReturnType: Automatic
|
||||
PenaltyBreakAssignment: 1000000
|
||||
PenaltyBreakAssignment: 0
|
||||
PenaltyReturnTypeOnItsOwnLine: 100000000
|
||||
PenaltyExcessCharacter: 10000000
|
||||
PenaltyBreakOpenParenthesis: 0
|
||||
|
||||
+22
-2
@@ -1,11 +1,23 @@
|
||||
cmake_minimum_required(VERSION 3.25)
|
||||
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(Templates)
|
||||
include(Platform)
|
||||
|
||||
test_big_endian(is_big_endian)
|
||||
if (is_big_endian)
|
||||
message(STATUS "Target is Big Endian")
|
||||
add_compile_definitions(FX_ENDIAN=1)
|
||||
else ()
|
||||
message(STATUS "Target is Little Endian")
|
||||
add_compile_definitions(FX_ENDIAN=0)
|
||||
endif ()
|
||||
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
set(CMAKE_C_EXTENSIONS OFF)
|
||||
|
||||
@@ -49,4 +61,12 @@ foreach (assembly ${fx_assemblies})
|
||||
add_subdirectory(assemblies/${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")
|
||||
|
||||
@@ -7,8 +7,7 @@ FX_ASSEMBLY_BEGIN()
|
||||
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "array", fx_array);
|
||||
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "bitbuffer", fx_bitbuffer);
|
||||
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "bitmap", fx_bitmap);
|
||||
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "dict", fx_dict);
|
||||
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "hashtable", fx_hashtable);
|
||||
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "hashmap", fx_hashmap);
|
||||
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "list", fx_list);
|
||||
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "tree", fx_tree);
|
||||
FX_ASSEMBLY_END()
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
add_fx_assembly(
|
||||
NAME fx.diagnostics
|
||||
NAMESPACES fx.diagnostics
|
||||
DEPENDENCIES fx.runtime fx.collections fx.io)
|
||||
@@ -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)
|
||||
@@ -1,3 +1,4 @@
|
||||
add_fx_assembly(
|
||||
NAME fx.runtime
|
||||
NAMESPACES fx fx.reflection)
|
||||
NAMESPACES fx fx.reflection
|
||||
DEPENDENCIES Threads::Threads)
|
||||
|
||||
+44
-109
@@ -49,43 +49,37 @@ if (FX_STATIC)
|
||||
set(_lib_suffix "-s")
|
||||
endif ()
|
||||
|
||||
set(supported_components Core Ds Term Cmd Io Serial Compress)
|
||||
set(components ${FX_FIND_COMPONENTS})
|
||||
string(REPLACE ";" ", " supported_components_string_list "${supported_components}")
|
||||
|
||||
if (NOT components)
|
||||
set(components ${supported_components})
|
||||
endif ()
|
||||
|
||||
set(assemblies ${FX_FIND_COMPONENTS})
|
||||
set(required_vars)
|
||||
|
||||
foreach (component ${components})
|
||||
if (NOT "${component}" IN_LIST supported_components)
|
||||
message(FATAL_ERROR "'${component}' is not a valid FX module.\nSupported modules: ${supported_components_string_list}")
|
||||
endif ()
|
||||
if (NOT FX_INCLUDE_DIR)
|
||||
find_path(FX_INCLUDE_DIR
|
||||
NAMES fx/misc.h ${FX_FIND_ARGS}
|
||||
PATH_SUFFIXES include
|
||||
PATHS ${FX_SEARCH_PATHS})
|
||||
endif ()
|
||||
|
||||
string(TOLOWER ${component} header_name)
|
||||
set(lib_name ${header_name}${_lib_suffix})
|
||||
set(required_vars FX_INCLUDE_DIR)
|
||||
|
||||
if (NOT FX_${component}_INCLUDE_DIR)
|
||||
find_path(FX_${component}_INCLUDE_DIR
|
||||
NAMES fx/${header_name}.h ${FX_FIND_ARGS}
|
||||
PATH_SUFFIXES include
|
||||
PATHS ${FX_SEARCH_PATHS})
|
||||
endif ()
|
||||
foreach (assembly ${assemblies})
|
||||
string(TOLOWER ${assembly} header_name)
|
||||
string(REPLACE "." "_" macro_name ${assembly})
|
||||
string(TOUPPER ${macro_name} macro_name)
|
||||
|
||||
if (NOT FX_${component}_LIBRARY)
|
||||
find_library(FX_${component}_LIBRARY
|
||||
NAMES fx-${lib_name} ${FX_FIND_ARGS}
|
||||
set(lib_name ${assembly}${_lib_suffix})
|
||||
|
||||
if (NOT ${macro_name}_LIBRARY)
|
||||
find_library(${macro_name}_LIBRARY
|
||||
NAMES ${lib_name} ${FX_FIND_ARGS}
|
||||
PATH_SUFFIXES lib
|
||||
PATHS ${FX_SEARCH_PATHS})
|
||||
else ()
|
||||
# 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()
|
||||
|
||||
list(APPEND required_vars FX_${component}_INCLUDE_DIR FX_${component}_LIBRARY)
|
||||
endforeach (component)
|
||||
list(APPEND required_vars ${macro_name}_LIBRARY)
|
||||
endforeach (assembly)
|
||||
|
||||
unset(FX_FIND_ARGS)
|
||||
|
||||
@@ -96,94 +90,35 @@ find_package_handle_standard_args(FX
|
||||
|
||||
if (FX_FOUND)
|
||||
set(created_targets)
|
||||
foreach (component ${components})
|
||||
string(TOLOWER ${component} header_name)
|
||||
set(lib_name ${header_name}${_lib_suffix})
|
||||
foreach (assembly ${assemblies})
|
||||
set(target_name ${assembly})
|
||||
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})
|
||||
add_library(FX::${component} UNKNOWN IMPORTED)
|
||||
set_target_properties(FX::${component} PROPERTIES
|
||||
INTERFACE_INCLUDE_DIRECTORIES "${FX_${component}_INCLUDE_DIR}")
|
||||
target_compile_definitions(FX::${component} INTERFACE _CRT_SECURE_NO_WARNINGS=1)
|
||||
string(TOLOWER ${assembly} header_name)
|
||||
string(REPLACE "." "_" macro_name ${assembly})
|
||||
string(REPLACE "." "_" macro_name ${assembly})
|
||||
string(TOUPPER ${macro_name} macro_name)
|
||||
|
||||
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)
|
||||
target_compile_definitions(FX::${component} INTERFACE FX_STATIC=1)
|
||||
target_compile_definitions(FX::${target_name} INTERFACE FX_STATIC=1)
|
||||
endif ()
|
||||
|
||||
set_target_properties(FX::${component} PROPERTIES
|
||||
set_target_properties(FX::${target_name} PROPERTIES
|
||||
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
|
||||
IMPORTED_LOCATION "${FX_${component}_LIBRARY}")
|
||||
set(created_targets ${created_targets} ${component})
|
||||
IMPORTED_LOCATION "${${macro_name}_LIBRARY}")
|
||||
set(created_targets ${created_targets} ${assembly})
|
||||
endif ()
|
||||
endforeach (component)
|
||||
|
||||
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)
|
||||
endforeach (assembly)
|
||||
endif()
|
||||
|
||||
@@ -19,7 +19,7 @@ function(get_platform_details)
|
||||
elseif ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
|
||||
set(system_name "win32")
|
||||
else ()
|
||||
message(FATAL_ERROR "Unsupported platform: ${CMAKE_SYSTEM_NAME}")
|
||||
string(TOLOWER "${CMAKE_SYSTEM_NAME}" system_name)
|
||||
endif ()
|
||||
|
||||
if ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64")
|
||||
@@ -27,7 +27,7 @@ function(get_platform_details)
|
||||
elseif ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "arm64")
|
||||
set(system_arch "aarch64")
|
||||
else ()
|
||||
message(FATAL_ERROR "Unsupported architecture: ${CMAKE_SYSTEM_PROCESSOR}")
|
||||
string(TOLOWER "${CMAKE_SYSTEM_PROCESSOR}" system_arch)
|
||||
endif ()
|
||||
|
||||
set(${arg_SYSTEM} ${system_name} PARENT_SCOPE)
|
||||
|
||||
+16
-20
@@ -18,10 +18,12 @@ function(add_fx_assembly)
|
||||
|
||||
set(assembly_include_paths "")
|
||||
set(assembly_sources ${CMAKE_CURRENT_SOURCE_DIR}/assembly.c)
|
||||
set(assembly_headers)
|
||||
|
||||
foreach (dir ${arg_NAMESPACES})
|
||||
add_subdirectory(${fx_source_root}/${dir} ${fx_build_root}/namespaces/${dir})
|
||||
set(assembly_sources ${assembly_sources} ${namespace_sources})
|
||||
set(assembly_headers ${assembly_headers} ${namespace_headers})
|
||||
set(assembly_include_paths ${assembly_include_paths} ${namespace_include_paths})
|
||||
endforeach (dir)
|
||||
|
||||
@@ -45,31 +47,23 @@ function(add_fx_assembly)
|
||||
target_compile_definitions(${assembly_target_name} PRIVATE ${def})
|
||||
endforeach (def)
|
||||
|
||||
foreach (ns ${arg_NAMESPACES})
|
||||
file(GLOB test_sources ${fx_source_root}/${ns}/test/*.c)
|
||||
foreach (test_file ${test_sources})
|
||||
get_filename_component(test_name ${test_file} NAME_WE)
|
||||
set(test_name ${ns}-${test_name})
|
||||
string(REPLACE "." "-" test_name ${test_name})
|
||||
add_executable(${test_name} ${test_file})
|
||||
target_link_libraries(${test_name} ${assembly_target_name})
|
||||
endforeach (test_file)
|
||||
endforeach (ns)
|
||||
if (fx_enable_tests)
|
||||
foreach (ns ${arg_NAMESPACES})
|
||||
file(GLOB test_sources ${fx_source_root}/${ns}/test/*.c)
|
||||
foreach (test_file ${test_sources})
|
||||
get_filename_component(test_name ${test_file} NAME_WE)
|
||||
set(test_name ${ns}-${test_name})
|
||||
string(REPLACE "." "-" test_name ${test_name})
|
||||
add_executable(${test_name} ${test_file})
|
||||
target_link_libraries(${test_name} ${assembly_target_name})
|
||||
endforeach (test_file)
|
||||
endforeach (ns)
|
||||
endif ()
|
||||
|
||||
set_target_properties(${assembly_target_name} PROPERTIES
|
||||
FOLDER "${assembly_name}")
|
||||
|
||||
TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
|
||||
if(IS_BIG_ENDIAN)
|
||||
target_compile_definitions(${assembly_target_name} PRIVATE
|
||||
BIG_ENDIAN)
|
||||
else()
|
||||
target_compile_definitions(${assembly_target_name} PRIVATE
|
||||
LITTLE_ENDIAN)
|
||||
endif()
|
||||
|
||||
install(TARGETS ${assembly_target_name})
|
||||
install(FILES ${headers} DESTINATION include/${assembly_path})
|
||||
endfunction(add_fx_assembly)
|
||||
|
||||
macro(export_fx_namespace_details ns_name)
|
||||
@@ -96,6 +90,7 @@ macro(export_fx_namespace_details ns_name)
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/sys/${fx_system_name}/${fx_system_arch}/*.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/sys/${fx_system_name}/${fx_system_arch}/*.S)
|
||||
file(GLOB headers include/${namespace_path}/*.h)
|
||||
set(namespace_headers ${headers} PARENT_SCOPE)
|
||||
set(namespace_sources
|
||||
${namespace_sources}
|
||||
${base_namespace_sources}
|
||||
@@ -110,5 +105,6 @@ macro(export_fx_namespace_details ns_name)
|
||||
PARENT_SCOPE)
|
||||
set(internal_libs ${internal_libs} PARENT_SCOPE)
|
||||
set(internal_defines ${internal_defines} PARENT_SCOPE)
|
||||
install(FILES ${headers} DESTINATION include/${namespace_path})
|
||||
endmacro(export_fx_namespace_details)
|
||||
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
#include "command.h"
|
||||
|
||||
#include <fx/cmd.h>
|
||||
#include <fx/cmdline/cmd.h>
|
||||
#include <fx/string.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "command.h"
|
||||
|
||||
#include <fx/cmd.h>
|
||||
#include <fx/cmdline/cmd.h>
|
||||
#include <fx/misc.h>
|
||||
#include <fx/string.h>
|
||||
#include <fx/term/print.h>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "command.h"
|
||||
|
||||
#include <fx/bst.h>
|
||||
#include <fx/cmd.h>
|
||||
#include <fx/cmdline/cmd.h>
|
||||
#include <fx/string.h>
|
||||
#include <fx/term/print.h>
|
||||
#include <fx/term/tty.h>
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#define _FX_COMMAND_H_
|
||||
|
||||
#include <fx/bst.h>
|
||||
#include <fx/cmd.h>
|
||||
#include <fx/cmdline/cmd.h>
|
||||
#include <fx/queue.h>
|
||||
#include <fx/string.h>
|
||||
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
#include "command.h"
|
||||
|
||||
#include <fx/cmd.h>
|
||||
#include <fx/cmdline/cmd.h>
|
||||
#include <fx/string.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
+1
-1
@@ -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_stringstream_ptr(opt_name));
|
||||
fx_stringstream_get_cstr(opt_name));
|
||||
fx_i("usage: %s", fx_string_get_cstr(opt_string));
|
||||
fx_i("for more information, use `" F_YELLOW "--help" F_RESET "`");
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include <fx/cmd.h>
|
||||
#include <fx/cmdline/cmd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
enum {
|
||||
@@ -27,16 +27,24 @@ const char *text
|
||||
"great pleasure.";
|
||||
|
||||
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");
|
||||
|
||||
fx_arglist_iterator it;
|
||||
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)) {
|
||||
printf("opt:%u,arg:%u,i:%zu,value: %s\n", it.opt_id,
|
||||
it.value->val_id, it.i, it.value->val_str);
|
||||
printf("opt:%u,arg:%u,i:%zu,value: %s\n",
|
||||
it.opt_id,
|
||||
it.value->val_id,
|
||||
it.i,
|
||||
it.value->val_str);
|
||||
|
||||
fx_arglist_iterator_next(&it);
|
||||
}
|
||||
|
||||
+90
-112
@@ -2,6 +2,7 @@
|
||||
#include <fx/iterator.h>
|
||||
#include <fx/stream.h>
|
||||
#include <fx/string.h>
|
||||
#include <fx/value.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -12,17 +13,17 @@ struct fx_array_p {
|
||||
size_t ar_len;
|
||||
/* maximum number of items that can currently be stored in array */
|
||||
size_t ar_cap;
|
||||
fx_object **ar_data;
|
||||
fx_value *ar_data;
|
||||
};
|
||||
|
||||
struct fx_array_iterator_p {
|
||||
fx_array *_a;
|
||||
const fx_array *_a;
|
||||
struct fx_array_p *_a_p;
|
||||
|
||||
/** The index of the current value */
|
||||
size_t i;
|
||||
/** The current value */
|
||||
fx_object *value;
|
||||
fx_value *value;
|
||||
};
|
||||
|
||||
/*** PRIVATE FUNCTIONS ********************************************************/
|
||||
@@ -32,7 +33,7 @@ static fx_status resize_array(struct fx_array_p *array, size_t new_capacity)
|
||||
if (array->ar_cap < new_capacity) {
|
||||
void *new_data = realloc(
|
||||
array->ar_data,
|
||||
new_capacity * sizeof(struct fx_dsref *));
|
||||
new_capacity * sizeof(fx_value));
|
||||
if (!new_data) {
|
||||
return FX_ERR_NO_MEMORY;
|
||||
}
|
||||
@@ -40,12 +41,12 @@ static fx_status resize_array(struct fx_array_p *array, size_t new_capacity)
|
||||
array->ar_data = new_data;
|
||||
} else {
|
||||
for (size_t i = new_capacity; i < array->ar_len; i++) {
|
||||
fx_object_unref(array->ar_data[i]);
|
||||
fx_value_unset(&array->ar_data[i]);
|
||||
}
|
||||
|
||||
void *new_data = realloc(
|
||||
array->ar_data,
|
||||
new_capacity * sizeof(struct fx_dsref *));
|
||||
new_capacity * sizeof(fx_value));
|
||||
if (!new_data) {
|
||||
return FX_ERR_NO_MEMORY;
|
||||
}
|
||||
@@ -63,7 +64,7 @@ static fx_status resize_array(struct fx_array_p *array, size_t new_capacity)
|
||||
|
||||
static fx_status array_insert(
|
||||
struct fx_array_p *array,
|
||||
fx_object *value,
|
||||
fx_value value,
|
||||
size_t at)
|
||||
{
|
||||
if (at == FX_NPOS) {
|
||||
@@ -84,13 +85,13 @@ static fx_status array_insert(
|
||||
}
|
||||
}
|
||||
|
||||
fx_object **src = array->ar_data + at;
|
||||
fx_object **dest = src + 1;
|
||||
size_t move_len = (array->ar_len - at) * sizeof(struct fx_dsref *);
|
||||
fx_value *src = array->ar_data + at;
|
||||
fx_value *dest = src + 1;
|
||||
size_t move_len = (array->ar_len - at) * sizeof(fx_value);
|
||||
|
||||
memmove(dest, src, move_len);
|
||||
|
||||
array->ar_data[at] = fx_object_ref(value);
|
||||
fx_value_copy(&array->ar_data[at], &value);
|
||||
array->ar_len++;
|
||||
|
||||
return FX_SUCCESS;
|
||||
@@ -102,11 +103,11 @@ static fx_status array_remove(struct fx_array_p *array, size_t at)
|
||||
return FX_ERR_OUT_OF_BOUNDS;
|
||||
}
|
||||
|
||||
fx_object **src = array->ar_data + at;
|
||||
fx_object **dest = src + 1;
|
||||
size_t move_len = array->ar_len * sizeof(struct fx_dsref *);
|
||||
fx_value *src = array->ar_data + at;
|
||||
fx_value *dest = src + 1;
|
||||
size_t move_len = array->ar_len * sizeof(fx_value);
|
||||
|
||||
fx_object_unref(array->ar_data[at]);
|
||||
fx_value_unset(&array->ar_data[at]);
|
||||
|
||||
memmove(dest, src, move_len);
|
||||
|
||||
@@ -125,17 +126,17 @@ static fx_status array_remove_back(struct fx_array_p *array)
|
||||
return array_remove(array, array->ar_len - 1);
|
||||
}
|
||||
|
||||
static fx_object *array_pop(struct fx_array_p *array, size_t at)
|
||||
static fx_value array_pop(struct fx_array_p *array, size_t at)
|
||||
{
|
||||
if (at >= array->ar_len) {
|
||||
return NULL;
|
||||
return FX_VALUE_EMPTY;
|
||||
}
|
||||
|
||||
fx_object **src = array->ar_data + at;
|
||||
fx_object **dest = src + 1;
|
||||
size_t move_len = array->ar_len * sizeof(struct fx_dsref *);
|
||||
fx_value *dest = array->ar_data + at;
|
||||
fx_value *src = dest + 1;
|
||||
size_t move_len = array->ar_len * sizeof(fx_value);
|
||||
|
||||
fx_object *out = array->ar_data[at];
|
||||
fx_value out = array->ar_data[at];
|
||||
|
||||
memmove(dest, src, move_len);
|
||||
|
||||
@@ -144,40 +145,40 @@ static fx_object *array_pop(struct fx_array_p *array, size_t at)
|
||||
return out;
|
||||
}
|
||||
|
||||
static fx_object *array_pop_front(struct fx_array_p *array)
|
||||
static fx_value array_pop_front(struct fx_array_p *array)
|
||||
{
|
||||
return array_pop(array, 0);
|
||||
}
|
||||
|
||||
static fx_object *array_pop_back(struct fx_array_p *array)
|
||||
static fx_value array_pop_back(struct fx_array_p *array)
|
||||
{
|
||||
return array_pop(array, array->ar_len - 1);
|
||||
}
|
||||
|
||||
static fx_object *array_at(const struct fx_array_p *array, size_t at)
|
||||
static fx_value *array_get_ref(const struct fx_array_p *array, size_t at)
|
||||
{
|
||||
if (at >= array->ar_len) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return array->ar_data[at];
|
||||
return &array->ar_data[at];
|
||||
}
|
||||
|
||||
static fx_object *array_get(struct fx_array_p *array, size_t at)
|
||||
static fx_value array_get(struct fx_array_p *array, size_t at)
|
||||
{
|
||||
if (at >= array->ar_len) {
|
||||
return NULL;
|
||||
return FX_VALUE_EMPTY;
|
||||
}
|
||||
|
||||
return fx_object_ref(array->ar_data[at]);
|
||||
return fx_value_copy_return(array->ar_data[at]);
|
||||
}
|
||||
|
||||
static size_t array_size(const struct fx_array_p *array)
|
||||
static size_t array_get_size(const struct fx_array_p *array)
|
||||
{
|
||||
return array->ar_len;
|
||||
}
|
||||
|
||||
static size_t array_capacity(const struct fx_array_p *array)
|
||||
static size_t array_get_capacity(const struct fx_array_p *array)
|
||||
{
|
||||
return array->ar_cap;
|
||||
}
|
||||
@@ -189,18 +190,16 @@ static void array_clear(struct fx_array_p *array)
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < array->ar_len; i++) {
|
||||
fx_object_unref(array->ar_data[i]);
|
||||
fx_value_unset(&array->ar_data[i]);
|
||||
}
|
||||
|
||||
memset(array->ar_data, 0x0, array->ar_cap * sizeof(fx_object *));
|
||||
memset(array->ar_data, 0x0, array->ar_cap * sizeof(fx_value));
|
||||
array->ar_len = 0;
|
||||
}
|
||||
|
||||
/*** PUBLIC FUNCTIONS *********************************************************/
|
||||
|
||||
fx_array *fx_array_create_with_values(
|
||||
fx_object *const *values,
|
||||
size_t nr_values)
|
||||
fx_array *fx_array_create_with_values(const fx_value *values, size_t nr_values)
|
||||
{
|
||||
fx_array *array = fx_array_create();
|
||||
if (!array) {
|
||||
@@ -211,14 +210,14 @@ fx_array *fx_array_create_with_values(
|
||||
|
||||
size_t real_nr_values = 0;
|
||||
for (size_t i = 0; i < nr_values; i++) {
|
||||
if (values[i]) {
|
||||
if (fx_value_is_set(&values[i])) {
|
||||
real_nr_values++;
|
||||
}
|
||||
}
|
||||
|
||||
p->ar_len = real_nr_values;
|
||||
p->ar_cap = real_nr_values;
|
||||
p->ar_data = calloc(real_nr_values, sizeof(struct fx_dsref *));
|
||||
p->ar_data = calloc(real_nr_values, sizeof(fx_value));
|
||||
if (!p->ar_data) {
|
||||
fx_array_unref(array);
|
||||
return NULL;
|
||||
@@ -226,13 +225,13 @@ fx_array *fx_array_create_with_values(
|
||||
|
||||
size_t index = 0;
|
||||
for (size_t i = 0; i < nr_values; i++) {
|
||||
p->ar_data[index++] = fx_object_ref(values[i]);
|
||||
fx_value_copy(&p->ar_data[index++], &values[i]);
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
fx_status fx_array_insert(fx_array *array, fx_object *value, size_t at)
|
||||
fx_status fx_array_insert(fx_array *array, fx_value value, size_t at)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_ARRAY, array_insert, array, value, at);
|
||||
}
|
||||
@@ -252,39 +251,39 @@ fx_status fx_array_remove_back(fx_array *array)
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_remove_back, array);
|
||||
}
|
||||
|
||||
fx_object *fx_array_pop(fx_array *array, size_t at)
|
||||
fx_value fx_array_pop(fx_array *array, size_t at)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_ARRAY, array_pop, array, at);
|
||||
}
|
||||
|
||||
fx_object *fx_array_pop_front(fx_array *array)
|
||||
fx_value fx_array_pop_front(fx_array *array)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_pop_front, array);
|
||||
}
|
||||
|
||||
fx_object *fx_array_pop_back(fx_array *array)
|
||||
fx_value fx_array_pop_back(fx_array *array)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_pop_back, array);
|
||||
}
|
||||
|
||||
fx_object *fx_array_at(const fx_array *array, size_t at)
|
||||
fx_value *fx_array_get_ref(const fx_array *array, size_t at)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_ARRAY, array_at, array, at);
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_ARRAY, array_get_ref, array, at);
|
||||
}
|
||||
|
||||
fx_object *fx_array_get(fx_array *array, size_t at)
|
||||
fx_value fx_array_get(const fx_array *array, size_t at)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_ARRAY, array_get, array, at);
|
||||
}
|
||||
|
||||
size_t fx_array_size(const fx_array *array)
|
||||
size_t fx_array_get_size(const fx_array *array)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_size, array);
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_get_size, array);
|
||||
}
|
||||
|
||||
size_t fx_array_capacity(const fx_array *array)
|
||||
size_t fx_array_get_capacity(const fx_array *array)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_capacity, array);
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_get_capacity, array);
|
||||
}
|
||||
|
||||
void fx_array_clear(fx_array *array)
|
||||
@@ -294,12 +293,12 @@ void fx_array_clear(fx_array *array)
|
||||
|
||||
/*** PUBLIC ALIAS FUNCTIONS ***************************************************/
|
||||
|
||||
fx_status fx_array_append(fx_array *array, fx_object *value)
|
||||
fx_status fx_array_push_back(fx_array *array, fx_value value)
|
||||
{
|
||||
return fx_array_insert(array, value, FX_NPOS);
|
||||
}
|
||||
|
||||
fx_status fx_array_prepend(fx_array *array, fx_object *value)
|
||||
fx_status fx_array_push_front(fx_array *array, fx_value value)
|
||||
{
|
||||
return fx_array_insert(array, value, 0);
|
||||
}
|
||||
@@ -317,7 +316,7 @@ static void array_fini(fx_object *obj, void *priv)
|
||||
|
||||
if (array->ar_data) {
|
||||
for (size_t i = 0; i < array->ar_len; i++) {
|
||||
fx_object_unref(array->ar_data[i]);
|
||||
fx_value_unset(&array->ar_data[i]);
|
||||
}
|
||||
|
||||
free(array->ar_data);
|
||||
@@ -325,29 +324,34 @@ static void array_fini(fx_object *obj, void *priv)
|
||||
}
|
||||
}
|
||||
|
||||
static void array_to_string(const fx_object *obj, fx_stream *out)
|
||||
static fx_status array_to_string(
|
||||
const fx_value *obj,
|
||||
fx_stream *out,
|
||||
const char *format)
|
||||
{
|
||||
struct fx_array_p *array = fx_object_get_private(obj, FX_TYPE_ARRAY);
|
||||
struct fx_array_p *array = fx_object_get_private(
|
||||
obj->v_object,
|
||||
FX_TYPE_ARRAY);
|
||||
|
||||
if (!array->ar_len) {
|
||||
fx_stream_write_cstr(out, "[]", NULL);
|
||||
return;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
fx_stream_write_cstr(out, "[\n", NULL);
|
||||
|
||||
fx_stream_push_indent(out, 1);
|
||||
size_t len = array_size(array);
|
||||
size_t len = array_get_size(array);
|
||||
|
||||
for (size_t i = 0; i < array->ar_len; i++) {
|
||||
fx_object *value = array->ar_data[i];
|
||||
bool is_string = fx_object_is_type(value, FX_TYPE_STRING);
|
||||
fx_value *value = &array->ar_data[i];
|
||||
bool is_string = fx_value_is_type(value, FX_TYPE_STRING);
|
||||
|
||||
if (is_string) {
|
||||
fx_stream_write_char(out, '"');
|
||||
}
|
||||
|
||||
fx_object_to_string(value, out);
|
||||
fx_value_to_string(value, out, NULL);
|
||||
|
||||
if (is_string) {
|
||||
fx_stream_write_char(out, '"');
|
||||
@@ -362,40 +366,23 @@ static void array_to_string(const fx_object *obj, fx_stream *out)
|
||||
|
||||
fx_stream_pop_indent(out);
|
||||
fx_stream_write_char(out, ']');
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
/*** 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);
|
||||
struct fx_array_iterator_p *it
|
||||
= fx_object_get_private(it_obj, FX_TYPE_ARRAY_ITERATOR);
|
||||
struct fx_array_iterator_p *it = fx_object_get_private(
|
||||
it_obj,
|
||||
FX_TYPE_ARRAY_ITERATOR);
|
||||
it->_a = obj;
|
||||
it->_a_p = fx_object_get_private(obj, FX_TYPE_ARRAY);
|
||||
it->i = 0;
|
||||
|
||||
if (it->_a_p->ar_len > 0) {
|
||||
it->value = it->_a_p->ar_data[0];
|
||||
} else {
|
||||
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
||||
it->value = NULL;
|
||||
}
|
||||
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
static const fx_iterator *iterable_cbegin(const fx_object *obj)
|
||||
{
|
||||
fx_array_iterator *it_obj = fx_object_create(FX_TYPE_ARRAY_ITERATOR);
|
||||
struct fx_array_iterator_p *it
|
||||
= fx_object_get_private(it_obj, FX_TYPE_ARRAY_ITERATOR);
|
||||
it->_a = (fx_array *)obj;
|
||||
it->_a_p = fx_object_get_private(obj, FX_TYPE_ARRAY);
|
||||
it->i = 0;
|
||||
|
||||
if (it->_a_p->ar_len > 0) {
|
||||
it->value = it->_a_p->ar_data[0];
|
||||
it->value = &it->_a_p->ar_data[0];
|
||||
} else {
|
||||
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
||||
it->value = NULL;
|
||||
@@ -406,12 +393,13 @@ static const fx_iterator *iterable_cbegin(const fx_object *obj)
|
||||
|
||||
static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_array_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_ARRAY_ITERATOR);
|
||||
struct fx_array_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_TYPE_ARRAY_ITERATOR);
|
||||
struct fx_array_p *array = it->_a_p;
|
||||
|
||||
if (it->value == NULL || it->i >= array->ar_len) {
|
||||
return false;
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
it->i++;
|
||||
@@ -419,7 +407,7 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||
if (it->i >= array->ar_len) {
|
||||
it->value = NULL;
|
||||
} else {
|
||||
it->value = array->ar_data[it->i];
|
||||
it->value = &array->ar_data[it->i];
|
||||
}
|
||||
|
||||
return (it->value != NULL) ? FX_SUCCESS : FX_ERR_NO_DATA;
|
||||
@@ -427,22 +415,19 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||
|
||||
static enum fx_status iterator_erase(fx_iterator *obj)
|
||||
{
|
||||
struct fx_array_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_ARRAY_ITERATOR);
|
||||
struct fx_array_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_TYPE_ARRAY_ITERATOR);
|
||||
struct fx_array_p *array = it->_a_p;
|
||||
|
||||
if (it->i >= array->ar_len) {
|
||||
return FX_ERR_OUT_OF_BOUNDS;
|
||||
}
|
||||
|
||||
if (array->ar_data[it->i] != it->value) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
array_remove(array, it->i);
|
||||
|
||||
if (it->i < array->ar_len) {
|
||||
it->value = array->ar_data[it->i];
|
||||
it->value = &array->ar_data[it->i];
|
||||
} else {
|
||||
it->value = NULL;
|
||||
}
|
||||
@@ -450,36 +435,31 @@ static enum fx_status iterator_erase(fx_iterator *obj)
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_iterator_value iterator_get_value(fx_iterator *obj)
|
||||
static const fx_value *iterator_get_value(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_array_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_ARRAY_ITERATOR);
|
||||
struct fx_array_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_TYPE_ARRAY_ITERATOR);
|
||||
struct fx_array_p *array = it->_a_p;
|
||||
|
||||
return FX_ITERATOR_VALUE_PTR(it->value);
|
||||
}
|
||||
if (it->i >= array->ar_len) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_array_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_ARRAY_ITERATOR);
|
||||
|
||||
return FX_ITERATOR_VALUE_CPTR(it->value);
|
||||
return it->value;
|
||||
}
|
||||
|
||||
static enum fx_status iterator_is_valid(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_array_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_ARRAY_ITERATOR);
|
||||
struct fx_array_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_TYPE_ARRAY_ITERATOR);
|
||||
struct fx_array_p *array = it->_a_p;
|
||||
|
||||
if (it->i >= array->ar_len) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (array->ar_data[it->i] != it->value) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (it->value != NULL) ? FX_SUCCESS : FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
@@ -493,7 +473,6 @@ FX_TYPE_CLASS_BEGIN(fx_array)
|
||||
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
|
||||
FX_INTERFACE_ENTRY(it_begin) = iterable_begin;
|
||||
FX_INTERFACE_ENTRY(it_cbegin) = iterable_cbegin;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
|
||||
FX_TYPE_CLASS_END(fx_array)
|
||||
|
||||
@@ -516,7 +495,6 @@ FX_TYPE_CLASS_BEGIN(fx_array_iterator)
|
||||
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
|
||||
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
|
||||
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
|
||||
FX_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
|
||||
FX_TYPE_CLASS_END(fx_array_iterator)
|
||||
|
||||
|
||||
+10
-3
@@ -1,6 +1,7 @@
|
||||
#include <fx/bitop.h>
|
||||
#include <fx/collections/bitmap.h>
|
||||
#include <fx/stream.h>
|
||||
#include <fx/value.h>
|
||||
#include <string.h>
|
||||
|
||||
#define BITS_PER_WORD (8 * sizeof(bitmap_word_t))
|
||||
@@ -306,10 +307,14 @@ static void bitmap_fini(fx_object *obj, void *priv)
|
||||
struct fx_bitmap_p *map = priv;
|
||||
}
|
||||
|
||||
static void bitmap_to_string(const fx_object *obj, fx_stream *out)
|
||||
static fx_status bitmap_to_string(
|
||||
const fx_value *obj,
|
||||
fx_stream *out,
|
||||
const char *format)
|
||||
{
|
||||
const struct fx_bitmap_p *map
|
||||
= fx_object_get_private(obj, FX_TYPE_BITMAP);
|
||||
const struct fx_bitmap_p *map = fx_object_get_private(
|
||||
obj->v_object,
|
||||
FX_TYPE_BITMAP);
|
||||
|
||||
unsigned char *bytes = (unsigned char *)map->map_words;
|
||||
size_t nr_bytes = map->map_nr_words * sizeof(bitmap_word_t);
|
||||
@@ -339,6 +344,8 @@ static void bitmap_to_string(const fx_object *obj, fx_stream *out)
|
||||
|
||||
fx_stream_write_fmt(out, NULL, "%c", (c & mask) ? '1' : '0');
|
||||
}
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
@@ -1,732 +0,0 @@
|
||||
#include <fx/collections/dict.h>
|
||||
#include <fx/hash.h>
|
||||
#include <fx/status.h>
|
||||
#include <fx/stream.h>
|
||||
#include <fx/string.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/*** PRIVATE DATA *************************************************************/
|
||||
|
||||
struct fx_dict_bucket_item {
|
||||
fx_queue_entry bi_entry;
|
||||
fx_string *bi_str;
|
||||
fx_object *bi_value;
|
||||
};
|
||||
|
||||
struct fx_dict_bucket {
|
||||
fx_bst_node bk_node;
|
||||
uint64_t bk_hash;
|
||||
fx_queue bk_items;
|
||||
};
|
||||
|
||||
struct fx_dict_p {
|
||||
fx_bst d_buckets;
|
||||
};
|
||||
|
||||
struct fx_dict_iterator_p {
|
||||
size_t i;
|
||||
fx_dict_item item;
|
||||
|
||||
fx_dict *_d;
|
||||
struct fx_dict_p *_d_p;
|
||||
fx_bst_node *_cbn;
|
||||
fx_queue_entry *_cqe;
|
||||
};
|
||||
|
||||
/*** MISC FUNCTIONS ***********************************************************/
|
||||
|
||||
static FX_BST_DEFINE_SIMPLE_GET(
|
||||
struct fx_dict_bucket,
|
||||
uint64_t,
|
||||
bk_node,
|
||||
bk_hash,
|
||||
get_bucket);
|
||||
static FX_BST_DEFINE_SIMPLE_INSERT(
|
||||
struct fx_dict_bucket,
|
||||
bk_node,
|
||||
bk_hash,
|
||||
put_bucket);
|
||||
|
||||
/*** PRIVATE FUNCTIONS ********************************************************/
|
||||
|
||||
static struct fx_dict_bucket *create_bucket(void)
|
||||
{
|
||||
struct fx_dict_bucket *bucket = malloc(sizeof *bucket);
|
||||
if (!bucket) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(bucket, 0x0, sizeof *bucket);
|
||||
return bucket;
|
||||
}
|
||||
|
||||
static struct fx_dict_bucket_item *create_bucket_item(void)
|
||||
{
|
||||
struct fx_dict_bucket_item *item = malloc(sizeof *item);
|
||||
if (!item) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(item, 0x0, sizeof *item);
|
||||
return item;
|
||||
}
|
||||
|
||||
static fx_status dict_put(
|
||||
struct fx_dict_p *dict,
|
||||
const char *key,
|
||||
fx_object *value)
|
||||
{
|
||||
uint64_t hash = fx_hash_cstr(key);
|
||||
struct fx_dict_bucket *bucket = get_bucket(&dict->d_buckets, hash);
|
||||
if (!bucket) {
|
||||
bucket = create_bucket();
|
||||
if (!bucket) {
|
||||
return FX_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
bucket->bk_hash = hash;
|
||||
put_bucket(&dict->d_buckets, bucket);
|
||||
}
|
||||
|
||||
struct fx_dict_bucket_item *item = create_bucket_item();
|
||||
if (!item) {
|
||||
return FX_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
item->bi_str = fx_string_create_from_cstr(key);
|
||||
item->bi_value = fx_object_ref(value);
|
||||
|
||||
fx_queue_push_back(&bucket->bk_items, &item->bi_entry);
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_status dict_put_sk(
|
||||
struct fx_dict_p *dict,
|
||||
const fx_string *key,
|
||||
fx_object *value)
|
||||
{
|
||||
uint64_t hash = fx_string_hash(key);
|
||||
struct fx_dict_bucket *bucket = get_bucket(&dict->d_buckets, hash);
|
||||
if (!bucket) {
|
||||
bucket = create_bucket();
|
||||
if (!bucket) {
|
||||
return FX_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
bucket->bk_hash = hash;
|
||||
put_bucket(&dict->d_buckets, bucket);
|
||||
}
|
||||
|
||||
struct fx_dict_bucket_item *item = create_bucket_item();
|
||||
if (!item) {
|
||||
return FX_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
item->bi_str = fx_string_duplicate(key);
|
||||
item->bi_value = fx_object_ref(value);
|
||||
|
||||
fx_queue_push_back(&bucket->bk_items, &item->bi_entry);
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_object *dict_at(const struct fx_dict_p *dict, const char *key)
|
||||
{
|
||||
uint64_t hash = fx_hash_cstr(key);
|
||||
struct fx_dict_bucket *bucket = get_bucket(&dict->d_buckets, hash);
|
||||
if (!bucket) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_queue_entry *entry = fx_queue_first(&bucket->bk_items);
|
||||
while (entry) {
|
||||
struct fx_dict_bucket_item *item
|
||||
= fx_unbox(struct fx_dict_bucket_item, entry, bi_entry);
|
||||
|
||||
if (!strcmp(fx_string_get_cstr(item->bi_str), key)) {
|
||||
return item->bi_value;
|
||||
}
|
||||
|
||||
entry = fx_queue_next(entry);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static fx_object *dict_at_sk(const struct fx_dict_p *dict, const fx_string *key)
|
||||
{
|
||||
uint64_t hash = fx_string_hash(key);
|
||||
struct fx_dict_bucket *bucket = get_bucket(&dict->d_buckets, hash);
|
||||
if (!bucket) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_queue_entry *entry = fx_queue_first(&bucket->bk_items);
|
||||
while (entry) {
|
||||
struct fx_dict_bucket_item *item
|
||||
= fx_unbox(struct fx_dict_bucket_item, entry, bi_entry);
|
||||
|
||||
if (fx_string_compare(item->bi_str, key)) {
|
||||
return item->bi_value;
|
||||
}
|
||||
|
||||
entry = fx_queue_next(entry);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static fx_object *dict_get(struct fx_dict_p *dict, const char *key)
|
||||
{
|
||||
fx_object *value = dict_at(dict, key);
|
||||
if (value) {
|
||||
fx_object_ref(value);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
static fx_object *dict_get_sk(struct fx_dict_p *dict, const fx_string *key)
|
||||
{
|
||||
fx_object *value = dict_at_sk(dict, key);
|
||||
if (value) {
|
||||
fx_object_ref(value);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
static bool dict_has_key(const struct fx_dict_p *dict, const char *key)
|
||||
{
|
||||
return dict_at(dict, key) != NULL;
|
||||
}
|
||||
|
||||
static bool dict_has_skey(const struct fx_dict_p *dict, const fx_string *key)
|
||||
{
|
||||
return dict_at_sk(dict, key) != NULL;
|
||||
}
|
||||
|
||||
static size_t dict_get_size(const struct fx_dict_p *dict)
|
||||
{
|
||||
size_t count = 0;
|
||||
#if 0
|
||||
fx_bst_iterator it1;
|
||||
fx_queue_iterator it2;
|
||||
|
||||
fx_bst_foreach (&it1, &dict->d_buckets) {
|
||||
struct fx_dict_bucket *bucket
|
||||
= fx_unbox(struct fx_dict_bucket, it1.node, bk_node);
|
||||
|
||||
fx_queue_foreach (&it2, &bucket->bk_items) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
static bool dict_is_empty(const struct fx_dict_p *dict)
|
||||
{
|
||||
fx_bst_node *first_node = fx_bst_first(&dict->d_buckets);
|
||||
struct fx_dict_bucket *first_bucket
|
||||
= fx_unbox(struct fx_dict_bucket, first_node, bk_node);
|
||||
if (!first_bucket) {
|
||||
return true;
|
||||
}
|
||||
|
||||
fx_queue_entry *first_entry = fx_queue_first(&first_bucket->bk_items);
|
||||
struct fx_dict_bucket_item *first_item
|
||||
= fx_unbox(struct fx_dict_bucket_item, first_entry, bi_entry);
|
||||
if (!first_item) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool get_next_node(
|
||||
struct fx_bst_node *cur_node,
|
||||
struct fx_queue_entry *cur_entry,
|
||||
struct fx_bst_node **out_next_node,
|
||||
struct fx_queue_entry **out_next_entry)
|
||||
{
|
||||
struct fx_dict_bucket *cur_bucket
|
||||
= fx_unbox(struct fx_dict_bucket, cur_node, bk_node);
|
||||
if (!cur_bucket) {
|
||||
return false;
|
||||
}
|
||||
|
||||
struct fx_dict_bucket_item *cur_item
|
||||
= fx_unbox(struct fx_dict_bucket_item, cur_entry, bi_entry);
|
||||
if (!cur_item) {
|
||||
return false;
|
||||
}
|
||||
|
||||
struct fx_bst_node *next_node = cur_node;
|
||||
struct fx_queue_entry *next_entry = fx_queue_next(cur_entry);
|
||||
if (!next_entry) {
|
||||
next_node = fx_bst_next(cur_node);
|
||||
if (!next_node) {
|
||||
return false;
|
||||
}
|
||||
|
||||
struct fx_dict_bucket *next_bucket
|
||||
= fx_unbox(struct fx_dict_bucket, next_node, bk_node);
|
||||
if (!next_bucket) {
|
||||
return false;
|
||||
}
|
||||
|
||||
next_entry = fx_queue_first(&next_bucket->bk_items);
|
||||
if (!next_entry) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
struct fx_dict_bucket_item *next_item
|
||||
= fx_unbox(struct fx_dict_bucket_item, next_entry, bi_entry);
|
||||
if (!next_item) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*out_next_node = next_node;
|
||||
*out_next_entry = next_entry;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static fx_status delete_item(
|
||||
struct fx_dict_p *dict,
|
||||
struct fx_dict_bucket *bucket,
|
||||
struct fx_dict_bucket_item *item)
|
||||
{
|
||||
fx_queue_delete(&bucket->bk_items, &item->bi_entry);
|
||||
free(item);
|
||||
|
||||
if (fx_queue_empty(&bucket->bk_items)) {
|
||||
fx_bst_delete(&dict->d_buckets, &bucket->bk_node);
|
||||
free(bucket);
|
||||
}
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
/*** PUBLIC FUNCTIONS *********************************************************/
|
||||
|
||||
#if 0
|
||||
fx_dict *fx_dict_create_with_items(const fx_dict_item *items)
|
||||
{
|
||||
fx_dict *dict = fx_dict_create();
|
||||
if (!dict) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_dict_p *p = fx_object_get_private(dict, FX_TYPE_DICT);
|
||||
|
||||
for (size_t i = 0; items[i].key; i++) {
|
||||
dict_put(p, items[i].key, items[i].value);
|
||||
}
|
||||
|
||||
return dict;
|
||||
}
|
||||
#endif
|
||||
|
||||
fx_status fx_dict_put(fx_dict *dict, const char *key, fx_object *value)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_put, dict, key, value);
|
||||
}
|
||||
|
||||
fx_status fx_dict_put_sk(fx_dict *dict, const fx_string *key, fx_object *value)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_put_sk, dict, key, value);
|
||||
}
|
||||
|
||||
fx_object *fx_dict_at(const fx_dict *dict, const char *key)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_at, dict, key);
|
||||
}
|
||||
|
||||
fx_object *fx_dict_at_sk(const fx_dict *dict, const fx_string *key)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_at_sk, dict, key);
|
||||
}
|
||||
|
||||
fx_object *fx_dict_get(fx_dict *dict, const char *key)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_get, dict, key);
|
||||
}
|
||||
|
||||
fx_object *fx_dict_get_sk(fx_dict *dict, const fx_string *key)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_get_sk, dict, key);
|
||||
}
|
||||
|
||||
bool fx_dict_has_key(const fx_dict *dict, const char *key)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_has_key, dict, key);
|
||||
}
|
||||
|
||||
bool fx_dict_has_skey(const fx_dict *dict, const fx_string *key)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_has_skey, dict, key);
|
||||
}
|
||||
|
||||
size_t fx_dict_get_size(const fx_dict *dict)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DICT, dict_get_size, dict);
|
||||
}
|
||||
|
||||
bool fx_dict_is_empty(const fx_dict *dict)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DICT, dict_is_empty, dict);
|
||||
}
|
||||
|
||||
/*** PUBLIC ALIAS FUNCTIONS ***************************************************/
|
||||
/*** VIRTUAL FUNCTIONS ********************************************************/
|
||||
|
||||
static void dict_init(fx_object *obj, void *priv)
|
||||
{
|
||||
struct fx_dict_p *dict = priv;
|
||||
}
|
||||
|
||||
static void dict_fini(fx_object *obj, void *priv)
|
||||
{
|
||||
struct fx_dict_p *dict = priv;
|
||||
|
||||
struct fx_bst_node *node = fx_bst_first(&dict->d_buckets);
|
||||
while (node) {
|
||||
struct fx_dict_bucket *bucket
|
||||
= fx_unbox(struct fx_dict_bucket, node, bk_node);
|
||||
struct fx_bst_node *next_node = fx_bst_next(node);
|
||||
fx_bst_delete(&dict->d_buckets, node);
|
||||
|
||||
struct fx_queue_entry *entry
|
||||
= fx_queue_first(&bucket->bk_items);
|
||||
while (entry) {
|
||||
struct fx_dict_bucket_item *item = fx_unbox(
|
||||
struct fx_dict_bucket_item,
|
||||
entry,
|
||||
bi_entry);
|
||||
struct fx_queue_entry *next_entry
|
||||
= fx_queue_next(entry);
|
||||
fx_queue_delete(&bucket->bk_items, entry);
|
||||
|
||||
free(item->bi_str);
|
||||
fx_object_unref(item->bi_value);
|
||||
free(item);
|
||||
|
||||
entry = next_entry;
|
||||
}
|
||||
|
||||
free(bucket);
|
||||
node = next_node;
|
||||
}
|
||||
}
|
||||
|
||||
static void dict_to_string(const fx_object *obj, fx_stream *out)
|
||||
{
|
||||
struct fx_dict_p *dict = fx_object_get_private(obj, FX_TYPE_DICT);
|
||||
|
||||
if (dict_is_empty(dict)) {
|
||||
fx_stream_write_cstr(out, "{}", NULL);
|
||||
return;
|
||||
}
|
||||
|
||||
fx_stream_write_cstr(out, "{\n", NULL);
|
||||
|
||||
fx_stream_push_indent(out, 1);
|
||||
size_t len = dict_get_size(dict);
|
||||
size_t i = 0;
|
||||
|
||||
struct fx_bst_node *node = fx_bst_first(&dict->d_buckets);
|
||||
while (node) {
|
||||
struct fx_dict_bucket *bucket
|
||||
= fx_unbox(struct fx_dict_bucket, node, bk_node);
|
||||
|
||||
struct fx_queue_entry *entry
|
||||
= fx_queue_first(&bucket->bk_items);
|
||||
while (entry) {
|
||||
struct fx_dict_bucket_item *item = fx_unbox(
|
||||
struct fx_dict_bucket_item,
|
||||
entry,
|
||||
bi_entry);
|
||||
|
||||
fx_object_to_string(item->bi_str, out);
|
||||
fx_stream_write_cstr(out, ": ", NULL);
|
||||
|
||||
bool is_string = fx_object_is_type(
|
||||
item->bi_value,
|
||||
FX_TYPE_STRING);
|
||||
|
||||
if (is_string) {
|
||||
fx_stream_write_char(out, '"');
|
||||
}
|
||||
|
||||
fx_object_to_string(item->bi_value, out);
|
||||
|
||||
if (is_string) {
|
||||
fx_stream_write_char(out, '"');
|
||||
}
|
||||
|
||||
if (i < len - 1) {
|
||||
fx_stream_write_cstr(out, ",", NULL);
|
||||
}
|
||||
|
||||
fx_stream_write_char(out, '\n');
|
||||
entry = fx_queue_next(entry);
|
||||
i++;
|
||||
}
|
||||
node = fx_bst_next(node);
|
||||
}
|
||||
|
||||
fx_stream_pop_indent(out);
|
||||
fx_stream_write_char(out, '}');
|
||||
}
|
||||
|
||||
/*** ITERATOR FUNCTIONS *******************************************************/
|
||||
|
||||
static fx_iterator *iterable_begin(fx_dict *dict)
|
||||
{
|
||||
fx_iterator *it_obj = fx_object_create(FX_TYPE_DICT_ITERATOR);
|
||||
if (!it_obj) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_dict_iterator_p *it
|
||||
= fx_object_get_private(it_obj, FX_TYPE_DICT_ITERATOR);
|
||||
|
||||
it->i = 0;
|
||||
it->_d = dict;
|
||||
it->_d_p = fx_object_get_private(dict, FX_TYPE_DICT);
|
||||
|
||||
if (dict_is_empty(it->_d_p)) {
|
||||
it->item.key = NULL;
|
||||
it->item.value = NULL;
|
||||
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
fx_bst_node *first_node = fx_bst_first(&it->_d_p->d_buckets);
|
||||
struct fx_dict_bucket *first_bucket
|
||||
= fx_unbox(struct fx_dict_bucket, first_node, bk_node);
|
||||
if (!first_bucket) {
|
||||
it->item.key = NULL;
|
||||
it->item.value = NULL;
|
||||
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
fx_queue_entry *first_entry = fx_queue_first(&first_bucket->bk_items);
|
||||
struct fx_dict_bucket_item *first_item
|
||||
= fx_unbox(struct fx_dict_bucket_item, first_entry, bi_entry);
|
||||
if (!first_item) {
|
||||
it->item.key = NULL;
|
||||
it->item.value = NULL;
|
||||
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
it->item.key = first_item->bi_str;
|
||||
it->item.value = first_item->bi_value;
|
||||
|
||||
it->_d = dict;
|
||||
it->_cbn = first_node;
|
||||
it->_cqe = first_entry;
|
||||
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
static const fx_iterator *iterable_cbegin(const fx_dict *dict)
|
||||
{
|
||||
fx_iterator *it_obj = fx_object_create(FX_TYPE_DICT_ITERATOR);
|
||||
if (!it_obj) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_dict_iterator_p *it
|
||||
= fx_object_get_private(it_obj, FX_TYPE_DICT_ITERATOR);
|
||||
|
||||
it->i = 0;
|
||||
it->_d = (fx_dict *)dict;
|
||||
it->_d_p = fx_object_get_private(dict, FX_TYPE_DICT);
|
||||
|
||||
if (dict_is_empty(it->_d_p)) {
|
||||
it->item.key = NULL;
|
||||
it->item.value = NULL;
|
||||
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
fx_bst_node *first_node = fx_bst_first(&it->_d_p->d_buckets);
|
||||
struct fx_dict_bucket *first_bucket
|
||||
= fx_unbox(struct fx_dict_bucket, first_node, bk_node);
|
||||
if (!first_bucket) {
|
||||
it->item.key = NULL;
|
||||
it->item.value = NULL;
|
||||
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
fx_queue_entry *first_entry = fx_queue_first(&first_bucket->bk_items);
|
||||
struct fx_dict_bucket_item *first_item
|
||||
= fx_unbox(struct fx_dict_bucket_item, first_entry, bi_entry);
|
||||
if (!first_item) {
|
||||
it->item.key = NULL;
|
||||
it->item.value = NULL;
|
||||
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
it->item.key = first_item->bi_str;
|
||||
it->item.value = first_item->bi_value;
|
||||
|
||||
it->_cbn = first_node;
|
||||
it->_cqe = first_entry;
|
||||
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_dict_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_DICT_ITERATOR);
|
||||
|
||||
struct fx_bst_node *next_node;
|
||||
struct fx_queue_entry *next_entry;
|
||||
if (!get_next_node(it->_cbn, it->_cqe, &next_node, &next_entry)) {
|
||||
it->item.key = NULL;
|
||||
it->item.value = NULL;
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
struct fx_dict_bucket_item *next_item
|
||||
= fx_unbox(struct fx_dict_bucket_item, next_entry, bi_entry);
|
||||
|
||||
if (!next_item) {
|
||||
it->item.key = NULL;
|
||||
it->item.value = NULL;
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
it->i++;
|
||||
it->item.key = next_item->bi_str;
|
||||
it->item.value = next_item->bi_value;
|
||||
|
||||
it->_cbn = next_node;
|
||||
it->_cqe = next_entry;
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static enum fx_status iterator_erase(fx_iterator *obj)
|
||||
{
|
||||
struct fx_dict_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_DICT_ITERATOR);
|
||||
|
||||
if ((it->item.key || it->item.value) && !(it->_cbn && it->_cqe)) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
if (!it->item.key || !it->_cqe) {
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
struct fx_bst_node *next_node;
|
||||
struct fx_queue_entry *next_entry;
|
||||
if (!get_next_node(it->_cbn, it->_cqe, &next_node, &next_entry)) {
|
||||
it->item.key = NULL;
|
||||
it->item.value = NULL;
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
struct fx_dict_bucket *cur_bucket
|
||||
= fx_unbox(struct fx_dict_bucket, it->_cbn, bk_node);
|
||||
struct fx_dict_bucket_item *cur_item
|
||||
= fx_unbox(struct fx_dict_bucket_item, it->_cqe, bi_entry);
|
||||
|
||||
struct fx_dict_bucket_item *next_item
|
||||
= fx_unbox(struct fx_dict_bucket_item, next_entry, bi_entry);
|
||||
|
||||
fx_status status = delete_item(it->_d_p, cur_bucket, cur_item);
|
||||
if (FX_ERR(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
if (next_item) {
|
||||
it->item.key = next_item->bi_str;
|
||||
it->item.value = next_item->bi_value;
|
||||
|
||||
it->_cbn = next_node;
|
||||
it->_cqe = next_entry;
|
||||
} else {
|
||||
it->item.key = NULL;
|
||||
it->item.value = NULL;
|
||||
|
||||
it->_cbn = NULL;
|
||||
it->_cqe = NULL;
|
||||
}
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_iterator_value iterator_get_value(fx_iterator *obj)
|
||||
{
|
||||
struct fx_dict_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_DICT_ITERATOR);
|
||||
|
||||
return FX_ITERATOR_VALUE_PTR(&it->item);
|
||||
}
|
||||
|
||||
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_dict_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_DICT_ITERATOR);
|
||||
|
||||
return FX_ITERATOR_VALUE_CPTR(&it->item);
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
// ---- fx_dict DEFINITION
|
||||
FX_TYPE_CLASS_BEGIN(fx_dict)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = dict_to_string;
|
||||
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) = iterable_begin;
|
||||
FX_INTERFACE_ENTRY(it_cbegin) = iterable_cbegin;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
|
||||
FX_TYPE_CLASS_END(fx_dict)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_dict)
|
||||
FX_TYPE_ID(0xd2af61d9, 0xd0be, 0x4960, 0xbe3f, 0x509749814c10);
|
||||
FX_TYPE_CLASS(fx_dict_class);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_ITERABLE);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_dict_p);
|
||||
FX_TYPE_INSTANCE_INIT(dict_init);
|
||||
FX_TYPE_INSTANCE_FINI(dict_fini);
|
||||
FX_TYPE_DEFINITION_END(fx_dict)
|
||||
|
||||
// ---- fx_dict_iterator DEFINITION
|
||||
FX_TYPE_CLASS_BEGIN(fx_dict_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_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
|
||||
FX_TYPE_CLASS_END(fx_dict_iterator)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_dict_iterator)
|
||||
FX_TYPE_ID(0x9ea96701, 0x1713, 0x4a3e, 0xbf63, 0xdc856b456f3b);
|
||||
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
|
||||
FX_TYPE_CLASS(fx_dict_iterator_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_dict_iterator_p);
|
||||
FX_TYPE_DEFINITION_END(fx_dict_iterator)
|
||||
+76
-55
@@ -32,8 +32,9 @@ struct fx_hashmap_p {
|
||||
struct fx_hashmap_iterator_p {
|
||||
size_t i;
|
||||
fx_hashmap_item item;
|
||||
fx_value item_value;
|
||||
|
||||
fx_hashmap *_h;
|
||||
const fx_hashmap *_h;
|
||||
struct fx_hashmap_p *_h_p;
|
||||
fx_bst_node *_cbn;
|
||||
fx_queue_entry *_cqe;
|
||||
@@ -112,14 +113,18 @@ static bool get_next_node(
|
||||
struct fx_bst_node **out_next_node,
|
||||
struct fx_queue_entry **out_next_entry)
|
||||
{
|
||||
struct fx_hashmap_bucket *cur_bucket
|
||||
= fx_unbox(struct fx_hashmap_bucket, cur_node, bk_node);
|
||||
struct fx_hashmap_bucket *cur_bucket = fx_unbox(
|
||||
struct fx_hashmap_bucket,
|
||||
cur_node,
|
||||
bk_node);
|
||||
if (!cur_bucket) {
|
||||
return false;
|
||||
}
|
||||
|
||||
struct fx_hashmap_bucket_item *cur_item
|
||||
= fx_unbox(struct fx_hashmap_bucket_item, cur_entry, bi_entry);
|
||||
struct fx_hashmap_bucket_item *cur_item = fx_unbox(
|
||||
struct fx_hashmap_bucket_item,
|
||||
cur_entry,
|
||||
bi_entry);
|
||||
if (!cur_item) {
|
||||
return false;
|
||||
}
|
||||
@@ -146,8 +151,10 @@ static bool get_next_node(
|
||||
}
|
||||
}
|
||||
|
||||
struct fx_hashmap_bucket_item *next_item
|
||||
= fx_unbox(struct fx_hashmap_bucket_item, next_entry, bi_entry);
|
||||
struct fx_hashmap_bucket_item *next_item = fx_unbox(
|
||||
struct fx_hashmap_bucket_item,
|
||||
next_entry,
|
||||
bi_entry);
|
||||
if (!next_item) {
|
||||
return false;
|
||||
}
|
||||
@@ -186,8 +193,9 @@ static fx_status hashmap_put(
|
||||
const fx_hashmap_value *value)
|
||||
{
|
||||
uint64_t hash = hash_key(key);
|
||||
struct fx_hashmap_bucket *bucket
|
||||
= get_bucket(&hashmap->h_buckets, hash);
|
||||
struct fx_hashmap_bucket *bucket = get_bucket(
|
||||
&hashmap->h_buckets,
|
||||
hash);
|
||||
|
||||
if (!bucket) {
|
||||
bucket = create_bucket();
|
||||
@@ -234,8 +242,9 @@ static const struct fx_hashmap_value *hashmap_get(
|
||||
{
|
||||
uint64_t hash = hash_key(key);
|
||||
|
||||
struct fx_hashmap_bucket *bucket
|
||||
= get_bucket(&hashmap->h_buckets, hash);
|
||||
struct fx_hashmap_bucket *bucket = get_bucket(
|
||||
&hashmap->h_buckets,
|
||||
hash);
|
||||
if (!bucket) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -262,8 +271,9 @@ static bool hashmap_has_key(
|
||||
const fx_hashmap_key *key)
|
||||
{
|
||||
uint64_t hash = hash_key(key);
|
||||
struct fx_hashmap_bucket *bucket
|
||||
= get_bucket(&hashmap->h_buckets, hash);
|
||||
struct fx_hashmap_bucket *bucket = get_bucket(
|
||||
&hashmap->h_buckets,
|
||||
hash);
|
||||
if (!bucket) {
|
||||
return false;
|
||||
}
|
||||
@@ -293,8 +303,10 @@ static size_t hashmap_get_size(const struct fx_hashmap_p *hashmap)
|
||||
static bool hashmap_is_empty(const struct fx_hashmap_p *hashmap)
|
||||
{
|
||||
fx_bst_node *first_node = fx_bst_first(&hashmap->h_buckets);
|
||||
struct fx_hashmap_bucket *first_bucket
|
||||
= fx_unbox(struct fx_hashmap_bucket, first_node, bk_node);
|
||||
struct fx_hashmap_bucket *first_bucket = fx_unbox(
|
||||
struct fx_hashmap_bucket,
|
||||
first_node,
|
||||
bk_node);
|
||||
if (!first_bucket) {
|
||||
return true;
|
||||
}
|
||||
@@ -358,8 +370,9 @@ fx_hashmap *fx_hashmap_create_with_items(const fx_hashmap_item *items)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_hashmap_p *p
|
||||
= fx_object_get_private(hashmap, FX_TYPE_HASHMAP);
|
||||
struct fx_hashmap_p *p = fx_object_get_private(
|
||||
hashmap,
|
||||
FX_TYPE_HASHMAP);
|
||||
|
||||
for (size_t i = 0; items[i].key.key_data && items[i].key.key_size;
|
||||
i++) {
|
||||
@@ -408,12 +421,13 @@ bool fx_hashmap_is_empty(const fx_hashmap *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_TYPE_HASHMAP_ITERATOR);
|
||||
struct fx_hashmap_iterator_p *it
|
||||
= fx_object_get_private(it_obj, FX_TYPE_HASHMAP_ITERATOR);
|
||||
fx_hashmap_iterator *it_obj = fx_object_create(
|
||||
FX_TYPE_HASHMAP_ITERATOR);
|
||||
struct fx_hashmap_iterator_p *it = fx_object_get_private(
|
||||
it_obj,
|
||||
FX_TYPE_HASHMAP_ITERATOR);
|
||||
|
||||
it->_h = hashmap;
|
||||
it->_h_p = fx_object_get_private(hashmap, FX_TYPE_HASHMAP);
|
||||
@@ -426,16 +440,18 @@ fx_iterator *fx_hashmap_begin(fx_hashmap *hashmap)
|
||||
}
|
||||
|
||||
struct fx_bst_node *first_node = fx_bst_first(&it->_h_p->h_buckets);
|
||||
struct fx_hashmap_bucket *first_bucket
|
||||
= fx_unbox(struct fx_hashmap_bucket, first_node, bk_node);
|
||||
struct fx_hashmap_bucket *first_bucket = fx_unbox(
|
||||
struct fx_hashmap_bucket,
|
||||
first_node,
|
||||
bk_node);
|
||||
if (!first_bucket) {
|
||||
memset(&it->item, 0x0, sizeof it->item);
|
||||
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
struct fx_queue_entry *first_entry
|
||||
= fx_queue_first(&first_bucket->bk_items);
|
||||
struct fx_queue_entry *first_entry = fx_queue_first(
|
||||
&first_bucket->bk_items);
|
||||
struct fx_hashmap_bucket_item *first_item = fx_unbox(
|
||||
struct fx_hashmap_bucket_item,
|
||||
first_entry,
|
||||
@@ -473,8 +489,10 @@ static void hashmap_fini(fx_object *obj, void *priv)
|
||||
|
||||
struct fx_bst_node *node = fx_bst_first(&map->h_buckets);
|
||||
while (node) {
|
||||
struct fx_hashmap_bucket *b
|
||||
= fx_unbox(struct fx_hashmap_bucket, node, bk_node);
|
||||
struct fx_hashmap_bucket *b = fx_unbox(
|
||||
struct fx_hashmap_bucket,
|
||||
node,
|
||||
bk_node);
|
||||
struct fx_bst_node *next_node = fx_bst_next(node);
|
||||
fx_bst_delete(&map->h_buckets, node);
|
||||
|
||||
@@ -484,8 +502,8 @@ static void hashmap_fini(fx_object *obj, void *priv)
|
||||
struct fx_hashmap_bucket_item,
|
||||
entry,
|
||||
bi_entry);
|
||||
struct fx_queue_entry *next_entry
|
||||
= fx_queue_next(entry);
|
||||
struct fx_queue_entry *next_entry = fx_queue_next(
|
||||
entry);
|
||||
fx_queue_delete(&b->bk_items, entry);
|
||||
|
||||
if (map->h_key_dtor) {
|
||||
@@ -510,8 +528,9 @@ static void hashmap_fini(fx_object *obj, void *priv)
|
||||
|
||||
static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_hashmap_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_HASHMAP_ITERATOR);
|
||||
struct fx_hashmap_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_TYPE_HASHMAP_ITERATOR);
|
||||
|
||||
struct fx_bst_node *next_node;
|
||||
struct fx_queue_entry *next_entry;
|
||||
@@ -520,8 +539,10 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
struct fx_hashmap_bucket_item *next_item
|
||||
= fx_unbox(struct fx_hashmap_bucket_item, next_entry, bi_entry);
|
||||
struct fx_hashmap_bucket_item *next_item = fx_unbox(
|
||||
struct fx_hashmap_bucket_item,
|
||||
next_entry,
|
||||
bi_entry);
|
||||
|
||||
if (!next_item) {
|
||||
memset(&it->item, 0x0, sizeof it->item);
|
||||
@@ -540,8 +561,9 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||
|
||||
static enum fx_status iterator_erase(fx_iterator *obj)
|
||||
{
|
||||
struct fx_hashmap_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_HASHMAP_ITERATOR);
|
||||
struct fx_hashmap_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_TYPE_HASHMAP_ITERATOR);
|
||||
|
||||
if ((it->item.key.key_data || it->item.value.value_data)
|
||||
&& !(it->_cbn && it->_cqe)) {
|
||||
@@ -559,13 +581,19 @@ static enum fx_status iterator_erase(fx_iterator *obj)
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
struct fx_hashmap_bucket *cur_bucket
|
||||
= fx_unbox(struct fx_hashmap_bucket, it->_cbn, bk_node);
|
||||
struct fx_hashmap_bucket_item *cur_item
|
||||
= fx_unbox(struct fx_hashmap_bucket_item, it->_cqe, bi_entry);
|
||||
struct fx_hashmap_bucket *cur_bucket = fx_unbox(
|
||||
struct fx_hashmap_bucket,
|
||||
it->_cbn,
|
||||
bk_node);
|
||||
struct fx_hashmap_bucket_item *cur_item = fx_unbox(
|
||||
struct fx_hashmap_bucket_item,
|
||||
it->_cqe,
|
||||
bi_entry);
|
||||
|
||||
struct fx_hashmap_bucket_item *next_item
|
||||
= fx_unbox(struct fx_hashmap_bucket_item, next_entry, bi_entry);
|
||||
struct fx_hashmap_bucket_item *next_item = fx_unbox(
|
||||
struct fx_hashmap_bucket_item,
|
||||
next_entry,
|
||||
bi_entry);
|
||||
|
||||
fx_status status = delete_item(it->_h_p, cur_bucket, cur_item);
|
||||
if (FX_ERR(status)) {
|
||||
@@ -590,18 +618,13 @@ static enum fx_status iterator_erase(fx_iterator *obj)
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_iterator_value iterator_get_value(fx_iterator *obj)
|
||||
static const fx_value *iterator_get_value(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_hashmap_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_HASHMAP_ITERATOR);
|
||||
return FX_ITERATOR_VALUE_PTR(&it->item);
|
||||
}
|
||||
|
||||
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
|
||||
{
|
||||
const struct fx_hashmap_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_HASHMAP_ITERATOR);
|
||||
return FX_ITERATOR_VALUE_CPTR(&it->item);
|
||||
struct fx_hashmap_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_TYPE_HASHMAP_ITERATOR);
|
||||
it->item_value = FX_POINTER(&it->item);
|
||||
return &it->item_value;
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
@@ -614,7 +637,6 @@ FX_TYPE_CLASS_BEGIN(fx_hashmap)
|
||||
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
|
||||
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_CLASS_END(fx_hashmap)
|
||||
|
||||
@@ -637,7 +659,6 @@ FX_TYPE_CLASS_BEGIN(fx_hashmap_iterator)
|
||||
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
|
||||
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
|
||||
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
|
||||
FX_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
|
||||
FX_TYPE_CLASS_END(fx_hashmap_iterator)
|
||||
|
||||
|
||||
@@ -0,0 +1,765 @@
|
||||
#include <assert.h>
|
||||
#include <fx/collections/hashtable.h>
|
||||
#include <fx/misc.h>
|
||||
#include <fx/reflection/property.h>
|
||||
#include <fx/status.h>
|
||||
#include <fx/string.h>
|
||||
#include <fx/vector.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/*** PRIVATE DATA *************************************************************/
|
||||
|
||||
/* these primes are used as array capacities to avoid hash collisions. */
|
||||
static const size_t primes[] = {
|
||||
11,
|
||||
23,
|
||||
47,
|
||||
97,
|
||||
197,
|
||||
397,
|
||||
797,
|
||||
1597,
|
||||
3203,
|
||||
6421,
|
||||
12853,
|
||||
25717,
|
||||
51437,
|
||||
};
|
||||
|
||||
struct table_item {
|
||||
fx_hashtable_item *i_item;
|
||||
};
|
||||
|
||||
struct fx_hashtable_item_p {
|
||||
fx_hashtable_item *i_self;
|
||||
fx_value i_key, i_value;
|
||||
};
|
||||
|
||||
struct fx_hashtable_p {
|
||||
struct table_item *t_items;
|
||||
/* number of items currently stored in t_items */
|
||||
size_t t_count;
|
||||
/* index in primes[] that represents the current maximum size of
|
||||
* t_items */
|
||||
size_t t_max_index;
|
||||
};
|
||||
|
||||
struct fx_hashtable_iterator_p {
|
||||
size_t i;
|
||||
fx_hashtable_item *item;
|
||||
fx_value item_value;
|
||||
|
||||
const fx_hashtable *_h;
|
||||
struct fx_hashtable_p *_h_p;
|
||||
};
|
||||
|
||||
/*** PRIVATE FUNCTIONS ********************************************************/
|
||||
|
||||
static fx_status convert_key_to_index(
|
||||
const fx_value *v,
|
||||
struct table_item *table,
|
||||
size_t table_size,
|
||||
size_t *out_index)
|
||||
{
|
||||
uint64_t hash = 0;
|
||||
fx_status status = fx_value_hash(v, &hash);
|
||||
if (!FX_OK(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
size_t base_index = hash % 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(
|
||||
table[index].i_item,
|
||||
FX_TYPE_HASHTABLE_ITEM);
|
||||
int cmp = fx_value_compare(&item->i_key, v);
|
||||
if (cmp == 0) {
|
||||
index_found = true;
|
||||
break;
|
||||
}
|
||||
|
||||
skip:
|
||||
index = (base_index + (i * i)) % table_size;
|
||||
i++;
|
||||
}
|
||||
|
||||
if (!index_found) {
|
||||
return FX_ERR_NO_ENTRY;
|
||||
}
|
||||
|
||||
assert(index < table_size);
|
||||
*out_index = index;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_status allocate_index_for_key(
|
||||
const fx_value *v,
|
||||
struct table_item *table,
|
||||
size_t table_size,
|
||||
size_t *out_index)
|
||||
{
|
||||
uint64_t hash = 0;
|
||||
fx_status status = fx_value_hash(v, &hash);
|
||||
if (!FX_OK(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
size_t index = hash % table_size;
|
||||
bool ok = false;
|
||||
|
||||
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(
|
||||
table[index].i_item,
|
||||
FX_TYPE_HASHTABLE_ITEM);
|
||||
int cmp = fx_value_compare(&item->i_key, v);
|
||||
if (cmp == 0) {
|
||||
ok = true;
|
||||
break;
|
||||
}
|
||||
|
||||
index = (index + (i * i)) % table_size;
|
||||
i++;
|
||||
}
|
||||
|
||||
if (!ok) {
|
||||
return FX_ERR_NO_SPACE;
|
||||
}
|
||||
|
||||
assert(index < table_size);
|
||||
*out_index = index;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_status add_item_to_table(
|
||||
fx_hashtable_item *item,
|
||||
struct table_item *table,
|
||||
size_t table_capacity)
|
||||
{
|
||||
size_t index = 0;
|
||||
struct fx_hashtable_item_p *item_p
|
||||
= fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
|
||||
fx_status status = allocate_index_for_key(
|
||||
&item_p->i_key,
|
||||
table,
|
||||
table_capacity,
|
||||
&index);
|
||||
if (!FX_OK(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
if (index >= table_capacity) {
|
||||
return FX_ERR_NO_SPACE;
|
||||
}
|
||||
|
||||
struct table_item *bucket = &table[index];
|
||||
if (bucket->i_item) {
|
||||
fx_hashtable_item_unref(bucket->i_item);
|
||||
}
|
||||
|
||||
bucket->i_item = item;
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_status destroy_table(
|
||||
struct table_item *table,
|
||||
size_t length,
|
||||
bool destroy_items)
|
||||
{
|
||||
if (!destroy_items) {
|
||||
free(table);
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < length; i++) {
|
||||
struct table_item *item = &table[i];
|
||||
if (!item->i_item) {
|
||||
continue;
|
||||
}
|
||||
|
||||
fx_hashtable_item_unref(item->i_item);
|
||||
}
|
||||
|
||||
free(table);
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_status migrate_table(
|
||||
struct table_item *src_table,
|
||||
size_t src_length,
|
||||
struct table_item *dest_table,
|
||||
size_t dest_capacity)
|
||||
{
|
||||
for (size_t i = 0; i < src_length; i++) {
|
||||
struct table_item *item = &src_table[i];
|
||||
if (!item->i_item) {
|
||||
continue;
|
||||
}
|
||||
|
||||
add_item_to_table(item->i_item, dest_table, dest_capacity);
|
||||
}
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_status resize_table(
|
||||
struct fx_hashtable_p *hashtable,
|
||||
size_t new_capacity_index)
|
||||
{
|
||||
size_t capacity = primes[new_capacity_index];
|
||||
struct table_item *new_table = calloc(capacity, sizeof *new_table);
|
||||
if (!new_table) {
|
||||
return FX_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
fx_status status = migrate_table(
|
||||
hashtable->t_items,
|
||||
primes[hashtable->t_max_index],
|
||||
new_table,
|
||||
capacity);
|
||||
if (!FX_OK(status)) {
|
||||
free(new_table);
|
||||
return status;
|
||||
}
|
||||
|
||||
free(hashtable->t_items);
|
||||
hashtable->t_items = new_table;
|
||||
hashtable->t_max_index = new_capacity_index;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_status hashtable_put(
|
||||
struct fx_hashtable_p *hashtable,
|
||||
fx_value *key,
|
||||
fx_value *value)
|
||||
{
|
||||
fx_status status = FX_SUCCESS;
|
||||
size_t capacity = primes[hashtable->t_max_index];
|
||||
if (hashtable->t_count + 1 > capacity) {
|
||||
status = resize_table(hashtable, hashtable->t_max_index + 1);
|
||||
if (!FX_OK(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
capacity = primes[hashtable->t_max_index];
|
||||
}
|
||||
|
||||
size_t index = 0;
|
||||
do {
|
||||
status = allocate_index_for_key(
|
||||
key,
|
||||
hashtable->t_items,
|
||||
capacity,
|
||||
&index);
|
||||
if (status == FX_ERR_NO_SPACE) {
|
||||
status = resize_table(
|
||||
hashtable,
|
||||
hashtable->t_max_index + 1);
|
||||
if (!FX_OK(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
|
||||
fx_value_copy(&item_p->i_value, value);
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static const fx_value *hashtable_get(
|
||||
const struct fx_hashtable_p *hashtable,
|
||||
const fx_value *key)
|
||||
{
|
||||
size_t index = 0;
|
||||
size_t capacity = primes[hashtable->t_max_index];
|
||||
fx_status status = convert_key_to_index(
|
||||
key,
|
||||
hashtable->t_items,
|
||||
capacity,
|
||||
&index);
|
||||
if (!FX_OK(status)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const fx_hashtable_item *item = hashtable->t_items[index].i_item;
|
||||
if (!item) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const struct fx_hashtable_item_p *item_p
|
||||
= fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
|
||||
return &item_p->i_value;
|
||||
}
|
||||
|
||||
static size_t hashtable_get_count(const struct fx_hashtable_p *hashtable)
|
||||
{
|
||||
return hashtable->t_count;
|
||||
}
|
||||
|
||||
static bool hashtable_is_empty(const struct fx_hashtable_p *hashtable)
|
||||
{
|
||||
return hashtable->t_count == 0;
|
||||
}
|
||||
|
||||
static const fx_value *hashtable_item_get_key(
|
||||
const struct fx_hashtable_item_p *item)
|
||||
{
|
||||
return &item->i_key;
|
||||
}
|
||||
|
||||
static const fx_value *hashtable_item_get_value(
|
||||
const struct fx_hashtable_item_p *item)
|
||||
{
|
||||
return &item->i_value;
|
||||
}
|
||||
|
||||
/*** PUBLIC FUNCTIONS *********************************************************/
|
||||
|
||||
fx_status fx_hashtable_put(
|
||||
fx_hashtable *hashtable,
|
||||
fx_value *key,
|
||||
fx_value *value)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_TYPE_HASHTABLE,
|
||||
hashtable_put,
|
||||
hashtable,
|
||||
key,
|
||||
value);
|
||||
}
|
||||
|
||||
const fx_value *fx_hashtable_get(
|
||||
const fx_hashtable *hashtable,
|
||||
const fx_value *key)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_TYPE_HASHTABLE,
|
||||
hashtable_get,
|
||||
hashtable,
|
||||
key);
|
||||
}
|
||||
|
||||
size_t fx_hashtable_get_count(const fx_hashtable *hashtable)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
FX_TYPE_HASHTABLE,
|
||||
hashtable_get_count,
|
||||
hashtable);
|
||||
}
|
||||
|
||||
bool fx_hashtable_is_empty(const fx_hashtable *hashtable)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
FX_TYPE_HASHTABLE,
|
||||
hashtable_is_empty,
|
||||
hashtable);
|
||||
}
|
||||
|
||||
const fx_value *fx_hashtable_item_get_key(const fx_hashtable_item *item)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
FX_TYPE_HASHTABLE_ITEM,
|
||||
hashtable_item_get_key,
|
||||
item);
|
||||
}
|
||||
|
||||
const fx_value *fx_hashtable_item_get_value(const fx_hashtable_item *item)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
FX_TYPE_HASHTABLE_ITEM,
|
||||
hashtable_item_get_value,
|
||||
item);
|
||||
}
|
||||
|
||||
const fx_iterator *fx_hashtable_begin(const fx_hashtable *hashtable)
|
||||
{
|
||||
fx_hashtable_iterator *it_obj
|
||||
= fx_object_create(FX_TYPE_HASHTABLE_ITERATOR);
|
||||
struct fx_hashtable_iterator_p *it
|
||||
= fx_object_get_private(it_obj, FX_TYPE_HASHTABLE_ITERATOR);
|
||||
|
||||
it->_h = hashtable;
|
||||
it->_h_p = fx_object_get_private(hashtable, FX_TYPE_HASHTABLE);
|
||||
|
||||
it->i = 0;
|
||||
if (fx_hashtable_is_empty(hashtable)) {
|
||||
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
struct table_item *table = it->_h_p->t_items;
|
||||
size_t capacity = primes[it->_h_p->t_max_index];
|
||||
|
||||
while (it->i < capacity && table[it->i].i_item == NULL) {
|
||||
it->i++;
|
||||
}
|
||||
|
||||
it->item_value = FX_VALUE_OBJECT_REF(table[it->i].i_item);
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
/*** VIRTUAL FUNCTIONS ********************************************************/
|
||||
|
||||
static void hashtable_init(fx_object *obj, void *priv)
|
||||
{
|
||||
struct fx_hashtable_p *map = priv;
|
||||
map->t_max_index = 0;
|
||||
map->t_count = 0;
|
||||
|
||||
size_t capacity = primes[map->t_max_index];
|
||||
map->t_items = calloc(capacity, sizeof *map->t_items);
|
||||
}
|
||||
|
||||
static void hashtable_fini(fx_object *obj, void *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(
|
||||
const fx_value *obj,
|
||||
fx_stream *out,
|
||||
const char *format)
|
||||
{
|
||||
struct fx_hashtable_p *hashtable
|
||||
= fx_object_get_private(obj->v_object, FX_TYPE_HASHTABLE);
|
||||
|
||||
if (!hashtable->t_count) {
|
||||
fx_stream_write_cstr(out, "{}", NULL);
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
fx_stream_write_cstr(out, "{\n", NULL);
|
||||
|
||||
fx_stream_push_indent(out, 1);
|
||||
|
||||
size_t nr_written = 0;
|
||||
size_t capacity = primes[hashtable->t_max_index];
|
||||
for (size_t i = 0; i < capacity; i++) {
|
||||
fx_hashtable_item *item = hashtable->t_items[i].i_item;
|
||||
if (!item) {
|
||||
continue;
|
||||
}
|
||||
|
||||
nr_written++;
|
||||
struct fx_hashtable_item_p *item_p
|
||||
= fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
|
||||
|
||||
const char *cstr = NULL;
|
||||
fx_value_get_cstr(&item_p->i_key, &cstr);
|
||||
|
||||
if (cstr) {
|
||||
fx_stream_write_char(out, '"');
|
||||
}
|
||||
|
||||
fx_value_to_string(&item_p->i_key, out, NULL);
|
||||
|
||||
if (cstr) {
|
||||
fx_stream_write_char(out, '"');
|
||||
}
|
||||
|
||||
fx_stream_write_cstr(out, " = ", NULL);
|
||||
|
||||
fx_value_get_cstr(&item_p->i_value, &cstr);
|
||||
if (cstr) {
|
||||
fx_stream_write_char(out, '"');
|
||||
}
|
||||
|
||||
fx_value_to_string(&item_p->i_value, out, NULL);
|
||||
|
||||
if (cstr) {
|
||||
fx_stream_write_char(out, '"');
|
||||
}
|
||||
|
||||
if (nr_written != hashtable->t_count) {
|
||||
fx_stream_write_char(out, ',');
|
||||
}
|
||||
|
||||
fx_stream_write_char(out, '\n');
|
||||
}
|
||||
|
||||
fx_stream_pop_indent(out);
|
||||
fx_stream_write_char(out, '}');
|
||||
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(
|
||||
const fx_value *item_value,
|
||||
const fx_property *prop,
|
||||
fx_value *out)
|
||||
{
|
||||
fx_hashtable_item *item = NULL;
|
||||
fx_value_get_object(item_value, &item);
|
||||
if (!item) {
|
||||
return FX_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
struct fx_hashtable_item_p *item_p
|
||||
= fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
|
||||
fx_value_copy(out, &item_p->i_key);
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_status get_value(
|
||||
const fx_value *item_value,
|
||||
const fx_property *prop,
|
||||
fx_value *out)
|
||||
{
|
||||
fx_hashtable_item *item = NULL;
|
||||
fx_value_get_object(item_value, &item);
|
||||
if (!item) {
|
||||
return FX_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
struct fx_hashtable_item_p *item_p
|
||||
= fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
|
||||
fx_value_copy(out, &item_p->i_value);
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
/*** ITERATOR FUNCTIONS *******************************************************/
|
||||
|
||||
static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_hashtable_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_HASHTABLE_ITERATOR);
|
||||
|
||||
fx_value_unset(&it->item_value);
|
||||
struct table_item *table = it->_h_p->t_items;
|
||||
size_t capacity = primes[it->_h_p->t_max_index];
|
||||
|
||||
it->i++;
|
||||
while (it->i < capacity && table[it->i].i_item == NULL) {
|
||||
it->i++;
|
||||
}
|
||||
|
||||
if (it->i >= capacity || !table[it->i].i_item) {
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
it->item_value = FX_VALUE_OBJECT_REF(table[it->i].i_item);
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static enum fx_status iterator_erase(fx_iterator *obj)
|
||||
{
|
||||
struct fx_hashtable_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_HASHTABLE_ITERATOR);
|
||||
|
||||
struct table_item *table = it->_h_p->t_items;
|
||||
size_t capacity = primes[it->_h_p->t_max_index];
|
||||
|
||||
if (table[it->i].i_item) {
|
||||
fx_hashtable_item_unref(table[it->i].i_item);
|
||||
table[it->i].i_item = NULL;
|
||||
}
|
||||
|
||||
it->i++;
|
||||
while (it->i < capacity && table[it->i].i_item == NULL) {
|
||||
it->i++;
|
||||
}
|
||||
|
||||
if (it->i >= capacity || !table[it->i].i_item) {
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static const fx_value *iterator_get_value(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_hashtable_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_HASHTABLE_ITERATOR);
|
||||
struct table_item *table = it->_h_p->t_items;
|
||||
size_t capacity = primes[it->_h_p->t_max_index];
|
||||
|
||||
if (it->i >= capacity) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (table[it->i].i_item) {
|
||||
return &it->item_value;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
// ---- fx_hashtable DEFINITION
|
||||
FX_TYPE_CLASS_BEGIN(fx_hashtable)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
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_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
|
||||
FX_INTERFACE_ENTRY(it_begin) = fx_hashtable_begin;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
|
||||
FX_TYPE_CLASS_END(fx_hashtable)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_hashtable)
|
||||
FX_TYPE_ID(0xc73b431c, 0x5bd5, 0x45ac, 0x888b, 0x35d352db1fe4);
|
||||
FX_TYPE_NAME("fx.collections.hashtable");
|
||||
FX_TYPE_CLASS(fx_hashtable_class);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_ITERABLE);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_hashtable_p);
|
||||
FX_TYPE_INSTANCE_INIT(hashtable_init);
|
||||
FX_TYPE_INSTANCE_FINI(hashtable_fini);
|
||||
FX_TYPE_DEFINITION_END(fx_hashtable)
|
||||
|
||||
// ---- fx_hashtable_iterator DEFINITION
|
||||
FX_TYPE_CLASS_BEGIN(fx_hashtable_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_hashtable_iterator)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_hashtable_iterator)
|
||||
FX_TYPE_ID(0x42c6836d, 0xf801, 0x49f9, 0xba58, 0xf9cd6f1cb22d);
|
||||
FX_TYPE_NAME("fx.collections.hashtable.iterator");
|
||||
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
|
||||
FX_TYPE_CLASS(fx_hashtable_iterator_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_hashtable_iterator_p);
|
||||
FX_TYPE_INSTANCE_FINI(hashtable_iterator_fini);
|
||||
FX_TYPE_DEFINITION_END(fx_hashtable_iterator)
|
||||
|
||||
// ---- fx_hashtable_item DEFINITION
|
||||
FX_TYPE_CLASS_BEGIN(fx_hashtable_item)
|
||||
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_PROPERTY("key", get_key, NULL);
|
||||
FX_TYPE_PROPERTY("value", get_value, NULL);
|
||||
FX_TYPE_CLASS_END(fx_hashtable_item)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_hashtable_item)
|
||||
FX_TYPE_ID(0x748f78f3, 0x53df, 0x406c, 0x8777, 0x2075bd11fb97);
|
||||
FX_TYPE_NAME("fx.collections.hashtable.item");
|
||||
FX_TYPE_EXTENDS(FX_TYPE_OBJECT);
|
||||
FX_TYPE_CLASS(fx_hashtable_item_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_hashtable_item_p);
|
||||
FX_TYPE_INSTANCE_FINI(hashtable_item_fini);
|
||||
FX_TYPE_DEFINITION_END(fx_hashtable_item)
|
||||
@@ -1,13 +1,5 @@
|
||||
/**
|
||||
* A heterogeneous array of objects. fx_array only stores references
|
||||
* to the objects that it contains, not the object data itself.
|
||||
*
|
||||
* fx_array stores pointers to objects in a single contiguous array,
|
||||
* but this is an implementation detail that may change in the future.
|
||||
* Users of fx_array should not rely on this being the case.
|
||||
*/
|
||||
#ifndef FX_DS_ARRAY_H_
|
||||
#define FX_DS_ARRAY_H_
|
||||
#ifndef FX_COLLECTIONS_ARRAY_H_
|
||||
#define FX_COLLECTIONS_ARRAY_H_
|
||||
|
||||
#include <fx/iterator.h>
|
||||
#include <fx/macros.h>
|
||||
@@ -35,187 +27,37 @@ FX_API fx_type_id fx_array_iterator_get_type(void);
|
||||
|
||||
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_array, FX_TYPE_ARRAY);
|
||||
|
||||
/**
|
||||
* Creates an fx_array initialised with the contents of the provided
|
||||
* fx_object pointer array. The fx_array will take a reference to each
|
||||
* object specified in `values`, and will increment the reference count.
|
||||
* The order of objects in the new fx_array will be the same as the order
|
||||
* of objects in `values`. Any NULL pointers in the `values` array will
|
||||
* be ignored, and will not result in gaps in the created fx_array.
|
||||
* However, `nr_values` should be large enough to cover the final
|
||||
* non-NULL pointer in `values`, including any NULL pointers in-between.
|
||||
*
|
||||
* @param values The list of object pointers which should make up the
|
||||
* contents of the new fx_array.
|
||||
* @param nr_values The size of the `values` array.
|
||||
* @return A pointer to the new fx_array, or NULL if an error occurred.
|
||||
*/
|
||||
FX_API fx_array *fx_array_create_with_values(
|
||||
fx_object *const *values,
|
||||
const fx_value *values,
|
||||
size_t nr_values);
|
||||
|
||||
/**
|
||||
* Remove all object references from an fx_array, resetting the size of the
|
||||
* array to zero. The reference counts of all objects in the array will be
|
||||
* decremented.
|
||||
*
|
||||
* @param array The fx_array to clear.
|
||||
*/
|
||||
FX_API void fx_array_clear(fx_array *array);
|
||||
|
||||
/**
|
||||
* Inserts an object at the end of an fx_array. The reference count of
|
||||
* the object will be incremented.
|
||||
*
|
||||
* @param array The fx_array to append the object to.
|
||||
* @param value The object to append.
|
||||
* @return FX_SUCCESS if the object was appended successfully, or an
|
||||
* error code if an error occurred.
|
||||
*/
|
||||
FX_API fx_status fx_array_append(fx_array *array, fx_object *value);
|
||||
FX_API fx_status fx_array_push_back(fx_array *array, fx_value value);
|
||||
|
||||
/**
|
||||
* Inserts an object at the beginning of an fx_array. The reference count
|
||||
* of the object will be incremented. All other objects in the array
|
||||
* will be moved to make space for the object being pre-pended.
|
||||
*
|
||||
* @param array The fx_array to prepend the object to.
|
||||
* @param value The object to prepend.
|
||||
* @return FX_SUCCESS if the object was prepended successfully, or an
|
||||
* error code if an error occurred.
|
||||
*/
|
||||
FX_API fx_status fx_array_prepend(fx_array *array, fx_object *value);
|
||||
FX_API fx_status fx_array_push_front(fx_array *array, fx_value value);
|
||||
|
||||
/**
|
||||
* Inserts an object into an fx_array at a given index. The reference
|
||||
* count of the object will be incremented. If the specified index is at
|
||||
* the beginning or mid-way through the array (i.e. not at the end),
|
||||
* some or all of the objects already in the array will be moved to make
|
||||
* space for the object being inserted.
|
||||
*
|
||||
* @param array The fx_array to insert the object into.
|
||||
* @param value The object to insert.
|
||||
* @param at The index to insert the object at. If the index is
|
||||
* `FX_NPOS`, the object will be inserted at the end of the fx_array.
|
||||
* @return FX_SUCCESS if the object was inserted, or a status code
|
||||
* describing any error that occurred.
|
||||
*/
|
||||
FX_API fx_status fx_array_insert(fx_array *array, fx_object *value, size_t at);
|
||||
FX_API fx_status fx_array_insert(fx_array *array, fx_value value, size_t at);
|
||||
|
||||
/**
|
||||
* Removes the object at the specified index from an fx_array. The
|
||||
* reference count of the removed object will be decremented. If the
|
||||
* specified index is at the beginning or mid-way through the array
|
||||
* (i.e. not at the end), the remaining objects will be moved to fill
|
||||
* the empty space created by the object's removal.
|
||||
*
|
||||
* @param array The fx_array to remove the object from.
|
||||
* @param at The index of the object to be removed.
|
||||
* @return FX_SUCCESS if the object was removed, or a status code
|
||||
* describing any error that occurred.
|
||||
*/
|
||||
FX_API fx_status fx_array_remove(fx_array *array, size_t at);
|
||||
|
||||
/**
|
||||
* Removes the object at the beginning of an fx_array. The reference count
|
||||
* of the removed object will be decremented. The remaining objects will be
|
||||
* moved to fill the empty space created by the object's removal.
|
||||
*
|
||||
* @param array The fx_array to remove the object from.
|
||||
* @return FX_SUCCESS if the object was removed, or a status code describing any
|
||||
* error that occurred.
|
||||
*/
|
||||
FX_API fx_status fx_array_remove_front(fx_array *array);
|
||||
|
||||
/**
|
||||
* Removes the object at the end of an fx_array. The reference count
|
||||
* of the removed object will be decremented.
|
||||
*
|
||||
* @param array The fx_array to remove the object from.
|
||||
* @return FX_SUCCESS if the object was removed, or a status code describing any
|
||||
* error that occurred.
|
||||
*/
|
||||
FX_API fx_status fx_array_remove_back(fx_array *array);
|
||||
|
||||
/**
|
||||
* Removes the object at the specified index of an fx_array, and returns
|
||||
* a pointer to it. The reference count of the removed object will NOT
|
||||
* be decremented. The caller becomes the owner of the array's reference
|
||||
* to the object. If the specified index is at the beginning or mid-way
|
||||
* through the array (i.e. not at the end), the remaining objects will
|
||||
* be moved to fill the empty space created by the object's removal.
|
||||
*
|
||||
* @param array The fx_array to remove the object from.
|
||||
* @param at The index of the object to be removed.
|
||||
* @return An pointer to the removed object. This pointer is owned by
|
||||
* the caller. Returns NULL if an error occurred.
|
||||
*/
|
||||
FX_API fx_object *fx_array_pop(fx_array *array, size_t at);
|
||||
FX_API fx_value fx_array_pop(fx_array *array, size_t at);
|
||||
|
||||
/**
|
||||
* Removes the object at the beginning of an fx_array, and returns a
|
||||
* pointer to it. The reference count of the removed object will NOT be
|
||||
* decremented. The caller becomes the owner of the array's reference to
|
||||
* the object. The remaining objects in the fx_array will be moved to
|
||||
* fill the empty space left by the removed object.
|
||||
*
|
||||
* @param array The fx_array to remove the object from.
|
||||
* @return An pointer to the removed object. This pointer is owned by
|
||||
* the caller. Returns NULL if an error occurred.
|
||||
*/
|
||||
FX_API fx_object *fx_array_pop_front(fx_array *array);
|
||||
FX_API fx_value fx_array_pop_front(fx_array *array);
|
||||
|
||||
/**
|
||||
* Removes the object at the end of an fx_array, and returns a pointer to it.
|
||||
* The reference count of the removed object will NOT be decremented. The caller
|
||||
* becomes the owner of the array's reference to the object.
|
||||
*
|
||||
* @param array The fx_array to remove the object from.
|
||||
* @return An pointer to the removed object. This pointer is owned by the
|
||||
* caller. Returns NULL if an error occurred.
|
||||
*/
|
||||
FX_API fx_object *fx_array_pop_back(fx_array *array);
|
||||
FX_API fx_value fx_array_pop_back(fx_array *array);
|
||||
|
||||
/**
|
||||
* Returns an unowned pointer to the object at the given index of an fx_array.
|
||||
* The caller does not own the returned pointer, and MUST NOT release it.
|
||||
*
|
||||
* @param array The fx_array.
|
||||
* @param at The index of the object to return.
|
||||
* @return A pointer to the object at the given index. This pointer is NOT owned
|
||||
* by the caller. Returns NULL if an error occurred.
|
||||
*/
|
||||
FX_API fx_object *fx_array_at(const fx_array *array, size_t at);
|
||||
FX_API fx_value *fx_array_get_ref(const fx_array *array, size_t at);
|
||||
|
||||
/**
|
||||
* Returns an owned pointer to the object at the given index of an
|
||||
* fx_array. The caller owns the returned pointer, and must release it
|
||||
* when they are finished with it.
|
||||
*
|
||||
* @param array The fx_array.
|
||||
* @param at The index of the object to return.
|
||||
* @return A pointer to the object at the given index. This pointer is
|
||||
* owned by the caller. Returns NULL if an error occurred.
|
||||
*/
|
||||
FX_API fx_object *fx_array_get(fx_array *array, size_t at);
|
||||
FX_API fx_value fx_array_get(const fx_array *array, size_t at);
|
||||
|
||||
/**
|
||||
* Returns the number of objects contained in an fx_array.
|
||||
*
|
||||
* @param array The fx_array.
|
||||
* @return The number of objects contained in the fx_array.
|
||||
*/
|
||||
FX_API size_t fx_array_size(const fx_array *array);
|
||||
FX_API size_t fx_array_get_size(const fx_array *array);
|
||||
|
||||
/**
|
||||
* Returns the current maximum capacity of an fx_array. This represents
|
||||
* the number of objects that can be stored in an fx_array before its
|
||||
* internal buffer would need to be re-sized.
|
||||
*
|
||||
* @param array The fx_array.
|
||||
* @return The maximum capacity of the fx_array.
|
||||
*/
|
||||
FX_API size_t fx_array_capacity(const fx_array *array);
|
||||
FX_API size_t fx_array_get_capacity(const fx_array *array);
|
||||
|
||||
FX_DECLS_END;
|
||||
|
||||
|
||||
@@ -1,64 +0,0 @@
|
||||
#ifndef FX_DS_DICT_H_
|
||||
#define FX_DS_DICT_H_
|
||||
|
||||
#include <fx/bst.h>
|
||||
#include <fx/macros.h>
|
||||
#include <fx/misc.h>
|
||||
#include <fx/queue.h>
|
||||
#include <fx/status.h>
|
||||
#include <fx/string.h>
|
||||
|
||||
FX_DECLS_BEGIN;
|
||||
|
||||
#define FX_TYPE_DICT (fx_dict_get_type())
|
||||
#define FX_TYPE_DICT_ITERATOR (fx_dict_iterator_get_type())
|
||||
|
||||
struct fx_dict_p;
|
||||
|
||||
FX_DECLARE_TYPE(fx_dict);
|
||||
FX_DECLARE_TYPE(fx_dict_iterator);
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_dict)
|
||||
FX_TYPE_CLASS_DECLARATION_END(fx_dict)
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_dict_iterator)
|
||||
FX_TYPE_CLASS_DECLARATION_END(fx_dict_iterator)
|
||||
|
||||
#define FX_DICT_ITEM(k, v) {.key = (k), .value = (v)}
|
||||
#define FX_DICT_ITEM_END {.key = NULL, .value = NULL}
|
||||
|
||||
#define fx_dict_foreach(it, dict) \
|
||||
for (int z__fx_unique_name() = fx_dict_iterator_begin(dict, it); \
|
||||
(it)->key != NULL; \
|
||||
fx_dict_iterator_next(it))
|
||||
|
||||
typedef struct fx_dict_item {
|
||||
const fx_string *key;
|
||||
fx_object *value;
|
||||
} fx_dict_item;
|
||||
|
||||
FX_API fx_type_id fx_dict_get_type(void);
|
||||
FX_API fx_type_id fx_dict_iterator_get_type(void);
|
||||
|
||||
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_dict, FX_TYPE_DICT);
|
||||
|
||||
#if 0
|
||||
FX_API fx_dict *fx_dict_create_with_items(const fx_dict_item *items);
|
||||
#endif
|
||||
|
||||
FX_API fx_status fx_dict_put(fx_dict *dict, const char *key, fx_object *value);
|
||||
FX_API fx_status
|
||||
fx_dict_put_sk(fx_dict *dict, const fx_string *key, fx_object *value);
|
||||
FX_API fx_object *fx_dict_at(const fx_dict *dict, const char *key);
|
||||
FX_API fx_object *fx_dict_at_sk(const fx_dict *dict, const fx_string *key);
|
||||
FX_API fx_object *fx_dict_get(fx_dict *dict, const char *key);
|
||||
FX_API fx_object *fx_dict_get_sk(fx_dict *dict, const fx_string *key);
|
||||
|
||||
FX_API bool fx_dict_has_key(const fx_dict *dict, const char *key);
|
||||
FX_API bool fx_dict_has_skey(const fx_dict *dict, const fx_string *key);
|
||||
FX_API size_t fx_dict_get_size(const fx_dict *dict);
|
||||
FX_API bool fx_dict_is_empty(const fx_dict *dict);
|
||||
|
||||
FX_DECLS_END;
|
||||
|
||||
#endif
|
||||
@@ -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 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_DECLS_END;
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
#ifndef FX_COLLECTIONS_HASHTABLE_H_
|
||||
#define FX_COLLECTIONS_HASHTABLE_H_
|
||||
|
||||
#include <fx/bst.h>
|
||||
#include <fx/iterator.h>
|
||||
#include <fx/macros.h>
|
||||
#include <fx/misc.h>
|
||||
#include <fx/queue.h>
|
||||
#include <fx/status.h>
|
||||
#include <fx/value.h>
|
||||
#include <stddef.h>
|
||||
|
||||
FX_DECLS_BEGIN;
|
||||
|
||||
#define FX_TYPE_HASHTABLE (fx_hashtable_get_type())
|
||||
#define FX_TYPE_HASHTABLE_ITERATOR (fx_hashtable_iterator_get_type())
|
||||
#define FX_TYPE_HASHTABLE_ITEM (fx_hashtable_item_get_type())
|
||||
|
||||
FX_DECLARE_TYPE(fx_hashtable);
|
||||
FX_DECLARE_TYPE(fx_hashtable_iterator);
|
||||
FX_DECLARE_TYPE(fx_hashtable_item);
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_hashtable)
|
||||
FX_TYPE_CLASS_DECLARATION_END(fx_hashtable)
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_hashtable_iterator)
|
||||
FX_TYPE_CLASS_DECLARATION_END(fx_hashtable_iterator)
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_hashtable_item)
|
||||
FX_TYPE_CLASS_DECLARATION_END(fx_hashtable_item)
|
||||
|
||||
FX_API fx_type_id fx_hashtable_get_type(void);
|
||||
FX_API fx_type_id fx_hashtable_iterator_get_type(void);
|
||||
FX_API fx_type_id fx_hashtable_item_get_type(void);
|
||||
|
||||
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_hashtable, FX_TYPE_HASHTABLE);
|
||||
FX_API fx_hashtable *fx_hashtable_create(void);
|
||||
|
||||
FX_API fx_status
|
||||
fx_hashtable_put(fx_hashtable *hashtable, fx_value *key, fx_value *value);
|
||||
FX_API const fx_value *fx_hashtable_get(
|
||||
const fx_hashtable *hashtable,
|
||||
const fx_value *key);
|
||||
|
||||
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 const fx_iterator *fx_hashtable_begin(const fx_hashtable *hashtable);
|
||||
|
||||
FX_API const fx_value *fx_hashtable_item_get_key(const fx_hashtable_item *item);
|
||||
FX_API const fx_value *fx_hashtable_item_get_value(
|
||||
const fx_hashtable_item *item);
|
||||
|
||||
FX_DECLS_END;
|
||||
|
||||
#endif
|
||||
@@ -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 fx_iterator *fx_list_begin(fx_list *q);
|
||||
FX_API const fx_iterator *fx_list_cbegin(const fx_list *q);
|
||||
FX_API const fx_iterator *fx_list_begin(const fx_list *q);
|
||||
|
||||
FX_DECLS_END;
|
||||
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
#ifndef FX_DS_TREE_H_
|
||||
#define FX_DS_TREE_H_
|
||||
|
||||
#include <fx/macros.h>
|
||||
#include <fx/misc.h>
|
||||
#include <fx/queue.h>
|
||||
#include <fx/string.h>
|
||||
|
||||
FX_DECLS_BEGIN;
|
||||
|
||||
#define FX_TYPE_TREE (fx_tree_get_type())
|
||||
#define FX_TYPE_TREE_ITERATOR (fx_tree_iterator_get_type())
|
||||
|
||||
FX_DECLARE_TYPE(fx_tree);
|
||||
FX_DECLARE_TYPE(fx_tree_iterator);
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_tree)
|
||||
FX_TYPE_CLASS_DECLARATION_END(fx_tree)
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_tree_iterator)
|
||||
FX_TYPE_CLASS_DECLARATION_END(fx_tree_iterator)
|
||||
|
||||
#define FX_TREE_NODE_INIT ((fx_tree_node) {0})
|
||||
|
||||
#define FX_TREE_CONTAINER(t, m, v) \
|
||||
((void *)((v) ? (uintptr_t)(v) - (offsetof(t, m)) : 0))
|
||||
|
||||
typedef struct fx_tree_node {
|
||||
struct fx_tree_node *__p01, *__p02, *__p03;
|
||||
struct fx_queue_entry __q01;
|
||||
} fx_tree_node;
|
||||
|
||||
FX_API fx_type_id fx_tree_get_type(void);
|
||||
FX_API fx_type_id fx_tree_iterator_get_type(void);
|
||||
|
||||
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_tree, FX_TYPE_TREE);
|
||||
|
||||
FX_API void fx_tree_set_root(fx_tree *tree, struct fx_tree_node *node);
|
||||
|
||||
FX_API void fx_tree_node_add_child(fx_tree_node *parent, fx_tree_node *child);
|
||||
FX_API void fx_tree_node_add_sibling(fx_tree_node *node, fx_tree_node *to_add);
|
||||
|
||||
FX_API fx_tree_node *fx_tree_node_get_child(fx_tree_node *node, size_t at);
|
||||
FX_API fx_tree_node *fx_tree_node_get_parent(fx_tree_node *node);
|
||||
|
||||
FX_API fx_iterator *fx_tree_begin(fx_tree *tree);
|
||||
FX_API const fx_iterator *fx_tree_cbegin(const fx_tree *tree);
|
||||
|
||||
FX_API fx_iterator *fx_tree_node_begin(fx_tree_node *node);
|
||||
FX_API const fx_iterator *fx_tree_node_cbegin(const fx_tree_node *node);
|
||||
|
||||
FX_API fx_iterator *fx_tree_node_begin_recursive(fx_tree_node *node);
|
||||
FX_API const fx_iterator *fx_tree_node_cbegin_recursive(
|
||||
const fx_tree_node *node);
|
||||
|
||||
FX_DECLS_END;
|
||||
|
||||
#endif
|
||||
+49
-56
@@ -23,6 +23,7 @@ struct fx_list_iterator_p {
|
||||
|
||||
size_t i;
|
||||
void *item;
|
||||
fx_value item_value;
|
||||
fx_list_entry *entry;
|
||||
};
|
||||
|
||||
@@ -41,8 +42,10 @@ static void *list_first_item(const struct fx_list_p *q)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_list_entry *list_entry
|
||||
= fx_unbox(struct fx_list_entry, entry, e_entry);
|
||||
struct fx_list_entry *list_entry = fx_unbox(
|
||||
struct fx_list_entry,
|
||||
entry,
|
||||
e_entry);
|
||||
|
||||
return list_entry->e_data;
|
||||
}
|
||||
@@ -55,8 +58,10 @@ static void *list_last_item(const struct fx_list_p *q)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_list_entry *list_entry
|
||||
= fx_unbox(struct fx_list_entry, entry, e_entry);
|
||||
struct fx_list_entry *list_entry = fx_unbox(
|
||||
struct fx_list_entry,
|
||||
entry,
|
||||
e_entry);
|
||||
|
||||
return list_entry->e_data;
|
||||
}
|
||||
@@ -69,8 +74,10 @@ static struct fx_list_entry *list_first_entry(const struct fx_list_p *q)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_list_entry *list_entry
|
||||
= fx_unbox(struct fx_list_entry, entry, e_entry);
|
||||
struct fx_list_entry *list_entry = fx_unbox(
|
||||
struct fx_list_entry,
|
||||
entry,
|
||||
e_entry);
|
||||
|
||||
return list_entry;
|
||||
}
|
||||
@@ -83,8 +90,10 @@ static struct fx_list_entry *list_last_entry(const struct fx_list_p *q)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_list_entry *list_entry
|
||||
= fx_unbox(struct fx_list_entry, entry, e_entry);
|
||||
struct fx_list_entry *list_entry = fx_unbox(
|
||||
struct fx_list_entry,
|
||||
entry,
|
||||
e_entry);
|
||||
|
||||
return list_entry;
|
||||
}
|
||||
@@ -169,8 +178,10 @@ static void *list_pop_front(struct fx_list_p *q)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_list_entry *list_entry
|
||||
= fx_unbox(struct fx_list_entry, entry, e_entry);
|
||||
struct fx_list_entry *list_entry = fx_unbox(
|
||||
struct fx_list_entry,
|
||||
entry,
|
||||
e_entry);
|
||||
|
||||
void *item = list_entry->e_data;
|
||||
free(list_entry);
|
||||
@@ -187,8 +198,10 @@ static void *list_pop_back(struct fx_list_p *q)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_list_entry *list_entry
|
||||
= fx_unbox(struct fx_list_entry, entry, e_entry);
|
||||
struct fx_list_entry *list_entry = fx_unbox(
|
||||
struct fx_list_entry,
|
||||
entry,
|
||||
e_entry);
|
||||
|
||||
void *item = list_entry->e_data;
|
||||
free(list_entry);
|
||||
@@ -202,8 +215,10 @@ static struct fx_list_entry *find_item(struct fx_list_p *list, void *item)
|
||||
{
|
||||
struct fx_queue_entry *entry = fx_queue_first(&list->l_queue);
|
||||
while (entry) {
|
||||
struct fx_list_entry *list_entry
|
||||
= fx_unbox(struct fx_list_entry, entry, e_entry);
|
||||
struct fx_list_entry *list_entry = fx_unbox(
|
||||
struct fx_list_entry,
|
||||
entry,
|
||||
e_entry);
|
||||
|
||||
if (list_entry->e_data == item) {
|
||||
return list_entry;
|
||||
@@ -246,8 +261,10 @@ static void list_delete_all(struct fx_list_p *q)
|
||||
{
|
||||
struct fx_queue_entry *entry = fx_queue_first(&q->l_queue);
|
||||
while (entry) {
|
||||
struct fx_list_entry *list_entry
|
||||
= fx_unbox(struct fx_list_entry, entry, e_entry);
|
||||
struct fx_list_entry *list_entry = fx_unbox(
|
||||
struct fx_list_entry,
|
||||
entry,
|
||||
e_entry);
|
||||
struct fx_queue_entry *next = fx_queue_next(entry);
|
||||
|
||||
free(list_entry);
|
||||
@@ -385,30 +402,12 @@ void *fx_list_entry_value(const struct fx_list_entry *entry)
|
||||
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);
|
||||
struct fx_list_iterator_p *it
|
||||
= fx_object_get_private(it_obj, FX_TYPE_LIST_ITERATOR);
|
||||
struct fx_list_iterator_p *it = fx_object_get_private(
|
||||
it_obj,
|
||||
FX_TYPE_LIST_ITERATOR);
|
||||
|
||||
it->_q = (fx_list *)q;
|
||||
it->_q_p = fx_object_get_private(q, FX_TYPE_LIST);
|
||||
@@ -440,8 +439,9 @@ static void list_fini(fx_object *obj, void *priv)
|
||||
|
||||
static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_list_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_LIST_ITERATOR);
|
||||
struct fx_list_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_TYPE_LIST_ITERATOR);
|
||||
|
||||
if (!it->_q_entry) {
|
||||
it->entry = NULL;
|
||||
@@ -467,8 +467,9 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||
|
||||
static enum fx_status iterator_erase(fx_iterator *obj)
|
||||
{
|
||||
struct fx_list_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_LIST_ITERATOR);
|
||||
struct fx_list_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_TYPE_LIST_ITERATOR);
|
||||
|
||||
if (!it->entry || !it->_q_entry) {
|
||||
return FX_ERR_OUT_OF_BOUNDS;
|
||||
@@ -488,20 +489,14 @@ static enum fx_status iterator_erase(fx_iterator *obj)
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_iterator_value iterator_get_value(fx_iterator *obj)
|
||||
static const fx_value *iterator_get_value(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_list_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_LIST_ITERATOR);
|
||||
struct fx_list_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_TYPE_LIST_ITERATOR);
|
||||
|
||||
return FX_ITERATOR_VALUE_PTR(it->item);
|
||||
}
|
||||
|
||||
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_list_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_LIST_ITERATOR);
|
||||
|
||||
return FX_ITERATOR_VALUE_CPTR(it->item);
|
||||
it->item_value = FX_POINTER(it->item);
|
||||
return &it->item_value;
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
@@ -514,7 +509,6 @@ FX_TYPE_CLASS_BEGIN(fx_list)
|
||||
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
|
||||
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_CLASS_END(fx_list)
|
||||
|
||||
@@ -537,7 +531,6 @@ FX_TYPE_CLASS_BEGIN(fx_list_iterator)
|
||||
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
|
||||
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
|
||||
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
|
||||
FX_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
|
||||
FX_TYPE_CLASS_END(fx_list_iterator)
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
int main(void)
|
||||
{
|
||||
#if 0
|
||||
fx_array *array = fx_array_create();
|
||||
fx_array_append(array, fx_int_create(32));
|
||||
fx_array_append(array, fx_int_create(64));
|
||||
@@ -17,5 +18,6 @@ int main(void)
|
||||
fx_iterator_unref(it);
|
||||
|
||||
fx_array_unref(array);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
#include <fx/collections/hashtable.h>
|
||||
#include <fx/int.h>
|
||||
#include <fx/reflection/type.h>
|
||||
#include <fx/stream.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
fx_hashtable *ht = fx_hashtable_create();
|
||||
fx_hashtable_put(ht, &FX_INT(32), &FX_INT(64));
|
||||
fx_hashtable_put(ht, &FX_CSTR("hello"), &FX_INT(128));
|
||||
|
||||
const fx_value *v = fx_hashtable_get(ht, &FX_INT(32));
|
||||
if (v) {
|
||||
printf("32= ");
|
||||
fx_value_to_string(v, fx_stdout, NULL);
|
||||
printf("\n");
|
||||
} else {
|
||||
printf("no value\n");
|
||||
}
|
||||
|
||||
const fx_iterator *it = fx_hashtable_begin(ht);
|
||||
fx_foreach(val, it)
|
||||
{
|
||||
fx_hashtable_item *item = NULL;
|
||||
fx_value_get_object(val, &item);
|
||||
printf("item %p\n", item);
|
||||
const fx_value *key, *value;
|
||||
key = fx_hashtable_item_get_key(item);
|
||||
value = fx_hashtable_item_get_value(item);
|
||||
|
||||
fx_iterator *prop_it = fx_type_get_properties(
|
||||
fx_type_get_by_id(FX_TYPE_HASHTABLE_ITEM));
|
||||
fx_foreach(prop_val, prop_it)
|
||||
{
|
||||
fx_property *prop;
|
||||
fx_value_get_object(prop_val, &prop);
|
||||
printf("%s = ", fx_property_get_name(prop));
|
||||
fx_value value = FX_VALUE_EMPTY;
|
||||
fx_property_get_value(prop, val, &value);
|
||||
fx_value_to_string(&value, fx_stdout, NULL);
|
||||
printf("\n");
|
||||
}
|
||||
fx_iterator_unref(prop_it);
|
||||
}
|
||||
fx_iterator_unref(it);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,12 +1,7 @@
|
||||
#include <fx/double.h>
|
||||
#include <fx/float.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
fx_double *d = fx_double_create(6.8);
|
||||
|
||||
printf("double=%lf\n", fx_double_get_value(d));
|
||||
fx_double_unref(d);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
|
||||
int main(void)
|
||||
{
|
||||
fx_string *string = FX_CSTR("Hello, world!");
|
||||
fx_string *string = fx_string_create_from_cstr("Hello, world!");
|
||||
printf("string object = ");
|
||||
fx_object_to_string(string, fx_stdout);
|
||||
fx_object_to_string(string, fx_stdout, NULL);
|
||||
printf("\n");
|
||||
fx_string_unref(string);
|
||||
return 0;
|
||||
|
||||
@@ -11,7 +11,7 @@ int main(int argc, const char **argv)
|
||||
fx_stream_read_all_bytes_s(fx_stdin, dest_stream, buf, &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_stream_buffer_unref(buf);
|
||||
|
||||
@@ -1,128 +0,0 @@
|
||||
#include <fx/bst.h>
|
||||
#include <fx/collections/dict.h>
|
||||
#include <fx/collections/tree.h>
|
||||
#include <fx/int.h>
|
||||
#include <fx/iterator.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define NITEMS 16
|
||||
|
||||
struct tree_item {
|
||||
int value;
|
||||
fx_tree_node node;
|
||||
};
|
||||
|
||||
struct bst_item {
|
||||
int value;
|
||||
fx_bst_node node;
|
||||
};
|
||||
|
||||
FX_BST_DEFINE_SIMPLE_GET(struct bst_item, int, node, value, get_node)
|
||||
FX_BST_DEFINE_SIMPLE_INSERT(struct bst_item, node, value, put_node)
|
||||
|
||||
int main(void)
|
||||
{
|
||||
fx_dict *dict = fx_dict_create();
|
||||
fx_dict_put(dict, "hello", fx_int_create(32));
|
||||
fx_dict_put(dict, "world", fx_int_create(64));
|
||||
fx_dict_put(dict, "more", fx_int_create(128));
|
||||
fx_dict_put(dict, "other", fx_int_create(256));
|
||||
|
||||
fx_iterator *it = fx_iterator_begin(dict);
|
||||
|
||||
size_t i = 0;
|
||||
fx_foreach(fx_dict_item *, item, it)
|
||||
{
|
||||
printf("item %zu: %s=%" PRIdPTR "\n",
|
||||
i++,
|
||||
fx_string_get_cstr(item->key),
|
||||
fx_int_get_value(item->value));
|
||||
}
|
||||
|
||||
fx_iterator_unref(it);
|
||||
|
||||
fx_tree *tree = fx_tree_create();
|
||||
struct tree_item items2[NITEMS];
|
||||
|
||||
for (int i = 0; i < NITEMS; i++) {
|
||||
items2[i].value = i;
|
||||
items2[i].node = FX_TREE_NODE_INIT;
|
||||
}
|
||||
|
||||
fx_tree_set_root(tree, &items2[0].node);
|
||||
|
||||
fx_tree_node_add_child(&items2[0].node, &items2[1].node);
|
||||
fx_tree_node_add_child(&items2[0].node, &items2[2].node);
|
||||
fx_tree_node_add_child(&items2[0].node, &items2[3].node);
|
||||
fx_tree_node_add_child(&items2[0].node, &items2[7].node);
|
||||
fx_tree_node_add_child(&items2[1].node, &items2[4].node);
|
||||
fx_tree_node_add_child(&items2[1].node, &items2[5].node);
|
||||
fx_tree_node_add_child(&items2[4].node, &items2[6].node);
|
||||
|
||||
#if 0
|
||||
it = fx_iterator_begin(tree);
|
||||
fx_tree_iterator it2;
|
||||
fx_tree_foreach(&it2, tree)
|
||||
{
|
||||
struct tree_item *item = fx_unbox(struct tree_item, it2.node, node);
|
||||
|
||||
for (size_t i = 0; i < it2.depth; i++) {
|
||||
fputs(" ", stdout);
|
||||
}
|
||||
|
||||
printf("%u\n", item->value);
|
||||
}
|
||||
|
||||
fx_bst bst = {0};
|
||||
struct bst_item items3[NITEMS] = {0};
|
||||
for (int i = 0; i < NITEMS; i++) {
|
||||
items3[i].value = i;
|
||||
put_node(&bst, &items3[i]);
|
||||
}
|
||||
|
||||
printf("\n\n");
|
||||
|
||||
fx_bst_iterator it3;
|
||||
fx_bst_foreach (&it3, &bst) {
|
||||
struct bst_item *item
|
||||
= fx_unbox(struct bst_item, it3.node, node);
|
||||
|
||||
for (size_t i = 0; i < it3.depth; i++) {
|
||||
fputs(" ", stdout);
|
||||
}
|
||||
|
||||
printf("%d\n", item->value);
|
||||
}
|
||||
|
||||
fx_bst_iterator_begin(&bst, &it3);
|
||||
while (fx_bst_iterator_is_valid(&it3)) {
|
||||
struct bst_item *item
|
||||
= fx_unbox(struct bst_item, it3.node, node);
|
||||
|
||||
if (item->value == 9) {
|
||||
fx_bst_iterator_erase(&it3);
|
||||
} else {
|
||||
fx_bst_iterator_next(&it3);
|
||||
}
|
||||
}
|
||||
|
||||
printf("\n\n");
|
||||
|
||||
fx_bst_foreach (&it3, &bst) {
|
||||
struct bst_item *item
|
||||
= fx_unbox(struct bst_item, it3.node, node);
|
||||
|
||||
for (size_t i = 0; i < it3.depth; i++) {
|
||||
fputs(" ", stdout);
|
||||
}
|
||||
|
||||
printf("%d\n", item->value);
|
||||
}
|
||||
|
||||
fx_tree_unref(tree);
|
||||
#endif
|
||||
fx_dict_unref(dict);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,390 +0,0 @@
|
||||
#include <fx/collections/tree.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define ITERATOR_RECURSIVE 0x01u
|
||||
|
||||
#define ITERATOR_IS_RECURSIVE(it) (((it)->_f01 & ITERATOR_RECURSIVE) != 0)
|
||||
#define ITERATOR_UNSET_RECURSIVE(it) ((it)->_f01 &= ~ITERATOR_RECURSIVE)
|
||||
#define ITERATOR_SET_RECURSIVE(it) ((it)->_f01 |= ITERATOR_RECURSIVE)
|
||||
|
||||
#define NODE_PARENT(n) ((n)->__p01)
|
||||
#define NODE_FIRST_CHILD(n) ((n)->__p02)
|
||||
#define NODE_NEXT_SIBLING(n) ((n)->__p03)
|
||||
|
||||
/*** PRIVATE DATA *************************************************************/
|
||||
|
||||
struct fx_tree_p {
|
||||
struct fx_tree_node *t_root;
|
||||
};
|
||||
|
||||
struct fx_tree_iterator_p {
|
||||
size_t i, depth;
|
||||
fx_tree_node *node;
|
||||
|
||||
unsigned char _f01;
|
||||
};
|
||||
|
||||
/*** PRIVATE FUNCTIONS ********************************************************/
|
||||
|
||||
static void tree_set_root(struct fx_tree_p *tree, struct fx_tree_node *node)
|
||||
{
|
||||
tree->t_root = node;
|
||||
}
|
||||
|
||||
static const struct fx_tree_node *next_node(
|
||||
const struct fx_tree_node *node,
|
||||
bool recursive,
|
||||
int *depth_diff)
|
||||
{
|
||||
if (!node) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!recursive) {
|
||||
node = NODE_NEXT_SIBLING(node);
|
||||
return node;
|
||||
}
|
||||
|
||||
int d = 0;
|
||||
struct fx_tree_node *next = NODE_FIRST_CHILD(node);
|
||||
if (next) {
|
||||
d = 1;
|
||||
*depth_diff = d;
|
||||
return next;
|
||||
}
|
||||
|
||||
const struct fx_tree_node *n = node;
|
||||
next = NODE_NEXT_SIBLING(n);
|
||||
while (!next) {
|
||||
n = NODE_PARENT(n);
|
||||
if (!n) {
|
||||
break;
|
||||
}
|
||||
|
||||
d--;
|
||||
next = NODE_NEXT_SIBLING(n);
|
||||
}
|
||||
|
||||
*depth_diff = d;
|
||||
return next;
|
||||
}
|
||||
|
||||
static void remove_node(struct fx_tree_node *node)
|
||||
{
|
||||
struct fx_tree_node *parent = NODE_PARENT(node);
|
||||
if (!parent) {
|
||||
return;
|
||||
}
|
||||
|
||||
struct fx_tree_node *n0 = NULL, *n1 = NULL;
|
||||
n0 = NODE_FIRST_CHILD(parent);
|
||||
|
||||
while (n0) {
|
||||
if (n0 == node) {
|
||||
break;
|
||||
}
|
||||
|
||||
n1 = n0;
|
||||
n0 = NODE_NEXT_SIBLING(n0);
|
||||
}
|
||||
|
||||
if (!n0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (n1) {
|
||||
NODE_NEXT_SIBLING(n1) = NODE_NEXT_SIBLING(n0);
|
||||
} else {
|
||||
NODE_FIRST_CHILD(parent) = NODE_NEXT_SIBLING(n0);
|
||||
}
|
||||
|
||||
NODE_PARENT(n0) = NODE_NEXT_SIBLING(n0) = NULL;
|
||||
}
|
||||
|
||||
static void reparent_children(
|
||||
struct fx_tree_node *old_parent,
|
||||
struct fx_tree_node *new_parent)
|
||||
{
|
||||
struct fx_tree_node *last = NODE_FIRST_CHILD(new_parent);
|
||||
while (last && NODE_NEXT_SIBLING(last)) {
|
||||
last = NODE_NEXT_SIBLING(last);
|
||||
}
|
||||
|
||||
struct fx_tree_node *cur = NODE_FIRST_CHILD(old_parent);
|
||||
while (cur) {
|
||||
struct fx_tree_node *next = NODE_NEXT_SIBLING(cur);
|
||||
NODE_PARENT(cur) = new_parent;
|
||||
NODE_NEXT_SIBLING(cur) = NULL;
|
||||
|
||||
if (last) {
|
||||
NODE_NEXT_SIBLING(last) = cur;
|
||||
} else {
|
||||
NODE_FIRST_CHILD(new_parent) = cur;
|
||||
}
|
||||
|
||||
last = cur;
|
||||
cur = next;
|
||||
}
|
||||
}
|
||||
|
||||
/*** PUBLIC FUNCTIONS *********************************************************/
|
||||
|
||||
void fx_tree_set_root(fx_tree *tree, struct fx_tree_node *node)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_TREE, tree_set_root, tree, node);
|
||||
}
|
||||
|
||||
void fx_tree_node_add_child(
|
||||
struct fx_tree_node *parent,
|
||||
struct fx_tree_node *child)
|
||||
{
|
||||
if (NODE_PARENT(child)) {
|
||||
return;
|
||||
}
|
||||
|
||||
NODE_PARENT(child) = parent;
|
||||
if (!NODE_FIRST_CHILD(parent)) {
|
||||
NODE_FIRST_CHILD(parent) = child;
|
||||
return;
|
||||
}
|
||||
|
||||
struct fx_tree_node *cur = NODE_FIRST_CHILD(parent);
|
||||
while (NODE_NEXT_SIBLING(cur)) {
|
||||
cur = NODE_NEXT_SIBLING(cur);
|
||||
}
|
||||
|
||||
NODE_NEXT_SIBLING(cur) = child;
|
||||
}
|
||||
|
||||
void fx_tree_node_add_sibling(
|
||||
struct fx_tree_node *node,
|
||||
struct fx_tree_node *to_add)
|
||||
{
|
||||
if (NODE_PARENT(to_add) || !NODE_PARENT(node)) {
|
||||
return;
|
||||
}
|
||||
|
||||
fx_tree_node_add_child(NODE_PARENT(node), to_add);
|
||||
}
|
||||
|
||||
struct fx_tree_node *fx_tree_node_get_child(
|
||||
struct fx_tree_node *node,
|
||||
size_t at)
|
||||
{
|
||||
size_t i = 0;
|
||||
struct fx_tree_node *cur = NODE_FIRST_CHILD(node);
|
||||
|
||||
while (i < at) {
|
||||
if (!cur) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
cur = NODE_NEXT_SIBLING(cur);
|
||||
i++;
|
||||
}
|
||||
|
||||
return cur;
|
||||
}
|
||||
|
||||
fx_iterator *fx_tree_begin(fx_tree *tree)
|
||||
{
|
||||
struct fx_tree_p *p = fx_object_get_private(tree, FX_TYPE_TREE);
|
||||
return fx_tree_node_begin(p->t_root);
|
||||
}
|
||||
|
||||
const fx_iterator *fx_tree_cbegin(const fx_tree *tree)
|
||||
{
|
||||
struct fx_tree_p *p = fx_object_get_private(tree, FX_TYPE_TREE);
|
||||
return fx_tree_node_begin(p->t_root);
|
||||
}
|
||||
|
||||
fx_iterator *fx_tree_node_begin(struct fx_tree_node *node)
|
||||
{
|
||||
fx_tree_iterator *it_obj = fx_object_create(FX_TYPE_TREE_ITERATOR);
|
||||
struct fx_tree_iterator_p *it
|
||||
= fx_object_get_private(it_obj, FX_TYPE_TREE_ITERATOR);
|
||||
|
||||
it->node = NODE_FIRST_CHILD(node);
|
||||
it->i = 0;
|
||||
it->depth = 0;
|
||||
|
||||
ITERATOR_UNSET_RECURSIVE(it);
|
||||
|
||||
if (!it->node) {
|
||||
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
||||
}
|
||||
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
const fx_iterator *fx_tree_node_cbegin(const struct fx_tree_node *node)
|
||||
{
|
||||
return fx_tree_node_begin((struct fx_tree_node *)node);
|
||||
}
|
||||
|
||||
fx_iterator *fx_tree_node_begin_recursive(struct fx_tree_node *node)
|
||||
{
|
||||
fx_tree_iterator *it_obj = fx_object_create(FX_TYPE_TREE_ITERATOR);
|
||||
struct fx_tree_iterator_p *it
|
||||
= fx_object_get_private(it_obj, FX_TYPE_TREE_ITERATOR);
|
||||
|
||||
it->node = node;
|
||||
it->i = 0;
|
||||
it->depth = 0;
|
||||
|
||||
ITERATOR_SET_RECURSIVE(it);
|
||||
|
||||
if (!it->node) {
|
||||
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
||||
}
|
||||
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
const fx_iterator *fx_tree_node_cbegin_recursive(
|
||||
const struct fx_tree_node *node)
|
||||
{
|
||||
return fx_tree_node_begin_recursive((struct fx_tree_node *)node);
|
||||
}
|
||||
|
||||
/*** VIRTUAL FUNCTIONS ********************************************************/
|
||||
|
||||
static void tree_init(fx_object *obj, void *priv)
|
||||
{
|
||||
struct fx_tree_p *tree = priv;
|
||||
}
|
||||
|
||||
static void tree_fini(fx_object *obj, void *priv)
|
||||
{
|
||||
struct fx_tree_p *tree = priv;
|
||||
}
|
||||
|
||||
/*** ITERATOR FUNCTIONS *******************************************************/
|
||||
|
||||
static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_tree_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_TREE_ITERATOR);
|
||||
|
||||
int depth_diff = 0;
|
||||
const struct fx_tree_node *next
|
||||
= next_node(it->node, ITERATOR_IS_RECURSIVE(it), &depth_diff);
|
||||
|
||||
if (next) {
|
||||
it->depth += depth_diff;
|
||||
it->i++;
|
||||
} else {
|
||||
it->depth = 0;
|
||||
it->i = 0;
|
||||
}
|
||||
|
||||
it->node = (struct fx_tree_node *)next;
|
||||
return (it->node != NULL) ? FX_SUCCESS : FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
static enum fx_status iterator_erase(fx_iterator *obj)
|
||||
{
|
||||
struct fx_tree_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_TREE_ITERATOR);
|
||||
|
||||
if (!it->node) {
|
||||
return FX_ERR_OUT_OF_BOUNDS;
|
||||
}
|
||||
|
||||
struct fx_tree_node *parent = NODE_PARENT(it->node);
|
||||
if (!parent) {
|
||||
return FX_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
int d = 0;
|
||||
struct fx_tree_node *n = it->node;
|
||||
struct fx_tree_node *next = NODE_NEXT_SIBLING(n);
|
||||
|
||||
if (!next) {
|
||||
next = NODE_FIRST_CHILD(n);
|
||||
}
|
||||
|
||||
while (!next) {
|
||||
n = NODE_PARENT(n);
|
||||
if (!n) {
|
||||
break;
|
||||
}
|
||||
|
||||
d--;
|
||||
next = NODE_NEXT_SIBLING(n);
|
||||
}
|
||||
|
||||
remove_node(it->node);
|
||||
reparent_children(it->node, parent);
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_iterator_value iterator_get_value(fx_iterator *obj)
|
||||
{
|
||||
struct fx_tree_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_TREE_ITERATOR);
|
||||
|
||||
if (!it->node) {
|
||||
return FX_ITERATOR_VALUE_NULL;
|
||||
}
|
||||
|
||||
return FX_ITERATOR_VALUE_PTR(it->node);
|
||||
}
|
||||
|
||||
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_tree_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_TREE_ITERATOR);
|
||||
|
||||
if (!it->node) {
|
||||
return FX_ITERATOR_VALUE_NULL;
|
||||
}
|
||||
|
||||
return FX_ITERATOR_VALUE_CPTR(it->node);
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
// ---- fx_tree DEFINITION
|
||||
FX_TYPE_CLASS_BEGIN(fx_tree)
|
||||
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) = fx_tree_begin;
|
||||
FX_INTERFACE_ENTRY(it_cbegin) = fx_tree_cbegin;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
|
||||
FX_TYPE_CLASS_END(fx_tree)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_tree)
|
||||
FX_TYPE_ID(0x8d8fa36b, 0xc515, 0x4803, 0x8124, 0xfd704f01b8ae);
|
||||
FX_TYPE_CLASS(fx_tree_class);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_ITERABLE);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_tree_p);
|
||||
FX_TYPE_INSTANCE_INIT(tree_init);
|
||||
FX_TYPE_INSTANCE_FINI(tree_fini);
|
||||
FX_TYPE_DEFINITION_END(fx_tree)
|
||||
|
||||
// ---- fx_tree_iterator DEFINITION
|
||||
FX_TYPE_CLASS_BEGIN(fx_tree_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_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
|
||||
FX_TYPE_CLASS_END(fx_tree_iterator)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_tree_iterator)
|
||||
FX_TYPE_ID(0xb896e671, 0x84b2, 0x4892, 0xaf09, 0x407f305f4bf8);
|
||||
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
|
||||
FX_TYPE_CLASS(fx_tree_iterator_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_tree_iterator_p);
|
||||
FX_TYPE_DEFINITION_END(fx_tree_iterator)
|
||||
@@ -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
|
||||
@@ -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)
|
||||
@@ -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)
|
||||
@@ -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;
|
||||
}
|
||||
@@ -50,7 +50,7 @@ FX_API fx_type_id fx_directory_iterator_get_type(void);
|
||||
|
||||
FX_API fx_result fx_directory_open(
|
||||
fx_directory *root,
|
||||
const fx_path *path,
|
||||
fx_value path,
|
||||
fx_directory_open_flags flags,
|
||||
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 fx_result fx_directory_delete(fx_directory *dir);
|
||||
|
||||
FX_API bool fx_directory_path_exists(
|
||||
const fx_directory *root,
|
||||
const fx_path *path);
|
||||
FX_API bool fx_directory_path_is_file(
|
||||
const fx_directory *root,
|
||||
const fx_path *path);
|
||||
FX_API bool fx_directory_path_exists(const fx_directory *root, fx_value path);
|
||||
FX_API bool fx_directory_path_is_file(const fx_directory *root, fx_value path);
|
||||
FX_API bool fx_directory_path_is_directory(
|
||||
const fx_directory *root,
|
||||
const fx_path *path);
|
||||
fx_value path);
|
||||
FX_API fx_result fx_directory_path_stat(
|
||||
const fx_directory *root,
|
||||
const fx_path *path,
|
||||
fx_value path,
|
||||
struct fx_file_info *out);
|
||||
FX_API fx_result fx_directory_path_unlink(
|
||||
const fx_directory *root,
|
||||
const fx_path *path);
|
||||
FX_API fx_result
|
||||
fx_directory_path_unlink(const fx_directory *root, fx_value path);
|
||||
|
||||
FX_API fx_iterator *fx_directory_begin(
|
||||
fx_directory *dir,
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <fx/macros.h>
|
||||
#include <fx/misc.h>
|
||||
#include <fx/stream.h>
|
||||
#include <fx/value.h>
|
||||
|
||||
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_TYPE_FWDREF(fx_directory) * root,
|
||||
const FX_TYPE_FWDREF(fx_path) * path,
|
||||
fx_value path,
|
||||
fx_file_mode mode,
|
||||
fx_file **out);
|
||||
FX_API fx_result fx_file_open_temp(fx_file_mode mode, fx_file **out);
|
||||
|
||||
@@ -18,18 +18,25 @@ FX_TYPE_CLASS_DECLARATION_END(fx_path)
|
||||
|
||||
#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;
|
||||
|
||||
FX_API fx_type_id fx_path_get_type(void);
|
||||
|
||||
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_path, FX_TYPE_PATH);
|
||||
|
||||
FX_API fx_path *fx_path_create_root();
|
||||
FX_API fx_path *fx_path_create_cwd();
|
||||
FX_API fx_path *fx_path_create_from_cstr(const char *path);
|
||||
FX_API fx_path *fx_path_get_system(fx_system_path path_type);
|
||||
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_relative(const fx_path *in);
|
||||
@@ -49,7 +56,7 @@ FX_API enum fx_status fx_path_get_filename(
|
||||
const fx_path *path,
|
||||
fx_string *out_name);
|
||||
|
||||
FX_API const char *fx_path_ptr(const fx_path *path);
|
||||
FX_API const char *fx_path_get_cstr(const fx_path *path);
|
||||
FX_API size_t fx_path_length(const fx_path *path);
|
||||
|
||||
FX_DECLS_END;
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
+65
-78
@@ -1,3 +1,5 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
|
||||
#include "misc.h"
|
||||
#include "posix.h"
|
||||
|
||||
@@ -35,6 +37,7 @@ struct fx_directory_iterator_p {
|
||||
fx_directory *root;
|
||||
|
||||
fx_directory_entry entry;
|
||||
fx_value entry_value;
|
||||
};
|
||||
|
||||
/*** 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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
return fx_path_ptr(dir->d_path_rel);
|
||||
return fx_path_get_cstr(dir->d_path_rel);
|
||||
}
|
||||
|
||||
static fx_result directory_delete(
|
||||
@@ -85,14 +88,12 @@ static fx_result directory_delete(
|
||||
|
||||
static bool directory_path_exists(
|
||||
const struct fx_directory_p *root,
|
||||
const fx_path *path)
|
||||
fx_value path)
|
||||
{
|
||||
const fx_path *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) {
|
||||
return false;
|
||||
}
|
||||
@@ -105,14 +106,14 @@ static bool directory_path_exists(
|
||||
|
||||
static bool directory_path_is_file(
|
||||
const struct fx_directory_p *root,
|
||||
const fx_path *path)
|
||||
fx_value path)
|
||||
{
|
||||
const fx_path *parts[] = {
|
||||
root ? root->d_path_abs : NULL,
|
||||
path,
|
||||
};
|
||||
const fx_value *parts[] = {};
|
||||
|
||||
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) {
|
||||
return false;
|
||||
}
|
||||
@@ -125,14 +126,14 @@ static bool directory_path_is_file(
|
||||
|
||||
static bool directory_path_is_directory(
|
||||
const struct fx_directory_p *root,
|
||||
const fx_path *path)
|
||||
fx_value path)
|
||||
{
|
||||
const fx_path *parts[] = {
|
||||
root ? root->d_path_abs : NULL,
|
||||
path,
|
||||
};
|
||||
const fx_value *parts[] = {};
|
||||
|
||||
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) {
|
||||
return false;
|
||||
}
|
||||
@@ -145,15 +146,15 @@ static bool directory_path_is_directory(
|
||||
|
||||
static fx_result directory_path_stat(
|
||||
const struct fx_directory_p *root,
|
||||
const fx_path *path,
|
||||
fx_value path,
|
||||
struct fx_file_info *out)
|
||||
{
|
||||
const fx_path *parts[] = {
|
||||
root ? root->d_path_abs : NULL,
|
||||
path,
|
||||
};
|
||||
const fx_value *parts[] = {};
|
||||
|
||||
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) {
|
||||
return FX_RESULT_ERR(NO_MEMORY);
|
||||
}
|
||||
@@ -166,14 +167,14 @@ static fx_result directory_path_stat(
|
||||
|
||||
static fx_result directory_path_unlink(
|
||||
const struct fx_directory_p *root,
|
||||
const fx_path *path)
|
||||
fx_value path)
|
||||
{
|
||||
const fx_path *parts[] = {
|
||||
root ? root->d_path_abs : NULL,
|
||||
path,
|
||||
};
|
||||
const fx_value *parts[] = {};
|
||||
|
||||
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) {
|
||||
return FX_RESULT_ERR(NO_MEMORY);
|
||||
}
|
||||
@@ -253,7 +254,7 @@ static fx_result create_directory_hierarchy(
|
||||
|
||||
static fx_result directory_open(
|
||||
struct fx_directory_p *root,
|
||||
const fx_path *path,
|
||||
fx_value path,
|
||||
fx_directory_open_flags flags,
|
||||
fx_directory **out)
|
||||
{
|
||||
@@ -261,7 +262,10 @@ static fx_result directory_open(
|
||||
|
||||
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) {
|
||||
while (*path_cstr == '/') {
|
||||
path_cstr++;
|
||||
@@ -295,16 +299,18 @@ static fx_result directory_open(
|
||||
= fx_object_get_private(dir, FX_TYPE_DIRECTORY);
|
||||
|
||||
if (!root) {
|
||||
cwd = fx_path_create_cwd();
|
||||
cwd = fx_path_get_system(FX_PATH_CWD);
|
||||
}
|
||||
|
||||
const fx_path *parts[] = {
|
||||
root ? root->d_path_abs : cwd,
|
||||
path,
|
||||
const fx_value *parts[] = {
|
||||
&FX_VALUE_OBJECT(root ? root->d_path_abs : cwd),
|
||||
&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) {
|
||||
fx_stringstream_unref(path_str);
|
||||
return FX_RESULT_ERR(NO_MEMORY);
|
||||
}
|
||||
|
||||
@@ -313,7 +319,7 @@ static fx_result directory_open(
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
if (flags & FX_DIRECTORY_OPEN_DELETE_ON_CLOSE) {
|
||||
@@ -321,6 +327,7 @@ static fx_result directory_open(
|
||||
}
|
||||
|
||||
*out = dir;
|
||||
fx_stringstream_unref(path_str);
|
||||
|
||||
return FX_RESULT_SUCCESS;
|
||||
}
|
||||
@@ -329,7 +336,7 @@ static fx_result directory_open(
|
||||
|
||||
fx_result fx_directory_open(
|
||||
fx_directory *root,
|
||||
const fx_path *path,
|
||||
fx_value path,
|
||||
fx_directory_open_flags flags,
|
||||
fx_directory **out)
|
||||
{
|
||||
@@ -350,19 +357,17 @@ fx_result fx_directory_open_temp(fx_directory **out)
|
||||
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_directory *dir = NULL;
|
||||
fx_result status = fx_directory_open(
|
||||
FX_DIRECTORY_ROOT,
|
||||
rpath,
|
||||
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) {
|
||||
fx_path_unref(rpath);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -370,9 +375,7 @@ fx_result fx_directory_open_temp(fx_directory **out)
|
||||
p->d_flags |= DIRECTORY_DELETE_ON_CLOSE;
|
||||
}
|
||||
|
||||
fx_path_unlink(rpath);
|
||||
fx_path_unref(rpath);
|
||||
|
||||
*out = dir;
|
||||
return status;
|
||||
}
|
||||
}
|
||||
@@ -416,7 +419,7 @@ fx_result fx_directory_delete(fx_directory *dir)
|
||||
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_TYPE_DIRECTORY,
|
||||
@@ -425,7 +428,7 @@ bool fx_directory_path_exists(const fx_directory *root, const fx_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_TYPE_DIRECTORY,
|
||||
@@ -434,9 +437,7 @@ bool fx_directory_path_is_file(const fx_directory *root, const fx_path *path)
|
||||
path);
|
||||
}
|
||||
|
||||
bool fx_directory_path_is_directory(
|
||||
const fx_directory *root,
|
||||
const fx_path *path)
|
||||
bool fx_directory_path_is_directory(const fx_directory *root, fx_value path)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_TYPE_DIRECTORY,
|
||||
@@ -447,7 +448,7 @@ bool fx_directory_path_is_directory(
|
||||
|
||||
fx_result fx_directory_path_stat(
|
||||
const fx_directory *root,
|
||||
const fx_path *path,
|
||||
fx_value path,
|
||||
struct fx_file_info *out)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
@@ -458,9 +459,7 @@ fx_result fx_directory_path_stat(
|
||||
out);
|
||||
}
|
||||
|
||||
fx_result fx_directory_path_unlink(
|
||||
const fx_directory *root,
|
||||
const fx_path *path)
|
||||
fx_result fx_directory_path_unlink(const fx_directory *root, fx_value path)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_TYPE_DIRECTORY,
|
||||
@@ -508,6 +507,8 @@ static void update_iterator_data(struct fx_directory_iterator_p *it)
|
||||
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;
|
||||
|
||||
@@ -565,7 +566,7 @@ fx_iterator *fx_directory_begin(
|
||||
int fts_flags = FTS_COMFOLLOW | FTS_NOCHDIR;
|
||||
|
||||
const char *path_list[] = {
|
||||
fx_path_ptr(it->_p->d_path_abs),
|
||||
fx_path_get_cstr(it->_p->d_path_abs),
|
||||
NULL,
|
||||
};
|
||||
|
||||
@@ -614,12 +615,7 @@ fx_iterator *fx_directory_begin(
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
static fx_iterator *iterator_begin(fx_object *obj)
|
||||
{
|
||||
return fx_directory_begin(obj, FX_DIRECTORY_ITERATE_PARENT_FIRST);
|
||||
}
|
||||
|
||||
static const fx_iterator *iterator_cbegin(const fx_object *obj)
|
||||
static const fx_iterator *iterator_begin(const fx_object *obj)
|
||||
{
|
||||
return fx_directory_begin(
|
||||
(fx_object *)obj,
|
||||
@@ -679,8 +675,9 @@ 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, it->entry.filepath);
|
||||
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);
|
||||
@@ -690,20 +687,12 @@ static enum fx_status iterator_erase(fx_iterator *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
|
||||
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
|
||||
|
||||
return FX_ITERATOR_VALUE_PTR(&it->entry);
|
||||
}
|
||||
|
||||
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);
|
||||
return &it->entry_value;
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
@@ -716,7 +705,6 @@ FX_TYPE_CLASS_BEGIN(fx_directory)
|
||||
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
|
||||
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_CLASS_END(fx_directory)
|
||||
|
||||
@@ -739,7 +727,6 @@ FX_TYPE_CLASS_BEGIN(fx_directory_iterator)
|
||||
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
|
||||
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
|
||||
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
|
||||
FX_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
|
||||
FX_TYPE_CLASS_END(fx_directory_iterator)
|
||||
|
||||
|
||||
+27
-24
@@ -1,3 +1,5 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
|
||||
#include "misc.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_string_unref(filename);
|
||||
|
||||
const fx_path *parts[] = {
|
||||
dir,
|
||||
shadow_filename,
|
||||
const fx_value *parts[] = {
|
||||
&FX_VALUE_OBJECT(dir),
|
||||
&FX_VALUE_OBJECT(shadow_filename),
|
||||
};
|
||||
|
||||
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(shadow_filename);
|
||||
|
||||
@@ -101,7 +103,7 @@ static fx_result file_open_shadow(
|
||||
fx_file *shadow_file;
|
||||
fx_result status = fx_file_open(
|
||||
FX_DIRECTORY_ROOT,
|
||||
shadow_filepath,
|
||||
FX_VALUE_OBJECT(shadow_filepath),
|
||||
mode,
|
||||
&shadow_file);
|
||||
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);
|
||||
fx_path *tmp_name_p = fx_path_create_from_cstr(tmp_name);
|
||||
|
||||
const fx_path *parts[] = {
|
||||
dir_path,
|
||||
tmp_name_p,
|
||||
const fx_value *parts[] = {
|
||||
&FX_VALUE_OBJECT(dir_path),
|
||||
&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);
|
||||
|
||||
@@ -248,11 +252,15 @@ static enum fx_status file_swap_shadow(
|
||||
|
||||
int err;
|
||||
|
||||
err = rename(fx_path_ptr(main_file->path), fx_path_ptr(tmp_path));
|
||||
err = rename(
|
||||
fx_path_ptr(shadow_file->path),
|
||||
fx_path_ptr(main_file->path));
|
||||
err = rename(fx_path_ptr(tmp_path), fx_path_ptr(shadow_file->path));
|
||||
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));
|
||||
|
||||
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_directory *root,
|
||||
const fx_path *path,
|
||||
fx_value path,
|
||||
enum fx_file_mode mode,
|
||||
fx_file **out)
|
||||
{
|
||||
const fx_path *file_path = path;
|
||||
unsigned int flags = fx_mode_to_unix_mode(mode);
|
||||
|
||||
if (flags == (unsigned int)-1) {
|
||||
@@ -422,16 +429,12 @@ fx_result fx_file_open(
|
||||
if (root) {
|
||||
root_path = fx_directory_get_path(root);
|
||||
} else {
|
||||
root_path = fx_path_create_cwd();
|
||||
root_path = fx_path_get_system(FX_PATH_CWD);
|
||||
free_root_path = true;
|
||||
}
|
||||
|
||||
const fx_path *parts[] = {
|
||||
root_path,
|
||||
file_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_path), &path);
|
||||
|
||||
if (free_root_path) {
|
||||
fx_path_unref((fx_path *)root_path);
|
||||
@@ -441,7 +444,7 @@ fx_result fx_file_open(
|
||||
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) {
|
||||
fx_path_unref(abs_path);
|
||||
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_DIRECTORY_ROOT,
|
||||
rpath,
|
||||
FX_VALUE_OBJECT(rpath),
|
||||
mode | FX_FILE_CREATE_ONLY,
|
||||
out);
|
||||
|
||||
|
||||
+106
-44
@@ -91,22 +91,22 @@ static bool path_is_directory(const struct fx_path_p *path)
|
||||
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_append_s(dest->p_pathstr, src->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->p_pathstr) != '/') {
|
||||
char s[] = {'/', 0};
|
||||
fx_string_append_cstr(dest->p_pathstr, s);
|
||||
&& fx_string_get_first_char(src) != '/') {
|
||||
fx_string_append_c(dest->p_pathstr, '/');
|
||||
}
|
||||
|
||||
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)
|
||||
@@ -199,33 +199,6 @@ fx_path *fx_path_create_root()
|
||||
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 *path = fx_path_create();
|
||||
@@ -258,6 +231,49 @@ fx_path *fx_path_create_from_cstr(const char *cstr)
|
||||
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 *new_path = fx_path_create();
|
||||
@@ -277,7 +293,7 @@ fx_path *fx_path_duplicate(const fx_path *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();
|
||||
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
|
||||
= fx_object_get_private(result, FX_TYPE_PATH);
|
||||
|
||||
for (size_t i = 0; i < nr_paths; i++) {
|
||||
if (paths[i]) {
|
||||
struct fx_path_p *path_p
|
||||
= fx_object_get_private(paths[i], FX_TYPE_PATH);
|
||||
append_path(result_p, path_p);
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -363,7 +418,7 @@ enum fx_status fx_path_get_filename(const fx_path *path, fx_string *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);
|
||||
}
|
||||
@@ -392,11 +447,18 @@ void path_fini(fx_object *obj, void *priv)
|
||||
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 *********************************************************/
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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
@@ -37,6 +37,7 @@ struct fx_directory_iterator_p {
|
||||
fx_directory *root;
|
||||
|
||||
fx_directory_entry entry;
|
||||
fx_value entry_value;
|
||||
};
|
||||
|
||||
/*** 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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
return fx_path_ptr(dir->d_path_rel);
|
||||
return fx_path_get_cstr(dir->d_path_rel);
|
||||
}
|
||||
|
||||
static fx_result directory_delete(
|
||||
@@ -89,12 +90,13 @@ static bool directory_path_exists(
|
||||
const struct fx_directory_p *root,
|
||||
const fx_path *path)
|
||||
{
|
||||
const fx_path *parts[] = {
|
||||
root ? root->d_path_abs : NULL,
|
||||
path,
|
||||
const fx_value *parts[] = {
|
||||
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
|
||||
&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) {
|
||||
return false;
|
||||
}
|
||||
@@ -109,12 +111,13 @@ static bool directory_path_is_file(
|
||||
const struct fx_directory_p *root,
|
||||
const fx_path *path)
|
||||
{
|
||||
const fx_path *parts[] = {
|
||||
root ? root->d_path_abs : NULL,
|
||||
path,
|
||||
const fx_value *parts[] = {
|
||||
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
|
||||
&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) {
|
||||
return false;
|
||||
}
|
||||
@@ -129,12 +132,13 @@ static bool directory_path_is_directory(
|
||||
const struct fx_directory_p *root,
|
||||
const fx_path *path)
|
||||
{
|
||||
const fx_path *parts[] = {
|
||||
root ? root->d_path_abs : NULL,
|
||||
path,
|
||||
const fx_value *parts[] = {
|
||||
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
|
||||
&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) {
|
||||
return false;
|
||||
}
|
||||
@@ -150,12 +154,13 @@ static fx_result directory_path_stat(
|
||||
const fx_path *path,
|
||||
struct fx_file_info *out)
|
||||
{
|
||||
const fx_path *parts[] = {
|
||||
root ? root->d_path_abs : NULL,
|
||||
path,
|
||||
const fx_value *parts[] = {
|
||||
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
|
||||
&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) {
|
||||
return FX_RESULT_ERR(NO_MEMORY);
|
||||
}
|
||||
@@ -170,12 +175,13 @@ static fx_result directory_path_unlink(
|
||||
const struct fx_directory_p *root,
|
||||
const fx_path *path)
|
||||
{
|
||||
const fx_path *parts[] = {
|
||||
root ? root->d_path_abs : NULL,
|
||||
path,
|
||||
const fx_value *parts[] = {
|
||||
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
|
||||
&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) {
|
||||
return FX_RESULT_ERR(NO_MEMORY);
|
||||
}
|
||||
@@ -263,7 +269,7 @@ static fx_result directory_open(
|
||||
|
||||
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) {
|
||||
while (*path_cstr == '/') {
|
||||
path_cstr++;
|
||||
@@ -297,15 +303,16 @@ static fx_result directory_open(
|
||||
= fx_object_get_private(dir, FX_TYPE_DIRECTORY);
|
||||
|
||||
if (!root) {
|
||||
cwd = fx_path_create_cwd();
|
||||
cwd = fx_path_get_system(FX_PATH_CWD);
|
||||
}
|
||||
|
||||
const fx_path *parts[] = {
|
||||
root ? root->d_path_abs : cwd,
|
||||
path,
|
||||
const fx_value *parts[] = {
|
||||
&FX_VALUE_OBJECT(root ? root->d_path_abs : cwd),
|
||||
&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) {
|
||||
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(
|
||||
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;
|
||||
|
||||
@@ -567,7 +576,7 @@ fx_iterator *fx_directory_begin(
|
||||
int fts_flags = FTS_COMFOLLOW | FTS_NOCHDIR;
|
||||
|
||||
const char *path_list[] = {
|
||||
fx_path_ptr(it->_p->d_path_abs),
|
||||
fx_path_get_cstr(it->_p->d_path_abs),
|
||||
NULL,
|
||||
};
|
||||
|
||||
@@ -616,12 +625,7 @@ fx_iterator *fx_directory_begin(
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
static fx_iterator *iterator_begin(fx_object *obj)
|
||||
{
|
||||
return fx_directory_begin(obj, FX_DIRECTORY_ITERATE_PARENT_FIRST);
|
||||
}
|
||||
|
||||
static const fx_iterator *iterator_cbegin(const fx_object *obj)
|
||||
static const fx_iterator *iterator_begin(const fx_object *obj)
|
||||
{
|
||||
return fx_directory_begin(
|
||||
(fx_object *)obj,
|
||||
@@ -692,20 +696,12 @@ static enum fx_status iterator_erase(fx_iterator *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
|
||||
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
|
||||
|
||||
return FX_ITERATOR_VALUE_PTR(&it->entry);
|
||||
}
|
||||
|
||||
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);
|
||||
return &it->entry_value;
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
@@ -718,7 +714,6 @@ FX_TYPE_CLASS_BEGIN(fx_directory)
|
||||
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
|
||||
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_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_erase) = iterator_erase;
|
||||
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_CLASS_END(fx_directory_iterator)
|
||||
|
||||
|
||||
+25
-18
@@ -86,13 +86,13 @@ static fx_result file_open_shadow(
|
||||
= fx_path_create_from_cstr(fx_string_get_cstr(filename));
|
||||
fx_string_unref(filename);
|
||||
|
||||
const fx_path *parts[] = {
|
||||
dir,
|
||||
shadow_filename,
|
||||
const fx_value *parts[] = {
|
||||
&FX_VALUE_OBJECT(dir),
|
||||
&FX_VALUE_OBJECT(shadow_filename),
|
||||
};
|
||||
|
||||
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(shadow_filename);
|
||||
|
||||
@@ -229,12 +229,14 @@ static enum fx_status file_swap_shadow(
|
||||
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_path *parts[] = {
|
||||
dir_path,
|
||||
tmp_name_p,
|
||||
const fx_value *parts[] = {
|
||||
&FX_VALUE_OBJECT(dir_path),
|
||||
&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);
|
||||
|
||||
@@ -250,11 +252,15 @@ static enum fx_status file_swap_shadow(
|
||||
|
||||
int err;
|
||||
|
||||
err = rename(fx_path_ptr(main_file->path), fx_path_ptr(tmp_path));
|
||||
err = rename(
|
||||
fx_path_ptr(shadow_file->path),
|
||||
fx_path_ptr(main_file->path));
|
||||
err = rename(fx_path_ptr(tmp_path), fx_path_ptr(shadow_file->path));
|
||||
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));
|
||||
|
||||
fx_path_unref(tmp_path);
|
||||
|
||||
@@ -424,16 +430,17 @@ fx_result fx_file_open(
|
||||
if (root) {
|
||||
root_path = fx_directory_get_path(root);
|
||||
} else {
|
||||
root_path = fx_path_create_cwd();
|
||||
root_path = fx_path_get_system(FX_PATH_CWD);
|
||||
free_root_path = true;
|
||||
}
|
||||
|
||||
const fx_path *parts[] = {
|
||||
root_path,
|
||||
file_path,
|
||||
const fx_value *parts[] = {
|
||||
&FX_VALUE_OBJECT(root_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) {
|
||||
fx_path_unref((fx_path *)root_path);
|
||||
@@ -443,7 +450,7 @@ fx_result fx_file_open(
|
||||
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) {
|
||||
fx_path_unref(abs_path);
|
||||
return FX_RESULT_STATUS(
|
||||
|
||||
+106
-44
@@ -91,22 +91,22 @@ static bool path_is_directory(const struct fx_path_p *path)
|
||||
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_append_s(dest->p_pathstr, src->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->p_pathstr) != '/') {
|
||||
char s[] = {'/', 0};
|
||||
fx_string_append_cstr(dest->p_pathstr, s);
|
||||
&& fx_string_get_first_char(src) != '/') {
|
||||
fx_string_append_c(dest->p_pathstr, '/');
|
||||
}
|
||||
|
||||
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)
|
||||
@@ -199,33 +199,6 @@ fx_path *fx_path_create_root()
|
||||
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 *path = fx_path_create();
|
||||
@@ -258,6 +231,49 @@ fx_path *fx_path_create_from_cstr(const char *cstr)
|
||||
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 *new_path = fx_path_create();
|
||||
@@ -277,7 +293,7 @@ fx_path *fx_path_duplicate(const fx_path *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();
|
||||
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
|
||||
= fx_object_get_private(result, FX_TYPE_PATH);
|
||||
|
||||
for (size_t i = 0; i < nr_paths; i++) {
|
||||
if (paths[i]) {
|
||||
struct fx_path_p *path_p
|
||||
= fx_object_get_private(paths[i], FX_TYPE_PATH);
|
||||
append_path(result_p, path_p);
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -363,7 +418,7 @@ enum fx_status fx_path_get_filename(const fx_path *path, fx_string *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);
|
||||
}
|
||||
@@ -392,11 +447,18 @@ void path_fini(fx_object *obj, void *priv)
|
||||
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 *********************************************************/
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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)
|
||||
@@ -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)
|
||||
@@ -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)
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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
|
||||
@@ -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)
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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)
|
||||
@@ -60,7 +60,7 @@ enum fx_status fx_directory_open(
|
||||
}
|
||||
|
||||
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,
|
||||
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->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(wildcard);
|
||||
@@ -224,7 +224,7 @@ static bool move_into_directory(struct fx_directory_iterator *it, const char *di
|
||||
static bool move_to_first_item(
|
||||
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) {
|
||||
return false;
|
||||
|
||||
@@ -205,13 +205,13 @@ bool fx_path_is_absolute(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;
|
||||
}
|
||||
|
||||
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) {
|
||||
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)
|
||||
{
|
||||
DWORD attrib = GetFileAttributesA(fx_path_ptr(path));
|
||||
DWORD attrib = GetFileAttributesA(fx_path_get_cstr(path));
|
||||
if (attrib == INVALID_FILE_ATTRIBUTES) {
|
||||
return false;
|
||||
}
|
||||
@@ -229,7 +229,7 @@ bool fx_path_is_directory(const struct fx_path *path)
|
||||
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);
|
||||
}
|
||||
|
||||
+4
-1
@@ -11,7 +11,10 @@ int main(int argc, const char **argv)
|
||||
|
||||
fx_directory *dir;
|
||||
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)) {
|
||||
fx_throw(result);
|
||||
return -1;
|
||||
|
||||
@@ -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
@@ -9,7 +9,7 @@ int main(int argc, const char **argv)
|
||||
const char *path = argv[1];
|
||||
|
||||
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)) {
|
||||
fx_throw(result);
|
||||
return -1;
|
||||
|
||||
@@ -6,10 +6,9 @@
|
||||
int main(int argc, const char **argv)
|
||||
{
|
||||
fx_file *dest;
|
||||
fx_path *path = fx_path_create_from_cstr("data.txt");
|
||||
fx_result result = fx_file_open(
|
||||
NULL,
|
||||
path,
|
||||
FX_CSTR("data.txt"),
|
||||
FX_FILE_WRITE_ONLY | FX_FILE_CREATE,
|
||||
&dest);
|
||||
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);
|
||||
|
||||
fx_path_unref(path);
|
||||
fx_file_unref(dest);
|
||||
return 0;
|
||||
}
|
||||
|
||||
+11
-5
@@ -12,18 +12,24 @@ int main(int argc, const char **argv)
|
||||
}
|
||||
|
||||
fx_directory *dir = NULL;
|
||||
fx_path *path = fx_path_create_from_cstr(argv[1]);
|
||||
fx_result result = fx_directory_open(FX_DIRECTORY_ROOT, path, 0, &dir);
|
||||
fx_result result = fx_directory_open(
|
||||
FX_DIRECTORY_ROOT,
|
||||
FX_CSTR(argv[1]),
|
||||
0,
|
||||
&dir);
|
||||
|
||||
if (fx_result_is_error(result)) {
|
||||
fx_throw(result);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fx_iterator *it = fx_directory_begin(dir, FX_DIRECTORY_ITERATE_PARENT_FIRST);
|
||||
fx_foreach(fx_directory_entry *, entry, it)
|
||||
fx_iterator *it
|
||||
= fx_directory_begin(dir, FX_DIRECTORY_ITERATE_PARENT_FIRST);
|
||||
fx_foreach(val, it)
|
||||
{
|
||||
printf("%s\n", fx_path_ptr(entry->filepath));
|
||||
fx_directory_entry *entry = NULL;
|
||||
fx_value_get_pointer(val, (void **)&entry);
|
||||
printf("%s\n", fx_path_get_cstr(entry->filepath));
|
||||
}
|
||||
|
||||
fx_iterator_unref(it);
|
||||
|
||||
+363
-25
@@ -1,8 +1,24 @@
|
||||
#include "fx/namemap.h"
|
||||
|
||||
#include <fx/bst.h>
|
||||
#include <fx/hash.h>
|
||||
#include <fx/iterator.h>
|
||||
#include <fx/macros.h>
|
||||
#include <fx/queue.h>
|
||||
#include <fx/reflection/assembly.h>
|
||||
#include <fx/reflection/type.h>
|
||||
|
||||
FX_API fx_type_id fx_assembly_iterator_get_type();
|
||||
FX_API fx_type_id fx_assembly_type_iterator_get_type();
|
||||
|
||||
FX_DECLARE_TYPE(fx_assembly_iterator);
|
||||
FX_DECLARE_TYPE(fx_assembly_type_iterator);
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_assembly_iterator)
|
||||
FX_TYPE_CLASS_DECLARATION_END(fx_assembly_iterator)
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_assembly_type_iterator)
|
||||
FX_TYPE_CLASS_DECLARATION_END(fx_assembly_type_iterator)
|
||||
|
||||
enum map_entry_type {
|
||||
MAP_ENTRY_NONE = 0,
|
||||
@@ -12,6 +28,7 @@ enum map_entry_type {
|
||||
|
||||
struct map_entry {
|
||||
enum map_entry_type e_type;
|
||||
struct map_bucket *e_bucket;
|
||||
uint64_t e_hash;
|
||||
union {
|
||||
fx_queue_entry e_entry;
|
||||
@@ -39,8 +56,10 @@ struct type {
|
||||
};
|
||||
|
||||
struct fx_assembly_p {
|
||||
fx_assembly *a_self;
|
||||
const char *a_name;
|
||||
struct map a_types;
|
||||
fx_namemap_entry a_entry;
|
||||
|
||||
struct {
|
||||
long v_major;
|
||||
@@ -50,6 +69,16 @@ struct fx_assembly_p {
|
||||
} a_version;
|
||||
};
|
||||
|
||||
struct fx_assembly_iterator_p {
|
||||
fx_namemap_entry *it_cur;
|
||||
fx_value it_value;
|
||||
};
|
||||
|
||||
struct fx_assembly_type_iterator_p {
|
||||
struct type *it_item;
|
||||
fx_value it_value;
|
||||
};
|
||||
|
||||
/*** PRIVATE FUNCTIONS ********************************************************/
|
||||
|
||||
FX_BST_DEFINE_SIMPLE_GET(
|
||||
@@ -60,6 +89,8 @@ FX_BST_DEFINE_SIMPLE_GET(
|
||||
map_get_entry);
|
||||
FX_BST_DEFINE_SIMPLE_INSERT(struct map_entry, e_node, e_hash, map_put_entry);
|
||||
|
||||
static fx_namemap assembly_map = FX_NAMEMAP_INIT;
|
||||
|
||||
static struct map_bucket *map_item_convert_to_bucket(
|
||||
struct map *map,
|
||||
struct map_item *item)
|
||||
@@ -78,6 +109,28 @@ static struct map_bucket *map_item_convert_to_bucket(
|
||||
return bucket;
|
||||
}
|
||||
|
||||
static struct map_item *map_first_item(const struct map *map)
|
||||
{
|
||||
fx_bst_node *first = fx_bst_first(&map->m_entries);
|
||||
if (!first) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct map_entry *entry = fx_unbox(struct map_entry, first, e_node);
|
||||
if (entry->e_type == MAP_ENTRY_ITEM) {
|
||||
return (struct map_item *)entry;
|
||||
}
|
||||
|
||||
struct map_bucket *bucket = (struct map_bucket *)entry;
|
||||
fx_queue_entry *first_entry = fx_queue_first(&bucket->b_items);
|
||||
if (!first_entry) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
entry = fx_unbox(struct map_entry, first_entry, e_entry);
|
||||
return (struct map_item *)entry;
|
||||
}
|
||||
|
||||
static void map_put(struct map *map, const char *name, struct map_item *item)
|
||||
{
|
||||
uint64_t hash = fx_hash_cstr(name);
|
||||
@@ -109,34 +162,81 @@ static void map_put(struct map *map, const char *name, struct map_item *item)
|
||||
}
|
||||
}
|
||||
|
||||
static void assembly_set_name(struct fx_assembly_p *asm, const char *name)
|
||||
static struct map_item *map_item_next(struct map_item *item)
|
||||
{
|
||||
asm->a_name = name;
|
||||
struct map_bucket *bucket = item->i_entry.e_bucket;
|
||||
struct map_entry *next_item = NULL;
|
||||
if (bucket) {
|
||||
struct fx_queue_entry *q_item
|
||||
= fx_queue_next(&item->i_entry.e_entry);
|
||||
if (!q_item) {
|
||||
struct fx_bst_node *node
|
||||
= fx_bst_next(&bucket->b_entry.e_node);
|
||||
next_item = fx_unbox(struct map_entry, node, e_node);
|
||||
} else {
|
||||
next_item = fx_unbox(struct map_entry, q_item, e_entry);
|
||||
}
|
||||
} else {
|
||||
struct fx_bst_node *node = fx_bst_next(&item->i_entry.e_node);
|
||||
next_item = fx_unbox(struct map_entry, node, e_node);
|
||||
}
|
||||
|
||||
if (!next_item) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (next_item->e_type == MAP_ENTRY_BUCKET) {
|
||||
bucket = (struct map_bucket *)next_item;
|
||||
struct fx_queue_entry *q_item
|
||||
= fx_queue_first(&bucket->b_items);
|
||||
next_item = fx_unbox(struct map_entry, q_item, e_entry);
|
||||
}
|
||||
|
||||
return (struct map_item *)next_item;
|
||||
}
|
||||
|
||||
static fx_status assembly_register(struct fx_assembly_p *assembly)
|
||||
{
|
||||
if (fx_namemap_get(&assembly_map, assembly->a_name)) {
|
||||
return FX_ERR_NAME_EXISTS;
|
||||
}
|
||||
|
||||
fx_namemap_put(&assembly_map, assembly->a_name, &assembly->a_entry);
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static void assembly_set_name(struct fx_assembly_p *assembly, const char *name)
|
||||
{
|
||||
assembly->a_name = name;
|
||||
}
|
||||
|
||||
static void assembly_set_version(
|
||||
struct fx_assembly_p *asm,
|
||||
struct fx_assembly_p *assembly,
|
||||
long major,
|
||||
long minor,
|
||||
long build,
|
||||
long revision)
|
||||
{
|
||||
asm->a_version.v_major = major;
|
||||
asm->a_version.v_minor = minor;
|
||||
asm->a_version.v_build = build;
|
||||
asm->a_version.v_revision = revision;
|
||||
assembly->a_version.v_major = major;
|
||||
assembly->a_version.v_minor = minor;
|
||||
assembly->a_version.v_build = build;
|
||||
assembly->a_version.v_revision = revision;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
static void assembly_add_type(
|
||||
struct fx_assembly_p *asm,
|
||||
struct fx_assembly_p *assembly,
|
||||
const char *full_name,
|
||||
fx_type_id type_id)
|
||||
{
|
||||
struct type *type = malloc(sizeof *type);
|
||||
memset(type, 0x0, sizeof *type);
|
||||
|
||||
map_put(&asm->a_types, full_name, &type->e_map_item);
|
||||
type->e_type = type_id;
|
||||
fx_type *ty = (fx_type *)fx_type_get_by_id(type_id);
|
||||
fx_type_set_assembly(ty, assembly->a_self);
|
||||
|
||||
map_put(&assembly->a_types, full_name, &type->e_map_item);
|
||||
}
|
||||
|
||||
static void map_item_dump(struct map_item *item)
|
||||
@@ -145,17 +245,17 @@ static void map_item_dump(struct map_item *item)
|
||||
printf(" Type: %s\n", type->e_map_item.i_name);
|
||||
}
|
||||
|
||||
static void assembly_dump(struct fx_assembly_p *asm)
|
||||
static void assembly_dump(struct fx_assembly_p *assembly)
|
||||
{
|
||||
printf("Loaded assembly:\n");
|
||||
printf(" %s, Version=%ld.%ld.%ld.%ld\n",
|
||||
asm->a_name,
|
||||
asm->a_version.v_major,
|
||||
asm->a_version.v_minor,
|
||||
asm->a_version.v_build,
|
||||
asm->a_version.v_revision);
|
||||
assembly->a_name,
|
||||
assembly->a_version.v_major,
|
||||
assembly->a_version.v_minor,
|
||||
assembly->a_version.v_build,
|
||||
assembly->a_version.v_revision);
|
||||
|
||||
fx_bst_node *cur_node = fx_bst_first(&asm->a_types.m_entries);
|
||||
fx_bst_node *cur_node = fx_bst_first(&assembly->a_types.m_entries);
|
||||
while (cur_node) {
|
||||
struct map_entry *entry
|
||||
= fx_unbox(struct map_entry, cur_node, e_node);
|
||||
@@ -187,19 +287,116 @@ static void assembly_dump(struct fx_assembly_p *asm)
|
||||
}
|
||||
}
|
||||
|
||||
static fx_iterator *assembly_get_types(const struct fx_assembly_p *assembly)
|
||||
{
|
||||
fx_assembly_type_iterator *it
|
||||
= fx_object_create(fx_assembly_type_iterator_get_type());
|
||||
if (!it) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_assembly_type_iterator_p *it_p = fx_object_get_private(
|
||||
it,
|
||||
fx_assembly_type_iterator_get_type());
|
||||
struct map_item *item = map_first_item(&assembly->a_types);
|
||||
if (!item) {
|
||||
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;
|
||||
}
|
||||
|
||||
static const char *assembly_get_name(const struct fx_assembly_p *assembly)
|
||||
{
|
||||
return assembly->a_name;
|
||||
}
|
||||
|
||||
static void assembly_get_version(
|
||||
const struct fx_assembly_p *assembly,
|
||||
long *out_major,
|
||||
long *out_minor,
|
||||
long *out_build,
|
||||
long *out_revision)
|
||||
{
|
||||
if (out_major) {
|
||||
*out_major = assembly->a_version.v_major;
|
||||
}
|
||||
|
||||
if (out_minor) {
|
||||
*out_minor = assembly->a_version.v_minor;
|
||||
}
|
||||
|
||||
if (out_build) {
|
||||
*out_build = assembly->a_version.v_build;
|
||||
}
|
||||
|
||||
if (out_revision) {
|
||||
*out_revision = assembly->a_version.v_revision;
|
||||
}
|
||||
}
|
||||
|
||||
/*** PUBLIC FUNCTIONS *********************************************************/
|
||||
|
||||
void fx_assembly_set_name(fx_assembly *asm, const char *name)
|
||||
fx_iterator *fx_assembly_get_all(void)
|
||||
{
|
||||
fx_assembly_iterator *it
|
||||
= fx_object_create(fx_assembly_iterator_get_type());
|
||||
if (!it) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_assembly_iterator_p *it_p
|
||||
= fx_object_get_private(it, fx_assembly_iterator_get_type());
|
||||
it_p->it_cur = fx_namemap_first(&assembly_map);
|
||||
if (!it_p->it_cur) {
|
||||
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;
|
||||
}
|
||||
|
||||
const fx_assembly *fx_assembly_get_by_name(const char *name)
|
||||
{
|
||||
const fx_namemap_entry *entry = fx_namemap_get(&assembly_map, name);
|
||||
if (!entry) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_assembly_p *assembly
|
||||
= fx_unbox(struct fx_assembly_p, entry, a_entry);
|
||||
return assembly->a_self;
|
||||
}
|
||||
|
||||
fx_status fx_assembly_register(fx_assembly *assembly)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
FX_REFLECTION_TYPE_ASSEMBLY,
|
||||
assembly_register,
|
||||
assembly);
|
||||
}
|
||||
|
||||
void fx_assembly_set_name(fx_assembly *assembly, const char *name)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_V(
|
||||
FX_REFLECTION_TYPE_ASSEMBLY,
|
||||
assembly_set_name,
|
||||
asm,
|
||||
assembly,
|
||||
name);
|
||||
}
|
||||
|
||||
void fx_assembly_set_version(
|
||||
fx_assembly *asm,
|
||||
fx_assembly *assembly,
|
||||
long major,
|
||||
long minor,
|
||||
long build,
|
||||
@@ -208,7 +405,7 @@ void fx_assembly_set_version(
|
||||
FX_CLASS_DISPATCH_STATIC_V(
|
||||
FX_REFLECTION_TYPE_ASSEMBLY,
|
||||
assembly_set_version,
|
||||
asm,
|
||||
assembly,
|
||||
major,
|
||||
minor,
|
||||
build,
|
||||
@@ -216,39 +413,141 @@ void fx_assembly_set_version(
|
||||
}
|
||||
|
||||
void fx_assembly_add_type(
|
||||
fx_assembly *asm,
|
||||
fx_assembly *assembly,
|
||||
const char *full_name,
|
||||
fx_type_id type_id)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_V(
|
||||
FX_REFLECTION_TYPE_ASSEMBLY,
|
||||
assembly_add_type,
|
||||
asm,
|
||||
assembly,
|
||||
full_name,
|
||||
type_id);
|
||||
}
|
||||
|
||||
void fx_assembly_dump(const fx_assembly *asm)
|
||||
fx_iterator *fx_assembly_get_types(const fx_assembly *assembly)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
FX_REFLECTION_TYPE_ASSEMBLY,
|
||||
assembly_get_types,
|
||||
assembly);
|
||||
}
|
||||
|
||||
const char *fx_assembly_get_name(const fx_assembly *assembly)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
FX_REFLECTION_TYPE_ASSEMBLY,
|
||||
assembly_get_name,
|
||||
assembly);
|
||||
}
|
||||
|
||||
void fx_assembly_get_version(
|
||||
const fx_assembly *assembly,
|
||||
long *out_major,
|
||||
long *out_minor,
|
||||
long *out_build,
|
||||
long *out_revision)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_V(
|
||||
FX_REFLECTION_TYPE_ASSEMBLY,
|
||||
assembly_get_version,
|
||||
assembly,
|
||||
out_major,
|
||||
out_minor,
|
||||
out_build,
|
||||
out_revision);
|
||||
}
|
||||
|
||||
void fx_assembly_dump(const fx_assembly *assembly)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
FX_REFLECTION_TYPE_ASSEMBLY,
|
||||
assembly_dump,
|
||||
asm);
|
||||
assembly);
|
||||
}
|
||||
|
||||
/*** VIRTUAL FUNCTIONS ********************************************************/
|
||||
|
||||
static void assembly_init(fx_object *obj, void *priv)
|
||||
{
|
||||
struct fx_assembly_p *assembly = priv;
|
||||
assembly->a_self = obj;
|
||||
}
|
||||
|
||||
static void assembly_fini(fx_object *obj, void *priv)
|
||||
{
|
||||
}
|
||||
|
||||
/*** ITERATOR DEFINITION ******************************************************/
|
||||
|
||||
static enum fx_status type_iterator_move_next(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_assembly_type_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
fx_assembly_type_iterator_get_type());
|
||||
if (!it->it_item) {
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
fx_value_unset(&it->it_value);
|
||||
struct map_item *next = map_item_next(&it->it_item->e_map_item);
|
||||
it->it_item = fx_unbox(struct type, next, e_map_item);
|
||||
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 const fx_value *type_iterator_get_value(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_assembly_type_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
fx_assembly_type_iterator_get_type());
|
||||
if (!it->it_item) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return &it->it_value;
|
||||
}
|
||||
|
||||
static enum fx_status assembly_iterator_move_next(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 FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
fx_value_unset(&it->it_value);
|
||||
it->it_cur = fx_namemap_next(&assembly_map, it->it_cur);
|
||||
if (!it->it_cur) {
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
struct fx_assembly_p *assembly
|
||||
= fx_unbox(struct fx_assembly_p, it->it_cur, a_entry);
|
||||
|
||||
it->it_value = FX_VALUE_OBJECT_REF(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 *********************************************************/
|
||||
|
||||
// ---- fx_string DEFINITION
|
||||
FX_TYPE_CLASS_BEGIN(fx_assembly)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = NULL;
|
||||
@@ -257,8 +556,47 @@ FX_TYPE_CLASS_END(fx_assembly)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_assembly)
|
||||
FX_TYPE_ID(0xf6690c30, 0x6642, 0x42f0, 0xb79f, 0xe2baf3684b1b);
|
||||
FX_TYPE_NAME("fx.reflection.assembly");
|
||||
FX_TYPE_CLASS(fx_assembly_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_assembly_p);
|
||||
FX_TYPE_INSTANCE_INIT(assembly_init);
|
||||
FX_TYPE_INSTANCE_FINI(assembly_fini);
|
||||
FX_TYPE_DEFINITION_END(fx_assembly)
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_assembly_type_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) = type_iterator_move_next;
|
||||
FX_INTERFACE_ENTRY(it_get_value) = type_iterator_get_value;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
|
||||
FX_TYPE_CLASS_END(fx_assembly_type_iterator)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_assembly_type_iterator)
|
||||
FX_TYPE_ID(0x74ecb8df, 0x155d, 0x4e45, 0x96a6, 0x0f71e3ea0a1e);
|
||||
FX_TYPE_NAME("fx.reflection.assembly.type_iterator");
|
||||
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
|
||||
FX_TYPE_CLASS(fx_assembly_type_iterator_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_assembly_type_iterator_p);
|
||||
FX_TYPE_DEFINITION_END(fx_assembly_type_iterator)
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_assembly_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) = assembly_iterator_move_next;
|
||||
FX_INTERFACE_ENTRY(it_get_value) = assembly_iterator_get_value;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
|
||||
FX_TYPE_CLASS_END(fx_assembly_iterator)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_assembly_iterator)
|
||||
FX_TYPE_ID(0x391f8d65, 0x9baf, 0x4941, 0xbdbe, 0xe739226f8947);
|
||||
FX_TYPE_NAME("fx.reflection.assembly.iterator");
|
||||
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
|
||||
FX_TYPE_CLASS(fx_assembly_iterator_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_assembly_iterator_p);
|
||||
FX_TYPE_DEFINITION_END(fx_assembly_iterator)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <fx/macros.h>
|
||||
#include <fx/reflection/function.h>
|
||||
#include <fx/string.h>
|
||||
#include <fx/value-type.h>
|
||||
#include <fx/value.h>
|
||||
#include <platform/callvm.h>
|
||||
|
||||
@@ -8,8 +9,8 @@ struct fx_function_p {
|
||||
char *func_name;
|
||||
fx_function_flags func_flags;
|
||||
fx_function_impl func_impl;
|
||||
fx_value_type func_return_type;
|
||||
fx_value_type *func_arg_types;
|
||||
fx_type_id func_return_type;
|
||||
fx_type_id *func_arg_types;
|
||||
/* number of explicit arguments that the function takes.
|
||||
* if the FX_FUNCTION_F_VARARG flag is set, the function supports a
|
||||
* variable number of arguments, which may be handled differently by the
|
||||
@@ -61,6 +62,29 @@ static fx_status function_bind(
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static void push_arg(
|
||||
const fx_value *arg,
|
||||
fx_type_id param_type,
|
||||
struct callvm *vm)
|
||||
{
|
||||
if (!param_type) {
|
||||
callvm_push(vm, arg);
|
||||
return;
|
||||
}
|
||||
|
||||
unsigned int param_value_type = __fx_type_get_value_type(param_type);
|
||||
if (param_value_type == __FX_VALUE_TYPE_CSTR
|
||||
&& fx_value_is_type(arg, FX_TYPE_STRING)) {
|
||||
fx_object *obj;
|
||||
fx_value_get_object(arg, &obj);
|
||||
fx_value cstr = FX_CSTR(fx_string_get_cstr(obj));
|
||||
callvm_push(vm, &cstr);
|
||||
return;
|
||||
}
|
||||
|
||||
callvm_push(vm, arg);
|
||||
}
|
||||
|
||||
static fx_status function_invoke(
|
||||
const struct fx_function_p *func,
|
||||
const fx_value *args,
|
||||
@@ -84,14 +108,27 @@ static fx_status function_invoke(
|
||||
struct callvm vm = {0};
|
||||
callvm_reset(&vm, nr_fixed_args);
|
||||
|
||||
size_t param_index = 0;
|
||||
for (size_t i = 0; i < func->func_nr_bound_args; i++) {
|
||||
const fx_value *arg = &func->func_bound_args[i];
|
||||
callvm_push(&vm, arg);
|
||||
fx_type_id param_type = NULL;
|
||||
if (param_index < func->func_nr_args) {
|
||||
param_type = func->func_arg_types[param_index];
|
||||
}
|
||||
|
||||
push_arg(arg, param_type, &vm);
|
||||
param_index++;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < nr_args; i++) {
|
||||
const fx_value *arg = &args[i];
|
||||
callvm_push(&vm, arg);
|
||||
fx_type_id param_type = NULL;
|
||||
if (param_index < func->func_nr_args) {
|
||||
param_type = func->func_arg_types[param_index];
|
||||
}
|
||||
|
||||
push_arg(arg, param_type, &vm);
|
||||
param_index++;
|
||||
}
|
||||
|
||||
*return_value = callvm_invoke(
|
||||
@@ -109,9 +146,9 @@ fx_function *fx_function_create(
|
||||
const char *name,
|
||||
fx_function_flags flags,
|
||||
fx_function_impl impl,
|
||||
const fx_value_type *args,
|
||||
const fx_type_id *args,
|
||||
size_t nr_args,
|
||||
fx_value_type return_type)
|
||||
fx_type_id return_type)
|
||||
{
|
||||
fx_function *func = fx_object_create(FX_REFLECTION_TYPE_FUNCTION);
|
||||
if (!func) {
|
||||
@@ -122,7 +159,7 @@ fx_function *fx_function_create(
|
||||
func,
|
||||
FX_REFLECTION_TYPE_FUNCTION);
|
||||
|
||||
if (nr_args == 1 && args[0] == FX_VALUE_TYPE_NONE) {
|
||||
if (nr_args == 1 && args[0] == NULL) {
|
||||
nr_args = 0;
|
||||
}
|
||||
|
||||
@@ -191,8 +228,7 @@ static void function_fini(fx_object *obj, void *priv)
|
||||
}
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION
|
||||
* *********************************************************/
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_function)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef FX_REFLECTION_ASSEMBLY_H_
|
||||
#define FX_REFLECTION_ASSEMBLY_H_
|
||||
|
||||
#include <fx/iterator.h>
|
||||
#include <fx/macros.h>
|
||||
|
||||
FX_DECLS_BEGIN;
|
||||
@@ -16,20 +17,33 @@ FX_API fx_type_id fx_assembly_get_type();
|
||||
|
||||
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_assembly, FX_REFLECTION_TYPE_ASSEMBLY);
|
||||
|
||||
FX_API void fx_assembly_set_name(fx_assembly *asm, const char *name);
|
||||
FX_API fx_iterator *fx_assembly_get_all(void);
|
||||
FX_API const fx_assembly *fx_assembly_get_by_name(const char *name);
|
||||
|
||||
FX_API fx_status fx_assembly_register(fx_assembly *assembly);
|
||||
FX_API void fx_assembly_set_name(fx_assembly *assembly, const char *name);
|
||||
FX_API void fx_assembly_set_version(
|
||||
fx_assembly *asm,
|
||||
fx_assembly *assembly,
|
||||
long major,
|
||||
long minor,
|
||||
long build,
|
||||
long revision);
|
||||
|
||||
FX_API void fx_assembly_add_type(
|
||||
fx_assembly *asm,
|
||||
fx_assembly *assembly,
|
||||
const char *full_name,
|
||||
fx_type_id type_id);
|
||||
FX_API fx_iterator *fx_assembly_get_types(const fx_assembly *assembly);
|
||||
|
||||
FX_API void fx_assembly_dump(const fx_assembly *asm);
|
||||
FX_API const char *fx_assembly_get_name(const fx_assembly *assembly);
|
||||
FX_API void fx_assembly_get_version(
|
||||
const fx_assembly *assembly,
|
||||
long *out_major,
|
||||
long *out_minor,
|
||||
long *out_build,
|
||||
long *out_revision);
|
||||
|
||||
FX_API void fx_assembly_dump(const fx_assembly *assembly);
|
||||
|
||||
FX_DECLS_END;
|
||||
|
||||
|
||||
@@ -32,9 +32,9 @@ FX_API fx_function *fx_function_create(
|
||||
const char *name,
|
||||
fx_function_flags flags,
|
||||
fx_function_impl impl,
|
||||
const fx_value_type *args,
|
||||
const fx_type_id *args,
|
||||
size_t nr_args,
|
||||
fx_value_type return_type);
|
||||
fx_type_id return_type);
|
||||
FX_API fx_status
|
||||
fx_function_bind(fx_function *func, fx_value *args, size_t nr_args);
|
||||
FX_API fx_status fx_function_invoke(
|
||||
|
||||
@@ -1,4 +1,38 @@
|
||||
#ifndef FX_REFLECTION_PROPERTY_H_
|
||||
#define FX_REFLECTION_PROPERTY_H_
|
||||
|
||||
#include <fx/macros.h>
|
||||
#include <fx/value.h>
|
||||
|
||||
FX_DECLS_BEGIN;
|
||||
|
||||
#define FX_REFLECTION_TYPE_PROPERTY (fx_property_get_type())
|
||||
|
||||
FX_DECLARE_TYPE(fx_property)
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_property)
|
||||
FX_TYPE_CLASS_DECLARATION_END(fx_property)
|
||||
|
||||
typedef fx_status (
|
||||
*fx_property_getter)(const fx_value *, const fx_property *, fx_value *);
|
||||
typedef fx_status (
|
||||
*fx_property_setter)(fx_value *, const fx_property *, fx_value *);
|
||||
|
||||
FX_API fx_type_id fx_property_get_type(void);
|
||||
|
||||
FX_API fx_property *fx_property_create(
|
||||
const char *name,
|
||||
fx_property_getter get,
|
||||
fx_property_setter set);
|
||||
|
||||
FX_API const char *fx_property_get_name(const fx_property *prop);
|
||||
FX_API fx_status fx_property_get_value(
|
||||
const fx_property *prop,
|
||||
const fx_value *container,
|
||||
fx_value *out);
|
||||
FX_API fx_status fx_property_set_value(
|
||||
const fx_property *prop,
|
||||
fx_value *container,
|
||||
fx_value *new_value);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
#ifndef FX_REFLECTION_TYPE_H_
|
||||
#define FX_REFLECTION_TYPE_H_
|
||||
|
||||
#include <fx/iterator.h>
|
||||
#include <fx/macros.h>
|
||||
#include <fx/reflection/assembly.h>
|
||||
#include <fx/reflection/function.h>
|
||||
#include <fx/reflection/property.h>
|
||||
|
||||
FX_DECLS_BEGIN;
|
||||
|
||||
@@ -17,14 +20,27 @@ FX_TYPE_CLASS_DECLARATION_END(fx_type)
|
||||
|
||||
FX_API fx_type_id fx_type_get_type();
|
||||
|
||||
FX_API const fx_assembly *fx_type_get_assembly(const fx_type *ty);
|
||||
FX_API fx_status fx_type_set_assembly(fx_type *ty, fx_assembly *assembly);
|
||||
|
||||
FX_API const char *fx_type_get_name(const fx_type *ty);
|
||||
FX_API fx_type_flags fx_type_get_flags(const fx_type *ty);
|
||||
FX_API const fx_function *fx_type_get_function(
|
||||
const fx_type *ty,
|
||||
const char *name);
|
||||
FX_API fx_iterator *fx_type_get_functions(const fx_type *ty);
|
||||
FX_API const fx_property *fx_type_get_property(
|
||||
const fx_type *ty,
|
||||
const char *name);
|
||||
FX_API fx_iterator *fx_type_get_properties(const fx_type *ty);
|
||||
|
||||
FX_API const fx_type *fx_type_get_by_id(fx_type_id id);
|
||||
FX_API const fx_type *fx_type_get_by_name(const char *name);
|
||||
|
||||
FX_API fx_type_id fx_type_get_id(const fx_type *ty);
|
||||
FX_API const fx_type *fx_type_get_parent(const fx_type *ty);
|
||||
FX_API void *fx_type_get_interface(const fx_type *ty, fx_type_id interface_id);
|
||||
|
||||
FX_API fx_type *__fx_type_create(struct fx_type_info *opaque);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
#include <fx/macros.h>
|
||||
#include <fx/reflection/property.h>
|
||||
#include <fx/string.h>
|
||||
#include <fx/value-type.h>
|
||||
#include <fx/value.h>
|
||||
#include <platform/callvm.h>
|
||||
|
||||
struct fx_property_p {
|
||||
fx_property *prop_self;
|
||||
char *prop_name;
|
||||
fx_property_getter prop_getter;
|
||||
fx_property_setter prop_setter;
|
||||
};
|
||||
|
||||
/*** PRIVATE FUNCTIONS ********************************************************/
|
||||
|
||||
static const char *property_get_name(const struct fx_property_p *prop)
|
||||
{
|
||||
return prop->prop_name;
|
||||
}
|
||||
|
||||
static fx_status property_get_value(
|
||||
const struct fx_property_p *prop,
|
||||
const fx_value *container,
|
||||
fx_value *out)
|
||||
{
|
||||
if (!prop->prop_getter) {
|
||||
return FX_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
return prop->prop_getter(container, prop->prop_self, out);
|
||||
}
|
||||
|
||||
static fx_status property_set_value(
|
||||
const struct fx_property_p *prop,
|
||||
fx_value *container,
|
||||
fx_value *new_value)
|
||||
{
|
||||
if (!prop->prop_setter) {
|
||||
return FX_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
return prop->prop_setter(container, prop->prop_self, new_value);
|
||||
}
|
||||
|
||||
/*** PUBLIC FUNCTIONS *********************************************************/
|
||||
|
||||
fx_property *fx_property_create(
|
||||
const char *name,
|
||||
fx_property_getter getter,
|
||||
fx_property_setter setter)
|
||||
{
|
||||
fx_property *prop = fx_object_create(FX_REFLECTION_TYPE_PROPERTY);
|
||||
if (!prop) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_property_p *p = fx_object_get_private(
|
||||
prop,
|
||||
FX_REFLECTION_TYPE_PROPERTY);
|
||||
|
||||
p->prop_name = fx_strdup(name);
|
||||
if (!p->prop_name) {
|
||||
fx_property_unref(prop);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
p->prop_self = prop;
|
||||
p->prop_getter = getter;
|
||||
p->prop_setter = setter;
|
||||
|
||||
return prop;
|
||||
}
|
||||
|
||||
const char *fx_property_get_name(const fx_property *prop)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
FX_REFLECTION_TYPE_PROPERTY,
|
||||
property_get_name,
|
||||
prop);
|
||||
}
|
||||
|
||||
fx_status fx_property_get_value(
|
||||
const fx_property *prop,
|
||||
const fx_value *container,
|
||||
fx_value *out)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_REFLECTION_TYPE_PROPERTY,
|
||||
property_get_value,
|
||||
prop,
|
||||
container,
|
||||
out);
|
||||
}
|
||||
|
||||
fx_status fx_property_set_value(
|
||||
const fx_property *prop,
|
||||
fx_value *container,
|
||||
fx_value *new_value)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_REFLECTION_TYPE_PROPERTY,
|
||||
property_set_value,
|
||||
prop,
|
||||
container,
|
||||
new_value);
|
||||
}
|
||||
|
||||
/*** VIRTUAL FUNCTIONS ********************************************************/
|
||||
|
||||
static void property_init(fx_object *obj, void *priv)
|
||||
{
|
||||
}
|
||||
|
||||
static void property_fini(fx_object *obj, void *priv)
|
||||
{
|
||||
struct fx_property_p *p = priv;
|
||||
if (p->prop_name) {
|
||||
free(p->prop_name);
|
||||
p->prop_name = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION
|
||||
* *********************************************************/
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_property)
|
||||
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_property)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_property)
|
||||
FX_TYPE_ID(0x7007a5ef, 0x3caf, 0x4215, 0x8c0b, 0xd54bf8195624);
|
||||
FX_TYPE_NAME("fx.reflection.property");
|
||||
FX_TYPE_CLASS(fx_property_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_property_p);
|
||||
FX_TYPE_INSTANCE_INIT(property_init);
|
||||
FX_TYPE_INSTANCE_FINI(property_fini);
|
||||
FX_TYPE_DEFINITION_END(fx_property)
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
#include <fx/type.h>
|
||||
#include <fx/value-type.h>
|
||||
#include <fx/value.h>
|
||||
#include <platform/callvm.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
@@ -77,13 +80,88 @@ void callvm_push(struct callvm *vm, const fx_value *value)
|
||||
return;
|
||||
}
|
||||
|
||||
switch (value->v_type.t_primitive) {
|
||||
case FX_VALUE_TYPE_DOUBLE:
|
||||
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;
|
||||
default:
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,17 +172,69 @@ extern void callvm_invoke_v(fx_function_impl impl, struct callvm *vm);
|
||||
fx_value callvm_invoke(
|
||||
struct callvm *vm,
|
||||
fx_function_impl impl,
|
||||
fx_value_type return_type)
|
||||
fx_type_id return_type)
|
||||
{
|
||||
switch (return_type) {
|
||||
case FX_VALUE_TYPE_NONE:
|
||||
callvm_invoke_v(impl, vm);
|
||||
break;
|
||||
case FX_VALUE_TYPE_DOUBLE:
|
||||
return FX_VALUE_DOUBLE(callvm_invoke_d(impl, vm));
|
||||
default:
|
||||
return FX_VALUE_INT(callvm_invoke_i(impl, vm));
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,11 @@
|
||||
#include <fx/value.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define CALLVM_INVOKE_PROTOTYPE(suffix) \
|
||||
fx_value callvm_invoke_##suffix( \
|
||||
struct callvm *vm, \
|
||||
fx_function_impl impl)
|
||||
|
||||
#define MAX_FIXED_ARGS ((unsigned int)-1)
|
||||
#define MAX_DOUBLE_ARGS 8
|
||||
#define MAX_INT_ARGS 8
|
||||
@@ -26,11 +31,13 @@ struct callvm {
|
||||
uintptr_t *vm_arg_excess;
|
||||
};
|
||||
|
||||
typedef fx_value (*callvm_invoke_impl)(struct callvm *, fx_function_impl);
|
||||
|
||||
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_value_type return_type);
|
||||
fx_type_id return_type);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#include <fx/type.h>
|
||||
#include <fx/value-type.h>
|
||||
#include <platform/callvm.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
@@ -11,100 +13,229 @@
|
||||
}
|
||||
#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_double_count = 0;
|
||||
vm->vm_arg_fixed = max_fixed_args;
|
||||
vm->vm_arg_excess_count = 0;
|
||||
vm->vm_double_excess_count = 0;
|
||||
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)
|
||||
static void expand_excess(struct callvm *vm)
|
||||
{
|
||||
size_t new_capacity = vm->vm_arg_excess_max * 2;
|
||||
if (!new_capacity) {
|
||||
new_capacity = 4;
|
||||
}
|
||||
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);
|
||||
void *buf = realloc(
|
||||
vm->vm_arg_excess,
|
||||
new_capacity * sizeof *vm->vm_arg_excess);
|
||||
|
||||
if (!buf) {
|
||||
return;
|
||||
}
|
||||
if (!buf) {
|
||||
return;
|
||||
}
|
||||
|
||||
vm->vm_arg_excess = buf;
|
||||
vm->vm_arg_excess_max = new_capacity;
|
||||
vm->vm_arg_excess = buf;
|
||||
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) {
|
||||
expand_excess(vm);
|
||||
}
|
||||
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++;
|
||||
vm->vm_arg_excess[vm->vm_arg_excess_count++] = value;
|
||||
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) {
|
||||
push_excess(vm, value);
|
||||
return;
|
||||
}
|
||||
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++;
|
||||
vm->vm_arg_int[vm->vm_arg_int_count++] = value;
|
||||
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) {
|
||||
push_excess(vm, *(uintptr_t*)&value);
|
||||
return;
|
||||
}
|
||||
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++;
|
||||
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++;
|
||||
}
|
||||
if (vm->vm_arg_count > vm->vm_arg_fixed) {
|
||||
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) {
|
||||
case FX_VALUE_TYPE_DOUBLE:
|
||||
push_double(vm, value->v_double);
|
||||
break;
|
||||
default:
|
||||
push_int(vm, (uintptr_t)value->v_pointer);
|
||||
break;
|
||||
}
|
||||
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);
|
||||
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_value_type return_type)
|
||||
struct callvm *vm,
|
||||
fx_function_impl impl,
|
||||
fx_type_id return_type)
|
||||
{
|
||||
switch (return_type) {
|
||||
case FX_VALUE_TYPE_NONE:
|
||||
callvm_invoke_v(impl, vm);
|
||||
break;
|
||||
case FX_VALUE_TYPE_DOUBLE:
|
||||
return FX_VALUE_DOUBLE(callvm_invoke_d(impl, vm));
|
||||
default:
|
||||
return FX_VALUE_INT(callvm_invoke_i(impl, vm));
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
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(
|
||||
struct callvm *vm,
|
||||
fx_function_impl impl,
|
||||
fx_value_type return_type);
|
||||
fx_type_id return_type);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
+337
-6
@@ -1,11 +1,44 @@
|
||||
#include "../fx/type.h"
|
||||
|
||||
#include <fx/macros.h>
|
||||
#include <fx/reflection/type.h>
|
||||
#include <fx/type.h>
|
||||
|
||||
struct fx_type_registration;
|
||||
|
||||
#define FX_REFLECTION_TYPE_TYPE_FUNCTION_ITERATOR \
|
||||
(fx_type_function_iterator_get_type())
|
||||
#define FX_REFLECTION_TYPE_TYPE_PROPERTY_ITERATOR \
|
||||
(fx_type_property_iterator_get_type())
|
||||
|
||||
FX_DECLARE_TYPE(fx_type_function_iterator);
|
||||
FX_DECLARE_TYPE(fx_type_property_iterator);
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_type_function_iterator)
|
||||
FX_TYPE_CLASS_DECLARATION_END(fx_type_function_iterator)
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_type_property_iterator)
|
||||
FX_TYPE_CLASS_DECLARATION_END(fx_type_property_iterator)
|
||||
|
||||
FX_API fx_type_id fx_type_function_iterator_get_type();
|
||||
FX_API fx_type_id fx_type_property_iterator_get_type();
|
||||
|
||||
struct fx_type_p {
|
||||
struct fx_type_info *ty_info;
|
||||
fx_assembly *ty_assembly;
|
||||
};
|
||||
|
||||
struct fx_type_function_iterator_p {
|
||||
struct fx_namemap *it_src;
|
||||
struct fx_namemap_entry *it_cur;
|
||||
fx_value it_value;
|
||||
};
|
||||
|
||||
struct fx_type_property_iterator_p {
|
||||
const fx_type_info *it_cur_type;
|
||||
struct fx_namemap *it_src;
|
||||
struct fx_namemap_entry *it_cur;
|
||||
fx_value it_value;
|
||||
};
|
||||
|
||||
/*** PRIVATE FUNCTIONS ********************************************************/
|
||||
@@ -16,11 +49,27 @@ extern fx_function *fx_type_info_get_function_by_name(
|
||||
const struct fx_type_info *ty,
|
||||
const char *name);
|
||||
|
||||
static const fx_assembly *type_get_assembly(const struct fx_type_p *ty)
|
||||
{
|
||||
return ty->ty_assembly;
|
||||
}
|
||||
|
||||
static fx_status type_set_assembly(struct fx_type_p *ty, fx_assembly *assembly)
|
||||
{
|
||||
ty->ty_assembly = assembly;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static const char *type_get_name(const struct fx_type_p *ty)
|
||||
{
|
||||
return ty->ty_info->ty_name;
|
||||
}
|
||||
|
||||
static fx_type_flags type_get_flags(const struct fx_type_p *ty)
|
||||
{
|
||||
return ty->ty_info->ty_flags;
|
||||
}
|
||||
|
||||
const fx_function *type_get_function(
|
||||
const struct fx_type_p *ty,
|
||||
const char *name)
|
||||
@@ -28,6 +77,102 @@ const fx_function *type_get_function(
|
||||
return fx_type_info_get_function_by_name(ty->ty_info, name);
|
||||
}
|
||||
|
||||
fx_iterator *type_get_functions(const struct fx_type_p *ty)
|
||||
{
|
||||
fx_type_function_iterator *it_obj
|
||||
= fx_object_create(FX_REFLECTION_TYPE_TYPE_FUNCTION_ITERATOR);
|
||||
struct fx_type_function_iterator_p *it = fx_object_get_private(
|
||||
it_obj,
|
||||
FX_REFLECTION_TYPE_TYPE_FUNCTION_ITERATOR);
|
||||
|
||||
it->it_src = &ty->ty_info->ty_functions;
|
||||
it->it_cur = fx_namemap_first(it->it_src);
|
||||
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
const fx_property *type_get_property(
|
||||
const struct fx_type_p *ty,
|
||||
const char *name)
|
||||
{
|
||||
struct fx_type_info *cur = ty->ty_info;
|
||||
while (cur) {
|
||||
const fx_property *prop
|
||||
= fx_type_info_get_property_by_name(cur, name);
|
||||
if (prop) {
|
||||
return prop;
|
||||
}
|
||||
|
||||
if (fx_type_id_compare(&cur->ty_id, FX_TYPE_OBJECT) == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
cur = fx_type_info_get_by_id(&cur->ty_parent_id);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static fx_iterator *type_get_properties(const struct fx_type_p *ty)
|
||||
{
|
||||
fx_type_property_iterator *it_obj
|
||||
= fx_object_create(FX_REFLECTION_TYPE_TYPE_PROPERTY_ITERATOR);
|
||||
struct fx_type_property_iterator_p *it = fx_object_get_private(
|
||||
it_obj,
|
||||
FX_REFLECTION_TYPE_TYPE_PROPERTY_ITERATOR);
|
||||
|
||||
it->it_cur_type = ty->ty_info;
|
||||
it->it_src = &ty->ty_info->ty_properties;
|
||||
it->it_cur = fx_namemap_first(it->it_src);
|
||||
while (!it->it_cur) {
|
||||
if (fx_type_id_compare(&it->it_cur_type->ty_id, FX_TYPE_OBJECT)
|
||||
== 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
const struct fx_type_info *next_type
|
||||
= fx_type_info_get_by_id(&it->it_cur_type
|
||||
->ty_parent_id);
|
||||
if (!next_type) {
|
||||
break;
|
||||
}
|
||||
|
||||
it->it_cur_type = next_type;
|
||||
it->it_cur = fx_namemap_first(&it->it_cur_type->ty_properties);
|
||||
}
|
||||
|
||||
if (!it->it_cur) {
|
||||
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;
|
||||
}
|
||||
|
||||
static fx_type_id type_get_id(const struct fx_type_p *ty)
|
||||
{
|
||||
return &ty->ty_info->ty_id;
|
||||
}
|
||||
|
||||
static const fx_type *type_get_parent(const struct fx_type_p *ty)
|
||||
{
|
||||
if (fx_type_id_compare(&ty->ty_info->ty_id, FX_TYPE_OBJECT) == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return fx_type_get_by_id(&ty->ty_info->ty_parent_id);
|
||||
}
|
||||
|
||||
static void *type_get_interface(
|
||||
const struct fx_type_p *ty,
|
||||
fx_type_id interface_id)
|
||||
{
|
||||
return fx_class_get_interface(ty->ty_info->ty_class, interface_id);
|
||||
}
|
||||
|
||||
/*** PUBLIC FUNCTIONS *********************************************************/
|
||||
|
||||
fx_type *__fx_type_create(struct fx_type_info *type_info)
|
||||
@@ -37,19 +182,40 @@ fx_type *__fx_type_create(struct fx_type_info *type_info)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_type_p *p = fx_object_get_private(
|
||||
out,
|
||||
FX_REFLECTION_TYPE_TYPE);
|
||||
struct fx_type_p *p
|
||||
= fx_object_get_private(out, FX_REFLECTION_TYPE_TYPE);
|
||||
p->ty_info = type_info;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
const fx_assembly *fx_type_get_assembly(const fx_type *ty)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
FX_REFLECTION_TYPE_TYPE,
|
||||
type_get_assembly,
|
||||
ty);
|
||||
}
|
||||
|
||||
fx_status fx_type_set_assembly(fx_type *ty, fx_assembly *assembly)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_REFLECTION_TYPE_TYPE,
|
||||
type_set_assembly,
|
||||
ty,
|
||||
assembly);
|
||||
}
|
||||
|
||||
const char *fx_type_get_name(const fx_type *ty)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_REFLECTION_TYPE_TYPE, type_get_name, ty);
|
||||
}
|
||||
|
||||
fx_type_flags fx_type_get_flags(const fx_type *ty)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_REFLECTION_TYPE_TYPE, type_get_flags, ty);
|
||||
}
|
||||
|
||||
const fx_function *fx_type_get_function(const fx_type *ty, const char *name)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
@@ -59,6 +225,31 @@ const fx_function *fx_type_get_function(const fx_type *ty, const char *name)
|
||||
name);
|
||||
}
|
||||
|
||||
fx_iterator *fx_type_get_functions(const fx_type *ty)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
FX_REFLECTION_TYPE_TYPE,
|
||||
type_get_functions,
|
||||
ty);
|
||||
}
|
||||
|
||||
const fx_property *fx_type_get_property(const fx_type *ty, const char *name)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_REFLECTION_TYPE_TYPE,
|
||||
type_get_property,
|
||||
ty,
|
||||
name);
|
||||
}
|
||||
|
||||
fx_iterator *fx_type_get_properties(const fx_type *ty)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
FX_REFLECTION_TYPE_TYPE,
|
||||
type_get_properties,
|
||||
ty);
|
||||
}
|
||||
|
||||
const fx_type *fx_type_get_by_id(fx_type_id id)
|
||||
{
|
||||
struct fx_type_info *ty = fx_type_info_get_by_id(id);
|
||||
@@ -71,6 +262,28 @@ const fx_type *fx_type_get_by_name(const char *name)
|
||||
return ty ? ty->ty_metatype : NULL;
|
||||
}
|
||||
|
||||
fx_type_id fx_type_get_id(const fx_type *ty)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_REFLECTION_TYPE_TYPE, type_get_id, ty);
|
||||
}
|
||||
|
||||
const fx_type *fx_type_get_parent(const fx_type *ty)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
FX_REFLECTION_TYPE_TYPE,
|
||||
type_get_parent,
|
||||
ty);
|
||||
}
|
||||
|
||||
void *fx_type_get_interface(const fx_type *ty, fx_type_id interface_id)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_REFLECTION_TYPE_TYPE,
|
||||
type_get_interface,
|
||||
ty,
|
||||
interface_id);
|
||||
}
|
||||
|
||||
/*** VIRTUAL FUNCTIONS ********************************************************/
|
||||
|
||||
static void type_init(fx_object *obj, void *priv)
|
||||
@@ -81,12 +294,98 @@ static void type_fini(fx_object *obj, void *priv)
|
||||
{
|
||||
}
|
||||
|
||||
/*** ITERATOR FUNCTIONS *******************************************************/
|
||||
|
||||
static enum fx_status function_iterator_move_next(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_type_function_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_REFLECTION_TYPE_TYPE_FUNCTION_ITERATOR);
|
||||
if (!it->it_cur) {
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
fx_value_unset(&it->it_value);
|
||||
it->it_cur = fx_namemap_next(it->it_src, it->it_cur);
|
||||
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 const fx_value *function_iterator_get_value(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_type_function_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_REFLECTION_TYPE_TYPE_FUNCTION_ITERATOR);
|
||||
if (!it->it_cur) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return &it->it_value;
|
||||
}
|
||||
|
||||
static enum fx_status property_iterator_move_next(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_type_property_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_REFLECTION_TYPE_TYPE_PROPERTY_ITERATOR);
|
||||
if (!it->it_cur) {
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
it->it_cur = fx_namemap_next(it->it_src, it->it_cur);
|
||||
if (!it->it_cur) {
|
||||
if (fx_type_id_compare(&it->it_cur_type->ty_id, FX_TYPE_OBJECT)
|
||||
== 0) {
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
const struct fx_type_info *next_type
|
||||
= fx_type_info_get_by_id(&it->it_cur_type
|
||||
->ty_parent_id);
|
||||
if (!next_type) {
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
it->it_cur_type = next_type;
|
||||
it->it_cur = fx_namemap_first(&it->it_cur_type->ty_properties);
|
||||
}
|
||||
|
||||
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 const fx_value *property_iterator_get_value(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_type_property_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_REFLECTION_TYPE_TYPE_PROPERTY_ITERATOR);
|
||||
if (!it->it_cur) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_type_property *prop
|
||||
= fx_unbox(struct fx_type_property, it->it_cur, p_entry);
|
||||
|
||||
return &it->it_value;
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_type)
|
||||
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_type)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_type)
|
||||
@@ -97,3 +396,35 @@ FX_TYPE_DEFINITION_BEGIN(fx_type)
|
||||
FX_TYPE_INSTANCE_INIT(type_init);
|
||||
FX_TYPE_INSTANCE_FINI(type_fini);
|
||||
FX_TYPE_DEFINITION_END(fx_type)
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_type_function_iterator)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
|
||||
FX_INTERFACE_ENTRY(it_move_next) = function_iterator_move_next;
|
||||
FX_INTERFACE_ENTRY(it_erase) = NULL;
|
||||
FX_INTERFACE_ENTRY(it_get_value) = function_iterator_get_value;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
|
||||
FX_TYPE_CLASS_END(fx_type_function_iterator)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_type_function_iterator)
|
||||
FX_TYPE_ID(0xb29012d5, 0x4d6c, 0x4e3c, 0x9fe5, 0x7d1a8a9e1075);
|
||||
FX_TYPE_NAME("fx.reflection.type.function_iterator");
|
||||
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
|
||||
FX_TYPE_CLASS(fx_type_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_type_function_iterator_p);
|
||||
FX_TYPE_DEFINITION_END(fx_type_function_iterator)
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_type_property_iterator)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
|
||||
FX_INTERFACE_ENTRY(it_move_next) = property_iterator_move_next;
|
||||
FX_INTERFACE_ENTRY(it_erase) = NULL;
|
||||
FX_INTERFACE_ENTRY(it_get_value) = property_iterator_get_value;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
|
||||
FX_TYPE_CLASS_END(fx_type_property_iterator)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_type_property_iterator)
|
||||
FX_TYPE_ID(0x91fcbdd4, 0x7c54, 0x48c2, 0xa5a4, 0x186f76efd84c);
|
||||
FX_TYPE_NAME("fx.reflection.type.property_iterator");
|
||||
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
|
||||
FX_TYPE_CLASS(fx_type_property_iterator_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_type_property_iterator_p);
|
||||
FX_TYPE_DEFINITION_END(fx_type_property_iterator)
|
||||
|
||||
+2
-2
@@ -5,7 +5,7 @@
|
||||
|
||||
static struct fx_error *bitcode_serialise(
|
||||
fx_serial_ctx *serial,
|
||||
fx_object *src,
|
||||
const fx_value *src,
|
||||
fx_stream *dest,
|
||||
enum fx_serial_flags flags)
|
||||
{
|
||||
@@ -15,7 +15,7 @@ static struct fx_error *bitcode_serialise(
|
||||
static struct fx_error *bitcode_deserialise(
|
||||
fx_serial_ctx *serial,
|
||||
fx_stream *src,
|
||||
fx_object **dest,
|
||||
fx_value *dest,
|
||||
enum fx_serial_flags flags)
|
||||
{
|
||||
return FX_RESULT_ERR(NOT_SUPPORTED);
|
||||
|
||||
+2
-2
@@ -44,7 +44,7 @@ FX_TYPE_DEFINITION_END(fx_serial_ctx)
|
||||
|
||||
fx_result fx_serial_ctx_serialise(
|
||||
fx_serial_ctx *ctx,
|
||||
fx_object *src,
|
||||
const fx_value *src,
|
||||
fx_stream *dest,
|
||||
enum fx_serial_flags flags)
|
||||
{
|
||||
@@ -62,7 +62,7 @@ fx_result fx_serial_ctx_serialise(
|
||||
fx_result fx_serial_ctx_deserialise(
|
||||
fx_serial_ctx *ctx,
|
||||
fx_stream *src,
|
||||
fx_object **dest,
|
||||
fx_value *dest,
|
||||
enum fx_serial_flags flags)
|
||||
{
|
||||
FX_CLASS_DISPATCH_VIRTUAL(
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <fx/object.h>
|
||||
#include <fx/status.h>
|
||||
#include <fx/stream.h>
|
||||
#include <fx/value.h>
|
||||
|
||||
FX_DECLS_BEGIN;
|
||||
|
||||
@@ -21,13 +22,13 @@ FX_DECLARE_TYPE(fx_serial_ctx);
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_serial_ctx)
|
||||
fx_result (*s_serialise)(
|
||||
fx_serial_ctx *,
|
||||
fx_object *,
|
||||
const fx_value *,
|
||||
fx_stream *,
|
||||
fx_serial_flags);
|
||||
fx_result (*s_deserialise)(
|
||||
fx_serial_ctx *,
|
||||
fx_stream *,
|
||||
fx_object **,
|
||||
fx_value *,
|
||||
fx_serial_flags);
|
||||
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_serial_ctx *ctx,
|
||||
fx_object *src,
|
||||
const fx_value *src,
|
||||
fx_stream *dest,
|
||||
fx_serial_flags flags);
|
||||
|
||||
FX_API fx_result fx_serial_ctx_deserialise(
|
||||
fx_serial_ctx *ctx,
|
||||
fx_stream *src,
|
||||
fx_object **dest,
|
||||
fx_value *dest,
|
||||
fx_serial_flags flags);
|
||||
|
||||
FX_DECLS_END;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#include <fx/collections/array.h>
|
||||
#include <fx/collections/dict.h>
|
||||
#include <fx/int.h>
|
||||
#include <fx/serial/ctx.h>
|
||||
#include <fx/serial/toml.h>
|
||||
@@ -9,6 +8,7 @@
|
||||
|
||||
int main(void)
|
||||
{
|
||||
#if 0
|
||||
fx_serial_ctx *ctx = fx_toml_serial_ctx_create();
|
||||
|
||||
fx_dict *dict = fx_dict_create();
|
||||
@@ -33,6 +33,7 @@ int main(void)
|
||||
|
||||
fx_dict_unref(dict);
|
||||
fx_serial_ctx_unref(ctx);
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,23 +1,26 @@
|
||||
#include <fx/bool.h>
|
||||
#include <fx/collections/array.h>
|
||||
#include <fx/collections/datetime.h>
|
||||
#include <fx/collections/dict.h>
|
||||
#include <fx/double.h>
|
||||
#include <fx/collections/hashtable.h>
|
||||
#include <fx/datetime.h>
|
||||
#include <fx/float.h>
|
||||
#include <fx/int.h>
|
||||
#include <fx/serial/ctx.h>
|
||||
#include <fx/serial/toml.h>
|
||||
#include <fx/string.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)
|
||||
{
|
||||
fx_stream_write_cstr(fx_stdout, "\"", NULL);
|
||||
|
||||
const fx_iterator *it = fx_iterator_cbegin(data);
|
||||
fx_foreach_c(fx_wchar, c, it)
|
||||
const fx_iterator *it = fx_iterator_begin(data);
|
||||
fx_foreach(val, it)
|
||||
{
|
||||
fx_wchar c;
|
||||
fx_value_get_wchar(val, &c);
|
||||
if (c >= 0x10000) {
|
||||
c -= 0x10000;
|
||||
long hi = 0xD800 | ((c >> 10) & 0x3FF);
|
||||
@@ -53,68 +56,48 @@ void write_tagged_string(fx_string *data)
|
||||
fx_stream_write_cstr(fx_stdout, " }", NULL);
|
||||
}
|
||||
|
||||
void write_tagged_integer(fx_int *data)
|
||||
void write_tagged_integer(const fx_value *data)
|
||||
{
|
||||
fx_stream_write_cstr(
|
||||
fx_stdout,
|
||||
"{ \"type\": \"integer\", \"value\": \"",
|
||||
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_fmt(fx_stdout, NULL, "%lld", data->v_i64, NULL);
|
||||
fx_stream_write_cstr(fx_stdout, "\" }", NULL);
|
||||
}
|
||||
|
||||
void write_tagged_float(fx_double *data)
|
||||
void write_tagged_float(const fx_value *data)
|
||||
{
|
||||
fx_stream_write_cstr(
|
||||
fx_stdout,
|
||||
"{ \"type\": \"float\", \"value\": \"",
|
||||
NULL);
|
||||
|
||||
if (fx_double_is_inf_positive(data)) {
|
||||
double d = data->v_double;
|
||||
if (d == INFINITY) {
|
||||
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);
|
||||
} else if (fx_double_is_nan_positive(data)) {
|
||||
} else if (d == NAN) {
|
||||
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);
|
||||
} else if (d <= 1e-9 || d >= 1e9) {
|
||||
fx_stream_write_fmt(fx_stdout, NULL, "%g", d, NULL);
|
||||
} else {
|
||||
double v = fx_double_get_value(data);
|
||||
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_fmt(fx_stdout, NULL, "%.9lf", d, NULL);
|
||||
}
|
||||
|
||||
fx_stream_write_cstr(fx_stdout, "\" }", NULL);
|
||||
}
|
||||
|
||||
void write_tagged_bool(fx_bool *data)
|
||||
void write_tagged_bool(const fx_value *data)
|
||||
{
|
||||
fx_stream_write_fmt(
|
||||
fx_stdout,
|
||||
NULL,
|
||||
"{ \"type\": \"bool\", \"value\": \"%s\" }",
|
||||
fx_bool_get_value(data) ? "true" : "false",
|
||||
data->v_bool ? "true" : "false",
|
||||
NULL);
|
||||
}
|
||||
|
||||
@@ -145,31 +128,37 @@ void write_tagged_datetime(fx_datetime *data)
|
||||
|
||||
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_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_string_unref(new_data);
|
||||
fx_stringstream_unref(new_data);
|
||||
}
|
||||
|
||||
void write_tagged_dict(fx_dict *data)
|
||||
void write_tagged_hashtable(fx_hashtable *data)
|
||||
{
|
||||
fx_stream_write_cstr(fx_stdout, "{ ", NULL);
|
||||
|
||||
int i = 0;
|
||||
|
||||
fx_iterator *it = fx_iterator_begin(data);
|
||||
fx_foreach(fx_dict_item *, item, it)
|
||||
const fx_iterator *it = fx_iterator_begin(data);
|
||||
fx_foreach(v, it)
|
||||
{
|
||||
fx_hashtable_item *item;
|
||||
fx_value_get_object(v, &item);
|
||||
if (i++ > 0) {
|
||||
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);
|
||||
write_tagged_value(item->value);
|
||||
write_tagged_value(value);
|
||||
}
|
||||
fx_iterator_unref(it);
|
||||
|
||||
@@ -181,35 +170,35 @@ void write_tagged_array(fx_array *data)
|
||||
fx_stream_write_cstr(fx_stdout, "[ ", NULL);
|
||||
|
||||
int i = 0;
|
||||
fx_iterator *it = fx_iterator_begin(data);
|
||||
fx_foreach(fx_object *, obj, it)
|
||||
const fx_iterator *it = fx_iterator_begin(data);
|
||||
fx_foreach(val, it)
|
||||
{
|
||||
if (i++ > 0) {
|
||||
fx_stream_write_cstr(fx_stdout, ", ", NULL);
|
||||
}
|
||||
|
||||
write_tagged_value(obj);
|
||||
write_tagged_value(val);
|
||||
}
|
||||
fx_iterator_unref(it);
|
||||
|
||||
fx_stream_write_cstr(fx_stdout, " ]", NULL);
|
||||
}
|
||||
|
||||
void write_tagged_value(fx_object *data)
|
||||
void write_tagged_value(const fx_value *data)
|
||||
{
|
||||
if (fx_object_is_type(data, FX_TYPE_DICT)) {
|
||||
write_tagged_dict(data);
|
||||
} else if (fx_object_is_type(data, FX_TYPE_ARRAY)) {
|
||||
write_tagged_array(data);
|
||||
} else if (fx_object_is_type(data, FX_TYPE_STRING)) {
|
||||
write_tagged_string(data);
|
||||
} else if (fx_object_is_type(data, FX_TYPE_DATETIME)) {
|
||||
write_tagged_datetime(data);
|
||||
} else if (fx_object_is_type(data, FX_TYPE_INT)) {
|
||||
write_tagged_integer(data);
|
||||
} else if (fx_object_is_type(data, FX_TYPE_BOOL)) {
|
||||
if (fx_value_is_type(data, FX_TYPE_HASHTABLE)) {
|
||||
write_tagged_hashtable(data->v_object);
|
||||
} else if (fx_value_is_type(data, FX_TYPE_ARRAY)) {
|
||||
write_tagged_array(data->v_object);
|
||||
} else if (fx_value_is_type(data, FX_TYPE_STRING)) {
|
||||
write_tagged_string(data->v_object);
|
||||
} else if (fx_value_is_type(data, FX_TYPE_DATETIME)) {
|
||||
write_tagged_datetime(data->v_object);
|
||||
} else if (fx_value_is_type(data, FX_TYPE_BOOL)) {
|
||||
write_tagged_bool(data);
|
||||
} else if (fx_object_is_type(data, FX_TYPE_DOUBLE)) {
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
@@ -221,19 +210,18 @@ int main(void)
|
||||
|
||||
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);
|
||||
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_serial_ctx_unref(ctx);
|
||||
fx_object_unref(data);
|
||||
fx_value_unset(&data);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
+198
-180
@@ -1,17 +1,17 @@
|
||||
#include <fx/bool.h>
|
||||
#include <fx/collections/array.h>
|
||||
#include <fx/collections/datetime.h>
|
||||
#include <fx/collections/dict.h>
|
||||
#include <fx/collections/hashmap.h>
|
||||
#include <fx/double.h>
|
||||
#include <fx/collections/hashtable.h>
|
||||
#include <fx/datetime.h>
|
||||
#include <fx/error.h>
|
||||
#include <fx/float.h>
|
||||
#include <fx/int.h>
|
||||
#include <fx/serial/ctx.h>
|
||||
#include <fx/serial/toml.h>
|
||||
#include <fx/status.h>
|
||||
#include <fx/string.h>
|
||||
#include <fx/stringstream.h>
|
||||
#include <fx/uint.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -117,9 +117,9 @@ struct ctx {
|
||||
fx_stream *ctx_src;
|
||||
fx_string *ctx_wordbuf;
|
||||
fx_string *ctx_linebuf;
|
||||
fx_iterator *ctx_linebuf_ptr;
|
||||
const fx_iterator *ctx_linebuf_ptr;
|
||||
fx_result ctx_result;
|
||||
fx_hashmap *ctx_objects_flags;
|
||||
fx_hashtable *ctx_objects_flags;
|
||||
|
||||
fx_queue ctx_tokens;
|
||||
};
|
||||
@@ -133,28 +133,18 @@ static void ctx_set_object_flags(
|
||||
return;
|
||||
}
|
||||
|
||||
fx_hashmap_key key = {
|
||||
.key_data = obj,
|
||||
.key_size = sizeof(fx_object *),
|
||||
.key_flags = FX_HASHMAP_KEY_F_INTVALUE,
|
||||
};
|
||||
|
||||
const fx_hashmap_value *old_value
|
||||
= fx_hashmap_get(ctx->ctx_objects_flags, &key);
|
||||
fx_value ptr = FX_POINTER(obj);
|
||||
const fx_value *old_value
|
||||
= fx_hashtable_get(ctx->ctx_objects_flags, &ptr);
|
||||
|
||||
enum object_flags new_flags = 0;
|
||||
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;
|
||||
|
||||
fx_hashmap_value value = {
|
||||
.value_data = (void *)new_flags,
|
||||
.value_size = sizeof new_flags,
|
||||
};
|
||||
|
||||
fx_hashmap_put(ctx->ctx_objects_flags, &key, &value);
|
||||
fx_hashtable_put(ctx->ctx_objects_flags, &ptr, &FX_I32(new_flags));
|
||||
}
|
||||
|
||||
static void ctx_clear_object_flags(
|
||||
@@ -166,28 +156,18 @@ static void ctx_clear_object_flags(
|
||||
return;
|
||||
}
|
||||
|
||||
fx_hashmap_key key = {
|
||||
.key_data = obj,
|
||||
.key_size = sizeof(fx_object *),
|
||||
.key_flags = FX_HASHMAP_KEY_F_INTVALUE,
|
||||
};
|
||||
|
||||
const fx_hashmap_value *old_value
|
||||
= fx_hashmap_get(ctx->ctx_objects_flags, &key);
|
||||
fx_value ptr = FX_POINTER(obj);
|
||||
const fx_value *old_value
|
||||
= fx_hashtable_get(ctx->ctx_objects_flags, &ptr);
|
||||
|
||||
enum object_flags new_flags = 0;
|
||||
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;
|
||||
|
||||
fx_hashmap_value value = {
|
||||
.value_data = (void *)new_flags,
|
||||
.value_size = sizeof new_flags,
|
||||
};
|
||||
|
||||
fx_hashmap_put(ctx->ctx_objects_flags, &key, &value);
|
||||
fx_hashtable_put(ctx->ctx_objects_flags, &ptr, &FX_I32(new_flags));
|
||||
}
|
||||
|
||||
static enum object_flags ctx_get_object_flags(struct ctx *ctx, fx_object *obj)
|
||||
@@ -196,16 +176,13 @@ static enum object_flags ctx_get_object_flags(struct ctx *ctx, fx_object *obj)
|
||||
return 0;
|
||||
}
|
||||
|
||||
fx_hashmap_key key = {
|
||||
.key_data = obj,
|
||||
.key_size = sizeof(fx_object *),
|
||||
.key_flags = FX_HASHMAP_KEY_F_INTVALUE,
|
||||
};
|
||||
fx_value ptr = FX_POINTER(obj);
|
||||
const fx_value *old_value
|
||||
= fx_hashtable_get(ctx->ctx_objects_flags, &ptr);
|
||||
|
||||
const fx_hashmap_value *value
|
||||
= fx_hashmap_get(ctx->ctx_objects_flags, &key);
|
||||
if (value) {
|
||||
return (enum object_flags)(uintptr_t)value->value_data;
|
||||
enum object_flags new_flags = 0;
|
||||
if (old_value) {
|
||||
return (enum object_flags)old_value->v_i32;
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -295,7 +272,7 @@ static fx_wchar advance_char(struct ctx *ctx)
|
||||
|
||||
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)) {
|
||||
ctx->ctx_result = FX_RESULT_ERR(BAD_FORMAT);
|
||||
@@ -328,7 +305,7 @@ static fx_wchar peek_char(struct ctx *ctx)
|
||||
|
||||
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)) {
|
||||
ctx->ctx_result = FX_RESULT_ERR(BAD_FORMAT);
|
||||
@@ -924,8 +901,11 @@ static void split_word(struct ctx *ctx, fx_string *wordbuf)
|
||||
FX_STRING_TOK_F_INCLUDE_EMPTY_TOKENS);
|
||||
|
||||
size_t i = 0;
|
||||
fx_foreach_c(const char *, tok, it)
|
||||
fx_foreach(tokv, it)
|
||||
{
|
||||
const char *tok = NULL;
|
||||
fx_value_get_cstr(tokv, &tok);
|
||||
|
||||
if (i > 0) {
|
||||
enqueue_token(ctx, TOK_DOT);
|
||||
}
|
||||
@@ -1027,9 +1007,11 @@ static void read_word(struct ctx *ctx)
|
||||
return;
|
||||
}
|
||||
|
||||
fx_iterator *it = fx_iterator_begin(wordbuf);
|
||||
fx_foreach(fx_wchar, c, it)
|
||||
const fx_iterator *it = fx_iterator_begin(wordbuf);
|
||||
fx_foreach(cv, it)
|
||||
{
|
||||
fx_wchar c = FX_WCHAR_INVALID;
|
||||
fx_value_get_wchar(cv, &c);
|
||||
/* only allow ASCII numbers/letters here */
|
||||
bool ok = isalnum(c) || c == '_' || c == '-' || c == '.';
|
||||
if (!ok) {
|
||||
@@ -1473,14 +1455,24 @@ static fx_result advance_token(struct ctx *ctx)
|
||||
|
||||
start:
|
||||
c = peek_char(ctx);
|
||||
while (isspace(c) && c != '\n' && c != '\r') {
|
||||
advance_char(ctx);
|
||||
c = peek_char(ctx);
|
||||
|
||||
if (c <= 0) {
|
||||
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) {
|
||||
ctx->ctx_flags |= CTX_EOF;
|
||||
return FX_RESULT_ERR(NO_DATA);
|
||||
if (c <= 31 && c != '\n' && c != '\r' && c != '\t') {
|
||||
ctx->ctx_result = FX_RESULT_ERR(BAD_FORMAT);
|
||||
return ctx->ctx_result;
|
||||
}
|
||||
|
||||
while (fx_wchar_is_space(c) && c != '\n' && c != '\r') {
|
||||
advance_char(ctx);
|
||||
c = peek_char(ctx);
|
||||
}
|
||||
|
||||
#if 1
|
||||
@@ -1550,7 +1542,7 @@ static void ctx_cleanup(struct ctx *ctx)
|
||||
}
|
||||
|
||||
if (ctx->ctx_objects_flags) {
|
||||
fx_hashmap_unref(ctx->ctx_objects_flags);
|
||||
fx_hashtable_unref(ctx->ctx_objects_flags);
|
||||
ctx->ctx_objects_flags = NULL;
|
||||
}
|
||||
}
|
||||
@@ -1562,14 +1554,14 @@ static fx_result ctx_init(struct ctx *ctx)
|
||||
ctx->ctx_linebuf = 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;
|
||||
}
|
||||
|
||||
static fx_result toml_serialise(
|
||||
fx_serial_ctx *serial,
|
||||
fx_object *src,
|
||||
const fx_value *src,
|
||||
fx_stream *dest,
|
||||
enum fx_serial_flags flags)
|
||||
{
|
||||
@@ -1663,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_key_value_pair(struct ctx *ctx, fx_dict *container);
|
||||
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_timestamp(struct ctx *ctx, fx_object **result)
|
||||
static fx_result parse_timestamp(struct ctx *ctx, fx_value *result)
|
||||
{
|
||||
struct token *tok = peek_token(ctx);
|
||||
fx_datetime *dt = tok->tok_value.time;
|
||||
tok->tok_value.time = NULL;
|
||||
|
||||
*result = (dt);
|
||||
*result = FX_VALUE_OBJECT(dt);
|
||||
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);
|
||||
fx_string *str = fx_string_duplicate(tok->tok_str);
|
||||
@@ -1684,88 +1676,61 @@ static fx_result parse_string(struct ctx *ctx, fx_object **result)
|
||||
return FX_RESULT_ERR(NO_MEMORY);
|
||||
}
|
||||
|
||||
*result = (str);
|
||||
*result = FX_VALUE_OBJECT(str);
|
||||
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)
|
||||
{
|
||||
struct token *tok = peek_token(ctx);
|
||||
fx_int *val = fx_int_create(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);
|
||||
*result = FX_I64(tok->tok_value.i.v);
|
||||
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)
|
||||
{
|
||||
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.v >= 0) {
|
||||
fx_double_set_value_inf(val);
|
||||
*result = FX_DOUBLE(INFINITY);
|
||||
} else {
|
||||
fx_double_set_value_inf_negative(val);
|
||||
*result = FX_DOUBLE(-INFINITY);
|
||||
}
|
||||
} else if (tok->tok_value.f.nan) {
|
||||
if (tok->tok_value.f.v >= 0) {
|
||||
fx_double_set_value_nan(val);
|
||||
*result = FX_DOUBLE(NAN);
|
||||
} else {
|
||||
fx_double_set_value_nan_negative(val);
|
||||
*result = FX_DOUBLE(-NAN);
|
||||
}
|
||||
} else {
|
||||
*result = FX_DOUBLE(tok->tok_value.f.v);
|
||||
}
|
||||
|
||||
*result = (val);
|
||||
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)
|
||||
{
|
||||
struct token *tok = peek_token(ctx);
|
||||
fx_bool *val = fx_bool_create(tok->tok_value.b);
|
||||
if (!val) {
|
||||
return FX_RESULT_ERR(NO_MEMORY);
|
||||
}
|
||||
|
||||
*result = (val);
|
||||
*result = FX_BOOL(tok->tok_value.b);
|
||||
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);
|
||||
|
||||
advance_token(ctx);
|
||||
|
||||
fx_dict *table = fx_dict_create();
|
||||
fx_hashtable *table = fx_hashtable_create();
|
||||
if (!table) {
|
||||
return FX_RESULT_ERR(NO_MEMORY);
|
||||
}
|
||||
|
||||
struct token *tok = peek_token(ctx);
|
||||
if (tok && tok->tok_type == TOK_RIGHT_BRACE) {
|
||||
*result = (table);
|
||||
*result = FX_VALUE_OBJECT(table);
|
||||
return FX_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -1775,14 +1740,14 @@ static fx_result parse_table_inline(struct ctx *ctx, fx_object **result)
|
||||
fx_object *value;
|
||||
fx_result status = parse_key_value_pair(ctx, table);
|
||||
if (!FX_OK(status)) {
|
||||
fx_dict_unref(table);
|
||||
fx_hashtable_unref(table);
|
||||
return status;
|
||||
}
|
||||
|
||||
tok = peek_token(ctx);
|
||||
|
||||
if (!tok) {
|
||||
fx_dict_unref(table);
|
||||
fx_hashtable_unref(table);
|
||||
return status;
|
||||
}
|
||||
|
||||
@@ -1794,12 +1759,12 @@ static fx_result parse_table_inline(struct ctx *ctx, fx_object **result)
|
||||
advance_token(ctx);
|
||||
break;
|
||||
default:
|
||||
fx_dict_unref(table);
|
||||
fx_hashtable_unref(table);
|
||||
return FX_RESULT_ERR(BAD_FORMAT);
|
||||
}
|
||||
}
|
||||
|
||||
*result = (table);
|
||||
*result = FX_VALUE_OBJECT(table);
|
||||
return FX_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -1813,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;
|
||||
ENABLE_EXTENDED_LEXING(ctx);
|
||||
@@ -1850,14 +1815,15 @@ static fx_result parse_array_inline(struct ctx *ctx, fx_object **result)
|
||||
break;
|
||||
}
|
||||
|
||||
fx_object *value;
|
||||
fx_value value;
|
||||
fx_result status = parse_value(ctx, &value);
|
||||
if (!FX_OK(status)) {
|
||||
fx_array_unref(array);
|
||||
return status;
|
||||
}
|
||||
|
||||
fx_array_append(array, FX_RV(value));
|
||||
fx_array_push_back(array, value);
|
||||
fx_value_unset(&value);
|
||||
ENABLE_EXTENDED_LEXING(ctx);
|
||||
|
||||
advance_token(ctx);
|
||||
@@ -1880,13 +1846,12 @@ static fx_result parse_array_inline(struct ctx *ctx, fx_object **result)
|
||||
}
|
||||
|
||||
DISABLE_EXTENDED_LEXING(ctx);
|
||||
*result = (array);
|
||||
*result = FX_VALUE_OBJECT(array);
|
||||
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);
|
||||
if (!tok) {
|
||||
return FX_RESULT_ERR(BAD_FORMAT);
|
||||
@@ -1912,7 +1877,7 @@ static fx_result parse_value(struct ctx *ctx, fx_object **result)
|
||||
}
|
||||
}
|
||||
|
||||
static fx_result parse_key_value_pair(struct ctx *ctx, fx_dict *container)
|
||||
static fx_result parse_key_value_pair(struct ctx *ctx, fx_hashtable *container)
|
||||
{
|
||||
struct token *tok = peek_token(ctx);
|
||||
if (!IS_VALID_KEY_COMPONENT(tok)) {
|
||||
@@ -1931,36 +1896,42 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_dict *container)
|
||||
}
|
||||
|
||||
while (tok && tok->tok_type == TOK_DOT) {
|
||||
fx_object *sub_dict = fx_dict_at_sk(container, key);
|
||||
if (!sub_dict) {
|
||||
sub_dict = (fx_dict_create());
|
||||
fx_dict_put_sk(container, key, FX_RV(sub_dict));
|
||||
} else if (
|
||||
sub_dict
|
||||
&& !fx_object_is_type(sub_dict, FX_TYPE_DICT)) {
|
||||
free(key);
|
||||
const fx_value *child
|
||||
= fx_hashtable_get(container, &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_value_get_object(child, &subtable);
|
||||
if (!subtable) {
|
||||
subtable = (fx_hashtable_create());
|
||||
fx_hashtable_put(
|
||||
container,
|
||||
&FX_VALUE_OBJECT(key),
|
||||
&FX_VALUE_OBJECT(subtable));
|
||||
}
|
||||
|
||||
#if 1
|
||||
enum object_flags flags = ctx_get_object_flags(ctx, sub_dict);
|
||||
enum object_flags flags = ctx_get_object_flags(ctx, subtable);
|
||||
if (flags
|
||||
& (OBJECT_KV_END_DEFINED | OBJECT_HEADER_END_DEFINED)) {
|
||||
free(key);
|
||||
fx_string_unref(key);
|
||||
return FX_RESULT_ERR(BAD_FORMAT);
|
||||
}
|
||||
#endif
|
||||
|
||||
ctx_set_object_flags(ctx, sub_dict, OBJECT_KV_MID_DEFINED);
|
||||
ctx_set_object_flags(ctx, subtable, OBJECT_KV_MID_DEFINED);
|
||||
|
||||
advance_token(ctx);
|
||||
tok = peek_token(ctx);
|
||||
if (!IS_VALID_KEY_COMPONENT(tok)) {
|
||||
free(key);
|
||||
fx_string_unref(key);
|
||||
return FX_RESULT_ERR(BAD_FORMAT);
|
||||
}
|
||||
|
||||
container = sub_dict;
|
||||
container = subtable;
|
||||
fx_string_unref(key);
|
||||
key = fx_string_duplicate(tok->tok_str);
|
||||
if (!key) {
|
||||
@@ -1971,7 +1942,7 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_dict *container)
|
||||
tok = peek_token(ctx);
|
||||
}
|
||||
|
||||
if (fx_dict_has_skey(container, key)) {
|
||||
if (fx_hashtable_get(container, &FX_VALUE_OBJECT(key))) {
|
||||
return FX_RESULT_ERR(BAD_FORMAT);
|
||||
}
|
||||
|
||||
@@ -1986,7 +1957,7 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_dict *container)
|
||||
ENABLE_EXTENDED_LEXING(ctx);
|
||||
advance_token(ctx);
|
||||
|
||||
fx_object *value = NULL;
|
||||
fx_value value = FX_VALUE_EMPTY;
|
||||
fx_result result = parse_value(ctx, &value);
|
||||
|
||||
DISABLE_EXTENDED_LEXING(ctx);
|
||||
@@ -2001,20 +1972,24 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_dict *container)
|
||||
return fx_result_propagate(result);
|
||||
}
|
||||
|
||||
fx_dict_put_sk(container, key, FX_RV(value));
|
||||
fx_hashtable_put(container, &FX_VALUE_OBJECT(key), &value);
|
||||
|
||||
if (fx_object_is_type(value, FX_TYPE_DICT)
|
||||
|| fx_object_is_type(value, FX_TYPE_ARRAY)) {
|
||||
ctx_set_object_flags(ctx, value, OBJECT_KV_END_DEFINED);
|
||||
if (fx_value_is_type(&value, FX_TYPE_HASHTABLE)
|
||||
|| fx_value_is_type(&value, FX_TYPE_ARRAY)) {
|
||||
ctx_set_object_flags(
|
||||
ctx,
|
||||
value.v_object,
|
||||
OBJECT_KV_END_DEFINED);
|
||||
}
|
||||
|
||||
fx_value_unset(&value);
|
||||
return FX_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_result parse_table_header(
|
||||
struct ctx *ctx,
|
||||
fx_dict *container,
|
||||
fx_dict **new_container)
|
||||
fx_hashtable *container,
|
||||
fx_hashtable **new_container)
|
||||
{
|
||||
advance_token(ctx);
|
||||
struct token *tok = peek_token(ctx);
|
||||
@@ -2034,17 +2009,24 @@ static fx_result parse_table_header(
|
||||
}
|
||||
|
||||
while (tok && tok->tok_type == TOK_DOT) {
|
||||
fx_object *sub_dict = fx_dict_at_sk(container, key);
|
||||
enum object_flags flags = ctx_get_object_flags(ctx, sub_dict);
|
||||
if (!sub_dict) {
|
||||
sub_dict = (fx_dict_create());
|
||||
fx_dict_put_sk(container, key, FX_RV(sub_dict));
|
||||
} else if (fx_object_is_type(sub_dict, FX_TYPE_ARRAY)) {
|
||||
|
||||
sub_dict = fx_array_at(
|
||||
sub_dict,
|
||||
fx_array_size(sub_dict) - 1);
|
||||
} else if (!fx_object_is_type(sub_dict, FX_TYPE_DICT)) {
|
||||
const fx_value *value
|
||||
= fx_hashtable_get(container, &FX_VALUE_OBJECT(key));
|
||||
fx_object *subtable = NULL;
|
||||
fx_value_get_object(value, &subtable);
|
||||
enum object_flags flags = ctx_get_object_flags(ctx, subtable);
|
||||
if (!value) {
|
||||
subtable = (fx_hashtable_create());
|
||||
fx_hashtable_put(
|
||||
container,
|
||||
&FX_VALUE_OBJECT(key),
|
||||
&FX_VALUE_OBJECT(subtable));
|
||||
fx_object_unref(subtable);
|
||||
} else if (fx_value_is_type(value, FX_TYPE_ARRAY)) {
|
||||
value = fx_array_get_ref(
|
||||
subtable,
|
||||
fx_array_get_size(subtable) - 1);
|
||||
fx_value_get_object(value, &subtable);
|
||||
} else if (!fx_value_is_type(value, FX_TYPE_HASHTABLE)) {
|
||||
return FX_RESULT_ERR(BAD_FORMAT);
|
||||
}
|
||||
|
||||
@@ -2058,9 +2040,9 @@ static fx_result parse_table_header(
|
||||
return FX_RESULT_ERR(BAD_FORMAT);
|
||||
}
|
||||
|
||||
ctx_set_object_flags(ctx, sub_dict, OBJECT_HEADER_MID_DEFINED);
|
||||
ctx_set_object_flags(ctx, subtable, OBJECT_HEADER_MID_DEFINED);
|
||||
|
||||
container = sub_dict;
|
||||
container = subtable;
|
||||
fx_string_unref(key);
|
||||
key = fx_string_duplicate(tok->tok_str);
|
||||
if (!key) {
|
||||
@@ -2075,20 +2057,31 @@ static fx_result parse_table_header(
|
||||
return FX_RESULT_ERR(BAD_FORMAT);
|
||||
}
|
||||
|
||||
fx_dict *new_table = fx_dict_at_sk(container, key);
|
||||
const fx_value *child
|
||||
= fx_hashtable_get(container, &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_value_get_object(child, &new_table);
|
||||
|
||||
if (!new_table) {
|
||||
new_table = fx_dict_create();
|
||||
new_table = fx_hashtable_create();
|
||||
|
||||
if (!new_table) {
|
||||
free(key);
|
||||
fx_string_unref(key);
|
||||
return FX_RESULT_ERR(NO_MEMORY);
|
||||
}
|
||||
|
||||
fx_dict_put_sk(container, key, FX_RV(new_table));
|
||||
fx_hashtable_put(
|
||||
container,
|
||||
&FX_VALUE_OBJECT(key),
|
||||
&FX_VALUE_OBJECT(new_table));
|
||||
fx_hashtable_unref(new_table);
|
||||
}
|
||||
|
||||
if (!fx_object_is_type((new_table), FX_TYPE_DICT)) {
|
||||
if (!fx_object_is_type((new_table), FX_TYPE_HASHTABLE)) {
|
||||
return FX_RESULT_ERR(BAD_FORMAT);
|
||||
}
|
||||
|
||||
@@ -2109,8 +2102,8 @@ static fx_result parse_table_header(
|
||||
|
||||
static fx_result parse_array_header(
|
||||
struct ctx *ctx,
|
||||
fx_dict *container,
|
||||
fx_dict **new_container)
|
||||
fx_hashtable *container,
|
||||
fx_hashtable **new_container)
|
||||
{
|
||||
advance_token(ctx);
|
||||
struct token *tok = peek_token(ctx);
|
||||
@@ -2130,15 +2123,29 @@ static fx_result parse_array_header(
|
||||
}
|
||||
|
||||
while (tok && tok->tok_type == TOK_DOT) {
|
||||
fx_object *sub_dict = fx_dict_at_sk(container, key);
|
||||
if (!sub_dict) {
|
||||
sub_dict = (fx_dict_create());
|
||||
fx_dict_put_sk(container, key, FX_RV(sub_dict));
|
||||
} else if (fx_object_is_type(sub_dict, FX_TYPE_ARRAY)) {
|
||||
sub_dict = fx_array_at(
|
||||
const fx_value *child
|
||||
= fx_hashtable_get(container, &FX_VALUE_OBJECT(key));
|
||||
fx_object *sub_dict = NULL;
|
||||
fx_value_get_object(child, &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());
|
||||
fx_hashtable_put(
|
||||
container,
|
||||
&FX_VALUE_OBJECT(key),
|
||||
&FX_VALUE_OBJECT(sub_dict));
|
||||
fx_hashtable_unref(sub_dict);
|
||||
} else if (fx_value_is_type(child, FX_TYPE_ARRAY)) {
|
||||
child = fx_array_get_ref(
|
||||
sub_dict,
|
||||
fx_array_size(sub_dict) - 1);
|
||||
} else if (!fx_object_is_type(sub_dict, FX_TYPE_DICT)) {
|
||||
fx_array_get_size(sub_dict) - 1);
|
||||
sub_dict = child->v_object;
|
||||
} else if (!fx_value_is_type(child, FX_TYPE_HASHTABLE)) {
|
||||
return FX_RESULT_ERR(BAD_FORMAT);
|
||||
}
|
||||
|
||||
@@ -2163,37 +2170,48 @@ static fx_result parse_array_header(
|
||||
return FX_RESULT_ERR(BAD_FORMAT);
|
||||
}
|
||||
|
||||
fx_array *array = fx_dict_get_sk(container, key);
|
||||
const fx_value *child
|
||||
= fx_hashtable_get(container, &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_value_get_object(child, &array);
|
||||
|
||||
if (!array) {
|
||||
array = fx_array_create();
|
||||
fx_dict_put_sk(container, key, FX_RV(array));
|
||||
fx_hashtable_put(
|
||||
container,
|
||||
&FX_VALUE_OBJECT(key),
|
||||
&FX_VALUE_OBJECT(array));
|
||||
} else if (!fx_object_is_type(array, FX_TYPE_ARRAY)) {
|
||||
return FX_RESULT_ERR(BAD_FORMAT);
|
||||
}
|
||||
free(key);
|
||||
fx_string_unref(key);
|
||||
|
||||
enum object_flags flags = ctx_get_object_flags(ctx, (array));
|
||||
if (flags & OBJECT_KV_END_DEFINED) {
|
||||
return FX_RESULT_ERR(NO_MEMORY);
|
||||
return FX_RESULT_ERR(BAD_FORMAT);
|
||||
}
|
||||
|
||||
fx_dict *new_table = fx_dict_create();
|
||||
fx_hashtable *new_table = fx_hashtable_create();
|
||||
if (!new_table) {
|
||||
return FX_RESULT_ERR(NO_MEMORY);
|
||||
}
|
||||
|
||||
fx_array_append(array, FX_RV(new_table));
|
||||
fx_array_push_back(array, FX_VALUE_OBJECT(new_table));
|
||||
|
||||
advance_token(ctx);
|
||||
*new_container = new_table;
|
||||
return FX_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_result parse_root(struct ctx *ctx, fx_dict **out)
|
||||
static fx_result parse_root(struct ctx *ctx, fx_hashtable **out)
|
||||
{
|
||||
fx_result result = FX_RESULT_SUCCESS;
|
||||
fx_dict *root = fx_dict_create();
|
||||
fx_dict *current = root;
|
||||
fx_hashtable *root = fx_hashtable_create();
|
||||
fx_hashtable *current = root;
|
||||
|
||||
while (!(ctx->ctx_flags & CTX_EOF) && fx_result_is_success(result)) {
|
||||
struct token *tok = peek_token(ctx);
|
||||
@@ -2253,7 +2271,7 @@ static fx_result parse_root(struct ctx *ctx, fx_dict **out)
|
||||
}
|
||||
|
||||
if (fx_result_is_error(result)) {
|
||||
fx_dict_unref(root);
|
||||
fx_hashtable_unref(root);
|
||||
root = NULL;
|
||||
}
|
||||
|
||||
@@ -2264,7 +2282,7 @@ static fx_result parse_root(struct ctx *ctx, fx_dict **out)
|
||||
static fx_result toml_deserialise(
|
||||
fx_serial_ctx *serial,
|
||||
fx_stream *src,
|
||||
fx_object **dest,
|
||||
fx_value *dest,
|
||||
enum fx_serial_flags flags)
|
||||
{
|
||||
struct ctx ctx = {0};
|
||||
@@ -2285,17 +2303,17 @@ static fx_result toml_deserialise(
|
||||
}
|
||||
|
||||
if (ctx.ctx_flags & CTX_EOF) {
|
||||
*dest = (fx_dict_create());
|
||||
*dest = FX_VALUE_OBJECT(fx_hashtable_create());
|
||||
return FX_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
fx_dict *data = NULL;
|
||||
fx_hashtable *data = NULL;
|
||||
ctx.ctx_result = parse_root(&ctx, &data);
|
||||
if (fx_result_is_error(ctx.ctx_result)) {
|
||||
return fx_result_propagate(ctx.ctx_result);
|
||||
}
|
||||
|
||||
*dest = (data);
|
||||
*dest = FX_VALUE_OBJECT(data);
|
||||
#if 0
|
||||
ctx.ctx_flags
|
||||
= CTX_ENABLE_NUMBERS | CTX_ENABLE_TIMESTAMPS | CTX_ENABLE_BOOLS;
|
||||
|
||||
@@ -50,6 +50,11 @@ enum {
|
||||
FX_KEY_BACKSPACE,
|
||||
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_KEY_EOF = 0xFFFFFFFF,
|
||||
@@ -153,6 +158,7 @@ FX_API void fx_tty_set_vmode(
|
||||
struct fx_tty *tty,
|
||||
const struct fx_tty_vmode *vmode);
|
||||
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(
|
||||
struct fx_tty *tty,
|
||||
|
||||
+3
-7
@@ -102,8 +102,8 @@ static print_function format_printers[] = {
|
||||
[FX_PRINT_WARN] = print_warn,
|
||||
[FX_PRINT_ERR] = print_err,
|
||||
};
|
||||
static size_t nr_format_printers
|
||||
= sizeof format_printers / sizeof format_printers[FX_PRINT_NORMAL];
|
||||
static size_t nr_format_printers = sizeof format_printers
|
||||
/ sizeof format_printers[FX_PRINT_NORMAL];
|
||||
|
||||
fx_status fx_print(fx_print_format format, const char *str, ...)
|
||||
{
|
||||
@@ -140,11 +140,7 @@ int fx_printf(const char *format, ...)
|
||||
{
|
||||
va_list arg;
|
||||
va_start(arg, format);
|
||||
int x = fx_tty_vprintf(
|
||||
fx_stdtty,
|
||||
FX_TTY_DISABLE_INTERPOLATED_FORMATTING,
|
||||
format,
|
||||
arg);
|
||||
int x = fx_tty_vprintf(fx_stdtty, 0, format, arg);
|
||||
va_end(arg);
|
||||
return x;
|
||||
}
|
||||
|
||||
+14
-947
@@ -1,975 +1,42 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// \author (c) Marco Paland (info@paland.com)
|
||||
// 2014-2019, PALANDesign Hannover, Germany
|
||||
//
|
||||
// \license The MIT License (MIT)
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
//
|
||||
// \brief Tiny printf, sprintf and (v)snprintf implementation, optimized for speed on
|
||||
// embedded systems with a very limited resources. These routines are thread
|
||||
// safe and reentrant!
|
||||
// Use this instead of the bloated standard/newlib printf cause these use
|
||||
// malloc for printf (and may not be thread safe).
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "printf.h"
|
||||
|
||||
#include <fx/term/tty.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
// define this globally (e.g. gcc -DPRINTF_INCLUDE_CONFIG_H ...) to include the
|
||||
// printf_config.h header file
|
||||
// default: undefined
|
||||
#ifdef PRINTF_INCLUDE_CONFIG_H
|
||||
#include "printf_config.h"
|
||||
#endif
|
||||
|
||||
// 'ntoa' conversion buffer size, this must be big enough to hold one converted
|
||||
// numeric number including padded zeros (dynamically created on stack)
|
||||
// default: 32 byte
|
||||
#ifndef PRINTF_NTOA_BUFFER_SIZE
|
||||
#define PRINTF_NTOA_BUFFER_SIZE 32U
|
||||
#endif
|
||||
|
||||
// 'ftoa' conversion buffer size, this must be big enough to hold one converted
|
||||
// float number including padded zeros (dynamically created on stack)
|
||||
// default: 32 byte
|
||||
#ifndef PRINTF_FTOA_BUFFER_SIZE
|
||||
#define PRINTF_FTOA_BUFFER_SIZE 32U
|
||||
#endif
|
||||
|
||||
// support for the floating point type (%f)
|
||||
// default: activated
|
||||
#ifndef PRINTF_DISABLE_SUPPORT_FLOAT
|
||||
#define PRINTF_SUPPORT_FLOAT
|
||||
#endif
|
||||
|
||||
// support for exponential floating point notation (%e/%g)
|
||||
// default: activated
|
||||
#ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL
|
||||
#define PRINTF_SUPPORT_EXPONENTIAL
|
||||
#endif
|
||||
|
||||
// define the default floating point precision
|
||||
// default: 6 digits
|
||||
#ifndef PRINTF_DEFAULT_FLOAT_PRECISION
|
||||
#define PRINTF_DEFAULT_FLOAT_PRECISION 6U
|
||||
#endif
|
||||
|
||||
// define the largest float suitable to print with %f
|
||||
// default: 1e9
|
||||
#ifndef PRINTF_MAX_FLOAT
|
||||
#define PRINTF_MAX_FLOAT 1e9
|
||||
#endif
|
||||
|
||||
// support for the long long types (%llu or %p)
|
||||
// default: activated
|
||||
#ifndef PRINTF_DISABLE_SUPPORT_LONG_LONG
|
||||
#define PRINTF_SUPPORT_LONG_LONG
|
||||
#endif
|
||||
|
||||
// support for the ptrdiff_t type (%t)
|
||||
// ptrdiff_t is normally defined in <stddef.h> as long or long long type
|
||||
// default: activated
|
||||
#ifndef PRINTF_DISABLE_SUPPORT_PTRDIFF_T
|
||||
#define PRINTF_SUPPORT_PTRDIFF_T
|
||||
#endif
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// internal flag definitions
|
||||
#define FLAGS_ZEROPAD (1U << 0U)
|
||||
#define FLAGS_LEFT (1U << 1U)
|
||||
#define FLAGS_PLUS (1U << 2U)
|
||||
#define FLAGS_SPACE (1U << 3U)
|
||||
#define FLAGS_HASH (1U << 4U)
|
||||
#define FLAGS_UPPERCASE (1U << 5U)
|
||||
#define FLAGS_CHAR (1U << 6U)
|
||||
#define FLAGS_SHORT (1U << 7U)
|
||||
#define FLAGS_LONG (1U << 8U)
|
||||
#define FLAGS_LONG_LONG (1U << 9U)
|
||||
#define FLAGS_PRECISION (1U << 10U)
|
||||
#define FLAGS_ADAPT_EXP (1U << 11U)
|
||||
|
||||
// import float.h for DBL_MAX
|
||||
#if defined(PRINTF_SUPPORT_FLOAT)
|
||||
#include <float.h>
|
||||
#endif
|
||||
|
||||
struct out_tty_args {
|
||||
struct fx_tty *tty;
|
||||
enum fx_tty_print_flags flags;
|
||||
bool format_ch;
|
||||
};
|
||||
|
||||
// output function type
|
||||
typedef void (*out_fct_type)(char character, struct out_tty_args *args);
|
||||
|
||||
// wrapper (used as buffer) for output function type
|
||||
typedef struct {
|
||||
void (*fct)(char character, void *arg);
|
||||
void *arg;
|
||||
} out_fct_wrap_type;
|
||||
|
||||
// internal buffer output
|
||||
static inline void _out_buffer(
|
||||
char character, void *buffer, size_t idx, size_t maxlen)
|
||||
{
|
||||
if (idx < maxlen) {
|
||||
((char *)buffer)[idx] = character;
|
||||
}
|
||||
}
|
||||
|
||||
// internal null output
|
||||
static inline void _out_null(char character, void *buffer, size_t idx, size_t maxlen)
|
||||
{
|
||||
(void)character;
|
||||
(void)buffer;
|
||||
(void)idx;
|
||||
(void)maxlen;
|
||||
}
|
||||
|
||||
// internal output function wrapper
|
||||
static inline void _out_fct(char character, void *buffer, size_t idx, size_t maxlen)
|
||||
{
|
||||
(void)idx;
|
||||
(void)maxlen;
|
||||
if (character) {
|
||||
// buffer is the output fct pointer
|
||||
((out_fct_wrap_type *)buffer)
|
||||
->fct(character, ((out_fct_wrap_type *)buffer)->arg);
|
||||
}
|
||||
}
|
||||
|
||||
// internal secure strlen
|
||||
// \return The length of the string (excluding the terminating 0) limited by 'maxsize'
|
||||
static inline unsigned int _strnlen_s(const char *str, size_t maxsize)
|
||||
{
|
||||
const char *s;
|
||||
for (s = str; *s && maxsize--; ++s)
|
||||
;
|
||||
return (unsigned int)(s - str);
|
||||
}
|
||||
|
||||
// internal test if char is a digit (0-9)
|
||||
// \return true if char is a digit
|
||||
static inline bool _is_digit(char ch)
|
||||
{
|
||||
return (ch >= '0') && (ch <= '9');
|
||||
}
|
||||
|
||||
// internal ASCII string to unsigned int conversion
|
||||
static unsigned int _atoi(const char **str)
|
||||
{
|
||||
unsigned int i = 0U;
|
||||
while (_is_digit(**str)) {
|
||||
i = i * 10U + (unsigned int)(*((*str)++) - '0');
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
// output the specified string in reverse, taking care of any zero-padding
|
||||
static size_t _out_rev(
|
||||
out_fct_type out, struct out_tty_args *args, const char *buf,
|
||||
size_t len, unsigned int width, unsigned int flags)
|
||||
{
|
||||
size_t idx = 0;
|
||||
|
||||
// pad spaces up to given width
|
||||
if (!(flags & FLAGS_LEFT) && !(flags & FLAGS_ZEROPAD)) {
|
||||
for (size_t i = len; i < width; i++) {
|
||||
out(' ', args);
|
||||
}
|
||||
}
|
||||
|
||||
// reverse string
|
||||
while (len) {
|
||||
out(buf[--len], args);
|
||||
}
|
||||
|
||||
// append pad spaces up to given width
|
||||
if (flags & FLAGS_LEFT) {
|
||||
while (idx < width) {
|
||||
out(' ', args);
|
||||
idx++;
|
||||
}
|
||||
}
|
||||
|
||||
return idx;
|
||||
}
|
||||
|
||||
// internal itoa format
|
||||
static size_t _ntoa_format(
|
||||
out_fct_type out, struct out_tty_args *args, char *buf, size_t len,
|
||||
bool negative, unsigned int base, unsigned int prec, unsigned int width,
|
||||
unsigned int flags)
|
||||
{
|
||||
// pad leading zeros
|
||||
if (!(flags & FLAGS_LEFT)) {
|
||||
if (width && (flags & FLAGS_ZEROPAD)
|
||||
&& (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) {
|
||||
width--;
|
||||
}
|
||||
while ((len < prec) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
|
||||
buf[len++] = '0';
|
||||
}
|
||||
while ((flags & FLAGS_ZEROPAD) && (len < width)
|
||||
&& (len < PRINTF_NTOA_BUFFER_SIZE)) {
|
||||
buf[len++] = '0';
|
||||
}
|
||||
}
|
||||
|
||||
// handle hash
|
||||
if (flags & FLAGS_HASH) {
|
||||
if (!(flags & FLAGS_PRECISION) && len
|
||||
&& ((len == prec) || (len == width))) {
|
||||
len--;
|
||||
if (len && (base == 16U)) {
|
||||
len--;
|
||||
}
|
||||
}
|
||||
if ((base == 16U) && !(flags & FLAGS_UPPERCASE)
|
||||
&& (len < PRINTF_NTOA_BUFFER_SIZE)) {
|
||||
buf[len++] = 'x';
|
||||
} else if (
|
||||
(base == 16U) && (flags & FLAGS_UPPERCASE)
|
||||
&& (len < PRINTF_NTOA_BUFFER_SIZE)) {
|
||||
buf[len++] = 'X';
|
||||
} else if ((base == 2U) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
|
||||
buf[len++] = 'b';
|
||||
}
|
||||
if (len < PRINTF_NTOA_BUFFER_SIZE) {
|
||||
buf[len++] = '0';
|
||||
}
|
||||
}
|
||||
|
||||
if (len < PRINTF_NTOA_BUFFER_SIZE) {
|
||||
if (negative) {
|
||||
buf[len++] = '-';
|
||||
} else if (flags & FLAGS_PLUS) {
|
||||
buf[len++] = '+'; // ignore the space if the '+' exists
|
||||
} else if (flags & FLAGS_SPACE) {
|
||||
buf[len++] = ' ';
|
||||
}
|
||||
}
|
||||
|
||||
return _out_rev(out, args, buf, len, width, flags);
|
||||
}
|
||||
|
||||
// internal itoa for 'long' type
|
||||
static size_t _ntoa_long(
|
||||
out_fct_type out, struct out_tty_args *args, unsigned long value,
|
||||
bool negative, unsigned long base, unsigned int prec,
|
||||
unsigned int width, unsigned int flags)
|
||||
{
|
||||
char buf[PRINTF_NTOA_BUFFER_SIZE];
|
||||
size_t len = 0U;
|
||||
|
||||
// no hash for 0 values
|
||||
if (!value) {
|
||||
flags &= ~FLAGS_HASH;
|
||||
}
|
||||
|
||||
// write if precision != 0 and value is != 0
|
||||
if (!(flags & FLAGS_PRECISION) || value) {
|
||||
do {
|
||||
const char digit = (char)(value % base);
|
||||
buf[len++] = digit < 10
|
||||
? '0' + digit
|
||||
: (flags & FLAGS_UPPERCASE ? 'A' : 'a')
|
||||
+ digit - 10;
|
||||
value /= base;
|
||||
} while (value && (len < PRINTF_NTOA_BUFFER_SIZE));
|
||||
}
|
||||
|
||||
return _ntoa_format(
|
||||
out, args, buf, len, negative, (unsigned int)base, prec, width,
|
||||
flags);
|
||||
}
|
||||
|
||||
// internal itoa for 'long long' type
|
||||
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
||||
static size_t _ntoa_long_long(
|
||||
out_fct_type out, struct out_tty_args *args, unsigned long long value,
|
||||
bool negative, unsigned long long base, unsigned int prec,
|
||||
unsigned int width, unsigned int flags)
|
||||
{
|
||||
char buf[PRINTF_NTOA_BUFFER_SIZE];
|
||||
size_t len = 0U;
|
||||
|
||||
// no hash for 0 values
|
||||
if (!value) {
|
||||
flags &= ~FLAGS_HASH;
|
||||
}
|
||||
|
||||
// write if precision != 0 and value is != 0
|
||||
if (!(flags & FLAGS_PRECISION) || value) {
|
||||
do {
|
||||
const char digit = (char)(value % base);
|
||||
buf[len++] = digit < 10
|
||||
? '0' + digit
|
||||
: (flags & FLAGS_UPPERCASE ? 'A' : 'a')
|
||||
+ digit - 10;
|
||||
value /= base;
|
||||
} while (value && (len < PRINTF_NTOA_BUFFER_SIZE));
|
||||
}
|
||||
|
||||
return _ntoa_format(
|
||||
out, args, buf, len, negative, (unsigned int)base, prec, width,
|
||||
flags);
|
||||
}
|
||||
#endif // PRINTF_SUPPORT_LONG_LONG
|
||||
|
||||
#if defined(PRINTF_SUPPORT_FLOAT)
|
||||
|
||||
#if defined(PRINTF_SUPPORT_EXPONENTIAL)
|
||||
// forward declaration so that _ftoa can switch to exp notation for values > PRINTF_MAX_FLOAT
|
||||
static size_t _etoa(
|
||||
out_fct_type out, struct out_tty_args *args, double value,
|
||||
unsigned int prec, unsigned int width, unsigned int flags);
|
||||
#endif
|
||||
|
||||
// internal ftoa for fixed decimal floating point
|
||||
static size_t _ftoa(
|
||||
out_fct_type out, struct out_tty_args *args, double value,
|
||||
unsigned int prec, unsigned int width, unsigned int flags)
|
||||
{
|
||||
char buf[PRINTF_FTOA_BUFFER_SIZE];
|
||||
size_t len = 0U;
|
||||
double diff = 0.0;
|
||||
|
||||
// powers of 10
|
||||
static const double pow10[]
|
||||
= {1, 10, 100, 1000, 10000,
|
||||
100000, 1000000, 10000000, 100000000, 1000000000};
|
||||
|
||||
// test for special values
|
||||
if (value != value)
|
||||
return _out_rev(out, args, "nan", 3, width, flags);
|
||||
if (value < -DBL_MAX)
|
||||
return _out_rev(out, args, "fni-", 4, width, flags);
|
||||
if (value > DBL_MAX)
|
||||
return _out_rev(
|
||||
out, args, (flags & FLAGS_PLUS) ? "fni+" : "fni",
|
||||
(flags & FLAGS_PLUS) ? 4U : 3U, width, flags);
|
||||
|
||||
// test for very large values
|
||||
// standard printf behavior is to print EVERY whole number digit --
|
||||
// which could be 100s of characters overflowing your buffers == bad
|
||||
if ((value > PRINTF_MAX_FLOAT) || (value < -PRINTF_MAX_FLOAT)) {
|
||||
#if defined(PRINTF_SUPPORT_EXPONENTIAL)
|
||||
return _etoa(out, args, value, prec, width, flags);
|
||||
#else
|
||||
return 0U;
|
||||
#endif
|
||||
}
|
||||
|
||||
// test for negative
|
||||
bool negative = false;
|
||||
if (value < 0) {
|
||||
negative = true;
|
||||
value = 0 - value;
|
||||
}
|
||||
|
||||
// set default precision, if not set explicitly
|
||||
if (!(flags & FLAGS_PRECISION)) {
|
||||
prec = PRINTF_DEFAULT_FLOAT_PRECISION;
|
||||
}
|
||||
// limit precision to 9, cause a prec >= 10 can lead to overflow errors
|
||||
while ((len < PRINTF_FTOA_BUFFER_SIZE) && (prec > 9U)) {
|
||||
buf[len++] = '0';
|
||||
prec--;
|
||||
}
|
||||
|
||||
int whole = (int)value;
|
||||
double tmp = (value - whole) * pow10[prec];
|
||||
unsigned long frac = (unsigned long)tmp;
|
||||
diff = tmp - frac;
|
||||
|
||||
if (diff > 0.5) {
|
||||
++frac;
|
||||
// handle rollover, e.g. case 0.99 with prec 1 is 1.0
|
||||
if (frac >= pow10[prec]) {
|
||||
frac = 0;
|
||||
++whole;
|
||||
}
|
||||
} else if (diff < 0.5) {
|
||||
} else if ((frac == 0U) || (frac & 1U)) {
|
||||
// if halfway, round up if odd OR if last digit is 0
|
||||
++frac;
|
||||
}
|
||||
|
||||
if (prec == 0U) {
|
||||
diff = value - (double)whole;
|
||||
if ((!(diff < 0.5) || (diff > 0.5)) && (whole & 1)) {
|
||||
// exactly 0.5 and ODD, then round up
|
||||
// 1.5 -> 2, but 2.5 -> 2
|
||||
++whole;
|
||||
}
|
||||
} else {
|
||||
unsigned int count = prec;
|
||||
// now do fractional part, as an unsigned number
|
||||
while (len < PRINTF_FTOA_BUFFER_SIZE) {
|
||||
--count;
|
||||
buf[len++] = (char)(48U + (frac % 10U));
|
||||
if (!(frac /= 10U)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// add extra 0s
|
||||
while ((len < PRINTF_FTOA_BUFFER_SIZE) && (count-- > 0U)) {
|
||||
buf[len++] = '0';
|
||||
}
|
||||
if (len < PRINTF_FTOA_BUFFER_SIZE) {
|
||||
// add decimal
|
||||
buf[len++] = '.';
|
||||
}
|
||||
}
|
||||
|
||||
// do whole part, number is reversed
|
||||
while (len < PRINTF_FTOA_BUFFER_SIZE) {
|
||||
buf[len++] = (char)(48 + (whole % 10));
|
||||
if (!(whole /= 10)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// pad leading zeros
|
||||
if (!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD)) {
|
||||
if (width && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) {
|
||||
width--;
|
||||
}
|
||||
while ((len < width) && (len < PRINTF_FTOA_BUFFER_SIZE)) {
|
||||
buf[len++] = '0';
|
||||
}
|
||||
}
|
||||
|
||||
if (len < PRINTF_FTOA_BUFFER_SIZE) {
|
||||
if (negative) {
|
||||
buf[len++] = '-';
|
||||
} else if (flags & FLAGS_PLUS) {
|
||||
buf[len++] = '+'; // ignore the space if the '+' exists
|
||||
} else if (flags & FLAGS_SPACE) {
|
||||
buf[len++] = ' ';
|
||||
}
|
||||
}
|
||||
|
||||
return _out_rev(out, args, buf, len, width, flags);
|
||||
}
|
||||
|
||||
#if defined(PRINTF_SUPPORT_EXPONENTIAL)
|
||||
// internal ftoa variant for exponential floating-point type, contributed by Martijn Jasperse <m.jasperse@gmail.com>
|
||||
static size_t _etoa(
|
||||
out_fct_type out, struct out_tty_args *args, double value,
|
||||
unsigned int prec, unsigned int width, unsigned int flags)
|
||||
{
|
||||
// check for NaN and special values
|
||||
if ((value != value) || (value > DBL_MAX) || (value < -DBL_MAX)) {
|
||||
return _ftoa(out, args, value, prec, width, flags);
|
||||
}
|
||||
|
||||
// determine the sign
|
||||
const bool negative = value < 0;
|
||||
if (negative) {
|
||||
value = -value;
|
||||
}
|
||||
|
||||
// default precision
|
||||
if (!(flags & FLAGS_PRECISION)) {
|
||||
prec = PRINTF_DEFAULT_FLOAT_PRECISION;
|
||||
}
|
||||
|
||||
// determine the decimal exponent
|
||||
// based on the algorithm by David Gay (https://www.ampl.com/netlib/fp/dtoa.c)
|
||||
union {
|
||||
uint64_t U;
|
||||
double F;
|
||||
} conv;
|
||||
|
||||
conv.F = value;
|
||||
int exp2 = (int)((conv.U >> 52U) & 0x07FFU) - 1023; // effectively log2
|
||||
conv.U = (conv.U & ((1ULL << 52U) - 1U))
|
||||
| (1023ULL << 52U); // drop the exponent so conv.F is now in [1,2)
|
||||
// now approximate log10 from the log2 integer part and an expansion of ln around 1.5
|
||||
int expval = (int)(0.1760912590558 + exp2 * 0.301029995663981
|
||||
+ (conv.F - 1.5) * 0.289529654602168);
|
||||
// now we want to compute 10^expval but we want to be sure it won't overflow
|
||||
exp2 = (int)(expval * 3.321928094887362 + 0.5);
|
||||
const double z = expval * 2.302585092994046 - exp2 * 0.6931471805599453;
|
||||
const double z2 = z * z;
|
||||
conv.U = (uint64_t)(exp2 + 1023) << 52U;
|
||||
// compute exp(z) using continued fractions, see https://en.wikipedia.org/wiki/Exponential_function#Continued_fractions_for_ex
|
||||
conv.F *= 1 + 2 * z / (2 - z + (z2 / (6 + (z2 / (10 + z2 / 14)))));
|
||||
// correct for rounding errors
|
||||
if (value < conv.F) {
|
||||
expval--;
|
||||
conv.F /= 10;
|
||||
}
|
||||
|
||||
// the exponent format is "%+03d" and largest value is "307", so set aside 4-5 characters
|
||||
unsigned int minwidth = ((expval < 100) && (expval > -100)) ? 4U : 5U;
|
||||
|
||||
// in "%g" mode, "prec" is the number of *significant figures* not decimals
|
||||
if (flags & FLAGS_ADAPT_EXP) {
|
||||
// do we want to fall-back to "%f" mode?
|
||||
if ((value >= 1e-4) && (value < 1e6)) {
|
||||
if ((int)prec > expval) {
|
||||
prec = (unsigned)((int)prec - expval - 1);
|
||||
} else {
|
||||
prec = 0;
|
||||
}
|
||||
flags |= FLAGS_PRECISION; // make sure _ftoa respects precision
|
||||
// no characters in exponent
|
||||
minwidth = 0U;
|
||||
expval = 0;
|
||||
} else {
|
||||
// we use one sigfig for the whole part
|
||||
if ((prec > 0) && (flags & FLAGS_PRECISION)) {
|
||||
--prec;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// will everything fit?
|
||||
unsigned int fwidth = width;
|
||||
if (width > minwidth) {
|
||||
// we didn't fall-back so subtract the characters required for the exponent
|
||||
fwidth -= minwidth;
|
||||
} else {
|
||||
// not enough characters, so go back to default sizing
|
||||
fwidth = 0U;
|
||||
}
|
||||
if ((flags & FLAGS_LEFT) && minwidth) {
|
||||
// if we're padding on the right, DON'T pad the floating part
|
||||
fwidth = 0U;
|
||||
}
|
||||
|
||||
// rescale the float value
|
||||
if (expval) {
|
||||
value /= conv.F;
|
||||
}
|
||||
|
||||
// output the floating part
|
||||
size_t idx
|
||||
= _ftoa(out, args, negative ? -value : value, prec, fwidth,
|
||||
flags & ~FLAGS_ADAPT_EXP);
|
||||
|
||||
// output the exponent part
|
||||
if (minwidth) {
|
||||
// output the exponential symbol
|
||||
out((flags & FLAGS_UPPERCASE) ? 'E' : 'e', args);
|
||||
// output the exponent value
|
||||
idx = _ntoa_long(
|
||||
out, args, (expval < 0) ? -expval : expval, expval < 0,
|
||||
10, 0, minwidth - 1, FLAGS_ZEROPAD | FLAGS_PLUS);
|
||||
// might need to right-pad spaces
|
||||
if (flags & FLAGS_LEFT) {
|
||||
while (idx < width) {
|
||||
out(' ', args);
|
||||
idx++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return idx;
|
||||
}
|
||||
#endif // PRINTF_SUPPORT_EXPONENTIAL
|
||||
#endif // PRINTF_SUPPORT_FLOAT
|
||||
|
||||
#define set_format_ch(args) ((args)->format_ch = true)
|
||||
#define unset_format_ch(args) ((args)->format_ch = false)
|
||||
|
||||
// internal vsnprintf
|
||||
static int _vsnprintf(
|
||||
out_fct_type out, struct out_tty_args *args, const char *format, va_list va)
|
||||
{
|
||||
unsigned int flags, width, precision, n;
|
||||
size_t idx = 0U;
|
||||
|
||||
while (*format) {
|
||||
// format specifier? %[flags][width][.precision][length]
|
||||
if (*format != '%') {
|
||||
// no
|
||||
set_format_ch(args);
|
||||
out(*format, args);
|
||||
unset_format_ch(args);
|
||||
format++;
|
||||
continue;
|
||||
} else {
|
||||
// yes, evaluate it
|
||||
format++;
|
||||
}
|
||||
|
||||
// evaluate flags
|
||||
flags = 0U;
|
||||
do {
|
||||
switch (*format) {
|
||||
case '0':
|
||||
flags |= FLAGS_ZEROPAD;
|
||||
format++;
|
||||
n = 1U;
|
||||
break;
|
||||
case '-':
|
||||
flags |= FLAGS_LEFT;
|
||||
format++;
|
||||
n = 1U;
|
||||
break;
|
||||
case '+':
|
||||
flags |= FLAGS_PLUS;
|
||||
format++;
|
||||
n = 1U;
|
||||
break;
|
||||
case ' ':
|
||||
flags |= FLAGS_SPACE;
|
||||
format++;
|
||||
n = 1U;
|
||||
break;
|
||||
case '#':
|
||||
flags |= FLAGS_HASH;
|
||||
format++;
|
||||
n = 1U;
|
||||
break;
|
||||
default:
|
||||
n = 0U;
|
||||
break;
|
||||
}
|
||||
} while (n);
|
||||
|
||||
// evaluate width field
|
||||
width = 0U;
|
||||
if (_is_digit(*format)) {
|
||||
width = _atoi(&format);
|
||||
} else if (*format == '*') {
|
||||
const int w = va_arg(va, int);
|
||||
if (w < 0) {
|
||||
flags |= FLAGS_LEFT; // reverse padding
|
||||
width = (unsigned int)-w;
|
||||
} else {
|
||||
width = (unsigned int)w;
|
||||
}
|
||||
format++;
|
||||
}
|
||||
|
||||
// evaluate precision field
|
||||
precision = 0U;
|
||||
if (*format == '.') {
|
||||
flags |= FLAGS_PRECISION;
|
||||
format++;
|
||||
if (_is_digit(*format)) {
|
||||
precision = _atoi(&format);
|
||||
} else if (*format == '*') {
|
||||
const int prec = (int)va_arg(va, int);
|
||||
precision = prec > 0 ? (unsigned int)prec : 0U;
|
||||
format++;
|
||||
}
|
||||
}
|
||||
|
||||
// evaluate length field
|
||||
switch (*format) {
|
||||
case 'l':
|
||||
flags |= FLAGS_LONG;
|
||||
format++;
|
||||
if (*format == 'l') {
|
||||
flags |= FLAGS_LONG_LONG;
|
||||
format++;
|
||||
}
|
||||
break;
|
||||
case 'h':
|
||||
flags |= FLAGS_SHORT;
|
||||
format++;
|
||||
if (*format == 'h') {
|
||||
flags |= FLAGS_CHAR;
|
||||
format++;
|
||||
}
|
||||
break;
|
||||
#if defined(PRINTF_SUPPORT_PTRDIFF_T)
|
||||
case 't':
|
||||
flags
|
||||
|= (sizeof(ptrdiff_t) == sizeof(long)
|
||||
? FLAGS_LONG
|
||||
: FLAGS_LONG_LONG);
|
||||
format++;
|
||||
break;
|
||||
#endif
|
||||
case 'j':
|
||||
flags
|
||||
|= (sizeof(intmax_t) == sizeof(long)
|
||||
? FLAGS_LONG
|
||||
: FLAGS_LONG_LONG);
|
||||
format++;
|
||||
break;
|
||||
case 'z':
|
||||
flags
|
||||
|= (sizeof(size_t) == sizeof(long)
|
||||
? FLAGS_LONG
|
||||
: FLAGS_LONG_LONG);
|
||||
format++;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// evaluate specifier
|
||||
switch (*format) {
|
||||
case 'd':
|
||||
case 'i':
|
||||
case 'u':
|
||||
case 'x':
|
||||
case 'X':
|
||||
case 'o':
|
||||
case 'b': {
|
||||
// set the base
|
||||
unsigned int base;
|
||||
if (*format == 'x' || *format == 'X') {
|
||||
base = 16U;
|
||||
} else if (*format == 'o') {
|
||||
base = 8U;
|
||||
} else if (*format == 'b') {
|
||||
base = 2U;
|
||||
} else {
|
||||
base = 10U;
|
||||
flags &= ~FLAGS_HASH; // no hash for dec format
|
||||
}
|
||||
// uppercase
|
||||
if (*format == 'X') {
|
||||
flags |= FLAGS_UPPERCASE;
|
||||
}
|
||||
|
||||
// no plus or space flag for u, x, X, o, b
|
||||
if ((*format != 'i') && (*format != 'd')) {
|
||||
flags &= ~(FLAGS_PLUS | FLAGS_SPACE);
|
||||
}
|
||||
|
||||
// ignore '0' flag when precision is given
|
||||
if (flags & FLAGS_PRECISION) {
|
||||
flags &= ~FLAGS_ZEROPAD;
|
||||
}
|
||||
|
||||
// convert the integer
|
||||
if ((*format == 'i') || (*format == 'd')) {
|
||||
// signed
|
||||
if (flags & FLAGS_LONG_LONG) {
|
||||
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
||||
const long long value
|
||||
= va_arg(va, long long);
|
||||
idx = _ntoa_long_long(
|
||||
out, args,
|
||||
(unsigned long long)(value > 0 ? value
|
||||
: 0 - value),
|
||||
value < 0, base, precision,
|
||||
width, flags);
|
||||
#endif
|
||||
} else if (flags & FLAGS_LONG) {
|
||||
const long value = va_arg(va, long);
|
||||
idx = _ntoa_long(
|
||||
out, args,
|
||||
(unsigned long)(value > 0 ? value
|
||||
: 0 - value),
|
||||
value < 0, base, precision,
|
||||
width, flags);
|
||||
} else {
|
||||
const int value
|
||||
= (flags & FLAGS_CHAR)
|
||||
? (char)va_arg(va, int)
|
||||
: (flags & FLAGS_SHORT)
|
||||
? (short int)va_arg(va, int)
|
||||
: va_arg(va, int);
|
||||
idx = _ntoa_long(
|
||||
out, args,
|
||||
(unsigned int)(value > 0 ? value
|
||||
: 0 - value),
|
||||
value < 0, base, precision,
|
||||
width, flags);
|
||||
}
|
||||
} else {
|
||||
// unsigned
|
||||
if (flags & FLAGS_LONG_LONG) {
|
||||
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
||||
idx = _ntoa_long_long(
|
||||
out, args,
|
||||
va_arg(va, unsigned long long),
|
||||
false, base, precision, width,
|
||||
flags);
|
||||
#endif
|
||||
} else if (flags & FLAGS_LONG) {
|
||||
idx = _ntoa_long(
|
||||
out, args,
|
||||
va_arg(va, unsigned long), false,
|
||||
base, precision, width, flags);
|
||||
} else {
|
||||
const unsigned int value
|
||||
= (flags & FLAGS_CHAR)
|
||||
? (unsigned char)va_arg(
|
||||
va, unsigned int)
|
||||
: (flags & FLAGS_SHORT)
|
||||
? (unsigned short int)va_arg(
|
||||
va, unsigned int)
|
||||
: va_arg(va, unsigned int);
|
||||
idx = _ntoa_long(
|
||||
out, args, value, false, base,
|
||||
precision, width, flags);
|
||||
}
|
||||
}
|
||||
format++;
|
||||
break;
|
||||
}
|
||||
#if defined(PRINTF_SUPPORT_FLOAT)
|
||||
case 'f':
|
||||
case 'F':
|
||||
if (*format == 'F')
|
||||
flags |= FLAGS_UPPERCASE;
|
||||
idx
|
||||
= _ftoa(out, args, va_arg(va, double),
|
||||
precision, width, flags);
|
||||
format++;
|
||||
break;
|
||||
#if defined(PRINTF_SUPPORT_EXPONENTIAL)
|
||||
case 'e':
|
||||
case 'E':
|
||||
case 'g':
|
||||
case 'G':
|
||||
if ((*format == 'g') || (*format == 'G'))
|
||||
flags |= FLAGS_ADAPT_EXP;
|
||||
if ((*format == 'E') || (*format == 'G'))
|
||||
flags |= FLAGS_UPPERCASE;
|
||||
idx
|
||||
= _etoa(out, args, va_arg(va, double),
|
||||
precision, width, flags);
|
||||
format++;
|
||||
break;
|
||||
#endif // PRINTF_SUPPORT_EXPONENTIAL
|
||||
#endif // PRINTF_SUPPORT_FLOAT
|
||||
case 'c': {
|
||||
unsigned int l = 1U;
|
||||
// pre padding
|
||||
if (!(flags & FLAGS_LEFT)) {
|
||||
while (l++ < width) {
|
||||
out(' ', args);
|
||||
}
|
||||
}
|
||||
// char output
|
||||
out((char)va_arg(va, int), args);
|
||||
// post padding
|
||||
if (flags & FLAGS_LEFT) {
|
||||
while (l++ < width) {
|
||||
out(' ', args);
|
||||
}
|
||||
}
|
||||
format++;
|
||||
break;
|
||||
}
|
||||
|
||||
case 's': {
|
||||
const char *p = va_arg(va, char *);
|
||||
unsigned int l = _strnlen_s(
|
||||
p, precision ? precision : (size_t)-1);
|
||||
// pre padding
|
||||
if (flags & FLAGS_PRECISION) {
|
||||
l = (l < precision ? l : precision);
|
||||
}
|
||||
if (!(flags & FLAGS_LEFT)) {
|
||||
while (l++ < width) {
|
||||
out(' ', args);
|
||||
}
|
||||
}
|
||||
// string output
|
||||
while ((*p != 0)
|
||||
&& (!(flags & FLAGS_PRECISION) || precision--)) {
|
||||
out(*(p++), args);
|
||||
}
|
||||
// post padding
|
||||
if (flags & FLAGS_LEFT) {
|
||||
while (l++ < width) {
|
||||
out(' ', args);
|
||||
}
|
||||
}
|
||||
format++;
|
||||
break;
|
||||
}
|
||||
|
||||
case 'p': {
|
||||
width = sizeof(void *) * 2U;
|
||||
flags |= FLAGS_ZEROPAD | FLAGS_UPPERCASE;
|
||||
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
||||
const bool is_ll = sizeof(uintptr_t) == sizeof(long long);
|
||||
if (is_ll) {
|
||||
idx = _ntoa_long_long(
|
||||
out, args, (uintptr_t)va_arg(va, void *),
|
||||
false, 16U, precision, width, flags);
|
||||
} else {
|
||||
#endif
|
||||
idx = _ntoa_long(
|
||||
out, args,
|
||||
(unsigned long)((uintptr_t)va_arg(
|
||||
va, void *)),
|
||||
false, 16U, precision, width, flags);
|
||||
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
||||
}
|
||||
#endif
|
||||
format++;
|
||||
break;
|
||||
}
|
||||
|
||||
case '%':
|
||||
set_format_ch(args);
|
||||
out('%', args);
|
||||
unset_format_ch(args);
|
||||
format++;
|
||||
break;
|
||||
|
||||
default:
|
||||
set_format_ch(args);
|
||||
out(*format, args);
|
||||
unset_format_ch(args);
|
||||
format++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// termination
|
||||
out((char)0, args);
|
||||
|
||||
// return written chars without terminating \0
|
||||
return (int)idx;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static void out_tty(char c, struct out_tty_args *args)
|
||||
static void out_tty(char c, void *argp)
|
||||
{
|
||||
if (c == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
struct out_tty_args *args = argp;
|
||||
enum fx_tty_print_flags flags = args->flags;
|
||||
|
||||
if (!args->format_ch && (flags & FX_TTY_DISABLE_INTERPOLATED_FORMATTING)) {
|
||||
if (!args->format_ch
|
||||
&& (flags & FX_TTY_DISABLE_INTERPOLATED_FORMATTING)) {
|
||||
flags |= FX_TTY_DISABLE_FORMATTING;
|
||||
}
|
||||
|
||||
fx_tty_putc(args->tty, flags, c);
|
||||
}
|
||||
|
||||
extern int z__fx_fctprintf(
|
||||
void (*out)(char c, void *extra_arg),
|
||||
void *extra_arg,
|
||||
const char *format,
|
||||
va_list arg);
|
||||
|
||||
int fx_tty_vprintf(
|
||||
struct fx_tty *tty, enum fx_tty_print_flags flags, const char *format,
|
||||
struct fx_tty *tty,
|
||||
enum fx_tty_print_flags flags,
|
||||
const char *format,
|
||||
va_list args)
|
||||
{
|
||||
struct out_tty_args tty_args = {
|
||||
@@ -977,6 +44,6 @@ int fx_tty_vprintf(
|
||||
.tty = tty,
|
||||
};
|
||||
|
||||
const int ret = _vsnprintf(out_tty, &tty_args, format, args);
|
||||
const int ret = z__fx_fctprintf(out_tty, &tty_args, format, args);
|
||||
return ret;
|
||||
}
|
||||
|
||||
+91
-8
@@ -1,5 +1,8 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
|
||||
#include "../../tty.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <fx/term/tty.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -101,6 +104,7 @@ static void init_tty(struct fx_tty *tty, FILE *in, FILE *out)
|
||||
|
||||
tcgetattr(fd, &tty->t_ios_default);
|
||||
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_oflag &= ~(OPOST);
|
||||
@@ -205,8 +209,14 @@ void fx_tty_reset_vmode(struct fx_tty *tty)
|
||||
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(
|
||||
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) {
|
||||
return -1;
|
||||
@@ -243,7 +253,9 @@ static int compare_colour(
|
||||
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) {
|
||||
return -1;
|
||||
@@ -271,7 +283,9 @@ static void put_ansi_attrib(struct fx_tty *tty, const char *s)
|
||||
}
|
||||
|
||||
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) {
|
||||
return;
|
||||
@@ -309,7 +323,8 @@ static void set_attrib(
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if (compare_colour(old, new) == 0) {
|
||||
@@ -345,7 +360,8 @@ static void set_fg(
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if (compare_colour(old, new) == 0) {
|
||||
@@ -458,7 +474,10 @@ fx_keycode fx_tty_read_key(struct fx_tty *tty)
|
||||
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) {
|
||||
for (int i = 0; i > pos; i--) {
|
||||
@@ -487,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) {
|
||||
/* we don't need this functionality right now */
|
||||
@@ -528,7 +550,9 @@ 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)
|
||||
struct fx_tty *tty,
|
||||
unsigned int *w,
|
||||
unsigned int *h)
|
||||
{
|
||||
if (!(tty->t_flags & FX_TTY_INTERACTIVE)) {
|
||||
return -1;
|
||||
@@ -550,3 +574,62 @@ enum fx_status fx_tty_get_dimensions(
|
||||
|
||||
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
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "../../tty.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <fx/term/tty.h>
|
||||
#include <stdio.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);
|
||||
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_oflag &= ~(OPOST);
|
||||
@@ -207,8 +209,14 @@ void fx_tty_reset_vmode(struct fx_tty *tty)
|
||||
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(
|
||||
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) {
|
||||
return -1;
|
||||
@@ -245,7 +253,9 @@ static int compare_colour(
|
||||
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) {
|
||||
return -1;
|
||||
@@ -273,45 +283,48 @@ static void put_ansi_attrib(struct fx_tty *tty, const char *s)
|
||||
}
|
||||
|
||||
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) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* 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);
|
||||
}
|
||||
|
||||
/* 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);
|
||||
}
|
||||
|
||||
/* 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);
|
||||
}
|
||||
|
||||
/* 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);
|
||||
}
|
||||
|
||||
/* 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);
|
||||
}
|
||||
|
||||
/* 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);
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if (compare_colour(old, new) == 0) {
|
||||
@@ -347,7 +360,8 @@ static void set_fg(
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if (compare_colour(old, new) == 0) {
|
||||
@@ -460,7 +474,10 @@ fx_keycode fx_tty_read_key(struct fx_tty *tty)
|
||||
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) {
|
||||
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) {
|
||||
/* 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(
|
||||
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)) {
|
||||
return -1;
|
||||
@@ -552,3 +574,62 @@ enum fx_status fx_tty_get_dimensions(
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user