Compare commits
51 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fa9572d33a | |||
| 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 | |||
| 5ead9118b3 |
+1
-1
@@ -45,7 +45,7 @@ SpacesBeforeTrailingComments: 3
|
|||||||
TabWidth: 8
|
TabWidth: 8
|
||||||
UseTab: AlignWithSpaces
|
UseTab: AlignWithSpaces
|
||||||
BreakAfterReturnType: Automatic
|
BreakAfterReturnType: Automatic
|
||||||
PenaltyBreakAssignment: 1000000
|
PenaltyBreakAssignment: 0
|
||||||
PenaltyReturnTypeOnItsOwnLine: 100000000
|
PenaltyReturnTypeOnItsOwnLine: 100000000
|
||||||
PenaltyExcessCharacter: 10000000
|
PenaltyExcessCharacter: 10000000
|
||||||
PenaltyBreakOpenParenthesis: 0
|
PenaltyBreakOpenParenthesis: 0
|
||||||
|
|||||||
+18
-7
@@ -1,12 +1,24 @@
|
|||||||
cmake_minimum_required(VERSION 3.25)
|
cmake_minimum_required(VERSION 3.25)
|
||||||
project(fx C ASM)
|
project(fx C ASM)
|
||||||
|
|
||||||
|
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
|
||||||
include (TestBigEndian)
|
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_STANDARD 99)
|
||||||
set(CMAKE_C_EXTENSIONS OFF)
|
set(CMAKE_C_EXTENSIONS OFF)
|
||||||
|
|
||||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake)
|
|
||||||
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
|
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
|
||||||
|
|
||||||
set(fx_source_root ${CMAKE_CURRENT_SOURCE_DIR})
|
set(fx_source_root ${CMAKE_CURRENT_SOURCE_DIR})
|
||||||
@@ -33,10 +45,9 @@ endif ()
|
|||||||
|
|
||||||
message(STATUS "Floating point support: ${fx_enable_floating_point}")
|
message(STATUS "Floating point support: ${fx_enable_floating_point}")
|
||||||
|
|
||||||
set(fx_system_name ${CMAKE_SYSTEM_NAME})
|
get_platform_details(
|
||||||
string(TOLOWER ${fx_system_name} fx_system_name)
|
SYSTEM fx_system_name
|
||||||
set(fx_system_arch ${CMAKE_SYSTEM_PROCESSOR})
|
PROCESSOR fx_system_arch)
|
||||||
string(TOLOWER ${fx_system_arch} fx_system_arch)
|
|
||||||
|
|
||||||
message(STATUS "Target system: ${fx_system_name}-${fx_system_arch}")
|
message(STATUS "Target system: ${fx_system_name}-${fx_system_arch}")
|
||||||
|
|
||||||
@@ -44,10 +55,10 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
|
|||||||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
|
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
|
||||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin)
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin)
|
||||||
|
|
||||||
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Templates.cmake)
|
|
||||||
|
|
||||||
foreach (assembly ${fx_assemblies})
|
foreach (assembly ${fx_assemblies})
|
||||||
add_subdirectory(assemblies/${assembly})
|
add_subdirectory(assemblies/${assembly})
|
||||||
endforeach (assembly)
|
endforeach (assembly)
|
||||||
|
|
||||||
|
if (fx_enable_tests)
|
||||||
add_subdirectory(test)
|
add_subdirectory(test)
|
||||||
|
endif ()
|
||||||
|
|||||||
@@ -7,8 +7,7 @@ FX_ASSEMBLY_BEGIN()
|
|||||||
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "array", fx_array);
|
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "array", fx_array);
|
||||||
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "bitbuffer", fx_bitbuffer);
|
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "bitbuffer", fx_bitbuffer);
|
||||||
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "bitmap", fx_bitmap);
|
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", "hashmap", fx_hashmap);
|
||||||
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "list", fx_list);
|
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "list", fx_list);
|
||||||
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "tree", fx_tree);
|
|
||||||
FX_ASSEMBLY_END()
|
FX_ASSEMBLY_END()
|
||||||
|
|||||||
+43
-108
@@ -49,43 +49,37 @@ if (FX_STATIC)
|
|||||||
set(_lib_suffix "-s")
|
set(_lib_suffix "-s")
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
set(supported_components Core Ds Term Cmd Io Serial Compress)
|
set(assemblies ${FX_FIND_COMPONENTS})
|
||||||
set(components ${FX_FIND_COMPONENTS})
|
|
||||||
string(REPLACE ";" ", " supported_components_string_list "${supported_components}")
|
|
||||||
|
|
||||||
if (NOT components)
|
|
||||||
set(components ${supported_components})
|
|
||||||
endif ()
|
|
||||||
|
|
||||||
set(required_vars)
|
set(required_vars)
|
||||||
|
|
||||||
foreach (component ${components})
|
if (NOT FX_INCLUDE_DIR)
|
||||||
if (NOT "${component}" IN_LIST supported_components)
|
find_path(FX_INCLUDE_DIR
|
||||||
message(FATAL_ERROR "'${component}' is not a valid FX module.\nSupported modules: ${supported_components_string_list}")
|
NAMES fx/misc.h ${FX_FIND_ARGS}
|
||||||
endif ()
|
|
||||||
|
|
||||||
string(TOLOWER ${component} header_name)
|
|
||||||
set(lib_name ${header_name}${_lib_suffix})
|
|
||||||
|
|
||||||
if (NOT FX_${component}_INCLUDE_DIR)
|
|
||||||
find_path(FX_${component}_INCLUDE_DIR
|
|
||||||
NAMES fx/${header_name}.h ${FX_FIND_ARGS}
|
|
||||||
PATH_SUFFIXES include
|
PATH_SUFFIXES include
|
||||||
PATHS ${FX_SEARCH_PATHS})
|
PATHS ${FX_SEARCH_PATHS})
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
if (NOT FX_${component}_LIBRARY)
|
set(required_vars FX_INCLUDE_DIR)
|
||||||
find_library(FX_${component}_LIBRARY
|
|
||||||
NAMES fx-${lib_name} ${FX_FIND_ARGS}
|
foreach (assembly ${assemblies})
|
||||||
|
string(TOLOWER ${assembly} header_name)
|
||||||
|
string(REPLACE "." "_" macro_name ${assembly})
|
||||||
|
string(TOUPPER ${macro_name} macro_name)
|
||||||
|
|
||||||
|
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
|
PATH_SUFFIXES lib
|
||||||
PATHS ${FX_SEARCH_PATHS})
|
PATHS ${FX_SEARCH_PATHS})
|
||||||
else ()
|
else ()
|
||||||
# on Windows, ensure paths are in canonical format (forward slahes):
|
# on Windows, ensure paths are in canonical format (forward slahes):
|
||||||
file(TO_CMAKE_PATH "${FX_${component}_LIBRARY}" FX_${component}_LIBRARY)
|
file(TO_CMAKE_PATH "${${macro_name}_LIBRARY}" ${macro_name}_LIBRARY)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
list(APPEND required_vars FX_${component}_INCLUDE_DIR FX_${component}_LIBRARY)
|
list(APPEND required_vars ${macro_name}_LIBRARY)
|
||||||
endforeach (component)
|
endforeach (assembly)
|
||||||
|
|
||||||
unset(FX_FIND_ARGS)
|
unset(FX_FIND_ARGS)
|
||||||
|
|
||||||
@@ -96,94 +90,35 @@ find_package_handle_standard_args(FX
|
|||||||
|
|
||||||
if (FX_FOUND)
|
if (FX_FOUND)
|
||||||
set(created_targets)
|
set(created_targets)
|
||||||
foreach (component ${components})
|
foreach (assembly ${assemblies})
|
||||||
string(TOLOWER ${component} header_name)
|
set(target_name ${assembly})
|
||||||
set(lib_name ${header_name}${_lib_suffix})
|
string(REPLACE "fx." "" target_name ${target_name})
|
||||||
|
string(SUBSTRING ${target_name} 0 1 target_name_prefix)
|
||||||
|
string(TOUPPER ${target_name_prefix} target_name_prefix)
|
||||||
|
string(SUBSTRING ${target_name} 1 -1 target_name_suffix)
|
||||||
|
set(target_name ${target_name_prefix}${target_name_suffix})
|
||||||
|
|
||||||
if(NOT TARGET FX::${component})
|
string(TOLOWER ${assembly} header_name)
|
||||||
add_library(FX::${component} UNKNOWN IMPORTED)
|
string(REPLACE "." "_" macro_name ${assembly})
|
||||||
set_target_properties(FX::${component} PROPERTIES
|
string(REPLACE "." "_" macro_name ${assembly})
|
||||||
INTERFACE_INCLUDE_DIRECTORIES "${FX_${component}_INCLUDE_DIR}")
|
string(TOUPPER ${macro_name} macro_name)
|
||||||
target_compile_definitions(FX::${component} INTERFACE _CRT_SECURE_NO_WARNINGS=1)
|
|
||||||
|
set(lib_name ${assembly}${_lib_suffix})
|
||||||
|
|
||||||
|
if (NOT TARGET FX::${target_name})
|
||||||
|
add_library(FX::${target_name} UNKNOWN IMPORTED)
|
||||||
|
set_target_properties(FX::${target_name} PROPERTIES
|
||||||
|
INTERFACE_INCLUDE_DIRECTORIES "${FX_INCLUDE_DIR}")
|
||||||
|
target_compile_definitions(FX::${target_name} INTERFACE _CRT_SECURE_NO_WARNINGS=1)
|
||||||
|
|
||||||
if (FX_STATIC)
|
if (FX_STATIC)
|
||||||
target_compile_definitions(FX::${component} INTERFACE FX_STATIC=1)
|
target_compile_definitions(FX::${target_name} INTERFACE FX_STATIC=1)
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
set_target_properties(FX::${component} PROPERTIES
|
set_target_properties(FX::${target_name} PROPERTIES
|
||||||
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
|
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
|
||||||
IMPORTED_LOCATION "${FX_${component}_LIBRARY}")
|
IMPORTED_LOCATION "${${macro_name}_LIBRARY}")
|
||||||
set(created_targets ${created_targets} ${component})
|
set(created_targets ${created_targets} ${assembly})
|
||||||
endif ()
|
endif ()
|
||||||
endforeach (component)
|
endforeach (assembly)
|
||||||
|
|
||||||
foreach (component ${created_targets})
|
|
||||||
if ("${component}" STREQUAL "Ds")
|
|
||||||
if (NOT TARGET FX::Core)
|
|
||||||
message(FATAL_ERROR "FX: Module 'Ds' depends on 'Core', which was not specified in find_package()")
|
|
||||||
endif ()
|
|
||||||
|
|
||||||
target_link_libraries(FX::Ds INTERFACE FX::Core)
|
|
||||||
endif ()
|
|
||||||
|
|
||||||
if ("${component}" STREQUAL "Term")
|
|
||||||
if (NOT TARGET FX::Core)
|
|
||||||
message(FATAL_ERROR "FX: Module 'Term' depends on 'Core', which was not specified in find_package()")
|
|
||||||
endif ()
|
|
||||||
|
|
||||||
if (NOT TARGET FX::Ds)
|
|
||||||
message(FATAL_ERROR "FX: Module 'Term' depends on 'Ds', which was not specified in find_package()")
|
|
||||||
endif ()
|
|
||||||
|
|
||||||
target_link_libraries(FX::Term INTERFACE FX::Core FX::Ds)
|
|
||||||
endif ()
|
|
||||||
|
|
||||||
if ("${component}" STREQUAL "Serial")
|
|
||||||
if (NOT TARGET FX::Core)
|
|
||||||
message(FATAL_ERROR "FX: Module 'Serial' depends on 'Core', which was not specified in find_package()")
|
|
||||||
endif ()
|
|
||||||
|
|
||||||
if (NOT TARGET FX::Ds)
|
|
||||||
message(FATAL_ERROR "FX: Module 'Serial' depends on 'Ds', which was not specified in find_package()")
|
|
||||||
endif ()
|
|
||||||
|
|
||||||
target_link_libraries(FX::Serial INTERFACE FX::Core FX::Ds)
|
|
||||||
endif ()
|
|
||||||
|
|
||||||
if ("${component}" STREQUAL "Cmd")
|
|
||||||
if (NOT TARGET FX::Core)
|
|
||||||
message(FATAL_ERROR "FX: Module 'Cmd' depends on 'Core', which was not specified in find_package()")
|
|
||||||
endif ()
|
|
||||||
|
|
||||||
if (NOT TARGET FX::Ds)
|
|
||||||
message(FATAL_ERROR "FX: Module 'Cmd' depends on 'Ds', which was not specified in find_package()")
|
|
||||||
endif ()
|
|
||||||
|
|
||||||
if (NOT TARGET FX::Term)
|
|
||||||
message(FATAL_ERROR "FX: Module 'Cmd' depends on 'Term', which was not specified in find_package()")
|
|
||||||
endif ()
|
|
||||||
|
|
||||||
target_link_libraries(FX::Cmd INTERFACE FX::Core FX::Ds FX::Term)
|
|
||||||
endif ()
|
|
||||||
|
|
||||||
if ("${component}" STREQUAL "Io")
|
|
||||||
if (NOT TARGET FX::Core)
|
|
||||||
message(FATAL_ERROR "FX: Module 'Io' depends on 'Core', which was not specified in find_package()")
|
|
||||||
endif ()
|
|
||||||
|
|
||||||
if (NOT TARGET FX::Ds)
|
|
||||||
message(FATAL_ERROR "FX: Module 'Io' depends on 'Ds', which was not specified in find_package()")
|
|
||||||
endif ()
|
|
||||||
|
|
||||||
target_link_libraries(FX::Io INTERFACE FX::Core FX::Ds)
|
|
||||||
endif ()
|
|
||||||
|
|
||||||
if ("${component}" STREQUAL "Compress")
|
|
||||||
if (NOT TARGET FX::Core)
|
|
||||||
message(FATAL_ERROR "FX: Module 'Compress' depends on 'Core', which was not specified in find_package()")
|
|
||||||
endif ()
|
|
||||||
|
|
||||||
target_link_libraries(FX::Compress INTERFACE FX::Core FX::Ds)
|
|
||||||
endif ()
|
|
||||||
endforeach (component)
|
|
||||||
endif()
|
endif()
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
function(get_platform_details)
|
||||||
|
set(options)
|
||||||
|
set(one_value_args SYSTEM PROCESSOR)
|
||||||
|
set(multi_value_args
|
||||||
|
NAMESPACES
|
||||||
|
DEPENDENCIES
|
||||||
|
LIBS)
|
||||||
|
cmake_parse_arguments(PARSE_ARGV 0 arg
|
||||||
|
"${options}"
|
||||||
|
"${one_value_args}"
|
||||||
|
"${multi_value_args}")
|
||||||
|
|
||||||
|
set(system_name "")
|
||||||
|
|
||||||
|
if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
|
||||||
|
set(system_name "linux")
|
||||||
|
elseif ("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
|
||||||
|
set(system_name "apple")
|
||||||
|
elseif ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
|
||||||
|
set(system_name "win32")
|
||||||
|
else ()
|
||||||
|
string(TOLOWER "${CMAKE_SYSTEM_NAME}" system_name)
|
||||||
|
endif ()
|
||||||
|
|
||||||
|
if ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64")
|
||||||
|
set(system_arch "amd64")
|
||||||
|
elseif ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "arm64")
|
||||||
|
set(system_arch "aarch64")
|
||||||
|
else ()
|
||||||
|
string(TOLOWER "${CMAKE_SYSTEM_PROCESSOR}" system_arch)
|
||||||
|
endif ()
|
||||||
|
|
||||||
|
set(${arg_SYSTEM} ${system_name} PARENT_SCOPE)
|
||||||
|
set(${arg_PROCESSOR} ${system_arch} PARENT_SCOPE)
|
||||||
|
endfunction (get_platform_details)
|
||||||
|
|
||||||
+6
-10
@@ -18,10 +18,12 @@ function(add_fx_assembly)
|
|||||||
|
|
||||||
set(assembly_include_paths "")
|
set(assembly_include_paths "")
|
||||||
set(assembly_sources ${CMAKE_CURRENT_SOURCE_DIR}/assembly.c)
|
set(assembly_sources ${CMAKE_CURRENT_SOURCE_DIR}/assembly.c)
|
||||||
|
set(assembly_headers)
|
||||||
|
|
||||||
foreach (dir ${arg_NAMESPACES})
|
foreach (dir ${arg_NAMESPACES})
|
||||||
add_subdirectory(${fx_source_root}/${dir} ${fx_build_root}/namespaces/${dir})
|
add_subdirectory(${fx_source_root}/${dir} ${fx_build_root}/namespaces/${dir})
|
||||||
set(assembly_sources ${assembly_sources} ${namespace_sources})
|
set(assembly_sources ${assembly_sources} ${namespace_sources})
|
||||||
|
set(assembly_headers ${assembly_headers} ${namespace_headers})
|
||||||
set(assembly_include_paths ${assembly_include_paths} ${namespace_include_paths})
|
set(assembly_include_paths ${assembly_include_paths} ${namespace_include_paths})
|
||||||
endforeach (dir)
|
endforeach (dir)
|
||||||
|
|
||||||
@@ -45,6 +47,7 @@ function(add_fx_assembly)
|
|||||||
target_compile_definitions(${assembly_target_name} PRIVATE ${def})
|
target_compile_definitions(${assembly_target_name} PRIVATE ${def})
|
||||||
endforeach (def)
|
endforeach (def)
|
||||||
|
|
||||||
|
if (fx_enable_tests)
|
||||||
foreach (ns ${arg_NAMESPACES})
|
foreach (ns ${arg_NAMESPACES})
|
||||||
file(GLOB test_sources ${fx_source_root}/${ns}/test/*.c)
|
file(GLOB test_sources ${fx_source_root}/${ns}/test/*.c)
|
||||||
foreach (test_file ${test_sources})
|
foreach (test_file ${test_sources})
|
||||||
@@ -55,21 +58,12 @@ function(add_fx_assembly)
|
|||||||
target_link_libraries(${test_name} ${assembly_target_name})
|
target_link_libraries(${test_name} ${assembly_target_name})
|
||||||
endforeach (test_file)
|
endforeach (test_file)
|
||||||
endforeach (ns)
|
endforeach (ns)
|
||||||
|
endif ()
|
||||||
|
|
||||||
set_target_properties(${assembly_target_name} PROPERTIES
|
set_target_properties(${assembly_target_name} PROPERTIES
|
||||||
FOLDER "${assembly_name}")
|
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(TARGETS ${assembly_target_name})
|
||||||
install(FILES ${headers} DESTINATION include/${assembly_path})
|
|
||||||
endfunction(add_fx_assembly)
|
endfunction(add_fx_assembly)
|
||||||
|
|
||||||
macro(export_fx_namespace_details ns_name)
|
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}/*.h
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/sys/${fx_system_name}/${fx_system_arch}/*.S)
|
${CMAKE_CURRENT_SOURCE_DIR}/sys/${fx_system_name}/${fx_system_arch}/*.S)
|
||||||
file(GLOB headers include/${namespace_path}/*.h)
|
file(GLOB headers include/${namespace_path}/*.h)
|
||||||
|
set(namespace_headers ${headers} PARENT_SCOPE)
|
||||||
set(namespace_sources
|
set(namespace_sources
|
||||||
${namespace_sources}
|
${namespace_sources}
|
||||||
${base_namespace_sources}
|
${base_namespace_sources}
|
||||||
@@ -110,5 +105,6 @@ macro(export_fx_namespace_details ns_name)
|
|||||||
PARENT_SCOPE)
|
PARENT_SCOPE)
|
||||||
set(internal_libs ${internal_libs} PARENT_SCOPE)
|
set(internal_libs ${internal_libs} PARENT_SCOPE)
|
||||||
set(internal_defines ${internal_defines} PARENT_SCOPE)
|
set(internal_defines ${internal_defines} PARENT_SCOPE)
|
||||||
|
install(FILES ${headers} DESTINATION include/${namespace_path})
|
||||||
endmacro(export_fx_namespace_details)
|
endmacro(export_fx_namespace_details)
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
#include "command.h"
|
#include "command.h"
|
||||||
|
|
||||||
#include <fx/cmd.h>
|
#include <fx/cmdline/cmd.h>
|
||||||
#include <fx/string.h>
|
#include <fx/string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#include "command.h"
|
#include "command.h"
|
||||||
|
|
||||||
#include <fx/cmd.h>
|
#include <fx/cmdline/cmd.h>
|
||||||
#include <fx/misc.h>
|
#include <fx/misc.h>
|
||||||
#include <fx/string.h>
|
#include <fx/string.h>
|
||||||
#include <fx/term/print.h>
|
#include <fx/term/print.h>
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#include "command.h"
|
#include "command.h"
|
||||||
|
|
||||||
#include <fx/bst.h>
|
#include <fx/bst.h>
|
||||||
#include <fx/cmd.h>
|
#include <fx/cmdline/cmd.h>
|
||||||
#include <fx/string.h>
|
#include <fx/string.h>
|
||||||
#include <fx/term/print.h>
|
#include <fx/term/print.h>
|
||||||
#include <fx/term/tty.h>
|
#include <fx/term/tty.h>
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
#define _FX_COMMAND_H_
|
#define _FX_COMMAND_H_
|
||||||
|
|
||||||
#include <fx/bst.h>
|
#include <fx/bst.h>
|
||||||
#include <fx/cmd.h>
|
#include <fx/cmdline/cmd.h>
|
||||||
#include <fx/queue.h>
|
#include <fx/queue.h>
|
||||||
#include <fx/string.h>
|
#include <fx/string.h>
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
#include "command.h"
|
#include "command.h"
|
||||||
|
|
||||||
#include <fx/cmd.h>
|
#include <fx/cmdline/cmd.h>
|
||||||
#include <fx/string.h>
|
#include <fx/string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#include <fx/cmd.h>
|
#include <fx/cmdline/cmd.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
@@ -27,16 +27,24 @@ const char *text
|
|||||||
"great pleasure.";
|
"great pleasure.";
|
||||||
|
|
||||||
static int test_command(
|
static int test_command(
|
||||||
const fx_command *self, const fx_arglist *opt, const fx_array *args)
|
const fx_command *self,
|
||||||
|
const fx_arglist *opt,
|
||||||
|
const fx_array *args)
|
||||||
{
|
{
|
||||||
printf("Hello, world!\n");
|
printf("Hello, world!\n");
|
||||||
|
|
||||||
fx_arglist_iterator it;
|
fx_arglist_iterator it;
|
||||||
fx_arglist_iterator_begin(
|
fx_arglist_iterator_begin(
|
||||||
opt, FX_COMMAND_INVALID_ID, FX_COMMAND_INVALID_ID, &it);
|
opt,
|
||||||
|
FX_COMMAND_INVALID_ID,
|
||||||
|
FX_COMMAND_INVALID_ID,
|
||||||
|
&it);
|
||||||
while (fx_arglist_iterator_is_valid(&it)) {
|
while (fx_arglist_iterator_is_valid(&it)) {
|
||||||
printf("opt:%u,arg:%u,i:%zu,value: %s\n", it.opt_id,
|
printf("opt:%u,arg:%u,i:%zu,value: %s\n",
|
||||||
it.value->val_id, it.i, it.value->val_str);
|
it.opt_id,
|
||||||
|
it.value->val_id,
|
||||||
|
it.i,
|
||||||
|
it.value->val_str);
|
||||||
|
|
||||||
fx_arglist_iterator_next(&it);
|
fx_arglist_iterator_next(&it);
|
||||||
}
|
}
|
||||||
|
|||||||
+89
-111
@@ -2,6 +2,7 @@
|
|||||||
#include <fx/iterator.h>
|
#include <fx/iterator.h>
|
||||||
#include <fx/stream.h>
|
#include <fx/stream.h>
|
||||||
#include <fx/string.h>
|
#include <fx/string.h>
|
||||||
|
#include <fx/value.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@@ -12,17 +13,17 @@ struct fx_array_p {
|
|||||||
size_t ar_len;
|
size_t ar_len;
|
||||||
/* maximum number of items that can currently be stored in array */
|
/* maximum number of items that can currently be stored in array */
|
||||||
size_t ar_cap;
|
size_t ar_cap;
|
||||||
fx_object **ar_data;
|
fx_value *ar_data;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct fx_array_iterator_p {
|
struct fx_array_iterator_p {
|
||||||
fx_array *_a;
|
const fx_array *_a;
|
||||||
struct fx_array_p *_a_p;
|
struct fx_array_p *_a_p;
|
||||||
|
|
||||||
/** The index of the current value */
|
/** The index of the current value */
|
||||||
size_t i;
|
size_t i;
|
||||||
/** The current value */
|
/** The current value */
|
||||||
fx_object *value;
|
fx_value *value;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*** PRIVATE FUNCTIONS ********************************************************/
|
/*** 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) {
|
if (array->ar_cap < new_capacity) {
|
||||||
void *new_data = realloc(
|
void *new_data = realloc(
|
||||||
array->ar_data,
|
array->ar_data,
|
||||||
new_capacity * sizeof(struct fx_dsref *));
|
new_capacity * sizeof(fx_value));
|
||||||
if (!new_data) {
|
if (!new_data) {
|
||||||
return FX_ERR_NO_MEMORY;
|
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;
|
array->ar_data = new_data;
|
||||||
} else {
|
} else {
|
||||||
for (size_t i = new_capacity; i < array->ar_len; i++) {
|
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(
|
void *new_data = realloc(
|
||||||
array->ar_data,
|
array->ar_data,
|
||||||
new_capacity * sizeof(struct fx_dsref *));
|
new_capacity * sizeof(fx_value));
|
||||||
if (!new_data) {
|
if (!new_data) {
|
||||||
return FX_ERR_NO_MEMORY;
|
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(
|
static fx_status array_insert(
|
||||||
struct fx_array_p *array,
|
struct fx_array_p *array,
|
||||||
fx_object *value,
|
fx_value value,
|
||||||
size_t at)
|
size_t at)
|
||||||
{
|
{
|
||||||
if (at == FX_NPOS) {
|
if (at == FX_NPOS) {
|
||||||
@@ -84,13 +85,13 @@ static fx_status array_insert(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fx_object **src = array->ar_data + at;
|
fx_value *src = array->ar_data + at;
|
||||||
fx_object **dest = src + 1;
|
fx_value *dest = src + 1;
|
||||||
size_t move_len = (array->ar_len - at) * sizeof(struct fx_dsref *);
|
size_t move_len = (array->ar_len - at) * sizeof(fx_value);
|
||||||
|
|
||||||
memmove(dest, src, move_len);
|
memmove(dest, src, move_len);
|
||||||
|
|
||||||
array->ar_data[at] = fx_object_ref(value);
|
fx_value_copy(&array->ar_data[at], &value);
|
||||||
array->ar_len++;
|
array->ar_len++;
|
||||||
|
|
||||||
return FX_SUCCESS;
|
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;
|
return FX_ERR_OUT_OF_BOUNDS;
|
||||||
}
|
}
|
||||||
|
|
||||||
fx_object **src = array->ar_data + at;
|
fx_value *src = array->ar_data + at;
|
||||||
fx_object **dest = src + 1;
|
fx_value *dest = src + 1;
|
||||||
size_t move_len = array->ar_len * sizeof(struct fx_dsref *);
|
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);
|
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);
|
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) {
|
if (at >= array->ar_len) {
|
||||||
return NULL;
|
return FX_VALUE_EMPTY;
|
||||||
}
|
}
|
||||||
|
|
||||||
fx_object **src = array->ar_data + at;
|
fx_value *dest = array->ar_data + at;
|
||||||
fx_object **dest = src + 1;
|
fx_value *src = dest + 1;
|
||||||
size_t move_len = array->ar_len * sizeof(struct fx_dsref *);
|
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);
|
memmove(dest, src, move_len);
|
||||||
|
|
||||||
@@ -144,40 +145,40 @@ static fx_object *array_pop(struct fx_array_p *array, size_t at)
|
|||||||
return out;
|
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);
|
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);
|
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) {
|
if (at >= array->ar_len) {
|
||||||
return NULL;
|
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) {
|
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;
|
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;
|
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++) {
|
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;
|
array->ar_len = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*** PUBLIC FUNCTIONS *********************************************************/
|
/*** PUBLIC FUNCTIONS *********************************************************/
|
||||||
|
|
||||||
fx_array *fx_array_create_with_values(
|
fx_array *fx_array_create_with_values(const fx_value *values, size_t nr_values)
|
||||||
fx_object *const *values,
|
|
||||||
size_t nr_values)
|
|
||||||
{
|
{
|
||||||
fx_array *array = fx_array_create();
|
fx_array *array = fx_array_create();
|
||||||
if (!array) {
|
if (!array) {
|
||||||
@@ -211,14 +210,14 @@ fx_array *fx_array_create_with_values(
|
|||||||
|
|
||||||
size_t real_nr_values = 0;
|
size_t real_nr_values = 0;
|
||||||
for (size_t i = 0; i < nr_values; i++) {
|
for (size_t i = 0; i < nr_values; i++) {
|
||||||
if (values[i]) {
|
if (fx_value_is_set(&values[i])) {
|
||||||
real_nr_values++;
|
real_nr_values++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
p->ar_len = real_nr_values;
|
p->ar_len = real_nr_values;
|
||||||
p->ar_cap = 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) {
|
if (!p->ar_data) {
|
||||||
fx_array_unref(array);
|
fx_array_unref(array);
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -226,13 +225,13 @@ fx_array *fx_array_create_with_values(
|
|||||||
|
|
||||||
size_t index = 0;
|
size_t index = 0;
|
||||||
for (size_t i = 0; i < nr_values; i++) {
|
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;
|
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);
|
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_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_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_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_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);
|
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)
|
void fx_array_clear(fx_array *array)
|
||||||
@@ -294,12 +293,12 @@ void fx_array_clear(fx_array *array)
|
|||||||
|
|
||||||
/*** PUBLIC ALIAS FUNCTIONS ***************************************************/
|
/*** 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);
|
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);
|
return fx_array_insert(array, value, 0);
|
||||||
}
|
}
|
||||||
@@ -317,7 +316,7 @@ static void array_fini(fx_object *obj, void *priv)
|
|||||||
|
|
||||||
if (array->ar_data) {
|
if (array->ar_data) {
|
||||||
for (size_t i = 0; i < array->ar_len; i++) {
|
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);
|
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) {
|
if (!array->ar_len) {
|
||||||
fx_stream_write_cstr(out, "[]", NULL);
|
fx_stream_write_cstr(out, "[]", NULL);
|
||||||
return;
|
return FX_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
fx_stream_write_cstr(out, "[\n", NULL);
|
fx_stream_write_cstr(out, "[\n", NULL);
|
||||||
|
|
||||||
fx_stream_push_indent(out, 1);
|
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++) {
|
for (size_t i = 0; i < array->ar_len; i++) {
|
||||||
fx_object *value = array->ar_data[i];
|
fx_value *value = &array->ar_data[i];
|
||||||
bool is_string = fx_object_is_type(value, FX_TYPE_STRING);
|
bool is_string = fx_value_is_type(value, FX_TYPE_STRING);
|
||||||
|
|
||||||
if (is_string) {
|
if (is_string) {
|
||||||
fx_stream_write_char(out, '"');
|
fx_stream_write_char(out, '"');
|
||||||
}
|
}
|
||||||
|
|
||||||
fx_object_to_string(value, out);
|
fx_value_to_string(value, out, NULL);
|
||||||
|
|
||||||
if (is_string) {
|
if (is_string) {
|
||||||
fx_stream_write_char(out, '"');
|
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_pop_indent(out);
|
||||||
fx_stream_write_char(out, ']');
|
fx_stream_write_char(out, ']');
|
||||||
|
return FX_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*** ITERATOR FUNCTIONS *******************************************************/
|
/*** ITERATOR FUNCTIONS *******************************************************/
|
||||||
|
|
||||||
static fx_iterator *iterable_begin(fx_object *obj)
|
static const fx_iterator *iterable_begin(const fx_object *obj)
|
||||||
{
|
{
|
||||||
fx_array_iterator *it_obj = fx_object_create(FX_TYPE_ARRAY_ITERATOR);
|
fx_array_iterator *it_obj = fx_object_create(FX_TYPE_ARRAY_ITERATOR);
|
||||||
struct fx_array_iterator_p *it
|
struct fx_array_iterator_p *it = fx_object_get_private(
|
||||||
= fx_object_get_private(it_obj, FX_TYPE_ARRAY_ITERATOR);
|
it_obj,
|
||||||
|
FX_TYPE_ARRAY_ITERATOR);
|
||||||
it->_a = obj;
|
it->_a = obj;
|
||||||
it->_a_p = fx_object_get_private(obj, FX_TYPE_ARRAY);
|
it->_a_p = fx_object_get_private(obj, FX_TYPE_ARRAY);
|
||||||
it->i = 0;
|
it->i = 0;
|
||||||
|
|
||||||
if (it->_a_p->ar_len > 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
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];
|
|
||||||
} else {
|
} else {
|
||||||
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
||||||
it->value = NULL;
|
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)
|
static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||||
{
|
{
|
||||||
struct fx_array_iterator_p *it
|
struct fx_array_iterator_p *it = fx_object_get_private(
|
||||||
= fx_object_get_private(obj, FX_TYPE_ARRAY_ITERATOR);
|
obj,
|
||||||
|
FX_TYPE_ARRAY_ITERATOR);
|
||||||
struct fx_array_p *array = it->_a_p;
|
struct fx_array_p *array = it->_a_p;
|
||||||
|
|
||||||
if (it->value == NULL || it->i >= array->ar_len) {
|
if (it->value == NULL || it->i >= array->ar_len) {
|
||||||
return false;
|
return FX_ERR_NO_DATA;
|
||||||
}
|
}
|
||||||
|
|
||||||
it->i++;
|
it->i++;
|
||||||
@@ -419,7 +407,7 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
|
|||||||
if (it->i >= array->ar_len) {
|
if (it->i >= array->ar_len) {
|
||||||
it->value = NULL;
|
it->value = NULL;
|
||||||
} else {
|
} 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;
|
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)
|
static enum fx_status iterator_erase(fx_iterator *obj)
|
||||||
{
|
{
|
||||||
struct fx_array_iterator_p *it
|
struct fx_array_iterator_p *it = fx_object_get_private(
|
||||||
= fx_object_get_private(obj, FX_TYPE_ARRAY_ITERATOR);
|
obj,
|
||||||
|
FX_TYPE_ARRAY_ITERATOR);
|
||||||
struct fx_array_p *array = it->_a_p;
|
struct fx_array_p *array = it->_a_p;
|
||||||
|
|
||||||
if (it->i >= array->ar_len) {
|
if (it->i >= array->ar_len) {
|
||||||
return FX_ERR_OUT_OF_BOUNDS;
|
return FX_ERR_OUT_OF_BOUNDS;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (array->ar_data[it->i] != it->value) {
|
|
||||||
return FX_ERR_BAD_STATE;
|
|
||||||
}
|
|
||||||
|
|
||||||
array_remove(array, it->i);
|
array_remove(array, it->i);
|
||||||
|
|
||||||
if (it->i < array->ar_len) {
|
if (it->i < array->ar_len) {
|
||||||
it->value = array->ar_data[it->i];
|
it->value = &array->ar_data[it->i];
|
||||||
} else {
|
} else {
|
||||||
it->value = NULL;
|
it->value = NULL;
|
||||||
}
|
}
|
||||||
@@ -450,36 +435,31 @@ static enum fx_status iterator_erase(fx_iterator *obj)
|
|||||||
return FX_SUCCESS;
|
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
|
struct fx_array_iterator_p *it = fx_object_get_private(
|
||||||
= fx_object_get_private(obj, FX_TYPE_ARRAY_ITERATOR);
|
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)
|
return it->value;
|
||||||
{
|
|
||||||
struct fx_array_iterator_p *it
|
|
||||||
= fx_object_get_private(obj, FX_TYPE_ARRAY_ITERATOR);
|
|
||||||
|
|
||||||
return FX_ITERATOR_VALUE_CPTR(it->value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static enum fx_status iterator_is_valid(const fx_iterator *obj)
|
static enum fx_status iterator_is_valid(const fx_iterator *obj)
|
||||||
{
|
{
|
||||||
struct fx_array_iterator_p *it
|
struct fx_array_iterator_p *it = fx_object_get_private(
|
||||||
= fx_object_get_private(obj, FX_TYPE_ARRAY_ITERATOR);
|
obj,
|
||||||
|
FX_TYPE_ARRAY_ITERATOR);
|
||||||
struct fx_array_p *array = it->_a_p;
|
struct fx_array_p *array = it->_a_p;
|
||||||
|
|
||||||
if (it->i >= array->ar_len) {
|
if (it->i >= array->ar_len) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (array->ar_data[it->i] != it->value) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (it->value != NULL) ? FX_SUCCESS : FX_ERR_NO_DATA;
|
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_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
|
||||||
FX_INTERFACE_ENTRY(it_begin) = iterable_begin;
|
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_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
|
||||||
FX_TYPE_CLASS_END(fx_array)
|
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_move_next) = iterator_move_next;
|
||||||
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
|
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
|
||||||
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
|
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
|
||||||
FX_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue;
|
|
||||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
|
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
|
||||||
FX_TYPE_CLASS_END(fx_array_iterator)
|
FX_TYPE_CLASS_END(fx_array_iterator)
|
||||||
|
|
||||||
|
|||||||
+10
-3
@@ -1,6 +1,7 @@
|
|||||||
#include <fx/bitop.h>
|
#include <fx/bitop.h>
|
||||||
#include <fx/collections/bitmap.h>
|
#include <fx/collections/bitmap.h>
|
||||||
#include <fx/stream.h>
|
#include <fx/stream.h>
|
||||||
|
#include <fx/value.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define BITS_PER_WORD (8 * sizeof(bitmap_word_t))
|
#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;
|
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
|
const struct fx_bitmap_p *map = fx_object_get_private(
|
||||||
= fx_object_get_private(obj, FX_TYPE_BITMAP);
|
obj->v_object,
|
||||||
|
FX_TYPE_BITMAP);
|
||||||
|
|
||||||
unsigned char *bytes = (unsigned char *)map->map_words;
|
unsigned char *bytes = (unsigned char *)map->map_words;
|
||||||
size_t nr_bytes = map->map_nr_words * sizeof(bitmap_word_t);
|
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');
|
fx_stream_write_fmt(out, NULL, "%c", (c & mask) ? '1' : '0');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return FX_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*** CLASS DEFINITION *********************************************************/
|
/*** 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 {
|
struct fx_hashmap_iterator_p {
|
||||||
size_t i;
|
size_t i;
|
||||||
fx_hashmap_item item;
|
fx_hashmap_item item;
|
||||||
|
fx_value item_value;
|
||||||
|
|
||||||
fx_hashmap *_h;
|
const fx_hashmap *_h;
|
||||||
struct fx_hashmap_p *_h_p;
|
struct fx_hashmap_p *_h_p;
|
||||||
fx_bst_node *_cbn;
|
fx_bst_node *_cbn;
|
||||||
fx_queue_entry *_cqe;
|
fx_queue_entry *_cqe;
|
||||||
@@ -112,14 +113,18 @@ static bool get_next_node(
|
|||||||
struct fx_bst_node **out_next_node,
|
struct fx_bst_node **out_next_node,
|
||||||
struct fx_queue_entry **out_next_entry)
|
struct fx_queue_entry **out_next_entry)
|
||||||
{
|
{
|
||||||
struct fx_hashmap_bucket *cur_bucket
|
struct fx_hashmap_bucket *cur_bucket = fx_unbox(
|
||||||
= fx_unbox(struct fx_hashmap_bucket, cur_node, bk_node);
|
struct fx_hashmap_bucket,
|
||||||
|
cur_node,
|
||||||
|
bk_node);
|
||||||
if (!cur_bucket) {
|
if (!cur_bucket) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct fx_hashmap_bucket_item *cur_item
|
struct fx_hashmap_bucket_item *cur_item = fx_unbox(
|
||||||
= fx_unbox(struct fx_hashmap_bucket_item, cur_entry, bi_entry);
|
struct fx_hashmap_bucket_item,
|
||||||
|
cur_entry,
|
||||||
|
bi_entry);
|
||||||
if (!cur_item) {
|
if (!cur_item) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -146,8 +151,10 @@ static bool get_next_node(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct fx_hashmap_bucket_item *next_item
|
struct fx_hashmap_bucket_item *next_item = fx_unbox(
|
||||||
= fx_unbox(struct fx_hashmap_bucket_item, next_entry, bi_entry);
|
struct fx_hashmap_bucket_item,
|
||||||
|
next_entry,
|
||||||
|
bi_entry);
|
||||||
if (!next_item) {
|
if (!next_item) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -186,8 +193,9 @@ static fx_status hashmap_put(
|
|||||||
const fx_hashmap_value *value)
|
const fx_hashmap_value *value)
|
||||||
{
|
{
|
||||||
uint64_t hash = hash_key(key);
|
uint64_t hash = hash_key(key);
|
||||||
struct fx_hashmap_bucket *bucket
|
struct fx_hashmap_bucket *bucket = get_bucket(
|
||||||
= get_bucket(&hashmap->h_buckets, hash);
|
&hashmap->h_buckets,
|
||||||
|
hash);
|
||||||
|
|
||||||
if (!bucket) {
|
if (!bucket) {
|
||||||
bucket = create_bucket();
|
bucket = create_bucket();
|
||||||
@@ -234,8 +242,9 @@ static const struct fx_hashmap_value *hashmap_get(
|
|||||||
{
|
{
|
||||||
uint64_t hash = hash_key(key);
|
uint64_t hash = hash_key(key);
|
||||||
|
|
||||||
struct fx_hashmap_bucket *bucket
|
struct fx_hashmap_bucket *bucket = get_bucket(
|
||||||
= get_bucket(&hashmap->h_buckets, hash);
|
&hashmap->h_buckets,
|
||||||
|
hash);
|
||||||
if (!bucket) {
|
if (!bucket) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -262,8 +271,9 @@ static bool hashmap_has_key(
|
|||||||
const fx_hashmap_key *key)
|
const fx_hashmap_key *key)
|
||||||
{
|
{
|
||||||
uint64_t hash = hash_key(key);
|
uint64_t hash = hash_key(key);
|
||||||
struct fx_hashmap_bucket *bucket
|
struct fx_hashmap_bucket *bucket = get_bucket(
|
||||||
= get_bucket(&hashmap->h_buckets, hash);
|
&hashmap->h_buckets,
|
||||||
|
hash);
|
||||||
if (!bucket) {
|
if (!bucket) {
|
||||||
return false;
|
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)
|
static bool hashmap_is_empty(const struct fx_hashmap_p *hashmap)
|
||||||
{
|
{
|
||||||
fx_bst_node *first_node = fx_bst_first(&hashmap->h_buckets);
|
fx_bst_node *first_node = fx_bst_first(&hashmap->h_buckets);
|
||||||
struct fx_hashmap_bucket *first_bucket
|
struct fx_hashmap_bucket *first_bucket = fx_unbox(
|
||||||
= fx_unbox(struct fx_hashmap_bucket, first_node, bk_node);
|
struct fx_hashmap_bucket,
|
||||||
|
first_node,
|
||||||
|
bk_node);
|
||||||
if (!first_bucket) {
|
if (!first_bucket) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -358,8 +370,9 @@ fx_hashmap *fx_hashmap_create_with_items(const fx_hashmap_item *items)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct fx_hashmap_p *p
|
struct fx_hashmap_p *p = fx_object_get_private(
|
||||||
= fx_object_get_private(hashmap, FX_TYPE_HASHMAP);
|
hashmap,
|
||||||
|
FX_TYPE_HASHMAP);
|
||||||
|
|
||||||
for (size_t i = 0; items[i].key.key_data && items[i].key.key_size;
|
for (size_t i = 0; items[i].key.key_data && items[i].key.key_size;
|
||||||
i++) {
|
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_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_hashmap_iterator *it_obj = fx_object_create(
|
||||||
= fx_object_create(FX_TYPE_HASHMAP_ITERATOR);
|
FX_TYPE_HASHMAP_ITERATOR);
|
||||||
struct fx_hashmap_iterator_p *it
|
struct fx_hashmap_iterator_p *it = fx_object_get_private(
|
||||||
= fx_object_get_private(it_obj, FX_TYPE_HASHMAP_ITERATOR);
|
it_obj,
|
||||||
|
FX_TYPE_HASHMAP_ITERATOR);
|
||||||
|
|
||||||
it->_h = hashmap;
|
it->_h = hashmap;
|
||||||
it->_h_p = fx_object_get_private(hashmap, FX_TYPE_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_bst_node *first_node = fx_bst_first(&it->_h_p->h_buckets);
|
||||||
struct fx_hashmap_bucket *first_bucket
|
struct fx_hashmap_bucket *first_bucket = fx_unbox(
|
||||||
= fx_unbox(struct fx_hashmap_bucket, first_node, bk_node);
|
struct fx_hashmap_bucket,
|
||||||
|
first_node,
|
||||||
|
bk_node);
|
||||||
if (!first_bucket) {
|
if (!first_bucket) {
|
||||||
memset(&it->item, 0x0, sizeof it->item);
|
memset(&it->item, 0x0, sizeof it->item);
|
||||||
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
||||||
return it_obj;
|
return it_obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct fx_queue_entry *first_entry
|
struct fx_queue_entry *first_entry = fx_queue_first(
|
||||||
= fx_queue_first(&first_bucket->bk_items);
|
&first_bucket->bk_items);
|
||||||
struct fx_hashmap_bucket_item *first_item = fx_unbox(
|
struct fx_hashmap_bucket_item *first_item = fx_unbox(
|
||||||
struct fx_hashmap_bucket_item,
|
struct fx_hashmap_bucket_item,
|
||||||
first_entry,
|
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);
|
struct fx_bst_node *node = fx_bst_first(&map->h_buckets);
|
||||||
while (node) {
|
while (node) {
|
||||||
struct fx_hashmap_bucket *b
|
struct fx_hashmap_bucket *b = fx_unbox(
|
||||||
= fx_unbox(struct fx_hashmap_bucket, node, bk_node);
|
struct fx_hashmap_bucket,
|
||||||
|
node,
|
||||||
|
bk_node);
|
||||||
struct fx_bst_node *next_node = fx_bst_next(node);
|
struct fx_bst_node *next_node = fx_bst_next(node);
|
||||||
fx_bst_delete(&map->h_buckets, 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,
|
struct fx_hashmap_bucket_item,
|
||||||
entry,
|
entry,
|
||||||
bi_entry);
|
bi_entry);
|
||||||
struct fx_queue_entry *next_entry
|
struct fx_queue_entry *next_entry = fx_queue_next(
|
||||||
= fx_queue_next(entry);
|
entry);
|
||||||
fx_queue_delete(&b->bk_items, entry);
|
fx_queue_delete(&b->bk_items, entry);
|
||||||
|
|
||||||
if (map->h_key_dtor) {
|
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)
|
static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||||
{
|
{
|
||||||
struct fx_hashmap_iterator_p *it
|
struct fx_hashmap_iterator_p *it = fx_object_get_private(
|
||||||
= fx_object_get_private(obj, FX_TYPE_HASHMAP_ITERATOR);
|
obj,
|
||||||
|
FX_TYPE_HASHMAP_ITERATOR);
|
||||||
|
|
||||||
struct fx_bst_node *next_node;
|
struct fx_bst_node *next_node;
|
||||||
struct fx_queue_entry *next_entry;
|
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;
|
return FX_ERR_NO_DATA;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct fx_hashmap_bucket_item *next_item
|
struct fx_hashmap_bucket_item *next_item = fx_unbox(
|
||||||
= fx_unbox(struct fx_hashmap_bucket_item, next_entry, bi_entry);
|
struct fx_hashmap_bucket_item,
|
||||||
|
next_entry,
|
||||||
|
bi_entry);
|
||||||
|
|
||||||
if (!next_item) {
|
if (!next_item) {
|
||||||
memset(&it->item, 0x0, sizeof it->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)
|
static enum fx_status iterator_erase(fx_iterator *obj)
|
||||||
{
|
{
|
||||||
struct fx_hashmap_iterator_p *it
|
struct fx_hashmap_iterator_p *it = fx_object_get_private(
|
||||||
= fx_object_get_private(obj, FX_TYPE_HASHMAP_ITERATOR);
|
obj,
|
||||||
|
FX_TYPE_HASHMAP_ITERATOR);
|
||||||
|
|
||||||
if ((it->item.key.key_data || it->item.value.value_data)
|
if ((it->item.key.key_data || it->item.value.value_data)
|
||||||
&& !(it->_cbn && it->_cqe)) {
|
&& !(it->_cbn && it->_cqe)) {
|
||||||
@@ -559,13 +581,19 @@ static enum fx_status iterator_erase(fx_iterator *obj)
|
|||||||
return FX_ERR_NO_DATA;
|
return FX_ERR_NO_DATA;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct fx_hashmap_bucket *cur_bucket
|
struct fx_hashmap_bucket *cur_bucket = fx_unbox(
|
||||||
= fx_unbox(struct fx_hashmap_bucket, it->_cbn, bk_node);
|
struct fx_hashmap_bucket,
|
||||||
struct fx_hashmap_bucket_item *cur_item
|
it->_cbn,
|
||||||
= fx_unbox(struct fx_hashmap_bucket_item, it->_cqe, bi_entry);
|
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
|
struct fx_hashmap_bucket_item *next_item = fx_unbox(
|
||||||
= fx_unbox(struct fx_hashmap_bucket_item, next_entry, bi_entry);
|
struct fx_hashmap_bucket_item,
|
||||||
|
next_entry,
|
||||||
|
bi_entry);
|
||||||
|
|
||||||
fx_status status = delete_item(it->_h_p, cur_bucket, cur_item);
|
fx_status status = delete_item(it->_h_p, cur_bucket, cur_item);
|
||||||
if (FX_ERR(status)) {
|
if (FX_ERR(status)) {
|
||||||
@@ -590,18 +618,13 @@ static enum fx_status iterator_erase(fx_iterator *obj)
|
|||||||
return FX_SUCCESS;
|
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
|
struct fx_hashmap_iterator_p *it = fx_object_get_private(
|
||||||
= fx_object_get_private(obj, FX_TYPE_HASHMAP_ITERATOR);
|
obj,
|
||||||
return FX_ITERATOR_VALUE_PTR(&it->item);
|
FX_TYPE_HASHMAP_ITERATOR);
|
||||||
}
|
it->item_value = FX_POINTER(&it->item);
|
||||||
|
return &it->item_value;
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*** CLASS DEFINITION *********************************************************/
|
/*** CLASS DEFINITION *********************************************************/
|
||||||
@@ -614,7 +637,6 @@ FX_TYPE_CLASS_BEGIN(fx_hashmap)
|
|||||||
|
|
||||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
|
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
|
||||||
FX_INTERFACE_ENTRY(it_begin) = fx_hashmap_begin;
|
FX_INTERFACE_ENTRY(it_begin) = fx_hashmap_begin;
|
||||||
FX_INTERFACE_ENTRY(it_cbegin) = fx_hashmap_cbegin;
|
|
||||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
|
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
|
||||||
FX_TYPE_CLASS_END(fx_hashmap)
|
FX_TYPE_CLASS_END(fx_hashmap)
|
||||||
|
|
||||||
@@ -637,7 +659,6 @@ FX_TYPE_CLASS_BEGIN(fx_hashmap_iterator)
|
|||||||
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
|
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
|
||||||
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
|
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
|
||||||
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
|
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
|
||||||
FX_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue;
|
|
||||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
|
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
|
||||||
FX_TYPE_CLASS_END(fx_hashmap_iterator)
|
FX_TYPE_CLASS_END(fx_hashmap_iterator)
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,695 @@
|
|||||||
|
#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 index = hash % table_size, i = 1;
|
||||||
|
bool index_found = false;
|
||||||
|
while (table[index].i_item && i <= table_size) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
index = (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 = 1; 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 = fx_object_create(FX_TYPE_HASHTABLE_ITEM);
|
||||||
|
if (!item) {
|
||||||
|
return FX_ERR_NO_MEMORY;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct fx_hashtable_item_p *item_p
|
||||||
|
= fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
|
||||||
|
fx_value_copy(&item_p->i_key, key);
|
||||||
|
fx_value_copy(&item_p->i_value, value);
|
||||||
|
|
||||||
|
hashtable->t_items[index].i_item = item;
|
||||||
|
hashtable->t_count++;
|
||||||
|
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 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 (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_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 @@
|
|||||||
/**
|
#ifndef FX_COLLECTIONS_ARRAY_H_
|
||||||
* A heterogeneous array of objects. fx_array only stores references
|
#define FX_COLLECTIONS_ARRAY_H_
|
||||||
* 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_
|
|
||||||
|
|
||||||
#include <fx/iterator.h>
|
#include <fx/iterator.h>
|
||||||
#include <fx/macros.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);
|
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_API fx_array *fx_array_create_with_values(
|
||||||
fx_object *const *values,
|
const fx_value *values,
|
||||||
size_t nr_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);
|
FX_API void fx_array_clear(fx_array *array);
|
||||||
|
|
||||||
/**
|
FX_API fx_status fx_array_push_back(fx_array *array, fx_value value);
|
||||||
* 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_front(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_insert(fx_array *array, fx_value value, size_t at);
|
||||||
* 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);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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);
|
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);
|
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);
|
FX_API fx_status fx_array_remove_back(fx_array *array);
|
||||||
|
|
||||||
/**
|
FX_API fx_value fx_array_pop(fx_array *array, size_t at);
|
||||||
* 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_front(fx_array *array);
|
||||||
* 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_back(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_get_ref(const fx_array *array, size_t at);
|
||||||
* 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(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 size_t fx_array_get_size(const fx_array *array);
|
||||||
* 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_capacity(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_DECLS_END;
|
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 size_t fx_hashmap_get_size(const fx_hashmap *hashmap);
|
||||||
FX_API bool fx_hashmap_is_empty(const fx_hashmap *hashmap);
|
FX_API bool fx_hashmap_is_empty(const fx_hashmap *hashmap);
|
||||||
|
|
||||||
FX_API fx_iterator *fx_hashmap_begin(fx_hashmap *hashmap);
|
FX_API const fx_iterator *fx_hashmap_begin(const fx_hashmap *hashmap);
|
||||||
FX_API const fx_iterator *fx_hashmap_cbegin(const fx_hashmap *hashmap);
|
FX_API const fx_iterator *fx_hashmap_cbegin(const fx_hashmap *hashmap);
|
||||||
|
|
||||||
FX_DECLS_END;
|
FX_DECLS_END;
|
||||||
|
|||||||
@@ -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 void *fx_list_entry_value(const fx_list_entry *entry);
|
||||||
|
|
||||||
FX_API fx_iterator *fx_list_begin(fx_list *q);
|
FX_API const fx_iterator *fx_list_begin(const fx_list *q);
|
||||||
FX_API const fx_iterator *fx_list_cbegin(const fx_list *q);
|
|
||||||
|
|
||||||
FX_DECLS_END;
|
FX_DECLS_END;
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
size_t i;
|
||||||
void *item;
|
void *item;
|
||||||
|
fx_value item_value;
|
||||||
fx_list_entry *entry;
|
fx_list_entry *entry;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -41,8 +42,10 @@ static void *list_first_item(const struct fx_list_p *q)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct fx_list_entry *list_entry
|
struct fx_list_entry *list_entry = fx_unbox(
|
||||||
= fx_unbox(struct fx_list_entry, entry, e_entry);
|
struct fx_list_entry,
|
||||||
|
entry,
|
||||||
|
e_entry);
|
||||||
|
|
||||||
return list_entry->e_data;
|
return list_entry->e_data;
|
||||||
}
|
}
|
||||||
@@ -55,8 +58,10 @@ static void *list_last_item(const struct fx_list_p *q)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct fx_list_entry *list_entry
|
struct fx_list_entry *list_entry = fx_unbox(
|
||||||
= fx_unbox(struct fx_list_entry, entry, e_entry);
|
struct fx_list_entry,
|
||||||
|
entry,
|
||||||
|
e_entry);
|
||||||
|
|
||||||
return list_entry->e_data;
|
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;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct fx_list_entry *list_entry
|
struct fx_list_entry *list_entry = fx_unbox(
|
||||||
= fx_unbox(struct fx_list_entry, entry, e_entry);
|
struct fx_list_entry,
|
||||||
|
entry,
|
||||||
|
e_entry);
|
||||||
|
|
||||||
return list_entry;
|
return list_entry;
|
||||||
}
|
}
|
||||||
@@ -83,8 +90,10 @@ static struct fx_list_entry *list_last_entry(const struct fx_list_p *q)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct fx_list_entry *list_entry
|
struct fx_list_entry *list_entry = fx_unbox(
|
||||||
= fx_unbox(struct fx_list_entry, entry, e_entry);
|
struct fx_list_entry,
|
||||||
|
entry,
|
||||||
|
e_entry);
|
||||||
|
|
||||||
return list_entry;
|
return list_entry;
|
||||||
}
|
}
|
||||||
@@ -169,8 +178,10 @@ static void *list_pop_front(struct fx_list_p *q)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct fx_list_entry *list_entry
|
struct fx_list_entry *list_entry = fx_unbox(
|
||||||
= fx_unbox(struct fx_list_entry, entry, e_entry);
|
struct fx_list_entry,
|
||||||
|
entry,
|
||||||
|
e_entry);
|
||||||
|
|
||||||
void *item = list_entry->e_data;
|
void *item = list_entry->e_data;
|
||||||
free(list_entry);
|
free(list_entry);
|
||||||
@@ -187,8 +198,10 @@ static void *list_pop_back(struct fx_list_p *q)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct fx_list_entry *list_entry
|
struct fx_list_entry *list_entry = fx_unbox(
|
||||||
= fx_unbox(struct fx_list_entry, entry, e_entry);
|
struct fx_list_entry,
|
||||||
|
entry,
|
||||||
|
e_entry);
|
||||||
|
|
||||||
void *item = list_entry->e_data;
|
void *item = list_entry->e_data;
|
||||||
free(list_entry);
|
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);
|
struct fx_queue_entry *entry = fx_queue_first(&list->l_queue);
|
||||||
while (entry) {
|
while (entry) {
|
||||||
struct fx_list_entry *list_entry
|
struct fx_list_entry *list_entry = fx_unbox(
|
||||||
= fx_unbox(struct fx_list_entry, entry, e_entry);
|
struct fx_list_entry,
|
||||||
|
entry,
|
||||||
|
e_entry);
|
||||||
|
|
||||||
if (list_entry->e_data == item) {
|
if (list_entry->e_data == item) {
|
||||||
return list_entry;
|
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);
|
struct fx_queue_entry *entry = fx_queue_first(&q->l_queue);
|
||||||
while (entry) {
|
while (entry) {
|
||||||
struct fx_list_entry *list_entry
|
struct fx_list_entry *list_entry = fx_unbox(
|
||||||
= fx_unbox(struct fx_list_entry, entry, e_entry);
|
struct fx_list_entry,
|
||||||
|
entry,
|
||||||
|
e_entry);
|
||||||
struct fx_queue_entry *next = fx_queue_next(entry);
|
struct fx_queue_entry *next = fx_queue_next(entry);
|
||||||
|
|
||||||
free(list_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;
|
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);
|
fx_list_iterator *it_obj = fx_object_create(FX_TYPE_LIST_ITERATOR);
|
||||||
struct fx_list_iterator_p *it
|
struct fx_list_iterator_p *it = fx_object_get_private(
|
||||||
= fx_object_get_private(it_obj, FX_TYPE_LIST_ITERATOR);
|
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);
|
|
||||||
|
|
||||||
it->_q = (fx_list *)q;
|
it->_q = (fx_list *)q;
|
||||||
it->_q_p = fx_object_get_private(q, FX_TYPE_LIST);
|
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)
|
static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||||
{
|
{
|
||||||
struct fx_list_iterator_p *it
|
struct fx_list_iterator_p *it = fx_object_get_private(
|
||||||
= fx_object_get_private(obj, FX_TYPE_LIST_ITERATOR);
|
obj,
|
||||||
|
FX_TYPE_LIST_ITERATOR);
|
||||||
|
|
||||||
if (!it->_q_entry) {
|
if (!it->_q_entry) {
|
||||||
it->entry = NULL;
|
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)
|
static enum fx_status iterator_erase(fx_iterator *obj)
|
||||||
{
|
{
|
||||||
struct fx_list_iterator_p *it
|
struct fx_list_iterator_p *it = fx_object_get_private(
|
||||||
= fx_object_get_private(obj, FX_TYPE_LIST_ITERATOR);
|
obj,
|
||||||
|
FX_TYPE_LIST_ITERATOR);
|
||||||
|
|
||||||
if (!it->entry || !it->_q_entry) {
|
if (!it->entry || !it->_q_entry) {
|
||||||
return FX_ERR_OUT_OF_BOUNDS;
|
return FX_ERR_OUT_OF_BOUNDS;
|
||||||
@@ -488,20 +489,14 @@ static enum fx_status iterator_erase(fx_iterator *obj)
|
|||||||
return FX_SUCCESS;
|
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
|
struct fx_list_iterator_p *it = fx_object_get_private(
|
||||||
= fx_object_get_private(obj, FX_TYPE_LIST_ITERATOR);
|
obj,
|
||||||
|
FX_TYPE_LIST_ITERATOR);
|
||||||
|
|
||||||
return FX_ITERATOR_VALUE_PTR(it->item);
|
it->item_value = FX_POINTER(it->item);
|
||||||
}
|
return &it->item_value;
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*** CLASS DEFINITION *********************************************************/
|
/*** CLASS DEFINITION *********************************************************/
|
||||||
@@ -514,7 +509,6 @@ FX_TYPE_CLASS_BEGIN(fx_list)
|
|||||||
|
|
||||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
|
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
|
||||||
FX_INTERFACE_ENTRY(it_begin) = fx_list_begin;
|
FX_INTERFACE_ENTRY(it_begin) = fx_list_begin;
|
||||||
FX_INTERFACE_ENTRY(it_cbegin) = fx_list_cbegin;
|
|
||||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
|
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
|
||||||
FX_TYPE_CLASS_END(fx_list)
|
FX_TYPE_CLASS_END(fx_list)
|
||||||
|
|
||||||
@@ -537,7 +531,6 @@ FX_TYPE_CLASS_BEGIN(fx_list_iterator)
|
|||||||
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
|
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
|
||||||
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
|
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
|
||||||
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
|
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
|
||||||
FX_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue;
|
|
||||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
|
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
|
||||||
FX_TYPE_CLASS_END(fx_list_iterator)
|
FX_TYPE_CLASS_END(fx_list_iterator)
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
|
#if 0
|
||||||
fx_array *array = fx_array_create();
|
fx_array *array = fx_array_create();
|
||||||
fx_array_append(array, fx_int_create(32));
|
fx_array_append(array, fx_int_create(32));
|
||||||
fx_array_append(array, fx_int_create(64));
|
fx_array_append(array, fx_int_create(64));
|
||||||
@@ -17,5 +18,6 @@ int main(void)
|
|||||||
fx_iterator_unref(it);
|
fx_iterator_unref(it);
|
||||||
|
|
||||||
fx_array_unref(array);
|
fx_array_unref(array);
|
||||||
|
#endif
|
||||||
return 0;
|
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>
|
#include <stdio.h>
|
||||||
|
|
||||||
int main(void)
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,9 @@
|
|||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
fx_string *string = FX_CSTR("Hello, world!");
|
fx_string *string = fx_string_create_from_cstr("Hello, world!");
|
||||||
printf("string object = ");
|
printf("string object = ");
|
||||||
fx_object_to_string(string, fx_stdout);
|
fx_object_to_string(string, fx_stdout, NULL);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
fx_string_unref(string);
|
fx_string_unref(string);
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -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)
|
|
||||||
@@ -35,6 +35,7 @@ struct fx_directory_iterator_p {
|
|||||||
fx_directory *root;
|
fx_directory *root;
|
||||||
|
|
||||||
fx_directory_entry entry;
|
fx_directory_entry entry;
|
||||||
|
fx_value entry_value;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*** PRIVATE FUNCTIONS ********************************************************/
|
/*** PRIVATE FUNCTIONS ********************************************************/
|
||||||
@@ -64,8 +65,9 @@ static fx_result directory_delete(
|
|||||||
{
|
{
|
||||||
enum fx_status status = FX_SUCCESS;
|
enum fx_status status = FX_SUCCESS;
|
||||||
|
|
||||||
fx_iterator *it
|
fx_iterator *it = fx_directory_begin(
|
||||||
= fx_directory_begin(dir, FX_DIRECTORY_ITERATE_PARENT_LAST);
|
dir,
|
||||||
|
FX_DIRECTORY_ITERATE_PARENT_LAST);
|
||||||
while (FX_OK(fx_iterator_get_status(it))) {
|
while (FX_OK(fx_iterator_get_status(it))) {
|
||||||
fx_iterator_erase(it);
|
fx_iterator_erase(it);
|
||||||
}
|
}
|
||||||
@@ -291,8 +293,9 @@ static fx_result directory_open(
|
|||||||
|
|
||||||
fx_directory *dir = fx_object_create(FX_TYPE_DIRECTORY);
|
fx_directory *dir = fx_object_create(FX_TYPE_DIRECTORY);
|
||||||
fx_path *cwd = NULL;
|
fx_path *cwd = NULL;
|
||||||
struct fx_directory_p *p
|
struct fx_directory_p *p = fx_object_get_private(
|
||||||
= fx_object_get_private(dir, FX_TYPE_DIRECTORY);
|
dir,
|
||||||
|
FX_TYPE_DIRECTORY);
|
||||||
|
|
||||||
if (!root) {
|
if (!root) {
|
||||||
cwd = fx_path_create_cwd();
|
cwd = fx_path_create_cwd();
|
||||||
@@ -358,8 +361,9 @@ fx_result fx_directory_open_temp(fx_directory **out)
|
|||||||
rpath,
|
rpath,
|
||||||
FX_DIRECTORY_OPEN_CREATE,
|
FX_DIRECTORY_OPEN_CREATE,
|
||||||
&dir);
|
&dir);
|
||||||
struct fx_directory_p *p
|
struct fx_directory_p *p = fx_object_get_private(
|
||||||
= fx_object_get_private(dir, FX_TYPE_DIRECTORY);
|
dir,
|
||||||
|
FX_TYPE_DIRECTORY);
|
||||||
|
|
||||||
if (fx_error_get_status_code(status) == FX_ERR_NAME_EXISTS) {
|
if (fx_error_get_status_code(status) == FX_ERR_NAME_EXISTS) {
|
||||||
fx_path_unref(rpath);
|
fx_path_unref(rpath);
|
||||||
@@ -372,8 +376,6 @@ fx_result fx_directory_open_temp(fx_directory **out)
|
|||||||
|
|
||||||
fx_path_unlink(rpath);
|
fx_path_unlink(rpath);
|
||||||
fx_path_unref(rpath);
|
fx_path_unref(rpath);
|
||||||
|
|
||||||
return status;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -408,8 +410,9 @@ const char *fx_directory_get_rel_path_cstr(const fx_directory *dir)
|
|||||||
|
|
||||||
fx_result fx_directory_delete(fx_directory *dir)
|
fx_result fx_directory_delete(fx_directory *dir)
|
||||||
{
|
{
|
||||||
struct fx_directory_p *p
|
struct fx_directory_p *p = fx_object_get_private(
|
||||||
= fx_object_get_private(dir, FX_TYPE_DIRECTORY);
|
dir,
|
||||||
|
FX_TYPE_DIRECTORY);
|
||||||
p->d_flags |= DIRECTORY_DELETE_ON_CLOSE;
|
p->d_flags |= DIRECTORY_DELETE_ON_CLOSE;
|
||||||
/* TODO allow object release functions to return a fx_result value */
|
/* TODO allow object release functions to return a fx_result value */
|
||||||
fx_directory_unref(dir);
|
fx_directory_unref(dir);
|
||||||
@@ -508,6 +511,8 @@ static void update_iterator_data(struct fx_directory_iterator_p *it)
|
|||||||
fx_path *path = fx_path_create_from_cstr(
|
fx_path *path = fx_path_create_from_cstr(
|
||||||
ent->fts_path + fx_path_length(it->_p->d_path_abs) + 1);
|
ent->fts_path + fx_path_length(it->_p->d_path_abs) + 1);
|
||||||
|
|
||||||
|
fx_value_unset(&it->entry_value);
|
||||||
|
it->entry_value = FX_POINTER(&it->entry);
|
||||||
it->entry.filename = ent->fts_name;
|
it->entry.filename = ent->fts_name;
|
||||||
it->entry.filepath = path;
|
it->entry.filepath = path;
|
||||||
|
|
||||||
@@ -555,8 +560,9 @@ fx_iterator *fx_directory_begin(
|
|||||||
enum fx_directory_iterator_flags flags)
|
enum fx_directory_iterator_flags flags)
|
||||||
{
|
{
|
||||||
fx_iterator *it_obj = fx_object_create(FX_TYPE_DIRECTORY_ITERATOR);
|
fx_iterator *it_obj = fx_object_create(FX_TYPE_DIRECTORY_ITERATOR);
|
||||||
struct fx_directory_iterator_p *it
|
struct fx_directory_iterator_p *it = fx_object_get_private(
|
||||||
= fx_object_get_private(it_obj, FX_TYPE_DIRECTORY_ITERATOR);
|
it_obj,
|
||||||
|
FX_TYPE_DIRECTORY_ITERATOR);
|
||||||
|
|
||||||
it->flags = flags;
|
it->flags = flags;
|
||||||
it->root = directory;
|
it->root = directory;
|
||||||
@@ -614,12 +620,7 @@ fx_iterator *fx_directory_begin(
|
|||||||
return it_obj;
|
return it_obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
static fx_iterator *iterator_begin(fx_object *obj)
|
static const fx_iterator *iterator_begin(const fx_object *obj)
|
||||||
{
|
|
||||||
return fx_directory_begin(obj, FX_DIRECTORY_ITERATE_PARENT_FIRST);
|
|
||||||
}
|
|
||||||
|
|
||||||
static const fx_iterator *iterator_cbegin(const fx_object *obj)
|
|
||||||
{
|
{
|
||||||
return fx_directory_begin(
|
return fx_directory_begin(
|
||||||
(fx_object *)obj,
|
(fx_object *)obj,
|
||||||
@@ -628,8 +629,9 @@ static const fx_iterator *iterator_cbegin(const fx_object *obj)
|
|||||||
|
|
||||||
static enum fx_status iterator_move_next(const fx_iterator *obj)
|
static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||||
{
|
{
|
||||||
struct fx_directory_iterator_p *it
|
struct fx_directory_iterator_p *it = fx_object_get_private(
|
||||||
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
|
obj,
|
||||||
|
FX_TYPE_DIRECTORY_ITERATOR);
|
||||||
if (!it || !it->fts) {
|
if (!it || !it->fts) {
|
||||||
return FX_ERR_NO_DATA;
|
return FX_ERR_NO_DATA;
|
||||||
}
|
}
|
||||||
@@ -677,10 +679,12 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
|
|||||||
|
|
||||||
static enum fx_status iterator_erase(fx_iterator *obj)
|
static enum fx_status iterator_erase(fx_iterator *obj)
|
||||||
{
|
{
|
||||||
struct fx_directory_iterator_p *it
|
struct fx_directory_iterator_p *it = fx_object_get_private(
|
||||||
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
|
obj,
|
||||||
fx_result result
|
FX_TYPE_DIRECTORY_ITERATOR);
|
||||||
= fx_directory_path_unlink(it->root, it->entry.filepath);
|
fx_result result = fx_directory_path_unlink(
|
||||||
|
it->root,
|
||||||
|
it->entry.filepath);
|
||||||
if (fx_result_is_error(result)) {
|
if (fx_result_is_error(result)) {
|
||||||
enum fx_status status = fx_error_get_status_code(result);
|
enum fx_status status = fx_error_get_status_code(result);
|
||||||
fx_error_discard(result);
|
fx_error_discard(result);
|
||||||
@@ -690,20 +694,13 @@ static enum fx_status iterator_erase(fx_iterator *obj)
|
|||||||
return iterator_move_next(obj);
|
return iterator_move_next(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
static fx_iterator_value iterator_get_value(fx_iterator *obj)
|
static const fx_value *iterator_get_value(const fx_iterator *obj)
|
||||||
{
|
{
|
||||||
struct fx_directory_iterator_p *it
|
struct fx_directory_iterator_p *it = fx_object_get_private(
|
||||||
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
|
obj,
|
||||||
|
FX_TYPE_DIRECTORY_ITERATOR);
|
||||||
|
|
||||||
return FX_ITERATOR_VALUE_PTR(&it->entry);
|
return &it->entry_value;
|
||||||
}
|
|
||||||
|
|
||||||
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
|
|
||||||
{
|
|
||||||
struct fx_directory_iterator_p *it
|
|
||||||
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
|
|
||||||
|
|
||||||
return FX_ITERATOR_VALUE_CPTR(&it->entry);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*** CLASS DEFINITION *********************************************************/
|
/*** CLASS DEFINITION *********************************************************/
|
||||||
@@ -716,7 +713,6 @@ FX_TYPE_CLASS_BEGIN(fx_directory)
|
|||||||
|
|
||||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
|
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
|
||||||
FX_INTERFACE_ENTRY(it_begin) = iterator_begin;
|
FX_INTERFACE_ENTRY(it_begin) = iterator_begin;
|
||||||
FX_INTERFACE_ENTRY(it_cbegin) = iterator_cbegin;
|
|
||||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
|
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
|
||||||
FX_TYPE_CLASS_END(fx_directory)
|
FX_TYPE_CLASS_END(fx_directory)
|
||||||
|
|
||||||
@@ -739,7 +735,6 @@ FX_TYPE_CLASS_BEGIN(fx_directory_iterator)
|
|||||||
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
|
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
|
||||||
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
|
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
|
||||||
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
|
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
|
||||||
FX_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue;
|
|
||||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
|
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
|
||||||
FX_TYPE_CLASS_END(fx_directory_iterator)
|
FX_TYPE_CLASS_END(fx_directory_iterator)
|
||||||
|
|
||||||
@@ -6,6 +6,7 @@
|
|||||||
#include <fx/io/file.h>
|
#include <fx/io/file.h>
|
||||||
#include <fx/io/path.h>
|
#include <fx/io/path.h>
|
||||||
#include <fx/string.h>
|
#include <fx/string.h>
|
||||||
|
#include <fx/value.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
@@ -134,8 +135,8 @@ static enum fx_status path_get_directory(
|
|||||||
size_t dir_path_len = (size_t)(sep - path_cstr);
|
size_t dir_path_len = (size_t)(sep - path_cstr);
|
||||||
fx_string *dir_path_s = fx_string_get_substr(path_str, 0, dir_path_len);
|
fx_string *dir_path_s = fx_string_get_substr(path_str, 0, dir_path_len);
|
||||||
|
|
||||||
fx_path *dir_path
|
fx_path *dir_path = fx_path_create_from_cstr(
|
||||||
= fx_path_create_from_cstr(fx_string_get_cstr(dir_path_s));
|
fx_string_get_cstr(dir_path_s));
|
||||||
fx_string_unref(dir_path_s);
|
fx_string_unref(dir_path_s);
|
||||||
|
|
||||||
*out_dir_path = dir_path;
|
*out_dir_path = dir_path;
|
||||||
@@ -284,13 +285,15 @@ fx_path *fx_path_join(const fx_path *paths[], size_t nr_paths)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct fx_path_p *result_p
|
struct fx_path_p *result_p = fx_object_get_private(
|
||||||
= fx_object_get_private(result, FX_TYPE_PATH);
|
result,
|
||||||
|
FX_TYPE_PATH);
|
||||||
|
|
||||||
for (size_t i = 0; i < nr_paths; i++) {
|
for (size_t i = 0; i < nr_paths; i++) {
|
||||||
if (paths[i]) {
|
if (paths[i]) {
|
||||||
struct fx_path_p *path_p
|
struct fx_path_p *path_p = fx_object_get_private(
|
||||||
= fx_object_get_private(paths[i], FX_TYPE_PATH);
|
paths[i],
|
||||||
|
FX_TYPE_PATH);
|
||||||
append_path(result_p, path_p);
|
append_path(result_p, path_p);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -385,18 +388,24 @@ static void path_init(fx_object *obj, void *priv)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void path_fini(fx_object *obj, void *priv)
|
static void path_fini(fx_object *obj, void *priv)
|
||||||
{
|
{
|
||||||
struct fx_path_p *path = priv;
|
struct fx_path_p *path = priv;
|
||||||
|
|
||||||
fx_string_unref(path->p_pathstr);
|
fx_string_unref(path->p_pathstr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void path_to_string(const fx_object *obj, fx_stream *out)
|
static fx_status path_to_string(
|
||||||
|
const fx_value *obj,
|
||||||
|
fx_stream *out,
|
||||||
|
const char *format)
|
||||||
{
|
{
|
||||||
struct fx_path_p *path = fx_object_get_private(obj, FX_TYPE_PATH);
|
struct fx_path_p *path = fx_object_get_private(
|
||||||
|
obj->v_object,
|
||||||
|
FX_TYPE_PATH);
|
||||||
|
|
||||||
fx_stream_write_cstr(out, fx_string_get_cstr(path->p_pathstr), NULL);
|
fx_stream_write_cstr(out, fx_string_get_cstr(path->p_pathstr), NULL);
|
||||||
|
return FX_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*** CLASS DEFINITION *********************************************************/
|
/*** CLASS DEFINITION *********************************************************/
|
||||||
@@ -37,6 +37,7 @@ struct fx_directory_iterator_p {
|
|||||||
fx_directory *root;
|
fx_directory *root;
|
||||||
|
|
||||||
fx_directory_entry entry;
|
fx_directory_entry entry;
|
||||||
|
fx_value entry_value;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*** PRIVATE FUNCTIONS ********************************************************/
|
/*** PRIVATE FUNCTIONS ********************************************************/
|
||||||
@@ -510,6 +511,8 @@ static void update_iterator_data(struct fx_directory_iterator_p *it)
|
|||||||
fx_path *path = fx_path_create_from_cstr(
|
fx_path *path = fx_path_create_from_cstr(
|
||||||
ent->fts_path + fx_path_length(it->_p->d_path_abs) + 1);
|
ent->fts_path + fx_path_length(it->_p->d_path_abs) + 1);
|
||||||
|
|
||||||
|
fx_value_unset(&it->entry_value);
|
||||||
|
it->entry_value = FX_POINTER(&it->entry);
|
||||||
it->entry.filename = ent->fts_name;
|
it->entry.filename = ent->fts_name;
|
||||||
it->entry.filepath = path;
|
it->entry.filepath = path;
|
||||||
|
|
||||||
@@ -616,12 +619,7 @@ fx_iterator *fx_directory_begin(
|
|||||||
return it_obj;
|
return it_obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
static fx_iterator *iterator_begin(fx_object *obj)
|
static const fx_iterator *iterator_begin(const fx_object *obj)
|
||||||
{
|
|
||||||
return fx_directory_begin(obj, FX_DIRECTORY_ITERATE_PARENT_FIRST);
|
|
||||||
}
|
|
||||||
|
|
||||||
static const fx_iterator *iterator_cbegin(const fx_object *obj)
|
|
||||||
{
|
{
|
||||||
return fx_directory_begin(
|
return fx_directory_begin(
|
||||||
(fx_object *)obj,
|
(fx_object *)obj,
|
||||||
@@ -692,20 +690,12 @@ static enum fx_status iterator_erase(fx_iterator *obj)
|
|||||||
return iterator_move_next(obj);
|
return iterator_move_next(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
static fx_iterator_value iterator_get_value(fx_iterator *obj)
|
static const fx_value *iterator_get_value(const fx_iterator *obj)
|
||||||
{
|
{
|
||||||
struct fx_directory_iterator_p *it
|
struct fx_directory_iterator_p *it
|
||||||
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
|
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
|
||||||
|
|
||||||
return FX_ITERATOR_VALUE_PTR(&it->entry);
|
return &it->entry_value;
|
||||||
}
|
|
||||||
|
|
||||||
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
|
|
||||||
{
|
|
||||||
struct fx_directory_iterator_p *it
|
|
||||||
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
|
|
||||||
|
|
||||||
return FX_ITERATOR_VALUE_CPTR(&it->entry);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*** CLASS DEFINITION *********************************************************/
|
/*** CLASS DEFINITION *********************************************************/
|
||||||
@@ -718,7 +708,6 @@ FX_TYPE_CLASS_BEGIN(fx_directory)
|
|||||||
|
|
||||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
|
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
|
||||||
FX_INTERFACE_ENTRY(it_begin) = iterator_begin;
|
FX_INTERFACE_ENTRY(it_begin) = iterator_begin;
|
||||||
FX_INTERFACE_ENTRY(it_cbegin) = iterator_cbegin;
|
|
||||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
|
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
|
||||||
FX_TYPE_CLASS_END(fx_directory)
|
FX_TYPE_CLASS_END(fx_directory)
|
||||||
|
|
||||||
@@ -741,7 +730,6 @@ FX_TYPE_CLASS_BEGIN(fx_directory_iterator)
|
|||||||
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
|
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
|
||||||
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
|
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
|
||||||
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
|
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
|
||||||
FX_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue;
|
|
||||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
|
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
|
||||||
FX_TYPE_CLASS_END(fx_directory_iterator)
|
FX_TYPE_CLASS_END(fx_directory_iterator)
|
||||||
|
|
||||||
|
|||||||
+10
-3
@@ -392,11 +392,18 @@ void path_fini(fx_object *obj, void *priv)
|
|||||||
fx_string_unref(path->p_pathstr);
|
fx_string_unref(path->p_pathstr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void path_to_string(const fx_object *obj, fx_stream *out)
|
static fx_status path_to_string(
|
||||||
|
const fx_value *obj,
|
||||||
|
fx_stream *out,
|
||||||
|
const char *format)
|
||||||
{
|
{
|
||||||
struct fx_path_p *path = fx_object_get_private(obj, FX_TYPE_PATH);
|
struct fx_path_p *path
|
||||||
|
= fx_object_get_private(obj->v_object, FX_TYPE_PATH);
|
||||||
|
|
||||||
fx_stream_write_cstr(out, fx_string_get_cstr(path->p_pathstr), NULL);
|
return fx_stream_write_cstr(
|
||||||
|
out,
|
||||||
|
fx_string_get_cstr(path->p_pathstr),
|
||||||
|
NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*** CLASS DEFINITION *********************************************************/
|
/*** CLASS DEFINITION *********************************************************/
|
||||||
|
|||||||
+6
-2
@@ -20,9 +20,13 @@ int main(int argc, const char **argv)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fx_iterator *it = fx_directory_begin(dir, FX_DIRECTORY_ITERATE_PARENT_FIRST);
|
fx_iterator *it = fx_directory_begin(
|
||||||
fx_foreach(fx_directory_entry *, entry, it)
|
dir,
|
||||||
|
FX_DIRECTORY_ITERATE_PARENT_FIRST);
|
||||||
|
fx_foreach(val, it)
|
||||||
{
|
{
|
||||||
|
fx_directory_entry *entry = NULL;
|
||||||
|
fx_value_get_pointer(val, (void **)&entry);
|
||||||
printf("%s\n", fx_path_ptr(entry->filepath));
|
printf("%s\n", fx_path_ptr(entry->filepath));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+363
-25
@@ -1,8 +1,24 @@
|
|||||||
|
#include "fx/namemap.h"
|
||||||
|
|
||||||
#include <fx/bst.h>
|
#include <fx/bst.h>
|
||||||
#include <fx/hash.h>
|
#include <fx/hash.h>
|
||||||
|
#include <fx/iterator.h>
|
||||||
#include <fx/macros.h>
|
#include <fx/macros.h>
|
||||||
#include <fx/queue.h>
|
#include <fx/queue.h>
|
||||||
#include <fx/reflection/assembly.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 {
|
enum map_entry_type {
|
||||||
MAP_ENTRY_NONE = 0,
|
MAP_ENTRY_NONE = 0,
|
||||||
@@ -12,6 +28,7 @@ enum map_entry_type {
|
|||||||
|
|
||||||
struct map_entry {
|
struct map_entry {
|
||||||
enum map_entry_type e_type;
|
enum map_entry_type e_type;
|
||||||
|
struct map_bucket *e_bucket;
|
||||||
uint64_t e_hash;
|
uint64_t e_hash;
|
||||||
union {
|
union {
|
||||||
fx_queue_entry e_entry;
|
fx_queue_entry e_entry;
|
||||||
@@ -39,8 +56,10 @@ struct type {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct fx_assembly_p {
|
struct fx_assembly_p {
|
||||||
|
fx_assembly *a_self;
|
||||||
const char *a_name;
|
const char *a_name;
|
||||||
struct map a_types;
|
struct map a_types;
|
||||||
|
fx_namemap_entry a_entry;
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
long v_major;
|
long v_major;
|
||||||
@@ -50,6 +69,16 @@ struct fx_assembly_p {
|
|||||||
} a_version;
|
} 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 ********************************************************/
|
/*** PRIVATE FUNCTIONS ********************************************************/
|
||||||
|
|
||||||
FX_BST_DEFINE_SIMPLE_GET(
|
FX_BST_DEFINE_SIMPLE_GET(
|
||||||
@@ -60,6 +89,8 @@ FX_BST_DEFINE_SIMPLE_GET(
|
|||||||
map_get_entry);
|
map_get_entry);
|
||||||
FX_BST_DEFINE_SIMPLE_INSERT(struct map_entry, e_node, e_hash, map_put_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(
|
static struct map_bucket *map_item_convert_to_bucket(
|
||||||
struct map *map,
|
struct map *map,
|
||||||
struct map_item *item)
|
struct map_item *item)
|
||||||
@@ -78,6 +109,28 @@ static struct map_bucket *map_item_convert_to_bucket(
|
|||||||
return 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)
|
static void map_put(struct map *map, const char *name, struct map_item *item)
|
||||||
{
|
{
|
||||||
uint64_t hash = fx_hash_cstr(name);
|
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(
|
static void assembly_set_version(
|
||||||
struct fx_assembly_p *asm,
|
struct fx_assembly_p *assembly,
|
||||||
long major,
|
long major,
|
||||||
long minor,
|
long minor,
|
||||||
long build,
|
long build,
|
||||||
long revision)
|
long revision)
|
||||||
{
|
{
|
||||||
asm->a_version.v_major = major;
|
assembly->a_version.v_major = major;
|
||||||
asm->a_version.v_minor = minor;
|
assembly->a_version.v_minor = minor;
|
||||||
asm->a_version.v_build = build;
|
assembly->a_version.v_build = build;
|
||||||
asm->a_version.v_revision = revision;
|
assembly->a_version.v_revision = revision;
|
||||||
}
|
}
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
static void assembly_add_type(
|
static void assembly_add_type(
|
||||||
struct fx_assembly_p *asm,
|
struct fx_assembly_p *assembly,
|
||||||
const char *full_name,
|
const char *full_name,
|
||||||
fx_type_id type_id)
|
fx_type_id type_id)
|
||||||
{
|
{
|
||||||
struct type *type = malloc(sizeof *type);
|
struct type *type = malloc(sizeof *type);
|
||||||
memset(type, 0x0, 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)
|
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);
|
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("Loaded assembly:\n");
|
||||||
printf(" %s, Version=%ld.%ld.%ld.%ld\n",
|
printf(" %s, Version=%ld.%ld.%ld.%ld\n",
|
||||||
asm->a_name,
|
assembly->a_name,
|
||||||
asm->a_version.v_major,
|
assembly->a_version.v_major,
|
||||||
asm->a_version.v_minor,
|
assembly->a_version.v_minor,
|
||||||
asm->a_version.v_build,
|
assembly->a_version.v_build,
|
||||||
asm->a_version.v_revision);
|
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) {
|
while (cur_node) {
|
||||||
struct map_entry *entry
|
struct map_entry *entry
|
||||||
= fx_unbox(struct map_entry, cur_node, e_node);
|
= 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 *********************************************************/
|
/*** 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_CLASS_DISPATCH_STATIC_V(
|
||||||
FX_REFLECTION_TYPE_ASSEMBLY,
|
FX_REFLECTION_TYPE_ASSEMBLY,
|
||||||
assembly_set_name,
|
assembly_set_name,
|
||||||
asm,
|
assembly,
|
||||||
name);
|
name);
|
||||||
}
|
}
|
||||||
|
|
||||||
void fx_assembly_set_version(
|
void fx_assembly_set_version(
|
||||||
fx_assembly *asm,
|
fx_assembly *assembly,
|
||||||
long major,
|
long major,
|
||||||
long minor,
|
long minor,
|
||||||
long build,
|
long build,
|
||||||
@@ -208,7 +405,7 @@ void fx_assembly_set_version(
|
|||||||
FX_CLASS_DISPATCH_STATIC_V(
|
FX_CLASS_DISPATCH_STATIC_V(
|
||||||
FX_REFLECTION_TYPE_ASSEMBLY,
|
FX_REFLECTION_TYPE_ASSEMBLY,
|
||||||
assembly_set_version,
|
assembly_set_version,
|
||||||
asm,
|
assembly,
|
||||||
major,
|
major,
|
||||||
minor,
|
minor,
|
||||||
build,
|
build,
|
||||||
@@ -216,39 +413,141 @@ void fx_assembly_set_version(
|
|||||||
}
|
}
|
||||||
|
|
||||||
void fx_assembly_add_type(
|
void fx_assembly_add_type(
|
||||||
fx_assembly *asm,
|
fx_assembly *assembly,
|
||||||
const char *full_name,
|
const char *full_name,
|
||||||
fx_type_id type_id)
|
fx_type_id type_id)
|
||||||
{
|
{
|
||||||
FX_CLASS_DISPATCH_STATIC_V(
|
FX_CLASS_DISPATCH_STATIC_V(
|
||||||
FX_REFLECTION_TYPE_ASSEMBLY,
|
FX_REFLECTION_TYPE_ASSEMBLY,
|
||||||
assembly_add_type,
|
assembly_add_type,
|
||||||
asm,
|
assembly,
|
||||||
full_name,
|
full_name,
|
||||||
type_id);
|
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_CLASS_DISPATCH_STATIC_0(
|
||||||
FX_REFLECTION_TYPE_ASSEMBLY,
|
FX_REFLECTION_TYPE_ASSEMBLY,
|
||||||
assembly_dump,
|
assembly_dump,
|
||||||
asm);
|
assembly);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*** VIRTUAL FUNCTIONS ********************************************************/
|
/*** VIRTUAL FUNCTIONS ********************************************************/
|
||||||
|
|
||||||
static void assembly_init(fx_object *obj, void *priv)
|
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)
|
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 *********************************************************/
|
/*** CLASS DEFINITION *********************************************************/
|
||||||
|
|
||||||
// ---- fx_string DEFINITION
|
|
||||||
FX_TYPE_CLASS_BEGIN(fx_assembly)
|
FX_TYPE_CLASS_BEGIN(fx_assembly)
|
||||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||||
FX_INTERFACE_ENTRY(to_string) = NULL;
|
FX_INTERFACE_ENTRY(to_string) = NULL;
|
||||||
@@ -257,8 +556,47 @@ FX_TYPE_CLASS_END(fx_assembly)
|
|||||||
|
|
||||||
FX_TYPE_DEFINITION_BEGIN(fx_assembly)
|
FX_TYPE_DEFINITION_BEGIN(fx_assembly)
|
||||||
FX_TYPE_ID(0xf6690c30, 0x6642, 0x42f0, 0xb79f, 0xe2baf3684b1b);
|
FX_TYPE_ID(0xf6690c30, 0x6642, 0x42f0, 0xb79f, 0xe2baf3684b1b);
|
||||||
|
FX_TYPE_NAME("fx.reflection.assembly");
|
||||||
FX_TYPE_CLASS(fx_assembly_class);
|
FX_TYPE_CLASS(fx_assembly_class);
|
||||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_assembly_p);
|
FX_TYPE_INSTANCE_PRIVATE(struct fx_assembly_p);
|
||||||
FX_TYPE_INSTANCE_INIT(assembly_init);
|
FX_TYPE_INSTANCE_INIT(assembly_init);
|
||||||
FX_TYPE_INSTANCE_FINI(assembly_fini);
|
FX_TYPE_INSTANCE_FINI(assembly_fini);
|
||||||
FX_TYPE_DEFINITION_END(fx_assembly)
|
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/macros.h>
|
||||||
#include <fx/reflection/function.h>
|
#include <fx/reflection/function.h>
|
||||||
#include <fx/string.h>
|
#include <fx/string.h>
|
||||||
|
#include <fx/value-type.h>
|
||||||
#include <fx/value.h>
|
#include <fx/value.h>
|
||||||
#include <platform/callvm.h>
|
#include <platform/callvm.h>
|
||||||
|
|
||||||
@@ -8,8 +9,8 @@ struct fx_function_p {
|
|||||||
char *func_name;
|
char *func_name;
|
||||||
fx_function_flags func_flags;
|
fx_function_flags func_flags;
|
||||||
fx_function_impl func_impl;
|
fx_function_impl func_impl;
|
||||||
fx_value_type func_return_type;
|
fx_type_id func_return_type;
|
||||||
fx_value_type *func_arg_types;
|
fx_type_id *func_arg_types;
|
||||||
/* number of explicit arguments that the function takes.
|
/* number of explicit arguments that the function takes.
|
||||||
* if the FX_FUNCTION_F_VARARG flag is set, the function supports a
|
* if the FX_FUNCTION_F_VARARG flag is set, the function supports a
|
||||||
* variable number of arguments, which may be handled differently by the
|
* variable number of arguments, which may be handled differently by the
|
||||||
@@ -61,6 +62,29 @@ static fx_status function_bind(
|
|||||||
return FX_SUCCESS;
|
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(
|
static fx_status function_invoke(
|
||||||
const struct fx_function_p *func,
|
const struct fx_function_p *func,
|
||||||
const fx_value *args,
|
const fx_value *args,
|
||||||
@@ -84,14 +108,27 @@ static fx_status function_invoke(
|
|||||||
struct callvm vm = {0};
|
struct callvm vm = {0};
|
||||||
callvm_reset(&vm, nr_fixed_args);
|
callvm_reset(&vm, nr_fixed_args);
|
||||||
|
|
||||||
|
size_t param_index = 0;
|
||||||
for (size_t i = 0; i < func->func_nr_bound_args; i++) {
|
for (size_t i = 0; i < func->func_nr_bound_args; i++) {
|
||||||
const fx_value *arg = &func->func_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++) {
|
for (size_t i = 0; i < nr_args; i++) {
|
||||||
const fx_value *arg = &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(
|
*return_value = callvm_invoke(
|
||||||
@@ -109,9 +146,9 @@ fx_function *fx_function_create(
|
|||||||
const char *name,
|
const char *name,
|
||||||
fx_function_flags flags,
|
fx_function_flags flags,
|
||||||
fx_function_impl impl,
|
fx_function_impl impl,
|
||||||
const fx_value_type *args,
|
const fx_type_id *args,
|
||||||
size_t nr_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);
|
fx_function *func = fx_object_create(FX_REFLECTION_TYPE_FUNCTION);
|
||||||
if (!func) {
|
if (!func) {
|
||||||
@@ -122,7 +159,7 @@ fx_function *fx_function_create(
|
|||||||
func,
|
func,
|
||||||
FX_REFLECTION_TYPE_FUNCTION);
|
FX_REFLECTION_TYPE_FUNCTION);
|
||||||
|
|
||||||
if (nr_args == 1 && args[0] == FX_VALUE_TYPE_NONE) {
|
if (nr_args == 1 && args[0] == NULL) {
|
||||||
nr_args = 0;
|
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_CLASS_BEGIN(fx_function)
|
||||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#ifndef FX_REFLECTION_ASSEMBLY_H_
|
#ifndef FX_REFLECTION_ASSEMBLY_H_
|
||||||
#define FX_REFLECTION_ASSEMBLY_H_
|
#define FX_REFLECTION_ASSEMBLY_H_
|
||||||
|
|
||||||
|
#include <fx/iterator.h>
|
||||||
#include <fx/macros.h>
|
#include <fx/macros.h>
|
||||||
|
|
||||||
FX_DECLS_BEGIN;
|
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_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_API void fx_assembly_set_version(
|
||||||
fx_assembly *asm,
|
fx_assembly *assembly,
|
||||||
long major,
|
long major,
|
||||||
long minor,
|
long minor,
|
||||||
long build,
|
long build,
|
||||||
long revision);
|
long revision);
|
||||||
|
|
||||||
FX_API void fx_assembly_add_type(
|
FX_API void fx_assembly_add_type(
|
||||||
fx_assembly *asm,
|
fx_assembly *assembly,
|
||||||
const char *full_name,
|
const char *full_name,
|
||||||
fx_type_id type_id);
|
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;
|
FX_DECLS_END;
|
||||||
|
|
||||||
|
|||||||
@@ -32,9 +32,9 @@ FX_API fx_function *fx_function_create(
|
|||||||
const char *name,
|
const char *name,
|
||||||
fx_function_flags flags,
|
fx_function_flags flags,
|
||||||
fx_function_impl impl,
|
fx_function_impl impl,
|
||||||
const fx_value_type *args,
|
const fx_type_id *args,
|
||||||
size_t nr_args,
|
size_t nr_args,
|
||||||
fx_value_type return_type);
|
fx_type_id return_type);
|
||||||
FX_API fx_status
|
FX_API fx_status
|
||||||
fx_function_bind(fx_function *func, fx_value *args, size_t nr_args);
|
fx_function_bind(fx_function *func, fx_value *args, size_t nr_args);
|
||||||
FX_API fx_status fx_function_invoke(
|
FX_API fx_status fx_function_invoke(
|
||||||
|
|||||||
@@ -1,4 +1,38 @@
|
|||||||
#ifndef FX_REFLECTION_PROPERTY_H_
|
#ifndef FX_REFLECTION_PROPERTY_H_
|
||||||
#define 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
|
#endif
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
#ifndef FX_REFLECTION_TYPE_H_
|
#ifndef FX_REFLECTION_TYPE_H_
|
||||||
#define FX_REFLECTION_TYPE_H_
|
#define FX_REFLECTION_TYPE_H_
|
||||||
|
|
||||||
|
#include <fx/iterator.h>
|
||||||
#include <fx/macros.h>
|
#include <fx/macros.h>
|
||||||
|
#include <fx/reflection/assembly.h>
|
||||||
#include <fx/reflection/function.h>
|
#include <fx/reflection/function.h>
|
||||||
|
#include <fx/reflection/property.h>
|
||||||
|
|
||||||
FX_DECLS_BEGIN;
|
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 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 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(
|
FX_API const fx_function *fx_type_get_function(
|
||||||
const fx_type *ty,
|
const fx_type *ty,
|
||||||
const char *name);
|
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_id(fx_type_id id);
|
||||||
FX_API const fx_type *fx_type_get_by_name(const char *name);
|
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);
|
FX_API fx_type *__fx_type_create(struct fx_type_info *opaque);
|
||||||
|
|
||||||
#endif
|
#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)
|
||||||
|
|||||||
@@ -0,0 +1,240 @@
|
|||||||
|
#include <fx/type.h>
|
||||||
|
#include <fx/value-type.h>
|
||||||
|
#include <fx/value.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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void callvm_push(struct callvm *vm, const fx_value *value)
|
||||||
|
{
|
||||||
|
if (vm->vm_arg_count >= vm->vm_arg_fixed) {
|
||||||
|
push_excess(vm, (uintptr_t)value->v_pointer);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
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));
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
#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 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
|
||||||
|
|
||||||
|
/* 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;
|
||||||
|
};
|
||||||
|
|
||||||
|
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_type_id return_type);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,110 +0,0 @@
|
|||||||
#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;
|
|
||||||
}
|
|
||||||
|
|
||||||
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++;
|
|
||||||
}
|
|
||||||
|
|
||||||
void callvm_push(struct callvm *vm, const fx_value *value)
|
|
||||||
{
|
|
||||||
if (vm->vm_arg_count >= vm->vm_arg_fixed) {
|
|
||||||
push_excess(vm, (uintptr_t)value->v_pointer);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
return FX_VALUE_EMPTY;
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,241 @@
|
|||||||
|
#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));
|
||||||
|
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;
|
||||||
|
}
|
||||||
+1
-1
@@ -32,6 +32,6 @@ extern void callvm_push(struct callvm *vm, const fx_value *value);
|
|||||||
extern fx_value callvm_invoke(
|
extern fx_value callvm_invoke(
|
||||||
struct callvm *vm,
|
struct callvm *vm,
|
||||||
fx_function_impl impl,
|
fx_function_impl impl,
|
||||||
fx_value_type return_type);
|
fx_type_id return_type);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,110 +0,0 @@
|
|||||||
#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)
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
return FX_VALUE_EMPTY;
|
|
||||||
}
|
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
+3
-2
@@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
#define MAX_FIXED_ARGS ((unsigned int)-1)
|
#define MAX_FIXED_ARGS ((unsigned int)-1)
|
||||||
#define MAX_DOUBLE_ARGS 8
|
#define MAX_DOUBLE_ARGS 8
|
||||||
#define MAX_INT_ARGS 8
|
#define MAX_INT_ARGS 6
|
||||||
|
|
||||||
/* dyn-dispatch.S depends on the layout of this struct */
|
/* dyn-dispatch.S depends on the layout of this struct */
|
||||||
struct callvm {
|
struct callvm {
|
||||||
@@ -24,6 +24,7 @@ struct callvm {
|
|||||||
uintptr_t vm_arg_int[MAX_INT_ARGS];
|
uintptr_t vm_arg_int[MAX_INT_ARGS];
|
||||||
double vm_arg_double[MAX_DOUBLE_ARGS];
|
double vm_arg_double[MAX_DOUBLE_ARGS];
|
||||||
uintptr_t *vm_arg_excess;
|
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_reset(struct callvm *vm, unsigned int max_fixed_args);
|
||||||
@@ -31,6 +32,6 @@ extern void callvm_push(struct callvm *vm, const fx_value *value);
|
|||||||
extern fx_value callvm_invoke(
|
extern fx_value callvm_invoke(
|
||||||
struct callvm *vm,
|
struct callvm *vm,
|
||||||
fx_function_impl impl,
|
fx_function_impl impl,
|
||||||
fx_value_type return_type);
|
fx_type_id return_type);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
+337
-6
@@ -1,11 +1,44 @@
|
|||||||
|
#include "../fx/type.h"
|
||||||
|
|
||||||
#include <fx/macros.h>
|
#include <fx/macros.h>
|
||||||
#include <fx/reflection/type.h>
|
#include <fx/reflection/type.h>
|
||||||
#include <fx/type.h>
|
#include <fx/type.h>
|
||||||
|
|
||||||
struct fx_type_registration;
|
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_p {
|
||||||
struct fx_type_info *ty_info;
|
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 ********************************************************/
|
/*** PRIVATE FUNCTIONS ********************************************************/
|
||||||
@@ -16,11 +49,27 @@ extern fx_function *fx_type_info_get_function_by_name(
|
|||||||
const struct fx_type_info *ty,
|
const struct fx_type_info *ty,
|
||||||
const char *name);
|
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)
|
static const char *type_get_name(const struct fx_type_p *ty)
|
||||||
{
|
{
|
||||||
return ty->ty_info->ty_name;
|
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 fx_function *type_get_function(
|
||||||
const struct fx_type_p *ty,
|
const struct fx_type_p *ty,
|
||||||
const char *name)
|
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);
|
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 *********************************************************/
|
/*** PUBLIC FUNCTIONS *********************************************************/
|
||||||
|
|
||||||
fx_type *__fx_type_create(struct fx_type_info *type_info)
|
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;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct fx_type_p *p = fx_object_get_private(
|
struct fx_type_p *p
|
||||||
out,
|
= fx_object_get_private(out, FX_REFLECTION_TYPE_TYPE);
|
||||||
FX_REFLECTION_TYPE_TYPE);
|
|
||||||
p->ty_info = type_info;
|
p->ty_info = type_info;
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
const char *fx_type_get_name(const fx_type *ty)
|
||||||
{
|
{
|
||||||
FX_CLASS_DISPATCH_STATIC_0(FX_REFLECTION_TYPE_TYPE, type_get_name, 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)
|
const fx_function *fx_type_get_function(const fx_type *ty, const char *name)
|
||||||
{
|
{
|
||||||
FX_CLASS_DISPATCH_STATIC(
|
FX_CLASS_DISPATCH_STATIC(
|
||||||
@@ -59,6 +225,31 @@ const fx_function *fx_type_get_function(const fx_type *ty, const char *name)
|
|||||||
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)
|
const fx_type *fx_type_get_by_id(fx_type_id id)
|
||||||
{
|
{
|
||||||
struct fx_type_info *ty = fx_type_info_get_by_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;
|
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 ********************************************************/
|
/*** VIRTUAL FUNCTIONS ********************************************************/
|
||||||
|
|
||||||
static void type_init(fx_object *obj, void *priv)
|
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 *********************************************************/
|
/*** CLASS DEFINITION *********************************************************/
|
||||||
|
|
||||||
FX_TYPE_CLASS_BEGIN(fx_type)
|
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_CLASS_END(fx_type)
|
||||||
|
|
||||||
FX_TYPE_DEFINITION_BEGIN(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_INIT(type_init);
|
||||||
FX_TYPE_INSTANCE_FINI(type_fini);
|
FX_TYPE_INSTANCE_FINI(type_fini);
|
||||||
FX_TYPE_DEFINITION_END(fx_type)
|
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(
|
static struct fx_error *bitcode_serialise(
|
||||||
fx_serial_ctx *serial,
|
fx_serial_ctx *serial,
|
||||||
fx_object *src,
|
const fx_value *src,
|
||||||
fx_stream *dest,
|
fx_stream *dest,
|
||||||
enum fx_serial_flags flags)
|
enum fx_serial_flags flags)
|
||||||
{
|
{
|
||||||
@@ -15,7 +15,7 @@ static struct fx_error *bitcode_serialise(
|
|||||||
static struct fx_error *bitcode_deserialise(
|
static struct fx_error *bitcode_deserialise(
|
||||||
fx_serial_ctx *serial,
|
fx_serial_ctx *serial,
|
||||||
fx_stream *src,
|
fx_stream *src,
|
||||||
fx_object **dest,
|
fx_value *dest,
|
||||||
enum fx_serial_flags flags)
|
enum fx_serial_flags flags)
|
||||||
{
|
{
|
||||||
return FX_RESULT_ERR(NOT_SUPPORTED);
|
return FX_RESULT_ERR(NOT_SUPPORTED);
|
||||||
|
|||||||
+2
-2
@@ -44,7 +44,7 @@ FX_TYPE_DEFINITION_END(fx_serial_ctx)
|
|||||||
|
|
||||||
fx_result fx_serial_ctx_serialise(
|
fx_result fx_serial_ctx_serialise(
|
||||||
fx_serial_ctx *ctx,
|
fx_serial_ctx *ctx,
|
||||||
fx_object *src,
|
const fx_value *src,
|
||||||
fx_stream *dest,
|
fx_stream *dest,
|
||||||
enum fx_serial_flags flags)
|
enum fx_serial_flags flags)
|
||||||
{
|
{
|
||||||
@@ -62,7 +62,7 @@ fx_result fx_serial_ctx_serialise(
|
|||||||
fx_result fx_serial_ctx_deserialise(
|
fx_result fx_serial_ctx_deserialise(
|
||||||
fx_serial_ctx *ctx,
|
fx_serial_ctx *ctx,
|
||||||
fx_stream *src,
|
fx_stream *src,
|
||||||
fx_object **dest,
|
fx_value *dest,
|
||||||
enum fx_serial_flags flags)
|
enum fx_serial_flags flags)
|
||||||
{
|
{
|
||||||
FX_CLASS_DISPATCH_VIRTUAL(
|
FX_CLASS_DISPATCH_VIRTUAL(
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
#include <fx/object.h>
|
#include <fx/object.h>
|
||||||
#include <fx/status.h>
|
#include <fx/status.h>
|
||||||
#include <fx/stream.h>
|
#include <fx/stream.h>
|
||||||
|
#include <fx/value.h>
|
||||||
|
|
||||||
FX_DECLS_BEGIN;
|
FX_DECLS_BEGIN;
|
||||||
|
|
||||||
@@ -21,13 +22,13 @@ FX_DECLARE_TYPE(fx_serial_ctx);
|
|||||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_serial_ctx)
|
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_serial_ctx)
|
||||||
fx_result (*s_serialise)(
|
fx_result (*s_serialise)(
|
||||||
fx_serial_ctx *,
|
fx_serial_ctx *,
|
||||||
fx_object *,
|
const fx_value *,
|
||||||
fx_stream *,
|
fx_stream *,
|
||||||
fx_serial_flags);
|
fx_serial_flags);
|
||||||
fx_result (*s_deserialise)(
|
fx_result (*s_deserialise)(
|
||||||
fx_serial_ctx *,
|
fx_serial_ctx *,
|
||||||
fx_stream *,
|
fx_stream *,
|
||||||
fx_object **,
|
fx_value *,
|
||||||
fx_serial_flags);
|
fx_serial_flags);
|
||||||
FX_TYPE_CLASS_DECLARATION_END(fx_serial_ctx)
|
FX_TYPE_CLASS_DECLARATION_END(fx_serial_ctx)
|
||||||
|
|
||||||
@@ -39,14 +40,14 @@ FX_API fx_type_id fx_serial_ctx_get_type(void);
|
|||||||
|
|
||||||
FX_API fx_result fx_serial_ctx_serialise(
|
FX_API fx_result fx_serial_ctx_serialise(
|
||||||
fx_serial_ctx *ctx,
|
fx_serial_ctx *ctx,
|
||||||
fx_object *src,
|
const fx_value *src,
|
||||||
fx_stream *dest,
|
fx_stream *dest,
|
||||||
fx_serial_flags flags);
|
fx_serial_flags flags);
|
||||||
|
|
||||||
FX_API fx_result fx_serial_ctx_deserialise(
|
FX_API fx_result fx_serial_ctx_deserialise(
|
||||||
fx_serial_ctx *ctx,
|
fx_serial_ctx *ctx,
|
||||||
fx_stream *src,
|
fx_stream *src,
|
||||||
fx_object **dest,
|
fx_value *dest,
|
||||||
fx_serial_flags flags);
|
fx_serial_flags flags);
|
||||||
|
|
||||||
FX_DECLS_END;
|
FX_DECLS_END;
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
#include <fx/collections/array.h>
|
#include <fx/collections/array.h>
|
||||||
#include <fx/collections/dict.h>
|
|
||||||
#include <fx/int.h>
|
#include <fx/int.h>
|
||||||
#include <fx/serial/ctx.h>
|
#include <fx/serial/ctx.h>
|
||||||
#include <fx/serial/toml.h>
|
#include <fx/serial/toml.h>
|
||||||
@@ -9,6 +8,7 @@
|
|||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
|
#if 0
|
||||||
fx_serial_ctx *ctx = fx_toml_serial_ctx_create();
|
fx_serial_ctx *ctx = fx_toml_serial_ctx_create();
|
||||||
|
|
||||||
fx_dict *dict = fx_dict_create();
|
fx_dict *dict = fx_dict_create();
|
||||||
@@ -33,6 +33,7 @@ int main(void)
|
|||||||
|
|
||||||
fx_dict_unref(dict);
|
fx_dict_unref(dict);
|
||||||
fx_serial_ctx_unref(ctx);
|
fx_serial_ctx_unref(ctx);
|
||||||
|
#endif
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,23 +1,26 @@
|
|||||||
#include <fx/bool.h>
|
#include <fx/bool.h>
|
||||||
#include <fx/collections/array.h>
|
#include <fx/collections/array.h>
|
||||||
#include <fx/collections/datetime.h>
|
#include <fx/collections/hashtable.h>
|
||||||
#include <fx/collections/dict.h>
|
#include <fx/datetime.h>
|
||||||
#include <fx/double.h>
|
#include <fx/float.h>
|
||||||
#include <fx/int.h>
|
#include <fx/int.h>
|
||||||
#include <fx/serial/ctx.h>
|
#include <fx/serial/ctx.h>
|
||||||
#include <fx/serial/toml.h>
|
#include <fx/serial/toml.h>
|
||||||
#include <fx/string.h>
|
#include <fx/string.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
void write_tagged_value(fx_object *data);
|
void write_tagged_value(const fx_value *data);
|
||||||
|
|
||||||
void write_raw_string(const fx_string *data)
|
void write_raw_string(const fx_string *data)
|
||||||
{
|
{
|
||||||
fx_stream_write_cstr(fx_stdout, "\"", NULL);
|
fx_stream_write_cstr(fx_stdout, "\"", NULL);
|
||||||
|
|
||||||
const fx_iterator *it = fx_iterator_cbegin(data);
|
const fx_iterator *it = fx_iterator_begin(data);
|
||||||
fx_foreach_c(fx_wchar, c, it)
|
fx_foreach(val, it)
|
||||||
{
|
{
|
||||||
|
fx_wchar c;
|
||||||
|
fx_value_get_wchar(val, &c);
|
||||||
if (c >= 0x10000) {
|
if (c >= 0x10000) {
|
||||||
c -= 0x10000;
|
c -= 0x10000;
|
||||||
long hi = 0xD800 | ((c >> 10) & 0x3FF);
|
long hi = 0xD800 | ((c >> 10) & 0x3FF);
|
||||||
@@ -53,68 +56,48 @@ void write_tagged_string(fx_string *data)
|
|||||||
fx_stream_write_cstr(fx_stdout, " }", NULL);
|
fx_stream_write_cstr(fx_stdout, " }", NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void write_tagged_integer(fx_int *data)
|
void write_tagged_integer(const fx_value *data)
|
||||||
{
|
{
|
||||||
fx_stream_write_cstr(
|
fx_stream_write_cstr(
|
||||||
fx_stdout,
|
fx_stdout,
|
||||||
"{ \"type\": \"integer\", \"value\": \"",
|
"{ \"type\": \"integer\", \"value\": \"",
|
||||||
NULL);
|
NULL);
|
||||||
|
fx_stream_write_fmt(fx_stdout, NULL, "%lld", data->v_i64, NULL);
|
||||||
if (fx_int_is_inf_positive(data)) {
|
|
||||||
fx_stream_write_cstr(fx_stdout, "inf", NULL);
|
|
||||||
} else if (fx_int_is_inf_negative(data)) {
|
|
||||||
fx_stream_write_cstr(fx_stdout, "-inf", NULL);
|
|
||||||
} else if (fx_int_is_nan_positive(data)) {
|
|
||||||
fx_stream_write_cstr(fx_stdout, "nan", NULL);
|
|
||||||
} else if (fx_int_is_nan_negative(data)) {
|
|
||||||
fx_stream_write_cstr(fx_stdout, "-nan", NULL);
|
|
||||||
} else {
|
|
||||||
fx_stream_write_fmt(
|
|
||||||
fx_stdout,
|
|
||||||
NULL,
|
|
||||||
"%lld",
|
|
||||||
fx_int_get_value(data),
|
|
||||||
NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
fx_stream_write_cstr(fx_stdout, "\" }", NULL);
|
fx_stream_write_cstr(fx_stdout, "\" }", NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void write_tagged_float(fx_double *data)
|
void write_tagged_float(const fx_value *data)
|
||||||
{
|
{
|
||||||
fx_stream_write_cstr(
|
fx_stream_write_cstr(
|
||||||
fx_stdout,
|
fx_stdout,
|
||||||
"{ \"type\": \"float\", \"value\": \"",
|
"{ \"type\": \"float\", \"value\": \"",
|
||||||
NULL);
|
NULL);
|
||||||
|
|
||||||
if (fx_double_is_inf_positive(data)) {
|
double d = data->v_double;
|
||||||
|
if (d == INFINITY) {
|
||||||
fx_stream_write_cstr(fx_stdout, "inf", NULL);
|
fx_stream_write_cstr(fx_stdout, "inf", NULL);
|
||||||
} else if (fx_double_is_inf_negative(data)) {
|
} else if (d == -INFINITY) {
|
||||||
fx_stream_write_cstr(fx_stdout, "-inf", NULL);
|
fx_stream_write_cstr(fx_stdout, "-inf", NULL);
|
||||||
} else if (fx_double_is_nan_positive(data)) {
|
} else if (d == NAN) {
|
||||||
fx_stream_write_cstr(fx_stdout, "nan", NULL);
|
fx_stream_write_cstr(fx_stdout, "nan", NULL);
|
||||||
} else if (fx_double_is_nan_negative(data)) {
|
} else if (d == -NAN) {
|
||||||
fx_stream_write_cstr(fx_stdout, "-nan", NULL);
|
fx_stream_write_cstr(fx_stdout, "-nan", NULL);
|
||||||
|
} else if (d <= 1e-9 || d >= 1e9) {
|
||||||
|
fx_stream_write_fmt(fx_stdout, NULL, "%g", d, NULL);
|
||||||
} else {
|
} else {
|
||||||
double v = fx_double_get_value(data);
|
fx_stream_write_fmt(fx_stdout, NULL, "%.9lf", d, NULL);
|
||||||
if ((v <= 0.00000001 && v > 0) || (v >= -0.00000001 && v < 0)
|
|
||||||
|| (v >= 1000000000) || (v <= -1000000000)) {
|
|
||||||
fx_stream_write_fmt(fx_stdout, NULL, "%.15e", v, NULL);
|
|
||||||
} else {
|
|
||||||
fx_stream_write_fmt(fx_stdout, NULL, "%.15f", v, NULL);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fx_stream_write_cstr(fx_stdout, "\" }", NULL);
|
fx_stream_write_cstr(fx_stdout, "\" }", NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void write_tagged_bool(fx_bool *data)
|
void write_tagged_bool(const fx_value *data)
|
||||||
{
|
{
|
||||||
fx_stream_write_fmt(
|
fx_stream_write_fmt(
|
||||||
fx_stdout,
|
fx_stdout,
|
||||||
NULL,
|
NULL,
|
||||||
"{ \"type\": \"bool\", \"value\": \"%s\" }",
|
"{ \"type\": \"bool\", \"value\": \"%s\" }",
|
||||||
fx_bool_get_value(data) ? "true" : "false",
|
data->v_bool ? "true" : "false",
|
||||||
NULL);
|
NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,31 +128,37 @@ void write_tagged_datetime(fx_datetime *data)
|
|||||||
|
|
||||||
fx_stream_write_cstr(fx_stdout, "\", \"value\": \"", NULL);
|
fx_stream_write_cstr(fx_stdout, "\", \"value\": \"", NULL);
|
||||||
|
|
||||||
fx_string *new_data = fx_string_create();
|
fx_stringstream *new_data = fx_stringstream_create();
|
||||||
fx_datetime_to_string(data, FX_DATETIME_FORMAT_RFC3339, new_data);
|
fx_datetime_to_string(data, FX_DATETIME_FORMAT_RFC3339, new_data);
|
||||||
fx_stream_write_cstr(fx_stdout, fx_string_get_cstr(new_data), NULL);
|
fx_stream_write_cstr(fx_stdout, fx_stringstream_ptr(new_data), NULL);
|
||||||
|
|
||||||
fx_stream_write_cstr(fx_stdout, "\" }", NULL);
|
fx_stream_write_cstr(fx_stdout, "\" }", NULL);
|
||||||
|
|
||||||
fx_string_unref(new_data);
|
fx_stringstream_unref(new_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
void write_tagged_dict(fx_dict *data)
|
void write_tagged_hashtable(fx_hashtable *data)
|
||||||
{
|
{
|
||||||
fx_stream_write_cstr(fx_stdout, "{ ", NULL);
|
fx_stream_write_cstr(fx_stdout, "{ ", NULL);
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
fx_iterator *it = fx_iterator_begin(data);
|
const fx_iterator *it = fx_iterator_begin(data);
|
||||||
fx_foreach(fx_dict_item *, item, it)
|
fx_foreach(v, it)
|
||||||
{
|
{
|
||||||
|
fx_hashtable_item *item;
|
||||||
|
fx_value_get_object(v, &item);
|
||||||
if (i++ > 0) {
|
if (i++ > 0) {
|
||||||
fx_stream_write_cstr(fx_stdout, ", ", NULL);
|
fx_stream_write_cstr(fx_stdout, ", ", NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
write_raw_string(item->key);
|
const fx_value *key = fx_hashtable_item_get_key(item);
|
||||||
|
const fx_value *value = fx_hashtable_item_get_value(item);
|
||||||
|
fx_string *key_str;
|
||||||
|
fx_value_get_object(key, &key_str);
|
||||||
|
write_raw_string(key_str);
|
||||||
fx_stream_write_cstr(fx_stdout, ": ", NULL);
|
fx_stream_write_cstr(fx_stdout, ": ", NULL);
|
||||||
write_tagged_value(item->value);
|
write_tagged_value(value);
|
||||||
}
|
}
|
||||||
fx_iterator_unref(it);
|
fx_iterator_unref(it);
|
||||||
|
|
||||||
@@ -181,35 +170,35 @@ void write_tagged_array(fx_array *data)
|
|||||||
fx_stream_write_cstr(fx_stdout, "[ ", NULL);
|
fx_stream_write_cstr(fx_stdout, "[ ", NULL);
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
fx_iterator *it = fx_iterator_begin(data);
|
const fx_iterator *it = fx_iterator_begin(data);
|
||||||
fx_foreach(fx_object *, obj, it)
|
fx_foreach(val, it)
|
||||||
{
|
{
|
||||||
if (i++ > 0) {
|
if (i++ > 0) {
|
||||||
fx_stream_write_cstr(fx_stdout, ", ", NULL);
|
fx_stream_write_cstr(fx_stdout, ", ", NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
write_tagged_value(obj);
|
write_tagged_value(val);
|
||||||
}
|
}
|
||||||
fx_iterator_unref(it);
|
fx_iterator_unref(it);
|
||||||
|
|
||||||
fx_stream_write_cstr(fx_stdout, " ]", NULL);
|
fx_stream_write_cstr(fx_stdout, " ]", NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void write_tagged_value(fx_object *data)
|
void write_tagged_value(const fx_value *data)
|
||||||
{
|
{
|
||||||
if (fx_object_is_type(data, FX_TYPE_DICT)) {
|
if (fx_value_is_type(data, FX_TYPE_HASHTABLE)) {
|
||||||
write_tagged_dict(data);
|
write_tagged_hashtable(data->v_object);
|
||||||
} else if (fx_object_is_type(data, FX_TYPE_ARRAY)) {
|
} else if (fx_value_is_type(data, FX_TYPE_ARRAY)) {
|
||||||
write_tagged_array(data);
|
write_tagged_array(data->v_object);
|
||||||
} else if (fx_object_is_type(data, FX_TYPE_STRING)) {
|
} else if (fx_value_is_type(data, FX_TYPE_STRING)) {
|
||||||
write_tagged_string(data);
|
write_tagged_string(data->v_object);
|
||||||
} else if (fx_object_is_type(data, FX_TYPE_DATETIME)) {
|
} else if (fx_value_is_type(data, FX_TYPE_DATETIME)) {
|
||||||
write_tagged_datetime(data);
|
write_tagged_datetime(data->v_object);
|
||||||
} else if (fx_object_is_type(data, FX_TYPE_INT)) {
|
} else if (fx_value_is_type(data, FX_TYPE_BOOL)) {
|
||||||
write_tagged_integer(data);
|
|
||||||
} else if (fx_object_is_type(data, FX_TYPE_BOOL)) {
|
|
||||||
write_tagged_bool(data);
|
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);
|
write_tagged_float(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -221,19 +210,18 @@ int main(void)
|
|||||||
|
|
||||||
fx_serial_ctx *ctx = fx_toml_serial_ctx_create();
|
fx_serial_ctx *ctx = fx_toml_serial_ctx_create();
|
||||||
|
|
||||||
fx_object *data;
|
fx_value data;
|
||||||
fx_result result = fx_serial_ctx_deserialise(ctx, src, &data, 0);
|
fx_result result = fx_serial_ctx_deserialise(ctx, src, &data, 0);
|
||||||
if (fx_result_is_error(result)) {
|
if (fx_result_is_error(result)) {
|
||||||
fx_throw(result);
|
return 1;
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
write_tagged_value(data);
|
write_tagged_value(&data);
|
||||||
|
|
||||||
fx_stream_write_char(fx_stdout, '\n');
|
fx_stream_write_char(fx_stdout, '\n');
|
||||||
|
|
||||||
fx_serial_ctx_unref(ctx);
|
fx_serial_ctx_unref(ctx);
|
||||||
fx_object_unref(data);
|
fx_value_unset(&data);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
+198
-179
@@ -1,17 +1,17 @@
|
|||||||
#include <fx/bool.h>
|
#include <fx/bool.h>
|
||||||
#include <fx/collections/array.h>
|
#include <fx/collections/array.h>
|
||||||
#include <fx/collections/datetime.h>
|
|
||||||
#include <fx/collections/dict.h>
|
|
||||||
#include <fx/collections/hashmap.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/error.h>
|
||||||
|
#include <fx/float.h>
|
||||||
#include <fx/int.h>
|
#include <fx/int.h>
|
||||||
#include <fx/serial/ctx.h>
|
#include <fx/serial/ctx.h>
|
||||||
#include <fx/serial/toml.h>
|
#include <fx/serial/toml.h>
|
||||||
#include <fx/status.h>
|
#include <fx/status.h>
|
||||||
#include <fx/string.h>
|
#include <fx/string.h>
|
||||||
#include <fx/stringstream.h>
|
#include <fx/stringstream.h>
|
||||||
#include <fx/uint.h>
|
#include <math.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -117,9 +117,9 @@ struct ctx {
|
|||||||
fx_stream *ctx_src;
|
fx_stream *ctx_src;
|
||||||
fx_string *ctx_wordbuf;
|
fx_string *ctx_wordbuf;
|
||||||
fx_string *ctx_linebuf;
|
fx_string *ctx_linebuf;
|
||||||
fx_iterator *ctx_linebuf_ptr;
|
const fx_iterator *ctx_linebuf_ptr;
|
||||||
fx_result ctx_result;
|
fx_result ctx_result;
|
||||||
fx_hashmap *ctx_objects_flags;
|
fx_hashtable *ctx_objects_flags;
|
||||||
|
|
||||||
fx_queue ctx_tokens;
|
fx_queue ctx_tokens;
|
||||||
};
|
};
|
||||||
@@ -133,28 +133,18 @@ static void ctx_set_object_flags(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
fx_hashmap_key key = {
|
fx_value ptr = FX_POINTER(obj);
|
||||||
.key_data = obj,
|
const fx_value *old_value
|
||||||
.key_size = sizeof(fx_object *),
|
= fx_hashtable_get(ctx->ctx_objects_flags, &ptr);
|
||||||
.key_flags = FX_HASHMAP_KEY_F_INTVALUE,
|
|
||||||
};
|
|
||||||
|
|
||||||
const fx_hashmap_value *old_value
|
|
||||||
= fx_hashmap_get(ctx->ctx_objects_flags, &key);
|
|
||||||
|
|
||||||
enum object_flags new_flags = 0;
|
enum object_flags new_flags = 0;
|
||||||
if (old_value) {
|
if (old_value) {
|
||||||
new_flags = (enum object_flags)(uintptr_t)old_value->value_data;
|
new_flags = (enum object_flags)old_value->v_i32;
|
||||||
}
|
}
|
||||||
|
|
||||||
new_flags |= flags;
|
new_flags |= flags;
|
||||||
|
|
||||||
fx_hashmap_value value = {
|
fx_hashtable_put(ctx->ctx_objects_flags, &ptr, &FX_I32(new_flags));
|
||||||
.value_data = (void *)new_flags,
|
|
||||||
.value_size = sizeof new_flags,
|
|
||||||
};
|
|
||||||
|
|
||||||
fx_hashmap_put(ctx->ctx_objects_flags, &key, &value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ctx_clear_object_flags(
|
static void ctx_clear_object_flags(
|
||||||
@@ -166,28 +156,18 @@ static void ctx_clear_object_flags(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
fx_hashmap_key key = {
|
fx_value ptr = FX_POINTER(obj);
|
||||||
.key_data = obj,
|
const fx_value *old_value
|
||||||
.key_size = sizeof(fx_object *),
|
= fx_hashtable_get(ctx->ctx_objects_flags, &ptr);
|
||||||
.key_flags = FX_HASHMAP_KEY_F_INTVALUE,
|
|
||||||
};
|
|
||||||
|
|
||||||
const fx_hashmap_value *old_value
|
|
||||||
= fx_hashmap_get(ctx->ctx_objects_flags, &key);
|
|
||||||
|
|
||||||
enum object_flags new_flags = 0;
|
enum object_flags new_flags = 0;
|
||||||
if (old_value) {
|
if (old_value) {
|
||||||
new_flags = (enum object_flags)(uintptr_t)old_value->value_data;
|
new_flags = (enum object_flags)old_value->v_i32;
|
||||||
}
|
}
|
||||||
|
|
||||||
new_flags &= ~mask;
|
new_flags &= ~mask;
|
||||||
|
|
||||||
fx_hashmap_value value = {
|
fx_hashtable_put(ctx->ctx_objects_flags, &ptr, &FX_I32(new_flags));
|
||||||
.value_data = (void *)new_flags,
|
|
||||||
.value_size = sizeof new_flags,
|
|
||||||
};
|
|
||||||
|
|
||||||
fx_hashmap_put(ctx->ctx_objects_flags, &key, &value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static enum object_flags ctx_get_object_flags(struct ctx *ctx, fx_object *obj)
|
static enum object_flags ctx_get_object_flags(struct ctx *ctx, fx_object *obj)
|
||||||
@@ -196,16 +176,13 @@ static enum object_flags ctx_get_object_flags(struct ctx *ctx, fx_object *obj)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
fx_hashmap_key key = {
|
fx_value ptr = FX_POINTER(obj);
|
||||||
.key_data = obj,
|
const fx_value *old_value
|
||||||
.key_size = sizeof(fx_object *),
|
= fx_hashtable_get(ctx->ctx_objects_flags, &ptr);
|
||||||
.key_flags = FX_HASHMAP_KEY_F_INTVALUE,
|
|
||||||
};
|
|
||||||
|
|
||||||
const fx_hashmap_value *value
|
enum object_flags new_flags = 0;
|
||||||
= fx_hashmap_get(ctx->ctx_objects_flags, &key);
|
if (old_value) {
|
||||||
if (value) {
|
return (enum object_flags)old_value->v_i32;
|
||||||
return (enum object_flags)(uintptr_t)value->value_data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@@ -295,7 +272,7 @@ static fx_wchar advance_char(struct ctx *ctx)
|
|||||||
|
|
||||||
const char *s = fx_string_get_cstr(ctx->ctx_linebuf);
|
const char *s = fx_string_get_cstr(ctx->ctx_linebuf);
|
||||||
|
|
||||||
fx_wchar c = fx_iterator_get_value(ctx->ctx_linebuf_ptr).v_int;
|
fx_wchar c = fx_iterator_get_value(ctx->ctx_linebuf_ptr)->v_wchar;
|
||||||
|
|
||||||
if (!is_valid_char(c)) {
|
if (!is_valid_char(c)) {
|
||||||
ctx->ctx_result = FX_RESULT_ERR(BAD_FORMAT);
|
ctx->ctx_result = FX_RESULT_ERR(BAD_FORMAT);
|
||||||
@@ -328,7 +305,7 @@ static fx_wchar peek_char(struct ctx *ctx)
|
|||||||
|
|
||||||
const char *s = fx_string_get_cstr(ctx->ctx_linebuf);
|
const char *s = fx_string_get_cstr(ctx->ctx_linebuf);
|
||||||
|
|
||||||
fx_wchar c = fx_iterator_get_value(ctx->ctx_linebuf_ptr).v_int;
|
fx_wchar c = fx_iterator_get_value(ctx->ctx_linebuf_ptr)->v_wchar;
|
||||||
|
|
||||||
if (!is_valid_char(c)) {
|
if (!is_valid_char(c)) {
|
||||||
ctx->ctx_result = FX_RESULT_ERR(BAD_FORMAT);
|
ctx->ctx_result = FX_RESULT_ERR(BAD_FORMAT);
|
||||||
@@ -924,8 +901,11 @@ static void split_word(struct ctx *ctx, fx_string *wordbuf)
|
|||||||
FX_STRING_TOK_F_INCLUDE_EMPTY_TOKENS);
|
FX_STRING_TOK_F_INCLUDE_EMPTY_TOKENS);
|
||||||
|
|
||||||
size_t i = 0;
|
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) {
|
if (i > 0) {
|
||||||
enqueue_token(ctx, TOK_DOT);
|
enqueue_token(ctx, TOK_DOT);
|
||||||
}
|
}
|
||||||
@@ -1027,9 +1007,11 @@ static void read_word(struct ctx *ctx)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
fx_iterator *it = fx_iterator_begin(wordbuf);
|
const fx_iterator *it = fx_iterator_begin(wordbuf);
|
||||||
fx_foreach(fx_wchar, c, it)
|
fx_foreach(cv, it)
|
||||||
{
|
{
|
||||||
|
fx_wchar c = FX_WCHAR_INVALID;
|
||||||
|
fx_value_get_wchar(cv, &c);
|
||||||
/* only allow ASCII numbers/letters here */
|
/* only allow ASCII numbers/letters here */
|
||||||
bool ok = isalnum(c) || c == '_' || c == '-' || c == '.';
|
bool ok = isalnum(c) || c == '_' || c == '-' || c == '.';
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
@@ -1473,14 +1455,24 @@ static fx_result advance_token(struct ctx *ctx)
|
|||||||
|
|
||||||
start:
|
start:
|
||||||
c = peek_char(ctx);
|
c = peek_char(ctx);
|
||||||
while (isspace(c) && c != '\n' && c != '\r') {
|
|
||||||
advance_char(ctx);
|
if (c <= 0) {
|
||||||
c = peek_char(ctx);
|
if (fx_error_get_status_code(ctx->ctx_result)
|
||||||
|
== FX_ERR_NO_DATA) {
|
||||||
|
ctx->ctx_flags |= CTX_EOF;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (c == -1) {
|
return ctx->ctx_result;
|
||||||
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
|
#if 1
|
||||||
@@ -1550,7 +1542,7 @@ static void ctx_cleanup(struct ctx *ctx)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (ctx->ctx_objects_flags) {
|
if (ctx->ctx_objects_flags) {
|
||||||
fx_hashmap_unref(ctx->ctx_objects_flags);
|
fx_hashtable_unref(ctx->ctx_objects_flags);
|
||||||
ctx->ctx_objects_flags = NULL;
|
ctx->ctx_objects_flags = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1562,14 +1554,14 @@ static fx_result ctx_init(struct ctx *ctx)
|
|||||||
ctx->ctx_linebuf = fx_string_create();
|
ctx->ctx_linebuf = fx_string_create();
|
||||||
ctx->ctx_wordbuf = fx_string_create();
|
ctx->ctx_wordbuf = fx_string_create();
|
||||||
|
|
||||||
ctx->ctx_objects_flags = fx_hashmap_create(NULL, NULL);
|
ctx->ctx_objects_flags = fx_hashtable_create();
|
||||||
|
|
||||||
return FX_RESULT_SUCCESS;
|
return FX_RESULT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static fx_result toml_serialise(
|
static fx_result toml_serialise(
|
||||||
fx_serial_ctx *serial,
|
fx_serial_ctx *serial,
|
||||||
fx_object *src,
|
const fx_value *src,
|
||||||
fx_stream *dest,
|
fx_stream *dest,
|
||||||
enum fx_serial_flags flags)
|
enum fx_serial_flags flags)
|
||||||
{
|
{
|
||||||
@@ -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_value(struct ctx *ctx, fx_value *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);
|
||||||
|
|
||||||
static fx_result parse_timestamp(struct ctx *ctx, fx_object **result)
|
static fx_result parse_timestamp(struct ctx *ctx, fx_value *result)
|
||||||
{
|
{
|
||||||
struct token *tok = peek_token(ctx);
|
struct token *tok = peek_token(ctx);
|
||||||
fx_datetime *dt = tok->tok_value.time;
|
fx_datetime *dt = tok->tok_value.time;
|
||||||
tok->tok_value.time = NULL;
|
tok->tok_value.time = NULL;
|
||||||
|
|
||||||
*result = (dt);
|
*result = FX_VALUE_OBJECT(dt);
|
||||||
return FX_RESULT_SUCCESS;
|
return FX_RESULT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static fx_result parse_string(struct ctx *ctx, fx_object **result)
|
static fx_result parse_string(struct ctx *ctx, fx_value *result)
|
||||||
{
|
{
|
||||||
struct token *tok = peek_token(ctx);
|
struct token *tok = peek_token(ctx);
|
||||||
fx_string *str = fx_string_duplicate(tok->tok_str);
|
fx_string *str = fx_string_duplicate(tok->tok_str);
|
||||||
@@ -1684,88 +1676,61 @@ static fx_result parse_string(struct ctx *ctx, fx_object **result)
|
|||||||
return FX_RESULT_ERR(NO_MEMORY);
|
return FX_RESULT_ERR(NO_MEMORY);
|
||||||
}
|
}
|
||||||
|
|
||||||
*result = (str);
|
*result = FX_VALUE_OBJECT(str);
|
||||||
return FX_RESULT_SUCCESS;
|
return FX_RESULT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static fx_result parse_int(struct ctx *ctx, fx_object **result)
|
static fx_result parse_int(struct ctx *ctx, fx_value *result)
|
||||||
{
|
{
|
||||||
struct token *tok = peek_token(ctx);
|
struct token *tok = peek_token(ctx);
|
||||||
fx_int *val = fx_int_create(tok->tok_value.i.v);
|
*result = FX_I64(tok->tok_value.i.v);
|
||||||
if (!val) {
|
|
||||||
return FX_RESULT_ERR(NO_MEMORY);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tok->tok_value.i.inf) {
|
|
||||||
if (tok->tok_value.i.v >= 0) {
|
|
||||||
fx_int_set_value_inf(val);
|
|
||||||
} else {
|
|
||||||
fx_int_set_value_inf_negative(val);
|
|
||||||
}
|
|
||||||
} else if (tok->tok_value.i.nan) {
|
|
||||||
if (tok->tok_value.i.v >= 0) {
|
|
||||||
fx_int_set_value_nan(val);
|
|
||||||
} else {
|
|
||||||
fx_int_set_value_nan_negative(val);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
*result = (val);
|
|
||||||
return FX_RESULT_SUCCESS;
|
return FX_RESULT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static fx_result parse_float(struct ctx *ctx, fx_object **result)
|
static fx_result parse_float(struct ctx *ctx, fx_value *result)
|
||||||
{
|
{
|
||||||
struct token *tok = peek_token(ctx);
|
struct token *tok = peek_token(ctx);
|
||||||
fx_double *val = fx_double_create(tok->tok_value.f.v);
|
|
||||||
if (!val) {
|
|
||||||
return FX_RESULT_ERR(NO_MEMORY);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tok->tok_value.f.inf) {
|
if (tok->tok_value.f.inf) {
|
||||||
if (tok->tok_value.f.v >= 0) {
|
if (tok->tok_value.f.v >= 0) {
|
||||||
fx_double_set_value_inf(val);
|
*result = FX_DOUBLE(INFINITY);
|
||||||
} else {
|
} else {
|
||||||
fx_double_set_value_inf_negative(val);
|
*result = FX_DOUBLE(-INFINITY);
|
||||||
}
|
}
|
||||||
} else if (tok->tok_value.f.nan) {
|
} else if (tok->tok_value.f.nan) {
|
||||||
if (tok->tok_value.f.v >= 0) {
|
if (tok->tok_value.f.v >= 0) {
|
||||||
fx_double_set_value_nan(val);
|
*result = FX_DOUBLE(NAN);
|
||||||
} else {
|
} else {
|
||||||
fx_double_set_value_nan_negative(val);
|
*result = FX_DOUBLE(-NAN);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
*result = FX_DOUBLE(tok->tok_value.f.v);
|
||||||
}
|
}
|
||||||
|
|
||||||
*result = (val);
|
|
||||||
return FX_RESULT_SUCCESS;
|
return FX_RESULT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static fx_result parse_bool(struct ctx *ctx, fx_object **result)
|
static fx_result parse_bool(struct ctx *ctx, fx_value *result)
|
||||||
{
|
{
|
||||||
struct token *tok = peek_token(ctx);
|
struct token *tok = peek_token(ctx);
|
||||||
fx_bool *val = fx_bool_create(tok->tok_value.b);
|
*result = FX_BOOL(tok->tok_value.b);
|
||||||
if (!val) {
|
|
||||||
return FX_RESULT_ERR(NO_MEMORY);
|
|
||||||
}
|
|
||||||
|
|
||||||
*result = (val);
|
|
||||||
return FX_RESULT_SUCCESS;
|
return FX_RESULT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static fx_result parse_table_inline(struct ctx *ctx, fx_object **result)
|
static fx_result parse_table_inline(struct ctx *ctx, fx_value *result)
|
||||||
{
|
{
|
||||||
DISABLE_EXTENDED_LEXING(ctx);
|
DISABLE_EXTENDED_LEXING(ctx);
|
||||||
|
|
||||||
advance_token(ctx);
|
advance_token(ctx);
|
||||||
|
|
||||||
fx_dict *table = fx_dict_create();
|
fx_hashtable *table = fx_hashtable_create();
|
||||||
if (!table) {
|
if (!table) {
|
||||||
return FX_RESULT_ERR(NO_MEMORY);
|
return FX_RESULT_ERR(NO_MEMORY);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct token *tok = peek_token(ctx);
|
struct token *tok = peek_token(ctx);
|
||||||
if (tok && tok->tok_type == TOK_RIGHT_BRACE) {
|
if (tok && tok->tok_type == TOK_RIGHT_BRACE) {
|
||||||
*result = (table);
|
*result = FX_VALUE_OBJECT(table);
|
||||||
return FX_RESULT_SUCCESS;
|
return FX_RESULT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1775,14 +1740,14 @@ static fx_result parse_table_inline(struct ctx *ctx, fx_object **result)
|
|||||||
fx_object *value;
|
fx_object *value;
|
||||||
fx_result status = parse_key_value_pair(ctx, table);
|
fx_result status = parse_key_value_pair(ctx, table);
|
||||||
if (!FX_OK(status)) {
|
if (!FX_OK(status)) {
|
||||||
fx_dict_unref(table);
|
fx_hashtable_unref(table);
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
tok = peek_token(ctx);
|
tok = peek_token(ctx);
|
||||||
|
|
||||||
if (!tok) {
|
if (!tok) {
|
||||||
fx_dict_unref(table);
|
fx_hashtable_unref(table);
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1794,12 +1759,12 @@ static fx_result parse_table_inline(struct ctx *ctx, fx_object **result)
|
|||||||
advance_token(ctx);
|
advance_token(ctx);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
fx_dict_unref(table);
|
fx_hashtable_unref(table);
|
||||||
return FX_RESULT_ERR(BAD_FORMAT);
|
return FX_RESULT_ERR(BAD_FORMAT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*result = (table);
|
*result = FX_VALUE_OBJECT(table);
|
||||||
return FX_RESULT_SUCCESS;
|
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;
|
bool done = false;
|
||||||
ENABLE_EXTENDED_LEXING(ctx);
|
ENABLE_EXTENDED_LEXING(ctx);
|
||||||
@@ -1850,14 +1815,15 @@ static fx_result parse_array_inline(struct ctx *ctx, fx_object **result)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
fx_object *value;
|
fx_value value;
|
||||||
fx_result status = parse_value(ctx, &value);
|
fx_result status = parse_value(ctx, &value);
|
||||||
if (!FX_OK(status)) {
|
if (!FX_OK(status)) {
|
||||||
fx_array_unref(array);
|
fx_array_unref(array);
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
fx_array_append(array, FX_RV(value));
|
fx_array_push_back(array, value);
|
||||||
|
fx_value_unset(&value);
|
||||||
ENABLE_EXTENDED_LEXING(ctx);
|
ENABLE_EXTENDED_LEXING(ctx);
|
||||||
|
|
||||||
advance_token(ctx);
|
advance_token(ctx);
|
||||||
@@ -1880,11 +1846,11 @@ static fx_result parse_array_inline(struct ctx *ctx, fx_object **result)
|
|||||||
}
|
}
|
||||||
|
|
||||||
DISABLE_EXTENDED_LEXING(ctx);
|
DISABLE_EXTENDED_LEXING(ctx);
|
||||||
*result = (array);
|
*result = FX_VALUE_OBJECT(array);
|
||||||
return FX_RESULT_SUCCESS;
|
return FX_RESULT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static fx_result parse_value(struct ctx *ctx, fx_object **result)
|
static fx_result parse_value(struct ctx *ctx, fx_value *result)
|
||||||
{
|
{
|
||||||
|
|
||||||
struct token *tok = peek_token(ctx);
|
struct token *tok = peek_token(ctx);
|
||||||
@@ -1912,7 +1878,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);
|
struct token *tok = peek_token(ctx);
|
||||||
if (!IS_VALID_KEY_COMPONENT(tok)) {
|
if (!IS_VALID_KEY_COMPONENT(tok)) {
|
||||||
@@ -1931,36 +1897,42 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_dict *container)
|
|||||||
}
|
}
|
||||||
|
|
||||||
while (tok && tok->tok_type == TOK_DOT) {
|
while (tok && tok->tok_type == TOK_DOT) {
|
||||||
fx_object *sub_dict = fx_dict_at_sk(container, key);
|
const fx_value *child
|
||||||
if (!sub_dict) {
|
= fx_hashtable_get(container, &FX_VALUE_OBJECT(key));
|
||||||
sub_dict = (fx_dict_create());
|
if (child && !fx_value_is_type(child, FX_TYPE_HASHTABLE)) {
|
||||||
fx_dict_put_sk(container, key, FX_RV(sub_dict));
|
fx_string_unref(key);
|
||||||
} else if (
|
|
||||||
sub_dict
|
|
||||||
&& !fx_object_is_type(sub_dict, FX_TYPE_DICT)) {
|
|
||||||
free(key);
|
|
||||||
return FX_RESULT_ERR(BAD_FORMAT);
|
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
|
#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
|
if (flags
|
||||||
& (OBJECT_KV_END_DEFINED | OBJECT_HEADER_END_DEFINED)) {
|
& (OBJECT_KV_END_DEFINED | OBJECT_HEADER_END_DEFINED)) {
|
||||||
free(key);
|
fx_string_unref(key);
|
||||||
return FX_RESULT_ERR(BAD_FORMAT);
|
return FX_RESULT_ERR(BAD_FORMAT);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
ctx_set_object_flags(ctx, sub_dict, OBJECT_KV_MID_DEFINED);
|
ctx_set_object_flags(ctx, subtable, OBJECT_KV_MID_DEFINED);
|
||||||
|
|
||||||
advance_token(ctx);
|
advance_token(ctx);
|
||||||
tok = peek_token(ctx);
|
tok = peek_token(ctx);
|
||||||
if (!IS_VALID_KEY_COMPONENT(tok)) {
|
if (!IS_VALID_KEY_COMPONENT(tok)) {
|
||||||
free(key);
|
fx_string_unref(key);
|
||||||
return FX_RESULT_ERR(BAD_FORMAT);
|
return FX_RESULT_ERR(BAD_FORMAT);
|
||||||
}
|
}
|
||||||
|
|
||||||
container = sub_dict;
|
container = subtable;
|
||||||
fx_string_unref(key);
|
fx_string_unref(key);
|
||||||
key = fx_string_duplicate(tok->tok_str);
|
key = fx_string_duplicate(tok->tok_str);
|
||||||
if (!key) {
|
if (!key) {
|
||||||
@@ -1971,7 +1943,7 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_dict *container)
|
|||||||
tok = peek_token(ctx);
|
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);
|
return FX_RESULT_ERR(BAD_FORMAT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1986,7 +1958,7 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_dict *container)
|
|||||||
ENABLE_EXTENDED_LEXING(ctx);
|
ENABLE_EXTENDED_LEXING(ctx);
|
||||||
advance_token(ctx);
|
advance_token(ctx);
|
||||||
|
|
||||||
fx_object *value = NULL;
|
fx_value value = FX_VALUE_EMPTY;
|
||||||
fx_result result = parse_value(ctx, &value);
|
fx_result result = parse_value(ctx, &value);
|
||||||
|
|
||||||
DISABLE_EXTENDED_LEXING(ctx);
|
DISABLE_EXTENDED_LEXING(ctx);
|
||||||
@@ -2001,20 +1973,24 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_dict *container)
|
|||||||
return fx_result_propagate(result);
|
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)
|
if (fx_value_is_type(&value, FX_TYPE_HASHTABLE)
|
||||||
|| fx_object_is_type(value, FX_TYPE_ARRAY)) {
|
|| fx_value_is_type(&value, FX_TYPE_ARRAY)) {
|
||||||
ctx_set_object_flags(ctx, value, OBJECT_KV_END_DEFINED);
|
ctx_set_object_flags(
|
||||||
|
ctx,
|
||||||
|
value.v_object,
|
||||||
|
OBJECT_KV_END_DEFINED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fx_value_unset(&value);
|
||||||
return FX_RESULT_SUCCESS;
|
return FX_RESULT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static fx_result parse_table_header(
|
static fx_result parse_table_header(
|
||||||
struct ctx *ctx,
|
struct ctx *ctx,
|
||||||
fx_dict *container,
|
fx_hashtable *container,
|
||||||
fx_dict **new_container)
|
fx_hashtable **new_container)
|
||||||
{
|
{
|
||||||
advance_token(ctx);
|
advance_token(ctx);
|
||||||
struct token *tok = peek_token(ctx);
|
struct token *tok = peek_token(ctx);
|
||||||
@@ -2034,17 +2010,24 @@ static fx_result parse_table_header(
|
|||||||
}
|
}
|
||||||
|
|
||||||
while (tok && tok->tok_type == TOK_DOT) {
|
while (tok && tok->tok_type == TOK_DOT) {
|
||||||
fx_object *sub_dict = fx_dict_at_sk(container, key);
|
const fx_value *value
|
||||||
enum object_flags flags = ctx_get_object_flags(ctx, sub_dict);
|
= fx_hashtable_get(container, &FX_VALUE_OBJECT(key));
|
||||||
if (!sub_dict) {
|
fx_object *subtable = NULL;
|
||||||
sub_dict = (fx_dict_create());
|
fx_value_get_object(value, &subtable);
|
||||||
fx_dict_put_sk(container, key, FX_RV(sub_dict));
|
enum object_flags flags = ctx_get_object_flags(ctx, subtable);
|
||||||
} else if (fx_object_is_type(sub_dict, FX_TYPE_ARRAY)) {
|
if (!value) {
|
||||||
|
subtable = (fx_hashtable_create());
|
||||||
sub_dict = fx_array_at(
|
fx_hashtable_put(
|
||||||
sub_dict,
|
container,
|
||||||
fx_array_size(sub_dict) - 1);
|
&FX_VALUE_OBJECT(key),
|
||||||
} else if (!fx_object_is_type(sub_dict, FX_TYPE_DICT)) {
|
&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);
|
return FX_RESULT_ERR(BAD_FORMAT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2058,9 +2041,9 @@ static fx_result parse_table_header(
|
|||||||
return FX_RESULT_ERR(BAD_FORMAT);
|
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);
|
fx_string_unref(key);
|
||||||
key = fx_string_duplicate(tok->tok_str);
|
key = fx_string_duplicate(tok->tok_str);
|
||||||
if (!key) {
|
if (!key) {
|
||||||
@@ -2075,20 +2058,31 @@ static fx_result parse_table_header(
|
|||||||
return FX_RESULT_ERR(BAD_FORMAT);
|
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) {
|
if (!new_table) {
|
||||||
new_table = fx_dict_create();
|
new_table = fx_hashtable_create();
|
||||||
|
|
||||||
if (!new_table) {
|
if (!new_table) {
|
||||||
free(key);
|
fx_string_unref(key);
|
||||||
return FX_RESULT_ERR(NO_MEMORY);
|
return FX_RESULT_ERR(NO_MEMORY);
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
return FX_RESULT_ERR(BAD_FORMAT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2109,8 +2103,8 @@ static fx_result parse_table_header(
|
|||||||
|
|
||||||
static fx_result parse_array_header(
|
static fx_result parse_array_header(
|
||||||
struct ctx *ctx,
|
struct ctx *ctx,
|
||||||
fx_dict *container,
|
fx_hashtable *container,
|
||||||
fx_dict **new_container)
|
fx_hashtable **new_container)
|
||||||
{
|
{
|
||||||
advance_token(ctx);
|
advance_token(ctx);
|
||||||
struct token *tok = peek_token(ctx);
|
struct token *tok = peek_token(ctx);
|
||||||
@@ -2130,15 +2124,29 @@ static fx_result parse_array_header(
|
|||||||
}
|
}
|
||||||
|
|
||||||
while (tok && tok->tok_type == TOK_DOT) {
|
while (tok && tok->tok_type == TOK_DOT) {
|
||||||
fx_object *sub_dict = fx_dict_at_sk(container, key);
|
const fx_value *child
|
||||||
if (!sub_dict) {
|
= fx_hashtable_get(container, &FX_VALUE_OBJECT(key));
|
||||||
sub_dict = (fx_dict_create());
|
fx_object *sub_dict = NULL;
|
||||||
fx_dict_put_sk(container, key, FX_RV(sub_dict));
|
fx_value_get_object(child, &sub_dict);
|
||||||
} else if (fx_object_is_type(sub_dict, FX_TYPE_ARRAY)) {
|
|
||||||
sub_dict = fx_array_at(
|
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,
|
sub_dict,
|
||||||
fx_array_size(sub_dict) - 1);
|
fx_array_get_size(sub_dict) - 1);
|
||||||
} else if (!fx_object_is_type(sub_dict, FX_TYPE_DICT)) {
|
sub_dict = child->v_object;
|
||||||
|
} else if (!fx_value_is_type(child, FX_TYPE_HASHTABLE)) {
|
||||||
return FX_RESULT_ERR(BAD_FORMAT);
|
return FX_RESULT_ERR(BAD_FORMAT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2163,37 +2171,48 @@ static fx_result parse_array_header(
|
|||||||
return FX_RESULT_ERR(BAD_FORMAT);
|
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) {
|
if (!array) {
|
||||||
array = fx_array_create();
|
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)) {
|
} else if (!fx_object_is_type(array, FX_TYPE_ARRAY)) {
|
||||||
return FX_RESULT_ERR(BAD_FORMAT);
|
return FX_RESULT_ERR(BAD_FORMAT);
|
||||||
}
|
}
|
||||||
free(key);
|
fx_string_unref(key);
|
||||||
|
|
||||||
enum object_flags flags = ctx_get_object_flags(ctx, (array));
|
enum object_flags flags = ctx_get_object_flags(ctx, (array));
|
||||||
if (flags & OBJECT_KV_END_DEFINED) {
|
if (flags & OBJECT_KV_END_DEFINED) {
|
||||||
return FX_RESULT_ERR(NO_MEMORY);
|
return FX_RESULT_ERR(BAD_FORMAT);
|
||||||
}
|
}
|
||||||
|
|
||||||
fx_dict *new_table = fx_dict_create();
|
fx_hashtable *new_table = fx_hashtable_create();
|
||||||
if (!new_table) {
|
if (!new_table) {
|
||||||
return FX_RESULT_ERR(NO_MEMORY);
|
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);
|
advance_token(ctx);
|
||||||
*new_container = new_table;
|
*new_container = new_table;
|
||||||
return FX_RESULT_SUCCESS;
|
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_result result = FX_RESULT_SUCCESS;
|
||||||
fx_dict *root = fx_dict_create();
|
fx_hashtable *root = fx_hashtable_create();
|
||||||
fx_dict *current = root;
|
fx_hashtable *current = root;
|
||||||
|
|
||||||
while (!(ctx->ctx_flags & CTX_EOF) && fx_result_is_success(result)) {
|
while (!(ctx->ctx_flags & CTX_EOF) && fx_result_is_success(result)) {
|
||||||
struct token *tok = peek_token(ctx);
|
struct token *tok = peek_token(ctx);
|
||||||
@@ -2253,7 +2272,7 @@ static fx_result parse_root(struct ctx *ctx, fx_dict **out)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (fx_result_is_error(result)) {
|
if (fx_result_is_error(result)) {
|
||||||
fx_dict_unref(root);
|
fx_hashtable_unref(root);
|
||||||
root = NULL;
|
root = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2264,7 +2283,7 @@ static fx_result parse_root(struct ctx *ctx, fx_dict **out)
|
|||||||
static fx_result toml_deserialise(
|
static fx_result toml_deserialise(
|
||||||
fx_serial_ctx *serial,
|
fx_serial_ctx *serial,
|
||||||
fx_stream *src,
|
fx_stream *src,
|
||||||
fx_object **dest,
|
fx_value *dest,
|
||||||
enum fx_serial_flags flags)
|
enum fx_serial_flags flags)
|
||||||
{
|
{
|
||||||
struct ctx ctx = {0};
|
struct ctx ctx = {0};
|
||||||
@@ -2285,17 +2304,17 @@ static fx_result toml_deserialise(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (ctx.ctx_flags & CTX_EOF) {
|
if (ctx.ctx_flags & CTX_EOF) {
|
||||||
*dest = (fx_dict_create());
|
*dest = FX_VALUE_OBJECT(fx_hashtable_create());
|
||||||
return FX_RESULT_SUCCESS;
|
return FX_RESULT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
fx_dict *data = NULL;
|
fx_hashtable *data = NULL;
|
||||||
ctx.ctx_result = parse_root(&ctx, &data);
|
ctx.ctx_result = parse_root(&ctx, &data);
|
||||||
if (fx_result_is_error(ctx.ctx_result)) {
|
if (fx_result_is_error(ctx.ctx_result)) {
|
||||||
return fx_result_propagate(ctx.ctx_result);
|
return fx_result_propagate(ctx.ctx_result);
|
||||||
}
|
}
|
||||||
|
|
||||||
*dest = (data);
|
*dest = FX_VALUE_OBJECT(data);
|
||||||
#if 0
|
#if 0
|
||||||
ctx.ctx_flags
|
ctx.ctx_flags
|
||||||
= CTX_ENABLE_NUMBERS | CTX_ENABLE_TIMESTAMPS | CTX_ENABLE_BOOLS;
|
= CTX_ENABLE_NUMBERS | CTX_ENABLE_TIMESTAMPS | CTX_ENABLE_BOOLS;
|
||||||
|
|||||||
+3
-7
@@ -102,8 +102,8 @@ static print_function format_printers[] = {
|
|||||||
[FX_PRINT_WARN] = print_warn,
|
[FX_PRINT_WARN] = print_warn,
|
||||||
[FX_PRINT_ERR] = print_err,
|
[FX_PRINT_ERR] = print_err,
|
||||||
};
|
};
|
||||||
static size_t nr_format_printers
|
static size_t nr_format_printers = sizeof format_printers
|
||||||
= sizeof format_printers / sizeof format_printers[FX_PRINT_NORMAL];
|
/ sizeof format_printers[FX_PRINT_NORMAL];
|
||||||
|
|
||||||
fx_status fx_print(fx_print_format format, const char *str, ...)
|
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_list arg;
|
||||||
va_start(arg, format);
|
va_start(arg, format);
|
||||||
int x = fx_tty_vprintf(
|
int x = fx_tty_vprintf(fx_stdtty, 0, format, arg);
|
||||||
fx_stdtty,
|
|
||||||
FX_TTY_DISABLE_INTERPOLATED_FORMATTING,
|
|
||||||
format,
|
|
||||||
arg);
|
|
||||||
va_end(arg);
|
va_end(arg);
|
||||||
return x;
|
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 "printf.h"
|
||||||
|
|
||||||
#include <fx/term/tty.h>
|
#include <fx/term/tty.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.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 out_tty_args {
|
||||||
struct fx_tty *tty;
|
struct fx_tty *tty;
|
||||||
enum fx_tty_print_flags flags;
|
enum fx_tty_print_flags flags;
|
||||||
bool format_ch;
|
bool format_ch;
|
||||||
};
|
};
|
||||||
|
|
||||||
// output function type
|
static void out_tty(char c, void *argp)
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
if (c == 0) {
|
if (c == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct out_tty_args *args = argp;
|
||||||
enum fx_tty_print_flags flags = args->flags;
|
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;
|
flags |= FX_TTY_DISABLE_FORMATTING;
|
||||||
}
|
}
|
||||||
|
|
||||||
fx_tty_putc(args->tty, flags, c);
|
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(
|
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)
|
va_list args)
|
||||||
{
|
{
|
||||||
struct out_tty_args tty_args = {
|
struct out_tty_args tty_args = {
|
||||||
@@ -977,6 +44,6 @@ int fx_tty_vprintf(
|
|||||||
.tty = tty,
|
.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;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include "../../tty.h"
|
#include "../../tty.h"
|
||||||
|
|
||||||
|
#include <fx/encoding.h>
|
||||||
#include <fx/term/tty.h>
|
#include <fx/term/tty.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -102,9 +103,10 @@ static void init_tty(struct fx_tty *tty, FILE *in, FILE *out)
|
|||||||
tcgetattr(fd, &tty->t_ios_default);
|
tcgetattr(fd, &tty->t_ios_default);
|
||||||
memcpy(&tty->t_ios_raw, &tty->t_ios_default, sizeof tty->t_ios_raw);
|
memcpy(&tty->t_ios_raw, &tty->t_ios_default, sizeof tty->t_ios_raw);
|
||||||
|
|
||||||
tty->t_ios_raw.c_iflag &= ~(ICRNL | IXON);
|
tty->t_ios_raw.c_iflag &= ~(BRKINT | INPCK | ISTRIP | ICRNL | IXON);
|
||||||
tty->t_ios_raw.c_oflag &= ~(OPOST);
|
tty->t_ios_raw.c_oflag &= ~(OPOST);
|
||||||
tty->t_ios_raw.c_lflag &= ~(ECHO | ICANON | IEXTEN);
|
tty->t_ios_raw.c_cflag |= (CS8);
|
||||||
|
tty->t_ios_raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
|
||||||
|
|
||||||
tty->t_flags |= FX_TTY_INIT;
|
tty->t_flags |= FX_TTY_INIT;
|
||||||
}
|
}
|
||||||
@@ -206,7 +208,8 @@ void fx_tty_reset_vmode(struct fx_tty *tty)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int compare_colour(
|
static int compare_colour(
|
||||||
const struct fx_tty_colour *a, const struct fx_tty_colour *b)
|
const struct fx_tty_colour *a,
|
||||||
|
const struct fx_tty_colour *b)
|
||||||
{
|
{
|
||||||
if (a->c_mode != b->c_mode) {
|
if (a->c_mode != b->c_mode) {
|
||||||
return -1;
|
return -1;
|
||||||
@@ -243,7 +246,9 @@ static int compare_colour(
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int compare_vmode(const struct fx_tty_vmode *a, const struct fx_tty_vmode *b)
|
static int compare_vmode(
|
||||||
|
const struct fx_tty_vmode *a,
|
||||||
|
const struct fx_tty_vmode *b)
|
||||||
{
|
{
|
||||||
if (a->v_attrib != b->v_attrib) {
|
if (a->v_attrib != b->v_attrib) {
|
||||||
return -1;
|
return -1;
|
||||||
@@ -271,7 +276,9 @@ static void put_ansi_attrib(struct fx_tty *tty, const char *s)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void set_attrib(
|
static void set_attrib(
|
||||||
struct fx_tty *tty, enum fx_tty_attrib old, enum fx_tty_attrib new)
|
struct fx_tty *tty,
|
||||||
|
enum fx_tty_attrib old,
|
||||||
|
enum fx_tty_attrib new)
|
||||||
{
|
{
|
||||||
if (old == new) {
|
if (old == new) {
|
||||||
return;
|
return;
|
||||||
@@ -309,7 +316,8 @@ static void set_attrib(
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void set_fg(
|
static void set_fg(
|
||||||
struct fx_tty *tty, const struct fx_tty_colour *old,
|
struct fx_tty *tty,
|
||||||
|
const struct fx_tty_colour *old,
|
||||||
const struct fx_tty_colour *new)
|
const struct fx_tty_colour *new)
|
||||||
{
|
{
|
||||||
if (compare_colour(old, new) == 0) {
|
if (compare_colour(old, new) == 0) {
|
||||||
@@ -345,7 +353,8 @@ static void set_fg(
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void set_bg(
|
static void set_bg(
|
||||||
struct fx_tty *tty, const struct fx_tty_colour *old,
|
struct fx_tty *tty,
|
||||||
|
const struct fx_tty_colour *old,
|
||||||
const struct fx_tty_colour *new)
|
const struct fx_tty_colour *new)
|
||||||
{
|
{
|
||||||
if (compare_colour(old, new) == 0) {
|
if (compare_colour(old, new) == 0) {
|
||||||
@@ -399,6 +408,24 @@ void fx_tty_set_vmode(struct fx_tty *tty, const struct fx_tty_vmode *vmode)
|
|||||||
memcpy(&tty->t_vmode, vmode, sizeof *vmode);
|
memcpy(&tty->t_vmode, vmode, sizeof *vmode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static fx_keycode read_utf8(int fd, char header, int len)
|
||||||
|
{
|
||||||
|
char s[4] = {header, 0, 0, 0};
|
||||||
|
|
||||||
|
for (int i = 1; i < len; i++) {
|
||||||
|
char c;
|
||||||
|
int v = read(fd, &c, 1);
|
||||||
|
|
||||||
|
if (v != 1) {
|
||||||
|
return FX_KEY_EOF;
|
||||||
|
}
|
||||||
|
|
||||||
|
s[i] = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
return fx_wchar_utf8_codepoint_decode(s);
|
||||||
|
}
|
||||||
|
|
||||||
fx_keycode fx_tty_read_key(struct fx_tty *tty)
|
fx_keycode fx_tty_read_key(struct fx_tty *tty)
|
||||||
{
|
{
|
||||||
char c;
|
char c;
|
||||||
@@ -423,6 +450,11 @@ fx_keycode fx_tty_read_key(struct fx_tty *tty)
|
|||||||
return FX_TTY_CTRL_KEY(c + 'a' - 1);
|
return FX_TTY_CTRL_KEY(c + 'a' - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int utf8_len = fx_wchar_utf8_header_decode(c);
|
||||||
|
if (utf8_len > 1) {
|
||||||
|
return read_utf8(fd, c, utf8_len);
|
||||||
|
}
|
||||||
|
|
||||||
if (c != 0x1b) {
|
if (c != 0x1b) {
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
@@ -458,7 +490,10 @@ fx_keycode fx_tty_read_key(struct fx_tty *tty)
|
|||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
void fx_tty_move_cursor_x(struct fx_tty *tty, enum fx_tty_position_base base, int pos)
|
void fx_tty_move_cursor_x(
|
||||||
|
struct fx_tty *tty,
|
||||||
|
enum fx_tty_position_base base,
|
||||||
|
int pos)
|
||||||
{
|
{
|
||||||
if (base == FX_TTY_POS_CURSOR && pos < 0 && pos >= -4) {
|
if (base == FX_TTY_POS_CURSOR && pos < 0 && pos >= -4) {
|
||||||
for (int i = 0; i > pos; i--) {
|
for (int i = 0; i > pos; i--) {
|
||||||
@@ -487,7 +522,10 @@ void fx_tty_move_cursor_x(struct fx_tty *tty, enum fx_tty_position_base base, in
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void fx_tty_move_cursor_y(struct fx_tty *tty, enum fx_tty_position_base base, int pos)
|
void fx_tty_move_cursor_y(
|
||||||
|
struct fx_tty *tty,
|
||||||
|
enum fx_tty_position_base base,
|
||||||
|
int pos)
|
||||||
{
|
{
|
||||||
if (base == FX_TTY_POS_START) {
|
if (base == FX_TTY_POS_START) {
|
||||||
/* we don't need this functionality right now */
|
/* we don't need this functionality right now */
|
||||||
@@ -528,7 +566,9 @@ void fx_tty_clear(struct fx_tty *tty, enum fx_tty_clear_mode mode)
|
|||||||
}
|
}
|
||||||
|
|
||||||
enum fx_status fx_tty_get_dimensions(
|
enum fx_status fx_tty_get_dimensions(
|
||||||
struct fx_tty *tty, unsigned int *w, unsigned int *h)
|
struct fx_tty *tty,
|
||||||
|
unsigned int *w,
|
||||||
|
unsigned int *h)
|
||||||
{
|
{
|
||||||
if (!(tty->t_flags & FX_TTY_INTERACTIVE)) {
|
if (!(tty->t_flags & FX_TTY_INTERACTIVE)) {
|
||||||
return -1;
|
return -1;
|
||||||
+1
-1
@@ -1,2 +1,2 @@
|
|||||||
set(source_dirs hash)
|
set(source_dirs hash int float)
|
||||||
export_fx_namespace_details(fx)
|
export_fx_namespace_details(fx)
|
||||||
|
|||||||
@@ -1,84 +1,86 @@
|
|||||||
|
#include "int/convert.h"
|
||||||
|
|
||||||
#include <fx/bool.h>
|
#include <fx/bool.h>
|
||||||
#include <fx/stream.h>
|
#include <fx/macros.h>
|
||||||
|
#include <fx/value-type.h>
|
||||||
|
|
||||||
/*** PRIVATE DATA *************************************************************/
|
HASH(bool)
|
||||||
|
|
||||||
struct fx_bool_p {
|
GENERIC_CONVERT(bool, i16, i16)
|
||||||
bool b_value;
|
GENERIC_CONVERT(bool, u16, u16)
|
||||||
};
|
GENERIC_CONVERT(bool, i32, i32)
|
||||||
|
GENERIC_CONVERT(bool, u32, u32)
|
||||||
|
GENERIC_CONVERT(bool, i64, i64)
|
||||||
|
GENERIC_CONVERT(bool, u64, u64)
|
||||||
|
GENERIC_CONVERT(bool, iptr, iptr)
|
||||||
|
GENERIC_CONVERT(bool, uptr, uptr)
|
||||||
|
GENERIC_CONVERT(bool, sbyte, sbyte)
|
||||||
|
GENERIC_CONVERT(bool, byte, byte)
|
||||||
|
GENERIC_CONVERT(bool, short, short)
|
||||||
|
GENERIC_CONVERT(bool, ushort, unsigned short)
|
||||||
|
GENERIC_CONVERT(bool, int, int)
|
||||||
|
GENERIC_CONVERT(bool, uint, unsigned int)
|
||||||
|
GENERIC_CONVERT(bool, long, long)
|
||||||
|
GENERIC_CONVERT(bool, ulong, unsigned long)
|
||||||
|
GENERIC_CONVERT(bool, longlong, long long)
|
||||||
|
GENERIC_CONVERT(bool, ulonglong, unsigned long long)
|
||||||
|
GENERIC_CONVERT(bool, size, size_t)
|
||||||
|
GENERIC_CONVERT(bool, float, float)
|
||||||
|
GENERIC_CONVERT(bool, double, double)
|
||||||
|
|
||||||
/*** PRIVATE FUNCTIONS ********************************************************/
|
COMPARE(bool, bool)
|
||||||
|
|
||||||
static void bool_set_value(struct fx_bool_p *i, bool v)
|
static fx_status to_string(
|
||||||
|
const fx_value *v,
|
||||||
|
fx_stream *out,
|
||||||
|
const char *format)
|
||||||
{
|
{
|
||||||
i->b_value = v;
|
return fx_stream_write_cstr(out, v->v_bool ? "true" : "false", NULL);
|
||||||
}
|
|
||||||
|
|
||||||
static bool bool_get_value(struct fx_bool_p *i)
|
|
||||||
{
|
|
||||||
return i->b_value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*** PUBLIC FUNCTIONS *********************************************************/
|
|
||||||
|
|
||||||
fx_bool *fx_bool_create(bool value)
|
|
||||||
{
|
|
||||||
fx_bool *i = fx_object_create(FX_TYPE_BOOL);
|
|
||||||
if (!i) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct fx_bool_p *p = fx_object_get_private(i, FX_TYPE_BOOL);
|
|
||||||
p->b_value = value;
|
|
||||||
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
void fx_bool_set_value(fx_bool *i, bool v)
|
|
||||||
{
|
|
||||||
FX_CLASS_DISPATCH_STATIC_V(FX_TYPE_BOOL, bool_set_value, i, v);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool fx_bool_get_value(const fx_bool *i)
|
|
||||||
{
|
|
||||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_BOOL, bool_get_value, i);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*** VIRTUAL FUNCTIONS ********************************************************/
|
|
||||||
|
|
||||||
static void bool_init(fx_object *obj, void *priv)
|
|
||||||
{
|
|
||||||
struct fx_bool_p *i = priv;
|
|
||||||
|
|
||||||
i->b_value = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void bool_fini(fx_object *obj, void *priv)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
static void bool_to_string(const fx_object *obj, fx_stream *out)
|
|
||||||
{
|
|
||||||
struct fx_bool_p *i = fx_object_get_private(obj, FX_TYPE_BOOL);
|
|
||||||
if (i->b_value == false) {
|
|
||||||
fx_stream_write_cstr(out, "false", NULL);
|
|
||||||
} else {
|
|
||||||
fx_stream_write_cstr(out, "true", NULL);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*** CLASS DEFINITION *********************************************************/
|
/*** CLASS DEFINITION *********************************************************/
|
||||||
|
|
||||||
FX_TYPE_CLASS_BEGIN(fx_bool)
|
FX_TYPE_CLASS_BEGIN(fx_bool)
|
||||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||||
FX_INTERFACE_ENTRY(to_string) = bool_to_string;
|
FX_INTERFACE_ENTRY(to_string) = to_string;
|
||||||
|
FX_INTERFACE_ENTRY(hash) = hash;
|
||||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||||
|
|
||||||
|
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_comparable, FX_TYPE_COMPARABLE)
|
||||||
|
FX_INTERFACE_ENTRY(c_compare) = compare;
|
||||||
|
FX_TYPE_VTABLE_INTERFACE_END(fx_comparable, FX_TYPE_COMPARABLE)
|
||||||
|
|
||||||
|
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_convertible, FX_TYPE_CONVERTIBLE)
|
||||||
|
FX_INTERFACE_ENTRY(c_to_i16) = to_i16;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_u16) = to_u16;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_i32) = to_i32;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_u32) = to_u32;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_i64) = to_i64;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_u64) = to_u64;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_iptr) = to_iptr;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_uptr) = to_uptr;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_sbyte) = to_sbyte;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_byte) = to_byte;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_short) = to_short;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_ushort) = to_ushort;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_int) = to_int;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_uint) = to_uint;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_long) = to_long;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_ulong) = to_ulong;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_longlong) = to_longlong;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_ulonglong) = to_ulonglong;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_size) = to_size;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_float) = to_float;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_double) = to_double;
|
||||||
|
FX_TYPE_VTABLE_INTERFACE_END(fx_convertible, FX_TYPE_CONVERTIBLE)
|
||||||
FX_TYPE_CLASS_END(fx_bool)
|
FX_TYPE_CLASS_END(fx_bool)
|
||||||
|
|
||||||
FX_TYPE_DEFINITION_BEGIN(fx_bool)
|
FX_TYPE_DEFINITION_BEGIN(fx_bool)
|
||||||
FX_TYPE_ID(0x9c8453bf, 0xfc92, 0x4b0a, 0xbcaf, 0xd0c6cdba9310);
|
__FX_VALUE_TYPE_ID(BOOL);
|
||||||
|
FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE);
|
||||||
|
FX_TYPE_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
|
||||||
|
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
|
||||||
|
FX_TYPE_NAME("fx.bool");
|
||||||
FX_TYPE_CLASS(fx_bool_class);
|
FX_TYPE_CLASS(fx_bool_class);
|
||||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_bool_p);
|
FX_TYPE_INSTANCE_PRIVATE(bool);
|
||||||
FX_TYPE_INSTANCE_INIT(bool_init);
|
|
||||||
FX_TYPE_INSTANCE_FINI(bool_fini);
|
|
||||||
FX_TYPE_DEFINITION_END(fx_bool)
|
FX_TYPE_DEFINITION_END(fx_bool)
|
||||||
|
|||||||
@@ -244,6 +244,15 @@ enum fx_status fx_bstr_write_cstr(
|
|||||||
return bstr_puts(str, s, nr_written);
|
return bstr_puts(str, s, nr_written);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum fx_status fx_bstr_write_wstr(
|
||||||
|
struct fx_bstr *str,
|
||||||
|
const fx_wchar *s,
|
||||||
|
size_t *nr_written)
|
||||||
|
{
|
||||||
|
/* TODO */
|
||||||
|
return FX_ERR_NOT_SUPPORTED;
|
||||||
|
}
|
||||||
|
|
||||||
enum fx_status fx_bstr_write_cstr_list(
|
enum fx_status fx_bstr_write_cstr_list(
|
||||||
struct fx_bstr *str,
|
struct fx_bstr *str,
|
||||||
const char **strs,
|
const char **strs,
|
||||||
@@ -348,8 +357,11 @@ enum fx_status fx_bstr_write_fmt(
|
|||||||
{
|
{
|
||||||
va_list arg;
|
va_list arg;
|
||||||
va_start(arg, format);
|
va_start(arg, format);
|
||||||
enum fx_status result
|
enum fx_status result = fx_bstr_write_vfmt(
|
||||||
= fx_bstr_write_vfmt(str, nr_written, format, arg);
|
str,
|
||||||
|
nr_written,
|
||||||
|
format,
|
||||||
|
arg);
|
||||||
va_end(arg);
|
va_end(arg);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
#include <fx/comparable.h>
|
||||||
|
#include <fx/macros.h>
|
||||||
|
|
||||||
|
FX_TYPE_CLASS_BEGIN(fx_comparable)
|
||||||
|
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_comparable)
|
||||||
|
|
||||||
|
FX_TYPE_DEFINITION_BEGIN(fx_comparable)
|
||||||
|
FX_TYPE_FLAGS(FX_TYPE_F_ABSTRACT);
|
||||||
|
FX_TYPE_ID(0xb25ab0c6, 0xfddb, 0x447f, 0xa54d, 0x44c4a0ef74a1);
|
||||||
|
FX_TYPE_NAME("fx.comparable");
|
||||||
|
FX_TYPE_CLASS(fx_comparable_class);
|
||||||
|
FX_TYPE_DEFINITION_END(fx_comparable)
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
#include <fx/convertible.h>
|
||||||
|
#include <fx/macros.h>
|
||||||
|
|
||||||
|
FX_TYPE_CLASS_BEGIN(fx_convertible)
|
||||||
|
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_convertible)
|
||||||
|
|
||||||
|
FX_TYPE_DEFINITION_BEGIN(fx_convertible)
|
||||||
|
FX_TYPE_FLAGS(FX_TYPE_F_ABSTRACT);
|
||||||
|
FX_TYPE_ID(0x8443b648, 0x83ea, 0x4a65, 0x85ef, 0x5570b0157d0e);
|
||||||
|
FX_TYPE_NAME("fx.convertible");
|
||||||
|
FX_TYPE_CLASS(fx_convertible_class);
|
||||||
|
FX_TYPE_DEFINITION_END(fx_convertible)
|
||||||
@@ -0,0 +1,580 @@
|
|||||||
|
#include <ctype.h>
|
||||||
|
#include <fx/comparable.h>
|
||||||
|
#include <fx/convertible.h>
|
||||||
|
#include <fx/cstr.h>
|
||||||
|
#include <fx/hash.h>
|
||||||
|
#include <fx/operable.h>
|
||||||
|
#include <fx/stream.h>
|
||||||
|
#include <fx/value-type.h>
|
||||||
|
#include <fx/value.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
|
static fx_status to_string(
|
||||||
|
const fx_value *v,
|
||||||
|
fx_stream *out,
|
||||||
|
const char *format)
|
||||||
|
{
|
||||||
|
if (!v->v_cstr) {
|
||||||
|
return FX_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
return fx_stream_write_fmt(out, NULL, "%s", v->v_cstr);
|
||||||
|
}
|
||||||
|
|
||||||
|
static i32 compare(const fx_value *left, const fx_value *right)
|
||||||
|
{
|
||||||
|
const char *s;
|
||||||
|
if (!FX_OK(fx_value_get_cstr(right, &s))) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return strcmp(left->v_cstr, s);
|
||||||
|
}
|
||||||
|
|
||||||
|
static fx_status hash(const fx_value *v, uint64_t *out_hash)
|
||||||
|
{
|
||||||
|
*out_hash = fx_hash_cstr(v->v_cstr);
|
||||||
|
return FX_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
fx_status cstr_to_char(const char *in, fx_wchar *out)
|
||||||
|
{
|
||||||
|
if (in[0] == '\0' || in[1] == '\0') {
|
||||||
|
*out = in[0];
|
||||||
|
return FX_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
fx_status cstr_to_bool(const char *in, bool *out)
|
||||||
|
{
|
||||||
|
if (!strcmp(in, "1") || !strcmp(in, "true") || strcmp(in, "True")) {
|
||||||
|
*out = true;
|
||||||
|
return FX_SUCCESS;
|
||||||
|
} else if (
|
||||||
|
!strcmp(in, "0") || !strcmp(in, "false")
|
||||||
|
|| strcmp(in, "False")) {
|
||||||
|
*out = false;
|
||||||
|
return FX_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
fx_status cstr_to_i16(const char *in, i16 *out)
|
||||||
|
{
|
||||||
|
char *ep;
|
||||||
|
long v = strtol(in, &ep, 0);
|
||||||
|
if (*ep) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (v < INT16_MIN || v > INT16_MAX) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = (i16)v;
|
||||||
|
return FX_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
fx_status cstr_to_u16(const char *in, u16 *out)
|
||||||
|
{
|
||||||
|
char *ep;
|
||||||
|
unsigned long v = strtoul(in, &ep, 0);
|
||||||
|
if (*ep) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (v > UINT16_MAX) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = (u16)v;
|
||||||
|
return FX_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
fx_status cstr_to_i32(const char *in, i32 *out)
|
||||||
|
{
|
||||||
|
char *ep;
|
||||||
|
long long v = strtoll(in, &ep, 0);
|
||||||
|
if (*ep) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (v < INT32_MIN || v > INT32_MAX) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = (i32)v;
|
||||||
|
return FX_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
fx_status cstr_to_u32(const char *in, u32 *out)
|
||||||
|
{
|
||||||
|
char *ep;
|
||||||
|
unsigned long long v = strtoull(in, &ep, 0);
|
||||||
|
if (*ep) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (v > UINT32_MAX) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = (u32)v;
|
||||||
|
return FX_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
fx_status cstr_to_i64(const char *in, i64 *out)
|
||||||
|
{
|
||||||
|
char *ep;
|
||||||
|
long long v = strtoll(in, &ep, 0);
|
||||||
|
if (*ep) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (v < INT64_MIN || v > INT64_MAX) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = (i64)v;
|
||||||
|
return FX_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
fx_status cstr_to_u64(const char *in, u64 *out)
|
||||||
|
{
|
||||||
|
char *ep;
|
||||||
|
unsigned long long v = strtoull(in, &ep, 0);
|
||||||
|
if (*ep) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (v > UINT64_MAX) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = (u16)v;
|
||||||
|
return FX_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
fx_status cstr_to_iptr(const char *in, iptr *out)
|
||||||
|
{
|
||||||
|
char *ep;
|
||||||
|
intptr_t v = strtoimax(in, &ep, 0);
|
||||||
|
if (*ep) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (v < INTPTR_MIN || v > INTPTR_MAX) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = (iptr)v;
|
||||||
|
return FX_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
fx_status cstr_to_uptr(const char *in, uptr *out)
|
||||||
|
{
|
||||||
|
char *ep;
|
||||||
|
uintptr_t v = strtoumax(in, &ep, 0);
|
||||||
|
if (*ep) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (v > UINTPTR_MAX) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = (uptr)v;
|
||||||
|
return FX_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
fx_status cstr_to_sbyte(const char *in, sbyte *out)
|
||||||
|
{
|
||||||
|
char *ep;
|
||||||
|
long v = strtol(in, &ep, 0);
|
||||||
|
if (*ep) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (v > INT8_MAX) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = (sbyte)v;
|
||||||
|
return FX_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
fx_status cstr_to_byte(const char *in, byte *out)
|
||||||
|
{
|
||||||
|
char *ep;
|
||||||
|
unsigned long v = strtoul(in, &ep, 0);
|
||||||
|
if (*ep) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (v > UINT8_MAX) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = (byte)v;
|
||||||
|
return FX_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
fx_status cstr_to_short(const char *in, short *out)
|
||||||
|
{
|
||||||
|
char *ep;
|
||||||
|
long v = strtol(in, &ep, 0);
|
||||||
|
if (*ep) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (v < SHRT_MIN || v > SHRT_MAX) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = (short)v;
|
||||||
|
return FX_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
fx_status cstr_to_ushort(const char *in, unsigned short *out)
|
||||||
|
{
|
||||||
|
char *ep;
|
||||||
|
unsigned long v = strtoul(in, &ep, 0);
|
||||||
|
if (*ep) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (v > USHRT_MAX) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = (unsigned short)v;
|
||||||
|
return FX_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
fx_status cstr_to_int(const char *in, int *out)
|
||||||
|
{
|
||||||
|
char *ep;
|
||||||
|
long v = strtol(in, &ep, 0);
|
||||||
|
if (*ep) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (v < INT_MIN || v > INT_MAX) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = (int)v;
|
||||||
|
return FX_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
fx_status cstr_to_uint(const char *in, unsigned int *out)
|
||||||
|
{
|
||||||
|
char *ep;
|
||||||
|
unsigned long v = strtoul(in, &ep, 0);
|
||||||
|
if (*ep) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (v > UINT_MAX) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = (unsigned int)v;
|
||||||
|
return FX_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
fx_status cstr_to_long(const char *in, long *out)
|
||||||
|
{
|
||||||
|
char *ep;
|
||||||
|
long long v = strtoll(in, &ep, 0);
|
||||||
|
if (*ep) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (v < LONG_MIN || v > LONG_MAX) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = (long)v;
|
||||||
|
return FX_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
fx_status cstr_to_ulong(const char *in, unsigned long *out)
|
||||||
|
{
|
||||||
|
char *ep;
|
||||||
|
unsigned long long v = strtoull(in, &ep, 0);
|
||||||
|
if (*ep) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (v > ULONG_MAX) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = (unsigned long)v;
|
||||||
|
return FX_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
fx_status cstr_to_longlong(const char *in, long long *out)
|
||||||
|
{
|
||||||
|
char *ep;
|
||||||
|
long long v = strtoll(in, &ep, 0);
|
||||||
|
if (*ep) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (v < LLONG_MIN || v > LLONG_MAX) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = (long long)v;
|
||||||
|
return FX_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
fx_status cstr_to_ulonglong(const char *in, unsigned long long *out)
|
||||||
|
{
|
||||||
|
char *ep;
|
||||||
|
unsigned long long v = strtoull(in, &ep, 0);
|
||||||
|
if (*ep) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (v > ULLONG_MAX) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = (unsigned long long)v;
|
||||||
|
return FX_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
fx_status cstr_to_size(const char *in, size_t *out)
|
||||||
|
{
|
||||||
|
char *ep;
|
||||||
|
unsigned long long v = strtoull(in, &ep, 0);
|
||||||
|
if (*ep) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (v > SIZE_MAX) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = (size_t)v;
|
||||||
|
return FX_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
fx_status cstr_to_float(const char *in, float *out)
|
||||||
|
{
|
||||||
|
#if FX_ENABLE_FLOATING_POINT
|
||||||
|
char *ep;
|
||||||
|
float v = strtof(in, &ep);
|
||||||
|
if (*ep) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = v;
|
||||||
|
return FX_SUCCESS;
|
||||||
|
#else
|
||||||
|
return FX_ERR_NOT_SUPPORTED;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
fx_status cstr_to_double(const char *in, double *out)
|
||||||
|
{
|
||||||
|
#if FX_ENABLE_FLOATING_POINT
|
||||||
|
char *ep;
|
||||||
|
double v = strtod(in, &ep);
|
||||||
|
if (*ep) {
|
||||||
|
return FX_ERR_BAD_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = v;
|
||||||
|
return FX_SUCCESS;
|
||||||
|
#else
|
||||||
|
return FX_ERR_NOT_SUPPORTED;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
fx_status cstr_to_cstr(const char *in, const char **out)
|
||||||
|
{
|
||||||
|
*out = in;
|
||||||
|
return FX_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define CSTR_CONVERTER(fx_type, c_type) \
|
||||||
|
static fx_status to_##fx_type(const fx_value *in, c_type *out) \
|
||||||
|
{ \
|
||||||
|
const char *s = NULL; \
|
||||||
|
fx_value_get_cstr(in, &s); \
|
||||||
|
return cstr_to_##fx_type(s, out); \
|
||||||
|
}
|
||||||
|
|
||||||
|
CSTR_CONVERTER(bool, bool)
|
||||||
|
CSTR_CONVERTER(i16, i16)
|
||||||
|
CSTR_CONVERTER(u16, u16)
|
||||||
|
CSTR_CONVERTER(i32, i32)
|
||||||
|
CSTR_CONVERTER(u32, u32)
|
||||||
|
CSTR_CONVERTER(i64, i64)
|
||||||
|
CSTR_CONVERTER(u64, u64)
|
||||||
|
CSTR_CONVERTER(iptr, iptr)
|
||||||
|
CSTR_CONVERTER(uptr, uptr)
|
||||||
|
CSTR_CONVERTER(sbyte, sbyte)
|
||||||
|
CSTR_CONVERTER(byte, byte)
|
||||||
|
CSTR_CONVERTER(short, short)
|
||||||
|
CSTR_CONVERTER(ushort, unsigned short)
|
||||||
|
CSTR_CONVERTER(int, int)
|
||||||
|
CSTR_CONVERTER(uint, unsigned int)
|
||||||
|
CSTR_CONVERTER(long, long)
|
||||||
|
CSTR_CONVERTER(ulong, unsigned long)
|
||||||
|
CSTR_CONVERTER(longlong, long long)
|
||||||
|
CSTR_CONVERTER(ulonglong, unsigned long long)
|
||||||
|
CSTR_CONVERTER(size, size_t)
|
||||||
|
CSTR_CONVERTER(float, float)
|
||||||
|
CSTR_CONVERTER(double, double)
|
||||||
|
CSTR_CONVERTER(cstr, const char *)
|
||||||
|
|
||||||
|
static fx_status add(const fx_value *l, const fx_value *r, fx_value *out)
|
||||||
|
{
|
||||||
|
const char *left = l->v_cstr;
|
||||||
|
const char *right = r->v_cstr;
|
||||||
|
fx_string *result = fx_string_create_from_cstr(left);
|
||||||
|
if (!result) {
|
||||||
|
return FX_ERR_NO_MEMORY;
|
||||||
|
}
|
||||||
|
|
||||||
|
fx_string_append_cstr(result, right);
|
||||||
|
*out = FX_VALUE_OBJECT(result);
|
||||||
|
|
||||||
|
return FX_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*** CLASS DEFINITION *********************************************************/
|
||||||
|
|
||||||
|
FX_TYPE_CLASS_BEGIN(fx_cstr)
|
||||||
|
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||||
|
FX_INTERFACE_ENTRY(to_string) = to_string;
|
||||||
|
FX_INTERFACE_ENTRY(hash) = hash;
|
||||||
|
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||||
|
|
||||||
|
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE)
|
||||||
|
FX_INTERFACE_ENTRY(op_add) = add;
|
||||||
|
FX_TYPE_VTABLE_INTERFACE_END(fx_operable, FX_TYPE_OPERABLE)
|
||||||
|
|
||||||
|
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_comparable, FX_TYPE_COMPARABLE)
|
||||||
|
FX_INTERFACE_ENTRY(c_compare) = compare;
|
||||||
|
FX_TYPE_VTABLE_INTERFACE_END(fx_comparable, FX_TYPE_COMPARABLE)
|
||||||
|
|
||||||
|
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_convertible, FX_TYPE_CONVERTIBLE)
|
||||||
|
FX_INTERFACE_ENTRY(c_to_bool) = to_bool;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_bool) = to_bool;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_i16) = to_i16;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_u16) = to_u16;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_u32) = to_u32;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_i64) = to_i64;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_u64) = to_u64;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_iptr) = to_iptr;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_uptr) = to_uptr;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_sbyte) = to_sbyte;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_byte) = to_byte;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_short) = to_short;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_ushort) = to_ushort;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_int) = to_int;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_uint) = to_uint;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_long) = to_long;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_ulong) = to_ulong;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_longlong) = to_longlong;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_ulonglong) = to_ulonglong;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_size) = to_size;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_float) = to_float;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_double) = to_double;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_cstr) = to_cstr;
|
||||||
|
FX_TYPE_VTABLE_INTERFACE_END(fx_convertible, FX_TYPE_CONVERTIBLE)
|
||||||
|
FX_TYPE_CLASS_END(fx_cstr)
|
||||||
|
|
||||||
|
FX_TYPE_DEFINITION_BEGIN(fx_cstr)
|
||||||
|
__FX_VALUE_TYPE_ID(CSTR);
|
||||||
|
FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE);
|
||||||
|
FX_TYPE_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
|
||||||
|
FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE);
|
||||||
|
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
|
||||||
|
FX_TYPE_NAME("fx.cstr");
|
||||||
|
FX_TYPE_CLASS(fx_cstr_class);
|
||||||
|
FX_TYPE_INSTANCE_PRIVATE(char *);
|
||||||
|
FX_TYPE_DEFINITION_END(fx_cstr)
|
||||||
|
|
||||||
|
/*** MISC FUNCTIONS ***********************************************************/
|
||||||
|
|
||||||
|
char *fx_strdup(const char *s)
|
||||||
|
{
|
||||||
|
size_t len = strlen(s);
|
||||||
|
char *p = malloc(len + 1);
|
||||||
|
if (!p) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(p, s, len);
|
||||||
|
p[len] = '\0';
|
||||||
|
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t fx_strlen(const char *s, fx_strlen_flags flags)
|
||||||
|
{
|
||||||
|
if (!(flags & (FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD))) {
|
||||||
|
return strlen(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t out = 0;
|
||||||
|
for (size_t i = 0; s[i]; i++) {
|
||||||
|
if (s[i] == '\033' && (flags & FX_STRLEN_IGNORE_ESC)) {
|
||||||
|
while (!isalpha(s[i]) && s[i]) {
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (s[i] == '[' && (flags & FX_STRLEN_IGNORE_MOD)) {
|
||||||
|
i++;
|
||||||
|
if (s[i] == '[') {
|
||||||
|
out++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (s[i] != ']' && s[i]) {
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
out++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t fx_strcmp_nocase(const char *a, const char *b)
|
||||||
|
{
|
||||||
|
while (1) {
|
||||||
|
char ch_a = tolower(*a);
|
||||||
|
char ch_b = tolower(*b);
|
||||||
|
|
||||||
|
if (!ch_a && !ch_b) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ch_a != ch_b) {
|
||||||
|
return ch_a - ch_b;
|
||||||
|
}
|
||||||
|
|
||||||
|
a++;
|
||||||
|
b++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
#ifndef CSTR_H_
|
||||||
|
#define CSTR_H_
|
||||||
|
|
||||||
|
#include <fx/status.h>
|
||||||
|
#include <fx/value.h>
|
||||||
|
|
||||||
|
extern fx_status cstr_to_char(const char *in, fx_wchar *out);
|
||||||
|
|
||||||
|
extern fx_status cstr_to_bool(const char *in, bool *out);
|
||||||
|
extern fx_status cstr_to_i16(const char *in, i16 *out);
|
||||||
|
extern fx_status cstr_to_u16(const char *in, u16 *out);
|
||||||
|
extern fx_status cstr_to_i32(const char *in, i32 *out);
|
||||||
|
extern fx_status cstr_to_u32(const char *in, u32 *out);
|
||||||
|
extern fx_status cstr_to_i64(const char *in, i64 *out);
|
||||||
|
extern fx_status cstr_to_u64(const char *in, u64 *out);
|
||||||
|
extern fx_status cstr_to_iptr(const char *in, iptr *out);
|
||||||
|
extern fx_status cstr_to_uptr(const char *in, uptr *out);
|
||||||
|
|
||||||
|
extern fx_status cstr_to_sbyte(const char *in, sbyte *out);
|
||||||
|
extern fx_status cstr_to_byte(const char *in, byte *out);
|
||||||
|
extern fx_status cstr_to_short(const char *in, short *out);
|
||||||
|
extern fx_status cstr_to_ushort(const char *in, unsigned short *out);
|
||||||
|
extern fx_status cstr_to_int(const char *in, int *out);
|
||||||
|
extern fx_status cstr_to_uint(const char *in, unsigned int *out);
|
||||||
|
extern fx_status cstr_to_long(const char *in, long *out);
|
||||||
|
extern fx_status cstr_to_ulong(const char *in, unsigned long *out);
|
||||||
|
extern fx_status cstr_to_longlong(const char *in, long long *out);
|
||||||
|
extern fx_status cstr_to_ulonglong(const char *in, unsigned long long *out);
|
||||||
|
extern fx_status cstr_to_size(const char *in, size_t *out);
|
||||||
|
|
||||||
|
extern fx_status cstr_to_float(const char *in, float *out);
|
||||||
|
extern fx_status cstr_to_double(const char *in, double *out);
|
||||||
|
extern fx_status cstr_to_cstr(const char *in, const char **out);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
#include <fx/collections/datetime.h>
|
#include <fx/datetime.h>
|
||||||
#include <fx/stream.h>
|
#include <fx/stream.h>
|
||||||
#include <fx/string.h>
|
#include <fx/string.h>
|
||||||
|
#include <fx/value.h>
|
||||||
|
|
||||||
/*** PRIVATE DATA *************************************************************/
|
/*** PRIVATE DATA *************************************************************/
|
||||||
|
|
||||||
@@ -338,7 +339,7 @@ static enum fx_status encode_rfc3339(
|
|||||||
dt->dt_sec);
|
dt->dt_sec);
|
||||||
|
|
||||||
if (dt->dt_msec > 0) {
|
if (dt->dt_msec > 0) {
|
||||||
fx_stream_write_fmt(out, NULL, ".%04ld", dt->dt_msec);
|
fx_stream_write_fmt(out, NULL, ".%03ld", dt->dt_msec);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!dt->dt_localtime) {
|
if (!dt->dt_localtime) {
|
||||||
@@ -566,9 +567,13 @@ static void datetime_fini(fx_object *obj, void *priv)
|
|||||||
struct fx_datetime_p *dt = priv;
|
struct fx_datetime_p *dt = priv;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _datetime_to_string(const fx_object *obj, fx_stream *out)
|
static fx_status _datetime_to_string(
|
||||||
|
const fx_value *obj,
|
||||||
|
fx_stream *out,
|
||||||
|
const char *format)
|
||||||
{
|
{
|
||||||
struct fx_datetime_p *dt = fx_object_get_private(obj, FX_TYPE_DATETIME);
|
struct fx_datetime_p *dt
|
||||||
|
= fx_object_get_private(obj->v_object, FX_TYPE_DATETIME);
|
||||||
|
|
||||||
if (dt->dt_has_date) {
|
if (dt->dt_has_date) {
|
||||||
fx_stream_write_fmt(
|
fx_stream_write_fmt(
|
||||||
@@ -607,6 +612,8 @@ static void _datetime_to_string(const fx_object *obj, fx_stream *out)
|
|||||||
dt->dt_zone_offset_minute);
|
dt->dt_zone_offset_minute);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return FX_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*** CLASS DEFINITION *********************************************************/
|
/*** CLASS DEFINITION *********************************************************/
|
||||||
-280
@@ -1,280 +0,0 @@
|
|||||||
#include <fx/double.h>
|
|
||||||
#include <fx/stream.h>
|
|
||||||
#include <inttypes.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
/*** PRIVATE DATA *************************************************************/
|
|
||||||
|
|
||||||
enum double_type {
|
|
||||||
DOUBLE_REGULAR = 0,
|
|
||||||
DOUBLE_INF_POSITIVE,
|
|
||||||
DOUBLE_INF_NEGATIVE,
|
|
||||||
DOUBLE_NAN_POSITIVE,
|
|
||||||
DOUBLE_NAN_NEGATIVE,
|
|
||||||
};
|
|
||||||
|
|
||||||
struct fx_double_p {
|
|
||||||
enum double_type i_type;
|
|
||||||
double i_value;
|
|
||||||
};
|
|
||||||
|
|
||||||
/*** PRIVATE FUNCTIONS ********************************************************/
|
|
||||||
|
|
||||||
static bool double_get_value(const struct fx_double_p *i)
|
|
||||||
{
|
|
||||||
return i->i_value;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void double_set_value(struct fx_double_p *i, double v)
|
|
||||||
{
|
|
||||||
i->i_type = DOUBLE_REGULAR;
|
|
||||||
i->i_value = v;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void double_set_value_nan(struct fx_double_p *i)
|
|
||||||
{
|
|
||||||
i->i_type = DOUBLE_NAN_POSITIVE;
|
|
||||||
i->i_value = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void double_set_value_nan_negative(struct fx_double_p *i)
|
|
||||||
{
|
|
||||||
i->i_type = DOUBLE_NAN_NEGATIVE;
|
|
||||||
i->i_value = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void double_set_value_inf(struct fx_double_p *i)
|
|
||||||
{
|
|
||||||
i->i_type = DOUBLE_INF_POSITIVE;
|
|
||||||
i->i_value = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void double_set_value_inf_negative(struct fx_double_p *i)
|
|
||||||
{
|
|
||||||
i->i_type = DOUBLE_INF_NEGATIVE;
|
|
||||||
i->i_value = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool double_is_nan(struct fx_double_p *i)
|
|
||||||
{
|
|
||||||
return i->i_type == DOUBLE_NAN_POSITIVE
|
|
||||||
|| i->i_type == DOUBLE_NAN_NEGATIVE;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool double_is_nan_positive(struct fx_double_p *i)
|
|
||||||
{
|
|
||||||
return i->i_type == DOUBLE_NAN_POSITIVE;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool double_is_nan_negative(struct fx_double_p *i)
|
|
||||||
{
|
|
||||||
return i->i_type == DOUBLE_NAN_NEGATIVE;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool double_is_inf(struct fx_double_p *i)
|
|
||||||
{
|
|
||||||
return i->i_type == DOUBLE_INF_POSITIVE
|
|
||||||
|| i->i_type == DOUBLE_INF_NEGATIVE;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool double_is_inf_positive(struct fx_double_p *i)
|
|
||||||
{
|
|
||||||
return i->i_type == DOUBLE_INF_POSITIVE;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool double_is_inf_negative(struct fx_double_p *i)
|
|
||||||
{
|
|
||||||
return i->i_type == DOUBLE_INF_NEGATIVE;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*** PUBLIC FUNCTIONS *********************************************************/
|
|
||||||
|
|
||||||
fx_double *fx_double_create(double value)
|
|
||||||
{
|
|
||||||
fx_double *i = fx_object_create(FX_TYPE_DOUBLE);
|
|
||||||
if (!i) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct fx_double_p *p = fx_object_get_private(i, FX_TYPE_DOUBLE);
|
|
||||||
p->i_type = DOUBLE_REGULAR;
|
|
||||||
p->i_value = value;
|
|
||||||
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
fx_double *fx_double_create_nan(void)
|
|
||||||
{
|
|
||||||
fx_double *i = fx_object_create(FX_TYPE_DOUBLE);
|
|
||||||
if (!i) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct fx_double_p *p = fx_object_get_private(i, FX_TYPE_DOUBLE);
|
|
||||||
p->i_type = DOUBLE_NAN_POSITIVE;
|
|
||||||
p->i_value = 0;
|
|
||||||
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
fx_double *fx_double_create_nan_negative(void)
|
|
||||||
{
|
|
||||||
fx_double *i = fx_object_create(FX_TYPE_DOUBLE);
|
|
||||||
if (!i) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct fx_double_p *p = fx_object_get_private(i, FX_TYPE_DOUBLE);
|
|
||||||
p->i_type = DOUBLE_NAN_NEGATIVE;
|
|
||||||
p->i_value = 0;
|
|
||||||
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
fx_double *fx_double_create_inf(void)
|
|
||||||
{
|
|
||||||
fx_double *i = fx_object_create(FX_TYPE_DOUBLE);
|
|
||||||
if (!i) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct fx_double_p *p = fx_object_get_private(i, FX_TYPE_DOUBLE);
|
|
||||||
p->i_type = DOUBLE_INF_POSITIVE;
|
|
||||||
p->i_value = 0;
|
|
||||||
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
fx_double *fx_double_create_inf_negative(void)
|
|
||||||
{
|
|
||||||
fx_double *i = fx_object_create(FX_TYPE_DOUBLE);
|
|
||||||
if (!i) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct fx_double_p *p = fx_object_get_private(i, FX_TYPE_DOUBLE);
|
|
||||||
p->i_type = DOUBLE_INF_NEGATIVE;
|
|
||||||
p->i_value = 0;
|
|
||||||
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
double fx_double_get_value(const fx_double *i)
|
|
||||||
{
|
|
||||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DOUBLE, double_get_value, i);
|
|
||||||
}
|
|
||||||
|
|
||||||
void fx_double_set_value(fx_double *i, double v)
|
|
||||||
{
|
|
||||||
FX_CLASS_DISPATCH_STATIC_V(FX_TYPE_DOUBLE, double_set_value, i, v);
|
|
||||||
}
|
|
||||||
|
|
||||||
void fx_double_set_value_nan(fx_double *i)
|
|
||||||
{
|
|
||||||
FX_CLASS_DISPATCH_STATIC_V0(FX_TYPE_DOUBLE, double_set_value_nan, i);
|
|
||||||
}
|
|
||||||
|
|
||||||
void fx_double_set_value_nan_negative(fx_double *i)
|
|
||||||
{
|
|
||||||
FX_CLASS_DISPATCH_STATIC_V0(
|
|
||||||
FX_TYPE_DOUBLE,
|
|
||||||
double_set_value_nan_negative,
|
|
||||||
i);
|
|
||||||
}
|
|
||||||
|
|
||||||
void fx_double_set_value_inf(fx_double *i)
|
|
||||||
{
|
|
||||||
FX_CLASS_DISPATCH_STATIC_V0(FX_TYPE_DOUBLE, double_set_value_inf, i);
|
|
||||||
}
|
|
||||||
|
|
||||||
void fx_double_set_value_inf_negative(fx_double *i)
|
|
||||||
{
|
|
||||||
FX_CLASS_DISPATCH_STATIC_V0(
|
|
||||||
FX_TYPE_DOUBLE,
|
|
||||||
double_set_value_inf_negative,
|
|
||||||
i);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool fx_double_is_nan(const fx_double *i)
|
|
||||||
{
|
|
||||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DOUBLE, double_is_nan, i);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool fx_double_is_nan_positive(const fx_double *i)
|
|
||||||
{
|
|
||||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DOUBLE, double_is_nan_positive, i);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool fx_double_is_nan_negative(const fx_double *i)
|
|
||||||
{
|
|
||||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DOUBLE, double_is_nan_negative, i);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool fx_double_is_inf(const fx_double *i)
|
|
||||||
{
|
|
||||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DOUBLE, double_is_inf, i);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool fx_double_is_inf_positive(const fx_double *i)
|
|
||||||
{
|
|
||||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DOUBLE, double_is_inf_positive, i);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool fx_double_is_inf_negative(const fx_double *i)
|
|
||||||
{
|
|
||||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DOUBLE, double_is_inf_negative, i);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*** VIRTUAL FUNCTIONS ********************************************************/
|
|
||||||
|
|
||||||
static void double_init(fx_object *obj, void *priv)
|
|
||||||
{
|
|
||||||
struct fx_double_p *i = priv;
|
|
||||||
|
|
||||||
i->i_type = DOUBLE_REGULAR;
|
|
||||||
i->i_value = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void double_fini(fx_object *obj, void *priv)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
static void double_to_string(const fx_object *obj, fx_stream *out)
|
|
||||||
{
|
|
||||||
struct fx_double_p *i = fx_object_get_private(obj, FX_TYPE_DOUBLE);
|
|
||||||
switch (i->i_type) {
|
|
||||||
case DOUBLE_REGULAR:
|
|
||||||
fx_stream_write_fmt(out, NULL, "%" PRIdPTR, i->i_value);
|
|
||||||
break;
|
|
||||||
case DOUBLE_INF_POSITIVE:
|
|
||||||
fx_stream_write_cstr(out, "inf", NULL);
|
|
||||||
break;
|
|
||||||
case DOUBLE_INF_NEGATIVE:
|
|
||||||
fx_stream_write_cstr(out, "-inf", NULL);
|
|
||||||
break;
|
|
||||||
case DOUBLE_NAN_POSITIVE:
|
|
||||||
fx_stream_write_cstr(out, "NaN", NULL);
|
|
||||||
break;
|
|
||||||
case DOUBLE_NAN_NEGATIVE:
|
|
||||||
fx_stream_write_cstr(out, "-NaN", NULL);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*** CLASS DEFINITION *********************************************************/
|
|
||||||
|
|
||||||
FX_TYPE_CLASS_BEGIN(fx_double)
|
|
||||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
|
||||||
FX_INTERFACE_ENTRY(to_string) = double_to_string;
|
|
||||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
|
||||||
FX_TYPE_CLASS_END(fx_double)
|
|
||||||
|
|
||||||
FX_TYPE_DEFINITION_BEGIN(fx_double)
|
|
||||||
FX_TYPE_ID(0x3b20f57a, 0x2ddf, 0x4682, 0x81c4, 0x4fe404a6524e);
|
|
||||||
FX_TYPE_CLASS(fx_double_class);
|
|
||||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_double_p);
|
|
||||||
FX_TYPE_INSTANCE_INIT(double_init);
|
|
||||||
FX_TYPE_INSTANCE_FINI(double_fini);
|
|
||||||
FX_TYPE_DEFINITION_END(fx_double)
|
|
||||||
+61
-1
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
bool fx_wchar_is_number(fx_wchar c)
|
bool fx_wchar_is_number(fx_wchar c)
|
||||||
{
|
{
|
||||||
return iswdigit((wchar_t)c);
|
return (c >= '0' && c <= '9');
|
||||||
}
|
}
|
||||||
|
|
||||||
bool fx_wchar_is_alpha(fx_wchar c)
|
bool fx_wchar_is_alpha(fx_wchar c)
|
||||||
@@ -1177,6 +1177,66 @@ bool fx_wchar_is_alpha(fx_wchar c)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool wchar_is_control(fx_wchar c)
|
||||||
|
{
|
||||||
|
if (c >= 0x0000 && c <= 0x001F) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (c == 0x007F) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (c >= 0x0080 && c <= 0x009F) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (c) {
|
||||||
|
case 0x00AD:
|
||||||
|
case 0x034F:
|
||||||
|
case 0x070F:
|
||||||
|
case 0x180E:
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (c >= 0x200B && c <= 0x200F) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (c >= 0x202A && c <= 0x202E) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (c >= 0x2060 && c <= 0x2064) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (c >= 0x2066 && c <= 0x206F) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (c == 0xFEFF) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (c >= 0xFFF9 && c <= 0xFFFB) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (c >= 0x13430 && c <= 0x13438) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool fx_wchar_is_graph(fx_wchar c)
|
||||||
|
{
|
||||||
|
return !wchar_is_control(c);
|
||||||
|
}
|
||||||
|
|
||||||
bool fx_wchar_is_hex_digit(fx_wchar c)
|
bool fx_wchar_is_hex_digit(fx_wchar c)
|
||||||
{
|
{
|
||||||
return iswxdigit(c);
|
return iswxdigit(c);
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include <fx/endian.h>
|
#include <fx/endian.h>
|
||||||
|
#if 0
|
||||||
|
|
||||||
fx_i16 fx_i16_htob(uint16_t v)
|
fx_i16 fx_i16_htob(uint16_t v)
|
||||||
{
|
{
|
||||||
@@ -211,3 +212,4 @@ uint64_t fx_i64_stoh(fx_i64 v)
|
|||||||
|
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|||||||
@@ -0,0 +1,101 @@
|
|||||||
|
#include "../int/convert.h"
|
||||||
|
|
||||||
|
#include <fx/float.h>
|
||||||
|
#include <fx/value-type.h>
|
||||||
|
|
||||||
|
BOOL_CONVERT(double)
|
||||||
|
GENERIC_CONVERT(double, i16, i16)
|
||||||
|
GENERIC_CONVERT(double, u16, u16)
|
||||||
|
GENERIC_CONVERT(double, i32, i32)
|
||||||
|
GENERIC_CONVERT(double, u32, u32)
|
||||||
|
GENERIC_CONVERT(double, i64, i64)
|
||||||
|
GENERIC_CONVERT(double, u64, u64)
|
||||||
|
GENERIC_CONVERT(double, iptr, iptr)
|
||||||
|
GENERIC_CONVERT(double, uptr, uptr)
|
||||||
|
GENERIC_CONVERT(double, sbyte, sbyte)
|
||||||
|
GENERIC_CONVERT(double, byte, byte)
|
||||||
|
GENERIC_CONVERT(double, short, short)
|
||||||
|
GENERIC_CONVERT(double, ushort, unsigned short)
|
||||||
|
GENERIC_CONVERT(double, int, int)
|
||||||
|
GENERIC_CONVERT(double, uint, unsigned int)
|
||||||
|
GENERIC_CONVERT(double, long, long)
|
||||||
|
GENERIC_CONVERT(double, ulong, unsigned long)
|
||||||
|
GENERIC_CONVERT(double, longlong, long long)
|
||||||
|
GENERIC_CONVERT(double, ulonglong, unsigned long long)
|
||||||
|
GENERIC_CONVERT(double, size, size_t)
|
||||||
|
GENERIC_CONVERT(double, float, float)
|
||||||
|
|
||||||
|
TO_STRING(double, "%g")
|
||||||
|
|
||||||
|
BINARY_OP(add, +, double)
|
||||||
|
BINARY_OP(subtract, -, double)
|
||||||
|
BINARY_OP(multiply, *, double)
|
||||||
|
BINARY_OP(divide, /, double)
|
||||||
|
PREFIX_OP(increment, ++, double)
|
||||||
|
PREFIX_OP(decrement, --, double)
|
||||||
|
IN_PLACE_OP(add_in_place, +=, double)
|
||||||
|
IN_PLACE_OP(subtract_in_place, -=, double)
|
||||||
|
IN_PLACE_OP(multiply_in_place, *=, double)
|
||||||
|
IN_PLACE_OP(divide_in_place, /=, double)
|
||||||
|
|
||||||
|
COMPARE(double, double)
|
||||||
|
|
||||||
|
/*** CLASS DEFINITION *********************************************************/
|
||||||
|
|
||||||
|
FX_TYPE_CLASS_BEGIN(fx_double)
|
||||||
|
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||||
|
FX_INTERFACE_ENTRY(to_string) = to_string;
|
||||||
|
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||||
|
|
||||||
|
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_comparable, FX_TYPE_COMPARABLE)
|
||||||
|
FX_INTERFACE_ENTRY(c_compare) = compare;
|
||||||
|
FX_TYPE_VTABLE_INTERFACE_END(fx_comparable, FX_TYPE_COMPARABLE)
|
||||||
|
|
||||||
|
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE)
|
||||||
|
FX_INTERFACE_ENTRY(op_add) = add;
|
||||||
|
FX_INTERFACE_ENTRY(op_subtract) = subtract;
|
||||||
|
FX_INTERFACE_ENTRY(op_multiply) = multiply;
|
||||||
|
FX_INTERFACE_ENTRY(op_divide) = divide;
|
||||||
|
FX_INTERFACE_ENTRY(op_increment) = increment;
|
||||||
|
FX_INTERFACE_ENTRY(op_decrement) = decrement;
|
||||||
|
FX_INTERFACE_ENTRY(op_add_in_place) = add_in_place;
|
||||||
|
FX_INTERFACE_ENTRY(op_subtract_in_place) = subtract_in_place;
|
||||||
|
FX_INTERFACE_ENTRY(op_multiply_in_place) = multiply_in_place;
|
||||||
|
FX_INTERFACE_ENTRY(op_divide_in_place) = divide_in_place;
|
||||||
|
FX_TYPE_VTABLE_INTERFACE_END(fx_operable, FX_TYPE_OPERABLE)
|
||||||
|
|
||||||
|
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_convertible, FX_TYPE_CONVERTIBLE)
|
||||||
|
FX_INTERFACE_ENTRY(c_to_bool) = to_bool;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_i16) = to_i16;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_u16) = to_u16;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_i32) = to_i32;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_u32) = to_u32;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_i64) = to_i64;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_u64) = to_u64;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_iptr) = to_iptr;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_uptr) = to_uptr;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_sbyte) = to_sbyte;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_byte) = to_byte;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_short) = to_short;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_ushort) = to_ushort;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_int) = to_int;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_uint) = to_uint;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_long) = to_long;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_ulong) = to_ulong;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_longlong) = to_longlong;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_ulonglong) = to_ulonglong;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_size) = to_size;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_float) = to_float;
|
||||||
|
FX_TYPE_VTABLE_INTERFACE_END(fx_convertible, FX_TYPE_CONVERTIBLE)
|
||||||
|
FX_TYPE_CLASS_END(fx_double)
|
||||||
|
|
||||||
|
FX_TYPE_DEFINITION_BEGIN(fx_double)
|
||||||
|
__FX_VALUE_TYPE_ID(DOUBLE);
|
||||||
|
FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE);
|
||||||
|
FX_TYPE_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
|
||||||
|
FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE);
|
||||||
|
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
|
||||||
|
FX_TYPE_NAME("fx.double");
|
||||||
|
FX_TYPE_CLASS(fx_double_class);
|
||||||
|
FX_TYPE_INSTANCE_PRIVATE(double);
|
||||||
|
FX_TYPE_DEFINITION_END(fx_double)
|
||||||
@@ -0,0 +1,100 @@
|
|||||||
|
#include "../int/convert.h"
|
||||||
|
|
||||||
|
#include <fx/float.h>
|
||||||
|
#include <fx/value-type.h>
|
||||||
|
|
||||||
|
TO_STRING(float, "%g")
|
||||||
|
|
||||||
|
BOOL_CONVERT(float)
|
||||||
|
GENERIC_CONVERT(float, i16, i16)
|
||||||
|
GENERIC_CONVERT(float, u16, u16)
|
||||||
|
GENERIC_CONVERT(float, i32, i32)
|
||||||
|
GENERIC_CONVERT(float, u32, u32)
|
||||||
|
GENERIC_CONVERT(float, i64, i64)
|
||||||
|
GENERIC_CONVERT(float, u64, u64)
|
||||||
|
GENERIC_CONVERT(float, iptr, iptr)
|
||||||
|
GENERIC_CONVERT(float, uptr, uptr)
|
||||||
|
GENERIC_CONVERT(float, sbyte, sbyte)
|
||||||
|
GENERIC_CONVERT(float, byte, byte)
|
||||||
|
GENERIC_CONVERT(float, short, short)
|
||||||
|
GENERIC_CONVERT(float, ushort, unsigned short)
|
||||||
|
GENERIC_CONVERT(float, int, int)
|
||||||
|
GENERIC_CONVERT(float, uint, unsigned int)
|
||||||
|
GENERIC_CONVERT(float, long, long)
|
||||||
|
GENERIC_CONVERT(float, ulong, unsigned long)
|
||||||
|
GENERIC_CONVERT(float, longlong, long long)
|
||||||
|
GENERIC_CONVERT(float, ulonglong, unsigned long long)
|
||||||
|
GENERIC_CONVERT(float, size, size_t)
|
||||||
|
GENERIC_CONVERT(float, double, double)
|
||||||
|
|
||||||
|
BINARY_OP(add, +, float)
|
||||||
|
BINARY_OP(subtract, -, float)
|
||||||
|
BINARY_OP(multiply, *, float)
|
||||||
|
BINARY_OP(divide, /, float)
|
||||||
|
PREFIX_OP(increment, ++, float)
|
||||||
|
PREFIX_OP(decrement, --, float)
|
||||||
|
IN_PLACE_OP(add_in_place, +=, float)
|
||||||
|
IN_PLACE_OP(subtract_in_place, -=, float)
|
||||||
|
IN_PLACE_OP(multiply_in_place, *=, float)
|
||||||
|
IN_PLACE_OP(divide_in_place, /=, float)
|
||||||
|
|
||||||
|
COMPARE(float, float)
|
||||||
|
|
||||||
|
/*** CLASS DEFINITION *********************************************************/
|
||||||
|
|
||||||
|
FX_TYPE_CLASS_BEGIN(fx_float)
|
||||||
|
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||||
|
FX_INTERFACE_ENTRY(to_string) = to_string;
|
||||||
|
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||||
|
|
||||||
|
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_comparable, FX_TYPE_COMPARABLE)
|
||||||
|
FX_INTERFACE_ENTRY(c_compare) = compare;
|
||||||
|
FX_TYPE_VTABLE_INTERFACE_END(fx_comparable, FX_TYPE_COMPARABLE)
|
||||||
|
|
||||||
|
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE)
|
||||||
|
FX_INTERFACE_ENTRY(op_add) = add;
|
||||||
|
FX_INTERFACE_ENTRY(op_subtract) = subtract;
|
||||||
|
FX_INTERFACE_ENTRY(op_multiply) = multiply;
|
||||||
|
FX_INTERFACE_ENTRY(op_divide) = divide;
|
||||||
|
FX_INTERFACE_ENTRY(op_increment) = increment;
|
||||||
|
FX_INTERFACE_ENTRY(op_decrement) = decrement;
|
||||||
|
FX_INTERFACE_ENTRY(op_add_in_place) = add_in_place;
|
||||||
|
FX_INTERFACE_ENTRY(op_subtract_in_place) = subtract_in_place;
|
||||||
|
FX_INTERFACE_ENTRY(op_multiply_in_place) = multiply_in_place;
|
||||||
|
FX_INTERFACE_ENTRY(op_divide_in_place) = divide_in_place;
|
||||||
|
FX_TYPE_VTABLE_INTERFACE_END(fx_operable, FX_TYPE_OPERABLE)
|
||||||
|
|
||||||
|
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_convertible, FX_TYPE_CONVERTIBLE)
|
||||||
|
FX_INTERFACE_ENTRY(c_to_bool) = to_bool;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_i16) = to_i16;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_u16) = to_u16;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_i32) = to_i32;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_u32) = to_u32;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_i64) = to_i64;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_u64) = to_u64;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_iptr) = to_iptr;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_uptr) = to_uptr;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_sbyte) = to_sbyte;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_byte) = to_byte;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_short) = to_short;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_ushort) = to_ushort;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_int) = to_int;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_uint) = to_uint;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_long) = to_long;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_ulong) = to_ulong;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_longlong) = to_longlong;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_ulonglong) = to_ulonglong;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_size) = to_size;
|
||||||
|
FX_INTERFACE_ENTRY(c_to_double) = to_double;
|
||||||
|
FX_TYPE_VTABLE_INTERFACE_END(fx_convertible, FX_TYPE_CONVERTIBLE)
|
||||||
|
FX_TYPE_CLASS_END(fx_float)
|
||||||
|
|
||||||
|
FX_TYPE_DEFINITION_BEGIN(fx_float)
|
||||||
|
__FX_VALUE_TYPE_ID(FLOAT);
|
||||||
|
FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE);
|
||||||
|
FX_TYPE_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
|
||||||
|
FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE);
|
||||||
|
FX_TYPE_NAME("fx.float");
|
||||||
|
FX_TYPE_CLASS(fx_float_class);
|
||||||
|
FX_TYPE_INSTANCE_PRIVATE(float);
|
||||||
|
FX_TYPE_DEFINITION_END(fx_float)
|
||||||
+29
-2
@@ -40,8 +40,8 @@ static const struct fx_hash_function_ops *hash_functions[] = {
|
|||||||
[FX_HASH_SHAKE128] = &z__fx_shake128_ops,
|
[FX_HASH_SHAKE128] = &z__fx_shake128_ops,
|
||||||
[FX_HASH_SHAKE256] = &z__fx_shake256_ops,
|
[FX_HASH_SHAKE256] = &z__fx_shake256_ops,
|
||||||
};
|
};
|
||||||
static const size_t nr_hash_functions
|
static const size_t nr_hash_functions = sizeof hash_functions
|
||||||
= sizeof hash_functions / sizeof hash_functions[0];
|
/ sizeof hash_functions[0];
|
||||||
|
|
||||||
uint64_t fx_hash_cstr(const char *s)
|
uint64_t fx_hash_cstr(const char *s)
|
||||||
{
|
{
|
||||||
@@ -66,6 +66,33 @@ uint64_t fx_hash_cstr_ex(const char *s, size_t *len)
|
|||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint64_t fx_hash_wstr(const fx_wchar *s)
|
||||||
|
{
|
||||||
|
uint64_t hash = FNV1_OFFSET_BASIS;
|
||||||
|
size_t i = 0;
|
||||||
|
|
||||||
|
for (i = 0; s[i]; i++) {
|
||||||
|
hash ^= s[i];
|
||||||
|
hash *= FNV1_PRIME;
|
||||||
|
}
|
||||||
|
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t fx_hash_buffer(const void *p, size_t len)
|
||||||
|
{
|
||||||
|
const char *s = p;
|
||||||
|
uint64_t hash = FNV1_OFFSET_BASIS;
|
||||||
|
size_t i = 0;
|
||||||
|
|
||||||
|
for (i = 0; i < len; i++) {
|
||||||
|
hash ^= s[i];
|
||||||
|
hash *= FNV1_PRIME;
|
||||||
|
}
|
||||||
|
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
|
||||||
enum fx_status fx_hash_ctx_init(
|
enum fx_status fx_hash_ctx_init(
|
||||||
struct fx_hash_ctx *ctx,
|
struct fx_hash_ctx *ctx,
|
||||||
enum fx_hash_function func)
|
enum fx_hash_function func)
|
||||||
|
|||||||
+8
-9
@@ -30,18 +30,18 @@ A million repetitions of "a"
|
|||||||
|
|
||||||
/* blk0() and blk() perform the initial expand. */
|
/* blk0() and blk() perform the initial expand. */
|
||||||
/* I got the idea of expanding during the round function from SSLeay */
|
/* I got the idea of expanding during the round function from SSLeay */
|
||||||
#if defined(LITTLE_ENDIAN)
|
#if FX_ENDIAN == 0
|
||||||
#define blk0(i) \
|
#define blk0(i) \
|
||||||
(block->l[i] = (rol(block->l[i], 24) & 0xFF00FF00) \
|
(block->l[i] = (rol(block->l[i], 24) & 0xFF00FF00) \
|
||||||
| (rol(block->l[i], 8) & 0x00FF00FF))
|
| (rol(block->l[i], 8) & 0x00FF00FF))
|
||||||
#elif defined(BIG_ENDIAN)
|
#elif FX_ENDIAN == 1
|
||||||
#define blk0(i) block->l[i]
|
#define blk0(i) block->l[i]
|
||||||
#else
|
#else
|
||||||
#error "Endianness not defined!"
|
#error "Endianness not defined!"
|
||||||
#endif
|
#endif
|
||||||
#define blk(i) \
|
#define blk(i) \
|
||||||
(block->l[i & 15] \
|
(block->l[i & 15] = rol( \
|
||||||
= rol(block->l[(i + 13) & 15] ^ block->l[(i + 8) & 15] \
|
block->l[(i + 13) & 15] ^ block->l[(i + 8) & 15] \
|
||||||
^ block->l[(i + 2) & 15] ^ block->l[i & 15], \
|
^ block->l[(i + 2) & 15] ^ block->l[i & 15], \
|
||||||
1))
|
1))
|
||||||
|
|
||||||
@@ -235,9 +235,8 @@ static void sha_finish(struct fx_hash_ctx *context, void *out, size_t max)
|
|||||||
unsigned char c;
|
unsigned char c;
|
||||||
|
|
||||||
for (i = 0; i < 8; i++) {
|
for (i = 0; i < 8; i++) {
|
||||||
finalcount[i]
|
finalcount[i] = (unsigned char)((context->ctx_state.sha1.count[(
|
||||||
= (unsigned char)((context->ctx_state.sha1
|
i >= 4 ? 0 : 1)]
|
||||||
.count[(i >= 4 ? 0 : 1)]
|
|
||||||
>> ((3 - (i & 3)) * 8))
|
>> ((3 - (i & 3)) * 8))
|
||||||
& 255); /* Endian independent */
|
& 255); /* Endian independent */
|
||||||
}
|
}
|
||||||
@@ -251,8 +250,8 @@ static void sha_finish(struct fx_hash_ctx *context, void *out, size_t max)
|
|||||||
}
|
}
|
||||||
sha_update(context, finalcount, 8); /* Should cause a SHA1Transform() */
|
sha_update(context, finalcount, 8); /* Should cause a SHA1Transform() */
|
||||||
for (i = 0; i < 20; i++) {
|
for (i = 0; i < 20; i++) {
|
||||||
digest[i]
|
digest[i] = (unsigned char)((context->ctx_state.sha1
|
||||||
= (unsigned char)((context->ctx_state.sha1.state[i >> 2]
|
.state[i >> 2]
|
||||||
>> ((3 - (i & 3)) * 8))
|
>> ((3 - (i & 3)) * 8))
|
||||||
& 255);
|
& 255);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#ifndef FX_CORE_BSTR_H_
|
#ifndef FX_CORE_BSTR_H_
|
||||||
#define FX_CORE_BSTR_H_
|
#define FX_CORE_BSTR_H_
|
||||||
|
|
||||||
|
#include <fx/encoding.h>
|
||||||
#include <fx/misc.h>
|
#include <fx/misc.h>
|
||||||
#include <fx/status.h>
|
#include <fx/status.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
@@ -77,6 +78,9 @@ FX_API fx_status fx_bstr_write_vfmt(
|
|||||||
const char *format,
|
const char *format,
|
||||||
va_list arg);
|
va_list arg);
|
||||||
|
|
||||||
|
FX_API fx_status
|
||||||
|
fx_bstr_write_wstr(fx_bstr *strv, const fx_wchar *str, size_t *nr_written);
|
||||||
|
|
||||||
FX_API char *fx_bstr_rope(const struct fx_rope *rope, size_t *nr_written);
|
FX_API char *fx_bstr_rope(const struct fx_rope *rope, size_t *nr_written);
|
||||||
FX_API char *fx_bstr_fmt(size_t *nr_written, const char *format, ...);
|
FX_API char *fx_bstr_fmt(size_t *nr_written, const char *format, ...);
|
||||||
FX_API char *fx_bstr_vfmt(size_t *nr_written, const char *format, va_list arg);
|
FX_API char *fx_bstr_vfmt(size_t *nr_written, const char *format, va_list arg);
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
#ifndef FX_COMPARABLE_H_
|
||||||
|
#define FX_COMPARABLE_H_
|
||||||
|
|
||||||
|
#include <fx/int-primitives.h>
|
||||||
|
#include <fx/macros.h>
|
||||||
|
#include <fx/value.h>
|
||||||
|
|
||||||
|
FX_DECLS_BEGIN;
|
||||||
|
|
||||||
|
#define FX_TYPE_COMPARABLE (fx_comparable_get_type())
|
||||||
|
|
||||||
|
FX_DECLARE_TYPE(fx_comparable);
|
||||||
|
|
||||||
|
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_comparable)
|
||||||
|
i32 (*c_compare)(const fx_value *a, const fx_value *b);
|
||||||
|
FX_TYPE_CLASS_DECLARATION_END(fx_comparable)
|
||||||
|
|
||||||
|
FX_API fx_type_id fx_comparable_get_type(void);
|
||||||
|
|
||||||
|
FX_DECLS_END;
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
#ifndef FX_CONVERTIBLE_H_
|
||||||
|
#define FX_CONVERTIBLE_H_
|
||||||
|
|
||||||
|
#include <fx/encoding.h>
|
||||||
|
#include <fx/int-primitives.h>
|
||||||
|
#include <fx/macros.h>
|
||||||
|
#include <fx/string.h>
|
||||||
|
#include <fx/value.h>
|
||||||
|
|
||||||
|
FX_DECLS_BEGIN;
|
||||||
|
|
||||||
|
#define FX_TYPE_CONVERTIBLE (fx_convertible_get_type())
|
||||||
|
|
||||||
|
FX_DECLARE_TYPE(fx_convertible);
|
||||||
|
|
||||||
|
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_convertible)
|
||||||
|
fx_status (*c_to_char)(const fx_value *in, fx_wchar *out);
|
||||||
|
|
||||||
|
fx_status (*c_to_bool)(const fx_value *in, bool *out);
|
||||||
|
fx_status (*c_to_i16)(const fx_value *in, i16 *out);
|
||||||
|
fx_status (*c_to_u16)(const fx_value *in, u16 *out);
|
||||||
|
fx_status (*c_to_i32)(const fx_value *in, i32 *out);
|
||||||
|
fx_status (*c_to_u32)(const fx_value *in, u32 *out);
|
||||||
|
fx_status (*c_to_i64)(const fx_value *in, i64 *out);
|
||||||
|
fx_status (*c_to_u64)(const fx_value *in, u64 *out);
|
||||||
|
fx_status (*c_to_iptr)(const fx_value *in, iptr *out);
|
||||||
|
fx_status (*c_to_uptr)(const fx_value *in, uptr *out);
|
||||||
|
|
||||||
|
fx_status (*c_to_sbyte)(const fx_value *in, sbyte *out);
|
||||||
|
fx_status (*c_to_byte)(const fx_value *in, byte *out);
|
||||||
|
fx_status (*c_to_short)(const fx_value *in, short *out);
|
||||||
|
fx_status (*c_to_ushort)(const fx_value *in, unsigned short *out);
|
||||||
|
fx_status (*c_to_int)(const fx_value *in, int *out);
|
||||||
|
fx_status (*c_to_uint)(const fx_value *in, unsigned int *out);
|
||||||
|
fx_status (*c_to_long)(const fx_value *in, long *out);
|
||||||
|
fx_status (*c_to_ulong)(const fx_value *in, unsigned long *out);
|
||||||
|
fx_status (*c_to_longlong)(const fx_value *in, long long *out);
|
||||||
|
fx_status (
|
||||||
|
*c_to_ulonglong)(const fx_value *in, unsigned long long *out);
|
||||||
|
fx_status (*c_to_size)(const fx_value *in, size_t *out);
|
||||||
|
|
||||||
|
fx_status (*c_to_float)(const fx_value *in, float *out);
|
||||||
|
fx_status (*c_to_double)(const fx_value *in, double *out);
|
||||||
|
|
||||||
|
fx_status (*c_to_cstr)(const fx_value *in, const char **out);
|
||||||
|
fx_status (*c_to_wchar)(const fx_value *in, fx_wchar *out);
|
||||||
|
fx_status (*c_to_wstr)(const fx_value *in, const fx_wchar **out);
|
||||||
|
|
||||||
|
fx_status (*c_to_object)(
|
||||||
|
const fx_value *in,
|
||||||
|
fx_value *out,
|
||||||
|
fx_type_id convert_to);
|
||||||
|
FX_TYPE_CLASS_DECLARATION_END(fx_convertible)
|
||||||
|
|
||||||
|
FX_API fx_type_id fx_convertible_get_type(void);
|
||||||
|
|
||||||
|
FX_DECLS_END;
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
#ifndef FX_CSTR_H_
|
||||||
|
#define FX_CSTR_H_
|
||||||
|
|
||||||
|
#include <fx/encoding.h>
|
||||||
|
#include <fx/macros.h>
|
||||||
|
|
||||||
|
FX_DECLS_BEGIN;
|
||||||
|
|
||||||
|
typedef enum fx_strlen_flags {
|
||||||
|
FX_STRLEN_NORMAL = 0,
|
||||||
|
FX_STRLEN_IGNORE_ESC = 0x01u,
|
||||||
|
FX_STRLEN_IGNORE_MOD = 0x02u,
|
||||||
|
FX_STRLEN_CODEPOINTS = 0x04u,
|
||||||
|
} fx_strlen_flags;
|
||||||
|
|
||||||
|
#define FX_TYPE_CSTR (fx_cstr_get_type())
|
||||||
|
|
||||||
|
FX_DECLARE_TYPE(fx_cstr);
|
||||||
|
|
||||||
|
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_cstr)
|
||||||
|
FX_TYPE_CLASS_DECLARATION_END(fx_cstr)
|
||||||
|
|
||||||
|
FX_API fx_type_id fx_cstr_get_type(void);
|
||||||
|
|
||||||
|
FX_API char *fx_strdup(const char *s);
|
||||||
|
FX_API size_t fx_strlen(const char *s, fx_strlen_flags flags);
|
||||||
|
FX_API size_t fx_strcmp_nocase(const char *a, const char *b);
|
||||||
|
|
||||||
|
FX_DECLS_END;
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
#ifndef FX_DOUBLE_H_
|
|
||||||
#define FX_DOUBLE_H_
|
|
||||||
|
|
||||||
#include <fx/macros.h>
|
|
||||||
|
|
||||||
FX_DECLS_BEGIN;
|
|
||||||
|
|
||||||
#define FX_TYPE_DOUBLE (fx_double_get_type())
|
|
||||||
|
|
||||||
FX_DECLARE_TYPE(fx_double);
|
|
||||||
|
|
||||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_double)
|
|
||||||
FX_TYPE_CLASS_DECLARATION_END(fx_double)
|
|
||||||
|
|
||||||
FX_API fx_type_id fx_double_get_type(void);
|
|
||||||
|
|
||||||
FX_API fx_double *fx_double_create(double value);
|
|
||||||
FX_API fx_double *fx_double_create_nan(void);
|
|
||||||
FX_API fx_double *fx_double_create_nan_negative(void);
|
|
||||||
FX_API fx_double *fx_double_create_inf(void);
|
|
||||||
FX_API fx_double *fx_double_create_inf_negative(void);
|
|
||||||
|
|
||||||
FX_API void fx_double_set_value(fx_double *i, double v);
|
|
||||||
FX_API void fx_double_set_value_nan(fx_double *i);
|
|
||||||
FX_API void fx_double_set_value_nan_negative(fx_double *i);
|
|
||||||
FX_API void fx_double_set_value_inf(fx_double *i);
|
|
||||||
FX_API void fx_double_set_value_inf_negative(fx_double *i);
|
|
||||||
|
|
||||||
FX_API bool fx_double_is_nan(const fx_double *i);
|
|
||||||
FX_API bool fx_double_is_nan_positive(const fx_double *i);
|
|
||||||
FX_API bool fx_double_is_nan_negative(const fx_double *i);
|
|
||||||
FX_API bool fx_double_is_inf(const fx_double *i);
|
|
||||||
FX_API bool fx_double_is_inf_positive(const fx_double *i);
|
|
||||||
FX_API bool fx_double_is_inf_negative(const fx_double *i);
|
|
||||||
|
|
||||||
FX_API double fx_double_get_value(const fx_double *i);
|
|
||||||
|
|
||||||
FX_DECLS_END;
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -11,6 +11,7 @@ typedef int32_t fx_wchar;
|
|||||||
|
|
||||||
FX_API bool fx_wchar_is_alpha(fx_wchar c);
|
FX_API bool fx_wchar_is_alpha(fx_wchar c);
|
||||||
FX_API bool fx_wchar_is_number(fx_wchar c);
|
FX_API bool fx_wchar_is_number(fx_wchar c);
|
||||||
|
FX_API bool fx_wchar_is_graph(fx_wchar c);
|
||||||
static inline bool fx_wchar_is_bin_digit(fx_wchar c)
|
static inline bool fx_wchar_is_bin_digit(fx_wchar c)
|
||||||
{
|
{
|
||||||
return c >= '0' && c <= '1';
|
return c >= '0' && c <= '1';
|
||||||
@@ -35,8 +36,7 @@ FX_API fx_wchar fx_wchar_utf8_codepoint_decode(const char *s);
|
|||||||
FX_API unsigned int fx_wchar_utf8_codepoint_encode(fx_wchar c, char s[4]);
|
FX_API unsigned int fx_wchar_utf8_codepoint_encode(fx_wchar c, char s[4]);
|
||||||
FX_API unsigned int fx_wchar_utf8_codepoint_stride(const char *s);
|
FX_API unsigned int fx_wchar_utf8_codepoint_stride(const char *s);
|
||||||
FX_API size_t fx_wchar_utf8_codepoint_count(const char *s, size_t nr_bytes);
|
FX_API size_t fx_wchar_utf8_codepoint_count(const char *s, size_t nr_bytes);
|
||||||
FX_API size_t fx_wchar_utf8_string_encoded_size(
|
FX_API size_t
|
||||||
const fx_wchar *s,
|
fx_wchar_utf8_string_encoded_size(const fx_wchar *s, size_t nr_codepoints);
|
||||||
size_t nr_codepoints);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+2
-18
@@ -4,6 +4,7 @@
|
|||||||
#include <fx/misc.h>
|
#include <fx/misc.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#if 0
|
||||||
typedef struct {
|
typedef struct {
|
||||||
union {
|
union {
|
||||||
unsigned char i_bytes[sizeof(uint16_t)];
|
unsigned char i_bytes[sizeof(uint16_t)];
|
||||||
@@ -27,23 +28,6 @@ typedef struct {
|
|||||||
uint64_t i_uval;
|
uint64_t i_uval;
|
||||||
};
|
};
|
||||||
} fx_i64;
|
} fx_i64;
|
||||||
|
#endif
|
||||||
FX_API fx_i16 fx_i16_htob(uint16_t v);
|
|
||||||
FX_API fx_i16 fx_i16_htos(uint16_t v);
|
|
||||||
|
|
||||||
FX_API uint16_t fx_i16_btoh(fx_i16 v);
|
|
||||||
FX_API uint16_t fx_i16_stoh(fx_i16 v);
|
|
||||||
|
|
||||||
FX_API fx_i32 fx_i32_htob(uint32_t v);
|
|
||||||
FX_API fx_i32 fx_i32_htos(uint32_t v);
|
|
||||||
|
|
||||||
FX_API uint32_t fx_i32_btoh(fx_i32 v);
|
|
||||||
FX_API uint32_t fx_i32_stoh(fx_i32 v);
|
|
||||||
|
|
||||||
FX_API fx_i64 fx_i64_htob(uint64_t v);
|
|
||||||
FX_API fx_i64 fx_i64_htos(uint64_t v);
|
|
||||||
|
|
||||||
FX_API uint64_t fx_i64_btoh(fx_i64 v);
|
|
||||||
FX_API uint64_t fx_i64_stoh(fx_i64 v);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
#ifndef FX_DOUBLE_H_
|
||||||
|
#define FX_DOUBLE_H_
|
||||||
|
|
||||||
|
#include <fx/macros.h>
|
||||||
|
|
||||||
|
FX_DECLS_BEGIN;
|
||||||
|
|
||||||
|
#define FX_TYPE_FLOAT (fx_float_get_type())
|
||||||
|
#define FX_TYPE_DOUBLE (fx_double_get_type())
|
||||||
|
|
||||||
|
FX_DECLARE_TYPE(fx_float);
|
||||||
|
FX_DECLARE_TYPE(fx_double);
|
||||||
|
|
||||||
|
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_float)
|
||||||
|
FX_TYPE_CLASS_DECLARATION_END(fx_float)
|
||||||
|
|
||||||
|
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_double)
|
||||||
|
FX_TYPE_CLASS_DECLARATION_END(fx_double)
|
||||||
|
|
||||||
|
FX_API fx_type_id fx_float_get_type(void);
|
||||||
|
FX_API fx_type_id fx_double_get_type(void);
|
||||||
|
|
||||||
|
FX_DECLS_END;
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
#ifndef FX_CORE_HASH_H_
|
#ifndef FX_CORE_HASH_H_
|
||||||
#define FX_CORE_HASH_H_
|
#define FX_CORE_HASH_H_
|
||||||
|
|
||||||
|
#include <fx/encoding.h>
|
||||||
#include <fx/misc.h>
|
#include <fx/misc.h>
|
||||||
#include <fx/status.h>
|
#include <fx/status.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
@@ -99,15 +100,16 @@ typedef struct fx_hash_ctx {
|
|||||||
} fx_hash_ctx;
|
} fx_hash_ctx;
|
||||||
|
|
||||||
FX_API uint64_t fx_hash_cstr(const char *s);
|
FX_API uint64_t fx_hash_cstr(const char *s);
|
||||||
|
FX_API uint64_t fx_hash_wstr(const fx_wchar *s);
|
||||||
FX_API uint64_t fx_hash_cstr_ex(const char *s, size_t *len);
|
FX_API uint64_t fx_hash_cstr_ex(const char *s, size_t *len);
|
||||||
|
FX_API uint64_t fx_hash_buffer(const void *s, size_t len);
|
||||||
|
|
||||||
FX_API fx_status fx_hash_ctx_init(fx_hash_ctx *ctx, fx_hash_function func);
|
FX_API fx_status fx_hash_ctx_init(fx_hash_ctx *ctx, fx_hash_function func);
|
||||||
FX_API fx_status fx_hash_ctx_reset(fx_hash_ctx *ctx);
|
FX_API fx_status fx_hash_ctx_reset(fx_hash_ctx *ctx);
|
||||||
FX_API fx_status
|
FX_API fx_status
|
||||||
fx_hash_ctx_update(fx_hash_ctx *ctx, const void *p, size_t len);
|
fx_hash_ctx_update(fx_hash_ctx *ctx, const void *p, size_t len);
|
||||||
FX_API fx_status fx_hash_ctx_update_rope(
|
FX_API fx_status
|
||||||
fx_hash_ctx *ctx,
|
fx_hash_ctx_update_rope(fx_hash_ctx *ctx, const struct fx_rope *rope);
|
||||||
const struct fx_rope *rope);
|
|
||||||
FX_API fx_status
|
FX_API fx_status
|
||||||
fx_hash_ctx_finish(fx_hash_ctx *ctx, void *out_digest, size_t out_max);
|
fx_hash_ctx_finish(fx_hash_ctx *ctx, void *out_digest, size_t out_max);
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
#ifndef FX_INT_PRIMITIVES_H_
|
||||||
|
#define FX_INT_PRIMITIVES_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
typedef int8_t sbyte;
|
||||||
|
typedef uint8_t byte;
|
||||||
|
typedef int16_t i16;
|
||||||
|
typedef uint16_t u16;
|
||||||
|
typedef int32_t i32;
|
||||||
|
typedef uint32_t u32;
|
||||||
|
typedef int64_t i64;
|
||||||
|
typedef uint64_t u64;
|
||||||
|
typedef intptr_t iptr;
|
||||||
|
typedef uintptr_t uptr;
|
||||||
|
|
||||||
|
#endif
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user