diff --git a/CMakeLists.txt b/CMakeLists.txt index 943cf43..ce96caf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(bluelib C) +project(fx C) include (TestBigEndian) @@ -9,31 +9,31 @@ set(CMAKE_C_EXTENSIONS OFF) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake) set_property(GLOBAL PROPERTY USE_FOLDERS ON) -set(b_modules core ds serial term cmd io compress) +set(fx_modules core ds serial term cmd io compress) -set(b_system_name ${CMAKE_SYSTEM_NAME}) -string(TOLOWER ${b_system_name} b_system_name) +set(fx_system_name ${CMAKE_SYSTEM_NAME}) +string(TOLOWER ${fx_system_name} fx_system_name) -message(STATUS "System name: ${b_system_name}") +message(STATUS "System name: ${fx_system_name}") set(CMAKE_ARCHIVE_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) -foreach (module ${b_modules}) +foreach (module ${fx_modules}) add_subdirectory(${module}) if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/test/${module}) message(STATUS "Building unit tests for module ${module}") if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/test/${module}/${module}-units.c) - add_executable(blue-${module}-units + add_executable(fx-${module}-units test/${module}/${module}-units.c misc/AllTests.c misc/CuTest.c misc/CuTest.h) - target_link_libraries(blue-${module}-units blue-${module}) - target_include_directories(blue-${module}-units PRIVATE misc/) - set_target_properties(blue-${module}-units PROPERTIES FOLDER "Tests/${module}") + target_link_libraries(fx-${module}-units fx-${module}) + target_include_directories(fx-${module}-units PRIVATE misc/) + set_target_properties(fx-${module}-units PROPERTIES FOLDER "Tests/${module}") endif () @@ -42,11 +42,11 @@ foreach (module ${b_modules}) foreach (test_file ${test_sources}) get_filename_component(test_name ${test_file} NAME_WE) - add_executable(blue-${module}-${test_name} ${test_file}) + add_executable(fx-${module}-${test_name} ${test_file}) - set_target_properties(blue-${module}-${test_name} PROPERTIES FOLDER "Tests/${module}") + set_target_properties(fx-${module}-${test_name} PROPERTIES FOLDER "Tests/${module}") - target_link_libraries(blue-${module}-${test_name} blue-${module}) + target_link_libraries(fx-${module}-${test_name} fx-${module}) endforeach (test_file) endif () endforeach (module) @@ -56,11 +56,11 @@ list(REMOVE_ITEM test_sources "${CMAKE_CURRENT_SOURCE_DIR}/test/units.c") foreach (test_file ${test_sources}) get_filename_component(test_name ${test_file} NAME_WE) - add_executable(blue-${test_name} ${test_file}) + add_executable(fx-${test_name} ${test_file}) - set_target_properties(blue-${test_name} PROPERTIES FOLDER "Tests") + set_target_properties(fx-${test_name} PROPERTIES FOLDER "Tests") - foreach (module ${b_modules}) - target_link_libraries(blue-${test_name} blue-${module}) + foreach (module ${fx_modules}) + target_link_libraries(fx-${test_name} fx-${module}) endforeach (module) endforeach (test_file) diff --git a/cmake/FindBluelib.cmake b/cmake/FindBluelib.cmake deleted file mode 100644 index 648ceaa..0000000 --- a/cmake/FindBluelib.cmake +++ /dev/null @@ -1,189 +0,0 @@ -#[=======================================================================[.rst: -FindBluelib ------------- - -Find the Bluelib library and header directories - -Imported Targets -^^^^^^^^^^^^^^^^ - -This module defines the following :prop_tgt:`IMPORTED` target: - -``Bluelib::Bluelib`` -The Bluelib library, if found - -Result Variables -^^^^^^^^^^^^^^^^ - -This module will set the following variables in your project: - -``Bluelib_FOUND`` -true if the Bluelib C headers and libraries were found -``Bluelib_INCLUDE_DIR`` -directories containing the Bluelib C headers. - -``Bluelib_LIBRARY`` -the C library to link against - -Hints -^^^^^ - -The user may set the environment variable ``Bluelib_PREFIX`` to the root -directory of a Bluelib library installation. -#]=======================================================================] - -set (Bluelib_SEARCH_PATHS - ~/Library/Frameworks - /Library/Frameworks - /usr/local - /usr/local/share - /usr - /sw # Fink - /opt/local # DarwinPorts - /opt/csw # Blastwave - /opt - ${Bluelib_PREFIX} - $ENV{Bluelib_PREFIX}) - -if (Bluelib_STATIC) - set(_lib_suffix "-s") -endif () - -set(supported_components Core Ds Term Cmd Io Serial Compress) -set(components ${Bluelib_FIND_COMPONENTS}) -string(REPLACE ";" ", " supported_components_string_list "${supported_components}") - -if (NOT components) - set(components ${supported_components}) -endif () - -set(required_vars) - -foreach (component ${components}) - if (NOT "${component}" IN_LIST supported_components) - message(FATAL_ERROR "'${component}' is not a valid Bluelib module.\nSupported modules: ${supported_components_string_list}") - endif () - - string(TOLOWER ${component} header_name) - set(lib_name ${header_name}${_lib_suffix}) - - if (NOT Bluelib_${component}_INCLUDE_DIR) - find_path(Bluelib_${component}_INCLUDE_DIR - NAMES blue/${header_name}.h ${Bluelib_FIND_ARGS} - PATH_SUFFIXES include - PATHS ${Bluelib_SEARCH_PATHS}) - endif () - - if (NOT Bluelib_${component}_LIBRARY) - find_library(Bluelib_${component}_LIBRARY - NAMES blue-${lib_name} ${Bluelib_FIND_ARGS} - PATH_SUFFIXES lib - PATHS ${Bluelib_SEARCH_PATHS}) - else () - # on Windows, ensure paths are in canonical format (forward slahes): - file(TO_CMAKE_PATH "${Bluelib_${component}_LIBRARY}" Bluelib_${component}_LIBRARY) - endif() - - list(APPEND required_vars Bluelib_${component}_INCLUDE_DIR Bluelib_${component}_LIBRARY) -endforeach (component) - -unset(Bluelib_FIND_ARGS) - -include(FindPackageHandleStandardArgs) - -find_package_handle_standard_args(Bluelib - REQUIRED_VARS ${required_vars}) - -if (Bluelib_FOUND) - set(created_targets) - foreach (component ${components}) - string(TOLOWER ${component} header_name) - set(lib_name ${header_name}${_lib_suffix}) - - if(NOT TARGET Bluelib::${component}) - add_library(Bluelib::${component} UNKNOWN IMPORTED) - set_target_properties(Bluelib::${component} PROPERTIES - INTERFACE_INCLUDE_DIRECTORIES "${Bluelib_${component}_INCLUDE_DIR}") - target_compile_definitions(Bluelib::${component} INTERFACE _CRT_SECURE_NO_WARNINGS=1) - - if (Bluelib_STATIC) - target_compile_definitions(Bluelib::${component} INTERFACE BLUELIB_STATIC=1) - endif () - - set_target_properties(Bluelib::${component} PROPERTIES - IMPORTED_LINK_INTERFACE_LANGUAGES "C" - IMPORTED_LOCATION "${Bluelib_${component}_LIBRARY}") - set(created_targets ${created_targets} ${component}) - endif () - endforeach (component) - - foreach (component ${created_targets}) - if ("${component}" STREQUAL "Ds") - if (NOT TARGET Bluelib::Core) - message(FATAL_ERROR "Bluelib: Module 'Ds' depends on 'Core', which was not specified in find_package()") - endif () - - target_link_libraries(Bluelib::Ds INTERFACE Bluelib::Core) - endif () - - if ("${component}" STREQUAL "Term") - if (NOT TARGET Bluelib::Core) - message(FATAL_ERROR "Bluelib: Module 'Term' depends on 'Core', which was not specified in find_package()") - endif () - - if (NOT TARGET Bluelib::Ds) - message(FATAL_ERROR "Bluelib: Module 'Term' depends on 'Ds', which was not specified in find_package()") - endif () - - target_link_libraries(Bluelib::Term INTERFACE Bluelib::Core Bluelib::Ds) - endif () - - if ("${component}" STREQUAL "Serial") - if (NOT TARGET Bluelib::Core) - message(FATAL_ERROR "Bluelib: Module 'Serial' depends on 'Core', which was not specified in find_package()") - endif () - - if (NOT TARGET Bluelib::Ds) - message(FATAL_ERROR "Bluelib: Module 'Serial' depends on 'Ds', which was not specified in find_package()") - endif () - - target_link_libraries(Bluelib::Serial INTERFACE Bluelib::Core Bluelib::Ds) - endif () - - if ("${component}" STREQUAL "Cmd") - if (NOT TARGET Bluelib::Core) - message(FATAL_ERROR "Bluelib: Module 'Cmd' depends on 'Core', which was not specified in find_package()") - endif () - - if (NOT TARGET Bluelib::Ds) - message(FATAL_ERROR "Bluelib: Module 'Cmd' depends on 'Ds', which was not specified in find_package()") - endif () - - if (NOT TARGET Bluelib::Term) - message(FATAL_ERROR "Bluelib: Module 'Cmd' depends on 'Term', which was not specified in find_package()") - endif () - - target_link_libraries(Bluelib::Cmd INTERFACE Bluelib::Core Bluelib::Ds Bluelib::Term) - endif () - - if ("${component}" STREQUAL "Io") - if (NOT TARGET Bluelib::Core) - message(FATAL_ERROR "Bluelib: Module 'Io' depends on 'Core', which was not specified in find_package()") - endif () - - if (NOT TARGET Bluelib::Ds) - message(FATAL_ERROR "Bluelib: Module 'Io' depends on 'Ds', which was not specified in find_package()") - endif () - - target_link_libraries(Bluelib::Io INTERFACE Bluelib::Core Bluelib::Ds) - endif () - - if ("${component}" STREQUAL "Compress") - if (NOT TARGET Bluelib::Core) - message(FATAL_ERROR "Bluelib: Module 'Compress' depends on 'Core', which was not specified in find_package()") - endif () - - target_link_libraries(Bluelib::Compress INTERFACE Bluelib::Core Bluelib::Ds) - endif () - endforeach (component) -endif() diff --git a/cmake/FindFX.cmake b/cmake/FindFX.cmake new file mode 100644 index 0000000..735ae13 --- /dev/null +++ b/cmake/FindFX.cmake @@ -0,0 +1,189 @@ +#[=======================================================================[.rst: +FindFX +------------ + +Find the FX library and header directories + +Imported Targets +^^^^^^^^^^^^^^^^ + +This module defines the following :prop_tgt:`IMPORTED` target: + +``FX::FX`` +The FX library, if found + +Result Variables +^^^^^^^^^^^^^^^^ + +This module will set the following variables in your project: + +``FX_FOUND`` +true if the FX C headers and libraries were found +``FX_INCLUDE_DIR`` +directories containing the FX C headers. + +``FX_LIBRARY`` +the C library to link against + +Hints +^^^^^ + +The user may set the environment variable ``FX_PREFIX`` to the root +directory of a FX library installation. +#]=======================================================================] + +set (FX_SEARCH_PATHS + ~/Library/Frameworks + /Library/Frameworks + /usr/local + /usr/local/share + /usr + /sw # Fink + /opt/local # DarwinPorts + /opt/csw # Blastwave + /opt + ${FX_PREFIX} + $ENV{FX_PREFIX}) + +if (FX_STATIC) + set(_lib_suffix "-s") +endif () + +set(supported_components Core Ds Term Cmd Io Serial Compress) +set(components ${FX_FIND_COMPONENTS}) +string(REPLACE ";" ", " supported_components_string_list "${supported_components}") + +if (NOT components) + set(components ${supported_components}) +endif () + +set(required_vars) + +foreach (component ${components}) + if (NOT "${component}" IN_LIST supported_components) + message(FATAL_ERROR "'${component}' is not a valid FX module.\nSupported modules: ${supported_components_string_list}") + endif () + + 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 + PATHS ${FX_SEARCH_PATHS}) + endif () + + if (NOT FX_${component}_LIBRARY) + find_library(FX_${component}_LIBRARY + NAMES fx-${lib_name} ${FX_FIND_ARGS} + PATH_SUFFIXES lib + PATHS ${FX_SEARCH_PATHS}) + else () + # on Windows, ensure paths are in canonical format (forward slahes): + file(TO_CMAKE_PATH "${FX_${component}_LIBRARY}" FX_${component}_LIBRARY) + endif() + + list(APPEND required_vars FX_${component}_INCLUDE_DIR FX_${component}_LIBRARY) +endforeach (component) + +unset(FX_FIND_ARGS) + +include(FindPackageHandleStandardArgs) + +find_package_handle_standard_args(FX + REQUIRED_VARS ${required_vars}) + +if (FX_FOUND) + set(created_targets) + foreach (component ${components}) + string(TOLOWER ${component} header_name) + set(lib_name ${header_name}${_lib_suffix}) + + if(NOT TARGET FX::${component}) + add_library(FX::${component} UNKNOWN IMPORTED) + set_target_properties(FX::${component} PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${FX_${component}_INCLUDE_DIR}") + target_compile_definitions(FX::${component} INTERFACE _CRT_SECURE_NO_WARNINGS=1) + + if (FX_STATIC) + target_compile_definitions(FX::${component} INTERFACE FX_STATIC=1) + endif () + + set_target_properties(FX::${component} PROPERTIES + IMPORTED_LINK_INTERFACE_LANGUAGES "C" + IMPORTED_LOCATION "${FX_${component}_LIBRARY}") + set(created_targets ${created_targets} ${component}) + endif () + endforeach (component) + + foreach (component ${created_targets}) + if ("${component}" STREQUAL "Ds") + if (NOT TARGET FX::Core) + message(FATAL_ERROR "FX: Module 'Ds' depends on 'Core', which was not specified in find_package()") + endif () + + target_link_libraries(FX::Ds INTERFACE FX::Core) + endif () + + if ("${component}" STREQUAL "Term") + if (NOT TARGET FX::Core) + message(FATAL_ERROR "FX: Module 'Term' depends on 'Core', which was not specified in find_package()") + endif () + + if (NOT TARGET FX::Ds) + message(FATAL_ERROR "FX: Module 'Term' depends on 'Ds', which was not specified in find_package()") + endif () + + target_link_libraries(FX::Term INTERFACE FX::Core FX::Ds) + endif () + + if ("${component}" STREQUAL "Serial") + if (NOT TARGET FX::Core) + message(FATAL_ERROR "FX: Module 'Serial' depends on 'Core', which was not specified in find_package()") + endif () + + if (NOT TARGET FX::Ds) + message(FATAL_ERROR "FX: Module 'Serial' depends on 'Ds', which was not specified in find_package()") + endif () + + target_link_libraries(FX::Serial INTERFACE FX::Core FX::Ds) + endif () + + if ("${component}" STREQUAL "Cmd") + if (NOT TARGET FX::Core) + message(FATAL_ERROR "FX: Module 'Cmd' depends on 'Core', which was not specified in find_package()") + endif () + + if (NOT TARGET FX::Ds) + message(FATAL_ERROR "FX: Module 'Cmd' depends on 'Ds', which was not specified in find_package()") + endif () + + if (NOT TARGET FX::Term) + message(FATAL_ERROR "FX: Module 'Cmd' depends on 'Term', which was not specified in find_package()") + endif () + + target_link_libraries(FX::Cmd INTERFACE FX::Core FX::Ds FX::Term) + endif () + + if ("${component}" STREQUAL "Io") + if (NOT TARGET FX::Core) + message(FATAL_ERROR "FX: Module 'Io' depends on 'Core', which was not specified in find_package()") + endif () + + if (NOT TARGET FX::Ds) + message(FATAL_ERROR "FX: Module 'Io' depends on 'Ds', which was not specified in find_package()") + endif () + + target_link_libraries(FX::Io INTERFACE FX::Core FX::Ds) + endif () + + if ("${component}" STREQUAL "Compress") + if (NOT TARGET FX::Core) + message(FATAL_ERROR "FX: Module 'Compress' depends on 'Core', which was not specified in find_package()") + endif () + + target_link_libraries(FX::Compress INTERFACE FX::Core FX::Ds) + endif () + endforeach (component) +endif() diff --git a/cmake/Templates.cmake b/cmake/Templates.cmake index f450796..6aa0b4b 100644 --- a/cmake/Templates.cmake +++ b/cmake/Templates.cmake @@ -1,4 +1,4 @@ -function(add_bluelib_module) +function(add_fx_module) set(options) set(one_value_args NAME) set(multi_value_args @@ -22,86 +22,86 @@ function(add_bluelib_module) set(sources ${sources} ${dir_sources}) endforeach (dir) - file(GLOB sys_sources sys/${b_system_name}/*.c sys/${b_system_name}/*.h) - set(root_header include/blue/${module_name}.h) - file(GLOB headers include/blue/${module_name}/*.h) + file(GLOB sys_sources sys/${fx_system_name}/*.c sys/${fx_system_name}/*.h) + set(root_header include/fx/${module_name}.h) + file(GLOB headers include/fx/${module_name}/*.h) string(REPLACE "-" "_" module_preproc_token ${module_name}) string(TOUPPER ${module_preproc_token} module_preproc_token) - set(module_preproc_token BLUELIB_${module_preproc_token}) + set(module_preproc_token FX_${module_preproc_token}) message(STATUS "Building module ${module_name} (shared)") - add_library(blue-${module_name} SHARED + add_library(fx-${module_name} SHARED ${sources} ${sys_sources} ${root_header} ${headers} ${arg_EXTRA_SOURCES}) message(STATUS "Building module ${module_name} (static)") - add_library(blue-${module_name}-s STATIC + add_library(fx-${module_name}-s STATIC ${sources} ${sys_sources} ${root_header} ${headers} ${arg_EXTRA_SOURCES}) - target_include_directories(blue-${module_name} PUBLIC include/) - target_include_directories(blue-${module_name}-s PUBLIC include/) + target_include_directories(fx-${module_name} PUBLIC include/) + target_include_directories(fx-${module_name}-s PUBLIC include/) - target_compile_definitions(blue-${module_name} PUBLIC + target_compile_definitions(fx-${module_name} PUBLIC ${module_preproc_token} - BLUELIB_EXPORT=1) - target_compile_definitions(blue-${module_name}-s PUBLIC + FX_EXPORT=1) + target_compile_definitions(fx-${module_name}-s PUBLIC ${module_preproc_token} - BLUELIB_EXPORT=1 - BLUELIB_STATIC=1) + FX_EXPORT=1 + FX_STATIC=1) - set_target_properties(blue-${module_name} + set_target_properties(fx-${module_name} PROPERTIES POSITION_INDEPENDENT_CODE ON) foreach (dep ${arg_DEPENDENCIES}) - target_link_libraries(blue-${module_name} blue-${dep}) - target_link_libraries(blue-${module_name}-s blue-${dep}-s) + target_link_libraries(fx-${module_name} fx-${dep}) + target_link_libraries(fx-${module_name}-s fx-${dep}-s) endforeach (dep) foreach (lib ${arg_LIBS}) - target_link_libraries(blue-${module_name} ${lib}) - target_link_libraries(blue-${module_name}-s ${lib}) + target_link_libraries(fx-${module_name} ${lib}) + target_link_libraries(fx-${module_name}-s ${lib}) endforeach (lib) foreach (dir ${arg_INCLUDE_DIRS}) - target_include_directories(blue-${module_name} PRIVATE + target_include_directories(fx-${module_name} PRIVATE ${dir}) - target_include_directories(blue-${module_name}-s PRIVATE + target_include_directories(fx-${module_name}-s PRIVATE ${dir}) endforeach (dir) foreach (def ${arg_DEFINES}) - target_compile_definitions(blue-${module_name} PRIVATE + target_compile_definitions(fx-${module_name} PRIVATE ${def}) - target_compile_definitions(blue-${module_name}-s PRIVATE + target_compile_definitions(fx-${module_name}-s PRIVATE ${def}) endforeach (def) - set_target_properties(blue-${module_name} PROPERTIES + set_target_properties(fx-${module_name} PROPERTIES FOLDER "Shared/${module_name}") - set_target_properties(blue-${module_name}-s PROPERTIES + set_target_properties(fx-${module_name}-s PROPERTIES FOLDER "Static/${module_name}") TEST_BIG_ENDIAN(IS_BIG_ENDIAN) if(IS_BIG_ENDIAN) - target_compile_definitions(blue-${module_name} PRIVATE + target_compile_definitions(fx-${module_name} PRIVATE BIG_ENDIAN) - target_compile_definitions(blue-${module_name}-s PRIVATE + target_compile_definitions(fx-${module_name}-s PRIVATE BIG_ENDIAN) else() - target_compile_definitions(blue-${module_name} PRIVATE + target_compile_definitions(fx-${module_name} PRIVATE LITTLE_ENDIAN) - target_compile_definitions(blue-${module_name}-s PRIVATE + target_compile_definitions(fx-${module_name}-s PRIVATE LITTLE_ENDIAN) endif() - install(TARGETS blue-${module_name} blue-${module_name}-s) - install(FILES ${root_header} DESTINATION include/blue) - install(FILES ${headers} DESTINATION include/blue/${module_name}) -endfunction(add_bluelib_module) + install(TARGETS fx-${module_name} fx-${module_name}-s) + install(FILES ${root_header} DESTINATION include/fx) + install(FILES ${headers} DESTINATION include/fx/${module_name}) +endfunction(add_fx_module) diff --git a/cmd/CMakeLists.txt b/cmd/CMakeLists.txt index 41fc4c0..8e61f07 100644 --- a/cmd/CMakeLists.txt +++ b/cmd/CMakeLists.txt @@ -1,3 +1,3 @@ include(../cmake/Templates.cmake) -add_bluelib_module(NAME cmd DEPENDENCIES core ds term) +add_fx_module(NAME cmd DEPENDENCIES core ds term) diff --git a/cmd/arg.c b/cmd/arg.c index c68f269..30e6b42 100644 --- a/cmd/arg.c +++ b/cmd/arg.c @@ -1,13 +1,13 @@ #include "command.h" -#include -#include +#include +#include #include #include -struct b_command_arg *b_command_arg_create(void) +struct fx_command_arg *fx_command_arg_create(void) { - struct b_command_arg *out = malloc(sizeof *out); + struct fx_command_arg *out = malloc(sizeof *out); if (!out) { return out; } @@ -16,7 +16,7 @@ struct b_command_arg *b_command_arg_create(void) return out; } -void b_command_arg_destroy(struct b_command_arg *arg) +void fx_command_arg_destroy(struct fx_command_arg *arg) { if (arg->arg_name) { free(arg->arg_name); @@ -37,11 +37,11 @@ void b_command_arg_destroy(struct b_command_arg *arg) free(arg); } -b_status b_command_arg_set_name(struct b_command_arg *arg, const char *name) +fx_status fx_command_arg_set_name(struct fx_command_arg *arg, const char *name) { - char *n = b_strdup(name); + char *n = fx_strdup(name); if (!n) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } if (arg->arg_name) { @@ -50,15 +50,15 @@ b_status b_command_arg_set_name(struct b_command_arg *arg, const char *name) } arg->arg_name = n; - return B_SUCCESS; + return FX_SUCCESS; } -b_status b_command_arg_set_description( - struct b_command_arg *arg, const char *description) +fx_status fx_command_arg_set_description( + struct fx_command_arg *arg, const char *description) { - char *desc = b_strdup(description); + char *desc = fx_strdup(description); if (!desc) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } if (arg->arg_description) { @@ -67,18 +67,18 @@ b_status b_command_arg_set_description( } arg->arg_description = desc; - return B_SUCCESS; + return FX_SUCCESS; } -b_status b_command_arg_set_nr_values( - struct b_command_arg *arg, enum b_command_arg_value_count nr_values) +fx_status fx_command_arg_set_nr_values( + struct fx_command_arg *arg, enum fx_command_arg_value_count nr_values) { arg->arg_nr_values = nr_values; - return B_SUCCESS; + return FX_SUCCESS; } -b_status b_command_arg_set_allowed_values( - struct b_command_arg *arg, const char **allowed_values) +fx_status fx_command_arg_set_allowed_values( + struct fx_command_arg *arg, const char **allowed_values) { size_t count; for (count = 0; allowed_values[count]; count++) @@ -86,35 +86,35 @@ b_status b_command_arg_set_allowed_values( char **values = calloc(count + 1, sizeof *values); if (!values) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } for (size_t i = 0; i < count; i++) { - values[i] = b_strdup(allowed_values[i]); + values[i] = fx_strdup(allowed_values[i]); if (!values[i]) { /* TODO also free strings in `values` */ free(values); - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } } arg->arg_allowed_values = values; - return B_SUCCESS; + return FX_SUCCESS; } -void z__b_get_arg_usage_string(struct b_command_arg *arg, bool colour, b_string *out) +void z__fx_get_arg_usage_string(struct fx_command_arg *arg, bool colour, fx_string *out) { bool optional = false, multi = false; switch (arg->arg_nr_values) { - case B_ARG_0_OR_1_VALUES: + case FX_ARG_0_OR_1_VALUES: optional = true; multi = false; break; - case B_ARG_0_OR_MORE_VALUES: + case FX_ARG_0_OR_MORE_VALUES: optional = true; multi = true; break; - case B_ARG_1_OR_MORE_VALUES: + case FX_ARG_1_OR_MORE_VALUES: optional = false; multi = true; break; @@ -125,30 +125,30 @@ void z__b_get_arg_usage_string(struct b_command_arg *arg, bool colour, b_string } if (optional) { - b_string_append_cstrf( + fx_string_append_cstrf( out, colour ? F_GREEN "[[%s]" : "[[%s]", arg->arg_name); } else { - b_string_append_cstrf( + fx_string_append_cstrf( out, colour ? F_GREEN "<%s>" : "<%s>", arg->arg_name); } for (int i = 1; i < arg->arg_nr_values; i++) { - b_string_append_cstrf(out, " <%s>", arg->arg_name); + fx_string_append_cstrf(out, " <%s>", arg->arg_name); } if (multi) { - b_string_append_cstr(out, "..."); + fx_string_append_cstr(out, "..."); } if (colour) { - b_string_append_cstr(out, F_RESET); + fx_string_append_cstr(out, F_RESET); } } -void z__b_get_arg_description(struct b_command_arg *arg, b_string *out) +void z__fx_get_arg_description(struct fx_command_arg *arg, fx_string *out) { if (arg->arg_description) { - b_string_append_cstr(out, arg->arg_description); + fx_string_append_cstr(out, arg->arg_description); } if (!arg->arg_allowed_values) { @@ -156,19 +156,19 @@ void z__b_get_arg_description(struct b_command_arg *arg, b_string *out) } if (arg->arg_description) { - b_string_append_cstr(out, " "); + fx_string_append_cstr(out, " "); } - b_string_append_cstr(out, "[[values:"); + fx_string_append_cstr(out, "[[values:"); for (size_t i = 0; arg->arg_allowed_values[i]; i++) { if (i > 0) { - b_string_append_cstr(out, ","); + fx_string_append_cstr(out, ","); } - b_string_append_cstrf( + fx_string_append_cstrf( out, " " F_GREEN "%s" F_RESET, arg->arg_allowed_values[i]); } - b_string_append_cstr(out, "]"); + fx_string_append_cstr(out, "]"); } diff --git a/cmd/arglist.c b/cmd/arglist.c index 12c889f..7cb2ff4 100644 --- a/cmd/arglist.c +++ b/cmd/arglist.c @@ -1,30 +1,30 @@ -#include "blue/core/misc.h" #include "command.h" -#include -#include -#include -#include +#include +#include +#include +#include +#include #include #include #include #include -B_BTREE_DEFINE_SIMPLE_INSERT( - struct b_arglist_option, opt_node, opt_id, put_arglist_option) -B_BTREE_DEFINE_SIMPLE_GET( - struct b_arglist_option, unsigned int, opt_node, opt_id, get_arglist_option) +FX_BTREE_DEFINE_SIMPLE_INSERT( + struct fx_arglist_option, opt_node, opt_id, put_arglist_option) +FX_BTREE_DEFINE_SIMPLE_GET( + struct fx_arglist_option, unsigned int, opt_node, opt_id, get_arglist_option) -B_BTREE_DEFINE_SIMPLE_INSERT( - struct b_arglist_value, val_node, val_id, put_arglist_value) -B_BTREE_DEFINE_SIMPLE_GET( - struct b_arglist_value, unsigned int, val_node, val_id, get_arglist_value) +FX_BTREE_DEFINE_SIMPLE_INSERT( + struct fx_arglist_value, val_node, val_id, put_arglist_value) +FX_BTREE_DEFINE_SIMPLE_GET( + struct fx_arglist_value, unsigned int, val_node, val_id, get_arglist_value) struct argv_parser { - struct b_command *cmd; - struct b_arglist *arglist; + struct fx_command *cmd; + struct fx_arglist *arglist; - b_queue_entry *arg_it; + fx_queue_entry *arg_it; int nr_values_cur_arg; int argc; @@ -51,9 +51,9 @@ static const char *advance(struct argv_parser *parser) return parser->argv[parser->index++]; } -struct b_arglist *b_arglist_create(void) +struct fx_arglist *fx_arglist_create(void) { - struct b_arglist *out = malloc(sizeof *out); + struct fx_arglist *out = malloc(sizeof *out); if (!out) { return NULL; } @@ -62,19 +62,19 @@ struct b_arglist *b_arglist_create(void) return out; } -static void move_to_subcommand(struct argv_parser *parser, struct b_command *cmd) +static void move_to_subcommand(struct argv_parser *parser, struct fx_command *cmd) { parser->cmd = cmd; parser->arglist->list_command = cmd; - parser->arg_it = b_queue_first(&cmd->b_arg); + parser->arg_it = fx_queue_first(&cmd->c_arg); parser->nr_values_cur_arg = 0; } -static b_status set_opt(struct b_arglist *args, struct b_command_option *opt) +static fx_status set_opt(struct fx_arglist *args, struct fx_command_option *opt) { - struct b_arglist_option *arg_opt = malloc(sizeof *arg_opt); + struct fx_arglist_option *arg_opt = malloc(sizeof *arg_opt); if (!arg_opt) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } memset(arg_opt, 0x0, sizeof *arg_opt); @@ -82,24 +82,24 @@ static b_status set_opt(struct b_arglist *args, struct b_command_option *opt) arg_opt->opt_id = opt->opt_id; put_arglist_option(&args->list_options, arg_opt); - return B_SUCCESS; + return FX_SUCCESS; } -static b_status put_arg( - struct b_arglist *args, struct b_command_arg *arg, const char *value) +static fx_status put_arg( + struct fx_arglist *args, struct fx_command_arg *arg, const char *value) { - struct b_arglist_option *arglist_opt - = get_arglist_option(&args->list_options, B_COMMAND_INVALID_ID); + struct fx_arglist_option *arglist_opt + = get_arglist_option(&args->list_options, FX_COMMAND_INVALID_ID); if (!arglist_opt) { arglist_opt = malloc(sizeof *arglist_opt); if (!arglist_opt) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } memset(arglist_opt, 0x0, sizeof *arglist_opt); - arglist_opt->opt_id = B_COMMAND_INVALID_ID; + arglist_opt->opt_id = FX_COMMAND_INVALID_ID; put_arglist_option(&args->list_options, arglist_opt); } @@ -114,28 +114,28 @@ static b_status put_arg( } if (!value_ok) { - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } } - struct b_arglist_value *val = malloc(sizeof *val); + struct fx_arglist_value *val = malloc(sizeof *val); if (!val) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } memset(val, 0x0, sizeof *val); val->val_id = arg->arg_id; - val->val_type = B_COMMAND_ARG_STRING; - val->val_str = b_strdup(value); + val->val_type = FX_COMMAND_ARG_STRING; + val->val_str = fx_strdup(value); put_arglist_value(&arglist_opt->opt_values, val); - return B_SUCCESS; + return FX_SUCCESS; } -static b_status put_opt_arg( - struct b_arglist_option *arglist_opt, struct b_command_option *opt, - struct b_command_arg *arg, const char *value) +static fx_status put_opt_arg( + struct fx_arglist_option *arglist_opt, struct fx_command_option *opt, + struct fx_command_arg *arg, const char *value) { if (arg->arg_allowed_values) { @@ -149,29 +149,29 @@ static b_status put_opt_arg( } if (!value_ok) { - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } } - struct b_arglist_value *val = malloc(sizeof *val); + struct fx_arglist_value *val = malloc(sizeof *val); if (!val) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } memset(val, 0x0, sizeof *val); val->val_id = arg->arg_id; - val->val_type = B_COMMAND_ARG_STRING; - val->val_str = b_strdup(value); + val->val_type = FX_COMMAND_ARG_STRING; + val->val_str = fx_strdup(value); put_arglist_value(&arglist_opt->opt_values, val); - return B_SUCCESS; + return FX_SUCCESS; } #if 0 static void report_missing_args( - struct argv_parser *parser, struct b_command_option *opt, - struct b_command_arg *arg, int args_supplied) + struct argv_parser *parser, struct fx_command_option *opt, + struct fx_command_arg *arg, int args_supplied) { } @@ -180,44 +180,44 @@ static void report_unexpected_arg(struct argv_parser *parser, const char *value) } #endif -static b_status check_required_args(struct argv_parser *parser) +static fx_status check_required_args(struct argv_parser *parser) { if (!parser->arg_it) { - return B_SUCCESS; + return FX_SUCCESS; } - struct b_command_arg *arg - = b_unbox(struct b_command_arg, parser->arg_it, arg_entry); + struct fx_command_arg *arg + = fx_unbox(struct fx_command_arg, parser->arg_it, arg_entry); if (!arg) { - return B_SUCCESS; + return FX_SUCCESS; } - if (arg->arg_nr_values == B_ARG_0_OR_MORE_VALUES) { - return B_SUCCESS; + if (arg->arg_nr_values == FX_ARG_0_OR_MORE_VALUES) { + return FX_SUCCESS; } - if (arg->arg_nr_values == B_ARG_1_OR_MORE_VALUES) { + if (arg->arg_nr_values == FX_ARG_1_OR_MORE_VALUES) { if (parser->nr_values_cur_arg > 0) { - return B_SUCCESS; + return FX_SUCCESS; } - b_arglist_report_missing_args( - parser->arglist, B_COMMAND_INVALID_ID, arg->arg_id, + fx_arglist_report_missing_args( + parser->arglist, FX_COMMAND_INVALID_ID, arg->arg_id, parser->nr_values_cur_arg); - return B_ERR_BAD_FORMAT; + return FX_ERR_BAD_FORMAT; } if (parser->nr_values_cur_arg != arg->arg_nr_values) { - b_arglist_report_missing_args( - parser->arglist, B_COMMAND_INVALID_ID, arg->arg_id, + fx_arglist_report_missing_args( + parser->arglist, FX_COMMAND_INVALID_ID, arg->arg_id, parser->nr_values_cur_arg); - return B_ERR_BAD_FORMAT; + return FX_ERR_BAD_FORMAT; } - return B_SUCCESS; + return FX_SUCCESS; } -static b_status parse_arg(struct argv_parser *parser) +static fx_status parse_arg(struct argv_parser *parser) { while (1) { const char *value = peek(parser); @@ -225,8 +225,8 @@ static b_status parse_arg(struct argv_parser *parser) break; } - struct b_command *subcmd - = b_command_get_subcommand_with_name(parser->cmd, value); + struct fx_command *subcmd + = fx_command_get_subcommand_with_name(parser->cmd, value); if (subcmd) { move_to_subcommand(parser, subcmd); parser->arglist->list_argv_last_command @@ -235,64 +235,64 @@ static b_status parse_arg(struct argv_parser *parser) continue; } - struct b_command_arg *arg = b_unbox( - struct b_command_arg, parser->arg_it, arg_entry); + struct fx_command_arg *arg = fx_unbox( + struct fx_command_arg, parser->arg_it, arg_entry); if (!arg) { - b_arglist_report_unexpected_arg(parser->arglist, value); - return B_ERR_BAD_FORMAT; + fx_arglist_report_unexpected_arg(parser->arglist, value); + return FX_ERR_BAD_FORMAT; } - b_status status = put_arg(parser->arglist, arg, value); + fx_status status = put_arg(parser->arglist, arg, value); parser->nr_values_cur_arg++; advance(parser); - if (status == B_ERR_INVALID_ARGUMENT) { - b_arglist_report_invalid_arg_value( - parser->arglist, B_COMMAND_INVALID_ID, + if (status == FX_ERR_INVALID_ARGUMENT) { + fx_arglist_report_invalid_arg_value( + parser->arglist, FX_COMMAND_INVALID_ID, arg->arg_id, value); } - if (B_ERR(status)) { + if (FX_ERR(status)) { return status; } - if (arg->arg_nr_values == B_ARG_0_OR_1_VALUES) { - parser->arg_it = b_queue_next(parser->arg_it); + if (arg->arg_nr_values == FX_ARG_0_OR_1_VALUES) { + parser->arg_it = fx_queue_next(parser->arg_it); parser->nr_values_cur_arg = 0; continue; } if (parser->nr_values_cur_arg == arg->arg_nr_values) { - parser->arg_it = b_queue_next(parser->arg_it); + parser->arg_it = fx_queue_next(parser->arg_it); parser->nr_values_cur_arg = 0; continue; } } - return B_SUCCESS; + return FX_SUCCESS; } -static struct b_command_arg *get_first_arg(struct b_command *cmd) +static struct fx_command_arg *get_first_arg(struct fx_command *cmd) { - struct b_command_arg *arg = NULL; + struct fx_command_arg *arg = NULL; - struct b_queue_entry *first_arg = b_queue_first(&cmd->b_arg); + struct fx_queue_entry *first_arg = fx_queue_first(&cmd->c_arg); if (first_arg) { - arg = b_unbox(struct b_command_arg, first_arg, arg_entry); + arg = fx_unbox(struct fx_command_arg, first_arg, arg_entry); } return arg; } -static b_status parse_short_opt(struct argv_parser *parser) +static fx_status parse_short_opt(struct argv_parser *parser) { const char *flags = peek(parser); flags += 1; - struct b_command_option *opt = NULL; - struct b_command_arg *arg = NULL; - struct b_command *subcmd = NULL; + struct fx_command_option *opt = NULL; + struct fx_command_arg *arg = NULL; + struct fx_command *subcmd = NULL; int nr_args_cur_opt = 0; @@ -303,9 +303,9 @@ static b_status parse_short_opt(struct argv_parser *parser) } subcmd = NULL; - opt = b_command_get_option_with_short_name(parser->cmd, flag); + opt = fx_command_get_option_with_short_name(parser->cmd, flag); if (!opt) { - subcmd = b_command_get_subcommand_with_short_name( + subcmd = fx_command_get_subcommand_with_short_name( parser->cmd, flag); } @@ -318,22 +318,22 @@ static b_status parse_short_opt(struct argv_parser *parser) } if (!opt) { - b_string *usage = z__b_command_default_usage_string( + fx_string *usage = z__fx_command_default_usage_string( parser->cmd, NULL, parser->arglist); - b_err("unrecognised argument '" F_YELLOW "-%c" F_RESET - "'\n\n", - flag, b_string_ptr(usage)); - b_i("usage: %s", b_string_ptr(usage)); - b_i("for more information, use '" F_YELLOW - "--help" F_RESET "'\n"); - b_string_unref(usage); + fx_err("unrecognised argument '" F_YELLOW "-%c" F_RESET + "'\n\n", + flag, fx_string_ptr(usage)); + fx_i("usage: %s", fx_string_ptr(usage)); + fx_i("for more information, use '" F_YELLOW + "--help" F_RESET "'\n"); + fx_string_unref(usage); - return B_ERR_NO_ENTRY; + return FX_ERR_NO_ENTRY; } flags++; - if (b_queue_empty(&opt->opt_args)) { + if (fx_queue_empty(&opt->opt_args)) { set_opt(parser->arglist, opt); continue; } @@ -341,15 +341,15 @@ static b_status parse_short_opt(struct argv_parser *parser) break; } - if (!opt || b_queue_empty(&opt->opt_args)) { + if (!opt || fx_queue_empty(&opt->opt_args)) { advance(parser); - return B_SUCCESS; + return FX_SUCCESS; } - struct b_arglist_option *arglist_opt = malloc(sizeof *arglist_opt); + struct fx_arglist_option *arglist_opt = malloc(sizeof *arglist_opt); if (!arglist_opt) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } memset(arglist_opt, 0x0, sizeof *arglist_opt); @@ -357,7 +357,7 @@ static b_status parse_short_opt(struct argv_parser *parser) arglist_opt->opt_id = opt->opt_id; put_arglist_option(&parser->arglist->list_options, arglist_opt); - struct b_queue_entry *entry = b_queue_first(&opt->opt_args); + struct fx_queue_entry *entry = fx_queue_first(&opt->opt_args); const char *value = flags; if (*value == '\0') { advance(parser); @@ -369,48 +369,48 @@ static b_status parse_short_opt(struct argv_parser *parser) value = NULL; } - arg = b_unbox(struct b_command_arg, entry, arg_entry); + arg = fx_unbox(struct fx_command_arg, entry, arg_entry); if (!value || *value == '\0' || *value == '-') { value = NULL; } if (value) { - b_status status + fx_status status = put_opt_arg(arglist_opt, opt, arg, value); nr_args_cur_opt++; - if (status == B_ERR_INVALID_ARGUMENT) { - b_arglist_report_invalid_arg_value( - parser->arglist, B_COMMAND_INVALID_ID, + if (status == FX_ERR_INVALID_ARGUMENT) { + fx_arglist_report_invalid_arg_value( + parser->arglist, FX_COMMAND_INVALID_ID, arg->arg_id, value); } - if (B_ERR(status)) { + if (FX_ERR(status)) { return status; } } - if (arg->arg_nr_values == B_ARG_0_OR_1_VALUES) { + if (arg->arg_nr_values == FX_ARG_0_OR_1_VALUES) { nr_args_cur_opt = 0; goto next_value; } - if (arg->arg_nr_values == B_ARG_0_OR_MORE_VALUES && !value) { + if (arg->arg_nr_values == FX_ARG_0_OR_MORE_VALUES && !value) { nr_args_cur_opt = 0; goto next_value; } - if (arg->arg_nr_values == B_ARG_1_OR_MORE_VALUES && !value) { + if (arg->arg_nr_values == FX_ARG_1_OR_MORE_VALUES && !value) { if (nr_args_cur_opt > 0) { nr_args_cur_opt = 0; goto next_value; } - b_arglist_report_missing_args( + fx_arglist_report_missing_args( parser->arglist, opt->opt_id, arg->arg_id, nr_args_cur_opt); - return B_ERR_BAD_FORMAT; + return FX_ERR_BAD_FORMAT; } if (nr_args_cur_opt == arg->arg_nr_values) { @@ -419,43 +419,43 @@ static b_status parse_short_opt(struct argv_parser *parser) } if (!value) { - b_arglist_report_missing_args( + fx_arglist_report_missing_args( parser->arglist, opt->opt_id, arg->arg_id, nr_args_cur_opt); - return B_ERR_BAD_FORMAT; + return FX_ERR_BAD_FORMAT; } next_value: - entry = b_queue_next(entry); + entry = fx_queue_next(entry); value = advance(parser); } - return B_SUCCESS; + return FX_SUCCESS; } -static b_status parse_long_opt(struct argv_parser *parser) +static fx_status parse_long_opt(struct argv_parser *parser) { const char *opt_name = peek(parser); opt_name += 2; - struct b_command_option *opt = NULL; - struct b_command *subcmd = NULL; + struct fx_command_option *opt = NULL; + struct fx_command *subcmd = NULL; - opt = b_command_get_option_with_long_name(parser->cmd, opt_name); + opt = fx_command_get_option_with_long_name(parser->cmd, opt_name); if (!opt) { - subcmd = b_command_get_subcommand_with_long_name( + subcmd = fx_command_get_subcommand_with_long_name( parser->cmd, opt_name); } if (!opt && !subcmd) { - b_string *usage = z__b_command_default_usage_string( + fx_string *usage = z__fx_command_default_usage_string( parser->cmd, NULL, parser->arglist); - b_err("unrecognised argument '" F_YELLOW "--%s" F_RESET - "'\n\nusage: %s\n\nfor more information, use '" F_YELLOW - "--help" F_RESET "'\n", - opt_name, b_string_ptr(usage)); - b_string_unref(usage); + fx_err("unrecognised argument '" F_YELLOW "--%s" F_RESET + "'\n\nusage: %s\n\nfor more information, use '" F_YELLOW + "--help" F_RESET "'\n", + opt_name, fx_string_ptr(usage)); + fx_string_unref(usage); - return B_ERR_NO_ENTRY; + return FX_ERR_NO_ENTRY; } advance(parser); @@ -463,20 +463,20 @@ static b_status parse_long_opt(struct argv_parser *parser) if (subcmd) { move_to_subcommand(parser, subcmd); parser->arglist->list_argv_last_command = parser->index + 1; - return B_SUCCESS; + return FX_SUCCESS; } - if (b_queue_empty(&opt->opt_args)) { + if (fx_queue_empty(&opt->opt_args)) { return set_opt(parser->arglist, opt); } int nr_args_total = 0; int nr_args_cur_opt = 0; - struct b_arglist_option *arglist_opt = malloc(sizeof *arglist_opt); + struct fx_arglist_option *arglist_opt = malloc(sizeof *arglist_opt); if (!arglist_opt) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } memset(arglist_opt, 0x0, sizeof *arglist_opt); @@ -484,11 +484,11 @@ static b_status parse_long_opt(struct argv_parser *parser) arglist_opt->opt_id = opt->opt_id; put_arglist_option(&parser->arglist->list_options, arglist_opt); - struct b_queue_entry *entry = b_queue_first(&opt->opt_args); - struct b_command_arg *arg = NULL; + struct fx_queue_entry *entry = fx_queue_first(&opt->opt_args); + struct fx_command_arg *arg = NULL; while (entry) { - arg = b_unbox(struct b_command_arg, entry, arg_entry); + arg = fx_unbox(struct fx_command_arg, entry, arg_entry); const char *value = peek(parser); if (!value || value[0] == '-') { @@ -496,75 +496,75 @@ static b_status parse_long_opt(struct argv_parser *parser) } if (value) { - b_status status + fx_status status = put_opt_arg(arglist_opt, opt, arg, value); nr_args_total++; nr_args_cur_opt++; advance(parser); - if (status == B_ERR_INVALID_ARGUMENT) { - b_arglist_report_invalid_arg_value( - parser->arglist, B_COMMAND_INVALID_ID, + if (status == FX_ERR_INVALID_ARGUMENT) { + fx_arglist_report_invalid_arg_value( + parser->arglist, FX_COMMAND_INVALID_ID, arg->arg_id, value); } - if (B_ERR(status)) { + if (FX_ERR(status)) { return status; } } - if (arg->arg_nr_values == B_ARG_0_OR_1_VALUES) { - entry = b_queue_next(entry); + if (arg->arg_nr_values == FX_ARG_0_OR_1_VALUES) { + entry = fx_queue_next(entry); nr_args_cur_opt = 0; continue; } - if (arg->arg_nr_values == B_ARG_0_OR_MORE_VALUES && !value) { - entry = b_queue_next(entry); + if (arg->arg_nr_values == FX_ARG_0_OR_MORE_VALUES && !value) { + entry = fx_queue_next(entry); nr_args_cur_opt = 0; continue; } - if (arg->arg_nr_values == B_ARG_1_OR_MORE_VALUES && !value) { + if (arg->arg_nr_values == FX_ARG_1_OR_MORE_VALUES && !value) { if (nr_args_cur_opt > 0) { - entry = b_queue_next(entry); + entry = fx_queue_next(entry); nr_args_cur_opt = 0; continue; } - b_arglist_report_missing_args( + fx_arglist_report_missing_args( parser->arglist, opt->opt_id, arg->arg_id, nr_args_cur_opt); - return B_ERR_BAD_FORMAT; + return FX_ERR_BAD_FORMAT; } if (nr_args_cur_opt == arg->arg_nr_values) { - entry = b_queue_next(entry); + entry = fx_queue_next(entry); nr_args_cur_opt = 0; continue; } if (!value) { - b_arglist_report_missing_args( + fx_arglist_report_missing_args( parser->arglist, opt->opt_id, arg->arg_id, nr_args_cur_opt); - return B_ERR_BAD_FORMAT; + return FX_ERR_BAD_FORMAT; } } - return B_SUCCESS; + return FX_SUCCESS; } -static bool should_show_help(struct b_command *cmd, struct b_arglist *args) +static bool should_show_help(struct fx_command *cmd, struct fx_arglist *args) { bool show_help = false; size_t count = 0; - b_arglist_iterator it; - b_arglist_foreach(&it, args) + fx_arglist_iterator it; + fx_arglist_foreach(&it, args) { - if (it.opt_id == B_COMMAND_OPTION_HELP) { + if (it.opt_id == FX_COMMAND_OPTION_HELP) { show_help = true; break; } @@ -572,15 +572,16 @@ static bool should_show_help(struct b_command *cmd, struct b_arglist *args) count++; } - if (count == 0 && (cmd->b_flags & B_COMMAND_SHOW_HELP_BY_DEFAULT)) { + if (count == 0 && (cmd->c_flags & FX_COMMAND_SHOW_HELP_BY_DEFAULT)) { show_help = true; } return show_help; } -b_status b_arglist_parse( - struct b_arglist *args, struct b_command **cmd, int argc, const char **argv) +fx_status fx_arglist_parse( + struct fx_arglist *args, struct fx_command **cmd, int argc, + const char **argv) { struct argv_parser parser = { .arglist = args, @@ -590,7 +591,7 @@ b_status b_arglist_parse( move_to_subcommand(&parser, *cmd); - b_status status = B_SUCCESS; + fx_status status = FX_SUCCESS; bool arg_only = false; while (1) { @@ -600,14 +601,14 @@ b_status b_arglist_parse( } size_t len = strlen(arg); - status = B_SUCCESS; + status = FX_SUCCESS; if (len < 2 || arg_only) { status = parse_arg(&parser); } else if (arg[0] == '-' && arg[1] != '-') { status = parse_short_opt(&parser); } else if (arg[0] == '-' && arg[1] == '-' && arg[2] == '\0') { - status = B_SUCCESS; + status = FX_SUCCESS; arg_only = true; advance(&parser); continue; @@ -617,7 +618,7 @@ b_status b_arglist_parse( status = parse_arg(&parser); } - if (B_ERR(status)) { + if (FX_ERR(status)) { return status; } } @@ -628,54 +629,54 @@ b_status b_arglist_parse( status = check_required_args(&parser); } - if (B_ERR(status)) { + if (FX_ERR(status)) { return status; } *cmd = parser.cmd; - return B_SUCCESS; + return FX_SUCCESS; } -static void arglist_option_destroy(struct b_arglist_option *opt) +static void arglist_option_destroy(struct fx_arglist_option *opt) { free(opt); } -static void arglist_value_destroy(struct b_arglist_value *val) +static void arglist_value_destroy(struct fx_arglist_value *val) { - if (val->val_type == B_COMMAND_ARG_STRING) { + if (val->val_type == FX_COMMAND_ARG_STRING) { free(val->val_str); } free(val); } -void b_arglist_destroy(struct b_arglist *args) +void fx_arglist_destroy(struct fx_arglist *args) { - b_btree_node *opt_it, *args_it; - b_btree_node *opt_next, *args_next; + fx_bst_node *opt_it, *args_it; + fx_bst_node *opt_next, *args_next; - opt_it = b_btree_first(&args->list_options); + opt_it = fx_bst_first(&args->list_options); while (opt_it) { - struct b_arglist_option *opt - = b_unbox(struct b_arglist_option, opt_it, opt_node); - opt_next = b_btree_next(opt_it); + struct fx_arglist_option *opt + = fx_unbox(struct fx_arglist_option, opt_it, opt_node); + opt_next = fx_bst_next(opt_it); - args_it = b_btree_first(&opt->opt_values); + args_it = fx_bst_first(&opt->opt_values); while (args_it) { - struct b_arglist_value *val = b_unbox( - struct b_arglist_value, args_it, val_node); - args_next = b_btree_next(args_it); + struct fx_arglist_value *val = fx_unbox( + struct fx_arglist_value, args_it, val_node); + args_next = fx_bst_next(args_it); if (val) { - b_btree_delete(&opt->opt_values, args_it); + fx_bst_delete(&opt->opt_values, args_it); arglist_value_destroy(val); } args_it = args_next; } - b_btree_delete(&args->list_options, opt_it); + fx_bst_delete(&args->list_options, opt_it); arglist_option_destroy(opt); opt_it = opt_next; @@ -684,201 +685,202 @@ void b_arglist_destroy(struct b_arglist *args) free(args); } -b_status b_arglist_get_string( - const b_arglist *args, unsigned int opt_id, unsigned int arg_id, +fx_status fx_arglist_get_string( + const fx_arglist *args, unsigned int opt_id, unsigned int arg_id, unsigned int index, const char **out) { - b_arglist_iterator it = {0}; - b_arglist_iterator_begin(args, opt_id, arg_id, &it); - while (b_arglist_iterator_is_valid(&it)) { + fx_arglist_iterator it = {0}; + fx_arglist_iterator_begin(args, opt_id, arg_id, &it); + while (fx_arglist_iterator_is_valid(&it)) { if (index > 0) { index--; - b_arglist_iterator_next(&it); + fx_arglist_iterator_next(&it); continue; } if (it.value && it.value->val_str) { *out = it.value->val_str; - return B_SUCCESS; + return FX_SUCCESS; } - b_arglist_iterator_next(&it); + fx_arglist_iterator_next(&it); } - return B_ERR_NO_ENTRY; + return FX_ERR_NO_ENTRY; } -b_status b_arglist_get_int( - const b_arglist *args, unsigned int opt_id, unsigned int arg_id, +fx_status fx_arglist_get_int( + const fx_arglist *args, unsigned int opt_id, unsigned int arg_id, unsigned int index, long long *out) { - b_arglist_iterator it = {0}; - b_arglist_iterator_begin(args, opt_id, arg_id, &it); - while (b_arglist_iterator_is_valid(&it)) { + fx_arglist_iterator it = {0}; + fx_arglist_iterator_begin(args, opt_id, arg_id, &it); + while (fx_arglist_iterator_is_valid(&it)) { if (index > 0) { index--; - b_arglist_iterator_next(&it); + fx_arglist_iterator_next(&it); continue; } if (it.value) { *out = it.value->val_int; - return B_SUCCESS; + return FX_SUCCESS; } - b_arglist_iterator_next(&it); + fx_arglist_iterator_next(&it); } - return B_ERR_NO_ENTRY; + return FX_ERR_NO_ENTRY; } -b_status b_arglist_get_uint( - const b_arglist *args, unsigned int opt_id, unsigned int arg_id, +fx_status fx_arglist_get_uint( + const fx_arglist *args, unsigned int opt_id, unsigned int arg_id, unsigned int index, unsigned long long *out) { - b_arglist_iterator it = {0}; - b_arglist_iterator_begin(args, opt_id, arg_id, &it); - while (b_arglist_iterator_is_valid(&it)) { + fx_arglist_iterator it = {0}; + fx_arglist_iterator_begin(args, opt_id, arg_id, &it); + while (fx_arglist_iterator_is_valid(&it)) { if (index > 0) { index--; - b_arglist_iterator_next(&it); + fx_arglist_iterator_next(&it); continue; } if (it.value && it.value->val_uint) { *out = it.value->val_uint; - return B_SUCCESS; + return FX_SUCCESS; } - b_arglist_iterator_next(&it); + fx_arglist_iterator_next(&it); } - return B_ERR_NO_ENTRY; + return FX_ERR_NO_ENTRY; } -b_status b_arglist_get_option( - const b_arglist *args, unsigned int opt_id, unsigned int index, - b_arglist_option **out) +fx_status fx_arglist_get_option( + const fx_arglist *args, unsigned int opt_id, unsigned int index, + fx_arglist_option **out) { - struct b_btree_node *node = b_btree_first(&args->list_options); + struct fx_bst_node *node = fx_bst_first(&args->list_options); while (node) { - b_arglist_option *cur = b_unbox(b_arglist_option, node, opt_node); + fx_arglist_option *cur + = fx_unbox(fx_arglist_option, node, opt_node); if (cur->opt_id != opt_id) { - node = b_btree_next(node); + node = fx_bst_next(node); continue; } if (index == 0) { *out = cur; - return B_SUCCESS; + return FX_SUCCESS; } - node = b_btree_next(node); + node = fx_bst_next(node); index--; } - return B_ERR_NO_ENTRY; + return FX_ERR_NO_ENTRY; } -size_t b_arglist_get_count( - const b_arglist *args, unsigned int opt_id, unsigned int arg_id) +size_t fx_arglist_get_count( + const fx_arglist *args, unsigned int opt_id, unsigned int arg_id) { size_t count = 0; - b_arglist_iterator it; - b_arglist_iterator_begin(args, opt_id, arg_id, &it); - while (b_arglist_iterator_is_valid(&it)) { + fx_arglist_iterator it; + fx_arglist_iterator_begin(args, opt_id, arg_id, &it); + while (fx_arglist_iterator_is_valid(&it)) { count++; - b_arglist_iterator_next(&it); + fx_arglist_iterator_next(&it); } return count; } -b_status b_arglist_option_get_value( - const b_arglist_option *opt, unsigned int arg_id, unsigned int index, - b_arglist_value **out) +fx_status fx_arglist_option_get_value( + const fx_arglist_option *opt, unsigned int arg_id, unsigned int index, + fx_arglist_value **out) { - struct b_btree_node *node = b_btree_first(&opt->opt_values); + struct fx_bst_node *node = fx_bst_first(&opt->opt_values); while (node) { - b_arglist_value *cur = b_unbox(b_arglist_value, node, val_node); + fx_arglist_value *cur = fx_unbox(fx_arglist_value, node, val_node); if (cur->val_id != arg_id) { - node = b_btree_next(node); + node = fx_bst_next(node); continue; } if (index == 0) { *out = cur; - return B_SUCCESS; + return FX_SUCCESS; } - node = b_btree_next(node); + node = fx_bst_next(node); index--; } - return B_ERR_NO_ENTRY; + return FX_ERR_NO_ENTRY; } /************************ arglist iterator functions **************************/ -static struct b_arglist_option *advance_to_next_opt(struct b_arglist_iterator *it) +static struct fx_arglist_option *advance_to_next_opt(struct fx_arglist_iterator *it) { - struct b_arglist_option *opt; + struct fx_arglist_option *opt; - // b_btree_iterator_next(&it->_opt_it); + // fx_bst_iterator_next(&it->_opt_it); while (it->_opt_it) { - opt = b_unbox(struct b_arglist_option, it->_opt_it, opt_node); + opt = fx_unbox(struct fx_arglist_option, it->_opt_it, opt_node); if (opt && (opt->opt_id == it->_opt_filter - || it->_opt_filter == B_COMMAND_INVALID_ID)) { + || it->_opt_filter == FX_COMMAND_INVALID_ID)) { it->opt_id = opt->opt_id; return opt; } - it->_opt_it = b_btree_next(it->_opt_it); + it->_opt_it = fx_bst_next(it->_opt_it); } return NULL; } -static struct b_arglist_value *advance_to_next_arg(struct b_arglist_iterator *it) +static struct fx_arglist_value *advance_to_next_arg(struct fx_arglist_iterator *it) { - struct b_arglist_value *val; + struct fx_arglist_value *val; - // b_btree_iterator_next(&it->_arg_it); + // fx_bst_iterator_next(&it->_arg_it); while (it->_arg_it) { - val = b_unbox(struct b_arglist_value, it->_arg_it, val_node); + val = fx_unbox(struct fx_arglist_value, it->_arg_it, val_node); if (val && (val->val_id == it->_arg_filter - || it->_arg_filter == B_COMMAND_INVALID_ID)) { + || it->_arg_filter == FX_COMMAND_INVALID_ID)) { it->value = val; return val; } - it->_arg_it = b_btree_next(it->_arg_it); + it->_arg_it = fx_bst_next(it->_arg_it); } return NULL; } -int b_arglist_iterator_begin( - const struct b_arglist *args, unsigned int opt_filter, - unsigned int arg_filter, struct b_arglist_iterator *it) +int fx_arglist_iterator_begin( + const struct fx_arglist *args, unsigned int opt_filter, + unsigned int arg_filter, struct fx_arglist_iterator *it) { memset(it, 0x0, sizeof *it); - it->opt_id = B_COMMAND_INVALID_ID; + it->opt_id = FX_COMMAND_INVALID_ID; it->_opt_filter = opt_filter; it->_arg_filter = arg_filter; - it->_opt_it = b_btree_first(&args->list_options); - struct b_arglist_option *opt = NULL; - struct b_arglist_value *val = NULL; + it->_opt_it = fx_bst_first(&args->list_options); + struct fx_arglist_option *opt = NULL; + struct fx_arglist_value *val = NULL; while (1) { if (!it->_opt_it) { @@ -886,31 +888,32 @@ int b_arglist_iterator_begin( return -1; } - opt = b_unbox(struct b_arglist_option, it->_opt_it, opt_node); + opt = fx_unbox(struct fx_arglist_option, it->_opt_it, opt_node); if (!opt || (opt_filter != opt->opt_id - && opt_filter != B_COMMAND_INVALID_ID)) { - it->_opt_it = b_btree_next(it->_opt_it); + && opt_filter != FX_COMMAND_INVALID_ID)) { + it->_opt_it = fx_bst_next(it->_opt_it); continue; } - it->_arg_it = b_btree_first(&opt->opt_values); + it->_arg_it = fx_bst_first(&opt->opt_values); bool done = false; while (1) { if (!it->_arg_it) { - if (arg_filter == B_COMMAND_INVALID_ID) { + if (arg_filter == FX_COMMAND_INVALID_ID) { done = true; } break; } - val = b_unbox(struct b_arglist_value, it->_arg_it, val_node); + val = fx_unbox( + struct fx_arglist_value, it->_arg_it, val_node); if (!val || (arg_filter != val->val_id - && arg_filter != B_COMMAND_INVALID_ID)) { - it->_arg_it = b_btree_next(it->_arg_it); + && arg_filter != FX_COMMAND_INVALID_ID)) { + it->_arg_it = fx_bst_next(it->_arg_it); continue; } @@ -922,7 +925,7 @@ int b_arglist_iterator_begin( break; } - it->_opt_it = b_btree_next(it->_opt_it); + it->_opt_it = fx_bst_next(it->_opt_it); } it->opt_id = opt->opt_id; @@ -931,12 +934,12 @@ int b_arglist_iterator_begin( return 0; } -bool b_arglist_iterator_next(struct b_arglist_iterator *it) +bool fx_arglist_iterator_next(struct fx_arglist_iterator *it) { - struct b_arglist_option *opt; - struct b_arglist_value *val; + struct fx_arglist_option *opt; + struct fx_arglist_value *val; - it->_arg_it = b_btree_next(it->_arg_it); + it->_arg_it = fx_bst_next(it->_arg_it); while (1) { val = advance_to_next_arg(it); @@ -947,62 +950,62 @@ bool b_arglist_iterator_next(struct b_arglist_iterator *it) return true; } - it->_opt_it = b_btree_next(it->_opt_it); + it->_opt_it = fx_bst_next(it->_opt_it); opt = advance_to_next_opt(it); if (!opt) { it->value = NULL; - it->opt_id = B_COMMAND_INVALID_ID; + it->opt_id = FX_COMMAND_INVALID_ID; return false; } - it->_arg_it = b_btree_first(&opt->opt_values); + it->_arg_it = fx_bst_first(&opt->opt_values); - if (it->_arg_filter == B_COMMAND_INVALID_ID) { + if (it->_arg_filter == FX_COMMAND_INVALID_ID) { return true; } } } -bool b_arglist_iterator_is_valid(const struct b_arglist_iterator *it) +bool fx_arglist_iterator_is_valid(const struct fx_arglist_iterator *it) { - return it->opt_id != B_COMMAND_INVALID_ID || it->value != NULL; + return it->opt_id != FX_COMMAND_INVALID_ID || it->value != NULL; } /********************* arglist option iterator functions **********************/ -static struct b_arglist_option *advance_to_next_opt2( - struct b_arglist_option_iterator *it) +static struct fx_arglist_option *advance_to_next_opt2( + struct fx_arglist_option_iterator *it) { - struct b_arglist_option *opt; + struct fx_arglist_option *opt; while (it->_opt_it) { - opt = b_unbox(struct b_arglist_option, it->_opt_it, opt_node); + opt = fx_unbox(struct fx_arglist_option, it->_opt_it, opt_node); if (opt && (opt->opt_id == it->_opt_filter - || it->_opt_filter == B_COMMAND_INVALID_ID)) { + || it->_opt_filter == FX_COMMAND_INVALID_ID)) { it->opt_id = opt->opt_id; return opt; } - it->_opt_it = b_btree_next(it->_opt_it); + it->_opt_it = fx_bst_next(it->_opt_it); } return NULL; } -int b_arglist_option_iterator_begin( - const struct b_arglist *args, unsigned int opt_filter, - struct b_arglist_option_iterator *it) +int fx_arglist_option_iterator_begin( + const struct fx_arglist *args, unsigned int opt_filter, + struct fx_arglist_option_iterator *it) { memset(it, 0x0, sizeof *it); - it->opt_id = B_COMMAND_INVALID_ID; + it->opt_id = FX_COMMAND_INVALID_ID; it->_opt_filter = opt_filter; - it->_opt_it = b_btree_first(&args->list_options); - struct b_arglist_option *opt = NULL; - struct b_arglist_value *val = NULL; + it->_opt_it = fx_bst_first(&args->list_options); + struct fx_arglist_option *opt = NULL; + struct fx_arglist_value *val = NULL; while (1) { if (!it->_opt_it) { @@ -1010,14 +1013,14 @@ int b_arglist_option_iterator_begin( return -1; } - opt = b_unbox(struct b_arglist_option, it->_opt_it, opt_node); + opt = fx_unbox(struct fx_arglist_option, it->_opt_it, opt_node); if (opt && (opt_filter == opt->opt_id - || opt_filter == B_COMMAND_INVALID_ID)) { + || opt_filter == FX_COMMAND_INVALID_ID)) { break; } - it->_opt_it = b_btree_next(it->_opt_it); + it->_opt_it = fx_bst_next(it->_opt_it); } it->opt_id = opt->opt_id; @@ -1026,16 +1029,16 @@ int b_arglist_option_iterator_begin( return 0; } -bool b_arglist_option_iterator_next(struct b_arglist_option_iterator *it) +bool fx_arglist_option_iterator_next(struct fx_arglist_option_iterator *it) { - struct b_arglist_option *opt; - struct b_arglist_value *val; + struct fx_arglist_option *opt; + struct fx_arglist_value *val; - it->_opt_it = b_btree_next(it->_opt_it); + it->_opt_it = fx_bst_next(it->_opt_it); opt = advance_to_next_opt2(it); if (!opt) { it->opt = NULL; - it->opt_id = B_COMMAND_INVALID_ID; + it->opt_id = FX_COMMAND_INVALID_ID; return false; } @@ -1044,7 +1047,7 @@ bool b_arglist_option_iterator_next(struct b_arglist_option_iterator *it) return true; } -bool b_arglist_option_iterator_is_valid(const struct b_arglist_option_iterator *it) +bool fx_arglist_option_iterator_is_valid(const struct fx_arglist_option_iterator *it) { - return it->opt_id != B_COMMAND_INVALID_ID || it->opt != NULL; + return it->opt_id != FX_COMMAND_INVALID_ID || it->opt != NULL; } diff --git a/cmd/command.c b/cmd/command.c index a492ef8..8fa44cd 100644 --- a/cmd/command.c +++ b/cmd/command.c @@ -1,20 +1,20 @@ #include "command.h" -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #include #include #include -#define OUTPUT_STREAM b_stdtty_err +#define OUTPUT_STREAM fx_stdtty_err -static struct b_btree command_list = {0}; +static struct fx_bst command_list = {0}; -B_BTREE_DEFINE_SIMPLE_GET(struct b_command, unsigned int, b_node, b_id, get_command) -B_BTREE_DEFINE_SIMPLE_INSERT(struct b_command, b_node, b_id, put_command) +FX_BTREE_DEFINE_SIMPLE_GET(struct fx_command, unsigned int, c_node, c_id, get_command) +FX_BTREE_DEFINE_SIMPLE_INSERT(struct fx_command, c_node, c_id, put_command) enum item_type { ITEM_OPTION, @@ -24,47 +24,47 @@ enum item_type { static void command_list_cleanup(void) { - struct b_btree_node *node = b_btree_first(&command_list); + struct fx_bst_node *node = fx_bst_first(&command_list); while (node) { - struct b_command *cmd = b_unbox(struct b_command, node, b_node); + struct fx_command *cmd = fx_unbox(struct fx_command, node, c_node); if (!cmd) { break; } - struct b_btree_node *next = b_btree_next(node); - b_btree_delete(&command_list, node); - b_command_destroy(cmd); + struct fx_bst_node *next = fx_bst_next(node); + fx_bst_delete(&command_list, node); + fx_command_destroy(cmd); node = next; } } -struct b_command *b_command_create(unsigned int id) +struct fx_command *fx_command_create(unsigned int id) { - struct b_command *out = malloc(sizeof *out); + struct fx_command *out = malloc(sizeof *out); if (!out) { return NULL; } memset(out, 0x0, sizeof *out); - out->b_id = id; - out->b_parent_id = B_COMMAND_INVALID_ID; + out->c_id = id; + out->c_parent_id = FX_COMMAND_INVALID_ID; return out; } -static void command_usage_destroy(struct b_command_usage *usage) +static void command_usage_destroy(struct fx_command_usage *usage) { - struct b_queue_entry *entry = b_queue_first(&usage->u_parts); + struct fx_queue_entry *entry = fx_queue_first(&usage->u_parts); while (entry) { - struct b_command_usage_entry *arg - = b_unbox(struct b_command_usage_entry, entry, e_entry); + struct fx_command_usage_entry *arg + = fx_unbox(struct fx_command_usage_entry, entry, e_entry); if (!arg) { continue; } - struct b_queue_entry *next = b_queue_next(entry); - b_queue_delete(&usage->u_parts, entry); + struct fx_queue_entry *next = fx_queue_next(entry); + fx_queue_delete(&usage->u_parts, entry); free(arg); entry = next; @@ -73,81 +73,81 @@ static void command_usage_destroy(struct b_command_usage *usage) free(usage); } -void b_command_destroy(struct b_command *cmd) +void fx_command_destroy(struct fx_command *cmd) { - if (cmd->b_name) { - free(cmd->b_name); + if (cmd->c_name) { + free(cmd->c_name); } - if (cmd->b_long_name) { - free(cmd->b_long_name); + if (cmd->c_long_name) { + free(cmd->c_long_name); } - if (cmd->b_description) { - free(cmd->b_description); + if (cmd->c_description) { + free(cmd->c_description); } - struct b_queue_entry *entry = b_queue_first(&cmd->b_opt); - struct b_queue_entry *next = NULL; + struct fx_queue_entry *entry = fx_queue_first(&cmd->c_opt); + struct fx_queue_entry *next = NULL; while (entry) { - struct b_command_option *opt - = b_unbox(struct b_command_option, entry, opt_entry); + struct fx_command_option *opt + = fx_unbox(struct fx_command_option, entry, opt_entry); if (!opt) { break; } - next = b_queue_next(entry); - b_queue_delete(&cmd->b_opt, entry); + next = fx_queue_next(entry); + fx_queue_delete(&cmd->c_opt, entry); - b_command_option_destroy(opt); + fx_command_option_destroy(opt); entry = next; } - entry = b_queue_first(&cmd->b_arg); + entry = fx_queue_first(&cmd->c_arg); while (entry) { - struct b_command_arg *arg - = b_unbox(struct b_command_arg, entry, arg_entry); + struct fx_command_arg *arg + = fx_unbox(struct fx_command_arg, entry, arg_entry); if (!arg) { break; } - next = b_queue_next(entry); - b_queue_delete(&cmd->b_arg, entry); + next = fx_queue_next(entry); + fx_queue_delete(&cmd->c_arg, entry); - b_command_arg_destroy(arg); + fx_command_arg_destroy(arg); entry = next; } #if 0 - entry = b_queue_first(&cmd->b_subcommands); + entry = fx_queue_first(&cmd->c_subcommands); while (entry) { - struct b_command *subcmd - = b_unbox(struct b_command, entry, b_entry); + struct fx_command *subcmd + = fx_unbox(struct fx_command, entry, c_entry); if (!subcmd) { break; } - next = b_queue_next(entry); + next = fx_queue_next(entry); - b_command_destroy(subcmd); + fx_command_destroy(subcmd); entry = next; } #endif - entry = b_queue_first(&cmd->b_usage); + entry = fx_queue_first(&cmd->c_usage); while (entry) { - struct b_command_usage *usage - = b_unbox(struct b_command_usage, entry, u_entry); + struct fx_command_usage *usage + = fx_unbox(struct fx_command_usage, entry, u_entry); if (!usage) { break; } - next = b_queue_next(entry); - b_queue_delete(&cmd->b_usage, entry); + next = fx_queue_next(entry); + fx_queue_delete(&cmd->c_usage, entry); command_usage_destroy(usage); @@ -157,155 +157,156 @@ void b_command_destroy(struct b_command *cmd) free(cmd); } -b_status b_command_register(struct b_command *cmd) +fx_status fx_command_register(struct fx_command *cmd) { - struct b_command *tmp = get_command(&command_list, cmd->b_id); + struct fx_command *tmp = get_command(&command_list, cmd->c_id); if (tmp) { - return B_ERR_NAME_EXISTS; + return FX_ERR_NAME_EXISTS; } - if (b_btree_empty(&command_list)) { + if (fx_bst_empty(&command_list)) { atexit(command_list_cleanup); } put_command(&command_list, cmd); - return B_SUCCESS; + return FX_SUCCESS; } -b_status b_command_set_name(struct b_command *cmd, const char *name) +fx_status fx_command_set_name(struct fx_command *cmd, const char *name) { - char *n = b_strdup(name); + char *n = fx_strdup(name); if (!n) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } - if (cmd->b_name) { - free(cmd->b_name); - cmd->b_name = NULL; + if (cmd->c_name) { + free(cmd->c_name); + cmd->c_name = NULL; } - cmd->b_name = n; - return B_SUCCESS; + cmd->c_name = n; + return FX_SUCCESS; } -b_status b_command_set_long_name(struct b_command *cmd, const char *name) +fx_status fx_command_set_long_name(struct fx_command *cmd, const char *name) { - char *n = b_strdup(name); + char *n = fx_strdup(name); if (!n) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } - if (cmd->b_long_name) { - free(cmd->b_long_name); - cmd->b_long_name = NULL; + if (cmd->c_long_name) { + free(cmd->c_long_name); + cmd->c_long_name = NULL; } - cmd->b_long_name = n; - return B_SUCCESS; + cmd->c_long_name = n; + return FX_SUCCESS; } -b_status b_command_set_short_name(struct b_command *cmd, char name) +fx_status fx_command_set_short_name(struct fx_command *cmd, char name) { - cmd->b_short_name = name; - return B_SUCCESS; + cmd->c_short_name = name; + return FX_SUCCESS; } -b_status b_command_set_flags(struct b_command *cmd, b_command_flags flags) +fx_status fx_command_set_flags(struct fx_command *cmd, fx_command_flags flags) { - cmd->b_flags = flags; - return B_SUCCESS; + cmd->c_flags = flags; + return FX_SUCCESS; } -b_status b_command_set_description(struct b_command *cmd, const char *description) +fx_status fx_command_set_description(struct fx_command *cmd, const char *description) { - char *desc = b_strdup(description); + char *desc = fx_strdup(description); if (!desc) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } - if (cmd->b_description) { - free(cmd->b_description); - cmd->b_description = NULL; + if (cmd->c_description) { + free(cmd->c_description); + cmd->c_description = NULL; } - cmd->b_description = desc; - return B_SUCCESS; + cmd->c_description = desc; + return FX_SUCCESS; } -b_status b_command_set_callback(struct b_command *cmd, b_command_callback callback) +fx_status fx_command_set_callback( + struct fx_command *cmd, fx_command_callback callback) { - cmd->b_callback = callback; - return B_SUCCESS; + cmd->c_callback = callback; + return FX_SUCCESS; } -b_status b_command_set_parent(b_command *cmd, unsigned int parent_id) +fx_status fx_command_set_parent(fx_command *cmd, unsigned int parent_id) { - cmd->b_parent_id = parent_id; - cmd->b_parent = NULL; - return B_SUCCESS; + cmd->c_parent_id = parent_id; + cmd->c_parent = NULL; + return FX_SUCCESS; } -struct b_command_option *b_command_add_option(struct b_command *cmd, int id) +struct fx_command_option *fx_command_add_option(struct fx_command *cmd, int id) { - struct b_command_option *opt = b_command_option_create(); + struct fx_command_option *opt = fx_command_option_create(); if (!opt) { return NULL; } opt->opt_id = id; - b_queue_push_back(&cmd->b_opt, &opt->opt_entry); + fx_queue_push_back(&cmd->c_opt, &opt->opt_entry); return opt; } -struct b_command_arg *b_command_add_arg(struct b_command *cmd, int id) +struct fx_command_arg *fx_command_add_arg(struct fx_command *cmd, int id) { - struct b_command_arg *arg = b_command_arg_create(); + struct fx_command_arg *arg = fx_command_arg_create(); if (!arg) { return NULL; } arg->arg_id = id; - b_queue_push_back(&cmd->b_arg, &arg->arg_entry); + fx_queue_push_back(&cmd->c_arg, &arg->arg_entry); return arg; } -struct b_command_usage *b_command_add_usage(struct b_command *cmd) +struct fx_command_usage *fx_command_add_usage(struct fx_command *cmd) { - struct b_command_usage *usage = malloc(sizeof *usage); + struct fx_command_usage *usage = malloc(sizeof *usage); if (!usage) { return NULL; } memset(usage, 0x0, sizeof *usage); - b_queue_push_back(&cmd->b_usage, &usage->u_entry); + fx_queue_push_back(&cmd->c_usage, &usage->u_entry); return usage; } -const struct b_command_option *b_command_get_option( - const struct b_command *cmd, int id) +const struct fx_command_option *fx_command_get_option( + const struct fx_command *cmd, int id) { - struct b_queue_entry *cur = b_queue_first(&cmd->b_opt); + struct fx_queue_entry *cur = fx_queue_first(&cmd->c_opt); while (cur) { - const struct b_command_option *opt - = b_unbox(struct b_command_option, cur, opt_entry); + const struct fx_command_option *opt + = fx_unbox(struct fx_command_option, cur, opt_entry); if (opt->opt_id == id) { return opt; } - cur = b_queue_next(cur); + cur = fx_queue_next(cur); } return NULL; } -b_status b_command_usage_add_option( - struct b_command_usage *usage, struct b_command_option *opt) +fx_status fx_command_usage_add_option( + struct fx_command_usage *usage, struct fx_command_option *opt) { - struct b_command_usage_entry *u_opt = malloc(sizeof *u_opt); + struct fx_command_usage_entry *u_opt = malloc(sizeof *u_opt); if (!u_opt) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } memset(u_opt, 0x0, sizeof *u_opt); @@ -313,16 +314,16 @@ b_status b_command_usage_add_option( u_opt->e_type = CMD_USAGE_OPT; u_opt->e_opt = opt; - b_queue_push_back(&usage->u_parts, &u_opt->e_entry); - return B_SUCCESS; + fx_queue_push_back(&usage->u_parts, &u_opt->e_entry); + return FX_SUCCESS; } -b_status b_command_usage_add_arg( - struct b_command_usage *usage, struct b_command_arg *arg) +fx_status fx_command_usage_add_arg( + struct fx_command_usage *usage, struct fx_command_arg *arg) { - struct b_command_usage_entry *u_arg = malloc(sizeof *u_arg); + struct fx_command_usage_entry *u_arg = malloc(sizeof *u_arg); if (!u_arg) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } memset(u_arg, 0x0, sizeof *u_arg); @@ -330,15 +331,15 @@ b_status b_command_usage_add_arg( u_arg->e_type = CMD_USAGE_ARG; u_arg->e_arg = arg; - b_queue_push_back(&usage->u_parts, &u_arg->e_entry); - return B_SUCCESS; + fx_queue_push_back(&usage->u_parts, &u_arg->e_entry); + return FX_SUCCESS; } -b_status b_command_usage_add_command(b_command_usage *usage, unsigned int cmd_id) +fx_status fx_command_usage_add_command(fx_command_usage *usage, unsigned int cmd_id) { - struct b_command_usage_entry *u_cmd = malloc(sizeof *u_cmd); + struct fx_command_usage_entry *u_cmd = malloc(sizeof *u_cmd); if (!u_cmd) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } memset(u_cmd, 0x0, sizeof *u_cmd); @@ -346,194 +347,196 @@ b_status b_command_usage_add_command(b_command_usage *usage, unsigned int cmd_id u_cmd->e_type = CMD_USAGE_COMMAND; u_cmd->e_cmd_id = cmd_id; - b_queue_push_back(&usage->u_parts, &u_cmd->e_entry); - return B_SUCCESS; + fx_queue_push_back(&usage->u_parts, &u_cmd->e_entry); + return FX_SUCCESS; } -static void prepend_command_name(struct b_command *cmd, b_string *out) +static void prepend_command_name(struct fx_command *cmd, fx_string *out) { int nr_names = 0; - cmd->b_name &&nr_names++; - cmd->b_long_name &&nr_names++; - cmd->b_short_name &&nr_names++; + cmd->c_name &&nr_names++; + cmd->c_long_name &&nr_names++; + cmd->c_short_name &&nr_names++; if (nr_names > 1) { - b_string_prepend_cstr(out, "}"); + fx_string_prepend_cstr(out, "}"); int r = 0; - if (cmd->b_name) { - b_string_prepend_cstrf(out, "%s", cmd->b_name); + if (cmd->c_name) { + fx_string_prepend_cstrf(out, "%s", cmd->c_name); r++; } if (r == 1) { - b_string_prepend_cstr(out, "|"); + fx_string_prepend_cstr(out, "|"); } - if (cmd->b_long_name) { - b_string_prepend_cstrf(out, "--%s", cmd->b_long_name); + if (cmd->c_long_name) { + fx_string_prepend_cstrf(out, "--%s", cmd->c_long_name); r++; } if (r == 2) { - b_string_prepend_cstr(out, "|"); + fx_string_prepend_cstr(out, "|"); } - if (cmd->b_short_name) { - b_string_prepend_cstrf(out, "-%c", cmd->b_short_name); + if (cmd->c_short_name) { + fx_string_prepend_cstrf(out, "-%c", cmd->c_short_name); r++; } - b_string_prepend_cstr(out, "{"); + fx_string_prepend_cstr(out, "{"); } else { - b_string_prepend_cstr(out, cmd->b_name); + fx_string_prepend_cstr(out, cmd->c_name); } } static void get_qualified_command_name( - struct b_command *cmd, const struct b_arglist *args, b_string *out) + struct fx_command *cmd, const struct fx_arglist *args, fx_string *out) { for (unsigned int i = 0; i <= args->list_argv_last_command; i++) { if (i > 0) { - b_string_append_cstr(out, " "); + fx_string_append_cstr(out, " "); } - b_string_append_cstr(out, args->list_argv[i]); + fx_string_append_cstr(out, args->list_argv[i]); } #if 0 prepend_command_name(cmd, out); - cmd = cmd->b_parent; + cmd = cmd->c_parent; while (cmd) { - b_string_prepend_cstr(out, " "); + fx_string_prepend_cstr(out, " "); prepend_command_name(cmd, out); - cmd = cmd->b_parent; + cmd = cmd->c_parent; } #endif } static void get_usage_string( - struct b_command *cmd, struct b_arglist *args, - struct b_command_usage *usage, b_string *out) + struct fx_command *cmd, struct fx_arglist *args, + struct fx_command_usage *usage, fx_string *out) { get_qualified_command_name(cmd, args, out); - b_string *cmd_name = b_string_create(); + fx_string *cmd_name = fx_string_create(); - struct b_queue_entry *q_entry = b_queue_first(&usage->u_parts); + struct fx_queue_entry *q_entry = fx_queue_first(&usage->u_parts); while (q_entry) { - struct b_command_usage_entry *entry - = b_unbox(struct b_command_usage_entry, q_entry, e_entry); + struct fx_command_usage_entry *entry = fx_unbox( + struct fx_command_usage_entry, q_entry, e_entry); if (!entry) { break; } - b_string_clear(cmd_name); - b_string_append_cstr(out, " "); + fx_string_clear(cmd_name); + fx_string_append_cstr(out, " "); switch (entry->e_type) { case CMD_USAGE_ARG: if (entry->e_arg) { - z__b_get_arg_usage_string(entry->e_arg, false, out); + z__fx_get_arg_usage_string(entry->e_arg, false, out); } else { - b_string_append_cstr(out, "[[ARGS]"); + fx_string_append_cstr(out, "[[ARGS]"); } break; case CMD_USAGE_OPT: if (entry->e_opt) { - z__b_get_option_usage_string( + z__fx_get_option_usage_string( entry->e_opt, CMD_STR_DIRECT_USAGE, out); } else { - b_string_append_cstr(out, "[[OPTIONS]"); + fx_string_append_cstr(out, "[[OPTIONS]"); } break; case CMD_USAGE_COMMAND: - if (entry->e_cmd_id == B_COMMAND_INVALID_ID) { - b_string_append_cstr(out, "[[COMMAND]"); + if (entry->e_cmd_id == FX_COMMAND_INVALID_ID) { + fx_string_append_cstr(out, "[[COMMAND]"); break; } - struct b_command *subcmd_info + struct fx_command *subcmd_info = get_command(&command_list, entry->e_cmd_id); prepend_command_name(subcmd_info, cmd_name); - b_string_append_s(out, cmd_name); + fx_string_append_s(out, cmd_name); break; default: break; } - q_entry = b_queue_next(q_entry); + q_entry = fx_queue_next(q_entry); } - b_string_unref(cmd_name); + fx_string_unref(cmd_name); } -b_string *z__b_command_default_usage_string( - struct b_command *cmd, struct b_command_option *with_opt, - const struct b_arglist *args) +fx_string *z__fx_command_default_usage_string( + struct fx_command *cmd, struct fx_command_option *with_opt, + const struct fx_arglist *args) { - b_string *str = b_string_create(); + fx_string *str = fx_string_create(); get_qualified_command_name(cmd, args, str); if (with_opt) { - b_string_append_cstr(str, " "); - z__b_get_option_usage_string(with_opt, CMD_STR_DIRECT_USAGE, str); - } else if (!b_queue_empty(&cmd->b_opt)) { - b_string_append_cstr(str, " [OPTIONS]"); + fx_string_append_cstr(str, " "); + z__fx_get_option_usage_string(with_opt, CMD_STR_DIRECT_USAGE, str); + } else if (!fx_queue_empty(&cmd->c_opt)) { + fx_string_append_cstr(str, " [OPTIONS]"); } - struct b_queue_entry *entry = b_queue_first(&cmd->b_arg); + struct fx_queue_entry *entry = fx_queue_first(&cmd->c_arg); while (entry) { - struct b_command_arg *arg - = b_unbox(struct b_command_arg, entry, arg_entry); + struct fx_command_arg *arg + = fx_unbox(struct fx_command_arg, entry, arg_entry); - b_string_append_cstr(str, " "); - z__b_get_arg_usage_string(arg, false, str); + fx_string_append_cstr(str, " "); + z__fx_get_arg_usage_string(arg, false, str); - entry = b_queue_next(entry); + entry = fx_queue_next(entry); } - if (!b_queue_empty(&cmd->b_subcommands)) { - b_string_append_cstr(str, " [COMMAND]"); + if (!fx_queue_empty(&cmd->c_subcommands)) { + fx_string_append_cstr(str, " [COMMAND]"); } return str; } -static void get_command_string(struct b_command *cmd, b_string *out) +static void get_command_string(struct fx_command *cmd, fx_string *out) { int r = 0; - if (cmd->b_name) { - b_string_append_cstrf(out, F_GREEN "%s" F_RESET, cmd->b_name); + if (cmd->c_name) { + fx_string_append_cstrf(out, F_GREEN "%s" F_RESET, cmd->c_name); r++; } - if (cmd->b_short_name) { + if (cmd->c_short_name) { if (r > 0) { - b_string_append_cstr(out, ", "); + fx_string_append_cstr(out, ", "); } - b_string_append_cstrf(out, F_GREEN "-%c" F_RESET, cmd->b_short_name); + fx_string_append_cstrf( + out, F_GREEN "-%c" F_RESET, cmd->c_short_name); r++; } - if (cmd->b_long_name) { + if (cmd->c_long_name) { if (r > 0) { - b_string_append_cstr(out, ", "); + fx_string_append_cstr(out, ", "); } - b_string_append_cstrf(out, F_GREEN "--%s" F_RESET, cmd->b_long_name); + fx_string_append_cstrf( + out, F_GREEN "--%s" F_RESET, cmd->c_long_name); r++; } } -static void get_command_description(struct b_command *cmd, b_string *out) +static void get_command_description(struct fx_command *cmd, fx_string *out) { - b_string_append_cstr(out, cmd->b_description); + fx_string_append_cstr(out, cmd->c_description); } /* criteria for printing the option description on a separate line: @@ -548,10 +551,10 @@ static void get_command_description(struct b_command *cmd, b_string *out) && desc_len >= (term_width - newline_threshold)) \ || desc_len > (3 * term_width - newline_threshold)) -static void print_options_list(struct b_command *cmd) +static void print_options_list(struct fx_command *cmd) { unsigned int term_width = 0; - b_tty_get_dimensions(OUTPUT_STREAM, &term_width, NULL); + fx_tty_get_dimensions(OUTPUT_STREAM, &term_width, NULL); unsigned int newline_threshold = 1000000; @@ -559,300 +562,302 @@ static void print_options_list(struct b_command *cmd) newline_threshold = term_width - 12 - 35; } - b_tty_printf(OUTPUT_STREAM, 0, "\n" F_YELLOW "OPTIONS:" F_RESET "\n"); + fx_tty_printf(OUTPUT_STREAM, 0, "\n" F_YELLOW "OPTIONS:" F_RESET "\n"); - size_t desb_margin = 0; - b_string *opt_str = b_string_create(); - b_string *desc_str = b_string_create(); + size_t desc_margin = 0; + fx_string *opt_str = fx_string_create(); + fx_string *desc_str = fx_string_create(); - struct b_queue_entry *entry = b_queue_first(&cmd->b_opt); + struct fx_queue_entry *entry = fx_queue_first(&cmd->c_opt); while (entry) { - struct b_command_option *opt - = b_unbox(struct b_command_option, entry, opt_entry); + struct fx_command_option *opt + = fx_unbox(struct fx_command_option, entry, opt_entry); - b_string_clear(opt_str); - b_string_clear(desc_str); - z__b_get_option_usage_string(opt, CMD_STR_COLOUR, opt_str); - z__b_get_option_description(opt, desc_str); + fx_string_clear(opt_str); + fx_string_clear(desc_str); + z__fx_get_option_usage_string(opt, CMD_STR_COLOUR, opt_str); + z__fx_get_option_description(opt, desc_str); - size_t opt_len = b_string_get_size( - opt_str, B_STRLEN_IGNORE_ESC - | B_STRLEN_IGNORE_MOD) + size_t opt_len = fx_string_get_size( + opt_str, FX_STRLEN_IGNORE_ESC + | FX_STRLEN_IGNORE_MOD) + 4; - size_t desc_len = b_string_get_size( - desc_str, B_STRLEN_IGNORE_ESC | B_STRLEN_IGNORE_MOD); + size_t desc_len = fx_string_get_size( + desc_str, FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD); if (description_on_separate_line(opt_len, desc_len)) { goto skip; } - if (opt_len > desb_margin) { - desb_margin = opt_len; + if (opt_len > desc_margin) { + desc_margin = opt_len; } skip: - entry = b_queue_next(entry); + entry = fx_queue_next(entry); } - b_paragraph_format format = {0}; - format.p_flags = B_PARAGRAPH_DONT_INDENT_FIRST_LINE; - format.p_left_margin = desb_margin + 4; + fx_paragraph_format format = {0}; + format.p_flags = FX_PARAGRAPH_DONT_INDENT_FIRST_LINE; + format.p_left_margin = desc_margin + 4; format.p_right_margin = 4; size_t i = 0; - entry = b_queue_first(&cmd->b_opt); + entry = fx_queue_first(&cmd->c_opt); while (entry) { - struct b_command_option *opt - = b_unbox(struct b_command_option, entry, opt_entry); + struct fx_command_option *opt + = fx_unbox(struct fx_command_option, entry, opt_entry); if (!opt) { break; } - b_string_clear(opt_str); - b_string_clear(desc_str); + fx_string_clear(opt_str); + fx_string_clear(desc_str); - z__b_get_option_usage_string(opt, CMD_STR_COLOUR, opt_str); - z__b_get_option_description(opt, desc_str); - size_t opt_len = b_string_get_size( - opt_str, B_STRLEN_IGNORE_ESC - | B_STRLEN_IGNORE_MOD) + z__fx_get_option_usage_string(opt, CMD_STR_COLOUR, opt_str); + z__fx_get_option_description(opt, desc_str); + size_t opt_len = fx_string_get_size( + opt_str, FX_STRLEN_IGNORE_ESC + | FX_STRLEN_IGNORE_MOD) + 4; - size_t desc_len = b_string_get_size( - desc_str, B_STRLEN_IGNORE_ESC | B_STRLEN_IGNORE_MOD); + size_t desc_len = fx_string_get_size( + desc_str, FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD); bool new_paragraph = description_on_separate_line(opt_len, desc_len); if (new_paragraph) { - b_tty_putc(OUTPUT_STREAM, 0, '\n'); + fx_tty_putc(OUTPUT_STREAM, 0, '\n'); } - b_tty_puts(OUTPUT_STREAM, 0, " "); - b_tty_puts(OUTPUT_STREAM, 0, b_string_ptr(opt_str)); + fx_tty_puts(OUTPUT_STREAM, 0, " "); + fx_tty_puts(OUTPUT_STREAM, 0, fx_string_ptr(opt_str)); if (new_paragraph) { format.p_flags = 0; format.p_left_margin = 8; format.p_right_margin = 4; - b_tty_putc(OUTPUT_STREAM, 0, '\n'); + fx_tty_putc(OUTPUT_STREAM, 0, '\n'); } else { - format.p_flags = B_PARAGRAPH_DONT_INDENT_FIRST_LINE; - format.p_left_margin = desb_margin + 4; + format.p_flags = FX_PARAGRAPH_DONT_INDENT_FIRST_LINE; + format.p_left_margin = desc_margin + 4; format.p_right_margin = 4; } unsigned int len = opt_len; while (len < format.p_left_margin) { - b_tty_putc(OUTPUT_STREAM, 0, ' '); + fx_tty_putc(OUTPUT_STREAM, 0, ' '); len++; } - b_print_paragraph(b_string_ptr(desc_str), OUTPUT_STREAM, &format); + fx_print_paragraph(fx_string_ptr(desc_str), OUTPUT_STREAM, &format); if (new_paragraph) { - b_tty_putc(OUTPUT_STREAM, 0, '\n'); + fx_tty_putc(OUTPUT_STREAM, 0, '\n'); } - entry = b_queue_next(entry); + entry = fx_queue_next(entry); } - b_string_unref(opt_str); - b_string_unref(desc_str); + fx_string_unref(opt_str); + fx_string_unref(desc_str); } -static void print_args_list(struct b_command *cmd) +static void print_args_list(struct fx_command *cmd) { - b_tty_printf(OUTPUT_STREAM, 0, "\n" F_YELLOW "ARGS:" F_RESET "\n"); + fx_tty_printf(OUTPUT_STREAM, 0, "\n" F_YELLOW "ARGS:" F_RESET "\n"); - size_t desb_margin = 0; - b_string *str = b_string_create(); + size_t desc_margin = 0; + fx_string *str = fx_string_create(); - struct b_queue_entry *entry = b_queue_first(&cmd->b_arg); + struct fx_queue_entry *entry = fx_queue_first(&cmd->c_arg); while (entry) { - struct b_command_arg *arg - = b_unbox(struct b_command_arg, entry, arg_entry); + struct fx_command_arg *arg + = fx_unbox(struct fx_command_arg, entry, arg_entry); - b_string_clear(str); - z__b_get_arg_usage_string(arg, true, str); - size_t len = b_string_get_size( - str, B_STRLEN_IGNORE_ESC | B_STRLEN_IGNORE_MOD) + fx_string_clear(str); + z__fx_get_arg_usage_string(arg, true, str); + size_t len = fx_string_get_size( + str, FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD) + 4; - if (len > desb_margin) { - desb_margin = len; + if (len > desc_margin) { + desc_margin = len; } - entry = b_queue_next(entry); + entry = fx_queue_next(entry); } - b_paragraph_format format = {0}; - format.p_flags = B_PARAGRAPH_DONT_INDENT_FIRST_LINE; - format.p_left_margin = desb_margin + 4; + fx_paragraph_format format = {0}; + format.p_flags = FX_PARAGRAPH_DONT_INDENT_FIRST_LINE; + format.p_left_margin = desc_margin + 4; format.p_right_margin = 4; size_t i = 0; - entry = b_queue_first(&cmd->b_arg); + entry = fx_queue_first(&cmd->c_arg); while (entry) { - struct b_command_arg *arg - = b_unbox(struct b_command_arg, entry, arg_entry); + struct fx_command_arg *arg + = fx_unbox(struct fx_command_arg, entry, arg_entry); - b_string_clear(str); - z__b_get_arg_usage_string(arg, true, str); + fx_string_clear(str); + z__fx_get_arg_usage_string(arg, true, str); - b_tty_puts(OUTPUT_STREAM, 0, " "); - b_tty_puts(OUTPUT_STREAM, 0, b_string_ptr(str)); - unsigned int len - = b_string_get_size( - str, B_STRLEN_IGNORE_ESC | B_STRLEN_IGNORE_MOD) - + 4; + fx_tty_puts(OUTPUT_STREAM, 0, " "); + fx_tty_puts(OUTPUT_STREAM, 0, fx_string_ptr(str)); + unsigned int len = fx_string_get_size( + str, FX_STRLEN_IGNORE_ESC + | FX_STRLEN_IGNORE_MOD) + + 4; while (len < format.p_left_margin) { - b_tty_putc(OUTPUT_STREAM, 0, ' '); + fx_tty_putc(OUTPUT_STREAM, 0, ' '); len++; } - b_string_clear(str); - z__b_get_arg_description(arg, str); + fx_string_clear(str); + z__fx_get_arg_description(arg, str); - b_print_paragraph(b_string_ptr(str), OUTPUT_STREAM, &format); - entry = b_queue_next(entry); + fx_print_paragraph(fx_string_ptr(str), OUTPUT_STREAM, &format); + entry = fx_queue_next(entry); } - b_string_unref(str); + fx_string_unref(str); } -static void print_commands_list(struct b_command *cmd) +static void print_commands_list(struct fx_command *cmd) { - b_tty_puts(OUTPUT_STREAM, 0, "\n" F_YELLOW "COMMANDS:" F_RESET "\n"); + fx_tty_puts(OUTPUT_STREAM, 0, "\n" F_YELLOW "COMMANDS:" F_RESET "\n"); - size_t desb_margin = 0; - b_string *str = b_string_create(); + size_t desc_margin = 0; + fx_string *str = fx_string_create(); - struct b_queue_entry *entry = b_queue_first(&cmd->b_subcommands); + struct fx_queue_entry *entry = fx_queue_first(&cmd->c_subcommands); while (entry) { - struct b_command *sub = b_unbox(struct b_command, entry, b_entry); + struct fx_command *sub + = fx_unbox(struct fx_command, entry, c_entry); - b_string_clear(str); + fx_string_clear(str); get_command_string(sub, str); - size_t len = b_string_get_size( - str, B_STRLEN_IGNORE_ESC | B_STRLEN_IGNORE_MOD) + size_t len = fx_string_get_size( + str, FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD) + 4; - if (len > desb_margin) { - desb_margin = len; + if (len > desc_margin) { + desc_margin = len; } - entry = b_queue_next(entry); + entry = fx_queue_next(entry); } - b_paragraph_format format = {0}; - format.p_flags = B_PARAGRAPH_DONT_INDENT_FIRST_LINE; - format.p_left_margin = desb_margin + 4; + fx_paragraph_format format = {0}; + format.p_flags = FX_PARAGRAPH_DONT_INDENT_FIRST_LINE; + format.p_left_margin = desc_margin + 4; format.p_right_margin = 4; size_t i = 0; - entry = b_queue_first(&cmd->b_subcommands); + entry = fx_queue_first(&cmd->c_subcommands); while (entry) { - struct b_command *sub = b_unbox(struct b_command, entry, b_entry); + struct fx_command *sub + = fx_unbox(struct fx_command, entry, c_entry); - b_string_clear(str); + fx_string_clear(str); get_command_string(sub, str); - b_tty_puts(OUTPUT_STREAM, 0, " "); - b_tty_puts(OUTPUT_STREAM, 0, b_string_ptr(str)); - unsigned int len - = b_string_get_size( - str, B_STRLEN_IGNORE_ESC | B_STRLEN_IGNORE_MOD) - + 4; + fx_tty_puts(OUTPUT_STREAM, 0, " "); + fx_tty_puts(OUTPUT_STREAM, 0, fx_string_ptr(str)); + unsigned int len = fx_string_get_size( + str, FX_STRLEN_IGNORE_ESC + | FX_STRLEN_IGNORE_MOD) + + 4; while (len < format.p_left_margin) { - b_tty_putc(OUTPUT_STREAM, 0, ' '); + fx_tty_putc(OUTPUT_STREAM, 0, ' '); len++; } - b_string_clear(str); + fx_string_clear(str); get_command_description(sub, str); - b_print_paragraph(b_string_ptr(str), OUTPUT_STREAM, &format); + fx_print_paragraph(fx_string_ptr(str), OUTPUT_STREAM, &format); - entry = b_queue_next(entry); + entry = fx_queue_next(entry); } - b_string_unref(str); + fx_string_unref(str); } -struct b_command *b_command_get_subcommand_with_name( - struct b_command *cmd, const char *name) +struct fx_command *fx_command_get_subcommand_with_name( + struct fx_command *cmd, const char *name) { - struct b_queue_entry *entry = b_queue_first(&cmd->b_subcommands); + struct fx_queue_entry *entry = fx_queue_first(&cmd->c_subcommands); while (entry) { - struct b_command *subcmd - = b_unbox(struct b_command, entry, b_entry); - if (!subcmd || !subcmd->b_name) { + struct fx_command *subcmd + = fx_unbox(struct fx_command, entry, c_entry); + if (!subcmd || !subcmd->c_name) { goto skip; } - if (!strcmp(subcmd->b_name, name)) { + if (!strcmp(subcmd->c_name, name)) { return subcmd; } skip: - entry = b_queue_next(entry); + entry = fx_queue_next(entry); } return NULL; } -struct b_command *b_command_get_subcommand_with_long_name( - struct b_command *cmd, const char *long_name) +struct fx_command *fx_command_get_subcommand_with_long_name( + struct fx_command *cmd, const char *long_name) { - struct b_queue_entry *entry = b_queue_first(&cmd->b_subcommands); + struct fx_queue_entry *entry = fx_queue_first(&cmd->c_subcommands); while (entry) { - struct b_command *subcmd - = b_unbox(struct b_command, entry, b_entry); - if (!subcmd || !subcmd->b_long_name) { + struct fx_command *subcmd + = fx_unbox(struct fx_command, entry, c_entry); + if (!subcmd || !subcmd->c_long_name) { goto skip; } - if (!strcmp(subcmd->b_name, long_name)) { + if (!strcmp(subcmd->c_name, long_name)) { return subcmd; } skip: - entry = b_queue_next(entry); + entry = fx_queue_next(entry); } return NULL; } -struct b_command *b_command_get_subcommand_with_short_name( - struct b_command *cmd, char short_name) +struct fx_command *fx_command_get_subcommand_with_short_name( + struct fx_command *cmd, char short_name) { - struct b_queue_entry *entry = b_queue_first(&cmd->b_subcommands); + struct fx_queue_entry *entry = fx_queue_first(&cmd->c_subcommands); while (entry) { - struct b_command *subcmd - = b_unbox(struct b_command, entry, b_entry); - if (!subcmd || !subcmd->b_short_name) { + struct fx_command *subcmd + = fx_unbox(struct fx_command, entry, c_entry); + if (!subcmd || !subcmd->c_short_name) { goto skip; } - if (subcmd->b_short_name == short_name) { + if (subcmd->c_short_name == short_name) { return subcmd; } skip: - entry = b_queue_next(entry); + entry = fx_queue_next(entry); } return NULL; } -struct b_command_option *b_command_get_option_with_long_name( - struct b_command *cmd, const char *long_name) +struct fx_command_option *fx_command_get_option_with_long_name( + struct fx_command *cmd, const char *long_name) { - struct b_queue_entry *entry = b_queue_first(&cmd->b_opt); + struct fx_queue_entry *entry = fx_queue_first(&cmd->c_opt); while (entry) { - struct b_command_option *opt - = b_unbox(struct b_command_option, entry, opt_entry); + struct fx_command_option *opt + = fx_unbox(struct fx_command_option, entry, opt_entry); if (!opt || !opt->opt_long_name) { goto skip; } @@ -862,19 +867,19 @@ struct b_command_option *b_command_get_option_with_long_name( } skip: - entry = b_queue_next(entry); + entry = fx_queue_next(entry); } return NULL; } -struct b_command_option *b_command_get_option_with_short_name( - struct b_command *cmd, char short_name) +struct fx_command_option *fx_command_get_option_with_short_name( + struct fx_command *cmd, char short_name) { - struct b_queue_entry *entry = b_queue_first(&cmd->b_opt); + struct fx_queue_entry *entry = fx_queue_first(&cmd->c_opt); while (entry) { - struct b_command_option *opt - = b_unbox(struct b_command_option, entry, opt_entry); + struct fx_command_option *opt + = fx_unbox(struct fx_command_option, entry, opt_entry); if (!opt || !opt->opt_long_name) { goto skip; } @@ -884,121 +889,121 @@ struct b_command_option *b_command_get_option_with_short_name( } skip: - entry = b_queue_next(entry); + entry = fx_queue_next(entry); } return NULL; } -struct b_command_option *b_command_get_option_with_id( - struct b_command *cmd, unsigned int id) +struct fx_command_option *fx_command_get_option_with_id( + struct fx_command *cmd, unsigned int id) { - struct b_queue_entry *entry = b_queue_first(&cmd->b_opt); + struct fx_queue_entry *entry = fx_queue_first(&cmd->c_opt); while (entry) { - struct b_command_option *opt - = b_unbox(struct b_command_option, entry, opt_entry); + struct fx_command_option *opt + = fx_unbox(struct fx_command_option, entry, opt_entry); if (opt->opt_id == id) { return opt; } - entry = b_queue_next(entry); + entry = fx_queue_next(entry); } return NULL; } -struct b_command_arg *b_command_get_arg_with_id( - struct b_command *cmd, unsigned int id) +struct fx_command_arg *fx_command_get_arg_with_id( + struct fx_command *cmd, unsigned int id) { - struct b_queue_entry *entry = b_queue_first(&cmd->b_arg); + struct fx_queue_entry *entry = fx_queue_first(&cmd->c_arg); while (entry) { - struct b_command_arg *arg - = b_unbox(struct b_command_arg, entry, arg_entry); + struct fx_command_arg *arg + = fx_unbox(struct fx_command_arg, entry, arg_entry); if (arg->arg_id == id) { return arg; } - entry = b_queue_next(entry); + entry = fx_queue_next(entry); } return NULL; } -static void print_usage(struct b_command *cmd, struct b_arglist *args) +static void print_usage(struct fx_command *cmd, struct fx_arglist *args) { - b_paragraph_format format = {0}; + fx_paragraph_format format = {0}; format.p_left_margin = format.p_right_margin = 4; - b_tty_puts(OUTPUT_STREAM, 0, F_YELLOW "USAGE:" F_RESET "\n"); + fx_tty_puts(OUTPUT_STREAM, 0, F_YELLOW "USAGE:" F_RESET "\n"); - if (b_queue_empty(&cmd->b_usage)) { - b_string *usage - = z__b_command_default_usage_string(cmd, NULL, args); - b_print_paragraph(b_string_ptr(usage), OUTPUT_STREAM, &format); - b_string_unref(usage); + if (fx_queue_empty(&cmd->c_usage)) { + fx_string *usage + = z__fx_command_default_usage_string(cmd, NULL, args); + fx_print_paragraph(fx_string_ptr(usage), OUTPUT_STREAM, &format); + fx_string_unref(usage); return; } - b_string *str = b_string_create(); - struct b_queue_entry *entry = b_queue_first(&cmd->b_usage); + fx_string *str = fx_string_create(); + struct fx_queue_entry *entry = fx_queue_first(&cmd->c_usage); while (entry) { - struct b_command_usage *usage - = b_unbox(struct b_command_usage, entry, u_entry); + struct fx_command_usage *usage + = fx_unbox(struct fx_command_usage, entry, u_entry); - b_string_clear(str); + fx_string_clear(str); get_usage_string(cmd, args, usage, str); - b_print_paragraph(b_string_ptr(str), OUTPUT_STREAM, &format); + fx_print_paragraph(fx_string_ptr(str), OUTPUT_STREAM, &format); - entry = b_queue_next(entry); + entry = fx_queue_next(entry); } - b_string_unref(str); + fx_string_unref(str); } -static void print_help(struct b_command *cmd, struct b_arglist *args) +static void print_help(struct fx_command *cmd, struct fx_arglist *args) { - b_paragraph_format format = {0}; + fx_paragraph_format format = {0}; - if (!cmd->b_parent) { - b_tty_printf( - OUTPUT_STREAM, 0, F_GREEN "%s" F_RESET "\n", cmd->b_name); + if (!cmd->c_parent) { + fx_tty_printf( + OUTPUT_STREAM, 0, F_GREEN "%s" F_RESET "\n", cmd->c_name); } - if (cmd->b_description) { - b_print_paragraph(cmd->b_description, OUTPUT_STREAM, &format); + if (cmd->c_description) { + fx_print_paragraph(cmd->c_description, OUTPUT_STREAM, &format); } - b_tty_putc(OUTPUT_STREAM, 0, '\n'); + fx_tty_putc(OUTPUT_STREAM, 0, '\n'); print_usage(cmd, args); - if (!b_queue_empty(&cmd->b_opt)) { + if (!fx_queue_empty(&cmd->c_opt)) { print_options_list(cmd); } - if (!b_queue_empty(&cmd->b_arg)) { + if (!fx_queue_empty(&cmd->c_arg)) { print_args_list(cmd); } - if (!b_queue_empty(&cmd->b_subcommands)) { + if (!fx_queue_empty(&cmd->c_subcommands)) { print_commands_list(cmd); } } -static int execute_command(struct b_command *cmd, struct b_arglist *args) +static int execute_command(struct fx_command *cmd, struct fx_arglist *args) { if (!cmd) { return -1; } - size_t nr_items = b_arglist_get_count( - args, B_COMMAND_INVALID_ID, B_COMMAND_INVALID_ID); - size_t nr_help = b_arglist_get_count( - args, B_COMMAND_OPTION_HELP, B_COMMAND_INVALID_ID); + size_t nr_items = fx_arglist_get_count( + args, FX_COMMAND_INVALID_ID, FX_COMMAND_INVALID_ID); + size_t nr_help = fx_arglist_get_count( + args, FX_COMMAND_OPTION_HELP, FX_COMMAND_INVALID_ID); - if ((cmd->b_flags & B_COMMAND_SHOW_HELP_BY_DEFAULT) && nr_items == 0) { + if ((cmd->c_flags & FX_COMMAND_SHOW_HELP_BY_DEFAULT) && nr_items == 0) { print_help(cmd, args); return 0; } @@ -1008,69 +1013,69 @@ static int execute_command(struct b_command *cmd, struct b_arglist *args) return 0; } - if (cmd->b_callback) { - return cmd->b_callback(cmd, args, NULL); + if (cmd->c_callback) { + return cmd->c_callback(cmd, args, NULL); } return -1; } -static b_status add_subcommand(struct b_command *parent, struct b_command *child) +static fx_status add_subcommand(struct fx_command *parent, struct fx_command *child) { - b_queue_push_back(&parent->b_subcommands, &child->b_entry); - child->b_parent = parent; + fx_queue_push_back(&parent->c_subcommands, &child->c_entry); + child->c_parent = parent; - return B_SUCCESS; + return FX_SUCCESS; } -static int resolve_command_parents(struct b_btree *commands) +static int resolve_command_parents(struct fx_bst *commands) { - struct b_btree_node *node = b_btree_first(commands); + struct fx_bst_node *node = fx_bst_first(commands); while (node) { - struct b_command *cmd = b_unbox(struct b_command, node, b_node); + struct fx_command *cmd = fx_unbox(struct fx_command, node, c_node); - if (cmd->b_parent_id == B_COMMAND_INVALID_ID) { + if (cmd->c_parent_id == FX_COMMAND_INVALID_ID) { goto skip; } - cmd->b_parent = get_command(commands, cmd->b_parent_id); - if (!cmd->b_parent) { + cmd->c_parent = get_command(commands, cmd->c_parent_id); + if (!cmd->c_parent) { return -1; } - add_subcommand(cmd->b_parent, cmd); + add_subcommand(cmd->c_parent, cmd); skip: - node = b_btree_next(node); + node = fx_bst_next(node); } return 0; } -int b_command_dispatch(unsigned int cmd_id, int argc, const char **argv) +int fx_command_dispatch(unsigned int cmd_id, int argc, const char **argv) { if (resolve_command_parents(&command_list) != 0) { return -1; } - struct b_command *cmd = get_command(&command_list, cmd_id); + struct fx_command *cmd = get_command(&command_list, cmd_id); if (!cmd) { return -1; } - struct b_arglist *args = b_arglist_create(); + struct fx_arglist *args = fx_arglist_create(); args->list_argc = argc; args->list_argv = argv; args->list_command = cmd; - b_status status = b_arglist_parse(args, &cmd, argc, argv); - if (B_ERR(status)) { - b_arglist_destroy(args); + fx_status status = fx_arglist_parse(args, &cmd, argc, argv); + if (FX_ERR(status)) { + fx_arglist_destroy(args); return -1; } int ret = execute_command(cmd, args); - b_arglist_destroy(args); + fx_arglist_destroy(args); // print_help(cmd); return ret; } diff --git a/cmd/command.h b/cmd/command.h index edfa246..e900dc9 100644 --- a/cmd/command.h +++ b/cmd/command.h @@ -1,10 +1,10 @@ -#ifndef _B_COMMAND_H_ -#define _B_COMMAND_H_ +#ifndef _FX_COMMAND_H_ +#define _FX_COMMAND_H_ -#include -#include -#include -#include +#include +#include +#include +#include #define F_RED "[bright_red]" #define F_GREEN "[bright_green]" @@ -20,132 +20,132 @@ enum cmd_string_flags { CMD_STR_DIRECT_USAGE = 0x02u, }; -enum b_command_usage_entry_type { +enum fx_command_usage_entry_type { CMD_USAGE_NONE = 0, CMD_USAGE_ARG, CMD_USAGE_OPT, CMD_USAGE_COMMAND, }; -struct b_command { - unsigned int b_id; - unsigned int b_parent_id; - struct b_command *b_parent; - enum b_command_flags b_flags; +struct fx_command { + unsigned int c_id; + unsigned int c_parent_id; + struct fx_command *c_parent; + enum fx_command_flags c_flags; - char *b_name; - char *b_long_name; - char b_short_name; - char *b_description; - struct b_queue b_opt; - struct b_queue b_arg; - struct b_queue b_subcommands; - struct b_queue b_usage; + char *c_name; + char *c_long_name; + char c_short_name; + char *c_description; + struct fx_queue c_opt; + struct fx_queue c_arg; + struct fx_queue c_subcommands; + struct fx_queue c_usage; - b_command_callback b_callback; - struct b_queue_entry b_entry; - struct b_btree_node b_node; + fx_command_callback c_callback; + struct fx_queue_entry c_entry; + struct fx_bst_node c_node; }; -struct b_command_usage_entry { - struct b_queue_entry e_entry; - enum b_command_usage_entry_type e_type; +struct fx_command_usage_entry { + struct fx_queue_entry e_entry; + enum fx_command_usage_entry_type e_type; union { - struct b_command_option *e_opt; - struct b_command_arg *e_arg; + struct fx_command_option *e_opt; + struct fx_command_arg *e_arg; unsigned int e_cmd_id; }; }; -struct b_command_usage { - b_command_usage_flags u_flags; - struct b_queue_entry u_entry; +struct fx_command_usage { + fx_command_usage_flags u_flags; + struct fx_queue_entry u_entry; - struct b_queue u_parts; + struct fx_queue u_parts; }; -struct b_command_option { +struct fx_command_option { unsigned int opt_id; char *opt_long_name; char opt_short_name; char *opt_description; - // b_command_arg_value_count arg_nr_values; + // fx_command_arg_value_count arg_nr_values; // char **arg_allowed_values; - struct b_queue opt_args; - struct b_queue_entry opt_entry; + struct fx_queue opt_args; + struct fx_queue_entry opt_entry; }; -struct b_command_arg { +struct fx_command_arg { unsigned int arg_id; char *arg_name; char *arg_description; - b_command_arg_value_count arg_nr_values; + fx_command_arg_value_count arg_nr_values; char **arg_allowed_values; - struct b_queue_entry arg_entry; + struct fx_queue_entry arg_entry; }; -struct b_arglist_option { +struct fx_arglist_option { unsigned int opt_id; - struct b_btree opt_values; - struct b_btree_node opt_node; + struct fx_bst opt_values; + struct fx_bst_node opt_node; }; -struct b_arglist { +struct fx_arglist { size_t list_argc; const char **list_argv; unsigned int list_argv_last_command; - struct b_command *list_command; - struct b_btree list_options; + struct fx_command *list_command; + struct fx_bst list_options; }; -BLUE_API struct b_command *b_command_get_subcommand_with_name( - struct b_command *cmd, const char *name); -BLUE_API struct b_command *b_command_get_subcommand_with_long_name( - struct b_command *cmd, const char *long_name); -BLUE_API struct b_command *b_command_get_subcommand_with_short_name( - struct b_command *cmd, char short_name); +FX_API struct fx_command *fx_command_get_subcommand_with_name( + struct fx_command *cmd, const char *name); +FX_API struct fx_command *fx_command_get_subcommand_with_long_name( + struct fx_command *cmd, const char *long_name); +FX_API struct fx_command *fx_command_get_subcommand_with_short_name( + struct fx_command *cmd, char short_name); -BLUE_API struct b_command_option *b_command_get_option_with_long_name( - struct b_command *cmd, const char *long_name); -BLUE_API struct b_command_option *b_command_get_option_with_short_name( - struct b_command *cmd, char short_name); -BLUE_API struct b_command_option *b_command_get_option_with_id( - struct b_command *cmd, unsigned int id); -BLUE_API struct b_command_arg *b_command_get_arg_with_id( - struct b_command *cmd, unsigned int id); +FX_API struct fx_command_option *fx_command_get_option_with_long_name( + struct fx_command *cmd, const char *long_name); +FX_API struct fx_command_option *fx_command_get_option_with_short_name( + struct fx_command *cmd, char short_name); +FX_API struct fx_command_option *fx_command_get_option_with_id( + struct fx_command *cmd, unsigned int id); +FX_API struct fx_command_arg *fx_command_get_arg_with_id( + struct fx_command *cmd, unsigned int id); -BLUE_API struct b_command_arg *b_command_option_get_arg_with_id( - struct b_command_option *opt, unsigned int id); +FX_API struct fx_command_arg *fx_command_option_get_arg_with_id( + struct fx_command_option *opt, unsigned int id); -BLUE_API struct b_command_option *b_command_option_create(void); -BLUE_API void b_command_option_destroy(struct b_command_option *opt); +FX_API struct fx_command_option *fx_command_option_create(void); +FX_API void fx_command_option_destroy(struct fx_command_option *opt); -BLUE_API struct b_command_arg *b_command_arg_create(void); -BLUE_API void b_command_arg_destroy(struct b_command_arg *arg); +FX_API struct fx_command_arg *fx_command_arg_create(void); +FX_API void fx_command_arg_destroy(struct fx_command_arg *arg); -BLUE_API struct b_arglist *b_arglist_create(void); -BLUE_API b_status b_arglist_parse( - struct b_arglist *args, struct b_command **cmd, int argc, +FX_API struct fx_arglist *fx_arglist_create(void); +FX_API fx_status fx_arglist_parse( + struct fx_arglist *args, struct fx_command **cmd, int argc, const char **argv); -BLUE_API void b_arglist_destroy(struct b_arglist *args); +FX_API void fx_arglist_destroy(struct fx_arglist *args); -BLUE_API b_string *z__b_command_default_usage_string( - struct b_command *cmd, struct b_command_option *with_opt, - const struct b_arglist *args); +FX_API fx_string *z__fx_command_default_usage_string( + struct fx_command *cmd, struct fx_command_option *with_opt, + const struct fx_arglist *args); -BLUE_API void z__b_get_arg_usage_string( - struct b_command_arg *arg, bool colour, b_string *out); -BLUE_API void z__b_get_arg_description(struct b_command_arg *arg, b_string *out); +FX_API void z__fx_get_arg_usage_string( + struct fx_command_arg *arg, bool colour, fx_string *out); +FX_API void z__fx_get_arg_description(struct fx_command_arg *arg, fx_string *out); -BLUE_API void z__b_get_option_usage_string( - struct b_command_option *opt, enum cmd_string_flags flags, b_string *out); -BLUE_API void z__b_get_option_description( - struct b_command_option *opt, b_string *out); +FX_API void z__fx_get_option_usage_string( + struct fx_command_option *opt, enum cmd_string_flags flags, fx_string *out); +FX_API void z__fx_get_option_description( + struct fx_command_option *opt, fx_string *out); #endif diff --git a/cmd/include/blue/cmd.h b/cmd/include/blue/cmd.h deleted file mode 100644 index f769854..0000000 --- a/cmd/include/blue/cmd.h +++ /dev/null @@ -1,289 +0,0 @@ -#ifndef BLUE_CMD_H_ -#define BLUE_CMD_H_ - -#include -#include -#include -#include -#include -#include -#include - -#define b_arglist_foreach(it, q) \ - for (int z__b_unique_name() = b_arglist_iterator_begin( \ - q, B_COMMAND_INVALID_ID, B_COMMAND_INVALID_ID, (it)); \ - b_arglist_iterator_is_valid(it); b_arglist_iterator_next(it)) - -#define b_arglist_foreach_filtered(it, q, opt_id, arg_id) \ - for (int z__b_unique_name() \ - = b_arglist_iterator_begin(q, opt_id, arg_id, (it)); \ - b_arglist_iterator_is_valid(it); b_arglist_iterator_next(it)) - -#define b_arglist_option_foreach(it, q) \ - for (int z__b_unique_name() \ - = b_arglist_option_iterator_begin(q, B_COMMAND_INVALID_ID, (it)); \ - b_arglist_option_iterator_is_valid(it); \ - b_arglist_option_iterator_next(it)) - -#define b_arglist_option_foreach_filtered(it, q, opt_id) \ - for (int z__b_unique_name() \ - = b_arglist_option_iterator_begin(q, opt_id, (it)); \ - b_arglist_option_iterator_is_valid(it); \ - b_arglist_option_iterator_next(it)) - -#define B_COMMAND(id, parent_id) \ - static b_command *command_##id = NULL; \ - static void __init_##id( \ - b_command *, b_command_option *, b_command_arg *, \ - b_command_usage *); \ - B_INIT(init_##id) \ - { \ - command_##id = b_command_create(id); \ - if ((parent_id) != B_COMMAND_INVALID_ID) { \ - b_command_set_parent(command_##id, parent_id); \ - } \ - __init_##id(command_##id, NULL, NULL, NULL); \ - b_command_register(command_##id); \ - } \ - static void __init_##id( \ - b_command *this_cmd, b_command_option *this_opt, \ - b_command_arg *this_arg, b_command_usage *this_usage) - -#define B_COMMAND_NAME(name) b_command_set_name(this_cmd, (name)) -#define B_COMMAND_LONG_NAME(name) b_command_set_long_name(this_cmd, (name)) -#define B_COMMAND_SHORT_NAME(name) b_command_set_short_name(this_cmd, (name)) -#define B_COMMAND_DESC(desc) b_command_set_description(this_cmd, (desc)) -#define B_COMMAND_FLAGS(flags) b_command_set_flags(this_cmd, (flags)) -#define B_COMMAND_FUNCTION(fn) b_command_set_callback(this_cmd, (fn)) - -#define B_COMMAND_OPTION(id) \ - b_command_option *opt_##id = b_command_add_option(this_cmd, (id)); \ - this_opt = opt_##id; \ - if (this_opt) - -#define B_COMMAND_OPTION_GEN(id) \ - b_command_option *z__b_unique_name() \ - = b_command_add_option(this_cmd, (id)); \ - this_opt = z__b_unique_name(); \ - if (this_opt) - -#define B_COMMAND_HELP_OPTION() \ - do { \ - b_command_option *opt \ - = b_command_add_option(this_cmd, B_COMMAND_OPTION_HELP); \ - b_command_option_set_description(opt, "Show this help message"); \ - b_command_option_set_short_name(opt, 'h'); \ - b_command_option_set_long_name(opt, "help"); \ - } while (0) - -#define B_OPTION_LONG_NAME(name) \ - b_command_option_set_long_name(this_opt, (name)) - -#define B_OPTION_SHORT_NAME(name) \ - b_command_option_set_short_name(this_opt, (name)) - -#define B_OPTION_DESC(desc) b_command_option_set_description(this_opt, (desc)) - -#define B_OPTION_ARG(id) \ - b_command_arg *arg_##id = b_command_option_add_arg(this_opt, (id)); \ - this_arg = arg_##id; \ - if (this_arg) - -#define B_COMMAND_ARG(id) \ - b_command_arg *arg_##id = b_command_add_arg(this_cmd, (id)); \ - this_arg = arg_##id; \ - if (this_arg) - -#define B_ARG_NAME(name) b_command_arg_set_name(this_arg, (name)) - -#define B_ARG_DESC(desc) b_command_arg_set_description(this_arg, (desc)) - -#define B_ARG_NR_VALUES(nr_values) \ - b_command_arg_set_nr_values(this_arg, (nr_values)) - -#define B_ARG_ALLOWED_VALUES(...) \ - static const char *allowed_values[] = { \ - __VA_ARGS__, \ - NULL, \ - }; \ - b_command_arg_set_allowed_values(this_arg, allowed_values) - -#define B_COMMAND_USAGE() \ - b_command_usage *z__b_unique_name() = b_command_add_usage(this_cmd); \ - this_usage = z__b_unique_name(); \ - if (this_usage) - -#define B_COMMAND_USAGE_COMMAND(cmd_id) \ - b_command_usage_add_command(this_usage, cmd_id) - -#define B_COMMAND_USAGE_COMMAND_PLACEHOLDER() \ - b_command_usage_add_command(this_usage, B_COMMAND_INVALID_ID) - -#define B_COMMAND_USAGE_OPT(opt_id) \ - b_command_usage_add_option(this_usage, opt_##opt_id) - -#define B_COMMAND_USAGE_OPT_PLACEHOLDER() \ - b_command_usage_add_option(this_usage, NULL) - -#define B_COMMAND_USAGE_ARG(opt_id) \ - b_command_usage_add_arg(this_usage, arg_##opt_id) - -#define B_COMMAND_USAGE_ARG_PLACEHOLDER() \ - b_command_usage_add_arg(this_usage, NULL) - -#define B_COMMAND_OPTION_HELP ((uintptr_t)0xF0000001) -#define B_COMMAND_INVALID_ID ((uintptr_t)0xFFFFFFFF) - -typedef enum b_command_arg_value_count { - B_ARG_0_OR_1_VALUES = -1, - B_ARG_0_OR_MORE_VALUES = -2, - B_ARG_1_OR_MORE_VALUES = -3, -} b_command_arg_value_count; - -typedef enum b_command_arg_type { - B_COMMAND_ARG_NONE = 0, - B_COMMAND_ARG_STRING, - B_COMMAND_ARG_SIGNED_INT, - B_COMMAND_ARG_UNSIGNED_INT, - - B_COMMAND_ARG_INT = B_COMMAND_ARG_SIGNED_INT, -} b_command_arg_type; - -typedef enum b_command_flags { - B_COMMAND_SHOW_HELP_BY_DEFAULT = 0x01u, -} b_command_flags; - -typedef enum b_command_usage_flags { - B_COMMAND_USAGE_SHOW_OPTIONS = 0x01u, -} b_command_usage_flags; - -typedef struct b_arglist_value { - unsigned int val_id; - b_command_arg_type val_type; - struct b_btree_node val_node; - - union { - char *val_str; - long long val_int; - unsigned long long val_uint; - }; -} b_arglist_value; - -typedef struct b_arglist_iterator { - size_t i; - - unsigned int opt_id; - struct b_arglist_value *value; - - b_btree_node *_opt_it, *_arg_it; - unsigned int _opt_filter, _arg_filter; -} b_arglist_iterator; - -typedef struct b_arglist_option_iterator { - size_t i; - - unsigned int opt_id; - struct b_arglist_option *opt; - - b_btree_node *_opt_it; - unsigned int _opt_filter; -} b_arglist_option_iterator; - -typedef struct b_command b_command; -typedef struct b_command_option b_command_option; -typedef struct b_command_arg b_command_arg; -typedef struct b_command_usage b_command_usage; -typedef struct b_arglist b_arglist; -typedef struct b_arglist_option b_arglist_option; - -typedef int (*b_command_callback)( - const b_command *, const b_arglist *, const b_array *); - -BLUE_API b_command *b_command_create(unsigned int id); -BLUE_API void b_command_destroy(b_command *cmd); -BLUE_API b_status b_command_register(b_command *cmd); -BLUE_API int b_command_dispatch(unsigned int cmd_id, int argc, const char **argv); - -BLUE_API b_status b_command_set_name(b_command *cmd, const char *name); -BLUE_API b_status b_command_set_long_name(b_command *cmd, const char *name); -BLUE_API b_status b_command_set_short_name(b_command *cmd, char name); -BLUE_API b_status b_command_set_flags(b_command *cmd, b_command_flags flags); -BLUE_API b_status b_command_set_description(b_command *cmd, const char *description); -BLUE_API b_status b_command_set_callback( - b_command *cmd, b_command_callback callback); -BLUE_API b_status b_command_set_parent(b_command *cmd, unsigned int parent_id); -BLUE_API b_command_option *b_command_add_option(b_command *cmd, int id); -BLUE_API b_command_arg *b_command_add_arg(b_command *cmd, int id); -BLUE_API b_command_usage *b_command_add_usage(b_command *cmd); - -BLUE_API const b_command_option *b_command_get_option(const b_command *cmd, int id); - -BLUE_API const char *b_command_option_get_long_name(const b_command_option *opt); -BLUE_API char b_command_option_get_short_name(const b_command_option *opt); -BLUE_API const char *b_command_option_get_description(b_command_option *opt); -BLUE_API b_status b_command_option_set_long_name( - b_command_option *opt, const char *name); -BLUE_API b_status b_command_option_set_short_name(b_command_option *opt, char name); -BLUE_API b_status b_command_option_set_description( - b_command_option *opt, const char *description); -BLUE_API b_command_arg *b_command_option_add_arg(b_command_option *opt, int id); - -BLUE_API b_status b_command_arg_set_name(b_command_arg *arg, const char *name); -BLUE_API b_status b_command_arg_set_description( - b_command_arg *arg, const char *description); -BLUE_API b_status b_command_arg_set_nr_values( - b_command_arg *arg, b_command_arg_value_count nr_values); -BLUE_API b_status b_command_arg_set_allowed_values( - b_command_arg *arg, const char **allowed_values); - -BLUE_API b_status b_command_usage_add_option( - b_command_usage *usage, b_command_option *opt); -BLUE_API b_status b_command_usage_add_arg( - b_command_usage *usage, b_command_arg *opt); -BLUE_API b_status b_command_usage_add_command( - b_command_usage *usage, unsigned int cmd_id); - -BLUE_API b_status b_arglist_get_string( - const b_arglist *args, unsigned int opt_id, unsigned int arg_id, - unsigned int index, const char **out); -BLUE_API b_status b_arglist_get_int( - const b_arglist *args, unsigned int opt_id, unsigned int arg_id, - unsigned int index, long long *out); -BLUE_API b_status b_arglist_get_uint( - const b_arglist *args, unsigned int opt_id, unsigned int arg_id, - unsigned int index, unsigned long long *out); -BLUE_API b_status b_arglist_get_option( - const b_arglist *args, unsigned int opt_id, unsigned int index, - b_arglist_option **out); -BLUE_API size_t b_arglist_get_count( - const b_arglist *args, unsigned int opt_id, unsigned int arg_id); - -BLUE_API b_status b_arglist_report_missing_option( - const b_arglist *args, unsigned int opt_id); -BLUE_API b_status b_arglist_report_unexpected_arg( - const b_arglist *args, const char *value); -BLUE_API b_status b_arglist_report_invalid_arg_value( - const b_arglist *args, unsigned int opt_id, unsigned int arg_id, - const char *value); -BLUE_API b_status b_arglist_report_missing_args( - const b_arglist *args, unsigned int opt_id, unsigned int arg_id, - size_t nr_supplied); - -BLUE_API b_status b_arglist_option_get_value( - const b_arglist_option *opt, unsigned int arg_id, unsigned int index, - b_arglist_value **out); - -BLUE_API int b_arglist_iterator_begin( - const b_arglist *args, unsigned int opt_filter, unsigned int arg_filter, - b_arglist_iterator *it); -BLUE_API bool b_arglist_iterator_next(b_arglist_iterator *it); -BLUE_API bool b_arglist_iterator_is_valid(const b_arglist_iterator *it); - -BLUE_API int b_arglist_option_iterator_begin( - const b_arglist *args, unsigned int opt_filter, - b_arglist_option_iterator *it); -BLUE_API bool b_arglist_option_iterator_next(b_arglist_option_iterator *it); -BLUE_API bool b_arglist_option_iterator_is_valid( - const b_arglist_option_iterator *it); - -#endif diff --git a/cmd/include/fx/cmd.h b/cmd/include/fx/cmd.h new file mode 100644 index 0000000..35cda05 --- /dev/null +++ b/cmd/include/fx/cmd.h @@ -0,0 +1,289 @@ +#ifndef FX_CMD_H_ +#define FX_CMD_H_ + +#include +#include +#include +#include +#include +#include +#include + +#define fx_arglist_foreach(it, q) \ + for (int z__fx_unique_name() = fx_arglist_iterator_begin( \ + q, FX_COMMAND_INVALID_ID, FX_COMMAND_INVALID_ID, (it)); \ + fx_arglist_iterator_is_valid(it); fx_arglist_iterator_next(it)) + +#define fx_arglist_foreach_filtered(it, q, opt_id, arg_id) \ + for (int z__fx_unique_name() \ + = fx_arglist_iterator_begin(q, opt_id, arg_id, (it)); \ + fx_arglist_iterator_is_valid(it); fx_arglist_iterator_next(it)) + +#define fx_arglist_option_foreach(it, q) \ + for (int z__fx_unique_name() \ + = fx_arglist_option_iterator_begin(q, FX_COMMAND_INVALID_ID, (it)); \ + fx_arglist_option_iterator_is_valid(it); \ + fx_arglist_option_iterator_next(it)) + +#define fx_arglist_option_foreach_filtered(it, q, opt_id) \ + for (int z__fx_unique_name() \ + = fx_arglist_option_iterator_begin(q, opt_id, (it)); \ + fx_arglist_option_iterator_is_valid(it); \ + fx_arglist_option_iterator_next(it)) + +#define FX_COMMAND(id, parent_id) \ + static fx_command *command_##id = NULL; \ + static void __init_##id( \ + fx_command *, fx_command_option *, fx_command_arg *, \ + fx_command_usage *); \ + FX_INIT(init_##id) \ + { \ + command_##id = fx_command_create(id); \ + if ((parent_id) != FX_COMMAND_INVALID_ID) { \ + fx_command_set_parent(command_##id, parent_id); \ + } \ + __init_##id(command_##id, NULL, NULL, NULL); \ + fx_command_register(command_##id); \ + } \ + static void __init_##id( \ + fx_command *this_cmd, fx_command_option *this_opt, \ + fx_command_arg *this_arg, fx_command_usage *this_usage) + +#define FX_COMMAND_NAME(name) fx_command_set_name(this_cmd, (name)) +#define FX_COMMAND_LONG_NAME(name) fx_command_set_long_name(this_cmd, (name)) +#define FX_COMMAND_SHORT_NAME(name) fx_command_set_short_name(this_cmd, (name)) +#define FX_COMMAND_DESC(desc) fx_command_set_description(this_cmd, (desc)) +#define FX_COMMAND_FLAGS(flags) fx_command_set_flags(this_cmd, (flags)) +#define FX_COMMAND_FUNCTION(fn) fx_command_set_callback(this_cmd, (fn)) + +#define FX_COMMAND_OPTION(id) \ + fx_command_option *opt_##id = fx_command_add_option(this_cmd, (id)); \ + this_opt = opt_##id; \ + if (this_opt) + +#define FX_COMMAND_OPTION_GEN(id) \ + fx_command_option *z__fx_unique_name() \ + = fx_command_add_option(this_cmd, (id)); \ + this_opt = z__fx_unique_name(); \ + if (this_opt) + +#define FX_COMMAND_HELP_OPTION() \ + do { \ + fx_command_option *opt \ + = fx_command_add_option(this_cmd, FX_COMMAND_OPTION_HELP); \ + fx_command_option_set_description(opt, "Show this help message"); \ + fx_command_option_set_short_name(opt, 'h'); \ + fx_command_option_set_long_name(opt, "help"); \ + } while (0) + +#define FX_OPTION_LONG_NAME(name) \ + fx_command_option_set_long_name(this_opt, (name)) + +#define FX_OPTION_SHORT_NAME(name) \ + fx_command_option_set_short_name(this_opt, (name)) + +#define FX_OPTION_DESC(desc) fx_command_option_set_description(this_opt, (desc)) + +#define FX_OPTION_ARG(id) \ + fx_command_arg *arg_##id = fx_command_option_add_arg(this_opt, (id)); \ + this_arg = arg_##id; \ + if (this_arg) + +#define FX_COMMAND_ARG(id) \ + fx_command_arg *arg_##id = fx_command_add_arg(this_cmd, (id)); \ + this_arg = arg_##id; \ + if (this_arg) + +#define FX_ARG_NAME(name) fx_command_arg_set_name(this_arg, (name)) + +#define FX_ARG_DESC(desc) fx_command_arg_set_description(this_arg, (desc)) + +#define FX_ARG_NR_VALUES(nr_values) \ + fx_command_arg_set_nr_values(this_arg, (nr_values)) + +#define FX_ARG_ALLOWED_VALUES(...) \ + static const char *allowed_values[] = { \ + __VA_ARGS__, \ + NULL, \ + }; \ + fx_command_arg_set_allowed_values(this_arg, allowed_values) + +#define FX_COMMAND_USAGE() \ + fx_command_usage *z__fx_unique_name() = fx_command_add_usage(this_cmd); \ + this_usage = z__fx_unique_name(); \ + if (this_usage) + +#define FX_COMMAND_USAGE_COMMAND(cmd_id) \ + fx_command_usage_add_command(this_usage, cmd_id) + +#define FX_COMMAND_USAGE_COMMAND_PLACEHOLDER() \ + fx_command_usage_add_command(this_usage, FX_COMMAND_INVALID_ID) + +#define FX_COMMAND_USAGE_OPT(opt_id) \ + fx_command_usage_add_option(this_usage, opt_##opt_id) + +#define FX_COMMAND_USAGE_OPT_PLACEHOLDER() \ + fx_command_usage_add_option(this_usage, NULL) + +#define FX_COMMAND_USAGE_ARG(opt_id) \ + fx_command_usage_add_arg(this_usage, arg_##opt_id) + +#define FX_COMMAND_USAGE_ARG_PLACEHOLDER() \ + fx_command_usage_add_arg(this_usage, NULL) + +#define FX_COMMAND_OPTION_HELP ((uintptr_t)0xF0000001) +#define FX_COMMAND_INVALID_ID ((uintptr_t)0xFFFFFFFF) + +typedef enum fx_command_arg_value_count { + FX_ARG_0_OR_1_VALUES = -1, + FX_ARG_0_OR_MORE_VALUES = -2, + FX_ARG_1_OR_MORE_VALUES = -3, +} fx_command_arg_value_count; + +typedef enum fx_command_arg_type { + FX_COMMAND_ARG_NONE = 0, + FX_COMMAND_ARG_STRING, + FX_COMMAND_ARG_SIGNED_INT, + FX_COMMAND_ARG_UNSIGNED_INT, + + FX_COMMAND_ARG_INT = FX_COMMAND_ARG_SIGNED_INT, +} fx_command_arg_type; + +typedef enum fx_command_flags { + FX_COMMAND_SHOW_HELP_BY_DEFAULT = 0x01u, +} fx_command_flags; + +typedef enum fx_command_usage_flags { + FX_COMMAND_USAGE_SHOW_OPTIONS = 0x01u, +} fx_command_usage_flags; + +typedef struct fx_arglist_value { + unsigned int val_id; + fx_command_arg_type val_type; + struct fx_bst_node val_node; + + union { + char *val_str; + long long val_int; + unsigned long long val_uint; + }; +} fx_arglist_value; + +typedef struct fx_arglist_iterator { + size_t i; + + unsigned int opt_id; + struct fx_arglist_value *value; + + fx_bst_node *_opt_it, *_arg_it; + unsigned int _opt_filter, _arg_filter; +} fx_arglist_iterator; + +typedef struct fx_arglist_option_iterator { + size_t i; + + unsigned int opt_id; + struct fx_arglist_option *opt; + + fx_bst_node *_opt_it; + unsigned int _opt_filter; +} fx_arglist_option_iterator; + +typedef struct fx_command fx_command; +typedef struct fx_command_option fx_command_option; +typedef struct fx_command_arg fx_command_arg; +typedef struct fx_command_usage fx_command_usage; +typedef struct fx_arglist fx_arglist; +typedef struct fx_arglist_option fx_arglist_option; + +typedef int (*fx_command_callback)( + const fx_command *, const fx_arglist *, const fx_array *); + +FX_API fx_command *fx_command_create(unsigned int id); +FX_API void fx_command_destroy(fx_command *cmd); +FX_API fx_status fx_command_register(fx_command *cmd); +FX_API int fx_command_dispatch(unsigned int cmd_id, int argc, const char **argv); + +FX_API fx_status fx_command_set_name(fx_command *cmd, const char *name); +FX_API fx_status fx_command_set_long_name(fx_command *cmd, const char *name); +FX_API fx_status fx_command_set_short_name(fx_command *cmd, char name); +FX_API fx_status fx_command_set_flags(fx_command *cmd, fx_command_flags flags); +FX_API fx_status fx_command_set_description(fx_command *cmd, const char *description); +FX_API fx_status fx_command_set_callback( + fx_command *cmd, fx_command_callback callback); +FX_API fx_status fx_command_set_parent(fx_command *cmd, unsigned int parent_id); +FX_API fx_command_option *fx_command_add_option(fx_command *cmd, int id); +FX_API fx_command_arg *fx_command_add_arg(fx_command *cmd, int id); +FX_API fx_command_usage *fx_command_add_usage(fx_command *cmd); + +FX_API const fx_command_option *fx_command_get_option(const fx_command *cmd, int id); + +FX_API const char *fx_command_option_get_long_name(const fx_command_option *opt); +FX_API char fx_command_option_get_short_name(const fx_command_option *opt); +FX_API const char *fx_command_option_get_description(fx_command_option *opt); +FX_API fx_status fx_command_option_set_long_name( + fx_command_option *opt, const char *name); +FX_API fx_status fx_command_option_set_short_name(fx_command_option *opt, char name); +FX_API fx_status fx_command_option_set_description( + fx_command_option *opt, const char *description); +FX_API fx_command_arg *fx_command_option_add_arg(fx_command_option *opt, int id); + +FX_API fx_status fx_command_arg_set_name(fx_command_arg *arg, const char *name); +FX_API fx_status fx_command_arg_set_description( + fx_command_arg *arg, const char *description); +FX_API fx_status fx_command_arg_set_nr_values( + fx_command_arg *arg, fx_command_arg_value_count nr_values); +FX_API fx_status fx_command_arg_set_allowed_values( + fx_command_arg *arg, const char **allowed_values); + +FX_API fx_status fx_command_usage_add_option( + fx_command_usage *usage, fx_command_option *opt); +FX_API fx_status fx_command_usage_add_arg( + fx_command_usage *usage, fx_command_arg *opt); +FX_API fx_status fx_command_usage_add_command( + fx_command_usage *usage, unsigned int cmd_id); + +FX_API fx_status fx_arglist_get_string( + const fx_arglist *args, unsigned int opt_id, unsigned int arg_id, + unsigned int index, const char **out); +FX_API fx_status fx_arglist_get_int( + const fx_arglist *args, unsigned int opt_id, unsigned int arg_id, + unsigned int index, long long *out); +FX_API fx_status fx_arglist_get_uint( + const fx_arglist *args, unsigned int opt_id, unsigned int arg_id, + unsigned int index, unsigned long long *out); +FX_API fx_status fx_arglist_get_option( + const fx_arglist *args, unsigned int opt_id, unsigned int index, + fx_arglist_option **out); +FX_API size_t fx_arglist_get_count( + const fx_arglist *args, unsigned int opt_id, unsigned int arg_id); + +FX_API fx_status fx_arglist_report_missing_option( + const fx_arglist *args, unsigned int opt_id); +FX_API fx_status fx_arglist_report_unexpected_arg( + const fx_arglist *args, const char *value); +FX_API fx_status fx_arglist_report_invalid_arg_value( + const fx_arglist *args, unsigned int opt_id, unsigned int arg_id, + const char *value); +FX_API fx_status fx_arglist_report_missing_args( + const fx_arglist *args, unsigned int opt_id, unsigned int arg_id, + size_t nr_supplied); + +FX_API fx_status fx_arglist_option_get_value( + const fx_arglist_option *opt, unsigned int arg_id, unsigned int index, + fx_arglist_value **out); + +FX_API int fx_arglist_iterator_begin( + const fx_arglist *args, unsigned int opt_filter, unsigned int arg_filter, + fx_arglist_iterator *it); +FX_API bool fx_arglist_iterator_next(fx_arglist_iterator *it); +FX_API bool fx_arglist_iterator_is_valid(const fx_arglist_iterator *it); + +FX_API int fx_arglist_option_iterator_begin( + const fx_arglist *args, unsigned int opt_filter, + fx_arglist_option_iterator *it); +FX_API bool fx_arglist_option_iterator_next(fx_arglist_option_iterator *it); +FX_API bool fx_arglist_option_iterator_is_valid( + const fx_arglist_option_iterator *it); + +#endif diff --git a/cmd/option.c b/cmd/option.c index acc814e..5b487bc 100644 --- a/cmd/option.c +++ b/cmd/option.c @@ -1,13 +1,13 @@ #include "command.h" -#include -#include +#include +#include #include #include -struct b_command_option *b_command_option_create(void) +struct fx_command_option *fx_command_option_create(void) { - struct b_command_option *out = malloc(sizeof *out); + struct fx_command_option *out = malloc(sizeof *out); if (!out) { return out; } @@ -16,7 +16,7 @@ struct b_command_option *b_command_option_create(void) return out; } -void b_command_option_destroy(struct b_command_option *opt) +void fx_command_option_destroy(struct fx_command_option *opt) { if (opt->opt_long_name) { free(opt->opt_long_name); @@ -26,43 +26,43 @@ void b_command_option_destroy(struct b_command_option *opt) free(opt->opt_description); } - struct b_queue_entry *entry = b_queue_first(&opt->opt_args); + struct fx_queue_entry *entry = fx_queue_first(&opt->opt_args); while (entry) { - struct b_command_arg *arg - = b_unbox(struct b_command_arg, entry, arg_entry); + struct fx_command_arg *arg + = fx_unbox(struct fx_command_arg, entry, arg_entry); - struct b_queue_entry *next = b_queue_next(entry); - b_queue_delete(&opt->opt_args, entry); + struct fx_queue_entry *next = fx_queue_next(entry); + fx_queue_delete(&opt->opt_args, entry); - b_command_arg_destroy(arg); + fx_command_arg_destroy(arg); entry = next; } free(opt); } -const char *b_command_option_get_long_name(const struct b_command_option *opt) +const char *fx_command_option_get_long_name(const struct fx_command_option *opt) { return opt->opt_long_name; } -char b_command_option_get_short_name(const struct b_command_option *opt) +char fx_command_option_get_short_name(const struct fx_command_option *opt) { return opt->opt_short_name; } -const char *b_command_option_get_description(struct b_command_option *opt) +const char *fx_command_option_get_description(struct fx_command_option *opt) { return opt->opt_description; } -b_status b_command_option_set_long_name( - struct b_command_option *opt, const char *name) +fx_status fx_command_option_set_long_name( + struct fx_command_option *opt, const char *name) { - char *n = b_strdup(name); + char *n = fx_strdup(name); if (!n) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } if (opt->opt_long_name) { @@ -71,21 +71,21 @@ b_status b_command_option_set_long_name( } opt->opt_long_name = n; - return B_SUCCESS; + return FX_SUCCESS; } -b_status b_command_option_set_short_name(struct b_command_option *opt, char name) +fx_status fx_command_option_set_short_name(struct fx_command_option *opt, char name) { opt->opt_short_name = name; - return B_SUCCESS; + return FX_SUCCESS; } -b_status b_command_option_set_description( - struct b_command_option *opt, const char *description) +fx_status fx_command_option_set_description( + struct fx_command_option *opt, const char *description) { - char *desc = b_strdup(description); + char *desc = fx_strdup(description); if (!desc) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } if (opt->opt_description) { @@ -94,12 +94,12 @@ b_status b_command_option_set_description( } opt->opt_description = desc; - return B_SUCCESS; + return FX_SUCCESS; } -struct b_command_arg *b_command_option_add_arg(struct b_command_option *opt, int id) +struct fx_command_arg *fx_command_option_add_arg(struct fx_command_option *opt, int id) { - struct b_command_arg *arg = malloc(sizeof *arg); + struct fx_command_arg *arg = malloc(sizeof *arg); if (!arg) { return NULL; } @@ -108,111 +108,111 @@ struct b_command_arg *b_command_option_add_arg(struct b_command_option *opt, int arg->arg_id = id; - b_queue_push_back(&opt->opt_args, &arg->arg_entry); + fx_queue_push_back(&opt->opt_args, &arg->arg_entry); return arg; } -void z__b_get_option_description(struct b_command_option *opt, b_string *out) +void z__fx_get_option_description(struct fx_command_option *opt, fx_string *out) { if (opt->opt_description) { - b_string_append_cstr(out, opt->opt_description); + fx_string_append_cstr(out, opt->opt_description); } - size_t nr_args = b_queue_length(&opt->opt_args); + size_t nr_args = fx_queue_length(&opt->opt_args); bool close_bracket = false; size_t i = 0; - struct b_queue_entry *entry = b_queue_first(&opt->opt_args); + struct fx_queue_entry *entry = fx_queue_first(&opt->opt_args); while (entry) { - struct b_command_arg *arg - = b_unbox(struct b_command_arg, entry, arg_entry); + struct fx_command_arg *arg + = fx_unbox(struct fx_command_arg, entry, arg_entry); if (!arg || !arg->arg_allowed_values) { goto skip; } if (i > 0) { - b_string_append_cstr(out, "; "); + fx_string_append_cstr(out, "; "); } else { - b_string_append_cstr(out, " [["); + fx_string_append_cstr(out, " [["); close_bracket = true; } if (nr_args > 1) { - b_string_append_cstrf( + fx_string_append_cstrf( out, "values for `%s`:", arg->arg_name); } else { - b_string_append_cstr(out, "values:"); + fx_string_append_cstr(out, "values:"); } for (size_t i = 0; arg->arg_allowed_values[i]; i++) { if (i > 0) { - b_string_append_cstr(out, ","); + fx_string_append_cstr(out, ","); } - b_string_append_cstrf( + fx_string_append_cstrf( out, " " F_GREEN "%s" F_RESET, arg->arg_allowed_values[i]); } skip: i++; - entry = b_queue_next(entry); + entry = fx_queue_next(entry); } if (close_bracket) { - b_string_append_cstr(out, "]"); + fx_string_append_cstr(out, "]"); } } -void z__b_get_option_usage_string( - struct b_command_option *opt, enum cmd_string_flags flags, b_string *out) +void z__fx_get_option_usage_string( + struct fx_command_option *opt, enum cmd_string_flags flags, fx_string *out) { if (flags & CMD_STR_DIRECT_USAGE) { - b_string_append_cstr(out, "{"); + fx_string_append_cstr(out, "{"); } if (opt->opt_short_name) { - b_string_append_cstrf( + fx_string_append_cstrf( out, (flags & CMD_STR_COLOUR) ? F_GREEN "-%c" F_RESET : "-%c", opt->opt_short_name); } if (opt->opt_short_name && opt->opt_long_name) { - b_string_append_cstr( + fx_string_append_cstr( out, (flags & CMD_STR_DIRECT_USAGE) ? "|" : ", "); } if (opt->opt_long_name) { - b_string_append_cstrf( + fx_string_append_cstrf( out, (flags & CMD_STR_COLOUR) ? F_GREEN "--%s" F_RESET : "--%s", opt->opt_long_name); } if (flags & CMD_STR_DIRECT_USAGE) { - b_string_append_cstr(out, "}"); + fx_string_append_cstr(out, "}"); } - struct b_queue_entry *entry = b_queue_first(&opt->opt_args); + struct fx_queue_entry *entry = fx_queue_first(&opt->opt_args); while (entry) { - struct b_command_arg *arg - = b_unbox(struct b_command_arg, entry, arg_entry); + struct fx_command_arg *arg + = fx_unbox(struct fx_command_arg, entry, arg_entry); if (!arg) { goto skip; } bool optional = false, multi = false; switch (arg->arg_nr_values) { - case B_ARG_0_OR_1_VALUES: + case FX_ARG_0_OR_1_VALUES: optional = true; multi = false; break; - case B_ARG_0_OR_MORE_VALUES: + case FX_ARG_0_OR_MORE_VALUES: optional = true; multi = true; break; - case B_ARG_1_OR_MORE_VALUES: + case FX_ARG_1_OR_MORE_VALUES: optional = false; multi = true; break; @@ -223,13 +223,13 @@ void z__b_get_option_usage_string( } if (optional) { - b_string_append_cstrf( + fx_string_append_cstrf( out, (flags & CMD_STR_COLOUR) ? " " F_GREEN "[%s]" : " [%s]", arg->arg_name); } else { - b_string_append_cstrf( + fx_string_append_cstrf( out, (flags & CMD_STR_COLOUR) ? " " F_GREEN "<%s>" : " <%s>", @@ -237,36 +237,36 @@ void z__b_get_option_usage_string( } for (int i = 1; i < arg->arg_nr_values; i++) { - b_string_append_cstrf(out, " <%s>", arg->arg_name); + fx_string_append_cstrf(out, " <%s>", arg->arg_name); } if (multi) { - b_string_append_cstr(out, "..."); + fx_string_append_cstr(out, "..."); } if (flags & CMD_STR_COLOUR) { - b_string_append_cstr(out, F_RESET); + fx_string_append_cstr(out, F_RESET); } skip: - entry = b_queue_next(entry); + entry = fx_queue_next(entry); } } -struct b_command_arg *b_command_option_get_arg_with_id( - struct b_command_option *opt, unsigned int id) +struct fx_command_arg *fx_command_option_get_arg_with_id( + struct fx_command_option *opt, unsigned int id) { - struct b_queue_entry *entry = b_queue_first(&opt->opt_args); + struct fx_queue_entry *entry = fx_queue_first(&opt->opt_args); while (entry) { - struct b_command_arg *arg - = b_unbox(struct b_command_arg, entry, arg_entry); + struct fx_command_arg *arg + = fx_unbox(struct fx_command_arg, entry, arg_entry); if (arg->arg_id == id) { return arg; } - entry = b_queue_next(entry); + entry = fx_queue_next(entry); } return NULL; diff --git a/cmd/report.c b/cmd/report.c index 0539113..3c7bbfe 100644 --- a/cmd/report.c +++ b/cmd/report.c @@ -1,28 +1,28 @@ #include "command.h" #include -#include -#include -#include +#include +#include +#include #include -enum b_status b_arglist_report_missing_option( - const b_arglist *args, unsigned int opt_id) +enum fx_status fx_arglist_report_missing_option( + const fx_arglist *args, unsigned int opt_id) { - struct b_command_option *opt = NULL; + struct fx_command_option *opt = NULL; - if (opt_id != B_COMMAND_INVALID_ID) { - opt = b_command_get_option_with_id(args->list_command, opt_id); + if (opt_id != FX_COMMAND_INVALID_ID) { + opt = fx_command_get_option_with_id(args->list_command, opt_id); } if (!opt) { - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } - b_string *opt_string = b_string_create(); - z__b_get_option_usage_string(opt, 0, opt_string); + fx_string *opt_string = fx_string_create(); + z__fx_get_option_usage_string(opt, 0, opt_string); - b_stringstream *opt_name = b_stringstream_create(); + fx_stringstream *opt_name = fx_stringstream_create(); int opt_names = 0; if (opt->opt_short_name) { @@ -34,121 +34,121 @@ enum b_status b_arglist_report_missing_option( } if (opt_names == 2) { - b_stream_write_fmt( + fx_stream_write_fmt( opt_name, NULL, "-%c / --%s", opt->opt_short_name, opt->opt_long_name); } else if (opt->opt_short_name) { - b_stream_write_fmt(opt_name, NULL, "-%c", opt->opt_short_name); + fx_stream_write_fmt(opt_name, NULL, "-%c", opt->opt_short_name); } else if (opt->opt_long_name) { - b_stream_write_fmt(opt_name, NULL, "--%s", opt->opt_long_name); + fx_stream_write_fmt(opt_name, NULL, "--%s", opt->opt_long_name); } - b_err("required option `" F_YELLOW "%s" F_RESET "` was not specified.", - b_stringstream_ptr(opt_name)); - b_i("usage: %s", b_string_ptr(opt_string)); - b_i("for more information, use `" F_YELLOW "--help" F_RESET "`"); + fx_err("required option `" F_YELLOW "%s" F_RESET "` was not specified.", + fx_stringstream_ptr(opt_name)); + fx_i("usage: %s", fx_string_ptr(opt_string)); + fx_i("for more information, use `" F_YELLOW "--help" F_RESET "`"); - b_string_unref(opt_string); - b_stringstream_unref(opt_name); + fx_string_unref(opt_string); + fx_stringstream_unref(opt_name); - return B_SUCCESS; + return FX_SUCCESS; } -enum b_status b_arglist_report_unexpected_arg( - const b_arglist *args, const char *value) +enum fx_status fx_arglist_report_unexpected_arg( + const fx_arglist *args, const char *value) { - b_string *usage = z__b_command_default_usage_string( + fx_string *usage = z__fx_command_default_usage_string( args->list_command, NULL, args); - b_err("unexpected argument '" F_YELLOW "%s" F_RESET "' found.", value); - b_i("usage: %s", b_string_ptr(usage)); - b_i("for more information, use '" F_YELLOW "--help" F_RESET "'"); + fx_err("unexpected argument '" F_YELLOW "%s" F_RESET "' found.", value); + fx_i("usage: %s", fx_string_ptr(usage)); + fx_i("for more information, use '" F_YELLOW "--help" F_RESET "'"); - return B_SUCCESS; + return FX_SUCCESS; } -enum b_status b_arglist_report_invalid_arg_value( - const b_arglist *args, unsigned int opt_id, unsigned int arg_id, +enum fx_status fx_arglist_report_invalid_arg_value( + const fx_arglist *args, unsigned int opt_id, unsigned int arg_id, const char *value) { - struct b_command_option *opt = NULL; - struct b_command_arg *arg = NULL; + struct fx_command_option *opt = NULL; + struct fx_command_arg *arg = NULL; - if (opt_id != B_COMMAND_INVALID_ID) { - opt = b_command_get_option_with_id(args->list_command, opt_id); + if (opt_id != FX_COMMAND_INVALID_ID) { + opt = fx_command_get_option_with_id(args->list_command, opt_id); } - if (arg_id != B_COMMAND_INVALID_ID) { - arg = opt ? b_command_option_get_arg_with_id(opt, arg_id) - : b_command_get_arg_with_id(args->list_command, arg_id); + if (arg_id != FX_COMMAND_INVALID_ID) { + arg = opt ? fx_command_option_get_arg_with_id(opt, arg_id) + : fx_command_get_arg_with_id(args->list_command, arg_id); } - b_string *usage = z__b_command_default_usage_string( + fx_string *usage = z__fx_command_default_usage_string( args->list_command, opt, args); - b_string *opt_string = b_string_create(); + fx_string *opt_string = fx_string_create(); if (opt) { - z__b_get_option_usage_string(opt, 0, opt_string); + z__fx_get_option_usage_string(opt, 0, opt_string); } else { - z__b_get_arg_usage_string(arg, 0, opt_string); + z__fx_get_arg_usage_string(arg, 0, opt_string); } - b_err("invalid value '" F_YELLOW "%s" F_RESET "' for '" F_YELLOW + fx_err("invalid value '" F_YELLOW "%s" F_RESET "' for '" F_YELLOW "%s" F_RESET "'.", - value, b_string_ptr(opt_string)); + value, fx_string_ptr(opt_string)); if (opt) { - b_i("'" F_YELLOW "%s" F_RESET + fx_i("'" F_YELLOW "%s" F_RESET "' accepts the following values for '" F_YELLOW "%s" F_RESET "':", - b_string_ptr(opt_string), arg->arg_name); + fx_string_ptr(opt_string), arg->arg_name); } else { - b_i("'" F_YELLOW "%s" F_RESET "' accepts the following values:", - b_string_ptr(opt_string)); + fx_i("'" F_YELLOW "%s" F_RESET "' accepts the following values:", + fx_string_ptr(opt_string)); } for (int i = 0; arg->arg_allowed_values[i]; i++) { - b_printf( + fx_printf( " * " F_GREEN "%s" F_RESET "\n", arg->arg_allowed_values[i]); } - b_printf("\n"); - b_i("usage: %s", b_string_ptr(usage)); - b_i("for more information, use '" F_YELLOW "--help" F_RESET); + fx_printf("\n"); + fx_i("usage: %s", fx_string_ptr(usage)); + fx_i("for more information, use '" F_YELLOW "--help" F_RESET); - b_string_unref(usage); - b_string_unref(opt_string); + fx_string_unref(usage); + fx_string_unref(opt_string); - return B_SUCCESS; + return FX_SUCCESS; } -enum b_status b_arglist_report_missing_args( - const b_arglist *args, unsigned int opt_id, unsigned int arg_id, +enum fx_status fx_arglist_report_missing_args( + const fx_arglist *args, unsigned int opt_id, unsigned int arg_id, size_t args_supplied) { - struct b_command_option *opt = NULL; - struct b_command_arg *arg = NULL; + struct fx_command_option *opt = NULL; + struct fx_command_arg *arg = NULL; - if (opt_id != B_COMMAND_INVALID_ID) { - opt = b_command_get_option_with_id(args->list_command, opt_id); + if (opt_id != FX_COMMAND_INVALID_ID) { + opt = fx_command_get_option_with_id(args->list_command, opt_id); assert(opt); } - if (arg_id != B_COMMAND_INVALID_ID) { - arg = opt ? b_command_option_get_arg_with_id(opt, arg_id) - : b_command_get_arg_with_id(args->list_command, arg_id); + if (arg_id != FX_COMMAND_INVALID_ID) { + arg = opt ? fx_command_option_get_arg_with_id(opt, arg_id) + : fx_command_get_arg_with_id(args->list_command, arg_id); assert(arg); } - b_string *usage = z__b_command_default_usage_string( + fx_string *usage = z__fx_command_default_usage_string( args->list_command, opt, args); - b_string *opt_string = b_string_create(); + fx_string *opt_string = fx_string_create(); if (opt) { - z__b_get_option_usage_string(opt, 0, opt_string); + z__fx_get_option_usage_string(opt, 0, opt_string); } else { - z__b_get_arg_usage_string(arg, 0, opt_string); + z__fx_get_arg_usage_string(arg, 0, opt_string); } char supplied_arg_str[64]; @@ -170,7 +170,7 @@ enum b_status b_arglist_report_missing_args( char required_arg_count[64]; switch (arg->arg_nr_values) { - case B_ARG_1_OR_MORE_VALUES: + case FX_ARG_1_OR_MORE_VALUES: snprintf( required_arg_count, sizeof required_arg_count, "one or more"); @@ -181,15 +181,15 @@ enum b_status b_arglist_report_missing_args( arg->arg_nr_values); } - b_err("argument `" F_YELLOW "%s" F_RESET "` requires " F_GREEN_BOLD + fx_err("argument `" F_YELLOW "%s" F_RESET "` requires " F_GREEN_BOLD "%s" F_RESET " `" F_YELLOW "%s" F_RESET "` value%s, but %s.", - b_string_ptr(opt_string), required_arg_count, arg->arg_name, + fx_string_ptr(opt_string), required_arg_count, arg->arg_name, (arg->arg_nr_values == 1) ? "" : "s", supplied_arg_str); - b_i("usage: %s", b_string_ptr(usage)); - b_i("for more information, use '" F_YELLOW "--help" F_RESET "'"); + fx_i("usage: %s", fx_string_ptr(usage)); + fx_i("for more information, use '" F_YELLOW "--help" F_RESET "'"); - b_string_unref(usage); - b_string_unref(opt_string); + fx_string_unref(usage); + fx_string_unref(opt_string); - return B_SUCCESS; + return FX_SUCCESS; } diff --git a/compress/CMakeLists.txt b/compress/CMakeLists.txt index 3707dd1..9a68aab 100644 --- a/compress/CMakeLists.txt +++ b/compress/CMakeLists.txt @@ -6,11 +6,11 @@ if (ZSTD_FOUND) set(libs ${libs} ${ZSTD_LIBRARY}) set(include_dirs ${include_dirs} ${ZSTD_INCLUDE_DIR}) set(function_sources ${function_sources} ${CMAKE_CURRENT_SOURCE_DIR}/function/zstd.c) - set(defines ${defines} B_COMPRESSOR_SUPPORTED_ZSTD) - message(STATUS "Enabling ZSTD support in blue-compress") + set(defines ${defines} FX_COMPRESSOR_SUPPORTED_ZSTD) + message(STATUS "Enabling ZSTD support in fx-compress") endif () -add_bluelib_module( +add_fx_module( NAME compress DEPENDENCIES core EXTRA_SOURCES ${function_sources} diff --git a/compress/compressor.c b/compress/compressor.c index 630e7f6..7f9aca5 100644 --- a/compress/compressor.c +++ b/compress/compressor.c @@ -1,15 +1,15 @@ #include -#include -#include +#include +#include #include #include #define COMPRESSOR_DISPATCH_STATIC(func, compressor, ...) \ do { \ struct compressor_data _compressor; \ - enum b_status status \ + enum fx_status status \ = compressor_get_data(compressor, &_compressor); \ - if (!B_OK(status)) { \ + if (!FX_OK(status)) { \ return status; \ } \ return func(&_compressor, __VA_ARGS__); \ @@ -17,9 +17,9 @@ #define COMPRESSOR_DISPATCH_STATIC_0(func, compressor) \ do { \ struct compressor_data _compressor; \ - enum b_status status \ + enum fx_status status \ = compressor_get_data(compressor, &_compressor); \ - if (!B_OK(status)) { \ + if (!FX_OK(status)) { \ return status; \ } \ return func(&_compressor); \ @@ -28,106 +28,106 @@ /*** PRIVATE DATA *************************************************************/ struct compressor_data { - b_compressor *c_obj; - b_compressor_class *c_ops; - b_compressor_data *c_data; + fx_compressor *c_obj; + fx_compressor_class *c_ops; + fx_compressor_data *c_data; }; /*** PRIVATE FUNCTIONS ********************************************************/ -static enum b_status compressor_get_data( - b_compressor *compressor, struct compressor_data *out) +static enum fx_status compressor_get_data( + fx_compressor *compressor, struct compressor_data *out) { out->c_obj = compressor; - return b_object_get_data( - compressor, B_TYPE_COMPRESSOR, NULL, (void **)&out->c_data, + return fx_object_get_data( + compressor, FX_TYPE_COMPRESSOR, NULL, (void **)&out->c_data, (void **)&out->c_ops); } -static enum b_status compressor_get_mode( - struct compressor_data *p, enum b_compressor_mode *out) +static enum fx_status compressor_get_mode( + struct compressor_data *p, enum fx_compressor_mode *out) { if (out) { *out = p->c_data->c_mode; } - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status compressor_set_mode( - struct compressor_data *p, enum b_compressor_mode mode) +static enum fx_status compressor_set_mode( + struct compressor_data *p, enum fx_compressor_mode mode) { if (!p->c_ops->c_set_mode) { - return B_ERR_NOT_SUPPORTED; + return FX_ERR_NOT_SUPPORTED; } return p->c_ops->c_set_mode(p->c_obj, mode); } -static enum b_status compressor_set_buffer( - struct compressor_data *p, b_ringbuffer *inbuf, b_ringbuffer *outbuf) +static enum fx_status compressor_set_buffer( + struct compressor_data *p, fx_ringbuffer *inbuf, fx_ringbuffer *outbuf) { p->c_data->c_in = inbuf; p->c_data->c_out = outbuf; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status compress(struct compressor_data *p) +static enum fx_status compress(struct compressor_data *p) { - if (p->c_data->c_mode != B_COMPRESSOR_MODE_COMPRESS) { - return B_ERR_BAD_STATE; + if (p->c_data->c_mode != FX_COMPRESSOR_MODE_COMPRESS) { + return FX_ERR_BAD_STATE; } if (!p->c_ops->c_compress) { - return B_ERR_NOT_SUPPORTED; + return FX_ERR_NOT_SUPPORTED; } return p->c_ops->c_compress(p->c_obj); } -static enum b_status decompress(struct compressor_data *p) +static enum fx_status decompress(struct compressor_data *p) { - if (p->c_data->c_mode != B_COMPRESSOR_MODE_DECOMPRESS) { - return B_ERR_BAD_STATE; + if (p->c_data->c_mode != FX_COMPRESSOR_MODE_DECOMPRESS) { + return FX_ERR_BAD_STATE; } if (!p->c_ops->c_decompress) { - return B_ERR_NOT_SUPPORTED; + return FX_ERR_NOT_SUPPORTED; } return p->c_ops->c_decompress(p->c_obj); } -static enum b_status compressor_step(struct compressor_data *p) +static enum fx_status compressor_step(struct compressor_data *p) { switch (p->c_data->c_mode) { - case B_COMPRESSOR_MODE_COMPRESS: + case FX_COMPRESSOR_MODE_COMPRESS: return compress(p); - case B_COMPRESSOR_MODE_DECOMPRESS: + case FX_COMPRESSOR_MODE_DECOMPRESS: return decompress(p); default: - return B_ERR_BAD_STATE; + return FX_ERR_BAD_STATE; } } -static enum b_status compressor_end(struct compressor_data *p) +static enum fx_status compressor_end(struct compressor_data *p) { - if (p->c_data->c_mode != B_COMPRESSOR_MODE_COMPRESS) { - return B_SUCCESS; + if (p->c_data->c_mode != FX_COMPRESSOR_MODE_COMPRESS) { + return FX_SUCCESS; } if (!p->c_ops->c_compress_end) { - return B_ERR_NOT_SUPPORTED; + return FX_ERR_NOT_SUPPORTED; } - while (b_ringbuffer_available_data_remaining(p->c_data->c_in)) { - if (!b_ringbuffer_write_capacity_remaining(p->c_data->c_out)) { - return B_ERR_NO_SPACE; + while (fx_ringbuffer_available_data_remaining(p->c_data->c_in)) { + if (!fx_ringbuffer_write_capacity_remaining(p->c_data->c_out)) { + return FX_ERR_NO_SPACE; } - enum b_status status = compressor_step(p); - if (!B_OK(status)) { + enum fx_status status = compressor_step(p); + if (!FX_OK(status)) { return status; } } @@ -135,105 +135,105 @@ static enum b_status compressor_end(struct compressor_data *p) return p->c_ops->c_compress_end(p->c_obj); } -static enum b_status compressor_reset(struct compressor_data *p) +static enum fx_status compressor_reset(struct compressor_data *p) { - p->c_data->c_flags &= ~B_COMPRESSOR_EOF; + p->c_data->c_flags &= ~FX_COMPRESSOR_EOF; if (p->c_ops->c_reset) { return p->c_ops->c_reset(p->c_obj); } - return B_SUCCESS; + return FX_SUCCESS; } static bool compressor_eof(const struct compressor_data *p) { - return (p->c_data->c_flags & B_COMPRESSOR_EOF) != 0; + return (p->c_data->c_flags & FX_COMPRESSOR_EOF) != 0; } /*** PUBLIC FUNCTIONS *********************************************************/ -enum b_status b_compressor_get_buffer_size( - b_type type, b_compressor_mode mode, size_t *inbuf_size, size_t *outbuf_size) +enum fx_status fx_compressor_get_buffer_size( + fx_type type, fx_compressor_mode mode, size_t *inbuf_size, size_t *outbuf_size) { - b_class *c = b_class_get(type); + fx_class *c = fx_class_get(type); if (!c) { - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } - b_compressor_class *ops = b_class_get_interface(c, B_TYPE_COMPRESSOR); + fx_compressor_class *ops = fx_class_get_interface(c, FX_TYPE_COMPRESSOR); if (!ops) { - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } if (!ops->c_buffer_size) { - return B_ERR_NOT_SUPPORTED; + return FX_ERR_NOT_SUPPORTED; } return ops->c_buffer_size(mode, inbuf_size, outbuf_size); } -enum b_status b_compressor_get_mode( - const b_compressor *compressor, enum b_compressor_mode *out) +enum fx_status fx_compressor_get_mode( + const fx_compressor *compressor, enum fx_compressor_mode *out) { COMPRESSOR_DISPATCH_STATIC( - compressor_get_mode, (b_compressor *)compressor, out); + compressor_get_mode, (fx_compressor *)compressor, out); } -enum b_status b_compressor_set_mode( - b_compressor *compressor, enum b_compressor_mode mode) +enum fx_status fx_compressor_set_mode( + fx_compressor *compressor, enum fx_compressor_mode mode) { COMPRESSOR_DISPATCH_STATIC(compressor_set_mode, compressor, mode); } -enum b_status b_compressor_set_buffer( - b_compressor *compressor, b_ringbuffer *inbuf, b_ringbuffer *outbuf) +enum fx_status fx_compressor_set_buffer( + fx_compressor *compressor, fx_ringbuffer *inbuf, fx_ringbuffer *outbuf) { COMPRESSOR_DISPATCH_STATIC(compressor_set_buffer, compressor, inbuf, outbuf); } -enum b_status b_compressor_step(b_compressor *compressor) +enum fx_status fx_compressor_step(fx_compressor *compressor) { COMPRESSOR_DISPATCH_STATIC_0(compressor_step, compressor); } -enum b_status b_compressor_end(b_compressor *compressor) +enum fx_status fx_compressor_end(fx_compressor *compressor) { COMPRESSOR_DISPATCH_STATIC_0(compressor_end, compressor); } -enum b_status b_compressor_reset(b_compressor *compressor) +enum fx_status fx_compressor_reset(fx_compressor *compressor) { COMPRESSOR_DISPATCH_STATIC_0(compressor_reset, compressor); } -bool b_compressor_eof(const b_compressor *compressor) +bool fx_compressor_eof(const fx_compressor *compressor) { - COMPRESSOR_DISPATCH_STATIC_0(compressor_eof, (b_compressor *)compressor); + COMPRESSOR_DISPATCH_STATIC_0(compressor_eof, (fx_compressor *)compressor); } /*** VIRTUAL FUNCTIONS ********************************************************/ -static void compressor_init(b_object *obj, void *priv) +static void compressor_init(fx_object *obj, void *priv) { } -static void compressor_fini(b_object *obj, void *priv) +static void compressor_fini(fx_object *obj, void *priv) { } /*** CLASS DEFINITION *********************************************************/ -B_TYPE_CLASS_DEFINITION_BEGIN(b_compressor) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) -B_TYPE_CLASS_DEFINITION_END(b_compressor) +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_compressor) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_DEFINITION_END(fx_compressor) -B_TYPE_DEFINITION_BEGIN(b_compressor) - B_TYPE_ID(0x452ee0f9, 0xfe12, 0x48a1, 0xb596, 0xad5b7a3940e7); - B_TYPE_CLASS(b_compressor_class); - B_TYPE_INSTANCE_PROTECTED(b_compressor_data); - B_TYPE_INSTANCE_INIT(compressor_init); - B_TYPE_INSTANCE_FINI(compressor_fini); -B_TYPE_DEFINITION_END(b_compressor) +FX_TYPE_DEFINITION_BEGIN(fx_compressor) + FX_TYPE_ID(0x452ee0f9, 0xfe12, 0x48a1, 0xb596, 0xad5b7a3940e7); + FX_TYPE_CLASS(fx_compressor_class); + FX_TYPE_INSTANCE_PROTECTED(fx_compressor_data); + FX_TYPE_INSTANCE_INIT(compressor_init); + FX_TYPE_INSTANCE_FINI(compressor_fini); +FX_TYPE_DEFINITION_END(fx_compressor) diff --git a/compress/cstream.c b/compress/cstream.c index 5da6148..f5d83b0 100644 --- a/compress/cstream.c +++ b/compress/cstream.c @@ -1,5 +1,5 @@ -#include -#include +#include +#include #include #include @@ -9,28 +9,28 @@ enum cstream_flags { CSTREAM_CURSOR_MOVED = 0x01u, }; -struct b_cstream_p { +struct fx_cstream_p { enum cstream_flags s_flags; - b_stream *s_endpoint; - b_compressor *s_compressor; + fx_stream *s_endpoint; + fx_compressor *s_compressor; /* s_in is the input buffer, and s_out is the output buffer. * * the input buffer holds data that will be provided to the * (de)compression function. in compression mode, this data is provided - * by the code using the cstream (via b_cstream_write). in decompression + * by the code using the cstream (via fx_cstream_write). in decompression * mode, this data is read from s_endpoint. * * the output buffer holds data produced by the (de)compression * function. in compression mode, this data will be written to * s_endpoint. in decompression mode, this data will be returned to the - * code using the cstream (via b_cstream_read) + * code using the cstream (via fx_cstream_read) * * heavy usage of cstream's compressed sections facility can result * in the input buffer holding uncompressed data while the stream is in * decompression mode. this is handled by the uncompressed read code path. */ - b_ringbuffer *s_in, *s_out; - b_compressor_mode s_mode; + fx_ringbuffer *s_in, *s_out; + fx_compressor_mode s_mode; unsigned int s_compression_depth; /* tracks the number of bytes read from or written to the endpoint. @@ -63,10 +63,10 @@ struct b_cstream_p { * stream in the current section. * * in compression mode, this tracks the number of bytes given to - * b_cstream_write + * fx_cstream_write * * in decompression mode, this tracks the number of bytes returned by - * b_cstream_read + * fx_cstream_read */ size_t s_tx_bytes_uncompressed; @@ -77,20 +77,20 @@ struct b_cstream_p { /*** PRIVATE FUNCTIONS ********************************************************/ -static enum b_status read_cursor( - struct b_cstream_p *stream, void *buf, size_t count, size_t *out_nr_read) +static enum fx_status read_cursor( + struct fx_cstream_p *stream, void *buf, size_t count, size_t *out_nr_read) { - return b_stream_read_bytes(stream->s_endpoint, buf, count, out_nr_read); + return fx_stream_read_bytes(stream->s_endpoint, buf, count, out_nr_read); } -static enum b_status read_uncompressed( - struct b_cstream_p *stream, void *buf, size_t count, size_t *out_nr_read) +static enum fx_status read_uncompressed( + struct fx_cstream_p *stream, void *buf, size_t count, size_t *out_nr_read) { size_t remaining = count; unsigned char *dest = buf; size_t nr_read_from_buf = 0; size_t nr_read_from_endpoint = 0; - enum b_status status = B_SUCCESS; + enum fx_status status = FX_SUCCESS; /* liberal usage of begin_compressed_section and end_compressed_section * can result in uncompressed data getting stuck in the input buffer. @@ -100,9 +100,9 @@ static enum b_status read_uncompressed( while (remaining > 0) { const void *data; size_t available; - status = b_ringbuffer_open_read_buffer( + status = fx_ringbuffer_open_read_buffer( stream->s_in, &data, &available); - if (!B_OK(status)) { + if (!FX_OK(status)) { break; } @@ -113,7 +113,7 @@ static enum b_status read_uncompressed( memcpy(dest, data, to_copy); - b_ringbuffer_close_read_buffer(stream->s_in, &data, to_copy); + fx_ringbuffer_close_read_buffer(stream->s_in, &data, to_copy); stream->s_tx_bytes_uncompressed += to_copy; stream->s_tx_bytes += to_copy; @@ -124,10 +124,10 @@ static enum b_status read_uncompressed( if (remaining == 0) { *out_nr_read = nr_read_from_buf; - return B_SUCCESS; + return FX_SUCCESS; } - status = b_stream_read_bytes( + status = fx_stream_read_bytes( stream->s_endpoint, dest, remaining, &nr_read_from_endpoint); stream->s_tx_bytes_uncompressed += nr_read_from_endpoint; stream->s_tx_bytes += nr_read_from_endpoint; @@ -141,38 +141,38 @@ static enum b_status read_uncompressed( * note that uncompressed data that is trailing the compressed blob may * also be read by this function, but this will be handled by read_uncompressed. */ -static enum b_status refill_input_buffer(struct b_cstream_p *stream) +static enum fx_status refill_input_buffer(struct fx_cstream_p *stream) { - enum b_status status = B_SUCCESS; + enum fx_status status = FX_SUCCESS; size_t nr_read = 0; while (1) { void *data; size_t capacity; - status = b_ringbuffer_open_write_buffer( + status = fx_ringbuffer_open_write_buffer( stream->s_in, &data, &capacity); - if (!B_OK(status)) { + if (!FX_OK(status)) { break; } size_t r = 0; - status = b_stream_read_bytes( + status = fx_stream_read_bytes( stream->s_endpoint, data, capacity, &r); - b_ringbuffer_close_write_buffer(stream->s_in, &data, r); + fx_ringbuffer_close_write_buffer(stream->s_in, &data, r); nr_read += r; if (r < capacity) { break; } - if (!B_OK(status)) { + if (!FX_OK(status)) { break; } } - if (status == B_ERR_NO_SPACE && nr_read > 0) { - status = B_SUCCESS; + if (status == FX_ERR_NO_SPACE && nr_read > 0) { + status = FX_SUCCESS; } return status; @@ -180,25 +180,25 @@ static enum b_status refill_input_buffer(struct b_cstream_p *stream) /* push compressed data out of the input buffer, through the (de)compressor, * and store the resulting uncompressed data in the output buffer */ -static enum b_status refill_output_buffer(struct b_cstream_p *stream) +static enum fx_status refill_output_buffer(struct fx_cstream_p *stream) { - enum b_status status = B_SUCCESS; + enum fx_status status = FX_SUCCESS; - if (b_compressor_eof(stream->s_compressor)) { - return B_ERR_NO_DATA; + if (fx_compressor_eof(stream->s_compressor)) { + return FX_ERR_NO_DATA; } - if (!b_ringbuffer_available_data_remaining(stream->s_in)) { + if (!fx_ringbuffer_available_data_remaining(stream->s_in)) { status = refill_input_buffer(stream); } - if (!B_OK(status)) { + if (!FX_OK(status)) { return status; } - size_t bytes_before = b_ringbuffer_available_data_remaining(stream->s_in); - status = b_compressor_step(stream->s_compressor); - size_t bytes_after = b_ringbuffer_available_data_remaining(stream->s_in); + size_t bytes_before = fx_ringbuffer_available_data_remaining(stream->s_in); + status = fx_compressor_step(stream->s_compressor); + size_t bytes_after = fx_ringbuffer_available_data_remaining(stream->s_in); stream->s_tx_bytes_compressed += (bytes_before - bytes_after); stream->s_tx_bytes += (bytes_before - bytes_after); @@ -206,11 +206,11 @@ static enum b_status refill_output_buffer(struct b_cstream_p *stream) return status; } -static enum b_status cstream_read( - struct b_cstream_p *stream, void *buf, size_t count, size_t *out_nr_read) +static enum fx_status cstream_read( + struct fx_cstream_p *stream, void *buf, size_t count, size_t *out_nr_read) { - if (stream->s_mode != B_COMPRESSOR_MODE_DECOMPRESS) { - return B_ERR_BAD_STATE; + if (stream->s_mode != FX_COMPRESSOR_MODE_DECOMPRESS) { + return FX_ERR_BAD_STATE; } if (stream->s_flags & CSTREAM_CURSOR_MOVED) { @@ -224,22 +224,22 @@ static enum b_status cstream_read( unsigned char *dest = buf; size_t nr_read = 0; size_t remaining = count; - enum b_status status = B_SUCCESS; + enum fx_status status = FX_SUCCESS; while (remaining > 0) { - if (!b_ringbuffer_available_data_remaining(stream->s_out)) { + if (!fx_ringbuffer_available_data_remaining(stream->s_out)) { status = refill_output_buffer(stream); } - if (!B_OK(status)) { + if (!FX_OK(status)) { break; } const void *data; size_t available; - status = b_ringbuffer_open_read_buffer( + status = fx_ringbuffer_open_read_buffer( stream->s_out, &data, &available); - if (!B_OK(status)) { + if (!FX_OK(status)) { break; } @@ -250,7 +250,7 @@ static enum b_status cstream_read( memcpy(dest, data, to_copy); - b_ringbuffer_close_read_buffer(stream->s_out, &data, to_copy); + fx_ringbuffer_close_read_buffer(stream->s_out, &data, to_copy); stream->s_tx_bytes_uncompressed += to_copy; dest += to_copy; @@ -258,26 +258,26 @@ static enum b_status cstream_read( remaining -= to_copy; } - if (status == B_ERR_NO_DATA) { - status = B_SUCCESS; + if (status == FX_ERR_NO_DATA) { + status = FX_SUCCESS; } *out_nr_read = nr_read; return status; } -static enum b_status write_cursor( - struct b_cstream_p *stream, const void *buf, size_t count, size_t *nr_written) +static enum fx_status write_cursor( + struct fx_cstream_p *stream, const void *buf, size_t count, size_t *nr_written) { - return b_stream_write_bytes(stream->s_endpoint, buf, count, nr_written); + return fx_stream_write_bytes(stream->s_endpoint, buf, count, nr_written); } -static enum b_status write_uncompressed( - struct b_cstream_p *stream, const void *buf, size_t count, size_t *nr_written) +static enum fx_status write_uncompressed( + struct fx_cstream_p *stream, const void *buf, size_t count, size_t *nr_written) { size_t w = 0; - enum b_status status - = b_stream_write_bytes(stream->s_endpoint, buf, count, &w); + enum fx_status status + = fx_stream_write_bytes(stream->s_endpoint, buf, count, &w); stream->s_tx_bytes_uncompressed += w; stream->s_tx_bytes += w; @@ -288,35 +288,35 @@ static enum b_status write_uncompressed( /* push uncompressed data out of the input buffer, through the compressor, * and store the resulting compressed data in the output buffer */ -static enum b_status flush_input_buffer(struct b_cstream_p *stream) +static enum fx_status flush_input_buffer(struct fx_cstream_p *stream) { - if (!b_ringbuffer_available_data_remaining(stream->s_in)) { - return B_ERR_NO_DATA; + if (!fx_ringbuffer_available_data_remaining(stream->s_in)) { + return FX_ERR_NO_DATA; } - return b_compressor_step(stream->s_compressor); + return fx_compressor_step(stream->s_compressor); } /* push compressed data from the output buffer into the endpoint */ -static enum b_status flush_output_buffer(struct b_cstream_p *stream) +static enum fx_status flush_output_buffer(struct fx_cstream_p *stream) { - enum b_status status = B_SUCCESS; + enum fx_status status = FX_SUCCESS; size_t nr_written = 0; while (1) { const void *data; size_t capacity; - status = b_ringbuffer_open_read_buffer( + status = fx_ringbuffer_open_read_buffer( stream->s_out, &data, &capacity); - if (!B_OK(status)) { + if (!FX_OK(status)) { break; } size_t w = 0; - status = b_stream_write_bytes( + status = fx_stream_write_bytes( stream->s_endpoint, data, capacity, &w); - b_ringbuffer_close_read_buffer(stream->s_out, &data, w); + fx_ringbuffer_close_read_buffer(stream->s_out, &data, w); nr_written += w; stream->s_tx_bytes_compressed += w; stream->s_tx_bytes += w; @@ -325,24 +325,24 @@ static enum b_status flush_output_buffer(struct b_cstream_p *stream) break; } - if (!B_OK(status)) { + if (!FX_OK(status)) { break; } } - if (status == B_ERR_NO_DATA && nr_written > 0) { - status = B_SUCCESS; + if (status == FX_ERR_NO_DATA && nr_written > 0) { + status = FX_SUCCESS; } return status; } -static enum b_status cstream_write( - struct b_cstream_p *stream, const void *buf, size_t count, +static enum fx_status cstream_write( + struct fx_cstream_p *stream, const void *buf, size_t count, size_t *out_nr_written) { - if (stream->s_mode != B_COMPRESSOR_MODE_COMPRESS) { - return B_ERR_BAD_STATE; + if (stream->s_mode != FX_COMPRESSOR_MODE_COMPRESS) { + return FX_ERR_BAD_STATE; } if (stream->s_flags & CSTREAM_CURSOR_MOVED) { @@ -356,26 +356,26 @@ static enum b_status cstream_write( const unsigned char *src = buf; size_t nr_written = 0; size_t remaining = count; - enum b_status status = B_SUCCESS; + enum fx_status status = FX_SUCCESS; while (remaining > 0) { - if (!b_ringbuffer_write_capacity_remaining(stream->s_out)) { + if (!fx_ringbuffer_write_capacity_remaining(stream->s_out)) { status = flush_output_buffer(stream); } - if (!b_ringbuffer_write_capacity_remaining(stream->s_in)) { + if (!fx_ringbuffer_write_capacity_remaining(stream->s_in)) { status = flush_input_buffer(stream); } - if (!B_OK(status)) { + if (!FX_OK(status)) { break; } void *data; size_t available; - status = b_ringbuffer_open_write_buffer( + status = fx_ringbuffer_open_write_buffer( stream->s_in, &data, &available); - if (!B_OK(status)) { + if (!FX_OK(status)) { break; } @@ -386,7 +386,7 @@ static enum b_status cstream_write( memcpy(data, src, to_copy); - b_ringbuffer_close_write_buffer(stream->s_in, &data, to_copy); + fx_ringbuffer_close_write_buffer(stream->s_in, &data, to_copy); stream->s_tx_bytes_uncompressed += to_copy; src += to_copy; @@ -394,21 +394,21 @@ static enum b_status cstream_write( remaining -= to_copy; } - if (status == B_ERR_NO_DATA) { - status = B_SUCCESS; + if (status == FX_ERR_NO_DATA) { + status = FX_SUCCESS; } *out_nr_written = nr_written; return status; } -static enum b_status skip_uncompressed( - struct b_cstream_p *stream, size_t count, size_t *out_nr_skipped) +static enum fx_status skip_uncompressed( + struct fx_cstream_p *stream, size_t count, size_t *out_nr_skipped) { size_t remaining = count; size_t nr_read_from_buf = 0; size_t nr_read_from_endpoint = 0; - enum b_status status = B_SUCCESS; + enum fx_status status = FX_SUCCESS; /* liberal usage of begin_compressed_section and end_compressed_section * can result in uncompressed data getting stuck in the input buffer. @@ -418,9 +418,9 @@ static enum b_status skip_uncompressed( while (remaining > 0) { const void *data; size_t available; - status = b_ringbuffer_open_read_buffer( + status = fx_ringbuffer_open_read_buffer( stream->s_in, &data, &available); - if (!B_OK(status)) { + if (!FX_OK(status)) { break; } @@ -429,7 +429,7 @@ static enum b_status skip_uncompressed( to_copy = available; } - b_ringbuffer_close_read_buffer(stream->s_in, &data, to_copy); + fx_ringbuffer_close_read_buffer(stream->s_in, &data, to_copy); stream->s_tx_bytes_uncompressed += to_copy; stream->s_tx_bytes += to_copy; @@ -442,13 +442,13 @@ static enum b_status skip_uncompressed( *out_nr_skipped = nr_read_from_buf; } - return B_SUCCESS; + return FX_SUCCESS; } - size_t cursor = b_stream_cursor(stream->s_endpoint); + size_t cursor = fx_stream_cursor(stream->s_endpoint); - status = b_stream_seek(stream->s_endpoint, remaining, B_STREAM_SEEK_CURRENT); - nr_read_from_endpoint = b_stream_cursor(stream->s_endpoint) - cursor; + status = fx_stream_seek(stream->s_endpoint, remaining, FX_STREAM_SEEK_CURRENT); + nr_read_from_endpoint = fx_stream_cursor(stream->s_endpoint) - cursor; stream->s_tx_bytes_uncompressed += nr_read_from_endpoint; stream->s_tx_bytes += nr_read_from_endpoint; @@ -459,48 +459,48 @@ static enum b_status skip_uncompressed( return status; } -static enum b_status cstream_skip( - struct b_cstream_p *stream, size_t count, size_t *out_nr_skipped) +static enum fx_status cstream_skip( + struct fx_cstream_p *stream, size_t count, size_t *out_nr_skipped) { - if (stream->s_mode != B_COMPRESSOR_MODE_DECOMPRESS) { - return B_ERR_BAD_STATE; + if (stream->s_mode != FX_COMPRESSOR_MODE_DECOMPRESS) { + return FX_ERR_BAD_STATE; } if (stream->s_flags & CSTREAM_CURSOR_MOVED) { - return B_ERR_BAD_STATE; + return FX_ERR_BAD_STATE; } if (stream->s_compression_depth == 0) { return skip_uncompressed(stream, count, out_nr_skipped); } - if (b_compressor_eof(stream->s_compressor) - && !b_ringbuffer_available_data_remaining(stream->s_out)) { + if (fx_compressor_eof(stream->s_compressor) + && !fx_ringbuffer_available_data_remaining(stream->s_out)) { if (out_nr_skipped) { *out_nr_skipped = 0; } - return B_SUCCESS; + return FX_SUCCESS; } size_t nr_read = 0; size_t remaining = count; - enum b_status status = B_SUCCESS; + enum fx_status status = FX_SUCCESS; while (remaining > 0) { - if (!b_ringbuffer_available_data_remaining(stream->s_out)) { + if (!fx_ringbuffer_available_data_remaining(stream->s_out)) { status = refill_output_buffer(stream); } - if (!B_OK(status)) { + if (!FX_OK(status)) { break; } const void *data; size_t available; - status = b_ringbuffer_open_read_buffer( + status = fx_ringbuffer_open_read_buffer( stream->s_out, &data, &available); - if (!B_OK(status)) { + if (!FX_OK(status)) { break; } @@ -509,15 +509,15 @@ static enum b_status cstream_skip( to_copy = available; } - b_ringbuffer_close_read_buffer(stream->s_out, &data, to_copy); + fx_ringbuffer_close_read_buffer(stream->s_out, &data, to_copy); stream->s_tx_bytes_uncompressed += to_copy; nr_read += to_copy; remaining -= to_copy; } - if (status == B_ERR_NO_DATA) { - status = B_SUCCESS; + if (status == FX_ERR_NO_DATA) { + status = FX_SUCCESS; } if (out_nr_skipped) { @@ -527,22 +527,22 @@ static enum b_status cstream_skip( return status; } -static enum b_status cstream_reset(struct b_cstream_p *stream) +static enum fx_status cstream_reset(struct fx_cstream_p *stream) { - if (stream->s_mode != B_COMPRESSOR_MODE_DECOMPRESS) { - return B_ERR_BAD_STATE; + if (stream->s_mode != FX_COMPRESSOR_MODE_DECOMPRESS) { + return FX_ERR_BAD_STATE; } if (stream->s_flags & CSTREAM_CURSOR_MOVED) { - return B_ERR_BAD_STATE; + return FX_ERR_BAD_STATE; } stream->s_flags = 0; - b_stream_seek(stream->s_endpoint, 0, B_STREAM_SEEK_START); - b_ringbuffer_clear(stream->s_in); - b_ringbuffer_clear(stream->s_out); - b_compressor_reset(stream->s_compressor); + fx_stream_seek(stream->s_endpoint, 0, FX_STREAM_SEEK_START); + fx_ringbuffer_clear(stream->s_in); + fx_ringbuffer_clear(stream->s_out); + fx_compressor_reset(stream->s_compressor); stream->s_compression_depth = 0; stream->s_tx_bytes = 0; @@ -550,14 +550,14 @@ static enum b_status cstream_reset(struct b_cstream_p *stream) stream->s_tx_bytes_compressed = 0; stream->s_cursor = 0; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status cstream_begin_compressed_section( - struct b_cstream_p *stream, size_t *tx_uncompressed_bytes) +static enum fx_status cstream_begin_compressed_section( + struct fx_cstream_p *stream, size_t *tx_uncompressed_bytes) { if (stream->s_flags & CSTREAM_CURSOR_MOVED) { - return B_ERR_BAD_STATE; + return FX_ERR_BAD_STATE; } if (tx_uncompressed_bytes) { @@ -566,23 +566,23 @@ static enum b_status cstream_begin_compressed_section( if (stream->s_compression_depth > 0) { stream->s_compression_depth++; - return B_SUCCESS; + return FX_SUCCESS; } stream->s_compression_depth = 1; stream->s_tx_bytes_uncompressed = 0; stream->s_tx_bytes_compressed = 0; - b_compressor_reset(stream->s_compressor); + fx_compressor_reset(stream->s_compressor); - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status cstream_end_compressed_section( - struct b_cstream_p *stream, size_t *tx_compressed_bytes, +static enum fx_status cstream_end_compressed_section( + struct fx_cstream_p *stream, size_t *tx_compressed_bytes, size_t *tx_uncompressed_bytes) { if (stream->s_flags & CSTREAM_CURSOR_MOVED) { - return B_ERR_BAD_STATE; + return FX_ERR_BAD_STATE; } tx_compressed_bytes @@ -593,45 +593,45 @@ static enum b_status cstream_end_compressed_section( if (stream->s_compression_depth > 1) { stream->s_compression_depth--; - return B_SUCCESS; + return FX_SUCCESS; } stream->s_compression_depth = 0; - if (stream->s_mode == B_COMPRESSOR_MODE_DECOMPRESS) { + if (stream->s_mode == FX_COMPRESSOR_MODE_DECOMPRESS) { stream->s_tx_bytes_compressed = 0; stream->s_tx_bytes_uncompressed = 0; - return B_SUCCESS; + return FX_SUCCESS; } - enum b_status status = B_SUCCESS; + enum fx_status status = FX_SUCCESS; while (1) { - status = b_compressor_end(stream->s_compressor); - if (!B_OK(status) && status != B_ERR_NO_SPACE) { + status = fx_compressor_end(stream->s_compressor); + if (!FX_OK(status) && status != FX_ERR_NO_SPACE) { break; } status = flush_output_buffer(stream); - if (!B_OK(status)) { + if (!FX_OK(status)) { break; } - if (b_compressor_eof(stream->s_compressor)) { - status = B_SUCCESS; + if (fx_compressor_eof(stream->s_compressor)) { + status = FX_SUCCESS; break; } } /* refresh these output variables to account for any data - * written by b_compressor_end */ + * written by fx_compressor_end */ tx_compressed_bytes && (*tx_compressed_bytes = stream->s_tx_bytes_compressed); tx_uncompressed_bytes && (*tx_uncompressed_bytes = stream->s_tx_bytes_uncompressed); - if (!B_OK(status)) { + if (!FX_OK(status)) { return status; } @@ -641,250 +641,250 @@ static enum b_status cstream_end_compressed_section( return flush_output_buffer(stream); } -static bool cstream_in_compressed_section(const struct b_cstream_p *stream) +static bool cstream_in_compressed_section(const struct fx_cstream_p *stream) { return stream->s_compression_depth > 0; } -static enum b_status cstream_tx_bytes(const struct b_cstream_p *stream, size_t *out) +static enum fx_status cstream_tx_bytes(const struct fx_cstream_p *stream, size_t *out) { *out = stream->s_tx_bytes; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status cstream_tx_bytes_compressed( - const struct b_cstream_p *stream, size_t *out) +static enum fx_status cstream_tx_bytes_compressed( + const struct fx_cstream_p *stream, size_t *out) { *out = stream->s_tx_bytes_compressed; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status cstream_tx_bytes_uncompressed( - const struct b_cstream_p *stream, size_t *out) +static enum fx_status cstream_tx_bytes_uncompressed( + const struct fx_cstream_p *stream, size_t *out) { *out = stream->s_tx_bytes_uncompressed; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status cstream_set_cursor_position( - struct b_cstream_p *stream, size_t pos) +static enum fx_status cstream_set_cursor_position( + struct fx_cstream_p *stream, size_t pos) { if (stream->s_compression_depth > 0) { - return B_ERR_BAD_STATE; + return FX_ERR_BAD_STATE; } if (stream->s_flags & CSTREAM_CURSOR_MOVED) { - return B_ERR_BAD_STATE; + return FX_ERR_BAD_STATE; } - stream->s_cursor = b_stream_cursor(stream->s_endpoint); + stream->s_cursor = fx_stream_cursor(stream->s_endpoint); - enum b_status status - = b_stream_seek(stream->s_endpoint, pos, B_STREAM_SEEK_START); - if (!B_OK(status)) { + enum fx_status status + = fx_stream_seek(stream->s_endpoint, pos, FX_STREAM_SEEK_START); + if (!FX_OK(status)) { stream->s_cursor = 0; return status; } stream->s_flags |= CSTREAM_CURSOR_MOVED; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status cstream_restore_cursor_position(struct b_cstream_p *stream) +static enum fx_status cstream_restore_cursor_position(struct fx_cstream_p *stream) { if (!(stream->s_flags & CSTREAM_CURSOR_MOVED)) { - return B_ERR_BAD_STATE; + return FX_ERR_BAD_STATE; } - enum b_status status = b_stream_seek( - stream->s_endpoint, stream->s_cursor, B_STREAM_SEEK_START); + enum fx_status status = fx_stream_seek( + stream->s_endpoint, stream->s_cursor, FX_STREAM_SEEK_START); stream->s_cursor = 0; - if (!B_OK(status)) { + if (!FX_OK(status)) { return status; } stream->s_flags &= ~CSTREAM_CURSOR_MOVED; - return B_SUCCESS; + return FX_SUCCESS; } /*** PUBLIC FUNCTIONS *********************************************************/ -enum b_status b_cstream_open( - b_stream *endpoint, b_type compressor_type, b_compressor_mode mode, - b_cstream **out) +enum fx_status fx_cstream_open( + fx_stream *endpoint, fx_type compressor_type, fx_compressor_mode mode, + fx_cstream **out) { size_t inbuf_size = 0, outbuf_size = 0; - enum b_status status = b_compressor_get_buffer_size( + enum fx_status status = fx_compressor_get_buffer_size( compressor_type, mode, &inbuf_size, &outbuf_size); - if (!B_OK(status)) { + if (!FX_OK(status)) { return status; } - b_cstream *stream = b_object_create(B_TYPE_CSTREAM); + fx_cstream *stream = fx_object_create(FX_TYPE_CSTREAM); if (!stream) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } - struct b_cstream_p *p = b_object_get_private(stream, B_TYPE_CSTREAM); - b_stream_cfg *cfg = b_object_get_protected(stream, B_TYPE_STREAM); + struct fx_cstream_p *p = fx_object_get_private(stream, FX_TYPE_CSTREAM); + fx_stream_cfg *cfg = fx_object_get_protected(stream, FX_TYPE_STREAM); p->s_mode = mode; p->s_endpoint = endpoint; - cfg->s_mode = (mode == B_COMPRESSOR_MODE_COMPRESS) ? B_STREAM_WRITE - : B_STREAM_READ; + cfg->s_mode = (mode == FX_COMPRESSOR_MODE_COMPRESS) ? FX_STREAM_WRITE + : FX_STREAM_READ; - p->s_in = b_ringbuffer_create(inbuf_size + 1); - if (!B_OK(status)) { + p->s_in = fx_ringbuffer_create(inbuf_size + 1); + if (!FX_OK(status)) { free(stream); return status; } - p->s_out = b_ringbuffer_create(outbuf_size + 1); - if (!B_OK(status)) { - b_cstream_unref(stream); + p->s_out = fx_ringbuffer_create(outbuf_size + 1); + if (!FX_OK(status)) { + fx_cstream_unref(stream); return status; } - p->s_compressor = b_object_create(compressor_type); + p->s_compressor = fx_object_create(compressor_type); if (!p->s_compressor) { - b_cstream_unref(stream); + fx_cstream_unref(stream); - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } - b_compressor_set_buffer(p->s_compressor, p->s_in, p->s_out); - b_compressor_set_mode(p->s_compressor, mode); + fx_compressor_set_buffer(p->s_compressor, p->s_in, p->s_out); + fx_compressor_set_mode(p->s_compressor, mode); *out = stream; - return B_SUCCESS; + return FX_SUCCESS; } -enum b_status b_cstream_read( - b_cstream *stream, void *buf, size_t count, size_t *out_nr_read) +enum fx_status fx_cstream_read( + fx_cstream *stream, void *buf, size_t count, size_t *out_nr_read) { - B_CLASS_DISPATCH_STATIC( - B_TYPE_CSTREAM, cstream_read, stream, buf, count, out_nr_read); + FX_CLASS_DISPATCH_STATIC( + FX_TYPE_CSTREAM, cstream_read, stream, buf, count, out_nr_read); } -enum b_status b_cstream_write( - b_cstream *stream, const void *buf, size_t count, size_t *out_nr_written) +enum fx_status fx_cstream_write( + fx_cstream *stream, const void *buf, size_t count, size_t *out_nr_written) { - B_CLASS_DISPATCH_STATIC( - B_TYPE_CSTREAM, cstream_write, stream, buf, count, out_nr_written); + FX_CLASS_DISPATCH_STATIC( + FX_TYPE_CSTREAM, cstream_write, stream, buf, count, out_nr_written); } -enum b_status b_cstream_skip(b_cstream *stream, size_t count, size_t *out_nr_skipped) +enum fx_status fx_cstream_skip(fx_cstream *stream, size_t count, size_t *out_nr_skipped) { - B_CLASS_DISPATCH_STATIC( - B_TYPE_CSTREAM, cstream_skip, stream, count, out_nr_skipped); + FX_CLASS_DISPATCH_STATIC( + FX_TYPE_CSTREAM, cstream_skip, stream, count, out_nr_skipped); } -enum b_status b_cstream_reset(b_cstream *stream) +enum fx_status fx_cstream_reset(fx_cstream *stream) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_CSTREAM, cstream_reset, stream); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_CSTREAM, cstream_reset, stream); } -enum b_status b_cstream_begin_compressed_section( - b_cstream *stream, size_t *tx_uncompressed_bytes) +enum fx_status fx_cstream_begin_compressed_section( + fx_cstream *stream, size_t *tx_uncompressed_bytes) { - B_CLASS_DISPATCH_STATIC( - B_TYPE_CSTREAM, cstream_begin_compressed_section, stream, + FX_CLASS_DISPATCH_STATIC( + FX_TYPE_CSTREAM, cstream_begin_compressed_section, stream, tx_uncompressed_bytes); } -enum b_status b_cstream_end_compressed_section( - b_cstream *stream, size_t *tx_compressed_bytes, size_t *tx_uncompressed_bytes) +enum fx_status fx_cstream_end_compressed_section( + fx_cstream *stream, size_t *tx_compressed_bytes, size_t *tx_uncompressed_bytes) { - B_CLASS_DISPATCH_STATIC( - B_TYPE_CSTREAM, cstream_end_compressed_section, stream, + FX_CLASS_DISPATCH_STATIC( + FX_TYPE_CSTREAM, cstream_end_compressed_section, stream, tx_compressed_bytes, tx_uncompressed_bytes); } -bool b_cstream_in_compressed_section(const b_cstream *stream) +bool fx_cstream_in_compressed_section(const fx_cstream *stream) { - B_CLASS_DISPATCH_STATIC_0( - B_TYPE_CSTREAM, cstream_in_compressed_section, stream); + FX_CLASS_DISPATCH_STATIC_0( + FX_TYPE_CSTREAM, cstream_in_compressed_section, stream); } -enum b_status b_cstream_tx_bytes(const b_cstream *stream, size_t *out) +enum fx_status fx_cstream_tx_bytes(const fx_cstream *stream, size_t *out) { - B_CLASS_DISPATCH_STATIC(B_TYPE_CSTREAM, cstream_tx_bytes, stream, out); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_CSTREAM, cstream_tx_bytes, stream, out); } -enum b_status b_cstream_tx_bytes_compressed(const b_cstream *stream, size_t *out) +enum fx_status fx_cstream_tx_bytes_compressed(const fx_cstream *stream, size_t *out) { - B_CLASS_DISPATCH_STATIC( - B_TYPE_CSTREAM, cstream_tx_bytes_compressed, stream, out); + FX_CLASS_DISPATCH_STATIC( + FX_TYPE_CSTREAM, cstream_tx_bytes_compressed, stream, out); } -enum b_status b_cstream_tx_bytes_uncompressed(const b_cstream *stream, size_t *out) +enum fx_status fx_cstream_tx_bytes_uncompressed(const fx_cstream *stream, size_t *out) { - B_CLASS_DISPATCH_STATIC( - B_TYPE_CSTREAM, cstream_tx_bytes_uncompressed, stream, out); + FX_CLASS_DISPATCH_STATIC( + FX_TYPE_CSTREAM, cstream_tx_bytes_uncompressed, stream, out); } -enum b_status b_cstream_set_cursor_position(b_cstream *stream, size_t pos) +enum fx_status fx_cstream_set_cursor_position(fx_cstream *stream, size_t pos) { - B_CLASS_DISPATCH_STATIC( - B_TYPE_CSTREAM, cstream_set_cursor_position, stream, pos); + FX_CLASS_DISPATCH_STATIC( + FX_TYPE_CSTREAM, cstream_set_cursor_position, stream, pos); } -enum b_status b_cstream_restore_cursor_position(b_cstream *stream) +enum fx_status fx_cstream_restore_cursor_position(fx_cstream *stream) { - B_CLASS_DISPATCH_STATIC_0( - B_TYPE_CSTREAM, cstream_restore_cursor_position, stream); + FX_CLASS_DISPATCH_STATIC_0( + FX_TYPE_CSTREAM, cstream_restore_cursor_position, stream); } /*** VIRTUAL FUNCTIONS ********************************************************/ -static void cstream_init(b_object *obj, void *priv) +static void cstream_init(fx_object *obj, void *priv) { } -static void cstream_fini(b_object *obj, void *priv) +static void cstream_fini(fx_object *obj, void *priv) { - struct b_cstream_p *stream = priv; + struct fx_cstream_p *stream = priv; if (stream->s_compressor) { - b_compressor_unref(stream->s_compressor); + fx_compressor_unref(stream->s_compressor); } if (stream->s_in) { - b_ringbuffer_unref(stream->s_in); + fx_ringbuffer_unref(stream->s_in); } if (stream->s_out) { - b_ringbuffer_unref(stream->s_out); + fx_ringbuffer_unref(stream->s_out); } } /*** CLASS DEFINITION *********************************************************/ -B_TYPE_CLASS_DEFINITION_BEGIN(b_cstream) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_cstream) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) - B_TYPE_CLASS_INTERFACE_BEGIN(b_stream, B_TYPE_STREAM) - B_INTERFACE_ENTRY(s_close) = NULL; - B_INTERFACE_ENTRY(s_seek) = NULL; - B_INTERFACE_ENTRY(s_tell) = NULL; - B_INTERFACE_ENTRY(s_getc) = NULL; - B_INTERFACE_ENTRY(s_read) = b_cstream_read; - B_INTERFACE_ENTRY(s_write) = b_cstream_write; - B_INTERFACE_ENTRY(s_reserve) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_stream, B_TYPE_STREAM) -B_TYPE_CLASS_DEFINITION_END(b_cstream) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_stream, FX_TYPE_STREAM) + FX_INTERFACE_ENTRY(s_close) = NULL; + FX_INTERFACE_ENTRY(s_seek) = NULL; + FX_INTERFACE_ENTRY(s_tell) = NULL; + FX_INTERFACE_ENTRY(s_getc) = NULL; + FX_INTERFACE_ENTRY(s_read) = fx_cstream_read; + FX_INTERFACE_ENTRY(s_write) = fx_cstream_write; + FX_INTERFACE_ENTRY(s_reserve) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_stream, FX_TYPE_STREAM) +FX_TYPE_CLASS_DEFINITION_END(fx_cstream) -B_TYPE_DEFINITION_BEGIN(b_cstream) - B_TYPE_ID(0xe1e899b5, 0x6a3c, 0x4f9c, 0xafd0, 0xaab3f156615c); - B_TYPE_EXTENDS(B_TYPE_STREAM); - B_TYPE_CLASS(b_cstream_class); - B_TYPE_INSTANCE_PRIVATE(struct b_cstream_p); - B_TYPE_INSTANCE_INIT(cstream_init); - B_TYPE_INSTANCE_FINI(cstream_fini); -B_TYPE_DEFINITION_END(b_cstream) +FX_TYPE_DEFINITION_BEGIN(fx_cstream) + FX_TYPE_ID(0xe1e899b5, 0x6a3c, 0x4f9c, 0xafd0, 0xaab3f156615c); + FX_TYPE_EXTENDS(FX_TYPE_STREAM); + FX_TYPE_CLASS(fx_cstream_class); + FX_TYPE_INSTANCE_PRIVATE(struct fx_cstream_p); + FX_TYPE_INSTANCE_INIT(cstream_init); + FX_TYPE_INSTANCE_FINI(cstream_fini); +FX_TYPE_DEFINITION_END(fx_cstream) diff --git a/compress/function/zstd.c b/compress/function/zstd.c index 6899510..39b8a4c 100644 --- a/compress/function/zstd.c +++ b/compress/function/zstd.c @@ -1,10 +1,10 @@ -#include -#include +#include +#include #include /*** PRIVATE DATA *************************************************************/ -struct b_zstd_compressor_p { +struct fx_zstd_compressor_p { union { ZSTD_CCtx *zstd_c; ZSTD_DCtx *zstd_d; @@ -13,40 +13,40 @@ struct b_zstd_compressor_p { /*** PUBLIC FUNCTIONS *********************************************************/ -b_status b_zstd_compressor_get_buffer_size( - b_compressor_mode mode, size_t *inbuf_size, size_t *outbuf_size) +fx_status fx_zstd_compressor_get_buffer_size( + fx_compressor_mode mode, size_t *inbuf_size, size_t *outbuf_size) { switch (mode) { - case B_COMPRESSOR_MODE_COMPRESS: + case FX_COMPRESSOR_MODE_COMPRESS: *inbuf_size = ZSTD_CStreamInSize(); *outbuf_size = ZSTD_CStreamOutSize(); break; - case B_COMPRESSOR_MODE_DECOMPRESS: + case FX_COMPRESSOR_MODE_DECOMPRESS: *inbuf_size = ZSTD_DStreamInSize(); *outbuf_size = ZSTD_DStreamOutSize(); break; default: - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } - return B_SUCCESS; + return FX_SUCCESS; } /*** VIRTUAL FUNCTIONS ********************************************************/ -static void zstd_compressor_init(b_object *obj, void *priv) +static void zstd_compressor_init(fx_object *obj, void *priv) { } -static void zstd_compressor_fini(b_object *obj, void *priv) +static void zstd_compressor_fini(fx_object *obj, void *priv) { - b_compressor_data *c = b_object_get_protected(obj, B_TYPE_COMPRESSOR); - struct b_zstd_compressor_p *ctx = priv; + fx_compressor_data *c = fx_object_get_protected(obj, FX_TYPE_COMPRESSOR); + struct fx_zstd_compressor_p *ctx = priv; switch (c->c_mode) { - case B_COMPRESSOR_MODE_COMPRESS: + case FX_COMPRESSOR_MODE_COMPRESS: ZSTD_freeCCtx(ctx->zstd_c); break; - case B_COMPRESSOR_MODE_DECOMPRESS: + case FX_COMPRESSOR_MODE_DECOMPRESS: ZSTD_freeDCtx(ctx->zstd_d); break; default: @@ -54,52 +54,52 @@ static void zstd_compressor_fini(b_object *obj, void *priv) } } -static enum b_status reset(b_compressor *compressor) +static enum fx_status reset(fx_compressor *compressor) { - struct b_zstd_compressor_p *ctx - = b_object_get_private(compressor, B_TYPE_ZSTD_COMPRESSOR); - b_compressor_data *data - = b_object_get_protected(compressor, B_TYPE_COMPRESSOR); + struct fx_zstd_compressor_p *ctx + = fx_object_get_private(compressor, FX_TYPE_ZSTD_COMPRESSOR); + fx_compressor_data *data + = fx_object_get_protected(compressor, FX_TYPE_COMPRESSOR); if (!ctx || !data) { - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } switch (data->c_mode) { - case B_COMPRESSOR_MODE_COMPRESS: + case FX_COMPRESSOR_MODE_COMPRESS: ZSTD_CCtx_reset(ctx->zstd_c, ZSTD_reset_session_only); break; - case B_COMPRESSOR_MODE_DECOMPRESS: + case FX_COMPRESSOR_MODE_DECOMPRESS: ZSTD_DCtx_reset(ctx->zstd_d, ZSTD_reset_session_only); break; default: - return B_ERR_BAD_STATE; + return FX_ERR_BAD_STATE; } - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status compress(b_compressor *compressor) +static enum fx_status compress(fx_compressor *compressor) { - enum b_status status = B_SUCCESS; - struct b_zstd_compressor_p *ctx - = b_object_get_private(compressor, B_TYPE_ZSTD_COMPRESSOR); - b_compressor_data *data - = b_object_get_protected(compressor, B_TYPE_COMPRESSOR); + enum fx_status status = FX_SUCCESS; + struct fx_zstd_compressor_p *ctx + = fx_object_get_private(compressor, FX_TYPE_ZSTD_COMPRESSOR); + fx_compressor_data *data + = fx_object_get_protected(compressor, FX_TYPE_COMPRESSOR); if (!ctx || !data) { - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } - b_ringbuffer *in = data->c_in; - b_ringbuffer *out = data->c_out; + fx_ringbuffer *in = data->c_in; + fx_ringbuffer *out = data->c_out; - if (b_ringbuffer_available_data_remaining(in) == 0) { - return B_ERR_NO_DATA; + if (fx_ringbuffer_available_data_remaining(in) == 0) { + return FX_ERR_NO_DATA; } - if (b_ringbuffer_write_capacity_remaining(out) == 0) { - return B_ERR_NO_SPACE; + if (fx_ringbuffer_write_capacity_remaining(out) == 0) { + return FX_ERR_NO_SPACE; } size_t nr_consumed = 0; @@ -109,15 +109,15 @@ static enum b_status compress(b_compressor *compressor) const void *in_buf = NULL; void *out_buf = NULL; - status = b_ringbuffer_open_read_buffer(in, &in_buf, &in_available); - if (!B_OK(status)) { + status = fx_ringbuffer_open_read_buffer(in, &in_buf, &in_available); + if (!FX_OK(status)) { break; } - status = b_ringbuffer_open_write_buffer( + status = fx_ringbuffer_open_write_buffer( out, &out_buf, &out_capacity); - if (!B_OK(status)) { - b_ringbuffer_close_read_buffer(in, &in_buf, 0); + if (!FX_OK(status)) { + fx_ringbuffer_close_read_buffer(in, &in_buf, 0); break; } @@ -137,49 +137,49 @@ static enum b_status compress(b_compressor *compressor) size_t ret = ZSTD_compressStream2( ctx->zstd_c, &z_out, &z_in, ZSTD_e_continue); if (ZSTD_isError(ret)) { - status = B_ERR_COMPRESSION_FAILURE; + status = FX_ERR_COMPRESSION_FAILURE; break; } } while (z_in.pos < z_in.size && z_out.pos < z_out.size); nr_consumed += z_in.pos; - b_ringbuffer_close_read_buffer(in, &in_buf, z_in.pos); - b_ringbuffer_close_write_buffer(out, &out_buf, z_out.pos); + fx_ringbuffer_close_read_buffer(in, &in_buf, z_in.pos); + fx_ringbuffer_close_write_buffer(out, &out_buf, z_out.pos); } - if ((status == B_ERR_NO_SPACE || status == B_ERR_NO_DATA) + if ((status == FX_ERR_NO_SPACE || status == FX_ERR_NO_DATA) && nr_consumed > 0) { - status = B_SUCCESS; + status = FX_SUCCESS; } return status; } -static enum b_status compress_end(b_compressor *compressor) +static enum fx_status compress_end(fx_compressor *compressor) { - enum b_status status = B_SUCCESS; - struct b_zstd_compressor_p *ctx - = b_object_get_private(compressor, B_TYPE_ZSTD_COMPRESSOR); - b_compressor_data *data - = b_object_get_protected(compressor, B_TYPE_COMPRESSOR); + enum fx_status status = FX_SUCCESS; + struct fx_zstd_compressor_p *ctx + = fx_object_get_private(compressor, FX_TYPE_ZSTD_COMPRESSOR); + fx_compressor_data *data + = fx_object_get_protected(compressor, FX_TYPE_COMPRESSOR); if (!ctx || !data) { - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } - b_ringbuffer *out = data->c_out; - if (b_ringbuffer_write_capacity_remaining(out) == 0) { - return B_ERR_NO_SPACE; + fx_ringbuffer *out = data->c_out; + if (fx_ringbuffer_write_capacity_remaining(out) == 0) { + return FX_ERR_NO_SPACE; } bool finished = false; do { void *out_buf = NULL; size_t out_capacity = 0; - status = b_ringbuffer_open_write_buffer( + status = fx_ringbuffer_open_write_buffer( out, &out_buf, &out_capacity); - if (!B_OK(status)) { + if (!FX_OK(status)) { break; } @@ -194,61 +194,61 @@ static enum b_status compress_end(b_compressor *compressor) size_t ret = ZSTD_compressStream2( ctx->zstd_c, &z_out, &z_in, ZSTD_e_end); if (ZSTD_isError(ret)) { - status = B_ERR_COMPRESSION_FAILURE; + status = FX_ERR_COMPRESSION_FAILURE; finished = true; } if (ret == 0) { - data->c_flags |= B_COMPRESSOR_EOF; + data->c_flags |= FX_COMPRESSOR_EOF; finished = true; } } while (!finished && z_out.pos < z_out.size); - b_ringbuffer_close_write_buffer(out, &out_buf, z_out.pos); + fx_ringbuffer_close_write_buffer(out, &out_buf, z_out.pos); } while (!finished); return status; } -static enum b_status decompress(b_compressor *compressor) +static enum fx_status decompress(fx_compressor *compressor) { - enum b_status status = B_SUCCESS; - struct b_zstd_compressor_p *ctx - = b_object_get_private(compressor, B_TYPE_ZSTD_COMPRESSOR); - b_compressor_data *data - = b_object_get_protected(compressor, B_TYPE_COMPRESSOR); + enum fx_status status = FX_SUCCESS; + struct fx_zstd_compressor_p *ctx + = fx_object_get_private(compressor, FX_TYPE_ZSTD_COMPRESSOR); + fx_compressor_data *data + = fx_object_get_protected(compressor, FX_TYPE_COMPRESSOR); if (!ctx || !data) { - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } - b_ringbuffer *in = data->c_in; - b_ringbuffer *out = data->c_out; + fx_ringbuffer *in = data->c_in; + fx_ringbuffer *out = data->c_out; - if (b_ringbuffer_available_data_remaining(in) == 0) { - return B_ERR_NO_DATA; + if (fx_ringbuffer_available_data_remaining(in) == 0) { + return FX_ERR_NO_DATA; } - if (b_ringbuffer_write_capacity_remaining(out) == 0) { - return B_ERR_NO_SPACE; + if (fx_ringbuffer_write_capacity_remaining(out) == 0) { + return FX_ERR_NO_SPACE; } size_t nr_consumed = 0; - while (!(data->c_flags & B_COMPRESSOR_EOF)) { + while (!(data->c_flags & FX_COMPRESSOR_EOF)) { size_t in_available = 0, out_capacity = 0; const void *in_buf = NULL; void *out_buf = NULL; - status = b_ringbuffer_open_read_buffer(in, &in_buf, &in_available); - if (!B_OK(status)) { + status = fx_ringbuffer_open_read_buffer(in, &in_buf, &in_available); + if (!FX_OK(status)) { break; } - status = b_ringbuffer_open_write_buffer( + status = fx_ringbuffer_open_write_buffer( out, &out_buf, &out_capacity); - if (!B_OK(status)) { - b_ringbuffer_close_read_buffer(in, &in_buf, 0); + if (!FX_OK(status)) { + fx_ringbuffer_close_read_buffer(in, &in_buf, 0); break; } @@ -268,50 +268,50 @@ static enum b_status decompress(b_compressor *compressor) size_t ret = ZSTD_decompressStream( ctx->zstd_d, &z_out, &z_in); if (ZSTD_isError(ret)) { - status = B_ERR_COMPRESSION_FAILURE; + status = FX_ERR_COMPRESSION_FAILURE; break; } if (ret == 0) { - data->c_flags |= B_COMPRESSOR_EOF; + data->c_flags |= FX_COMPRESSOR_EOF; break; } } while (z_in.pos < z_in.size && z_out.pos < z_out.size); nr_consumed += z_in.pos; - b_ringbuffer_close_read_buffer(in, &in_buf, z_in.pos); - b_ringbuffer_close_write_buffer(out, &out_buf, z_out.pos); + fx_ringbuffer_close_read_buffer(in, &in_buf, z_in.pos); + fx_ringbuffer_close_write_buffer(out, &out_buf, z_out.pos); } - if ((status == B_ERR_NO_SPACE || status == B_ERR_NO_DATA) + if ((status == FX_ERR_NO_SPACE || status == FX_ERR_NO_DATA) && nr_consumed > 0) { - status = B_SUCCESS; + status = FX_SUCCESS; } return status; } -static enum b_status set_mode(b_compressor *compressor, b_compressor_mode mode) +static enum fx_status set_mode(fx_compressor *compressor, fx_compressor_mode mode) { - struct b_zstd_compressor_p *ctx - = b_object_get_private(compressor, B_TYPE_ZSTD_COMPRESSOR); - b_compressor_data *data - = b_object_get_protected(compressor, B_TYPE_COMPRESSOR); + struct fx_zstd_compressor_p *ctx + = fx_object_get_private(compressor, FX_TYPE_ZSTD_COMPRESSOR); + fx_compressor_data *data + = fx_object_get_protected(compressor, FX_TYPE_COMPRESSOR); if (!ctx || !data) { - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } if (mode == data->c_mode) { - return B_SUCCESS; + return FX_SUCCESS; } switch (data->c_mode) { - case B_COMPRESSOR_MODE_COMPRESS: + case FX_COMPRESSOR_MODE_COMPRESS: ZSTD_freeCCtx(ctx->zstd_c); break; - case B_COMPRESSOR_MODE_DECOMPRESS: + case FX_COMPRESSOR_MODE_DECOMPRESS: ZSTD_freeDCtx(ctx->zstd_d); break; default: @@ -321,42 +321,42 @@ static enum b_status set_mode(b_compressor *compressor, b_compressor_mode mode) data->c_mode = mode; switch (data->c_mode) { - case B_COMPRESSOR_MODE_COMPRESS: + case FX_COMPRESSOR_MODE_COMPRESS: ctx->zstd_c = ZSTD_createCCtx(); break; - case B_COMPRESSOR_MODE_DECOMPRESS: + case FX_COMPRESSOR_MODE_DECOMPRESS: ctx->zstd_d = ZSTD_createDCtx(); break; default: - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } - return B_SUCCESS; + return FX_SUCCESS; } /*** CLASS DEFINITION *********************************************************/ -B_TYPE_CLASS_DEFINITION_BEGIN(b_zstd_compressor) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_zstd_compressor) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) - B_TYPE_CLASS_INTERFACE_BEGIN(b_compressor, B_TYPE_COMPRESSOR) - B_INTERFACE_ENTRY(c_buffer_size) - = b_zstd_compressor_get_buffer_size; - B_INTERFACE_ENTRY(c_compress) = compress; - B_INTERFACE_ENTRY(c_compress_end) = compress_end; - B_INTERFACE_ENTRY(c_decompress) = decompress; - B_INTERFACE_ENTRY(c_reset) = reset; - B_INTERFACE_ENTRY(c_set_mode) = set_mode; - B_TYPE_CLASS_INTERFACE_END(b_compressor, B_TYPE_COMPRESSOR) -B_TYPE_CLASS_DEFINITION_END(b_zstd_compressor) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_compressor, FX_TYPE_COMPRESSOR) + FX_INTERFACE_ENTRY(c_buffer_size) + = fx_zstd_compressor_get_buffer_size; + FX_INTERFACE_ENTRY(c_compress) = compress; + FX_INTERFACE_ENTRY(c_compress_end) = compress_end; + FX_INTERFACE_ENTRY(c_decompress) = decompress; + FX_INTERFACE_ENTRY(c_reset) = reset; + FX_INTERFACE_ENTRY(c_set_mode) = set_mode; + FX_TYPE_CLASS_INTERFACE_END(fx_compressor, FX_TYPE_COMPRESSOR) +FX_TYPE_CLASS_DEFINITION_END(fx_zstd_compressor) -B_TYPE_DEFINITION_BEGIN(b_zstd_compressor) - B_TYPE_ID(0x51d437fc, 0xe789, 0x4105, 0xbac7, 0xe6b3f45df198); - B_TYPE_EXTENDS(B_TYPE_COMPRESSOR); - B_TYPE_CLASS(b_zstd_compressor_class); - B_TYPE_INSTANCE_PRIVATE(struct b_zstd_compressor_p); - B_TYPE_INSTANCE_INIT(zstd_compressor_init); - B_TYPE_INSTANCE_FINI(zstd_compressor_fini); -B_TYPE_DEFINITION_END(b_zstd_compressor) +FX_TYPE_DEFINITION_BEGIN(fx_zstd_compressor) + FX_TYPE_ID(0x51d437fc, 0xe789, 0x4105, 0xbac7, 0xe6b3f45df198); + FX_TYPE_EXTENDS(FX_TYPE_COMPRESSOR); + FX_TYPE_CLASS(fx_zstd_compressor_class); + FX_TYPE_INSTANCE_PRIVATE(struct fx_zstd_compressor_p); + FX_TYPE_INSTANCE_INIT(zstd_compressor_init); + FX_TYPE_INSTANCE_FINI(zstd_compressor_fini); +FX_TYPE_DEFINITION_END(fx_zstd_compressor) diff --git a/compress/include/blue/compress/compressor.h b/compress/include/blue/compress/compressor.h deleted file mode 100644 index ef11f11..0000000 --- a/compress/include/blue/compress/compressor.h +++ /dev/null @@ -1,67 +0,0 @@ -#ifndef BLUE_COMPRESS_COMPRESSOR_H_ -#define BLUE_COMPRESS_COMPRESSOR_H_ - -#include -#include -#include -#include -#include - -B_DECLS_BEGIN; - -typedef enum b_compressor_mode { - B_COMPRESSOR_MODE_NONE = 0, - B_COMPRESSOR_MODE_COMPRESS, - B_COMPRESSOR_MODE_DECOMPRESS, -} b_compressor_mode; - -typedef enum b_compressor_flags { - B_COMPRESSOR_EOF = 0x01u, -} b_compressor_flags; - -#define B_TYPE_COMPRESSOR (b_compressor_get_type()) - -B_DECLARE_TYPE(b_compressor); - -B_TYPE_CLASS_DECLARATION_BEGIN(b_compressor) - b_status (*c_buffer_size)(b_compressor_mode, size_t *, size_t *); - b_status (*c_set_mode)(b_compressor *, b_compressor_mode); - b_status (*c_compress)(b_compressor *); - b_status (*c_compress_end)(b_compressor *); - b_status (*c_decompress)(b_compressor *); - b_status (*c_reset)(b_compressor *); -B_TYPE_CLASS_DECLARATION_END(b_compressor) - -typedef struct b_compressor_data { - b_compressor_flags c_flags; - b_compressor_mode c_mode; - b_ringbuffer *c_in, *c_out; -} b_compressor_data; - -BLUE_API b_type b_compressor_get_type(void); - -#if 0 -BLUE_API b_status b_compressor_create( - const struct b_compression_function *func, enum b_compression_mode mode, - struct b_ringbuffer *inbuf, struct b_ringbuffer *outbuf, - b_compressor **out); -#endif - -BLUE_API b_status b_compressor_get_buffer_size( - b_type type, b_compressor_mode mode, size_t *inbuf_size, - size_t *outbuf_size); - -BLUE_API b_status b_compressor_get_mode( - const b_compressor *compressor, b_compressor_mode *out); -BLUE_API b_status b_compressor_set_mode( - b_compressor *compressor, b_compressor_mode mode); -BLUE_API b_status b_compressor_set_buffer( - b_compressor *compressor, b_ringbuffer *inbuf, b_ringbuffer *outbuf); -BLUE_API b_status b_compressor_step(b_compressor *compressor); -BLUE_API b_status b_compressor_end(b_compressor *compressor); -BLUE_API b_status b_compressor_reset(b_compressor *compressor); -BLUE_API bool b_compressor_eof(const b_compressor *compressor); - -B_DECLS_END; - -#endif diff --git a/compress/include/blue/compress/cstream.h b/compress/include/blue/compress/cstream.h deleted file mode 100644 index f382326..0000000 --- a/compress/include/blue/compress/cstream.h +++ /dev/null @@ -1,50 +0,0 @@ -#ifndef BLUE_COMPRESS_CSTREAM_H_ -#define BLUE_COMPRESS_CSTREAM_H_ - -#include -#include -#include - -B_DECLS_BEGIN; - -enum b_compressor_mode; - -#define B_TYPE_CSTREAM (b_cstream_get_type()) - -B_DECLARE_TYPE(b_cstream); - -B_TYPE_CLASS_DECLARATION_BEGIN(b_cstream) -B_TYPE_CLASS_DECLARATION_END(b_cstream) - -BLUE_API b_type b_cstream_get_type(void); - -BLUE_API b_status b_cstream_open( - b_stream *endpoint, b_type compressor_type, enum b_compressor_mode mode, - b_cstream **out); - -BLUE_API b_status b_cstream_read( - b_cstream *stream, void *buf, size_t count, size_t *nr_read); -BLUE_API b_status b_cstream_write( - b_cstream *stream, const void *buf, size_t count, size_t *nr_written); -BLUE_API b_status b_cstream_skip( - b_cstream *stream, size_t count, size_t *nr_skipped); -BLUE_API b_status b_cstream_reset(b_cstream *stream); - -BLUE_API b_status b_cstream_begin_compressed_section( - b_cstream *stream, size_t *tx_uncompressed_bytes); -BLUE_API b_status b_cstream_end_compressed_section( - b_cstream *stream, size_t *tx_compressed_bytes, - size_t *tx_uncompressed_bytes); -BLUE_API bool b_cstream_in_compressed_section(const b_cstream *stream); -BLUE_API b_status b_cstream_tx_bytes(const b_cstream *stream, size_t *out); -BLUE_API b_status b_cstream_tx_bytes_compressed( - const b_cstream *stream, size_t *out); -BLUE_API b_status b_cstream_tx_bytes_uncompressed( - const b_cstream *stream, size_t *out); - -BLUE_API b_status b_cstream_set_cursor_position(b_cstream *stream, size_t pos); -BLUE_API b_status b_cstream_restore_cursor_position(b_cstream *stream); - -B_DECLS_END; - -#endif diff --git a/compress/include/blue/compress/zstd.h b/compress/include/blue/compress/zstd.h deleted file mode 100644 index 45af52d..0000000 --- a/compress/include/blue/compress/zstd.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef BLUE_COMPRESS_ZSTD_H_ -#define BLUE_COMPRESS_ZSTD_H_ - -#include -#include -#include -#include -#include - -B_DECLS_BEGIN; - -#define B_TYPE_ZSTD_COMPRESSOR (b_zstd_compressor_get_type()) - -B_DECLARE_TYPE(b_zstd_compressor); - -B_TYPE_CLASS_DECLARATION_BEGIN(b_zstd_compressor) -B_TYPE_CLASS_DECLARATION_END(b_compressor) - -BLUE_API b_type b_zstd_compressor_get_type(void); - -BLUE_API b_status b_zstd_compressor_get_buffer_size( - b_compressor_mode mode, size_t *inbuf_size, size_t *outbuf_size); - -B_TYPE_DEFAULT_CONSTRUCTOR(b_zstd_compressor, B_TYPE_ZSTD_COMPRESSOR); - -B_DECLS_END; - -#endif diff --git a/compress/include/blue/compress.h b/compress/include/fx/compress.h similarity index 100% rename from compress/include/blue/compress.h rename to compress/include/fx/compress.h diff --git a/compress/include/fx/compress/compressor.h b/compress/include/fx/compress/compressor.h new file mode 100644 index 0000000..573d567 --- /dev/null +++ b/compress/include/fx/compress/compressor.h @@ -0,0 +1,67 @@ +#ifndef FX_COMPRESS_COMPRESSOR_H_ +#define FX_COMPRESS_COMPRESSOR_H_ + +#include +#include +#include +#include +#include + +FX_DECLS_BEGIN; + +typedef enum fx_compressor_mode { + FX_COMPRESSOR_MODE_NONE = 0, + FX_COMPRESSOR_MODE_COMPRESS, + FX_COMPRESSOR_MODE_DECOMPRESS, +} fx_compressor_mode; + +typedef enum fx_compressor_flags { + FX_COMPRESSOR_EOF = 0x01u, +} fx_compressor_flags; + +#define FX_TYPE_COMPRESSOR (fx_compressor_get_type()) + +FX_DECLARE_TYPE(fx_compressor); + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_compressor) + fx_status (*c_buffer_size)(fx_compressor_mode, size_t *, size_t *); + fx_status (*c_set_mode)(fx_compressor *, fx_compressor_mode); + fx_status (*c_compress)(fx_compressor *); + fx_status (*c_compress_end)(fx_compressor *); + fx_status (*c_decompress)(fx_compressor *); + fx_status (*c_reset)(fx_compressor *); +FX_TYPE_CLASS_DECLARATION_END(fx_compressor) + +typedef struct fx_compressor_data { + fx_compressor_flags c_flags; + fx_compressor_mode c_mode; + fx_ringbuffer *c_in, *c_out; +} fx_compressor_data; + +FX_API fx_type fx_compressor_get_type(void); + +#if 0 +FX_API fx_status fx_compressor_create( + const struct fx_compression_function *func, enum fx_compression_mode mode, + struct fx_ringbuffer *inbuf, struct fx_ringbuffer *outbuf, + fx_compressor **out); +#endif + +FX_API fx_status fx_compressor_get_buffer_size( + fx_type type, fx_compressor_mode mode, size_t *inbuf_size, + size_t *outbuf_size); + +FX_API fx_status fx_compressor_get_mode( + const fx_compressor *compressor, fx_compressor_mode *out); +FX_API fx_status fx_compressor_set_mode( + fx_compressor *compressor, fx_compressor_mode mode); +FX_API fx_status fx_compressor_set_buffer( + fx_compressor *compressor, fx_ringbuffer *inbuf, fx_ringbuffer *outbuf); +FX_API fx_status fx_compressor_step(fx_compressor *compressor); +FX_API fx_status fx_compressor_end(fx_compressor *compressor); +FX_API fx_status fx_compressor_reset(fx_compressor *compressor); +FX_API bool fx_compressor_eof(const fx_compressor *compressor); + +FX_DECLS_END; + +#endif diff --git a/compress/include/fx/compress/cstream.h b/compress/include/fx/compress/cstream.h new file mode 100644 index 0000000..4476adf --- /dev/null +++ b/compress/include/fx/compress/cstream.h @@ -0,0 +1,50 @@ +#ifndef FX_COMPRESS_CSTREAM_H_ +#define FX_COMPRESS_CSTREAM_H_ + +#include +#include +#include + +FX_DECLS_BEGIN; + +enum fx_compressor_mode; + +#define FX_TYPE_CSTREAM (fx_cstream_get_type()) + +FX_DECLARE_TYPE(fx_cstream); + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_cstream) +FX_TYPE_CLASS_DECLARATION_END(fx_cstream) + +FX_API fx_type fx_cstream_get_type(void); + +FX_API fx_status fx_cstream_open( + fx_stream *endpoint, fx_type compressor_type, enum fx_compressor_mode mode, + fx_cstream **out); + +FX_API fx_status fx_cstream_read( + fx_cstream *stream, void *buf, size_t count, size_t *nr_read); +FX_API fx_status fx_cstream_write( + fx_cstream *stream, const void *buf, size_t count, size_t *nr_written); +FX_API fx_status fx_cstream_skip( + fx_cstream *stream, size_t count, size_t *nr_skipped); +FX_API fx_status fx_cstream_reset(fx_cstream *stream); + +FX_API fx_status fx_cstream_begin_compressed_section( + fx_cstream *stream, size_t *tx_uncompressed_bytes); +FX_API fx_status fx_cstream_end_compressed_section( + fx_cstream *stream, size_t *tx_compressed_bytes, + size_t *tx_uncompressed_bytes); +FX_API bool fx_cstream_in_compressed_section(const fx_cstream *stream); +FX_API fx_status fx_cstream_tx_bytes(const fx_cstream *stream, size_t *out); +FX_API fx_status fx_cstream_tx_bytes_compressed( + const fx_cstream *stream, size_t *out); +FX_API fx_status fx_cstream_tx_bytes_uncompressed( + const fx_cstream *stream, size_t *out); + +FX_API fx_status fx_cstream_set_cursor_position(fx_cstream *stream, size_t pos); +FX_API fx_status fx_cstream_restore_cursor_position(fx_cstream *stream); + +FX_DECLS_END; + +#endif diff --git a/compress/include/fx/compress/zstd.h b/compress/include/fx/compress/zstd.h new file mode 100644 index 0000000..c556028 --- /dev/null +++ b/compress/include/fx/compress/zstd.h @@ -0,0 +1,28 @@ +#ifndef FX_COMPRESS_ZSTD_H_ +#define FX_COMPRESS_ZSTD_H_ + +#include +#include +#include +#include +#include + +FX_DECLS_BEGIN; + +#define FX_TYPE_ZSTD_COMPRESSOR (fx_zstd_compressor_get_type()) + +FX_DECLARE_TYPE(fx_zstd_compressor); + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_zstd_compressor) +FX_TYPE_CLASS_DECLARATION_END(fx_compressor) + +FX_API fx_type fx_zstd_compressor_get_type(void); + +FX_API fx_status fx_zstd_compressor_get_buffer_size( + fx_compressor_mode mode, size_t *inbuf_size, size_t *outbuf_size); + +FX_TYPE_DEFAULT_CONSTRUCTOR(fx_zstd_compressor, FX_TYPE_ZSTD_COMPRESSOR); + +FX_DECLS_END; + +#endif diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index cea53d3..d74b11b 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -1,5 +1,5 @@ include(../cmake/Templates.cmake) -add_bluelib_module( +add_fx_module( NAME core SUBDIRS hash) diff --git a/core/bstr.c b/core/bstr.c index 6a0dc8f..84871f1 100644 --- a/core/bstr.c +++ b/core/bstr.c @@ -1,37 +1,37 @@ #include "printf.h" -#include -#include +#include +#include #include #include #include #include #define CHECK_FLAG(str, f) ((((str)->bstr_flags) & (f)) == (f)) -#define IS_DYNAMIC(p) (CHECK_FLAG(p, B_BSTR_F_ALLOC)) +#define IS_DYNAMIC(p) (CHECK_FLAG(p, FX_BSTR_F_ALLOC)) /* number of bytes that bstr_buf is extended by when required */ #define CAPACITY_STEP 32 -void b_bstr_begin(struct b_bstr *str, char *buf, size_t max) +void fx_bstr_begin(struct fx_bstr *str, char *buf, size_t max) { memset(str, 0x0, sizeof *str); - str->bstr_magic = B_BSTR_MAGIC; + str->bstr_magic = FX_BSTR_MAGIC; str->bstr_buf = buf; str->bstr_capacity = max; str->bstr_len = 0; - str->bstr_flags = B_BSTR_F_NONE; + str->bstr_flags = FX_BSTR_F_NONE; } -void b_bstr_begin_dynamic(struct b_bstr *str) +void fx_bstr_begin_dynamic(struct fx_bstr *str) { memset(str, 0x0, sizeof *str); - str->bstr_magic = B_BSTR_MAGIC; + str->bstr_magic = FX_BSTR_MAGIC; str->bstr_buf = NULL; str->bstr_capacity = 0; str->bstr_len = 0; - str->bstr_flags = B_BSTR_F_ALLOC; + str->bstr_flags = FX_BSTR_F_ALLOC; } static char *truncate_buffer(char *s, size_t len) @@ -50,7 +50,7 @@ static char *truncate_buffer(char *s, size_t len) return final; } -char *b_bstr_end(struct b_bstr *str) +char *fx_bstr_end(struct fx_bstr *str) { char *out = str->bstr_buf; size_t len = str->bstr_len; @@ -70,29 +70,29 @@ char *b_bstr_end(struct b_bstr *str) return out; } -enum b_status b_bstr_reserve(struct b_bstr *strv, size_t len) +enum fx_status fx_bstr_reserve(struct fx_bstr *strv, size_t len) { if (!IS_DYNAMIC(strv)) { - return B_SUCCESS; + return FX_SUCCESS; } if (strv->bstr_capacity > 0 && strv->bstr_capacity - 1 >= len) { - return B_SUCCESS; + return FX_SUCCESS; } char *new_buf = realloc(strv->bstr_buf, len + 1); if (!new_buf) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } strv->bstr_buf = new_buf; strv->bstr_capacity = len + 1; strv->bstr_buf[strv->bstr_len] = '\0'; - return B_SUCCESS; + return FX_SUCCESS; } -static int current_indent(struct b_bstr *str) +static int current_indent(struct fx_bstr *str) { if (!str->bstr_istack || !str->bstr_istack_size) { return 0; @@ -101,7 +101,7 @@ static int current_indent(struct b_bstr *str) return str->bstr_istack[str->bstr_istack_ptr]; } -static void __formatter_putchar(struct b_bstr *str, char c) +static void __formatter_putchar(struct fx_bstr *str, char c) { if (str->bstr_capacity > 0 && str->bstr_len < str->bstr_capacity - 1) { str->bstr_buf[str->bstr_len++] = c; @@ -109,7 +109,7 @@ static void __formatter_putchar(struct b_bstr *str, char c) return; } - if (!CHECK_FLAG(str, B_BSTR_F_ALLOC)) { + if (!CHECK_FLAG(str, FX_BSTR_F_ALLOC)) { return; } @@ -127,7 +127,7 @@ static void __formatter_putchar(struct b_bstr *str, char c) str->bstr_buf[str->bstr_len] = '\0'; } -static void formatter_putchar(struct b_bstr *f, char c) +static void formatter_putchar(struct fx_bstr *f, char c) { if (f->bstr_add_indent && c != '\n') { int indent = current_indent(f); @@ -148,12 +148,12 @@ static void formatter_putchar(struct b_bstr *f, char c) static void bstr_fctprintf(char c, void *arg) { - struct b_bstr *str = arg; + struct fx_bstr *str = arg; formatter_putchar(str, c); } -static enum b_status bstr_putcs( - struct b_bstr *str, const char *s, size_t len, size_t *nr_written) +static enum fx_status bstr_putcs( + struct fx_bstr *str, const char *s, size_t len, size_t *nr_written) { for (size_t i = 0; i < len; i++) { formatter_putchar(str, s[i]); @@ -163,10 +163,10 @@ static enum b_status bstr_putcs( *nr_written = len; } - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status bstr_puts(struct b_bstr *str, const char *s, size_t *nr_written) +static enum fx_status bstr_puts(struct fx_bstr *str, const char *s, size_t *nr_written) { size_t i; for (i = 0; s[i]; i++) { @@ -177,10 +177,10 @@ static enum b_status bstr_puts(struct b_bstr *str, const char *s, size_t *nr_wri *nr_written = i; } - return B_SUCCESS; + return FX_SUCCESS; } -enum b_status b_bstr_push_indent(struct b_bstr *str, int indent) +enum fx_status fx_bstr_push_indent(struct fx_bstr *str, int indent) { if (!str->bstr_istack) { str->bstr_istack = calloc(4, sizeof(int)); @@ -193,7 +193,7 @@ enum b_status b_bstr_push_indent(struct b_bstr *str, int indent) str->bstr_istack, (str->bstr_istack_size + 4) * sizeof(int)); if (!buf) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } str->bstr_istack = buf; @@ -203,47 +203,47 @@ enum b_status b_bstr_push_indent(struct b_bstr *str, int indent) int cur_indent = str->bstr_istack[str->bstr_istack_ptr]; str->bstr_istack[++str->bstr_istack_ptr] = cur_indent + indent; - return B_SUCCESS; + return FX_SUCCESS; } -enum b_status b_bstr_pop_indent(struct b_bstr *strv) +enum fx_status fx_bstr_pop_indent(struct fx_bstr *strv) { if (strv->bstr_istack_ptr > 0) { strv->bstr_istack_ptr--; } - return B_SUCCESS; + return FX_SUCCESS; } -enum b_status b_bstr_write_char(struct b_bstr *str, char c) +enum fx_status fx_bstr_write_char(struct fx_bstr *str, char c) { formatter_putchar(str, c); - return B_SUCCESS; + return FX_SUCCESS; } -enum b_status b_bstr_write_chars( - struct b_bstr *str, const char *s, size_t len, size_t *nr_written) +enum fx_status fx_bstr_write_chars( + struct fx_bstr *str, const char *s, size_t len, size_t *nr_written) { return bstr_putcs(str, s, len, nr_written); } -enum b_status b_bstr_write_cstr(struct b_bstr *str, const char *s, size_t *nr_written) +enum fx_status fx_bstr_write_cstr(struct fx_bstr *str, const char *s, size_t *nr_written) { return bstr_puts(str, s, nr_written); } -enum b_status b_bstr_write_cstr_list( - struct b_bstr *str, const char **strs, size_t *nr_written) +enum fx_status fx_bstr_write_cstr_list( + struct fx_bstr *str, const char **strs, size_t *nr_written) { size_t w = 0; - enum b_status status = B_SUCCESS; + enum fx_status status = FX_SUCCESS; for (size_t i = 0; strs[i]; i++) { size_t tmp = 0; status = bstr_puts(str, strs[i], &tmp); w += tmp; - if (B_ERR(status)) { + if (FX_ERR(status)) { break; } } @@ -255,10 +255,10 @@ enum b_status b_bstr_write_cstr_list( return status; } -enum b_status b_bstr_write_cstr_array( - struct b_bstr *str, const char **strs, size_t count, size_t *nr_written) +enum fx_status fx_bstr_write_cstr_array( + struct fx_bstr *str, const char **strs, size_t count, size_t *nr_written) { - enum b_status status = B_SUCCESS; + enum fx_status status = FX_SUCCESS; size_t w = 0; for (size_t i = 0; i < count; i++) { @@ -270,7 +270,7 @@ enum b_status b_bstr_write_cstr_array( status = bstr_puts(str, strs[i], &tmp); w += tmp; - if (B_ERR(status)) { + if (FX_ERR(status)) { break; } } @@ -282,20 +282,20 @@ enum b_status b_bstr_write_cstr_array( return status; } -enum b_status b_bstr_add_many(struct b_bstr *str, size_t *nr_written, ...) +enum fx_status fx_bstr_add_many(struct fx_bstr *str, size_t *nr_written, ...) { va_list arg; va_start(arg, nr_written); const char *s = NULL; size_t w = 0; - enum b_status status = B_SUCCESS; + enum fx_status status = FX_SUCCESS; while ((s = va_arg(arg, const char *))) { size_t tmp = 0; status = bstr_puts(str, s, &tmp); w += tmp; - if (B_ERR(status)) { + if (FX_ERR(status)) { break; } } @@ -307,11 +307,11 @@ enum b_status b_bstr_add_many(struct b_bstr *str, size_t *nr_written, ...) return status; } -enum b_status b_bstr_write_rope( - b_bstr *strv, const struct b_rope *rope, size_t *nr_written) +enum fx_status fx_bstr_write_rope( + fx_bstr *strv, const struct fx_rope *rope, size_t *nr_written) { size_t start = strv->bstr_len; - enum b_status status = b_rope_to_bstr(rope, strv); + enum fx_status status = fx_rope_to_bstr(rope, strv); size_t end = strv->bstr_len; if (nr_written) { @@ -321,55 +321,55 @@ enum b_status b_bstr_write_rope( return status; } -enum b_status b_bstr_write_fmt( - struct b_bstr *str, size_t *nr_written, const char *format, ...) +enum fx_status fx_bstr_write_fmt( + struct fx_bstr *str, size_t *nr_written, const char *format, ...) { va_list arg; va_start(arg, format); - enum b_status result = b_bstr_write_vfmt(str, nr_written, format, arg); + enum fx_status result = fx_bstr_write_vfmt(str, nr_written, format, arg); va_end(arg); return result; } -enum b_status b_bstr_write_vfmt( - struct b_bstr *str, size_t *nr_written, const char *format, va_list arg) +enum fx_status fx_bstr_write_vfmt( + struct fx_bstr *str, size_t *nr_written, const char *format, va_list arg) { size_t start = str->bstr_len; - z__b_fctprintf(bstr_fctprintf, str, format, arg); + z__fx_fctprintf(bstr_fctprintf, str, format, arg); size_t end = str->bstr_len; if (nr_written) { *nr_written = end - start; } - /* TODO update z__b_fctprintf to support propagating error codes */ - return B_SUCCESS; + /* TODO update z__fx_fctprintf to support propagating error codes */ + return FX_SUCCESS; } -char *b_bstr_rope(const struct b_rope *rope, size_t *nr_written) +char *fx_bstr_rope(const struct fx_rope *rope, size_t *nr_written) { - struct b_bstr str; - b_bstr_begin_dynamic(&str); - b_bstr_write_rope(&str, rope, nr_written); + struct fx_bstr str; + fx_bstr_begin_dynamic(&str); + fx_bstr_write_rope(&str, rope, nr_written); - return b_bstr_end(&str); + return fx_bstr_end(&str); } -char *b_bstr_fmt(size_t *nr_written, const char *format, ...) +char *fx_bstr_fmt(size_t *nr_written, const char *format, ...) { va_list arg; va_start(arg, format); - char *s = b_bstr_vfmt(nr_written, format, arg); + char *s = fx_bstr_vfmt(nr_written, format, arg); va_end(arg); return s; } -char *b_bstr_vfmt(size_t *nr_written, const char *format, va_list arg) +char *fx_bstr_vfmt(size_t *nr_written, const char *format, va_list arg) { - struct b_bstr str; - b_bstr_begin_dynamic(&str); - b_bstr_write_vfmt(&str, nr_written, format, arg); + struct fx_bstr str; + fx_bstr_begin_dynamic(&str); + fx_bstr_write_vfmt(&str, nr_written, format, arg); - return b_bstr_end(&str); + return fx_bstr_end(&str); } diff --git a/core/btree.c b/core/btree.c index fdc2a50..caae5bc 100644 --- a/core/btree.c +++ b/core/btree.c @@ -50,45 +50,45 @@ this file intentionally excludes any kind of search function implementation. it is up to the programmer to implement their own tree node type - using b_btree_node, and their own search function using b_btree. + using fx_bst_node, and their own search function using fx_bst. this allows the programmer to define their own node types with complex non-integer key types. btree.h contains a number of macros to help define these functions. the macros do all the work, you just have to provide a comparator function. */ -#include +#include #include #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b)) -#define IS_LEFT_CHILD(p, c) ((p) && (c) && ((p)->b_left == (c))) -#define IS_RIGHT_CHILD(p, c) ((p) && (c) && ((p)->b_right == (c))) +#define IS_LEFT_CHILD(p, c) ((p) && (c) && ((p)->n_left == (c))) +#define IS_RIGHT_CHILD(p, c) ((p) && (c) && ((p)->n_right == (c))) -#define HAS_LEFT_CHILD(x) ((x) && ((x)->b_left)) -#define HAS_RIGHT_CHILD(x) ((x) && ((x)->b_right)) +#define HAS_LEFT_CHILD(x) ((x) && ((x)->n_left)) +#define HAS_RIGHT_CHILD(x) ((x) && ((x)->n_right)) -#define HAS_NO_CHILDREN(x) ((x) && (!(x)->b_left) && (!(x)->b_right)) +#define HAS_NO_CHILDREN(x) ((x) && (!(x)->n_left) && (!(x)->n_right)) #define HAS_ONE_CHILD(x) \ ((HAS_LEFT_CHILD(x) && !HAS_RIGHT_CHILD(x)) \ || (!HAS_LEFT_CHILD(x) && HAS_RIGHT_CHILD(x))) #define HAS_TWO_CHILDREN(x) (HAS_LEFT_CHILD(x) && HAS_RIGHT_CHILD(x)) -#define HEIGHT(x) ((x) ? (x)->b_height : 0) +#define HEIGHT(x) ((x) ? (x)->n_height : 0) -struct b_btree_iterator_p { +struct fx_bst_iterator_p { size_t i, depth; - b_btree_node *node; - b_btree *_b; + fx_bst_node *node; + fx_bst *_b; }; -static inline void update_height(struct b_btree_node *x) +static inline void update_height(struct fx_bst_node *x) { - x->b_height = MAX(HEIGHT(x->b_left), HEIGHT((x->b_right))) + 1; + x->n_height = MAX(HEIGHT(x->n_left), HEIGHT((x->n_right))) + 1; } -static inline int bf(struct b_btree_node *x) +static inline int bf(struct fx_bst_node *x) { int bf = 0; @@ -96,12 +96,12 @@ static inline int bf(struct b_btree_node *x) return bf; } - if (x->b_right) { - bf += x->b_right->b_height; + if (x->n_right) { + bf += x->n_right->n_height; } - if (x->b_left) { - bf -= x->b_left->b_height; + if (x->n_left) { + bf -= x->n_left->n_height; } return bf; @@ -130,38 +130,38 @@ static inline int bf(struct b_btree_node *x) / \ . . - note that this function does NOT update b_height for the rotated + note that this function does NOT update fx_height for the rotated nodes. it is up to you to call update_height_to_root(). */ -static void rotate_left(struct b_btree *tree, struct b_btree_node *x) +static void rotate_left(struct fx_bst *tree, struct fx_bst_node *x) { - struct b_btree_node *y = x->b_right; - struct b_btree_node *p = x->b_parent; + struct fx_bst_node *y = x->n_right; + struct fx_bst_node *p = x->n_parent; - if (y->b_left) { - y->b_left->b_parent = x; + if (y->n_left) { + y->n_left->n_parent = x; } - x->b_right = y->b_left; + x->n_right = y->n_left; if (!p) { - tree->b_root = y; - } else if (x == p->b_left) { - p->b_left = y; + tree->bst_root = y; + } else if (x == p->n_left) { + p->n_left = y; } else { - p->b_right = y; + p->n_right = y; } - x->b_parent = y; - y->b_left = x; - y->b_parent = p; + x->n_parent = y; + y->n_left = x; + y->n_parent = p; } -static void update_height_to_root(struct b_btree_node *x) +static void update_height_to_root(struct fx_bst_node *x) { while (x) { update_height(x); - x = x->b_parent; + x = x->n_parent; } } @@ -188,31 +188,31 @@ static void update_height_to_root(struct b_btree_node *x) / \ . . - note that this function does NOT update b_height for the rotated + note that this function does NOT update fx_height for the rotated nodes. it is up to you to call update_height_to_root(). */ -static void rotate_right(struct b_btree *tree, struct b_btree_node *y) +static void rotate_right(struct fx_bst *tree, struct fx_bst_node *y) { - struct b_btree_node *x = y->b_left; - struct b_btree_node *p = y->b_parent; + struct fx_bst_node *x = y->n_left; + struct fx_bst_node *p = y->n_parent; - if (x->b_right) { - x->b_right->b_parent = y; + if (x->n_right) { + x->n_right->n_parent = y; } - y->b_left = x->b_right; + y->n_left = x->n_right; if (!p) { - tree->b_root = x; - } else if (y == p->b_left) { - p->b_left = x; + tree->bst_root = x; + } else if (y == p->n_left) { + p->n_left = x; } else { - p->b_right = x; + p->n_right = x; } - y->b_parent = x; - x->b_right = y; - x->b_parent = p; + y->n_parent = x; + x->n_right = y; + x->n_parent = p; } /* for a given node Z, perform a right rotation on Z's right child, @@ -239,13 +239,13 @@ static void rotate_right(struct b_btree *tree, struct b_btree_node *y) . . . . note that, unlike rotate_left and rotate_right, this function - DOES update b_height for the rotated nodes (since it needs to be + DOES update fx_height for the rotated nodes (since it needs to be done in a certain order). */ -static void rotate_double_left(struct b_btree *tree, struct b_btree_node *z) +static void rotate_double_left(struct fx_bst *tree, struct fx_bst_node *z) { - struct b_btree_node *x = z->b_right; - struct b_btree_node *y = x->b_left; + struct fx_bst_node *x = z->n_right; + struct fx_bst_node *y = x->n_left; rotate_right(tree, x); rotate_left(tree, z); @@ -255,7 +255,7 @@ static void rotate_double_left(struct b_btree *tree, struct b_btree_node *z) while (y) { update_height(y); - y = y->b_parent; + y = y->n_parent; } } @@ -283,13 +283,13 @@ static void rotate_double_left(struct b_btree *tree, struct b_btree_node *z) . . . . note that, unlike rotate_left and rotate_right, this function - DOES update b_height for the rotated nodes (since it needs to be + DOES update fx_height for the rotated nodes (since it needs to be done in a certain order). */ -static void rotate_double_right(struct b_btree *tree, struct b_btree_node *z) +static void rotate_double_right(struct fx_bst *tree, struct fx_bst_node *z) { - struct b_btree_node *x = z->b_left; - struct b_btree_node *y = x->b_right; + struct fx_bst_node *x = z->n_left; + struct fx_bst_node *y = x->n_right; rotate_left(tree, x); rotate_right(tree, z); @@ -299,7 +299,7 @@ static void rotate_double_right(struct b_btree *tree, struct b_btree_node *z) while (y) { update_height(y); - y = y->b_parent; + y = y->n_parent; } } @@ -311,13 +311,13 @@ static void rotate_double_right(struct b_btree *tree, struct b_btree_node *z) is inserted into the tree. this function depends on all nodes in the tree having - correct b_height values. + correct fx_height values. @param w the node that was just inserted into the tree */ -static void insert_fixup(struct b_btree *tree, struct b_btree_node *w) +static void insert_fixup(struct fx_bst *tree, struct fx_bst_node *w) { - struct b_btree_node *z = NULL, *y = NULL, *x = NULL; + struct fx_bst_node *z = NULL, *y = NULL, *x = NULL; z = w; while (z) { @@ -344,7 +344,7 @@ static void insert_fixup(struct b_btree *tree, struct b_btree_node *w) next_ancestor: x = y; y = z; - z = z->b_parent; + z = z->n_parent; } } @@ -356,7 +356,7 @@ static void insert_fixup(struct b_btree *tree, struct b_btree_node *w) to restore balance after a node is deleted. this function depends on all nodes in the tree having - correct b_height values. + correct fx_height values. @param w one of the following: - the parent of the node that was deleted if the node @@ -366,20 +366,20 @@ static void insert_fixup(struct b_btree *tree, struct b_btree_node *w) - the node that replaced the node that was deleted, if the node that was deleted had one child. */ -static void delete_fixup(struct b_btree *tree, struct b_btree_node *w) +static void delete_fixup(struct fx_bst *tree, struct fx_bst_node *w) { - struct b_btree_node *z = w; + struct fx_bst_node *z = w; while (z) { if (bf(z) > 1) { - if (bf(z->b_right) >= 0) { + if (bf(z->n_right) >= 0) { rotate_left(tree, z); update_height_to_root(z); } else { rotate_double_left(tree, z); } } else if (bf(z) < -1) { - if (bf(z->b_left) <= 0) { + if (bf(z->n_left) <= 0) { rotate_right(tree, z); update_height_to_root(z); } else { @@ -387,23 +387,23 @@ static void delete_fixup(struct b_btree *tree, struct b_btree_node *w) } } - z = z->b_parent; + z = z->n_parent; } } -/* updates b_height for all nodes between the inserted node and the root +/* updates fx_height for all nodes between the inserted node and the root of the tree, and calls insert_fixup. @param node the node that was just inserted into the tree. */ -void b_btree_insert_fixup(struct b_btree *tree, struct b_btree_node *node) +void fx_bst_insert_fixup(struct fx_bst *tree, struct fx_bst_node *node) { - node->b_height = 0; + node->n_height = 0; - struct b_btree_node *cur = node; + struct fx_bst_node *cur = node; while (cur) { update_height(cur); - cur = cur->b_parent; + cur = cur->n_parent; } insert_fixup(tree, node); @@ -414,28 +414,28 @@ void b_btree_insert_fixup(struct b_btree *tree, struct b_btree_node *node) this function assumes that `node` has no children, and therefore doesn't need to be replaced. - updates b_height for all nodes between `node` and the tree root. + updates fx_height for all nodes between `node` and the tree root. @param node the node to delete. */ -static struct b_btree_node *remove_node_with_no_children( - struct b_btree *tree, struct b_btree_node *node) +static struct fx_bst_node *remove_node_with_no_children( + struct fx_bst *tree, struct fx_bst_node *node) { - struct b_btree_node *w = node->b_parent; - struct b_btree_node *p = node->b_parent; - node->b_parent = NULL; + struct fx_bst_node *w = node->n_parent; + struct fx_bst_node *p = node->n_parent; + node->n_parent = NULL; if (!p) { - tree->b_root = NULL; + tree->bst_root = NULL; } else if (IS_LEFT_CHILD(p, node)) { - p->b_left = NULL; + p->n_left = NULL; } else { - p->b_right = NULL; + p->n_right = NULL; } while (p) { update_height(p); - p = p->b_parent; + p = p->n_parent; } return w; @@ -446,40 +446,40 @@ static struct b_btree_node *remove_node_with_no_children( this function assumes that `node` has one child. the child of `node` is inherited by `node`'s parent, and `node` is removed. - updates b_height for all nodes between the node that replaced + updates fx_height for all nodes between the node that replaced `node` and the tree root. @param node the node to delete. */ -static struct b_btree_node *replace_node_with_one_subtree( - struct b_btree *tree, struct b_btree_node *node) +static struct fx_bst_node *replace_node_with_one_subtree( + struct fx_bst *tree, struct fx_bst_node *node) { - struct b_btree_node *p = node->b_parent; - struct b_btree_node *z = NULL; + struct fx_bst_node *p = node->n_parent; + struct fx_bst_node *z = NULL; if (HAS_LEFT_CHILD(node)) { - z = node->b_left; + z = node->n_left; } else { - z = node->b_right; + z = node->n_right; } - struct b_btree_node *w = z; + struct fx_bst_node *w = z; if (!p) { - tree->b_root = z; + tree->bst_root = z; } else if (IS_LEFT_CHILD(p, node)) { - p->b_left = z; + p->n_left = z; } else if (IS_RIGHT_CHILD(p, node)) { - p->b_right = z; + p->n_right = z; } - z->b_parent = p; + z->n_parent = p; - node->b_parent = NULL; - node->b_left = node->b_right = NULL; + node->n_parent = NULL; + node->n_left = node->n_right = NULL; while (z) { update_height(z); - z = z->b_parent; + z = z->n_parent; } return w; @@ -495,79 +495,79 @@ static struct b_btree_node *replace_node_with_one_subtree( if Y has a child (it will never have more than one), have Y's parent inherit Y's child. - updates b_height for all nodes between the deepest node that was modified + updates fx_height for all nodes between the deepest node that was modified and the tree root. @param z the node to delete. */ -static struct b_btree_node *replace_node_with_two_subtrees( - struct b_btree *tree, struct b_btree_node *z) +static struct fx_bst_node *replace_node_with_two_subtrees( + struct fx_bst *tree, struct fx_bst_node *z) { /* x will replace z */ - struct b_btree_node *x = z->b_left; + struct fx_bst_node *x = z->n_left; - while (x->b_right) { - x = x->b_right; + while (x->n_right) { + x = x->n_right; } /* y is the node that will replace x (if x has a left child) */ - struct b_btree_node *y = x->b_left; + struct fx_bst_node *y = x->n_left; /* w is the starting point for the height update and fixup */ - struct b_btree_node *w = x; - if (w->b_parent != z) { - w = w->b_parent; + struct fx_bst_node *w = x; + if (w->n_parent != z) { + w = w->n_parent; } if (y) { w = y; } - if (IS_LEFT_CHILD(x->b_parent, x)) { - x->b_parent->b_left = y; - } else if (IS_RIGHT_CHILD(x->b_parent, x)) { - x->b_parent->b_right = y; + if (IS_LEFT_CHILD(x->n_parent, x)) { + x->n_parent->n_left = y; + } else if (IS_RIGHT_CHILD(x->n_parent, x)) { + x->n_parent->n_right = y; } if (y) { - y->b_parent = x->b_parent; + y->n_parent = x->n_parent; } - if (IS_LEFT_CHILD(z->b_parent, z)) { - z->b_parent->b_left = x; - } else if (IS_RIGHT_CHILD(z->b_parent, z)) { - z->b_parent->b_right = x; + if (IS_LEFT_CHILD(z->n_parent, z)) { + z->n_parent->n_left = x; + } else if (IS_RIGHT_CHILD(z->n_parent, z)) { + z->n_parent->n_right = x; } - x->b_parent = z->b_parent; - x->b_left = z->b_left; - x->b_right = z->b_right; + x->n_parent = z->n_parent; + x->n_left = z->n_left; + x->n_right = z->n_right; - if (x->b_left) { - x->b_left->b_parent = x; + if (x->n_left) { + x->n_left->n_parent = x; } - if (x->b_right) { - x->b_right->b_parent = x; + if (x->n_right) { + x->n_right->n_parent = x; } - if (!x->b_parent) { - tree->b_root = x; + if (!x->n_parent) { + tree->bst_root = x; } - struct b_btree_node *cur = w; + struct fx_bst_node *cur = w; while (cur) { update_height(cur); - cur = cur->b_parent; + cur = cur->n_parent; } return w; } /* delete a node from the tree and re-balance it afterwards */ -void b_btree_delete(struct b_btree *tree, struct b_btree_node *node) +void fx_bst_delete(struct fx_bst *tree, struct fx_bst_node *node) { - struct b_btree_node *w = NULL; + struct fx_bst_node *w = NULL; if (HAS_NO_CHILDREN(node)) { w = remove_node_with_no_children(tree, node); @@ -581,14 +581,14 @@ void b_btree_delete(struct b_btree *tree, struct b_btree_node *node) delete_fixup(tree, w); } - node->b_left = node->b_right = node->b_parent = NULL; + node->n_left = node->n_right = node->n_parent = NULL; } -static struct b_btree_node *first_node(const struct b_btree *tree, int *depth) +static struct fx_bst_node *first_node(const struct fx_bst *tree, int *depth) { /* the first node in the tree is the node with the smallest key. we keep moving left until we can't go any further */ - struct b_btree_node *cur = tree->b_root; + struct fx_bst_node *cur = tree->bst_root; int d = 0; if (!cur) { @@ -596,47 +596,47 @@ static struct b_btree_node *first_node(const struct b_btree *tree, int *depth) return NULL; } - while (cur->b_left) { + while (cur->n_left) { d++; - cur = cur->b_left; + cur = cur->n_left; } *depth = d; return cur; } -struct b_btree_node *b_btree_first(const struct b_btree *tree) +struct fx_bst_node *fx_bst_first(const struct fx_bst *tree) { int d; return first_node(tree, &d); } -static struct b_btree_node *last_node(const struct b_btree *tree, int *depth) +static struct fx_bst_node *last_node(const struct fx_bst *tree, int *depth) { /* the first node in the tree is the node with the largest key. we keep moving right until we can't go any further */ - struct b_btree_node *cur = tree->b_root; + struct fx_bst_node *cur = tree->bst_root; int d = 0; if (!cur) { return NULL; } - while (cur->b_right) { + while (cur->n_right) { d++; - cur = cur->b_right; + cur = cur->n_right; } *depth = d; return cur; } -b_btree_node *b_btree_last(const struct b_btree *tree) +fx_bst_node *fx_bst_last(const struct fx_bst *tree) { int d; return last_node(tree, &d); } -static b_btree_node *next_node(const struct b_btree_node *node, int *depth_diff) +static fx_bst_node *next_node(const struct fx_bst_node *node, int *depth_diff) { if (!node) { return NULL; @@ -653,13 +653,13 @@ static b_btree_node *next_node(const struct b_btree_node *node, int *depth_diff) the sub-tree rooted at `node`. we need to go back to our parent and continue the search elsewhere. */ - if (node->b_right) { + if (node->n_right) { /* case 1: step into `node`'s right sub-tree and keep going left to find the smallest node */ - struct b_btree_node *cur = node->b_right; + struct fx_bst_node *cur = node->n_right; depth++; - while (cur->b_left) { - cur = cur->b_left; + while (cur->n_left) { + cur = cur->n_left; depth++; } @@ -671,16 +671,16 @@ static b_btree_node *next_node(const struct b_btree_node *node, int *depth_diff) if we encounter a step where we are our parent's left child, we've found a parent with a value larger than us. this parent is the in-order successor of `node` */ - while (node->b_parent && node->b_parent->b_left != node) { - node = node->b_parent; + while (node->n_parent && node->n_parent->n_left != node) { + node = node->n_parent; depth--; } *depth_diff = depth - 1; - return node->b_parent; + return node->n_parent; } -static b_btree_node *prev_node(const struct b_btree_node *node, int *depth_diff) +static fx_bst_node *prev_node(const struct fx_bst_node *node, int *depth_diff) { if (!node) { return NULL; @@ -697,13 +697,13 @@ static b_btree_node *prev_node(const struct b_btree_node *node, int *depth_diff) the sub-tree rooted at `node`. we need to go back to our parent and continue the search elsewhere. */ - if (node->b_left) { + if (node->n_left) { /* case 1: step into `node`'s left sub-tree and keep going right to find the largest node */ - b_btree_node *cur = node->b_left; + fx_bst_node *cur = node->n_left; depth++; - while (cur->b_right) { - cur = cur->b_right; + while (cur->n_right) { + cur = cur->n_right; depth++; } @@ -715,65 +715,65 @@ static b_btree_node *prev_node(const struct b_btree_node *node, int *depth_diff) if we encounter a step where we are our parent's right child, we've found a parent with a value smaller than us. this parent is the in-order predecessor of `node`. */ - while (node->b_parent && node->b_parent->b_right != node) { - node = node->b_parent; + while (node->n_parent && node->n_parent->n_right != node) { + node = node->n_parent; depth--; } *depth_diff = depth - 1; - return node->b_parent; + return node->n_parent; } -b_btree_node *b_btree_next(const struct b_btree_node *node) +fx_bst_node *fx_bst_next(const struct fx_bst_node *node) { int d; return next_node(node, &d); } -b_btree_node *b_btree_prev(const struct b_btree_node *node) +fx_bst_node *fx_bst_prev(const struct fx_bst_node *node) { int d; return prev_node(node, &d); } -void b_btree_move( - struct b_btree *tree, struct b_btree_node *dest, struct b_btree_node *src) +void fx_bst_move( + struct fx_bst *tree, struct fx_bst_node *dest, struct fx_bst_node *src) { - if (src->b_parent) { - if (src->b_parent->b_left == src) { - src->b_parent->b_left = dest; + if (src->n_parent) { + if (src->n_parent->n_left == src) { + src->n_parent->n_left = dest; } else { - src->b_parent->b_right = dest; + src->n_parent->n_right = dest; } } - if (src->b_left) { - src->b_left->b_parent = dest; + if (src->n_left) { + src->n_left->n_parent = dest; } - if (src->b_right) { - src->b_right->b_parent = dest; + if (src->n_right) { + src->n_right->n_parent = dest; } - if (tree->b_root == src) { - tree->b_root = dest; + if (tree->bst_root == src) { + tree->bst_root = dest; } memmove(dest, src, sizeof *src); } -b_iterator *b_btree_begin(struct b_btree *tree) +fx_iterator *fx_bst_begin(struct fx_bst *tree) { - b_iterator *it_obj = b_object_create(B_TYPE_BTREE_ITERATOR); + fx_iterator *it_obj = fx_object_create(FX_TYPE_BTREE_ITERATOR); if (!it_obj) { return NULL; } - struct b_btree_iterator_p *it - = b_object_get_private(it_obj, B_TYPE_BTREE_ITERATOR); + struct fx_bst_iterator_p *it + = fx_object_get_private(it_obj, FX_TYPE_BTREE_ITERATOR); int depth = 0; - it->_b = (struct b_btree *)tree; + it->_b = (struct fx_bst *)tree; it->i = 0; it->node = first_node(tree, &depth); it->depth = depth; @@ -781,18 +781,18 @@ b_iterator *b_btree_begin(struct b_btree *tree) return it_obj; } -const b_iterator *b_btree_cbegin(const struct b_btree *tree) +const fx_iterator *fx_bst_cbegin(const struct fx_bst *tree) { - b_iterator *it_obj = b_object_create(B_TYPE_BTREE_ITERATOR); + fx_iterator *it_obj = fx_object_create(FX_TYPE_BTREE_ITERATOR); if (!it_obj) { return NULL; } - struct b_btree_iterator_p *it - = b_object_get_private(it_obj, B_TYPE_BTREE_ITERATOR); + struct fx_bst_iterator_p *it + = fx_object_get_private(it_obj, FX_TYPE_BTREE_ITERATOR); int depth = 0; - it->_b = (struct b_btree *)tree; + it->_b = (struct fx_bst *)tree; it->i = 0; it->node = first_node(tree, &depth); it->depth = depth; @@ -800,13 +800,13 @@ const b_iterator *b_btree_cbegin(const struct b_btree *tree) return it_obj; } -static enum b_status iterator_move_next(const b_iterator *obj) +static enum fx_status iterator_move_next(const fx_iterator *obj) { - struct b_btree_iterator_p *it - = b_object_get_private(obj, B_TYPE_BTREE_ITERATOR); + struct fx_bst_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_BTREE_ITERATOR); int depth_diff = 0; - struct b_btree_node *next = next_node(it->node, &depth_diff); + struct fx_bst_node *next = next_node(it->node, &depth_diff); if (!next) { it->node = NULL; @@ -821,19 +821,19 @@ static enum b_status iterator_move_next(const b_iterator *obj) return true; } -static enum b_status iterator_erase(b_iterator *obj) +static enum fx_status iterator_erase(fx_iterator *obj) { - struct b_btree_iterator_p *it - = b_object_get_private(obj, B_TYPE_BTREE_ITERATOR); + struct fx_bst_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_BTREE_ITERATOR); if (!it->node) { - return B_ERR_OUT_OF_BOUNDS; + return FX_ERR_OUT_OF_BOUNDS; } int depth_diff = 0; - struct b_btree_node *next = next_node(it->node, &depth_diff); + struct fx_bst_node *next = next_node(it->node, &depth_diff); - b_btree_delete(it->_b, it->node); + fx_bst_delete(it->_b, it->node); if (!next) { it->node = NULL; it->depth = 0; @@ -841,51 +841,51 @@ static enum b_status iterator_erase(b_iterator *obj) it->node = next; it->depth = 0; - struct b_btree_node *cur = next->b_parent; + struct fx_bst_node *cur = next->n_parent; while (cur) { it->depth++; - cur = cur->b_parent; + cur = cur->n_parent; } } - return B_SUCCESS; + return FX_SUCCESS; } -static b_iterator_value iterator_get_value(b_iterator *obj) +static fx_iterator_value iterator_get_value(fx_iterator *obj) { - struct b_btree_iterator_p *it - = b_object_get_private(obj, B_TYPE_BTREE_ITERATOR); + struct fx_bst_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_BTREE_ITERATOR); - return B_ITERATOR_VALUE_PTR(it->node); + return FX_ITERATOR_VALUE_PTR(it->node); } -static const b_iterator_value iterator_get_cvalue(const b_iterator *obj) +static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj) { - struct b_btree_iterator_p *it - = b_object_get_private(obj, B_TYPE_BTREE_ITERATOR); + struct fx_bst_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_BTREE_ITERATOR); - return B_ITERATOR_VALUE_CPTR(it->node); + return FX_ITERATOR_VALUE_CPTR(it->node); } /*** CLASS DEFINITION *********************************************************/ -// ---- b_btree_iterator DEFINITION -B_TYPE_CLASS_DEFINITION_BEGIN(b_btree_iterator) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) +// ---- fx_bst_iterator DEFINITION +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_bst_iterator) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) - B_TYPE_CLASS_INTERFACE_BEGIN(b_iterator, B_TYPE_ITERATOR) - B_INTERFACE_ENTRY(it_move_next) = iterator_move_next; - B_INTERFACE_ENTRY(it_erase) = iterator_erase; - B_INTERFACE_ENTRY(it_get_value) = iterator_get_value; - B_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue; - B_TYPE_CLASS_INTERFACE_END(b_iterator, B_TYPE_ITERATOR) -B_TYPE_CLASS_DEFINITION_END(b_btree_iterator) + FX_TYPE_CLASS_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_CLASS_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR) +FX_TYPE_CLASS_DEFINITION_END(fx_bst_iterator) -B_TYPE_DEFINITION_BEGIN(b_btree_iterator) - B_TYPE_ID(0x432779d7, 0xc03a, 0x48ea, 0xae8f, 0x12c666c767ae); - B_TYPE_EXTENDS(B_TYPE_ITERATOR); - B_TYPE_CLASS(b_btree_iterator_class); - B_TYPE_INSTANCE_PRIVATE(struct b_btree_iterator_p); -B_TYPE_DEFINITION_END(b_btree_iterator) +FX_TYPE_DEFINITION_BEGIN(fx_bst_iterator) + FX_TYPE_ID(0x432779d7, 0xc03a, 0x48ea, 0xae8f, 0x12c666c767ae); + FX_TYPE_EXTENDS(FX_TYPE_ITERATOR); + FX_TYPE_CLASS(fx_bst_iterator_class); + FX_TYPE_INSTANCE_PRIVATE(struct fx_bst_iterator_p); +FX_TYPE_DEFINITION_END(fx_bst_iterator) diff --git a/core/class.c b/core/class.c index 6d5942a..101452c 100644 --- a/core/class.c +++ b/core/class.c @@ -3,13 +3,13 @@ #include "type.h" #include -#include +#include #include #include -void *b_class_get(b_type id) +void *fx_class_get(fx_type id) { - struct b_type_registration *r = b_type_get_registration(id); + struct fx_type_registration *r = fx_type_get_registration(id); if (!r) { return NULL; } @@ -17,28 +17,28 @@ void *b_class_get(b_type id) return r->r_class; } -const char *b_class_get_name(const struct _b_class *c) +const char *fx_class_get_name(const struct _fx_class *c) { if (!c) { return NULL; } - assert(c->c_magic == B_CLASS_MAGIC); + assert(c->c_magic == FX_CLASS_MAGIC); return c->c_type->r_info->t_name; } -void *b_class_get_interface(const struct _b_class *c, const union b_type *id) +void *fx_class_get_interface(const struct _fx_class *c, const union fx_type *id) { if (!c) { return NULL; } - assert(c->c_magic == B_CLASS_MAGIC); + assert(c->c_magic == FX_CLASS_MAGIC); - const struct b_type_registration *type_reg = c->c_type; - struct b_type_component *comp - = b_type_get_component(&type_reg->r_components, id); + const struct fx_type_registration *type_reg = c->c_type; + struct fx_type_component *comp + = fx_type_get_component(&type_reg->r_components, id); if (!comp) { return NULL; @@ -47,33 +47,33 @@ void *b_class_get_interface(const struct _b_class *c, const union b_type *id) return (char *)c + comp->c_class_data_offset; } -b_result b_class_instantiate( - struct b_type_registration *type, struct _b_class **out_class) +fx_result fx_class_instantiate( + struct fx_type_registration *type, struct _fx_class **out_class) { - struct _b_class *out = malloc(type->r_class_size); + struct _fx_class *out = malloc(type->r_class_size); if (!out) { - return B_RESULT_ERR(NO_MEMORY); + return FX_RESULT_ERR(NO_MEMORY); } memset(out, 0x0, type->r_class_size); - out->c_magic = B_CLASS_MAGIC; + out->c_magic = FX_CLASS_MAGIC; out->c_type = type; - struct b_queue_entry *entry = b_queue_first(&type->r_class_hierarchy); + struct fx_queue_entry *entry = fx_queue_first(&type->r_class_hierarchy); while (entry) { - struct b_type_component *comp - = b_unbox(struct b_type_component, entry, c_entry); - const struct b_type_info *class_info = comp->c_type->r_info; + struct fx_type_component *comp + = fx_unbox(struct fx_type_component, entry, c_entry); + const struct fx_type_info *class_info = comp->c_type->r_info; void *class_data = (char *)out + comp->c_class_data_offset; if (class_info->t_class_init) { class_info->t_class_init(out, class_data); } - entry = b_queue_next(entry); + entry = fx_queue_next(entry); } *out_class = out; - return B_RESULT_SUCCESS; + return FX_RESULT_SUCCESS; } diff --git a/core/class.h b/core/class.h index 252ffd9..8230c3e 100644 --- a/core/class.h +++ b/core/class.h @@ -1,18 +1,18 @@ #ifndef _CLASS_H_ #define _CLASS_H_ -#include -#include +#include +#include #include -struct b_type_registration; +struct fx_type_registration; -struct _b_class { +struct _fx_class { uint64_t c_magic; - const struct b_type_registration *c_type; + const struct fx_type_registration *c_type; }; -extern b_result b_class_instantiate( - struct b_type_registration *type, struct _b_class **out); +extern fx_result fx_class_instantiate( + struct fx_type_registration *type, struct _fx_class **out); #endif diff --git a/core/encoding.c b/core/encoding.c index 7d00ba0..6c69b15 100644 --- a/core/encoding.c +++ b/core/encoding.c @@ -1,12 +1,12 @@ -#include +#include #include -bool b_wchar_is_number(b_wchar c) +bool fx_wchar_is_number(fx_wchar c) { return iswnumber((wchar_t)c); } -bool b_wchar_is_alpha(b_wchar c) +bool fx_wchar_is_alpha(fx_wchar c) { if (c == 0) { return false; @@ -1177,28 +1177,28 @@ bool b_wchar_is_alpha(b_wchar c) } } -bool b_wchar_is_hex_digit(b_wchar c) +bool fx_wchar_is_hex_digit(fx_wchar c) { return isxdigit(c); } -bool b_wchar_is_space(b_wchar c) +bool fx_wchar_is_space(fx_wchar c) { return iswspace((wchar_t)c); } -bool b_wchar_is_punct(b_wchar c) +bool fx_wchar_is_punct(fx_wchar c) { return iswpunct((wchar_t)c); } -bool b_wchar_utf8_is_valid_scalar(b_wchar c) +bool fx_wchar_utf8_is_valid_scalar(fx_wchar c) { return (((c) >= 0x0000 && (c) <= 0xD7FF) || ((c) >= 0xE000 && (c) <= 0x10FFFF)); } -unsigned int b_wchar_utf8_header_decode(char c) +unsigned int fx_wchar_utf8_header_decode(char c) { unsigned int len = 0; @@ -1217,9 +1217,9 @@ unsigned int b_wchar_utf8_header_decode(char c) return len; } -unsigned int b_wchar_utf8_codepoint_size(b_wchar c) +unsigned int fx_wchar_utf8_codepoint_size(fx_wchar c) { - if (!b_wchar_utf8_is_valid_scalar(c)) { + if (!fx_wchar_utf8_is_valid_scalar(c)) { return 0; } @@ -1251,9 +1251,9 @@ static int32_t decode_utf8_trailer_byte(char c) return c & 0x3F; } -b_wchar b_wchar_utf8_codepoint_decode(const char *s) +fx_wchar fx_wchar_utf8_codepoint_decode(const char *s) { - b_wchar result = 0; + fx_wchar result = 0; int len = 0; if (!(s[0] & 0x80)) { @@ -1272,29 +1272,29 @@ b_wchar b_wchar_utf8_codepoint_decode(const char *s) result = s[0] & 0x07; result <<= 18; } else { - return B_WCHAR_INVALID; + return FX_WCHAR_INVALID; } for (int i = 1; i < len; i++) { int32_t c = decode_utf8_trailer_byte(s[i]); if (c == -1) { - return B_WCHAR_INVALID; + return FX_WCHAR_INVALID; } c <<= 6 * (len - i - 1); result |= c; } - if (!b_wchar_utf8_is_valid_scalar(result)) { - return B_WCHAR_INVALID; + if (!fx_wchar_utf8_is_valid_scalar(result)) { + return FX_WCHAR_INVALID; } return result; } -unsigned int b_wchar_utf8_codepoint_encode(b_wchar c, char s[4]) +unsigned int fx_wchar_utf8_codepoint_encode(fx_wchar c, char s[4]) { - unsigned int len = b_wchar_utf8_codepoint_size(c); + unsigned int len = fx_wchar_utf8_codepoint_size(c); switch (len) { case 1: @@ -1322,7 +1322,7 @@ unsigned int b_wchar_utf8_codepoint_encode(b_wchar c, char s[4]) return len; } -unsigned int b_wchar_utf8_codepoint_stride(const char *s) +unsigned int fx_wchar_utf8_codepoint_stride(const char *s) { char c = *s; @@ -1345,13 +1345,13 @@ unsigned int b_wchar_utf8_codepoint_stride(const char *s) return 0; } -size_t b_wchar_utf8_codepoint_count(const char *s, size_t nr_bytes) +size_t fx_wchar_utf8_codepoint_count(const char *s, size_t nr_bytes) { size_t nr_codepoints = 0; const char *end = s + nr_bytes; while (*s && s < end) { - size_t stride = b_wchar_utf8_codepoint_stride(s); + size_t stride = fx_wchar_utf8_codepoint_stride(s); if (stride == 0) { /* invalid codepoint */ return 0; @@ -1364,11 +1364,11 @@ size_t b_wchar_utf8_codepoint_count(const char *s, size_t nr_bytes) return nr_codepoints; } -size_t b_wchar_utf8_string_encoded_size(const b_wchar *s, size_t nr_codepoints) +size_t fx_wchar_utf8_string_encoded_size(const fx_wchar *s, size_t nr_codepoints) { size_t len = 0; for (size_t i = 0; i < nr_codepoints; i++) { - size_t l = b_wchar_utf8_codepoint_size(s[i]); + size_t l = fx_wchar_utf8_codepoint_size(s[i]); if (l == 0) { /* invalid codepoint */ return 0; diff --git a/core/endian.c b/core/endian.c index 5b4c297..e342e61 100644 --- a/core/endian.c +++ b/core/endian.c @@ -1,8 +1,8 @@ -#include +#include -b_i16 b_i16_htob(uint16_t v) +fx_i16 fx_i16_htob(uint16_t v) { - b_i16 x; + fx_i16 x; #ifdef BIG_ENDIAN x.i_uval = v; @@ -15,9 +15,9 @@ b_i16 b_i16_htob(uint16_t v) return x; } -b_i16 b_i16_htos(uint16_t v) +fx_i16 fx_i16_htos(uint16_t v) { - b_i16 x; + fx_i16 x; #ifdef LITTLE_ENDIAN x.i_uval = v; @@ -30,7 +30,7 @@ b_i16 b_i16_htos(uint16_t v) return x; } -uint16_t b_i16_btoh(b_i16 v) +uint16_t fx_i16_btoh(fx_i16 v) { uint16_t x; @@ -45,7 +45,7 @@ uint16_t b_i16_btoh(b_i16 v) return x; } -uint16_t b_i16_stoh(b_i16 v) +uint16_t fx_i16_stoh(fx_i16 v) { uint16_t x; @@ -60,9 +60,9 @@ uint16_t b_i16_stoh(b_i16 v) return x; } -b_i32 b_i32_htob(uint32_t v) +fx_i32 fx_i32_htob(uint32_t v) { - b_i32 x; + fx_i32 x; #ifdef BIG_ENDIAN x.i_uval = v; @@ -77,9 +77,9 @@ b_i32 b_i32_htob(uint32_t v) return x; } -b_i32 b_i32_htos(uint32_t v) +fx_i32 fx_i32_htos(uint32_t v) { - b_i32 x; + fx_i32 x; #ifdef LITTLE_ENDIAN x.i_uval = v; @@ -94,7 +94,7 @@ b_i32 b_i32_htos(uint32_t v) return x; } -uint32_t b_i32_btoh(b_i32 v) +uint32_t fx_i32_btoh(fx_i32 v) { uint32_t x; @@ -111,7 +111,7 @@ uint32_t b_i32_btoh(b_i32 v) return x; } -uint32_t b_i32_stoh(b_i32 v) +uint32_t fx_i32_stoh(fx_i32 v) { uint32_t x; @@ -128,9 +128,9 @@ uint32_t b_i32_stoh(b_i32 v) return x; } -b_i64 b_i64_htob(uint64_t v) +fx_i64 fx_i64_htob(uint64_t v) { - b_i64 x; + fx_i64 x; #ifdef BIG_ENDIAN x.i_uval = v; @@ -149,9 +149,9 @@ b_i64 b_i64_htob(uint64_t v) return x; } -b_i64 b_i64_htos(uint64_t v) +fx_i64 fx_i64_htos(uint64_t v) { - b_i64 x; + fx_i64 x; #ifdef LITTLE_ENDIAN x.i_uval = v; @@ -170,7 +170,7 @@ b_i64 b_i64_htos(uint64_t v) return x; } -uint64_t b_i64_btoh(b_i64 v) +uint64_t fx_i64_btoh(fx_i64 v) { uint64_t x; @@ -191,7 +191,7 @@ uint64_t b_i64_btoh(b_i64 v) return x; } -uint64_t b_i64_stoh(b_i64 v) +uint64_t fx_i64_stoh(fx_i64 v) { uint64_t x; diff --git a/core/error.c b/core/error.c index de856a8..78ae907 100644 --- a/core/error.c +++ b/core/error.c @@ -1,45 +1,45 @@ #include "error.h" -#include +#include #include #include #include static void default_error_reporter( - const struct b_error *error, enum b_error_report_flags flags); -static void free_error(struct b_error *error); -static void do_free_error(struct b_error *error); + const struct fx_error *error, enum fx_error_report_flags flags); +static void free_error(struct fx_error *error); +static void do_free_error(struct fx_error *error); -static const struct b_error_vendor builtin_vendor; +static const struct fx_error_vendor builtin_vendor; /* TODO protect these with locks */ -static struct b_queue free_errors = B_QUEUE_INIT; -static struct b_queue allocated_errors = B_QUEUE_INIT; -static b_error_report_function error_reporter = default_error_reporter; -static enum b_error_report_flags error_reporter_flags = B_ERROR_REPORT_DEFAULT; +static struct fx_queue free_errors = FX_QUEUE_INIT; +static struct fx_queue allocated_errors = FX_QUEUE_INIT; +static fx_error_report_function error_reporter = default_error_reporter; +static enum fx_error_report_flags error_reporter_flags = FX_ERROR_REPORT_DEFAULT; static void error_cleanup(void) { - struct b_queue_entry *entry = b_queue_pop_back(&allocated_errors); + struct fx_queue_entry *entry = fx_queue_pop_back(&allocated_errors); while (entry) { - struct b_error *error = b_unbox(struct b_error, entry, err_entry); + struct fx_error *error = fx_unbox(struct fx_error, entry, err_entry); do_free_error(error); free(error); - entry = b_queue_pop_back(&allocated_errors); + entry = fx_queue_pop_back(&allocated_errors); } - entry = b_queue_pop_back(&free_errors); + entry = fx_queue_pop_back(&free_errors); while (entry) { - struct b_error *error = b_unbox(struct b_error, entry, err_entry); + struct fx_error *error = fx_unbox(struct fx_error, entry, err_entry); free(error); - entry = b_queue_pop_back(&free_errors); + entry = fx_queue_pop_back(&free_errors); } } -bool b_result_is( - struct b_error *err, const b_error_vendor *vendor, b_error_status_code code) +bool fx_result_is( + struct fx_error *err, const fx_error_vendor *vendor, fx_error_status_code code) { if (!err) { return false; @@ -52,15 +52,15 @@ bool b_result_is( return err->err_code == code; } -const struct b_error_vendor *b_error_vendor_get_builtin(void) +const struct fx_error_vendor *fx_error_vendor_get_builtin(void) { return &builtin_vendor; } -const struct b_error_definition *b_error_vendor_get_error_definition( - const struct b_error_vendor *vendor, b_error_status_code code) +const struct fx_error_definition *fx_error_vendor_get_error_definition( + const struct fx_error_vendor *vendor, fx_error_status_code code) { - const struct b_error_definition *error_def = NULL; + const struct fx_error_definition *error_def = NULL; if (vendor->v_status_get_definition) { error_def = vendor->v_status_get_definition(vendor, code); @@ -82,26 +82,26 @@ const struct b_error_definition *b_error_vendor_get_error_definition( return error_def; } -const char *b_error_vendor_get_status_code_name( - const struct b_error_vendor *vendor, b_error_status_code code) +const char *fx_error_vendor_get_status_code_name( + const struct fx_error_vendor *vendor, fx_error_status_code code) { - const struct b_error_definition *error_def - = b_error_vendor_get_error_definition(vendor, code); + const struct fx_error_definition *error_def + = fx_error_vendor_get_error_definition(vendor, code); return error_def ? error_def->err_name : NULL; } -const char *b_error_vendor_get_status_code_description( - const struct b_error_vendor *vendor, b_error_status_code code) +const char *fx_error_vendor_get_status_code_description( + const struct fx_error_vendor *vendor, fx_error_status_code code) { - const struct b_error_definition *error_def - = b_error_vendor_get_error_definition(vendor, code); + const struct fx_error_definition *error_def + = fx_error_vendor_get_error_definition(vendor, code); return error_def ? error_def->err_message : NULL; } -const struct b_error_msg *b_error_vendor_get_msg( - const struct b_error_vendor *vendor, b_error_msg_id msg_id) +const struct fx_error_msg *fx_error_vendor_get_msg( + const struct fx_error_vendor *vendor, fx_error_msg_id msg_id) { - const struct b_error_msg *msg = NULL; + const struct fx_error_msg *msg = NULL; if (vendor->v_msg_get_definition) { msg = vendor->v_msg_get_definition(vendor, msg_id); @@ -123,65 +123,65 @@ const struct b_error_msg *b_error_vendor_get_msg( return msg; } -static struct b_error *allocate_error(void) +static struct fx_error *allocate_error(void) { - if (b_queue_empty(&allocated_errors) && b_queue_empty(&free_errors)) { + if (fx_queue_empty(&allocated_errors) && fx_queue_empty(&free_errors)) { atexit(error_cleanup); } - struct b_queue_entry *entry = b_queue_pop_back(&free_errors); + struct fx_queue_entry *entry = fx_queue_pop_back(&free_errors); if (entry) { - struct b_error *error = b_unbox(struct b_error, entry, err_entry); + struct fx_error *error = fx_unbox(struct fx_error, entry, err_entry); memset(error, 0x0, sizeof *error); - b_queue_push_back(&allocated_errors, entry); + fx_queue_push_back(&allocated_errors, entry); return error; } - struct b_error *error = malloc(sizeof *error); + struct fx_error *error = malloc(sizeof *error); if (!error) { return NULL; } memset(error, 0x0, sizeof *error); - b_queue_push_back(&allocated_errors, &error->err_entry); + fx_queue_push_back(&allocated_errors, &error->err_entry); return error; } -static void do_free_error(struct b_error *error) +static void do_free_error(struct fx_error *error) { - struct b_queue_entry *entry = b_queue_pop_back(&error->err_submsg); + struct fx_queue_entry *entry = fx_queue_pop_back(&error->err_submsg); while (entry) { - struct b_error_submsg *msg - = b_unbox(struct b_error_submsg, entry, msg_entry); + struct fx_error_submsg *msg + = fx_unbox(struct fx_error_submsg, entry, msg_entry); if (msg->msg_content) { free(msg->msg_content); } free(msg); - entry = b_queue_pop_back(&error->err_submsg); + entry = fx_queue_pop_back(&error->err_submsg); } - entry = b_queue_pop_back(&error->err_stack); + entry = fx_queue_pop_back(&error->err_stack); while (entry) { - struct b_error_stack_frame *frame - = b_unbox(struct b_error_stack_frame, entry, f_entry); + struct fx_error_stack_frame *frame + = fx_unbox(struct fx_error_stack_frame, entry, f_entry); free(frame); - entry = b_queue_pop_back(&error->err_stack); + entry = fx_queue_pop_back(&error->err_stack); } size_t nr_param = sizeof error->err_params / sizeof error->err_params[0]; for (size_t i = 0; i < nr_param; i++) { - const struct b_error_template_parameter_definition *param_def + const struct fx_error_template_parameter_definition *param_def = error->err_params[i].__param_def; if (!param_def) { continue; } - if (param_def->param_type == B_ERROR_TEMPLATE_PARAM_STRING) { + if (param_def->param_type == FX_ERROR_TEMPLATE_PARAM_STRING) { free((void *)error->err_params[i].param_value); } } @@ -195,21 +195,21 @@ static void do_free_error(struct b_error *error) } } -static void free_error(struct b_error *error) +static void free_error(struct fx_error *error) { - b_queue_delete(&allocated_errors, &error->err_entry); + fx_queue_delete(&allocated_errors, &error->err_entry); do_free_error(error); - b_queue_push_back(&free_errors, &error->err_entry); + fx_queue_push_back(&free_errors, &error->err_entry); } -struct b_error *z__b_error_create_string( - const b_error_vendor *vendor, b_error_status_code code, - struct b_error *caused_by, const char *file, unsigned int line, +struct fx_error *z__fx_error_create_string( + const fx_error_vendor *vendor, fx_error_status_code code, + struct fx_error *caused_by, const char *file, unsigned int line, const char *func, const char *description, va_list arg) { - struct b_error *error = allocate_error(); + struct fx_error *error = allocate_error(); if (!error) { return NULL; } @@ -219,36 +219,36 @@ struct b_error *z__b_error_create_string( error->err_caused_by = caused_by; if (description) { - b_stringstream *desc = b_stringstream_create(); - b_stream_write_vfmt(desc, NULL, description, arg); - error->err_description = b_stringstream_steal(desc); - b_stringstream_unref(desc); + fx_stringstream *desc = fx_stringstream_create(); + fx_stream_write_vfmt(desc, NULL, description, arg); + error->err_description = fx_stringstream_steal(desc); + fx_stringstream_unref(desc); } - return z__b_error_propagate(error, file, line, func); + return z__fx_error_propagate(error, file, line, func); } -struct b_error *z__b_error_create_msg( - const struct b_error_vendor *vendor, b_error_status_code code, - struct b_error *caused_by, const char *file, unsigned int line, - const char *func, b_error_msg_id msg_id, - const b_error_template_parameter params[]) +struct fx_error *z__fx_error_create_msg( + const struct fx_error_vendor *vendor, fx_error_status_code code, + struct fx_error *caused_by, const char *file, unsigned int line, + const char *func, fx_error_msg_id msg_id, + const fx_error_template_parameter params[]) { - const struct b_error_definition *error_def - = b_error_vendor_get_error_definition(vendor, code); + const struct fx_error_definition *error_def + = fx_error_vendor_get_error_definition(vendor, code); if (!error_def) { fprintf(stderr, "Undefined %s error %lu\n", vendor->v_name, code); abort(); } - const struct b_error_msg *msg = b_error_vendor_get_msg(vendor, msg_id); + const struct fx_error_msg *msg = fx_error_vendor_get_msg(vendor, msg_id); if (!msg) { fprintf(stderr, "Undefined %s error msg %lu\n", vendor->v_name, msg_id); abort(); } - struct b_error *error = allocate_error(); + struct fx_error *error = allocate_error(); if (!error) { return NULL; } @@ -259,17 +259,17 @@ struct b_error *z__b_error_create_msg( error->err_caused_by = caused_by; for (size_t i = 0; params[i].param_name; i++) { - const struct b_error_template_parameter *param = ¶ms[i]; + const struct fx_error_template_parameter *param = ¶ms[i]; - const struct b_error_template_parameter_definition *param_def - = b_error_msg_get_template_parameter(msg, param->param_name); + const struct fx_error_template_parameter_definition *param_def + = fx_error_msg_get_template_parameter(msg, param->param_name); if (!param_def) { continue; } memcpy(&error->err_params[i], param, sizeof *param); error->err_params[i].__param_def = param_def; - if (param_def->param_type == B_ERROR_TEMPLATE_PARAM_STRING) { + if (param_def->param_type == FX_ERROR_TEMPLATE_PARAM_STRING) { const char *s = (const char *)error->err_params[i].param_value; size_t len = strlen(s); @@ -286,22 +286,22 @@ struct b_error *z__b_error_create_msg( } } - return z__b_error_propagate(error, file, line, func); + return z__fx_error_propagate(error, file, line, func); } -struct b_error *z__b_error_create_template( - const struct b_error_vendor *vendor, b_error_status_code code, - struct b_error *caused_by, const char *file, unsigned int line, - const char *func, const b_error_template_parameter params[]) +struct fx_error *z__fx_error_create_template( + const struct fx_error_vendor *vendor, fx_error_status_code code, + struct fx_error *caused_by, const char *file, unsigned int line, + const char *func, const fx_error_template_parameter params[]) { - const struct b_error_definition *error_def - = b_error_vendor_get_error_definition(vendor, code); + const struct fx_error_definition *error_def + = fx_error_vendor_get_error_definition(vendor, code); if (!error_def) { fprintf(stderr, "Undefined %s error %lu\n", vendor->v_name, code); return NULL; } - struct b_error *error = allocate_error(); + struct fx_error *error = allocate_error(); if (!error) { return NULL; } @@ -311,10 +311,10 @@ struct b_error *z__b_error_create_template( error->err_caused_by = caused_by; for (size_t i = 0; params[i].param_name; i++) { - const struct b_error_template_parameter *param = ¶ms[i]; + const struct fx_error_template_parameter *param = ¶ms[i]; - const struct b_error_template_parameter_definition *param_def - = b_error_definition_get_template_parameter( + const struct fx_error_template_parameter_definition *param_def + = fx_error_definition_get_template_parameter( error_def, param->param_name); if (!param_def) { continue; @@ -322,7 +322,7 @@ struct b_error *z__b_error_create_template( memcpy(&error->err_params[i], param, sizeof *param); error->err_params[i].__param_def = param_def; - if (param_def->param_type == B_ERROR_TEMPLATE_PARAM_STRING) { + if (param_def->param_type == FX_ERROR_TEMPLATE_PARAM_STRING) { const char *s = (const char *)error->err_params[i].param_value; size_t len = strlen(s); @@ -339,11 +339,11 @@ struct b_error *z__b_error_create_template( } } - return z__b_error_propagate(error, file, line, func); + return z__fx_error_propagate(error, file, line, func); } -struct b_error *z__b_error_propagate( - struct b_error *error, const char *file, unsigned int line, +struct fx_error *z__fx_error_propagate( + struct fx_error *error, const char *file, unsigned int line, const char *function) { if (!error) { @@ -354,7 +354,7 @@ struct b_error *z__b_error_propagate( return error; } - struct b_error_stack_frame *frame = malloc(sizeof *frame); + struct fx_error_stack_frame *frame = malloc(sizeof *frame); if (!frame) { return error; } @@ -363,31 +363,31 @@ struct b_error *z__b_error_propagate( frame->f_line_number = line; frame->f_function = function; - b_queue_push_back(&error->err_stack, &frame->f_entry); + fx_queue_push_back(&error->err_stack, &frame->f_entry); return error; } -struct b_error *z__b_error_caused_by(struct b_error *error, struct b_error *caused_by) +struct fx_error *z__fx_error_caused_by(struct fx_error *error, struct fx_error *caused_by) { error->err_caused_by = caused_by; return error; } -struct b_error *z__b_error_caused_by_b_status( - struct b_error *error, enum b_status status) +struct fx_error *z__fx_error_caused_by_fx_status( + struct fx_error *error, enum fx_status status) { - error->err_caused_by = z__b_error_create( + error->err_caused_by = z__fx_error_create( &builtin_vendor, status, NULL, NULL, 0, NULL, NULL); return error; } -enum b_status b_error_add_submsg_string( - struct b_error *error, b_error_submsg_type type, const char *msg, ...) +enum fx_status fx_error_add_submsg_string( + struct fx_error *error, fx_error_submsg_type type, const char *msg, ...) { - struct b_error_submsg *submsg = malloc(sizeof *submsg); + struct fx_error_submsg *submsg = malloc(sizeof *submsg); if (!submsg) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } char buf[512]; @@ -404,7 +404,7 @@ enum b_status b_error_add_submsg_string( if (!content) { free(submsg); - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } memcpy(content, buf, len); @@ -413,43 +413,43 @@ enum b_status b_error_add_submsg_string( submsg->msg_type = type; submsg->msg_content = content; - b_queue_push_back(&error->err_submsg, &submsg->msg_entry); + fx_queue_push_back(&error->err_submsg, &submsg->msg_entry); - return B_SUCCESS; + return FX_SUCCESS; } -enum b_status z__b_error_add_submsg_template( - struct b_error *error, enum b_error_submsg_type type, - b_error_msg_id msg_id, struct b_error_template_parameter params[]) +enum fx_status z__fx_error_add_submsg_template( + struct fx_error *error, enum fx_error_submsg_type type, + fx_error_msg_id msg_id, struct fx_error_template_parameter params[]) { - const struct b_error_msg *msg - = b_error_vendor_get_msg(error->err_vendor, msg_id); + const struct fx_error_msg *msg + = fx_error_vendor_get_msg(error->err_vendor, msg_id); if (!msg) { fprintf(stderr, "Undefined %s error message %lu\n", error->err_vendor->v_name, msg_id); abort(); } - struct b_error_submsg *submsg = malloc(sizeof *submsg); + struct fx_error_submsg *submsg = malloc(sizeof *submsg); if (!submsg) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } submsg->msg_type = type; submsg->msg_msg = msg; for (size_t i = 0; params[i].param_name; i++) { - const struct b_error_template_parameter *param = ¶ms[i]; + const struct fx_error_template_parameter *param = ¶ms[i]; - const struct b_error_template_parameter_definition *param_def - = b_error_msg_get_template_parameter(msg, param->param_name); + const struct fx_error_template_parameter_definition *param_def + = fx_error_msg_get_template_parameter(msg, param->param_name); if (!param_def) { continue; } memcpy(&submsg->msg_params[i], param, sizeof *param); submsg->msg_params[i].__param_def = param_def; - if (param_def->param_type == B_ERROR_TEMPLATE_PARAM_STRING) { + if (param_def->param_type == FX_ERROR_TEMPLATE_PARAM_STRING) { const char *s = (const char *)submsg->msg_params[i].param_value; size_t len = strlen(s); @@ -466,46 +466,46 @@ enum b_status z__b_error_add_submsg_template( } } - b_queue_push_back(&error->err_submsg, &submsg->msg_entry); + fx_queue_push_back(&error->err_submsg, &submsg->msg_entry); - return B_SUCCESS; + return FX_SUCCESS; } -void b_error_discard(struct b_error *error) +void fx_error_discard(struct fx_error *error) { - b_queue_delete(&allocated_errors, &error->err_entry); - b_queue_push_back(&free_errors, &error->err_entry); + fx_queue_delete(&allocated_errors, &error->err_entry); + fx_queue_push_back(&free_errors, &error->err_entry); } -void z__b_error_throw( - struct b_error *error, const char *file, unsigned int line, const char *func) +void z__fx_error_throw( + struct fx_error *error, const char *file, unsigned int line, const char *func) { if (!error) { return; } - error = z__b_error_propagate(error, file, line, func); + error = z__fx_error_propagate(error, file, line, func); error_reporter(error, error_reporter_flags); free_error(error); } -b_error_status_code b_error_get_status_code(const struct b_error *error) +fx_error_status_code fx_error_get_status_code(const struct fx_error *error) { return error ? error->err_code : 0; } -const struct b_error_vendor *b_error_get_vendor(const struct b_error *error) +const struct fx_error_vendor *fx_error_get_vendor(const struct fx_error *error) { return error->err_vendor; } -const struct b_error_definition *b_error_get_definition(const struct b_error *error) +const struct fx_error_definition *fx_error_get_definition(const struct fx_error *error) { return error->err_def; } -const struct b_error_template_parameter *b_error_get_template_parameter( - const struct b_error *error, const char *param_name) +const struct fx_error_template_parameter *fx_error_get_template_parameter( + const struct fx_error *error, const char *param_name) { const size_t nr_params = sizeof error->err_params / sizeof error->err_params[0]; @@ -522,110 +522,110 @@ const struct b_error_template_parameter *b_error_get_template_parameter( return NULL; } -const struct b_error_template_parameter *b_error_get_template_parameters( - const struct b_error *error) +const struct fx_error_template_parameter *fx_error_get_template_parameters( + const struct fx_error *error) { return error->err_params; } -const char *b_error_get_description(const struct b_error *error) +const char *fx_error_get_description(const struct fx_error *error) { return error->err_description; } -const struct b_error_msg *b_error_get_msg(const b_error *error) +const struct fx_error_msg *fx_error_get_msg(const fx_error *error) { return error->err_msg; } -const struct b_error_submsg *b_error_get_first_submsg(const struct b_error *error) +const struct fx_error_submsg *fx_error_get_first_submsg(const struct fx_error *error) { - struct b_queue_entry *entry = b_queue_first(&error->err_submsg); + struct fx_queue_entry *entry = fx_queue_first(&error->err_submsg); if (!entry) { return NULL; } - return b_unbox(struct b_error_submsg, entry, msg_entry); + return fx_unbox(struct fx_error_submsg, entry, msg_entry); } -const struct b_error_submsg *b_error_get_next_submsg( - const struct b_error *error, const struct b_error_submsg *msg) +const struct fx_error_submsg *fx_error_get_next_submsg( + const struct fx_error *error, const struct fx_error_submsg *msg) { - struct b_queue_entry *entry = b_queue_next(&msg->msg_entry); + struct fx_queue_entry *entry = fx_queue_next(&msg->msg_entry); if (!entry) { return NULL; } - return b_unbox(struct b_error_submsg, entry, msg_entry); + return fx_unbox(struct fx_error_submsg, entry, msg_entry); } -const struct b_error_stack_frame *b_error_get_first_stack_frame( - const struct b_error *error) +const struct fx_error_stack_frame *fx_error_get_first_stack_frame( + const struct fx_error *error) { - struct b_queue_entry *entry = b_queue_first(&error->err_stack); + struct fx_queue_entry *entry = fx_queue_first(&error->err_stack); if (!entry) { return NULL; } - return b_unbox(struct b_error_stack_frame, entry, f_entry); + return fx_unbox(struct fx_error_stack_frame, entry, f_entry); } -const struct b_error_stack_frame *b_error_get_next_stack_frame( - const struct b_error *error, const struct b_error_stack_frame *frame) +const struct fx_error_stack_frame *fx_error_get_next_stack_frame( + const struct fx_error *error, const struct fx_error_stack_frame *frame) { - struct b_queue_entry *entry = b_queue_next(&frame->f_entry); + struct fx_queue_entry *entry = fx_queue_next(&frame->f_entry); if (!entry) { return NULL; } - return b_unbox(struct b_error_stack_frame, entry, f_entry); + return fx_unbox(struct fx_error_stack_frame, entry, f_entry); } -const b_error *b_error_get_caused_by(const struct b_error *error) +const fx_error *fx_error_get_caused_by(const struct fx_error *error) { return error->err_caused_by; } -enum b_error_submsg_type b_error_submsg_get_type(const struct b_error_submsg *msg) +enum fx_error_submsg_type fx_error_submsg_get_type(const struct fx_error_submsg *msg) { return msg->msg_type; } -const char *b_error_submsg_get_content(const struct b_error_submsg *msg) +const char *fx_error_submsg_get_content(const struct fx_error_submsg *msg) { return msg->msg_content; } -const struct b_error_msg *b_error_submsg_get_msg(const b_error_submsg *msg) +const struct fx_error_msg *fx_error_submsg_get_msg(const fx_error_submsg *msg) { return msg->msg_msg; } -const struct b_error_template_parameter *b_error_submsg_get_template_parameters( - const struct b_error_submsg *msg) +const struct fx_error_template_parameter *fx_error_submsg_get_template_parameters( + const struct fx_error_submsg *msg) { return msg->msg_params; } -const char *b_error_stack_frame_get_filepath(const struct b_error_stack_frame *frame) +const char *fx_error_stack_frame_get_filepath(const struct fx_error_stack_frame *frame) { return frame->f_file; } -unsigned int b_error_stack_frame_get_line_number( - const struct b_error_stack_frame *frame) +unsigned int fx_error_stack_frame_get_line_number( + const struct fx_error_stack_frame *frame) { return frame->f_line_number; } -const char *b_error_stack_frame_get_function_name( - const struct b_error_stack_frame *frame) +const char *fx_error_stack_frame_get_function_name( + const struct fx_error_stack_frame *frame) { return frame->f_function; } -const struct b_error_template_parameter_definition *b_error_definition_get_template_parameter( - const struct b_error_definition *error_def, const char *param_name) +const struct fx_error_template_parameter_definition *fx_error_definition_get_template_parameter( + const struct fx_error_definition *error_def, const char *param_name) { size_t nr_param = sizeof error_def->err_params / sizeof error_def->err_params[0]; @@ -643,13 +643,13 @@ const struct b_error_template_parameter_definition *b_error_definition_get_templ return NULL; } -const char *b_error_msg_get_content(const struct b_error_msg *msg) +const char *fx_error_msg_get_content(const struct fx_error_msg *msg) { return msg->msg_message; } -const struct b_error_template_parameter_definition *b_error_msg_get_template_parameter( - const struct b_error_msg *msg, const char *param_name) +const struct fx_error_template_parameter_definition *fx_error_msg_get_template_parameter( + const struct fx_error_msg *msg, const char *param_name) { size_t nr_param = sizeof msg->msg_params / sizeof msg->msg_params[0]; @@ -666,74 +666,74 @@ const struct b_error_template_parameter_definition *b_error_msg_get_template_par return NULL; } -void b_set_error_report_function( - b_error_report_function func, enum b_error_report_flags flags) +void fx_set_error_report_function( + fx_error_report_function func, enum fx_error_report_flags flags) { if (!func) { func = default_error_reporter; } if (!flags) { - flags = B_ERROR_REPORT_DEFAULT; + flags = FX_ERROR_REPORT_DEFAULT; } error_reporter = func; error_reporter_flags = flags; } -static const struct b_error_definition builtin_errors[] = { - B_ERROR_DEFINITION(B_SUCCESS, "SUCCESS", "Success"), - B_ERROR_DEFINITION(B_ERR_NO_MEMORY, "NO_MEMORY", "Out of memory"), - B_ERROR_DEFINITION( - B_ERR_OUT_OF_BOUNDS, "OUT_OF_BOUNDS", "Argument out of bounds"), - B_ERROR_DEFINITION( - B_ERR_INVALID_ARGUMENT, "INVALID_ARGUMENT", "Invalid argument"), - B_ERROR_DEFINITION( - B_ERR_NAME_EXISTS, "NAME_EXISTS", "Name already exists"), - B_ERROR_DEFINITION( - B_ERR_NOT_SUPPORTED, "NOT_SUPPORTED", "Operation not supported"), - B_ERROR_DEFINITION(B_ERR_BAD_STATE, "BAD_STATE", "Bad state"), - B_ERROR_DEFINITION(B_ERR_NO_ENTRY, "NO_ENTRY", "Name does not exist"), - B_ERROR_DEFINITION(B_ERR_NO_DATA, "NO_DATA", "No data available"), - B_ERROR_DEFINITION(B_ERR_NO_SPACE, "NO_DATA", "No space available"), - B_ERROR_DEFINITION( - B_ERR_UNKNOWN_FUNCTION, "UNKNOWN_FUNCTION", "Unknown function"), - B_ERROR_DEFINITION(B_ERR_BAD_FORMAT, "BAD_FORMAT", "Bad format"), - B_ERROR_DEFINITION(B_ERR_IO_FAILURE, "IO_FAILURE", "I/O failure"), - B_ERROR_DEFINITION( - B_ERR_IS_DIRECTORY, "IS_DIRECTORY", "Item is a directory"), - B_ERROR_DEFINITION( - B_ERR_NOT_DIRECTORY, "NOT_DIRECTORY", "Item is not a directory"), - B_ERROR_DEFINITION( - B_ERR_PERMISSION_DENIED, "PERMISSION_DENIED", +static const struct fx_error_definition builtin_errors[] = { + FX_ERROR_DEFINITION(FX_SUCCESS, "SUCCESS", "Success"), + FX_ERROR_DEFINITION(FX_ERR_NO_MEMORY, "NO_MEMORY", "Out of memory"), + FX_ERROR_DEFINITION( + FX_ERR_OUT_OF_BOUNDS, "OUT_OF_BOUNDS", "Argument out of bounds"), + FX_ERROR_DEFINITION( + FX_ERR_INVALID_ARGUMENT, "INVALID_ARGUMENT", "Invalid argument"), + FX_ERROR_DEFINITION( + FX_ERR_NAME_EXISTS, "NAME_EXISTS", "Name already exists"), + FX_ERROR_DEFINITION( + FX_ERR_NOT_SUPPORTED, "NOT_SUPPORTED", "Operation not supported"), + FX_ERROR_DEFINITION(FX_ERR_BAD_STATE, "BAD_STATE", "Bad state"), + FX_ERROR_DEFINITION(FX_ERR_NO_ENTRY, "NO_ENTRY", "Name does not exist"), + FX_ERROR_DEFINITION(FX_ERR_NO_DATA, "NO_DATA", "No data available"), + FX_ERROR_DEFINITION(FX_ERR_NO_SPACE, "NO_DATA", "No space available"), + FX_ERROR_DEFINITION( + FX_ERR_UNKNOWN_FUNCTION, "UNKNOWN_FUNCTION", "Unknown function"), + FX_ERROR_DEFINITION(FX_ERR_BAD_FORMAT, "BAD_FORMAT", "Bad format"), + FX_ERROR_DEFINITION(FX_ERR_IO_FAILURE, "IO_FAILURE", "I/O failure"), + FX_ERROR_DEFINITION( + FX_ERR_IS_DIRECTORY, "IS_DIRECTORY", "Item is a directory"), + FX_ERROR_DEFINITION( + FX_ERR_NOT_DIRECTORY, "NOT_DIRECTORY", "Item is not a directory"), + FX_ERROR_DEFINITION( + FX_ERR_PERMISSION_DENIED, "PERMISSION_DENIED", "Permission denied"), - B_ERROR_DEFINITION(B_ERR_BUSY, "BUSY", "Resource busy or locked"), - B_ERROR_DEFINITION( - B_ERR_COMPRESSION_FAILURE, "COMPRESSION_FAILURE", + FX_ERROR_DEFINITION(FX_ERR_BUSY, "BUSY", "Resource busy or locked"), + FX_ERROR_DEFINITION( + FX_ERR_COMPRESSION_FAILURE, "COMPRESSION_FAILURE", "Compression failure"), - B_ERROR_DEFINITION( - B_ERR_TYPE_REGISTRATION_FAILURE, "TYPE_REGISTRATION_FAILURE", + FX_ERROR_DEFINITION( + FX_ERR_TYPE_REGISTRATION_FAILURE, "TYPE_REGISTRATION_FAILURE", "Type registration failure"), - B_ERROR_DEFINITION( - B_ERR_CLASS_INIT_FAILURE, "CLASS_INIT_FAILURE", + FX_ERROR_DEFINITION( + FX_ERR_CLASS_INIT_FAILURE, "CLASS_INIT_FAILURE", "Class initialisation failure"), }; -static const struct b_error_msg builtin_msg[] = { - B_ERROR_MSG(B_MSG_TYPE_REGISTRATION_FAILURE, "Type registration failed"), - B_ERROR_MSG(B_MSG_CLASS_INIT_FAILURE, "Class initialisation failed"), - B_ERROR_MSG_TEMPLATE( - B_MSG_CLASS_SPECIFIES_UNKNOWN_INTERFACE, +static const struct fx_error_msg builtin_msg[] = { + FX_ERROR_MSG(FX_MSG_TYPE_REGISTRATION_FAILURE, "Type registration failed"), + FX_ERROR_MSG(FX_MSG_CLASS_INIT_FAILURE, "Class initialisation failed"), + FX_ERROR_MSG_TEMPLATE( + FX_MSG_CLASS_SPECIFIES_UNKNOWN_INTERFACE, "Class @[class_name] specifies an implementation for interface " "@[interface_name] that it does not extend or inherit", - B_ERROR_TEMPLATE_PARAM( - "class_name", B_ERROR_TEMPLATE_PARAM_STRING, "%s"), - B_ERROR_TEMPLATE_PARAM( - "interface_name", B_ERROR_TEMPLATE_PARAM_STRING, "%s")), + FX_ERROR_TEMPLATE_PARAM( + "class_name", FX_ERROR_TEMPLATE_PARAM_STRING, "%s"), + FX_ERROR_TEMPLATE_PARAM( + "interface_name", FX_ERROR_TEMPLATE_PARAM_STRING, "%s")), }; -static const struct b_error_vendor builtin_vendor = { - .v_name = "Blue", +static const struct fx_error_vendor builtin_vendor = { + .v_name = "FX", .v_error_definitions = builtin_errors, .v_error_definitions_length = sizeof builtin_errors, .v_msg = builtin_msg, @@ -741,46 +741,46 @@ static const struct b_error_vendor builtin_vendor = { }; static void get_error_id( - const struct b_error_vendor *vendor, const struct b_error *error, + const struct fx_error_vendor *vendor, const struct fx_error *error, char *out, size_t max) { const char *vendor_name = NULL; const char *error_name = NULL; - b_error_status_code code = b_error_get_status_code(error); + fx_error_status_code code = fx_error_get_status_code(error); if (vendor) { vendor_name = vendor->v_name; - error_name = b_error_vendor_get_status_code_name(vendor, code); + error_name = fx_error_vendor_get_status_code_name(vendor, code); } - b_stringstream *id = b_stringstream_create_with_buffer(out, max); + fx_stringstream *id = fx_stringstream_create_with_buffer(out, max); if (vendor_name) { - b_stream_write_string(id, vendor_name, NULL); + fx_stream_write_string(id, vendor_name, NULL); } if (error_name) { if (vendor_name) { - b_stream_write_string(id, ".", NULL); + fx_stream_write_string(id, ".", NULL); } - b_stream_write_string(id, error_name, NULL); + fx_stream_write_string(id, error_name, NULL); } else { if (vendor_name) { - b_stream_write_string(id, "#", NULL); + fx_stream_write_string(id, "#", NULL); } - b_stream_write_fmt(id, NULL, "%ld", code); + fx_stream_write_fmt(id, NULL, "%ld", code); } - b_stringstream_unref(id); + fx_stringstream_unref(id); } static void print_template_parameter( - const struct b_error_template_parameter *params, size_t nr_params, + const struct fx_error_template_parameter *params, size_t nr_params, const char *param_name) { - const struct b_error_template_parameter *param = NULL; + const struct fx_error_template_parameter *param = NULL; for (size_t i = 0; i < nr_params; i++) { if (!params[i].param_name) { @@ -800,57 +800,57 @@ static void print_template_parameter( const char *format = param->__param_def->param_format; switch (param->__param_def->param_type) { - case B_ERROR_TEMPLATE_PARAM_STRING: { + case FX_ERROR_TEMPLATE_PARAM_STRING: { const char *s = (const char *)param->param_value; fprintf(stderr, format, s); break; } - case B_ERROR_TEMPLATE_PARAM_CHAR: { + case FX_ERROR_TEMPLATE_PARAM_CHAR: { char v = (char)param->param_value; fprintf(stderr, format, v); break; } - case B_ERROR_TEMPLATE_PARAM_INT: { + case FX_ERROR_TEMPLATE_PARAM_INT: { int v = (int)param->param_value; fprintf(stderr, format, v); break; } - case B_ERROR_TEMPLATE_PARAM_UINT: { + case FX_ERROR_TEMPLATE_PARAM_UINT: { unsigned int v = (unsigned int)param->param_value; fprintf(stderr, format, v); break; } - case B_ERROR_TEMPLATE_PARAM_LONG: { + case FX_ERROR_TEMPLATE_PARAM_LONG: { long v = (long)param->param_value; fprintf(stderr, format, v); break; } - case B_ERROR_TEMPLATE_PARAM_ULONG: { + case FX_ERROR_TEMPLATE_PARAM_ULONG: { unsigned long v = (unsigned long)param->param_value; fprintf(stderr, format, v); break; } - case B_ERROR_TEMPLATE_PARAM_LONGLONG: { + case FX_ERROR_TEMPLATE_PARAM_LONGLONG: { long long v = (long long)param->param_value; fprintf(stderr, format, v); break; } - case B_ERROR_TEMPLATE_PARAM_ULONGLONG: { + case FX_ERROR_TEMPLATE_PARAM_ULONGLONG: { unsigned long long v = (unsigned long long)param->param_value; fprintf(stderr, format, v); break; } - case B_ERROR_TEMPLATE_PARAM_PTR: { + case FX_ERROR_TEMPLATE_PARAM_PTR: { const void *p = (const void *)param->param_value; fprintf(stderr, format, p); break; } - case B_ERROR_TEMPLATE_PARAM_INTPTR: { + case FX_ERROR_TEMPLATE_PARAM_INTPTR: { intptr_t v = (intptr_t)param->param_value; fprintf(stderr, format, v); break; } - case B_ERROR_TEMPLATE_PARAM_UINTPTR: { + case FX_ERROR_TEMPLATE_PARAM_UINTPTR: { uintptr_t v = (uintptr_t)param->param_value; fprintf(stderr, format, v); break; @@ -861,7 +861,7 @@ static void print_template_parameter( } static void print_content( - const struct b_error_template_parameter *params, size_t nr_params, + const struct fx_error_template_parameter *params, size_t nr_params, const char *s) { char modifier = 0; @@ -931,14 +931,14 @@ static void print_content( } } -static void print_submsg(const struct b_error *error) +static void print_submsg(const struct fx_error *error) { - const struct b_error_definition *error_def = b_error_get_definition(error); - const struct b_error_submsg *msg = b_error_get_first_submsg(error); + const struct fx_error_definition *error_def = fx_error_get_definition(error); + const struct fx_error_submsg *msg = fx_error_get_first_submsg(error); while (msg) { - enum b_error_submsg_type type = b_error_submsg_get_type(msg); - const char *content = b_error_submsg_get_content(msg); + enum fx_error_submsg_type type = fx_error_submsg_get_type(msg); + const char *content = fx_error_submsg_get_content(msg); fprintf(stderr, " > "); @@ -948,7 +948,7 @@ static void print_submsg(const struct b_error *error) content); fprintf(stderr, "\n"); - msg = b_error_get_next_submsg(error, msg); + msg = fx_error_get_next_submsg(error, msg); } } @@ -971,40 +971,40 @@ const char *get_short_filepath(const char *path) return path; } -static void print_stack_trace(const struct b_error *error) +static void print_stack_trace(const struct fx_error *error) { - const struct b_error_stack_frame *frame - = b_error_get_first_stack_frame(error); + const struct fx_error_stack_frame *frame + = fx_error_get_first_stack_frame(error); while (frame) { const char *file, *func; unsigned int line; - file = b_error_stack_frame_get_filepath(frame); - line = b_error_stack_frame_get_line_number(frame); - func = b_error_stack_frame_get_function_name(frame); + file = fx_error_stack_frame_get_filepath(frame); + line = fx_error_stack_frame_get_line_number(frame); + func = fx_error_stack_frame_get_function_name(frame); file = get_short_filepath(file); fprintf(stderr, " at %s() (%s:%u)\n", func, file, line); - frame = b_error_get_next_stack_frame(error, frame); + frame = fx_error_get_next_stack_frame(error, frame); } } static void report_error( - const b_error *error, b_error_report_flags flags, bool caused_by) + const fx_error *error, fx_error_report_flags flags, bool caused_by) { - const b_error_vendor *vendor = b_error_get_vendor(error); + const fx_error_vendor *vendor = fx_error_get_vendor(error); char error_id[128]; get_error_id(vendor, error, error_id, sizeof error_id); - const struct b_error_definition *error_def = b_error_get_definition(error); - b_error_status_code code = b_error_get_status_code(error); - const char *description = b_error_get_description(error); + const struct fx_error_definition *error_def = fx_error_get_definition(error); + fx_error_status_code code = fx_error_get_status_code(error); + const char *description = fx_error_get_description(error); if (!description && vendor) { - description = b_error_vendor_get_status_code_description( + description = fx_error_vendor_get_status_code_description( vendor, code); } @@ -1014,11 +1014,11 @@ static void report_error( fprintf(stderr, "==> ERROR "); } - if (flags & B_ERROR_REPORT_STATUS) { + if (flags & FX_ERROR_REPORT_STATUS) { fprintf(stderr, "%s", error_id); } - if (flags & B_ERROR_REPORT_DESCRIPTION) { + if (flags & FX_ERROR_REPORT_DESCRIPTION) { if (error->err_msg) { fprintf(stderr, ": "); print_content( @@ -1038,22 +1038,22 @@ static void report_error( fprintf(stderr, "\n"); - if (flags & B_ERROR_REPORT_SUBMSG) { + if (flags & FX_ERROR_REPORT_SUBMSG) { print_submsg(error); } - if (flags & B_ERROR_REPORT_STACK_TRACE) { + if (flags & FX_ERROR_REPORT_STACK_TRACE) { print_stack_trace(error); } - const struct b_error *cause = b_error_get_caused_by(error); - if (cause && (flags & B_ERROR_REPORT_CAUSE)) { + const struct fx_error *cause = fx_error_get_caused_by(error); + if (cause && (flags & FX_ERROR_REPORT_CAUSE)) { report_error(cause, flags, true); } } static void default_error_reporter( - const struct b_error *error, enum b_error_report_flags flags) + const struct fx_error *error, enum fx_error_report_flags flags) { report_error(error, flags, false); } diff --git a/core/error.h b/core/error.h index 19abae5..22eccd3 100644 --- a/core/error.h +++ b/core/error.h @@ -1,37 +1,37 @@ -#ifndef _BLUELIB_ERROR_H_ -#define _BLUELIB_ERROR_H_ +#ifndef _FX_ERROR_H_ +#define _FX_ERROR_H_ -#include -#include +#include +#include -struct b_error_stack_frame { - b_queue_entry f_entry; +struct fx_error_stack_frame { + fx_queue_entry f_entry; const char *f_file; unsigned int f_line_number; const char *f_function; }; -struct b_error_submsg { - b_queue_entry msg_entry; - b_error_submsg_type msg_type; +struct fx_error_submsg { + fx_queue_entry msg_entry; + fx_error_submsg_type msg_type; char *msg_content; - const struct b_error_msg *msg_msg; - struct b_error_template_parameter msg_params[B_ERROR_TEMPLATE_PARAMETER_MAX]; + const struct fx_error_msg *msg_msg; + struct fx_error_template_parameter msg_params[FX_ERROR_TEMPLATE_PARAMETER_MAX]; }; -struct b_error { - const struct b_error_vendor *err_vendor; - b_error_status_code err_code; +struct fx_error { + const struct fx_error_vendor *err_vendor; + fx_error_status_code err_code; - const struct b_error_definition *err_def; - const struct b_error_msg *err_msg; + const struct fx_error_definition *err_def; + const struct fx_error_msg *err_msg; char *err_description; - struct b_error_template_parameter err_params[B_ERROR_TEMPLATE_PARAMETER_MAX]; + struct fx_error_template_parameter err_params[FX_ERROR_TEMPLATE_PARAMETER_MAX]; - struct b_queue err_submsg; - struct b_queue err_stack; - b_queue_entry err_entry; - struct b_error *err_caused_by; + struct fx_queue err_submsg; + struct fx_queue err_stack; + fx_queue_entry err_entry; + struct fx_error *err_caused_by; }; #endif diff --git a/core/hash/hash.c b/core/hash/hash.c index f0ddc2f..8cffcff 100644 --- a/core/hash/hash.c +++ b/core/hash/hash.c @@ -1,7 +1,7 @@ #include "hash.h" -#include -#include +#include +#include #include #include #include @@ -10,46 +10,46 @@ #define FNV1_OFFSET_BASIS 0xcbf29ce484222325 #define FNV1_PRIME 0x100000001b3 -extern struct b_hash_function_ops z__b_md4_ops; -extern struct b_hash_function_ops z__b_md5_ops; -extern struct b_hash_function_ops z__b_sha1_ops; -extern struct b_hash_function_ops z__b_sha2_224_ops; -extern struct b_hash_function_ops z__b_sha2_256_ops; -extern struct b_hash_function_ops z__b_sha2_384_ops; -extern struct b_hash_function_ops z__b_sha2_512_ops; -extern struct b_hash_function_ops z__b_sha3_224_ops; -extern struct b_hash_function_ops z__b_sha3_256_ops; -extern struct b_hash_function_ops z__b_sha3_384_ops; -extern struct b_hash_function_ops z__b_sha3_512_ops; -extern struct b_hash_function_ops z__b_shake128_ops; -extern struct b_hash_function_ops z__b_shake256_ops; +extern struct fx_hash_function_ops z__fx_md4_ops; +extern struct fx_hash_function_ops z__fx_md5_ops; +extern struct fx_hash_function_ops z__fx_sha1_ops; +extern struct fx_hash_function_ops z__fx_sha2_224_ops; +extern struct fx_hash_function_ops z__fx_sha2_256_ops; +extern struct fx_hash_function_ops z__fx_sha2_384_ops; +extern struct fx_hash_function_ops z__fx_sha2_512_ops; +extern struct fx_hash_function_ops z__fx_sha3_224_ops; +extern struct fx_hash_function_ops z__fx_sha3_256_ops; +extern struct fx_hash_function_ops z__fx_sha3_384_ops; +extern struct fx_hash_function_ops z__fx_sha3_512_ops; +extern struct fx_hash_function_ops z__fx_shake128_ops; +extern struct fx_hash_function_ops z__fx_shake256_ops; -static const struct b_hash_function_ops *hash_functions[] = { - [B_HASH_NONE] = NULL, - [B_HASH_MD4] = &z__b_md4_ops, - [B_HASH_MD5] = &z__b_md5_ops, - [B_HASH_SHA1] = &z__b_sha1_ops, - [B_HASH_SHA2_224] = &z__b_sha2_224_ops, - [B_HASH_SHA2_256] = &z__b_sha2_256_ops, - [B_HASH_SHA2_384] = &z__b_sha2_384_ops, - [B_HASH_SHA2_512] = &z__b_sha2_512_ops, - [B_HASH_SHA3_224] = &z__b_sha3_224_ops, - [B_HASH_SHA3_256] = &z__b_sha3_256_ops, - [B_HASH_SHA3_384] = &z__b_sha3_384_ops, - [B_HASH_SHA3_512] = &z__b_sha3_512_ops, - [B_HASH_SHAKE128] = &z__b_shake128_ops, - [B_HASH_SHAKE256] = &z__b_shake256_ops, +static const struct fx_hash_function_ops *hash_functions[] = { + [FX_HASH_NONE] = NULL, + [FX_HASH_MD4] = &z__fx_md4_ops, + [FX_HASH_MD5] = &z__fx_md5_ops, + [FX_HASH_SHA1] = &z__fx_sha1_ops, + [FX_HASH_SHA2_224] = &z__fx_sha2_224_ops, + [FX_HASH_SHA2_256] = &z__fx_sha2_256_ops, + [FX_HASH_SHA2_384] = &z__fx_sha2_384_ops, + [FX_HASH_SHA2_512] = &z__fx_sha2_512_ops, + [FX_HASH_SHA3_224] = &z__fx_sha3_224_ops, + [FX_HASH_SHA3_256] = &z__fx_sha3_256_ops, + [FX_HASH_SHA3_384] = &z__fx_sha3_384_ops, + [FX_HASH_SHA3_512] = &z__fx_sha3_512_ops, + [FX_HASH_SHAKE128] = &z__fx_shake128_ops, + [FX_HASH_SHAKE256] = &z__fx_shake256_ops, }; static const size_t nr_hash_functions = sizeof hash_functions / sizeof hash_functions[0]; -uint64_t b_hash_cstr(const char *s) +uint64_t fx_hash_cstr(const char *s) { size_t x = 0; - return b_hash_cstr_ex(s, &x); + return fx_hash_cstr_ex(s, &x); } -uint64_t b_hash_cstr_ex(const char *s, size_t *len) +uint64_t fx_hash_cstr_ex(const char *s, size_t *len) { uint64_t hash = FNV1_OFFSET_BASIS; size_t i = 0; @@ -66,15 +66,15 @@ uint64_t b_hash_cstr_ex(const char *s, size_t *len) return hash; } -enum b_status b_hash_ctx_init(struct b_hash_ctx *ctx, enum b_hash_function func) +enum fx_status fx_hash_ctx_init(struct fx_hash_ctx *ctx, enum fx_hash_function func) { if (func < 0 || func >= nr_hash_functions) { - return B_ERR_NOT_SUPPORTED; + return FX_ERR_NOT_SUPPORTED; } - const struct b_hash_function_ops *hash_function = hash_functions[func]; + const struct fx_hash_function_ops *hash_function = hash_functions[func]; if (!hash_function) { - return B_ERR_NOT_SUPPORTED; + return FX_ERR_NOT_SUPPORTED; } memset(ctx, 0x0, sizeof *ctx); @@ -86,13 +86,13 @@ enum b_status b_hash_ctx_init(struct b_hash_ctx *ctx, enum b_hash_function func) hash_function->hash_init(ctx); } - return B_SUCCESS; + return FX_SUCCESS; } -enum b_status b_hash_ctx_reset(struct b_hash_ctx *ctx) +enum fx_status fx_hash_ctx_reset(struct fx_hash_ctx *ctx) { - if (ctx->ctx_func == B_HASH_NONE || !ctx->ctx_ops) { - return B_ERR_BAD_STATE; + if (ctx->ctx_func == FX_HASH_NONE || !ctx->ctx_ops) { + return FX_ERR_BAD_STATE; } memset(&ctx->ctx_state, 0x0, sizeof ctx->ctx_state); @@ -101,69 +101,69 @@ enum b_status b_hash_ctx_reset(struct b_hash_ctx *ctx) ctx->ctx_ops->hash_init(ctx); } - return B_SUCCESS; + return FX_SUCCESS; } -enum b_status b_hash_ctx_update(struct b_hash_ctx *ctx, const void *p, size_t len) +enum fx_status fx_hash_ctx_update(struct fx_hash_ctx *ctx, const void *p, size_t len) { if (!ctx->ctx_ops) { - return B_ERR_BAD_STATE; + return FX_ERR_BAD_STATE; } if (!ctx->ctx_ops->hash_update) { - return B_ERR_NOT_SUPPORTED; + return FX_ERR_NOT_SUPPORTED; } ctx->ctx_ops->hash_update(ctx, p, len); - return B_SUCCESS; + return FX_SUCCESS; } -static void update_rope(const b_rope *rope, void *arg) +static void update_rope(const fx_rope *rope, void *arg) { - struct b_hash_ctx *ctx = arg; - unsigned int type = B_ROPE_TYPE(rope->r_flags); + struct fx_hash_ctx *ctx = arg; + unsigned int type = FX_ROPE_TYPE(rope->r_flags); char tmp[64]; size_t len = 0; switch (type) { - case B_ROPE_F_CHAR: - b_hash_ctx_update(ctx, &rope->r_v.v_char, sizeof rope->r_v.v_char); + case FX_ROPE_F_CHAR: + fx_hash_ctx_update(ctx, &rope->r_v.v_char, sizeof rope->r_v.v_char); break; - case B_ROPE_F_CSTR: - case B_ROPE_F_CSTR_BORROWED: - case B_ROPE_F_CSTR_STATIC: - b_hash_ctx_update( + case FX_ROPE_F_CSTR: + case FX_ROPE_F_CSTR_BORROWED: + case FX_ROPE_F_CSTR_STATIC: + fx_hash_ctx_update( ctx, rope->r_v.v_cstr.s, strlen(rope->r_v.v_cstr.s)); break; - case B_ROPE_F_INT: + case FX_ROPE_F_INT: len = snprintf(tmp, sizeof tmp, "%" PRIdPTR, rope->r_v.v_int); - b_hash_ctx_update(ctx, tmp, len); + fx_hash_ctx_update(ctx, tmp, len); break; - case B_ROPE_F_UINT: + case FX_ROPE_F_UINT: len = snprintf(tmp, sizeof tmp, "%" PRIuPTR, rope->r_v.v_uint); - b_hash_ctx_update(ctx, tmp, len); + fx_hash_ctx_update(ctx, tmp, len); break; default: break; } } -enum b_status b_hash_ctx_update_rope( - struct b_hash_ctx *ctx, const struct b_rope *rope) +enum fx_status fx_hash_ctx_update_rope( + struct fx_hash_ctx *ctx, const struct fx_rope *rope) { - b_rope_iterate(rope, update_rope, ctx); - return B_SUCCESS; + fx_rope_iterate(rope, update_rope, ctx); + return FX_SUCCESS; } -enum b_status b_hash_ctx_finish( - struct b_hash_ctx *ctx, void *out_digest, size_t out_max) +enum fx_status fx_hash_ctx_finish( + struct fx_hash_ctx *ctx, void *out_digest, size_t out_max) { if (!ctx->ctx_ops) { - return B_ERR_BAD_STATE; + return FX_ERR_BAD_STATE; } if (!ctx->ctx_ops->hash_finish) { - return B_ERR_NOT_SUPPORTED; + return FX_ERR_NOT_SUPPORTED; } ctx->ctx_ops->hash_finish(ctx, out_digest, out_max); @@ -174,5 +174,5 @@ enum b_status b_hash_ctx_finish( ctx->ctx_ops->hash_init(ctx); } - return B_SUCCESS; + return FX_SUCCESS; } diff --git a/core/hash/hash.h b/core/hash/hash.h index 821544a..a02528f 100644 --- a/core/hash/hash.h +++ b/core/hash/hash.h @@ -3,12 +3,12 @@ #include -struct b_hash_ctx; +struct fx_hash_ctx; -struct b_hash_function_ops { - void (*hash_init)(struct b_hash_ctx *); - void (*hash_update)(struct b_hash_ctx *, const void *, size_t); - void (*hash_finish)(struct b_hash_ctx *, void *, size_t); +struct fx_hash_function_ops { + void (*hash_init)(struct fx_hash_ctx *); + void (*hash_update)(struct fx_hash_ctx *, const void *, size_t); + void (*hash_finish)(struct fx_hash_ctx *, void *, size_t); }; #endif diff --git a/core/hash/md4.c b/core/hash/md4.c index 0b19337..de1ce32 100644 --- a/core/hash/md4.c +++ b/core/hash/md4.c @@ -36,7 +36,7 @@ */ #include "hash.h" -#include +#include #include #include @@ -88,7 +88,7 @@ * This processes one or more 64-byte data blocks, but does NOT update the bit * counters. There are no alignment requirements. */ -static const void *body(struct b_hash_ctx *ctx, const void *data, unsigned long size) +static const void *body(struct fx_hash_ctx *ctx, const void *data, unsigned long size) { const unsigned char *ptr; uint32_t a, b, c, d; @@ -178,7 +178,7 @@ static const void *body(struct b_hash_ctx *ctx, const void *data, unsigned long return ptr; } -void md4_init(struct b_hash_ctx *ctx) +void md4_init(struct fx_hash_ctx *ctx) { ctx->ctx_state.md4.a = 0x67452301; ctx->ctx_state.md4.b = 0xefcdab89; @@ -189,7 +189,7 @@ void md4_init(struct b_hash_ctx *ctx) ctx->ctx_state.md4.hi = 0; } -void md4_update(struct b_hash_ctx *ctx, const void *data, unsigned long size) +void md4_update(struct fx_hash_ctx *ctx, const void *data, unsigned long size) { uint32_t saved_lo; unsigned long used, available; @@ -229,7 +229,7 @@ void md4_update(struct b_hash_ctx *ctx, const void *data, unsigned long size) (dst)[2] = (unsigned char)((src) >> 16); \ (dst)[3] = (unsigned char)((src) >> 24); -void md4_finish(struct b_hash_ctx *ctx, void *out, size_t max) +void md4_finish(struct fx_hash_ctx *ctx, void *out, size_t max) { unsigned char *result = out; unsigned long used, available; @@ -261,7 +261,7 @@ void md4_finish(struct b_hash_ctx *ctx, void *out, size_t max) OUT(&result[12], ctx->ctx_state.md4.d) } -struct b_hash_function_ops z__b_md4_ops = { +struct fx_hash_function_ops z__fx_md4_ops = { .hash_init = md4_init, .hash_update = md4_update, .hash_finish = md4_finish, diff --git a/core/hash/md5.c b/core/hash/md5.c index c8ffc38..794eb59 100644 --- a/core/hash/md5.c +++ b/core/hash/md5.c @@ -3,7 +3,7 @@ * and is in the public domain. */ #include "hash.h" -#include +#include #include #include #include @@ -19,7 +19,7 @@ #define STEP(f, a, b, c, d, x, t, s) \ (a += f(b, c, d) + x + t, a = ROTATE_LEFT(a, s), a += b) -void md5_init(struct b_hash_ctx *ctx) +void md5_init(struct fx_hash_ctx *ctx) { ctx->ctx_state.md5.a = 0x67452301; ctx->ctx_state.md5.b = 0xefcdab89; @@ -30,7 +30,7 @@ void md5_init(struct b_hash_ctx *ctx) ctx->ctx_state.md5.count[1] = 0; } -uint8_t *md5_transform(struct b_hash_ctx *ctx, const void *data, uintmax_t size) +uint8_t *md5_transform(struct fx_hash_ctx *ctx, const void *data, uintmax_t size) { uint8_t *ptr = (uint8_t *)data; uint32_t a, b, c, d, aa, bb, cc, dd; @@ -140,7 +140,7 @@ uint8_t *md5_transform(struct b_hash_ctx *ctx, const void *data, uintmax_t size) return ptr; } -void md5_update(struct b_hash_ctx *ctx, const void *buffer, size_t buffer_size) +void md5_update(struct fx_hash_ctx *ctx, const void *buffer, size_t buffer_size) { uint32_t saved_low = ctx->ctx_state.md5.count[0]; uint32_t used; @@ -178,7 +178,7 @@ void md5_update(struct b_hash_ctx *ctx, const void *buffer, size_t buffer_size) memcpy(ctx->ctx_state.md5.input, buffer, buffer_size); } -void md5_finish(struct b_hash_ctx *ctx, void *out, size_t max) +void md5_finish(struct fx_hash_ctx *ctx, void *out, size_t max) { uint32_t used = ctx->ctx_state.md5.count[0] & 0x3f; ctx->ctx_state.md5.input[used++] = 0x80; @@ -193,7 +193,7 @@ void md5_finish(struct b_hash_ctx *ctx, void *out, size_t max) memset(&ctx->ctx_state.md5.input[used], 0, free - 8); - unsigned char digest[B_DIGEST_LENGTH_MD5]; + unsigned char digest[FX_DIGEST_LENGTH_MD5]; ctx->ctx_state.md5.count[0] <<= 3; ctx->ctx_state.md5.input[56] = (uint8_t)(ctx->ctx_state.md5.count[0]); @@ -228,10 +228,10 @@ void md5_finish(struct b_hash_ctx *ctx, void *out, size_t max) digest[14] = (uint8_t)(ctx->ctx_state.md5.d >> 16); digest[15] = (uint8_t)(ctx->ctx_state.md5.d >> 24); - memcpy(out, digest, b_min(size_t, sizeof digest, max)); + memcpy(out, digest, fx_min(size_t, sizeof digest, max)); } -struct b_hash_function_ops z__b_md5_ops = { +struct fx_hash_function_ops z__fx_md5_ops = { .hash_init = md5_init, .hash_update = md5_update, .hash_finish = md5_finish, diff --git a/core/hash/sha1.c b/core/hash/sha1.c index 8f209ca..0dc6094 100644 --- a/core/hash/sha1.c +++ b/core/hash/sha1.c @@ -19,7 +19,7 @@ A million repetitions of "a" #include "hash.h" -#include +#include #include #include @@ -187,7 +187,7 @@ void SHA1Transform(uint32_t state[5], const unsigned char buffer[64]) /* sha_init - Initialize new context */ -static void sha_init(struct b_hash_ctx *context) +static void sha_init(struct fx_hash_ctx *context) { /* SHA1 initialization constants */ context->ctx_state.sha1.state[0] = 0x67452301; @@ -200,7 +200,7 @@ static void sha_init(struct b_hash_ctx *context) /* Run your data through this. */ -static void sha_update(struct b_hash_ctx *context, const void *p, size_t len) +static void sha_update(struct fx_hash_ctx *context, const void *p, size_t len) { const unsigned char *data = p; uint32_t i, j; @@ -226,7 +226,7 @@ static void sha_update(struct b_hash_ctx *context, const void *p, size_t len) /* Add padding and return the message digest. */ -static void sha_finish(struct b_hash_ctx *context, void *out, size_t max) +static void sha_finish(struct fx_hash_ctx *context, void *out, size_t max) { unsigned i; @@ -241,7 +241,7 @@ static void sha_finish(struct b_hash_ctx *context, void *out, size_t max) & 255); /* Endian independent */ } - char digest[B_DIGEST_LENGTH_SHA1]; + char digest[FX_DIGEST_LENGTH_SHA1]; c = 0200; sha_update(context, &c, 1); while ((context->ctx_state.sha1.count[0] & 504) != 448) { @@ -255,12 +255,12 @@ static void sha_finish(struct b_hash_ctx *context, void *out, size_t max) & 255); } - memcpy(out, digest, b_min(size_t, sizeof digest, max)); + memcpy(out, digest, fx_min(size_t, sizeof digest, max)); /* Wipe variables */ memset(&finalcount, '\0', sizeof(finalcount)); } -struct b_hash_function_ops z__b_sha1_ops = { +struct fx_hash_function_ops z__fx_sha1_ops = { .hash_init = sha_init, .hash_update = sha_update, .hash_finish = sha_finish, diff --git a/core/hash/sha2_224.c b/core/hash/sha2_224.c index a55d6a2..4355560 100644 --- a/core/hash/sha2_224.c +++ b/core/hash/sha2_224.c @@ -1,14 +1,14 @@ // SHA-224. Adapted from LibTomCrypt. This code is Public Domain #include "hash.h" -#include +#include #include -extern void z__b_sha2_256_update( - struct b_hash_ctx *md, const void *src, size_t inlen); -extern void z__b_sha2_256_finish(struct b_hash_ctx *md, void *out, size_t max); +extern void z__fx_sha2_256_update( + struct fx_hash_ctx *md, const void *src, size_t inlen); +extern void z__fx_sha2_256_finish(struct fx_hash_ctx *md, void *out, size_t max); -static void sha_init(struct b_hash_ctx *md) +static void sha_init(struct fx_hash_ctx *md) { md->ctx_state.sha2_256.curlen = 0; md->ctx_state.sha2_256.length = 0; @@ -22,20 +22,20 @@ static void sha_init(struct b_hash_ctx *md) md->ctx_state.sha2_256.state[7] = 0xbefa4fa4UL; } -static void sha_update(struct b_hash_ctx *md, const void *in, size_t inlen) +static void sha_update(struct fx_hash_ctx *md, const void *in, size_t inlen) { - z__b_sha2_256_update(md, in, inlen); + z__fx_sha2_256_update(md, in, inlen); } -static void sha_finish(struct b_hash_ctx *md, void *out, size_t max) +static void sha_finish(struct fx_hash_ctx *md, void *out, size_t max) { - unsigned char res[B_DIGEST_LENGTH_SHA2_256]; - z__b_sha2_256_finish(md, res, max); + unsigned char res[FX_DIGEST_LENGTH_SHA2_256]; + z__fx_sha2_256_finish(md, res, max); /* truncate the digest to 224 bits */ - memcpy(out, res, b_min(size_t, max, B_DIGEST_LENGTH_224)); + memcpy(out, res, fx_min(size_t, max, FX_DIGEST_LENGTH_224)); } -struct b_hash_function_ops z__b_sha2_224_ops = { +struct fx_hash_function_ops z__fx_sha2_224_ops = { .hash_init = sha_init, .hash_update = sha_update, .hash_finish = sha_finish, diff --git a/core/hash/sha2_256.c b/core/hash/sha2_256.c index 723d966..83af845 100644 --- a/core/hash/sha2_256.c +++ b/core/hash/sha2_256.c @@ -1,7 +1,7 @@ // SHA-256. Adapted from LibTomCrypt. This code is Public Domain #include "hash.h" -#include +#include #include static const uint32_t K[64] = { @@ -87,7 +87,7 @@ static void RND( (*h) = *t0 + *t1; } -static void sha_compress(struct b_hash_ctx *md, const unsigned char *buf) +static void sha_compress(struct fx_hash_ctx *md, const unsigned char *buf) { uint32_t S[8], W[64], t0, t1, t; @@ -127,7 +127,7 @@ static void sha_compress(struct b_hash_ctx *md, const unsigned char *buf) // Public interface -static void sha_init(struct b_hash_ctx *md) +static void sha_init(struct fx_hash_ctx *md) { md->ctx_state.sha2_256.curlen = 0; md->ctx_state.sha2_256.length = 0; @@ -141,7 +141,7 @@ static void sha_init(struct b_hash_ctx *md) md->ctx_state.sha2_256.state[7] = 0x5BE0CD19UL; } -void z__b_sha2_256_update(struct b_hash_ctx *md, const void *src, size_t inlen) +void z__fx_sha2_256_update(struct fx_hash_ctx *md, const void *src, size_t inlen) { const uint32_t block_size = sizeof md->ctx_state.sha2_256.buf; const unsigned char *in = (const unsigned char *)(src); @@ -171,7 +171,7 @@ void z__b_sha2_256_update(struct b_hash_ctx *md, const void *src, size_t inlen) } } } -void z__b_sha2_256_finish(struct b_hash_ctx *md, void *out, size_t max) +void z__fx_sha2_256_finish(struct fx_hash_ctx *md, void *out, size_t max) { // Increase the length of the message md->ctx_state.sha2_256.length += md->ctx_state.sha2_256.curlen * 8; @@ -198,18 +198,18 @@ void z__b_sha2_256_finish(struct b_hash_ctx *md, void *out, size_t max) store64(md->ctx_state.sha2_256.length, md->ctx_state.sha2_256.buf + 56); sha_compress(md, md->ctx_state.sha2_256.buf); - unsigned char digest[B_DIGEST_LENGTH_SHA2_256]; + unsigned char digest[FX_DIGEST_LENGTH_SHA2_256]; // Copy output for (int i = 0; i < 8; i++) store32(md->ctx_state.sha2_256.state[i], (unsigned char *)&digest[(4 * i)]); - memcpy(out, digest, b_min(size_t, sizeof digest, max)); + memcpy(out, digest, fx_min(size_t, sizeof digest, max)); } -struct b_hash_function_ops z__b_sha2_256_ops = { +struct fx_hash_function_ops z__fx_sha2_256_ops = { .hash_init = sha_init, - .hash_update = z__b_sha2_256_update, - .hash_finish = z__b_sha2_256_finish, + .hash_update = z__fx_sha2_256_update, + .hash_finish = z__fx_sha2_256_finish, }; diff --git a/core/hash/sha2_384.c b/core/hash/sha2_384.c index 4709a2e..c18c18d 100644 --- a/core/hash/sha2_384.c +++ b/core/hash/sha2_384.c @@ -1,14 +1,14 @@ // SHA-384. Adapted from LibTomCrypt. This code is Public Domain #include "hash.h" -#include +#include #include -extern void z__b_sha2_512_update( - struct b_hash_ctx *md, const void *src, size_t inlen); -extern void z__b_sha2_512_finish(struct b_hash_ctx *md, void *out, size_t max); +extern void z__fx_sha2_512_update( + struct fx_hash_ctx *md, const void *src, size_t inlen); +extern void z__fx_sha2_512_finish(struct fx_hash_ctx *md, void *out, size_t max); -void sha_init(struct b_hash_ctx *md) +void sha_init(struct fx_hash_ctx *md) { md->ctx_state.sha2_512.curlen = 0; md->ctx_state.sha2_512.length = 0; @@ -22,20 +22,20 @@ void sha_init(struct b_hash_ctx *md) md->ctx_state.sha2_512.state[7] = 0x47b5481dbefa4fa4ULL; } -static void sha_update(struct b_hash_ctx *md, const void *in, size_t inlen) +static void sha_update(struct fx_hash_ctx *md, const void *in, size_t inlen) { - z__b_sha2_512_update(md, in, inlen); + z__fx_sha2_512_update(md, in, inlen); } -static void sha_finish(struct b_hash_ctx *md, void *out, size_t max) +static void sha_finish(struct fx_hash_ctx *md, void *out, size_t max) { - unsigned char res[B_DIGEST_LENGTH_SHA2_512]; - z__b_sha2_512_finish(md, res, max); + unsigned char res[FX_DIGEST_LENGTH_SHA2_512]; + z__fx_sha2_512_finish(md, res, max); /* truncate the digest to 384 bits */ - memcpy(out, res, b_min(size_t, max, B_DIGEST_LENGTH_384)); + memcpy(out, res, fx_min(size_t, max, FX_DIGEST_LENGTH_384)); } -struct b_hash_function_ops z__b_sha2_384_ops = { +struct fx_hash_function_ops z__fx_sha2_384_ops = { .hash_init = sha_init, .hash_update = sha_update, .hash_finish = sha_finish, diff --git a/core/hash/sha2_512.c b/core/hash/sha2_512.c index ef43f34..955e922 100644 --- a/core/hash/sha2_512.c +++ b/core/hash/sha2_512.c @@ -1,7 +1,7 @@ // SHA-512. Adapted from LibTomCrypt. This code is Public Domain #include "hash.h" -#include +#include #include #include @@ -96,7 +96,7 @@ static void RND( *h = *t0 + *t1; } -static void sha_compress(struct b_hash_ctx *md, const unsigned char *buf) +static void sha_compress(struct fx_hash_ctx *md, const unsigned char *buf) { uint64_t S[8], W[80], t0, t1; @@ -144,7 +144,7 @@ static void sha_compress(struct b_hash_ctx *md, const unsigned char *buf) // Public interface -static void sha_init(struct b_hash_ctx *md) +static void sha_init(struct fx_hash_ctx *md) { md->ctx_state.sha2_512.curlen = 0; md->ctx_state.sha2_512.length = 0; @@ -158,7 +158,7 @@ static void sha_init(struct b_hash_ctx *md) md->ctx_state.sha2_512.state[7] = 0x5be0cd19137e2179ULL; } -void z__b_sha2_512_update(struct b_hash_ctx *md, const void *src, size_t inlen) +void z__fx_sha2_512_update(struct fx_hash_ctx *md, const void *src, size_t inlen) { const uint32_t block_size = sizeof md->ctx_state.sha2_512.block; const unsigned char *in = (const unsigned char *)src; @@ -189,7 +189,7 @@ void z__b_sha2_512_update(struct b_hash_ctx *md, const void *src, size_t inlen) } } -void z__b_sha2_512_finish(struct b_hash_ctx *md, void *out, size_t max) +void z__fx_sha2_512_finish(struct fx_hash_ctx *md, void *out, size_t max) { // Increase the length of the message md->ctx_state.sha2_512.length += md->ctx_state.sha2_512.curlen * 8ULL; @@ -218,7 +218,7 @@ void z__b_sha2_512_finish(struct b_hash_ctx *md, void *out, size_t max) store64(md->ctx_state.sha2_512.length, md->ctx_state.sha2_512.block + 120); sha_compress(md, md->ctx_state.sha2_512.block); - unsigned char digest[B_DIGEST_LENGTH_SHA2_512]; + unsigned char digest[FX_DIGEST_LENGTH_SHA2_512]; // Copy output for (int i = 0; i < 8; i++) { @@ -226,11 +226,11 @@ void z__b_sha2_512_finish(struct b_hash_ctx *md, void *out, size_t max) (unsigned char *)&digest[(8 * i)]); } - memcpy(out, digest, b_min(size_t, sizeof digest, max)); + memcpy(out, digest, fx_min(size_t, sizeof digest, max)); } -struct b_hash_function_ops z__b_sha2_512_ops = { +struct fx_hash_function_ops z__fx_sha2_512_ops = { .hash_init = sha_init, - .hash_update = z__b_sha2_512_update, - .hash_finish = z__b_sha2_512_finish, + .hash_update = z__fx_sha2_512_update, + .hash_finish = z__fx_sha2_512_finish, }; diff --git a/core/hash/sha3.c b/core/hash/sha3.c index b273874..b5382c1 100644 --- a/core/hash/sha3.c +++ b/core/hash/sha3.c @@ -30,8 +30,8 @@ #include "hash.h" -#include -#include +#include +#include #include #ifndef KECCAKF_ROUNDS @@ -134,7 +134,7 @@ void sha3_keccakf(uint64_t st[25]) // Initialize the context for SHA3 -int sha3_init(struct b_hash_ctx *c, int mdlen) +int sha3_init(struct fx_hash_ctx *c, int mdlen) { int i; @@ -149,7 +149,7 @@ int sha3_init(struct b_hash_ctx *c, int mdlen) // update state with more data -void sha3_update(struct b_hash_ctx *c, const void *data, size_t len) +void sha3_update(struct fx_hash_ctx *c, const void *data, size_t len) { size_t i; int j; @@ -168,7 +168,7 @@ void sha3_update(struct b_hash_ctx *c, const void *data, size_t len) // finalize and output a hash -int sha3_final(void *md, struct b_hash_ctx *c) +int sha3_final(void *md, struct fx_hash_ctx *c) { int i; @@ -187,7 +187,7 @@ int sha3_final(void *md, struct b_hash_ctx *c) void *sha3(const void *in, size_t inlen, void *md, int mdlen) { - struct b_hash_ctx sha3; + struct fx_hash_ctx sha3; sha3_init(&sha3, mdlen); sha3_update(&sha3, in, inlen); @@ -198,7 +198,7 @@ void *sha3(const void *in, size_t inlen, void *md, int mdlen) // SHAKE128 and SHAKE256 extensible-output functionality -void shake_xof(struct b_hash_ctx *c) +void shake_xof(struct fx_hash_ctx *c) { c->ctx_state.sha3.st.b[c->ctx_state.sha3.pt] ^= 0x1F; c->ctx_state.sha3.st.b[c->ctx_state.sha3.rsiz - 1] ^= 0x80; @@ -206,7 +206,7 @@ void shake_xof(struct b_hash_ctx *c) c->ctx_state.sha3.pt = 0; } -void shake_out(struct b_hash_ctx *c, void *out, size_t len) +void shake_out(struct fx_hash_ctx *c, void *out, size_t len) { size_t i; int j; @@ -222,111 +222,111 @@ void shake_out(struct b_hash_ctx *c, void *out, size_t len) c->ctx_state.sha3.pt = j; } -static void sha3_224_init(struct b_hash_ctx *ctx) +static void sha3_224_init(struct fx_hash_ctx *ctx) { - sha3_init(ctx, B_DIGEST_LENGTH_SHA3_224); + sha3_init(ctx, FX_DIGEST_LENGTH_SHA3_224); } -static void sha3_224_finish(struct b_hash_ctx *ctx, void *out, size_t max) +static void sha3_224_finish(struct fx_hash_ctx *ctx, void *out, size_t max) { - unsigned char md[B_DIGEST_LENGTH_SHA3_224]; + unsigned char md[FX_DIGEST_LENGTH_SHA3_224]; sha3_final(md, ctx); - memcpy(out, md, b_min(size_t, sizeof md, max)); + memcpy(out, md, fx_min(size_t, sizeof md, max)); } -static void sha3_256_init(struct b_hash_ctx *ctx) +static void sha3_256_init(struct fx_hash_ctx *ctx) { - sha3_init(ctx, B_DIGEST_LENGTH_SHA3_256); + sha3_init(ctx, FX_DIGEST_LENGTH_SHA3_256); } -static void sha3_256_finish(struct b_hash_ctx *ctx, void *out, size_t max) +static void sha3_256_finish(struct fx_hash_ctx *ctx, void *out, size_t max) { - unsigned char md[B_DIGEST_LENGTH_SHA3_256]; + unsigned char md[FX_DIGEST_LENGTH_SHA3_256]; sha3_final(md, ctx); - memcpy(out, md, b_min(size_t, sizeof md, max)); + memcpy(out, md, fx_min(size_t, sizeof md, max)); } -static void sha3_384_init(struct b_hash_ctx *ctx) +static void sha3_384_init(struct fx_hash_ctx *ctx) { - sha3_init(ctx, B_DIGEST_LENGTH_SHA3_384); + sha3_init(ctx, FX_DIGEST_LENGTH_SHA3_384); } -static void sha3_384_finish(struct b_hash_ctx *ctx, void *out, size_t max) +static void sha3_384_finish(struct fx_hash_ctx *ctx, void *out, size_t max) { - unsigned char md[B_DIGEST_LENGTH_SHA3_384]; + unsigned char md[FX_DIGEST_LENGTH_SHA3_384]; sha3_final(md, ctx); - memcpy(out, md, b_min(size_t, sizeof md, max)); + memcpy(out, md, fx_min(size_t, sizeof md, max)); } -static void sha3_512_init(struct b_hash_ctx *ctx) +static void sha3_512_init(struct fx_hash_ctx *ctx) { - sha3_init(ctx, B_DIGEST_LENGTH_SHA3_512); + sha3_init(ctx, FX_DIGEST_LENGTH_SHA3_512); } -static void sha3_512_finish(struct b_hash_ctx *ctx, void *out, size_t max) +static void sha3_512_finish(struct fx_hash_ctx *ctx, void *out, size_t max) { - unsigned char md[B_DIGEST_LENGTH_SHA3_512]; + unsigned char md[FX_DIGEST_LENGTH_SHA3_512]; sha3_final(md, ctx); - memcpy(out, md, b_min(size_t, sizeof md, max)); + memcpy(out, md, fx_min(size_t, sizeof md, max)); } -static void shake128_init(struct b_hash_ctx *ctx) +static void shake128_init(struct fx_hash_ctx *ctx) { - sha3_init(ctx, B_DIGEST_LENGTH_SHAKE128); + sha3_init(ctx, FX_DIGEST_LENGTH_SHAKE128); } -static void shake128_finish(struct b_hash_ctx *ctx, void *out, size_t max) +static void shake128_finish(struct fx_hash_ctx *ctx, void *out, size_t max) { - unsigned char md[B_DIGEST_LENGTH_SHAKE128]; + unsigned char md[FX_DIGEST_LENGTH_SHAKE128]; shake_xof(ctx); shake_out(ctx, md, sizeof md); - memcpy(out, md, b_min(size_t, sizeof md, max)); + memcpy(out, md, fx_min(size_t, sizeof md, max)); } -static void shake256_init(struct b_hash_ctx *ctx) +static void shake256_init(struct fx_hash_ctx *ctx) { - sha3_init(ctx, B_DIGEST_LENGTH_SHAKE256); + sha3_init(ctx, FX_DIGEST_LENGTH_SHAKE256); } -static void shake256_finish(struct b_hash_ctx *ctx, void *out, size_t max) +static void shake256_finish(struct fx_hash_ctx *ctx, void *out, size_t max) { - unsigned char md[B_DIGEST_LENGTH_SHAKE256]; + unsigned char md[FX_DIGEST_LENGTH_SHAKE256]; shake_xof(ctx); shake_out(ctx, md, sizeof md); - memcpy(out, md, b_min(size_t, sizeof md, max)); + memcpy(out, md, fx_min(size_t, sizeof md, max)); } -struct b_hash_function_ops z__b_sha3_224_ops = { +struct fx_hash_function_ops z__fx_sha3_224_ops = { .hash_init = sha3_224_init, .hash_update = sha3_update, .hash_finish = sha3_224_finish, }; -struct b_hash_function_ops z__b_sha3_256_ops = { +struct fx_hash_function_ops z__fx_sha3_256_ops = { .hash_init = sha3_256_init, .hash_update = sha3_update, .hash_finish = sha3_256_finish, }; -struct b_hash_function_ops z__b_sha3_384_ops = { +struct fx_hash_function_ops z__fx_sha3_384_ops = { .hash_init = sha3_384_init, .hash_update = sha3_update, .hash_finish = sha3_384_finish, }; -struct b_hash_function_ops z__b_sha3_512_ops = { +struct fx_hash_function_ops z__fx_sha3_512_ops = { .hash_init = sha3_512_init, .hash_update = sha3_update, .hash_finish = sha3_512_finish, }; -struct b_hash_function_ops z__b_shake128_ops = { +struct fx_hash_function_ops z__fx_shake128_ops = { .hash_init = shake128_init, .hash_update = sha3_update, .hash_finish = shake128_finish, }; -struct b_hash_function_ops z__b_shake256_ops = { +struct fx_hash_function_ops z__fx_shake256_ops = { .hash_init = shake256_init, .hash_update = sha3_update, .hash_finish = shake256_finish, diff --git a/core/include/blue/core/bstr.h b/core/include/blue/core/bstr.h deleted file mode 100644 index c344723..0000000 --- a/core/include/blue/core/bstr.h +++ /dev/null @@ -1,71 +0,0 @@ -#ifndef BLUE_CORE_BSTR_H_ -#define BLUE_CORE_BSTR_H_ - -#include -#include -#include -#include - -#define B_BSTR_MAGIC 0x5005500550055005ULL - -struct b_rope; - -enum b_bstr_flags { - B_BSTR_F_NONE = 0x00u, - B_BSTR_F_ALLOC = 0x01u, -}; - -typedef struct b_bstr { - uint64_t bstr_magic; - enum b_bstr_flags bstr_flags; - char *bstr_buf; - /* total number of characters in bstr_buf, not including null terminator */ - size_t bstr_len; - /* number of bytes allocated for bstr_buf (includes space for the null - * terminator) */ - size_t bstr_capacity; - int *bstr_istack; - int bstr_add_indent; - size_t bstr_istack_ptr, bstr_istack_size; -} b_bstr; - -BLUE_API void b_bstr_begin(b_bstr *strv, char *buf, size_t max); -BLUE_API void b_bstr_begin_dynamic(b_bstr *strv); -BLUE_API char *b_bstr_end(b_bstr *strv); -BLUE_API b_status b_bstr_reserve(b_bstr *strv, size_t len); - -static inline size_t b_bstr_get_size(const b_bstr *str) -{ - return str->bstr_len; -} - -static inline size_t b_bstr_get_capacity(const b_bstr *str) -{ - return str->bstr_capacity; -} - -BLUE_API b_status b_bstr_push_indent(b_bstr *strv, int indent); -BLUE_API b_status b_bstr_pop_indent(b_bstr *strv); - -BLUE_API b_status b_bstr_write_char(b_bstr *strv, char c); -BLUE_API b_status b_bstr_write_chars( - b_bstr *strv, const char *cs, size_t len, size_t *nr_written); -BLUE_API b_status b_bstr_write_cstr( - b_bstr *strv, const char *str, size_t *nr_written); -BLUE_API b_status b_bstr_write_cstr_list( - b_bstr *strv, const char **strs, size_t *nr_written); -BLUE_API b_status b_bstr_write_cstr_array( - b_bstr *strv, const char **strs, size_t count, size_t *nr_written); -BLUE_API b_status b_bstr_write_cstr_varg(b_bstr *strv, size_t *nr_written, ...); -BLUE_API b_status b_bstr_write_rope( - b_bstr *strv, const struct b_rope *rope, size_t *nr_written); -BLUE_API b_status b_bstr_write_fmt( - b_bstr *strv, size_t *nr_written, const char *format, ...); -BLUE_API b_status b_bstr_write_vfmt( - b_bstr *strv, size_t *nr_written, const char *format, va_list arg); - -BLUE_API char *b_bstr_rope(const struct b_rope *rope, size_t *nr_written); -BLUE_API char *b_bstr_fmt(size_t *nr_written, const char *format, ...); -BLUE_API char *b_bstr_vfmt(size_t *nr_written, const char *format, va_list arg); - -#endif diff --git a/core/include/blue/core/btree.h b/core/include/blue/core/btree.h deleted file mode 100644 index c055cdd..0000000 --- a/core/include/blue/core/btree.h +++ /dev/null @@ -1,359 +0,0 @@ -#ifndef BLUELIB_CORE_BTREE_H_ -#define BLUELIB_CORE_BTREE_H_ - -#include -#include -#include -#include -#include -#include - -B_DECLS_BEGIN; - -#define B_BTREE_INIT {0} -#define B_TYPE_BTREE_ITERATOR (b_btree_iterator_get_type()) - -B_DECLARE_TYPE(b_btree_iterator); - -B_TYPE_CLASS_DECLARATION_BEGIN(b_btree_iterator) -B_TYPE_CLASS_DECLARATION_END(b_btree_iterator) - -/* defines a simple node insertion function. - this function assumes that your nodes have simple integer keys that can be - compared with the usual operators. - - EXAMPLE: - if you have a tree node type like this: - - struct my_tree_node { - int key; - b_btree_node base; - } - - You would use the following call to generate an insert function for a tree - with this node type: - - BTREE_DEFINE_SIMPLE_INSERT( - struct my_tree_node, - base, - key, - my_tree_node_insert); - - Which would emit a function defined like: - - static void my_tree_node_insert(b_btree *tree, struct my_tree_node *node); - - @param node_type your custom tree node type. usually a structure that - contains a b_btree_node member. - @param container_node_member the name of the b_btree_node member variable - within your custom type. - @param container_key_member the name of the key member variable within your - custom type. - @param function_name the name of the function to generate. -*/ -#define B_BTREE_DEFINE_SIMPLE_INSERT( \ - node_type, container_node_member, container_key_member, function_name) \ - void function_name(b_btree *tree, node_type *node) \ - { \ - if (!tree->b_root) { \ - tree->b_root = &node->container_node_member; \ - b_btree_insert_fixup(tree, &node->container_node_member); \ - return; \ - } \ - \ - b_btree_node *cur = tree->b_root; \ - while (1) { \ - node_type *cur_node = b_unbox( \ - node_type, cur, container_node_member); \ - b_btree_node *next = NULL; \ - \ - if (node->container_key_member \ - >= cur_node->container_key_member) { \ - next = b_btree_right(cur); \ - \ - if (!next) { \ - b_btree_put_right( \ - cur, \ - &node->container_node_member); \ - break; \ - } \ - } else if ( \ - node->container_key_member \ - < cur_node->container_key_member) { \ - next = b_btree_left(cur); \ - \ - if (!next) { \ - b_btree_put_left( \ - cur, \ - &node->container_node_member); \ - break; \ - } \ - } \ - \ - cur = next; \ - } \ - \ - b_btree_insert_fixup(tree, &node->container_node_member); \ - } - -/* defines a node insertion function. - this function should be used for trees with complex node keys that cannot be - directly compared. a comparator for your keys must be supplied. - - EXAMPLE: - if you have a tree node type like this: - - struct my_tree_node { - complex_key_t key; - b_btree_node base; - } - - You would need to define a comparator function or macro with the following - signature: - - int my_comparator(struct my_tree_node *a, struct my_tree_node *b); - - Which implements the following: - - return -1 if a < b - return 0 if a == b - return 1 if a > b - - You would use the following call to generate an insert function for a tree - with this node type: - - BTREE_DEFINE_INSERT(struct my_tree_node, base, key, my_tree_node_insert, - my_comparator); - - Which would emit a function defined like: - - static void my_tree_node_insert(b_btree *tree, struct my_tree_node *node); - - @param node_type your custom tree node type. usually a structure that - contains a b_btree_node member. - @param container_node_member the name of the b_btree_node member variable - within your custom type. - @param container_key_member the name of the key member variable within your - custom type. - @param function_name the name of the function to generate. - @param comparator the name of a comparator function or functional-macro that - conforms to the requirements listed above. -*/ -#define B_BTREE_DEFINE_INSERT( \ - node_type, container_node_member, container_key_member, function_name, \ - comparator) \ - void function_name(b_btree *tree, node_type *node) \ - { \ - if (!tree->b_root) { \ - tree->b_root = &node->container_node_member; \ - b_btree_insert_fixup(tree, &node->container_node_member); \ - return; \ - } \ - \ - b_btree_node *cur = tree->b_root; \ - while (1) { \ - node_type *cur_node = b_unbox( \ - node_type, cur, container_node_member); \ - b_btree_node *next = NULL; \ - int cmp = comparator(node, cur_node); \ - \ - if (cmp >= 0) { \ - next = b_btree_right(cur); \ - \ - if (!next) { \ - b_btree_put_right( \ - cur, \ - &node->container_node_member); \ - break; \ - } \ - } else if (cmp < 0) { \ - next = b_btree_left(cur); \ - \ - if (!next) { \ - b_btree_put_left( \ - cur, \ - &node->container_node_member); \ - break; \ - } \ - } else { \ - return; \ - } \ - \ - cur = next; \ - } \ - \ - b_btree_insert_fixup(tree, &node->container_node_member); \ - } - -/* defines a simple tree search function. - this function assumes that your nodes have simple integer keys that can be - compared with the usual operators. - - EXAMPLE: - if you have a tree node type like this: - - struct my_tree_node { - int key; - b_btree_node base; - } - - You would use the following call to generate a search function for a tree - with this node type: - - BTREE_DEFINE_SIMPLE_GET(struct my_tree_node, int, base, key, - my_tree_node_get); - - Which would emit a function defined like: - - static struct my_tree_node *my_tree_node_get(b_btree *tree, int key); - - @param node_type your custom tree node type. usually a structure that - contains a b_btree_node member. - @param key_type the type name of the key embedded in your custom tree node - type. this type must be compatible with the builtin comparison operators. - @param container_node_member the name of the b_btree_node member variable - within your custom type. - @param container_key_member the name of the key member variable within your - custom type. - @param function_name the name of the function to generate. -*/ -#define B_BTREE_DEFINE_SIMPLE_GET( \ - node_type, key_type, container_node_member, container_key_member, \ - function_name) \ - node_type *function_name(const b_btree *tree, key_type key) \ - { \ - b_btree_node *cur = tree->b_root; \ - while (cur) { \ - node_type *cur_node = b_unbox( \ - node_type, cur, container_node_member); \ - if (key > cur_node->container_key_member) { \ - cur = b_btree_right(cur); \ - } else if (key < cur_node->container_key_member) { \ - cur = b_btree_left(cur); \ - } else { \ - return cur_node; \ - } \ - } \ - \ - return NULL; \ - } - -#define b_btree_foreach(it, btree) \ - for (int z__b_unique_name() = b_btree_iterator_begin(btree, it); \ - (it)->node != NULL; b_btree_iterator_next(it)) - -/* binary tree nodes. this *cannot* be used directly. you need to define a - custom node type that contains a member variable of type b_btree_node. - - you would then use the supplied macros to define functions to manipulate your - custom binary tree. -*/ -typedef struct b_btree_node { - struct b_btree_node *b_parent, *b_left, *b_right; - unsigned short b_height; -} b_btree_node; - -/* binary tree. unlike b_btree_node, you can define variables of type b_btree. - */ -typedef struct b_btree { - b_btree_node *b_root; -} b_btree; - -BLUE_API b_type b_btree_iterator_get_type(void); - -/* re-balance a binary tree after an insertion operation. - - NOTE that, if you define an insertion function using BTREE_DEFINE_INSERT or - similar, this function will automatically called for you. - - @param tree the tree to re-balance. - @param node the node that was just inserted into the tree. -*/ -BLUE_API void b_btree_insert_fixup(b_btree *tree, b_btree_node *node); - -/* delete a node from a binary tree and re-balance the tree afterwards. - - @param tree the tree to delete from - @param node the node to delete. -*/ -BLUE_API void b_btree_delete(b_btree *tree, b_btree_node *node); - -/* get the first node in a binary tree. - - this will be the node with the smallest key (i.e. the node that is - furthest-left from the root) -*/ -BLUE_API b_btree_node *b_btree_first(const b_btree *tree); - -/* get the last node in a binary tree. - - this will be the node with the largest key (i.e. the node that is - furthest-right from the root) -*/ -BLUE_API b_btree_node *b_btree_last(const b_btree *tree); -/* for any binary tree node, this function returns the node with the - * next-largest key value */ -BLUE_API b_btree_node *b_btree_next(const b_btree_node *node); -/* for any binary tree node, this function returns the node with the - * next-smallest key value */ -BLUE_API b_btree_node *b_btree_prev(const b_btree_node *node); -/* return true if the btree is empty, false otherwise */ -static inline bool b_btree_empty(const b_btree *tree) -{ - return tree->b_root == NULL; -} - -/* sets `child` as the immediate left-child of `parent` */ -static inline void b_btree_put_left(b_btree_node *parent, b_btree_node *child) -{ - parent->b_left = child; - child->b_parent = parent; -} - -/* sets `child` as the immediate right-child of `parent` */ -static inline void b_btree_put_right(b_btree_node *parent, b_btree_node *child) -{ - parent->b_right = child; - child->b_parent = parent; -} - -/* get the immediate left-child of `node` */ -static inline b_btree_node *b_btree_left(b_btree_node *node) -{ - return node->b_left; -} - -/* get the immediate right-child of `node` */ -static inline b_btree_node *b_btree_right(b_btree_node *node) -{ - return node->b_right; -} - -/* get the immediate parent of `node` */ -static inline b_btree_node *b_btree_parent(b_btree_node *node) -{ - return node->b_parent; -} - -BLUE_API void b_btree_move(b_btree *tree, b_btree_node *dest, b_btree_node *src); - -/* get the height of `node`. - - the height of a node is defined as the length of the longest path - between the node and a leaf node. - - this count includes the node itself, so the height of a leaf node will be 1. -*/ -static inline unsigned short b_btree_height(b_btree_node *node) -{ - return node->b_height; -} - -BLUE_API b_iterator *b_btree_begin(b_btree *tree); -BLUE_API const b_iterator *b_btree_cbegin(const b_btree *tree); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/core/include/blue/core/class.h b/core/include/blue/core/class.h deleted file mode 100644 index 326393c..0000000 --- a/core/include/blue/core/class.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef BLUE_OBJECT_CLASS_H_ -#define BLUE_OBJECT_CLASS_H_ - -#include - -#define B_CLASS_MAGIC 0xDEADFACEDCAFEBEDULL -#define B_CLASS(p) ((b_class *)(p)) - -typedef struct _b_class b_class; - -BLUE_API void *b_class_get(b_type id); -BLUE_API const char *b_class_get_name(const b_class *c); -BLUE_API void *b_class_get_interface(const b_class *c, b_type id); - -#endif diff --git a/core/include/blue/core/encoding.h b/core/include/blue/core/encoding.h deleted file mode 100644 index 50c3f90..0000000 --- a/core/include/blue/core/encoding.h +++ /dev/null @@ -1,41 +0,0 @@ -#ifndef BLUE_CORE_ENCODING_H_ -#define BLUE_CORE_ENCODING_H_ - -#include -#include -#include - -#define B_WCHAR_INVALID ((b_wchar) - 1) - -typedef int32_t b_wchar; - -BLUE_API bool b_wchar_is_alpha(b_wchar c); -BLUE_API bool b_wchar_is_number(b_wchar c); -static inline bool b_wchar_is_bin_digit(b_wchar c) -{ - return c >= '0' && c <= '1'; -} -static inline bool b_wchar_is_oct_digit(b_wchar c) -{ - return c >= '0' && c <= '7'; -} -BLUE_API bool b_wchar_is_hex_digit(b_wchar c); -BLUE_API bool b_wchar_is_space(b_wchar c); -static inline bool b_wchar_is_alnum(b_wchar c) -{ - return b_wchar_is_alpha(c) || b_wchar_is_number(c); -} - -BLUE_API bool b_wchar_is_punct(b_wchar c); - -BLUE_API bool b_wchar_utf8_is_valid_scalar(b_wchar c); -BLUE_API unsigned int b_wchar_utf8_header_decode(char c); -BLUE_API unsigned int b_wchar_utf8_codepoint_size(b_wchar c); -BLUE_API b_wchar b_wchar_utf8_codepoint_decode(const char *s); -BLUE_API unsigned int b_wchar_utf8_codepoint_encode(b_wchar c, char s[4]); -BLUE_API unsigned int b_wchar_utf8_codepoint_stride(const char *s); -BLUE_API size_t b_wchar_utf8_codepoint_count(const char *s, size_t nr_bytes); -BLUE_API size_t b_wchar_utf8_string_encoded_size( - const b_wchar *s, size_t nr_codepoints); - -#endif diff --git a/core/include/blue/core/endian.h b/core/include/blue/core/endian.h deleted file mode 100644 index 593e5ed..0000000 --- a/core/include/blue/core/endian.h +++ /dev/null @@ -1,49 +0,0 @@ -#ifndef BLUELIB_CORE_ENDIAN_H_ -#define BLUELIB_CORE_ENDIAN_H_ - -#include -#include - -typedef struct { - union { - unsigned char i_bytes[sizeof(uint16_t)]; - int16_t i_val; - uint16_t i_uval; - }; -} b_i16; - -typedef struct { - union { - unsigned char i_bytes[sizeof(uint32_t)]; - int32_t i_val; - uint32_t i_uval; - }; -} b_i32; - -typedef struct { - union { - unsigned char i_bytes[sizeof(uint64_t)]; - int64_t i_val; - uint64_t i_uval; - }; -} b_i64; - -BLUE_API b_i16 b_i16_htob(uint16_t v); -BLUE_API b_i16 b_i16_htos(uint16_t v); - -BLUE_API uint16_t b_i16_btoh(b_i16 v); -BLUE_API uint16_t b_i16_stoh(b_i16 v); - -BLUE_API b_i32 b_i32_htob(uint32_t v); -BLUE_API b_i32 b_i32_htos(uint32_t v); - -BLUE_API uint32_t b_i32_btoh(b_i32 v); -BLUE_API uint32_t b_i32_stoh(b_i32 v); - -BLUE_API b_i64 b_i64_htob(uint64_t v); -BLUE_API b_i64 b_i64_htos(uint64_t v); - -BLUE_API uint64_t b_i64_btoh(b_i64 v); -BLUE_API uint64_t b_i64_stoh(b_i64 v); - -#endif diff --git a/core/include/blue/core/error.h b/core/include/blue/core/error.h deleted file mode 100644 index 7035ef5..0000000 --- a/core/include/blue/core/error.h +++ /dev/null @@ -1,418 +0,0 @@ -#ifndef BLUE_CORE_ERROR_H_ -#define BLUE_CORE_ERROR_H_ - -#include -#include -#include -#include - -#define B_ERROR_TEMPLATE_PARAMETER_MAX 4 -#define B_ERROR_MSG_ID_INVALID ((unsigned long)-1) - -#define B_CATCH(err, expr) ((err = (expr)) != NULL) - -#define b_result_is_error(result) ((result) != NULL) -#define b_result_is_success(result) ((result) == NULL) - -#define B_RESULT_SUCCESS ((b_result)NULL) -#define B_RESULT_ERR(err_name) \ - b_error_with_code(b_error_vendor_get_builtin(), B_ERR_##err_name) -#define B_RESULT_ERR_WITH_STRING(err_name, ...) \ - b_error_with_string( \ - b_error_vendor_get_builtin(), B_ERR_##err_name, __VA_ARGS__) -#define B_RESULT_STATUS(code) \ - ((code) == B_SUCCESS \ - ? B_RESULT_SUCCESS \ - : (b_error_with_code(b_error_vendor_get_builtin(), code))) -#define B_RESULT_STATUS_WITH_STRING(code, ...) \ - ((code) == B_SUCCESS \ - ? B_RESULT_SUCCESS \ - : (b_error_with_string( \ - b_error_vendor_get_builtin(), code, __VA_ARGS__))) - -#define B_ERRORS_BUILTIN (b_error_vendor_get_builtin()) -#define B_ERRORS_ERRNO (b_error_vendor_get_errno()) - -#define B_ERROR_PARAM(name, value) \ - (b_error_template_parameter) \ - { \ - .param_name = (name), .param_value = (uintptr_t)(value), \ - } - -#define B_ERROR_TEMPLATE_PARAM(name, type, format) \ - (b_error_template_parameter_definition) \ - { \ - .param_name = (name), .param_type = (type), \ - .param_format = (format), \ - } -#define B_ERROR_DEFINITION(code, name, msg) \ - [code] = (b_error_definition) \ - { \ - .err_name = (name), .err_message = (msg), \ - } -#define B_ERROR_DEFINITION_TEMPLATE(code, name, msg, ...) \ - [code] = (b_error_definition) \ - { \ - .err_name = (name), .err_message = (msg), \ - .err_params = __VA_ARGS__, \ - } - -#define B_ERROR_MSG(id, content) \ - [id] = (b_error_msg) \ - { \ - .msg_message = (content), \ - } -#define B_ERROR_MSG_TEMPLATE(id, content, ...) \ - [id] = (b_error_msg) \ - { \ - .msg_message = (content), .msg_params = __VA_ARGS__, \ - } - -#define z__b_error_create_status(status_code) \ - (z__b_error_create( \ - b_error_vendor_get_builtin(), status_code, NULL, NULL, 0, \ - NULL, NULL)) - -/* Error creation macros */ -#define b_error_with_code(vendor, code) \ - (z__b_error_create( \ - vendor, code, NULL, __FILE__, __LINE__, __FUNCTION__, NULL)) -#define b_error_caused_by_error(vendor, code, cause_error) \ - (z__b_error_create( \ - vendor, code, cause_error, __FILE__, __LINE__, __FUNCTION__, NULL)) -#define b_error_caused_by_status(vendor, code, cause_status) \ - (z__b_error_create( \ - vendor, code, z__b_error_create_status(cause_status), \ - __FILE__, __LINE__, __FUNCTION__, NULL)) -#define b_error_caused_by_code(vendor, code, cause_vendor, cause_code) \ - (z__b_error_create( \ - vendor, code, b_error_with_code(cause_vendor, cause_code), \ - __FILE__, __LINE__, __FUNCTION__, NULL)) -#define b_error_with_string(vendor, code, ...) \ - (z__b_error_create( \ - vendor, code, NULL, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)) -#define b_error_with_string_caused_by_error(vendor, code, cause_error, ...) \ - (z__b_error_create( \ - vendor, code, cause_error, __FILE__, __LINE__, __FUNCTION__, \ - __VA_ARGS__)) -#define b_error_with_string_caused_by_status(vendor, code, cause_status, ...) \ - (z__b_error_create( \ - vendor, code, z__b_error_create_status(cause_status), \ - __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)) -#define b_error_with_msg(vendor, code, msg_id) \ - (z__b_error_create_msg( \ - vendor, code, NULL, __FILE__, __LINE__, __FUNCTION__, msg_id, \ - (b_error_template_parameter[]) {{}})) -#define b_error_with_msg_caused_by_error(vendor, code, cause_error, msg_id) \ - (z__b_error_create_msg( \ - vendor, code, cause_error, __FILE__, __LINE__, __FUNCTION__, \ - msg_id, (b_error_template_parameter[]) {{}})) -#define b_error_with_msg_caused_by_status(vendor, code, cause_status, msg_id) \ - (z__b_error_create_msg( \ - vendor, code, z__b_error_create_status(cause_status), \ - __FILE__, __LINE__, __FUNCTION__, msg_id, \ - (b_error_template_parameter[]) {{}})) -#define b_error_with_msg_template(vendor, code, msg_id, ...) \ - (z__b_error_create_msg( \ - vendor, code, NULL, __FILE__, __LINE__, __FUNCTION__, msg_id, \ - (b_error_template_parameter[]) {__VA_ARGS__, {}})) -#define b_error_with_msg_template_caused_by_error( \ - vendor, code, cause_error, msg_id, ...) \ - (z__b_error_create_msg( \ - vendor, code, cause_error, __FILE__, __LINE__, __FUNCTION__, \ - msg_id, (b_error_template_parameter[]) {__VA_ARGS__, {}})) -#define b_error_with_msg_template_caused_by_status( \ - vendor, code, cause_status, msg_id, ...) \ - (z__b_error_create_msg( \ - vendor, code, z__b_error_create_status(cause_status), \ - __FILE__, __LINE__, __FUNCTION__, msg_id, \ - (b_error_template_parameter[]) {__VA_ARGS__, {}})) -#define b_error_with_template(vendor, code, ...) \ - (z__b_error_create_template( \ - vendor, code, NULL, __FILE__, __LINE__, __FUNCTION__, \ - (b_error_template_parameter[]) {__VA_ARGS__, {}})) -#define b_error_with_template_caused_by_error(vendor, code, cause_error, ...) \ - (z__b_error_create_template( \ - vendor, code, cause_error, __FILE__, __LINE__, __FUNCTION__, \ - (b_error_template_parameter[]) {__VA_ARGS__, {}})) -#define b_error_with_template_caused_by_status(vendor, code, cause_status, ...) \ - (z__b_error_create_template( \ - vendor, code, z__b_error_create_status(cause_status), \ - __FILE__, __LINE__, __FUNCTION__, \ - (b_error_template_parameter[]) {__VA_ARGS__, {}})) - -/* Error propagation macros */ -#define b_result_propagate(err) \ - (z__b_error_propagate(err, __FILE__, __LINE__, __FUNCTION__)) -#define b_error_caused_by(err, caused_by) (z__b_error_caused_by(err, caused_by)) -#define b_error_caused_by_b_status(err, status) \ - (z__b_error_caused_by_b_status(err, status)) -#define b_error_replace(err, caused_by) \ - (z__b_error_propagate(err, __FILE__, __LINE__, __FUNCTION__)) - -/* Error throw macros */ -#define z__b_throw(err) (z__b_error_throw(err, NULL, 0, NULL)) -#define b_throw(err) (z__b_error_throw(err, __FILE__, __LINE__, __FUNCTION__)) -#define b_throw_status(status) \ - (z__b_error_throw( \ - z__b_error_create( \ - b_error_vendor_get_builtin(), status, NULL, NULL, 0, \ - NULL, NULL), \ - __FILE__, __LINE__, __FUNCTION__)) - -#define b_throw_status_string(status, ...) \ - (z__b_error_throw( \ - z__b_error_create( \ - b_error_vendor_get_builtin(), status, NULL, NULL, 0, \ - NULL, __VA_ARGS__), \ - __FILE__, __LINE__, __FUNCTION__)) -#define b_throw_error_code(vendor, code) \ - z__b_throw(b_error_with_code(vendor, code)) -#define b_throw_error_caused_by_error(vendor, code, cause) \ - z__b_throw(b_error_caused_by_error(vendor, code, cause)) -#define b_throw_error_caused_by_status(vendor, code, cause) \ - z__b_throw(b_error_caused_by_status(vendor, code, cause)) -#define b_throw_error_with_string(vendor, code, ...) \ - z__b_throw(b_error_with_string(vendor, code, __VA_ARGS__)) -#define b_throw_error_with_string_caused_by_error(vendor, code, cause, ...) \ - z__b_throw(b_error_with_string_caused_by_error( \ - vendor, code, cause, __VA_ARGS__)) -#define b_throw_error_with_string_caused_by_status(vendor, code, cause, ...) \ - z__b_throw(b_error_with_string_caused_by_status( \ - vendor, code, cause, __VA_ARGS__)) - -#define b_throw_error_with_msg(vendor, code, msg_id) \ - z__b_throw(b_error_with_msg(vendor, code, msg_id)) -#define b_throw_error_with_msg_caused_by_error(vendor, code, cause, msg_id) \ - z__b_throw(b_error_with_msg_caused_by_error(vendor, code, cause, msg_id)) -#define b_throw_error_with_msg_caused_by_status(vendor, code, cause, msg_id) \ - z__b_throw(b_error_with_msg_caused_by_status(vendor, code, cause, msg_id)) - -#define b_throw_error_with_msg_template(vendor, code, msg_id, ...) \ - z__b_throw(b_error_with_msg_template(vendor, code, msg_id, __VA_ARGS__)) -#define b_throw_error_with_msg_template_caused_by_error( \ - vendor, code, cause, msg_id, ...) \ - z__b_throw(b_error_with_msg_template_caused_by_error( \ - vendor, code, cause, msg_id, __VA_ARGS__)) -#define b_throw_error_with_msg_template_caused_by_status( \ - vendor, code, cause, msg_id, ...) \ - z__b_throw(b_error_with_msg_template_caused_by_status( \ - vendor, code, cause, msg_id, __VA_ARGS__)) - -#define b_throw_error_with_template(vendor, code, ...) \ - z__b_throw(b_error_with_template(vendor, code, __VA_ARGS__)) -#define b_throw_error_with_template_caused_by_error(vendor, code, cause, ...) \ - z__b_throw(b_error_with_template_caused_by_error( \ - vendor, code, cause, __VA_ARGS__)) -#define b_throw_error_with_template_caused_by_status(vendor, code, cause, ...) \ - z__b_throw(b_error_with_template_caused_by_status( \ - vendor, code, cause, __VA_ARGS__)) - -#define B_ERR_MSG(s) \ - { \ - .msg_type = B_ERROR_MESSAGE_ERROR, \ - .msg_content = (s), \ - } -#define B_ERR_MSG_WARN(s) \ - { \ - .msg_type = B_ERROR_MESSAGE_WARN, \ - .msg_content = (s), \ - } -#define B_ERR_MSG_INFO(s) \ - { \ - .msg_type = B_ERROR_MESSAGE_INFO, \ - .msg_content = (s), \ - } -#define B_ERR_MSG_END(s) \ - { \ - .msg_type = B_ERROR_MESSAGE_NONE, \ - .msg_content = NULL, \ - } - -typedef enum b_error_submsg_type { - B_ERROR_SUBMSG_NONE = 0, - B_ERROR_SUBMSG_ERROR, - B_ERROR_SUBMSG_WARNING, - B_ERROR_SUBMSG_INFO, -} b_error_submsg_type; - -typedef enum b_error_report_flags { - B_ERROR_REPORT_NONE = 0, - B_ERROR_REPORT_STATUS = 0x01u, - B_ERROR_REPORT_DESCRIPTION = 0x02u, - B_ERROR_REPORT_SUBMSG = 0x04u, - B_ERROR_REPORT_STACK_TRACE = 0x08u, - B_ERROR_REPORT_CAUSE = 0x10u, - - B_ERROR_REPORT_MINIMAL = B_ERROR_REPORT_STATUS | B_ERROR_REPORT_DESCRIPTION, - B_ERROR_REPORT_DEFAULT = B_ERROR_REPORT_MINIMAL | B_ERROR_REPORT_SUBMSG - | B_ERROR_REPORT_CAUSE, - B_ERROR_REPORT_ALL = B_ERROR_REPORT_DEFAULT | B_ERROR_REPORT_STACK_TRACE, -} b_error_report_flags; - -typedef enum b_error_template_parameter_type { - B_ERROR_TEMPLATE_PARAM_NONE = 0, - B_ERROR_TEMPLATE_PARAM_STRING, - B_ERROR_TEMPLATE_PARAM_CHAR, - B_ERROR_TEMPLATE_PARAM_INT, - B_ERROR_TEMPLATE_PARAM_UINT, - B_ERROR_TEMPLATE_PARAM_LONG, - B_ERROR_TEMPLATE_PARAM_ULONG, - B_ERROR_TEMPLATE_PARAM_LONGLONG, - B_ERROR_TEMPLATE_PARAM_ULONGLONG, - B_ERROR_TEMPLATE_PARAM_SIZE_T, - B_ERROR_TEMPLATE_PARAM_INTPTR, - B_ERROR_TEMPLATE_PARAM_UINTPTR, - B_ERROR_TEMPLATE_PARAM_PTR, -} b_error_template_parameter_type; - -typedef struct b_error_template_parameter_definition { - const char *param_name; - b_error_template_parameter_type param_type; - const char *param_format; -} b_error_template_parameter_definition; - -typedef struct b_error_template_parameter { - const char *param_name; - uintptr_t param_value; - const struct b_error_template_parameter_definition *__param_def; -} b_error_template_parameter; - -struct b_error_vendor; - -typedef struct b_error b_error; -typedef struct b_error *b_result; -typedef struct b_error_submsg b_error_submsg; -typedef struct b_error_stack_frame b_error_stack_frame; -typedef long b_error_status_code; -typedef unsigned long b_error_msg_id; - -typedef struct b_error_definition { - const char *err_name; - const char *err_message; - const b_error_template_parameter_definition err_params[B_ERROR_TEMPLATE_PARAMETER_MAX]; -} b_error_definition; - -typedef struct b_error_msg { - const char *msg_message; - const b_error_template_parameter_definition msg_params[B_ERROR_TEMPLATE_PARAMETER_MAX]; -} b_error_msg; - -typedef const b_error_definition *(*b_error_status_code_get_definition)( - const struct b_error_vendor *, b_error_status_code); -typedef const b_error_msg *(*b_error_msg_get_definition)( - const struct b_error_vendor *, b_error_msg_id); -typedef void (*b_error_report_function)( - const struct b_error *, b_error_report_flags); - -typedef struct b_error_vendor { - const char *v_name; - - b_error_status_code_get_definition v_status_get_definition; - b_error_msg_get_definition v_msg_get_definition; - - const b_error_definition *v_error_definitions; - size_t v_error_definitions_length; - - const b_error_msg *v_msg; - size_t v_msg_length; -} b_error_vendor; - -BLUE_API b_error *z__b_error_create_template( - const b_error_vendor *, b_error_status_code, b_error *, const char *, - unsigned int, const char *, const b_error_template_parameter[]); -BLUE_API b_error *z__b_error_create_string( - const b_error_vendor *, b_error_status_code, b_error *, const char *, - unsigned int, const char *, const char *, va_list); -BLUE_API b_error *z__b_error_create_msg( - const b_error_vendor *, b_error_status_code, b_error *, const char *, - unsigned int, const char *, b_error_msg_id, - const b_error_template_parameter[]); -BLUE_API b_error *z__b_error_propagate( - b_error *, const char *, unsigned int, const char *); -BLUE_API b_error *z__b_error_caused_by(b_error *, b_error *); -BLUE_API b_error *z__b_error_caused_by_b_status(b_error *, b_status); -BLUE_API void z__b_error_throw(b_error *, const char *, unsigned int, const char *); - -BLUE_API bool b_result_is( - b_result result, const b_error_vendor *vendor, b_error_status_code code); - -BLUE_API const b_error_vendor *b_error_vendor_get_builtin(void); -BLUE_API const b_error_vendor *b_error_vendor_get_errno(void); -BLUE_API const b_error_definition *b_error_vendor_get_error_definition( - const b_error_vendor *vendor, b_error_status_code code); -BLUE_API const char *b_error_vendor_get_status_code_name( - const b_error_vendor *vendor, b_error_status_code code); -BLUE_API const char *b_error_vendor_get_status_code_description( - const b_error_vendor *vendor, b_error_status_code code); -BLUE_API const b_error_msg *b_error_vendor_get_msg( - const b_error_vendor *vendor, b_error_msg_id msg_id); - -static inline b_error *z__b_error_create( - const b_error_vendor *v, b_error_status_code c, b_error *c2, - const char *f0, unsigned int l, const char *f1, const char *d, ...) -{ - va_list arg; - va_start(arg, d); - b_error *err = z__b_error_create_string(v, c, c2, f0, l, f1, d, arg); - va_end(arg); - return err; -} - -BLUE_API enum b_status b_error_add_submsg_string( - b_error *error, b_error_submsg_type type, const char *msg, ...); -BLUE_API enum b_status z__b_error_add_submsg_template( - b_error *error, b_error_submsg_type type, b_error_msg_id msg_id, - b_error_template_parameter param[]); -#define b_error_add_submsg(error, type, msg_id) \ - (z__b_error_add_submsg_template( \ - error, type, msg_id, (b_error_template_parameter[]) {{}})) -#define b_error_add_submsg_template(error, type, msg_id, ...) \ - (z__b_error_add_submsg_template( \ - error, type, msg_id, \ - (b_error_template_parameter[]) {__VA_ARGS__, {}})) - -BLUE_API void b_error_discard(b_error *error); - -BLUE_API b_error_status_code b_error_get_status_code(const b_error *error); -BLUE_API const b_error_vendor *b_error_get_vendor(const b_error *error); -BLUE_API const b_error_definition *b_error_get_definition(const b_error *error); -BLUE_API const b_error_template_parameter *b_error_get_template_parameter( - const b_error *error, const char *param_name); -BLUE_API const b_error_template_parameter *b_error_get_template_parameters( - const b_error *error); -BLUE_API const char *b_error_get_description(const b_error *error); -BLUE_API const b_error_msg *b_error_get_msg(const b_error *error); -BLUE_API const b_error_submsg *b_error_get_first_submsg(const b_error *error); -BLUE_API const b_error_submsg *b_error_get_next_submsg( - const b_error *error, const b_error_submsg *msg); -BLUE_API const b_error_stack_frame *b_error_get_first_stack_frame( - const b_error *error); -BLUE_API const b_error_stack_frame *b_error_get_next_stack_frame( - const b_error *error, const b_error_stack_frame *frame); -BLUE_API const b_error *b_error_get_caused_by(const b_error *error); - -BLUE_API b_error_submsg_type b_error_submsg_get_type(const b_error_submsg *msg); -BLUE_API const char *b_error_submsg_get_content(const b_error_submsg *msg); -BLUE_API const b_error_msg *b_error_submsg_get_msg(const b_error_submsg *msg); -BLUE_API const b_error_template_parameter *b_error_submsg_get_template_parameters( - const b_error_submsg *msg); - -BLUE_API const char *b_error_stack_frame_get_filepath( - const b_error_stack_frame *frame); -BLUE_API unsigned int b_error_stack_frame_get_line_number( - const b_error_stack_frame *frame); -BLUE_API const char *b_error_stack_frame_get_function_name( - const b_error_stack_frame *frame); - -BLUE_API const b_error_template_parameter_definition *b_error_definition_get_template_parameter( - const b_error_definition *error_def, const char *param_name); - -BLUE_API const char *b_error_msg_get_content(const b_error_msg *msg); -BLUE_API const b_error_template_parameter_definition *b_error_msg_get_template_parameter( - const b_error_msg *msg, const char *param_name); - -BLUE_API void b_set_error_report_function( - b_error_report_function func, b_error_report_flags flags); - -#endif diff --git a/core/include/blue/core/hash.h b/core/include/blue/core/hash.h deleted file mode 100644 index 1e6fe6a..0000000 --- a/core/include/blue/core/hash.h +++ /dev/null @@ -1,111 +0,0 @@ -#ifndef BLUELIB_CORE_HASH_H_ -#define BLUELIB_CORE_HASH_H_ - -#include -#include -#include -#include - -#define B_DIGEST_LENGTH_128 16 -#define B_DIGEST_LENGTH_160 20 -#define B_DIGEST_LENGTH_192 24 -#define B_DIGEST_LENGTH_224 28 -#define B_DIGEST_LENGTH_256 32 -#define B_DIGEST_LENGTH_384 48 -#define B_DIGEST_LENGTH_512 64 - -#define B_DIGEST_LENGTH_MD4 B_DIGEST_LENGTH_128 -#define B_DIGEST_LENGTH_MD5 B_DIGEST_LENGTH_128 -#define B_DIGEST_LENGTH_SHA1 B_DIGEST_LENGTH_160 -#define B_DIGEST_LENGTH_SHA2_224 B_DIGEST_LENGTH_224 -#define B_DIGEST_LENGTH_SHA2_256 B_DIGEST_LENGTH_256 -#define B_DIGEST_LENGTH_SHA2_384 B_DIGEST_LENGTH_384 -#define B_DIGEST_LENGTH_SHA2_512 B_DIGEST_LENGTH_512 -#define B_DIGEST_LENGTH_SHA3_224 B_DIGEST_LENGTH_224 -#define B_DIGEST_LENGTH_SHA3_256 B_DIGEST_LENGTH_256 -#define B_DIGEST_LENGTH_SHA3_384 B_DIGEST_LENGTH_384 -#define B_DIGEST_LENGTH_SHA3_512 B_DIGEST_LENGTH_512 -#define B_DIGEST_LENGTH_SHAKE128 B_DIGEST_LENGTH_128 -#define B_DIGEST_LENGTH_SHAKE256 B_DIGEST_LENGTH_256 - -struct b_hash_function_ops; -struct b_rope; - -typedef enum b_hash_function { - B_HASH_NONE = 0, - B_HASH_MD4, - B_HASH_MD5, - B_HASH_SHA1, - B_HASH_SHA2_224, - B_HASH_SHA2_256, - B_HASH_SHA2_384, - B_HASH_SHA2_512, - B_HASH_SHA3_224, - B_HASH_SHA3_256, - B_HASH_SHA3_384, - B_HASH_SHA3_512, - B_HASH_SHAKE128, - B_HASH_SHAKE256, -} b_hash_function; - -typedef struct b_hash_ctx { - b_hash_function ctx_func; - const struct b_hash_function_ops *ctx_ops; - - union { - struct { - uint32_t lo, hi; - uint32_t a, b, c, d; - uint32_t block[16]; - unsigned char buffer[64]; - } md4; - - struct { - unsigned int count[2]; - unsigned int a, b, c, d; - unsigned int block[16]; - unsigned char input[64]; - } md5; - - struct { - uint32_t state[5]; - uint32_t count[2]; - unsigned char buffer[64]; - } sha1; - - struct { - uint64_t curlen; - uint64_t length; - unsigned char buf[128]; - uint32_t state[8]; - } sha2_256; - - struct { - uint64_t curlen; - uint64_t length; - unsigned char block[256]; - uint64_t state[8]; - } sha2_512; - - struct { - union { - uint8_t b[200]; - uint64_t q[25]; - } st; - - int pt, rsiz, mdlen; - } sha3; - } ctx_state; -} b_hash_ctx; - -BLUE_API uint64_t b_hash_cstr(const char *s); -BLUE_API uint64_t b_hash_cstr_ex(const char *s, size_t *len); - -BLUE_API b_status b_hash_ctx_init(b_hash_ctx *ctx, b_hash_function func); -BLUE_API b_status b_hash_ctx_reset(b_hash_ctx *ctx); -BLUE_API b_status b_hash_ctx_update(b_hash_ctx *ctx, const void *p, size_t len); -BLUE_API b_status b_hash_ctx_update_rope(b_hash_ctx *ctx, const struct b_rope *rope); -BLUE_API b_status b_hash_ctx_finish( - b_hash_ctx *ctx, void *out_digest, size_t out_max); - -#endif diff --git a/core/include/blue/core/iterator.h b/core/include/blue/core/iterator.h deleted file mode 100644 index c1653c4..0000000 --- a/core/include/blue/core/iterator.h +++ /dev/null @@ -1,93 +0,0 @@ -#ifndef BLUE_CORE_ITERATOR_H_ -#define BLUE_CORE_ITERATOR_H_ - -#include -#include -#include -#include - -B_DECLS_BEGIN; - -#define b_foreach(type, var, iterator) \ - for (type var = (type)b_iterator_get_value(iterator).v_int; \ - B_OK(b_iterator_get_status(iterator)); \ - b_iterator_move_next(iterator), \ - var = (type)b_iterator_get_value(iterator).v_int) -#define b_foreach_ptr(type, var, iterator) \ - for (type *var = (type *)b_iterator_get_value(iterator).v_ptr; \ - B_OK(b_iterator_get_status(iterator)); \ - b_iterator_move_next(iterator), \ - var = (type *)b_iterator_get_value(iterator).v_ptr) -#define b_foreach_c(type, var, iterator) \ - for (type var = (type)b_iterator_get_cvalue(iterator).v_int; \ - B_OK(b_iterator_get_status(iterator)); \ - b_iterator_move_next(iterator), \ - var = (type)b_iterator_get_cvalue(iterator).v_int) -#define b_foreach_cptr(type, var, iterator) \ - for (const type *var \ - = (const type *)b_iterator_get_cvalue(iterator).v_cptr; \ - B_OK(b_iterator_get_status(iterator)); \ - b_iterator_move_next(iterator), \ - var = (const type *)b_iterator_get_cvalue(iterator).v_cptr) - -#define B_ITERATOR_VALUE_INT(v) ((b_iterator_value) {.v_int = (v)}) -#define B_ITERATOR_VALUE_PTR(v) ((b_iterator_value) {.v_ptr = (v)}) -#define B_ITERATOR_VALUE_CPTR(v) ((const b_iterator_value) {.v_cptr = (v)}) -#define B_ITERATOR_VALUE_NULL ((b_iterator_value) {}) -#define B_ITERATOR_VALUE_IS_NULL(v) ((v)->v_ptr == NULL) - -#define B_TYPE_ITERATOR (b_iterator_get_type()) -#define B_TYPE_ITERABLE (b_iterable_get_type()) - -typedef union b_iterator_value { - uintptr_t v_int; - void *v_ptr; - const void *v_cptr; -} b_iterator_value; - -__B_DECLARE_TYPE(b_iterator); - -B_DECLARE_TYPE(b_iterable); - -B_TYPE_CLASS_DECLARATION_BEGIN(b_iterator) - b_status (*it_move_next)(const b_iterator *); - b_status (*it_erase)(b_iterator *); - b_iterator_value (*it_get_value)(b_iterator *); - const b_iterator_value (*it_get_cvalue)(const b_iterator *); -B_TYPE_CLASS_DECLARATION_END(b_iterator) - -B_TYPE_CLASS_DECLARATION_BEGIN(b_iterable) - b_iterator *(*it_begin)(b_iterable *); - const b_iterator *(*it_cbegin)(const b_iterable *); -B_TYPE_CLASS_DECLARATION_END(b_iterable) - -BLUE_API b_type b_iterator_get_type(void); -BLUE_API b_type b_iterable_get_type(void); - -static inline const b_iterator *b_iterator_ref(const b_iterator *p) -{ - return b_object_ref((b_object *)p); -} -static inline void b_iterator_unref(const b_iterator *p) -{ - b_object_unref((b_object *)p); -} - -BLUE_API b_iterator *b_iterator_begin(b_iterable *it); -BLUE_API const b_iterator *b_iterator_cbegin(const b_iterable *it); - -BLUE_API b_status b_iterator_get_status(const b_iterator *it); -BLUE_API b_status b_iterator_set_status(const b_iterator *it, b_status status); - -BLUE_API b_status b_iterator_move_next(const b_iterator *it); -BLUE_API b_iterator_value b_iterator_get_value(b_iterator *it); -BLUE_API const b_iterator_value b_iterator_get_cvalue(const b_iterator *it); -BLUE_API b_status b_iterator_erase(b_iterator *it); -static inline bool b_iterator_is_valid(const b_iterator *it) -{ - return B_OK(b_iterator_get_status(it)); -} - -B_DECLS_END; - -#endif diff --git a/core/include/blue/core/macros.h b/core/include/blue/core/macros.h deleted file mode 100644 index 5f426d0..0000000 --- a/core/include/blue/core/macros.h +++ /dev/null @@ -1,197 +0,0 @@ -#ifndef BLUE_CORE_MACROS_H_ -#define BLUE_CORE_MACROS_H_ - -#include -#include -#include -#include -#include - -#define __B_IFACE_I0(p, x) p##x -#define __B_IFACE_I1(p, x) __B_IFACE_I0(p, x) - -/* Type definitions macros (for use in .c source file) */ - -#define B_TYPE_CLASS_DEFINITION_BEGIN(type_name) \ - static void type_name##_class_init(b_class *p, void *d) \ - { -#define B_TYPE_CLASS_DEFINITION_END(type_name) } - -#define B_TYPE_CLASS_INTERFACE_BEGIN(interface_name, interface_id) \ - interface_name##_class *__B_IFACE_I1(iface, __LINE__) \ - = b_class_get_interface(p, interface_id); \ - if (!__B_IFACE_I1(iface, __LINE__)) { \ - b_throw_error_with_msg_template( \ - B_ERRORS_BUILTIN, B_ERR_CLASS_INIT_FAILURE, \ - B_MSG_CLASS_SPECIFIES_UNKNOWN_INTERFACE, \ - B_ERROR_PARAM("class_name", b_class_get_name(p)), \ - B_ERROR_PARAM("interface_name", #interface_name)); \ - exit(-1); \ - } else { \ - interface_name##_class *iface = __B_IFACE_I1(iface, __LINE__); -#define B_TYPE_CLASS_INTERFACE_END(interface_name, interface_id) } -#define B_INTERFACE_ENTRY(slot) iface->slot - -#define B_TYPE_DEFINITION_BEGIN(name) \ - static b_type_info name##_type_info = {0}; \ - static void name##_class_init(b_class *, void *); \ - static void name##_type_init(void) \ - { \ - b_type_info *type_info = &name##_type_info; \ - unsigned int nr_vtables = 0; \ - type_info->t_name = #name; \ - type_info->t_class_init = name##_class_init; -#define B_TYPE_DEFINITION_END(name) \ - b_result result = b_type_register(type_info); \ - if (b_result_is_error(result)) { \ - b_throw_error_caused_by_error( \ - B_ERRORS_BUILTIN, B_ERR_TYPE_REGISTRATION_FAILURE, \ - result); \ - abort(); \ - } \ - } \ - b_type name##_get_type(void) \ - { \ - static b_once static_type_init = B_ONCE_INIT; \ - \ - if (b_init_once(&static_type_init)) { \ - name##_type_init(); \ - } \ - \ - return &name##_type_info.t_id; \ - } - -#define B_TYPE_ID(a, b, c, d, e) b_type_id_init(&type_info->t_id, a, b, c, d, e) -#define B_TYPE_EXTENDS(parent_id) \ - b_type_id_copy(parent_id, &type_info->t_parent_id) -#define B_TYPE_IMPLEMENTS(interface_id) \ - b_type_id_copy( \ - interface_id, \ - &type_info->t_interfaces[type_info->t_nr_interfaces++]) -#define B_TYPE_CLASS(class_struct) \ - type_info->t_class_size = sizeof(class_struct) -#define B_TYPE_FLAGS(flags) type_info->t_flags = (flags) -#define B_TYPE_INSTANCE_INIT(func) type_info->t_instance_init = (func) -#define B_TYPE_INSTANCE_FINI(func) type_info->t_instance_fini = (func) - -#if 0 -#define B_TYPE_VTABLE_BEGIN(vtable_struct, interface_id) \ - vtable_struct __B_IFACE_I1(iface, __LINE__) = {0}; \ - { \ - vtable_struct *iface = &__B_IFACE_I1(iface, __LINE__); \ - type_info->t_vtables[nr_vtables].v_vtable = iface; \ - type_info->t_vtables[nr_vtables].v_interface_id = interface_id; \ - nr_vtables++; -#define B_TYPE_VTABLE_END(vtable_struct, interface_id) } -#endif - -#define B_TYPE_INSTANCE_PRIVATE(instance_struct) \ - type_info->t_instance_private_size = sizeof(instance_struct) -#define B_TYPE_INSTANCE_PROTECTED(instance_struct) \ - type_info->t_instance_protected_size = sizeof(instance_struct) - -/* Type declaration macros (for use in .h header file) */ - -#define __B_DECLARE_TYPE(name) \ - typedef B_TYPE_FWDREF(name) name; \ - typedef struct _##name##_class name##_class; - -#define B_DECLARE_TYPE(name) \ - __B_DECLARE_TYPE(name); \ - static inline name *name##_ref(name *p) \ - { \ - return b_object_ref(p); \ - } \ - static inline void name##_unref(name *p) \ - { \ - b_object_unref(p); \ - } - -#define B_TYPE_CLASS_DECLARATION_BEGIN(name) struct _##name##_class { -#define B_TYPE_CLASS_DECLARATION_END(name) \ - } \ - ; - -#define B_TYPE_VIRTUAL_METHOD(return_type, method_name) \ - return_type(*method_name) - -#define B_TYPE_DEFAULT_CONSTRUCTOR(type_name, type_id) \ - static inline type_name *type_name##_create(void) \ - { \ - return b_object_create(type_id); \ - } - -/* Other macros */ - -#define B_CLASS_DISPATCH_VIRTUAL( \ - type_name, type_id, default_value, func, object, ...) \ - do { \ - type_name##_class *iface \ - = b_object_get_interface(object, type_id); \ - if (iface && iface->func) { \ - return iface->func(object, __VA_ARGS__); \ - } else { \ - return default_value; \ - } \ - } while (0) -#define B_CLASS_DISPATCH_VIRTUAL_0(type_name, type_id, default_value, func, object) \ - do { \ - type_name##_class *iface \ - = b_object_get_interface(object, type_id); \ - if (iface && iface->func) { \ - return iface->func(object); \ - } else { \ - return default_value; \ - } \ - } while (0) -#define B_CLASS_DISPATCH_VIRTUAL_V(type_name, type_id, func, object, ...) \ - do { \ - type_name##_class *iface \ - = b_object_get_interface(object, type_id); \ - if (iface && iface->func) { \ - iface->func(object, __VA_ARGS__); \ - return; \ - } \ - } while (0) -#define B_CLASS_DISPATCH_VIRTUAL_V0(type_name, type_id, func, object) \ - do { \ - type_name##_class *iface \ - = b_object_get_interface(object, type_id); \ - if (iface && iface->func) { \ - iface->func(object); \ - return; \ - } \ - } while (0) - -#define B_CLASS_DISPATCH_STATIC(type_id, func_name, obj, ...) \ - do { \ - void *priv = b_object_get_private(obj, type_id); \ - return func_name(priv, __VA_ARGS__); \ - } while (0) -#define B_CLASS_DISPATCH_STATIC_V(type_id, func_name, obj, ...) \ - do { \ - void *priv = b_object_get_private(obj, type_id); \ - func_name(priv, __VA_ARGS__); \ - } while (0) - -#define B_CLASS_DISPATCH_STATIC_0(type_id, func_name, obj) \ - do { \ - void *priv = b_object_get_private(obj, type_id); \ - return func_name(priv); \ - } while (0) - -#define B_CLASS_DISPATCH_STATIC_V0(type_id, func_name, obj) \ - do { \ - void *priv = b_object_get_private(obj, type_id); \ - func_name(priv); \ - } while (0) - -#ifdef __cplusplus -#define B_DECLS_BEGIN extern "C" { -#define B_DECLS_END } -#else -#define B_DECLS_BEGIN -#define B_DECLS_END -#endif - -#endif diff --git a/core/include/blue/core/misc.h b/core/include/blue/core/misc.h deleted file mode 100644 index 3e06ddf..0000000 --- a/core/include/blue/core/misc.h +++ /dev/null @@ -1,118 +0,0 @@ -#ifndef BLUE_CORE_MISC_H_ -#define BLUE_CORE_MISC_H_ - -#include -#include - -#ifndef _Nonnull -#define _Nonnull -#endif - -#define B_NPOS ((size_t)-1) - -#define b_min(type, x, y) (z__b_min_##type(x, y)) -#define b_max(type, x, y) (z__b_max_##type(x, y)) - -#define b_unbox(type, box, member) \ - ((type *_Nonnull)((box) ? (uintptr_t)(box) - (offsetof(type, member)) : 0)) - -#define z__b_merge_(a, b) a##b -#define z__b_label_(a) z__b_merge_(__unique_name_, a) -#define z__b_unique_name() z__b_label_(__LINE__) -#define z__b_numargs(arg_type, ...) \ - (sizeof((arg_type[]) {__VA_ARGS__}) / sizeof(arg_type)) - -#ifdef _MSC_VER -#ifdef BLUELIB_STATIC -#define BLUE_API extern -#else -#ifdef BLUELIB_EXPORT -#define BLUE_API extern __declspec(dllexport) -#else -#define BLUE_API extern __declspec(dllimport) -#endif -#endif -#else -#define BLUE_API extern -#endif - -static inline char z__b_min_char(char x, char y) -{ - return x < y ? x : y; -} -static inline unsigned char z__b_min_uchar(unsigned char x, unsigned char y) -{ - return x < y ? x : y; -} -static inline int z__b_min_int(int x, int y) -{ - return x < y ? x : y; -} -static inline unsigned int z__b_min_uint(unsigned int x, unsigned int y) -{ - return x < y ? x : y; -} -static inline long z__b_min_long(long x, long y) -{ - return x < y ? x : y; -} -static inline unsigned int z__b_min_ulong(unsigned long x, unsigned long y) -{ - return x < y ? x : y; -} -static inline long long z__b_min_longlong(long long x, long long y) -{ - return x < y ? x : y; -} -static inline unsigned long long z__b_min_ulonglong( - unsigned long long x, unsigned long long y) -{ - return x < y ? x : y; -} -static inline size_t z__b_min_size_t(size_t x, size_t y) -{ - return x < y ? x : y; -} - -static inline char z__b_max_char(char x, char y) -{ - return x > y ? x : y; -} -static inline unsigned char z__b_max_uchar(unsigned char x, unsigned char y) -{ - return x > y ? x : y; -} -static inline int z__b_max_int(int x, int y) -{ - return x > y ? x : y; -} -static inline unsigned int z__b_max_uint(unsigned int x, unsigned int y) -{ - return x > y ? x : y; -} -static inline long z__b_max_long(long x, long y) -{ - return x > y ? x : y; -} -static inline unsigned int z__b_max_ulong(unsigned long x, unsigned long y) -{ - return x > y ? x : y; -} -static inline long long z__b_max_longlong(long long x, long long y) -{ - return x > y ? x : y; -} -static inline unsigned long long z__b_max_ulonglong( - unsigned long long x, unsigned long long y) -{ - return x > y ? x : y; -} -static inline size_t z__b_max_size_t(size_t x, size_t y) -{ - return x > y ? x : y; -} - -BLUE_API size_t b_int_length(intptr_t v); -BLUE_API size_t b_uint_length(uintptr_t v); - -#endif // BLUE_CORE_MISC_H_ diff --git a/core/include/blue/core/object.h b/core/include/blue/core/object.h deleted file mode 100644 index 3063420..0000000 --- a/core/include/blue/core/object.h +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef BLUE_CORE_OBJECT_H_ -#define BLUE_CORE_OBJECT_H_ - -#include -#include - -#define B_OBJECT_MAGIC 0xDECAFC0C0ABEEF13ULL - -#define B_OBJECT(p) ((b_object *)(p)) -#define B_TYPE_OBJECT (b_object_get_type()) - -#define B_TYPE_FWDREF(name) struct _b_object - -#define B_RV(p) (b_object_make_rvalue(p)) - -typedef B_TYPE_FWDREF(b_object) b_object; - -typedef struct _b_object_class { - void (*to_string)(const b_object *, B_TYPE_FWDREF(b_stream) *); -} b_object_class; - -BLUE_API b_type b_object_get_type(void); - -BLUE_API void *b_object_get_private(const b_object *object, b_type type); -BLUE_API void *b_object_get_protected(const b_object *object, b_type type); -BLUE_API void *b_object_get_interface(const b_object *object, b_type type); -BLUE_API b_status b_object_get_data( - const b_object *object, b_type type, void **priv, void **prot, - void **iface); - -BLUE_API b_object *b_object_ref(b_object *p); -BLUE_API void b_object_unref(b_object *p); -BLUE_API b_object *b_object_make_rvalue(b_object *p); - -BLUE_API b_object *b_object_create(b_type type); -BLUE_API void b_object_to_string(const b_object *p, B_TYPE_FWDREF(b_stream) * out); -BLUE_API bool b_object_is_type(const b_object *p, b_type type); - -#endif diff --git a/core/include/blue/core/queue.h b/core/include/blue/core/queue.h deleted file mode 100644 index 253d37a..0000000 --- a/core/include/blue/core/queue.h +++ /dev/null @@ -1,82 +0,0 @@ -#ifndef BLUE_CORE_QUEUE_H_ -#define BLUE_CORE_QUEUE_H_ - -#include -#include -#include -#include -#include - -B_DECLS_BEGIN; - -#define B_TYPE_QUEUE_ITERATOR (b_queue_iterator_get_type()) - -B_DECLARE_TYPE(b_queue_iterator); - -B_TYPE_CLASS_DECLARATION_BEGIN(b_queue_iterator) -B_TYPE_CLASS_DECLARATION_END(b_queue_iterator) - -#define B_QUEUE_INIT ((b_queue) {.q_first = NULL, .q_last = NULL}) -#define B_QUEUE_ENTRY_INIT ((b_queue_entry) {.qe_next = NULL, .qe_prev = NULL}) - -typedef struct b_queue_entry { - struct b_queue_entry *qe_next; - struct b_queue_entry *qe_prev; -} b_queue_entry; - -typedef struct b_queue { - b_queue_entry *q_first; - b_queue_entry *q_last; -} b_queue; - -static inline void b_queue_init(b_queue *q) -{ - memset(q, 0x00, sizeof *q); -} -static inline bool b_queue_empty(const b_queue *q) -{ - return q ? (q->q_first == NULL) : true; -} - -static inline b_queue_entry *b_queue_first(const b_queue *q) -{ - return q ? q->q_first : NULL; -} -static inline b_queue_entry *b_queue_last(const b_queue *q) -{ - return q ? q->q_last : NULL; -} -static inline b_queue_entry *b_queue_next(const b_queue_entry *entry) -{ - return entry ? entry->qe_next : NULL; -} -static inline b_queue_entry *b_queue_prev(const b_queue_entry *entry) -{ - return entry ? entry->qe_prev : NULL; -} - -BLUE_API b_type b_queue_iterator_get_type(void); - -BLUE_API size_t b_queue_length(const b_queue *q); - -BLUE_API void b_queue_insert_before( - b_queue *q, b_queue_entry *entry, b_queue_entry *before); -BLUE_API void b_queue_insert_after( - b_queue *q, b_queue_entry *entry, b_queue_entry *after); - -BLUE_API void b_queue_push_front(b_queue *q, b_queue_entry *entry); -BLUE_API void b_queue_push_back(b_queue *q, b_queue_entry *entry); - -BLUE_API b_queue_entry *b_queue_pop_front(b_queue *q); -BLUE_API b_queue_entry *b_queue_pop_back(b_queue *q); - -BLUE_API void b_queue_move(b_queue *q, b_queue_entry *dest, b_queue_entry *src); -BLUE_API void b_queue_delete(b_queue *q, b_queue_entry *entry); -BLUE_API void b_queue_delete_all(b_queue *q); - -BLUE_API b_iterator *b_queue_begin(b_queue *q); -BLUE_API b_iterator *b_queue_cbegin(const b_queue *q); - -B_DECLS_END; - -#endif diff --git a/core/include/blue/core/random.h b/core/include/blue/core/random.h deleted file mode 100644 index 6c2693d..0000000 --- a/core/include/blue/core/random.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef BLUELIB_RANDOM_H_ -#define BLUELIB_RANDOM_H_ - -#include -#include - -struct b_random_algorithm; - -typedef enum b_random_flags { - /* algorithm selection */ - B_RANDOM_MT19937 = 0x01u, - - /* generation flags */ - B_RANDOM_SECURE = 0x100u, -} b_random_flags; - -typedef struct b_random_ctx { - b_random_flags __f; - struct b_random_algorithm *__a; - - union { - struct { - unsigned long long mt[312]; - size_t mti; - } __mt19937; - }; -} b_random_ctx; - -BLUE_API b_random_ctx *b_random_global_ctx(void); - -BLUE_API b_status b_random_init(b_random_ctx *ctx, b_random_flags flags); -BLUE_API unsigned long long b_random_next_int64(b_random_ctx *ctx); -BLUE_API double b_random_next_double(b_random_ctx *ctx); -BLUE_API void b_random_next_bytes( - b_random_ctx *ctx, unsigned char *out, size_t nbytes); - -#endif diff --git a/core/include/blue/core/ringbuffer.h b/core/include/blue/core/ringbuffer.h deleted file mode 100644 index ff6687b..0000000 --- a/core/include/blue/core/ringbuffer.h +++ /dev/null @@ -1,47 +0,0 @@ -#ifndef BLUE_CORE_RINGBUFFER_H_ -#define BLUE_CORE_RINGBUFFER_H_ - -#include -#include -#include - -B_DECLS_BEGIN; - -#define B_TYPE_RINGBUFFER (b_ringbuffer_get_type()) - -B_DECLARE_TYPE(b_ringbuffer); - -B_TYPE_CLASS_DECLARATION_BEGIN(b_ringbuffer) -B_TYPE_CLASS_DECLARATION_END(b_ringbuffer) - -BLUE_API b_type b_ringbuffer_get_type(void); - -BLUE_API b_ringbuffer *b_ringbuffer_create(size_t capacity); -BLUE_API b_ringbuffer *b_ringbuffer_create_with_buffer(void *ptr, size_t capacity); - -BLUE_API b_status b_ringbuffer_clear(b_ringbuffer *buf); - -BLUE_API b_status b_ringbuffer_read( - b_ringbuffer *buf, void *p, size_t count, size_t *nr_read); -BLUE_API b_status b_ringbuffer_write( - b_ringbuffer *buf, const void *p, size_t count, size_t *nr_written); - -BLUE_API int b_ringbuffer_getc(b_ringbuffer *buf); -BLUE_API b_status b_ringbuffer_putc(b_ringbuffer *buf, int c); - -BLUE_API size_t b_ringbuffer_write_capacity_remaining(const b_ringbuffer *buf); -BLUE_API size_t b_ringbuffer_available_data_remaining(const b_ringbuffer *buf); - -BLUE_API b_status b_ringbuffer_open_read_buffer( - b_ringbuffer *buf, const void **ptr, size_t *length); -BLUE_API b_status b_ringbuffer_close_read_buffer( - b_ringbuffer *buf, const void **ptr, size_t nr_read); - -BLUE_API b_status b_ringbuffer_open_write_buffer( - b_ringbuffer *buf, void **ptr, size_t *capacity); -BLUE_API b_status b_ringbuffer_close_write_buffer( - b_ringbuffer *buf, void **ptr, size_t nr_written); - -B_DECLS_END; - -#endif diff --git a/core/include/blue/core/rope.h b/core/include/blue/core/rope.h deleted file mode 100644 index f6ed7ae..0000000 --- a/core/include/blue/core/rope.h +++ /dev/null @@ -1,109 +0,0 @@ -#ifndef BLUE_CORE_ROPE_H_ -#define BLUE_CORE_ROPE_H_ - -#include -#include -#include -#include -#include - -struct b_string; -struct b_bstr; - -#define B_ROPE_TYPE(f) ((f) & 0xFF) - -#define B_ROPE_CHAR(c) \ - \ - { \ - .r_flags = B_ROPE_F_CHAR, .r_len_total = 1, \ - .r_v = {.v_char = (c) } \ - } - -#define B_ROPE_CSTR(str) \ - { \ - .r_flags = B_ROPE_F_CSTR_BORROWED, \ - .r_len_total = strlen(str), \ - .r_v = { \ - .v_cstr = { \ - .s = (str), \ - .hash = b_hash_cstr(str), \ - }, \ - }, \ - } - -#define B_ROPE_CSTR_STATIC(str) \ - { \ - .r_flags = B_ROPE_F_CSTR_STATIC, \ - .r_len_total = strlen(str), \ - .r_v = { \ - .v_cstr = { \ - .s = (str), \ - .hash = b_hash_cstr(str), \ - }, \ - }, \ - } -#define B_ROPE_INT(v) \ - \ - { \ - .r_flags = B_ROPE_F_INT, .r_len_total = b_int_length(v), \ - .r_v = {.v_int = (v) } \ - } -#define B_ROPE_UINT(v) \ - \ - { \ - .r_flags = B_ROPE_F_UINT, .r_len_total = b_uint_length(v), \ - .r_v = {.v_uint = (v) } \ - } - -typedef enum b_rope_flags { - B_ROPE_F_NONE = 0x0000u, - B_ROPE_F_CHAR = 0x0001u, - B_ROPE_F_CSTR = 0x0002u, - B_ROPE_F_CSTR_BORROWED = 0x0003u, - B_ROPE_F_CSTR_STATIC = 0x0004u, - B_ROPE_F_INT = 0x0005u, - B_ROPE_F_UINT = 0x0006u, - B_ROPE_F_COMPOSITE = 0x0007u, - B_ROPE_F_MALLOC = 0x0100u, -} b_rope_flags; - -typedef struct b_rope { - b_rope_flags r_flags; - unsigned long r_len_left, r_len_total; - - union { - char v_char; - intptr_t v_int; - uintptr_t v_uint; - - struct { - const char *s; - uint64_t hash; - } v_cstr; - - struct { - const struct b_rope *r_left, *r_right; - } v_composite; - } r_v; -} b_rope; - -BLUE_API void b_rope_init_char(b_rope *rope, char c); -BLUE_API void b_rope_init_cstr(b_rope *rope, const char *s); -BLUE_API void b_rope_init_cstr_borrowed(b_rope *rope, const char *s); -BLUE_API void b_rope_init_cstr_static(b_rope *rope, const char *s); -BLUE_API void b_rope_init_int(b_rope *rope, intptr_t v); -BLUE_API void b_rope_init_uint(b_rope *rope, uintptr_t v); - -BLUE_API void b_rope_destroy(b_rope *rope); - -BLUE_API void b_rope_iterate( - const b_rope *rope, void (*func)(const b_rope *, void *), void *arg); -BLUE_API size_t b_rope_get_size(const b_rope *rope); -BLUE_API void b_rope_concat(b_rope *result, const b_rope *left, const b_rope *right); -BLUE_API void b_rope_join(b_rope *result, const b_rope **ropes, size_t nr_ropes); - -BLUE_API b_status b_rope_to_cstr(const b_rope *rope, char *out, size_t max); -BLUE_API b_status b_rope_to_bstr(const b_rope *rope, struct b_bstr *str); -BLUE_API b_status b_rope_to_string(const b_rope *rope, b_stream *out); - -#endif diff --git a/core/include/blue/core/status.h b/core/include/blue/core/status.h deleted file mode 100644 index fc1175f..0000000 --- a/core/include/blue/core/status.h +++ /dev/null @@ -1,48 +0,0 @@ -#ifndef BLUELIB_CORE_STATUS_H_ -#define BLUELIB_CORE_STATUS_H_ - -#include - -#define B_OK(status) ((enum b_status)((uintptr_t)(status)) == B_SUCCESS) -#define B_ERR(status) ((status) != B_SUCCESS) - -typedef enum b_status { - B_SUCCESS = 0x00u, - B_ERR_NO_MEMORY, - B_ERR_OUT_OF_BOUNDS, - B_ERR_INVALID_ARGUMENT, - B_ERR_NAME_EXISTS, - B_ERR_NOT_SUPPORTED, - B_ERR_BAD_STATE, - B_ERR_NO_ENTRY, - B_ERR_NO_DATA, - B_ERR_NO_SPACE, - B_ERR_UNKNOWN_FUNCTION, - B_ERR_BAD_FORMAT, - B_ERR_IO_FAILURE, - B_ERR_IS_DIRECTORY, - B_ERR_NOT_DIRECTORY, - B_ERR_PERMISSION_DENIED, - B_ERR_BUSY, - - /* blue-compress specific code */ - B_ERR_COMPRESSION_FAILURE, - - /* blue-object specific code */ - B_ERR_TYPE_REGISTRATION_FAILURE, - B_ERR_CLASS_INIT_FAILURE, -} b_status; - -typedef enum b_status_msg { - B_MSG_SUCCESS = 0, - - /* blue-object specific messages */ - B_MSG_TYPE_REGISTRATION_FAILURE, - B_MSG_CLASS_INIT_FAILURE, - B_MSG_CLASS_SPECIFIES_UNKNOWN_INTERFACE, -} b_status_msg; - -BLUE_API const char *b_status_to_string(b_status status); -BLUE_API const char *b_status_description(b_status status); - -#endif diff --git a/core/include/blue/core/stream.h b/core/include/blue/core/stream.h deleted file mode 100644 index a77f80b..0000000 --- a/core/include/blue/core/stream.h +++ /dev/null @@ -1,100 +0,0 @@ -#ifndef BLUE_CORE_STREAM_H_ -#define BLUE_CORE_STREAM_H_ - -#include -#include -#include -#include -#include -#include - -B_DECLS_BEGIN; - -#define b_stdin (z__b_stream_get_stdin()) -#define b_stdout (z__b_stream_get_stdout()) -#define b_stderr (z__b_stream_get_stderr()) - -#define B_TYPE_STREAM (b_stream_get_type()) -#define B_TYPE_STREAM_BUFFER (b_stream_buffer_get_type()) - -B_DECLARE_TYPE(b_stream); -B_DECLARE_TYPE(b_stream_buffer); - -typedef enum b_stream_mode { - B_STREAM_READ = 0x01u, - B_STREAM_WRITE = 0x02u, - B_STREAM_BINARY = 0x10u, - Z__B_STREAM_STATIC = 0x80u, -} b_stream_mode; - -typedef enum b_stream_seek_origin { - B_STREAM_SEEK_START = 0x01u, - B_STREAM_SEEK_CURRENT = 0x02u, - B_STREAM_SEEK_END = 0x03u, -} b_stream_seek_origin; - -typedef struct b_stream_cfg { - b_stream_mode s_mode; -} b_stream_cfg; - -B_TYPE_CLASS_DECLARATION_BEGIN(b_stream) - b_status (*s_close)(b_stream *); - b_status (*s_seek)(b_stream *, long long, b_stream_seek_origin); - b_status (*s_tell)(const b_stream *, size_t *); - b_status (*s_getc)(b_stream *, b_wchar *); - b_status (*s_read)(b_stream *, void *, size_t, size_t *); - b_status (*s_write)(b_stream *, const void *, size_t, size_t *); - b_status (*s_reserve)(b_stream *, size_t); -B_TYPE_CLASS_DECLARATION_END(b_stream) - -B_TYPE_CLASS_DECLARATION_BEGIN(b_stream_buffer) -B_TYPE_CLASS_DECLARATION_END(b_stream_buffer) - -BLUE_API b_type b_stream_get_type(); -BLUE_API b_type b_stream_buffer_get_type(); - -BLUE_API b_stream *z__b_stream_get_stdin(void); -BLUE_API b_stream *z__b_stream_get_stdout(void); -BLUE_API b_stream *z__b_stream_get_stderr(void); - -BLUE_API b_stream_buffer *b_stream_buffer_create(void *p, size_t len); -BLUE_API b_stream_buffer *b_stream_buffer_create_dynamic(size_t buffer_size); - -BLUE_API b_stream *b_stream_open_fp(FILE *fp); - -BLUE_API b_status b_stream_reserve(b_stream *stream, size_t len); -BLUE_API b_status b_stream_seek( - b_stream *stream, long long offset, b_stream_seek_origin origin); -BLUE_API size_t b_stream_cursor(const b_stream *stream); - -BLUE_API b_status b_stream_push_indent(b_stream *stream, int indent); -BLUE_API b_status b_stream_pop_indent(b_stream *stream); - -BLUE_API b_status b_stream_read_char(b_stream *stream, b_wchar *c); - -BLUE_API b_status b_stream_read_bytes( - b_stream *stream, void *buf, size_t count, size_t *nr_read); - -BLUE_API b_status b_stream_read_line(b_stream *stream, char *s, size_t max); -BLUE_API b_status b_stream_read_line_s(b_stream *src, b_stream *dest); - -BLUE_API b_status b_stream_read_all_bytes( - b_stream *stream, void *p, size_t max, size_t *nr_read); -BLUE_API b_status b_stream_read_all_bytes_s( - b_stream *src, b_stream *dest, b_stream_buffer *buffer, size_t *nr_read); - -BLUE_API b_status b_stream_write_char(b_stream *stream, b_wchar c); -BLUE_API b_status b_stream_write_string( - b_stream *stream, const char *s, size_t *nr_written); - -BLUE_API b_status b_stream_write_bytes( - b_stream *stream, const void *buf, size_t count, size_t *nr_written); - -BLUE_API b_status b_stream_write_fmt( - b_stream *stream, size_t *nr_written, const char *format, ...); -BLUE_API b_status b_stream_write_vfmt( - b_stream *stream, size_t *nr_written, const char *format, va_list arg); - -B_DECLS_END; - -#endif diff --git a/core/include/blue/core/stringstream.h b/core/include/blue/core/stringstream.h deleted file mode 100644 index 18e9b7e..0000000 --- a/core/include/blue/core/stringstream.h +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef BLUE_CORE_STRINGSTREAM_H_ -#define BLUE_CORE_STRINGSTREAM_H_ - -#include -#include -#include -#include -#include - -B_DECLS_BEGIN; - -#define B_TYPE_STRINGSTREAM (b_stringstream_get_type()) - -B_DECLARE_TYPE(b_stringstream); - -B_TYPE_CLASS_DECLARATION_BEGIN(b_stringstream) -B_TYPE_CLASS_DECLARATION_END(b_stringstream) - -BLUE_API b_type b_stringstream_get_type(void); - -BLUE_API b_stringstream *b_stringstream_create(void); -BLUE_API b_stringstream *b_stringstream_create_with_buffer(char *buf, size_t max); - -BLUE_API b_status b_stringstream_reset(b_stringstream *strv); -BLUE_API b_status b_stringstream_reset_with_buffer( - b_stringstream *strv, char *buf, size_t max); - -BLUE_API const char *b_stringstream_ptr(const b_stringstream *strv); -BLUE_API char *b_stringstream_steal(b_stringstream *strv); - -BLUE_API size_t b_stringstream_get_length(const b_stringstream *strv); - -B_DECLS_END; - -#endif diff --git a/core/include/blue/core/thread.h b/core/include/blue/core/thread.h deleted file mode 100644 index 6efa9b5..0000000 --- a/core/include/blue/core/thread.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef BLUELIB_CORE_THREAD_H_ -#define BLUELIB_CORE_THREAD_H_ - -#include -#include -#include - -#if defined(__APPLE__) || defined(__linux__) -#include - -#define B_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER - -typedef pthread_mutex_t b_mutex; -#else -#error Unsupported compiler/system -#endif - -#define B_ONCE_INIT ((b_once)0) - -typedef struct b_thread b_thread; - -typedef int b_once; - -static inline bool b_init_once(b_once *once) -{ - int x = 0; - return b_cmpxchg(once, &x, 1); -} - -BLUE_API b_thread *b_thread_self(void); - -BLUE_API bool b_mutex_lock(b_mutex *mut); -BLUE_API bool b_mutex_trylock(b_mutex *mut); -BLUE_API bool b_mutex_unlock(b_mutex *mut); - -#endif diff --git a/core/include/blue/core/type.h b/core/include/blue/core/type.h deleted file mode 100644 index b486b9e..0000000 --- a/core/include/blue/core/type.h +++ /dev/null @@ -1,66 +0,0 @@ -#ifndef BLUE_CORE_TYPE_H_ -#define BLUE_CORE_TYPE_H_ - -#include -#include -#include -#include -#include - -#define B_TYPE_MAX_INTERFACES 64 - -struct _b_class; -struct _b_object; - -typedef void (*b_class_init_function)(struct _b_class *, void *); -typedef void (*b_instance_init_function)(struct _b_object *, void *); -typedef void (*b_instance_fini_function)(struct _b_object *, void *); - -typedef const union b_type { - struct { - uint64_t p00, p01; - } a; - - unsigned char b[16]; -} *b_type; - -typedef enum b_type_flags { - B_TYPE_F_ABSTRACT = 0x01u, -} b_type_flags; - -typedef struct b_type_info { - union b_type t_id; - union b_type t_parent_id; - const char *t_name; - b_type_flags t_flags; - union b_type t_interfaces[B_TYPE_MAX_INTERFACES]; - size_t t_nr_interfaces; - size_t t_class_size; - b_class_init_function t_class_init; - size_t t_instance_private_size; - size_t t_instance_protected_size; - b_instance_init_function t_instance_init; - b_instance_fini_function t_instance_fini; -} b_type_info; - -BLUE_API void b_type_id_init( - union b_type *out, uint32_t a, uint16_t b, uint16_t c, uint16_t d, - uint64_t e); -static inline void b_type_id_copy(b_type src, union b_type *dest) -{ - dest->a.p00 = src->a.p00; - dest->a.p01 = src->a.p01; -} - -static inline int b_type_id_compare(b_type a, b_type b) -{ - if (a == b) { - return 0; - } - - return memcmp(a, b, sizeof(union b_type)); -} - -BLUE_API b_result b_type_register(b_type_info *info); - -#endif diff --git a/core/include/blue/core.h b/core/include/fx/core.h similarity index 100% rename from core/include/blue/core.h rename to core/include/fx/core.h diff --git a/core/include/blue/core/bitop.h b/core/include/fx/core/bitop.h similarity index 55% rename from core/include/blue/core/bitop.h rename to core/include/fx/core/bitop.h index 079c4a3..c4b6ad9 100644 --- a/core/include/blue/core/bitop.h +++ b/core/include/fx/core/bitop.h @@ -1,14 +1,14 @@ -#ifndef BLUELIB_CORE_BITOP_H_ -#define BLUELIB_CORE_BITOP_H_ +#ifndef FX_CORE_BITOP_H_ +#define FX_CORE_BITOP_H_ -#include +#include -BLUE_API int b_popcountl(long v); -BLUE_API int b_ctzl(long v); -BLUE_API int b_clzl(long v); +FX_API int fx_popcountl(long v); +FX_API int fx_ctzl(long v); +FX_API int fx_clzl(long v); #if defined(__GNUC__) || defined(__clang__) -#define b_cmpxchg(v, expected_val, new_val) \ +#define fx_cmpxchg(v, expected_val, new_val) \ __atomic_compare_exchange_n( \ v, expected_val, new_val, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED) #elif defined(_MSC_VER) diff --git a/core/include/fx/core/bstr.h b/core/include/fx/core/bstr.h new file mode 100644 index 0000000..30a6faa --- /dev/null +++ b/core/include/fx/core/bstr.h @@ -0,0 +1,71 @@ +#ifndef FX_CORE_BSTR_H_ +#define FX_CORE_BSTR_H_ + +#include +#include +#include +#include + +#define FX_BSTR_MAGIC 0x5005500550055005ULL + +struct fx_rope; + +enum fx_bstr_flags { + FX_BSTR_F_NONE = 0x00u, + FX_BSTR_F_ALLOC = 0x01u, +}; + +typedef struct fx_bstr { + uint64_t bstr_magic; + enum fx_bstr_flags bstr_flags; + char *bstr_buf; + /* total number of characters in bstr_buf, not including null terminator */ + size_t bstr_len; + /* number of bytes allocated for bstr_buf (includes space for the null + * terminator) */ + size_t bstr_capacity; + int *bstr_istack; + int bstr_add_indent; + size_t bstr_istack_ptr, bstr_istack_size; +} fx_bstr; + +FX_API void fx_bstr_begin(fx_bstr *strv, char *buf, size_t max); +FX_API void fx_bstr_begin_dynamic(fx_bstr *strv); +FX_API char *fx_bstr_end(fx_bstr *strv); +FX_API fx_status fx_bstr_reserve(fx_bstr *strv, size_t len); + +static inline size_t fx_bstr_get_size(const fx_bstr *str) +{ + return str->bstr_len; +} + +static inline size_t fx_bstr_get_capacity(const fx_bstr *str) +{ + return str->bstr_capacity; +} + +FX_API fx_status fx_bstr_push_indent(fx_bstr *strv, int indent); +FX_API fx_status fx_bstr_pop_indent(fx_bstr *strv); + +FX_API fx_status fx_bstr_write_char(fx_bstr *strv, char c); +FX_API fx_status fx_bstr_write_chars( + fx_bstr *strv, const char *cs, size_t len, size_t *nr_written); +FX_API fx_status fx_bstr_write_cstr( + fx_bstr *strv, const char *str, size_t *nr_written); +FX_API fx_status fx_bstr_write_cstr_list( + fx_bstr *strv, const char **strs, size_t *nr_written); +FX_API fx_status fx_bstr_write_cstr_array( + fx_bstr *strv, const char **strs, size_t count, size_t *nr_written); +FX_API fx_status fx_bstr_write_cstr_varg(fx_bstr *strv, size_t *nr_written, ...); +FX_API fx_status fx_bstr_write_rope( + fx_bstr *strv, const struct fx_rope *rope, size_t *nr_written); +FX_API fx_status fx_bstr_write_fmt( + fx_bstr *strv, size_t *nr_written, const char *format, ...); +FX_API fx_status fx_bstr_write_vfmt( + fx_bstr *strv, size_t *nr_written, const char *format, va_list arg); + +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_vfmt(size_t *nr_written, const char *format, va_list arg); + +#endif diff --git a/core/include/fx/core/btree.h b/core/include/fx/core/btree.h new file mode 100644 index 0000000..82fb370 --- /dev/null +++ b/core/include/fx/core/btree.h @@ -0,0 +1,359 @@ +#ifndef FX_CORE_BST_H_ +#define FX_CORE_BST_H_ + +#include +#include +#include +#include +#include +#include + +FX_DECLS_BEGIN; + +#define FX_BTREE_INIT {0} +#define FX_TYPE_BTREE_ITERATOR (fx_bst_iterator_get_type()) + +FX_DECLARE_TYPE(fx_bst_iterator); + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_bst_iterator) +FX_TYPE_CLASS_DECLARATION_END(fx_bst_iterator) + +/* defines a simple node insertion function. + this function assumes that your nodes have simple integer keys that can be + compared with the usual operators. + + EXAMPLE: + if you have a tree node type like this: + + struct my_tree_node { + int key; + fx_bst_node base; + } + + You would use the following call to generate an insert function for a tree + with this node type: + + BTREE_DEFINE_SIMPLE_INSERT( + struct my_tree_node, + base, + key, + my_tree_node_insert); + + Which would emit a function defined like: + + static void my_tree_node_insert(fx_bst *tree, struct my_tree_node *node); + + @param node_type your custom tree node type. usually a structure that + contains a fx_bst_node member. + @param container_node_member the name of the fx_bst_node member variable + within your custom type. + @param container_key_member the name of the key member variable within your + custom type. + @param function_name the name of the function to generate. +*/ +#define FX_BTREE_DEFINE_SIMPLE_INSERT( \ + node_type, container_node_member, container_key_member, function_name) \ + void function_name(fx_bst *tree, node_type *node) \ + { \ + if (!tree->bst_root) { \ + tree->bst_root = &node->container_node_member; \ + fx_bst_insert_fixup(tree, &node->container_node_member); \ + return; \ + } \ + \ + fx_bst_node *cur = tree->bst_root; \ + while (1) { \ + node_type *cur_node = fx_unbox( \ + node_type, cur, container_node_member); \ + fx_bst_node *next = NULL; \ + \ + if (node->container_key_member \ + >= cur_node->container_key_member) { \ + next = fx_bst_right(cur); \ + \ + if (!next) { \ + fx_bst_put_right( \ + cur, \ + &node->container_node_member); \ + break; \ + } \ + } else if ( \ + node->container_key_member \ + < cur_node->container_key_member) { \ + next = fx_bst_left(cur); \ + \ + if (!next) { \ + fx_bst_put_left( \ + cur, \ + &node->container_node_member); \ + break; \ + } \ + } \ + \ + cur = next; \ + } \ + \ + fx_bst_insert_fixup(tree, &node->container_node_member); \ + } + +/* defines a node insertion function. + this function should be used for trees with complex node keys that cannot be + directly compared. a comparator for your keys must be supplied. + + EXAMPLE: + if you have a tree node type like this: + + struct my_tree_node { + complex_key_t key; + fx_bst_node base; + } + + You would need to define a comparator function or macro with the following + signature: + + int my_comparator(struct my_tree_node *a, struct my_tree_node *b); + + Which implements the following: + + return -1 if a < b + return 0 if a == b + return 1 if a > b + + You would use the following call to generate an insert function for a tree + with this node type: + + BTREE_DEFINE_INSERT(struct my_tree_node, base, key, my_tree_node_insert, + my_comparator); + + Which would emit a function defined like: + + static void my_tree_node_insert(fx_bst *tree, struct my_tree_node *node); + + @param node_type your custom tree node type. usually a structure that + contains a fx_bst_node member. + @param container_node_member the name of the fx_bst_node member variable + within your custom type. + @param container_key_member the name of the key member variable within your + custom type. + @param function_name the name of the function to generate. + @param comparator the name of a comparator function or functional-macro that + conforms to the requirements listed above. +*/ +#define FX_BTREE_DEFINE_INSERT( \ + node_type, container_node_member, container_key_member, function_name, \ + comparator) \ + void function_name(fx_bst *tree, node_type *node) \ + { \ + if (!tree->bst_root) { \ + tree->bst_root = &node->container_node_member; \ + fx_bst_insert_fixup(tree, &node->container_node_member); \ + return; \ + } \ + \ + fx_bst_node *cur = tree->bst_root; \ + while (1) { \ + node_type *cur_node = fx_unbox( \ + node_type, cur, container_node_member); \ + fx_bst_node *next = NULL; \ + int cmp = comparator(node, cur_node); \ + \ + if (cmp >= 0) { \ + next = fx_bst_right(cur); \ + \ + if (!next) { \ + fx_bst_put_right( \ + cur, \ + &node->container_node_member); \ + break; \ + } \ + } else if (cmp < 0) { \ + next = fx_bst_left(cur); \ + \ + if (!next) { \ + fx_bst_put_left( \ + cur, \ + &node->container_node_member); \ + break; \ + } \ + } else { \ + return; \ + } \ + \ + cur = next; \ + } \ + \ + fx_bst_insert_fixup(tree, &node->container_node_member); \ + } + +/* defines a simple tree search function. + this function assumes that your nodes have simple integer keys that can be + compared with the usual operators. + + EXAMPLE: + if you have a tree node type like this: + + struct my_tree_node { + int key; + fx_bst_node base; + } + + You would use the following call to generate a search function for a tree + with this node type: + + BTREE_DEFINE_SIMPLE_GET(struct my_tree_node, int, base, key, + my_tree_node_get); + + Which would emit a function defined like: + + static struct my_tree_node *my_tree_node_get(fx_bst *tree, int key); + + @param node_type your custom tree node type. usually a structure that + contains a fx_bst_node member. + @param key_type the type name of the key embedded in your custom tree node + type. this type must be compatible with the builtin comparison operators. + @param container_node_member the name of the fx_bst_node member variable + within your custom type. + @param container_key_member the name of the key member variable within your + custom type. + @param function_name the name of the function to generate. +*/ +#define FX_BTREE_DEFINE_SIMPLE_GET( \ + node_type, key_type, container_node_member, container_key_member, \ + function_name) \ + node_type *function_name(const fx_bst *tree, key_type key) \ + { \ + fx_bst_node *cur = tree->bst_root; \ + while (cur) { \ + node_type *cur_node = fx_unbox( \ + node_type, cur, container_node_member); \ + if (key > cur_node->container_key_member) { \ + cur = fx_bst_right(cur); \ + } else if (key < cur_node->container_key_member) { \ + cur = fx_bst_left(cur); \ + } else { \ + return cur_node; \ + } \ + } \ + \ + return NULL; \ + } + +#define fx_bst_foreach(it, bst) \ + for (int z__fx_unique_name() = fx_bst_iterator_begin(bst, it); \ + (it)->node != NULL; fx_bst_iterator_next(it)) + +/* binary tree nodes. this *cannot* be used directly. you need to define a + custom node type that contains a member variable of type fx_bst_node. + + you would then use the supplied macros to define functions to manipulate your + custom binary tree. +*/ +typedef struct fx_bst_node { + struct fx_bst_node *n_parent, *n_left, *n_right; + unsigned short n_height; +} fx_bst_node; + +/* binary tree. unlike fx_bst_node, you can define variables of type fx_bst. + */ +typedef struct fx_bst { + fx_bst_node *bst_root; +} fx_bst; + +FX_API fx_type fx_bst_iterator_get_type(void); + +/* re-balance a binary tree after an insertion operation. + + NOTE that, if you define an insertion function using BTREE_DEFINE_INSERT or + similar, this function will automatically called for you. + + @param tree the tree to re-balance. + @param node the node that was just inserted into the tree. +*/ +FX_API void fx_bst_insert_fixup(fx_bst *tree, fx_bst_node *node); + +/* delete a node from a binary tree and re-balance the tree afterwards. + + @param tree the tree to delete from + @param node the node to delete. +*/ +FX_API void fx_bst_delete(fx_bst *tree, fx_bst_node *node); + +/* get the first node in a binary tree. + + this will be the node with the smallest key (i.e. the node that is + furthest-left from the root) +*/ +FX_API fx_bst_node *fx_bst_first(const fx_bst *tree); + +/* get the last node in a binary tree. + + this will be the node with the largest key (i.e. the node that is + furthest-right from the root) +*/ +FX_API fx_bst_node *fx_bst_last(const fx_bst *tree); +/* for any binary tree node, this function returns the node with the + * next-largest key value */ +FX_API fx_bst_node *fx_bst_next(const fx_bst_node *node); +/* for any binary tree node, this function returns the node with the + * next-smallest key value */ +FX_API fx_bst_node *fx_bst_prev(const fx_bst_node *node); +/* return true if the bst is empty, false otherwise */ +static inline bool fx_bst_empty(const fx_bst *tree) +{ + return tree->bst_root == NULL; +} + +/* sets `child` as the immediate left-child of `parent` */ +static inline void fx_bst_put_left(fx_bst_node *parent, fx_bst_node *child) +{ + parent->n_left = child; + child->n_parent = parent; +} + +/* sets `child` as the immediate right-child of `parent` */ +static inline void fx_bst_put_right(fx_bst_node *parent, fx_bst_node *child) +{ + parent->n_right = child; + child->n_parent = parent; +} + +/* get the immediate left-child of `node` */ +static inline fx_bst_node *fx_bst_left(fx_bst_node *node) +{ + return node->n_left; +} + +/* get the immediate right-child of `node` */ +static inline fx_bst_node *fx_bst_right(fx_bst_node *node) +{ + return node->n_right; +} + +/* get the immediate parent of `node` */ +static inline fx_bst_node *fx_bst_parent(fx_bst_node *node) +{ + return node->n_parent; +} + +FX_API void fx_bst_move(fx_bst *tree, fx_bst_node *dest, fx_bst_node *src); + +/* get the height of `node`. + + the height of a node is defined as the length of the longest path + between the node and a leaf node. + + this count includes the node itself, so the height of a leaf node will be 1. +*/ +static inline unsigned short fx_bst_height(fx_bst_node *node) +{ + return node->n_height; +} + +FX_API fx_iterator *fx_bst_begin(fx_bst *tree); +FX_API const fx_iterator *fx_bst_cbegin(const fx_bst *tree); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/core/include/fx/core/class.h b/core/include/fx/core/class.h new file mode 100644 index 0000000..32c77df --- /dev/null +++ b/core/include/fx/core/class.h @@ -0,0 +1,15 @@ +#ifndef FX_OBJECT_CLASS_H_ +#define FX_OBJECT_CLASS_H_ + +#include + +#define FX_CLASS_MAGIC 0xDEADFACEDCAFEBEDULL +#define FX_CLASS(p) ((fx_class *)(p)) + +typedef struct _fx_class fx_class; + +FX_API void *fx_class_get(fx_type id); +FX_API const char *fx_class_get_name(const fx_class *c); +FX_API void *fx_class_get_interface(const fx_class *c, fx_type id); + +#endif diff --git a/core/include/fx/core/encoding.h b/core/include/fx/core/encoding.h new file mode 100644 index 0000000..1347b27 --- /dev/null +++ b/core/include/fx/core/encoding.h @@ -0,0 +1,41 @@ +#ifndef FX_CORE_ENCODING_H_ +#define FX_CORE_ENCODING_H_ + +#include +#include +#include + +#define FX_WCHAR_INVALID ((fx_wchar) - 1) + +typedef int32_t fx_wchar; + +FX_API bool fx_wchar_is_alpha(fx_wchar c); +FX_API bool fx_wchar_is_number(fx_wchar c); +static inline bool fx_wchar_is_bin_digit(fx_wchar c) +{ + return c >= '0' && c <= '1'; +} +static inline bool fx_wchar_is_oct_digit(fx_wchar c) +{ + return c >= '0' && c <= '7'; +} +FX_API bool fx_wchar_is_hex_digit(fx_wchar c); +FX_API bool fx_wchar_is_space(fx_wchar c); +static inline bool fx_wchar_is_alnum(fx_wchar c) +{ + return fx_wchar_is_alpha(c) || fx_wchar_is_number(c); +} + +FX_API bool fx_wchar_is_punct(fx_wchar c); + +FX_API bool fx_wchar_utf8_is_valid_scalar(fx_wchar c); +FX_API unsigned int fx_wchar_utf8_header_decode(char c); +FX_API unsigned int fx_wchar_utf8_codepoint_size(fx_wchar c); +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_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_string_encoded_size( + const fx_wchar *s, size_t nr_codepoints); + +#endif diff --git a/core/include/fx/core/endian.h b/core/include/fx/core/endian.h new file mode 100644 index 0000000..d1a8e6e --- /dev/null +++ b/core/include/fx/core/endian.h @@ -0,0 +1,49 @@ +#ifndef FX_CORE_ENDIAN_H_ +#define FX_CORE_ENDIAN_H_ + +#include +#include + +typedef struct { + union { + unsigned char i_bytes[sizeof(uint16_t)]; + int16_t i_val; + uint16_t i_uval; + }; +} fx_i16; + +typedef struct { + union { + unsigned char i_bytes[sizeof(uint32_t)]; + int32_t i_val; + uint32_t i_uval; + }; +} fx_i32; + +typedef struct { + union { + unsigned char i_bytes[sizeof(uint64_t)]; + int64_t i_val; + uint64_t i_uval; + }; +} fx_i64; + +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 diff --git a/core/include/fx/core/error.h b/core/include/fx/core/error.h new file mode 100644 index 0000000..3c52071 --- /dev/null +++ b/core/include/fx/core/error.h @@ -0,0 +1,418 @@ +#ifndef FX_CORE_ERROR_H_ +#define FX_CORE_ERROR_H_ + +#include +#include +#include +#include + +#define FX_ERROR_TEMPLATE_PARAMETER_MAX 4 +#define FX_ERROR_MSG_ID_INVALID ((unsigned long)-1) + +#define FX_CATCH(err, expr) ((err = (expr)) != NULL) + +#define fx_result_is_error(result) ((result) != NULL) +#define fx_result_is_success(result) ((result) == NULL) + +#define FX_RESULT_SUCCESS ((fx_result)NULL) +#define FX_RESULT_ERR(err_name) \ + fx_error_with_code(fx_error_vendor_get_builtin(), FX_ERR_##err_name) +#define FX_RESULT_ERR_WITH_STRING(err_name, ...) \ + fx_error_with_string( \ + fx_error_vendor_get_builtin(), FX_ERR_##err_name, __VA_ARGS__) +#define FX_RESULT_STATUS(code) \ + ((code) == FX_SUCCESS \ + ? FX_RESULT_SUCCESS \ + : (fx_error_with_code(fx_error_vendor_get_builtin(), code))) +#define FX_RESULT_STATUS_WITH_STRING(code, ...) \ + ((code) == FX_SUCCESS \ + ? FX_RESULT_SUCCESS \ + : (fx_error_with_string( \ + fx_error_vendor_get_builtin(), code, __VA_ARGS__))) + +#define FX_ERRORS_BUILTIN (fx_error_vendor_get_builtin()) +#define FX_ERRORS_ERRNO (fx_error_vendor_get_errno()) + +#define FX_ERROR_PARAM(name, value) \ + (fx_error_template_parameter) \ + { \ + .param_name = (name), .param_value = (uintptr_t)(value), \ + } + +#define FX_ERROR_TEMPLATE_PARAM(name, type, format) \ + (fx_error_template_parameter_definition) \ + { \ + .param_name = (name), .param_type = (type), \ + .param_format = (format), \ + } +#define FX_ERROR_DEFINITION(code, name, msg) \ + [code] = (fx_error_definition) \ + { \ + .err_name = (name), .err_message = (msg), \ + } +#define FX_ERROR_DEFINITION_TEMPLATE(code, name, msg, ...) \ + [code] = (fx_error_definition) \ + { \ + .err_name = (name), .err_message = (msg), \ + .err_params = __VA_ARGS__, \ + } + +#define FX_ERROR_MSG(id, content) \ + [id] = (fx_error_msg) \ + { \ + .msg_message = (content), \ + } +#define FX_ERROR_MSG_TEMPLATE(id, content, ...) \ + [id] = (fx_error_msg) \ + { \ + .msg_message = (content), .msg_params = __VA_ARGS__, \ + } + +#define z__fx_error_create_status(status_code) \ + (z__fx_error_create( \ + fx_error_vendor_get_builtin(), status_code, NULL, NULL, 0, \ + NULL, NULL)) + +/* Error creation macros */ +#define fx_error_with_code(vendor, code) \ + (z__fx_error_create( \ + vendor, code, NULL, __FILE__, __LINE__, __FUNCTION__, NULL)) +#define fx_error_caused_by_error(vendor, code, cause_error) \ + (z__fx_error_create( \ + vendor, code, cause_error, __FILE__, __LINE__, __FUNCTION__, NULL)) +#define fx_error_caused_by_status(vendor, code, cause_status) \ + (z__fx_error_create( \ + vendor, code, z__fx_error_create_status(cause_status), \ + __FILE__, __LINE__, __FUNCTION__, NULL)) +#define fx_error_caused_by_code(vendor, code, cause_vendor, cause_code) \ + (z__fx_error_create( \ + vendor, code, fx_error_with_code(cause_vendor, cause_code), \ + __FILE__, __LINE__, __FUNCTION__, NULL)) +#define fx_error_with_string(vendor, code, ...) \ + (z__fx_error_create( \ + vendor, code, NULL, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)) +#define fx_error_with_string_caused_by_error(vendor, code, cause_error, ...) \ + (z__fx_error_create( \ + vendor, code, cause_error, __FILE__, __LINE__, __FUNCTION__, \ + __VA_ARGS__)) +#define fx_error_with_string_caused_by_status(vendor, code, cause_status, ...) \ + (z__fx_error_create( \ + vendor, code, z__fx_error_create_status(cause_status), \ + __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)) +#define fx_error_with_msg(vendor, code, msg_id) \ + (z__fx_error_create_msg( \ + vendor, code, NULL, __FILE__, __LINE__, __FUNCTION__, msg_id, \ + (fx_error_template_parameter[]) {{}})) +#define fx_error_with_msg_caused_by_error(vendor, code, cause_error, msg_id) \ + (z__fx_error_create_msg( \ + vendor, code, cause_error, __FILE__, __LINE__, __FUNCTION__, \ + msg_id, (fx_error_template_parameter[]) {{}})) +#define fx_error_with_msg_caused_by_status(vendor, code, cause_status, msg_id) \ + (z__fx_error_create_msg( \ + vendor, code, z__fx_error_create_status(cause_status), \ + __FILE__, __LINE__, __FUNCTION__, msg_id, \ + (fx_error_template_parameter[]) {{}})) +#define fx_error_with_msg_template(vendor, code, msg_id, ...) \ + (z__fx_error_create_msg( \ + vendor, code, NULL, __FILE__, __LINE__, __FUNCTION__, msg_id, \ + (fx_error_template_parameter[]) {__VA_ARGS__, {}})) +#define fx_error_with_msg_template_caused_by_error( \ + vendor, code, cause_error, msg_id, ...) \ + (z__fx_error_create_msg( \ + vendor, code, cause_error, __FILE__, __LINE__, __FUNCTION__, \ + msg_id, (fx_error_template_parameter[]) {__VA_ARGS__, {}})) +#define fx_error_with_msg_template_caused_by_status( \ + vendor, code, cause_status, msg_id, ...) \ + (z__fx_error_create_msg( \ + vendor, code, z__fx_error_create_status(cause_status), \ + __FILE__, __LINE__, __FUNCTION__, msg_id, \ + (fx_error_template_parameter[]) {__VA_ARGS__, {}})) +#define fx_error_with_template(vendor, code, ...) \ + (z__fx_error_create_template( \ + vendor, code, NULL, __FILE__, __LINE__, __FUNCTION__, \ + (fx_error_template_parameter[]) {__VA_ARGS__, {}})) +#define fx_error_with_template_caused_by_error(vendor, code, cause_error, ...) \ + (z__fx_error_create_template( \ + vendor, code, cause_error, __FILE__, __LINE__, __FUNCTION__, \ + (fx_error_template_parameter[]) {__VA_ARGS__, {}})) +#define fx_error_with_template_caused_by_status(vendor, code, cause_status, ...) \ + (z__fx_error_create_template( \ + vendor, code, z__fx_error_create_status(cause_status), \ + __FILE__, __LINE__, __FUNCTION__, \ + (fx_error_template_parameter[]) {__VA_ARGS__, {}})) + +/* Error propagation macros */ +#define fx_result_propagate(err) \ + (z__fx_error_propagate(err, __FILE__, __LINE__, __FUNCTION__)) +#define fx_error_caused_by(err, caused_by) (z__fx_error_caused_by(err, caused_by)) +#define fx_error_caused_by_fx_status(err, status) \ + (z__fx_error_caused_by_fx_status(err, status)) +#define fx_error_replace(err, caused_by) \ + (z__fx_error_propagate(err, __FILE__, __LINE__, __FUNCTION__)) + +/* Error throw macros */ +#define z__fx_throw(err) (z__fx_error_throw(err, NULL, 0, NULL)) +#define fx_throw(err) (z__fx_error_throw(err, __FILE__, __LINE__, __FUNCTION__)) +#define fx_throw_status(status) \ + (z__fx_error_throw( \ + z__fx_error_create( \ + fx_error_vendor_get_builtin(), status, NULL, NULL, 0, \ + NULL, NULL), \ + __FILE__, __LINE__, __FUNCTION__)) + +#define fx_throw_status_string(status, ...) \ + (z__fx_error_throw( \ + z__fx_error_create( \ + fx_error_vendor_get_builtin(), status, NULL, NULL, 0, \ + NULL, __VA_ARGS__), \ + __FILE__, __LINE__, __FUNCTION__)) +#define fx_throw_error_code(vendor, code) \ + z__fx_throw(fx_error_with_code(vendor, code)) +#define fx_throw_error_caused_by_error(vendor, code, cause) \ + z__fx_throw(fx_error_caused_by_error(vendor, code, cause)) +#define fx_throw_error_caused_by_status(vendor, code, cause) \ + z__fx_throw(fx_error_caused_by_status(vendor, code, cause)) +#define fx_throw_error_with_string(vendor, code, ...) \ + z__fx_throw(fx_error_with_string(vendor, code, __VA_ARGS__)) +#define fx_throw_error_with_string_caused_by_error(vendor, code, cause, ...) \ + z__fx_throw(fx_error_with_string_caused_by_error( \ + vendor, code, cause, __VA_ARGS__)) +#define fx_throw_error_with_string_caused_by_status(vendor, code, cause, ...) \ + z__fx_throw(fx_error_with_string_caused_by_status( \ + vendor, code, cause, __VA_ARGS__)) + +#define fx_throw_error_with_msg(vendor, code, msg_id) \ + z__fx_throw(fx_error_with_msg(vendor, code, msg_id)) +#define fx_throw_error_with_msg_caused_by_error(vendor, code, cause, msg_id) \ + z__fx_throw(fx_error_with_msg_caused_by_error(vendor, code, cause, msg_id)) +#define fx_throw_error_with_msg_caused_by_status(vendor, code, cause, msg_id) \ + z__fx_throw(fx_error_with_msg_caused_by_status(vendor, code, cause, msg_id)) + +#define fx_throw_error_with_msg_template(vendor, code, msg_id, ...) \ + z__fx_throw(fx_error_with_msg_template(vendor, code, msg_id, __VA_ARGS__)) +#define fx_throw_error_with_msg_template_caused_by_error( \ + vendor, code, cause, msg_id, ...) \ + z__fx_throw(fx_error_with_msg_template_caused_by_error( \ + vendor, code, cause, msg_id, __VA_ARGS__)) +#define fx_throw_error_with_msg_template_caused_by_status( \ + vendor, code, cause, msg_id, ...) \ + z__fx_throw(fx_error_with_msg_template_caused_by_status( \ + vendor, code, cause, msg_id, __VA_ARGS__)) + +#define fx_throw_error_with_template(vendor, code, ...) \ + z__fx_throw(fx_error_with_template(vendor, code, __VA_ARGS__)) +#define fx_throw_error_with_template_caused_by_error(vendor, code, cause, ...) \ + z__fx_throw(fx_error_with_template_caused_by_error( \ + vendor, code, cause, __VA_ARGS__)) +#define fx_throw_error_with_template_caused_by_status(vendor, code, cause, ...) \ + z__fx_throw(fx_error_with_template_caused_by_status( \ + vendor, code, cause, __VA_ARGS__)) + +#define FX_ERR_MSG(s) \ + { \ + .msg_type = FX_ERROR_MESSAGE_ERROR, \ + .msg_content = (s), \ + } +#define FX_ERR_MSG_WARN(s) \ + { \ + .msg_type = FX_ERROR_MESSAGE_WARN, \ + .msg_content = (s), \ + } +#define FX_ERR_MSG_INFO(s) \ + { \ + .msg_type = FX_ERROR_MESSAGE_INFO, \ + .msg_content = (s), \ + } +#define FX_ERR_MSG_END(s) \ + { \ + .msg_type = FX_ERROR_MESSAGE_NONE, \ + .msg_content = NULL, \ + } + +typedef enum fx_error_submsg_type { + FX_ERROR_SUBMSG_NONE = 0, + FX_ERROR_SUBMSG_ERROR, + FX_ERROR_SUBMSG_WARNING, + FX_ERROR_SUBMSG_INFO, +} fx_error_submsg_type; + +typedef enum fx_error_report_flags { + FX_ERROR_REPORT_NONE = 0, + FX_ERROR_REPORT_STATUS = 0x01u, + FX_ERROR_REPORT_DESCRIPTION = 0x02u, + FX_ERROR_REPORT_SUBMSG = 0x04u, + FX_ERROR_REPORT_STACK_TRACE = 0x08u, + FX_ERROR_REPORT_CAUSE = 0x10u, + + FX_ERROR_REPORT_MINIMAL = FX_ERROR_REPORT_STATUS | FX_ERROR_REPORT_DESCRIPTION, + FX_ERROR_REPORT_DEFAULT = FX_ERROR_REPORT_MINIMAL | FX_ERROR_REPORT_SUBMSG + | FX_ERROR_REPORT_CAUSE, + FX_ERROR_REPORT_ALL = FX_ERROR_REPORT_DEFAULT | FX_ERROR_REPORT_STACK_TRACE, +} fx_error_report_flags; + +typedef enum fx_error_template_parameter_type { + FX_ERROR_TEMPLATE_PARAM_NONE = 0, + FX_ERROR_TEMPLATE_PARAM_STRING, + FX_ERROR_TEMPLATE_PARAM_CHAR, + FX_ERROR_TEMPLATE_PARAM_INT, + FX_ERROR_TEMPLATE_PARAM_UINT, + FX_ERROR_TEMPLATE_PARAM_LONG, + FX_ERROR_TEMPLATE_PARAM_ULONG, + FX_ERROR_TEMPLATE_PARAM_LONGLONG, + FX_ERROR_TEMPLATE_PARAM_ULONGLONG, + FX_ERROR_TEMPLATE_PARAM_SIZE_T, + FX_ERROR_TEMPLATE_PARAM_INTPTR, + FX_ERROR_TEMPLATE_PARAM_UINTPTR, + FX_ERROR_TEMPLATE_PARAM_PTR, +} fx_error_template_parameter_type; + +typedef struct fx_error_template_parameter_definition { + const char *param_name; + fx_error_template_parameter_type param_type; + const char *param_format; +} fx_error_template_parameter_definition; + +typedef struct fx_error_template_parameter { + const char *param_name; + uintptr_t param_value; + const struct fx_error_template_parameter_definition *__param_def; +} fx_error_template_parameter; + +struct fx_error_vendor; + +typedef struct fx_error fx_error; +typedef struct fx_error *fx_result; +typedef struct fx_error_submsg fx_error_submsg; +typedef struct fx_error_stack_frame fx_error_stack_frame; +typedef long fx_error_status_code; +typedef unsigned long fx_error_msg_id; + +typedef struct fx_error_definition { + const char *err_name; + const char *err_message; + const fx_error_template_parameter_definition err_params[FX_ERROR_TEMPLATE_PARAMETER_MAX]; +} fx_error_definition; + +typedef struct fx_error_msg { + const char *msg_message; + const fx_error_template_parameter_definition msg_params[FX_ERROR_TEMPLATE_PARAMETER_MAX]; +} fx_error_msg; + +typedef const fx_error_definition *(*fx_error_status_code_get_definition)( + const struct fx_error_vendor *, fx_error_status_code); +typedef const fx_error_msg *(*fx_error_msg_get_definition)( + const struct fx_error_vendor *, fx_error_msg_id); +typedef void (*fx_error_report_function)( + const struct fx_error *, fx_error_report_flags); + +typedef struct fx_error_vendor { + const char *v_name; + + fx_error_status_code_get_definition v_status_get_definition; + fx_error_msg_get_definition v_msg_get_definition; + + const fx_error_definition *v_error_definitions; + size_t v_error_definitions_length; + + const fx_error_msg *v_msg; + size_t v_msg_length; +} fx_error_vendor; + +FX_API fx_error *z__fx_error_create_template( + const fx_error_vendor *, fx_error_status_code, fx_error *, const char *, + unsigned int, const char *, const fx_error_template_parameter[]); +FX_API fx_error *z__fx_error_create_string( + const fx_error_vendor *, fx_error_status_code, fx_error *, const char *, + unsigned int, const char *, const char *, va_list); +FX_API fx_error *z__fx_error_create_msg( + const fx_error_vendor *, fx_error_status_code, fx_error *, const char *, + unsigned int, const char *, fx_error_msg_id, + const fx_error_template_parameter[]); +FX_API fx_error *z__fx_error_propagate( + fx_error *, const char *, unsigned int, const char *); +FX_API fx_error *z__fx_error_caused_by(fx_error *, fx_error *); +FX_API fx_error *z__fx_error_caused_by_fx_status(fx_error *, fx_status); +FX_API void z__fx_error_throw(fx_error *, const char *, unsigned int, const char *); + +FX_API bool fx_result_is( + fx_result result, const fx_error_vendor *vendor, fx_error_status_code code); + +FX_API const fx_error_vendor *fx_error_vendor_get_builtin(void); +FX_API const fx_error_vendor *fx_error_vendor_get_errno(void); +FX_API const fx_error_definition *fx_error_vendor_get_error_definition( + const fx_error_vendor *vendor, fx_error_status_code code); +FX_API const char *fx_error_vendor_get_status_code_name( + const fx_error_vendor *vendor, fx_error_status_code code); +FX_API const char *fx_error_vendor_get_status_code_description( + const fx_error_vendor *vendor, fx_error_status_code code); +FX_API const fx_error_msg *fx_error_vendor_get_msg( + const fx_error_vendor *vendor, fx_error_msg_id msg_id); + +static inline fx_error *z__fx_error_create( + const fx_error_vendor *v, fx_error_status_code c, fx_error *c2, + const char *f0, unsigned int l, const char *f1, const char *d, ...) +{ + va_list arg; + va_start(arg, d); + fx_error *err = z__fx_error_create_string(v, c, c2, f0, l, f1, d, arg); + va_end(arg); + return err; +} + +FX_API enum fx_status fx_error_add_submsg_string( + fx_error *error, fx_error_submsg_type type, const char *msg, ...); +FX_API enum fx_status z__fx_error_add_submsg_template( + fx_error *error, fx_error_submsg_type type, fx_error_msg_id msg_id, + fx_error_template_parameter param[]); +#define fx_error_add_submsg(error, type, msg_id) \ + (z__fx_error_add_submsg_template( \ + error, type, msg_id, (fx_error_template_parameter[]) {{}})) +#define fx_error_add_submsg_template(error, type, msg_id, ...) \ + (z__fx_error_add_submsg_template( \ + error, type, msg_id, \ + (fx_error_template_parameter[]) {__VA_ARGS__, {}})) + +FX_API void fx_error_discard(fx_error *error); + +FX_API fx_error_status_code fx_error_get_status_code(const fx_error *error); +FX_API const fx_error_vendor *fx_error_get_vendor(const fx_error *error); +FX_API const fx_error_definition *fx_error_get_definition(const fx_error *error); +FX_API const fx_error_template_parameter *fx_error_get_template_parameter( + const fx_error *error, const char *param_name); +FX_API const fx_error_template_parameter *fx_error_get_template_parameters( + const fx_error *error); +FX_API const char *fx_error_get_description(const fx_error *error); +FX_API const fx_error_msg *fx_error_get_msg(const fx_error *error); +FX_API const fx_error_submsg *fx_error_get_first_submsg(const fx_error *error); +FX_API const fx_error_submsg *fx_error_get_next_submsg( + const fx_error *error, const fx_error_submsg *msg); +FX_API const fx_error_stack_frame *fx_error_get_first_stack_frame( + const fx_error *error); +FX_API const fx_error_stack_frame *fx_error_get_next_stack_frame( + const fx_error *error, const fx_error_stack_frame *frame); +FX_API const fx_error *fx_error_get_caused_by(const fx_error *error); + +FX_API fx_error_submsg_type fx_error_submsg_get_type(const fx_error_submsg *msg); +FX_API const char *fx_error_submsg_get_content(const fx_error_submsg *msg); +FX_API const fx_error_msg *fx_error_submsg_get_msg(const fx_error_submsg *msg); +FX_API const fx_error_template_parameter *fx_error_submsg_get_template_parameters( + const fx_error_submsg *msg); + +FX_API const char *fx_error_stack_frame_get_filepath( + const fx_error_stack_frame *frame); +FX_API unsigned int fx_error_stack_frame_get_line_number( + const fx_error_stack_frame *frame); +FX_API const char *fx_error_stack_frame_get_function_name( + const fx_error_stack_frame *frame); + +FX_API const fx_error_template_parameter_definition *fx_error_definition_get_template_parameter( + const fx_error_definition *error_def, const char *param_name); + +FX_API const char *fx_error_msg_get_content(const fx_error_msg *msg); +FX_API const fx_error_template_parameter_definition *fx_error_msg_get_template_parameter( + const fx_error_msg *msg, const char *param_name); + +FX_API void fx_set_error_report_function( + fx_error_report_function func, fx_error_report_flags flags); + +#endif diff --git a/core/include/blue/core/exception.h b/core/include/fx/core/exception.h similarity index 100% rename from core/include/blue/core/exception.h rename to core/include/fx/core/exception.h diff --git a/core/include/fx/core/hash.h b/core/include/fx/core/hash.h new file mode 100644 index 0000000..d3d44a6 --- /dev/null +++ b/core/include/fx/core/hash.h @@ -0,0 +1,111 @@ +#ifndef FX_CORE_HASH_H_ +#define FX_CORE_HASH_H_ + +#include +#include +#include +#include + +#define FX_DIGEST_LENGTH_128 16 +#define FX_DIGEST_LENGTH_160 20 +#define FX_DIGEST_LENGTH_192 24 +#define FX_DIGEST_LENGTH_224 28 +#define FX_DIGEST_LENGTH_256 32 +#define FX_DIGEST_LENGTH_384 48 +#define FX_DIGEST_LENGTH_512 64 + +#define FX_DIGEST_LENGTH_MD4 FX_DIGEST_LENGTH_128 +#define FX_DIGEST_LENGTH_MD5 FX_DIGEST_LENGTH_128 +#define FX_DIGEST_LENGTH_SHA1 FX_DIGEST_LENGTH_160 +#define FX_DIGEST_LENGTH_SHA2_224 FX_DIGEST_LENGTH_224 +#define FX_DIGEST_LENGTH_SHA2_256 FX_DIGEST_LENGTH_256 +#define FX_DIGEST_LENGTH_SHA2_384 FX_DIGEST_LENGTH_384 +#define FX_DIGEST_LENGTH_SHA2_512 FX_DIGEST_LENGTH_512 +#define FX_DIGEST_LENGTH_SHA3_224 FX_DIGEST_LENGTH_224 +#define FX_DIGEST_LENGTH_SHA3_256 FX_DIGEST_LENGTH_256 +#define FX_DIGEST_LENGTH_SHA3_384 FX_DIGEST_LENGTH_384 +#define FX_DIGEST_LENGTH_SHA3_512 FX_DIGEST_LENGTH_512 +#define FX_DIGEST_LENGTH_SHAKE128 FX_DIGEST_LENGTH_128 +#define FX_DIGEST_LENGTH_SHAKE256 FX_DIGEST_LENGTH_256 + +struct fx_hash_function_ops; +struct fx_rope; + +typedef enum fx_hash_function { + FX_HASH_NONE = 0, + FX_HASH_MD4, + FX_HASH_MD5, + FX_HASH_SHA1, + FX_HASH_SHA2_224, + FX_HASH_SHA2_256, + FX_HASH_SHA2_384, + FX_HASH_SHA2_512, + FX_HASH_SHA3_224, + FX_HASH_SHA3_256, + FX_HASH_SHA3_384, + FX_HASH_SHA3_512, + FX_HASH_SHAKE128, + FX_HASH_SHAKE256, +} fx_hash_function; + +typedef struct fx_hash_ctx { + fx_hash_function ctx_func; + const struct fx_hash_function_ops *ctx_ops; + + union { + struct { + uint32_t lo, hi; + uint32_t a, b, c, d; + uint32_t block[16]; + unsigned char buffer[64]; + } md4; + + struct { + unsigned int count[2]; + unsigned int a, b, c, d; + unsigned int block[16]; + unsigned char input[64]; + } md5; + + struct { + uint32_t state[5]; + uint32_t count[2]; + unsigned char buffer[64]; + } sha1; + + struct { + uint64_t curlen; + uint64_t length; + unsigned char buf[128]; + uint32_t state[8]; + } sha2_256; + + struct { + uint64_t curlen; + uint64_t length; + unsigned char block[256]; + uint64_t state[8]; + } sha2_512; + + struct { + union { + uint8_t b[200]; + uint64_t q[25]; + } st; + + int pt, rsiz, mdlen; + } sha3; + } ctx_state; +} fx_hash_ctx; + +FX_API uint64_t fx_hash_cstr(const char *s); +FX_API uint64_t fx_hash_cstr_ex(const char *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_reset(fx_hash_ctx *ctx); +FX_API fx_status fx_hash_ctx_update(fx_hash_ctx *ctx, const void *p, size_t len); +FX_API fx_status fx_hash_ctx_update_rope(fx_hash_ctx *ctx, const struct fx_rope *rope); +FX_API fx_status fx_hash_ctx_finish( + fx_hash_ctx *ctx, void *out_digest, size_t out_max); + +#endif diff --git a/core/include/blue/core/init.h b/core/include/fx/core/init.h similarity index 73% rename from core/include/blue/core/init.h rename to core/include/fx/core/init.h index 49ac19d..8e338bd 100644 --- a/core/include/blue/core/init.h +++ b/core/include/fx/core/init.h @@ -1,8 +1,8 @@ -#ifndef BLUELIB_INIT_H_ -#define BLUELIB_INIT_H_ +#ifndef FX_INIT_H_ +#define FX_INIT_H_ #ifdef __cplusplus -#define B_INIT(f) \ +#define FX_INIT(f) \ static void f(void); \ struct f##_t_ { \ f##_t_(void) \ @@ -14,17 +14,17 @@ static void f(void) #elif defined(_MSC_VER) #pragma section(".CRT$XCU", read) -#define B_INIT2_(f, p) \ +#define FX_INIT2_(f, p) \ static void f(void); \ __declspec(allocate(".CRT$XCU")) void (*f##_)(void) = f; \ __pragma(comment(linker, "/include:" p #f "_")) static void f(void) #ifdef _WIN64 -#define B_INIT(f) B_INIT2_(f, "") +#define FX_INIT(f) FX_INIT2_(f, "") #else -#define B_INIT(f) B_INIT2_(f, "_") +#define FX_INIT(f) FX_INIT2_(f, "_") #endif #else -#define B_INIT(f) \ +#define FX_INIT(f) \ static void f(void) __attribute__((constructor)); \ static void f(void) #endif diff --git a/core/include/fx/core/iterator.h b/core/include/fx/core/iterator.h new file mode 100644 index 0000000..a91dd3e --- /dev/null +++ b/core/include/fx/core/iterator.h @@ -0,0 +1,93 @@ +#ifndef FX_CORE_ITERATOR_H_ +#define FX_CORE_ITERATOR_H_ + +#include +#include +#include +#include + +FX_DECLS_BEGIN; + +#define fx_foreach(type, var, iterator) \ + for (type var = (type)fx_iterator_get_value(iterator).v_int; \ + FX_OK(fx_iterator_get_status(iterator)); \ + fx_iterator_move_next(iterator), \ + var = (type)fx_iterator_get_value(iterator).v_int) +#define fx_foreach_ptr(type, var, iterator) \ + for (type *var = (type *)fx_iterator_get_value(iterator).v_ptr; \ + FX_OK(fx_iterator_get_status(iterator)); \ + fx_iterator_move_next(iterator), \ + var = (type *)fx_iterator_get_value(iterator).v_ptr) +#define fx_foreach_c(type, var, iterator) \ + for (type var = (type)fx_iterator_get_cvalue(iterator).v_int; \ + FX_OK(fx_iterator_get_status(iterator)); \ + fx_iterator_move_next(iterator), \ + var = (type)fx_iterator_get_cvalue(iterator).v_int) +#define fx_foreach_cptr(type, var, iterator) \ + for (const type *var \ + = (const type *)fx_iterator_get_cvalue(iterator).v_cptr; \ + FX_OK(fx_iterator_get_status(iterator)); \ + fx_iterator_move_next(iterator), \ + var = (const type *)fx_iterator_get_cvalue(iterator).v_cptr) + +#define FX_ITERATOR_VALUE_INT(v) ((fx_iterator_value) {.v_int = (v)}) +#define FX_ITERATOR_VALUE_PTR(v) ((fx_iterator_value) {.v_ptr = (v)}) +#define FX_ITERATOR_VALUE_CPTR(v) ((const fx_iterator_value) {.v_cptr = (v)}) +#define FX_ITERATOR_VALUE_NULL ((fx_iterator_value) {}) +#define FX_ITERATOR_VALUE_IS_NULL(v) ((v)->v_ptr == NULL) + +#define FX_TYPE_ITERATOR (fx_iterator_get_type()) +#define FX_TYPE_ITERABLE (fx_iterable_get_type()) + +typedef union fx_iterator_value { + uintptr_t v_int; + void *v_ptr; + const void *v_cptr; +} fx_iterator_value; + +__FX_DECLARE_TYPE(fx_iterator); + +FX_DECLARE_TYPE(fx_iterable); + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_iterator) + fx_status (*it_move_next)(const fx_iterator *); + fx_status (*it_erase)(fx_iterator *); + fx_iterator_value (*it_get_value)(fx_iterator *); + const fx_iterator_value (*it_get_cvalue)(const fx_iterator *); +FX_TYPE_CLASS_DECLARATION_END(fx_iterator) + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_iterable) + fx_iterator *(*it_begin)(fx_iterable *); + const fx_iterator *(*it_cbegin)(const fx_iterable *); +FX_TYPE_CLASS_DECLARATION_END(fx_iterable) + +FX_API fx_type fx_iterator_get_type(void); +FX_API fx_type fx_iterable_get_type(void); + +static inline const fx_iterator *fx_iterator_ref(const fx_iterator *p) +{ + return fx_object_ref((fx_object *)p); +} +static inline void fx_iterator_unref(const fx_iterator *p) +{ + fx_object_unref((fx_object *)p); +} + +FX_API fx_iterator *fx_iterator_begin(fx_iterable *it); +FX_API const fx_iterator *fx_iterator_cbegin(const fx_iterable *it); + +FX_API fx_status fx_iterator_get_status(const fx_iterator *it); +FX_API fx_status fx_iterator_set_status(const fx_iterator *it, fx_status status); + +FX_API fx_status fx_iterator_move_next(const fx_iterator *it); +FX_API fx_iterator_value fx_iterator_get_value(fx_iterator *it); +FX_API const fx_iterator_value fx_iterator_get_cvalue(const fx_iterator *it); +FX_API fx_status fx_iterator_erase(fx_iterator *it); +static inline bool fx_iterator_is_valid(const fx_iterator *it) +{ + return FX_OK(fx_iterator_get_status(it)); +} + +FX_DECLS_END; + +#endif diff --git a/core/include/fx/core/macros.h b/core/include/fx/core/macros.h new file mode 100644 index 0000000..c134597 --- /dev/null +++ b/core/include/fx/core/macros.h @@ -0,0 +1,197 @@ +#ifndef FX_CORE_MACROS_H_ +#define FX_CORE_MACROS_H_ + +#include +#include +#include +#include +#include + +#define __FX_IFACE_I0(p, x) p##x +#define __FX_IFACE_I1(p, x) __FX_IFACE_I0(p, x) + +/* Type definitions macros (for use in .c source file) */ + +#define FX_TYPE_CLASS_DEFINITION_BEGIN(type_name) \ + static void type_name##_class_init(fx_class *p, void *d) \ + { +#define FX_TYPE_CLASS_DEFINITION_END(type_name) } + +#define FX_TYPE_CLASS_INTERFACE_BEGIN(interface_name, interface_id) \ + interface_name##_class *__FX_IFACE_I1(iface, __LINE__) \ + = fx_class_get_interface(p, interface_id); \ + if (!__FX_IFACE_I1(iface, __LINE__)) { \ + fx_throw_error_with_msg_template( \ + FX_ERRORS_BUILTIN, FX_ERR_CLASS_INIT_FAILURE, \ + FX_MSG_CLASS_SPECIFIES_UNKNOWN_INTERFACE, \ + FX_ERROR_PARAM("class_name", fx_class_get_name(p)), \ + FX_ERROR_PARAM("interface_name", #interface_name)); \ + exit(-1); \ + } else { \ + interface_name##_class *iface = __FX_IFACE_I1(iface, __LINE__); +#define FX_TYPE_CLASS_INTERFACE_END(interface_name, interface_id) } +#define FX_INTERFACE_ENTRY(slot) iface->slot + +#define FX_TYPE_DEFINITION_BEGIN(name) \ + static fx_type_info name##_type_info = {0}; \ + static void name##_class_init(fx_class *, void *); \ + static void name##_type_init(void) \ + { \ + fx_type_info *type_info = &name##_type_info; \ + unsigned int nr_vtables = 0; \ + type_info->t_name = #name; \ + type_info->t_class_init = name##_class_init; +#define FX_TYPE_DEFINITION_END(name) \ + fx_result result = fx_type_register(type_info); \ + if (fx_result_is_error(result)) { \ + fx_throw_error_caused_by_error( \ + FX_ERRORS_BUILTIN, FX_ERR_TYPE_REGISTRATION_FAILURE, \ + result); \ + abort(); \ + } \ + } \ + fx_type name##_get_type(void) \ + { \ + static fx_once static_type_init = FX_ONCE_INIT; \ + \ + if (fx_init_once(&static_type_init)) { \ + name##_type_init(); \ + } \ + \ + return &name##_type_info.t_id; \ + } + +#define FX_TYPE_ID(a, b, c, d, e) fx_type_id_init(&type_info->t_id, a, b, c, d, e) +#define FX_TYPE_EXTENDS(parent_id) \ + fx_type_id_copy(parent_id, &type_info->t_parent_id) +#define FX_TYPE_IMPLEMENTS(interface_id) \ + fx_type_id_copy( \ + interface_id, \ + &type_info->t_interfaces[type_info->t_nr_interfaces++]) +#define FX_TYPE_CLASS(class_struct) \ + type_info->t_class_size = sizeof(class_struct) +#define FX_TYPE_FLAGS(flags) type_info->t_flags = (flags) +#define FX_TYPE_INSTANCE_INIT(func) type_info->t_instance_init = (func) +#define FX_TYPE_INSTANCE_FINI(func) type_info->t_instance_fini = (func) + +#if 0 +#define FX_TYPE_VTABLE_BEGIN(vtable_struct, interface_id) \ + vtable_struct __FX_IFACE_I1(iface, __LINE__) = {0}; \ + { \ + vtable_struct *iface = &__FX_IFACE_I1(iface, __LINE__); \ + type_info->t_vtables[nr_vtables].v_vtable = iface; \ + type_info->t_vtables[nr_vtables].v_interface_id = interface_id; \ + nr_vtables++; +#define FX_TYPE_VTABLE_END(vtable_struct, interface_id) } +#endif + +#define FX_TYPE_INSTANCE_PRIVATE(instance_struct) \ + type_info->t_instance_private_size = sizeof(instance_struct) +#define FX_TYPE_INSTANCE_PROTECTED(instance_struct) \ + type_info->t_instance_protected_size = sizeof(instance_struct) + +/* Type declaration macros (for use in .h header file) */ + +#define __FX_DECLARE_TYPE(name) \ + typedef FX_TYPE_FWDREF(name) name; \ + typedef struct _##name##_class name##_class; + +#define FX_DECLARE_TYPE(name) \ + __FX_DECLARE_TYPE(name); \ + static inline name *name##_ref(name *p) \ + { \ + return fx_object_ref(p); \ + } \ + static inline void name##_unref(name *p) \ + { \ + fx_object_unref(p); \ + } + +#define FX_TYPE_CLASS_DECLARATION_BEGIN(name) struct _##name##_class { +#define FX_TYPE_CLASS_DECLARATION_END(name) \ + } \ + ; + +#define FX_TYPE_VIRTUAL_METHOD(return_type, method_name) \ + return_type(*method_name) + +#define FX_TYPE_DEFAULT_CONSTRUCTOR(type_name, type_id) \ + static inline type_name *type_name##_create(void) \ + { \ + return fx_object_create(type_id); \ + } + +/* Other macros */ + +#define FX_CLASS_DISPATCH_VIRTUAL( \ + type_name, type_id, default_value, func, object, ...) \ + do { \ + type_name##_class *iface \ + = fx_object_get_interface(object, type_id); \ + if (iface && iface->func) { \ + return iface->func(object, __VA_ARGS__); \ + } else { \ + return default_value; \ + } \ + } while (0) +#define FX_CLASS_DISPATCH_VIRTUAL_0(type_name, type_id, default_value, func, object) \ + do { \ + type_name##_class *iface \ + = fx_object_get_interface(object, type_id); \ + if (iface && iface->func) { \ + return iface->func(object); \ + } else { \ + return default_value; \ + } \ + } while (0) +#define FX_CLASS_DISPATCH_VIRTUAL_V(type_name, type_id, func, object, ...) \ + do { \ + type_name##_class *iface \ + = fx_object_get_interface(object, type_id); \ + if (iface && iface->func) { \ + iface->func(object, __VA_ARGS__); \ + return; \ + } \ + } while (0) +#define FX_CLASS_DISPATCH_VIRTUAL_V0(type_name, type_id, func, object) \ + do { \ + type_name##_class *iface \ + = fx_object_get_interface(object, type_id); \ + if (iface && iface->func) { \ + iface->func(object); \ + return; \ + } \ + } while (0) + +#define FX_CLASS_DISPATCH_STATIC(type_id, func_name, obj, ...) \ + do { \ + void *priv = fx_object_get_private(obj, type_id); \ + return func_name(priv, __VA_ARGS__); \ + } while (0) +#define FX_CLASS_DISPATCH_STATIC_V(type_id, func_name, obj, ...) \ + do { \ + void *priv = fx_object_get_private(obj, type_id); \ + func_name(priv, __VA_ARGS__); \ + } while (0) + +#define FX_CLASS_DISPATCH_STATIC_0(type_id, func_name, obj) \ + do { \ + void *priv = fx_object_get_private(obj, type_id); \ + return func_name(priv); \ + } while (0) + +#define FX_CLASS_DISPATCH_STATIC_V0(type_id, func_name, obj) \ + do { \ + void *priv = fx_object_get_private(obj, type_id); \ + func_name(priv); \ + } while (0) + +#ifdef __cplusplus +#define FX_DECLS_BEGIN extern "C" { +#define FX_DECLS_END } +#else +#define FX_DECLS_BEGIN +#define FX_DECLS_END +#endif + +#endif diff --git a/core/include/fx/core/misc.h b/core/include/fx/core/misc.h new file mode 100644 index 0000000..aaa4753 --- /dev/null +++ b/core/include/fx/core/misc.h @@ -0,0 +1,118 @@ +#ifndef FX_CORE_MISC_H_ +#define FX_CORE_MISC_H_ + +#include +#include + +#ifndef _Nonnull +#define _Nonnull +#endif + +#define FX_NPOS ((size_t)-1) + +#define fx_min(type, x, y) (z__fx_min_##type(x, y)) +#define fx_max(type, x, y) (z__fx_max_##type(x, y)) + +#define fx_unbox(type, box, member) \ + ((type *_Nonnull)((box) ? (uintptr_t)(box) - (offsetof(type, member)) : 0)) + +#define z__fx_merge_(a, b) a##b +#define z__fx_label_(a) z__fx_merge_(__unique_name_, a) +#define z__fx_unique_name() z__fx_label_(__LINE__) +#define z__fx_numargs(arg_type, ...) \ + (sizeof((arg_type[]) {__VA_ARGS__}) / sizeof(arg_type)) + +#ifdef _MSC_VER +#ifdef FX_STATIC +#define FX_API extern +#else +#ifdef FX_EXPORT +#define FX_API extern __declspec(dllexport) +#else +#define FX_API extern __declspec(dllimport) +#endif +#endif +#else +#define FX_API extern +#endif + +static inline char z__fx_min_char(char x, char y) +{ + return x < y ? x : y; +} +static inline unsigned char z__fx_min_uchar(unsigned char x, unsigned char y) +{ + return x < y ? x : y; +} +static inline int z__fx_min_int(int x, int y) +{ + return x < y ? x : y; +} +static inline unsigned int z__fx_min_uint(unsigned int x, unsigned int y) +{ + return x < y ? x : y; +} +static inline long z__fx_min_long(long x, long y) +{ + return x < y ? x : y; +} +static inline unsigned int z__fx_min_ulong(unsigned long x, unsigned long y) +{ + return x < y ? x : y; +} +static inline long long z__fx_min_longlong(long long x, long long y) +{ + return x < y ? x : y; +} +static inline unsigned long long z__fx_min_ulonglong( + unsigned long long x, unsigned long long y) +{ + return x < y ? x : y; +} +static inline size_t z__fx_min_size_t(size_t x, size_t y) +{ + return x < y ? x : y; +} + +static inline char z__fx_max_char(char x, char y) +{ + return x > y ? x : y; +} +static inline unsigned char z__fx_max_uchar(unsigned char x, unsigned char y) +{ + return x > y ? x : y; +} +static inline int z__fx_max_int(int x, int y) +{ + return x > y ? x : y; +} +static inline unsigned int z__fx_max_uint(unsigned int x, unsigned int y) +{ + return x > y ? x : y; +} +static inline long z__fx_max_long(long x, long y) +{ + return x > y ? x : y; +} +static inline unsigned int z__fx_max_ulong(unsigned long x, unsigned long y) +{ + return x > y ? x : y; +} +static inline long long z__fx_max_longlong(long long x, long long y) +{ + return x > y ? x : y; +} +static inline unsigned long long z__fx_max_ulonglong( + unsigned long long x, unsigned long long y) +{ + return x > y ? x : y; +} +static inline size_t z__fx_max_size_t(size_t x, size_t y) +{ + return x > y ? x : y; +} + +FX_API size_t fx_int_length(intptr_t v); +FX_API size_t fx_uint_length(uintptr_t v); + +#endif // FX_CORE_MISC_H_ diff --git a/core/include/fx/core/object.h b/core/include/fx/core/object.h new file mode 100644 index 0000000..6705021 --- /dev/null +++ b/core/include/fx/core/object.h @@ -0,0 +1,39 @@ +#ifndef FX_CORE_OBJECT_H_ +#define FX_CORE_OBJECT_H_ + +#include +#include + +#define FX_OBJECT_MAGIC 0xDECAFC0C0ABEEF13ULL + +#define FX_OBJECT(p) ((fx_object *)(p)) +#define FX_TYPE_OBJECT (fx_object_get_type()) + +#define FX_TYPE_FWDREF(name) struct _fx_object + +#define FX_RV(p) (fx_object_make_rvalue(p)) + +typedef FX_TYPE_FWDREF(fx_object) fx_object; + +typedef struct _fx_object_class { + void (*to_string)(const fx_object *, FX_TYPE_FWDREF(fx_stream) *); +} fx_object_class; + +FX_API fx_type fx_object_get_type(void); + +FX_API void *fx_object_get_private(const fx_object *object, fx_type type); +FX_API void *fx_object_get_protected(const fx_object *object, fx_type type); +FX_API void *fx_object_get_interface(const fx_object *object, fx_type type); +FX_API fx_status fx_object_get_data( + const fx_object *object, fx_type type, void **priv, void **prot, + void **iface); + +FX_API fx_object *fx_object_ref(fx_object *p); +FX_API void fx_object_unref(fx_object *p); +FX_API fx_object *fx_object_make_rvalue(fx_object *p); + +FX_API fx_object *fx_object_create(fx_type type); +FX_API void fx_object_to_string(const fx_object *p, FX_TYPE_FWDREF(fx_stream) * out); +FX_API bool fx_object_is_type(const fx_object *p, fx_type type); + +#endif diff --git a/core/include/fx/core/queue.h b/core/include/fx/core/queue.h new file mode 100644 index 0000000..2ebbed3 --- /dev/null +++ b/core/include/fx/core/queue.h @@ -0,0 +1,82 @@ +#ifndef FX_CORE_QUEUE_H_ +#define FX_CORE_QUEUE_H_ + +#include +#include +#include +#include +#include + +FX_DECLS_BEGIN; + +#define FX_TYPE_QUEUE_ITERATOR (fx_queue_iterator_get_type()) + +FX_DECLARE_TYPE(fx_queue_iterator); + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_queue_iterator) +FX_TYPE_CLASS_DECLARATION_END(fx_queue_iterator) + +#define FX_QUEUE_INIT ((fx_queue) {.q_first = NULL, .q_last = NULL}) +#define FX_QUEUE_ENTRY_INIT ((fx_queue_entry) {.qe_next = NULL, .qe_prev = NULL}) + +typedef struct fx_queue_entry { + struct fx_queue_entry *qe_next; + struct fx_queue_entry *qe_prev; +} fx_queue_entry; + +typedef struct fx_queue { + fx_queue_entry *q_first; + fx_queue_entry *q_last; +} fx_queue; + +static inline void fx_queue_init(fx_queue *q) +{ + memset(q, 0x00, sizeof *q); +} +static inline bool fx_queue_empty(const fx_queue *q) +{ + return q ? (q->q_first == NULL) : true; +} + +static inline fx_queue_entry *fx_queue_first(const fx_queue *q) +{ + return q ? q->q_first : NULL; +} +static inline fx_queue_entry *fx_queue_last(const fx_queue *q) +{ + return q ? q->q_last : NULL; +} +static inline fx_queue_entry *fx_queue_next(const fx_queue_entry *entry) +{ + return entry ? entry->qe_next : NULL; +} +static inline fx_queue_entry *fx_queue_prev(const fx_queue_entry *entry) +{ + return entry ? entry->qe_prev : NULL; +} + +FX_API fx_type fx_queue_iterator_get_type(void); + +FX_API size_t fx_queue_length(const fx_queue *q); + +FX_API void fx_queue_insert_before( + fx_queue *q, fx_queue_entry *entry, fx_queue_entry *before); +FX_API void fx_queue_insert_after( + fx_queue *q, fx_queue_entry *entry, fx_queue_entry *after); + +FX_API void fx_queue_push_front(fx_queue *q, fx_queue_entry *entry); +FX_API void fx_queue_push_back(fx_queue *q, fx_queue_entry *entry); + +FX_API fx_queue_entry *fx_queue_pop_front(fx_queue *q); +FX_API fx_queue_entry *fx_queue_pop_back(fx_queue *q); + +FX_API void fx_queue_move(fx_queue *q, fx_queue_entry *dest, fx_queue_entry *src); +FX_API void fx_queue_delete(fx_queue *q, fx_queue_entry *entry); +FX_API void fx_queue_delete_all(fx_queue *q); + +FX_API fx_iterator *fx_queue_begin(fx_queue *q); +FX_API fx_iterator *fx_queue_cbegin(const fx_queue *q); + +FX_DECLS_END; + +#endif diff --git a/core/include/fx/core/random.h b/core/include/fx/core/random.h new file mode 100644 index 0000000..583312e --- /dev/null +++ b/core/include/fx/core/random.h @@ -0,0 +1,37 @@ +#ifndef FX_RANDOM_H_ +#define FX_RANDOM_H_ + +#include +#include + +struct fx_random_algorithm; + +typedef enum fx_random_flags { + /* algorithm selection */ + FX_RANDOM_MT19937 = 0x01u, + + /* generation flags */ + FX_RANDOM_SECURE = 0x100u, +} fx_random_flags; + +typedef struct fx_random_ctx { + fx_random_flags __f; + struct fx_random_algorithm *__a; + + union { + struct { + unsigned long long mt[312]; + size_t mti; + } __mt19937; + }; +} fx_random_ctx; + +FX_API fx_random_ctx *fx_random_global_ctx(void); + +FX_API fx_status fx_random_init(fx_random_ctx *ctx, fx_random_flags flags); +FX_API unsigned long long fx_random_next_int64(fx_random_ctx *ctx); +FX_API double fx_random_next_double(fx_random_ctx *ctx); +FX_API void fx_random_next_bytes( + fx_random_ctx *ctx, unsigned char *out, size_t nbytes); + +#endif diff --git a/core/include/fx/core/ringbuffer.h b/core/include/fx/core/ringbuffer.h new file mode 100644 index 0000000..1c6fe49 --- /dev/null +++ b/core/include/fx/core/ringbuffer.h @@ -0,0 +1,47 @@ +#ifndef FX_CORE_RINGBUFFER_H_ +#define FX_CORE_RINGBUFFER_H_ + +#include +#include +#include + +FX_DECLS_BEGIN; + +#define FX_TYPE_RINGBUFFER (fx_ringbuffer_get_type()) + +FX_DECLARE_TYPE(fx_ringbuffer); + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_ringbuffer) +FX_TYPE_CLASS_DECLARATION_END(fx_ringbuffer) + +FX_API fx_type fx_ringbuffer_get_type(void); + +FX_API fx_ringbuffer *fx_ringbuffer_create(size_t capacity); +FX_API fx_ringbuffer *fx_ringbuffer_create_with_buffer(void *ptr, size_t capacity); + +FX_API fx_status fx_ringbuffer_clear(fx_ringbuffer *buf); + +FX_API fx_status fx_ringbuffer_read( + fx_ringbuffer *buf, void *p, size_t count, size_t *nr_read); +FX_API fx_status fx_ringbuffer_write( + fx_ringbuffer *buf, const void *p, size_t count, size_t *nr_written); + +FX_API int fx_ringbuffer_getc(fx_ringbuffer *buf); +FX_API fx_status fx_ringbuffer_putc(fx_ringbuffer *buf, int c); + +FX_API size_t fx_ringbuffer_write_capacity_remaining(const fx_ringbuffer *buf); +FX_API size_t fx_ringbuffer_available_data_remaining(const fx_ringbuffer *buf); + +FX_API fx_status fx_ringbuffer_open_read_buffer( + fx_ringbuffer *buf, const void **ptr, size_t *length); +FX_API fx_status fx_ringbuffer_close_read_buffer( + fx_ringbuffer *buf, const void **ptr, size_t nr_read); + +FX_API fx_status fx_ringbuffer_open_write_buffer( + fx_ringbuffer *buf, void **ptr, size_t *capacity); +FX_API fx_status fx_ringbuffer_close_write_buffer( + fx_ringbuffer *buf, void **ptr, size_t nr_written); + +FX_DECLS_END; + +#endif diff --git a/core/include/fx/core/rope.h b/core/include/fx/core/rope.h new file mode 100644 index 0000000..387fbd5 --- /dev/null +++ b/core/include/fx/core/rope.h @@ -0,0 +1,109 @@ +#ifndef FX_CORE_ROPE_H_ +#define FX_CORE_ROPE_H_ + +#include +#include +#include +#include +#include + +struct fx_string; +struct fx_bstr; + +#define FX_ROPE_TYPE(f) ((f) & 0xFF) + +#define FX_ROPE_CHAR(c) \ + \ + { \ + .r_flags = FX_ROPE_F_CHAR, .r_len_total = 1, \ + .r_v = {.v_char = (c) } \ + } + +#define FX_ROPE_CSTR(str) \ + { \ + .r_flags = FX_ROPE_F_CSTR_BORROWED, \ + .r_len_total = strlen(str), \ + .r_v = { \ + .v_cstr = { \ + .s = (str), \ + .hash = fx_hash_cstr(str), \ + }, \ + }, \ + } + +#define FX_ROPE_CSTR_STATIC(str) \ + { \ + .r_flags = FX_ROPE_F_CSTR_STATIC, \ + .r_len_total = strlen(str), \ + .r_v = { \ + .v_cstr = { \ + .s = (str), \ + .hash = fx_hash_cstr(str), \ + }, \ + }, \ + } +#define FX_ROPE_INT(v) \ + \ + { \ + .r_flags = FX_ROPE_F_INT, .r_len_total = fx_int_length(v), \ + .r_v = {.v_int = (v) } \ + } +#define FX_ROPE_UINT(v) \ + \ + { \ + .r_flags = FX_ROPE_F_UINT, .r_len_total = fx_uint_length(v), \ + .r_v = {.v_uint = (v) } \ + } + +typedef enum fx_rope_flags { + FX_ROPE_F_NONE = 0x0000u, + FX_ROPE_F_CHAR = 0x0001u, + FX_ROPE_F_CSTR = 0x0002u, + FX_ROPE_F_CSTR_BORROWED = 0x0003u, + FX_ROPE_F_CSTR_STATIC = 0x0004u, + FX_ROPE_F_INT = 0x0005u, + FX_ROPE_F_UINT = 0x0006u, + FX_ROPE_F_COMPOSITE = 0x0007u, + FX_ROPE_F_MALLOC = 0x0100u, +} fx_rope_flags; + +typedef struct fx_rope { + fx_rope_flags r_flags; + unsigned long r_len_left, r_len_total; + + union { + char v_char; + intptr_t v_int; + uintptr_t v_uint; + + struct { + const char *s; + uint64_t hash; + } v_cstr; + + struct { + const struct fx_rope *r_left, *r_right; + } v_composite; + } r_v; +} fx_rope; + +FX_API void fx_rope_init_char(fx_rope *rope, char c); +FX_API void fx_rope_init_cstr(fx_rope *rope, const char *s); +FX_API void fx_rope_init_cstr_borrowed(fx_rope *rope, const char *s); +FX_API void fx_rope_init_cstr_static(fx_rope *rope, const char *s); +FX_API void fx_rope_init_int(fx_rope *rope, intptr_t v); +FX_API void fx_rope_init_uint(fx_rope *rope, uintptr_t v); + +FX_API void fx_rope_destroy(fx_rope *rope); + +FX_API void fx_rope_iterate( + const fx_rope *rope, void (*func)(const fx_rope *, void *), void *arg); +FX_API size_t fx_rope_get_size(const fx_rope *rope); +FX_API void fx_rope_concat(fx_rope *result, const fx_rope *left, const fx_rope *right); +FX_API void fx_rope_join(fx_rope *result, const fx_rope **ropes, size_t nr_ropes); + +FX_API fx_status fx_rope_to_cstr(const fx_rope *rope, char *out, size_t max); +FX_API fx_status fx_rope_to_bstr(const fx_rope *rope, struct fx_bstr *str); +FX_API fx_status fx_rope_to_string(const fx_rope *rope, fx_stream *out); + +#endif diff --git a/core/include/fx/core/status.h b/core/include/fx/core/status.h new file mode 100644 index 0000000..b5368c2 --- /dev/null +++ b/core/include/fx/core/status.h @@ -0,0 +1,48 @@ +#ifndef FX_CORE_STATUS_H_ +#define FX_CORE_STATUS_H_ + +#include + +#define FX_OK(status) ((enum fx_status)((uintptr_t)(status)) == FX_SUCCESS) +#define FX_ERR(status) ((status) != FX_SUCCESS) + +typedef enum fx_status { + FX_SUCCESS = 0x00u, + FX_ERR_NO_MEMORY, + FX_ERR_OUT_OF_BOUNDS, + FX_ERR_INVALID_ARGUMENT, + FX_ERR_NAME_EXISTS, + FX_ERR_NOT_SUPPORTED, + FX_ERR_BAD_STATE, + FX_ERR_NO_ENTRY, + FX_ERR_NO_DATA, + FX_ERR_NO_SPACE, + FX_ERR_UNKNOWN_FUNCTION, + FX_ERR_BAD_FORMAT, + FX_ERR_IO_FAILURE, + FX_ERR_IS_DIRECTORY, + FX_ERR_NOT_DIRECTORY, + FX_ERR_PERMISSION_DENIED, + FX_ERR_BUSY, + + /* fx-compress specific code */ + FX_ERR_COMPRESSION_FAILURE, + + /* fx-object specific code */ + FX_ERR_TYPE_REGISTRATION_FAILURE, + FX_ERR_CLASS_INIT_FAILURE, +} fx_status; + +typedef enum fx_status_msg { + FX_MSG_SUCCESS = 0, + + /* fx-object specific messages */ + FX_MSG_TYPE_REGISTRATION_FAILURE, + FX_MSG_CLASS_INIT_FAILURE, + FX_MSG_CLASS_SPECIFIES_UNKNOWN_INTERFACE, +} fx_status_msg; + +FX_API const char *fx_status_to_string(fx_status status); +FX_API const char *fx_status_description(fx_status status); + +#endif diff --git a/core/include/fx/core/stream.h b/core/include/fx/core/stream.h new file mode 100644 index 0000000..3f51883 --- /dev/null +++ b/core/include/fx/core/stream.h @@ -0,0 +1,100 @@ +#ifndef FX_CORE_STREAM_H_ +#define FX_CORE_STREAM_H_ + +#include +#include +#include +#include +#include +#include + +FX_DECLS_BEGIN; + +#define fx_stdin (z__fx_stream_get_stdin()) +#define fx_stdout (z__fx_stream_get_stdout()) +#define fx_stderr (z__fx_stream_get_stderr()) + +#define FX_TYPE_STREAM (fx_stream_get_type()) +#define FX_TYPE_STREAM_BUFFER (fx_stream_buffer_get_type()) + +FX_DECLARE_TYPE(fx_stream); +FX_DECLARE_TYPE(fx_stream_buffer); + +typedef enum fx_stream_mode { + FX_STREAM_READ = 0x01u, + FX_STREAM_WRITE = 0x02u, + FX_STREAM_BINARY = 0x10u, + Z__FX_STREAM_STATIC = 0x80u, +} fx_stream_mode; + +typedef enum fx_stream_seek_origin { + FX_STREAM_SEEK_START = 0x01u, + FX_STREAM_SEEK_CURRENT = 0x02u, + FX_STREAM_SEEK_END = 0x03u, +} fx_stream_seek_origin; + +typedef struct fx_stream_cfg { + fx_stream_mode s_mode; +} fx_stream_cfg; + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_stream) + fx_status (*s_close)(fx_stream *); + fx_status (*s_seek)(fx_stream *, long long, fx_stream_seek_origin); + fx_status (*s_tell)(const fx_stream *, size_t *); + fx_status (*s_getc)(fx_stream *, fx_wchar *); + fx_status (*s_read)(fx_stream *, void *, size_t, size_t *); + fx_status (*s_write)(fx_stream *, const void *, size_t, size_t *); + fx_status (*s_reserve)(fx_stream *, size_t); +FX_TYPE_CLASS_DECLARATION_END(fx_stream) + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_stream_buffer) +FX_TYPE_CLASS_DECLARATION_END(fx_stream_buffer) + +FX_API fx_type fx_stream_get_type(); +FX_API fx_type fx_stream_buffer_get_type(); + +FX_API fx_stream *z__fx_stream_get_stdin(void); +FX_API fx_stream *z__fx_stream_get_stdout(void); +FX_API fx_stream *z__fx_stream_get_stderr(void); + +FX_API fx_stream_buffer *fx_stream_buffer_create(void *p, size_t len); +FX_API fx_stream_buffer *fx_stream_buffer_create_dynamic(size_t buffer_size); + +FX_API fx_stream *fx_stream_open_fp(FILE *fp); + +FX_API fx_status fx_stream_reserve(fx_stream *stream, size_t len); +FX_API fx_status fx_stream_seek( + fx_stream *stream, long long offset, fx_stream_seek_origin origin); +FX_API size_t fx_stream_cursor(const fx_stream *stream); + +FX_API fx_status fx_stream_push_indent(fx_stream *stream, int indent); +FX_API fx_status fx_stream_pop_indent(fx_stream *stream); + +FX_API fx_status fx_stream_read_char(fx_stream *stream, fx_wchar *c); + +FX_API fx_status fx_stream_read_bytes( + fx_stream *stream, void *buf, size_t count, size_t *nr_read); + +FX_API fx_status fx_stream_read_line(fx_stream *stream, char *s, size_t max); +FX_API fx_status fx_stream_read_line_s(fx_stream *src, fx_stream *dest); + +FX_API fx_status fx_stream_read_all_bytes( + fx_stream *stream, void *p, size_t max, size_t *nr_read); +FX_API fx_status fx_stream_read_all_bytes_s( + fx_stream *src, fx_stream *dest, fx_stream_buffer *buffer, size_t *nr_read); + +FX_API fx_status fx_stream_write_char(fx_stream *stream, fx_wchar c); +FX_API fx_status fx_stream_write_string( + fx_stream *stream, const char *s, size_t *nr_written); + +FX_API fx_status fx_stream_write_bytes( + fx_stream *stream, const void *buf, size_t count, size_t *nr_written); + +FX_API fx_status fx_stream_write_fmt( + fx_stream *stream, size_t *nr_written, const char *format, ...); +FX_API fx_status fx_stream_write_vfmt( + fx_stream *stream, size_t *nr_written, const char *format, va_list arg); + +FX_DECLS_END; + +#endif diff --git a/core/include/fx/core/stringstream.h b/core/include/fx/core/stringstream.h new file mode 100644 index 0000000..bb66843 --- /dev/null +++ b/core/include/fx/core/stringstream.h @@ -0,0 +1,35 @@ +#ifndef FX_CORE_STRINGSTREAM_H_ +#define FX_CORE_STRINGSTREAM_H_ + +#include +#include +#include +#include +#include + +FX_DECLS_BEGIN; + +#define FX_TYPE_STRINGSTREAM (fx_stringstream_get_type()) + +FX_DECLARE_TYPE(fx_stringstream); + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_stringstream) +FX_TYPE_CLASS_DECLARATION_END(fx_stringstream) + +FX_API fx_type fx_stringstream_get_type(void); + +FX_API fx_stringstream *fx_stringstream_create(void); +FX_API fx_stringstream *fx_stringstream_create_with_buffer(char *buf, size_t max); + +FX_API fx_status fx_stringstream_reset(fx_stringstream *strv); +FX_API fx_status fx_stringstream_reset_with_buffer( + fx_stringstream *strv, char *buf, size_t max); + +FX_API const char *fx_stringstream_ptr(const fx_stringstream *strv); +FX_API char *fx_stringstream_steal(fx_stringstream *strv); + +FX_API size_t fx_stringstream_get_length(const fx_stringstream *strv); + +FX_DECLS_END; + +#endif diff --git a/core/include/fx/core/thread.h b/core/include/fx/core/thread.h new file mode 100644 index 0000000..cf5cab8 --- /dev/null +++ b/core/include/fx/core/thread.h @@ -0,0 +1,36 @@ +#ifndef FX_CORE_THREAD_H_ +#define FX_CORE_THREAD_H_ + +#include +#include +#include + +#if defined(__APPLE__) || defined(__linux__) +#include + +#define FX_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER + +typedef pthread_mutex_t fx_mutex; +#else +#error Unsupported compiler/system +#endif + +#define FX_ONCE_INIT ((fx_once)0) + +typedef struct fx_thread fx_thread; + +typedef int fx_once; + +static inline bool fx_init_once(fx_once *once) +{ + int x = 0; + return fx_cmpxchg(once, &x, 1); +} + +FX_API fx_thread *fx_thread_self(void); + +FX_API bool fx_mutex_lock(fx_mutex *mut); +FX_API bool fx_mutex_trylock(fx_mutex *mut); +FX_API bool fx_mutex_unlock(fx_mutex *mut); + +#endif diff --git a/core/include/fx/core/type.h b/core/include/fx/core/type.h new file mode 100644 index 0000000..b33efc2 --- /dev/null +++ b/core/include/fx/core/type.h @@ -0,0 +1,66 @@ +#ifndef FX_CORE_TYPE_H_ +#define FX_CORE_TYPE_H_ + +#include +#include +#include +#include +#include + +#define FX_TYPE_MAX_INTERFACES 64 + +struct _fx_class; +struct _fx_object; + +typedef void (*fx_class_init_function)(struct _fx_class *, void *); +typedef void (*fx_instance_init_function)(struct _fx_object *, void *); +typedef void (*fx_instance_fini_function)(struct _fx_object *, void *); + +typedef const union fx_type { + struct { + uint64_t p00, p01; + } a; + + unsigned char b[16]; +} *fx_type; + +typedef enum fx_type_flags { + FX_TYPE_F_ABSTRACT = 0x01u, +} fx_type_flags; + +typedef struct fx_type_info { + union fx_type t_id; + union fx_type t_parent_id; + const char *t_name; + fx_type_flags t_flags; + union fx_type t_interfaces[FX_TYPE_MAX_INTERFACES]; + size_t t_nr_interfaces; + size_t t_class_size; + fx_class_init_function t_class_init; + size_t t_instance_private_size; + size_t t_instance_protected_size; + fx_instance_init_function t_instance_init; + fx_instance_fini_function t_instance_fini; +} fx_type_info; + +FX_API void fx_type_id_init( + union fx_type *out, uint32_t a, uint16_t b, uint16_t c, uint16_t d, + uint64_t e); +static inline void fx_type_id_copy(fx_type src, union fx_type *dest) +{ + dest->a.p00 = src->a.p00; + dest->a.p01 = src->a.p01; +} + +static inline int fx_type_id_compare(fx_type a, fx_type b) +{ + if (a == b) { + return 0; + } + + return memcmp(a, b, sizeof(union fx_type)); +} + +FX_API fx_result fx_type_register(fx_type_info *info); + +#endif diff --git a/core/iterator.c b/core/iterator.c index d311fe8..2282694 100644 --- a/core/iterator.c +++ b/core/iterator.c @@ -1,85 +1,85 @@ -#include +#include /*** PRIVATE DATA *************************************************************/ -struct b_iterator_p { - enum b_status it_status; +struct fx_iterator_p { + enum fx_status it_status; }; /*** PRIVATE FUNCTIONS ********************************************************/ -static enum b_status iterator_get_status(const struct b_iterator_p *it) +static enum fx_status iterator_get_status(const struct fx_iterator_p *it) { return it->it_status; } -static enum b_status iterator_set_status(struct b_iterator_p *it, b_status status) +static enum fx_status iterator_set_status(struct fx_iterator_p *it, fx_status status) { it->it_status = status; - return B_SUCCESS; + return FX_SUCCESS; } /*** PUBLIC FUNCTIONS *********************************************************/ -b_iterator *b_iterator_begin(b_iterable *it) +fx_iterator *fx_iterator_begin(fx_iterable *it) { - B_CLASS_DISPATCH_VIRTUAL_0(b_iterable, B_TYPE_ITERABLE, NULL, it_begin, it); + FX_CLASS_DISPATCH_VIRTUAL_0(fx_iterable, FX_TYPE_ITERABLE, NULL, it_begin, it); } -const b_iterator *b_iterator_cbegin(const b_iterable *it) +const fx_iterator *fx_iterator_cbegin(const fx_iterable *it) { - B_CLASS_DISPATCH_VIRTUAL_0(b_iterable, B_TYPE_ITERABLE, NULL, it_cbegin, it); + FX_CLASS_DISPATCH_VIRTUAL_0(fx_iterable, FX_TYPE_ITERABLE, NULL, it_cbegin, it); } -enum b_status b_iterator_get_status(const b_iterator *it) +enum fx_status fx_iterator_get_status(const fx_iterator *it) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_ITERATOR, iterator_get_status, it); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ITERATOR, iterator_get_status, it); } -enum b_status b_iterator_set_status(const b_iterator *it, b_status status) +enum fx_status fx_iterator_set_status(const fx_iterator *it, fx_status status) { - B_CLASS_DISPATCH_STATIC(B_TYPE_ITERATOR, iterator_set_status, it, status); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_ITERATOR, iterator_set_status, it, status); } -enum b_status b_iterator_move_next(const b_iterator *it) +enum fx_status fx_iterator_move_next(const fx_iterator *it) { - enum b_status status = B_ERR_NOT_SUPPORTED; + enum fx_status status = FX_ERR_NOT_SUPPORTED; - b_iterator_class *iface = b_object_get_interface(it, B_TYPE_ITERATOR); + fx_iterator_class *iface = fx_object_get_interface(it, FX_TYPE_ITERATOR); if (iface && iface->it_move_next) { status = iface->it_move_next(it); } - struct b_iterator_p *p = b_object_get_private(it, B_TYPE_ITERATOR); + struct fx_iterator_p *p = fx_object_get_private(it, FX_TYPE_ITERATOR); p->it_status = status; return status; } -b_iterator_value b_iterator_get_value(b_iterator *it) +fx_iterator_value fx_iterator_get_value(fx_iterator *it) { - B_CLASS_DISPATCH_VIRTUAL_0( - b_iterator, B_TYPE_ITERATOR, B_ITERATOR_VALUE_NULL, + FX_CLASS_DISPATCH_VIRTUAL_0( + fx_iterator, FX_TYPE_ITERATOR, FX_ITERATOR_VALUE_NULL, it_get_value, it); } -const b_iterator_value b_iterator_get_cvalue(const b_iterator *it) +const fx_iterator_value fx_iterator_get_cvalue(const fx_iterator *it) { - B_CLASS_DISPATCH_VIRTUAL_0( - b_iterator, B_TYPE_ITERATOR, B_ITERATOR_VALUE_NULL, + FX_CLASS_DISPATCH_VIRTUAL_0( + fx_iterator, FX_TYPE_ITERATOR, FX_ITERATOR_VALUE_NULL, it_get_cvalue, it); } -b_status b_iterator_erase(b_iterator *it) +fx_status fx_iterator_erase(fx_iterator *it) { - enum b_status status = B_ERR_NOT_SUPPORTED; + enum fx_status status = FX_ERR_NOT_SUPPORTED; - b_iterator_class *iface = b_object_get_interface(it, B_TYPE_ITERATOR); + fx_iterator_class *iface = fx_object_get_interface(it, FX_TYPE_ITERATOR); if (iface && iface->it_erase) { status = iface->it_erase(it); } - struct b_iterator_p *p = b_object_get_private(it, B_TYPE_ITERATOR); + struct fx_iterator_p *p = fx_object_get_private(it, FX_TYPE_ITERATOR); p->it_status = status; return status; @@ -87,29 +87,29 @@ b_status b_iterator_erase(b_iterator *it) /*** CLASS DEFINITION *********************************************************/ -// ---- b_iterator DEFINITION -B_TYPE_CLASS_DEFINITION_BEGIN(b_iterator) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) -B_TYPE_CLASS_DEFINITION_END(b_iterator) +// ---- fx_iterator DEFINITION +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_iterator) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_DEFINITION_END(fx_iterator) -B_TYPE_DEFINITION_BEGIN(b_iterator) - B_TYPE_FLAGS(B_TYPE_F_ABSTRACT); - B_TYPE_ID(0xfd40b67f, 0x7087, 0x40a9, 0x8fd8, 0x8ae27bd58c9e); - B_TYPE_CLASS(b_iterator_class); - B_TYPE_INSTANCE_PRIVATE(struct b_iterator_p); -B_TYPE_DEFINITION_END(b_iterator) +FX_TYPE_DEFINITION_BEGIN(fx_iterator) + FX_TYPE_FLAGS(FX_TYPE_F_ABSTRACT); + FX_TYPE_ID(0xfd40b67f, 0x7087, 0x40a9, 0x8fd8, 0x8ae27bd58c9e); + FX_TYPE_CLASS(fx_iterator_class); + FX_TYPE_INSTANCE_PRIVATE(struct fx_iterator_p); +FX_TYPE_DEFINITION_END(fx_iterator) -// ---- b_iterable DEFINITION -B_TYPE_CLASS_DEFINITION_BEGIN(b_iterable) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) -B_TYPE_CLASS_DEFINITION_END(b_iterable) +// ---- fx_iterable DEFINITION +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_iterable) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_DEFINITION_END(fx_iterable) -B_TYPE_DEFINITION_BEGIN(b_iterable) - B_TYPE_FLAGS(B_TYPE_F_ABSTRACT); - B_TYPE_ID(0x4bbabf2d, 0xfc5d, 0x40cc, 0x89fc, 0x164085e47f73); - B_TYPE_CLASS(b_iterable_class); -B_TYPE_DEFINITION_END(b_iterable) +FX_TYPE_DEFINITION_BEGIN(fx_iterable) + FX_TYPE_FLAGS(FX_TYPE_F_ABSTRACT); + FX_TYPE_ID(0x4bbabf2d, 0xfc5d, 0x40cc, 0x89fc, 0x164085e47f73); + FX_TYPE_CLASS(fx_iterable_class); +FX_TYPE_DEFINITION_END(fx_iterable) diff --git a/core/misc.c b/core/misc.c index b2294e7..9770067 100644 --- a/core/misc.c +++ b/core/misc.c @@ -1,10 +1,10 @@ -#include +#include #define LENGTH_RET(v, boundary, len) \ if ((v) < (boundary)) \ return (len); -size_t b_int_length(intptr_t v) +size_t fx_int_length(intptr_t v) { size_t len = 0; if (v < 0) { @@ -12,10 +12,10 @@ size_t b_int_length(intptr_t v) len++; } - return len + b_uint_length(v); + return len + fx_uint_length(v); } -size_t b_uint_length(uintptr_t v) +size_t fx_uint_length(uintptr_t v) { LENGTH_RET(v, 10, 1); LENGTH_RET(v, 100, 2); diff --git a/core/mt19937.c b/core/mt19937.c index 3e7523e..8e222ba 100644 --- a/core/mt19937.c +++ b/core/mt19937.c @@ -55,7 +55,7 @@ #include "random.h" -#include +#include #define NN 312 #define MM 156 @@ -64,7 +64,7 @@ #define LM 0x7FFFFFFFULL /* Least significant 31 bits */ /* initializes mt[NN] with a seed */ -static void init_genrand64(struct b_random_ctx *context, unsigned long long seed) +static void init_genrand64(struct fx_random_ctx *context, unsigned long long seed) { context->__mt19937.mt[0] = seed; for (context->__mt19937.mti = 1; context->__mt19937.mti < NN; @@ -82,7 +82,7 @@ static void init_genrand64(struct b_random_ctx *context, unsigned long long seed /* init_key is the array for initializing keys */ /* key_length is its length */ static void init_by_array64( - struct b_random_ctx *context, unsigned long long init_key[], + struct fx_random_ctx *context, unsigned long long init_key[], unsigned long long key_length) { unsigned long long i, j, k; @@ -125,7 +125,7 @@ static void init_by_array64( } /* generates a random number on [0, 2^64-1]-interval */ -static uint64_t genrand64_int64(struct b_random_ctx *context) +static uint64_t genrand64_int64(struct fx_random_ctx *context) { #if 0 /* This is the original implementation. It is replaced by the alternate implementation, below. */ @@ -218,25 +218,25 @@ static uint64_t genrand64_int64(struct b_random_ctx *context) } /* generates a random number on [0,1]-real-interval */ -double genrand64_real1(struct b_random_ctx *context) +double genrand64_real1(struct fx_random_ctx *context) { return (genrand64_int64(context) >> 11) * (1.0 / 9007199254740991.0); } /* generates a random number on [0,1)-real-interval */ -double genrand64_real2(struct b_random_ctx *context) +double genrand64_real2(struct fx_random_ctx *context) { return (genrand64_int64(context) >> 11) * (1.0 / 9007199254740992.0); } /* generates a random number on (0,1)-real-interval */ -double genrand64_real3(struct b_random_ctx *context) +double genrand64_real3(struct fx_random_ctx *context) { return ((genrand64_int64(context) >> 12) + 0.5) * (1.0 / 4503599627370496.0); } -struct b_random_algorithm z__b_gen_mt19937 = { +struct fx_random_algorithm z__fx_gen_mt19937 = { .gen_name = "mt19937", .gen_init = init_genrand64, .gen_getrand = genrand64_int64, diff --git a/core/object.c b/core/object.c index 44ca165..7516796 100644 --- a/core/object.c +++ b/core/object.c @@ -3,42 +3,42 @@ #include "type.h" #include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include -B_TYPE_CLASS_DEFINITION_BEGIN(b_object) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) -B_TYPE_CLASS_DEFINITION_END(b_object) +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_object) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_DEFINITION_END(fx_object) -B_TYPE_DEFINITION_BEGIN(b_object) - B_TYPE_ID(0x45f15a2c, 0x6831, 0x4bef, 0xb350, 0x15c650679211); - B_TYPE_CLASS(b_object_class); -B_TYPE_DEFINITION_END(b_object) +FX_TYPE_DEFINITION_BEGIN(fx_object) + FX_TYPE_ID(0x45f15a2c, 0x6831, 0x4bef, 0xb350, 0x15c650679211); + FX_TYPE_CLASS(fx_object_class); +FX_TYPE_DEFINITION_END(fx_object) -b_result b_object_instantiate( - struct b_type_registration *type, struct _b_object **out_object) +fx_result fx_object_instantiate( + struct fx_type_registration *type, struct _fx_object **out_object) { - struct _b_object *out = malloc(type->r_instance_size); + struct _fx_object *out = malloc(type->r_instance_size); if (!out) { - return B_RESULT_ERR(NO_MEMORY); + return FX_RESULT_ERR(NO_MEMORY); } memset(out, 0x0, type->r_instance_size); - out->obj_magic = B_OBJECT_MAGIC; + out->obj_magic = FX_OBJECT_MAGIC; out->obj_type = type; out->obj_ref = 1; - struct b_queue_entry *entry = b_queue_first(&type->r_class_hierarchy); + struct fx_queue_entry *entry = fx_queue_first(&type->r_class_hierarchy); while (entry) { - struct b_type_component *comp - = b_unbox(struct b_type_component, entry, c_entry); - const struct b_type_info *class_info = comp->c_type->r_info; + struct fx_type_component *comp + = fx_unbox(struct fx_type_component, entry, c_entry); + const struct fx_type_info *class_info = comp->c_type->r_info; void *private_data = (char *)out + comp->c_instance_private_data_offset; @@ -51,62 +51,62 @@ b_result b_object_instantiate( = comp->c_instance_private_data_offset; } - entry = b_queue_next(entry); + entry = fx_queue_next(entry); } *out_object = out; - return B_RESULT_SUCCESS; + return FX_RESULT_SUCCESS; } -struct _b_object *b_object_create(b_type type) +struct _fx_object *fx_object_create(fx_type type) { - struct b_type_registration *type_reg = b_type_get_registration(type); + struct fx_type_registration *type_reg = fx_type_get_registration(type); if (!type_reg) { return NULL; } - struct _b_object *out = NULL; - b_result result = b_object_instantiate(type_reg, &out); - if (b_result_is_error(result)) { - b_error_discard(result); + struct _fx_object *out = NULL; + fx_result result = fx_object_instantiate(type_reg, &out); + if (fx_result_is_error(result)) { + fx_error_discard(result); return NULL; } return out; } -void b_object_to_string(const struct _b_object *p, b_stream *out) +void fx_object_to_string(const struct _fx_object *p, fx_stream *out) { - B_CLASS_DISPATCH_VIRTUAL_V(b_object, B_TYPE_OBJECT, to_string, p, out); - b_stream_write_fmt(out, NULL, "<%s@%p>", p->obj_type->r_info->t_name, p); + FX_CLASS_DISPATCH_VIRTUAL_V(fx_object, FX_TYPE_OBJECT, to_string, p, out); + fx_stream_write_fmt(out, NULL, "<%s@%p>", p->obj_type->r_info->t_name, p); } -bool b_object_is_type(const struct _b_object *p, b_type type) +bool fx_object_is_type(const struct _fx_object *p, fx_type type) { - if (b_type_id_compare(&p->obj_type->r_info->t_id, type) == 0) { + if (fx_type_id_compare(&p->obj_type->r_info->t_id, type) == 0) { return true; } - struct b_type_component *comp - = b_type_get_component(&p->obj_type->r_components, type); + struct fx_type_component *comp + = fx_type_get_component(&p->obj_type->r_components, type); return comp != NULL; } -void *b_object_get_private(const struct _b_object *object, b_type type) +void *fx_object_get_private(const struct _fx_object *object, fx_type type) { if (!object) { return NULL; } - assert(object->obj_magic == B_OBJECT_MAGIC); + assert(object->obj_magic == FX_OBJECT_MAGIC); - if (b_type_id_compare(&object->obj_type->r_info->t_id, type) == 0) { + if (fx_type_id_compare(&object->obj_type->r_info->t_id, type) == 0) { return (char *)object + object->obj_main_priv_offset; } - struct b_type_component *comp - = b_type_get_component(&object->obj_type->r_components, type); + struct fx_type_component *comp + = fx_type_get_component(&object->obj_type->r_components, type); if (!comp) { return NULL; } @@ -114,16 +114,16 @@ void *b_object_get_private(const struct _b_object *object, b_type type) return (char *)object + comp->c_instance_private_data_offset; } -void *b_object_get_protected(const struct _b_object *object, b_type type) +void *fx_object_get_protected(const struct _fx_object *object, fx_type type) { if (!object) { return NULL; } - assert(object->obj_magic == B_OBJECT_MAGIC); + assert(object->obj_magic == FX_OBJECT_MAGIC); - struct b_type_component *comp - = b_type_get_component(&object->obj_type->r_components, type); + struct fx_type_component *comp + = fx_type_get_component(&object->obj_type->r_components, type); if (!comp) { return NULL; } @@ -131,31 +131,31 @@ void *b_object_get_protected(const struct _b_object *object, b_type type) return (char *)object + comp->c_instance_protected_data_offset; } -void *b_object_get_interface(const struct _b_object *object, b_type type) +void *fx_object_get_interface(const struct _fx_object *object, fx_type type) { if (!object) { return NULL; } - assert(object->obj_magic == B_OBJECT_MAGIC); + assert(object->obj_magic == FX_OBJECT_MAGIC); - return b_class_get_interface(object->obj_type->r_class, type); + return fx_class_get_interface(object->obj_type->r_class, type); } -enum b_status b_object_get_data( - const struct _b_object *object, b_type type, void **priv, void **prot, +enum fx_status fx_object_get_data( + const struct _fx_object *object, fx_type type, void **priv, void **prot, void **iface) { if (!object) { - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } - assert(object->obj_magic == B_OBJECT_MAGIC); + assert(object->obj_magic == FX_OBJECT_MAGIC); - struct b_type_component *comp - = b_type_get_component(&object->obj_type->r_components, type); + struct fx_type_component *comp + = fx_type_get_component(&object->obj_type->r_components, type); if (!comp) { - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } if (priv) { @@ -171,16 +171,16 @@ enum b_status b_object_get_data( + comp->c_class_data_offset; } - return B_SUCCESS; + return FX_SUCCESS; } -struct _b_object *b_object_ref(struct _b_object *p) +struct _fx_object *fx_object_ref(struct _fx_object *p) { p->obj_ref++; return p; } -void b_object_unref(struct _b_object *p) +void fx_object_unref(struct _fx_object *p) { if (p->obj_ref > 1) { p->obj_ref--; @@ -188,14 +188,14 @@ void b_object_unref(struct _b_object *p) } p->obj_ref = 0; - const struct b_type_registration *type = p->obj_type; + const struct fx_type_registration *type = p->obj_type; - struct b_queue_entry *cur = b_queue_last(&type->r_class_hierarchy); + struct fx_queue_entry *cur = fx_queue_last(&type->r_class_hierarchy); while (cur) { - struct b_type_component *comp - = b_unbox(struct b_type_component, cur, c_entry); + struct fx_type_component *comp + = fx_unbox(struct fx_type_component, cur, c_entry); - const struct b_type_info *class_info = comp->c_type->r_info; + const struct fx_type_info *class_info = comp->c_type->r_info; void *private_data = (char *)p + comp->c_instance_private_data_offset; @@ -203,7 +203,7 @@ void b_object_unref(struct _b_object *p) class_info->t_instance_fini(p, private_data); } - cur = b_queue_prev(cur); + cur = fx_queue_prev(cur); } p->obj_magic = 0; @@ -212,7 +212,7 @@ void b_object_unref(struct _b_object *p) free(p); } -struct _b_object *b_object_make_rvalue(struct _b_object *p) +struct _fx_object *fx_object_make_rvalue(struct _fx_object *p) { p->obj_ref = 0; return p; diff --git a/core/object.h b/core/object.h index ba7d6ba..622362b 100644 --- a/core/object.h +++ b/core/object.h @@ -1,19 +1,19 @@ #ifndef _OBJECT_H_ #define _OBJECT_H_ -#include -#include +#include +#include #include -struct b_type_registration; +struct fx_type_registration; -struct _b_object { +struct _fx_object { uint64_t obj_magic; - const struct b_type_registration *obj_type; + const struct fx_type_registration *obj_type; unsigned int obj_ref, obj_main_priv_offset; }; -extern b_result b_object_instantiate( - struct b_type_registration *type, struct _b_object **out); +extern fx_result fx_object_instantiate( + struct fx_type_registration *type, struct _fx_object **out); #endif diff --git a/core/printf.c b/core/printf.c index f13a858..4e918e4 100644 --- a/core/printf.c +++ b/core/printf.c @@ -1621,7 +1621,7 @@ static int vsnprintf_impl(output_gadget_t *output, const char *format, va_list a return (int)output->pos; } -int z__b_fctprintf( +int z__fx_fctprintf( void (*out)(char c, void *extra_arg), void *extra_arg, const char *format, va_list arg) { diff --git a/core/printf.h b/core/printf.h index 59d92cf..040ae4f 100644 --- a/core/printf.h +++ b/core/printf.h @@ -132,7 +132,7 @@ void putchar_(char c); * */ PRINTF_VISIBILITY -int z__b_fctprintf( +int z__fx_fctprintf( void (*out)(char c, void *extra_arg), void *extra_arg, const char *format, va_list arg) ATTR_VPRINTF(3); diff --git a/core/queue.c b/core/queue.c index a914c1f..15b950f 100644 --- a/core/queue.c +++ b/core/queue.c @@ -1,15 +1,15 @@ -#include +#include -struct b_queue_iterator_p { +struct fx_queue_iterator_p { size_t i; - b_queue_entry *entry; - b_queue *_q; + fx_queue_entry *entry; + fx_queue *_q; }; -size_t b_queue_length(const struct b_queue *q) +size_t fx_queue_length(const struct fx_queue *q) { size_t i = 0; - struct b_queue_entry *x = q->q_first; + struct fx_queue_entry *x = q->q_first; while (x) { i++; x = x->qe_next; @@ -18,10 +18,10 @@ size_t b_queue_length(const struct b_queue *q) return i; } -void b_queue_insert_before( - struct b_queue *q, struct b_queue_entry *entry, struct b_queue_entry *before) +void fx_queue_insert_before( + struct fx_queue *q, struct fx_queue_entry *entry, struct fx_queue_entry *before) { - struct b_queue_entry *x = before->qe_prev; + struct fx_queue_entry *x = before->qe_prev; if (x) { x->qe_next = entry; } else { @@ -34,10 +34,10 @@ void b_queue_insert_before( entry->qe_next = before; } -void b_queue_insert_after( - struct b_queue *q, struct b_queue_entry *entry, struct b_queue_entry *after) +void fx_queue_insert_after( + struct fx_queue *q, struct fx_queue_entry *entry, struct fx_queue_entry *after) { - struct b_queue_entry *x = after->qe_next; + struct fx_queue_entry *x = after->qe_next; if (x) { x->qe_prev = entry; } else { @@ -50,7 +50,7 @@ void b_queue_insert_after( entry->qe_prev = after; } -void b_queue_push_front(struct b_queue *q, struct b_queue_entry *entry) +void fx_queue_push_front(struct fx_queue *q, struct fx_queue_entry *entry) { if (q->q_first) { q->q_first->qe_prev = entry; @@ -66,7 +66,7 @@ void b_queue_push_front(struct b_queue *q, struct b_queue_entry *entry) } } -void b_queue_push_back(struct b_queue *q, struct b_queue_entry *entry) +void fx_queue_push_back(struct fx_queue *q, struct fx_queue_entry *entry) { if (q->q_last) { q->q_last->qe_next = entry; @@ -82,27 +82,27 @@ void b_queue_push_back(struct b_queue *q, struct b_queue_entry *entry) } } -struct b_queue_entry *b_queue_pop_front(struct b_queue *q) +struct fx_queue_entry *fx_queue_pop_front(struct fx_queue *q) { - struct b_queue_entry *x = q->q_first; + struct fx_queue_entry *x = q->q_first; if (x) { - b_queue_delete(q, x); + fx_queue_delete(q, x); } return x; } -struct b_queue_entry *b_queue_pop_back(struct b_queue *q) +struct fx_queue_entry *fx_queue_pop_back(struct fx_queue *q) { - struct b_queue_entry *x = q->q_last; + struct fx_queue_entry *x = q->q_last; if (x) { - b_queue_delete(q, x); + fx_queue_delete(q, x); } return x; } -void b_queue_move(b_queue *q, b_queue_entry *dest, b_queue_entry *src) +void fx_queue_move(fx_queue *q, fx_queue_entry *dest, fx_queue_entry *src) { if (src->qe_next) { src->qe_next->qe_prev = dest; @@ -123,7 +123,7 @@ void b_queue_move(b_queue *q, b_queue_entry *dest, b_queue_entry *src) memmove(dest, src, sizeof *src); } -void b_queue_delete(struct b_queue *q, struct b_queue_entry *entry) +void fx_queue_delete(struct fx_queue *q, struct fx_queue_entry *entry) { if (!entry) { return; @@ -148,11 +148,11 @@ void b_queue_delete(struct b_queue *q, struct b_queue_entry *entry) entry->qe_next = entry->qe_prev = NULL; } -void b_queue_delete_all(struct b_queue *q) +void fx_queue_delete_all(struct fx_queue *q) { - struct b_queue_entry *x = q->q_first; + struct fx_queue_entry *x = q->q_first; while (x) { - struct b_queue_entry *next = x->qe_next; + struct fx_queue_entry *next = x->qe_next; x->qe_next = x->qe_prev = NULL; x = next; } @@ -160,86 +160,86 @@ void b_queue_delete_all(struct b_queue *q) q->q_first = q->q_last = NULL; } -b_iterator *b_queue_begin(struct b_queue *q) +fx_iterator *fx_queue_begin(struct fx_queue *q) { - b_queue_iterator *it_obj = b_object_create(B_TYPE_QUEUE_ITERATOR); - struct b_queue_iterator_p *it - = b_object_get_private(it_obj, B_TYPE_QUEUE_ITERATOR); + fx_queue_iterator *it_obj = fx_object_create(FX_TYPE_QUEUE_ITERATOR); + struct fx_queue_iterator_p *it + = fx_object_get_private(it_obj, FX_TYPE_QUEUE_ITERATOR); - it->_q = (struct b_queue *)q; + it->_q = (struct fx_queue *)q; it->entry = q->q_first; it->i = 0; if (!it->entry) { - b_iterator_set_status(it_obj, B_ERR_NO_DATA); + fx_iterator_set_status(it_obj, FX_ERR_NO_DATA); } return it_obj; } -static enum b_status iterator_move_next(const b_iterator *obj) +static enum fx_status iterator_move_next(const fx_iterator *obj) { - struct b_queue_iterator_p *it - = b_object_get_private(obj, B_TYPE_QUEUE_ITERATOR); + struct fx_queue_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_QUEUE_ITERATOR); if (!it->entry) { - return B_ERR_NO_DATA; + return FX_ERR_NO_DATA; } it->entry = it->entry->qe_next; it->i++; - return (it->entry != NULL) ? B_SUCCESS : B_ERR_NO_DATA; + return (it->entry != NULL) ? FX_SUCCESS : FX_ERR_NO_DATA; } -static enum b_status iterator_erase(b_iterator *obj) +static enum fx_status iterator_erase(fx_iterator *obj) { - struct b_queue_iterator_p *it - = b_object_get_private(obj, B_TYPE_QUEUE_ITERATOR); + struct fx_queue_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_QUEUE_ITERATOR); if (!it->entry) { - return B_ERR_OUT_OF_BOUNDS; + return FX_ERR_OUT_OF_BOUNDS; } - struct b_queue_entry *next = it->entry->qe_next; - b_queue_delete(it->_q, it->entry); + struct fx_queue_entry *next = it->entry->qe_next; + fx_queue_delete(it->_q, it->entry); it->entry = next; - return B_SUCCESS; + return FX_SUCCESS; } -static b_iterator_value iterator_get_value(b_iterator *obj) +static fx_iterator_value iterator_get_value(fx_iterator *obj) { - struct b_queue_iterator_p *it - = b_object_get_private(obj, B_TYPE_QUEUE_ITERATOR); + struct fx_queue_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_QUEUE_ITERATOR); - return B_ITERATOR_VALUE_PTR(it->entry); + return FX_ITERATOR_VALUE_PTR(it->entry); } -static const b_iterator_value iterator_get_cvalue(const b_iterator *obj) +static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj) { - struct b_queue_iterator_p *it - = b_object_get_private(obj, B_TYPE_QUEUE_ITERATOR); + struct fx_queue_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_QUEUE_ITERATOR); - return B_ITERATOR_VALUE_CPTR(it->entry); + return FX_ITERATOR_VALUE_CPTR(it->entry); } -B_TYPE_CLASS_DEFINITION_BEGIN(b_queue_iterator) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_queue_iterator) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) - B_TYPE_CLASS_INTERFACE_BEGIN(b_iterator, B_TYPE_ITERATOR) - B_INTERFACE_ENTRY(it_move_next) = iterator_move_next; - B_INTERFACE_ENTRY(it_erase) = iterator_erase; - B_INTERFACE_ENTRY(it_get_value) = iterator_get_value; - B_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue; - B_TYPE_CLASS_INTERFACE_END(b_iterator, B_TYPE_ITERATOR) -B_TYPE_CLASS_DEFINITION_END(b_queue_iterator) + FX_TYPE_CLASS_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_CLASS_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR) +FX_TYPE_CLASS_DEFINITION_END(fx_queue_iterator) -B_TYPE_DEFINITION_BEGIN(b_queue_iterator) - B_TYPE_ID(0x560dc263, 0xff98, 0x4812, 0x9b29, 0xa1218bd70881); - B_TYPE_EXTENDS(B_TYPE_ITERATOR); - B_TYPE_CLASS(b_queue_iterator_class); - B_TYPE_INSTANCE_PRIVATE(struct b_queue_iterator_p); -B_TYPE_DEFINITION_END(b_queue_iterator) +FX_TYPE_DEFINITION_BEGIN(fx_queue_iterator) + FX_TYPE_ID(0x560dc263, 0xff98, 0x4812, 0x9b29, 0xa1218bd70881); + FX_TYPE_EXTENDS(FX_TYPE_ITERATOR); + FX_TYPE_CLASS(fx_queue_iterator_class); + FX_TYPE_INSTANCE_PRIVATE(struct fx_queue_iterator_p); +FX_TYPE_DEFINITION_END(fx_queue_iterator) diff --git a/core/random.c b/core/random.c index 796fad7..9e255dd 100644 --- a/core/random.c +++ b/core/random.c @@ -1,24 +1,24 @@ #include "random.h" -#include +#include #define GET_ALGORITHM_ID(flags) ((flags) & 0xFF) -BLUE_API struct b_random_algorithm z__b_gen_mt19937; +FX_API struct fx_random_algorithm z__fx_gen_mt19937; -static struct b_random_algorithm *generators[] = { - [B_RANDOM_MT19937] = &z__b_gen_mt19937, +static struct fx_random_algorithm *generators[] = { + [FX_RANDOM_MT19937] = &z__fx_gen_mt19937, }; static size_t nr_generators = sizeof generators / sizeof generators[0]; -static struct b_random_ctx g_global_ctx = {0}; +static struct fx_random_ctx g_global_ctx = {0}; static void init_global_ctx(void) { - b_random_init(&g_global_ctx, B_RANDOM_MT19937); + fx_random_init(&g_global_ctx, FX_RANDOM_MT19937); } -struct b_random_ctx *b_random_global_ctx(void) +struct fx_random_ctx *fx_random_global_ctx(void) { if (!g_global_ctx.__f) { init_global_ctx(); @@ -27,31 +27,31 @@ struct b_random_ctx *b_random_global_ctx(void) return &g_global_ctx; } -b_status b_random_init(struct b_random_ctx *ctx, b_random_flags flags) +fx_status fx_random_init(struct fx_random_ctx *ctx, fx_random_flags flags) { unsigned int algorithm_id = GET_ALGORITHM_ID(flags); if (algorithm_id >= nr_generators || !generators[algorithm_id]) { - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } - struct b_random_algorithm *algorithm = generators[algorithm_id]; + struct fx_random_algorithm *algorithm = generators[algorithm_id]; ctx->__f = flags; ctx->__a = algorithm; unsigned long long seed; - if (flags & B_RANDOM_SECURE) { - seed = z__b_platform_random_seed_secure(); + if (flags & FX_RANDOM_SECURE) { + seed = z__fx_platform_random_seed_secure(); } else { - seed = z__b_platform_random_seed(); + seed = z__fx_platform_random_seed(); } algorithm->gen_init(ctx, seed); - return B_SUCCESS; + return FX_SUCCESS; } -unsigned long long b_random_next_int64(struct b_random_ctx *ctx) +unsigned long long fx_random_next_int64(struct fx_random_ctx *ctx) { if (!ctx->__a || !ctx->__a->gen_getrand) { return 0; @@ -60,9 +60,9 @@ unsigned long long b_random_next_int64(struct b_random_ctx *ctx) return ctx->__a->gen_getrand(ctx); } -double b_random_next_double(struct b_random_ctx *ctx) +double fx_random_next_double(struct fx_random_ctx *ctx) { - unsigned long long v = b_random_next_int64(ctx); + unsigned long long v = fx_random_next_int64(ctx); if (!v) { return 0.0; } @@ -70,7 +70,7 @@ double b_random_next_double(struct b_random_ctx *ctx) return (double)(v >> 11) * (1.0 / 9007199254740991.0); } -void b_random_next_bytes(struct b_random_ctx *ctx, unsigned char *out, size_t nbytes) +void fx_random_next_bytes(struct fx_random_ctx *ctx, unsigned char *out, size_t nbytes) { size_t n_qwords = 0; n_qwords = nbytes >> 3; @@ -82,7 +82,7 @@ void b_random_next_bytes(struct b_random_ctx *ctx, unsigned char *out, size_t nb size_t bytes_written = 0; for (size_t i = 0; i < n_qwords; i++) { uint64_t *dest = (uint64_t *)&out[bytes_written]; - *dest = b_random_next_int64(ctx); + *dest = fx_random_next_int64(ctx); bytes_written += 8; } @@ -90,7 +90,7 @@ void b_random_next_bytes(struct b_random_ctx *ctx, unsigned char *out, size_t nb return; } - uint64_t padding = b_random_next_int64(ctx); + uint64_t padding = fx_random_next_int64(ctx); unsigned char *padding_bytes = (unsigned char *)&padding; for (size_t i = 0; i < 8; i++) { diff --git a/core/random.h b/core/random.h index 076f6c1..fe6174e 100644 --- a/core/random.h +++ b/core/random.h @@ -1,18 +1,18 @@ -#ifndef _BLUELIB_RANDOM_H_ -#define _BLUELIB_RANDOM_H_ +#ifndef _FX_RANDOM_H_ +#define _FX_RANDOM_H_ #include -#include +#include -struct b_random_ctx; +struct fx_random_ctx; -struct b_random_algorithm { +struct fx_random_algorithm { const char *gen_name; - void(*gen_init)(struct b_random_ctx *, unsigned long long seed); - uint64_t(*gen_getrand)(struct b_random_ctx *); + void(*gen_init)(struct fx_random_ctx *, unsigned long long seed); + uint64_t(*gen_getrand)(struct fx_random_ctx *); }; -BLUE_API uint64_t z__b_platform_random_seed(void); -BLUE_API uint64_t z__b_platform_random_seed_secure(void); +FX_API uint64_t z__fx_platform_random_seed(void); +FX_API uint64_t z__fx_platform_random_seed_secure(void); #endif diff --git a/core/ringbuffer.c b/core/ringbuffer.c index efb4565..714c2cf 100644 --- a/core/ringbuffer.c +++ b/core/ringbuffer.c @@ -1,4 +1,4 @@ -#include +#include #include #include @@ -14,7 +14,7 @@ enum ringbuffer_flags { RINGBUFFER_WRITE_LOCKED = 0x08u, }; -struct b_ringbuffer_p { +struct fx_ringbuffer_p { enum ringbuffer_flags r_flags; void *r_buf, *r_opened_buf; unsigned long r_capacity, r_opened_capacity; @@ -23,18 +23,18 @@ struct b_ringbuffer_p { /*** PRIVATE FUNCTIONS ********************************************************/ -static enum b_status ringbuffer_clear(struct b_ringbuffer_p *buf) +static enum fx_status ringbuffer_clear(struct fx_ringbuffer_p *buf) { if (BUF_LOCKED(buf)) { - return B_ERR_BUSY; + return FX_ERR_BUSY; } buf->r_read_ptr = buf->r_write_ptr = 0; - return B_SUCCESS; + return FX_SUCCESS; } -static size_t ringbuffer_write_capacity_remaining(const struct b_ringbuffer_p *buf) +static size_t ringbuffer_write_capacity_remaining(const struct fx_ringbuffer_p *buf) { if (buf->r_read_ptr > buf->r_write_ptr) { return buf->r_read_ptr - buf->r_write_ptr - 1; @@ -43,7 +43,7 @@ static size_t ringbuffer_write_capacity_remaining(const struct b_ringbuffer_p *b } } -static size_t ringbuffer_available_data_remaining(const struct b_ringbuffer_p *buf) +static size_t ringbuffer_available_data_remaining(const struct fx_ringbuffer_p *buf) { if (buf->r_read_ptr < buf->r_write_ptr) { return buf->r_write_ptr - buf->r_read_ptr; @@ -54,11 +54,11 @@ static size_t ringbuffer_available_data_remaining(const struct b_ringbuffer_p *b } } -static enum b_status ringbuffer_open_read_buffer( - struct b_ringbuffer_p *buf, const void **ptr, size_t *length) +static enum fx_status ringbuffer_open_read_buffer( + struct fx_ringbuffer_p *buf, const void **ptr, size_t *length) { if (BUF_LOCKED(buf)) { - return B_ERR_BUSY; + return FX_ERR_BUSY; } size_t contiguous_capacity = 0; @@ -69,7 +69,7 @@ static enum b_status ringbuffer_open_read_buffer( } if (contiguous_capacity == 0) { - return B_ERR_NO_DATA; + return FX_ERR_NO_DATA; } buf->r_opened_buf = (char *)buf->r_buf + buf->r_read_ptr; @@ -79,22 +79,22 @@ static enum b_status ringbuffer_open_read_buffer( *ptr = buf->r_opened_buf; *length = contiguous_capacity; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status ringbuffer_close_read_buffer( - struct b_ringbuffer_p *buf, const void **ptr, size_t nr_read) +static enum fx_status ringbuffer_close_read_buffer( + struct fx_ringbuffer_p *buf, const void **ptr, size_t nr_read) { if (!(buf->r_flags & RINGBUFFER_READ_LOCKED)) { - return B_ERR_BAD_STATE; + return FX_ERR_BAD_STATE; } if (*ptr != buf->r_opened_buf) { - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } if (nr_read > buf->r_opened_capacity) { - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } buf->r_read_ptr += nr_read; @@ -114,14 +114,14 @@ static enum b_status ringbuffer_close_read_buffer( buf->r_opened_capacity = 0; buf->r_flags &= ~RINGBUFFER_READ_LOCKED; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status ringbuffer_open_write_buffer( - struct b_ringbuffer_p *buf, void **ptr, size_t *capacity) +static enum fx_status ringbuffer_open_write_buffer( + struct fx_ringbuffer_p *buf, void **ptr, size_t *capacity) { if (BUF_LOCKED(buf)) { - return B_ERR_BUSY; + return FX_ERR_BUSY; } size_t contiguous_capacity = 0; @@ -136,7 +136,7 @@ static enum b_status ringbuffer_open_write_buffer( } if (contiguous_capacity == 0) { - return B_ERR_NO_SPACE; + return FX_ERR_NO_SPACE; } buf->r_opened_buf = (char *)buf->r_buf + buf->r_write_ptr; @@ -146,22 +146,22 @@ static enum b_status ringbuffer_open_write_buffer( *ptr = buf->r_opened_buf; *capacity = contiguous_capacity; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status ringbuffer_close_write_buffer( - struct b_ringbuffer_p *buf, void **ptr, size_t nr_written) +static enum fx_status ringbuffer_close_write_buffer( + struct fx_ringbuffer_p *buf, void **ptr, size_t nr_written) { if (!(buf->r_flags & RINGBUFFER_WRITE_LOCKED)) { - return B_ERR_BAD_STATE; + return FX_ERR_BAD_STATE; } if (*ptr != buf->r_opened_buf) { - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } if (nr_written > buf->r_opened_capacity) { - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } buf->r_write_ptr += nr_written; @@ -173,14 +173,14 @@ static enum b_status ringbuffer_close_write_buffer( buf->r_opened_capacity = 0; buf->r_flags &= ~RINGBUFFER_WRITE_LOCKED; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status ringbuffer_read( - struct b_ringbuffer_p *buf, void *p, size_t count, size_t *nr_read) +static enum fx_status ringbuffer_read( + struct fx_ringbuffer_p *buf, void *p, size_t count, size_t *nr_read) { if (BUF_LOCKED(buf)) { - return B_ERR_BUSY; + return FX_ERR_BUSY; } size_t r = 0; @@ -190,14 +190,14 @@ static enum b_status ringbuffer_read( while (remaining > 0) { const void *src; size_t available; - enum b_status status + enum fx_status status = ringbuffer_open_read_buffer(buf, &src, &available); - if (status == B_ERR_NO_DATA) { + if (status == FX_ERR_NO_DATA) { break; } - if (!B_OK(status)) { + if (!FX_OK(status)) { return status; } @@ -216,14 +216,14 @@ static enum b_status ringbuffer_read( } *nr_read = r; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status ringbuffer_write( - struct b_ringbuffer_p *buf, const void *p, size_t count, size_t *nr_written) +static enum fx_status ringbuffer_write( + struct fx_ringbuffer_p *buf, const void *p, size_t count, size_t *nr_written) { if (BUF_LOCKED(buf)) { - return B_ERR_BUSY; + return FX_ERR_BUSY; } size_t w = 0; @@ -233,14 +233,14 @@ static enum b_status ringbuffer_write( while (remaining > 0) { void *dest; size_t available; - enum b_status status + enum fx_status status = ringbuffer_open_write_buffer(buf, &dest, &available); - if (status == B_ERR_NO_SPACE) { + if (status == FX_ERR_NO_SPACE) { break; } - if (!B_OK(status)) { + if (!FX_OK(status)) { return status; } @@ -259,10 +259,10 @@ static enum b_status ringbuffer_write( } *nr_written = w; - return B_SUCCESS; + return FX_SUCCESS; } -static int ringbuffer_getc(struct b_ringbuffer_p *buf) +static int ringbuffer_getc(struct fx_ringbuffer_p *buf) { size_t available = ringbuffer_available_data_remaining(buf); if (available == 0) { @@ -279,11 +279,11 @@ static int ringbuffer_getc(struct b_ringbuffer_p *buf) return c; } -static enum b_status ringbuffer_putc(struct b_ringbuffer_p *buf, int c) +static enum fx_status ringbuffer_putc(struct fx_ringbuffer_p *buf, int c) { size_t available = ringbuffer_write_capacity_remaining(buf); if (available == 0) { - return B_ERR_NO_SPACE; + return FX_ERR_NO_SPACE; } char *p = buf->r_buf; @@ -298,19 +298,19 @@ static enum b_status ringbuffer_putc(struct b_ringbuffer_p *buf, int c) /*** PUBLIC FUNCTIONS *********************************************************/ -b_ringbuffer *b_ringbuffer_create(size_t capacity) +fx_ringbuffer *fx_ringbuffer_create(size_t capacity) { - b_ringbuffer *ringbuf = b_object_create(B_TYPE_RINGBUFFER); + fx_ringbuffer *ringbuf = fx_object_create(FX_TYPE_RINGBUFFER); if (!ringbuf) { return NULL; } - struct b_ringbuffer_p *p - = b_object_get_private(ringbuf, B_TYPE_RINGBUFFER); + struct fx_ringbuffer_p *p + = fx_object_get_private(ringbuf, FX_TYPE_RINGBUFFER); void *buffer = malloc(capacity); if (!buffer) { - b_ringbuffer_unref(ringbuf); + fx_ringbuffer_unref(ringbuf); return NULL; } @@ -321,15 +321,15 @@ b_ringbuffer *b_ringbuffer_create(size_t capacity) return ringbuf; } -b_ringbuffer *b_ringbuffer_create_with_buffer(void *ptr, size_t capacity) +fx_ringbuffer *fx_ringbuffer_create_with_buffer(void *ptr, size_t capacity) { - b_ringbuffer *ringbuf = b_object_create(B_TYPE_RINGBUFFER); + fx_ringbuffer *ringbuf = fx_object_create(FX_TYPE_RINGBUFFER); if (!ringbuf) { return NULL; } - struct b_ringbuffer_p *p - = b_object_get_private(ringbuf, B_TYPE_RINGBUFFER); + struct fx_ringbuffer_p *p + = fx_object_get_private(ringbuf, FX_TYPE_RINGBUFFER); p->r_flags = 0; p->r_buf = ptr; @@ -338,87 +338,87 @@ b_ringbuffer *b_ringbuffer_create_with_buffer(void *ptr, size_t capacity) return ringbuf; } -enum b_status b_ringbuffer_clear(b_ringbuffer *buf) +enum fx_status fx_ringbuffer_clear(fx_ringbuffer *buf) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_RINGBUFFER, ringbuffer_clear, buf); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_RINGBUFFER, ringbuffer_clear, buf); } -enum b_status b_ringbuffer_read( - b_ringbuffer *buf, void *p, size_t count, size_t *nr_read) +enum fx_status fx_ringbuffer_read( + fx_ringbuffer *buf, void *p, size_t count, size_t *nr_read) { - B_CLASS_DISPATCH_STATIC( - B_TYPE_RINGBUFFER, ringbuffer_read, buf, p, count, nr_read); + FX_CLASS_DISPATCH_STATIC( + FX_TYPE_RINGBUFFER, ringbuffer_read, buf, p, count, nr_read); } -enum b_status b_ringbuffer_write( - b_ringbuffer *buf, const void *p, size_t count, size_t *nr_written) +enum fx_status fx_ringbuffer_write( + fx_ringbuffer *buf, const void *p, size_t count, size_t *nr_written) { - B_CLASS_DISPATCH_STATIC( - B_TYPE_RINGBUFFER, ringbuffer_write, buf, p, count, nr_written); + FX_CLASS_DISPATCH_STATIC( + FX_TYPE_RINGBUFFER, ringbuffer_write, buf, p, count, nr_written); } -int b_ringbuffer_getc(b_ringbuffer *buf) +int fx_ringbuffer_getc(fx_ringbuffer *buf) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_RINGBUFFER, ringbuffer_getc, buf); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_RINGBUFFER, ringbuffer_getc, buf); } -enum b_status b_ringbuffer_putc(b_ringbuffer *buf, int c) +enum fx_status fx_ringbuffer_putc(fx_ringbuffer *buf, int c) { - B_CLASS_DISPATCH_STATIC(B_TYPE_RINGBUFFER, ringbuffer_putc, buf, c); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_RINGBUFFER, ringbuffer_putc, buf, c); } -size_t b_ringbuffer_write_capacity_remaining(const b_ringbuffer *buf) +size_t fx_ringbuffer_write_capacity_remaining(const fx_ringbuffer *buf) { - B_CLASS_DISPATCH_STATIC_0( - B_TYPE_RINGBUFFER, ringbuffer_write_capacity_remaining, buf); + FX_CLASS_DISPATCH_STATIC_0( + FX_TYPE_RINGBUFFER, ringbuffer_write_capacity_remaining, buf); } -size_t b_ringbuffer_available_data_remaining(const b_ringbuffer *buf) +size_t fx_ringbuffer_available_data_remaining(const fx_ringbuffer *buf) { - B_CLASS_DISPATCH_STATIC_0( - B_TYPE_RINGBUFFER, ringbuffer_available_data_remaining, buf); + FX_CLASS_DISPATCH_STATIC_0( + FX_TYPE_RINGBUFFER, ringbuffer_available_data_remaining, buf); } -enum b_status b_ringbuffer_open_read_buffer( - b_ringbuffer *buf, const void **ptr, size_t *length) +enum fx_status fx_ringbuffer_open_read_buffer( + fx_ringbuffer *buf, const void **ptr, size_t *length) { - B_CLASS_DISPATCH_STATIC( - B_TYPE_RINGBUFFER, ringbuffer_open_read_buffer, buf, ptr, length); + FX_CLASS_DISPATCH_STATIC( + FX_TYPE_RINGBUFFER, ringbuffer_open_read_buffer, buf, ptr, length); } -enum b_status b_ringbuffer_close_read_buffer( - b_ringbuffer *buf, const void **ptr, size_t nr_read) +enum fx_status fx_ringbuffer_close_read_buffer( + fx_ringbuffer *buf, const void **ptr, size_t nr_read) { - B_CLASS_DISPATCH_STATIC( - B_TYPE_RINGBUFFER, ringbuffer_close_read_buffer, buf, ptr, nr_read); + FX_CLASS_DISPATCH_STATIC( + FX_TYPE_RINGBUFFER, ringbuffer_close_read_buffer, buf, ptr, nr_read); } -enum b_status b_ringbuffer_open_write_buffer( - b_ringbuffer *buf, void **ptr, size_t *capacity) +enum fx_status fx_ringbuffer_open_write_buffer( + fx_ringbuffer *buf, void **ptr, size_t *capacity) { - B_CLASS_DISPATCH_STATIC( - B_TYPE_RINGBUFFER, ringbuffer_open_write_buffer, buf, ptr, + FX_CLASS_DISPATCH_STATIC( + FX_TYPE_RINGBUFFER, ringbuffer_open_write_buffer, buf, ptr, capacity); } -enum b_status b_ringbuffer_close_write_buffer( - b_ringbuffer *buf, void **ptr, size_t nr_written) +enum fx_status fx_ringbuffer_close_write_buffer( + fx_ringbuffer *buf, void **ptr, size_t nr_written) { - B_CLASS_DISPATCH_STATIC( - B_TYPE_RINGBUFFER, ringbuffer_close_write_buffer, buf, ptr, + FX_CLASS_DISPATCH_STATIC( + FX_TYPE_RINGBUFFER, ringbuffer_close_write_buffer, buf, ptr, nr_written); } /*** VIRTUAL FUNCTIONS ********************************************************/ -static void ringbuffer_init(b_object *obj, void *priv) +static void ringbuffer_init(fx_object *obj, void *priv) { - struct b_ringbuffer_p *buf = priv; + struct fx_ringbuffer_p *buf = priv; } -static void ringbuffer_fini(b_object *obj, void *priv) +static void ringbuffer_fini(fx_object *obj, void *priv) { - struct b_ringbuffer_p *buf = priv; + struct fx_ringbuffer_p *buf = priv; if (buf->r_flags & RINGBUFFER_BUFFER_MALLOC) { free(buf->r_buf); @@ -427,16 +427,16 @@ static void ringbuffer_fini(b_object *obj, void *priv) /*** CLASS DEFINITION *********************************************************/ -B_TYPE_CLASS_DEFINITION_BEGIN(b_ringbuffer) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) -B_TYPE_CLASS_DEFINITION_END(b_ringbuffer) +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_ringbuffer) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_DEFINITION_END(fx_ringbuffer) -B_TYPE_DEFINITION_BEGIN(b_ringbuffer) - B_TYPE_ID(0xb0493774, 0xef13, 0x4905, 0xa865, 0x1595607ccad9); - B_TYPE_CLASS(b_ringbuffer_class); - B_TYPE_INSTANCE_PRIVATE(struct b_ringbuffer_p); - B_TYPE_INSTANCE_INIT(ringbuffer_init); - B_TYPE_INSTANCE_FINI(ringbuffer_fini); -B_TYPE_DEFINITION_END(b_ringbuffer) +FX_TYPE_DEFINITION_BEGIN(fx_ringbuffer) + FX_TYPE_ID(0xb0493774, 0xef13, 0x4905, 0xa865, 0x1595607ccad9); + FX_TYPE_CLASS(fx_ringbuffer_class); + FX_TYPE_INSTANCE_PRIVATE(struct fx_ringbuffer_p); + FX_TYPE_INSTANCE_INIT(ringbuffer_init); + FX_TYPE_INSTANCE_FINI(ringbuffer_fini); +FX_TYPE_DEFINITION_END(fx_ringbuffer) diff --git a/core/rope.c b/core/rope.c index 1fe2c59..2a440c1 100644 --- a/core/rope.c +++ b/core/rope.c @@ -1,23 +1,23 @@ -#include -#include +#include +#include #include #include #include #include -void b_rope_init_char(struct b_rope *rope, char c) +void fx_rope_init_char(struct fx_rope *rope, char c) { memset(rope, 0x0, sizeof *rope); - rope->r_flags = B_ROPE_F_CHAR; + rope->r_flags = FX_ROPE_F_CHAR; rope->r_len_left = rope->r_len_total = 1; rope->r_v.v_char = c; } -void b_rope_init_cstr(struct b_rope *rope, const char *s) +void fx_rope_init_cstr(struct fx_rope *rope, const char *s) { memset(rope, 0x0, sizeof *rope); - rope->r_flags = B_ROPE_F_CSTR; - rope->r_v.v_cstr.hash = b_hash_cstr_ex(s, &rope->r_len_total); + rope->r_flags = FX_ROPE_F_CSTR; + rope->r_v.v_cstr.hash = fx_hash_cstr_ex(s, &rope->r_len_total); rope->r_len_left = rope->r_len_total; char *s2 = malloc(rope->r_len_total + 1); @@ -32,110 +32,110 @@ void b_rope_init_cstr(struct b_rope *rope, const char *s) rope->r_v.v_cstr.s = s2; } -void b_rope_init_cstr_borrowed(struct b_rope *rope, const char *s) +void fx_rope_init_cstr_borrowed(struct fx_rope *rope, const char *s) { memset(rope, 0x0, sizeof *rope); - rope->r_flags = B_ROPE_F_CSTR_BORROWED; + rope->r_flags = FX_ROPE_F_CSTR_BORROWED; rope->r_v.v_cstr.s = s; - rope->r_v.v_cstr.hash = b_hash_cstr_ex(s, &rope->r_len_total); + rope->r_v.v_cstr.hash = fx_hash_cstr_ex(s, &rope->r_len_total); rope->r_len_left = rope->r_len_total; } -void b_rope_init_cstr_static(struct b_rope *rope, const char *s) +void fx_rope_init_cstr_static(struct fx_rope *rope, const char *s) { memset(rope, 0x0, sizeof *rope); - rope->r_flags = B_ROPE_F_CSTR_STATIC; + rope->r_flags = FX_ROPE_F_CSTR_STATIC; rope->r_v.v_cstr.s = s; - rope->r_v.v_cstr.hash = b_hash_cstr_ex(s, &rope->r_len_total); + rope->r_v.v_cstr.hash = fx_hash_cstr_ex(s, &rope->r_len_total); rope->r_len_left = rope->r_len_total; } -void b_rope_init_int(struct b_rope *rope, intptr_t v) +void fx_rope_init_int(struct fx_rope *rope, intptr_t v) { memset(rope, 0x0, sizeof *rope); - rope->r_flags = B_ROPE_F_INT; - rope->r_len_left = rope->r_len_total = b_int_length(v); + rope->r_flags = FX_ROPE_F_INT; + rope->r_len_left = rope->r_len_total = fx_int_length(v); rope->r_v.v_int = v; } -void b_rope_init_uint(struct b_rope *rope, uintptr_t v) +void fx_rope_init_uint(struct fx_rope *rope, uintptr_t v) { memset(rope, 0x0, sizeof *rope); - rope->r_flags = B_ROPE_F_UINT; - rope->r_len_left = rope->r_len_total = b_uint_length(v); + rope->r_flags = FX_ROPE_F_UINT; + rope->r_len_left = rope->r_len_total = fx_uint_length(v); rope->r_v.v_uint = v; } -void b_rope_destroy(struct b_rope *rope) +void fx_rope_destroy(struct fx_rope *rope) { - unsigned int type = B_ROPE_TYPE(rope->r_flags); + unsigned int type = FX_ROPE_TYPE(rope->r_flags); switch (type) { - case B_ROPE_F_CSTR: + case FX_ROPE_F_CSTR: free((char *)rope->r_v.v_cstr.s); break; - case B_ROPE_F_COMPOSITE: - b_rope_destroy((struct b_rope *)rope->r_v.v_composite.r_left); - b_rope_destroy((struct b_rope *)rope->r_v.v_composite.r_right); + case FX_ROPE_F_COMPOSITE: + fx_rope_destroy((struct fx_rope *)rope->r_v.v_composite.r_left); + fx_rope_destroy((struct fx_rope *)rope->r_v.v_composite.r_right); break; default: break; } - if (rope->r_flags & B_ROPE_F_MALLOC) { + if (rope->r_flags & FX_ROPE_F_MALLOC) { free(rope); } } -void b_rope_iterate( - const struct b_rope *rope, void (*func)(const b_rope *, void *), void *arg) +void fx_rope_iterate( + const struct fx_rope *rope, void (*func)(const fx_rope *, void *), void *arg) { - if (B_ROPE_TYPE(rope->r_flags) != B_ROPE_F_COMPOSITE) { + if (FX_ROPE_TYPE(rope->r_flags) != FX_ROPE_F_COMPOSITE) { func(rope, arg); return; } if (rope->r_v.v_composite.r_left) { - b_rope_iterate(rope->r_v.v_composite.r_left, func, arg); + fx_rope_iterate(rope->r_v.v_composite.r_left, func, arg); } if (rope->r_v.v_composite.r_right) { - b_rope_iterate(rope->r_v.v_composite.r_right, func, arg); + fx_rope_iterate(rope->r_v.v_composite.r_right, func, arg); } } -size_t b_rope_get_size(const struct b_rope *rope) +size_t fx_rope_get_size(const struct fx_rope *rope) { return rope->r_len_total; } -void b_rope_concat( - struct b_rope *result, const struct b_rope *left, const struct b_rope *right) +void fx_rope_concat( + struct fx_rope *result, const struct fx_rope *left, const struct fx_rope *right) { memset(result, 0x0, sizeof *result); - result->r_flags = B_ROPE_F_COMPOSITE; + result->r_flags = FX_ROPE_F_COMPOSITE; result->r_len_left = left->r_len_total; result->r_len_total = left->r_len_total + right->r_len_total; result->r_v.v_composite.r_left = left; result->r_v.v_composite.r_right = right; } -static struct b_rope *create_composite_node(void) +static struct fx_rope *create_composite_node(void) { - struct b_rope *out = malloc(sizeof *out); + struct fx_rope *out = malloc(sizeof *out); if (!out) { return NULL; } memset(out, 0x0, sizeof *out); - out->r_flags = B_ROPE_F_COMPOSITE | B_ROPE_F_MALLOC; + out->r_flags = FX_ROPE_F_COMPOSITE | FX_ROPE_F_MALLOC; return out; } -void b_rope_join(b_rope *result, const b_rope **ropes, size_t nr_ropes) +void fx_rope_join(fx_rope *result, const fx_rope **ropes, size_t nr_ropes) { if (nr_ropes == 0) { return; @@ -146,17 +146,17 @@ void b_rope_join(b_rope *result, const b_rope **ropes, size_t nr_ropes) return; } - struct b_rope *prev = NULL; + struct fx_rope *prev = NULL; for (size_t i = 0; i < nr_ropes - 1; i++) { - const struct b_rope *left = ropes[i]; - const struct b_rope *right = ropes[i + 1]; + const struct fx_rope *left = ropes[i]; + const struct fx_rope *right = ropes[i + 1]; if (prev) { left = prev; } - struct b_rope *composite = create_composite_node(); + struct fx_rope *composite = create_composite_node(); if (!composite) { return; } @@ -171,26 +171,26 @@ void b_rope_join(b_rope *result, const b_rope **ropes, size_t nr_ropes) *result = *prev; - result->r_flags &= ~B_ROPE_F_MALLOC; + result->r_flags &= ~FX_ROPE_F_MALLOC; free(prev); } static void rope_iterate( - const struct b_rope *rope, - void (*callback)(const struct b_rope *, void *), void *arg) + const struct fx_rope *rope, + void (*callback)(const struct fx_rope *, void *), void *arg) { - unsigned int type = B_ROPE_TYPE(rope->r_flags); + unsigned int type = FX_ROPE_TYPE(rope->r_flags); switch (type) { - case B_ROPE_F_CHAR: - case B_ROPE_F_CSTR: - case B_ROPE_F_CSTR_BORROWED: - case B_ROPE_F_CSTR_STATIC: - case B_ROPE_F_INT: - case B_ROPE_F_UINT: + case FX_ROPE_F_CHAR: + case FX_ROPE_F_CSTR: + case FX_ROPE_F_CSTR_BORROWED: + case FX_ROPE_F_CSTR_STATIC: + case FX_ROPE_F_INT: + case FX_ROPE_F_UINT: callback(rope, arg); break; - case B_ROPE_F_COMPOSITE: + case FX_ROPE_F_COMPOSITE: rope_iterate(rope->r_v.v_composite.r_left, callback, arg); rope_iterate(rope->r_v.v_composite.r_right, callback, arg); break; @@ -199,77 +199,77 @@ static void rope_iterate( } } -static void to_bstr(const struct b_rope *rope, void *arg) +static void to_bstr(const struct fx_rope *rope, void *arg) { - b_bstr *str = arg; + fx_bstr *str = arg; - unsigned int type = B_ROPE_TYPE(rope->r_flags); + unsigned int type = FX_ROPE_TYPE(rope->r_flags); switch (type) { - case B_ROPE_F_CHAR: - b_bstr_write_char(str, rope->r_v.v_char); + case FX_ROPE_F_CHAR: + fx_bstr_write_char(str, rope->r_v.v_char); break; - case B_ROPE_F_CSTR: - case B_ROPE_F_CSTR_BORROWED: - case B_ROPE_F_CSTR_STATIC: - b_bstr_write_cstr(str, rope->r_v.v_cstr.s, NULL); + case FX_ROPE_F_CSTR: + case FX_ROPE_F_CSTR_BORROWED: + case FX_ROPE_F_CSTR_STATIC: + fx_bstr_write_cstr(str, rope->r_v.v_cstr.s, NULL); break; - case B_ROPE_F_INT: - b_bstr_write_fmt(str, NULL, "%" PRIdPTR, rope->r_v.v_int); + case FX_ROPE_F_INT: + fx_bstr_write_fmt(str, NULL, "%" PRIdPTR, rope->r_v.v_int); break; - case B_ROPE_F_UINT: - b_bstr_write_fmt(str, NULL, "%" PRIuPTR, rope->r_v.v_uint); + case FX_ROPE_F_UINT: + fx_bstr_write_fmt(str, NULL, "%" PRIuPTR, rope->r_v.v_uint); break; default: break; } } -static void to_stream(const struct b_rope *rope, void *arg) +static void to_stream(const struct fx_rope *rope, void *arg) { - b_stream *out = arg; + fx_stream *out = arg; - unsigned int type = B_ROPE_TYPE(rope->r_flags); + unsigned int type = FX_ROPE_TYPE(rope->r_flags); switch (type) { - case B_ROPE_F_CHAR: - b_stream_write_char(out, rope->r_v.v_char); + case FX_ROPE_F_CHAR: + fx_stream_write_char(out, rope->r_v.v_char); break; - case B_ROPE_F_CSTR: - case B_ROPE_F_CSTR_BORROWED: - case B_ROPE_F_CSTR_STATIC: - b_stream_write_string(out, rope->r_v.v_cstr.s, NULL); + case FX_ROPE_F_CSTR: + case FX_ROPE_F_CSTR_BORROWED: + case FX_ROPE_F_CSTR_STATIC: + fx_stream_write_string(out, rope->r_v.v_cstr.s, NULL); break; - case B_ROPE_F_INT: - b_stream_write_fmt(out, NULL, "%" PRIdPTR, rope->r_v.v_int); + case FX_ROPE_F_INT: + fx_stream_write_fmt(out, NULL, "%" PRIdPTR, rope->r_v.v_int); break; - case B_ROPE_F_UINT: - b_stream_write_fmt(out, NULL, "%" PRIuPTR, rope->r_v.v_uint); + case FX_ROPE_F_UINT: + fx_stream_write_fmt(out, NULL, "%" PRIuPTR, rope->r_v.v_uint); break; default: break; } } -b_status b_rope_to_cstr(const struct b_rope *rope, char *out, size_t max) +fx_status fx_rope_to_cstr(const struct fx_rope *rope, char *out, size_t max) { - b_bstr str; - b_bstr_begin(&str, out, max); + fx_bstr str; + fx_bstr_begin(&str, out, max); rope_iterate(rope, to_bstr, &str); - return B_SUCCESS; + return FX_SUCCESS; } -b_status b_rope_to_bstr(const struct b_rope *rope, b_bstr *str) +fx_status fx_rope_to_bstr(const struct fx_rope *rope, fx_bstr *str) { rope_iterate(rope, to_bstr, str); - return B_SUCCESS; + return FX_SUCCESS; } -b_status b_rope_to_string(const struct b_rope *rope, b_stream *out) +fx_status fx_rope_to_string(const struct fx_rope *rope, fx_stream *out) { rope_iterate(rope, to_stream, out); - return B_SUCCESS; + return FX_SUCCESS; } diff --git a/core/status.c b/core/status.c index 3ed92e8..89e02e6 100644 --- a/core/status.c +++ b/core/status.c @@ -1,4 +1,4 @@ -#include +#include #include #define ENUM_STR(s) \ @@ -9,51 +9,51 @@ case c: \ return s; -const char *b_status_to_string(b_status status) +const char *fx_status_to_string(fx_status status) { switch (status) { - ENUM_STR(B_SUCCESS); - ENUM_STR(B_ERR_NO_MEMORY); - ENUM_STR(B_ERR_OUT_OF_BOUNDS); - ENUM_STR(B_ERR_INVALID_ARGUMENT); - ENUM_STR(B_ERR_NAME_EXISTS); - ENUM_STR(B_ERR_NOT_SUPPORTED); - ENUM_STR(B_ERR_BAD_STATE); - ENUM_STR(B_ERR_NO_ENTRY); - ENUM_STR(B_ERR_NO_DATA); - ENUM_STR(B_ERR_NO_SPACE); - ENUM_STR(B_ERR_UNKNOWN_FUNCTION); - ENUM_STR(B_ERR_BAD_FORMAT); - ENUM_STR(B_ERR_IO_FAILURE); - ENUM_STR(B_ERR_IS_DIRECTORY); - ENUM_STR(B_ERR_NOT_DIRECTORY); - ENUM_STR(B_ERR_PERMISSION_DENIED); - ENUM_STR(B_ERR_BUSY); + ENUM_STR(FX_SUCCESS); + ENUM_STR(FX_ERR_NO_MEMORY); + ENUM_STR(FX_ERR_OUT_OF_BOUNDS); + ENUM_STR(FX_ERR_INVALID_ARGUMENT); + ENUM_STR(FX_ERR_NAME_EXISTS); + ENUM_STR(FX_ERR_NOT_SUPPORTED); + ENUM_STR(FX_ERR_BAD_STATE); + ENUM_STR(FX_ERR_NO_ENTRY); + ENUM_STR(FX_ERR_NO_DATA); + ENUM_STR(FX_ERR_NO_SPACE); + ENUM_STR(FX_ERR_UNKNOWN_FUNCTION); + ENUM_STR(FX_ERR_BAD_FORMAT); + ENUM_STR(FX_ERR_IO_FAILURE); + ENUM_STR(FX_ERR_IS_DIRECTORY); + ENUM_STR(FX_ERR_NOT_DIRECTORY); + ENUM_STR(FX_ERR_PERMISSION_DENIED); + ENUM_STR(FX_ERR_BUSY); default: return NULL; } } -const char *b_status_description(b_status status) +const char *fx_status_description(fx_status status) { switch (status) { - ENUM_STR2(B_SUCCESS, "Success"); - ENUM_STR2(B_ERR_NO_MEMORY, "Out of memory"); - ENUM_STR2(B_ERR_OUT_OF_BOUNDS, "Argument out of bounds"); - ENUM_STR2(B_ERR_INVALID_ARGUMENT, "Invalid argument"); - ENUM_STR2(B_ERR_NAME_EXISTS, "Name already exists"); - ENUM_STR2(B_ERR_NOT_SUPPORTED, "Operation not supported"); - ENUM_STR2(B_ERR_BAD_STATE, "Bad state"); - ENUM_STR2(B_ERR_NO_ENTRY, "No entry"); - ENUM_STR2(B_ERR_NO_DATA, "No data available"); - ENUM_STR2(B_ERR_NO_SPACE, "No space available"); - ENUM_STR2(B_ERR_UNKNOWN_FUNCTION, "Unknown function"); - ENUM_STR2(B_ERR_BAD_FORMAT, "Bad format"); - ENUM_STR2(B_ERR_IO_FAILURE, "I/O failure"); - ENUM_STR2(B_ERR_IS_DIRECTORY, "Object is a directory"); - ENUM_STR2(B_ERR_NOT_DIRECTORY, "Object is not a directory"); - ENUM_STR2(B_ERR_PERMISSION_DENIED, "Permission denied"); - ENUM_STR2(B_ERR_BUSY, "Resource busy or locked"); + ENUM_STR2(FX_SUCCESS, "Success"); + ENUM_STR2(FX_ERR_NO_MEMORY, "Out of memory"); + ENUM_STR2(FX_ERR_OUT_OF_BOUNDS, "Argument out of bounds"); + ENUM_STR2(FX_ERR_INVALID_ARGUMENT, "Invalid argument"); + ENUM_STR2(FX_ERR_NAME_EXISTS, "Name already exists"); + ENUM_STR2(FX_ERR_NOT_SUPPORTED, "Operation not supported"); + ENUM_STR2(FX_ERR_BAD_STATE, "Bad state"); + ENUM_STR2(FX_ERR_NO_ENTRY, "No entry"); + ENUM_STR2(FX_ERR_NO_DATA, "No data available"); + ENUM_STR2(FX_ERR_NO_SPACE, "No space available"); + ENUM_STR2(FX_ERR_UNKNOWN_FUNCTION, "Unknown function"); + ENUM_STR2(FX_ERR_BAD_FORMAT, "Bad format"); + ENUM_STR2(FX_ERR_IO_FAILURE, "I/O failure"); + ENUM_STR2(FX_ERR_IS_DIRECTORY, "Object is a directory"); + ENUM_STR2(FX_ERR_NOT_DIRECTORY, "Object is not a directory"); + ENUM_STR2(FX_ERR_PERMISSION_DENIED, "Permission denied"); + ENUM_STR2(FX_ERR_BUSY, "Resource busy or locked"); default: return NULL; } diff --git a/core/stream.c b/core/stream.c index c54c670..366a5cc 100644 --- a/core/stream.c +++ b/core/stream.c @@ -1,21 +1,21 @@ #include "printf.h" -#include -#include +#include +#include #include #include #include #define READ_MAGIC(p) (*(uint64_t *)p) -#define IS_BSTR(p) (READ_MAGIC(p) == B_BSTR_MAGIC) +#define IS_BSTR(p) (READ_MAGIC(p) == FX_BSTR_MAGIC) -#define B_TYPE_STDIO_STREAM (b_stdio_stream_get_type()) +#define FX_TYPE_STDIO_STREAM (fx_stdio_stream_get_type()) #define STREAM_DISPATCH_VIRTUAL(func, stream, ...) \ do { \ struct stream_data _stream; \ - enum b_status status = stream_get_data(stream, &_stream); \ - if (!B_OK(status)) { \ + enum fx_status status = stream_get_data(stream, &_stream); \ + if (!FX_OK(status)) { \ return status; \ } \ return func(&_stream, __VA_ARGS__); \ @@ -23,8 +23,8 @@ #define STREAM_DISPATCH_VIRTUAL_0(func, stream) \ do { \ struct stream_data _stream; \ - enum b_status status = stream_get_data(stream, &_stream); \ - if (!B_OK(status)) { \ + enum fx_status status = stream_get_data(stream, &_stream); \ + if (!FX_OK(status)) { \ return status; \ } \ return func(&_stream); \ @@ -34,43 +34,43 @@ #define IDX_STDOUT 1 #define IDX_STDERR 2 -B_DECLARE_TYPE(b_stdio_stream); +FX_DECLARE_TYPE(fx_stdio_stream); -B_TYPE_CLASS_DECLARATION_BEGIN(b_stdio_stream) -B_TYPE_CLASS_DECLARATION_END(b_stdio_stream) +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_stdio_stream) +FX_TYPE_CLASS_DECLARATION_END(fx_stdio_stream) /*** PRIVATE DATA *************************************************************/ -typedef enum b_stream_buffer_flags { - B_STREAM_BUFFER_F_NONE = 0x00u, - B_STREAM_BUFFER_F_DYNAMIC = 0x01u, -} b_stream_buffer_flags; +typedef enum fx_stream_buffer_flags { + FX_STREAM_BUFFER_F_NONE = 0x00u, + FX_STREAM_BUFFER_F_DYNAMIC = 0x01u, +} fx_stream_buffer_flags; -struct b_stream_p { +struct fx_stream_p { int *s_istack; int s_add_indent; size_t s_istack_ptr, s_istack_size; void *s_ptr0, *s_ptr1; }; -struct b_stdio_stream_p { +struct fx_stdio_stream_p { FILE *s_fp; }; -struct b_stream_buffer_p { - b_stream_buffer_flags p_flags; +struct fx_stream_buffer_p { + fx_stream_buffer_flags p_flags; void *p_buf; size_t p_buf_len; }; struct stream_data { - b_stream *s_obj; - struct b_stream_p *s_private; - struct b_stream_cfg *s_cfg; - b_stream_class *s_ops; + fx_stream *s_obj; + struct fx_stream_p *s_private; + struct fx_stream_cfg *s_cfg; + fx_stream_class *s_ops; }; -static b_stream *stdio[] = { +static fx_stream *stdio[] = { [IDX_STDIN] = NULL, [IDX_STDOUT] = NULL, [IDX_STDERR] = NULL, @@ -78,17 +78,17 @@ static b_stream *stdio[] = { /*** PRIVATE FUNCTIONS ********************************************************/ -b_type b_stdio_stream_get_type(void); -static enum b_status stdio_read(b_stream *, void *, size_t, size_t *); -static enum b_status stdio_write(b_stream *, const void *, size_t, size_t *); -static enum b_status stdio_seek(b_stream *, long long, b_stream_seek_origin); -static enum b_status stdio_tell(const b_stream *, size_t *); +fx_type fx_stdio_stream_get_type(void); +static enum fx_status stdio_read(fx_stream *, void *, size_t, size_t *); +static enum fx_status stdio_write(fx_stream *, const void *, size_t, size_t *); +static enum fx_status stdio_seek(fx_stream *, long long, fx_stream_seek_origin); +static enum fx_status stdio_tell(const fx_stream *, size_t *); -static enum b_status stream_get_data(const b_stream *strp, struct stream_data *out) +static enum fx_status stream_get_data(const fx_stream *strp, struct stream_data *out) { - out->s_obj = (b_stream *)strp; - return b_object_get_data( - strp, B_TYPE_STREAM, (void **)&out->s_private, + out->s_obj = (fx_stream *)strp; + return fx_object_get_data( + strp, FX_TYPE_STREAM, (void **)&out->s_private, (void **)&out->s_cfg, (void **)&out->s_ops); } @@ -104,104 +104,104 @@ static int current_indent(struct stream_data *stream) static size_t stream_cursor(const struct stream_data *stream) { if (!stream || !stream->s_ops || !stream->s_ops->s_tell) { - return B_NPOS; + return FX_NPOS; } - size_t p = B_NPOS; - b_status status = stream->s_ops->s_tell(stream->s_obj, &p); - if (!B_OK(status)) { - return B_NPOS; + size_t p = FX_NPOS; + fx_status status = stream->s_ops->s_tell(stream->s_obj, &p); + if (!FX_OK(status)) { + return FX_NPOS; } return p; } -static enum b_status stream_seek( - struct stream_data *stream, long long offset, b_stream_seek_origin origin) +static enum fx_status stream_seek( + struct stream_data *stream, long long offset, fx_stream_seek_origin origin) { if (!stream || !stream->s_ops || !stream->s_ops->s_seek) { - return B_ERR_NOT_SUPPORTED; + return FX_ERR_NOT_SUPPORTED; } return stream->s_ops->s_seek(stream->s_obj, offset, origin); } -static enum b_status stream_reserve(struct stream_data *stream, size_t len) +static enum fx_status stream_reserve(struct stream_data *stream, size_t len) { if (!stream || !stream->s_ops || !stream->s_ops->s_reserve) { - return B_ERR_NOT_SUPPORTED; + return FX_ERR_NOT_SUPPORTED; } return stream->s_ops->s_reserve(stream->s_obj, len); } -static enum b_status read_char_binary(struct stream_data *stream, b_wchar *c) +static enum fx_status read_char_binary(struct stream_data *stream, fx_wchar *c) { size_t r; unsigned char v = 0; - enum b_status status = stream->s_ops->s_read(stream->s_obj, &v, 1, &r); + enum fx_status status = stream->s_ops->s_read(stream->s_obj, &v, 1, &r); *c = v; - if (status == B_SUCCESS && r < 1) { - status = B_ERR_NO_DATA; + if (status == FX_SUCCESS && r < 1) { + status = FX_ERR_NO_DATA; } return status; } -static enum b_status read_char_utf8(struct stream_data *stream, b_wchar *c) +static enum fx_status read_char_utf8(struct stream_data *stream, fx_wchar *c) { size_t r; unsigned char s[4]; unsigned int len = 0; - enum b_status status = stream->s_ops->s_read(stream->s_obj, s, 1, &r); + enum fx_status status = stream->s_ops->s_read(stream->s_obj, s, 1, &r); - if (!B_OK(status)) { + if (!FX_OK(status)) { return status; } if (r < 1) { - return B_ERR_NO_DATA; + return FX_ERR_NO_DATA; } - len = b_wchar_utf8_header_decode(s[0]); + len = fx_wchar_utf8_header_decode(s[0]); if (len <= 0 || len > 4) { - return B_ERR_BAD_FORMAT; + return FX_ERR_BAD_FORMAT; } if (len == 1) { *c = s[0]; - return B_SUCCESS; + return FX_SUCCESS; } status = stream->s_ops->s_read(stream->s_obj, s + 1, len - 1, &r); - if (!B_OK(status)) { + if (!FX_OK(status)) { return status; } if (r != len - 1) { - return B_ERR_BAD_FORMAT; + return FX_ERR_BAD_FORMAT; } - b_wchar result = b_wchar_utf8_codepoint_decode((char *)s); + fx_wchar result = fx_wchar_utf8_codepoint_decode((char *)s); - if (result == B_WCHAR_INVALID) { - return B_ERR_BAD_FORMAT; + if (result == FX_WCHAR_INVALID) { + return FX_ERR_BAD_FORMAT; } *c = result; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status stream_read_char(struct stream_data *stream, b_wchar *c) +static enum fx_status stream_read_char(struct stream_data *stream, fx_wchar *c) { - if (!(stream->s_cfg->s_mode & B_STREAM_READ)) { - return B_ERR_NOT_SUPPORTED; + if (!(stream->s_cfg->s_mode & FX_STREAM_READ)) { + return FX_ERR_NOT_SUPPORTED; } - enum b_status status = B_ERR_NOT_SUPPORTED; + enum fx_status status = FX_ERR_NOT_SUPPORTED; #if 0 if (stream->s_ops->s_getc) { @@ -210,55 +210,55 @@ static enum b_status stream_read_char(struct stream_data *stream, b_wchar *c) #endif if (!stream->s_ops->s_read) { - return B_ERR_NOT_SUPPORTED; + return FX_ERR_NOT_SUPPORTED; } - if (stream->s_cfg->s_mode & B_STREAM_BINARY) { + if (stream->s_cfg->s_mode & FX_STREAM_BINARY) { return read_char_binary(stream, c); } return read_char_utf8(stream, c); } -static enum b_status __write_char(struct stream_data *stream, b_wchar c) +static enum fx_status __write_char(struct stream_data *stream, fx_wchar c) { size_t w, len; - enum b_status status; + enum fx_status status; char s[4]; - if (stream->s_cfg->s_mode & B_STREAM_BINARY) { + if (stream->s_cfg->s_mode & FX_STREAM_BINARY) { s[0] = c & 0xFF; len = 1; } else { - len = b_wchar_utf8_codepoint_encode(c, s); + len = fx_wchar_utf8_codepoint_encode(c, s); if (len == 0 || len > 4) { - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } } status = stream->s_ops->s_write(stream->s_obj, (unsigned char *)s, len, &w); - if (!B_OK(status)) { + if (!FX_OK(status)) { return status; } - if (status == B_SUCCESS && w < len) { - status = B_ERR_IO_FAILURE; + if (status == FX_SUCCESS && w < len) { + status = FX_ERR_IO_FAILURE; } return status; } -static enum b_status stream_write_char(struct stream_data *stream, b_wchar c) +static enum fx_status stream_write_char(struct stream_data *stream, fx_wchar c) { - if (!(stream->s_cfg->s_mode & B_STREAM_WRITE)) { - return B_ERR_NOT_SUPPORTED; + if (!(stream->s_cfg->s_mode & FX_STREAM_WRITE)) { + return FX_ERR_NOT_SUPPORTED; } - enum b_status status = B_ERR_NOT_SUPPORTED; + enum fx_status status = FX_ERR_NOT_SUPPORTED; if (!stream->s_ops->s_write) { - return B_ERR_NOT_SUPPORTED; + return FX_ERR_NOT_SUPPORTED; } if (c == '\n') { @@ -285,17 +285,17 @@ static enum b_status stream_write_char(struct stream_data *stream, b_wchar c) stream->s_private->s_add_indent = 1; } - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status stream_read_bytes( +static enum fx_status stream_read_bytes( struct stream_data *stream, void *buf, size_t count, size_t *nr_read) { - if (!(stream->s_cfg->s_mode & B_STREAM_READ)) { - return B_ERR_NOT_SUPPORTED; + if (!(stream->s_cfg->s_mode & FX_STREAM_READ)) { + return FX_ERR_NOT_SUPPORTED; } - enum b_status status = B_ERR_NOT_SUPPORTED; + enum fx_status status = FX_ERR_NOT_SUPPORTED; if (!stream->s_ops->s_read) { return status; @@ -304,16 +304,16 @@ static enum b_status stream_read_bytes( return stream->s_ops->s_read(stream->s_obj, buf, count, nr_read); } -static enum b_status stream_read_line(struct stream_data *stream, char *s, size_t max) +static enum fx_status stream_read_line(struct stream_data *stream, char *s, size_t max) { - if (!(stream->s_cfg->s_mode & B_STREAM_READ)) { - return B_ERR_NOT_SUPPORTED; + if (!(stream->s_cfg->s_mode & FX_STREAM_READ)) { + return FX_ERR_NOT_SUPPORTED; } - enum b_status status = B_SUCCESS; + enum fx_status status = FX_SUCCESS; size_t i = 0; - b_wchar c = 0; + fx_wchar c = 0; while (1) { if (i >= max) { @@ -321,7 +321,7 @@ static enum b_status stream_read_line(struct stream_data *stream, char *s, size_ } status = stream_read_char(stream, &c); - if (status != B_SUCCESS) { + if (status != FX_SUCCESS) { break; } @@ -333,28 +333,28 @@ static enum b_status stream_read_line(struct stream_data *stream, char *s, size_ s[i] = '\0'; } - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status stream_read_line_s( +static enum fx_status stream_read_line_s( struct stream_data *src, struct stream_data *dest) { - if (!(src->s_cfg->s_mode & B_STREAM_READ)) { - return B_ERR_NOT_SUPPORTED; + if (!(src->s_cfg->s_mode & FX_STREAM_READ)) { + return FX_ERR_NOT_SUPPORTED; } - if (!(dest->s_cfg->s_mode & B_STREAM_WRITE)) { - return B_ERR_NOT_SUPPORTED; + if (!(dest->s_cfg->s_mode & FX_STREAM_WRITE)) { + return FX_ERR_NOT_SUPPORTED; } - enum b_status status = B_SUCCESS; + enum fx_status status = FX_SUCCESS; size_t i = 0; - b_wchar c = 0; + fx_wchar c = 0; while (1) { status = stream_read_char(src, &c); - if (status != B_SUCCESS) { + if (status != FX_SUCCESS) { break; } @@ -366,32 +366,32 @@ static enum b_status stream_read_line_s( } } - if (status == B_ERR_NO_DATA && i > 0) { - status = B_SUCCESS; + if (status == FX_ERR_NO_DATA && i > 0) { + status = FX_SUCCESS; } return status; } -static enum b_status stream_read_line_to_bstr( - struct stream_data *src, struct b_bstr *dest) +static enum fx_status stream_read_line_to_bstr( + struct stream_data *src, struct fx_bstr *dest) { - if (!(src->s_cfg->s_mode & B_STREAM_READ)) { - return B_ERR_NOT_SUPPORTED; + if (!(src->s_cfg->s_mode & FX_STREAM_READ)) { + return FX_ERR_NOT_SUPPORTED; } - enum b_status status = B_SUCCESS; + enum fx_status status = FX_SUCCESS; size_t i = 0; - b_wchar c = 0; + fx_wchar c = 0; while (1) { status = stream_read_char(src, &c); - if (status != B_SUCCESS) { + if (status != FX_SUCCESS) { break; } - b_bstr_write_char(dest, c); + fx_bstr_write_char(dest, c); i++; if (c == '\n') { @@ -399,21 +399,21 @@ static enum b_status stream_read_line_to_bstr( } } - if (status == B_ERR_NO_DATA && i > 0) { - status = B_SUCCESS; + if (status == FX_ERR_NO_DATA && i > 0) { + status = FX_SUCCESS; } return status; } -static enum b_status stream_write_bytes( +static enum fx_status stream_write_bytes( struct stream_data *stream, const void *buf, size_t count, size_t *nr_written) { - if (!(stream->s_cfg->s_mode & B_STREAM_WRITE)) { - return B_ERR_NOT_SUPPORTED; + if (!(stream->s_cfg->s_mode & FX_STREAM_WRITE)) { + return FX_ERR_NOT_SUPPORTED; } - enum b_status status = B_ERR_NOT_SUPPORTED; + enum fx_status status = FX_ERR_NOT_SUPPORTED; if (!stream->s_ops->s_write) { return status; @@ -422,81 +422,81 @@ static enum b_status stream_write_bytes( return stream->s_ops->s_write(stream->s_obj, buf, count, nr_written); } -static enum b_status stream_read_all_bytes( +static enum fx_status stream_read_all_bytes( struct stream_data *stream, void *p, size_t max, size_t *out_nr_read) { - if (!(stream->s_cfg->s_mode & B_STREAM_READ)) { - return B_ERR_NOT_SUPPORTED; + if (!(stream->s_cfg->s_mode & FX_STREAM_READ)) { + return FX_ERR_NOT_SUPPORTED; } - enum b_status status = B_SUCCESS; + enum fx_status status = FX_SUCCESS; size_t nr_read = 0; unsigned char *s = p; while (nr_read < max) { int c; status = stream_read_char(stream, &c); - if (status != B_SUCCESS) { + if (status != FX_SUCCESS) { break; } s[nr_read++] = c; } - if (status == B_ERR_NO_DATA && nr_read > 0) { - status = B_SUCCESS; + if (status == FX_ERR_NO_DATA && nr_read > 0) { + status = FX_SUCCESS; } *out_nr_read = nr_read; return status; } -static enum b_status stream_read_all_bytes_s( +static enum fx_status stream_read_all_bytes_s( struct stream_data *src, struct stream_data *dest, - struct b_stream_buffer_p *buffer, size_t *out_nr_read) + struct fx_stream_buffer_p *buffer, size_t *out_nr_read) { - if (!(src->s_cfg->s_mode & B_STREAM_READ)) { - return B_ERR_NOT_SUPPORTED; + if (!(src->s_cfg->s_mode & FX_STREAM_READ)) { + return FX_ERR_NOT_SUPPORTED; } - if (!(dest->s_cfg->s_mode & B_STREAM_WRITE)) { - return B_ERR_NOT_SUPPORTED; + if (!(dest->s_cfg->s_mode & FX_STREAM_WRITE)) { + return FX_ERR_NOT_SUPPORTED; } if (!buffer) { - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } if (src->s_ops->s_seek && dest->s_ops->s_reserve) { size_t offset = stream_cursor(src); - stream_seek(src, 0, B_STREAM_SEEK_END); + stream_seek(src, 0, FX_STREAM_SEEK_END); size_t length = stream_cursor(src); - stream_seek(src, offset, B_STREAM_SEEK_START); + stream_seek(src, offset, FX_STREAM_SEEK_START); stream_reserve(dest, length); } - enum b_status status = B_SUCCESS; + enum fx_status status = FX_SUCCESS; size_t nr_read = 0; while (1) { size_t r = 0, w = 0; status = stream_read_bytes( src, buffer->p_buf, buffer->p_buf_len, &r); - if (status != B_SUCCESS) { + if (status != FX_SUCCESS) { break; } status = stream_write_bytes(dest, buffer->p_buf, r, &w); nr_read += w; - if (status != B_SUCCESS || w != buffer->p_buf_len) { + if (status != FX_SUCCESS || w != buffer->p_buf_len) { break; } } - if (status == B_ERR_NO_DATA && nr_read > 0) { - status = B_SUCCESS; + if (status == FX_ERR_NO_DATA && nr_read > 0) { + status = FX_SUCCESS; } if (out_nr_read) { @@ -506,48 +506,48 @@ static enum b_status stream_read_all_bytes_s( return status; } -static enum b_status stream_read_all_bytes_to_bstr_s( - struct stream_data *src, struct b_bstr *dest, - struct b_stream_buffer_p *buffer, size_t *out_nr_read) +static enum fx_status stream_read_all_bytes_to_bstr_s( + struct stream_data *src, struct fx_bstr *dest, + struct fx_stream_buffer_p *buffer, size_t *out_nr_read) { - if (!(src->s_cfg->s_mode & B_STREAM_READ)) { - return B_ERR_NOT_SUPPORTED; + if (!(src->s_cfg->s_mode & FX_STREAM_READ)) { + return FX_ERR_NOT_SUPPORTED; } if (!buffer) { - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } if (src->s_ops->s_seek) { size_t offset = stream_cursor(src); - stream_seek(src, 0, B_STREAM_SEEK_END); + stream_seek(src, 0, FX_STREAM_SEEK_END); size_t length = stream_cursor(src); - stream_seek(src, offset, B_STREAM_SEEK_START); + stream_seek(src, offset, FX_STREAM_SEEK_START); - b_bstr_reserve(dest, length); + fx_bstr_reserve(dest, length); } - enum b_status status = B_SUCCESS; + enum fx_status status = FX_SUCCESS; size_t nr_read = 0; while (1) { size_t r = 0, w = 0; status = stream_read_bytes( src, buffer->p_buf, buffer->p_buf_len, &r); - if (status != B_SUCCESS) { + if (status != FX_SUCCESS) { break; } - status = b_bstr_write_chars(dest, buffer->p_buf, r, &w); + status = fx_bstr_write_chars(dest, buffer->p_buf, r, &w); nr_read += w; - if (status != B_SUCCESS || w != buffer->p_buf_len) { + if (status != FX_SUCCESS || w != buffer->p_buf_len) { break; } } - if (status == B_ERR_NO_DATA && nr_read > 0) { - status = B_SUCCESS; + if (status == FX_ERR_NO_DATA && nr_read > 0) { + status = FX_SUCCESS; } if (out_nr_read) { @@ -557,16 +557,16 @@ static enum b_status stream_read_all_bytes_to_bstr_s( return status; } -static enum b_status stream_write_string( +static enum fx_status stream_write_string( struct stream_data *stream, const char *s, size_t *nr_written) { size_t i; - enum b_status status = B_SUCCESS; + enum fx_status status = FX_SUCCESS; for (i = 0; s[i]; i++) { status = stream_write_char(stream, s[i]); - if (!B_OK(status)) { + if (!FX_OK(status)) { break; } } @@ -578,30 +578,30 @@ static enum b_status stream_write_string( return status; } -static b_stream *init_stdio_stream(FILE *fp, b_stream_mode mode) +static fx_stream *init_stdio_stream(FILE *fp, fx_stream_mode mode) { - b_stdio_stream *stream = b_object_create(B_TYPE_STDIO_STREAM); + fx_stdio_stream *stream = fx_object_create(FX_TYPE_STDIO_STREAM); if (!stream) { return NULL; } - struct b_stdio_stream_p *p - = b_object_get_private(stream, B_TYPE_STDIO_STREAM); - b_stream_cfg *cfg = b_object_get_protected(stream, B_TYPE_STREAM); + struct fx_stdio_stream_p *p + = fx_object_get_private(stream, FX_TYPE_STDIO_STREAM); + fx_stream_cfg *cfg = fx_object_get_protected(stream, FX_TYPE_STREAM); p->s_fp = fp; cfg->s_mode = mode; return stream; } -static enum b_status stream_push_indent(struct stream_data *stream, int indent) +static enum fx_status stream_push_indent(struct stream_data *stream, int indent) { - if (!(stream->s_cfg->s_mode & B_STREAM_WRITE)) { - return B_ERR_NOT_SUPPORTED; + if (!(stream->s_cfg->s_mode & FX_STREAM_WRITE)) { + return FX_ERR_NOT_SUPPORTED; } - if (stream->s_cfg->s_mode & B_STREAM_BINARY) { - return B_ERR_NOT_SUPPORTED; + if (stream->s_cfg->s_mode & FX_STREAM_BINARY) { + return FX_ERR_NOT_SUPPORTED; } if (!stream->s_private->s_istack) { @@ -615,7 +615,7 @@ static enum b_status stream_push_indent(struct stream_data *stream, int indent) stream->s_private->s_istack, (stream->s_private->s_istack_size + 4) * sizeof(int)); if (!buf) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } stream->s_private->s_istack = buf; @@ -627,26 +627,26 @@ static enum b_status stream_push_indent(struct stream_data *stream, int indent) stream->s_private->s_istack[++stream->s_private->s_istack_ptr] = cur_indent + indent; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status stream_pop_indent(struct stream_data *stream) +static enum fx_status stream_pop_indent(struct stream_data *stream) { - if (!(stream->s_cfg->s_mode & B_STREAM_WRITE)) { - return B_ERR_NOT_SUPPORTED; + if (!(stream->s_cfg->s_mode & FX_STREAM_WRITE)) { + return FX_ERR_NOT_SUPPORTED; } - if (stream->s_cfg->s_mode & B_STREAM_BINARY) { - return B_ERR_NOT_SUPPORTED; + if (stream->s_cfg->s_mode & FX_STREAM_BINARY) { + return FX_ERR_NOT_SUPPORTED; } if (!stream->s_private->s_istack || !stream->s_private->s_istack_size || !stream->s_private->s_istack_ptr) { - return B_SUCCESS; + return FX_SUCCESS; } stream->s_private->s_istack_ptr--; - return B_SUCCESS; + return FX_SUCCESS; } static void fctprintf_callback(char c, void *p) @@ -657,64 +657,64 @@ static void fctprintf_callback(char c, void *p) /*** PUBLIC FUNCTIONS *********************************************************/ -b_stream *z__b_stream_get_stdin(void) +fx_stream *z__fx_stream_get_stdin(void) { if (!stdio[IDX_STDIN]) { - stdio[IDX_STDIN] = init_stdio_stream(stdin, B_STREAM_READ); + stdio[IDX_STDIN] = init_stdio_stream(stdin, FX_STREAM_READ); } return stdio[IDX_STDIN]; } -b_stream *z__b_stream_get_stdout(void) +fx_stream *z__fx_stream_get_stdout(void) { if (!stdio[IDX_STDOUT]) { - stdio[IDX_STDOUT] = init_stdio_stream(stdout, B_STREAM_WRITE); + stdio[IDX_STDOUT] = init_stdio_stream(stdout, FX_STREAM_WRITE); } return stdio[IDX_STDOUT]; } -b_stream *z__b_stream_get_stderr(void) +fx_stream *z__fx_stream_get_stderr(void) { if (!stdio[IDX_STDERR]) { - stdio[IDX_STDERR] = init_stdio_stream(stderr, B_STREAM_WRITE); + stdio[IDX_STDERR] = init_stdio_stream(stderr, FX_STREAM_WRITE); } return stdio[IDX_STDERR]; } -b_stream_buffer *b_stream_buffer_create_dynamic(size_t buffer_size) +fx_stream_buffer *fx_stream_buffer_create_dynamic(size_t buffer_size) { - b_stream_buffer *buffer = b_object_create(B_TYPE_STREAM_BUFFER); + fx_stream_buffer *buffer = fx_object_create(FX_TYPE_STREAM_BUFFER); if (!buffer) { return NULL; } - struct b_stream_buffer_p *p - = b_object_get_private(buffer, B_TYPE_STREAM_BUFFER); + struct fx_stream_buffer_p *p + = fx_object_get_private(buffer, FX_TYPE_STREAM_BUFFER); p->p_buf = malloc(buffer_size); if (!p->p_buf) { - b_stream_buffer_unref(buffer); + fx_stream_buffer_unref(buffer); return NULL; } p->p_buf_len = buffer_size; - p->p_flags = B_STREAM_BUFFER_F_DYNAMIC; + p->p_flags = FX_STREAM_BUFFER_F_DYNAMIC; return buffer; } -b_stream_buffer *b_stream_buffer_create(void *buf, size_t len) +fx_stream_buffer *fx_stream_buffer_create(void *buf, size_t len) { - b_stream_buffer *buffer = b_object_create(B_TYPE_STREAM_BUFFER); + fx_stream_buffer *buffer = fx_object_create(FX_TYPE_STREAM_BUFFER); if (!buffer) { return NULL; } - struct b_stream_buffer_p *p - = b_object_get_private(buffer, B_TYPE_STREAM_BUFFER); + struct fx_stream_buffer_p *p + = fx_object_get_private(buffer, FX_TYPE_STREAM_BUFFER); p->p_buf = buf; p->p_buf_len = len; @@ -723,246 +723,246 @@ b_stream_buffer *b_stream_buffer_create(void *buf, size_t len) return buffer; } -b_stream *b_stream_open_fp(FILE *fp) +fx_stream *fx_stream_open_fp(FILE *fp) { - return init_stdio_stream(fp, B_STREAM_READ | B_STREAM_WRITE); + return init_stdio_stream(fp, FX_STREAM_READ | FX_STREAM_WRITE); } -enum b_status b_stream_reserve(b_stream *stream, size_t len) +enum fx_status fx_stream_reserve(fx_stream *stream, size_t len) { if (IS_BSTR(stream)) { - return B_ERR_NOT_SUPPORTED; + return FX_ERR_NOT_SUPPORTED; } - B_CLASS_DISPATCH_VIRTUAL( - b_stream, B_TYPE_STREAM, B_ERR_NOT_SUPPORTED, s_reserve, stream, + FX_CLASS_DISPATCH_VIRTUAL( + fx_stream, FX_TYPE_STREAM, FX_ERR_NOT_SUPPORTED, s_reserve, stream, len); } -enum b_status b_stream_seek( - b_stream *stream, long long offset, b_stream_seek_origin origin) +enum fx_status fx_stream_seek( + fx_stream *stream, long long offset, fx_stream_seek_origin origin) { if (IS_BSTR(stream)) { - return B_ERR_NOT_SUPPORTED; + return FX_ERR_NOT_SUPPORTED; } - B_CLASS_DISPATCH_VIRTUAL( - b_stream, B_TYPE_STREAM, B_ERR_NOT_SUPPORTED, s_seek, stream, + FX_CLASS_DISPATCH_VIRTUAL( + fx_stream, FX_TYPE_STREAM, FX_ERR_NOT_SUPPORTED, s_seek, stream, offset, origin); } -size_t b_stream_cursor(const b_stream *stream) +size_t fx_stream_cursor(const fx_stream *stream) { if (IS_BSTR(stream)) { - return b_bstr_get_size((b_bstr *)stream); + return fx_bstr_get_size((fx_bstr *)stream); } STREAM_DISPATCH_VIRTUAL_0(stream_cursor, stream); } -enum b_status b_stream_push_indent(b_stream *strp, int indent) +enum fx_status fx_stream_push_indent(fx_stream *strp, int indent) { if (IS_BSTR(strp)) { - return b_bstr_push_indent((b_bstr *)strp, indent); + return fx_bstr_push_indent((fx_bstr *)strp, indent); } STREAM_DISPATCH_VIRTUAL(stream_push_indent, strp, indent); } -enum b_status b_stream_pop_indent(b_stream *strp) +enum fx_status fx_stream_pop_indent(fx_stream *strp) { if (IS_BSTR(strp)) { - return b_bstr_pop_indent((b_bstr *)strp); + return fx_bstr_pop_indent((fx_bstr *)strp); } STREAM_DISPATCH_VIRTUAL_0(stream_pop_indent, strp); } -enum b_status b_stream_read_char(b_stream *strp, int *c) +enum fx_status fx_stream_read_char(fx_stream *strp, int *c) { if (IS_BSTR(strp)) { - return B_ERR_NOT_SUPPORTED; + return FX_ERR_NOT_SUPPORTED; } STREAM_DISPATCH_VIRTUAL(stream_read_char, strp, c); } -enum b_status b_stream_read_bytes( - b_stream *strp, void *buf, size_t count, size_t *nr_read) +enum fx_status fx_stream_read_bytes( + fx_stream *strp, void *buf, size_t count, size_t *nr_read) { if (IS_BSTR(strp)) { - return B_ERR_NOT_SUPPORTED; + return FX_ERR_NOT_SUPPORTED; } STREAM_DISPATCH_VIRTUAL(stream_read_bytes, strp, buf, count, nr_read); } -enum b_status b_stream_read_line(b_stream *strp, char *s, size_t max) +enum fx_status fx_stream_read_line(fx_stream *strp, char *s, size_t max) { if (IS_BSTR(strp)) { - return B_ERR_NOT_SUPPORTED; + return FX_ERR_NOT_SUPPORTED; } STREAM_DISPATCH_VIRTUAL(stream_read_line, strp, s, max); } -enum b_status b_stream_read_line_s(b_stream *src, b_stream *dest) +enum fx_status fx_stream_read_line_s(fx_stream *src, fx_stream *dest) { if (IS_BSTR(src)) { - return B_ERR_NOT_SUPPORTED; + return FX_ERR_NOT_SUPPORTED; } - enum b_status status; + enum fx_status status; struct stream_data src_p, dest_p; status = stream_get_data(src, &src_p); - if (!B_OK(status)) { + if (!FX_OK(status)) { return status; } if (IS_BSTR(dest)) { - return stream_read_line_to_bstr(&src_p, (b_bstr *)dest); + return stream_read_line_to_bstr(&src_p, (fx_bstr *)dest); } status = stream_get_data(dest, &dest_p); - if (!B_OK(status)) { + if (!FX_OK(status)) { return status; } return stream_read_line_s(&src_p, &dest_p); } -enum b_status b_stream_read_all_bytes( - b_stream *stream, void *p, size_t max, size_t *out_nr_read) +enum fx_status fx_stream_read_all_bytes( + fx_stream *stream, void *p, size_t max, size_t *out_nr_read) { if (IS_BSTR(stream)) { - return B_ERR_NOT_SUPPORTED; + return FX_ERR_NOT_SUPPORTED; } STREAM_DISPATCH_VIRTUAL(stream_read_all_bytes, stream, p, max, out_nr_read); } -enum b_status b_stream_read_all_bytes_s( - b_stream *src, b_stream *dest, b_stream_buffer *buffer, size_t *out_nr_read) +enum fx_status fx_stream_read_all_bytes_s( + fx_stream *src, fx_stream *dest, fx_stream_buffer *buffer, size_t *out_nr_read) { if (IS_BSTR(src)) { - return B_ERR_NOT_SUPPORTED; + return FX_ERR_NOT_SUPPORTED; } - enum b_status status; + enum fx_status status; struct stream_data src_p, dest_p; - struct b_stream_buffer_p *buffer_p; + struct fx_stream_buffer_p *buffer_p; status = stream_get_data(src, &src_p); - if (!B_OK(status)) { + if (!FX_OK(status)) { return status; } - buffer_p = b_object_get_private(buffer, B_TYPE_STREAM_BUFFER); + buffer_p = fx_object_get_private(buffer, FX_TYPE_STREAM_BUFFER); if (!buffer_p) { - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } if (IS_BSTR(dest)) { return stream_read_all_bytes_to_bstr_s( - &src_p, (b_bstr *)dest, buffer_p, out_nr_read); + &src_p, (fx_bstr *)dest, buffer_p, out_nr_read); } status = stream_get_data(dest, &dest_p); - if (!B_OK(status)) { + if (!FX_OK(status)) { return status; } return stream_read_all_bytes_s(&src_p, &dest_p, buffer_p, out_nr_read); } -enum b_status b_stream_write_char(b_stream *stream, b_wchar c) +enum fx_status fx_stream_write_char(fx_stream *stream, fx_wchar c) { if (IS_BSTR(stream)) { - return b_bstr_write_char((b_bstr *)stream, c); + return fx_bstr_write_char((fx_bstr *)stream, c); } STREAM_DISPATCH_VIRTUAL(stream_write_char, stream, c); } -enum b_status b_stream_write_string( - b_stream *stream, const char *s, size_t *nr_written) +enum fx_status fx_stream_write_string( + fx_stream *stream, const char *s, size_t *nr_written) { if (IS_BSTR(stream)) { - return b_bstr_write_cstr((b_bstr *)stream, s, nr_written); + return fx_bstr_write_cstr((fx_bstr *)stream, s, nr_written); } STREAM_DISPATCH_VIRTUAL(stream_write_string, stream, s, nr_written); } -enum b_status b_stream_write_bytes( - b_stream *stream, const void *buf, size_t count, size_t *nr_written) +enum fx_status fx_stream_write_bytes( + fx_stream *stream, const void *buf, size_t count, size_t *nr_written) { if (IS_BSTR(stream)) { - return b_bstr_write_chars((b_bstr *)stream, buf, count, nr_written); + return fx_bstr_write_chars((fx_bstr *)stream, buf, count, nr_written); } STREAM_DISPATCH_VIRTUAL(stream_write_bytes, stream, buf, count, nr_written); } -enum b_status b_stream_write_fmt( - b_stream *stream, size_t *nr_written, const char *format, ...) +enum fx_status fx_stream_write_fmt( + fx_stream *stream, size_t *nr_written, const char *format, ...) { if (IS_BSTR(stream)) { va_list arg; va_start(arg, format); - b_status w = b_bstr_write_vfmt( - (b_bstr *)stream, nr_written, format, arg); + fx_status w = fx_bstr_write_vfmt( + (fx_bstr *)stream, nr_written, format, arg); va_end(arg); return w; } struct stream_data p; - enum b_status status = stream_get_data(stream, &p); + enum fx_status status = stream_get_data(stream, &p); va_list arg; va_start(arg, format); - int w = z__b_fctprintf(fctprintf_callback, &p, format, arg); + int w = z__fx_fctprintf(fctprintf_callback, &p, format, arg); va_end(arg); if (nr_written) { *nr_written = w; } - return B_SUCCESS; + return FX_SUCCESS; } -enum b_status b_stream_write_vfmt( - b_stream *stream, size_t *nr_written, const char *format, va_list arg) +enum fx_status fx_stream_write_vfmt( + fx_stream *stream, size_t *nr_written, const char *format, va_list arg) { if (IS_BSTR(stream)) { - return b_bstr_write_vfmt((b_bstr *)stream, nr_written, format, arg); + return fx_bstr_write_vfmt((fx_bstr *)stream, nr_written, format, arg); } struct stream_data p; - enum b_status status = stream_get_data(stream, &p); + enum fx_status status = stream_get_data(stream, &p); - int w = z__b_fctprintf(fctprintf_callback, &p, format, arg); + int w = z__fx_fctprintf(fctprintf_callback, &p, format, arg); if (nr_written) { *nr_written = w; } - return B_SUCCESS; + return FX_SUCCESS; } /*** VIRTUAL FUNCTIONS ********************************************************/ -static void stream_init(b_object *obj, void *priv) +static void stream_init(fx_object *obj, void *priv) { - struct b_stream_p *stream = priv; + struct fx_stream_p *stream = priv; } -static void stream_fini(b_object *obj, void *priv) +static void stream_fini(fx_object *obj, void *priv) { - struct b_stream_p *stream = priv; - b_stream_class *ops = b_object_get_interface(obj, B_TYPE_STREAM); + struct fx_stream_p *stream = priv; + fx_stream_class *ops = fx_object_get_interface(obj, FX_TYPE_STREAM); if (stream->s_istack) { free(stream->s_istack); @@ -973,162 +973,162 @@ static void stream_fini(b_object *obj, void *priv) } } -static void stream_buffer_init(b_object *obj, void *priv) +static void stream_buffer_init(fx_object *obj, void *priv) { - struct b_stream_buffer_p *buffer = priv; + struct fx_stream_buffer_p *buffer = priv; } -static void stream_buffer_fini(b_object *obj, void *priv) +static void stream_buffer_fini(fx_object *obj, void *priv) { - struct b_stream_buffer_p *buffer = priv; - if (buffer->p_flags & B_STREAM_BUFFER_F_DYNAMIC) { + struct fx_stream_buffer_p *buffer = priv; + if (buffer->p_flags & FX_STREAM_BUFFER_F_DYNAMIC) { free(buffer->p_buf); } } -static void stdio_stream_init(b_object *obj, void *priv) +static void stdio_stream_init(fx_object *obj, void *priv) { - struct b_stdio_stream_p *stream = priv; + struct fx_stdio_stream_p *stream = priv; } -static void stdio_stream_fini(b_object *obj, void *priv) +static void stdio_stream_fini(fx_object *obj, void *priv) { - struct b_stdio_stream_p *stream = priv; + struct fx_stdio_stream_p *stream = priv; } -static enum b_status stdio_read( - b_stream *stream, void *out, size_t max, size_t *nr_read) +static enum fx_status stdio_read( + fx_stream *stream, void *out, size_t max, size_t *nr_read) { - struct b_stdio_stream_p *p - = b_object_get_private(stream, B_TYPE_STDIO_STREAM); + struct fx_stdio_stream_p *p + = fx_object_get_private(stream, FX_TYPE_STDIO_STREAM); - enum b_status status = B_SUCCESS; + enum fx_status status = FX_SUCCESS; size_t count = fread(out, 1, max, p->s_fp); if (ferror(p->s_fp)) { - status = B_ERR_IO_FAILURE; + status = FX_ERR_IO_FAILURE; } *nr_read = count; return status; } -static enum b_status stdio_write( - b_stream *stream, const void *data, size_t count, size_t *nr_written) +static enum fx_status stdio_write( + fx_stream *stream, const void *data, size_t count, size_t *nr_written) { - struct b_stdio_stream_p *p - = b_object_get_private(stream, B_TYPE_STDIO_STREAM); + struct fx_stdio_stream_p *p + = fx_object_get_private(stream, FX_TYPE_STDIO_STREAM); - enum b_status status = B_SUCCESS; + enum fx_status status = FX_SUCCESS; size_t w = fwrite(data, 1, count, p->s_fp); if (ferror(p->s_fp)) { - status = B_ERR_IO_FAILURE; + status = FX_ERR_IO_FAILURE; } *nr_written = w; return status; } -static enum b_status stdio_seek( - b_stream *stream, long long offset, b_stream_seek_origin origin) +static enum fx_status stdio_seek( + fx_stream *stream, long long offset, fx_stream_seek_origin origin) { - struct b_stdio_stream_p *p - = b_object_get_private(stream, B_TYPE_STDIO_STREAM); + struct fx_stdio_stream_p *p + = fx_object_get_private(stream, FX_TYPE_STDIO_STREAM); int whence = 0; switch (origin) { - case B_STREAM_SEEK_START: + case FX_STREAM_SEEK_START: whence = SEEK_SET; break; - case B_STREAM_SEEK_CURRENT: + case FX_STREAM_SEEK_CURRENT: whence = SEEK_CUR; break; - case B_STREAM_SEEK_END: + case FX_STREAM_SEEK_END: whence = SEEK_END; break; default: - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } int ret = fseek(p->s_fp, offset, whence); if (ret != 0) { - return B_ERR_NOT_SUPPORTED; + return FX_ERR_NOT_SUPPORTED; } - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status stdio_tell(const b_stream *stream, size_t *cursor) +static enum fx_status stdio_tell(const fx_stream *stream, size_t *cursor) { - struct b_stdio_stream_p *p - = b_object_get_private(stream, B_TYPE_STDIO_STREAM); + struct fx_stdio_stream_p *p + = fx_object_get_private(stream, FX_TYPE_STDIO_STREAM); long pos = ftell(p->s_fp); if (pos == -1L) { - return B_ERR_NOT_SUPPORTED; + return FX_ERR_NOT_SUPPORTED; } *cursor = (size_t)pos; - return B_SUCCESS; + return FX_SUCCESS; } /*** CLASS DEFINITION *********************************************************/ -// ---- b_stream DEFINITION -B_TYPE_CLASS_DEFINITION_BEGIN(b_stream) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) -B_TYPE_CLASS_DEFINITION_END(b_stream) +// ---- fx_stream DEFINITION +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_stream) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_DEFINITION_END(fx_stream) -B_TYPE_DEFINITION_BEGIN(b_stream) - B_TYPE_ID(0xa2c98988, 0x30e5, 0x47c7, 0x88cd, 0x6c8ea79f69cd); - B_TYPE_CLASS(b_stream_class); - B_TYPE_INSTANCE_PRIVATE(struct b_stream_p); - B_TYPE_INSTANCE_PROTECTED(b_stream_cfg); - B_TYPE_INSTANCE_INIT(stream_init); - B_TYPE_INSTANCE_FINI(stream_fini); -B_TYPE_DEFINITION_END(b_stream) +FX_TYPE_DEFINITION_BEGIN(fx_stream) + FX_TYPE_ID(0xa2c98988, 0x30e5, 0x47c7, 0x88cd, 0x6c8ea79f69cd); + FX_TYPE_CLASS(fx_stream_class); + FX_TYPE_INSTANCE_PRIVATE(struct fx_stream_p); + FX_TYPE_INSTANCE_PROTECTED(fx_stream_cfg); + FX_TYPE_INSTANCE_INIT(stream_init); + FX_TYPE_INSTANCE_FINI(stream_fini); +FX_TYPE_DEFINITION_END(fx_stream) -// ---- b_stream_buffer DEFINITION -B_TYPE_CLASS_DEFINITION_BEGIN(b_stream_buffer) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) -B_TYPE_CLASS_DEFINITION_END(b_stream_buffer) +// ---- fx_stream_buffer DEFINITION +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_stream_buffer) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_DEFINITION_END(fx_stream_buffer) -B_TYPE_DEFINITION_BEGIN(b_stream_buffer) - B_TYPE_ID(0x575c7be1, 0x665f, 0x41f8, 0xbfed, 0x6269a2985be0); - B_TYPE_CLASS(b_stream_buffer_class); - B_TYPE_INSTANCE_PRIVATE(struct b_stream_buffer_p); - B_TYPE_INSTANCE_INIT(stream_buffer_init); - B_TYPE_INSTANCE_FINI(stream_buffer_fini); -B_TYPE_DEFINITION_END(b_stream_buffer) +FX_TYPE_DEFINITION_BEGIN(fx_stream_buffer) + FX_TYPE_ID(0x575c7be1, 0x665f, 0x41f8, 0xbfed, 0x6269a2985be0); + FX_TYPE_CLASS(fx_stream_buffer_class); + FX_TYPE_INSTANCE_PRIVATE(struct fx_stream_buffer_p); + FX_TYPE_INSTANCE_INIT(stream_buffer_init); + FX_TYPE_INSTANCE_FINI(stream_buffer_fini); +FX_TYPE_DEFINITION_END(fx_stream_buffer) -// ---- b_stdio_stream DEFINITION -B_TYPE_CLASS_DEFINITION_BEGIN(b_stdio_stream) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) +// ---- fx_stdio_stream DEFINITION +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_stdio_stream) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) - B_TYPE_CLASS_INTERFACE_BEGIN(b_stream, B_TYPE_STREAM) - B_INTERFACE_ENTRY(s_close) = NULL; - B_INTERFACE_ENTRY(s_seek) = stdio_seek; - B_INTERFACE_ENTRY(s_tell) = stdio_tell; - B_INTERFACE_ENTRY(s_getc) = NULL; - B_INTERFACE_ENTRY(s_read) = stdio_read; - B_INTERFACE_ENTRY(s_write) = stdio_write; - B_INTERFACE_ENTRY(s_reserve) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_stream, B_TYPE_STREAM) -B_TYPE_CLASS_DEFINITION_END(b_stdio_stream) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_stream, FX_TYPE_STREAM) + FX_INTERFACE_ENTRY(s_close) = NULL; + FX_INTERFACE_ENTRY(s_seek) = stdio_seek; + FX_INTERFACE_ENTRY(s_tell) = stdio_tell; + FX_INTERFACE_ENTRY(s_getc) = NULL; + FX_INTERFACE_ENTRY(s_read) = stdio_read; + FX_INTERFACE_ENTRY(s_write) = stdio_write; + FX_INTERFACE_ENTRY(s_reserve) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_stream, FX_TYPE_STREAM) +FX_TYPE_CLASS_DEFINITION_END(fx_stdio_stream) -B_TYPE_DEFINITION_BEGIN(b_stdio_stream) - B_TYPE_ID(0x67678926, 0xd0b7, 0x4f99, 0xb83c, 0x790927597645); - B_TYPE_EXTENDS(B_TYPE_STREAM); - B_TYPE_CLASS(b_stdio_stream_class); - B_TYPE_INSTANCE_PRIVATE(struct b_stdio_stream_p); - B_TYPE_INSTANCE_INIT(stdio_stream_init); - B_TYPE_INSTANCE_FINI(stdio_stream_fini); -B_TYPE_DEFINITION_END(b_stdio_stream) +FX_TYPE_DEFINITION_BEGIN(fx_stdio_stream) + FX_TYPE_ID(0x67678926, 0xd0b7, 0x4f99, 0xb83c, 0x790927597645); + FX_TYPE_EXTENDS(FX_TYPE_STREAM); + FX_TYPE_CLASS(fx_stdio_stream_class); + FX_TYPE_INSTANCE_PRIVATE(struct fx_stdio_stream_p); + FX_TYPE_INSTANCE_INIT(stdio_stream_init); + FX_TYPE_INSTANCE_FINI(stdio_stream_fini); +FX_TYPE_DEFINITION_END(fx_stdio_stream) diff --git a/core/stringstream.c b/core/stringstream.c index b5bee2a..fdce04b 100644 --- a/core/stringstream.c +++ b/core/stringstream.c @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -8,7 +8,7 @@ /*** PRIVATE DATA *************************************************************/ -struct b_stringstream_p { +struct fx_stringstream_p { char *ss_buf; size_t ss_ptr; size_t ss_len; @@ -18,31 +18,31 @@ struct b_stringstream_p { /*** PRIVATE FUNCTIONS ********************************************************/ -static enum b_status __getc(struct b_stringstream_p *ss, b_wchar *out) +static enum fx_status __getc(struct fx_stringstream_p *ss, fx_wchar *out) { size_t available = ss->ss_len - ss->ss_ptr; const char *p = ss->ss_buf + ss->ss_ptr; - size_t to_copy = b_wchar_utf8_codepoint_stride(p); + size_t to_copy = fx_wchar_utf8_codepoint_stride(p); if (to_copy > available) { - return B_ERR_BAD_STATE; + return FX_ERR_BAD_STATE; } - b_wchar c = b_wchar_utf8_codepoint_decode(p); + fx_wchar c = fx_wchar_utf8_codepoint_decode(p); *out = c; - if (c == B_WCHAR_INVALID) { - return B_ERR_BAD_STATE; + if (c == FX_WCHAR_INVALID) { + return FX_ERR_BAD_STATE; } ss->ss_ptr += to_copy; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status __gets( - struct b_stringstream_p *ss, char *s, size_t len, size_t *nr_read) +static enum fx_status __gets( + struct fx_stringstream_p *ss, char *s, size_t len, size_t *nr_read) { size_t available = ss->ss_len - ss->ss_ptr; size_t to_copy = len; @@ -59,11 +59,11 @@ static enum b_status __gets( *nr_read = to_copy; } - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status __puts( - struct b_stringstream_p *ss, const char *s, size_t len, size_t *nr_written) +static enum fx_status __puts( + struct fx_stringstream_p *ss, const char *s, size_t len, size_t *nr_written) { size_t available = ss->ss_max - ss->ss_len; size_t to_copy = len; @@ -71,7 +71,7 @@ static enum b_status __puts( if (to_copy > available && ss->ss_alloc == 1) { char *new_buf = realloc(ss->ss_buf, ss->ss_len + to_copy + 1); if (!new_buf) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } ss->ss_buf = new_buf; @@ -93,10 +93,10 @@ static enum b_status __puts( *nr_written = to_copy; } - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status stringstream_reset(struct b_stringstream_p *ss) +static enum fx_status stringstream_reset(struct fx_stringstream_p *ss) { ss->ss_len = 0; @@ -104,11 +104,11 @@ static enum b_status stringstream_reset(struct b_stringstream_p *ss) *ss->ss_buf = 0; } - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status stringstream_reset_with_buffer( - struct b_stringstream_p *ss, char *buf, size_t max) +static enum fx_status stringstream_reset_with_buffer( + struct fx_stringstream_p *ss, char *buf, size_t max) { ss->ss_len = 0; @@ -120,20 +120,20 @@ static enum b_status stringstream_reset_with_buffer( ss->ss_max = max; ss->ss_alloc = 0; - return B_SUCCESS; + return FX_SUCCESS; } -static size_t stringstream_get_length(const struct b_stringstream_p *strv) +static size_t stringstream_get_length(const struct fx_stringstream_p *strv) { return strv->ss_len; } -static const char *stringstream_ptr(const struct b_stringstream_p *ss) +static const char *stringstream_ptr(const struct fx_stringstream_p *ss) { return ss->ss_buf; } -static char *stringstream_steal(struct b_stringstream_p *ss) +static char *stringstream_steal(struct fx_stringstream_p *ss) { char *out = ss->ss_buf; @@ -146,17 +146,17 @@ static char *stringstream_steal(struct b_stringstream_p *ss) /*** PUBLIC FUNCTIONS *********************************************************/ -b_stringstream *b_stringstream_create_with_buffer(char *buf, size_t max) +fx_stringstream *fx_stringstream_create_with_buffer(char *buf, size_t max) { - b_stringstream *s = b_object_create(B_TYPE_STRINGSTREAM); + fx_stringstream *s = fx_object_create(FX_TYPE_STRINGSTREAM); if (!s) { return NULL; } - b_stream_cfg *cfg = b_object_get_protected(s, B_TYPE_STREAM); - struct b_stringstream_p *p = b_object_get_private(s, B_TYPE_STRINGSTREAM); + fx_stream_cfg *cfg = fx_object_get_protected(s, FX_TYPE_STREAM); + struct fx_stringstream_p *p = fx_object_get_private(s, FX_TYPE_STRINGSTREAM); - cfg->s_mode = B_STREAM_READ | B_STREAM_WRITE | Z__B_STREAM_STATIC; + cfg->s_mode = FX_STREAM_READ | FX_STREAM_WRITE | Z__FX_STREAM_STATIC; p->ss_buf = buf; p->ss_max = max; @@ -166,21 +166,21 @@ b_stringstream *b_stringstream_create_with_buffer(char *buf, size_t max) return s; } -b_stringstream *b_stringstream_create(void) +fx_stringstream *fx_stringstream_create(void) { - b_stringstream *s = b_object_create(B_TYPE_STRINGSTREAM); + fx_stringstream *s = fx_object_create(FX_TYPE_STRINGSTREAM); if (!s) { return NULL; } - b_stream_cfg *cfg = b_object_get_protected(s, B_TYPE_STREAM); - struct b_stringstream_p *p = b_object_get_private(s, B_TYPE_STRINGSTREAM); + fx_stream_cfg *cfg = fx_object_get_protected(s, FX_TYPE_STREAM); + struct fx_stringstream_p *p = fx_object_get_private(s, FX_TYPE_STRINGSTREAM); - cfg->s_mode = B_STREAM_READ | B_STREAM_WRITE | Z__B_STREAM_STATIC; + cfg->s_mode = FX_STREAM_READ | FX_STREAM_WRITE | Z__FX_STREAM_STATIC; p->ss_buf = malloc(DEFAULT_CAPACITY + 1); if (!p->ss_buf) { - b_stringstream_unref(s); + fx_stringstream_unref(s); return NULL; } @@ -192,106 +192,106 @@ b_stringstream *b_stringstream_create(void) return s; } -size_t b_stringstream_get_length(const b_stringstream *strv) +size_t fx_stringstream_get_length(const fx_stringstream *strv) { - B_CLASS_DISPATCH_STATIC_0( - B_TYPE_STRINGSTREAM, stringstream_get_length, strv); + FX_CLASS_DISPATCH_STATIC_0( + FX_TYPE_STRINGSTREAM, stringstream_get_length, strv); } -enum b_status b_stringstream_reset(b_stringstream *strv) +enum fx_status fx_stringstream_reset(fx_stringstream *strv) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_STRINGSTREAM, stringstream_reset, strv); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_STRINGSTREAM, stringstream_reset, strv); } -enum b_status b_stringstream_reset_with_buffer( - b_stringstream *strv, char *buf, size_t max) +enum fx_status fx_stringstream_reset_with_buffer( + fx_stringstream *strv, char *buf, size_t max) { - B_CLASS_DISPATCH_STATIC( - B_TYPE_STRINGSTREAM, stringstream_reset_with_buffer, strv, buf, + FX_CLASS_DISPATCH_STATIC( + FX_TYPE_STRINGSTREAM, stringstream_reset_with_buffer, strv, buf, max); } -const char *b_stringstream_ptr(const b_stringstream *ss) +const char *fx_stringstream_ptr(const fx_stringstream *ss) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_STRINGSTREAM, stringstream_ptr, ss); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_STRINGSTREAM, stringstream_ptr, ss); } -char *b_stringstream_steal(b_stringstream *ss) +char *fx_stringstream_steal(fx_stringstream *ss) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_STRINGSTREAM, stringstream_steal, ss); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_STRINGSTREAM, stringstream_steal, ss); } /*** PUBLIC ALIAS FUNCTIONS ***************************************************/ /*** VIRTUAL FUNCTIONS ********************************************************/ -static void stringstream_init(b_object *obj, void *priv) +static void stringstream_init(fx_object *obj, void *priv) { - struct b_stringstream_p *stream = priv; + struct fx_stringstream_p *stream = priv; } -static void stringstream_fini(b_object *obj, void *priv) +static void stringstream_fini(fx_object *obj, void *priv) { - struct b_stringstream_p *stream = priv; + struct fx_stringstream_p *stream = priv; if (stream->ss_alloc && stream->ss_buf) { free(stream->ss_buf); } } -enum b_status stream_getc(b_stream *stream, b_wchar *c) +enum fx_status stream_getc(fx_stream *stream, fx_wchar *c) { - struct b_stringstream_p *s - = b_object_get_private(stream, B_TYPE_STRINGSTREAM); + struct fx_stringstream_p *s + = fx_object_get_private(stream, FX_TYPE_STRINGSTREAM); - enum b_status status = __getc(s, c); + enum fx_status status = __getc(s, c); return status; } -enum b_status stream_read(b_stream *stream, void *buf, size_t count, size_t *nr_read) +enum fx_status stream_read(fx_stream *stream, void *buf, size_t count, size_t *nr_read) { - struct b_stringstream_p *s - = b_object_get_private(stream, B_TYPE_STRINGSTREAM); + struct fx_stringstream_p *s + = fx_object_get_private(stream, FX_TYPE_STRINGSTREAM); - enum b_status status = __gets(s, buf, count, nr_read); + enum fx_status status = __gets(s, buf, count, nr_read); return status; } -enum b_status stream_write( - b_stream *stream, const void *buf, size_t count, size_t *nr_written) +enum fx_status stream_write( + fx_stream *stream, const void *buf, size_t count, size_t *nr_written) { - struct b_stringstream_p *s - = b_object_get_private(stream, B_TYPE_STRINGSTREAM); + struct fx_stringstream_p *s + = fx_object_get_private(stream, FX_TYPE_STRINGSTREAM); - enum b_status status = __puts(s, (const char *)buf, count, nr_written); + enum fx_status status = __puts(s, (const char *)buf, count, nr_written); return status; } /*** CLASS DEFINITION *********************************************************/ -B_TYPE_CLASS_DEFINITION_BEGIN(b_stringstream) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_stringstream) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) - B_TYPE_CLASS_INTERFACE_BEGIN(b_stream, B_TYPE_STREAM) - B_INTERFACE_ENTRY(s_close) = NULL; - B_INTERFACE_ENTRY(s_seek) = NULL; - B_INTERFACE_ENTRY(s_tell) = NULL; - B_INTERFACE_ENTRY(s_getc) = stream_getc; - B_INTERFACE_ENTRY(s_read) = stream_read; - B_INTERFACE_ENTRY(s_write) = stream_write; - B_INTERFACE_ENTRY(s_reserve) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_stream, B_TYPE_STREAM) -B_TYPE_CLASS_DEFINITION_END(b_stringstream) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_stream, FX_TYPE_STREAM) + FX_INTERFACE_ENTRY(s_close) = NULL; + FX_INTERFACE_ENTRY(s_seek) = NULL; + FX_INTERFACE_ENTRY(s_tell) = NULL; + FX_INTERFACE_ENTRY(s_getc) = stream_getc; + FX_INTERFACE_ENTRY(s_read) = stream_read; + FX_INTERFACE_ENTRY(s_write) = stream_write; + FX_INTERFACE_ENTRY(s_reserve) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_stream, FX_TYPE_STREAM) +FX_TYPE_CLASS_DEFINITION_END(fx_stringstream) -B_TYPE_DEFINITION_BEGIN(b_stringstream) - B_TYPE_ID(0x508a609a, 0xfac5, 0x4d31, 0x843a, 0x44b68ad329f3); - B_TYPE_EXTENDS(B_TYPE_STREAM); - B_TYPE_CLASS(b_stringstream_class); - B_TYPE_INSTANCE_PRIVATE(struct b_stringstream_p); - B_TYPE_INSTANCE_INIT(stringstream_init); - B_TYPE_INSTANCE_FINI(stringstream_fini); -B_TYPE_DEFINITION_END(b_stringstream) +FX_TYPE_DEFINITION_BEGIN(fx_stringstream) + FX_TYPE_ID(0x508a609a, 0xfac5, 0x4d31, 0x843a, 0x44b68ad329f3); + FX_TYPE_EXTENDS(FX_TYPE_STREAM); + FX_TYPE_CLASS(fx_stringstream_class); + FX_TYPE_INSTANCE_PRIVATE(struct fx_stringstream_p); + FX_TYPE_INSTANCE_INIT(stringstream_init); + FX_TYPE_INSTANCE_FINI(stringstream_fini); +FX_TYPE_DEFINITION_END(fx_stringstream) diff --git a/core/sys/darwin/bitop.c b/core/sys/darwin/bitop.c index 141f252..3653953 100644 --- a/core/sys/darwin/bitop.c +++ b/core/sys/darwin/bitop.c @@ -1,16 +1,16 @@ -#include +#include -int b_popcountl(long v) +int fx_popcountl(long v) { return __builtin_popcountl(v); } -int b_ctzl(long v) +int fx_ctzl(long v) { return __builtin_ctzl(v); } -int b_clzl(long v) +int fx_clzl(long v) { return __builtin_clzl(v); } diff --git a/core/sys/darwin/random.c b/core/sys/darwin/random.c index 7effc91..cfe9406 100644 --- a/core/sys/darwin/random.c +++ b/core/sys/darwin/random.c @@ -2,7 +2,7 @@ #include #include -uint64_t z__b_platform_random_seed() +uint64_t z__fx_platform_random_seed() { int fd = open("/dev/urandom", O_RDONLY); if (fd == -1) { @@ -18,7 +18,7 @@ uint64_t z__b_platform_random_seed() return v; } -uint64_t z__b_platform_random_seed_secure() +uint64_t z__fx_platform_random_seed_secure() { int fd = open("/dev/random", O_RDONLY); if (fd == -1) { diff --git a/core/sys/darwin/thread.c b/core/sys/darwin/thread.c index 3a5e119..e0fc3ea 100644 --- a/core/sys/darwin/thread.c +++ b/core/sys/darwin/thread.c @@ -1,20 +1,20 @@ #include -#include -#include +#include +#include #include #include -struct b_thread { +struct fx_thread { pthread_t thr_p; - b_result thr_result; + fx_result thr_result; }; -static b_once thread_tls_init_once = B_ONCE_INIT; +static fx_once thread_tls_init_once = FX_ONCE_INIT; static pthread_key_t thread_tls_key = {0}; static void thread_dtor(void *p) { - struct b_thread *thread = p; + struct fx_thread *thread = p; } static void thread_tls_init() @@ -22,13 +22,13 @@ static void thread_tls_init() pthread_key_create(&thread_tls_key, thread_dtor); } -struct b_thread *b_thread_self(void) +struct fx_thread *fx_thread_self(void) { - if (b_init_once(&thread_tls_init_once)) { + if (fx_init_once(&thread_tls_init_once)) { thread_tls_init(); } - struct b_thread *thread = pthread_getspecific(thread_tls_key); + struct fx_thread *thread = pthread_getspecific(thread_tls_key); if (thread) { return thread; } @@ -45,17 +45,17 @@ struct b_thread *b_thread_self(void) return thread; } -bool b_mutex_lock(b_mutex *mut) +bool fx_mutex_lock(fx_mutex *mut) { return pthread_mutex_lock(mut) == 0; } -bool b_mutex_trylock(b_mutex *mut) +bool fx_mutex_trylock(fx_mutex *mut) { return pthread_mutex_trylock(mut) == 0; } -bool b_mutex_unlock(b_mutex *mut) +bool fx_mutex_unlock(fx_mutex *mut) { return pthread_mutex_unlock(mut) == 0; } diff --git a/core/sys/linux/bitop.c b/core/sys/linux/bitop.c index 141f252..3653953 100644 --- a/core/sys/linux/bitop.c +++ b/core/sys/linux/bitop.c @@ -1,16 +1,16 @@ -#include +#include -int b_popcountl(long v) +int fx_popcountl(long v) { return __builtin_popcountl(v); } -int b_ctzl(long v) +int fx_ctzl(long v) { return __builtin_ctzl(v); } -int b_clzl(long v) +int fx_clzl(long v) { return __builtin_clzl(v); } diff --git a/core/sys/linux/random.c b/core/sys/linux/random.c index 7effc91..cfe9406 100644 --- a/core/sys/linux/random.c +++ b/core/sys/linux/random.c @@ -2,7 +2,7 @@ #include #include -uint64_t z__b_platform_random_seed() +uint64_t z__fx_platform_random_seed() { int fd = open("/dev/urandom", O_RDONLY); if (fd == -1) { @@ -18,7 +18,7 @@ uint64_t z__b_platform_random_seed() return v; } -uint64_t z__b_platform_random_seed_secure() +uint64_t z__fx_platform_random_seed_secure() { int fd = open("/dev/random", O_RDONLY); if (fd == -1) { diff --git a/core/sys/windows/bitop.c b/core/sys/windows/bitop.c index b5778d4..339b244 100644 --- a/core/sys/windows/bitop.c +++ b/core/sys/windows/bitop.c @@ -1,4 +1,4 @@ -#include +#include #include #include @@ -16,7 +16,7 @@ static int check_popcnt_support() return (d[2] & 0x800000) ? CPU_FEATURE_YES : CPU_FEATURE_NO; } -int b_popcountl(long v) +int fx_popcountl(long v) { if (cpu_supports_popcnt == CPU_FEATURE_UNKNOWN) { cpu_supports_popcnt = check_popcnt_support(); @@ -34,7 +34,7 @@ int b_popcountl(long v) return 0; } -int b_ctzl(long v) +int fx_ctzl(long v) { unsigned long trailing_zero = 0; @@ -51,7 +51,7 @@ int b_ctzl(long v) } } -int b_clzl(long v) +int fx_clzl(long v) { unsigned long leading_zero = 0; diff --git a/core/sys/windows/random.c b/core/sys/windows/random.c index 2145d99..19010ad 100644 --- a/core/sys/windows/random.c +++ b/core/sys/windows/random.c @@ -5,7 +5,7 @@ #include #include -uint64_t z__b_platform_random_seed_secure(void) +uint64_t z__fx_platform_random_seed_secure(void) { BOOL status; HCRYPTPROV hCryptProv; @@ -24,7 +24,7 @@ uint64_t z__b_platform_random_seed_secure(void) return status == TRUE ? v : (uint64_t)-1; } -uint64_t z__b_platform_random_seed(void) +uint64_t z__fx_platform_random_seed(void) { - return z__b_platform_random_seed_secure(); + return z__fx_platform_random_seed_secure(); } \ No newline at end of file diff --git a/core/type.c b/core/type.c index 99b6aaf..edc0697 100644 --- a/core/type.c +++ b/core/type.c @@ -3,16 +3,16 @@ #include "class.h" #include "object.h" -#include -#include -#include -#include +#include +#include +#include +#include #include #include #include -static struct b_btree type_list = B_BTREE_INIT; -static union b_type zero_id = {0}; +static struct fx_bst type_list = FX_BTREE_INIT; +static union fx_type zero_id = {0}; struct type_init_ctx { size_t ctx_class_offset; @@ -20,37 +20,37 @@ struct type_init_ctx { }; static inline int registration_compare( - const struct b_type_registration *a, const struct b_type_registration *b) + const struct fx_type_registration *a, const struct fx_type_registration *b) { - return b_type_id_compare(&a->r_info->t_id, &b->r_info->t_id); + return fx_type_id_compare(&a->r_info->t_id, &b->r_info->t_id); } static inline int component_compare( - const struct b_type_component *a, const struct b_type_component *b) + const struct fx_type_component *a, const struct fx_type_component *b) { - return b_type_id_compare(&a->c_type->r_info->t_id, &b->c_type->r_info->t_id); + return fx_type_id_compare(&a->c_type->r_info->t_id, &b->c_type->r_info->t_id); } -B_BTREE_DEFINE_INSERT( - struct b_type_registration, r_node, r_info->r_id, put_type, +FX_BTREE_DEFINE_INSERT( + struct fx_type_registration, r_node, r_info->r_id, put_type, registration_compare) -B_BTREE_DEFINE_INSERT( - struct b_type_component, c_node, &c_type->r_info->t_id, +FX_BTREE_DEFINE_INSERT( + struct fx_type_component, c_node, &c_type->r_info->t_id, put_type_component, component_compare) -static struct b_type_registration *get_type( - const b_btree *tree, const union b_type *key) +static struct fx_type_registration *get_type( + const fx_bst *tree, const union fx_type *key) { - b_btree_node *cur = tree->b_root; + fx_bst_node *cur = tree->bst_root; while (cur) { - struct b_type_registration *cur_node - = b_unbox(struct b_type_registration, cur, r_node); - int cmp = b_type_id_compare(key, &cur_node->r_info->t_id); + struct fx_type_registration *cur_node + = fx_unbox(struct fx_type_registration, cur, r_node); + int cmp = fx_type_id_compare(key, &cur_node->r_info->t_id); if (cmp > 0) { - cur = b_btree_right(cur); + cur = fx_bst_right(cur); } else if (cmp < 0) { - cur = b_btree_left(cur); + cur = fx_bst_left(cur); } else { return cur_node; } @@ -59,19 +59,19 @@ static struct b_type_registration *get_type( return NULL; } -struct b_type_component *b_type_get_component( - const b_btree *tree, const union b_type *key) +struct fx_type_component *fx_type_get_component( + const fx_bst *tree, const union fx_type *key) { - b_btree_node *cur = tree->b_root; + fx_bst_node *cur = tree->bst_root; while (cur) { - struct b_type_component *cur_node - = b_unbox(struct b_type_component, cur, c_node); - int cmp = b_type_id_compare(key, &cur_node->c_type->r_info->t_id); + struct fx_type_component *cur_node + = fx_unbox(struct fx_type_component, cur, c_node); + int cmp = fx_type_id_compare(key, &cur_node->c_type->r_info->t_id); if (cmp > 0) { - cur = b_btree_right(cur); + cur = fx_bst_right(cur); } else if (cmp < 0) { - cur = b_btree_left(cur); + cur = fx_bst_left(cur); } else { return cur_node; } @@ -80,10 +80,10 @@ struct b_type_component *b_type_get_component( return NULL; } -static struct b_type_component *create_type_component( - const struct b_type_registration *type_reg) +static struct fx_type_component *create_type_component( + const struct fx_type_registration *type_reg) { - struct b_type_component *c = malloc(sizeof *c); + struct fx_type_component *c = malloc(sizeof *c); if (!c) { return NULL; } @@ -95,15 +95,15 @@ static struct b_type_component *create_type_component( return c; } -void b_type_id_init( - union b_type *out, uint32_t a, uint16_t b, uint16_t c, uint16_t d, +void fx_type_id_init( + union fx_type *out, uint32_t a, uint16_t b, uint16_t c, uint16_t d, uint64_t e) { - b_i32 x_a = b_i32_htob(a); - b_i16 x_b = b_i16_htob(b); - b_i16 x_c = b_i16_htob(c); - b_i16 x_d = b_i16_htob(d); - b_i64 x_e = b_i64_htob(e); + fx_i32 x_a = fx_i32_htob(a); + fx_i16 x_b = fx_i16_htob(b); + fx_i16 x_c = fx_i16_htob(c); + fx_i16 x_d = fx_i16_htob(d); + fx_i64 x_e = fx_i64_htob(e); memcpy(&out->b[0], x_a.i_bytes, sizeof x_a.i_bytes); memcpy(&out->b[4], x_b.i_bytes, sizeof x_b.i_bytes); @@ -113,7 +113,7 @@ void b_type_id_init( } static void initialise_type_component( - struct b_type_component *comp, const struct b_type_info *info, + struct fx_type_component *comp, const struct fx_type_info *info, struct type_init_ctx *init_ctx) { comp->c_class_data_offset = init_ctx->ctx_class_offset; @@ -129,43 +129,43 @@ static void initialise_type_component( init_ctx->ctx_instance_offset += comp->c_instance_protected_data_size; } -static b_result locate_interface( - b_type interface_id, struct b_type_registration *dest, +static fx_result locate_interface( + fx_type interface_id, struct fx_type_registration *dest, struct type_init_ctx *init_ctx) { - struct b_type_component *interface_comp - = b_type_get_component(&dest->r_components, interface_id); + struct fx_type_component *interface_comp + = fx_type_get_component(&dest->r_components, interface_id); if (interface_comp) { - return B_RESULT_SUCCESS; + return FX_RESULT_SUCCESS; } - struct b_type_registration *interface_reg + struct fx_type_registration *interface_reg = get_type(&type_list, interface_id); if (!interface_reg) { - return B_RESULT_ERR(NO_ENTRY); + return FX_RESULT_ERR(NO_ENTRY); } interface_comp = create_type_component(interface_reg); if (!interface_comp) { - return B_RESULT_ERR(NO_MEMORY); + return FX_RESULT_ERR(NO_MEMORY); } initialise_type_component(interface_comp, interface_reg->r_info, init_ctx); put_type_component(&dest->r_components, interface_comp); - return B_RESULT_SUCCESS; + return FX_RESULT_SUCCESS; } -static b_result locate_interfaces( - const union b_type *interfaces, size_t nr_interfaces, - struct b_type_registration *dest, struct type_init_ctx *init_ctx) +static fx_result locate_interfaces( + const union fx_type *interfaces, size_t nr_interfaces, + struct fx_type_registration *dest, struct type_init_ctx *init_ctx) { - b_result result = B_RESULT_SUCCESS; + fx_result result = FX_RESULT_SUCCESS; for (size_t i = 0; i < nr_interfaces; i++) { - b_type interface_id = &interfaces[i]; + fx_type interface_id = &interfaces[i]; result = locate_interface(interface_id, dest, init_ctx); - if (b_result_is_error(result)) { + if (fx_result_is_error(result)) { break; } } @@ -173,45 +173,45 @@ static b_result locate_interfaces( return result; } -static b_result find_type_components(struct b_type_registration *reg) +static fx_result find_type_components(struct fx_type_registration *reg) { - const struct b_type_info *current = reg->r_info; - struct b_type_component *comp = create_type_component(reg); + const struct fx_type_info *current = reg->r_info; + struct fx_type_component *comp = create_type_component(reg); if (!comp) { - return B_RESULT_ERR(NO_MEMORY); + return FX_RESULT_ERR(NO_MEMORY); } struct type_init_ctx init_ctx = { - .ctx_instance_offset = sizeof(struct _b_object), - .ctx_class_offset = sizeof(struct _b_class), + .ctx_instance_offset = sizeof(struct _fx_object), + .ctx_class_offset = sizeof(struct _fx_class), }; put_type_component(®->r_components, comp); - b_queue_push_front(®->r_class_hierarchy, &comp->c_entry); + fx_queue_push_front(®->r_class_hierarchy, &comp->c_entry); - b_result result = locate_interfaces( + fx_result result = locate_interfaces( current->t_interfaces, current->t_nr_interfaces, reg, &init_ctx); - if (b_result_is_error(result)) { + if (fx_result_is_error(result)) { return result; } - b_type current_id = ¤t->t_parent_id; - if (!current_id || b_type_id_compare(current_id, &zero_id) == 0) { + fx_type current_id = ¤t->t_parent_id; + if (!current_id || fx_type_id_compare(current_id, &zero_id) == 0) { goto skip_class_hierarchy; } while (1) { - struct b_type_registration *dep_class + struct fx_type_registration *dep_class = get_type(&type_list, current_id); if (!dep_class) { - return B_RESULT_ERR(NO_ENTRY); + return FX_RESULT_ERR(NO_ENTRY); } - comp = b_type_get_component(®->r_components, current_id); + comp = fx_type_get_component(®->r_components, current_id); if (comp) { /* circular class dependency */ - // result = B_RESULT_ERR(INVALID_ARGUMENT); + // result = FX_RESULT_ERR(INVALID_ARGUMENT); // break; current_id = &dep_class->r_info->t_parent_id; continue; @@ -222,38 +222,38 @@ static b_result find_type_components(struct b_type_registration *reg) result = locate_interfaces( dep_class->r_info->t_interfaces, dep_class->r_info->t_nr_interfaces, reg, &init_ctx); - if (b_result_is_error(result)) { + if (fx_result_is_error(result)) { break; } put_type_component(®->r_components, comp); - b_queue_push_front(®->r_class_hierarchy, &comp->c_entry); + fx_queue_push_front(®->r_class_hierarchy, &comp->c_entry); - if (b_type_id_compare(current_id, B_TYPE_OBJECT) == 0) { + if (fx_type_id_compare(current_id, FX_TYPE_OBJECT) == 0) { break; } current_id = &dep_class->r_info->t_parent_id; } - b_queue_entry *entry = b_queue_first(®->r_class_hierarchy); + fx_queue_entry *entry = fx_queue_first(®->r_class_hierarchy); while (entry) { - comp = b_unbox(struct b_type_component, entry, c_entry); + comp = fx_unbox(struct fx_type_component, entry, c_entry); initialise_type_component(comp, comp->c_type->r_info, &init_ctx); - entry = b_queue_next(entry); + entry = fx_queue_next(entry); } - b_btree_node *node = b_btree_first(®->r_components); + fx_bst_node *node = fx_bst_first(®->r_components); while (node) { - comp = b_unbox(struct b_type_component, node, c_node); - if (comp->c_type->r_category == B_TYPE_CLASS) { + comp = fx_unbox(struct fx_type_component, node, c_node); + if (comp->c_type->r_category == FX_TYPE_CLASS) { /* this component was already initialised above */ - node = b_btree_next(node); + node = fx_bst_next(node); continue; } initialise_type_component(comp, comp->c_type->r_info, &init_ctx); - node = b_btree_next(node); + node = fx_bst_next(node); } skip_class_hierarchy: @@ -263,57 +263,57 @@ skip_class_hierarchy: return result; } -static bool type_has_base_class(struct b_type_info *info) +static bool type_has_base_class(struct fx_type_info *info) { - if (b_type_id_compare(&info->t_id, B_TYPE_OBJECT) == 0) { + if (fx_type_id_compare(&info->t_id, FX_TYPE_OBJECT) == 0) { return true; } - return b_type_id_compare(&info->t_parent_id, &zero_id) != 0; + return fx_type_id_compare(&info->t_parent_id, &zero_id) != 0; } -b_result b_type_register(struct b_type_info *info) +fx_result fx_type_register(struct fx_type_info *info) { if (!type_has_base_class(info)) { - b_type_id_copy(B_TYPE_OBJECT, &info->t_parent_id); + fx_type_id_copy(FX_TYPE_OBJECT, &info->t_parent_id); } - struct b_type_registration *r = get_type(&type_list, &info->t_id); + struct fx_type_registration *r = get_type(&type_list, &info->t_id); if (r) { - return B_RESULT_ERR(NAME_EXISTS); + return FX_RESULT_ERR(NAME_EXISTS); } r = malloc(sizeof *r); if (!r) { - return B_RESULT_ERR(NO_MEMORY); + return FX_RESULT_ERR(NO_MEMORY); } memset(r, 0x0, sizeof *r); - r->r_category = B_TYPE_CLASS; + r->r_category = FX_TYPE_CLASS; r->r_info = info; - b_result result = find_type_components(r); - if (b_result_is_error(result)) { + fx_result result = find_type_components(r); + if (fx_result_is_error(result)) { free(r); - return b_result_propagate(result); + return fx_result_propagate(result); } - result = b_class_instantiate(r, &r->r_class); + result = fx_class_instantiate(r, &r->r_class); if (!r->r_class) { free(r); - return b_error_with_msg_template_caused_by_error( - B_ERRORS_BUILTIN, B_ERR_TYPE_REGISTRATION_FAILURE, - result, B_MSG_TYPE_REGISTRATION_FAILURE, - B_ERROR_PARAM("typename", info->t_name)); + return fx_error_with_msg_template_caused_by_error( + FX_ERRORS_BUILTIN, FX_ERR_TYPE_REGISTRATION_FAILURE, + result, FX_MSG_TYPE_REGISTRATION_FAILURE, + FX_ERROR_PARAM("typename", info->t_name)); } put_type(&type_list, r); - return B_RESULT_SUCCESS; + return FX_RESULT_SUCCESS; } -struct b_type_registration *b_type_get_registration(b_type id) +struct fx_type_registration *fx_type_get_registration(fx_type id) { return get_type(&type_list, id); } diff --git a/core/type.h b/core/type.h index c728bdb..9e93af6 100644 --- a/core/type.h +++ b/core/type.h @@ -1,40 +1,40 @@ #ifndef _TYPE_H_ #define _TYPE_H_ -#include -#include -#include +#include +#include +#include #include -enum b_type_category { - B_TYPE_NONE = 0, - B_TYPE_CLASS, - B_TYPE_INTERFACE, +enum fx_type_category { + FX_TYPE_NONE = 0, + FX_TYPE_CLASS, + FX_TYPE_INTERFACE, }; -struct b_type_component { - struct b_btree_node c_node; - struct b_queue_entry c_entry; - const struct b_type_registration *c_type; +struct fx_type_component { + struct fx_bst_node c_node; + struct fx_queue_entry c_entry; + const struct fx_type_registration *c_type; size_t c_class_data_offset, c_class_data_size; size_t c_instance_private_data_offset, c_instance_private_data_size; size_t c_instance_protected_data_offset, c_instance_protected_data_size; }; -struct b_type_registration { - enum b_type_category r_category; - struct b_btree_node r_node; - const b_type_info *r_info; - struct _b_class *r_class; - struct b_btree r_components; - struct b_queue r_class_hierarchy; +struct fx_type_registration { + enum fx_type_category r_category; + struct fx_bst_node r_node; + const fx_type_info *r_info; + struct _fx_class *r_class; + struct fx_bst r_components; + struct fx_queue r_class_hierarchy; size_t r_instance_size, r_class_size; }; -extern struct b_type_registration *b_type_get_registration(b_type id); -extern struct b_type_component *b_type_get_component( - const b_btree *tree, const union b_type *key); +extern struct fx_type_registration *fx_type_get_registration(fx_type id); +extern struct fx_type_component *fx_type_get_component( + const fx_bst *tree, const union fx_type *key); #endif diff --git a/ds/CMakeLists.txt b/ds/CMakeLists.txt index 894e606..1ec4987 100644 --- a/ds/CMakeLists.txt +++ b/ds/CMakeLists.txt @@ -1,3 +1,3 @@ include(../cmake/Templates.cmake) -add_bluelib_module(NAME ds DEPENDENCIES core) +add_fx_module(NAME ds DEPENDENCIES core) diff --git a/ds/array.c b/ds/array.c index 480eb2f..b0119e8 100644 --- a/ds/array.c +++ b/ds/array.c @@ -1,51 +1,51 @@ -#include -#include -#include -#include +#include +#include +#include +#include #include #include /*** PRIVATE DATA *************************************************************/ -struct b_array_p { +struct fx_array_p { /* number of items in array */ size_t ar_len; /* maximum number of items that can currently be stored in array */ size_t ar_cap; - b_object **ar_data; + fx_object **ar_data; }; -struct b_array_iterator_p { - b_array *_a; - struct b_array_p *_a_p; +struct fx_array_iterator_p { + fx_array *_a; + struct fx_array_p *_a_p; /** The index of the current value */ size_t i; /** The current value */ - b_object *value; + fx_object *value; }; /*** PRIVATE FUNCTIONS ********************************************************/ -static b_status resize_array(struct b_array_p *array, size_t new_capacity) +static fx_status resize_array(struct fx_array_p *array, size_t new_capacity) { if (array->ar_cap < new_capacity) { void *new_data = realloc( - array->ar_data, new_capacity * sizeof(struct b_dsref *)); + array->ar_data, new_capacity * sizeof(struct fx_dsref *)); if (!new_data) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } array->ar_data = new_data; } else { for (size_t i = new_capacity; i < array->ar_len; i++) { - b_object_unref(array->ar_data[i]); + fx_object_unref(array->ar_data[i]); } void *new_data = realloc( - array->ar_data, new_capacity * sizeof(struct b_dsref *)); + array->ar_data, new_capacity * sizeof(struct fx_dsref *)); if (!new_data) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } array->ar_data = new_data; @@ -56,81 +56,81 @@ static b_status resize_array(struct b_array_p *array, size_t new_capacity) array->ar_len = new_capacity; } - return B_SUCCESS; + return FX_SUCCESS; } -static b_status array_insert(struct b_array_p *array, b_object *value, size_t at) +static fx_status array_insert(struct fx_array_p *array, fx_object *value, size_t at) { - if (at == B_NPOS) { + if (at == FX_NPOS) { at = array->ar_len; } if (at > array->ar_len) { - return B_ERR_OUT_OF_BOUNDS; + return FX_ERR_OUT_OF_BOUNDS; } - b_status status = B_SUCCESS; + fx_status status = FX_SUCCESS; if (array->ar_len + 1 > array->ar_cap) { status = resize_array(array, array->ar_cap + 8); - if (status != B_SUCCESS) { + if (status != FX_SUCCESS) { return status; } } - b_object **src = array->ar_data + at; - b_object **dest = src + 1; - size_t move_len = (array->ar_len - at) * sizeof(struct b_dsref *); + fx_object **src = array->ar_data + at; + fx_object **dest = src + 1; + size_t move_len = (array->ar_len - at) * sizeof(struct fx_dsref *); memmove(dest, src, move_len); - array->ar_data[at] = b_object_ref(value); + array->ar_data[at] = fx_object_ref(value); array->ar_len++; - return B_SUCCESS; + return FX_SUCCESS; } -static b_status array_remove(struct b_array_p *array, size_t at) +static fx_status array_remove(struct fx_array_p *array, size_t at) { if (at >= array->ar_len) { - return B_ERR_OUT_OF_BOUNDS; + return FX_ERR_OUT_OF_BOUNDS; } - b_object **src = array->ar_data + at; - b_object **dest = src + 1; - size_t move_len = array->ar_len * sizeof(struct b_dsref *); + fx_object **src = array->ar_data + at; + fx_object **dest = src + 1; + size_t move_len = array->ar_len * sizeof(struct fx_dsref *); - b_object_unref(array->ar_data[at]); + fx_object_unref(array->ar_data[at]); memmove(dest, src, move_len); array->ar_len--; - return B_SUCCESS; + return FX_SUCCESS; } -static b_status array_remove_front(struct b_array_p *array) +static fx_status array_remove_front(struct fx_array_p *array) { return array_remove(array, 0); } -static b_status array_remove_back(struct b_array_p *array) +static fx_status array_remove_back(struct fx_array_p *array) { return array_remove(array, array->ar_len - 1); } -static b_object *array_pop(struct b_array_p *array, size_t at) +static fx_object *array_pop(struct fx_array_p *array, size_t at) { if (at >= array->ar_len) { return NULL; } - b_object **src = array->ar_data + at; - b_object **dest = src + 1; - size_t move_len = array->ar_len * sizeof(struct b_dsref *); + fx_object **src = array->ar_data + at; + fx_object **dest = src + 1; + size_t move_len = array->ar_len * sizeof(struct fx_dsref *); - b_object *out = array->ar_data[at]; + fx_object *out = array->ar_data[at]; memmove(dest, src, move_len); @@ -139,17 +139,17 @@ static b_object *array_pop(struct b_array_p *array, size_t at) return out; } -static b_object *array_pop_front(struct b_array_p *array) +static fx_object *array_pop_front(struct fx_array_p *array) { return array_pop(array, 0); } -static b_object *array_pop_back(struct b_array_p *array) +static fx_object *array_pop_back(struct fx_array_p *array) { return array_pop(array, array->ar_len - 1); } -static b_object *array_at(const struct b_array_p *array, size_t at) +static fx_object *array_at(const struct fx_array_p *array, size_t at) { if (at >= array->ar_len) { return NULL; @@ -158,49 +158,49 @@ static b_object *array_at(const struct b_array_p *array, size_t at) return array->ar_data[at]; } -static b_object *array_get(struct b_array_p *array, size_t at) +static fx_object *array_get(struct fx_array_p *array, size_t at) { if (at >= array->ar_len) { return NULL; } - return b_object_ref(array->ar_data[at]); + return fx_object_ref(array->ar_data[at]); } -static size_t array_size(const struct b_array_p *array) +static size_t array_size(const struct fx_array_p *array) { return array->ar_len; } -static size_t array_capacity(const struct b_array_p *array) +static size_t array_capacity(const struct fx_array_p *array) { return array->ar_cap; } -static void array_clear(struct b_array_p *array) +static void array_clear(struct fx_array_p *array) { if (!array->ar_len) { return; } for (size_t i = 0; i < array->ar_len; i++) { - b_object_unref(array->ar_data[i]); + fx_object_unref(array->ar_data[i]); } - memset(array->ar_data, 0x0, array->ar_cap * sizeof(b_object *)); + memset(array->ar_data, 0x0, array->ar_cap * sizeof(fx_object *)); array->ar_len = 0; } /*** PUBLIC FUNCTIONS *********************************************************/ -b_array *b_array_create_with_values(b_object *const *values, size_t nr_values) +fx_array *fx_array_create_with_values(fx_object *const *values, size_t nr_values) { - b_array *array = b_array_create(); + fx_array *array = fx_array_create(); if (!array) { return NULL; } - struct b_array_p *p = b_object_get_private(array, B_TYPE_ARRAY); + struct fx_array_p *p = fx_object_get_private(array, FX_TYPE_ARRAY); size_t real_nr_values = 0; for (size_t i = 0; i < nr_values; i++) { @@ -211,106 +211,106 @@ b_array *b_array_create_with_values(b_object *const *values, size_t nr_values) p->ar_len = real_nr_values; p->ar_cap = real_nr_values; - p->ar_data = calloc(real_nr_values, sizeof(struct b_dsref *)); + p->ar_data = calloc(real_nr_values, sizeof(struct fx_dsref *)); if (!p->ar_data) { - b_array_unref(array); + fx_array_unref(array); return NULL; } size_t index = 0; for (size_t i = 0; i < nr_values; i++) { - p->ar_data[index++] = b_object_ref(values[i]); + p->ar_data[index++] = fx_object_ref(values[i]); } return array; } -b_status b_array_insert(b_array *array, b_object *value, size_t at) +fx_status fx_array_insert(fx_array *array, fx_object *value, size_t at) { - B_CLASS_DISPATCH_STATIC(B_TYPE_ARRAY, array_insert, array, value, at); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_ARRAY, array_insert, array, value, at); } -b_status b_array_remove(b_array *array, size_t at) +fx_status fx_array_remove(fx_array *array, size_t at) { - B_CLASS_DISPATCH_STATIC(B_TYPE_ARRAY, array_remove, array, at); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_ARRAY, array_remove, array, at); } -b_status b_array_remove_front(b_array *array) +fx_status fx_array_remove_front(fx_array *array) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_ARRAY, array_remove_front, array); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_remove_front, array); } -b_status b_array_remove_back(b_array *array) +fx_status fx_array_remove_back(fx_array *array) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_ARRAY, array_remove_back, array); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_remove_back, array); } -b_object *b_array_pop(b_array *array, size_t at) +fx_object *fx_array_pop(fx_array *array, size_t at) { - B_CLASS_DISPATCH_STATIC(B_TYPE_ARRAY, array_pop, array, at); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_ARRAY, array_pop, array, at); } -b_object *b_array_pop_front(b_array *array) +fx_object *fx_array_pop_front(fx_array *array) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_ARRAY, array_pop_front, array); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_pop_front, array); } -b_object *b_array_pop_back(b_array *array) +fx_object *fx_array_pop_back(fx_array *array) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_ARRAY, array_pop_back, array); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_pop_back, array); } -b_object *b_array_at(const b_array *array, size_t at) +fx_object *fx_array_at(const fx_array *array, size_t at) { - B_CLASS_DISPATCH_STATIC(B_TYPE_ARRAY, array_at, array, at); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_ARRAY, array_at, array, at); } -b_object *b_array_get(b_array *array, size_t at) +fx_object *fx_array_get(fx_array *array, size_t at) { - B_CLASS_DISPATCH_STATIC(B_TYPE_ARRAY, array_get, array, at); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_ARRAY, array_get, array, at); } -size_t b_array_size(const b_array *array) +size_t fx_array_size(const fx_array *array) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_ARRAY, array_size, array); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_size, array); } -size_t b_array_capacity(const b_array *array) +size_t fx_array_capacity(const fx_array *array) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_ARRAY, array_capacity, array); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_capacity, array); } -void b_array_clear(b_array *array) +void fx_array_clear(fx_array *array) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_ARRAY, array_clear, array); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_clear, array); } /*** PUBLIC ALIAS FUNCTIONS ***************************************************/ -b_status b_array_append(b_array *array, b_object *value) +fx_status fx_array_append(fx_array *array, fx_object *value) { - return b_array_insert(array, value, B_NPOS); + return fx_array_insert(array, value, FX_NPOS); } -b_status b_array_prepend(b_array *array, b_object *value) +fx_status fx_array_prepend(fx_array *array, fx_object *value) { - return b_array_insert(array, value, 0); + return fx_array_insert(array, value, 0); } /*** VIRTUAL FUNCTIONS ********************************************************/ -static void array_init(b_object *obj, void *priv) +static void array_init(fx_object *obj, void *priv) { - struct b_array_p *array = priv; + struct fx_array_p *array = priv; } -static void array_fini(b_object *obj, void *priv) +static void array_fini(fx_object *obj, void *priv) { - struct b_array_p *array = priv; + struct fx_array_p *array = priv; if (array->ar_data) { for (size_t i = 0; i < array->ar_len; i++) { - b_object_unref(array->ar_data[i]); + fx_object_unref(array->ar_data[i]); } free(array->ar_data); @@ -318,90 +318,90 @@ static void array_fini(b_object *obj, void *priv) } } -static void array_to_string(const b_object *obj, b_stream *out) +static void array_to_string(const fx_object *obj, fx_stream *out) { - struct b_array_p *array = b_object_get_private(obj, B_TYPE_ARRAY); + struct fx_array_p *array = fx_object_get_private(obj, FX_TYPE_ARRAY); if (!array->ar_len) { - b_stream_write_string(out, "[]", NULL); + fx_stream_write_string(out, "[]", NULL); return; } - b_stream_write_string(out, "[\n", NULL); + fx_stream_write_string(out, "[\n", NULL); - b_stream_push_indent(out, 1); + fx_stream_push_indent(out, 1); size_t len = array_size(array); for (size_t i = 0; i < array->ar_len; i++) { - b_object *value = array->ar_data[i]; - bool is_string = b_object_is_type(value, B_TYPE_STRING); + fx_object *value = array->ar_data[i]; + bool is_string = fx_object_is_type(value, FX_TYPE_STRING); if (is_string) { - b_stream_write_char(out, '"'); + fx_stream_write_char(out, '"'); } - b_object_to_string(value, out); + fx_object_to_string(value, out); if (is_string) { - b_stream_write_char(out, '"'); + fx_stream_write_char(out, '"'); } if (i < len - 1) { - b_stream_write_string(out, ",", NULL); + fx_stream_write_string(out, ",", NULL); } - b_stream_write_char(out, '\n'); + fx_stream_write_char(out, '\n'); } - b_stream_pop_indent(out); - b_stream_write_char(out, ']'); + fx_stream_pop_indent(out); + fx_stream_write_char(out, ']'); } /*** ITERATOR FUNCTIONS *******************************************************/ -static b_iterator *iterable_begin(b_object *obj) +static fx_iterator *iterable_begin(fx_object *obj) { - b_array_iterator *it_obj = b_object_create(B_TYPE_ARRAY_ITERATOR); - struct b_array_iterator_p *it - = b_object_get_private(it_obj, B_TYPE_ARRAY_ITERATOR); + fx_array_iterator *it_obj = fx_object_create(FX_TYPE_ARRAY_ITERATOR); + struct fx_array_iterator_p *it + = fx_object_get_private(it_obj, FX_TYPE_ARRAY_ITERATOR); it->_a = obj; - it->_a_p = b_object_get_private(obj, B_TYPE_ARRAY); + 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 { - b_iterator_set_status(it_obj, B_ERR_NO_DATA); + fx_iterator_set_status(it_obj, FX_ERR_NO_DATA); it->value = NULL; } return it_obj; } -static const b_iterator *iterable_cbegin(const b_object *obj) +static const fx_iterator *iterable_cbegin(const fx_object *obj) { - b_array_iterator *it_obj = b_object_create(B_TYPE_ARRAY_ITERATOR); - struct b_array_iterator_p *it - = b_object_get_private(it_obj, B_TYPE_ARRAY_ITERATOR); - it->_a = (b_array *)obj; - it->_a_p = b_object_get_private(obj, B_TYPE_ARRAY); + 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 { - b_iterator_set_status(it_obj, B_ERR_NO_DATA); + fx_iterator_set_status(it_obj, FX_ERR_NO_DATA); it->value = NULL; } return it_obj; } -static enum b_status iterator_move_next(const b_iterator *obj) +static enum fx_status iterator_move_next(const fx_iterator *obj) { - struct b_array_iterator_p *it - = b_object_get_private(obj, B_TYPE_ARRAY_ITERATOR); - struct b_array_p *array = it->_a_p; + struct fx_array_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_ARRAY_ITERATOR); + struct fx_array_p *array = it->_a_p; if (it->value == NULL || it->i >= array->ar_len) { return false; @@ -415,21 +415,21 @@ static enum b_status iterator_move_next(const b_iterator *obj) it->value = array->ar_data[it->i]; } - return (it->value != NULL) ? B_SUCCESS : B_ERR_NO_DATA; + return (it->value != NULL) ? FX_SUCCESS : FX_ERR_NO_DATA; } -static enum b_status iterator_erase(b_iterator *obj) +static enum fx_status iterator_erase(fx_iterator *obj) { - struct b_array_iterator_p *it - = b_object_get_private(obj, B_TYPE_ARRAY_ITERATOR); - struct b_array_p *array = it->_a_p; + struct fx_array_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_ARRAY_ITERATOR); + struct fx_array_p *array = it->_a_p; if (it->i >= array->ar_len) { - return B_ERR_OUT_OF_BOUNDS; + return FX_ERR_OUT_OF_BOUNDS; } if (array->ar_data[it->i] != it->value) { - return B_ERR_BAD_STATE; + return FX_ERR_BAD_STATE; } array_remove(array, it->i); @@ -440,30 +440,30 @@ static enum b_status iterator_erase(b_iterator *obj) it->value = NULL; } - return B_SUCCESS; + return FX_SUCCESS; } -static b_iterator_value iterator_get_value(b_iterator *obj) +static fx_iterator_value iterator_get_value(fx_iterator *obj) { - struct b_array_iterator_p *it - = b_object_get_private(obj, B_TYPE_ARRAY_ITERATOR); + struct fx_array_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_ARRAY_ITERATOR); - return B_ITERATOR_VALUE_PTR(it->value); + return FX_ITERATOR_VALUE_PTR(it->value); } -static const b_iterator_value iterator_get_cvalue(const b_iterator *obj) +static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj) { - struct b_array_iterator_p *it - = b_object_get_private(obj, B_TYPE_ARRAY_ITERATOR); + struct fx_array_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_ARRAY_ITERATOR); - return B_ITERATOR_VALUE_CPTR(it->value); + return FX_ITERATOR_VALUE_CPTR(it->value); } -static enum b_status iterator_is_valid(const b_iterator *obj) +static enum fx_status iterator_is_valid(const fx_iterator *obj) { - struct b_array_iterator_p *it - = b_object_get_private(obj, B_TYPE_ARRAY_ITERATOR); - struct b_array_p *array = it->_a_p; + struct fx_array_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_ARRAY_ITERATOR); + struct fx_array_p *array = it->_a_p; if (it->i >= array->ar_len) { return false; @@ -473,49 +473,49 @@ static enum b_status iterator_is_valid(const b_iterator *obj) return false; } - return (it->value != NULL) ? B_SUCCESS : B_ERR_NO_DATA; + return (it->value != NULL) ? FX_SUCCESS : FX_ERR_NO_DATA; } /*** CLASS DEFINITION *********************************************************/ -// ---- b_array DEFINITION -B_TYPE_CLASS_DEFINITION_BEGIN(b_array) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = array_to_string; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) +// ---- fx_array DEFINITION +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_array) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = array_to_string; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) - B_TYPE_CLASS_INTERFACE_BEGIN(b_iterable, B_TYPE_ITERABLE) - B_INTERFACE_ENTRY(it_begin) = iterable_begin; - B_INTERFACE_ENTRY(it_cbegin) = iterable_cbegin; - B_TYPE_CLASS_INTERFACE_END(b_iterable, B_TYPE_ITERABLE) -B_TYPE_CLASS_DEFINITION_END(b_array) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE) + FX_INTERFACE_ENTRY(it_begin) = iterable_begin; + FX_INTERFACE_ENTRY(it_cbegin) = iterable_cbegin; + FX_TYPE_CLASS_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE) +FX_TYPE_CLASS_DEFINITION_END(fx_array) -B_TYPE_DEFINITION_BEGIN(b_array) - B_TYPE_ID(0xe3c46da1, 0x5f37, 0x4e44, 0xb53b, 0xff5a6200191b); - B_TYPE_CLASS(b_array_class); - B_TYPE_IMPLEMENTS(B_TYPE_ITERABLE); - B_TYPE_INSTANCE_PRIVATE(struct b_array_p); - B_TYPE_INSTANCE_INIT(array_init); - B_TYPE_INSTANCE_FINI(array_fini); -B_TYPE_DEFINITION_END(b_array) +FX_TYPE_DEFINITION_BEGIN(fx_array) + FX_TYPE_ID(0xe3c46da1, 0x5f37, 0x4e44, 0xb53b, 0xff5a6200191b); + FX_TYPE_CLASS(fx_array_class); + FX_TYPE_IMPLEMENTS(FX_TYPE_ITERABLE); + FX_TYPE_INSTANCE_PRIVATE(struct fx_array_p); + FX_TYPE_INSTANCE_INIT(array_init); + FX_TYPE_INSTANCE_FINI(array_fini); +FX_TYPE_DEFINITION_END(fx_array) -// ---- b_array_iterator DEFINITION -B_TYPE_CLASS_DEFINITION_BEGIN(b_array_iterator) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) +// ---- fx_array_iterator DEFINITION +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_array_iterator) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) - B_TYPE_CLASS_INTERFACE_BEGIN(b_iterator, B_TYPE_ITERATOR) - B_INTERFACE_ENTRY(it_move_next) = iterator_move_next; - B_INTERFACE_ENTRY(it_erase) = iterator_erase; - B_INTERFACE_ENTRY(it_get_value) = iterator_get_value; - B_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue; - B_TYPE_CLASS_INTERFACE_END(b_iterator, B_TYPE_ITERATOR) -B_TYPE_CLASS_DEFINITION_END(b_array_iterator) + FX_TYPE_CLASS_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_CLASS_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR) +FX_TYPE_CLASS_DEFINITION_END(fx_array_iterator) -B_TYPE_DEFINITION_BEGIN(b_array_iterator) - B_TYPE_ID(0xe5e9e8b8, 0x14cb, 0x4192, 0x8138, 0xf45238a2ae73); - B_TYPE_EXTENDS(B_TYPE_ITERATOR); - B_TYPE_CLASS(b_array_iterator_class); - B_TYPE_INSTANCE_PRIVATE(struct b_array_iterator_p); -B_TYPE_DEFINITION_END(b_array_iterator) +FX_TYPE_DEFINITION_BEGIN(fx_array_iterator) + FX_TYPE_ID(0xe5e9e8b8, 0x14cb, 0x4192, 0x8138, 0xf45238a2ae73); + FX_TYPE_EXTENDS(FX_TYPE_ITERATOR); + FX_TYPE_CLASS(fx_array_iterator_class); + FX_TYPE_INSTANCE_PRIVATE(struct fx_array_iterator_p); +FX_TYPE_DEFINITION_END(fx_array_iterator) diff --git a/ds/bitbuffer.c b/ds/bitbuffer.c index 71ee411..f997423 100644 --- a/ds/bitbuffer.c +++ b/ds/bitbuffer.c @@ -1,8 +1,8 @@ -#include +#include /*** PRIVATE DATA *************************************************************/ -struct b_bitbuffer_p { +struct fx_bitbuffer_p { int x; }; @@ -11,28 +11,28 @@ struct b_bitbuffer_p { /*** PUBLIC ALIAS FUNCTIONS ***************************************************/ /*** VIRTUAL FUNCTIONS ********************************************************/ -static void bitbuffer_init(b_object *obj, void *priv) +static void bitbuffer_init(fx_object *obj, void *priv) { - struct b_bitbuffer_p *bitbuffer = priv; + struct fx_bitbuffer_p *bitbuffer = priv; } -static void bitbuffer_fini(b_object *obj, void *priv) +static void bitbuffer_fini(fx_object *obj, void *priv) { - struct b_bitbuffer_p *bitbuffer = priv; + struct fx_bitbuffer_p *bitbuffer = priv; } /*** CLASS DEFINITION *********************************************************/ -B_TYPE_CLASS_DEFINITION_BEGIN(b_bitbuffer) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) -B_TYPE_CLASS_DEFINITION_END(b_bitbuffer) +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_bitbuffer) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_DEFINITION_END(fx_bitbuffer) -B_TYPE_DEFINITION_BEGIN(b_bitbuffer) - B_TYPE_ID(0x628e33da, 0x3109, 0x4a5d, 0x98d5, 0xb0e4cb3ccb65); - B_TYPE_CLASS(b_bitbuffer_class); - B_TYPE_INSTANCE_PRIVATE(struct b_bitbuffer_p); - B_TYPE_INSTANCE_INIT(bitbuffer_init); - B_TYPE_INSTANCE_FINI(bitbuffer_fini); -B_TYPE_DEFINITION_END(b_bitbuffer) +FX_TYPE_DEFINITION_BEGIN(fx_bitbuffer) + FX_TYPE_ID(0x628e33da, 0x3109, 0x4a5d, 0x98d5, 0xb0e4cb3ccb65); + FX_TYPE_CLASS(fx_bitbuffer_class); + FX_TYPE_INSTANCE_PRIVATE(struct fx_bitbuffer_p); + FX_TYPE_INSTANCE_INIT(bitbuffer_init); + FX_TYPE_INSTANCE_FINI(bitbuffer_fini); +FX_TYPE_DEFINITION_END(fx_bitbuffer) diff --git a/ds/bitmap.c b/ds/bitmap.c index e831c66..b943ab7 100644 --- a/ds/bitmap.c +++ b/ds/bitmap.c @@ -1,6 +1,6 @@ -#include -#include -#include +#include +#include +#include #include #define BITS_PER_WORD (8 * sizeof(bitmap_word_t)) @@ -11,14 +11,14 @@ typedef unsigned long bitmap_word_t; -struct b_bitmap_p { +struct fx_bitmap_p { bitmap_word_t *map_words; size_t map_nr_words, map_nr_bits; }; /*** PRIVATE FUNCTIONS ********************************************************/ -static void bitmap_set_bit(struct b_bitmap_p *map, size_t bit) +static void bitmap_set_bit(struct fx_bitmap_p *map, size_t bit) { unsigned long index = bit / BITS_PER_WORD; unsigned long offset = (BITS_PER_WORD - bit - 1) & (BITS_PER_WORD - 1); @@ -27,7 +27,7 @@ static void bitmap_set_bit(struct b_bitmap_p *map, size_t bit) map->map_words[index] |= mask; } -static void bitmap_clear_bit(struct b_bitmap_p *map, size_t bit) +static void bitmap_clear_bit(struct fx_bitmap_p *map, size_t bit) { unsigned long index = bit / BITS_PER_WORD; unsigned long offset = bit & (BITS_PER_WORD - 1); @@ -36,25 +36,25 @@ static void bitmap_clear_bit(struct b_bitmap_p *map, size_t bit) map->map_words[index] &= ~mask; } -static void bitmap_set_range(struct b_bitmap_p *map, size_t first_bit, size_t nbits) +static void bitmap_set_range(struct fx_bitmap_p *map, size_t first_bit, size_t nbits) { } -static void bitmap_clear_range(struct b_bitmap_p *map, size_t first_bit, size_t nbits) +static void bitmap_clear_range(struct fx_bitmap_p *map, size_t first_bit, size_t nbits) { } -static void bitmap_set_all(struct b_bitmap_p *map) +static void bitmap_set_all(struct fx_bitmap_p *map) { memset(map->map_words, 0xFF, map->map_nr_words * sizeof(bitmap_word_t)); } -static void bitmap_clear_all(struct b_bitmap_p *map) +static void bitmap_clear_all(struct fx_bitmap_p *map) { memset(map->map_words, 0x00, map->map_nr_words * sizeof(bitmap_word_t)); } -static bool bitmap_check_bit(const struct b_bitmap_p *map, size_t bit) +static bool bitmap_check_bit(const struct fx_bitmap_p *map, size_t bit) { unsigned long index = bit / BITS_PER_WORD; unsigned long offset = (BITS_PER_WORD - bit - 1) & (BITS_PER_WORD - 1); @@ -63,12 +63,12 @@ static bool bitmap_check_bit(const struct b_bitmap_p *map, size_t bit) return (map->map_words[index] & mask) != 0; } -static size_t bitmap_count_set_bits(const struct b_bitmap_p *map) +static size_t bitmap_count_set_bits(const struct fx_bitmap_p *map) { size_t set_bits = 0; for (size_t i = 0; i < map->map_nr_words; i++) { - set_bits += b_popcountl(map->map_words[i]); + set_bits += fx_popcountl(map->map_words[i]); } if (set_bits > map->map_nr_bits) { @@ -78,12 +78,12 @@ static size_t bitmap_count_set_bits(const struct b_bitmap_p *map) return set_bits; } -static size_t bitmap_count_clear_bits(const struct b_bitmap_p *map) +static size_t bitmap_count_clear_bits(const struct fx_bitmap_p *map) { size_t clear_bits = 0; for (size_t i = 0; i < map->map_nr_words; i++) { - clear_bits += b_popcountl(~map->map_words[i]); + clear_bits += fx_popcountl(~map->map_words[i]); } if (clear_bits > map->map_nr_bits) { @@ -93,7 +93,7 @@ static size_t bitmap_count_clear_bits(const struct b_bitmap_p *map) return clear_bits; } -static size_t bitmap_highest_set_bit(const struct b_bitmap_p *map) +static size_t bitmap_highest_set_bit(const struct fx_bitmap_p *map) { unsigned long bit_index = 0; bitmap_word_t last_word = 0; @@ -107,13 +107,13 @@ static size_t bitmap_highest_set_bit(const struct b_bitmap_p *map) } if (last_word == 0x00) { - return B_NPOS; + return FX_NPOS; } - return bit_index + (BITS_PER_WORD - b_ctzl(last_word) - 1); + return bit_index + (BITS_PER_WORD - fx_ctzl(last_word) - 1); } -static size_t bitmap_highest_clear_bit(const struct b_bitmap_p *map) +static size_t bitmap_highest_clear_bit(const struct fx_bitmap_p *map) { unsigned long bit_index = 0; bitmap_word_t last_word = ~(bitmap_word_t)0; @@ -126,17 +126,17 @@ static size_t bitmap_highest_clear_bit(const struct b_bitmap_p *map) } if (last_word == ~(unsigned long)0) { - return B_NPOS; + return FX_NPOS; } if (last_word == 0) { return bit_index + BITS_PER_WORD - 1; } - return bit_index + (BITS_PER_WORD - b_ctzl(~last_word)) - 1; + return bit_index + (BITS_PER_WORD - fx_ctzl(~last_word)) - 1; } -static size_t bitmap_lowest_set_bit(const struct b_bitmap_p *map) +static size_t bitmap_lowest_set_bit(const struct fx_bitmap_p *map) { unsigned long bit_index = 0; bitmap_word_t last_word = 0; @@ -151,13 +151,13 @@ static size_t bitmap_lowest_set_bit(const struct b_bitmap_p *map) } if (last_word == 0x00) { - return B_NPOS; + return FX_NPOS; } - return bit_index + b_clzl(last_word); + return bit_index + fx_clzl(last_word); } -static size_t bitmap_lowest_clear_bit(const struct b_bitmap_p *map) +static size_t bitmap_lowest_clear_bit(const struct fx_bitmap_p *map) { unsigned long bit_index = 0; bitmap_word_t last_word = 0; @@ -176,116 +176,116 @@ static size_t bitmap_lowest_clear_bit(const struct b_bitmap_p *map) } if (last_word == (~(bitmap_word_t)0)) { - return B_NPOS; + return FX_NPOS; } - return bit_index + b_clzl(~last_word); + return bit_index + fx_clzl(~last_word); } /*** PUBLIC FUNCTIONS *********************************************************/ -b_bitmap *b_bitmap_create(size_t nr_bits) +fx_bitmap *fx_bitmap_create(size_t nr_bits) { - b_bitmap *map = b_object_create(B_TYPE_BITMAP); + fx_bitmap *map = fx_object_create(FX_TYPE_BITMAP); if (!map) { return NULL; } - struct b_bitmap_p *p = b_object_get_private(map, B_TYPE_BITMAP); + struct fx_bitmap_p *p = fx_object_get_private(map, FX_TYPE_BITMAP); p->map_nr_bits = nr_bits; p->map_nr_words = BITMAP_WORDS(nr_bits); p->map_words = calloc(p->map_nr_words, sizeof(bitmap_word_t)); if (!p->map_words) { - b_bitmap_unref(map); + fx_bitmap_unref(map); return NULL; } return map; } -void b_bitmap_set_bit(b_bitmap *map, size_t bit) +void fx_bitmap_set_bit(fx_bitmap *map, size_t bit) { - B_CLASS_DISPATCH_STATIC_V(B_TYPE_BITMAP, bitmap_set_bit, map, bit); + FX_CLASS_DISPATCH_STATIC_V(FX_TYPE_BITMAP, bitmap_set_bit, map, bit); } -void b_bitmap_clear_bit(b_bitmap *map, size_t bit) +void fx_bitmap_clear_bit(fx_bitmap *map, size_t bit) { - B_CLASS_DISPATCH_STATIC_V(B_TYPE_BITMAP, bitmap_clear_bit, map, bit); + FX_CLASS_DISPATCH_STATIC_V(FX_TYPE_BITMAP, bitmap_clear_bit, map, bit); } -void b_bitmap_set_range(b_bitmap *map, size_t first_bit, size_t nbits) +void fx_bitmap_set_range(fx_bitmap *map, size_t first_bit, size_t nbits) { - B_CLASS_DISPATCH_STATIC_V( - B_TYPE_BITMAP, bitmap_set_range, map, first_bit, nbits); + FX_CLASS_DISPATCH_STATIC_V( + FX_TYPE_BITMAP, bitmap_set_range, map, first_bit, nbits); } -void b_bitmap_clear_range(b_bitmap *map, size_t first_bit, size_t nbits) +void fx_bitmap_clear_range(fx_bitmap *map, size_t first_bit, size_t nbits) { - B_CLASS_DISPATCH_STATIC_V( - B_TYPE_BITMAP, bitmap_clear_range, map, first_bit, nbits); + FX_CLASS_DISPATCH_STATIC_V( + FX_TYPE_BITMAP, bitmap_clear_range, map, first_bit, nbits); } -void b_bitmap_set_all(b_bitmap *map) +void fx_bitmap_set_all(fx_bitmap *map) { - B_CLASS_DISPATCH_STATIC_V0(B_TYPE_BITMAP, bitmap_set_all, map); + FX_CLASS_DISPATCH_STATIC_V0(FX_TYPE_BITMAP, bitmap_set_all, map); } -void b_bitmap_clear_all(b_bitmap *map) +void fx_bitmap_clear_all(fx_bitmap *map) { - B_CLASS_DISPATCH_STATIC_V0(B_TYPE_BITMAP, bitmap_clear_all, map); + FX_CLASS_DISPATCH_STATIC_V0(FX_TYPE_BITMAP, bitmap_clear_all, map); } -bool b_bitmap_check_bit(const b_bitmap *map, size_t bit) +bool fx_bitmap_check_bit(const fx_bitmap *map, size_t bit) { - B_CLASS_DISPATCH_STATIC(B_TYPE_BITMAP, bitmap_check_bit, map, bit); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_BITMAP, bitmap_check_bit, map, bit); } -size_t b_bitmap_count_set_bits(const b_bitmap *map) +size_t fx_bitmap_count_set_bits(const fx_bitmap *map) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_BITMAP, bitmap_count_set_bits, map); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_BITMAP, bitmap_count_set_bits, map); } -size_t b_bitmap_count_clear_bits(const b_bitmap *map) +size_t fx_bitmap_count_clear_bits(const fx_bitmap *map) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_BITMAP, bitmap_count_clear_bits, map); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_BITMAP, bitmap_count_clear_bits, map); } -size_t b_bitmap_highest_set_bit(const b_bitmap *map) +size_t fx_bitmap_highest_set_bit(const fx_bitmap *map) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_BITMAP, bitmap_highest_set_bit, map); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_BITMAP, bitmap_highest_set_bit, map); } -size_t b_bitmap_highest_clear_bit(const b_bitmap *map) +size_t fx_bitmap_highest_clear_bit(const fx_bitmap *map) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_BITMAP, bitmap_highest_clear_bit, map); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_BITMAP, bitmap_highest_clear_bit, map); } -size_t b_bitmap_lowest_set_bit(const b_bitmap *map) +size_t fx_bitmap_lowest_set_bit(const fx_bitmap *map) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_BITMAP, bitmap_lowest_set_bit, map); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_BITMAP, bitmap_lowest_set_bit, map); } -size_t b_bitmap_lowest_clear_bit(const b_bitmap *map) +size_t fx_bitmap_lowest_clear_bit(const fx_bitmap *map) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_BITMAP, bitmap_lowest_clear_bit, map); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_BITMAP, bitmap_lowest_clear_bit, map); } /*** PUBLIC ALIAS FUNCTIONS ***************************************************/ /*** VIRTUAL FUNCTIONS ********************************************************/ -static void bitmap_init(b_object *obj, void *priv) +static void bitmap_init(fx_object *obj, void *priv) { - struct b_bitmap_p *map = priv; + struct fx_bitmap_p *map = priv; } -static void bitmap_fini(b_object *obj, void *priv) +static void bitmap_fini(fx_object *obj, void *priv) { - struct b_bitmap_p *map = priv; + struct fx_bitmap_p *map = priv; } -static void bitmap_to_string(const b_object *obj, b_stream *out) +static void bitmap_to_string(const fx_object *obj, fx_stream *out) { - const struct b_bitmap_p *map = b_object_get_private(obj, B_TYPE_BITMAP); + const struct fx_bitmap_p *map = fx_object_get_private(obj, FX_TYPE_BITMAP); unsigned char *bytes = (unsigned char *)map->map_words; size_t nr_bytes = map->map_nr_words * sizeof(bitmap_word_t); @@ -293,7 +293,7 @@ static void bitmap_to_string(const b_object *obj, b_stream *out) for (size_t i = 0; i < nr_bytes - 1; i++) { c = bytes[i]; - b_stream_write_fmt( + fx_stream_write_fmt( out, NULL, "%c%c%c%c%c%c%c%c", c & 0x80 ? '1' : '0', c & 0x40 ? '1' : '0', c & 0x20 ? '1' : '0', c & 0x10 ? '1' : '0', c & 0x08 ? '1' : '0', @@ -307,24 +307,24 @@ static void bitmap_to_string(const b_object *obj, b_stream *out) for (size_t i = 0; i < remaining_bits; i++) { - b_stream_write_fmt(out, NULL, "%c", (c & mask) ? '1' : '0'); + fx_stream_write_fmt(out, NULL, "%c", (c & mask) ? '1' : '0'); } } /*** CLASS DEFINITION *********************************************************/ -B_TYPE_CLASS_DEFINITION_BEGIN(b_bitmap) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = bitmap_to_string; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) -B_TYPE_CLASS_DEFINITION_END(b_bitmap) +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_bitmap) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = bitmap_to_string; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_DEFINITION_END(fx_bitmap) -B_TYPE_DEFINITION_BEGIN(b_bitmap) - B_TYPE_ID(0xea115cef, 0x8a63, 0x445f, 0x9474, 0xba9309d5dde8); - B_TYPE_CLASS(b_bitmap_class); - B_TYPE_INSTANCE_PRIVATE(struct b_bitmap_p); - B_TYPE_INSTANCE_INIT(bitmap_init); - B_TYPE_INSTANCE_FINI(bitmap_fini); -B_TYPE_DEFINITION_END(b_bitmap) +FX_TYPE_DEFINITION_BEGIN(fx_bitmap) + FX_TYPE_ID(0xea115cef, 0x8a63, 0x445f, 0x9474, 0xba9309d5dde8); + FX_TYPE_CLASS(fx_bitmap_class); + FX_TYPE_INSTANCE_PRIVATE(struct fx_bitmap_p); + FX_TYPE_INSTANCE_INIT(bitmap_init); + FX_TYPE_INSTANCE_FINI(bitmap_fini); +FX_TYPE_DEFINITION_END(fx_bitmap) /*** ITERATOR FUNCTIONS *******************************************************/ diff --git a/ds/buffer.c b/ds/buffer.c index 16935da..0fbba47 100644 --- a/ds/buffer.c +++ b/ds/buffer.c @@ -1,11 +1,11 @@ -#include -#include +#include +#include #include #include /*** PRIVATE DATA *************************************************************/ -struct b_buffer_p { +struct fx_buffer_p { /* number of items in buffer */ unsigned int buf_len; /* maximum number of items that can currently be stored in array */ @@ -17,13 +17,13 @@ struct b_buffer_p { /*** PRIVATE FUNCTIONS ********************************************************/ -static b_status resize_buffer(struct b_buffer_p *buffer, size_t new_capacity) +static fx_status resize_buffer(struct fx_buffer_p *buffer, size_t new_capacity) { if (buffer->buf_cap < new_capacity) { void *new_data = realloc( buffer->buf_data, new_capacity * buffer->buf_itemsz); if (!new_data) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } buffer->buf_data = new_data; @@ -31,7 +31,7 @@ static b_status resize_buffer(struct b_buffer_p *buffer, size_t new_capacity) void *new_data = realloc( buffer->buf_data, new_capacity * buffer->buf_itemsz); if (!new_data) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } buffer->buf_data = new_data; @@ -42,10 +42,10 @@ static b_status resize_buffer(struct b_buffer_p *buffer, size_t new_capacity) buffer->buf_len = new_capacity; } - return B_SUCCESS; + return FX_SUCCESS; } -static void *buffer_steal(struct b_buffer_p *buf) +static void *buffer_steal(struct fx_buffer_p *buf) { void *p = buf->buf_data; @@ -56,44 +56,44 @@ static void *buffer_steal(struct b_buffer_p *buf) return p; } -static enum b_status buffer_reserve(struct b_buffer_p *buf, size_t capacity) +static enum fx_status buffer_reserve(struct fx_buffer_p *buf, size_t capacity) { if (buf->buf_cap >= capacity) { - return B_SUCCESS; + return FX_SUCCESS; } return resize_buffer(buf, capacity); } -static enum b_status buffer_resize(struct b_buffer_p *buf, size_t length) +static enum fx_status buffer_resize(struct fx_buffer_p *buf, size_t length) { - enum b_status status = resize_buffer(buf, length); + enum fx_status status = resize_buffer(buf, length); - if (!B_OK(status)) { + if (!FX_OK(status)) { return status; } buf->buf_len = length; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status buffer_insert( - struct b_buffer_p *buffer, const void *p, size_t count, size_t at) +static enum fx_status buffer_insert( + struct fx_buffer_p *buffer, const void *p, size_t count, size_t at) { - if (at == B_NPOS) { + if (at == FX_NPOS) { at = buffer->buf_len; } if (at > buffer->buf_len) { - return B_ERR_OUT_OF_BOUNDS; + return FX_ERR_OUT_OF_BOUNDS; } - b_status status = B_SUCCESS; + fx_status status = FX_SUCCESS; if (buffer->buf_len + count > buffer->buf_cap) { status = resize_buffer(buffer, buffer->buf_cap + count); - if (status != B_SUCCESS) { + if (status != FX_SUCCESS) { return status; } } @@ -107,13 +107,13 @@ static enum b_status buffer_insert( memcpy(src, p, count * buffer->buf_itemsz); buffer->buf_len += count; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status buffer_remove(struct b_buffer_p *buffer, size_t at, size_t count) +static enum fx_status buffer_remove(struct fx_buffer_p *buffer, size_t at, size_t count) { if (at >= buffer->buf_len) { - return B_ERR_OUT_OF_BOUNDS; + return FX_ERR_OUT_OF_BOUNDS; } if (at + count >= buffer->buf_len) { @@ -128,15 +128,15 @@ static enum b_status buffer_remove(struct b_buffer_p *buffer, size_t at, size_t buffer->buf_len -= count; - return B_SUCCESS; + return FX_SUCCESS; } -static void *buffer_ptr(const struct b_buffer_p *buffer) +static void *buffer_ptr(const struct fx_buffer_p *buffer) { return buffer->buf_data; } -static void *buffer_get(const struct b_buffer_p *buffer, size_t at) +static void *buffer_get(const struct fx_buffer_p *buffer, size_t at) { if (at >= buffer->buf_len) { return NULL; @@ -145,56 +145,56 @@ static void *buffer_get(const struct b_buffer_p *buffer, size_t at) return (unsigned char *)buffer->buf_data + (at * buffer->buf_itemsz); } -static size_t buffer_size(const struct b_buffer_p *buffer) +static size_t buffer_size(const struct fx_buffer_p *buffer) { return buffer->buf_len; } -static size_t buffer_capacity(const struct b_buffer_p *buffer) +static size_t buffer_capacity(const struct fx_buffer_p *buffer) { return buffer->buf_cap; } -static enum b_status buffer_clear(struct b_buffer_p *buffer) +static enum fx_status buffer_clear(struct fx_buffer_p *buffer) { buffer->buf_len = 0; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status buffer_push_back( - struct b_buffer_p *buf, size_t count, void **p) +static enum fx_status buffer_push_back( + struct fx_buffer_p *buf, size_t count, void **p) { - enum b_status status = B_SUCCESS; + enum fx_status status = FX_SUCCESS; if (buf->buf_len + count > buf->buf_cap) { status = resize_buffer(buf, buf->buf_len + count); } - if (!B_OK(status)) { + if (!FX_OK(status)) { return status; } buf->buf_len += count; *p = buffer_get(buf, buf->buf_len - count); - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status buffer_push_front( - struct b_buffer_p *buf, size_t count, void **p) +static enum fx_status buffer_push_front( + struct fx_buffer_p *buf, size_t count, void **p) { - enum b_status status = B_SUCCESS; + enum fx_status status = FX_SUCCESS; if (buf->buf_len + count > buf->buf_cap) { status = resize_buffer(buf, buf->buf_len + count); } - if (!B_OK(status)) { + if (!FX_OK(status)) { return status; } void *src = buf->buf_data; - void *dest = b_buffer_get(buf->buf_data, count); + void *dest = fx_buffer_get(buf->buf_data, count); size_t len = count * buf->buf_itemsz; memmove(dest, src, len); @@ -202,82 +202,82 @@ static enum b_status buffer_push_front( *p = buffer_get(buf, 0); - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status buffer_pop_back(struct b_buffer_p *buf, size_t count) +static enum fx_status buffer_pop_back(struct fx_buffer_p *buf, size_t count) { if (count > buf->buf_len) { - return B_ERR_OUT_OF_BOUNDS; + return FX_ERR_OUT_OF_BOUNDS; } buf->buf_len -= count; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status buffer_pop_front(struct b_buffer_p *buf, size_t count) +static enum fx_status buffer_pop_front(struct fx_buffer_p *buf, size_t count) { if (count > buf->buf_len) { - return B_ERR_OUT_OF_BOUNDS; + return FX_ERR_OUT_OF_BOUNDS; } - void *src = b_buffer_get(buf->buf_data, count); + void *src = fx_buffer_get(buf->buf_data, count); void *dest = buf->buf_data; size_t len = (buf->buf_len - count) * buf->buf_itemsz; memmove(dest, src, len); buf->buf_len -= count; - return B_SUCCESS; + return FX_SUCCESS; } -static size_t buffer_get_size(const struct b_buffer_p *buf) +static size_t buffer_get_size(const struct fx_buffer_p *buf) { return buf->buf_len; } -static size_t buffer_get_item_size(const struct b_buffer_p *buf) +static size_t buffer_get_item_size(const struct fx_buffer_p *buf) { return buf->buf_itemsz; } -static size_t buffer_get_capacity(const struct b_buffer_p *buf) +static size_t buffer_get_capacity(const struct fx_buffer_p *buf) { return buf->buf_cap; } /*** PUBLIC FUNCTIONS *********************************************************/ -b_buffer *b_buffer_create(size_t item_sz) +fx_buffer *fx_buffer_create(size_t item_sz) { - b_buffer *buffer = b_object_create(B_TYPE_BUFFER); + fx_buffer *buffer = fx_object_create(FX_TYPE_BUFFER); if (!buffer) { return NULL; } - struct b_buffer_p *p = b_object_get_private(buffer, B_TYPE_BUFFER); + struct fx_buffer_p *p = fx_object_get_private(buffer, FX_TYPE_BUFFER); p->buf_itemsz = item_sz; return buffer; } -b_buffer *b_buffer_create_from_bytes(const void *buf, size_t len) +fx_buffer *fx_buffer_create_from_bytes(const void *buf, size_t len) { - b_buffer *buffer = b_object_create(B_TYPE_BUFFER); + fx_buffer *buffer = fx_object_create(FX_TYPE_BUFFER); if (!buffer) { return NULL; } - struct b_buffer_p *p = b_object_get_private(buffer, B_TYPE_BUFFER); + struct fx_buffer_p *p = fx_object_get_private(buffer, FX_TYPE_BUFFER); p->buf_len = len; p->buf_cap = len; p->buf_itemsz = 1; p->buf_data = calloc(len, 1); if (!p->buf_data) { - b_buffer_unref(buffer); + fx_buffer_unref(buffer); return NULL; } @@ -286,21 +286,21 @@ b_buffer *b_buffer_create_from_bytes(const void *buf, size_t len) return buffer; } -b_buffer *b_buffer_create_from_array(const void *buf, size_t item_sz, size_t len) +fx_buffer *fx_buffer_create_from_array(const void *buf, size_t item_sz, size_t len) { - b_buffer *buffer = b_object_create(B_TYPE_BUFFER); + fx_buffer *buffer = fx_object_create(FX_TYPE_BUFFER); if (!buffer) { return NULL; } - struct b_buffer_p *p = b_object_get_private(buffer, B_TYPE_BUFFER); + struct fx_buffer_p *p = fx_object_get_private(buffer, FX_TYPE_BUFFER); p->buf_len = len; p->buf_cap = len; p->buf_itemsz = item_sz; p->buf_data = calloc(len, item_sz); if (!p->buf_data) { - b_buffer_unref(buffer); + fx_buffer_unref(buffer); return NULL; } @@ -309,114 +309,114 @@ b_buffer *b_buffer_create_from_array(const void *buf, size_t item_sz, size_t len return buffer; } -void *b_buffer_steal(b_buffer *buf) +void *fx_buffer_steal(fx_buffer *buf) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_BUFFER, buffer_steal, buf); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_BUFFER, buffer_steal, buf); } -enum b_status b_buffer_reserve(b_buffer *buf, size_t capacity) +enum fx_status fx_buffer_reserve(fx_buffer *buf, size_t capacity) { - B_CLASS_DISPATCH_STATIC(B_TYPE_BUFFER, buffer_reserve, buf, capacity); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_BUFFER, buffer_reserve, buf, capacity); } -enum b_status b_buffer_resize(b_buffer *buf, size_t length) +enum fx_status fx_buffer_resize(fx_buffer *buf, size_t length) { - B_CLASS_DISPATCH_STATIC(B_TYPE_BUFFER, buffer_resize, buf, length); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_BUFFER, buffer_resize, buf, length); } -enum b_status b_buffer_insert( - b_buffer *buffer, const void *p, size_t count, size_t at) +enum fx_status fx_buffer_insert( + fx_buffer *buffer, const void *p, size_t count, size_t at) { - B_CLASS_DISPATCH_STATIC(B_TYPE_BUFFER, buffer_insert, buffer, p, count, at); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_BUFFER, buffer_insert, buffer, p, count, at); } -enum b_status b_buffer_remove(b_buffer *buffer, size_t at, size_t count) +enum fx_status fx_buffer_remove(fx_buffer *buffer, size_t at, size_t count) { - B_CLASS_DISPATCH_STATIC(B_TYPE_BUFFER, buffer_remove, buffer, at, count); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_BUFFER, buffer_remove, buffer, at, count); } -void *b_buffer_ptr(const b_buffer *buffer) +void *fx_buffer_ptr(const fx_buffer *buffer) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_BUFFER, buffer_ptr, buffer); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_BUFFER, buffer_ptr, buffer); } -void *b_buffer_get(const b_buffer *buffer, size_t at) +void *fx_buffer_get(const fx_buffer *buffer, size_t at) { - B_CLASS_DISPATCH_STATIC(B_TYPE_BUFFER, buffer_get, buffer, at); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_BUFFER, buffer_get, buffer, at); } -size_t b_buffer_size(const b_buffer *buffer) +size_t fx_buffer_size(const fx_buffer *buffer) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_BUFFER, buffer_size, buffer); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_BUFFER, buffer_size, buffer); } -size_t b_buffer_capacity(const b_buffer *buffer) +size_t fx_buffer_capacity(const fx_buffer *buffer) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_BUFFER, buffer_capacity, buffer); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_BUFFER, buffer_capacity, buffer); } -enum b_status b_buffer_clear(b_buffer *buffer) +enum fx_status fx_buffer_clear(fx_buffer *buffer) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_BUFFER, buffer_clear, buffer); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_BUFFER, buffer_clear, buffer); } -enum b_status b_buffer_push_back(b_buffer *buf, size_t count, void **p) +enum fx_status fx_buffer_push_back(fx_buffer *buf, size_t count, void **p) { - B_CLASS_DISPATCH_STATIC(B_TYPE_BUFFER, buffer_push_back, buf, count, p); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_BUFFER, buffer_push_back, buf, count, p); } -enum b_status b_buffer_push_front(b_buffer *buf, size_t count, void **p) +enum fx_status fx_buffer_push_front(fx_buffer *buf, size_t count, void **p) { - B_CLASS_DISPATCH_STATIC(B_TYPE_BUFFER, buffer_push_front, buf, count, p); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_BUFFER, buffer_push_front, buf, count, p); } -enum b_status b_buffer_pop_back(b_buffer *buf, size_t count) +enum fx_status fx_buffer_pop_back(fx_buffer *buf, size_t count) { - B_CLASS_DISPATCH_STATIC(B_TYPE_BUFFER, buffer_pop_back, buf, count); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_BUFFER, buffer_pop_back, buf, count); } -enum b_status b_buffer_pop_front(b_buffer *buf, size_t count) +enum fx_status fx_buffer_pop_front(fx_buffer *buf, size_t count) { - B_CLASS_DISPATCH_STATIC(B_TYPE_BUFFER, buffer_pop_front, buf, count); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_BUFFER, buffer_pop_front, buf, count); } -size_t b_buffer_get_size(const b_buffer *buf) +size_t fx_buffer_get_size(const fx_buffer *buf) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_BUFFER, buffer_get_size, buf); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_BUFFER, buffer_get_size, buf); } -size_t b_buffer_get_item_size(const b_buffer *buf) +size_t fx_buffer_get_item_size(const fx_buffer *buf) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_BUFFER, buffer_get_item_size, buf); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_BUFFER, buffer_get_item_size, buf); } -size_t b_buffer_get_capacity(const b_buffer *buf) +size_t fx_buffer_get_capacity(const fx_buffer *buf) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_BUFFER, buffer_get_capacity, buf); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_BUFFER, buffer_get_capacity, buf); } /*** PUBLIC ALIAS FUNCTIONS ***************************************************/ -enum b_status b_buffer_append(b_buffer *buffer, const void *p, size_t count) +enum fx_status fx_buffer_append(fx_buffer *buffer, const void *p, size_t count) { - return b_buffer_insert(buffer, p, count, B_NPOS); + return fx_buffer_insert(buffer, p, count, FX_NPOS); } -enum b_status b_buffer_prepend(b_buffer *buffer, const void *p, size_t count) +enum fx_status fx_buffer_prepend(fx_buffer *buffer, const void *p, size_t count) { - return b_buffer_insert(buffer, p, count, 0); + return fx_buffer_insert(buffer, p, count, 0); } /*** VIRTUAL FUNCTIONS ********************************************************/ -void buffer_init(b_object *obj, void *priv) +void buffer_init(fx_object *obj, void *priv) { - struct b_buffer_p *buffer = priv; + struct fx_buffer_p *buffer = priv; } -void buffer_fini(b_object *obj, void *priv) +void buffer_fini(fx_object *obj, void *priv) { - struct b_buffer_p *buffer = priv; + struct fx_buffer_p *buffer = priv; if (buffer->buf_data) { free(buffer->buf_data); @@ -426,16 +426,16 @@ void buffer_fini(b_object *obj, void *priv) /*** CLASS DEFINITION *********************************************************/ -B_TYPE_CLASS_DEFINITION_BEGIN(b_buffer) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) -B_TYPE_CLASS_DEFINITION_END(b_buffer) +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_buffer) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_DEFINITION_END(fx_buffer) -B_TYPE_DEFINITION_BEGIN(b_buffer) - B_TYPE_ID(0x323e6858, 0x7a43, 0x4484, 0xa6fb, 0xe3d1e47ae637); - B_TYPE_CLASS(b_buffer_class); - B_TYPE_INSTANCE_PRIVATE(struct b_buffer_p); - B_TYPE_INSTANCE_INIT(buffer_init); - B_TYPE_INSTANCE_FINI(buffer_fini); -B_TYPE_DEFINITION_END(b_buffer) +FX_TYPE_DEFINITION_BEGIN(fx_buffer) + FX_TYPE_ID(0x323e6858, 0x7a43, 0x4484, 0xa6fb, 0xe3d1e47ae637); + FX_TYPE_CLASS(fx_buffer_class); + FX_TYPE_INSTANCE_PRIVATE(struct fx_buffer_p); + FX_TYPE_INSTANCE_INIT(buffer_init); + FX_TYPE_INSTANCE_FINI(buffer_fini); +FX_TYPE_DEFINITION_END(fx_buffer) diff --git a/ds/datetime.c b/ds/datetime.c index 2614cd4..cb43bc5 100644 --- a/ds/datetime.c +++ b/ds/datetime.c @@ -1,10 +1,10 @@ -#include -#include -#include +#include +#include +#include /*** PRIVATE DATA *************************************************************/ -struct b_datetime_p { +struct fx_datetime_p { unsigned int dt_year, dt_month, dt_day; unsigned short dt_hour, dt_min, dt_sec; unsigned int dt_msec; @@ -16,7 +16,7 @@ struct b_datetime_p { /*** PRIVATE FUNCTIONS ********************************************************/ -static bool is_leap_year(const struct b_datetime_p *dt) +static bool is_leap_year(const struct fx_datetime_p *dt) { if ((dt->dt_year % 400) == 0) { return true; @@ -29,17 +29,17 @@ static bool is_leap_year(const struct b_datetime_p *dt) return false; } -static bool is_year_valid(const struct b_datetime_p *dt) +static bool is_year_valid(const struct fx_datetime_p *dt) { return dt->dt_year >= 0; } -static bool is_month_valid(const struct b_datetime_p *dt) +static bool is_month_valid(const struct fx_datetime_p *dt) { return dt->dt_month >= 1 && dt->dt_month <= 12; } -static bool is_day_valid(const struct b_datetime_p *dt) +static bool is_day_valid(const struct fx_datetime_p *dt) { if (dt->dt_day < 1) { return false; @@ -66,7 +66,7 @@ static bool is_day_valid(const struct b_datetime_p *dt) } } -static bool is_time_valid(const struct b_datetime_p *dt) +static bool is_time_valid(const struct fx_datetime_p *dt) { if (!(dt->dt_hour >= 0 && dt->dt_hour <= 23)) { return false; @@ -83,7 +83,7 @@ static bool is_time_valid(const struct b_datetime_p *dt) return true; } -static bool is_zone_valid(const struct b_datetime_p *dt) +static bool is_zone_valid(const struct fx_datetime_p *dt) { if (!(dt->dt_zone_offset_hour >= 0 && dt->dt_zone_offset_hour <= 23)) { return false; @@ -96,7 +96,7 @@ static bool is_zone_valid(const struct b_datetime_p *dt) return true; } -static bool validate(const struct b_datetime_p *dt) +static bool validate(const struct fx_datetime_p *dt) { if (dt->dt_has_date) { if (!is_year_valid(dt)) { @@ -125,14 +125,14 @@ static bool validate(const struct b_datetime_p *dt) return true; } -static b_datetime *parse_rfc3339(const char *s) +static fx_datetime *parse_rfc3339(const char *s) { - b_datetime *d = b_datetime_create(); + fx_datetime *d = fx_datetime_create(); if (!d) { return NULL; } - struct b_datetime_p *dt = b_object_get_private(d, B_TYPE_DATETIME); + struct fx_datetime_p *dt = fx_object_get_private(d, FX_TYPE_DATETIME); size_t len = strlen(s); @@ -305,37 +305,37 @@ static b_datetime *parse_rfc3339(const char *s) dt->dt_has_time = has_time; return d; fail: - b_datetime_unref(d); + fx_datetime_unref(d); return NULL; } -static enum b_status encode_rfc3339(const struct b_datetime_p *dt, b_stream *out) +static enum fx_status encode_rfc3339(const struct fx_datetime_p *dt, fx_stream *out) { if (dt->dt_has_date) { - b_stream_write_fmt( + fx_stream_write_fmt( out, NULL, "%04ld-%02ld-%02ld", dt->dt_year, dt->dt_month, dt->dt_day); } if (dt->dt_has_date && dt->dt_has_time) { - b_stream_write_char(out, 'T'); + fx_stream_write_char(out, 'T'); } if (dt->dt_has_time) { - b_stream_write_fmt( + fx_stream_write_fmt( out, NULL, "%02ld:%02ld:%02ld", dt->dt_hour, dt->dt_min, dt->dt_sec); if (dt->dt_msec > 0) { - b_stream_write_fmt(out, NULL, ".%04ld", dt->dt_msec); + fx_stream_write_fmt(out, NULL, ".%04ld", dt->dt_msec); } if (!dt->dt_localtime) { if (dt->dt_zone_offset_hour == 0 && dt->dt_zone_offset_minute == 0) { - b_stream_write_char(out, 'Z'); + fx_stream_write_char(out, 'Z'); } else { - b_stream_write_fmt( + fx_stream_write_fmt( out, NULL, "%c%02ld:%02ld", dt->dt_zone_offset_negative ? '-' : '+', dt->dt_zone_offset_hour, @@ -344,14 +344,14 @@ static enum b_status encode_rfc3339(const struct b_datetime_p *dt, b_stream *out } } - return B_SUCCESS; + return FX_SUCCESS; } static void datetime_to_string( - const struct b_datetime_p *dt, b_datetime_format format, b_stream *dest) + const struct fx_datetime_p *dt, fx_datetime_format format, fx_stream *dest) { switch (format) { - case B_DATETIME_FORMAT_RFC3339: + case FX_DATETIME_FORMAT_RFC3339: encode_rfc3339(dt, dest); break; default: @@ -359,79 +359,79 @@ static void datetime_to_string( } } -static bool datetime_is_localtime(const struct b_datetime_p *dt) +static bool datetime_is_localtime(const struct fx_datetime_p *dt) { return dt->dt_localtime; } -static bool datetime_has_date(const struct b_datetime_p *dt) +static bool datetime_has_date(const struct fx_datetime_p *dt) { return dt->dt_has_date; } -static bool datetime_has_time(const struct b_datetime_p *dt) +static bool datetime_has_time(const struct fx_datetime_p *dt) { return dt->dt_has_time; } -static long datetime_year(const struct b_datetime_p *dt) +static long datetime_year(const struct fx_datetime_p *dt) { return dt->dt_year; } -static long datetime_month(const struct b_datetime_p *dt) +static long datetime_month(const struct fx_datetime_p *dt) { return dt->dt_month; } -static long datetime_day(const struct b_datetime_p *dt) +static long datetime_day(const struct fx_datetime_p *dt) { return dt->dt_day; } -static long datetime_hour(const struct b_datetime_p *dt) +static long datetime_hour(const struct fx_datetime_p *dt) { return dt->dt_hour; } -static long datetime_minute(const struct b_datetime_p *dt) +static long datetime_minute(const struct fx_datetime_p *dt) { return dt->dt_min; } -static long datetime_second(const struct b_datetime_p *dt) +static long datetime_second(const struct fx_datetime_p *dt) { return dt->dt_sec; } -static long datetime_subsecond(const struct b_datetime_p *dt) +static long datetime_subsecond(const struct fx_datetime_p *dt) { return dt->dt_msec; } -static bool datetime_zone_offset_is_negative(const struct b_datetime_p *dt) +static bool datetime_zone_offset_is_negative(const struct fx_datetime_p *dt) { return dt->dt_zone_offset_negative; } -static long datetime_zone_offset_hour(const struct b_datetime_p *dt) +static long datetime_zone_offset_hour(const struct fx_datetime_p *dt) { return dt->dt_zone_offset_hour; } -static long datetime_zone_offset_minute(const struct b_datetime_p *dt) +static long datetime_zone_offset_minute(const struct fx_datetime_p *dt) { return dt->dt_zone_offset_minute; } /*** PUBLIC FUNCTIONS *********************************************************/ -b_datetime *b_datetime_parse(enum b_datetime_format format, const char *s) +fx_datetime *fx_datetime_parse(enum fx_datetime_format format, const char *s) { - b_datetime *dt = NULL; + fx_datetime *dt = NULL; switch (format) { - case B_DATETIME_FORMAT_RFC3339: + case FX_DATETIME_FORMAT_RFC3339: dt = parse_rfc3339(s); break; default: @@ -442,126 +442,126 @@ b_datetime *b_datetime_parse(enum b_datetime_format format, const char *s) return NULL; } - struct b_datetime_p *p = b_object_get_private(dt, B_TYPE_DATETIME); + struct fx_datetime_p *p = fx_object_get_private(dt, FX_TYPE_DATETIME); if (!validate(p)) { - b_datetime_unref(dt); + fx_datetime_unref(dt); return NULL; } return dt; } -void b_datetime_to_string( - const b_datetime *dt, b_datetime_format format, b_stream *dest) +void fx_datetime_to_string( + const fx_datetime *dt, fx_datetime_format format, fx_stream *dest) { - B_CLASS_DISPATCH_STATIC( - B_TYPE_DATETIME, datetime_to_string, dt, format, dest); + FX_CLASS_DISPATCH_STATIC( + FX_TYPE_DATETIME, datetime_to_string, dt, format, dest); } -bool b_datetime_is_localtime(const b_datetime *dt) +bool fx_datetime_is_localtime(const fx_datetime *dt) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_DATETIME, datetime_is_localtime, dt); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DATETIME, datetime_is_localtime, dt); } -bool b_datetime_has_date(const b_datetime *dt) +bool fx_datetime_has_date(const fx_datetime *dt) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_DATETIME, datetime_has_date, dt); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DATETIME, datetime_has_date, dt); } -bool b_datetime_has_time(const b_datetime *dt) +bool fx_datetime_has_time(const fx_datetime *dt) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_DATETIME, datetime_has_time, dt); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DATETIME, datetime_has_time, dt); } -long b_datetime_year(const b_datetime *dt) +long fx_datetime_year(const fx_datetime *dt) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_DATETIME, datetime_year, dt); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DATETIME, datetime_year, dt); } -long b_datetime_month(const b_datetime *dt) +long fx_datetime_month(const fx_datetime *dt) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_DATETIME, datetime_month, dt); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DATETIME, datetime_month, dt); } -long b_datetime_day(const b_datetime *dt) +long fx_datetime_day(const fx_datetime *dt) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_DATETIME, datetime_day, dt); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DATETIME, datetime_day, dt); } -long b_datetime_hour(const b_datetime *dt) +long fx_datetime_hour(const fx_datetime *dt) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_DATETIME, datetime_hour, dt); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DATETIME, datetime_hour, dt); } -long b_datetime_minute(const b_datetime *dt) +long fx_datetime_minute(const fx_datetime *dt) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_DATETIME, datetime_minute, dt); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DATETIME, datetime_minute, dt); } -long b_datetime_second(const b_datetime *dt) +long fx_datetime_second(const fx_datetime *dt) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_DATETIME, datetime_second, dt); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DATETIME, datetime_second, dt); } -long b_datetime_subsecond(const b_datetime *dt) +long fx_datetime_subsecond(const fx_datetime *dt) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_DATETIME, datetime_subsecond, dt); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DATETIME, datetime_subsecond, dt); } -bool b_datetime_zone_offset_is_negative(const b_datetime *dt) +bool fx_datetime_zone_offset_is_negative(const fx_datetime *dt) { - B_CLASS_DISPATCH_STATIC_0( - B_TYPE_DATETIME, datetime_zone_offset_is_negative, dt); + FX_CLASS_DISPATCH_STATIC_0( + FX_TYPE_DATETIME, datetime_zone_offset_is_negative, dt); } -long b_datetime_zone_offset_hour(const b_datetime *dt) +long fx_datetime_zone_offset_hour(const fx_datetime *dt) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_DATETIME, datetime_zone_offset_hour, dt); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DATETIME, datetime_zone_offset_hour, dt); } -long b_datetime_zone_offset_minute(const b_datetime *dt) +long fx_datetime_zone_offset_minute(const fx_datetime *dt) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_DATETIME, datetime_zone_offset_minute, dt); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DATETIME, datetime_zone_offset_minute, dt); } /*** VIRTUAL FUNCTIONS ********************************************************/ -static void datetime_init(b_object *obj, void *priv) +static void datetime_init(fx_object *obj, void *priv) { - struct b_datetime_p *dt = priv; + struct fx_datetime_p *dt = priv; } -static void datetime_fini(b_object *obj, void *priv) +static void datetime_fini(fx_object *obj, void *priv) { - struct b_datetime_p *dt = priv; + struct fx_datetime_p *dt = priv; } -static void _datetime_to_string(const b_object *obj, b_stream *out) +static void _datetime_to_string(const fx_object *obj, fx_stream *out) { - struct b_datetime_p *dt = b_object_get_private(obj, B_TYPE_DATETIME); + struct fx_datetime_p *dt = fx_object_get_private(obj, FX_TYPE_DATETIME); if (dt->dt_has_date) { - b_stream_write_fmt( + fx_stream_write_fmt( out, NULL, "%04ld-%02ld-%02ld", dt->dt_year, dt->dt_month, dt->dt_day); } if (dt->dt_has_date && dt->dt_has_time) { - b_stream_write_char(out, ' '); + fx_stream_write_char(out, ' '); } if (dt->dt_has_time) { - b_stream_write_fmt( + fx_stream_write_fmt( out, NULL, "%02ld:%02ld:%02ld", dt->dt_hour, dt->dt_min, dt->dt_sec); if (dt->dt_msec > 0) { - b_stream_write_fmt(out, NULL, ".%04ld", dt->dt_msec); + fx_stream_write_fmt(out, NULL, ".%04ld", dt->dt_msec); } if (!dt->dt_localtime) { - b_stream_write_fmt( + fx_stream_write_fmt( out, NULL, " %c%02ld:%02ld", dt->dt_zone_offset_negative ? '-' : '+', dt->dt_zone_offset_hour, @@ -572,16 +572,16 @@ static void _datetime_to_string(const b_object *obj, b_stream *out) /*** CLASS DEFINITION *********************************************************/ -B_TYPE_CLASS_DEFINITION_BEGIN(b_datetime) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = _datetime_to_string; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) -B_TYPE_CLASS_DEFINITION_END(b_datetime) +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_datetime) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = _datetime_to_string; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_DEFINITION_END(fx_datetime) -B_TYPE_DEFINITION_BEGIN(b_datetime) - B_TYPE_ID(0x06a6030b, 0x1e3c, 0x4be2, 0xbd23, 0xf34f4a8e68be); - B_TYPE_CLASS(b_datetime_class); - B_TYPE_INSTANCE_PRIVATE(struct b_datetime_p); - B_TYPE_INSTANCE_INIT(datetime_init); - B_TYPE_INSTANCE_FINI(datetime_fini); -B_TYPE_DEFINITION_END(b_datetime) +FX_TYPE_DEFINITION_BEGIN(fx_datetime) + FX_TYPE_ID(0x06a6030b, 0x1e3c, 0x4be2, 0xbd23, 0xf34f4a8e68be); + FX_TYPE_CLASS(fx_datetime_class); + FX_TYPE_INSTANCE_PRIVATE(struct fx_datetime_p); + FX_TYPE_INSTANCE_INIT(datetime_init); + FX_TYPE_INSTANCE_FINI(datetime_fini); +FX_TYPE_DEFINITION_END(fx_datetime) diff --git a/ds/dict.c b/ds/dict.c index 7f5f0e2..6355448 100644 --- a/ds/dict.c +++ b/ds/dict.c @@ -1,7 +1,7 @@ -#include -#include -#include -#include +#include +#include +#include +#include #include #include @@ -10,40 +10,40 @@ /*** PRIVATE DATA *************************************************************/ -struct b_dict_bucket_item { - b_queue_entry bi_entry; - b_string *bi_str; - b_object *bi_value; +struct fx_dict_bucket_item { + fx_queue_entry bi_entry; + fx_string *bi_str; + fx_object *bi_value; }; -struct b_dict_bucket { - b_btree_node bk_node; +struct fx_dict_bucket { + fx_bst_node bk_node; uint64_t bk_hash; - b_queue bk_items; + fx_queue bk_items; }; -struct b_dict_p { - b_btree d_buckets; +struct fx_dict_p { + fx_bst d_buckets; }; -struct b_dict_iterator_p { +struct fx_dict_iterator_p { size_t i; - b_dict_item item; + fx_dict_item item; - b_dict *_d; - struct b_dict_p *_d_p; - b_btree_node *_cbn; - b_queue_entry *_cqe; + fx_dict *_d; + struct fx_dict_p *_d_p; + fx_bst_node *_cbn; + fx_queue_entry *_cqe; }; /*** MISC FUNCTIONS ***********************************************************/ -static B_BTREE_DEFINE_SIMPLE_GET( - struct b_dict_bucket, uint64_t, bk_node, bk_hash, get_bucket); -static B_BTREE_DEFINE_SIMPLE_INSERT( - struct b_dict_bucket, bk_node, bk_hash, put_bucket); +static FX_BTREE_DEFINE_SIMPLE_GET( + struct fx_dict_bucket, uint64_t, bk_node, bk_hash, get_bucket); +static FX_BTREE_DEFINE_SIMPLE_INSERT( + struct fx_dict_bucket, bk_node, bk_hash, put_bucket); -uint64_t b_cstr_hash(const char *s) +uint64_t fx_cstr_hash(const char *s) { uint64_t hash = HASH_OFFSET_BASIS; @@ -57,9 +57,9 @@ uint64_t b_cstr_hash(const char *s) /*** PRIVATE FUNCTIONS ********************************************************/ -static struct b_dict_bucket *create_bucket(void) +static struct fx_dict_bucket *create_bucket(void) { - struct b_dict_bucket *bucket = malloc(sizeof *bucket); + struct fx_dict_bucket *bucket = malloc(sizeof *bucket); if (!bucket) { return NULL; } @@ -68,9 +68,9 @@ static struct b_dict_bucket *create_bucket(void) return bucket; } -static struct b_dict_bucket_item *create_bucket_item(void) +static struct fx_dict_bucket_item *create_bucket_item(void) { - struct b_dict_bucket_item *item = malloc(sizeof *item); + struct fx_dict_bucket_item *item = malloc(sizeof *item); if (!item) { return NULL; } @@ -79,149 +79,149 @@ static struct b_dict_bucket_item *create_bucket_item(void) return item; } -static b_status dict_put(struct b_dict_p *dict, const char *key, b_object *value) +static fx_status dict_put(struct fx_dict_p *dict, const char *key, fx_object *value) { - uint64_t hash = b_cstr_hash(key); - struct b_dict_bucket *bucket = get_bucket(&dict->d_buckets, hash); + uint64_t hash = fx_cstr_hash(key); + struct fx_dict_bucket *bucket = get_bucket(&dict->d_buckets, hash); if (!bucket) { bucket = create_bucket(); if (!bucket) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } bucket->bk_hash = hash; put_bucket(&dict->d_buckets, bucket); } - struct b_dict_bucket_item *item = create_bucket_item(); + struct fx_dict_bucket_item *item = create_bucket_item(); if (!item) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } - item->bi_str = b_string_create_from_cstr(key); - item->bi_value = b_object_ref(value); + item->bi_str = fx_string_create_from_cstr(key); + item->bi_value = fx_object_ref(value); - b_queue_push_back(&bucket->bk_items, &item->bi_entry); + fx_queue_push_back(&bucket->bk_items, &item->bi_entry); - return B_SUCCESS; + return FX_SUCCESS; } -static b_status dict_put_sk( - struct b_dict_p *dict, const b_string *key, b_object *value) +static fx_status dict_put_sk( + struct fx_dict_p *dict, const fx_string *key, fx_object *value) { - uint64_t hash = b_string_hash(key); - struct b_dict_bucket *bucket = get_bucket(&dict->d_buckets, hash); + 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 B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } bucket->bk_hash = hash; put_bucket(&dict->d_buckets, bucket); } - struct b_dict_bucket_item *item = create_bucket_item(); + struct fx_dict_bucket_item *item = create_bucket_item(); if (!item) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } - item->bi_str = b_string_duplicate(key); - item->bi_value = b_object_ref(value); + item->bi_str = fx_string_duplicate(key); + item->bi_value = fx_object_ref(value); - b_queue_push_back(&bucket->bk_items, &item->bi_entry); + fx_queue_push_back(&bucket->bk_items, &item->bi_entry); - return B_SUCCESS; + return FX_SUCCESS; } -static b_object *dict_at(const struct b_dict_p *dict, const char *key) +static fx_object *dict_at(const struct fx_dict_p *dict, const char *key) { - uint64_t hash = b_cstr_hash(key); - struct b_dict_bucket *bucket = get_bucket(&dict->d_buckets, hash); + uint64_t hash = fx_cstr_hash(key); + struct fx_dict_bucket *bucket = get_bucket(&dict->d_buckets, hash); if (!bucket) { return NULL; } - struct b_queue_entry *entry = b_queue_first(&bucket->bk_items); + struct fx_queue_entry *entry = fx_queue_first(&bucket->bk_items); while (entry) { - struct b_dict_bucket_item *item - = b_unbox(struct b_dict_bucket_item, entry, bi_entry); + struct fx_dict_bucket_item *item + = fx_unbox(struct fx_dict_bucket_item, entry, bi_entry); - if (!strcmp(b_string_ptr(item->bi_str), key)) { + if (!strcmp(fx_string_ptr(item->bi_str), key)) { return item->bi_value; } - entry = b_queue_next(entry); + entry = fx_queue_next(entry); } return NULL; } -static b_object *dict_at_sk(const struct b_dict_p *dict, const b_string *key) +static fx_object *dict_at_sk(const struct fx_dict_p *dict, const fx_string *key) { - uint64_t hash = b_string_hash(key); - struct b_dict_bucket *bucket = get_bucket(&dict->d_buckets, hash); + uint64_t hash = fx_string_hash(key); + struct fx_dict_bucket *bucket = get_bucket(&dict->d_buckets, hash); if (!bucket) { return NULL; } - struct b_queue_entry *entry = b_queue_first(&bucket->bk_items); + struct fx_queue_entry *entry = fx_queue_first(&bucket->bk_items); while (entry) { - struct b_dict_bucket_item *item - = b_unbox(struct b_dict_bucket_item, entry, bi_entry); + struct fx_dict_bucket_item *item + = fx_unbox(struct fx_dict_bucket_item, entry, bi_entry); - if (b_string_compare(item->bi_str, key)) { + if (fx_string_compare(item->bi_str, key)) { return item->bi_value; } - entry = b_queue_next(entry); + entry = fx_queue_next(entry); } return NULL; } -static b_object *dict_get(struct b_dict_p *dict, const char *key) +static fx_object *dict_get(struct fx_dict_p *dict, const char *key) { - b_object *value = dict_at(dict, key); + fx_object *value = dict_at(dict, key); if (value) { - b_object_ref(value); + fx_object_ref(value); } return value; } -static b_object *dict_get_sk(struct b_dict_p *dict, const b_string *key) +static fx_object *dict_get_sk(struct fx_dict_p *dict, const fx_string *key) { - b_object *value = dict_at_sk(dict, key); + fx_object *value = dict_at_sk(dict, key); if (value) { - b_object_ref(value); + fx_object_ref(value); } return value; } -static bool dict_has_key(const struct b_dict_p *dict, const char *key) +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 b_dict_p *dict, const b_string *key) +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 b_dict_p *dict) +static size_t dict_get_size(const struct fx_dict_p *dict) { size_t count = 0; #if 0 - b_btree_iterator it1; - b_queue_iterator it2; + fx_bst_iterator it1; + fx_queue_iterator it2; - b_btree_foreach (&it1, &dict->d_buckets) { - struct b_dict_bucket *bucket - = b_unbox(struct b_dict_bucket, it1.node, bk_node); + fx_bst_foreach (&it1, &dict->d_buckets) { + struct fx_dict_bucket *bucket + = fx_unbox(struct fx_dict_bucket, it1.node, bk_node); - b_queue_foreach (&it2, &bucket->bk_items) { + fx_queue_foreach (&it2, &bucket->bk_items) { count++; } } @@ -230,18 +230,18 @@ static size_t dict_get_size(const struct b_dict_p *dict) return count; } -static bool dict_is_empty(const struct b_dict_p *dict) +static bool dict_is_empty(const struct fx_dict_p *dict) { - b_btree_node *first_node = b_btree_first(&dict->d_buckets); - struct b_dict_bucket *first_bucket - = b_unbox(struct b_dict_bucket, first_node, bk_node); + 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; } - b_queue_entry *first_entry = b_queue_first(&first_bucket->bk_items); - struct b_dict_bucket_item *first_item - = b_unbox(struct b_dict_bucket_item, first_entry, bi_entry); + 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; } @@ -250,43 +250,43 @@ static bool dict_is_empty(const struct b_dict_p *dict) } static bool get_next_node( - struct b_btree_node *cur_node, struct b_queue_entry *cur_entry, - struct b_btree_node **out_next_node, struct b_queue_entry **out_next_entry) + 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 b_dict_bucket *cur_bucket - = b_unbox(struct b_dict_bucket, cur_node, bk_node); + struct fx_dict_bucket *cur_bucket + = fx_unbox(struct fx_dict_bucket, cur_node, bk_node); if (!cur_bucket) { return false; } - struct b_dict_bucket_item *cur_item - = b_unbox(struct b_dict_bucket_item, cur_entry, bi_entry); + struct fx_dict_bucket_item *cur_item + = fx_unbox(struct fx_dict_bucket_item, cur_entry, bi_entry); if (!cur_item) { return false; } - struct b_btree_node *next_node = cur_node; - struct b_queue_entry *next_entry = b_queue_next(cur_entry); + struct fx_bst_node *next_node = cur_node; + struct fx_queue_entry *next_entry = fx_queue_next(cur_entry); if (!next_entry) { - next_node = b_btree_next(cur_node); + next_node = fx_bst_next(cur_node); if (!next_node) { return false; } - struct b_dict_bucket *next_bucket - = b_unbox(struct b_dict_bucket, next_node, bk_node); + struct fx_dict_bucket *next_bucket + = fx_unbox(struct fx_dict_bucket, next_node, bk_node); if (!next_bucket) { return false; } - next_entry = b_queue_first(&next_bucket->bk_items); + next_entry = fx_queue_first(&next_bucket->bk_items); if (!next_entry) { return false; } } - struct b_dict_bucket_item *next_item - = b_unbox(struct b_dict_bucket_item, next_entry, bi_entry); + struct fx_dict_bucket_item *next_item + = fx_unbox(struct fx_dict_bucket_item, next_entry, bi_entry); if (!next_item) { return false; } @@ -297,32 +297,32 @@ static bool get_next_node( return true; } -static b_status delete_item( - struct b_dict_p *dict, struct b_dict_bucket *bucket, - struct b_dict_bucket_item *item) +static fx_status delete_item( + struct fx_dict_p *dict, struct fx_dict_bucket *bucket, + struct fx_dict_bucket_item *item) { - b_queue_delete(&bucket->bk_items, &item->bi_entry); + fx_queue_delete(&bucket->bk_items, &item->bi_entry); free(item); - if (b_queue_empty(&bucket->bk_items)) { - b_btree_delete(&dict->d_buckets, &bucket->bk_node); + if (fx_queue_empty(&bucket->bk_items)) { + fx_bst_delete(&dict->d_buckets, &bucket->bk_node); free(bucket); } - return B_SUCCESS; + return FX_SUCCESS; } /*** PUBLIC FUNCTIONS *********************************************************/ #if 0 -b_dict *b_dict_create_with_items(const b_dict_item *items) +fx_dict *fx_dict_create_with_items(const fx_dict_item *items) { - b_dict *dict = b_dict_create(); + fx_dict *dict = fx_dict_create(); if (!dict) { return NULL; } - struct b_dict_p *p = b_object_get_private(dict, B_TYPE_DICT); + 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); @@ -332,84 +332,84 @@ b_dict *b_dict_create_with_items(const b_dict_item *items) } #endif -b_status b_dict_put(b_dict *dict, const char *key, b_object *value) +fx_status fx_dict_put(fx_dict *dict, const char *key, fx_object *value) { - B_CLASS_DISPATCH_STATIC(B_TYPE_DICT, dict_put, dict, key, value); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_put, dict, key, value); } -b_status b_dict_put_sk(b_dict *dict, const b_string *key, b_object *value) +fx_status fx_dict_put_sk(fx_dict *dict, const fx_string *key, fx_object *value) { - B_CLASS_DISPATCH_STATIC(B_TYPE_DICT, dict_put_sk, dict, key, value); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_put_sk, dict, key, value); } -b_object *b_dict_at(const b_dict *dict, const char *key) +fx_object *fx_dict_at(const fx_dict *dict, const char *key) { - B_CLASS_DISPATCH_STATIC(B_TYPE_DICT, dict_at, dict, key); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_at, dict, key); } -b_object *b_dict_at_sk(const b_dict *dict, const b_string *key) +fx_object *fx_dict_at_sk(const fx_dict *dict, const fx_string *key) { - B_CLASS_DISPATCH_STATIC(B_TYPE_DICT, dict_at_sk, dict, key); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_at_sk, dict, key); } -b_object *b_dict_get(b_dict *dict, const char *key) +fx_object *fx_dict_get(fx_dict *dict, const char *key) { - B_CLASS_DISPATCH_STATIC(B_TYPE_DICT, dict_get, dict, key); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_get, dict, key); } -b_object *b_dict_get_sk(b_dict *dict, const b_string *key) +fx_object *fx_dict_get_sk(fx_dict *dict, const fx_string *key) { - B_CLASS_DISPATCH_STATIC(B_TYPE_DICT, dict_get_sk, dict, key); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_get_sk, dict, key); } -bool b_dict_has_key(const b_dict *dict, const char *key) +bool fx_dict_has_key(const fx_dict *dict, const char *key) { - B_CLASS_DISPATCH_STATIC(B_TYPE_DICT, dict_has_key, dict, key); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_has_key, dict, key); } -bool b_dict_has_skey(const b_dict *dict, const b_string *key) +bool fx_dict_has_skey(const fx_dict *dict, const fx_string *key) { - B_CLASS_DISPATCH_STATIC(B_TYPE_DICT, dict_has_skey, dict, key); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_has_skey, dict, key); } -size_t b_dict_get_size(const b_dict *dict) +size_t fx_dict_get_size(const fx_dict *dict) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_DICT, dict_get_size, dict); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DICT, dict_get_size, dict); } -bool b_dict_is_empty(const b_dict *dict) +bool fx_dict_is_empty(const fx_dict *dict) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_DICT, dict_is_empty, dict); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DICT, dict_is_empty, dict); } /*** PUBLIC ALIAS FUNCTIONS ***************************************************/ /*** VIRTUAL FUNCTIONS ********************************************************/ -static void dict_init(b_object *obj, void *priv) +static void dict_init(fx_object *obj, void *priv) { - struct b_dict_p *dict = priv; + struct fx_dict_p *dict = priv; } -static void dict_fini(b_object *obj, void *priv) +static void dict_fini(fx_object *obj, void *priv) { - struct b_dict_p *dict = priv; + struct fx_dict_p *dict = priv; - struct b_btree_node *node = b_btree_first(&dict->d_buckets); + struct fx_bst_node *node = fx_bst_first(&dict->d_buckets); while (node) { - struct b_dict_bucket *bucket - = b_unbox(struct b_dict_bucket, node, bk_node); - struct b_btree_node *next_node = b_btree_next(node); - b_btree_delete(&dict->d_buckets, 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 b_queue_entry *entry = b_queue_first(&bucket->bk_items); + struct fx_queue_entry *entry = fx_queue_first(&bucket->bk_items); while (entry) { - struct b_dict_bucket_item *item = b_unbox( - struct b_dict_bucket_item, entry, bi_entry); - struct b_queue_entry *next_entry = b_queue_next(entry); - b_queue_delete(&bucket->bk_items, 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); - b_object_unref(item->bi_value); + fx_object_unref(item->bi_value); free(item); entry = next_entry; @@ -420,102 +420,102 @@ static void dict_fini(b_object *obj, void *priv) } } -static void dict_to_string(const b_object *obj, b_stream *out) +static void dict_to_string(const fx_object *obj, fx_stream *out) { - struct b_dict_p *dict = b_object_get_private(obj, B_TYPE_DICT); + struct fx_dict_p *dict = fx_object_get_private(obj, FX_TYPE_DICT); if (dict_is_empty(dict)) { - b_stream_write_string(out, "{}", NULL); + fx_stream_write_string(out, "{}", NULL); return; } - b_stream_write_string(out, "{\n", NULL); + fx_stream_write_string(out, "{\n", NULL); - b_stream_push_indent(out, 1); + fx_stream_push_indent(out, 1); size_t len = dict_get_size(dict); size_t i = 0; - struct b_btree_node *node = b_btree_first(&dict->d_buckets); + struct fx_bst_node *node = fx_bst_first(&dict->d_buckets); while (node) { - struct b_dict_bucket *bucket - = b_unbox(struct b_dict_bucket, node, bk_node); + struct fx_dict_bucket *bucket + = fx_unbox(struct fx_dict_bucket, node, bk_node); - struct b_queue_entry *entry = b_queue_first(&bucket->bk_items); + struct fx_queue_entry *entry = fx_queue_first(&bucket->bk_items); while (entry) { - struct b_dict_bucket_item *item = b_unbox( - struct b_dict_bucket_item, entry, bi_entry); + struct fx_dict_bucket_item *item = fx_unbox( + struct fx_dict_bucket_item, entry, bi_entry); - b_object_to_string(item->bi_str, out); - b_stream_write_string(out, ": ", NULL); + fx_object_to_string(item->bi_str, out); + fx_stream_write_string(out, ": ", NULL); bool is_string - = b_object_is_type(item->bi_value, B_TYPE_STRING); + = fx_object_is_type(item->bi_value, FX_TYPE_STRING); if (is_string) { - b_stream_write_char(out, '"'); + fx_stream_write_char(out, '"'); } - b_object_to_string(item->bi_value, out); + fx_object_to_string(item->bi_value, out); if (is_string) { - b_stream_write_char(out, '"'); + fx_stream_write_char(out, '"'); } if (i < len - 1) { - b_stream_write_string(out, ",", NULL); + fx_stream_write_string(out, ",", NULL); } - b_stream_write_char(out, '\n'); - entry = b_queue_next(entry); + fx_stream_write_char(out, '\n'); + entry = fx_queue_next(entry); i++; } - node = b_btree_next(node); + node = fx_bst_next(node); } - b_stream_pop_indent(out); - b_stream_write_char(out, '}'); + fx_stream_pop_indent(out); + fx_stream_write_char(out, '}'); } /*** ITERATOR FUNCTIONS *******************************************************/ -static b_iterator *iterable_begin(b_dict *dict) +static fx_iterator *iterable_begin(fx_dict *dict) { - b_iterator *it_obj = b_object_create(B_TYPE_DICT_ITERATOR); + fx_iterator *it_obj = fx_object_create(FX_TYPE_DICT_ITERATOR); if (!it_obj) { return NULL; } - struct b_dict_iterator_p *it - = b_object_get_private(it_obj, B_TYPE_DICT_ITERATOR); + 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 = b_object_get_private(dict, B_TYPE_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; - b_iterator_set_status(it_obj, B_ERR_NO_DATA); + fx_iterator_set_status(it_obj, FX_ERR_NO_DATA); return it_obj; } - b_btree_node *first_node = b_btree_first(&it->_d_p->d_buckets); - struct b_dict_bucket *first_bucket - = b_unbox(struct b_dict_bucket, first_node, bk_node); + 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; - b_iterator_set_status(it_obj, B_ERR_NO_DATA); + fx_iterator_set_status(it_obj, FX_ERR_NO_DATA); return it_obj; } - b_queue_entry *first_entry = b_queue_first(&first_bucket->bk_items); - struct b_dict_bucket_item *first_item - = b_unbox(struct b_dict_bucket_item, first_entry, bi_entry); + 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; - b_iterator_set_status(it_obj, B_ERR_NO_DATA); + fx_iterator_set_status(it_obj, FX_ERR_NO_DATA); return it_obj; } @@ -529,44 +529,44 @@ static b_iterator *iterable_begin(b_dict *dict) return it_obj; } -static const b_iterator *iterable_cbegin(const b_dict *dict) +static const fx_iterator *iterable_cbegin(const fx_dict *dict) { - b_iterator *it_obj = b_object_create(B_TYPE_DICT_ITERATOR); + fx_iterator *it_obj = fx_object_create(FX_TYPE_DICT_ITERATOR); if (!it_obj) { return NULL; } - struct b_dict_iterator_p *it - = b_object_get_private(it_obj, B_TYPE_DICT_ITERATOR); + struct fx_dict_iterator_p *it + = fx_object_get_private(it_obj, FX_TYPE_DICT_ITERATOR); it->i = 0; - it->_d = (b_dict *)dict; - it->_d_p = b_object_get_private(dict, B_TYPE_DICT); + 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; - b_iterator_set_status(it_obj, B_ERR_NO_DATA); + fx_iterator_set_status(it_obj, FX_ERR_NO_DATA); return it_obj; } - b_btree_node *first_node = b_btree_first(&it->_d_p->d_buckets); - struct b_dict_bucket *first_bucket - = b_unbox(struct b_dict_bucket, first_node, bk_node); + 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; - b_iterator_set_status(it_obj, B_ERR_NO_DATA); + fx_iterator_set_status(it_obj, FX_ERR_NO_DATA); return it_obj; } - b_queue_entry *first_entry = b_queue_first(&first_bucket->bk_items); - struct b_dict_bucket_item *first_item - = b_unbox(struct b_dict_bucket_item, first_entry, bi_entry); + 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; - b_iterator_set_status(it_obj, B_ERR_NO_DATA); + fx_iterator_set_status(it_obj, FX_ERR_NO_DATA); return it_obj; } @@ -579,26 +579,26 @@ static const b_iterator *iterable_cbegin(const b_dict *dict) return it_obj; } -static enum b_status iterator_move_next(const b_iterator *obj) +static enum fx_status iterator_move_next(const fx_iterator *obj) { - struct b_dict_iterator_p *it - = b_object_get_private(obj, B_TYPE_DICT_ITERATOR); + struct fx_dict_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_DICT_ITERATOR); - struct b_btree_node *next_node; - struct b_queue_entry *next_entry; + 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 B_ERR_NO_DATA; + return FX_ERR_NO_DATA; } - struct b_dict_bucket_item *next_item - = b_unbox(struct b_dict_bucket_item, next_entry, bi_entry); + 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 B_ERR_NO_DATA; + return FX_ERR_NO_DATA; } it->i++; @@ -608,40 +608,40 @@ static enum b_status iterator_move_next(const b_iterator *obj) it->_cbn = next_node; it->_cqe = next_entry; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status iterator_erase(b_iterator *obj) +static enum fx_status iterator_erase(fx_iterator *obj) { - struct b_dict_iterator_p *it - = b_object_get_private(obj, B_TYPE_DICT_ITERATOR); + 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 B_ERR_BAD_STATE; + return FX_ERR_BAD_STATE; } if (!it->item.key || !it->_cqe) { - return B_ERR_NO_DATA; + return FX_ERR_NO_DATA; } - struct b_btree_node *next_node; - struct b_queue_entry *next_entry; + 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 B_ERR_NO_DATA; + return FX_ERR_NO_DATA; } - struct b_dict_bucket *cur_bucket - = b_unbox(struct b_dict_bucket, it->_cbn, bk_node); - struct b_dict_bucket_item *cur_item - = b_unbox(struct b_dict_bucket_item, it->_cqe, bi_entry); + 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 b_dict_bucket_item *next_item - = b_unbox(struct b_dict_bucket_item, next_entry, bi_entry); + struct fx_dict_bucket_item *next_item + = fx_unbox(struct fx_dict_bucket_item, next_entry, bi_entry); - b_status status = delete_item(it->_d_p, cur_bucket, cur_item); - if (B_ERR(status)) { + fx_status status = delete_item(it->_d_p, cur_bucket, cur_item); + if (FX_ERR(status)) { return status; } @@ -659,65 +659,65 @@ static enum b_status iterator_erase(b_iterator *obj) it->_cqe = NULL; } - return B_SUCCESS; + return FX_SUCCESS; } -static b_iterator_value iterator_get_value(b_iterator *obj) +static fx_iterator_value iterator_get_value(fx_iterator *obj) { - struct b_dict_iterator_p *it - = b_object_get_private(obj, B_TYPE_DICT_ITERATOR); + struct fx_dict_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_DICT_ITERATOR); - return B_ITERATOR_VALUE_PTR(&it->item); + return FX_ITERATOR_VALUE_PTR(&it->item); } -static const b_iterator_value iterator_get_cvalue(const b_iterator *obj) +static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj) { - struct b_dict_iterator_p *it - = b_object_get_private(obj, B_TYPE_DICT_ITERATOR); + struct fx_dict_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_DICT_ITERATOR); - return B_ITERATOR_VALUE_CPTR(&it->item); + return FX_ITERATOR_VALUE_CPTR(&it->item); } /*** CLASS DEFINITION *********************************************************/ -// ---- b_dict DEFINITION -B_TYPE_CLASS_DEFINITION_BEGIN(b_dict) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = dict_to_string; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) +// ---- fx_dict DEFINITION +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_dict) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = dict_to_string; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) - B_TYPE_CLASS_INTERFACE_BEGIN(b_iterable, B_TYPE_ITERABLE) - B_INTERFACE_ENTRY(it_begin) = iterable_begin; - B_INTERFACE_ENTRY(it_cbegin) = iterable_cbegin; - B_TYPE_CLASS_INTERFACE_END(b_iterable, B_TYPE_ITERABLE) -B_TYPE_CLASS_DEFINITION_END(b_dict) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE) + FX_INTERFACE_ENTRY(it_begin) = iterable_begin; + FX_INTERFACE_ENTRY(it_cbegin) = iterable_cbegin; + FX_TYPE_CLASS_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE) +FX_TYPE_CLASS_DEFINITION_END(fx_dict) -B_TYPE_DEFINITION_BEGIN(b_dict) - B_TYPE_ID(0xd2af61d9, 0xd0be, 0x4960, 0xbe3f, 0x509749814c10); - B_TYPE_CLASS(b_dict_class); - B_TYPE_IMPLEMENTS(B_TYPE_ITERABLE); - B_TYPE_INSTANCE_PRIVATE(struct b_dict_p); - B_TYPE_INSTANCE_INIT(dict_init); - B_TYPE_INSTANCE_FINI(dict_fini); -B_TYPE_DEFINITION_END(b_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) -// ---- b_dict_iterator DEFINITION -B_TYPE_CLASS_DEFINITION_BEGIN(b_dict_iterator) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) +// ---- fx_dict_iterator DEFINITION +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_dict_iterator) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) - B_TYPE_CLASS_INTERFACE_BEGIN(b_iterator, B_TYPE_ITERATOR) - B_INTERFACE_ENTRY(it_move_next) = iterator_move_next; - B_INTERFACE_ENTRY(it_erase) = iterator_erase; - B_INTERFACE_ENTRY(it_get_value) = iterator_get_value; - B_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue; - B_TYPE_CLASS_INTERFACE_END(b_iterator, B_TYPE_ITERATOR) -B_TYPE_CLASS_DEFINITION_END(b_dict_iterator) + FX_TYPE_CLASS_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_CLASS_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR) +FX_TYPE_CLASS_DEFINITION_END(fx_dict_iterator) -B_TYPE_DEFINITION_BEGIN(b_dict_iterator) - B_TYPE_ID(0x9ea96701, 0x1713, 0x4a3e, 0xbf63, 0xdc856b456f3b); - B_TYPE_EXTENDS(B_TYPE_ITERATOR); - B_TYPE_CLASS(b_dict_iterator_class); - B_TYPE_INSTANCE_PRIVATE(struct b_dict_iterator_p); -B_TYPE_DEFINITION_END(b_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) diff --git a/ds/hashmap.c b/ds/hashmap.c index 596e9fd..b5adf37 100644 --- a/ds/hashmap.c +++ b/ds/hashmap.c @@ -1,7 +1,7 @@ -#include -#include -#include -#include +#include +#include +#include +#include #include #include @@ -10,41 +10,41 @@ /*** PRIVATE DATA *************************************************************/ -struct b_hashmap_bucket_item { - struct b_queue_entry bi_entry; - struct b_hashmap_key bi_key; - struct b_hashmap_value bi_value; +struct fx_hashmap_bucket_item { + struct fx_queue_entry bi_entry; + struct fx_hashmap_key bi_key; + struct fx_hashmap_value bi_value; }; -struct b_hashmap_bucket { - struct b_btree_node bk_node; +struct fx_hashmap_bucket { + struct fx_bst_node bk_node; uint64_t bk_hash; - struct b_queue bk_items; + struct fx_queue bk_items; }; -struct b_hashmap_p { +struct fx_hashmap_p { size_t h_count; - struct b_btree h_buckets; - b_hashmap_key_destructor h_key_dtor; - b_hashmap_value_destructor h_value_dtor; + struct fx_bst h_buckets; + fx_hashmap_key_destructor h_key_dtor; + fx_hashmap_value_destructor h_value_dtor; }; -struct b_hashmap_iterator_p { +struct fx_hashmap_iterator_p { size_t i; - b_hashmap_item item; + fx_hashmap_item item; - b_hashmap *_h; - struct b_hashmap_p *_h_p; - b_btree_node *_cbn; - b_queue_entry *_cqe; + fx_hashmap *_h; + struct fx_hashmap_p *_h_p; + fx_bst_node *_cbn; + fx_queue_entry *_cqe; }; /*** PRIVATE FUNCTIONS ********************************************************/ -static B_BTREE_DEFINE_SIMPLE_GET( - struct b_hashmap_bucket, uint64_t, bk_node, bk_hash, get_bucket); -static B_BTREE_DEFINE_SIMPLE_INSERT( - struct b_hashmap_bucket, bk_node, bk_hash, put_bucket); +static FX_BTREE_DEFINE_SIMPLE_GET( + struct fx_hashmap_bucket, uint64_t, bk_node, bk_hash, get_bucket); +static FX_BTREE_DEFINE_SIMPLE_INSERT( + struct fx_hashmap_bucket, bk_node, bk_hash, put_bucket); static uint64_t hash_data(const void *p, size_t size) { @@ -59,9 +59,9 @@ static uint64_t hash_data(const void *p, size_t size) return hash; } -static uint64_t hash_key(const struct b_hashmap_key *key) +static uint64_t hash_key(const struct fx_hashmap_key *key) { - if (key->key_flags & B_HASHMAP_KEY_F_INTVALUE) { + if (key->key_flags & FX_HASHMAP_KEY_F_INTVALUE) { return hash_data(&key->key_data, sizeof key->key_data); } else { return hash_data(key->key_data, key->key_size); @@ -69,12 +69,12 @@ static uint64_t hash_key(const struct b_hashmap_key *key) } static bool compare_key( - const struct b_hashmap_key *a, const struct b_hashmap_key *b) + const struct fx_hashmap_key *a, const struct fx_hashmap_key *b) { - const void *a_data = NULL, *b_data = NULL; - size_t a_len = 0, b_len = 0; + const void *a_data = NULL, *fx_data = NULL; + size_t a_len = 0, fx_len = 0; - if (a->key_flags & B_HASHMAP_KEY_F_INTVALUE) { + if (a->key_flags & FX_HASHMAP_KEY_F_INTVALUE) { a_data = &a->key_data; a_len = sizeof a->key_data; } else { @@ -82,60 +82,60 @@ static bool compare_key( a_len = a->key_size; } - if (b->key_flags & B_HASHMAP_KEY_F_INTVALUE) { - b_data = &b->key_data; - b_len = sizeof b->key_data; + if (b->key_flags & FX_HASHMAP_KEY_F_INTVALUE) { + fx_data = &b->key_data; + fx_len = sizeof b->key_data; } else { - b_data = b->key_data; - b_len = b->key_size; + fx_data = b->key_data; + fx_len = b->key_size; } - if (a_len != b_len) { + if (a_len != fx_len) { return false; } size_t cmp_len = a_len; - return memcmp(a_data, b_data, cmp_len) == 0; + return memcmp(a_data, fx_data, cmp_len) == 0; } static bool get_next_node( - struct b_btree_node *cur_node, struct b_queue_entry *cur_entry, - struct b_btree_node **out_next_node, struct b_queue_entry **out_next_entry) + 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 b_hashmap_bucket *cur_bucket - = b_unbox(struct b_hashmap_bucket, cur_node, bk_node); + struct fx_hashmap_bucket *cur_bucket + = fx_unbox(struct fx_hashmap_bucket, cur_node, bk_node); if (!cur_bucket) { return false; } - struct b_hashmap_bucket_item *cur_item - = b_unbox(struct b_hashmap_bucket_item, cur_entry, bi_entry); + struct fx_hashmap_bucket_item *cur_item + = fx_unbox(struct fx_hashmap_bucket_item, cur_entry, bi_entry); if (!cur_item) { return false; } - struct b_btree_node *next_node = cur_node; - struct b_queue_entry *next_entry = b_queue_next(cur_entry); + struct fx_bst_node *next_node = cur_node; + struct fx_queue_entry *next_entry = fx_queue_next(cur_entry); if (!next_entry) { - next_node = b_btree_next(cur_node); + next_node = fx_bst_next(cur_node); if (!next_node) { return false; } - struct b_hashmap_bucket *next_bucket - = b_unbox(struct b_hashmap_bucket, next_node, bk_node); + struct fx_hashmap_bucket *next_bucket + = fx_unbox(struct fx_hashmap_bucket, next_node, bk_node); if (!next_bucket) { return false; } - next_entry = b_queue_first(&next_bucket->bk_items); + next_entry = fx_queue_first(&next_bucket->bk_items); if (!next_entry) { return false; } } - struct b_hashmap_bucket_item *next_item - = b_unbox(struct b_hashmap_bucket_item, next_entry, bi_entry); + struct fx_hashmap_bucket_item *next_item + = fx_unbox(struct fx_hashmap_bucket_item, next_entry, bi_entry); if (!next_item) { return false; } @@ -146,9 +146,9 @@ static bool get_next_node( return true; } -static struct b_hashmap_bucket *create_bucket(void) +static struct fx_hashmap_bucket *create_bucket(void) { - struct b_hashmap_bucket *bucket = malloc(sizeof *bucket); + struct fx_hashmap_bucket *bucket = malloc(sizeof *bucket); if (!bucket) { return NULL; } @@ -157,9 +157,9 @@ static struct b_hashmap_bucket *create_bucket(void) return bucket; } -static struct b_hashmap_bucket_item *create_bucket_item(void) +static struct fx_hashmap_bucket_item *create_bucket_item(void) { - struct b_hashmap_bucket_item *item = malloc(sizeof *item); + struct fx_hashmap_bucket_item *item = malloc(sizeof *item); if (!item) { return NULL; } @@ -168,116 +168,116 @@ static struct b_hashmap_bucket_item *create_bucket_item(void) return item; } -static b_status hashmap_put( - struct b_hashmap_p *hashmap, const b_hashmap_key *key, - const b_hashmap_value *value) +static fx_status hashmap_put( + struct fx_hashmap_p *hashmap, const fx_hashmap_key *key, + const fx_hashmap_value *value) { uint64_t hash = hash_key(key); - struct b_hashmap_bucket *bucket = get_bucket(&hashmap->h_buckets, hash); + struct fx_hashmap_bucket *bucket = get_bucket(&hashmap->h_buckets, hash); if (!bucket) { bucket = create_bucket(); if (!bucket) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } bucket->bk_hash = hash; put_bucket(&hashmap->h_buckets, bucket); } - struct b_queue_entry *entry = b_queue_first(&bucket->bk_items); + struct fx_queue_entry *entry = fx_queue_first(&bucket->bk_items); while (entry) { - struct b_hashmap_bucket_item *item - = b_unbox(struct b_hashmap_bucket_item, entry, bi_entry); + struct fx_hashmap_bucket_item *item + = fx_unbox(struct fx_hashmap_bucket_item, entry, bi_entry); if (compare_key(&item->bi_key, key)) { memcpy(&item->bi_value, value, sizeof *value); - return B_SUCCESS; + return FX_SUCCESS; } - entry = b_queue_next(entry); + entry = fx_queue_next(entry); } - struct b_hashmap_bucket_item *item = create_bucket_item(); + struct fx_hashmap_bucket_item *item = create_bucket_item(); if (!item) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } memcpy(&item->bi_key, key, sizeof *key); memcpy(&item->bi_value, value, sizeof *value); - b_queue_push_back(&bucket->bk_items, &item->bi_entry); + fx_queue_push_back(&bucket->bk_items, &item->bi_entry); hashmap->h_count++; - return B_SUCCESS; + return FX_SUCCESS; } -static const struct b_hashmap_value *hashmap_get( - const struct b_hashmap_p *hashmap, const struct b_hashmap_key *key) +static const struct fx_hashmap_value *hashmap_get( + const struct fx_hashmap_p *hashmap, const struct fx_hashmap_key *key) { uint64_t hash = hash_key(key); - struct b_hashmap_bucket *bucket = get_bucket(&hashmap->h_buckets, hash); + struct fx_hashmap_bucket *bucket = get_bucket(&hashmap->h_buckets, hash); if (!bucket) { return NULL; } - struct b_queue_entry *entry = b_queue_first(&bucket->bk_items); + struct fx_queue_entry *entry = fx_queue_first(&bucket->bk_items); while (entry) { - struct b_hashmap_bucket_item *item - = b_unbox(struct b_hashmap_bucket_item, entry, bi_entry); + struct fx_hashmap_bucket_item *item + = fx_unbox(struct fx_hashmap_bucket_item, entry, bi_entry); if (compare_key(&item->bi_key, key)) { return &item->bi_value; } - entry = b_queue_next(entry); + entry = fx_queue_next(entry); } return NULL; } static bool hashmap_has_key( - const struct b_hashmap_p *hashmap, const b_hashmap_key *key) + const struct fx_hashmap_p *hashmap, const fx_hashmap_key *key) { uint64_t hash = hash_key(key); - struct b_hashmap_bucket *bucket = get_bucket(&hashmap->h_buckets, hash); + struct fx_hashmap_bucket *bucket = get_bucket(&hashmap->h_buckets, hash); if (!bucket) { return false; } - struct b_queue_entry *entry = b_queue_first(&bucket->bk_items); + struct fx_queue_entry *entry = fx_queue_first(&bucket->bk_items); while (entry) { - struct b_hashmap_bucket_item *item - = b_unbox(struct b_hashmap_bucket_item, entry, bi_entry); + struct fx_hashmap_bucket_item *item + = fx_unbox(struct fx_hashmap_bucket_item, entry, bi_entry); if (compare_key(&item->bi_key, key)) { return true; } - entry = b_queue_next(entry); + entry = fx_queue_next(entry); } return false; } -static size_t hashmap_get_size(const struct b_hashmap_p *hashmap) +static size_t hashmap_get_size(const struct fx_hashmap_p *hashmap) { return hashmap->h_count; } -static bool hashmap_is_empty(const struct b_hashmap_p *hashmap) +static bool hashmap_is_empty(const struct fx_hashmap_p *hashmap) { - b_btree_node *first_node = b_btree_first(&hashmap->h_buckets); - struct b_hashmap_bucket *first_bucket - = b_unbox(struct b_hashmap_bucket, first_node, bk_node); + fx_bst_node *first_node = fx_bst_first(&hashmap->h_buckets); + struct fx_hashmap_bucket *first_bucket + = fx_unbox(struct fx_hashmap_bucket, first_node, bk_node); if (!first_bucket) { return true; } - b_queue_entry *first_entry = b_queue_first(&first_bucket->bk_items); - struct b_hashmap_bucket_item *first_item - = b_unbox(struct b_hashmap_bucket_item, first_entry, bi_entry); + fx_queue_entry *first_entry = fx_queue_first(&first_bucket->bk_items); + struct fx_hashmap_bucket_item *first_item + = fx_unbox(struct fx_hashmap_bucket_item, first_entry, bi_entry); if (!first_item) { return true; } @@ -285,11 +285,11 @@ static bool hashmap_is_empty(const struct b_hashmap_p *hashmap) return false; } -static b_status delete_item( - struct b_hashmap_p *hashmap, struct b_hashmap_bucket *bucket, - struct b_hashmap_bucket_item *item) +static fx_status delete_item( + struct fx_hashmap_p *hashmap, struct fx_hashmap_bucket *bucket, + struct fx_hashmap_bucket_item *item) { - b_queue_delete(&bucket->bk_items, &item->bi_entry); + fx_queue_delete(&bucket->bk_items, &item->bi_entry); if (hashmap->h_key_dtor) { hashmap->h_key_dtor((void *)item->bi_key.key_data); @@ -301,21 +301,21 @@ static b_status delete_item( free(item); - if (b_queue_empty(&bucket->bk_items)) { - b_btree_delete(&hashmap->h_buckets, &bucket->bk_node); + if (fx_queue_empty(&bucket->bk_items)) { + fx_bst_delete(&hashmap->h_buckets, &bucket->bk_node); free(bucket); } hashmap->h_count--; - return B_SUCCESS; + return FX_SUCCESS; } /*** PUBLIC FUNCTIONS *********************************************************/ -b_hashmap *b_hashmap_create( - b_hashmap_key_destructor key_dtor, b_hashmap_value_destructor value_dtor) +fx_hashmap *fx_hashmap_create( + fx_hashmap_key_destructor key_dtor, fx_hashmap_value_destructor value_dtor) { - b_hashmap *hashmap = b_object_create(B_TYPE_HASHMAP); + fx_hashmap *hashmap = fx_object_create(FX_TYPE_HASHMAP); if (!hashmap) { return NULL; } @@ -323,14 +323,14 @@ b_hashmap *b_hashmap_create( return hashmap; } -b_hashmap *b_hashmap_create_with_items(const b_hashmap_item *items) +fx_hashmap *fx_hashmap_create_with_items(const fx_hashmap_item *items) { - b_hashmap *hashmap = b_hashmap_create(NULL, NULL); + fx_hashmap *hashmap = fx_hashmap_create(NULL, NULL); if (!hashmap) { return NULL; } - struct b_hashmap_p *p = b_object_get_private(hashmap, B_TYPE_HASHMAP); + struct fx_hashmap_p *p = fx_object_get_private(hashmap, FX_TYPE_HASHMAP); for (size_t i = 0; items[i].key.key_data && items[i].key.key_size; i++) { hashmap_put(p, &items[i].key, &items[i].value); @@ -339,64 +339,64 @@ b_hashmap *b_hashmap_create_with_items(const b_hashmap_item *items) return hashmap; } -b_status b_hashmap_put( - b_hashmap *hashmap, const b_hashmap_key *key, const b_hashmap_value *value) +fx_status fx_hashmap_put( + fx_hashmap *hashmap, const fx_hashmap_key *key, const fx_hashmap_value *value) { - B_CLASS_DISPATCH_STATIC(B_TYPE_HASHMAP, hashmap_put, hashmap, key, value); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_HASHMAP, hashmap_put, hashmap, key, value); } -const struct b_hashmap_value *b_hashmap_get( - const b_hashmap *hashmap, const struct b_hashmap_key *key) +const struct fx_hashmap_value *fx_hashmap_get( + const fx_hashmap *hashmap, const struct fx_hashmap_key *key) { - B_CLASS_DISPATCH_STATIC(B_TYPE_HASHMAP, hashmap_get, hashmap, key); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_HASHMAP, hashmap_get, hashmap, key); } -bool b_hashmap_has_key(const b_hashmap *hashmap, const b_hashmap_key *key) +bool fx_hashmap_has_key(const fx_hashmap *hashmap, const fx_hashmap_key *key) { - B_CLASS_DISPATCH_STATIC(B_TYPE_HASHMAP, hashmap_has_key, hashmap, key); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_HASHMAP, hashmap_has_key, hashmap, key); } -size_t b_hashmap_get_size(const b_hashmap *hashmap) +size_t fx_hashmap_get_size(const fx_hashmap *hashmap) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_HASHMAP, hashmap_get_size, hashmap); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_HASHMAP, hashmap_get_size, hashmap); } -bool b_hashmap_is_empty(const b_hashmap *hashmap) +bool fx_hashmap_is_empty(const fx_hashmap *hashmap) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_HASHMAP, hashmap_is_empty, hashmap); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_HASHMAP, hashmap_is_empty, hashmap); } -b_iterator *b_hashmap_begin(b_hashmap *hashmap) +fx_iterator *fx_hashmap_begin(fx_hashmap *hashmap) { - b_hashmap_iterator *it_obj = b_object_create(B_TYPE_HASHMAP_ITERATOR); - struct b_hashmap_iterator_p *it - = b_object_get_private(it_obj, B_TYPE_HASHMAP_ITERATOR); + fx_hashmap_iterator *it_obj = fx_object_create(FX_TYPE_HASHMAP_ITERATOR); + struct fx_hashmap_iterator_p *it + = fx_object_get_private(it_obj, FX_TYPE_HASHMAP_ITERATOR); it->_h = hashmap; - it->_h_p = b_object_get_private(hashmap, B_TYPE_HASHMAP); + it->_h_p = fx_object_get_private(hashmap, FX_TYPE_HASHMAP); it->i = 0; - if (b_hashmap_is_empty(hashmap)) { + if (fx_hashmap_is_empty(hashmap)) { memset(&it->item, 0x0, sizeof it->item); - b_iterator_set_status(it_obj, B_ERR_NO_DATA); + fx_iterator_set_status(it_obj, FX_ERR_NO_DATA); return it_obj; } - struct b_btree_node *first_node = b_btree_first(&it->_h_p->h_buckets); - struct b_hashmap_bucket *first_bucket - = b_unbox(struct b_hashmap_bucket, first_node, bk_node); + struct fx_bst_node *first_node = fx_bst_first(&it->_h_p->h_buckets); + struct fx_hashmap_bucket *first_bucket + = fx_unbox(struct fx_hashmap_bucket, first_node, bk_node); if (!first_bucket) { memset(&it->item, 0x0, sizeof it->item); - b_iterator_set_status(it_obj, B_ERR_NO_DATA); + fx_iterator_set_status(it_obj, FX_ERR_NO_DATA); return it_obj; } - struct b_queue_entry *first_entry = b_queue_first(&first_bucket->bk_items); - struct b_hashmap_bucket_item *first_item - = b_unbox(struct b_hashmap_bucket_item, first_entry, bi_entry); + struct fx_queue_entry *first_entry = fx_queue_first(&first_bucket->bk_items); + struct fx_hashmap_bucket_item *first_item + = fx_unbox(struct fx_hashmap_bucket_item, first_entry, bi_entry); if (!first_item) { memset(&it->item, 0x0, sizeof it->item); - b_iterator_set_status(it_obj, B_ERR_NO_DATA); + fx_iterator_set_status(it_obj, FX_ERR_NO_DATA); return it_obj; } @@ -409,35 +409,35 @@ b_iterator *b_hashmap_begin(b_hashmap *hashmap) return it_obj; } -const b_iterator *b_hashmap_cbegin(const b_hashmap *hashmap) +const fx_iterator *fx_hashmap_cbegin(const fx_hashmap *hashmap) { - return b_hashmap_begin((b_hashmap *)hashmap); + return fx_hashmap_begin((fx_hashmap *)hashmap); } /*** VIRTUAL FUNCTIONS ********************************************************/ -static void hashmap_init(b_object *obj, void *priv) +static void hashmap_init(fx_object *obj, void *priv) { - struct b_hashmap_p *map = priv; + struct fx_hashmap_p *map = priv; } -static void hashmap_fini(b_object *obj, void *priv) +static void hashmap_fini(fx_object *obj, void *priv) { - struct b_hashmap_p *map = priv; + struct fx_hashmap_p *map = priv; - struct b_btree_node *node = b_btree_first(&map->h_buckets); + struct fx_bst_node *node = fx_bst_first(&map->h_buckets); while (node) { - struct b_hashmap_bucket *b - = b_unbox(struct b_hashmap_bucket, node, bk_node); - struct b_btree_node *next_node = b_btree_next(node); - b_btree_delete(&map->h_buckets, node); + struct fx_hashmap_bucket *b + = fx_unbox(struct fx_hashmap_bucket, node, bk_node); + struct fx_bst_node *next_node = fx_bst_next(node); + fx_bst_delete(&map->h_buckets, node); - struct b_queue_entry *entry = b_queue_first(&b->bk_items); + struct fx_queue_entry *entry = fx_queue_first(&b->bk_items); while (entry) { - struct b_hashmap_bucket_item *item = b_unbox( - struct b_hashmap_bucket_item, entry, bi_entry); - struct b_queue_entry *next_entry = b_queue_next(entry); - b_queue_delete(&b->bk_items, entry); + struct fx_hashmap_bucket_item *item = fx_unbox( + struct fx_hashmap_bucket_item, entry, bi_entry); + struct fx_queue_entry *next_entry = fx_queue_next(entry); + fx_queue_delete(&b->bk_items, entry); if (map->h_key_dtor) { map->h_key_dtor((void *)item->bi_key.key_data); @@ -458,24 +458,24 @@ static void hashmap_fini(b_object *obj, void *priv) /*** ITERATOR FUNCTIONS *******************************************************/ -static enum b_status iterator_move_next(const b_iterator *obj) +static enum fx_status iterator_move_next(const fx_iterator *obj) { - struct b_hashmap_iterator_p *it - = b_object_get_private(obj, B_TYPE_HASHMAP_ITERATOR); + struct fx_hashmap_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_HASHMAP_ITERATOR); - struct b_btree_node *next_node; - struct b_queue_entry *next_entry; + struct fx_bst_node *next_node; + struct fx_queue_entry *next_entry; if (!get_next_node(it->_cbn, it->_cqe, &next_node, &next_entry)) { memset(&it->item, 0x0, sizeof it->item); - return B_ERR_NO_DATA; + return FX_ERR_NO_DATA; } - struct b_hashmap_bucket_item *next_item - = b_unbox(struct b_hashmap_bucket_item, next_entry, bi_entry); + struct fx_hashmap_bucket_item *next_item + = fx_unbox(struct fx_hashmap_bucket_item, next_entry, bi_entry); if (!next_item) { memset(&it->item, 0x0, sizeof it->item); - return B_ERR_NO_DATA; + return FX_ERR_NO_DATA; } it->i++; @@ -485,40 +485,40 @@ static enum b_status iterator_move_next(const b_iterator *obj) it->_cbn = next_node; it->_cqe = next_entry; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status iterator_erase(b_iterator *obj) +static enum fx_status iterator_erase(fx_iterator *obj) { - struct b_hashmap_iterator_p *it - = b_object_get_private(obj, B_TYPE_HASHMAP_ITERATOR); + struct fx_hashmap_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_HASHMAP_ITERATOR); if ((it->item.key.key_data || it->item.value.value_data) && !(it->_cbn && it->_cqe)) { - return B_ERR_BAD_STATE; + return FX_ERR_BAD_STATE; } if (!it->item.key.key_data || !it->_cqe) { - return B_ERR_NO_DATA; + return FX_ERR_NO_DATA; } - struct b_btree_node *next_node; - struct b_queue_entry *next_entry; + struct fx_bst_node *next_node; + struct fx_queue_entry *next_entry; if (!get_next_node(it->_cbn, it->_cqe, &next_node, &next_entry)) { memset(&it->item, 0x0, sizeof it->item); - return B_ERR_NO_DATA; + return FX_ERR_NO_DATA; } - struct b_hashmap_bucket *cur_bucket - = b_unbox(struct b_hashmap_bucket, it->_cbn, bk_node); - struct b_hashmap_bucket_item *cur_item - = b_unbox(struct b_hashmap_bucket_item, it->_cqe, bi_entry); + struct fx_hashmap_bucket *cur_bucket + = fx_unbox(struct fx_hashmap_bucket, it->_cbn, bk_node); + struct fx_hashmap_bucket_item *cur_item + = fx_unbox(struct fx_hashmap_bucket_item, it->_cqe, bi_entry); - struct b_hashmap_bucket_item *next_item - = b_unbox(struct b_hashmap_bucket_item, next_entry, bi_entry); + struct fx_hashmap_bucket_item *next_item + = fx_unbox(struct fx_hashmap_bucket_item, next_entry, bi_entry); - b_status status = delete_item(it->_h_p, cur_bucket, cur_item); - if (B_ERR(status)) { + fx_status status = delete_item(it->_h_p, cur_bucket, cur_item); + if (FX_ERR(status)) { return status; } @@ -536,63 +536,63 @@ static enum b_status iterator_erase(b_iterator *obj) it->_cqe = NULL; } - return B_SUCCESS; + return FX_SUCCESS; } -static b_iterator_value iterator_get_value(b_iterator *obj) +static fx_iterator_value iterator_get_value(fx_iterator *obj) { - struct b_hashmap_iterator_p *it - = b_object_get_private(obj, B_TYPE_HASHMAP_ITERATOR); - return B_ITERATOR_VALUE_PTR(&it->item); + struct fx_hashmap_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_HASHMAP_ITERATOR); + return FX_ITERATOR_VALUE_PTR(&it->item); } -static const b_iterator_value iterator_get_cvalue(const b_iterator *obj) +static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj) { - const struct b_hashmap_iterator_p *it - = b_object_get_private(obj, B_TYPE_HASHMAP_ITERATOR); - return B_ITERATOR_VALUE_CPTR(&it->item); + 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 *********************************************************/ -// ---- b_hashmap DEFINITION -B_TYPE_CLASS_DEFINITION_BEGIN(b_hashmap) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) +// ---- fx_hashmap DEFINITION +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_hashmap) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) - B_TYPE_CLASS_INTERFACE_BEGIN(b_iterable, B_TYPE_ITERABLE) - B_INTERFACE_ENTRY(it_begin) = b_hashmap_begin; - B_INTERFACE_ENTRY(it_cbegin) = b_hashmap_cbegin; - B_TYPE_CLASS_INTERFACE_END(b_iterable, B_TYPE_ITERABLE) -B_TYPE_CLASS_DEFINITION_END(b_hashmap) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE) + FX_INTERFACE_ENTRY(it_begin) = fx_hashmap_begin; + FX_INTERFACE_ENTRY(it_cbegin) = fx_hashmap_cbegin; + FX_TYPE_CLASS_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE) +FX_TYPE_CLASS_DEFINITION_END(fx_hashmap) -B_TYPE_DEFINITION_BEGIN(b_hashmap) - B_TYPE_ID(0x7bf5bcd1, 0x1ff3, 0x4e43, 0xbed8, 0x7c74f28348bf); - B_TYPE_CLASS(b_hashmap_class); - B_TYPE_IMPLEMENTS(B_TYPE_ITERABLE); - B_TYPE_INSTANCE_PRIVATE(struct b_hashmap_p); - B_TYPE_INSTANCE_INIT(hashmap_init); - B_TYPE_INSTANCE_FINI(hashmap_fini); -B_TYPE_DEFINITION_END(b_hashmap) +FX_TYPE_DEFINITION_BEGIN(fx_hashmap) + FX_TYPE_ID(0x7bf5bcd1, 0x1ff3, 0x4e43, 0xbed8, 0x7c74f28348bf); + FX_TYPE_CLASS(fx_hashmap_class); + FX_TYPE_IMPLEMENTS(FX_TYPE_ITERABLE); + FX_TYPE_INSTANCE_PRIVATE(struct fx_hashmap_p); + FX_TYPE_INSTANCE_INIT(hashmap_init); + FX_TYPE_INSTANCE_FINI(hashmap_fini); +FX_TYPE_DEFINITION_END(fx_hashmap) -// ---- b_hashmap_iterator DEFINITION -B_TYPE_CLASS_DEFINITION_BEGIN(b_hashmap_iterator) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) +// ---- fx_hashmap_iterator DEFINITION +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_hashmap_iterator) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) - B_TYPE_CLASS_INTERFACE_BEGIN(b_iterator, B_TYPE_ITERATOR) - B_INTERFACE_ENTRY(it_move_next) = iterator_move_next; - B_INTERFACE_ENTRY(it_erase) = iterator_erase; - B_INTERFACE_ENTRY(it_get_value) = iterator_get_value; - B_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue; - B_TYPE_CLASS_INTERFACE_END(b_iterator, B_TYPE_ITERATOR) -B_TYPE_CLASS_DEFINITION_END(b_hashmap_iterator) + FX_TYPE_CLASS_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_CLASS_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR) +FX_TYPE_CLASS_DEFINITION_END(fx_hashmap_iterator) -B_TYPE_DEFINITION_BEGIN(b_hashmap_iterator) - B_TYPE_ID(0xd9658456, 0xdd80, 0x419a, 0xb23a, 0xb513013e6431); - B_TYPE_EXTENDS(B_TYPE_ITERATOR); - B_TYPE_CLASS(b_hashmap_iterator_class); - B_TYPE_INSTANCE_PRIVATE(struct b_hashmap_iterator_p); -B_TYPE_DEFINITION_END(b_hashmap_iterator) +FX_TYPE_DEFINITION_BEGIN(fx_hashmap_iterator) + FX_TYPE_ID(0xd9658456, 0xdd80, 0x419a, 0xb23a, 0xb513013e6431); + FX_TYPE_EXTENDS(FX_TYPE_ITERATOR); + FX_TYPE_CLASS(fx_hashmap_iterator_class); + FX_TYPE_INSTANCE_PRIVATE(struct fx_hashmap_iterator_p); +FX_TYPE_DEFINITION_END(fx_hashmap_iterator) diff --git a/ds/include/blue/ds/array.h b/ds/include/blue/ds/array.h deleted file mode 100644 index 6ab6cd0..0000000 --- a/ds/include/blue/ds/array.h +++ /dev/null @@ -1,218 +0,0 @@ -/** - * A heterogeneous array of objects. b_array only stores references - * to the objects that it contains, not the object data itself. - * - * b_array stores pointers to objects in a single contiguous array, - * but this is an implementation detail that may change in the future. - * Users of b_array should not rely on this being the case. - */ -#ifndef BLUE_DS_ARRAY_H_ -#define BLUE_DS_ARRAY_H_ - -#include -#include -#include -#include - -B_DECLS_BEGIN; - -#define B_TYPE_ARRAY (b_array_get_type()) -#define B_TYPE_ARRAY_ITERATOR (b_array_iterator_get_type()) - -struct b_array_p; - -B_DECLARE_TYPE(b_array); -B_DECLARE_TYPE(b_array_iterator); - -B_TYPE_CLASS_DECLARATION_BEGIN(b_array) -B_TYPE_CLASS_DECLARATION_END(b_array) - -B_TYPE_CLASS_DECLARATION_BEGIN(b_array_iterator) -B_TYPE_CLASS_DECLARATION_END(b_array_iterator) - -BLUE_API b_type b_array_get_type(void); -BLUE_API b_type b_array_iterator_get_type(void); - -B_TYPE_DEFAULT_CONSTRUCTOR(b_array, B_TYPE_ARRAY); - -/** - * Creates an b_array initialised with the contents of the provided - * b_object pointer array. The b_array will take a reference to each - * object specified in `values`, and will increment the reference count. - * The order of objects in the new b_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 b_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 b_array. - * @param nr_values The size of the `values` array. - * @return A pointer to the new b_array, or NULL if an error occurred. - */ -BLUE_API b_array *b_array_create_with_values( - b_object *const *values, size_t nr_values); - -/** - * Remove all object references from an b_array, resetting the size of the array to zero. - * The reference counts of all objects in the array will be decremented. - * - * @param array The b_array to clear. - */ -BLUE_API void b_array_clear(b_array *array); - -/** - * Inserts an object at the end of an b_array. The reference count of - * the object will be incremented. - * - * @param array The b_array to append the object to. - * @param value The object to append. - * @return B_SUCCESS if the object was appended successfully, or an - * error code if an error occurred. - */ -BLUE_API b_status b_array_append(b_array *array, b_object *value); - -/** - * Inserts an object at the beginning of an b_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 b_array to prepend the object to. - * @param value The object to prepend. - * @return B_SUCCESS if the object was prepended successfully, or an - * error code if an error occurred. - */ -BLUE_API b_status b_array_prepend(b_array *array, b_object *value); - -/** - * Inserts an object into an b_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 b_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 - * `B_NPOS`, the object will be inserted at the end of the b_array. - * @return B_SUCCESS if the object was inserted, or a status code - * describing any error that occurred. - */ -BLUE_API b_status b_array_insert(b_array *array, b_object *value, size_t at); - -/** - * Removes the object at the specified index from an b_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 b_array to remove the object from. - * @param at The index of the object to be removed. - * @return B_SUCCESS if the object was removed, or a status code - * describing any error that occurred. - */ -BLUE_API b_status b_array_remove(b_array *array, size_t at); - -/** - * Removes the object at the beginning of an b_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 b_array to remove the object from. - * @return B_SUCCESS if the object was removed, or a status code describing any error that occurred. - */ -BLUE_API b_status b_array_remove_front(b_array *array); - -/** - * Removes the object at the end of an b_array. The reference count - * of the removed object will be decremented. - * - * @param array The b_array to remove the object from. - * @return B_SUCCESS if the object was removed, or a status code describing any error that occurred. - */ -BLUE_API b_status b_array_remove_back(b_array *array); - -/** - * Removes the object at the specified index of an b_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 b_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. - */ -BLUE_API b_object *b_array_pop(b_array *array, size_t at); - -/** - * Removes the object at the beginning of an b_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 b_array will be moved to - * fill the empty space left by the removed object. - * - * @param array The b_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. - */ -BLUE_API b_object *b_array_pop_front(b_array *array); - -/** - * Removes the object at the end of an b_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 b_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. - */ -BLUE_API b_object *b_array_pop_back(b_array *array); - -/** - * Returns an unowned pointer to the object at the given index of an b_array. - * The caller does not own the returned pointer, and MUST NOT release it. - * - * @param array The b_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. - */ -BLUE_API b_object *b_array_at(const b_array *array, size_t at); - -/** - * Returns an owned pointer to the object at the given index of an - * b_array. The caller owns the returned pointer, and must release it - * when they are finished with it. - * - * @param array The b_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. - */ -BLUE_API b_object *b_array_get(b_array *array, size_t at); - -/** - * Returns the number of objects contained in an b_array. - * - * @param array The b_array. - * @return The number of objects contained in the b_array. - */ -BLUE_API size_t b_array_size(const b_array *array); - -/** - * Returns the current maximum capacity of an b_array. This represents - * the number of objects that can be stored in an b_array before its - * internal buffer would need to be re-sized. - * - * @param array The b_array. - * @return The maximum capacity of the b_array. - */ -BLUE_API size_t b_array_capacity(const b_array *array); - -B_DECLS_END; - -#endif diff --git a/ds/include/blue/ds/bitbuffer.h b/ds/include/blue/ds/bitbuffer.h deleted file mode 100644 index 0b8230f..0000000 --- a/ds/include/blue/ds/bitbuffer.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef BLUE_DS_BITBUFFER_H_ -#define BLUE_DS_BITBUFFER_H_ - -#include - -B_DECLS_BEGIN; - -B_DECLARE_TYPE(b_bitbuffer); - -B_TYPE_CLASS_DECLARATION_BEGIN(b_bitbuffer) - ; -B_TYPE_CLASS_DECLARATION_END(b_bitbuffer); - -BLUE_API b_status b_bitbuffer_put_bit(b_bitbuffer *buf, int bit); -BLUE_API b_status b_bitbuffer_put_bool(b_bitbuffer *buf, bool b); -BLUE_API b_status b_bitbuffer_put_int( - b_bitbuffer *buf, uint64_t v, unsigned int nr_bits); -BLUE_API b_status b_bitbuffer_put_bytes( - b_bitbuffer *buf, const void *p, size_t len, size_t bits_per_byte); -BLUE_API b_status b_bitbuffer_put_string( - b_bitbuffer *buf, const char *p, size_t len, size_t bits_per_char); - -B_DECLS_END; - -#endif diff --git a/ds/include/blue/ds/bitmap.h b/ds/include/blue/ds/bitmap.h deleted file mode 100644 index 2caad57..0000000 --- a/ds/include/blue/ds/bitmap.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef BLUE_DS_BITMAP_H_ -#define BLUE_DS_BITMAP_H_ - -#include -#include -#include - -B_DECLS_BEGIN; - -#define B_TYPE_BITMAP (b_bitmap_get_type()) - -B_DECLARE_TYPE(b_bitmap); - -B_TYPE_CLASS_DECLARATION_BEGIN(b_bitmap) -B_TYPE_CLASS_DECLARATION_END(b_bitmap) - -BLUE_API b_type b_bitmap_get_type(void); - -BLUE_API b_bitmap *b_bitmap_create(size_t nr_bits); - -BLUE_API void b_bitmap_set_bit(b_bitmap *map, size_t bit); -BLUE_API void b_bitmap_clear_bit(b_bitmap *map, size_t bit); -BLUE_API void b_bitmap_set_range(b_bitmap *map, size_t first_bit, size_t nbits); -BLUE_API void b_bitmap_clear_range(b_bitmap *map, size_t first_bit, size_t nbits); -BLUE_API void b_bitmap_set_all(b_bitmap *map); -BLUE_API void b_bitmap_clear_all(b_bitmap *map); - -BLUE_API bool b_bitmap_check_bit(const b_bitmap *map, size_t bit); - -BLUE_API size_t b_bitmap_count_set_bits(const b_bitmap *map); -BLUE_API size_t b_bitmap_count_clear_bits(const b_bitmap *map); - -BLUE_API size_t b_bitmap_highest_set_bit(const b_bitmap *map); -BLUE_API size_t b_bitmap_highest_clear_bit(const b_bitmap *map); -BLUE_API size_t b_bitmap_lowest_set_bit(const b_bitmap *map); -BLUE_API size_t b_bitmap_lowest_clear_bit(const b_bitmap *map); - -B_DECLS_END; - -#endif diff --git a/ds/include/blue/ds/buffer.h b/ds/include/blue/ds/buffer.h deleted file mode 100644 index 9885ad1..0000000 --- a/ds/include/blue/ds/buffer.h +++ /dev/null @@ -1,49 +0,0 @@ -#ifndef BLUE_DS_BUFFER_H_ -#define BLUE_DS_BUFFER_H_ - -#include -#include - -B_DECLS_BEGIN; - -#define B_TYPE_BUFFER (b_buffer_get_type()) - -B_DECLARE_TYPE(b_buffer); - -B_TYPE_CLASS_DECLARATION_BEGIN(b_buffer) -B_TYPE_CLASS_DECLARATION_END(b_buffer) - -BLUE_API b_type b_buffer_get_type(void); - -BLUE_API b_buffer *b_buffer_create(size_t item_sz); -BLUE_API b_buffer *b_buffer_create_from_bytes(const void *p, size_t len); -BLUE_API b_buffer *b_buffer_create_from_array( - const void *p, size_t item_sz, size_t len); - -BLUE_API void *b_buffer_steal(b_buffer *buf); -BLUE_API b_status b_buffer_reserve(b_buffer *buf, size_t capacity); -BLUE_API b_status b_buffer_resize(b_buffer *buf, size_t length); - -BLUE_API b_status b_buffer_append(b_buffer *dest, const void *p, size_t count); -BLUE_API b_status b_buffer_prepend(b_buffer *dest, const void *p, size_t count); -BLUE_API b_status b_buffer_insert( - b_buffer *dest, const void *p, size_t count, size_t at); -BLUE_API b_status b_buffer_remove(b_buffer *dest, size_t at, size_t count); -BLUE_API b_status b_buffer_clear(b_buffer *buf); - -BLUE_API b_status b_buffer_push_back(b_buffer *buf, size_t count, void **p); -BLUE_API b_status b_buffer_push_front(b_buffer *buf, size_t count, void **p); - -BLUE_API b_status b_buffer_pop_back(b_buffer *buf, size_t count); -BLUE_API b_status b_buffer_pop_front(b_buffer *buf, size_t count); - -BLUE_API size_t b_buffer_get_size(const b_buffer *buf); -BLUE_API size_t b_buffer_get_item_size(const b_buffer *buf); -BLUE_API size_t b_buffer_get_capacity(const b_buffer *buf); - -BLUE_API void *b_buffer_ptr(const b_buffer *buf); -BLUE_API void *b_buffer_get(const b_buffer *buf, size_t at); - -B_DECLS_END; - -#endif diff --git a/ds/include/blue/ds/datetime.h b/ds/include/blue/ds/datetime.h deleted file mode 100644 index 78f4773..0000000 --- a/ds/include/blue/ds/datetime.h +++ /dev/null @@ -1,48 +0,0 @@ -#ifndef BLUE_DS_DATETIME_H_ -#define BLUE_DS_DATETIME_H_ - -#include -#include -#include - -B_DECLS_BEGIN; - -#define B_TYPE_DATETIME (b_datetime_get_type()) - -B_DECLARE_TYPE(b_datetime); - -B_TYPE_CLASS_DECLARATION_BEGIN(b_datetime) -B_TYPE_CLASS_DECLARATION_END(b_datetime) - -typedef enum b_datetime_format { - B_DATETIME_FORMAT_RFC3339 = 1, -} b_datetime_format; - -BLUE_API b_type b_datetime_get_type(void); - -B_TYPE_DEFAULT_CONSTRUCTOR(b_datetime, B_TYPE_DATETIME); - -BLUE_API b_datetime *b_datetime_parse(b_datetime_format format, const char *s); -BLUE_API void b_datetime_to_string( - const b_datetime *dt, b_datetime_format format, - B_TYPE_FWDREF(b_stream) * dest); - -BLUE_API bool b_datetime_is_localtime(const b_datetime *dt); -BLUE_API bool b_datetime_has_date(const b_datetime *dt); -BLUE_API bool b_datetime_has_time(const b_datetime *dt); - -BLUE_API long b_datetime_year(const b_datetime *dt); -BLUE_API long b_datetime_month(const b_datetime *dt); -BLUE_API long b_datetime_day(const b_datetime *dt); -BLUE_API long b_datetime_hour(const b_datetime *dt); -BLUE_API long b_datetime_minute(const b_datetime *dt); -BLUE_API long b_datetime_second(const b_datetime *dt); -BLUE_API long b_datetime_subsecond(const b_datetime *dt); - -BLUE_API bool b_datetime_zone_offset_is_negative(const b_datetime *dt); -BLUE_API long b_datetime_zone_offset_hour(const b_datetime *dt); -BLUE_API long b_datetime_zone_offset_minute(const b_datetime *dt); - -B_DECLS_END; - -#endif diff --git a/ds/include/blue/ds/dict.h b/ds/include/blue/ds/dict.h deleted file mode 100644 index e3430c7..0000000 --- a/ds/include/blue/ds/dict.h +++ /dev/null @@ -1,62 +0,0 @@ -#ifndef BLUE_DS_DICT_H_ -#define BLUE_DS_DICT_H_ - -#include -#include -#include -#include -#include -#include - -B_DECLS_BEGIN; - -#define B_TYPE_DICT (b_dict_get_type()) -#define B_TYPE_DICT_ITERATOR (b_dict_iterator_get_type()) - -struct b_dict_p; - -B_DECLARE_TYPE(b_dict); -B_DECLARE_TYPE(b_dict_iterator); - -B_TYPE_CLASS_DECLARATION_BEGIN(b_dict) -B_TYPE_CLASS_DECLARATION_END(b_dict) - -B_TYPE_CLASS_DECLARATION_BEGIN(b_dict_iterator) -B_TYPE_CLASS_DECLARATION_END(b_dict_iterator) - -#define B_DICT_ITEM(k, v) {.key = (k), .value = (v)} -#define B_DICT_ITEM_END {.key = NULL, .value = NULL} - -#define b_dict_foreach(it, dict) \ - for (int z__b_unique_name() = b_dict_iterator_begin(dict, it); \ - (it)->key != NULL; b_dict_iterator_next(it)) - -typedef struct b_dict_item { - const b_string *key; - b_object *value; -} b_dict_item; - -BLUE_API b_type b_dict_get_type(void); -BLUE_API b_type b_dict_iterator_get_type(void); - -B_TYPE_DEFAULT_CONSTRUCTOR(b_dict, B_TYPE_DICT); - -#if 0 -BLUE_API b_dict *b_dict_create_with_items(const b_dict_item *items); -#endif - -BLUE_API b_status b_dict_put(b_dict *dict, const char *key, b_object *value); -BLUE_API b_status b_dict_put_sk(b_dict *dict, const b_string *key, b_object *value); -BLUE_API b_object *b_dict_at(const b_dict *dict, const char *key); -BLUE_API b_object *b_dict_at_sk(const b_dict *dict, const b_string *key); -BLUE_API b_object *b_dict_get(b_dict *dict, const char *key); -BLUE_API b_object *b_dict_get_sk(b_dict *dict, const b_string *key); - -BLUE_API bool b_dict_has_key(const b_dict *dict, const char *key); -BLUE_API bool b_dict_has_skey(const b_dict *dict, const b_string *key); -BLUE_API size_t b_dict_get_size(const b_dict *dict); -BLUE_API bool b_dict_is_empty(const b_dict *dict); - -B_DECLS_END; - -#endif diff --git a/ds/include/blue/ds/hashmap.h b/ds/include/blue/ds/hashmap.h deleted file mode 100644 index 04063b6..0000000 --- a/ds/include/blue/ds/hashmap.h +++ /dev/null @@ -1,83 +0,0 @@ -#ifndef BLUE_DS_HASHMAP_H_ -#define BLUE_DS_HASHMAP_H_ - -#include -#include -#include -#include -#include -#include - -B_DECLS_BEGIN; - -struct b_hashmap_p; - -#define B_TYPE_HASHMAP (b_hashmap_get_type()) -#define B_TYPE_HASHMAP_ITERATOR (b_hashmap_iterator_get_type()) - -B_DECLARE_TYPE(b_hashmap); -B_DECLARE_TYPE(b_hashmap_iterator); - -B_TYPE_CLASS_DECLARATION_BEGIN(b_hashmap) -B_TYPE_CLASS_DECLARATION_END(b_hashmap) - -B_TYPE_CLASS_DECLARATION_BEGIN(b_hashmap_iterator) -B_TYPE_CLASS_DECLARATION_END(b_hashmap_iterator) - -#define B_HASHMAP_KEY(k, ks) {.key_data = (k), .key_size = (ks)} -#define B_HASHMAP_VALUE(v, vs) {.value_data = (v), .value_size = (vs)} - -#define B_HASHMAP_ITEM(k, ks, v, vs) \ - {.key = B_HASHMAP_KEY(k, ks), .value = B_HASHMAP_VALUE(v, vs)} - -#define B_HASHMAP_ITEM_END {.key = {0}, .value = {0}} - -#define b_hashmap_foreach(it, hashmap) \ - for (int z__b_unique_name() = b_hashmap_iterator_begin(hashmap, it); \ - (it)->key != NULL; b_hashmap_iterator_next(it)) - -typedef void (*b_hashmap_key_destructor)(void *); -typedef void (*b_hashmap_value_destructor)(void *); - -typedef enum b_hashmap_key_flags { - B_HASHMAP_KEY_F_INTVALUE = 0x01u, -} b_hashmap_key_flags; - -typedef struct b_hashmap_key { - b_hashmap_key_flags key_flags; - const void *key_data; - size_t key_size; -} b_hashmap_key; - -typedef struct b_hashmap_value { - void *value_data; - size_t value_size; -} b_hashmap_value; - -typedef struct b_hashmap_item { - b_hashmap_key key; - b_hashmap_value value; -} b_hashmap_item; - -BLUE_API b_type b_hashmap_get_type(void); -BLUE_API b_type b_hashmap_iterator_get_type(void); - -BLUE_API b_hashmap *b_hashmap_create( - b_hashmap_key_destructor key_dtor, b_hashmap_value_destructor value_dtor); -BLUE_API b_hashmap *b_hashmap_create_with_items(const b_hashmap_item *items); - -BLUE_API b_status b_hashmap_put( - b_hashmap *hashmap, const b_hashmap_key *key, const b_hashmap_value *value); -BLUE_API const b_hashmap_value *b_hashmap_get( - const b_hashmap *hashmap, const b_hashmap_key *key); - -BLUE_API bool b_hashmap_has_key(const b_hashmap *hashmap, const b_hashmap_key *key); -BLUE_API size_t b_hashmap_get_size(const b_hashmap *hashmap); -BLUE_API bool b_hashmap_is_empty(const b_hashmap *hashmap); - -BLUE_API b_iterator *b_hashmap_begin(b_hashmap *hashmap); -BLUE_API const b_iterator *b_hashmap_cbegin(const b_hashmap *hashmap); - -B_DECLS_END; - -#endif diff --git a/ds/include/blue/ds/list.h b/ds/include/blue/ds/list.h deleted file mode 100644 index 122e92e..0000000 --- a/ds/include/blue/ds/list.h +++ /dev/null @@ -1,63 +0,0 @@ -#ifndef BLUE_DS_LIST_H_ -#define BLUE_DS_LIST_H_ - -#include -#include -#include - -B_DECLS_BEGIN; - -#define B_TYPE_LIST (b_list_get_type()) -#define B_TYPE_LIST_ITERATOR (b_list_iterator_get_type()) - -struct b_list_p; - -B_DECLARE_TYPE(b_list); -B_DECLARE_TYPE(b_list_iterator); - -B_TYPE_CLASS_DECLARATION_BEGIN(b_list) -B_TYPE_CLASS_DECLARATION_END(b_list) - -B_TYPE_CLASS_DECLARATION_BEGIN(b_list_iterator) -B_TYPE_CLASS_DECLARATION_END(b_list_iterator) - -typedef struct b_list_entry b_list_entry; - -BLUE_API b_type b_list_get_type(void); -BLUE_API b_type b_list_iterator_get_type(void); - -B_TYPE_DEFAULT_CONSTRUCTOR(b_list, B_TYPE_LIST); - -BLUE_API bool b_list_empty(b_list *q); -BLUE_API void *b_list_first_item(const b_list *q); -BLUE_API void *b_list_last_item(const b_list *q); -BLUE_API b_list_entry *b_list_first_entry(const b_list *q); -BLUE_API b_list_entry *b_list_last_entry(const b_list *q); -BLUE_API b_list_entry *b_list_next(const b_list_entry *entry); -BLUE_API b_list_entry *b_list_prev(const b_list_entry *entry); - -BLUE_API size_t b_list_length(const b_list *q); - -BLUE_API b_list_entry *b_list_insert_before( - b_list *q, void *ptr, b_list_entry *before); -BLUE_API b_list_entry *b_list_insert_after( - b_list *q, void *ptr, b_list_entry *after); - -BLUE_API b_list_entry *b_list_push_front(b_list *q, void *ptr); -BLUE_API b_list_entry *b_list_push_back(b_list *q, void *ptr); - -BLUE_API void *b_list_pop_front(b_list *q); -BLUE_API void *b_list_pop_back(b_list *q); - -BLUE_API b_status b_list_delete_item(b_list *q, void *ptr); -BLUE_API b_status b_list_delete_entry(b_list *q, b_list_entry *entry); -BLUE_API void b_list_delete_all(b_list *q); - -BLUE_API void *b_list_entry_value(const b_list_entry *entry); - -BLUE_API b_iterator *b_list_begin(b_list *q); -BLUE_API const b_iterator *b_list_cbegin(const b_list *q); - -B_DECLS_END; - -#endif diff --git a/ds/include/blue/ds/number.h b/ds/include/blue/ds/number.h deleted file mode 100644 index 78d5944..0000000 --- a/ds/include/blue/ds/number.h +++ /dev/null @@ -1,254 +0,0 @@ -#ifndef BLUE_DS_NUMBER_H -#define BLUE_DS_NUMBER_H - -#include -#include - -B_DECLS_BEGIN; - -#define B_INT8(v) (b_number_create_int8(v)) -#define B_INT16(v) (b_number_create_int16(v)) -#define B_INT32(v) (b_number_create_int32(v)) -#define B_INT64(v) (b_number_create_int64(v)) -#define B_FLOAT32(v) (b_number_create_float32(v)) -#define B_FLOAT64(v) (b_number_create_float64(v)) -#define B_CHAR(v) (b_number_create_char(v)) -#define B_SHORT(v) (b_number_create_short(v)) -#define B_INT(v) (b_number_create_int(v)) -#define B_LONG(v) (b_number_create_long(v)) -#define B_LONGLONG(v) (b_number_create_longlong(v)) -#define B_FLOAT(v) (b_number_create_float(v)) -#define B_DOUBLE(v) (b_number_create_double(v)) -#define B_SIZE_T(v) (b_number_create_size_t(v)) - -#define B_RV_INT8(v) B_RV(b_number_create_int8(v)) -#define B_RV_INT16(v) B_RV(b_number_create_int16(v)) -#define B_RV_INT32(v) B_RV(b_number_create_int32(v)) -#define B_RV_INT64(v) B_RV(b_number_create_int64(v)) -#define B_RV_FLOAT32(v) B_RV(b_number_create_float32(v)) -#define B_RV_FLOAT64(v) B_RV(b_number_create_float64(v)) -#define B_RV_CHAR(v) B_RV(b_number_create_char(v)) -#define B_RV_SHORT(v) B_RV(b_number_create_short(v)) -#define B_RV_INT(v) B_RV(b_number_create_int(v)) -#define B_RV_LONG(v) B_RV(b_number_create_long(v)) -#define B_RV_LONGLONG(v) B_RV(b_number_create_longlong(v)) -#define B_RV_FLOAT(v) B_RV(b_number_create_float(v)) -#define B_RV_DOUBLE(v) B_RV(b_number_create_double(v)) -#define B_RV_SIZE_T(v) B_RV(b_number_create_size_t(v)) - -#define B_NUMBER_IVAL(p) (b_number_get_size_t(p)) -#define B_NUMBER_FVAL(p) (b_number_get_double(p)) - -#define B_TYPE_NUMBER (b_number_get_type()) - -B_DECLARE_TYPE(b_number); - -B_TYPE_CLASS_DECLARATION_BEGIN(b_number) -B_TYPE_CLASS_DECLARATION_END(b_number) - -typedef enum b_number_type { - B_NUMBER_INT8, - B_NUMBER_INT16, - B_NUMBER_INT32, - B_NUMBER_INT64, - B_NUMBER_FLOAT32, - B_NUMBER_FLOAT64, - B_NUMBER_CHAR, - B_NUMBER_SHORT, - B_NUMBER_INT, - B_NUMBER_LONG, - B_NUMBER_LONGLONG, - B_NUMBER_FLOAT, - B_NUMBER_DOUBLE, - B_NUMBER_SIZE_T, - B_NUMBER_HANDLE, - B_NUMBER_TYPE_COUNT, - - B_NUMBER_BYTE = B_NUMBER_INT8, - B_NUMBER_WORD = B_NUMBER_INT16, - B_NUMBER_DWORD = B_NUMBER_INT32, - B_NUMBER_QWORD = B_NUMBER_INT64, -} b_number_type; - -BLUE_API b_type b_number_get_type(void); - -BLUE_API b_number *b_number_create(b_number_type type, void *value_ptr); - -static inline b_number *b_number_create_int8(int8_t value) -{ - return b_number_create(B_NUMBER_INT8, &value); -} -static inline b_number *b_number_create_int16(int16_t value) -{ - return b_number_create(B_NUMBER_INT16, &value); -} -static inline b_number *b_number_create_int32(int32_t value) -{ - return b_number_create(B_NUMBER_INT32, &value); -} -static inline b_number *b_number_create_int64(int64_t value) -{ - return b_number_create(B_NUMBER_INT64, &value); -} -static inline b_number *b_number_create_float32(float value) -{ - return b_number_create(B_NUMBER_FLOAT32, &value); -} -static inline b_number *b_number_create_float64(double value) -{ - return b_number_create(B_NUMBER_FLOAT64, &value); -} -static inline b_number *b_number_create_char(char value) -{ - return b_number_create(B_NUMBER_CHAR, &value); -} -static inline b_number *b_number_create_short(short value) -{ - return b_number_create(B_NUMBER_SHORT, &value); -} -static inline b_number *b_number_create_int(int value) -{ - return b_number_create(B_NUMBER_INT, &value); -} -static inline b_number *b_number_create_long(long value) -{ - return b_number_create(B_NUMBER_LONG, &value); -} -static inline b_number *b_number_create_longlong(long long value) -{ - return b_number_create(B_NUMBER_LONGLONG, &value); -} -static inline b_number *b_number_create_float(float value) -{ - return b_number_create(B_NUMBER_FLOAT, &value); -} -static inline b_number *b_number_create_double(double value) -{ - return b_number_create(B_NUMBER_DOUBLE, &value); -} -static inline b_number *b_number_create_size_t(size_t value) -{ - return b_number_create(B_NUMBER_SIZE_T, &value); -} - -BLUE_API b_number_type b_number_get_number_type(const b_number *number); -BLUE_API int b_number_get_value( - const b_number *number, b_number_type type, void *value_ptr); - -static inline int8_t b_number_get_int8(const b_number *number) -{ - int8_t v; - b_number_get_value(number, B_NUMBER_INT8, &v); - return v; -} - -static inline int16_t b_number_get_int16(const b_number *number) -{ - int16_t v; - b_number_get_value(number, B_NUMBER_INT16, &v); - return v; -} - -static inline int32_t b_number_get_int32(const b_number *number) -{ - int32_t v; - b_number_get_value(number, B_NUMBER_INT32, &v); - return v; -} - -static inline int64_t b_number_get_int64(const b_number *number) -{ - int64_t v; - b_number_get_value(number, B_NUMBER_INT64, &v); - return v; -} - -static inline float b_number_get_float32(const b_number *number) -{ - float v; - b_number_get_value(number, B_NUMBER_FLOAT32, &v); - return v; -} - -static inline double b_number_get_float64(const b_number *number) -{ - double v; - b_number_get_value(number, B_NUMBER_FLOAT64, &v); - return v; -} - -static inline char b_number_get_char(const b_number *number) -{ - char v; - b_number_get_value(number, B_NUMBER_CHAR, &v); - return v; -} - -static inline short b_number_get_short(const b_number *number) -{ - short v; - b_number_get_value(number, B_NUMBER_SHORT, &v); - return v; -} - -static inline int b_number_get_int(const b_number *number) -{ - int v; - b_number_get_value(number, B_NUMBER_INT, &v); - return v; -} - -static inline long b_number_get_long(const b_number *number) -{ - long v; - b_number_get_value(number, B_NUMBER_LONG, &v); - return v; -} - -static inline long long b_number_get_longlong(const b_number *number) -{ - long long v; - b_number_get_value(number, B_NUMBER_LONGLONG, &v); - return v; -} - -static inline float b_number_get_float(const b_number *number) -{ - float v; - b_number_get_value(number, B_NUMBER_FLOAT, &v); - return v; -} - -static inline double b_number_get_double(const b_number *number) -{ - double v; - b_number_get_value(number, B_NUMBER_DOUBLE, &v); - return v; -} - -static inline size_t b_number_get_size_t(const b_number *number) -{ - size_t v; - b_number_get_value(number, B_NUMBER_SIZE_T, &v); - return v; -} - -BLUE_API bool b_number_is_integer(const b_number *number); -BLUE_API bool b_number_is_float(const b_number *number); -BLUE_API bool b_number_is_inf(const b_number *number); -BLUE_API bool b_number_is_inf_positive(const b_number *number); -BLUE_API bool b_number_is_inf_negative(const b_number *number); -BLUE_API bool b_number_is_nan(const b_number *number); -BLUE_API bool b_number_is_nan_positive(const b_number *number); -BLUE_API bool b_number_is_nan_negative(const b_number *number); - -BLUE_API void b_number_set_inf_positive(b_number *number, bool v); -BLUE_API void b_number_set_inf_negative(b_number *number, bool v); -BLUE_API void b_number_set_nan_positive(b_number *number, bool v); -BLUE_API void b_number_set_nan_negative(b_number *number, bool v); - -BLUE_API size_t b_number_data_size(const b_number *number); - -B_DECLS_END; - -#endif diff --git a/ds/include/blue/ds/string.h b/ds/include/blue/ds/string.h deleted file mode 100644 index f00e7f0..0000000 --- a/ds/include/blue/ds/string.h +++ /dev/null @@ -1,129 +0,0 @@ -#ifndef BLUE_DS_STRING_H_ -#define BLUE_DS_STRING_H_ - -#include -#include -#include -#include -#include -#include - -B_DECLS_BEGIN; - -struct b_stream; -struct b_string_p; - -#define B_TYPE_STRING (b_string_get_type()) -#define B_TYPE_STRING_ITERATOR (b_string_iterator_get_type()) - -B_DECLARE_TYPE(b_string); -B_DECLARE_TYPE(b_string_iterator); - -B_TYPE_CLASS_DECLARATION_BEGIN(b_string) -B_TYPE_CLASS_DECLARATION_END(b_string) - -B_TYPE_CLASS_DECLARATION_BEGIN(b_string_iterator) -B_TYPE_CLASS_DECLARATION_END(b_string_iterator) - -#define B_CSTR(s) (b_string_create_from_cstr(s)) -#define B_RV_CSTR(s) (B_RV(b_string_create_from_cstr(s))) - -typedef enum b_strlen_flags { - B_STRLEN_NORMAL = 0, - B_STRLEN_IGNORE_ESC = 0x01u, - B_STRLEN_IGNORE_MOD = 0x02u, - B_STRLEN_CODEPOINTS = 0x04u, -} b_strlen_flags; - -typedef enum b_string_tokenise_flags { - B_STRING_TOK_F_NORMAL = 0x00u, - B_STRING_TOK_F_INCLUDE_EMPTY_TOKENS = 0x01u, -} b_string_tokenise_flags; - -BLUE_API b_type b_string_get_type(void); -BLUE_API b_type b_string_iterator_get_type(void); - -B_TYPE_DEFAULT_CONSTRUCTOR(b_string, B_TYPE_STRING); -BLUE_API b_string *b_string_create_from_cstr(const char *s); -BLUE_API b_string *b_string_create_from_wstr(const b_wchar *s); -BLUE_API b_string *b_string_create_from_c(char c, size_t count); -BLUE_API b_string *b_string_duplicate(const b_string *str); - -BLUE_API char *b_string_steal(b_string *str); -BLUE_API b_status b_string_reserve(b_string *str, size_t capacity); -BLUE_API b_status b_string_replace( - b_string *str, size_t start, size_t length, const char *new_data); -BLUE_API b_status b_string_replace_all(b_string *str, const char *new_data); -BLUE_API b_status b_string_replace_all_with_stringstream( - b_string *str, const b_stringstream *new_data); -BLUE_API b_status b_string_remove(b_string *str, size_t start, size_t length); -BLUE_API b_status b_string_transform(b_string *str, int (*transformer)(int)); -BLUE_API b_status b_string_trim(b_string *str); -static inline b_status b_string_toupper(b_string *str) -{ - return b_string_transform(str, toupper); -} -static inline b_status b_string_tolower(b_string *str) -{ - return b_string_transform(str, tolower); -} - -BLUE_API b_status b_string_append_c(b_string *dest, char c); -BLUE_API b_status b_string_append_wc(b_string *dest, b_wchar c); -BLUE_API b_status b_string_append_s(b_string *dest, const b_string *src); -BLUE_API b_status b_string_append_cstr(b_string *dest, const char *src); -BLUE_API b_status b_string_append_wstr(b_string *dest, const b_wchar *src); -BLUE_API b_status b_string_append_cstrf(b_string *dest, const char *format, ...); - -BLUE_API b_status b_string_prepend_c(b_string *dest, char c); -BLUE_API b_status b_string_prepend_wc(b_string *dest, b_wchar c); -BLUE_API b_status b_string_prepend_cstr(b_string *dest, const char *src); -BLUE_API b_status b_string_prepend_wstr(b_string *dest, const b_wchar *src); -BLUE_API b_status b_string_prepend_cstrf(b_string *dest, const char *format, ...); - -BLUE_API b_status b_string_insert_c(b_string *dest, char c, size_t at); -BLUE_API b_status b_string_insert_wc(b_string *dest, b_wchar c, size_t at); -BLUE_API b_status b_string_insert_s(b_string *dest, const b_string *src, size_t at); -BLUE_API b_status b_string_insert_cstr(b_string *dest, const char *src, size_t at); -BLUE_API b_status b_string_insert_wstr( - b_string *dest, const b_wchar *src, size_t at); -BLUE_API b_status b_string_insert_cstrn( - b_string *dest, const char *src, size_t len, size_t at); -BLUE_API b_status b_string_insert_wstrn( - b_string *dest, const char *src, size_t len, size_t at); -BLUE_API b_status b_string_insert_cstrf( - b_string *dest, size_t at, const char *format, ...); -BLUE_API void b_string_clear(b_string *str); - -BLUE_API b_iterator *b_string_tokenise( - b_string *str, const char *delims[], size_t nr_delims, - b_string_tokenise_flags flags); - -BLUE_API size_t b_string_get_size(const b_string *str, b_strlen_flags flags); -BLUE_API size_t b_string_get_capacity(const b_string *str); - -BLUE_API bool b_string_compare(const b_string *a, const b_string *b); - -BLUE_API char b_string_front(const b_string *str); -BLUE_API char b_string_back(const b_string *str); - -BLUE_API void b_string_pop_back(b_string *str); - -BLUE_API const char *b_string_ptr(const b_string *str); -BLUE_API b_string *b_string_substr(const b_string *str, size_t start, size_t len); - -BLUE_API int b_string_iterator_begin(const b_string *string, b_string_iterator *it); -BLUE_API bool b_string_iterator_next(b_string_iterator *it); -// BLUE_API b_status b_string_iterator_erase(b_string_iterator *it); -BLUE_API bool b_string_iterator_is_valid(const b_string_iterator *it); - -BLUE_API char *b_strdup(const char *s); -BLUE_API size_t b_strlen(const char *s, b_strlen_flags flags); -BLUE_API b_wchar *b_wstrdup(const b_wchar *s); -BLUE_API size_t b_wstrlen(const b_wchar *s); - -BLUE_API uint64_t b_string_hash(const b_string *s); - -B_DECLS_END; - -#endif diff --git a/ds/include/blue/ds/tree.h b/ds/include/blue/ds/tree.h deleted file mode 100644 index 7d2a272..0000000 --- a/ds/include/blue/ds/tree.h +++ /dev/null @@ -1,57 +0,0 @@ -#ifndef BLUE_DS_TREE_H_ -#define BLUE_DS_TREE_H_ - -#include -#include -#include -#include - -B_DECLS_BEGIN; - -#define B_TYPE_TREE (b_tree_get_type()) -#define B_TYPE_TREE_ITERATOR (b_tree_iterator_get_type()) - -B_DECLARE_TYPE(b_tree); -B_DECLARE_TYPE(b_tree_iterator); - -B_TYPE_CLASS_DECLARATION_BEGIN(b_tree) -B_TYPE_CLASS_DECLARATION_END(b_tree) - -B_TYPE_CLASS_DECLARATION_BEGIN(b_tree_iterator) -B_TYPE_CLASS_DECLARATION_END(b_tree_iterator) - -#define B_TREE_NODE_INIT ((b_tree_node) {0}) - -#define B_TREE_CONTAINER(t, m, v) \ - ((void *)((v) ? (uintptr_t)(v) - (offsetof(t, m)) : 0)) - -typedef struct b_tree_node { - struct b_tree_node *__p01, *__p02, *__p03; - struct b_queue_entry __q01; -} b_tree_node; - -BLUE_API b_type b_tree_get_type(void); -BLUE_API b_type b_tree_iterator_get_type(void); - -B_TYPE_DEFAULT_CONSTRUCTOR(b_tree, B_TYPE_TREE); - -BLUE_API void b_tree_set_root(b_tree *tree, struct b_tree_node *node); - -BLUE_API void b_tree_node_add_child(b_tree_node *parent, b_tree_node *child); -BLUE_API void b_tree_node_add_sibling(b_tree_node *node, b_tree_node *to_add); - -BLUE_API b_tree_node *b_tree_node_get_child(b_tree_node *node, size_t at); -BLUE_API b_tree_node *b_tree_node_get_parent(b_tree_node *node); - -BLUE_API b_iterator *b_tree_begin(b_tree *tree); -BLUE_API const b_iterator *b_tree_cbegin(const b_tree *tree); - -BLUE_API b_iterator *b_tree_node_begin(b_tree_node *node); -BLUE_API const b_iterator *b_tree_node_cbegin(const b_tree_node *node); - -BLUE_API b_iterator *b_tree_node_begin_recursive(b_tree_node *node); -BLUE_API const b_iterator *b_tree_node_cbegin_recursive(const b_tree_node *node); - -B_DECLS_END; - -#endif diff --git a/ds/include/blue/ds/uuid.h b/ds/include/blue/ds/uuid.h deleted file mode 100644 index 94eaf11..0000000 --- a/ds/include/blue/ds/uuid.h +++ /dev/null @@ -1,50 +0,0 @@ -#ifndef BLUE_DS_UUID_H_ -#define BLUE_DS_UUID_H_ - -#include -#include -#include - -#define B_UUID_NBYTES 16 -#define B_UUID_STRING_MAX 37 - -B_DECLS_BEGIN; - -#define B_TYPE_UUID (b_uuid_get_type()) - -B_DECLARE_TYPE(b_uuid); - -B_TYPE_CLASS_DECLARATION_BEGIN(b_uuid) -B_TYPE_CLASS_DECLARATION_END(b_uuid) - -typedef union b_uuid_bytes { - uint8_t uuid_bytes[B_UUID_NBYTES]; - uint16_t uuid_words[B_UUID_NBYTES / 2]; - uint32_t uuid_dwords[B_UUID_NBYTES / 4]; - uint64_t uuid_qwords[B_UUID_NBYTES / 8]; -} b_uuid_bytes; - -BLUE_API b_type b_uuid_get_type(void); - -B_TYPE_DEFAULT_CONSTRUCTOR(b_uuid, B_TYPE_UUID); - -BLUE_API b_uuid *b_uuid_create_from_bytes( - unsigned char u00, unsigned char u01, unsigned char u02, - unsigned char u03, unsigned char u04, unsigned char u05, - unsigned char u06, unsigned char u07, unsigned char u08, - unsigned char u09, unsigned char u10, unsigned char u11, unsigned char u12, - unsigned char u13, unsigned char u14, unsigned char u15); -BLUE_API b_uuid *b_uuid_create_from_bytev(const unsigned char bytes[B_UUID_NBYTES]); -BLUE_API b_uuid *b_uuid_create_from_uuid_bytes(const b_uuid_bytes *bytes); -BLUE_API b_uuid *b_uuid_create_from_string(const b_string *string); -BLUE_API b_uuid *b_uuid_create_from_cstr(const char *s); - -BLUE_API b_status b_uuid_to_cstr(const b_uuid *uuid, char out[B_UUID_STRING_MAX]); -BLUE_API void b_uuid_get_bytes( - const b_uuid *uuid, unsigned char bytes[B_UUID_NBYTES]); -BLUE_API void b_uuid_get_uuid_bytes(const b_uuid *uuid, b_uuid_bytes *bytes); -BLUE_API b_uuid_bytes *b_uuid_ptr(b_uuid *uuid); - -B_DECLS_END; - -#endif diff --git a/ds/include/blue/ds.h b/ds/include/fx/ds.h similarity index 100% rename from ds/include/blue/ds.h rename to ds/include/fx/ds.h diff --git a/ds/include/fx/ds/array.h b/ds/include/fx/ds/array.h new file mode 100644 index 0000000..302fe1e --- /dev/null +++ b/ds/include/fx/ds/array.h @@ -0,0 +1,218 @@ +/** + * A heterogeneous array of objects. fx_array only stores references + * to the objects that it contains, not the object data itself. + * + * fx_array stores pointers to objects in a single contiguous array, + * but this is an implementation detail that may change in the future. + * Users of fx_array should not rely on this being the case. + */ +#ifndef FX_DS_ARRAY_H_ +#define FX_DS_ARRAY_H_ + +#include +#include +#include +#include + +FX_DECLS_BEGIN; + +#define FX_TYPE_ARRAY (fx_array_get_type()) +#define FX_TYPE_ARRAY_ITERATOR (fx_array_iterator_get_type()) + +struct fx_array_p; + +FX_DECLARE_TYPE(fx_array); +FX_DECLARE_TYPE(fx_array_iterator); + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_array) +FX_TYPE_CLASS_DECLARATION_END(fx_array) + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_array_iterator) +FX_TYPE_CLASS_DECLARATION_END(fx_array_iterator) + +FX_API fx_type fx_array_get_type(void); +FX_API fx_type fx_array_iterator_get_type(void); + +FX_TYPE_DEFAULT_CONSTRUCTOR(fx_array, FX_TYPE_ARRAY); + +/** + * Creates an fx_array initialised with the contents of the provided + * fx_object pointer array. The fx_array will take a reference to each + * object specified in `values`, and will increment the reference count. + * The order of objects in the new fx_array will be the same as the order + * of objects in `values`. Any NULL pointers in the `values` array will + * be ignored, and will not result in gaps in the created fx_array. + * However, `nr_values` should be large enough to cover the final + * non-NULL pointer in `values`, including any NULL pointers in-between. + * + * @param values The list of object pointers which should make up the + * contents of the new fx_array. + * @param nr_values The size of the `values` array. + * @return A pointer to the new fx_array, or NULL if an error occurred. + */ +FX_API fx_array *fx_array_create_with_values( + fx_object *const *values, size_t nr_values); + +/** + * Remove all object references from an fx_array, resetting the size of the array to zero. + * The reference counts of all objects in the array will be decremented. + * + * @param array The fx_array to clear. + */ +FX_API void fx_array_clear(fx_array *array); + +/** + * Inserts an object at the end of an fx_array. The reference count of + * the object will be incremented. + * + * @param array The fx_array to append the object to. + * @param value The object to append. + * @return FX_SUCCESS if the object was appended successfully, or an + * error code if an error occurred. + */ +FX_API fx_status fx_array_append(fx_array *array, fx_object *value); + +/** + * 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); + +/** + * 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); + +/** + * Removes the object at the beginning of an fx_array. The reference count + * of the removed object will be decremented. The remaining objects will be moved + * to fill the empty space created by the object's removal. + * + * @param array The fx_array to remove the object from. + * @return FX_SUCCESS if the object was removed, or a status code describing any error that occurred. + */ +FX_API fx_status fx_array_remove_front(fx_array *array); + +/** + * Removes the object at the end of an fx_array. The reference count + * of the removed object will be decremented. + * + * @param array The fx_array to remove the object from. + * @return FX_SUCCESS if the object was removed, or a status code describing any error that occurred. + */ +FX_API fx_status fx_array_remove_back(fx_array *array); + +/** + * Removes the object at the specified index of an fx_array, and returns + * a pointer to it. The reference count of the removed object will NOT + * be decremented. The caller becomes the owner of the array's reference + * to the object. If the specified index is at the beginning or mid-way + * through the array (i.e. not at the end), the remaining objects will + * be moved to fill the empty space created by the object's removal. + * + * @param array The fx_array to remove the object from. + * @param at The index of the object to be removed. + * @return An pointer to the removed object. This pointer is owned by + * the caller. Returns NULL if an error occurred. + */ +FX_API fx_object *fx_array_pop(fx_array *array, size_t at); + +/** + * 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); + +/** + * 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); + +/** + * 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); + +/** + * 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); + +/** + * 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); + +/** + * 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; + +#endif diff --git a/ds/include/fx/ds/bitbuffer.h b/ds/include/fx/ds/bitbuffer.h new file mode 100644 index 0000000..e840032 --- /dev/null +++ b/ds/include/fx/ds/bitbuffer.h @@ -0,0 +1,25 @@ +#ifndef FX_DS_BITBUFFER_H_ +#define FX_DS_BITBUFFER_H_ + +#include + +FX_DECLS_BEGIN; + +FX_DECLARE_TYPE(fx_bitbuffer); + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_bitbuffer) + ; +FX_TYPE_CLASS_DECLARATION_END(fx_bitbuffer); + +FX_API fx_status fx_bitbuffer_put_bit(fx_bitbuffer *buf, int bit); +FX_API fx_status fx_bitbuffer_put_bool(fx_bitbuffer *buf, bool b); +FX_API fx_status fx_bitbuffer_put_int( + fx_bitbuffer *buf, uint64_t v, unsigned int nr_bits); +FX_API fx_status fx_bitbuffer_put_bytes( + fx_bitbuffer *buf, const void *p, size_t len, size_t bits_per_byte); +FX_API fx_status fx_bitbuffer_put_string( + fx_bitbuffer *buf, const char *p, size_t len, size_t bits_per_char); + +FX_DECLS_END; + +#endif diff --git a/ds/include/fx/ds/bitmap.h b/ds/include/fx/ds/bitmap.h new file mode 100644 index 0000000..3244b4f --- /dev/null +++ b/ds/include/fx/ds/bitmap.h @@ -0,0 +1,40 @@ +#ifndef FX_DS_BITMAP_H_ +#define FX_DS_BITMAP_H_ + +#include +#include +#include + +FX_DECLS_BEGIN; + +#define FX_TYPE_BITMAP (fx_bitmap_get_type()) + +FX_DECLARE_TYPE(fx_bitmap); + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_bitmap) +FX_TYPE_CLASS_DECLARATION_END(fx_bitmap) + +FX_API fx_type fx_bitmap_get_type(void); + +FX_API fx_bitmap *fx_bitmap_create(size_t nr_bits); + +FX_API void fx_bitmap_set_bit(fx_bitmap *map, size_t bit); +FX_API void fx_bitmap_clear_bit(fx_bitmap *map, size_t bit); +FX_API void fx_bitmap_set_range(fx_bitmap *map, size_t first_bit, size_t nbits); +FX_API void fx_bitmap_clear_range(fx_bitmap *map, size_t first_bit, size_t nbits); +FX_API void fx_bitmap_set_all(fx_bitmap *map); +FX_API void fx_bitmap_clear_all(fx_bitmap *map); + +FX_API bool fx_bitmap_check_bit(const fx_bitmap *map, size_t bit); + +FX_API size_t fx_bitmap_count_set_bits(const fx_bitmap *map); +FX_API size_t fx_bitmap_count_clear_bits(const fx_bitmap *map); + +FX_API size_t fx_bitmap_highest_set_bit(const fx_bitmap *map); +FX_API size_t fx_bitmap_highest_clear_bit(const fx_bitmap *map); +FX_API size_t fx_bitmap_lowest_set_bit(const fx_bitmap *map); +FX_API size_t fx_bitmap_lowest_clear_bit(const fx_bitmap *map); + +FX_DECLS_END; + +#endif diff --git a/ds/include/fx/ds/buffer.h b/ds/include/fx/ds/buffer.h new file mode 100644 index 0000000..f02e48e --- /dev/null +++ b/ds/include/fx/ds/buffer.h @@ -0,0 +1,49 @@ +#ifndef FX_DS_BUFFER_H_ +#define FX_DS_BUFFER_H_ + +#include +#include + +FX_DECLS_BEGIN; + +#define FX_TYPE_BUFFER (fx_buffer_get_type()) + +FX_DECLARE_TYPE(fx_buffer); + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_buffer) +FX_TYPE_CLASS_DECLARATION_END(fx_buffer) + +FX_API fx_type fx_buffer_get_type(void); + +FX_API fx_buffer *fx_buffer_create(size_t item_sz); +FX_API fx_buffer *fx_buffer_create_from_bytes(const void *p, size_t len); +FX_API fx_buffer *fx_buffer_create_from_array( + const void *p, size_t item_sz, size_t len); + +FX_API void *fx_buffer_steal(fx_buffer *buf); +FX_API fx_status fx_buffer_reserve(fx_buffer *buf, size_t capacity); +FX_API fx_status fx_buffer_resize(fx_buffer *buf, size_t length); + +FX_API fx_status fx_buffer_append(fx_buffer *dest, const void *p, size_t count); +FX_API fx_status fx_buffer_prepend(fx_buffer *dest, const void *p, size_t count); +FX_API fx_status fx_buffer_insert( + fx_buffer *dest, const void *p, size_t count, size_t at); +FX_API fx_status fx_buffer_remove(fx_buffer *dest, size_t at, size_t count); +FX_API fx_status fx_buffer_clear(fx_buffer *buf); + +FX_API fx_status fx_buffer_push_back(fx_buffer *buf, size_t count, void **p); +FX_API fx_status fx_buffer_push_front(fx_buffer *buf, size_t count, void **p); + +FX_API fx_status fx_buffer_pop_back(fx_buffer *buf, size_t count); +FX_API fx_status fx_buffer_pop_front(fx_buffer *buf, size_t count); + +FX_API size_t fx_buffer_get_size(const fx_buffer *buf); +FX_API size_t fx_buffer_get_item_size(const fx_buffer *buf); +FX_API size_t fx_buffer_get_capacity(const fx_buffer *buf); + +FX_API void *fx_buffer_ptr(const fx_buffer *buf); +FX_API void *fx_buffer_get(const fx_buffer *buf, size_t at); + +FX_DECLS_END; + +#endif diff --git a/ds/include/fx/ds/datetime.h b/ds/include/fx/ds/datetime.h new file mode 100644 index 0000000..ddf496a --- /dev/null +++ b/ds/include/fx/ds/datetime.h @@ -0,0 +1,48 @@ +#ifndef FX_DS_DATETIME_H_ +#define FX_DS_DATETIME_H_ + +#include +#include +#include + +FX_DECLS_BEGIN; + +#define FX_TYPE_DATETIME (fx_datetime_get_type()) + +FX_DECLARE_TYPE(fx_datetime); + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_datetime) +FX_TYPE_CLASS_DECLARATION_END(fx_datetime) + +typedef enum fx_datetime_format { + FX_DATETIME_FORMAT_RFC3339 = 1, +} fx_datetime_format; + +FX_API fx_type fx_datetime_get_type(void); + +FX_TYPE_DEFAULT_CONSTRUCTOR(fx_datetime, FX_TYPE_DATETIME); + +FX_API fx_datetime *fx_datetime_parse(fx_datetime_format format, const char *s); +FX_API void fx_datetime_to_string( + const fx_datetime *dt, fx_datetime_format format, + FX_TYPE_FWDREF(fx_stream) * dest); + +FX_API bool fx_datetime_is_localtime(const fx_datetime *dt); +FX_API bool fx_datetime_has_date(const fx_datetime *dt); +FX_API bool fx_datetime_has_time(const fx_datetime *dt); + +FX_API long fx_datetime_year(const fx_datetime *dt); +FX_API long fx_datetime_month(const fx_datetime *dt); +FX_API long fx_datetime_day(const fx_datetime *dt); +FX_API long fx_datetime_hour(const fx_datetime *dt); +FX_API long fx_datetime_minute(const fx_datetime *dt); +FX_API long fx_datetime_second(const fx_datetime *dt); +FX_API long fx_datetime_subsecond(const fx_datetime *dt); + +FX_API bool fx_datetime_zone_offset_is_negative(const fx_datetime *dt); +FX_API long fx_datetime_zone_offset_hour(const fx_datetime *dt); +FX_API long fx_datetime_zone_offset_minute(const fx_datetime *dt); + +FX_DECLS_END; + +#endif diff --git a/ds/include/fx/ds/dict.h b/ds/include/fx/ds/dict.h new file mode 100644 index 0000000..1bf58f0 --- /dev/null +++ b/ds/include/fx/ds/dict.h @@ -0,0 +1,62 @@ +#ifndef FX_DS_DICT_H_ +#define FX_DS_DICT_H_ + +#include +#include +#include +#include +#include +#include + +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 fx_dict_get_type(void); +FX_API fx_type 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 diff --git a/ds/include/fx/ds/hashmap.h b/ds/include/fx/ds/hashmap.h new file mode 100644 index 0000000..cd778be --- /dev/null +++ b/ds/include/fx/ds/hashmap.h @@ -0,0 +1,83 @@ +#ifndef FX_DS_HASHMAP_H_ +#define FX_DS_HASHMAP_H_ + +#include +#include +#include +#include +#include +#include + +FX_DECLS_BEGIN; + +struct fx_hashmap_p; + +#define FX_TYPE_HASHMAP (fx_hashmap_get_type()) +#define FX_TYPE_HASHMAP_ITERATOR (fx_hashmap_iterator_get_type()) + +FX_DECLARE_TYPE(fx_hashmap); +FX_DECLARE_TYPE(fx_hashmap_iterator); + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_hashmap) +FX_TYPE_CLASS_DECLARATION_END(fx_hashmap) + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_hashmap_iterator) +FX_TYPE_CLASS_DECLARATION_END(fx_hashmap_iterator) + +#define FX_HASHMAP_KEY(k, ks) {.key_data = (k), .key_size = (ks)} +#define FX_HASHMAP_VALUE(v, vs) {.value_data = (v), .value_size = (vs)} + +#define FX_HASHMAP_ITEM(k, ks, v, vs) \ + {.key = FX_HASHMAP_KEY(k, ks), .value = FX_HASHMAP_VALUE(v, vs)} + +#define FX_HASHMAP_ITEM_END {.key = {0}, .value = {0}} + +#define fx_hashmap_foreach(it, hashmap) \ + for (int z__fx_unique_name() = fx_hashmap_iterator_begin(hashmap, it); \ + (it)->key != NULL; fx_hashmap_iterator_next(it)) + +typedef void (*fx_hashmap_key_destructor)(void *); +typedef void (*fx_hashmap_value_destructor)(void *); + +typedef enum fx_hashmap_key_flags { + FX_HASHMAP_KEY_F_INTVALUE = 0x01u, +} fx_hashmap_key_flags; + +typedef struct fx_hashmap_key { + fx_hashmap_key_flags key_flags; + const void *key_data; + size_t key_size; +} fx_hashmap_key; + +typedef struct fx_hashmap_value { + void *value_data; + size_t value_size; +} fx_hashmap_value; + +typedef struct fx_hashmap_item { + fx_hashmap_key key; + fx_hashmap_value value; +} fx_hashmap_item; + +FX_API fx_type fx_hashmap_get_type(void); +FX_API fx_type fx_hashmap_iterator_get_type(void); + +FX_API fx_hashmap *fx_hashmap_create( + fx_hashmap_key_destructor key_dtor, fx_hashmap_value_destructor value_dtor); +FX_API fx_hashmap *fx_hashmap_create_with_items(const fx_hashmap_item *items); + +FX_API fx_status fx_hashmap_put( + fx_hashmap *hashmap, const fx_hashmap_key *key, const fx_hashmap_value *value); +FX_API const fx_hashmap_value *fx_hashmap_get( + const fx_hashmap *hashmap, const fx_hashmap_key *key); + +FX_API bool fx_hashmap_has_key(const fx_hashmap *hashmap, const fx_hashmap_key *key); +FX_API size_t fx_hashmap_get_size(const fx_hashmap *hashmap); +FX_API bool fx_hashmap_is_empty(const fx_hashmap *hashmap); + +FX_API fx_iterator *fx_hashmap_begin(fx_hashmap *hashmap); +FX_API const fx_iterator *fx_hashmap_cbegin(const fx_hashmap *hashmap); + +FX_DECLS_END; + +#endif diff --git a/ds/include/fx/ds/list.h b/ds/include/fx/ds/list.h new file mode 100644 index 0000000..728397d --- /dev/null +++ b/ds/include/fx/ds/list.h @@ -0,0 +1,63 @@ +#ifndef FX_DS_LIST_H_ +#define FX_DS_LIST_H_ + +#include +#include +#include + +FX_DECLS_BEGIN; + +#define FX_TYPE_LIST (fx_list_get_type()) +#define FX_TYPE_LIST_ITERATOR (fx_list_iterator_get_type()) + +struct fx_list_p; + +FX_DECLARE_TYPE(fx_list); +FX_DECLARE_TYPE(fx_list_iterator); + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_list) +FX_TYPE_CLASS_DECLARATION_END(fx_list) + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_list_iterator) +FX_TYPE_CLASS_DECLARATION_END(fx_list_iterator) + +typedef struct fx_list_entry fx_list_entry; + +FX_API fx_type fx_list_get_type(void); +FX_API fx_type fx_list_iterator_get_type(void); + +FX_TYPE_DEFAULT_CONSTRUCTOR(fx_list, FX_TYPE_LIST); + +FX_API bool fx_list_empty(fx_list *q); +FX_API void *fx_list_first_item(const fx_list *q); +FX_API void *fx_list_last_item(const fx_list *q); +FX_API fx_list_entry *fx_list_first_entry(const fx_list *q); +FX_API fx_list_entry *fx_list_last_entry(const fx_list *q); +FX_API fx_list_entry *fx_list_next(const fx_list_entry *entry); +FX_API fx_list_entry *fx_list_prev(const fx_list_entry *entry); + +FX_API size_t fx_list_length(const fx_list *q); + +FX_API fx_list_entry *fx_list_insert_before( + fx_list *q, void *ptr, fx_list_entry *before); +FX_API fx_list_entry *fx_list_insert_after( + fx_list *q, void *ptr, fx_list_entry *after); + +FX_API fx_list_entry *fx_list_push_front(fx_list *q, void *ptr); +FX_API fx_list_entry *fx_list_push_back(fx_list *q, void *ptr); + +FX_API void *fx_list_pop_front(fx_list *q); +FX_API void *fx_list_pop_back(fx_list *q); + +FX_API fx_status fx_list_delete_item(fx_list *q, void *ptr); +FX_API fx_status fx_list_delete_entry(fx_list *q, fx_list_entry *entry); +FX_API void fx_list_delete_all(fx_list *q); + +FX_API void *fx_list_entry_value(const fx_list_entry *entry); + +FX_API fx_iterator *fx_list_begin(fx_list *q); +FX_API const fx_iterator *fx_list_cbegin(const fx_list *q); + +FX_DECLS_END; + +#endif diff --git a/ds/include/fx/ds/number.h b/ds/include/fx/ds/number.h new file mode 100644 index 0000000..1bd06c9 --- /dev/null +++ b/ds/include/fx/ds/number.h @@ -0,0 +1,254 @@ +#ifndef FX_DS_NUMBER_H +#define FX_DS_NUMBER_H + +#include +#include + +FX_DECLS_BEGIN; + +#define FX_INT8(v) (fx_number_create_int8(v)) +#define FX_INT16(v) (fx_number_create_int16(v)) +#define FX_INT32(v) (fx_number_create_int32(v)) +#define FX_INT64(v) (fx_number_create_int64(v)) +#define FX_FLOAT32(v) (fx_number_create_float32(v)) +#define FX_FLOAT64(v) (fx_number_create_float64(v)) +#define FX_CHAR(v) (fx_number_create_char(v)) +#define FX_SHORT(v) (fx_number_create_short(v)) +#define FX_INT(v) (fx_number_create_int(v)) +#define FX_LONG(v) (fx_number_create_long(v)) +#define FX_LONGLONG(v) (fx_number_create_longlong(v)) +#define FX_FLOAT(v) (fx_number_create_float(v)) +#define FX_DOUBLE(v) (fx_number_create_double(v)) +#define FX_SIZE_T(v) (fx_number_create_size_t(v)) + +#define FX_RV_INT8(v) FX_RV(fx_number_create_int8(v)) +#define FX_RV_INT16(v) FX_RV(fx_number_create_int16(v)) +#define FX_RV_INT32(v) FX_RV(fx_number_create_int32(v)) +#define FX_RV_INT64(v) FX_RV(fx_number_create_int64(v)) +#define FX_RV_FLOAT32(v) FX_RV(fx_number_create_float32(v)) +#define FX_RV_FLOAT64(v) FX_RV(fx_number_create_float64(v)) +#define FX_RV_CHAR(v) FX_RV(fx_number_create_char(v)) +#define FX_RV_SHORT(v) FX_RV(fx_number_create_short(v)) +#define FX_RV_INT(v) FX_RV(fx_number_create_int(v)) +#define FX_RV_LONG(v) FX_RV(fx_number_create_long(v)) +#define FX_RV_LONGLONG(v) FX_RV(fx_number_create_longlong(v)) +#define FX_RV_FLOAT(v) FX_RV(fx_number_create_float(v)) +#define FX_RV_DOUBLE(v) FX_RV(fx_number_create_double(v)) +#define FX_RV_SIZE_T(v) FX_RV(fx_number_create_size_t(v)) + +#define FX_NUMBER_IVAL(p) (fx_number_get_size_t(p)) +#define FX_NUMBER_FVAL(p) (fx_number_get_double(p)) + +#define FX_TYPE_NUMBER (fx_number_get_type()) + +FX_DECLARE_TYPE(fx_number); + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_number) +FX_TYPE_CLASS_DECLARATION_END(fx_number) + +typedef enum fx_number_type { + FX_NUMBER_INT8, + FX_NUMBER_INT16, + FX_NUMBER_INT32, + FX_NUMBER_INT64, + FX_NUMBER_FLOAT32, + FX_NUMBER_FLOAT64, + FX_NUMBER_CHAR, + FX_NUMBER_SHORT, + FX_NUMBER_INT, + FX_NUMBER_LONG, + FX_NUMBER_LONGLONG, + FX_NUMBER_FLOAT, + FX_NUMBER_DOUBLE, + FX_NUMBER_SIZE_T, + FX_NUMBER_HANDLE, + FX_NUMBER_TYPE_COUNT, + + FX_NUMBER_BYTE = FX_NUMBER_INT8, + FX_NUMBER_WORD = FX_NUMBER_INT16, + FX_NUMBER_DWORD = FX_NUMBER_INT32, + FX_NUMBER_QWORD = FX_NUMBER_INT64, +} fx_number_type; + +FX_API fx_type fx_number_get_type(void); + +FX_API fx_number *fx_number_create(fx_number_type type, void *value_ptr); + +static inline fx_number *fx_number_create_int8(int8_t value) +{ + return fx_number_create(FX_NUMBER_INT8, &value); +} +static inline fx_number *fx_number_create_int16(int16_t value) +{ + return fx_number_create(FX_NUMBER_INT16, &value); +} +static inline fx_number *fx_number_create_int32(int32_t value) +{ + return fx_number_create(FX_NUMBER_INT32, &value); +} +static inline fx_number *fx_number_create_int64(int64_t value) +{ + return fx_number_create(FX_NUMBER_INT64, &value); +} +static inline fx_number *fx_number_create_float32(float value) +{ + return fx_number_create(FX_NUMBER_FLOAT32, &value); +} +static inline fx_number *fx_number_create_float64(double value) +{ + return fx_number_create(FX_NUMBER_FLOAT64, &value); +} +static inline fx_number *fx_number_create_char(char value) +{ + return fx_number_create(FX_NUMBER_CHAR, &value); +} +static inline fx_number *fx_number_create_short(short value) +{ + return fx_number_create(FX_NUMBER_SHORT, &value); +} +static inline fx_number *fx_number_create_int(int value) +{ + return fx_number_create(FX_NUMBER_INT, &value); +} +static inline fx_number *fx_number_create_long(long value) +{ + return fx_number_create(FX_NUMBER_LONG, &value); +} +static inline fx_number *fx_number_create_longlong(long long value) +{ + return fx_number_create(FX_NUMBER_LONGLONG, &value); +} +static inline fx_number *fx_number_create_float(float value) +{ + return fx_number_create(FX_NUMBER_FLOAT, &value); +} +static inline fx_number *fx_number_create_double(double value) +{ + return fx_number_create(FX_NUMBER_DOUBLE, &value); +} +static inline fx_number *fx_number_create_size_t(size_t value) +{ + return fx_number_create(FX_NUMBER_SIZE_T, &value); +} + +FX_API fx_number_type fx_number_get_number_type(const fx_number *number); +FX_API int fx_number_get_value( + const fx_number *number, fx_number_type type, void *value_ptr); + +static inline int8_t fx_number_get_int8(const fx_number *number) +{ + int8_t v; + fx_number_get_value(number, FX_NUMBER_INT8, &v); + return v; +} + +static inline int16_t fx_number_get_int16(const fx_number *number) +{ + int16_t v; + fx_number_get_value(number, FX_NUMBER_INT16, &v); + return v; +} + +static inline int32_t fx_number_get_int32(const fx_number *number) +{ + int32_t v; + fx_number_get_value(number, FX_NUMBER_INT32, &v); + return v; +} + +static inline int64_t fx_number_get_int64(const fx_number *number) +{ + int64_t v; + fx_number_get_value(number, FX_NUMBER_INT64, &v); + return v; +} + +static inline float fx_number_get_float32(const fx_number *number) +{ + float v; + fx_number_get_value(number, FX_NUMBER_FLOAT32, &v); + return v; +} + +static inline double fx_number_get_float64(const fx_number *number) +{ + double v; + fx_number_get_value(number, FX_NUMBER_FLOAT64, &v); + return v; +} + +static inline char fx_number_get_char(const fx_number *number) +{ + char v; + fx_number_get_value(number, FX_NUMBER_CHAR, &v); + return v; +} + +static inline short fx_number_get_short(const fx_number *number) +{ + short v; + fx_number_get_value(number, FX_NUMBER_SHORT, &v); + return v; +} + +static inline int fx_number_get_int(const fx_number *number) +{ + int v; + fx_number_get_value(number, FX_NUMBER_INT, &v); + return v; +} + +static inline long fx_number_get_long(const fx_number *number) +{ + long v; + fx_number_get_value(number, FX_NUMBER_LONG, &v); + return v; +} + +static inline long long fx_number_get_longlong(const fx_number *number) +{ + long long v; + fx_number_get_value(number, FX_NUMBER_LONGLONG, &v); + return v; +} + +static inline float fx_number_get_float(const fx_number *number) +{ + float v; + fx_number_get_value(number, FX_NUMBER_FLOAT, &v); + return v; +} + +static inline double fx_number_get_double(const fx_number *number) +{ + double v; + fx_number_get_value(number, FX_NUMBER_DOUBLE, &v); + return v; +} + +static inline size_t fx_number_get_size_t(const fx_number *number) +{ + size_t v; + fx_number_get_value(number, FX_NUMBER_SIZE_T, &v); + return v; +} + +FX_API bool fx_number_is_integer(const fx_number *number); +FX_API bool fx_number_is_float(const fx_number *number); +FX_API bool fx_number_is_inf(const fx_number *number); +FX_API bool fx_number_is_inf_positive(const fx_number *number); +FX_API bool fx_number_is_inf_negative(const fx_number *number); +FX_API bool fx_number_is_nan(const fx_number *number); +FX_API bool fx_number_is_nan_positive(const fx_number *number); +FX_API bool fx_number_is_nan_negative(const fx_number *number); + +FX_API void fx_number_set_inf_positive(fx_number *number, bool v); +FX_API void fx_number_set_inf_negative(fx_number *number, bool v); +FX_API void fx_number_set_nan_positive(fx_number *number, bool v); +FX_API void fx_number_set_nan_negative(fx_number *number, bool v); + +FX_API size_t fx_number_data_size(const fx_number *number); + +FX_DECLS_END; + +#endif diff --git a/ds/include/fx/ds/string.h b/ds/include/fx/ds/string.h new file mode 100644 index 0000000..e024a98 --- /dev/null +++ b/ds/include/fx/ds/string.h @@ -0,0 +1,129 @@ +#ifndef FX_DS_STRING_H_ +#define FX_DS_STRING_H_ + +#include +#include +#include +#include +#include +#include + +FX_DECLS_BEGIN; + +struct fx_stream; +struct fx_string_p; + +#define FX_TYPE_STRING (fx_string_get_type()) +#define FX_TYPE_STRING_ITERATOR (fx_string_iterator_get_type()) + +FX_DECLARE_TYPE(fx_string); +FX_DECLARE_TYPE(fx_string_iterator); + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_string) +FX_TYPE_CLASS_DECLARATION_END(fx_string) + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_string_iterator) +FX_TYPE_CLASS_DECLARATION_END(fx_string_iterator) + +#define FX_CSTR(s) (fx_string_create_from_cstr(s)) +#define FX_RV_CSTR(s) (FX_RV(fx_string_create_from_cstr(s))) + +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; + +typedef enum fx_string_tokenise_flags { + FX_STRING_TOK_F_NORMAL = 0x00u, + FX_STRING_TOK_F_INCLUDE_EMPTY_TOKENS = 0x01u, +} fx_string_tokenise_flags; + +FX_API fx_type fx_string_get_type(void); +FX_API fx_type fx_string_iterator_get_type(void); + +FX_TYPE_DEFAULT_CONSTRUCTOR(fx_string, FX_TYPE_STRING); +FX_API fx_string *fx_string_create_from_cstr(const char *s); +FX_API fx_string *fx_string_create_from_wstr(const fx_wchar *s); +FX_API fx_string *fx_string_create_from_c(char c, size_t count); +FX_API fx_string *fx_string_duplicate(const fx_string *str); + +FX_API char *fx_string_steal(fx_string *str); +FX_API fx_status fx_string_reserve(fx_string *str, size_t capacity); +FX_API fx_status fx_string_replace( + fx_string *str, size_t start, size_t length, const char *new_data); +FX_API fx_status fx_string_replace_all(fx_string *str, const char *new_data); +FX_API fx_status fx_string_replace_all_with_stringstream( + fx_string *str, const fx_stringstream *new_data); +FX_API fx_status fx_string_remove(fx_string *str, size_t start, size_t length); +FX_API fx_status fx_string_transform(fx_string *str, int (*transformer)(int)); +FX_API fx_status fx_string_trim(fx_string *str); +static inline fx_status fx_string_toupper(fx_string *str) +{ + return fx_string_transform(str, toupper); +} +static inline fx_status fx_string_tolower(fx_string *str) +{ + return fx_string_transform(str, tolower); +} + +FX_API fx_status fx_string_append_c(fx_string *dest, char c); +FX_API fx_status fx_string_append_wc(fx_string *dest, fx_wchar c); +FX_API fx_status fx_string_append_s(fx_string *dest, const fx_string *src); +FX_API fx_status fx_string_append_cstr(fx_string *dest, const char *src); +FX_API fx_status fx_string_append_wstr(fx_string *dest, const fx_wchar *src); +FX_API fx_status fx_string_append_cstrf(fx_string *dest, const char *format, ...); + +FX_API fx_status fx_string_prepend_c(fx_string *dest, char c); +FX_API fx_status fx_string_prepend_wc(fx_string *dest, fx_wchar c); +FX_API fx_status fx_string_prepend_cstr(fx_string *dest, const char *src); +FX_API fx_status fx_string_prepend_wstr(fx_string *dest, const fx_wchar *src); +FX_API fx_status fx_string_prepend_cstrf(fx_string *dest, const char *format, ...); + +FX_API fx_status fx_string_insert_c(fx_string *dest, char c, size_t at); +FX_API fx_status fx_string_insert_wc(fx_string *dest, fx_wchar c, size_t at); +FX_API fx_status fx_string_insert_s(fx_string *dest, const fx_string *src, size_t at); +FX_API fx_status fx_string_insert_cstr(fx_string *dest, const char *src, size_t at); +FX_API fx_status fx_string_insert_wstr( + fx_string *dest, const fx_wchar *src, size_t at); +FX_API fx_status fx_string_insert_cstrn( + fx_string *dest, const char *src, size_t len, size_t at); +FX_API fx_status fx_string_insert_wstrn( + fx_string *dest, const char *src, size_t len, size_t at); +FX_API fx_status fx_string_insert_cstrf( + fx_string *dest, size_t at, const char *format, ...); +FX_API void fx_string_clear(fx_string *str); + +FX_API fx_iterator *fx_string_tokenise( + fx_string *str, const char *delims[], size_t nr_delims, + fx_string_tokenise_flags flags); + +FX_API size_t fx_string_get_size(const fx_string *str, fx_strlen_flags flags); +FX_API size_t fx_string_get_capacity(const fx_string *str); + +FX_API bool fx_string_compare(const fx_string *a, const fx_string *b); + +FX_API char fx_string_front(const fx_string *str); +FX_API char fx_string_back(const fx_string *str); + +FX_API void fx_string_pop_back(fx_string *str); + +FX_API const char *fx_string_ptr(const fx_string *str); +FX_API fx_string *fx_string_substr(const fx_string *str, size_t start, size_t len); + +FX_API int fx_string_iterator_begin(const fx_string *string, fx_string_iterator *it); +FX_API bool fx_string_iterator_next(fx_string_iterator *it); +// FX_API fx_status fx_string_iterator_erase(fx_string_iterator *it); +FX_API bool fx_string_iterator_is_valid(const fx_string_iterator *it); + +FX_API char *fx_strdup(const char *s); +FX_API size_t fx_strlen(const char *s, fx_strlen_flags flags); +FX_API fx_wchar *fx_wstrdup(const fx_wchar *s); +FX_API size_t fx_wstrlen(const fx_wchar *s); + +FX_API uint64_t fx_string_hash(const fx_string *s); + +FX_DECLS_END; + +#endif diff --git a/ds/include/fx/ds/tree.h b/ds/include/fx/ds/tree.h new file mode 100644 index 0000000..9767bff --- /dev/null +++ b/ds/include/fx/ds/tree.h @@ -0,0 +1,57 @@ +#ifndef FX_DS_TREE_H_ +#define FX_DS_TREE_H_ + +#include +#include +#include +#include + +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 fx_tree_get_type(void); +FX_API fx_type 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 diff --git a/ds/include/fx/ds/uuid.h b/ds/include/fx/ds/uuid.h new file mode 100644 index 0000000..326c55f --- /dev/null +++ b/ds/include/fx/ds/uuid.h @@ -0,0 +1,50 @@ +#ifndef FX_DS_UUID_H_ +#define FX_DS_UUID_H_ + +#include +#include +#include + +#define FX_UUID_NBYTES 16 +#define FX_UUID_STRING_MAX 37 + +FX_DECLS_BEGIN; + +#define FX_TYPE_UUID (fx_uuid_get_type()) + +FX_DECLARE_TYPE(fx_uuid); + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_uuid) +FX_TYPE_CLASS_DECLARATION_END(fx_uuid) + +typedef union fx_uuid_bytes { + uint8_t uuid_bytes[FX_UUID_NBYTES]; + uint16_t uuid_words[FX_UUID_NBYTES / 2]; + uint32_t uuid_dwords[FX_UUID_NBYTES / 4]; + uint64_t uuid_qwords[FX_UUID_NBYTES / 8]; +} fx_uuid_bytes; + +FX_API fx_type fx_uuid_get_type(void); + +FX_TYPE_DEFAULT_CONSTRUCTOR(fx_uuid, FX_TYPE_UUID); + +FX_API fx_uuid *fx_uuid_create_from_bytes( + unsigned char u00, unsigned char u01, unsigned char u02, + unsigned char u03, unsigned char u04, unsigned char u05, + unsigned char u06, unsigned char u07, unsigned char u08, + unsigned char u09, unsigned char u10, unsigned char u11, unsigned char u12, + unsigned char u13, unsigned char u14, unsigned char u15); +FX_API fx_uuid *fx_uuid_create_from_bytev(const unsigned char bytes[FX_UUID_NBYTES]); +FX_API fx_uuid *fx_uuid_create_from_uuid_bytes(const fx_uuid_bytes *bytes); +FX_API fx_uuid *fx_uuid_create_from_string(const fx_string *string); +FX_API fx_uuid *fx_uuid_create_from_cstr(const char *s); + +FX_API fx_status fx_uuid_to_cstr(const fx_uuid *uuid, char out[FX_UUID_STRING_MAX]); +FX_API void fx_uuid_get_bytes( + const fx_uuid *uuid, unsigned char bytes[FX_UUID_NBYTES]); +FX_API void fx_uuid_get_uuid_bytes(const fx_uuid *uuid, fx_uuid_bytes *bytes); +FX_API fx_uuid_bytes *fx_uuid_ptr(fx_uuid *uuid); + +FX_DECLS_END; + +#endif diff --git a/ds/list.c b/ds/list.c index 7ac1a2e..4059e4c 100644 --- a/ds/list.c +++ b/ds/list.c @@ -1,102 +1,102 @@ -#include -#include -#include +#include +#include +#include #include #include /*** PRIVATE DATA *************************************************************/ -struct b_list_entry { - struct b_queue_entry e_entry; +struct fx_list_entry { + struct fx_queue_entry e_entry; void *e_data; }; -struct b_list_p { - struct b_queue l_queue; +struct fx_list_p { + struct fx_queue l_queue; size_t l_len; }; -struct b_list_iterator_p { - b_list *_q; - struct b_list_p *_q_p; - struct b_queue_entry *_q_entry; +struct fx_list_iterator_p { + fx_list *_q; + struct fx_list_p *_q_p; + struct fx_queue_entry *_q_entry; size_t i; void *item; - b_list_entry *entry; + fx_list_entry *entry; }; /*** PRIVATE FUNCTIONS ********************************************************/ -static bool list_empty(struct b_list_p *q) +static bool list_empty(struct fx_list_p *q) { return q->l_len == 0; } -static void *list_first_item(const struct b_list_p *q) +static void *list_first_item(const struct fx_list_p *q) { - struct b_queue_entry *entry = b_queue_first(&q->l_queue); + struct fx_queue_entry *entry = fx_queue_first(&q->l_queue); if (!entry) { return NULL; } - struct b_list_entry *list_entry - = b_unbox(struct b_list_entry, entry, e_entry); + struct fx_list_entry *list_entry + = fx_unbox(struct fx_list_entry, entry, e_entry); return list_entry->e_data; } -static void *list_last_item(const struct b_list_p *q) +static void *list_last_item(const struct fx_list_p *q) { - struct b_queue_entry *entry = b_queue_last(&q->l_queue); + struct fx_queue_entry *entry = fx_queue_last(&q->l_queue); if (!entry) { return NULL; } - struct b_list_entry *list_entry - = b_unbox(struct b_list_entry, entry, e_entry); + struct fx_list_entry *list_entry + = fx_unbox(struct fx_list_entry, entry, e_entry); return list_entry->e_data; } -static struct b_list_entry *list_first_entry(const struct b_list_p *q) +static struct fx_list_entry *list_first_entry(const struct fx_list_p *q) { - struct b_queue_entry *entry = b_queue_first(&q->l_queue); + struct fx_queue_entry *entry = fx_queue_first(&q->l_queue); if (!entry) { return NULL; } - struct b_list_entry *list_entry - = b_unbox(struct b_list_entry, entry, e_entry); + struct fx_list_entry *list_entry + = fx_unbox(struct fx_list_entry, entry, e_entry); return list_entry; } -static struct b_list_entry *list_last_entry(const struct b_list_p *q) +static struct fx_list_entry *list_last_entry(const struct fx_list_p *q) { - struct b_queue_entry *entry = b_queue_last(&q->l_queue); + struct fx_queue_entry *entry = fx_queue_last(&q->l_queue); if (!entry) { return NULL; } - struct b_list_entry *list_entry - = b_unbox(struct b_list_entry, entry, e_entry); + struct fx_list_entry *list_entry + = fx_unbox(struct fx_list_entry, entry, e_entry); return list_entry; } -static size_t list_length(const struct b_list_p *q) +static size_t list_length(const struct fx_list_p *q) { return q->l_len; } -static struct b_list_entry *make_entry(void *item) +static struct fx_list_entry *make_entry(void *item) { - struct b_list_entry *entry = malloc(sizeof *entry); + struct fx_list_entry *entry = malloc(sizeof *entry); if (!entry) { return NULL; } @@ -108,65 +108,65 @@ static struct b_list_entry *make_entry(void *item) return entry; } -static struct b_list_entry *list_insert_before( - struct b_list_p *q, void *ptr, struct b_list_entry *before) +static struct fx_list_entry *list_insert_before( + struct fx_list_p *q, void *ptr, struct fx_list_entry *before) { - struct b_list_entry *entry = make_entry(ptr); + struct fx_list_entry *entry = make_entry(ptr); if (!entry) { return NULL; } - b_queue_insert_before(&q->l_queue, &entry->e_entry, &before->e_entry); + fx_queue_insert_before(&q->l_queue, &entry->e_entry, &before->e_entry); q->l_len++; return entry; } -static struct b_list_entry *list_insert_after( - struct b_list_p *q, void *ptr, struct b_list_entry *after) +static struct fx_list_entry *list_insert_after( + struct fx_list_p *q, void *ptr, struct fx_list_entry *after) { - struct b_list_entry *entry = make_entry(ptr); + struct fx_list_entry *entry = make_entry(ptr); if (!entry) { return NULL; } - b_queue_insert_after(&q->l_queue, &entry->e_entry, &after->e_entry); + fx_queue_insert_after(&q->l_queue, &entry->e_entry, &after->e_entry); q->l_len++; return entry; } -static struct b_list_entry *list_push_front(struct b_list_p *q, void *ptr) +static struct fx_list_entry *list_push_front(struct fx_list_p *q, void *ptr) { - struct b_list_entry *entry = make_entry(ptr); + struct fx_list_entry *entry = make_entry(ptr); if (!entry) { return NULL; } - b_queue_push_front(&q->l_queue, &entry->e_entry); + fx_queue_push_front(&q->l_queue, &entry->e_entry); q->l_len++; return entry; } -static struct b_list_entry *list_push_back(struct b_list_p *q, void *ptr) +static struct fx_list_entry *list_push_back(struct fx_list_p *q, void *ptr) { - struct b_list_entry *entry = make_entry(ptr); + struct fx_list_entry *entry = make_entry(ptr); if (!entry) { return NULL; } - b_queue_push_back(&q->l_queue, &entry->e_entry); + fx_queue_push_back(&q->l_queue, &entry->e_entry); q->l_len++; return entry; } -static void *list_pop_front(struct b_list_p *q) +static void *list_pop_front(struct fx_list_p *q) { - struct b_queue_entry *entry = b_queue_pop_front(&q->l_queue); + struct fx_queue_entry *entry = fx_queue_pop_front(&q->l_queue); if (!entry) { return NULL; } - struct b_list_entry *list_entry - = b_unbox(struct b_list_entry, entry, e_entry); + struct fx_list_entry *list_entry + = fx_unbox(struct fx_list_entry, entry, e_entry); void *item = list_entry->e_data; free(list_entry); @@ -176,15 +176,15 @@ static void *list_pop_front(struct b_list_p *q) return item; } -static void *list_pop_back(struct b_list_p *q) +static void *list_pop_back(struct fx_list_p *q) { - struct b_queue_entry *entry = b_queue_pop_back(&q->l_queue); + struct fx_queue_entry *entry = fx_queue_pop_back(&q->l_queue); if (!entry) { return NULL; } - struct b_list_entry *list_entry - = b_unbox(struct b_list_entry, entry, e_entry); + struct fx_list_entry *list_entry + = fx_unbox(struct fx_list_entry, entry, e_entry); void *item = list_entry->e_data; free(list_entry); @@ -194,55 +194,55 @@ static void *list_pop_back(struct b_list_p *q) return item; } -static struct b_list_entry *find_item(struct b_list_p *list, void *item) +static struct fx_list_entry *find_item(struct fx_list_p *list, void *item) { - struct b_queue_entry *entry = b_queue_first(&list->l_queue); + struct fx_queue_entry *entry = fx_queue_first(&list->l_queue); while (entry) { - struct b_list_entry *list_entry - = b_unbox(struct b_list_entry, entry, e_entry); + struct fx_list_entry *list_entry + = fx_unbox(struct fx_list_entry, entry, e_entry); if (list_entry->e_data == item) { return list_entry; } - entry = b_queue_next(entry); + entry = fx_queue_next(entry); } return NULL; } -static b_status list_delete_item(struct b_list_p *q, void *ptr) +static fx_status list_delete_item(struct fx_list_p *q, void *ptr) { - struct b_list_entry *entry = find_item(q, ptr); + struct fx_list_entry *entry = find_item(q, ptr); if (!entry) { - return B_ERR_NO_ENTRY; + return FX_ERR_NO_ENTRY; } - b_queue_delete(&q->l_queue, &entry->e_entry); + fx_queue_delete(&q->l_queue, &entry->e_entry); q->l_len--; free(entry); - return B_SUCCESS; + return FX_SUCCESS; } -static b_status list_delete_entry(struct b_list_p *q, struct b_list_entry *entry) +static fx_status list_delete_entry(struct fx_list_p *q, struct fx_list_entry *entry) { - b_queue_delete(&q->l_queue, &entry->e_entry); + fx_queue_delete(&q->l_queue, &entry->e_entry); q->l_len--; free(entry); - return B_SUCCESS; + return FX_SUCCESS; } -static void list_delete_all(struct b_list_p *q) +static void list_delete_all(struct fx_list_p *q) { - struct b_queue_entry *entry = b_queue_first(&q->l_queue); + struct fx_queue_entry *entry = fx_queue_first(&q->l_queue); while (entry) { - struct b_list_entry *list_entry - = b_unbox(struct b_list_entry, entry, e_entry); - struct b_queue_entry *next = b_queue_next(entry); + struct fx_list_entry *list_entry + = fx_unbox(struct fx_list_entry, entry, e_entry); + struct fx_queue_entry *next = fx_queue_next(entry); free(list_entry); entry = next; @@ -253,130 +253,130 @@ static void list_delete_all(struct b_list_p *q) /*** PUBLIC FUNCTIONS *********************************************************/ -bool b_list_empty(b_list *q) +bool fx_list_empty(fx_list *q) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_LIST, list_empty, q); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_LIST, list_empty, q); } -void *b_list_first_item(const b_list *q) +void *fx_list_first_item(const fx_list *q) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_LIST, list_first_item, q); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_LIST, list_first_item, q); } -void *b_list_last_item(const b_list *q) +void *fx_list_last_item(const fx_list *q) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_LIST, list_last_item, q); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_LIST, list_last_item, q); } -struct b_list_entry *b_list_first_entry(const b_list *q) +struct fx_list_entry *fx_list_first_entry(const fx_list *q) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_LIST, list_first_entry, q); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_LIST, list_first_entry, q); } -struct b_list_entry *b_list_last_entry(const b_list *q) +struct fx_list_entry *fx_list_last_entry(const fx_list *q) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_LIST, b_list_last_entry, q); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_LIST, fx_list_last_entry, q); } -struct b_list_entry *b_list_next(const struct b_list_entry *entry) +struct fx_list_entry *fx_list_next(const struct fx_list_entry *entry) { if (!entry) { return NULL; } - struct b_queue_entry *next = b_queue_next(&entry->e_entry); + struct fx_queue_entry *next = fx_queue_next(&entry->e_entry); if (!next) { return NULL; } - return b_unbox(struct b_list_entry, next, e_entry); + return fx_unbox(struct fx_list_entry, next, e_entry); } -struct b_list_entry *b_list_prev(const struct b_list_entry *entry) +struct fx_list_entry *fx_list_prev(const struct fx_list_entry *entry) { if (!entry) { return NULL; } - struct b_queue_entry *next = b_queue_prev(&entry->e_entry); + struct fx_queue_entry *next = fx_queue_prev(&entry->e_entry); if (!next) { return NULL; } - return b_unbox(struct b_list_entry, next, e_entry); + return fx_unbox(struct fx_list_entry, next, e_entry); } -size_t b_list_length(const b_list *q) +size_t fx_list_length(const fx_list *q) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_LIST, list_length, q); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_LIST, list_length, q); } -struct b_list_entry *b_list_insert_before( - b_list *q, void *ptr, struct b_list_entry *before) +struct fx_list_entry *fx_list_insert_before( + fx_list *q, void *ptr, struct fx_list_entry *before) { - B_CLASS_DISPATCH_STATIC(B_TYPE_LIST, list_insert_before, q, ptr, before); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_LIST, list_insert_before, q, ptr, before); } -struct b_list_entry *b_list_insert_after( - b_list *q, void *ptr, struct b_list_entry *after) +struct fx_list_entry *fx_list_insert_after( + fx_list *q, void *ptr, struct fx_list_entry *after) { - B_CLASS_DISPATCH_STATIC(B_TYPE_LIST, list_insert_after, q, ptr, after); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_LIST, list_insert_after, q, ptr, after); } -struct b_list_entry *b_list_push_front(b_list *q, void *ptr) +struct fx_list_entry *fx_list_push_front(fx_list *q, void *ptr) { - B_CLASS_DISPATCH_STATIC(B_TYPE_LIST, list_push_front, q, ptr); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_LIST, list_push_front, q, ptr); } -struct b_list_entry *b_list_push_back(b_list *q, void *ptr) +struct fx_list_entry *fx_list_push_back(fx_list *q, void *ptr) { - B_CLASS_DISPATCH_STATIC(B_TYPE_LIST, list_push_back, q, ptr); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_LIST, list_push_back, q, ptr); } -void *b_list_pop_front(b_list *q) +void *fx_list_pop_front(fx_list *q) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_LIST, list_pop_front, q); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_LIST, list_pop_front, q); } -void *b_list_pop_back(b_list *q) +void *fx_list_pop_back(fx_list *q) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_LIST, list_pop_back, q); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_LIST, list_pop_back, q); } -b_status b_list_delete_item(b_list *q, void *ptr) +fx_status fx_list_delete_item(fx_list *q, void *ptr) { - B_CLASS_DISPATCH_STATIC(B_TYPE_LIST, list_delete_item, q, ptr); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_LIST, list_delete_item, q, ptr); } -b_status b_list_delete_entry(b_list *q, struct b_list_entry *entry) +fx_status fx_list_delete_entry(fx_list *q, struct fx_list_entry *entry) { - B_CLASS_DISPATCH_STATIC(B_TYPE_LIST, list_delete_entry, q, entry); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_LIST, list_delete_entry, q, entry); } -void b_list_delete_all(b_list *q) +void fx_list_delete_all(fx_list *q) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_LIST, list_delete_all, q); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_LIST, list_delete_all, q); } -void *b_list_entry_value(const struct b_list_entry *entry) +void *fx_list_entry_value(const struct fx_list_entry *entry) { return entry ? entry->e_data : NULL; } -b_iterator *b_list_begin(b_list *q) +fx_iterator *fx_list_begin(fx_list *q) { - b_list_iterator *it_obj = b_object_create(B_TYPE_LIST_ITERATOR); - struct b_list_iterator_p *it - = b_object_get_private(it_obj, B_TYPE_LIST_ITERATOR); + fx_list_iterator *it_obj = fx_object_create(FX_TYPE_LIST_ITERATOR); + struct fx_list_iterator_p *it + = fx_object_get_private(it_obj, FX_TYPE_LIST_ITERATOR); it->_q = q; - it->_q_p = b_object_get_private(q, B_TYPE_LIST); - it->_q_entry = b_queue_first(&it->_q_p->l_queue); + 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 = b_unbox(struct b_list_entry, it->_q_entry, e_entry); + it->entry = fx_unbox(struct fx_list_entry, it->_q_entry, e_entry); if (it->entry) { it->item = it->entry->e_data; } @@ -384,18 +384,18 @@ b_iterator *b_list_begin(b_list *q) return 0; } -const b_iterator *b_list_cbegin(const b_list *q) +const fx_iterator *fx_list_cbegin(const fx_list *q) { - b_list_iterator *it_obj = b_object_create(B_TYPE_LIST_ITERATOR); - struct b_list_iterator_p *it - = b_object_get_private(it_obj, B_TYPE_LIST_ITERATOR); + fx_list_iterator *it_obj = fx_object_create(FX_TYPE_LIST_ITERATOR); + struct fx_list_iterator_p *it + = fx_object_get_private(it_obj, FX_TYPE_LIST_ITERATOR); - it->_q = (b_list *)q; - it->_q_p = b_object_get_private(q, B_TYPE_LIST); - it->_q_entry = b_queue_first(&it->_q_p->l_queue); + it->_q = (fx_list *)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 = b_unbox(struct b_list_entry, it->_q_entry, e_entry); + it->entry = fx_unbox(struct fx_list_entry, it->_q_entry, e_entry); if (it->entry) { it->item = it->entry->e_data; } @@ -405,39 +405,39 @@ const b_iterator *b_list_cbegin(const b_list *q) /*** VIRTUAL FUNCTIONS ********************************************************/ -static void list_init(b_object *obj, void *priv) +static void list_init(fx_object *obj, void *priv) { - struct b_list_p *list = priv; + struct fx_list_p *list = priv; } -static void list_fini(b_object *obj, void *priv) +static void list_fini(fx_object *obj, void *priv) { - struct b_list_p *list = priv; + struct fx_list_p *list = priv; list_delete_all(list); } /*** ITERATOR FUNCTIONS *******************************************************/ -static enum b_status iterator_move_next(const b_iterator *obj) +static enum fx_status iterator_move_next(const fx_iterator *obj) { - struct b_list_iterator_p *it - = b_object_get_private(obj, B_TYPE_LIST_ITERATOR); + struct fx_list_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_LIST_ITERATOR); if (!it->_q_entry) { it->entry = NULL; it->item = NULL; - return B_ERR_NO_DATA; + return FX_ERR_NO_DATA; } - it->_q_entry = b_queue_next(it->_q_entry); + it->_q_entry = fx_queue_next(it->_q_entry); if (!it->_q_entry) { it->entry = NULL; it->item = NULL; - return B_ERR_NO_DATA; + return FX_ERR_NO_DATA; } it->i++; - it->entry = b_unbox(struct b_list_entry, it->_q_entry, e_entry); + it->entry = fx_unbox(struct fx_list_entry, it->_q_entry, e_entry); if (it->entry) { it->item = it->entry->e_data; } @@ -445,85 +445,85 @@ static enum b_status iterator_move_next(const b_iterator *obj) return it->entry != NULL; } -static enum b_status iterator_erase(b_iterator *obj) +static enum fx_status iterator_erase(fx_iterator *obj) { - struct b_list_iterator_p *it - = b_object_get_private(obj, B_TYPE_LIST_ITERATOR); + struct fx_list_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_LIST_ITERATOR); if (!it->entry || !it->_q_entry) { - return B_ERR_OUT_OF_BOUNDS; + return FX_ERR_OUT_OF_BOUNDS; } - struct b_queue_entry *next = b_queue_next(it->_q_entry); + struct fx_queue_entry *next = fx_queue_next(it->_q_entry); - b_queue_delete(&it->_q_p->l_queue, it->_q_entry); + fx_queue_delete(&it->_q_p->l_queue, it->_q_entry); free(it->entry); it->_q_entry = next; - it->entry = b_unbox(struct b_list_entry, it->_q_entry, e_entry); + it->entry = fx_unbox(struct fx_list_entry, it->_q_entry, e_entry); if (it->entry) { it->item = it->entry->e_data; } - return B_SUCCESS; + return FX_SUCCESS; } -static b_iterator_value iterator_get_value(b_iterator *obj) +static fx_iterator_value iterator_get_value(fx_iterator *obj) { - struct b_list_iterator_p *it - = b_object_get_private(obj, B_TYPE_LIST_ITERATOR); + struct fx_list_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_LIST_ITERATOR); - return B_ITERATOR_VALUE_PTR(it->item); + return FX_ITERATOR_VALUE_PTR(it->item); } -static const b_iterator_value iterator_get_cvalue(const b_iterator *obj) +static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj) { - struct b_list_iterator_p *it - = b_object_get_private(obj, B_TYPE_LIST_ITERATOR); + struct fx_list_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_LIST_ITERATOR); - return B_ITERATOR_VALUE_CPTR(it->item); + return FX_ITERATOR_VALUE_CPTR(it->item); } /*** CLASS DEFINITION *********************************************************/ -// ---- b_list DEFINITION -B_TYPE_CLASS_DEFINITION_BEGIN(b_list) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) +// ---- fx_list DEFINITION +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_list) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) - B_TYPE_CLASS_INTERFACE_BEGIN(b_iterable, B_TYPE_ITERABLE) - B_INTERFACE_ENTRY(it_begin) = b_list_begin; - B_INTERFACE_ENTRY(it_cbegin) = b_list_cbegin; - B_TYPE_CLASS_INTERFACE_END(b_iterable, B_TYPE_ITERABLE) -B_TYPE_CLASS_DEFINITION_END(b_list) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE) + FX_INTERFACE_ENTRY(it_begin) = fx_list_begin; + FX_INTERFACE_ENTRY(it_cbegin) = fx_list_cbegin; + FX_TYPE_CLASS_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE) +FX_TYPE_CLASS_DEFINITION_END(fx_list) -B_TYPE_DEFINITION_BEGIN(b_list) - B_TYPE_ID(0x8730e66f, 0x0fd9, 0x4773, 0x9bbd, 0x6428f6e495eb); - B_TYPE_CLASS(b_list_class); - B_TYPE_IMPLEMENTS(B_TYPE_ITERABLE); - B_TYPE_INSTANCE_PRIVATE(struct b_list_p); - B_TYPE_INSTANCE_INIT(list_init); - B_TYPE_INSTANCE_FINI(list_fini); -B_TYPE_DEFINITION_END(b_list) +FX_TYPE_DEFINITION_BEGIN(fx_list) + FX_TYPE_ID(0x8730e66f, 0x0fd9, 0x4773, 0x9bbd, 0x6428f6e495eb); + FX_TYPE_CLASS(fx_list_class); + FX_TYPE_IMPLEMENTS(FX_TYPE_ITERABLE); + FX_TYPE_INSTANCE_PRIVATE(struct fx_list_p); + FX_TYPE_INSTANCE_INIT(list_init); + FX_TYPE_INSTANCE_FINI(list_fini); +FX_TYPE_DEFINITION_END(fx_list) -// ---- b_list_iterator DEFINITION -B_TYPE_CLASS_DEFINITION_BEGIN(b_list_iterator) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) +// ---- fx_list_iterator DEFINITION +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_list_iterator) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) - B_TYPE_CLASS_INTERFACE_BEGIN(b_iterator, B_TYPE_ITERATOR) - B_INTERFACE_ENTRY(it_move_next) = iterator_move_next; - B_INTERFACE_ENTRY(it_erase) = iterator_erase; - B_INTERFACE_ENTRY(it_get_value) = iterator_get_value; - B_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue; - B_TYPE_CLASS_INTERFACE_END(b_iterator, B_TYPE_ITERATOR) -B_TYPE_CLASS_DEFINITION_END(b_list_iterator) + FX_TYPE_CLASS_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_CLASS_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR) +FX_TYPE_CLASS_DEFINITION_END(fx_list_iterator) -B_TYPE_DEFINITION_BEGIN(b_list_iterator) - B_TYPE_ID(0xd9658456, 0xdd80, 0x419a, 0xb23a, 0xb513013e6431); - B_TYPE_EXTENDS(B_TYPE_ITERATOR); - B_TYPE_CLASS(b_list_iterator_class); - B_TYPE_INSTANCE_PRIVATE(struct b_list_iterator_p); -B_TYPE_DEFINITION_END(b_list_iterator) +FX_TYPE_DEFINITION_BEGIN(fx_list_iterator) + FX_TYPE_ID(0xd9658456, 0xdd80, 0x419a, 0xb23a, 0xb513013e6431); + FX_TYPE_EXTENDS(FX_TYPE_ITERATOR); + FX_TYPE_CLASS(fx_list_iterator_class); + FX_TYPE_INSTANCE_PRIVATE(struct fx_list_iterator_p); +FX_TYPE_DEFINITION_END(fx_list_iterator) diff --git a/ds/number.c b/ds/number.c index 459e9df..d3f9172 100644 --- a/ds/number.c +++ b/ds/number.c @@ -1,19 +1,19 @@ -#include -#include +#include +#include #include #include #include /*** PRIVATE DATA *************************************************************/ -enum b_number_flags { +enum fx_number_flags { NUMBER_F_INF = 0x01u, NUMBER_F_NAN = 0x02u, }; -struct b_number_p { - b_number_type n_type; - enum b_number_flags n_flags; +struct fx_number_p { + fx_number_type n_type; + enum fx_number_flags n_flags; union { int8_t v_int8; int16_t v_int16; @@ -32,22 +32,22 @@ struct b_number_p { } n_value; }; -typedef int (*number_converter_t)(const struct b_number_p *, void *); +typedef int (*number_converter_t)(const struct fx_number_p *, void *); -static number_converter_t converters[B_NUMBER_TYPE_COUNT][B_NUMBER_TYPE_COUNT]; +static number_converter_t converters[FX_NUMBER_TYPE_COUNT][FX_NUMBER_TYPE_COUNT]; /*** PRIVATE FUNCTIONS ********************************************************/ -static b_number_type number_get_number_type(const struct b_number_p *number) +static fx_number_type number_get_number_type(const struct fx_number_p *number) { return number->n_type; } static int number_get_value( - const struct b_number_p *number, b_number_type type, void *value_ptr) + const struct fx_number_p *number, fx_number_type type, void *value_ptr) { - b_number_type srb_type = number->n_type; - b_number_type dest_type = type; + fx_number_type srb_type = number->n_type; + fx_number_type dest_type = type; number_converter_t converter = converters[srb_type][dest_type]; if (!converter) { @@ -57,210 +57,210 @@ static int number_get_value( return converter(number, value_ptr); } -static bool number_is_integer(const struct b_number_p *number) +static bool number_is_integer(const struct fx_number_p *number) { switch (number->n_type) { - case B_NUMBER_INT8: - case B_NUMBER_INT16: - case B_NUMBER_INT32: - case B_NUMBER_INT64: - case B_NUMBER_SIZE_T: - case B_NUMBER_CHAR: - case B_NUMBER_SHORT: - case B_NUMBER_INT: - case B_NUMBER_LONG: - case B_NUMBER_LONGLONG: - case B_NUMBER_HANDLE: + case FX_NUMBER_INT8: + case FX_NUMBER_INT16: + case FX_NUMBER_INT32: + case FX_NUMBER_INT64: + case FX_NUMBER_SIZE_T: + case FX_NUMBER_CHAR: + case FX_NUMBER_SHORT: + case FX_NUMBER_INT: + case FX_NUMBER_LONG: + case FX_NUMBER_LONGLONG: + case FX_NUMBER_HANDLE: return true; default: return false; } } -static bool number_is_float(const struct b_number_p *number) +static bool number_is_float(const struct fx_number_p *number) { switch (number->n_type) { - case B_NUMBER_FLOAT: - case B_NUMBER_DOUBLE: - case B_NUMBER_FLOAT32: - case B_NUMBER_FLOAT64: + case FX_NUMBER_FLOAT: + case FX_NUMBER_DOUBLE: + case FX_NUMBER_FLOAT32: + case FX_NUMBER_FLOAT64: return true; default: return false; } } -static bool number_is_inf(const struct b_number_p *number) +static bool number_is_inf(const struct fx_number_p *number) { return (number->n_flags & NUMBER_F_INF) != 0; } -static bool number_is_inf_positive(const struct b_number_p *number) +static bool number_is_inf_positive(const struct fx_number_p *number) { if (!(number->n_flags & NUMBER_F_INF)) { return false; } switch (number->n_type) { - case B_NUMBER_INT8: + case FX_NUMBER_INT8: return number->n_value.v_int8 >= 0; - case B_NUMBER_INT16: + case FX_NUMBER_INT16: return number->n_value.v_int16 >= 0; - case B_NUMBER_INT32: + case FX_NUMBER_INT32: return number->n_value.v_int32 >= 0; - case B_NUMBER_INT64: + case FX_NUMBER_INT64: return number->n_value.v_int64 >= 0; - case B_NUMBER_FLOAT32: + case FX_NUMBER_FLOAT32: return number->n_value.v_float32 >= 0; - case B_NUMBER_FLOAT64: + case FX_NUMBER_FLOAT64: return number->n_value.v_float64 >= 0; - case B_NUMBER_CHAR: + case FX_NUMBER_CHAR: return number->n_value.v_char >= 0; - case B_NUMBER_SHORT: + case FX_NUMBER_SHORT: return number->n_value.v_short >= 0; - case B_NUMBER_INT: + case FX_NUMBER_INT: return number->n_value.v_int >= 0; - case B_NUMBER_LONG: + case FX_NUMBER_LONG: return number->n_value.v_long >= 0; - case B_NUMBER_LONGLONG: + case FX_NUMBER_LONGLONG: return number->n_value.v_longlong >= 0; - case B_NUMBER_FLOAT: + case FX_NUMBER_FLOAT: return number->n_value.v_float >= 0; - case B_NUMBER_DOUBLE: + case FX_NUMBER_DOUBLE: return number->n_value.v_double >= 0; - case B_NUMBER_SIZE_T: + case FX_NUMBER_SIZE_T: return number->n_value.v_size_t >= 0; default: return true; } } -static bool number_is_inf_negative(const struct b_number_p *number) +static bool number_is_inf_negative(const struct fx_number_p *number) { if (!(number->n_flags & NUMBER_F_INF)) { return false; } switch (number->n_type) { - case B_NUMBER_INT8: + case FX_NUMBER_INT8: return number->n_value.v_int8 < 0; - case B_NUMBER_INT16: + case FX_NUMBER_INT16: return number->n_value.v_int16 < 0; - case B_NUMBER_INT32: + case FX_NUMBER_INT32: return number->n_value.v_int32 < 0; - case B_NUMBER_INT64: + case FX_NUMBER_INT64: return number->n_value.v_int64 < 0; - case B_NUMBER_FLOAT32: + case FX_NUMBER_FLOAT32: return number->n_value.v_float32 < 0; - case B_NUMBER_FLOAT64: + case FX_NUMBER_FLOAT64: return number->n_value.v_float64 < 0; - case B_NUMBER_CHAR: + case FX_NUMBER_CHAR: return number->n_value.v_char < 0; - case B_NUMBER_SHORT: + case FX_NUMBER_SHORT: return number->n_value.v_short < 0; - case B_NUMBER_INT: + case FX_NUMBER_INT: return number->n_value.v_int < 0; - case B_NUMBER_LONG: + case FX_NUMBER_LONG: return number->n_value.v_long < 0; - case B_NUMBER_LONGLONG: + case FX_NUMBER_LONGLONG: return number->n_value.v_longlong < 0; - case B_NUMBER_FLOAT: + case FX_NUMBER_FLOAT: return number->n_value.v_float < 0; - case B_NUMBER_DOUBLE: + case FX_NUMBER_DOUBLE: return number->n_value.v_double < 0; - case B_NUMBER_SIZE_T: + case FX_NUMBER_SIZE_T: return number->n_value.v_size_t < 0; default: return false; } } -static bool number_is_nan(const struct b_number_p *number) +static bool number_is_nan(const struct fx_number_p *number) { return (number->n_flags & NUMBER_F_NAN) != 0; } -static bool number_is_nan_positive(const struct b_number_p *number) +static bool number_is_nan_positive(const struct fx_number_p *number) { if (!(number->n_flags & NUMBER_F_NAN)) { return false; } switch (number->n_type) { - case B_NUMBER_INT8: + case FX_NUMBER_INT8: return number->n_value.v_int8 >= 0; - case B_NUMBER_INT16: + case FX_NUMBER_INT16: return number->n_value.v_int16 >= 0; - case B_NUMBER_INT32: + case FX_NUMBER_INT32: return number->n_value.v_int32 >= 0; - case B_NUMBER_INT64: + case FX_NUMBER_INT64: return number->n_value.v_int64 >= 0; - case B_NUMBER_FLOAT32: + case FX_NUMBER_FLOAT32: return number->n_value.v_float32 >= 0; - case B_NUMBER_FLOAT64: + case FX_NUMBER_FLOAT64: return number->n_value.v_float64 >= 0; - case B_NUMBER_CHAR: + case FX_NUMBER_CHAR: return number->n_value.v_char >= 0; - case B_NUMBER_SHORT: + case FX_NUMBER_SHORT: return number->n_value.v_short >= 0; - case B_NUMBER_INT: + case FX_NUMBER_INT: return number->n_value.v_int >= 0; - case B_NUMBER_LONG: + case FX_NUMBER_LONG: return number->n_value.v_long >= 0; - case B_NUMBER_LONGLONG: + case FX_NUMBER_LONGLONG: return number->n_value.v_longlong >= 0; - case B_NUMBER_FLOAT: + case FX_NUMBER_FLOAT: return number->n_value.v_float >= 0; - case B_NUMBER_DOUBLE: + case FX_NUMBER_DOUBLE: return number->n_value.v_double >= 0; - case B_NUMBER_SIZE_T: + case FX_NUMBER_SIZE_T: return number->n_value.v_size_t >= 0; default: return true; } } -static bool number_is_nan_negative(const struct b_number_p *number) +static bool number_is_nan_negative(const struct fx_number_p *number) { if (!(number->n_flags & NUMBER_F_NAN)) { return false; } switch (number->n_type) { - case B_NUMBER_INT8: + case FX_NUMBER_INT8: return number->n_value.v_int8 < 0; - case B_NUMBER_INT16: + case FX_NUMBER_INT16: return number->n_value.v_int16 < 0; - case B_NUMBER_INT32: + case FX_NUMBER_INT32: return number->n_value.v_int32 < 0; - case B_NUMBER_INT64: + case FX_NUMBER_INT64: return number->n_value.v_int64 < 0; - case B_NUMBER_FLOAT32: + case FX_NUMBER_FLOAT32: return number->n_value.v_float32 < 0; - case B_NUMBER_FLOAT64: + case FX_NUMBER_FLOAT64: return number->n_value.v_float64 < 0; - case B_NUMBER_CHAR: + case FX_NUMBER_CHAR: return number->n_value.v_char < 0; - case B_NUMBER_SHORT: + case FX_NUMBER_SHORT: return number->n_value.v_short < 0; - case B_NUMBER_INT: + case FX_NUMBER_INT: return number->n_value.v_int < 0; - case B_NUMBER_LONG: + case FX_NUMBER_LONG: return number->n_value.v_long < 0; - case B_NUMBER_LONGLONG: + case FX_NUMBER_LONGLONG: return number->n_value.v_longlong < 0; - case B_NUMBER_FLOAT: + case FX_NUMBER_FLOAT: return number->n_value.v_float < 0; - case B_NUMBER_DOUBLE: + case FX_NUMBER_DOUBLE: return number->n_value.v_double < 0; - case B_NUMBER_SIZE_T: + case FX_NUMBER_SIZE_T: return number->n_value.v_size_t < 0; default: return false; } } -static void number_set_inf_positive(struct b_number_p *number, bool v) +static void number_set_inf_positive(struct fx_number_p *number, bool v) { if (!v) { number->n_flags &= ~NUMBER_F_INF; @@ -271,46 +271,46 @@ static void number_set_inf_positive(struct b_number_p *number, bool v) number->n_flags |= NUMBER_F_INF; switch (number->n_type) { - case B_NUMBER_INT8: + case FX_NUMBER_INT8: number->n_value.v_int8 = 0; break; - case B_NUMBER_INT16: + case FX_NUMBER_INT16: number->n_value.v_int16 = 0; break; - case B_NUMBER_INT32: + case FX_NUMBER_INT32: number->n_value.v_int32 = 0; break; - case B_NUMBER_INT64: + case FX_NUMBER_INT64: number->n_value.v_int64 = 0; break; - case B_NUMBER_FLOAT32: + case FX_NUMBER_FLOAT32: number->n_value.v_float32 = 0; break; - case B_NUMBER_FLOAT64: + case FX_NUMBER_FLOAT64: number->n_value.v_float64 = 0; break; - case B_NUMBER_CHAR: + case FX_NUMBER_CHAR: number->n_value.v_char = 0; break; - case B_NUMBER_SHORT: + case FX_NUMBER_SHORT: number->n_value.v_short = 0; break; - case B_NUMBER_INT: + case FX_NUMBER_INT: number->n_value.v_int = 0; break; - case B_NUMBER_LONG: + case FX_NUMBER_LONG: number->n_value.v_long = 0; break; - case B_NUMBER_LONGLONG: + case FX_NUMBER_LONGLONG: number->n_value.v_longlong = 0; break; - case B_NUMBER_FLOAT: + case FX_NUMBER_FLOAT: number->n_value.v_float = 0; break; - case B_NUMBER_DOUBLE: + case FX_NUMBER_DOUBLE: number->n_value.v_double = 0; break; - case B_NUMBER_SIZE_T: + case FX_NUMBER_SIZE_T: number->n_value.v_size_t = 0; break; default: @@ -318,7 +318,7 @@ static void number_set_inf_positive(struct b_number_p *number, bool v) } } -static void number_set_inf_negative(struct b_number_p *number, bool v) +static void number_set_inf_negative(struct fx_number_p *number, bool v) { if (!v) { number->n_flags &= ~NUMBER_F_INF; @@ -329,46 +329,46 @@ static void number_set_inf_negative(struct b_number_p *number, bool v) number->n_flags |= NUMBER_F_INF; switch (number->n_type) { - case B_NUMBER_INT8: + case FX_NUMBER_INT8: number->n_value.v_int8 = -1; break; - case B_NUMBER_INT16: + case FX_NUMBER_INT16: number->n_value.v_int16 = -1; break; - case B_NUMBER_INT32: + case FX_NUMBER_INT32: number->n_value.v_int32 = -1; break; - case B_NUMBER_INT64: + case FX_NUMBER_INT64: number->n_value.v_int64 = -1; break; - case B_NUMBER_FLOAT32: + case FX_NUMBER_FLOAT32: number->n_value.v_float32 = -1; break; - case B_NUMBER_FLOAT64: + case FX_NUMBER_FLOAT64: number->n_value.v_float64 = -1; break; - case B_NUMBER_CHAR: + case FX_NUMBER_CHAR: number->n_value.v_char = -1; break; - case B_NUMBER_SHORT: + case FX_NUMBER_SHORT: number->n_value.v_short = -1; break; - case B_NUMBER_INT: + case FX_NUMBER_INT: number->n_value.v_int = -1; break; - case B_NUMBER_LONG: + case FX_NUMBER_LONG: number->n_value.v_long = -1; break; - case B_NUMBER_LONGLONG: + case FX_NUMBER_LONGLONG: number->n_value.v_longlong = -1; break; - case B_NUMBER_FLOAT: + case FX_NUMBER_FLOAT: number->n_value.v_float = -1; break; - case B_NUMBER_DOUBLE: + case FX_NUMBER_DOUBLE: number->n_value.v_double = -1; break; - case B_NUMBER_SIZE_T: + case FX_NUMBER_SIZE_T: number->n_value.v_size_t = -1; break; default: @@ -376,7 +376,7 @@ static void number_set_inf_negative(struct b_number_p *number, bool v) } } -static void number_set_nan_positive(struct b_number_p *number, bool v) +static void number_set_nan_positive(struct fx_number_p *number, bool v) { if (!v) { number->n_flags &= ~NUMBER_F_NAN; @@ -387,46 +387,46 @@ static void number_set_nan_positive(struct b_number_p *number, bool v) number->n_flags |= NUMBER_F_NAN; switch (number->n_type) { - case B_NUMBER_INT8: + case FX_NUMBER_INT8: number->n_value.v_int8 = 0; break; - case B_NUMBER_INT16: + case FX_NUMBER_INT16: number->n_value.v_int16 = 0; break; - case B_NUMBER_INT32: + case FX_NUMBER_INT32: number->n_value.v_int32 = 0; break; - case B_NUMBER_INT64: + case FX_NUMBER_INT64: number->n_value.v_int64 = 0; break; - case B_NUMBER_FLOAT32: + case FX_NUMBER_FLOAT32: number->n_value.v_float32 = 0; break; - case B_NUMBER_FLOAT64: + case FX_NUMBER_FLOAT64: number->n_value.v_float64 = 0; break; - case B_NUMBER_CHAR: + case FX_NUMBER_CHAR: number->n_value.v_char = 0; break; - case B_NUMBER_SHORT: + case FX_NUMBER_SHORT: number->n_value.v_short = 0; break; - case B_NUMBER_INT: + case FX_NUMBER_INT: number->n_value.v_int = 0; break; - case B_NUMBER_LONG: + case FX_NUMBER_LONG: number->n_value.v_long = 0; break; - case B_NUMBER_LONGLONG: + case FX_NUMBER_LONGLONG: number->n_value.v_longlong = 0; break; - case B_NUMBER_FLOAT: + case FX_NUMBER_FLOAT: number->n_value.v_float = 0; break; - case B_NUMBER_DOUBLE: + case FX_NUMBER_DOUBLE: number->n_value.v_double = 0; break; - case B_NUMBER_SIZE_T: + case FX_NUMBER_SIZE_T: number->n_value.v_size_t = 0; break; default: @@ -434,7 +434,7 @@ static void number_set_nan_positive(struct b_number_p *number, bool v) } } -static void number_set_nan_negative(struct b_number_p *number, bool v) +static void number_set_nan_negative(struct fx_number_p *number, bool v) { if (!v) { number->n_flags &= ~NUMBER_F_NAN; @@ -445,46 +445,46 @@ static void number_set_nan_negative(struct b_number_p *number, bool v) number->n_flags |= NUMBER_F_NAN; switch (number->n_type) { - case B_NUMBER_INT8: + case FX_NUMBER_INT8: number->n_value.v_int8 = -1; break; - case B_NUMBER_INT16: + case FX_NUMBER_INT16: number->n_value.v_int16 = -1; break; - case B_NUMBER_INT32: + case FX_NUMBER_INT32: number->n_value.v_int32 = -1; break; - case B_NUMBER_INT64: + case FX_NUMBER_INT64: number->n_value.v_int64 = -1; break; - case B_NUMBER_FLOAT32: + case FX_NUMBER_FLOAT32: number->n_value.v_float32 = -1; break; - case B_NUMBER_FLOAT64: + case FX_NUMBER_FLOAT64: number->n_value.v_float64 = -1; break; - case B_NUMBER_CHAR: + case FX_NUMBER_CHAR: number->n_value.v_char = -1; break; - case B_NUMBER_SHORT: + case FX_NUMBER_SHORT: number->n_value.v_short = -1; break; - case B_NUMBER_INT: + case FX_NUMBER_INT: number->n_value.v_int = -1; break; - case B_NUMBER_LONG: + case FX_NUMBER_LONG: number->n_value.v_long = -1; break; - case B_NUMBER_LONGLONG: + case FX_NUMBER_LONGLONG: number->n_value.v_longlong = -1; break; - case B_NUMBER_FLOAT: + case FX_NUMBER_FLOAT: number->n_value.v_float = -1; break; - case B_NUMBER_DOUBLE: + case FX_NUMBER_DOUBLE: number->n_value.v_double = -1; break; - case B_NUMBER_SIZE_T: + case FX_NUMBER_SIZE_T: number->n_value.v_size_t = -1; break; default: @@ -492,162 +492,162 @@ static void number_set_nan_negative(struct b_number_p *number, bool v) } } -static size_t number_data_size(const struct b_number_p *number) +static size_t number_data_size(const struct fx_number_p *number) { switch (number->n_type) { - case B_NUMBER_INT8: + case FX_NUMBER_INT8: return sizeof number->n_value.v_int8; - case B_NUMBER_INT16: + case FX_NUMBER_INT16: return sizeof number->n_value.v_int16; - case B_NUMBER_INT32: + case FX_NUMBER_INT32: return sizeof number->n_value.v_int32; - case B_NUMBER_INT64: + case FX_NUMBER_INT64: return sizeof number->n_value.v_int64; - case B_NUMBER_FLOAT32: + case FX_NUMBER_FLOAT32: return sizeof number->n_value.v_float32; - case B_NUMBER_FLOAT64: + case FX_NUMBER_FLOAT64: return sizeof number->n_value.v_float64; - case B_NUMBER_CHAR: + case FX_NUMBER_CHAR: return sizeof number->n_value.v_char; - case B_NUMBER_SHORT: + case FX_NUMBER_SHORT: return sizeof number->n_value.v_short; - case B_NUMBER_INT: + case FX_NUMBER_INT: return sizeof number->n_value.v_int; - case B_NUMBER_LONG: + case FX_NUMBER_LONG: return sizeof number->n_value.v_long; - case B_NUMBER_LONGLONG: + case FX_NUMBER_LONGLONG: return sizeof number->n_value.v_longlong; - case B_NUMBER_FLOAT: + case FX_NUMBER_FLOAT: return sizeof number->n_value.v_float; - case B_NUMBER_DOUBLE: + case FX_NUMBER_DOUBLE: return sizeof number->n_value.v_double; - case B_NUMBER_SIZE_T: + case FX_NUMBER_SIZE_T: return sizeof number->n_value.v_size_t; default: return 0; } } -static void print_inf(const struct b_number_p *n, b_stream *out) +static void print_inf(const struct fx_number_p *n, fx_stream *out) { switch (n->n_type) { - case B_NUMBER_INT8: - b_stream_write_string(out, n->n_value.v_int8 < 0 ? "-" : "", NULL); + case FX_NUMBER_INT8: + fx_stream_write_string(out, n->n_value.v_int8 < 0 ? "-" : "", NULL); break; - case B_NUMBER_INT16: - b_stream_write_string(out, n->n_value.v_int16 < 0 ? "-" : "", NULL); + case FX_NUMBER_INT16: + fx_stream_write_string(out, n->n_value.v_int16 < 0 ? "-" : "", NULL); break; - case B_NUMBER_INT32: - b_stream_write_string(out, n->n_value.v_int32 < 0 ? "-" : "", NULL); + case FX_NUMBER_INT32: + fx_stream_write_string(out, n->n_value.v_int32 < 0 ? "-" : "", NULL); break; - case B_NUMBER_INT64: - b_stream_write_string(out, n->n_value.v_int64 < 0 ? "-" : "", NULL); + case FX_NUMBER_INT64: + fx_stream_write_string(out, n->n_value.v_int64 < 0 ? "-" : "", NULL); break; - case B_NUMBER_FLOAT32: - b_stream_write_string( + case FX_NUMBER_FLOAT32: + fx_stream_write_string( out, n->n_value.v_float32 < 0 ? "-" : "", NULL); break; - case B_NUMBER_FLOAT64: - b_stream_write_string( + case FX_NUMBER_FLOAT64: + fx_stream_write_string( out, n->n_value.v_float64 < 0 ? "-" : "", NULL); break; - case B_NUMBER_CHAR: - b_stream_write_string(out, n->n_value.v_char < 0 ? "-" : "", NULL); + case FX_NUMBER_CHAR: + fx_stream_write_string(out, n->n_value.v_char < 0 ? "-" : "", NULL); break; - case B_NUMBER_SHORT: - b_stream_write_string(out, n->n_value.v_short < 0 ? "-" : "", NULL); + case FX_NUMBER_SHORT: + fx_stream_write_string(out, n->n_value.v_short < 0 ? "-" : "", NULL); break; - case B_NUMBER_INT: - b_stream_write_string(out, n->n_value.v_int < 0 ? "-" : "", NULL); + case FX_NUMBER_INT: + fx_stream_write_string(out, n->n_value.v_int < 0 ? "-" : "", NULL); break; - case B_NUMBER_LONG: - b_stream_write_string(out, n->n_value.v_long < 0 ? "-" : "", NULL); + case FX_NUMBER_LONG: + fx_stream_write_string(out, n->n_value.v_long < 0 ? "-" : "", NULL); break; - case B_NUMBER_LONGLONG: - b_stream_write_string( + case FX_NUMBER_LONGLONG: + fx_stream_write_string( out, n->n_value.v_longlong < 0 ? "-" : "", NULL); break; - case B_NUMBER_FLOAT: - b_stream_write_string(out, n->n_value.v_float < 0 ? "-" : "", NULL); + case FX_NUMBER_FLOAT: + fx_stream_write_string(out, n->n_value.v_float < 0 ? "-" : "", NULL); break; - case B_NUMBER_DOUBLE: - b_stream_write_string(out, n->n_value.v_double < 0 ? "-" : "", NULL); + case FX_NUMBER_DOUBLE: + fx_stream_write_string(out, n->n_value.v_double < 0 ? "-" : "", NULL); break; - case B_NUMBER_SIZE_T: - b_stream_write_string(out, n->n_value.v_size_t < 0 ? "-" : "", NULL); + case FX_NUMBER_SIZE_T: + fx_stream_write_string(out, n->n_value.v_size_t < 0 ? "-" : "", NULL); break; default: break; } - b_stream_write_string(out, "INF", NULL); + fx_stream_write_string(out, "INF", NULL); } -static void print_nan(const struct b_number_p *n, b_stream *out) +static void print_nan(const struct fx_number_p *n, fx_stream *out) { switch (n->n_type) { - case B_NUMBER_INT8: - b_stream_write_string(out, n->n_value.v_int8 < 0 ? "-" : "", NULL); + case FX_NUMBER_INT8: + fx_stream_write_string(out, n->n_value.v_int8 < 0 ? "-" : "", NULL); break; - case B_NUMBER_INT16: - b_stream_write_string(out, n->n_value.v_int16 < 0 ? "-" : "", NULL); + case FX_NUMBER_INT16: + fx_stream_write_string(out, n->n_value.v_int16 < 0 ? "-" : "", NULL); break; - case B_NUMBER_INT32: - b_stream_write_string(out, n->n_value.v_int32 < 0 ? "-" : "", NULL); + case FX_NUMBER_INT32: + fx_stream_write_string(out, n->n_value.v_int32 < 0 ? "-" : "", NULL); break; - case B_NUMBER_INT64: - b_stream_write_string(out, n->n_value.v_int64 < 0 ? "-" : "", NULL); + case FX_NUMBER_INT64: + fx_stream_write_string(out, n->n_value.v_int64 < 0 ? "-" : "", NULL); break; - case B_NUMBER_FLOAT32: - b_stream_write_string( + case FX_NUMBER_FLOAT32: + fx_stream_write_string( out, n->n_value.v_float32 < 0 ? "-" : "", NULL); break; - case B_NUMBER_FLOAT64: - b_stream_write_string( + case FX_NUMBER_FLOAT64: + fx_stream_write_string( out, n->n_value.v_float64 < 0 ? "-" : "", NULL); break; - case B_NUMBER_CHAR: - b_stream_write_string(out, n->n_value.v_char < 0 ? "-" : "", NULL); + case FX_NUMBER_CHAR: + fx_stream_write_string(out, n->n_value.v_char < 0 ? "-" : "", NULL); break; - case B_NUMBER_SHORT: - b_stream_write_string(out, n->n_value.v_short < 0 ? "-" : "", NULL); + case FX_NUMBER_SHORT: + fx_stream_write_string(out, n->n_value.v_short < 0 ? "-" : "", NULL); break; - case B_NUMBER_INT: - b_stream_write_string(out, n->n_value.v_int < 0 ? "-" : "", NULL); + case FX_NUMBER_INT: + fx_stream_write_string(out, n->n_value.v_int < 0 ? "-" : "", NULL); break; - case B_NUMBER_LONG: - b_stream_write_string(out, n->n_value.v_long < 0 ? "-" : "", NULL); + case FX_NUMBER_LONG: + fx_stream_write_string(out, n->n_value.v_long < 0 ? "-" : "", NULL); break; - case B_NUMBER_LONGLONG: - b_stream_write_string( + case FX_NUMBER_LONGLONG: + fx_stream_write_string( out, n->n_value.v_longlong < 0 ? "-" : "", NULL); break; - case B_NUMBER_FLOAT: - b_stream_write_string(out, n->n_value.v_float < 0 ? "-" : "", NULL); + case FX_NUMBER_FLOAT: + fx_stream_write_string(out, n->n_value.v_float < 0 ? "-" : "", NULL); break; - case B_NUMBER_DOUBLE: - b_stream_write_string(out, n->n_value.v_double < 0 ? "-" : "", NULL); + case FX_NUMBER_DOUBLE: + fx_stream_write_string(out, n->n_value.v_double < 0 ? "-" : "", NULL); break; - case B_NUMBER_SIZE_T: - b_stream_write_string(out, n->n_value.v_size_t < 0 ? "-" : "", NULL); + case FX_NUMBER_SIZE_T: + fx_stream_write_string(out, n->n_value.v_size_t < 0 ? "-" : "", NULL); break; default: break; } - b_stream_write_string(out, "NaN", NULL); + fx_stream_write_string(out, "NaN", NULL); } /*** PUBLIC FUNCTIONS *********************************************************/ -b_number *b_number_create(b_number_type type, void *value_ptr) +fx_number *fx_number_create(fx_number_type type, void *value_ptr) { - b_number *n = b_object_create(B_TYPE_NUMBER); + fx_number *n = fx_object_create(FX_TYPE_NUMBER); if (!n) { return NULL; } - struct b_number_p *p = b_object_get_private(n, B_TYPE_NUMBER); + struct fx_number_p *p = fx_object_get_private(n, FX_TYPE_NUMBER); p->n_type = type; @@ -656,49 +656,49 @@ b_number *b_number_create(b_number_type type, void *value_ptr) } switch (p->n_type) { - case B_NUMBER_INT8: + case FX_NUMBER_INT8: memcpy(&p->n_value.v_int8, value_ptr, sizeof p->n_value.v_int8); break; - case B_NUMBER_INT16: + case FX_NUMBER_INT16: memcpy(&p->n_value.v_int16, value_ptr, sizeof p->n_value.v_int16); break; - case B_NUMBER_INT32: + case FX_NUMBER_INT32: memcpy(&p->n_value.v_int32, value_ptr, sizeof p->n_value.v_int32); break; - case B_NUMBER_INT64: + case FX_NUMBER_INT64: memcpy(&p->n_value.v_int64, value_ptr, sizeof p->n_value.v_int64); break; - case B_NUMBER_FLOAT32: + case FX_NUMBER_FLOAT32: memcpy(&p->n_value.v_float32, value_ptr, sizeof p->n_value.v_float32); break; - case B_NUMBER_FLOAT64: + case FX_NUMBER_FLOAT64: memcpy(&p->n_value.v_float64, value_ptr, sizeof p->n_value.v_float64); break; - case B_NUMBER_CHAR: + case FX_NUMBER_CHAR: memcpy(&p->n_value.v_char, value_ptr, sizeof p->n_value.v_char); break; - case B_NUMBER_SHORT: + case FX_NUMBER_SHORT: memcpy(&p->n_value.v_short, value_ptr, sizeof p->n_value.v_short); break; - case B_NUMBER_INT: + case FX_NUMBER_INT: memcpy(&p->n_value.v_int, value_ptr, sizeof p->n_value.v_int); break; - case B_NUMBER_LONG: + case FX_NUMBER_LONG: memcpy(&p->n_value.v_long, value_ptr, sizeof p->n_value.v_long); break; - case B_NUMBER_LONGLONG: + case FX_NUMBER_LONGLONG: memcpy(&p->n_value.v_longlong, value_ptr, sizeof p->n_value.v_longlong); break; - case B_NUMBER_FLOAT: + case FX_NUMBER_FLOAT: memcpy(&p->n_value.v_float, value_ptr, sizeof p->n_value.v_float); break; - case B_NUMBER_DOUBLE: + case FX_NUMBER_DOUBLE: memcpy(&p->n_value.v_double, value_ptr, sizeof p->n_value.v_double); break; - case B_NUMBER_SIZE_T: + case FX_NUMBER_SIZE_T: memcpy(&p->n_value.v_size_t, value_ptr, sizeof p->n_value.v_size_t); break; default: @@ -708,97 +708,97 @@ b_number *b_number_create(b_number_type type, void *value_ptr) return n; } -b_number_type b_number_get_number_type(const b_number *number) +fx_number_type fx_number_get_number_type(const fx_number *number) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_NUMBER, number_get_number_type, number); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_NUMBER, number_get_number_type, number); } -int b_number_get_value(const b_number *number, b_number_type type, void *value_ptr) +int fx_number_get_value(const fx_number *number, fx_number_type type, void *value_ptr) { - B_CLASS_DISPATCH_STATIC( - B_TYPE_NUMBER, number_get_value, number, type, value_ptr); + FX_CLASS_DISPATCH_STATIC( + FX_TYPE_NUMBER, number_get_value, number, type, value_ptr); } -bool b_number_is_integer(const b_number *number) +bool fx_number_is_integer(const fx_number *number) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_NUMBER, number_is_integer, number); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_NUMBER, number_is_integer, number); } -bool b_number_is_float(const b_number *number) +bool fx_number_is_float(const fx_number *number) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_NUMBER, number_is_float, number); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_NUMBER, number_is_float, number); } -bool b_number_is_inf(const b_number *number) +bool fx_number_is_inf(const fx_number *number) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_NUMBER, number_is_inf, number); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_NUMBER, number_is_inf, number); } -bool b_number_is_inf_positive(const b_number *number) +bool fx_number_is_inf_positive(const fx_number *number) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_NUMBER, number_is_inf_positive, number); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_NUMBER, number_is_inf_positive, number); } -bool b_number_is_inf_negative(const b_number *number) +bool fx_number_is_inf_negative(const fx_number *number) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_NUMBER, number_is_inf_negative, number); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_NUMBER, number_is_inf_negative, number); } -bool b_number_is_nan(const b_number *number) +bool fx_number_is_nan(const fx_number *number) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_NUMBER, number_is_nan, number); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_NUMBER, number_is_nan, number); } -bool b_number_is_nan_positive(const b_number *number) +bool fx_number_is_nan_positive(const fx_number *number) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_NUMBER, number_is_nan_positive, number); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_NUMBER, number_is_nan_positive, number); } -bool b_number_is_nan_negative(const b_number *number) +bool fx_number_is_nan_negative(const fx_number *number) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_NUMBER, number_is_nan_negative, number); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_NUMBER, number_is_nan_negative, number); } -void b_number_set_inf_positive(b_number *number, bool v) +void fx_number_set_inf_positive(fx_number *number, bool v) { - B_CLASS_DISPATCH_STATIC(B_TYPE_NUMBER, number_set_inf_positive, number, v); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_NUMBER, number_set_inf_positive, number, v); } -void b_number_set_inf_negative(b_number *number, bool v) +void fx_number_set_inf_negative(fx_number *number, bool v) { - B_CLASS_DISPATCH_STATIC(B_TYPE_NUMBER, number_set_inf_negative, number, v); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_NUMBER, number_set_inf_negative, number, v); } -void b_number_set_nan_positive(b_number *number, bool v) +void fx_number_set_nan_positive(fx_number *number, bool v) { - B_CLASS_DISPATCH_STATIC(B_TYPE_NUMBER, number_set_nan_positive, number, v); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_NUMBER, number_set_nan_positive, number, v); } -void b_number_set_nan_negative(b_number *number, bool v) +void fx_number_set_nan_negative(fx_number *number, bool v) { - B_CLASS_DISPATCH_STATIC(B_TYPE_NUMBER, number_set_nan_negative, number, v); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_NUMBER, number_set_nan_negative, number, v); } -size_t b_number_data_size(const b_number *number) +size_t fx_number_data_size(const fx_number *number) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_NUMBER, number_data_size, number); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_NUMBER, number_data_size, number); } /*** VIRTUAL FUNCTIONS ********************************************************/ -static void number_init(b_object *obj, void *priv) +static void number_init(fx_object *obj, void *priv) { - struct b_number_p *number = priv; + struct fx_number_p *number = priv; } -static void number_fini(b_object *obj, void *priv) +static void number_fini(fx_object *obj, void *priv) { - struct b_number_p *number = priv; + struct fx_number_p *number = priv; } -static void number_to_string(const b_object *obj, b_stream *out) +static void number_to_string(const fx_object *obj, fx_stream *out) { - struct b_number_p *number = b_object_get_private(obj, B_TYPE_NUMBER); + struct fx_number_p *number = fx_object_get_private(obj, FX_TYPE_NUMBER); if (number->n_flags & NUMBER_F_INF) { print_inf(number, out); @@ -811,50 +811,50 @@ static void number_to_string(const b_object *obj, b_stream *out) } switch (number->n_type) { - case B_NUMBER_INT8: - b_stream_write_fmt(out, NULL, "%" PRIu8, number->n_value.v_int8); + case FX_NUMBER_INT8: + fx_stream_write_fmt(out, NULL, "%" PRIu8, number->n_value.v_int8); break; - case B_NUMBER_INT16: - b_stream_write_fmt(out, NULL, "%" PRIu16, number->n_value.v_int16); + case FX_NUMBER_INT16: + fx_stream_write_fmt(out, NULL, "%" PRIu16, number->n_value.v_int16); break; - case B_NUMBER_INT32: - b_stream_write_fmt(out, NULL, "%" PRIu32, number->n_value.v_int32); + case FX_NUMBER_INT32: + fx_stream_write_fmt(out, NULL, "%" PRIu32, number->n_value.v_int32); break; - case B_NUMBER_INT64: - b_stream_write_fmt(out, NULL, "%" PRIu64, number->n_value.v_int64); + case FX_NUMBER_INT64: + fx_stream_write_fmt(out, NULL, "%" PRIu64, number->n_value.v_int64); break; - case B_NUMBER_FLOAT32: - b_stream_write_fmt(out, NULL, "%f", number->n_value.v_float32); + case FX_NUMBER_FLOAT32: + fx_stream_write_fmt(out, NULL, "%f", number->n_value.v_float32); break; - case B_NUMBER_FLOAT64: - b_stream_write_fmt(out, NULL, "%lf", number->n_value.v_float64); + case FX_NUMBER_FLOAT64: + fx_stream_write_fmt(out, NULL, "%lf", number->n_value.v_float64); break; - case B_NUMBER_CHAR: - b_stream_write_fmt(out, NULL, "%d", number->n_value.v_char); + case FX_NUMBER_CHAR: + fx_stream_write_fmt(out, NULL, "%d", number->n_value.v_char); break; - case B_NUMBER_SHORT: - b_stream_write_fmt(out, NULL, "%d", number->n_value.v_short); + case FX_NUMBER_SHORT: + fx_stream_write_fmt(out, NULL, "%d", number->n_value.v_short); break; - case B_NUMBER_INT: - b_stream_write_fmt(out, NULL, "%d", number->n_value.v_int); + case FX_NUMBER_INT: + fx_stream_write_fmt(out, NULL, "%d", number->n_value.v_int); break; - case B_NUMBER_LONG: - b_stream_write_fmt(out, NULL, "%ld", number->n_value.v_long); + case FX_NUMBER_LONG: + fx_stream_write_fmt(out, NULL, "%ld", number->n_value.v_long); break; - case B_NUMBER_LONGLONG: - b_stream_write_fmt(out, NULL, "%lld", number->n_value.v_longlong); + case FX_NUMBER_LONGLONG: + fx_stream_write_fmt(out, NULL, "%lld", number->n_value.v_longlong); break; - case B_NUMBER_FLOAT: - b_stream_write_fmt(out, NULL, "%f", number->n_value.v_float); + case FX_NUMBER_FLOAT: + fx_stream_write_fmt(out, NULL, "%f", number->n_value.v_float); break; - case B_NUMBER_DOUBLE: - b_stream_write_fmt(out, NULL, "%lf", number->n_value.v_double); + case FX_NUMBER_DOUBLE: + fx_stream_write_fmt(out, NULL, "%lf", number->n_value.v_double); break; - case B_NUMBER_SIZE_T: - b_stream_write_fmt(out, NULL, "%zu", number->n_value.v_size_t); + case FX_NUMBER_SIZE_T: + fx_stream_write_fmt(out, NULL, "%zu", number->n_value.v_size_t); break; - case B_NUMBER_HANDLE: - b_stream_write_fmt( + case FX_NUMBER_HANDLE: + fx_stream_write_fmt( out, NULL, "%016" PRIx64, number->n_value.v_size_t); break; default: @@ -864,103 +864,103 @@ static void number_to_string(const b_object *obj, b_stream *out) /*** CLASS DEFINITION *********************************************************/ -B_TYPE_CLASS_DEFINITION_BEGIN(b_number) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = number_to_string; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) -B_TYPE_CLASS_DEFINITION_END(b_number) +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_number) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = number_to_string; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_DEFINITION_END(fx_number) -B_TYPE_DEFINITION_BEGIN(b_number) - B_TYPE_ID(0xa713b0ea, 0x240f, 0x4bc5, 0xbe73, 0xbc3e56401bd3); - B_TYPE_CLASS(b_number_class); - B_TYPE_INSTANCE_PRIVATE(struct b_number_p); - B_TYPE_INSTANCE_INIT(number_init); - B_TYPE_INSTANCE_FINI(number_fini); -B_TYPE_DEFINITION_END(b_number) +FX_TYPE_DEFINITION_BEGIN(fx_number) + FX_TYPE_ID(0xa713b0ea, 0x240f, 0x4bc5, 0xbe73, 0xbc3e56401bd3); + FX_TYPE_CLASS(fx_number_class); + FX_TYPE_INSTANCE_PRIVATE(struct fx_number_p); + FX_TYPE_INSTANCE_INIT(number_init); + FX_TYPE_INSTANCE_FINI(number_fini); +FX_TYPE_DEFINITION_END(fx_number) /*** MISC FUNCTIONS ***********************************************************/ /* ++++++++++++++++++ int8 -> xyz CONVERTER CALLBACKS ++++++++++++++++++ */ -static int int8_to_int8(const struct b_number_p *number, void *value_ptr) +static int int8_to_int8(const struct fx_number_p *number, void *value_ptr) { *(int8_t *)value_ptr = number->n_value.v_int8; return 0; } -static int int8_to_int16(const struct b_number_p *number, void *value_ptr) +static int int8_to_int16(const struct fx_number_p *number, void *value_ptr) { *(int16_t *)value_ptr = (int16_t)number->n_value.v_int8; return 0; } -static int int8_to_int32(const struct b_number_p *number, void *value_ptr) +static int int8_to_int32(const struct fx_number_p *number, void *value_ptr) { *(int32_t *)value_ptr = (int32_t)number->n_value.v_int8; return 0; } -static int int8_to_int64(const struct b_number_p *number, void *value_ptr) +static int int8_to_int64(const struct fx_number_p *number, void *value_ptr) { *(int64_t *)value_ptr = (int64_t)number->n_value.v_int8; return 0; } -static int int8_to_float32(const struct b_number_p *number, void *value_ptr) +static int int8_to_float32(const struct fx_number_p *number, void *value_ptr) { *(float *)value_ptr = number->n_value.v_int8; return 0; } -static int int8_to_float64(const struct b_number_p *number, void *value_ptr) +static int int8_to_float64(const struct fx_number_p *number, void *value_ptr) { *(double *)value_ptr = number->n_value.v_int8; return 0; } -static int int8_to_char(const struct b_number_p *number, void *value_ptr) +static int int8_to_char(const struct fx_number_p *number, void *value_ptr) { *(char *)value_ptr = number->n_value.v_int8; return 0; } -static int int8_to_short(const struct b_number_p *number, void *value_ptr) +static int int8_to_short(const struct fx_number_p *number, void *value_ptr) { *(short *)value_ptr = (short)number->n_value.v_int8; return 0; } -static int int8_to_int(const struct b_number_p *number, void *value_ptr) +static int int8_to_int(const struct fx_number_p *number, void *value_ptr) { *(int *)value_ptr = (int)number->n_value.v_int8; return 0; } -static int int8_to_long(const struct b_number_p *number, void *value_ptr) +static int int8_to_long(const struct fx_number_p *number, void *value_ptr) { *(long *)value_ptr = (long)number->n_value.v_int8; return 0; } -static int int8_to_longlong(const struct b_number_p *number, void *value_ptr) +static int int8_to_longlong(const struct fx_number_p *number, void *value_ptr) { *(long long *)value_ptr = (long long)number->n_value.v_int8; return 0; } -static int int8_to_float(const struct b_number_p *number, void *value_ptr) +static int int8_to_float(const struct fx_number_p *number, void *value_ptr) { *(float *)value_ptr = number->n_value.v_int8; return 0; } -static int int8_to_double(const struct b_number_p *number, void *value_ptr) +static int int8_to_double(const struct fx_number_p *number, void *value_ptr) { *(double *)value_ptr = number->n_value.v_int8; return 0; } -static int int8_to_size_t(const struct b_number_p *number, void *value_ptr) +static int int8_to_size_t(const struct fx_number_p *number, void *value_ptr) { *(size_t *)value_ptr = (size_t)number->n_value.v_int8; return 0; @@ -968,85 +968,85 @@ static int int8_to_size_t(const struct b_number_p *number, void *value_ptr) /* ++++++++++++++++++ int16 -> xyz CONVERTER CALLBACKS ++++++++++++++++++ */ -static int int16_to_int8(const struct b_number_p *number, void *value_ptr) +static int int16_to_int8(const struct fx_number_p *number, void *value_ptr) { *(int8_t *)value_ptr = (int8_t)number->n_value.v_int16; return 0; } -static int int16_to_int16(const struct b_number_p *number, void *value_ptr) +static int int16_to_int16(const struct fx_number_p *number, void *value_ptr) { *(int16_t *)value_ptr = number->n_value.v_int16; return 0; } -static int int16_to_int32(const struct b_number_p *number, void *value_ptr) +static int int16_to_int32(const struct fx_number_p *number, void *value_ptr) { *(int32_t *)value_ptr = number->n_value.v_int16; return 0; } -static int int16_to_int64(const struct b_number_p *number, void *value_ptr) +static int int16_to_int64(const struct fx_number_p *number, void *value_ptr) { *(int64_t *)value_ptr = number->n_value.v_int16; return 0; } -static int int16_to_float32(const struct b_number_p *number, void *value_ptr) +static int int16_to_float32(const struct fx_number_p *number, void *value_ptr) { *(float *)value_ptr = number->n_value.v_int16; return 0; } -static int int16_to_float64(const struct b_number_p *number, void *value_ptr) +static int int16_to_float64(const struct fx_number_p *number, void *value_ptr) { *(double *)value_ptr = number->n_value.v_int16; return 0; } -static int int16_to_char(const struct b_number_p *number, void *value_ptr) +static int int16_to_char(const struct fx_number_p *number, void *value_ptr) { *(char *)value_ptr = (char)number->n_value.v_int16; return 0; } -static int int16_to_short(const struct b_number_p *number, void *value_ptr) +static int int16_to_short(const struct fx_number_p *number, void *value_ptr) { *(short *)value_ptr = number->n_value.v_int16; return 0; } -static int int16_to_int(const struct b_number_p *number, void *value_ptr) +static int int16_to_int(const struct fx_number_p *number, void *value_ptr) { *(int *)value_ptr = number->n_value.v_int16; return 0; } -static int int16_to_long(const struct b_number_p *number, void *value_ptr) +static int int16_to_long(const struct fx_number_p *number, void *value_ptr) { *(long *)value_ptr = number->n_value.v_int16; return 0; } -static int int16_to_longlong(const struct b_number_p *number, void *value_ptr) +static int int16_to_longlong(const struct fx_number_p *number, void *value_ptr) { *(long long *)value_ptr = number->n_value.v_int16; return 0; } -static int int16_to_float(const struct b_number_p *number, void *value_ptr) +static int int16_to_float(const struct fx_number_p *number, void *value_ptr) { *(float *)value_ptr = number->n_value.v_int16; return 0; } -static int int16_to_double(const struct b_number_p *number, void *value_ptr) +static int int16_to_double(const struct fx_number_p *number, void *value_ptr) { *(double *)value_ptr = number->n_value.v_int16; return 0; } -static int int16_to_size_t(const struct b_number_p *number, void *value_ptr) +static int int16_to_size_t(const struct fx_number_p *number, void *value_ptr) { *(size_t *)value_ptr = number->n_value.v_int16; return 0; @@ -1054,85 +1054,85 @@ static int int16_to_size_t(const struct b_number_p *number, void *value_ptr) /* ++++++++++++++++++ int32 -> xyz CONVERTER CALLBACKS ++++++++++++++++++ */ -static int int32_to_int8(const struct b_number_p *number, void *value_ptr) +static int int32_to_int8(const struct fx_number_p *number, void *value_ptr) { *(int8_t *)value_ptr = (int8_t)number->n_value.v_int32; return 0; } -static int int32_to_int16(const struct b_number_p *number, void *value_ptr) +static int int32_to_int16(const struct fx_number_p *number, void *value_ptr) { *(int16_t *)value_ptr = (int16_t)number->n_value.v_int32; return 0; } -static int int32_to_int32(const struct b_number_p *number, void *value_ptr) +static int int32_to_int32(const struct fx_number_p *number, void *value_ptr) { *(int32_t *)value_ptr = number->n_value.v_int32; return 0; } -static int int32_to_int64(const struct b_number_p *number, void *value_ptr) +static int int32_to_int64(const struct fx_number_p *number, void *value_ptr) { *(int64_t *)value_ptr = number->n_value.v_int32; return 0; } -static int int32_to_float32(const struct b_number_p *number, void *value_ptr) +static int int32_to_float32(const struct fx_number_p *number, void *value_ptr) { *(float *)value_ptr = (float)number->n_value.v_int32; return 0; } -static int int32_to_float64(const struct b_number_p *number, void *value_ptr) +static int int32_to_float64(const struct fx_number_p *number, void *value_ptr) { *(double *)value_ptr = number->n_value.v_int32; return 0; } -static int int32_to_char(const struct b_number_p *number, void *value_ptr) +static int int32_to_char(const struct fx_number_p *number, void *value_ptr) { *(char *)value_ptr = (char)number->n_value.v_int32; return 0; } -static int int32_to_short(const struct b_number_p *number, void *value_ptr) +static int int32_to_short(const struct fx_number_p *number, void *value_ptr) { *(short *)value_ptr = (short)number->n_value.v_int32; return 0; } -static int int32_to_int(const struct b_number_p *number, void *value_ptr) +static int int32_to_int(const struct fx_number_p *number, void *value_ptr) { *(int *)value_ptr = number->n_value.v_int32; return 0; } -static int int32_to_long(const struct b_number_p *number, void *value_ptr) +static int int32_to_long(const struct fx_number_p *number, void *value_ptr) { *(long *)value_ptr = number->n_value.v_int32; return 0; } -static int int32_to_longlong(const struct b_number_p *number, void *value_ptr) +static int int32_to_longlong(const struct fx_number_p *number, void *value_ptr) { *(long long *)value_ptr = number->n_value.v_int32; return 0; } -static int int32_to_float(const struct b_number_p *number, void *value_ptr) +static int int32_to_float(const struct fx_number_p *number, void *value_ptr) { *(float *)value_ptr = (float)number->n_value.v_int32; return 0; } -static int int32_to_double(const struct b_number_p *number, void *value_ptr) +static int int32_to_double(const struct fx_number_p *number, void *value_ptr) { *(double *)value_ptr = number->n_value.v_int32; return 0; } -static int int32_to_size_t(const struct b_number_p *number, void *value_ptr) +static int int32_to_size_t(const struct fx_number_p *number, void *value_ptr) { *(size_t *)value_ptr = number->n_value.v_int32; return 0; @@ -1140,85 +1140,85 @@ static int int32_to_size_t(const struct b_number_p *number, void *value_ptr) /* ++++++++++++++++++ int64 -> xyz CONVERTER CALLBACKS ++++++++++++++++++ */ -static int int64_to_int8(const struct b_number_p *number, void *value_ptr) +static int int64_to_int8(const struct fx_number_p *number, void *value_ptr) { *(int8_t *)value_ptr = (int8_t)number->n_value.v_int64; return 0; } -static int int64_to_int16(const struct b_number_p *number, void *value_ptr) +static int int64_to_int16(const struct fx_number_p *number, void *value_ptr) { *(int16_t *)value_ptr = (int16_t)number->n_value.v_int64; return 0; } -static int int64_to_int32(const struct b_number_p *number, void *value_ptr) +static int int64_to_int32(const struct fx_number_p *number, void *value_ptr) { *(int32_t *)value_ptr = (int32_t)number->n_value.v_int64; return 0; } -static int int64_to_int64(const struct b_number_p *number, void *value_ptr) +static int int64_to_int64(const struct fx_number_p *number, void *value_ptr) { *(int64_t *)value_ptr = number->n_value.v_int64; return 0; } -static int int64_to_float32(const struct b_number_p *number, void *value_ptr) +static int int64_to_float32(const struct fx_number_p *number, void *value_ptr) { *(float *)value_ptr = (float)number->n_value.v_int64; return 0; } -static int int64_to_float64(const struct b_number_p *number, void *value_ptr) +static int int64_to_float64(const struct fx_number_p *number, void *value_ptr) { *(double *)value_ptr = (double)number->n_value.v_int64; return 0; } -static int int64_to_char(const struct b_number_p *number, void *value_ptr) +static int int64_to_char(const struct fx_number_p *number, void *value_ptr) { *(char *)value_ptr = (char)number->n_value.v_int64; return 0; } -static int int64_to_short(const struct b_number_p *number, void *value_ptr) +static int int64_to_short(const struct fx_number_p *number, void *value_ptr) { *(short *)value_ptr = (short)number->n_value.v_int64; return 0; } -static int int64_to_int(const struct b_number_p *number, void *value_ptr) +static int int64_to_int(const struct fx_number_p *number, void *value_ptr) { *(int *)value_ptr = (int)number->n_value.v_int64; return 0; } -static int int64_to_long(const struct b_number_p *number, void *value_ptr) +static int int64_to_long(const struct fx_number_p *number, void *value_ptr) { *(long *)value_ptr = number->n_value.v_int64; return 0; } -static int int64_to_longlong(const struct b_number_p *number, void *value_ptr) +static int int64_to_longlong(const struct fx_number_p *number, void *value_ptr) { *(long long *)value_ptr = number->n_value.v_int64; return 0; } -static int int64_to_float(const struct b_number_p *number, void *value_ptr) +static int int64_to_float(const struct fx_number_p *number, void *value_ptr) { *(float *)value_ptr = (float)number->n_value.v_int64; return 0; } -static int int64_to_double(const struct b_number_p *number, void *value_ptr) +static int int64_to_double(const struct fx_number_p *number, void *value_ptr) { *(double *)value_ptr = (double)number->n_value.v_int64; return 0; } -static int int64_to_size_t(const struct b_number_p *number, void *value_ptr) +static int int64_to_size_t(const struct fx_number_p *number, void *value_ptr) { *(size_t *)value_ptr = number->n_value.v_int64; return 0; @@ -1226,85 +1226,85 @@ static int int64_to_size_t(const struct b_number_p *number, void *value_ptr) /* ++++++++++++++++++ float32 -> xyz CONVERTER CALLBACKS ++++++++++++++++++ */ -static int float32_to_int8(const struct b_number_p *number, void *value_ptr) +static int float32_to_int8(const struct fx_number_p *number, void *value_ptr) { *(int8_t *)value_ptr = (int8_t)number->n_value.v_float32; return 0; } -static int float32_to_int16(const struct b_number_p *number, void *value_ptr) +static int float32_to_int16(const struct fx_number_p *number, void *value_ptr) { *(int16_t *)value_ptr = (int16_t)number->n_value.v_float32; return 0; } -static int float32_to_int32(const struct b_number_p *number, void *value_ptr) +static int float32_to_int32(const struct fx_number_p *number, void *value_ptr) { *(int32_t *)value_ptr = (int32_t)number->n_value.v_float32; return 0; } -static int float32_to_int64(const struct b_number_p *number, void *value_ptr) +static int float32_to_int64(const struct fx_number_p *number, void *value_ptr) { *(int64_t *)value_ptr = (int64_t)number->n_value.v_float32; return 0; } -static int float32_to_float32(const struct b_number_p *number, void *value_ptr) +static int float32_to_float32(const struct fx_number_p *number, void *value_ptr) { *(float *)value_ptr = number->n_value.v_float32; return 0; } -static int float32_to_float64(const struct b_number_p *number, void *value_ptr) +static int float32_to_float64(const struct fx_number_p *number, void *value_ptr) { *(double *)value_ptr = number->n_value.v_float32; return 0; } -static int float32_to_char(const struct b_number_p *number, void *value_ptr) +static int float32_to_char(const struct fx_number_p *number, void *value_ptr) { *(char *)value_ptr = (char)number->n_value.v_float32; return 0; } -static int float32_to_short(const struct b_number_p *number, void *value_ptr) +static int float32_to_short(const struct fx_number_p *number, void *value_ptr) { *(short *)value_ptr = (short)number->n_value.v_float32; return 0; } -static int float32_to_int(const struct b_number_p *number, void *value_ptr) +static int float32_to_int(const struct fx_number_p *number, void *value_ptr) { *(int *)value_ptr = (int)number->n_value.v_float32; return 0; } -static int float32_to_long(const struct b_number_p *number, void *value_ptr) +static int float32_to_long(const struct fx_number_p *number, void *value_ptr) { *(long *)value_ptr = number->n_value.v_float32; return 0; } -static int float32_to_longlong(const struct b_number_p *number, void *value_ptr) +static int float32_to_longlong(const struct fx_number_p *number, void *value_ptr) { *(long long *)value_ptr = number->n_value.v_float32; return 0; } -static int float32_to_float(const struct b_number_p *number, void *value_ptr) +static int float32_to_float(const struct fx_number_p *number, void *value_ptr) { *(float *)value_ptr = number->n_value.v_float32; return 0; } -static int float32_to_double(const struct b_number_p *number, void *value_ptr) +static int float32_to_double(const struct fx_number_p *number, void *value_ptr) { *(double *)value_ptr = number->n_value.v_float32; return 0; } -static int float32_to_size_t(const struct b_number_p *number, void *value_ptr) +static int float32_to_size_t(const struct fx_number_p *number, void *value_ptr) { *(size_t *)value_ptr = number->n_value.v_float32; return 0; @@ -1312,85 +1312,85 @@ static int float32_to_size_t(const struct b_number_p *number, void *value_ptr) /* ++++++++++++++++++ float64 -> xyz CONVERTER CALLBACKS ++++++++++++++++++ */ -static int float64_to_int8(const struct b_number_p *number, void *value_ptr) +static int float64_to_int8(const struct fx_number_p *number, void *value_ptr) { *(int8_t *)value_ptr = number->n_value.v_float64; return 0; } -static int float64_to_int16(const struct b_number_p *number, void *value_ptr) +static int float64_to_int16(const struct fx_number_p *number, void *value_ptr) { *(int16_t *)value_ptr = number->n_value.v_float64; return 0; } -static int float64_to_int32(const struct b_number_p *number, void *value_ptr) +static int float64_to_int32(const struct fx_number_p *number, void *value_ptr) { *(int32_t *)value_ptr = number->n_value.v_float64; return 0; } -static int float64_to_int64(const struct b_number_p *number, void *value_ptr) +static int float64_to_int64(const struct fx_number_p *number, void *value_ptr) { *(int64_t *)value_ptr = number->n_value.v_float64; return 0; } -static int float64_to_float32(const struct b_number_p *number, void *value_ptr) +static int float64_to_float32(const struct fx_number_p *number, void *value_ptr) { *(float *)value_ptr = number->n_value.v_float64; return 0; } -static int float64_to_float64(const struct b_number_p *number, void *value_ptr) +static int float64_to_float64(const struct fx_number_p *number, void *value_ptr) { *(double *)value_ptr = number->n_value.v_float64; return 0; } -static int float64_to_char(const struct b_number_p *number, void *value_ptr) +static int float64_to_char(const struct fx_number_p *number, void *value_ptr) { *(char *)value_ptr = number->n_value.v_float64; return 0; } -static int float64_to_short(const struct b_number_p *number, void *value_ptr) +static int float64_to_short(const struct fx_number_p *number, void *value_ptr) { *(short *)value_ptr = number->n_value.v_float64; return 0; } -static int float64_to_int(const struct b_number_p *number, void *value_ptr) +static int float64_to_int(const struct fx_number_p *number, void *value_ptr) { *(int *)value_ptr = number->n_value.v_float64; return 0; } -static int float64_to_long(const struct b_number_p *number, void *value_ptr) +static int float64_to_long(const struct fx_number_p *number, void *value_ptr) { *(long *)value_ptr = number->n_value.v_float64; return 0; } -static int float64_to_longlong(const struct b_number_p *number, void *value_ptr) +static int float64_to_longlong(const struct fx_number_p *number, void *value_ptr) { *(long long *)value_ptr = number->n_value.v_float64; return 0; } -static int float64_to_float(const struct b_number_p *number, void *value_ptr) +static int float64_to_float(const struct fx_number_p *number, void *value_ptr) { *(float *)value_ptr = number->n_value.v_float64; return 0; } -static int float64_to_double(const struct b_number_p *number, void *value_ptr) +static int float64_to_double(const struct fx_number_p *number, void *value_ptr) { *(double *)value_ptr = number->n_value.v_float64; return 0; } -static int float64_to_size_t(const struct b_number_p *number, void *value_ptr) +static int float64_to_size_t(const struct fx_number_p *number, void *value_ptr) { *(size_t *)value_ptr = number->n_value.v_float64; return 0; @@ -1398,85 +1398,85 @@ static int float64_to_size_t(const struct b_number_p *number, void *value_ptr) /* ++++++++++++++++++ char -> xyz CONVERTER CALLBACKS ++++++++++++++++++ */ -static int char_to_int8(const struct b_number_p *number, void *value_ptr) +static int char_to_int8(const struct fx_number_p *number, void *value_ptr) { *(int8_t *)value_ptr = number->n_value.v_char; return 0; } -static int char_to_int16(const struct b_number_p *number, void *value_ptr) +static int char_to_int16(const struct fx_number_p *number, void *value_ptr) { *(int16_t *)value_ptr = number->n_value.v_char; return 0; } -static int char_to_int32(const struct b_number_p *number, void *value_ptr) +static int char_to_int32(const struct fx_number_p *number, void *value_ptr) { *(int32_t *)value_ptr = number->n_value.v_char; return 0; } -static int char_to_int64(const struct b_number_p *number, void *value_ptr) +static int char_to_int64(const struct fx_number_p *number, void *value_ptr) { *(int64_t *)value_ptr = number->n_value.v_char; return 0; } -static int char_to_float32(const struct b_number_p *number, void *value_ptr) +static int char_to_float32(const struct fx_number_p *number, void *value_ptr) { *(float *)value_ptr = number->n_value.v_char; return 0; } -static int char_to_float64(const struct b_number_p *number, void *value_ptr) +static int char_to_float64(const struct fx_number_p *number, void *value_ptr) { *(double *)value_ptr = number->n_value.v_char; return 0; } -static int char_to_char(const struct b_number_p *number, void *value_ptr) +static int char_to_char(const struct fx_number_p *number, void *value_ptr) { *(char *)value_ptr = number->n_value.v_char; return 0; } -static int char_to_short(const struct b_number_p *number, void *value_ptr) +static int char_to_short(const struct fx_number_p *number, void *value_ptr) { *(short *)value_ptr = number->n_value.v_char; return 0; } -static int char_to_int(const struct b_number_p *number, void *value_ptr) +static int char_to_int(const struct fx_number_p *number, void *value_ptr) { *(int *)value_ptr = number->n_value.v_char; return 0; } -static int char_to_long(const struct b_number_p *number, void *value_ptr) +static int char_to_long(const struct fx_number_p *number, void *value_ptr) { *(long *)value_ptr = number->n_value.v_char; return 0; } -static int char_to_longlong(const struct b_number_p *number, void *value_ptr) +static int char_to_longlong(const struct fx_number_p *number, void *value_ptr) { *(long long *)value_ptr = number->n_value.v_char; return 0; } -static int char_to_float(const struct b_number_p *number, void *value_ptr) +static int char_to_float(const struct fx_number_p *number, void *value_ptr) { *(float *)value_ptr = number->n_value.v_char; return 0; } -static int char_to_double(const struct b_number_p *number, void *value_ptr) +static int char_to_double(const struct fx_number_p *number, void *value_ptr) { *(double *)value_ptr = number->n_value.v_char; return 0; } -static int char_to_size_t(const struct b_number_p *number, void *value_ptr) +static int char_to_size_t(const struct fx_number_p *number, void *value_ptr) { *(size_t *)value_ptr = number->n_value.v_char; return 0; @@ -1484,85 +1484,85 @@ static int char_to_size_t(const struct b_number_p *number, void *value_ptr) /* ++++++++++++++++++ short -> xyz CONVERTER CALLBACKS ++++++++++++++++++ */ -static int short_to_int8(const struct b_number_p *number, void *value_ptr) +static int short_to_int8(const struct fx_number_p *number, void *value_ptr) { *(int8_t *)value_ptr = number->n_value.v_short; return 0; } -static int short_to_int16(const struct b_number_p *number, void *value_ptr) +static int short_to_int16(const struct fx_number_p *number, void *value_ptr) { *(int16_t *)value_ptr = number->n_value.v_short; return 0; } -static int short_to_int32(const struct b_number_p *number, void *value_ptr) +static int short_to_int32(const struct fx_number_p *number, void *value_ptr) { *(int32_t *)value_ptr = number->n_value.v_short; return 0; } -static int short_to_int64(const struct b_number_p *number, void *value_ptr) +static int short_to_int64(const struct fx_number_p *number, void *value_ptr) { *(int64_t *)value_ptr = number->n_value.v_short; return 0; } -static int short_to_float32(const struct b_number_p *number, void *value_ptr) +static int short_to_float32(const struct fx_number_p *number, void *value_ptr) { *(float *)value_ptr = number->n_value.v_short; return 0; } -static int short_to_float64(const struct b_number_p *number, void *value_ptr) +static int short_to_float64(const struct fx_number_p *number, void *value_ptr) { *(double *)value_ptr = number->n_value.v_short; return 0; } -static int short_to_char(const struct b_number_p *number, void *value_ptr) +static int short_to_char(const struct fx_number_p *number, void *value_ptr) { *(char *)value_ptr = number->n_value.v_short; return 0; } -static int short_to_short(const struct b_number_p *number, void *value_ptr) +static int short_to_short(const struct fx_number_p *number, void *value_ptr) { *(short *)value_ptr = number->n_value.v_short; return 0; } -static int short_to_int(const struct b_number_p *number, void *value_ptr) +static int short_to_int(const struct fx_number_p *number, void *value_ptr) { *(int *)value_ptr = number->n_value.v_short; return 0; } -static int short_to_long(const struct b_number_p *number, void *value_ptr) +static int short_to_long(const struct fx_number_p *number, void *value_ptr) { *(long *)value_ptr = number->n_value.v_short; return 0; } -static int short_to_longlong(const struct b_number_p *number, void *value_ptr) +static int short_to_longlong(const struct fx_number_p *number, void *value_ptr) { *(long long *)value_ptr = number->n_value.v_short; return 0; } -static int short_to_float(const struct b_number_p *number, void *value_ptr) +static int short_to_float(const struct fx_number_p *number, void *value_ptr) { *(float *)value_ptr = number->n_value.v_short; return 0; } -static int short_to_double(const struct b_number_p *number, void *value_ptr) +static int short_to_double(const struct fx_number_p *number, void *value_ptr) { *(double *)value_ptr = number->n_value.v_short; return 0; } -static int short_to_size_t(const struct b_number_p *number, void *value_ptr) +static int short_to_size_t(const struct fx_number_p *number, void *value_ptr) { *(size_t *)value_ptr = number->n_value.v_short; return 0; @@ -1570,85 +1570,85 @@ static int short_to_size_t(const struct b_number_p *number, void *value_ptr) /* ++++++++++++++++++ int -> xyz CONVERTER CALLBACKS ++++++++++++++++++ */ -static int int_to_int8(const struct b_number_p *number, void *value_ptr) +static int int_to_int8(const struct fx_number_p *number, void *value_ptr) { *(int8_t *)value_ptr = number->n_value.v_int; return 0; } -static int int_to_int16(const struct b_number_p *number, void *value_ptr) +static int int_to_int16(const struct fx_number_p *number, void *value_ptr) { *(int16_t *)value_ptr = number->n_value.v_int; return 0; } -static int int_to_int32(const struct b_number_p *number, void *value_ptr) +static int int_to_int32(const struct fx_number_p *number, void *value_ptr) { *(int32_t *)value_ptr = number->n_value.v_int; return 0; } -static int int_to_int64(const struct b_number_p *number, void *value_ptr) +static int int_to_int64(const struct fx_number_p *number, void *value_ptr) { *(int64_t *)value_ptr = number->n_value.v_int; return 0; } -static int int_to_float32(const struct b_number_p *number, void *value_ptr) +static int int_to_float32(const struct fx_number_p *number, void *value_ptr) { *(float *)value_ptr = number->n_value.v_int; return 0; } -static int int_to_float64(const struct b_number_p *number, void *value_ptr) +static int int_to_float64(const struct fx_number_p *number, void *value_ptr) { *(double *)value_ptr = number->n_value.v_int; return 0; } -static int int_to_char(const struct b_number_p *number, void *value_ptr) +static int int_to_char(const struct fx_number_p *number, void *value_ptr) { *(char *)value_ptr = number->n_value.v_int; return 0; } -static int int_to_short(const struct b_number_p *number, void *value_ptr) +static int int_to_short(const struct fx_number_p *number, void *value_ptr) { *(short *)value_ptr = number->n_value.v_int; return 0; } -static int int_to_int(const struct b_number_p *number, void *value_ptr) +static int int_to_int(const struct fx_number_p *number, void *value_ptr) { *(int *)value_ptr = number->n_value.v_int; return 0; } -static int int_to_long(const struct b_number_p *number, void *value_ptr) +static int int_to_long(const struct fx_number_p *number, void *value_ptr) { *(long *)value_ptr = number->n_value.v_int; return 0; } -static int int_to_longlong(const struct b_number_p *number, void *value_ptr) +static int int_to_longlong(const struct fx_number_p *number, void *value_ptr) { *(long long *)value_ptr = number->n_value.v_int; return 0; } -static int int_to_float(const struct b_number_p *number, void *value_ptr) +static int int_to_float(const struct fx_number_p *number, void *value_ptr) { *(float *)value_ptr = number->n_value.v_int; return 0; } -static int int_to_double(const struct b_number_p *number, void *value_ptr) +static int int_to_double(const struct fx_number_p *number, void *value_ptr) { *(double *)value_ptr = number->n_value.v_int; return 0; } -static int int_to_size_t(const struct b_number_p *number, void *value_ptr) +static int int_to_size_t(const struct fx_number_p *number, void *value_ptr) { *(size_t *)value_ptr = number->n_value.v_int; return 0; @@ -1656,85 +1656,85 @@ static int int_to_size_t(const struct b_number_p *number, void *value_ptr) /* ++++++++++++++++++ long -> xyz CONVERTER CALLBACKS ++++++++++++++++++ */ -static int long_to_int8(const struct b_number_p *number, void *value_ptr) +static int long_to_int8(const struct fx_number_p *number, void *value_ptr) { *(int8_t *)value_ptr = number->n_value.v_long; return 0; } -static int long_to_int16(const struct b_number_p *number, void *value_ptr) +static int long_to_int16(const struct fx_number_p *number, void *value_ptr) { *(int16_t *)value_ptr = number->n_value.v_long; return 0; } -static int long_to_int32(const struct b_number_p *number, void *value_ptr) +static int long_to_int32(const struct fx_number_p *number, void *value_ptr) { *(int32_t *)value_ptr = number->n_value.v_long; return 0; } -static int long_to_int64(const struct b_number_p *number, void *value_ptr) +static int long_to_int64(const struct fx_number_p *number, void *value_ptr) { *(int64_t *)value_ptr = number->n_value.v_long; return 0; } -static int long_to_float32(const struct b_number_p *number, void *value_ptr) +static int long_to_float32(const struct fx_number_p *number, void *value_ptr) { *(float *)value_ptr = number->n_value.v_long; return 0; } -static int long_to_float64(const struct b_number_p *number, void *value_ptr) +static int long_to_float64(const struct fx_number_p *number, void *value_ptr) { *(double *)value_ptr = number->n_value.v_long; return 0; } -static int long_to_char(const struct b_number_p *number, void *value_ptr) +static int long_to_char(const struct fx_number_p *number, void *value_ptr) { *(char *)value_ptr = number->n_value.v_long; return 0; } -static int long_to_short(const struct b_number_p *number, void *value_ptr) +static int long_to_short(const struct fx_number_p *number, void *value_ptr) { *(short *)value_ptr = number->n_value.v_long; return 0; } -static int long_to_int(const struct b_number_p *number, void *value_ptr) +static int long_to_int(const struct fx_number_p *number, void *value_ptr) { *(int *)value_ptr = number->n_value.v_long; return 0; } -static int long_to_long(const struct b_number_p *number, void *value_ptr) +static int long_to_long(const struct fx_number_p *number, void *value_ptr) { *(long *)value_ptr = number->n_value.v_long; return 0; } -static int long_to_longlong(const struct b_number_p *number, void *value_ptr) +static int long_to_longlong(const struct fx_number_p *number, void *value_ptr) { *(long long *)value_ptr = number->n_value.v_long; return 0; } -static int long_to_float(const struct b_number_p *number, void *value_ptr) +static int long_to_float(const struct fx_number_p *number, void *value_ptr) { *(float *)value_ptr = number->n_value.v_long; return 0; } -static int long_to_double(const struct b_number_p *number, void *value_ptr) +static int long_to_double(const struct fx_number_p *number, void *value_ptr) { *(double *)value_ptr = number->n_value.v_long; return 0; } -static int long_to_size_t(const struct b_number_p *number, void *value_ptr) +static int long_to_size_t(const struct fx_number_p *number, void *value_ptr) { *(size_t *)value_ptr = number->n_value.v_long; return 0; @@ -1742,85 +1742,85 @@ static int long_to_size_t(const struct b_number_p *number, void *value_ptr) /* ++++++++++++++++++ longlong -> xyz CONVERTER CALLBACKS ++++++++++++++++++ */ -static int longlong_to_int8(const struct b_number_p *number, void *value_ptr) +static int longlong_to_int8(const struct fx_number_p *number, void *value_ptr) { *(int8_t *)value_ptr = number->n_value.v_longlong; return 0; } -static int longlong_to_int16(const struct b_number_p *number, void *value_ptr) +static int longlong_to_int16(const struct fx_number_p *number, void *value_ptr) { *(int16_t *)value_ptr = number->n_value.v_longlong; return 0; } -static int longlong_to_int32(const struct b_number_p *number, void *value_ptr) +static int longlong_to_int32(const struct fx_number_p *number, void *value_ptr) { *(int32_t *)value_ptr = number->n_value.v_longlong; return 0; } -static int longlong_to_int64(const struct b_number_p *number, void *value_ptr) +static int longlong_to_int64(const struct fx_number_p *number, void *value_ptr) { *(int64_t *)value_ptr = number->n_value.v_longlong; return 0; } -static int longlong_to_float32(const struct b_number_p *number, void *value_ptr) +static int longlong_to_float32(const struct fx_number_p *number, void *value_ptr) { *(float *)value_ptr = number->n_value.v_longlong; return 0; } -static int longlong_to_float64(const struct b_number_p *number, void *value_ptr) +static int longlong_to_float64(const struct fx_number_p *number, void *value_ptr) { *(double *)value_ptr = number->n_value.v_longlong; return 0; } -static int longlong_to_char(const struct b_number_p *number, void *value_ptr) +static int longlong_to_char(const struct fx_number_p *number, void *value_ptr) { *(char *)value_ptr = number->n_value.v_longlong; return 0; } -static int longlong_to_short(const struct b_number_p *number, void *value_ptr) +static int longlong_to_short(const struct fx_number_p *number, void *value_ptr) { *(short *)value_ptr = number->n_value.v_longlong; return 0; } -static int longlong_to_int(const struct b_number_p *number, void *value_ptr) +static int longlong_to_int(const struct fx_number_p *number, void *value_ptr) { *(int *)value_ptr = number->n_value.v_longlong; return 0; } -static int longlong_to_long(const struct b_number_p *number, void *value_ptr) +static int longlong_to_long(const struct fx_number_p *number, void *value_ptr) { *(long *)value_ptr = number->n_value.v_longlong; return 0; } -static int longlong_to_longlong(const struct b_number_p *number, void *value_ptr) +static int longlong_to_longlong(const struct fx_number_p *number, void *value_ptr) { *(long long *)value_ptr = number->n_value.v_longlong; return 0; } -static int longlong_to_float(const struct b_number_p *number, void *value_ptr) +static int longlong_to_float(const struct fx_number_p *number, void *value_ptr) { *(float *)value_ptr = number->n_value.v_longlong; return 0; } -static int longlong_to_double(const struct b_number_p *number, void *value_ptr) +static int longlong_to_double(const struct fx_number_p *number, void *value_ptr) { *(double *)value_ptr = number->n_value.v_longlong; return 0; } -static int longlong_to_size_t(const struct b_number_p *number, void *value_ptr) +static int longlong_to_size_t(const struct fx_number_p *number, void *value_ptr) { *(size_t *)value_ptr = number->n_value.v_longlong; return 0; @@ -1828,85 +1828,85 @@ static int longlong_to_size_t(const struct b_number_p *number, void *value_ptr) /* ++++++++++++++++++ float -> xyz CONVERTER CALLBACKS ++++++++++++++++++ */ -static int float_to_int8(const struct b_number_p *number, void *value_ptr) +static int float_to_int8(const struct fx_number_p *number, void *value_ptr) { *(int8_t *)value_ptr = number->n_value.v_float; return 0; } -static int float_to_int16(const struct b_number_p *number, void *value_ptr) +static int float_to_int16(const struct fx_number_p *number, void *value_ptr) { *(int16_t *)value_ptr = number->n_value.v_float; return 0; } -static int float_to_int32(const struct b_number_p *number, void *value_ptr) +static int float_to_int32(const struct fx_number_p *number, void *value_ptr) { *(int32_t *)value_ptr = number->n_value.v_float; return 0; } -static int float_to_int64(const struct b_number_p *number, void *value_ptr) +static int float_to_int64(const struct fx_number_p *number, void *value_ptr) { *(int64_t *)value_ptr = number->n_value.v_float; return 0; } -static int float_to_float32(const struct b_number_p *number, void *value_ptr) +static int float_to_float32(const struct fx_number_p *number, void *value_ptr) { *(float *)value_ptr = number->n_value.v_float; return 0; } -static int float_to_float64(const struct b_number_p *number, void *value_ptr) +static int float_to_float64(const struct fx_number_p *number, void *value_ptr) { *(double *)value_ptr = number->n_value.v_float; return 0; } -static int float_to_char(const struct b_number_p *number, void *value_ptr) +static int float_to_char(const struct fx_number_p *number, void *value_ptr) { *(char *)value_ptr = number->n_value.v_float; return 0; } -static int float_to_short(const struct b_number_p *number, void *value_ptr) +static int float_to_short(const struct fx_number_p *number, void *value_ptr) { *(short *)value_ptr = number->n_value.v_float; return 0; } -static int float_to_int(const struct b_number_p *number, void *value_ptr) +static int float_to_int(const struct fx_number_p *number, void *value_ptr) { *(int *)value_ptr = number->n_value.v_float; return 0; } -static int float_to_long(const struct b_number_p *number, void *value_ptr) +static int float_to_long(const struct fx_number_p *number, void *value_ptr) { *(long *)value_ptr = number->n_value.v_float; return 0; } -static int float_to_longlong(const struct b_number_p *number, void *value_ptr) +static int float_to_longlong(const struct fx_number_p *number, void *value_ptr) { *(long long *)value_ptr = number->n_value.v_float; return 0; } -static int float_to_float(const struct b_number_p *number, void *value_ptr) +static int float_to_float(const struct fx_number_p *number, void *value_ptr) { *(float *)value_ptr = number->n_value.v_float; return 0; } -static int float_to_double(const struct b_number_p *number, void *value_ptr) +static int float_to_double(const struct fx_number_p *number, void *value_ptr) { *(double *)value_ptr = number->n_value.v_float; return 0; } -static int float_to_size_t(const struct b_number_p *number, void *value_ptr) +static int float_to_size_t(const struct fx_number_p *number, void *value_ptr) { *(size_t *)value_ptr = number->n_value.v_float; return 0; @@ -1914,85 +1914,85 @@ static int float_to_size_t(const struct b_number_p *number, void *value_ptr) /* ++++++++++++++++++ double -> xyz CONVERTER CALLBACKS ++++++++++++++++++ */ -static int double_to_int8(const struct b_number_p *number, void *value_ptr) +static int double_to_int8(const struct fx_number_p *number, void *value_ptr) { *(int8_t *)value_ptr = number->n_value.v_double; return 0; } -static int double_to_int16(const struct b_number_p *number, void *value_ptr) +static int double_to_int16(const struct fx_number_p *number, void *value_ptr) { *(int16_t *)value_ptr = number->n_value.v_double; return 0; } -static int double_to_int32(const struct b_number_p *number, void *value_ptr) +static int double_to_int32(const struct fx_number_p *number, void *value_ptr) { *(int32_t *)value_ptr = number->n_value.v_double; return 0; } -static int double_to_int64(const struct b_number_p *number, void *value_ptr) +static int double_to_int64(const struct fx_number_p *number, void *value_ptr) { *(int64_t *)value_ptr = number->n_value.v_double; return 0; } -static int double_to_float32(const struct b_number_p *number, void *value_ptr) +static int double_to_float32(const struct fx_number_p *number, void *value_ptr) { *(float *)value_ptr = number->n_value.v_double; return 0; } -static int double_to_float64(const struct b_number_p *number, void *value_ptr) +static int double_to_float64(const struct fx_number_p *number, void *value_ptr) { *(double *)value_ptr = number->n_value.v_double; return 0; } -static int double_to_char(const struct b_number_p *number, void *value_ptr) +static int double_to_char(const struct fx_number_p *number, void *value_ptr) { *(char *)value_ptr = number->n_value.v_double; return 0; } -static int double_to_short(const struct b_number_p *number, void *value_ptr) +static int double_to_short(const struct fx_number_p *number, void *value_ptr) { *(short *)value_ptr = number->n_value.v_double; return 0; } -static int double_to_int(const struct b_number_p *number, void *value_ptr) +static int double_to_int(const struct fx_number_p *number, void *value_ptr) { *(int *)value_ptr = number->n_value.v_double; return 0; } -static int double_to_long(const struct b_number_p *number, void *value_ptr) +static int double_to_long(const struct fx_number_p *number, void *value_ptr) { *(long *)value_ptr = number->n_value.v_double; return 0; } -static int double_to_longlong(const struct b_number_p *number, void *value_ptr) +static int double_to_longlong(const struct fx_number_p *number, void *value_ptr) { *(long long *)value_ptr = number->n_value.v_double; return 0; } -static int double_to_float(const struct b_number_p *number, void *value_ptr) +static int double_to_float(const struct fx_number_p *number, void *value_ptr) { *(float *)value_ptr = number->n_value.v_double; return 0; } -static int double_to_double(const struct b_number_p *number, void *value_ptr) +static int double_to_double(const struct fx_number_p *number, void *value_ptr) { *(double *)value_ptr = number->n_value.v_double; return 0; } -static int double_to_size_t(const struct b_number_p *number, void *value_ptr) +static int double_to_size_t(const struct fx_number_p *number, void *value_ptr) { *(size_t *)value_ptr = number->n_value.v_double; return 0; @@ -2000,327 +2000,327 @@ static int double_to_size_t(const struct b_number_p *number, void *value_ptr) /* ++++++++++++++++++ size_t -> xyz CONVERTER CALLBACKS ++++++++++++++++++ */ -static int size_t_to_int8(const struct b_number_p *number, void *value_ptr) +static int size_t_to_int8(const struct fx_number_p *number, void *value_ptr) { *(int8_t *)value_ptr = number->n_value.v_size_t; return 0; } -static int size_t_to_int16(const struct b_number_p *number, void *value_ptr) +static int size_t_to_int16(const struct fx_number_p *number, void *value_ptr) { *(int16_t *)value_ptr = number->n_value.v_size_t; return 0; } -static int size_t_to_int32(const struct b_number_p *number, void *value_ptr) +static int size_t_to_int32(const struct fx_number_p *number, void *value_ptr) { *(int32_t *)value_ptr = number->n_value.v_size_t; return 0; } -static int size_t_to_int64(const struct b_number_p *number, void *value_ptr) +static int size_t_to_int64(const struct fx_number_p *number, void *value_ptr) { *(int64_t *)value_ptr = number->n_value.v_size_t; return 0; } -static int size_t_to_float32(const struct b_number_p *number, void *value_ptr) +static int size_t_to_float32(const struct fx_number_p *number, void *value_ptr) { *(float *)value_ptr = number->n_value.v_size_t; return 0; } -static int size_t_to_float64(const struct b_number_p *number, void *value_ptr) +static int size_t_to_float64(const struct fx_number_p *number, void *value_ptr) { *(double *)value_ptr = number->n_value.v_size_t; return 0; } -static int size_t_to_char(const struct b_number_p *number, void *value_ptr) +static int size_t_to_char(const struct fx_number_p *number, void *value_ptr) { *(char *)value_ptr = number->n_value.v_size_t; return 0; } -static int size_t_to_short(const struct b_number_p *number, void *value_ptr) +static int size_t_to_short(const struct fx_number_p *number, void *value_ptr) { *(short *)value_ptr = number->n_value.v_size_t; return 0; } -static int size_t_to_int(const struct b_number_p *number, void *value_ptr) +static int size_t_to_int(const struct fx_number_p *number, void *value_ptr) { *(int *)value_ptr = number->n_value.v_size_t; return 0; } -static int size_t_to_long(const struct b_number_p *number, void *value_ptr) +static int size_t_to_long(const struct fx_number_p *number, void *value_ptr) { *(long *)value_ptr = number->n_value.v_size_t; return 0; } -static int size_t_to_longlong(const struct b_number_p *number, void *value_ptr) +static int size_t_to_longlong(const struct fx_number_p *number, void *value_ptr) { *(long long *)value_ptr = number->n_value.v_size_t; return 0; } -static int size_t_to_float(const struct b_number_p *number, void *value_ptr) +static int size_t_to_float(const struct fx_number_p *number, void *value_ptr) { *(float *)value_ptr = number->n_value.v_size_t; return 0; } -static int size_t_to_double(const struct b_number_p *number, void *value_ptr) +static int size_t_to_double(const struct fx_number_p *number, void *value_ptr) { *(double *)value_ptr = number->n_value.v_size_t; return 0; } -static int size_t_to_size_t(const struct b_number_p *number, void *value_ptr) +static int size_t_to_size_t(const struct fx_number_p *number, void *value_ptr) { *(size_t *)value_ptr = number->n_value.v_size_t; return 0; } -static number_converter_t converters[B_NUMBER_TYPE_COUNT][B_NUMBER_TYPE_COUNT] = { +static number_converter_t converters[FX_NUMBER_TYPE_COUNT][FX_NUMBER_TYPE_COUNT] = { /* int8_t -> xyz CONVERTER CALLBACKS */ - [B_NUMBER_INT8] = { - [B_NUMBER_INT8] = int8_to_int8, - [B_NUMBER_INT16] = int8_to_int16, - [B_NUMBER_INT32] = int8_to_int32, - [B_NUMBER_INT64] = int8_to_int64, - [B_NUMBER_FLOAT32] = int8_to_float32, - [B_NUMBER_FLOAT64] = int8_to_float64, - [B_NUMBER_CHAR] = int8_to_char, - [B_NUMBER_SHORT] = int8_to_short, - [B_NUMBER_INT] = int8_to_int, - [B_NUMBER_LONG] = int8_to_long, - [B_NUMBER_LONGLONG] = int8_to_longlong, - [B_NUMBER_FLOAT] = int8_to_float, - [B_NUMBER_DOUBLE] = int8_to_double, - [B_NUMBER_SIZE_T] = int8_to_size_t, + [FX_NUMBER_INT8] = { + [FX_NUMBER_INT8] = int8_to_int8, + [FX_NUMBER_INT16] = int8_to_int16, + [FX_NUMBER_INT32] = int8_to_int32, + [FX_NUMBER_INT64] = int8_to_int64, + [FX_NUMBER_FLOAT32] = int8_to_float32, + [FX_NUMBER_FLOAT64] = int8_to_float64, + [FX_NUMBER_CHAR] = int8_to_char, + [FX_NUMBER_SHORT] = int8_to_short, + [FX_NUMBER_INT] = int8_to_int, + [FX_NUMBER_LONG] = int8_to_long, + [FX_NUMBER_LONGLONG] = int8_to_longlong, + [FX_NUMBER_FLOAT] = int8_to_float, + [FX_NUMBER_DOUBLE] = int8_to_double, + [FX_NUMBER_SIZE_T] = int8_to_size_t, }, /* int16 -> xyz CONVERTER CALLBACKS */ - [B_NUMBER_INT16] = { - [B_NUMBER_INT8] = int16_to_int8, - [B_NUMBER_INT16] = int16_to_int16, - [B_NUMBER_INT32] = int16_to_int32, - [B_NUMBER_INT64] = int16_to_int64, - [B_NUMBER_FLOAT32] = int16_to_float32, - [B_NUMBER_FLOAT64] = int16_to_float64, - [B_NUMBER_CHAR] = int16_to_char, - [B_NUMBER_SHORT] = int16_to_short, - [B_NUMBER_INT] = int16_to_int, - [B_NUMBER_LONG] = int16_to_long, - [B_NUMBER_LONGLONG] = int16_to_longlong, - [B_NUMBER_FLOAT] = int16_to_float, - [B_NUMBER_DOUBLE] = int16_to_double, - [B_NUMBER_SIZE_T] = int16_to_size_t, + [FX_NUMBER_INT16] = { + [FX_NUMBER_INT8] = int16_to_int8, + [FX_NUMBER_INT16] = int16_to_int16, + [FX_NUMBER_INT32] = int16_to_int32, + [FX_NUMBER_INT64] = int16_to_int64, + [FX_NUMBER_FLOAT32] = int16_to_float32, + [FX_NUMBER_FLOAT64] = int16_to_float64, + [FX_NUMBER_CHAR] = int16_to_char, + [FX_NUMBER_SHORT] = int16_to_short, + [FX_NUMBER_INT] = int16_to_int, + [FX_NUMBER_LONG] = int16_to_long, + [FX_NUMBER_LONGLONG] = int16_to_longlong, + [FX_NUMBER_FLOAT] = int16_to_float, + [FX_NUMBER_DOUBLE] = int16_to_double, + [FX_NUMBER_SIZE_T] = int16_to_size_t, }, /* int32 -> xyz CONVERTER CALLBACKS */ - [B_NUMBER_INT32] = { - [B_NUMBER_INT8] = int32_to_int8, - [B_NUMBER_INT16] = int32_to_int16, - [B_NUMBER_INT32] = int32_to_int32, - [B_NUMBER_INT64] = int32_to_int64, - [B_NUMBER_FLOAT32] = int32_to_float32, - [B_NUMBER_FLOAT64] = int32_to_float64, - [B_NUMBER_CHAR] = int32_to_char, - [B_NUMBER_SHORT] = int32_to_short, - [B_NUMBER_INT] = int32_to_int, - [B_NUMBER_LONG] = int32_to_long, - [B_NUMBER_LONGLONG] = int32_to_longlong, - [B_NUMBER_FLOAT] = int32_to_float, - [B_NUMBER_DOUBLE] = int32_to_double, - [B_NUMBER_SIZE_T] = int32_to_size_t, + [FX_NUMBER_INT32] = { + [FX_NUMBER_INT8] = int32_to_int8, + [FX_NUMBER_INT16] = int32_to_int16, + [FX_NUMBER_INT32] = int32_to_int32, + [FX_NUMBER_INT64] = int32_to_int64, + [FX_NUMBER_FLOAT32] = int32_to_float32, + [FX_NUMBER_FLOAT64] = int32_to_float64, + [FX_NUMBER_CHAR] = int32_to_char, + [FX_NUMBER_SHORT] = int32_to_short, + [FX_NUMBER_INT] = int32_to_int, + [FX_NUMBER_LONG] = int32_to_long, + [FX_NUMBER_LONGLONG] = int32_to_longlong, + [FX_NUMBER_FLOAT] = int32_to_float, + [FX_NUMBER_DOUBLE] = int32_to_double, + [FX_NUMBER_SIZE_T] = int32_to_size_t, }, /* int64 -> xyz CONVERTER CALLBACKS */ - [B_NUMBER_INT64] = { - [B_NUMBER_INT8] = int64_to_int8, - [B_NUMBER_INT16] = int64_to_int16, - [B_NUMBER_INT32] = int64_to_int32, - [B_NUMBER_INT64] = int64_to_int64, - [B_NUMBER_FLOAT32] = int64_to_float32, - [B_NUMBER_FLOAT64] = int64_to_float64, - [B_NUMBER_CHAR] = int64_to_char, - [B_NUMBER_SHORT] = int64_to_short, - [B_NUMBER_INT] = int64_to_int, - [B_NUMBER_LONG] = int64_to_long, - [B_NUMBER_LONGLONG] = int64_to_longlong, - [B_NUMBER_FLOAT] = int64_to_float, - [B_NUMBER_DOUBLE] = int64_to_double, - [B_NUMBER_SIZE_T] = int64_to_size_t, + [FX_NUMBER_INT64] = { + [FX_NUMBER_INT8] = int64_to_int8, + [FX_NUMBER_INT16] = int64_to_int16, + [FX_NUMBER_INT32] = int64_to_int32, + [FX_NUMBER_INT64] = int64_to_int64, + [FX_NUMBER_FLOAT32] = int64_to_float32, + [FX_NUMBER_FLOAT64] = int64_to_float64, + [FX_NUMBER_CHAR] = int64_to_char, + [FX_NUMBER_SHORT] = int64_to_short, + [FX_NUMBER_INT] = int64_to_int, + [FX_NUMBER_LONG] = int64_to_long, + [FX_NUMBER_LONGLONG] = int64_to_longlong, + [FX_NUMBER_FLOAT] = int64_to_float, + [FX_NUMBER_DOUBLE] = int64_to_double, + [FX_NUMBER_SIZE_T] = int64_to_size_t, }, /* float32 -> xyz CONVERTER CALLBACKS */ - [B_NUMBER_FLOAT32] = { - [B_NUMBER_INT8] = float32_to_int8, - [B_NUMBER_INT16] = float32_to_int16, - [B_NUMBER_INT32] = float32_to_int32, - [B_NUMBER_INT64] = float32_to_int64, - [B_NUMBER_FLOAT32] = float32_to_float32, - [B_NUMBER_FLOAT64] = float32_to_float64, - [B_NUMBER_CHAR] = float32_to_char, - [B_NUMBER_SHORT] = float32_to_short, - [B_NUMBER_INT] = float32_to_int, - [B_NUMBER_LONG] = float32_to_long, - [B_NUMBER_LONGLONG] = float32_to_longlong, - [B_NUMBER_FLOAT] = float32_to_float, - [B_NUMBER_DOUBLE] = float32_to_double, - [B_NUMBER_SIZE_T] = float32_to_size_t, + [FX_NUMBER_FLOAT32] = { + [FX_NUMBER_INT8] = float32_to_int8, + [FX_NUMBER_INT16] = float32_to_int16, + [FX_NUMBER_INT32] = float32_to_int32, + [FX_NUMBER_INT64] = float32_to_int64, + [FX_NUMBER_FLOAT32] = float32_to_float32, + [FX_NUMBER_FLOAT64] = float32_to_float64, + [FX_NUMBER_CHAR] = float32_to_char, + [FX_NUMBER_SHORT] = float32_to_short, + [FX_NUMBER_INT] = float32_to_int, + [FX_NUMBER_LONG] = float32_to_long, + [FX_NUMBER_LONGLONG] = float32_to_longlong, + [FX_NUMBER_FLOAT] = float32_to_float, + [FX_NUMBER_DOUBLE] = float32_to_double, + [FX_NUMBER_SIZE_T] = float32_to_size_t, }, /* float64 -> xyz CONVERTER CALLBACKS */ - [B_NUMBER_FLOAT64] = { - [B_NUMBER_INT8] = float64_to_int8, - [B_NUMBER_INT16] = float64_to_int16, - [B_NUMBER_INT32] = float64_to_int32, - [B_NUMBER_INT64] = float64_to_int64, - [B_NUMBER_FLOAT32] = float64_to_float32, - [B_NUMBER_FLOAT64] = float64_to_float64, - [B_NUMBER_CHAR] = float64_to_char, - [B_NUMBER_SHORT] = float64_to_short, - [B_NUMBER_INT] = float64_to_int, - [B_NUMBER_LONG] = float64_to_long, - [B_NUMBER_LONGLONG] = float64_to_longlong, - [B_NUMBER_FLOAT] = float64_to_float, - [B_NUMBER_DOUBLE] = float64_to_double, - [B_NUMBER_SIZE_T] = float64_to_size_t, + [FX_NUMBER_FLOAT64] = { + [FX_NUMBER_INT8] = float64_to_int8, + [FX_NUMBER_INT16] = float64_to_int16, + [FX_NUMBER_INT32] = float64_to_int32, + [FX_NUMBER_INT64] = float64_to_int64, + [FX_NUMBER_FLOAT32] = float64_to_float32, + [FX_NUMBER_FLOAT64] = float64_to_float64, + [FX_NUMBER_CHAR] = float64_to_char, + [FX_NUMBER_SHORT] = float64_to_short, + [FX_NUMBER_INT] = float64_to_int, + [FX_NUMBER_LONG] = float64_to_long, + [FX_NUMBER_LONGLONG] = float64_to_longlong, + [FX_NUMBER_FLOAT] = float64_to_float, + [FX_NUMBER_DOUBLE] = float64_to_double, + [FX_NUMBER_SIZE_T] = float64_to_size_t, }, /* char > xyz CONVERTER CALLBACKS */ - [B_NUMBER_CHAR] = { - [B_NUMBER_INT8] = char_to_int8, - [B_NUMBER_INT16] = char_to_int16, - [B_NUMBER_INT32] = char_to_int32, - [B_NUMBER_INT64] = char_to_int64, - [B_NUMBER_FLOAT32] = char_to_float32, - [B_NUMBER_FLOAT64] = char_to_float64, - [B_NUMBER_CHAR] = char_to_char, - [B_NUMBER_SHORT] = char_to_short, - [B_NUMBER_INT] = char_to_int, - [B_NUMBER_LONG] = char_to_long, - [B_NUMBER_LONGLONG] = char_to_longlong, - [B_NUMBER_FLOAT] = char_to_float, - [B_NUMBER_DOUBLE] = char_to_double, - [B_NUMBER_SIZE_T] = char_to_size_t, + [FX_NUMBER_CHAR] = { + [FX_NUMBER_INT8] = char_to_int8, + [FX_NUMBER_INT16] = char_to_int16, + [FX_NUMBER_INT32] = char_to_int32, + [FX_NUMBER_INT64] = char_to_int64, + [FX_NUMBER_FLOAT32] = char_to_float32, + [FX_NUMBER_FLOAT64] = char_to_float64, + [FX_NUMBER_CHAR] = char_to_char, + [FX_NUMBER_SHORT] = char_to_short, + [FX_NUMBER_INT] = char_to_int, + [FX_NUMBER_LONG] = char_to_long, + [FX_NUMBER_LONGLONG] = char_to_longlong, + [FX_NUMBER_FLOAT] = char_to_float, + [FX_NUMBER_DOUBLE] = char_to_double, + [FX_NUMBER_SIZE_T] = char_to_size_t, }, /* short -> xyz CONVERTER CALLBACKS */ - [B_NUMBER_SHORT] = { - [B_NUMBER_INT8] = short_to_int8, - [B_NUMBER_INT16] = short_to_int16, - [B_NUMBER_INT32] = short_to_int32, - [B_NUMBER_INT64] = short_to_int64, - [B_NUMBER_FLOAT32] = short_to_float32, - [B_NUMBER_FLOAT64] = short_to_float64, - [B_NUMBER_CHAR] = short_to_char, - [B_NUMBER_SHORT] = short_to_short, - [B_NUMBER_INT] = short_to_int, - [B_NUMBER_LONG] = short_to_long, - [B_NUMBER_LONGLONG] = short_to_longlong, - [B_NUMBER_FLOAT] = short_to_float, - [B_NUMBER_DOUBLE] = short_to_double, - [B_NUMBER_SIZE_T] = short_to_size_t, + [FX_NUMBER_SHORT] = { + [FX_NUMBER_INT8] = short_to_int8, + [FX_NUMBER_INT16] = short_to_int16, + [FX_NUMBER_INT32] = short_to_int32, + [FX_NUMBER_INT64] = short_to_int64, + [FX_NUMBER_FLOAT32] = short_to_float32, + [FX_NUMBER_FLOAT64] = short_to_float64, + [FX_NUMBER_CHAR] = short_to_char, + [FX_NUMBER_SHORT] = short_to_short, + [FX_NUMBER_INT] = short_to_int, + [FX_NUMBER_LONG] = short_to_long, + [FX_NUMBER_LONGLONG] = short_to_longlong, + [FX_NUMBER_FLOAT] = short_to_float, + [FX_NUMBER_DOUBLE] = short_to_double, + [FX_NUMBER_SIZE_T] = short_to_size_t, }, /* int -> xyz CONVERTER CALLBACKS */ - [B_NUMBER_INT] = { - [B_NUMBER_INT8] = int_to_int8, - [B_NUMBER_INT16] = int_to_int16, - [B_NUMBER_INT32] = int_to_int32, - [B_NUMBER_INT64] = int_to_int64, - [B_NUMBER_FLOAT32] = int_to_float32, - [B_NUMBER_FLOAT64] = int_to_float64, - [B_NUMBER_CHAR] = int_to_char, - [B_NUMBER_SHORT] = int_to_short, - [B_NUMBER_INT] = int_to_int, - [B_NUMBER_LONG] = int_to_long, - [B_NUMBER_LONGLONG] = int_to_longlong, - [B_NUMBER_FLOAT] = int_to_float, - [B_NUMBER_DOUBLE] = int_to_double, - [B_NUMBER_SIZE_T] = int_to_size_t, + [FX_NUMBER_INT] = { + [FX_NUMBER_INT8] = int_to_int8, + [FX_NUMBER_INT16] = int_to_int16, + [FX_NUMBER_INT32] = int_to_int32, + [FX_NUMBER_INT64] = int_to_int64, + [FX_NUMBER_FLOAT32] = int_to_float32, + [FX_NUMBER_FLOAT64] = int_to_float64, + [FX_NUMBER_CHAR] = int_to_char, + [FX_NUMBER_SHORT] = int_to_short, + [FX_NUMBER_INT] = int_to_int, + [FX_NUMBER_LONG] = int_to_long, + [FX_NUMBER_LONGLONG] = int_to_longlong, + [FX_NUMBER_FLOAT] = int_to_float, + [FX_NUMBER_DOUBLE] = int_to_double, + [FX_NUMBER_SIZE_T] = int_to_size_t, }, /* long -> xyz CONVERTER CALLBACKS */ - [B_NUMBER_LONG] = { - [B_NUMBER_INT8] = long_to_int8, - [B_NUMBER_INT16] = long_to_int16, - [B_NUMBER_INT32] = long_to_int32, - [B_NUMBER_INT64] = long_to_int64, - [B_NUMBER_FLOAT32] = long_to_float32, - [B_NUMBER_FLOAT64] = long_to_float64, - [B_NUMBER_CHAR] = long_to_char, - [B_NUMBER_SHORT] = long_to_short, - [B_NUMBER_INT] = long_to_int, - [B_NUMBER_LONG] = long_to_long, - [B_NUMBER_LONGLONG] = long_to_longlong, - [B_NUMBER_FLOAT] = long_to_float, - [B_NUMBER_DOUBLE] = long_to_double, - [B_NUMBER_SIZE_T] = long_to_size_t, + [FX_NUMBER_LONG] = { + [FX_NUMBER_INT8] = long_to_int8, + [FX_NUMBER_INT16] = long_to_int16, + [FX_NUMBER_INT32] = long_to_int32, + [FX_NUMBER_INT64] = long_to_int64, + [FX_NUMBER_FLOAT32] = long_to_float32, + [FX_NUMBER_FLOAT64] = long_to_float64, + [FX_NUMBER_CHAR] = long_to_char, + [FX_NUMBER_SHORT] = long_to_short, + [FX_NUMBER_INT] = long_to_int, + [FX_NUMBER_LONG] = long_to_long, + [FX_NUMBER_LONGLONG] = long_to_longlong, + [FX_NUMBER_FLOAT] = long_to_float, + [FX_NUMBER_DOUBLE] = long_to_double, + [FX_NUMBER_SIZE_T] = long_to_size_t, }, /* longlong -> xyz CONVERTER CALLBACKS */ - [B_NUMBER_LONGLONG] = { - [B_NUMBER_INT8] = longlong_to_int8, - [B_NUMBER_INT16] = longlong_to_int16, - [B_NUMBER_INT32] = longlong_to_int32, - [B_NUMBER_INT64] = longlong_to_int64, - [B_NUMBER_FLOAT32] = longlong_to_float32, - [B_NUMBER_FLOAT64] = longlong_to_float64, - [B_NUMBER_CHAR] = longlong_to_char, - [B_NUMBER_SHORT] = longlong_to_short, - [B_NUMBER_INT] = longlong_to_int, - [B_NUMBER_LONG] = longlong_to_long, - [B_NUMBER_LONGLONG] = longlong_to_longlong, - [B_NUMBER_FLOAT] = longlong_to_float, - [B_NUMBER_DOUBLE] = longlong_to_double, - [B_NUMBER_SIZE_T] = longlong_to_size_t, + [FX_NUMBER_LONGLONG] = { + [FX_NUMBER_INT8] = longlong_to_int8, + [FX_NUMBER_INT16] = longlong_to_int16, + [FX_NUMBER_INT32] = longlong_to_int32, + [FX_NUMBER_INT64] = longlong_to_int64, + [FX_NUMBER_FLOAT32] = longlong_to_float32, + [FX_NUMBER_FLOAT64] = longlong_to_float64, + [FX_NUMBER_CHAR] = longlong_to_char, + [FX_NUMBER_SHORT] = longlong_to_short, + [FX_NUMBER_INT] = longlong_to_int, + [FX_NUMBER_LONG] = longlong_to_long, + [FX_NUMBER_LONGLONG] = longlong_to_longlong, + [FX_NUMBER_FLOAT] = longlong_to_float, + [FX_NUMBER_DOUBLE] = longlong_to_double, + [FX_NUMBER_SIZE_T] = longlong_to_size_t, }, /* float -> xyz CONVERTER CALLBACKS */ - [B_NUMBER_FLOAT] = { - [B_NUMBER_INT8] = float_to_int8, - [B_NUMBER_INT16] = float_to_int16, - [B_NUMBER_INT32] = float_to_int32, - [B_NUMBER_INT64] = float_to_int64, - [B_NUMBER_FLOAT32] = float_to_float32, - [B_NUMBER_FLOAT64] = float_to_float64, - [B_NUMBER_CHAR] = float_to_char, - [B_NUMBER_SHORT] = float_to_short, - [B_NUMBER_INT] = float_to_int, - [B_NUMBER_LONG] = float_to_long, - [B_NUMBER_LONGLONG] = float_to_longlong, - [B_NUMBER_FLOAT] = float_to_float, - [B_NUMBER_DOUBLE] = float_to_double, - [B_NUMBER_SIZE_T] = float_to_size_t, + [FX_NUMBER_FLOAT] = { + [FX_NUMBER_INT8] = float_to_int8, + [FX_NUMBER_INT16] = float_to_int16, + [FX_NUMBER_INT32] = float_to_int32, + [FX_NUMBER_INT64] = float_to_int64, + [FX_NUMBER_FLOAT32] = float_to_float32, + [FX_NUMBER_FLOAT64] = float_to_float64, + [FX_NUMBER_CHAR] = float_to_char, + [FX_NUMBER_SHORT] = float_to_short, + [FX_NUMBER_INT] = float_to_int, + [FX_NUMBER_LONG] = float_to_long, + [FX_NUMBER_LONGLONG] = float_to_longlong, + [FX_NUMBER_FLOAT] = float_to_float, + [FX_NUMBER_DOUBLE] = float_to_double, + [FX_NUMBER_SIZE_T] = float_to_size_t, }, /* double -> xyz CONVERTER CALLBACKS */ - [B_NUMBER_DOUBLE] = { - [B_NUMBER_INT8] = double_to_int8, - [B_NUMBER_INT16] = double_to_int16, - [B_NUMBER_INT32] = double_to_int32, - [B_NUMBER_INT64] = double_to_int64, - [B_NUMBER_FLOAT32] = double_to_float32, - [B_NUMBER_FLOAT64] = double_to_float64, - [B_NUMBER_CHAR] = double_to_char, - [B_NUMBER_SHORT] = double_to_short, - [B_NUMBER_INT] = double_to_int, - [B_NUMBER_LONG] = double_to_long, - [B_NUMBER_LONGLONG] = double_to_longlong, - [B_NUMBER_FLOAT] = double_to_float, - [B_NUMBER_DOUBLE] = double_to_double, - [B_NUMBER_SIZE_T] = double_to_size_t, + [FX_NUMBER_DOUBLE] = { + [FX_NUMBER_INT8] = double_to_int8, + [FX_NUMBER_INT16] = double_to_int16, + [FX_NUMBER_INT32] = double_to_int32, + [FX_NUMBER_INT64] = double_to_int64, + [FX_NUMBER_FLOAT32] = double_to_float32, + [FX_NUMBER_FLOAT64] = double_to_float64, + [FX_NUMBER_CHAR] = double_to_char, + [FX_NUMBER_SHORT] = double_to_short, + [FX_NUMBER_INT] = double_to_int, + [FX_NUMBER_LONG] = double_to_long, + [FX_NUMBER_LONGLONG] = double_to_longlong, + [FX_NUMBER_FLOAT] = double_to_float, + [FX_NUMBER_DOUBLE] = double_to_double, + [FX_NUMBER_SIZE_T] = double_to_size_t, }, /* size_t -> xyz CONVERTER CALLBACKS */ - [B_NUMBER_SIZE_T] = { - [B_NUMBER_INT8] = size_t_to_int8, - [B_NUMBER_INT16] = size_t_to_int16, - [B_NUMBER_INT32] = size_t_to_int32, - [B_NUMBER_INT64] = size_t_to_int64, - [B_NUMBER_FLOAT32] = size_t_to_float32, - [B_NUMBER_FLOAT64] = size_t_to_float64, - [B_NUMBER_CHAR] = size_t_to_char, - [B_NUMBER_SHORT] = size_t_to_short, - [B_NUMBER_INT] = size_t_to_int, - [B_NUMBER_LONG] = size_t_to_long, - [B_NUMBER_LONGLONG] = size_t_to_longlong, - [B_NUMBER_FLOAT] = size_t_to_float, - [B_NUMBER_DOUBLE] = size_t_to_double, - [B_NUMBER_SIZE_T] = size_t_to_size_t, + [FX_NUMBER_SIZE_T] = { + [FX_NUMBER_INT8] = size_t_to_int8, + [FX_NUMBER_INT16] = size_t_to_int16, + [FX_NUMBER_INT32] = size_t_to_int32, + [FX_NUMBER_INT64] = size_t_to_int64, + [FX_NUMBER_FLOAT32] = size_t_to_float32, + [FX_NUMBER_FLOAT64] = size_t_to_float64, + [FX_NUMBER_CHAR] = size_t_to_char, + [FX_NUMBER_SHORT] = size_t_to_short, + [FX_NUMBER_INT] = size_t_to_int, + [FX_NUMBER_LONG] = size_t_to_long, + [FX_NUMBER_LONGLONG] = size_t_to_longlong, + [FX_NUMBER_FLOAT] = size_t_to_float, + [FX_NUMBER_DOUBLE] = size_t_to_double, + [FX_NUMBER_SIZE_T] = size_t_to_size_t, }, }; diff --git a/ds/string.c b/ds/string.c index 190964b..d29c190 100644 --- a/ds/string.c +++ b/ds/string.c @@ -1,6 +1,6 @@ -#include -#include -#include +#include +#include +#include #include #include #include @@ -25,7 +25,7 @@ enum iterator_mode { ITERATOR_MODE_TOKENS, }; -struct b_string_p { +struct fx_string_p { /* length of string in bytes, not including null-terminator. * a multi-byte utf-8 codepoint will be counted as multiple bytes here */ unsigned int s_len; @@ -41,10 +41,10 @@ struct b_string_p { } s_data; }; -struct b_string_iterator_p { +struct fx_string_iterator_p { int _m, _f; - b_string *_tmp; - struct b_string_p *_s_p, *_tmp_p; + fx_string *_tmp; + struct fx_string_p *_s_p, *_tmp_p; const char **_d; size_t _nd, _ds; @@ -52,7 +52,7 @@ struct b_string_iterator_p { size_t iteration_index; size_t byte_index; size_t codepoint_index; - b_wchar char_value; + fx_wchar char_value; const char *string_value; size_t string_length; size_t string_codepoints; @@ -60,13 +60,13 @@ struct b_string_iterator_p { /*** PRIVATE FUNCTIONS ********************************************************/ -static bool string_is_inline(const struct b_string_p *str) +static bool string_is_inline(const struct fx_string_p *str) { /* strings cannot go below STRING_INLINE_CAPACITY capacity */ return str->s_max == STRING_INLINE_CAPACITY; } -static char *string_ptr(const struct b_string_p *str) +static char *string_ptr(const struct fx_string_p *str) { if (string_is_inline(str)) { return (char *)str->s_data.d_inline; @@ -75,8 +75,8 @@ static char *string_ptr(const struct b_string_p *str) return str->s_data.d_external; } -static enum b_status convert_codepoint_range_to_byte_range( - const struct b_string_p *str, size_t cp_start, size_t cp_length, +static enum fx_status convert_codepoint_range_to_byte_range( + const struct fx_string_p *str, size_t cp_start, size_t cp_length, size_t *out_byte_start, size_t *out_byte_length) { const char *s = string_ptr(str); @@ -86,13 +86,13 @@ static enum b_status convert_codepoint_range_to_byte_range( const char *cp = &s[byte_offset]; if (!cp || byte_offset >= str->s_len) { /* out of range */ - return B_ERR_OUT_OF_BOUNDS; + return FX_ERR_OUT_OF_BOUNDS; } - size_t stride = b_wchar_utf8_codepoint_stride(cp); + size_t stride = fx_wchar_utf8_codepoint_stride(cp); if (!stride) { /* invalid codepoint */ - return B_ERR_BAD_STATE; + return FX_ERR_BAD_STATE; } byte_offset += stride; @@ -103,13 +103,13 @@ static enum b_status convert_codepoint_range_to_byte_range( const char *cp = &s[cp_offset]; if (!cp || (cp_offset >= str->s_len)) { /* out of range */ - return B_ERR_OUT_OF_BOUNDS; + return FX_ERR_OUT_OF_BOUNDS; } - size_t stride = b_wchar_utf8_codepoint_stride(cp); + size_t stride = fx_wchar_utf8_codepoint_stride(cp); if (!stride) { /* invalid codepoint */ - return B_ERR_BAD_STATE; + return FX_ERR_BAD_STATE; } byte_length += stride; @@ -123,10 +123,10 @@ static enum b_status convert_codepoint_range_to_byte_range( *out_byte_length = byte_length; } - return B_SUCCESS; + return FX_SUCCESS; } -static char *get_next_codepoint(struct b_string_p *str, char *this_codepoint) +static char *get_next_codepoint(struct fx_string_p *str, char *this_codepoint) { char c = *this_codepoint; char *end = this_codepoint - 1; @@ -146,7 +146,7 @@ static char *get_next_codepoint(struct b_string_p *str, char *this_codepoint) return this_codepoint + len; } -static char *get_previous_codepoint(struct b_string_p *str, char *this_codepoint) +static char *get_previous_codepoint(struct fx_string_p *str, char *this_codepoint) { char *start = string_ptr(str); char *end = this_codepoint - 1; @@ -178,7 +178,7 @@ static char *get_previous_codepoint(struct b_string_p *str, char *this_codepoint return NULL; } -static char *get_last_codepoint(struct b_string_p *str) +static char *get_last_codepoint(struct fx_string_p *str) { if (str->s_len == 0) { return NULL; @@ -187,7 +187,7 @@ static char *get_last_codepoint(struct b_string_p *str) return get_previous_codepoint(str, string_ptr(str) + str->s_len); } -static int string_make_inline(struct b_string_p *str) +static int string_make_inline(struct fx_string_p *str) { char *buffer = string_ptr(str); memcpy(str->s_data.d_inline, buffer, sizeof str->s_data.d_inline); @@ -203,7 +203,7 @@ static int string_make_inline(struct b_string_p *str) return 0; } -static int string_resize_large(struct b_string_p *str, size_t capacity) +static int string_resize_large(struct fx_string_p *str, size_t capacity) { char *buffer = string_ptr(str); char *new_buffer = realloc(buffer, capacity + 1); @@ -216,7 +216,7 @@ static int string_resize_large(struct b_string_p *str, size_t capacity) return 0; } -static int string_make_large(struct b_string_p *str, size_t capacity) +static int string_make_large(struct fx_string_p *str, size_t capacity) { const char *old_buffer = string_ptr(str); char *buffer = malloc(capacity + 1); @@ -232,7 +232,7 @@ static int string_make_large(struct b_string_p *str, size_t capacity) return 0; } -static int string_change_capacity(struct b_string_p *str, size_t capacity) +static int string_change_capacity(struct fx_string_p *str, size_t capacity) { size_t old_capacity = str->s_max; @@ -268,15 +268,15 @@ static int string_change_capacity(struct b_string_p *str, size_t capacity) return 0; } -static b_string *string_duplicate(const struct b_string_p *str) +static fx_string *string_duplicate(const struct fx_string_p *str) { - b_string *new_str = b_string_create(); + fx_string *new_str = fx_string_create(); if (!str) { return NULL; } - struct b_string_p *new_str_p - = b_object_get_private(new_str, B_TYPE_STRING); + struct fx_string_p *new_str_p + = fx_object_get_private(new_str, FX_TYPE_STRING); string_change_capacity(new_str_p, str->s_len); const char *src = string_ptr(str); @@ -289,7 +289,7 @@ static b_string *string_duplicate(const struct b_string_p *str) return new_str; } -static char *string_steal(struct b_string_p *str) +static char *string_steal(struct fx_string_p *str) { char *dest = NULL; char *src = string_ptr(str); @@ -310,25 +310,25 @@ static char *string_steal(struct b_string_p *str) return dest; } -static b_status string_reserve(struct b_string_p *str, size_t capacity) +static fx_status string_reserve(struct fx_string_p *str, size_t capacity) { if (str->s_max >= capacity) { - return B_SUCCESS; + return FX_SUCCESS; } int err = string_change_capacity(str, capacity); - return err == 0 ? B_SUCCESS : B_ERR_NO_MEMORY; + return err == 0 ? FX_SUCCESS : FX_ERR_NO_MEMORY; } -static enum b_status replace_ansi( - struct b_string_p *str, size_t start, size_t length, const char *new_data) +static enum fx_status replace_ansi( + struct fx_string_p *str, size_t start, size_t length, const char *new_data) { - b_status status = B_SUCCESS; + fx_status status = FX_SUCCESS; size_t new_data_len = strlen(new_data); if (start >= str->s_len) { - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } if (start + length >= str->s_len) { @@ -340,7 +340,7 @@ static enum b_status replace_ansi( status = string_reserve(str, new_str_len); } - if (!B_OK(status)) { + if (!FX_OK(status)) { return status; } @@ -357,14 +357,14 @@ static enum b_status replace_ansi( str->s_len = new_str_len; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status replace_utf8( - struct b_string_p *str, size_t start, size_t length, const char *new_data) +static enum fx_status replace_utf8( + struct fx_string_p *str, size_t start, size_t length, const char *new_data) { if (start >= str->s_codepoints) { - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } if (start + length >= str->s_codepoints) { @@ -373,17 +373,17 @@ static enum b_status replace_utf8( size_t new_data_nr_bytes = strlen(new_data); size_t new_data_nr_codepoints - = b_wchar_utf8_codepoint_count(new_data, new_data_nr_bytes); + = fx_wchar_utf8_codepoint_count(new_data, new_data_nr_bytes); if (new_data_nr_codepoints == 0) { /* new_data is not a valid utf-8 string */ - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } size_t old_data_offset = 0, old_data_nr_bytes = 0; size_t old_data_nr_codepoints = length; - enum b_status status = convert_codepoint_range_to_byte_range( + enum fx_status status = convert_codepoint_range_to_byte_range( str, start, length, &old_data_offset, &old_data_nr_bytes); - if (!B_OK(status)) { + if (!FX_OK(status)) { return status; } @@ -392,7 +392,7 @@ static enum b_status replace_utf8( status = string_reserve(str, new_total_bytes); } - if (!B_OK(status)) { + if (!FX_OK(status)) { return status; } @@ -411,11 +411,11 @@ static enum b_status replace_utf8( str->s_codepoints -= old_data_nr_codepoints; str->s_codepoints += new_data_nr_codepoints; - return B_SUCCESS; + return FX_SUCCESS; } -static b_status string_replace( - struct b_string_p *str, size_t start, size_t length, const char *new_data) +static fx_status string_replace( + struct fx_string_p *str, size_t start, size_t length, const char *new_data) { if (str->s_len == str->s_codepoints) { return replace_ansi(str, start, length, new_data); @@ -424,7 +424,7 @@ static b_status string_replace( return replace_utf8(str, start, length, new_data); } -static b_status string_replace_all(struct b_string_p *str, const char *new_data) +static fx_status string_replace_all(struct fx_string_p *str, const char *new_data) { size_t new_len = strlen(new_data); string_reserve(str, new_len); @@ -433,30 +433,30 @@ static b_status string_replace_all(struct b_string_p *str, const char *new_data) dest[new_len] = '\0'; str->s_len = new_len; - return B_SUCCESS; + return FX_SUCCESS; } -static b_status string_replace_all_with_stringstream( - struct b_string_p *str, const b_stringstream *new_data) +static fx_status string_replace_all_with_stringstream( + struct fx_string_p *str, const fx_stringstream *new_data) { - size_t new_len = b_stringstream_get_length(new_data); + size_t new_len = fx_stringstream_get_length(new_data); string_reserve(str, new_len); char *dest = string_ptr(str); - memcpy(dest, b_stringstream_ptr(new_data), new_len); + memcpy(dest, fx_stringstream_ptr(new_data), new_len); dest[new_len] = '\0'; str->s_len = new_len; - str->s_codepoints = b_wchar_utf8_codepoint_count(dest, new_len); + str->s_codepoints = fx_wchar_utf8_codepoint_count(dest, new_len); - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status remove_ansi(struct b_string_p *str, size_t start, size_t length) +static enum fx_status remove_ansi(struct fx_string_p *str, size_t start, size_t length) { - b_status status = B_SUCCESS; + fx_status status = FX_SUCCESS; if (start >= str->s_len) { - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } if (start + length >= str->s_len) { @@ -476,15 +476,15 @@ static enum b_status remove_ansi(struct b_string_p *str, size_t start, size_t le str->s_len = new_str_len; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status remove_utf8(struct b_string_p *str, size_t start, size_t length) +static enum fx_status remove_utf8(struct fx_string_p *str, size_t start, size_t length) { size_t remove_offset = 0, remove_nr_bytes = 0; - enum b_status status = convert_codepoint_range_to_byte_range( + enum fx_status status = convert_codepoint_range_to_byte_range( str, start, length, &remove_offset, &remove_nr_bytes); - if (!B_OK(status)) { + if (!FX_OK(status)) { return status; } @@ -502,11 +502,11 @@ static enum b_status remove_utf8(struct b_string_p *str, size_t start, size_t le str->s_len = new_total_bytes; str->s_codepoints -= length; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status string_remove( - struct b_string_p *str, size_t start, size_t length) +static enum fx_status string_remove( + struct fx_string_p *str, size_t start, size_t length) { if (str->s_len == str->s_codepoints) { return remove_ansi(str, start, length); @@ -515,7 +515,7 @@ static enum b_status string_remove( return remove_utf8(str, start, length); } -static b_status string_transform(struct b_string_p *str, int (*transformer)(int)) +static fx_status string_transform(struct fx_string_p *str, int (*transformer)(int)) { char *s = string_ptr(str); for (size_t i = 0; i < str->s_len; i++) { @@ -526,10 +526,10 @@ static b_status string_transform(struct b_string_p *str, int (*transformer)(int) } } - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status trim_ansi(struct b_string_p *str) +static enum fx_status trim_ansi(struct fx_string_p *str) { char *s = string_ptr(str); size_t whitespace_end = 0; @@ -552,18 +552,18 @@ static enum b_status trim_ansi(struct b_string_p *str) } } - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status trim_utf8(struct b_string_p *str) +static enum fx_status trim_utf8(struct fx_string_p *str) { char *s = string_ptr(str); size_t whitespace_end = 0; size_t nr_whitespace_codepoints = 0; for (size_t i = 0; i < str->s_len;) { - b_wchar c = b_wchar_utf8_codepoint_decode(&s[i]); + fx_wchar c = fx_wchar_utf8_codepoint_decode(&s[i]); - if (!b_wchar_is_space(s[i])) { + if (!fx_wchar_is_space(s[i])) { whitespace_end = i; break; } @@ -577,14 +577,14 @@ static enum b_status trim_utf8(struct b_string_p *str) char *p = get_last_codepoint(str); if (!p) { - return B_ERR_BAD_STATE; + return FX_ERR_BAD_STATE; } for (long i = str->s_len - 1; i >= 0;) { - b_wchar c = b_wchar_utf8_codepoint_decode(p); - size_t c_size = b_wchar_utf8_codepoint_size(c); + fx_wchar c = fx_wchar_utf8_codepoint_decode(p); + size_t c_size = fx_wchar_utf8_codepoint_size(c); - if (b_wchar_is_space(c)) { + if (fx_wchar_is_space(c)) { memset(p, 0, c_size); str->s_len -= c_size; str->s_codepoints--; @@ -595,13 +595,13 @@ static enum b_status trim_utf8(struct b_string_p *str) p = get_previous_codepoint(str, p); } - return B_SUCCESS; + return FX_SUCCESS; } -static b_status string_trim(struct b_string_p *str) +static fx_status string_trim(struct fx_string_p *str) { if (str->s_len == 0) { - return B_SUCCESS; + return FX_SUCCESS; } if (str->s_len == str->s_codepoints) { @@ -611,8 +611,8 @@ static b_status string_trim(struct b_string_p *str) return trim_utf8(str); } -static enum b_status string_insert_cstr_ansi( - struct b_string_p *dest, const char *src, size_t nr_bytes, size_t at) +static enum fx_status string_insert_cstr_ansi( + struct fx_string_p *dest, const char *src, size_t nr_bytes, size_t at) { if (at >= dest->s_len) { at = dest->s_len; @@ -633,11 +633,11 @@ static enum b_status string_insert_cstr_ansi( dest->s_len = new_size; dest->s_codepoints += nr_bytes; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status string_insert_cstr_utf8( - struct b_string_p *dest, const char *src, size_t nr_bytes, +static enum fx_status string_insert_cstr_utf8( + struct fx_string_p *dest, const char *src, size_t nr_bytes, size_t codepoint_offset) { if (codepoint_offset >= dest->s_codepoints) { @@ -645,7 +645,7 @@ static enum b_status string_insert_cstr_utf8( } size_t byte_offset = 0; - enum b_status status = B_SUCCESS; + enum fx_status status = FX_SUCCESS; if (codepoint_offset == dest->s_codepoints) { byte_offset = dest->s_len; @@ -654,7 +654,7 @@ static enum b_status string_insert_cstr_utf8( dest, 0, codepoint_offset, NULL, &byte_offset); } - if (!B_OK(status)) { + if (!FX_OK(status)) { return status; } @@ -672,22 +672,22 @@ static enum b_status string_insert_cstr_utf8( dest_buf[new_total_bytes] = '\0'; dest->s_len += nr_bytes; - dest->s_codepoints += b_wchar_utf8_codepoint_count(src, nr_bytes); + dest->s_codepoints += fx_wchar_utf8_codepoint_count(src, nr_bytes); - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status string_insert_wstr_ansi( - struct b_string_p *dest, const b_wchar *src, size_t nr_codepoints, size_t at) +static enum fx_status string_insert_wstr_ansi( + struct fx_string_p *dest, const fx_wchar *src, size_t nr_codepoints, size_t at) { if (at >= dest->s_len) { at = dest->s_len; } size_t utf8_encoded_size - = b_wchar_utf8_string_encoded_size(src, nr_codepoints); + = fx_wchar_utf8_string_encoded_size(src, nr_codepoints); if (utf8_encoded_size == 0) { - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } size_t new_total_bytes = dest->s_len + utf8_encoded_size; @@ -703,11 +703,11 @@ static enum b_status string_insert_wstr_ansi( char *ptr = dest_buf + at; for (size_t i = 0; i < nr_codepoints; i++) { char c[4]; - size_t c_len = b_wchar_utf8_codepoint_encode(src[i], c); + size_t c_len = fx_wchar_utf8_codepoint_encode(src[i], c); if (c_len == 0) { /* the input string was already checked by * get_utf8_encoded_size, so this should never happen */ - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } memcpy(ptr, c, c_len); @@ -719,11 +719,11 @@ static enum b_status string_insert_wstr_ansi( dest->s_len += utf8_encoded_size; dest->s_codepoints += nr_codepoints; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status string_insert_wstr_utf8( - struct b_string_p *dest, const b_wchar *src, size_t nr_codepoints, +static enum fx_status string_insert_wstr_utf8( + struct fx_string_p *dest, const fx_wchar *src, size_t nr_codepoints, size_t codepoint_offset) { if (codepoint_offset >= dest->s_codepoints) { @@ -731,9 +731,9 @@ static enum b_status string_insert_wstr_utf8( } size_t utf8_encoded_size - = b_wchar_utf8_string_encoded_size(src, nr_codepoints); + = fx_wchar_utf8_string_encoded_size(src, nr_codepoints); if (utf8_encoded_size == 0) { - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } size_t new_total_bytes = dest->s_len + utf8_encoded_size; @@ -742,7 +742,7 @@ static enum b_status string_insert_wstr_utf8( } size_t move_offset = 0; - enum b_status status = B_SUCCESS; + enum fx_status status = FX_SUCCESS; if (codepoint_offset == dest->s_codepoints) { move_offset = dest->s_len; @@ -751,7 +751,7 @@ static enum b_status string_insert_wstr_utf8( dest, 0, codepoint_offset, NULL, &move_offset); } - if (!B_OK(status)) { + if (!FX_OK(status)) { return status; } @@ -763,11 +763,11 @@ static enum b_status string_insert_wstr_utf8( char *ptr = dest_buf + move_offset; for (size_t i = 0; i < nr_codepoints; i++) { char c[4]; - size_t c_len = b_wchar_utf8_codepoint_encode(src[i], c); + size_t c_len = fx_wchar_utf8_codepoint_encode(src[i], c); if (c_len == 0) { /* the input string was already checked by * get_utf8_encoded_size, so this should never happen */ - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } memcpy(ptr, c, c_len); @@ -779,11 +779,11 @@ static enum b_status string_insert_wstr_utf8( dest->s_len += utf8_encoded_size; dest->s_codepoints += nr_codepoints; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status string_insert_cstr( - struct b_string_p *dest, const char *src, size_t nr_bytes, size_t at) +static enum fx_status string_insert_cstr( + struct fx_string_p *dest, const char *src, size_t nr_bytes, size_t at) { if (dest->s_len == dest->s_codepoints) { return string_insert_cstr_ansi(dest, src, nr_bytes, at); @@ -792,8 +792,8 @@ static enum b_status string_insert_cstr( return string_insert_cstr_utf8(dest, src, nr_bytes, at); } -static enum b_status string_insert_wstr( - struct b_string_p *dest, const b_wchar *src, size_t nr_codepoints, size_t at) +static enum fx_status string_insert_wstr( + struct fx_string_p *dest, const fx_wchar *src, size_t nr_codepoints, size_t at) { if (dest->s_len == dest->s_codepoints) { return string_insert_wstr_ansi(dest, src, nr_codepoints, at); @@ -802,31 +802,31 @@ static enum b_status string_insert_wstr( return string_insert_wstr_utf8(dest, src, nr_codepoints, at); } -static enum b_status string_insertf( - struct b_string_p *dest, size_t at, const char *format, va_list arg) +static enum fx_status string_insertf( + struct fx_string_p *dest, size_t at, const char *format, va_list arg) { char buf[1024]; size_t len = vsnprintf(buf, sizeof buf, format, arg); return string_insert_cstr(dest, buf, len, at); } -static enum b_status string_insert_c(struct b_string_p *dest, char c, size_t at) +static enum fx_status string_insert_c(struct fx_string_p *dest, char c, size_t at) { return string_insert_cstr(dest, &c, 1, at); } -static enum b_status string_insert_wc(struct b_string_p *dest, b_wchar c, size_t at) +static enum fx_status string_insert_wc(struct fx_string_p *dest, fx_wchar c, size_t at) { return string_insert_wstr(dest, &c, 1, at); } -static enum b_status string_insert_s( - struct b_string_p *dest, const struct b_string_p *src, size_t at) +static enum fx_status string_insert_s( + struct fx_string_p *dest, const struct fx_string_p *src, size_t at) { return string_insert_cstr(dest, string_ptr(src), src->s_len, at); } -static void string_clear(struct b_string_p *str) +static void string_clear(struct fx_string_p *str) { if (str->s_len == 0) { return; @@ -871,16 +871,16 @@ static bool has_prefixes( return false; } -static enum b_status find_next_token(struct b_string_iterator_p *it) +static enum fx_status find_next_token(struct fx_string_iterator_p *it) { size_t offset = it->_ds; size_t prefix_len = 0; char *start = string_ptr(it->_s_p); bool found_delim_last_time = (it->_f & STRING_TOK_F_FOUND_DELIM) != 0; bool found_delim = false; - bool include_empty = (it->_f & B_STRING_TOK_F_INCLUDE_EMPTY_TOKENS); + bool include_empty = (it->_f & FX_STRING_TOK_F_INCLUDE_EMPTY_TOKENS); bool found_null = false; - b_string_clear(it->_tmp); + fx_string_clear(it->_tmp); while (1) { char *s = start + offset; @@ -902,13 +902,13 @@ static enum b_status find_next_token(struct b_string_iterator_p *it) break; } - b_wchar c = b_wchar_utf8_codepoint_decode(s); - if (c == B_WCHAR_INVALID) { - return B_ERR_BAD_STATE; + fx_wchar c = fx_wchar_utf8_codepoint_decode(s); + if (c == FX_WCHAR_INVALID) { + return FX_ERR_BAD_STATE; } - b_string_append_wc(it->_tmp, c); - offset += b_wchar_utf8_codepoint_size(c); + fx_string_append_wc(it->_tmp, c); + offset += fx_wchar_utf8_codepoint_size(c); if (offset > it->_s_p->s_len) { break; @@ -925,32 +925,32 @@ static enum b_status find_next_token(struct b_string_iterator_p *it) it->string_value = NULL; it->string_length = 0; it->string_codepoints = 0; - return B_ERR_NO_DATA; + return FX_ERR_NO_DATA; } it->_ds = offset + prefix_len; - it->string_value = b_string_ptr(it->_tmp); + it->string_value = fx_string_ptr(it->_tmp); it->string_length = it->_tmp_p->s_len; it->string_codepoints = it->_tmp_p->s_codepoints; - return B_SUCCESS; + return FX_SUCCESS; } -static b_iterator *string_tokenise( - struct b_string_p *str, const char *delims[], size_t nr_delims, - b_string_tokenise_flags flags) +static fx_iterator *string_tokenise( + struct fx_string_p *str, const char *delims[], size_t nr_delims, + fx_string_tokenise_flags flags) { if (!nr_delims) { return NULL; } - b_string *tmp = b_string_create(); + fx_string *tmp = fx_string_create(); if (!tmp) { return NULL; } - b_string_iterator *it_obj = b_object_create(B_TYPE_STRING_ITERATOR); - struct b_string_iterator_p *it - = b_object_get_private(it_obj, B_TYPE_STRING_ITERATOR); + fx_string_iterator *it_obj = fx_object_create(FX_TYPE_STRING_ITERATOR); + struct fx_string_iterator_p *it + = fx_object_get_private(it_obj, FX_TYPE_STRING_ITERATOR); it->_m = ITERATOR_MODE_TOKENS; it->_d = delims; @@ -958,11 +958,11 @@ static b_iterator *string_tokenise( it->_s_p = str; it->_f = flags; it->_tmp = tmp; - it->_tmp_p = b_object_get_private(tmp, B_TYPE_STRING); + it->_tmp_p = fx_object_get_private(tmp, FX_TYPE_STRING); - enum b_status status = find_next_token(it); - if (!B_OK(status)) { - b_string_unref(tmp); + enum fx_status status = find_next_token(it); + if (!FX_OK(status)) { + fx_string_unref(tmp); it->_tmp = NULL; it->_tmp_p = NULL; } @@ -970,24 +970,24 @@ static b_iterator *string_tokenise( return it_obj; } -static size_t string_get_size(const struct b_string_p *str, b_strlen_flags flags) +static size_t string_get_size(const struct fx_string_p *str, fx_strlen_flags flags) { switch (flags) { - case B_STRLEN_NORMAL: + case FX_STRLEN_NORMAL: return str->s_len; - case B_STRLEN_CODEPOINTS: + case FX_STRLEN_CODEPOINTS: return str->s_codepoints; default: - return b_strlen(string_ptr(str), flags); + return fx_strlen(string_ptr(str), flags); } } -static size_t string_get_capacity(const struct b_string_p *str) +static size_t string_get_capacity(const struct fx_string_p *str) { return str->s_max; } -static bool string_compare(const struct b_string_p *a, const struct b_string_p *b) +static bool string_compare(const struct fx_string_p *a, const struct fx_string_p *b) { if (a->s_len != b->s_len) { return false; @@ -1009,7 +1009,7 @@ static bool string_compare(const struct b_string_p *a, const struct b_string_p * return true; } -static char string_front(const struct b_string_p *str) +static char string_front(const struct fx_string_p *str) { if (str->s_len == 0) { return 0; @@ -1019,7 +1019,7 @@ static char string_front(const struct b_string_p *str) return s[0]; } -static char string_back(const struct b_string_p *str) +static char string_back(const struct fx_string_p *str) { if (str->s_len == 0) { return 0; @@ -1029,7 +1029,7 @@ static char string_back(const struct b_string_p *str) return s[str->s_len - 1]; } -static void string_pop_back(struct b_string_p *str) +static void string_pop_back(struct fx_string_p *str) { if (str->s_len == 0) { return; @@ -1041,18 +1041,18 @@ static void string_pop_back(struct b_string_p *str) str->s_len--; } -static b_string *string_substr(const struct b_string_p *str, size_t start, size_t len) +static fx_string *string_substr(const struct fx_string_p *str, size_t start, size_t len) { - if (start > string_get_size(str, B_STRLEN_NORMAL)) { + if (start > string_get_size(str, FX_STRLEN_NORMAL)) { return NULL; } - if (start + len > string_get_size(str, B_STRLEN_NORMAL)) { - len = string_get_size(str, B_STRLEN_NORMAL) - start; + if (start + len > string_get_size(str, FX_STRLEN_NORMAL)) { + len = string_get_size(str, FX_STRLEN_NORMAL) - start; } - b_string *newstr = b_string_create(); - struct b_string_p *newstr_p = b_object_get_private(newstr, B_TYPE_STRING); + fx_string *newstr = fx_string_create(); + struct fx_string_p *newstr_p = fx_object_get_private(newstr, FX_TYPE_STRING); string_reserve(newstr_p, len); const char *src = string_ptr(str) + start; @@ -1064,7 +1064,7 @@ static b_string *string_substr(const struct b_string_p *str, size_t start, size_ return newstr; } -static uint64_t string_hash(const struct b_string_p *str) +static uint64_t string_hash(const struct fx_string_p *str) { #define FNV1_OFFSET_BASIS 0xcbf29ce484222325 #define FNV1_PRIME 0x100000001b3 @@ -1083,9 +1083,9 @@ static uint64_t string_hash(const struct b_string_p *str) /*** PUBLIC FUNCTIONS *********************************************************/ -b_string *b_string_create_from_cstr(const char *s) +fx_string *fx_string_create_from_cstr(const char *s) { - b_string *str = b_string_create(); + fx_string *str = fx_string_create(); if (!str) { return NULL; } @@ -1094,11 +1094,11 @@ b_string *b_string_create_from_cstr(const char *s) return str; } - struct b_string_p *p = b_object_get_private(str, B_TYPE_STRING); + struct fx_string_p *p = fx_object_get_private(str, FX_TYPE_STRING); size_t s_len = strlen(s); - size_t s_codepoints = b_wchar_utf8_codepoint_count(s, s_len); - b_string_reserve(str, s_len); + size_t s_codepoints = fx_wchar_utf8_codepoint_count(s, s_len); + fx_string_reserve(str, s_len); char *dest = string_ptr(p); memcpy(dest, s, s_len); @@ -1110,14 +1110,14 @@ b_string *b_string_create_from_cstr(const char *s) return str; } -b_string *b_string_create_from_c(char c, size_t count) +fx_string *fx_string_create_from_c(char c, size_t count) { - b_string *str = b_string_create(); + fx_string *str = fx_string_create(); if (!str) { return NULL; } - struct b_string_p *p = b_object_get_private(str, B_TYPE_STRING); + struct fx_string_p *p = fx_object_get_private(str, FX_TYPE_STRING); string_change_capacity(p, count); char *s = string_ptr(p); @@ -1130,360 +1130,360 @@ b_string *b_string_create_from_c(char c, size_t count) return str; } -b_string *b_string_duplicate(const b_string *str) +fx_string *fx_string_duplicate(const fx_string *str) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_STRING, string_duplicate, str); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_STRING, string_duplicate, str); } -char *b_string_steal(b_string *str) +char *fx_string_steal(fx_string *str) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_STRING, string_steal, str); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_STRING, string_steal, str); } -b_status b_string_reserve(b_string *str, size_t capacity) +fx_status fx_string_reserve(fx_string *str, size_t capacity) { - B_CLASS_DISPATCH_STATIC(B_TYPE_STRING, string_reserve, str, capacity); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_STRING, string_reserve, str, capacity); } -b_status b_string_replace( - b_string *str, size_t start, size_t length, const char *new_data) +fx_status fx_string_replace( + fx_string *str, size_t start, size_t length, const char *new_data) { - B_CLASS_DISPATCH_STATIC( - B_TYPE_STRING, string_replace, str, start, length, new_data); + FX_CLASS_DISPATCH_STATIC( + FX_TYPE_STRING, string_replace, str, start, length, new_data); } -b_status b_string_replace_all(b_string *str, const char *new_data) +fx_status fx_string_replace_all(fx_string *str, const char *new_data) { - B_CLASS_DISPATCH_STATIC(B_TYPE_STRING, string_replace_all, str, new_data); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_STRING, string_replace_all, str, new_data); } -b_status b_string_replace_all_with_stringstream( - b_string *str, const b_stringstream *new_data) +fx_status fx_string_replace_all_with_stringstream( + fx_string *str, const fx_stringstream *new_data) { - B_CLASS_DISPATCH_STATIC( - B_TYPE_STRING, string_replace_all_with_stringstream, str, new_data); + FX_CLASS_DISPATCH_STATIC( + FX_TYPE_STRING, string_replace_all_with_stringstream, str, new_data); } -enum b_status b_string_remove(b_string *str, size_t start, size_t length) +enum fx_status fx_string_remove(fx_string *str, size_t start, size_t length) { - B_CLASS_DISPATCH_STATIC(B_TYPE_STRING, string_remove, str, start, length); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_STRING, string_remove, str, start, length); } -b_status b_string_transform(b_string *str, int (*transformer)(int)) +fx_status fx_string_transform(fx_string *str, int (*transformer)(int)) { - B_CLASS_DISPATCH_STATIC(B_TYPE_STRING, string_transform, str, transformer); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_STRING, string_transform, str, transformer); } -b_status b_string_trim(b_string *str) +fx_status fx_string_trim(fx_string *str) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_STRING, string_trim, str); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_STRING, string_trim, str); } -enum b_status b_string_insert_c(b_string *dest, char c, size_t at) +enum fx_status fx_string_insert_c(fx_string *dest, char c, size_t at) { - B_CLASS_DISPATCH_STATIC(B_TYPE_STRING, string_insert_c, dest, c, at); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_STRING, string_insert_c, dest, c, at); } -enum b_status b_string_insert_wc(b_string *dest, b_wchar c, size_t at) +enum fx_status fx_string_insert_wc(fx_string *dest, fx_wchar c, size_t at) { - B_CLASS_DISPATCH_STATIC(B_TYPE_STRING, string_insert_wc, dest, c, at); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_STRING, string_insert_wc, dest, c, at); } -enum b_status b_string_insert_s(b_string *dest, const b_string *src, size_t at) +enum fx_status fx_string_insert_s(fx_string *dest, const fx_string *src, size_t at) { - struct b_string_p *dest_p = b_object_get_private(dest, B_TYPE_STRING); - const struct b_string_p *src_p = b_object_get_private(src, B_TYPE_STRING); + struct fx_string_p *dest_p = fx_object_get_private(dest, FX_TYPE_STRING); + const struct fx_string_p *src_p = fx_object_get_private(src, FX_TYPE_STRING); return string_insert_s(dest_p, src_p, at); } -enum b_status b_string_insert_cstr(b_string *dest, const char *src, size_t at) +enum fx_status fx_string_insert_cstr(fx_string *dest, const char *src, size_t at) { - struct b_string_p *dest_p = b_object_get_private(dest, B_TYPE_STRING); + struct fx_string_p *dest_p = fx_object_get_private(dest, FX_TYPE_STRING); return string_insert_cstr(dest_p, src, strlen(src), at); } -enum b_status b_string_insert_wstr(b_string *dest, const b_wchar *src, size_t at) +enum fx_status fx_string_insert_wstr(fx_string *dest, const fx_wchar *src, size_t at) { - struct b_string_p *dest_p = b_object_get_private(dest, B_TYPE_STRING); - return string_insert_wstr(dest_p, src, b_wstrlen(src), at); + struct fx_string_p *dest_p = fx_object_get_private(dest, FX_TYPE_STRING); + return string_insert_wstr(dest_p, src, fx_wstrlen(src), at); } -enum b_status b_string_insert_cstrf( - b_string *dest, size_t at, const char *format, ...) +enum fx_status fx_string_insert_cstrf( + fx_string *dest, size_t at, const char *format, ...) { - struct b_string_p *dest_p = b_object_get_private(dest, B_TYPE_STRING); + struct fx_string_p *dest_p = fx_object_get_private(dest, FX_TYPE_STRING); va_list arg; va_start(arg, format); - enum b_status status = string_insertf(dest_p, at, format, arg); + enum fx_status status = string_insertf(dest_p, at, format, arg); va_end(arg); return status; } -enum b_status b_string_insert_cstrn( - b_string *dest, const char *src, size_t len, size_t at) +enum fx_status fx_string_insert_cstrn( + fx_string *dest, const char *src, size_t len, size_t at) { - B_CLASS_DISPATCH_STATIC( - B_TYPE_STRING, string_insert_cstr, dest, src, len, at); + FX_CLASS_DISPATCH_STATIC( + FX_TYPE_STRING, string_insert_cstr, dest, src, len, at); } -enum b_status b_string_append_cstrf(b_string *dest, const char *format, ...) +enum fx_status fx_string_append_cstrf(fx_string *dest, const char *format, ...) { - struct b_string_p *dest_p = b_object_get_private(dest, B_TYPE_STRING); + struct fx_string_p *dest_p = fx_object_get_private(dest, FX_TYPE_STRING); va_list arg; va_start(arg, format); - enum b_status status = string_insertf(dest_p, SIZE_MAX, format, arg); + enum fx_status status = string_insertf(dest_p, SIZE_MAX, format, arg); va_end(arg); return status; } -enum b_status b_string_prepend_cstrf(b_string *dest, const char *format, ...) +enum fx_status fx_string_prepend_cstrf(fx_string *dest, const char *format, ...) { - struct b_string_p *dest_p = b_object_get_private(dest, B_TYPE_STRING); + struct fx_string_p *dest_p = fx_object_get_private(dest, FX_TYPE_STRING); va_list arg; va_start(arg, format); - enum b_status status = string_insertf(dest_p, 0, format, arg); + enum fx_status status = string_insertf(dest_p, 0, format, arg); va_end(arg); return status; } -void b_string_clear(b_string *str) +void fx_string_clear(fx_string *str) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_STRING, string_clear, str); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_STRING, string_clear, str); } -b_iterator *b_string_tokenise( - b_string *str, const char *delims[], size_t nr_delims, - b_string_tokenise_flags flags) +fx_iterator *fx_string_tokenise( + fx_string *str, const char *delims[], size_t nr_delims, + fx_string_tokenise_flags flags) { - B_CLASS_DISPATCH_STATIC( - B_TYPE_STRING, string_tokenise, str, delims, nr_delims, flags); + FX_CLASS_DISPATCH_STATIC( + FX_TYPE_STRING, string_tokenise, str, delims, nr_delims, flags); } -size_t b_string_get_size(const b_string *str, b_strlen_flags flags) +size_t fx_string_get_size(const fx_string *str, fx_strlen_flags flags) { - B_CLASS_DISPATCH_STATIC(B_TYPE_STRING, string_get_size, str, flags); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_STRING, string_get_size, str, flags); } -size_t b_string_get_capacity(const b_string *str) +size_t fx_string_get_capacity(const fx_string *str) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_STRING, string_get_capacity, str); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_STRING, string_get_capacity, str); } -bool b_string_compare(const b_string *a, const b_string *b) +bool fx_string_compare(const fx_string *a, const fx_string *b) { - struct b_string_p *ap = b_object_get_private(a, B_TYPE_STRING); - struct b_string_p *bp = b_object_get_private(a, B_TYPE_STRING); + struct fx_string_p *ap = fx_object_get_private(a, FX_TYPE_STRING); + struct fx_string_p *bp = fx_object_get_private(a, FX_TYPE_STRING); return string_compare(ap, bp); } -char b_string_front(const b_string *str) +char fx_string_front(const fx_string *str) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_STRING, string_front, str); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_STRING, string_front, str); } -char b_string_back(const b_string *str) +char fx_string_back(const fx_string *str) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_STRING, string_back, str); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_STRING, string_back, str); } -void b_string_pop_back(b_string *str) +void fx_string_pop_back(fx_string *str) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_STRING, string_pop_back, str); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_STRING, string_pop_back, str); } -const char *b_string_ptr(const b_string *str) +const char *fx_string_ptr(const fx_string *str) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_STRING, string_ptr, str); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_STRING, string_ptr, str); } -b_string *b_string_substr(const b_string *str, size_t start, size_t len) +fx_string *fx_string_substr(const fx_string *str, size_t start, size_t len) { - B_CLASS_DISPATCH_STATIC(B_TYPE_STRING, string_substr, str, start, len); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_STRING, string_substr, str, start, len); } -uint64_t b_string_hash(const b_string *str) +uint64_t fx_string_hash(const fx_string *str) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_STRING, string_hash, str); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_STRING, string_hash, str); } /*** PUBLIC ALIAS FUNCTIONS ***************************************************/ -enum b_status b_string_append_c(b_string *dest, char c) +enum fx_status fx_string_append_c(fx_string *dest, char c) { - return b_string_insert_c(dest, c, SIZE_MAX); + return fx_string_insert_c(dest, c, SIZE_MAX); } -enum b_status b_string_append_wc(b_string *dest, b_wchar c) +enum fx_status fx_string_append_wc(fx_string *dest, fx_wchar c) { - return b_string_insert_wc(dest, c, SIZE_MAX); + return fx_string_insert_wc(dest, c, SIZE_MAX); } -enum b_status b_string_append_s(b_string *dest, const b_string *src) +enum fx_status fx_string_append_s(fx_string *dest, const fx_string *src) { - return b_string_insert_s(dest, src, SIZE_MAX); + return fx_string_insert_s(dest, src, SIZE_MAX); } -enum b_status b_string_append_cstr(b_string *dest, const char *src) +enum fx_status fx_string_append_cstr(fx_string *dest, const char *src) { - return b_string_insert_cstr(dest, src, SIZE_MAX); + return fx_string_insert_cstr(dest, src, SIZE_MAX); } -enum b_status b_string_append_wstr(b_string *dest, const b_wchar *src) +enum fx_status fx_string_append_wstr(fx_string *dest, const fx_wchar *src) { - return b_string_insert_wstr(dest, src, SIZE_MAX); + return fx_string_insert_wstr(dest, src, SIZE_MAX); } -enum b_status b_string_prepend_c(b_string *dest, char c) +enum fx_status fx_string_prepend_c(fx_string *dest, char c) { - return b_string_insert_c(dest, c, 0); + return fx_string_insert_c(dest, c, 0); } -enum b_status b_string_prepend_wc(b_string *dest, b_wchar c) +enum fx_status fx_string_prepend_wc(fx_string *dest, fx_wchar c) { - return b_string_insert_wc(dest, c, 0); + return fx_string_insert_wc(dest, c, 0); } -enum b_status b_string_prepend_s(b_string *dest, const b_string *src) +enum fx_status fx_string_prepend_s(fx_string *dest, const fx_string *src) { - return b_string_insert_s(dest, src, 0); + return fx_string_insert_s(dest, src, 0); } -enum b_status b_string_prepend_cstr(b_string *dest, const char *src) +enum fx_status fx_string_prepend_cstr(fx_string *dest, const char *src) { - return b_string_insert_cstr(dest, src, 0); + return fx_string_insert_cstr(dest, src, 0); } -enum b_status b_string_prepend_wstr(b_string *dest, const b_wchar *src) +enum fx_status fx_string_prepend_wstr(fx_string *dest, const fx_wchar *src) { - return b_string_insert_wstr(dest, src, 0); + return fx_string_insert_wstr(dest, src, 0); } /*** VIRTUAL FUNCTIONS ********************************************************/ -static void string_init(b_object *obj, void *priv) +static void string_init(fx_object *obj, void *priv) { - struct b_string_p *str = priv; + struct fx_string_p *str = priv; str->s_len = 0; str->s_codepoints = 0; str->s_max = STRING_INLINE_CAPACITY; } -static void string_fini(b_object *obj, void *priv) +static void string_fini(fx_object *obj, void *priv) { - struct b_string_p *str = priv; + struct fx_string_p *str = priv; if (!string_is_inline(str)) { free(string_ptr(str)); } } -static void string_to_string(const b_object *obj, b_stream *out) +static void string_to_string(const fx_object *obj, fx_stream *out) { - struct b_string_p *str = b_object_get_private(obj, B_TYPE_STRING); + struct fx_string_p *str = fx_object_get_private(obj, FX_TYPE_STRING); const char *s = string_ptr(str); for (size_t i = 0; i < str->s_len; i++) { - b_stream_write_char(out, s[i]); + fx_stream_write_char(out, s[i]); } } /*** ITERATOR FUNCTIONS *******************************************************/ -static void iterator_fini(b_iterator *obj) +static void iterator_fini(fx_iterator *obj) { - struct b_string_iterator_p *it - = b_object_get_private(obj, B_TYPE_STRING_ITERATOR); + struct fx_string_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_STRING_ITERATOR); if (it->_tmp) { - b_string_unref(it->_tmp); + fx_string_unref(it->_tmp); } memset(it, 0x0, sizeof *it); } -static b_iterator *iterator_begin(b_object *obj) +static fx_iterator *iterator_begin(fx_object *obj) { - b_string_iterator *it_obj = b_object_create(B_TYPE_STRING_ITERATOR); - struct b_string_iterator_p *it - = b_object_get_private(it_obj, B_TYPE_STRING_ITERATOR); - struct b_string_p *p = b_object_get_private(obj, B_TYPE_STRING); + fx_string_iterator *it_obj = fx_object_create(FX_TYPE_STRING_ITERATOR); + struct fx_string_iterator_p *it + = fx_object_get_private(it_obj, FX_TYPE_STRING_ITERATOR); + struct fx_string_p *p = fx_object_get_private(obj, FX_TYPE_STRING); if (!p->s_len) { - b_iterator_set_status(it_obj, B_ERR_NO_DATA); + fx_iterator_set_status(it_obj, FX_ERR_NO_DATA); return it_obj; } const char *s = string_ptr(p); it->_m = ITERATOR_MODE_CHARS; it->_s_p = p; - it->char_value = b_wchar_utf8_codepoint_decode(s); + it->char_value = fx_wchar_utf8_codepoint_decode(s); - if (it->char_value == B_WCHAR_INVALID) { - b_iterator_set_status(it_obj, B_ERR_BAD_FORMAT); + if (it->char_value == FX_WCHAR_INVALID) { + fx_iterator_set_status(it_obj, FX_ERR_BAD_FORMAT); } return it_obj; } -static const b_iterator *iterator_cbegin(const b_object *obj) +static const fx_iterator *iterator_cbegin(const fx_object *obj) { - return iterator_begin((b_object *)obj); + return iterator_begin((fx_object *)obj); } -static enum b_status chars_iterator_move_next(struct b_string_iterator_p *it) +static enum fx_status chars_iterator_move_next(struct fx_string_iterator_p *it) { if (!it->_s_p) { - return B_ERR_NO_DATA; + return FX_ERR_NO_DATA; } - size_t stride = b_wchar_utf8_codepoint_size(it->char_value); + size_t stride = fx_wchar_utf8_codepoint_size(it->char_value); if (stride == 0) { - return B_ERR_NO_DATA; + return FX_ERR_NO_DATA; } it->byte_index += stride; it->codepoint_index += 1; if (it->byte_index >= it->_s_p->s_len) { - it->char_value = B_WCHAR_INVALID; - return B_ERR_NO_DATA; + it->char_value = FX_WCHAR_INVALID; + return FX_ERR_NO_DATA; } char *p = string_ptr(it->_s_p) + it->byte_index; - it->char_value = b_wchar_utf8_codepoint_decode(p); - if (it->char_value == B_WCHAR_INVALID) { - return B_ERR_BAD_FORMAT; + it->char_value = fx_wchar_utf8_codepoint_decode(p); + if (it->char_value == FX_WCHAR_INVALID) { + return FX_ERR_BAD_FORMAT; } it->iteration_index++; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status tokens_iterator_move_next(struct b_string_iterator_p *it) +static enum fx_status tokens_iterator_move_next(struct fx_string_iterator_p *it) { if (!it->_s_p) { - return B_ERR_NO_DATA; + return FX_ERR_NO_DATA; } - enum b_status status = find_next_token(it); - if (!B_OK(status)) { + enum fx_status status = find_next_token(it); + if (!FX_OK(status)) { return status; } it->string_value = string_ptr(it->_tmp_p); it->iteration_index++; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status iterator_move_next(const b_iterator *obj) +static enum fx_status iterator_move_next(const fx_iterator *obj) { - struct b_string_iterator_p *it - = b_object_get_private(obj, B_TYPE_STRING_ITERATOR); + struct fx_string_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_STRING_ITERATOR); switch (it->_m) { case ITERATOR_MODE_CHARS: @@ -1491,24 +1491,24 @@ static enum b_status iterator_move_next(const b_iterator *obj) case ITERATOR_MODE_TOKENS: return tokens_iterator_move_next(it); default: - return B_ERR_BAD_STATE; + return FX_ERR_BAD_STATE; } } -static b_iterator_value chars_iterator_get_value(struct b_string_iterator_p *it) +static fx_iterator_value chars_iterator_get_value(struct fx_string_iterator_p *it) { - return B_ITERATOR_VALUE_INT(it->char_value); + return FX_ITERATOR_VALUE_INT(it->char_value); } -static b_iterator_value tokens_iterator_get_value(struct b_string_iterator_p *it) +static fx_iterator_value tokens_iterator_get_value(struct fx_string_iterator_p *it) { - return B_ITERATOR_VALUE_CPTR(it->string_value); + return FX_ITERATOR_VALUE_CPTR(it->string_value); } -static b_iterator_value iterator_get_value(b_iterator *obj) +static fx_iterator_value iterator_get_value(fx_iterator *obj) { - struct b_string_iterator_p *it - = b_object_get_private(obj, B_TYPE_STRING_ITERATOR); + struct fx_string_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_STRING_ITERATOR); switch (it->_m) { case ITERATOR_MODE_CHARS: @@ -1516,14 +1516,14 @@ static b_iterator_value iterator_get_value(b_iterator *obj) case ITERATOR_MODE_TOKENS: return tokens_iterator_get_value(it); default: - return B_ITERATOR_VALUE_NULL; + return FX_ITERATOR_VALUE_NULL; } } -static const b_iterator_value iterator_get_cvalue(const b_iterator *obj) +static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj) { - struct b_string_iterator_p *it - = b_object_get_private(obj, B_TYPE_STRING_ITERATOR); + struct fx_string_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_STRING_ITERATOR); switch (it->_m) { case ITERATOR_MODE_CHARS: @@ -1531,57 +1531,57 @@ static const b_iterator_value iterator_get_cvalue(const b_iterator *obj) case ITERATOR_MODE_TOKENS: return tokens_iterator_get_value(it); default: - return B_ITERATOR_VALUE_NULL; + return FX_ITERATOR_VALUE_NULL; } } /*** CLASS DEFINITION *********************************************************/ -// ---- b_string DEFINITION -B_TYPE_CLASS_DEFINITION_BEGIN(b_string) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = string_to_string; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) +// ---- fx_string DEFINITION +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_string) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = string_to_string; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) - B_TYPE_CLASS_INTERFACE_BEGIN(b_iterable, B_TYPE_ITERABLE) - B_INTERFACE_ENTRY(it_begin) = iterator_begin; - B_INTERFACE_ENTRY(it_cbegin) = iterator_cbegin; - B_TYPE_CLASS_INTERFACE_END(b_iterable, B_TYPE_ITERABLE) -B_TYPE_CLASS_DEFINITION_END(b_string) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE) + FX_INTERFACE_ENTRY(it_begin) = iterator_begin; + FX_INTERFACE_ENTRY(it_cbegin) = iterator_cbegin; + FX_TYPE_CLASS_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE) +FX_TYPE_CLASS_DEFINITION_END(fx_string) -B_TYPE_DEFINITION_BEGIN(b_string) - B_TYPE_ID(0x200194f6, 0x0327, 0x4a82, 0xb9c9, 0xb62ddd038c33); - B_TYPE_IMPLEMENTS(B_TYPE_ITERABLE); - B_TYPE_CLASS(b_string_class); - B_TYPE_INSTANCE_PRIVATE(struct b_string_p); - B_TYPE_INSTANCE_INIT(string_init); - B_TYPE_INSTANCE_FINI(string_fini); -B_TYPE_DEFINITION_END(b_string) +FX_TYPE_DEFINITION_BEGIN(fx_string) + FX_TYPE_ID(0x200194f6, 0x0327, 0x4a82, 0xb9c9, 0xb62ddd038c33); + FX_TYPE_IMPLEMENTS(FX_TYPE_ITERABLE); + FX_TYPE_CLASS(fx_string_class); + FX_TYPE_INSTANCE_PRIVATE(struct fx_string_p); + FX_TYPE_INSTANCE_INIT(string_init); + FX_TYPE_INSTANCE_FINI(string_fini); +FX_TYPE_DEFINITION_END(fx_string) -// ---- b_string_iterator DEFINITION -B_TYPE_CLASS_DEFINITION_BEGIN(b_string_iterator) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) +// ---- fx_string_iterator DEFINITION +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_string_iterator) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) - B_TYPE_CLASS_INTERFACE_BEGIN(b_iterator, B_TYPE_ITERATOR) - B_INTERFACE_ENTRY(it_move_next) = iterator_move_next; - B_INTERFACE_ENTRY(it_erase) = NULL; - B_INTERFACE_ENTRY(it_get_value) = iterator_get_value; - B_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue; - B_TYPE_CLASS_INTERFACE_END(b_iterator, B_TYPE_ITERATOR) -B_TYPE_CLASS_DEFINITION_END(b_string_iterator) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR) + FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next; + FX_INTERFACE_ENTRY(it_erase) = NULL; + FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value; + FX_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue; + FX_TYPE_CLASS_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR) +FX_TYPE_CLASS_DEFINITION_END(fx_string_iterator) -B_TYPE_DEFINITION_BEGIN(b_string_iterator) - B_TYPE_ID(0xfc06cee1, 0xb63a, 0x4718, 0x9b8e, 0x3bd2eb7a8608); - B_TYPE_EXTENDS(B_TYPE_ITERATOR); - B_TYPE_CLASS(b_string_iterator_class); - B_TYPE_INSTANCE_PRIVATE(struct b_string_iterator_p); -B_TYPE_DEFINITION_END(b_string_iterator) +FX_TYPE_DEFINITION_BEGIN(fx_string_iterator) + FX_TYPE_ID(0xfc06cee1, 0xb63a, 0x4718, 0x9b8e, 0x3bd2eb7a8608); + FX_TYPE_EXTENDS(FX_TYPE_ITERATOR); + FX_TYPE_CLASS(fx_string_iterator_class); + FX_TYPE_INSTANCE_PRIVATE(struct fx_string_iterator_p); +FX_TYPE_DEFINITION_END(fx_string_iterator) /*** MISC FUNCTIONS ***********************************************************/ -char *b_strdup(const char *s) +char *fx_strdup(const char *s) { size_t len = strlen(s); char *p = malloc(len + 1); @@ -1595,15 +1595,15 @@ char *b_strdup(const char *s) return p; } -size_t b_strlen(const char *s, b_strlen_flags flags) +size_t fx_strlen(const char *s, fx_strlen_flags flags) { - if (!(flags & (B_STRLEN_IGNORE_ESC | B_STRLEN_IGNORE_MOD))) { + 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 & B_STRLEN_IGNORE_ESC)) { + if (s[i] == '\033' && (flags & FX_STRLEN_IGNORE_ESC)) { while (!isalpha(s[i]) && s[i]) { i++; } @@ -1611,7 +1611,7 @@ size_t b_strlen(const char *s, b_strlen_flags flags) continue; } - if (s[i] == '[' && (flags & B_STRLEN_IGNORE_MOD)) { + if (s[i] == '[' && (flags & FX_STRLEN_IGNORE_MOD)) { i++; if (s[i] == '[') { out++; @@ -1631,20 +1631,20 @@ size_t b_strlen(const char *s, b_strlen_flags flags) return out; } -b_wchar *b_wstrdup(const b_wchar *s) +fx_wchar *fx_wstrdup(const fx_wchar *s) { - size_t len = b_wstrlen(s); - b_wchar *buf = calloc(len + 1, sizeof(b_wchar)); + size_t len = fx_wstrlen(s); + fx_wchar *buf = calloc(len + 1, sizeof(fx_wchar)); if (!buf) { return NULL; } - memcpy(buf, s, len * sizeof(b_wchar)); + memcpy(buf, s, len * sizeof(fx_wchar)); return buf; } -size_t b_wstrlen(const b_wchar *s) +size_t fx_wstrlen(const fx_wchar *s) { size_t len; for (len = 0; s[len] != 0; len++) diff --git a/ds/tree.c b/ds/tree.c index e5e56ce..87526f3 100644 --- a/ds/tree.c +++ b/ds/tree.c @@ -1,4 +1,4 @@ -#include +#include #include #include @@ -14,26 +14,26 @@ /*** PRIVATE DATA *************************************************************/ -struct b_tree_p { - struct b_tree_node *t_root; +struct fx_tree_p { + struct fx_tree_node *t_root; }; -struct b_tree_iterator_p { +struct fx_tree_iterator_p { size_t i, depth; - b_tree_node *node; + fx_tree_node *node; unsigned char _f01; }; /*** PRIVATE FUNCTIONS ********************************************************/ -static void tree_set_root(struct b_tree_p *tree, struct b_tree_node *node) +static void tree_set_root(struct fx_tree_p *tree, struct fx_tree_node *node) { tree->t_root = node; } -static const struct b_tree_node *next_node( - const struct b_tree_node *node, bool recursive, int *depth_diff) +static const struct fx_tree_node *next_node( + const struct fx_tree_node *node, bool recursive, int *depth_diff) { if (!node) { return NULL; @@ -45,14 +45,14 @@ static const struct b_tree_node *next_node( } int d = 0; - struct b_tree_node *next = NODE_FIRST_CHILD(node); + struct fx_tree_node *next = NODE_FIRST_CHILD(node); if (next) { d = 1; *depth_diff = d; return next; } - const struct b_tree_node *n = node; + const struct fx_tree_node *n = node; next = NODE_NEXT_SIBLING(n); while (!next) { n = NODE_PARENT(n); @@ -68,14 +68,14 @@ static const struct b_tree_node *next_node( return next; } -static void remove_node(struct b_tree_node *node) +static void remove_node(struct fx_tree_node *node) { - struct b_tree_node *parent = NODE_PARENT(node); + struct fx_tree_node *parent = NODE_PARENT(node); if (!parent) { return; } - struct b_tree_node *n0 = NULL, *n1 = NULL; + struct fx_tree_node *n0 = NULL, *n1 = NULL; n0 = NODE_FIRST_CHILD(parent); while (n0) { @@ -101,16 +101,16 @@ static void remove_node(struct b_tree_node *node) } static void reparent_children( - struct b_tree_node *old_parent, struct b_tree_node *new_parent) + struct fx_tree_node *old_parent, struct fx_tree_node *new_parent) { - struct b_tree_node *last = NODE_FIRST_CHILD(new_parent); + struct fx_tree_node *last = NODE_FIRST_CHILD(new_parent); while (last && NODE_NEXT_SIBLING(last)) { last = NODE_NEXT_SIBLING(last); } - struct b_tree_node *cur = NODE_FIRST_CHILD(old_parent); + struct fx_tree_node *cur = NODE_FIRST_CHILD(old_parent); while (cur) { - struct b_tree_node *next = NODE_NEXT_SIBLING(cur); + struct fx_tree_node *next = NODE_NEXT_SIBLING(cur); NODE_PARENT(cur) = new_parent; NODE_NEXT_SIBLING(cur) = NULL; @@ -127,12 +127,12 @@ static void reparent_children( /*** PUBLIC FUNCTIONS *********************************************************/ -void b_tree_set_root(b_tree *tree, struct b_tree_node *node) +void fx_tree_set_root(fx_tree *tree, struct fx_tree_node *node) { - B_CLASS_DISPATCH_STATIC(B_TYPE_TREE, tree_set_root, tree, node); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_TREE, tree_set_root, tree, node); } -void b_tree_node_add_child(struct b_tree_node *parent, struct b_tree_node *child) +void fx_tree_node_add_child(struct fx_tree_node *parent, struct fx_tree_node *child) { if (NODE_PARENT(child)) { return; @@ -144,7 +144,7 @@ void b_tree_node_add_child(struct b_tree_node *parent, struct b_tree_node *child return; } - struct b_tree_node *cur = NODE_FIRST_CHILD(parent); + struct fx_tree_node *cur = NODE_FIRST_CHILD(parent); while (NODE_NEXT_SIBLING(cur)) { cur = NODE_NEXT_SIBLING(cur); } @@ -152,19 +152,19 @@ void b_tree_node_add_child(struct b_tree_node *parent, struct b_tree_node *child NODE_NEXT_SIBLING(cur) = child; } -void b_tree_node_add_sibling(struct b_tree_node *node, struct b_tree_node *to_add) +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; } - b_tree_node_add_child(NODE_PARENT(node), to_add); + fx_tree_node_add_child(NODE_PARENT(node), to_add); } -struct b_tree_node *b_tree_node_get_child(struct b_tree_node *node, size_t at) +struct fx_tree_node *fx_tree_node_get_child(struct fx_tree_node *node, size_t at) { size_t i = 0; - struct b_tree_node *cur = NODE_FIRST_CHILD(node); + struct fx_tree_node *cur = NODE_FIRST_CHILD(node); while (i < at) { if (!cur) { @@ -178,23 +178,23 @@ struct b_tree_node *b_tree_node_get_child(struct b_tree_node *node, size_t at) return cur; } -b_iterator *b_tree_begin(b_tree *tree) +fx_iterator *fx_tree_begin(fx_tree *tree) { - struct b_tree_p *p = b_object_get_private(tree, B_TYPE_TREE); - return b_tree_node_begin(p->t_root); + struct fx_tree_p *p = fx_object_get_private(tree, FX_TYPE_TREE); + return fx_tree_node_begin(p->t_root); } -const b_iterator *b_tree_cbegin(const b_tree *tree) +const fx_iterator *fx_tree_cbegin(const fx_tree *tree) { - struct b_tree_p *p = b_object_get_private(tree, B_TYPE_TREE); - return b_tree_node_begin(p->t_root); + struct fx_tree_p *p = fx_object_get_private(tree, FX_TYPE_TREE); + return fx_tree_node_begin(p->t_root); } -b_iterator *b_tree_node_begin(struct b_tree_node *node) +fx_iterator *fx_tree_node_begin(struct fx_tree_node *node) { - b_tree_iterator *it_obj = b_object_create(B_TYPE_TREE_ITERATOR); - struct b_tree_iterator_p *it - = b_object_get_private(it_obj, B_TYPE_TREE_ITERATOR); + 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; @@ -203,22 +203,22 @@ b_iterator *b_tree_node_begin(struct b_tree_node *node) ITERATOR_UNSET_RECURSIVE(it); if (!it->node) { - b_iterator_set_status(it_obj, B_ERR_NO_DATA); + fx_iterator_set_status(it_obj, FX_ERR_NO_DATA); } return it_obj; } -const b_iterator *b_tree_node_cbegin(const struct b_tree_node *node) +const fx_iterator *fx_tree_node_cbegin(const struct fx_tree_node *node) { - return b_tree_node_begin((struct b_tree_node *)node); + return fx_tree_node_begin((struct fx_tree_node *)node); } -b_iterator *b_tree_node_begin_recursive(struct b_tree_node *node) +fx_iterator *fx_tree_node_begin_recursive(struct fx_tree_node *node) { - b_tree_iterator *it_obj = b_object_create(B_TYPE_TREE_ITERATOR); - struct b_tree_iterator_p *it - = b_object_get_private(it_obj, B_TYPE_TREE_ITERATOR); + 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; @@ -227,38 +227,38 @@ b_iterator *b_tree_node_begin_recursive(struct b_tree_node *node) ITERATOR_SET_RECURSIVE(it); if (!it->node) { - b_iterator_set_status(it_obj, B_ERR_NO_DATA); + fx_iterator_set_status(it_obj, FX_ERR_NO_DATA); } return it_obj; } -const b_iterator *b_tree_node_cbegin_recursive(const struct b_tree_node *node) +const fx_iterator *fx_tree_node_cbegin_recursive(const struct fx_tree_node *node) { - return b_tree_node_begin_recursive((struct b_tree_node *)node); + return fx_tree_node_begin_recursive((struct fx_tree_node *)node); } /*** VIRTUAL FUNCTIONS ********************************************************/ -static void tree_init(b_object *obj, void *priv) +static void tree_init(fx_object *obj, void *priv) { - struct b_tree_p *tree = priv; + struct fx_tree_p *tree = priv; } -static void tree_fini(b_object *obj, void *priv) +static void tree_fini(fx_object *obj, void *priv) { - struct b_tree_p *tree = priv; + struct fx_tree_p *tree = priv; } /*** ITERATOR FUNCTIONS *******************************************************/ -static enum b_status iterator_move_next(const b_iterator *obj) +static enum fx_status iterator_move_next(const fx_iterator *obj) { - struct b_tree_iterator_p *it - = b_object_get_private(obj, B_TYPE_TREE_ITERATOR); + struct fx_tree_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_TREE_ITERATOR); int depth_diff = 0; - const struct b_tree_node *next + const struct fx_tree_node *next = next_node(it->node, ITERATOR_IS_RECURSIVE(it), &depth_diff); if (next) { @@ -269,27 +269,27 @@ static enum b_status iterator_move_next(const b_iterator *obj) it->i = 0; } - it->node = (struct b_tree_node *)next; - return (it->node != NULL) ? B_SUCCESS : B_ERR_NO_DATA; + it->node = (struct fx_tree_node *)next; + return (it->node != NULL) ? FX_SUCCESS : FX_ERR_NO_DATA; } -static enum b_status iterator_erase(b_iterator *obj) +static enum fx_status iterator_erase(fx_iterator *obj) { - struct b_tree_iterator_p *it - = b_object_get_private(obj, B_TYPE_TREE_ITERATOR); + struct fx_tree_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_TREE_ITERATOR); if (!it->node) { - return B_ERR_OUT_OF_BOUNDS; + return FX_ERR_OUT_OF_BOUNDS; } - struct b_tree_node *parent = NODE_PARENT(it->node); + struct fx_tree_node *parent = NODE_PARENT(it->node); if (!parent) { - return B_ERR_NOT_SUPPORTED; + return FX_ERR_NOT_SUPPORTED; } int d = 0; - struct b_tree_node *n = it->node; - struct b_tree_node *next = NODE_NEXT_SIBLING(n); + struct fx_tree_node *n = it->node; + struct fx_tree_node *next = NODE_NEXT_SIBLING(n); if (!next) { next = NODE_FIRST_CHILD(n); @@ -308,73 +308,73 @@ static enum b_status iterator_erase(b_iterator *obj) remove_node(it->node); reparent_children(it->node, parent); - return B_SUCCESS; + return FX_SUCCESS; } -static b_iterator_value iterator_get_value(b_iterator *obj) +static fx_iterator_value iterator_get_value(fx_iterator *obj) { - struct b_tree_iterator_p *it - = b_object_get_private(obj, B_TYPE_TREE_ITERATOR); + struct fx_tree_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_TREE_ITERATOR); if (!it->node) { - return B_ITERATOR_VALUE_NULL; + return FX_ITERATOR_VALUE_NULL; } - return B_ITERATOR_VALUE_PTR(it->node); + return FX_ITERATOR_VALUE_PTR(it->node); } -static const b_iterator_value iterator_get_cvalue(const b_iterator *obj) +static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj) { - struct b_tree_iterator_p *it - = b_object_get_private(obj, B_TYPE_TREE_ITERATOR); + struct fx_tree_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_TREE_ITERATOR); if (!it->node) { - return B_ITERATOR_VALUE_NULL; + return FX_ITERATOR_VALUE_NULL; } - return B_ITERATOR_VALUE_CPTR(it->node); + return FX_ITERATOR_VALUE_CPTR(it->node); } /*** CLASS DEFINITION *********************************************************/ -// ---- b_tree DEFINITION -B_TYPE_CLASS_DEFINITION_BEGIN(b_tree) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) +// ---- fx_tree DEFINITION +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_tree) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) - B_TYPE_CLASS_INTERFACE_BEGIN(b_iterable, B_TYPE_ITERABLE) - B_INTERFACE_ENTRY(it_begin) = b_tree_begin; - B_INTERFACE_ENTRY(it_cbegin) = b_tree_cbegin; - B_TYPE_CLASS_INTERFACE_END(b_iterable, B_TYPE_ITERABLE) -B_TYPE_CLASS_DEFINITION_END(b_tree) + FX_TYPE_CLASS_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_CLASS_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE) +FX_TYPE_CLASS_DEFINITION_END(fx_tree) -B_TYPE_DEFINITION_BEGIN(b_tree) - B_TYPE_ID(0x8d8fa36b, 0xc515, 0x4803, 0x8124, 0xfd704f01b8ae); - B_TYPE_CLASS(b_tree_class); - B_TYPE_IMPLEMENTS(B_TYPE_ITERABLE); - B_TYPE_INSTANCE_PRIVATE(struct b_tree_p); - B_TYPE_INSTANCE_INIT(tree_init); - B_TYPE_INSTANCE_FINI(tree_fini); -B_TYPE_DEFINITION_END(b_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) -// ---- b_tree_iterator DEFINITION -B_TYPE_CLASS_DEFINITION_BEGIN(b_tree_iterator) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) +// ---- fx_tree_iterator DEFINITION +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_tree_iterator) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) - B_TYPE_CLASS_INTERFACE_BEGIN(b_iterator, B_TYPE_ITERATOR) - B_INTERFACE_ENTRY(it_move_next) = iterator_move_next; - B_INTERFACE_ENTRY(it_erase) = iterator_erase; - B_INTERFACE_ENTRY(it_get_value) = iterator_get_value; - B_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue; - B_TYPE_CLASS_INTERFACE_END(b_iterator, B_TYPE_ITERATOR) -B_TYPE_CLASS_DEFINITION_END(b_tree_iterator) + FX_TYPE_CLASS_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_CLASS_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR) +FX_TYPE_CLASS_DEFINITION_END(fx_tree_iterator) -B_TYPE_DEFINITION_BEGIN(b_tree_iterator) - B_TYPE_ID(0xb896e671, 0x84b2, 0x4892, 0xaf09, 0x407f305f4bf8); - B_TYPE_EXTENDS(B_TYPE_ITERATOR); - B_TYPE_CLASS(b_tree_iterator_class); - B_TYPE_INSTANCE_PRIVATE(struct b_tree_iterator_p); -B_TYPE_DEFINITION_END(b_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) diff --git a/ds/uuid.c b/ds/uuid.c index 02151c7..4be30f8 100644 --- a/ds/uuid.c +++ b/ds/uuid.c @@ -1,6 +1,6 @@ -#include -#include -#include +#include +#include +#include #include #include #include @@ -8,17 +8,17 @@ /*** PRIVATE DATA *************************************************************/ -struct b_uuid_p { - union b_uuid_bytes uuid_bytes; +struct fx_uuid_p { + union fx_uuid_bytes uuid_bytes; }; /*** PRIVATE FUNCTIONS ********************************************************/ -static b_status uuid_to_cstr( - const struct b_uuid_p *uuid, char out[B_UUID_STRING_MAX]) +static fx_status uuid_to_cstr( + const struct fx_uuid_p *uuid, char out[FX_UUID_STRING_MAX]) { snprintf( - out, B_UUID_STRING_MAX, + out, FX_UUID_STRING_MAX, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%" "02x%02x", uuid->uuid_bytes.uuid_bytes[0], uuid->uuid_bytes.uuid_bytes[1], @@ -29,41 +29,41 @@ static b_status uuid_to_cstr( uuid->uuid_bytes.uuid_bytes[10], uuid->uuid_bytes.uuid_bytes[11], uuid->uuid_bytes.uuid_bytes[12], uuid->uuid_bytes.uuid_bytes[13], uuid->uuid_bytes.uuid_bytes[14], uuid->uuid_bytes.uuid_bytes[15]); - return B_SUCCESS; + return FX_SUCCESS; } static void uuid_get_bytes( - const struct b_uuid_p *uuid, unsigned char bytes[B_UUID_NBYTES]) + const struct fx_uuid_p *uuid, unsigned char bytes[FX_UUID_NBYTES]) { - memcpy(bytes, uuid->uuid_bytes.uuid_bytes, B_UUID_NBYTES); + memcpy(bytes, uuid->uuid_bytes.uuid_bytes, FX_UUID_NBYTES); } static void uuid_get_uuid_bytes( - const struct b_uuid_p *uuid, union b_uuid_bytes *bytes) + const struct fx_uuid_p *uuid, union fx_uuid_bytes *bytes) { memcpy(bytes, &uuid->uuid_bytes, sizeof *bytes); } -static union b_uuid_bytes *uuid_ptr(struct b_uuid_p *uuid) +static union fx_uuid_bytes *uuid_ptr(struct fx_uuid_p *uuid) { return &uuid->uuid_bytes; } /*** PUBLIC FUNCTIONS *********************************************************/ -b_uuid *b_uuid_create_from_bytes( +fx_uuid *fx_uuid_create_from_bytes( unsigned char u00, unsigned char u01, unsigned char u02, unsigned char u03, unsigned char u04, unsigned char u05, unsigned char u06, unsigned char u07, unsigned char u08, unsigned char u09, unsigned char u10, unsigned char u11, unsigned char u12, unsigned char u13, unsigned char u14, unsigned char u15) { - b_uuid *uuid = b_uuid_create(); + fx_uuid *uuid = fx_uuid_create(); if (!uuid) { return NULL; } - struct b_uuid_p *p = b_object_get_private(uuid, B_TYPE_UUID); + struct fx_uuid_p *p = fx_object_get_private(uuid, FX_TYPE_UUID); p->uuid_bytes.uuid_bytes[0] = u00; p->uuid_bytes.uuid_bytes[1] = u01; @@ -85,23 +85,23 @@ b_uuid *b_uuid_create_from_bytes( return uuid; } -b_uuid *b_uuid_create_from_bytev(const unsigned char bytes[B_UUID_NBYTES]) +fx_uuid *fx_uuid_create_from_bytev(const unsigned char bytes[FX_UUID_NBYTES]) { - b_uuid *uuid = b_uuid_create(); + fx_uuid *uuid = fx_uuid_create(); if (!uuid) { return NULL; } - struct b_uuid_p *p = b_object_get_private(uuid, B_TYPE_UUID); + struct fx_uuid_p *p = fx_object_get_private(uuid, FX_TYPE_UUID); - memcpy(p->uuid_bytes.uuid_bytes, bytes, B_UUID_NBYTES); + memcpy(p->uuid_bytes.uuid_bytes, bytes, FX_UUID_NBYTES); return uuid; } -b_uuid *b_uuid_create_from_cstr(const char *str) +fx_uuid *fx_uuid_create_from_cstr(const char *str) { - union b_uuid_bytes bytes; + union fx_uuid_bytes bytes; bool valid = true; bool is_guid = false; @@ -111,7 +111,7 @@ b_uuid *b_uuid_create_from_cstr(const char *str) } size_t i, byte = 0; - for (i = 0; str[i] && byte < B_UUID_NBYTES;) { + for (i = 0; str[i] && byte < FX_UUID_NBYTES;) { if (i == 8 || i == 13 || i == 18 || i == 23) { if (str[i] != '-') { valid = false; @@ -148,7 +148,7 @@ b_uuid *b_uuid_create_from_cstr(const char *str) } } - if (str[i] != '\0' || byte != B_UUID_NBYTES) { + if (str[i] != '\0' || byte != FX_UUID_NBYTES) { valid = false; } @@ -156,72 +156,72 @@ b_uuid *b_uuid_create_from_cstr(const char *str) return NULL; } - return b_uuid_create_from_uuid_bytes(&bytes); + return fx_uuid_create_from_uuid_bytes(&bytes); } -b_status b_uuid_to_cstr(const b_uuid *uuid, char out[B_UUID_STRING_MAX]) +fx_status fx_uuid_to_cstr(const fx_uuid *uuid, char out[FX_UUID_STRING_MAX]) { - B_CLASS_DISPATCH_STATIC(B_TYPE_UUID, uuid_to_cstr, uuid, out); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_UUID, uuid_to_cstr, uuid, out); } -void b_uuid_get_bytes(const b_uuid *uuid, unsigned char bytes[B_UUID_NBYTES]) +void fx_uuid_get_bytes(const fx_uuid *uuid, unsigned char bytes[FX_UUID_NBYTES]) { - B_CLASS_DISPATCH_STATIC(B_TYPE_UUID, uuid_get_bytes, uuid, bytes); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_UUID, uuid_get_bytes, uuid, bytes); } -void b_uuid_get_uuid_bytes(const b_uuid *uuid, union b_uuid_bytes *bytes) +void fx_uuid_get_uuid_bytes(const fx_uuid *uuid, union fx_uuid_bytes *bytes) { - B_CLASS_DISPATCH_STATIC(B_TYPE_UUID, uuid_get_uuid_bytes, uuid, bytes); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_UUID, uuid_get_uuid_bytes, uuid, bytes); } -union b_uuid_bytes *b_uuid_ptr(b_uuid *uuid) +union fx_uuid_bytes *fx_uuid_ptr(fx_uuid *uuid) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_UUID, uuid_ptr, uuid); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_UUID, uuid_ptr, uuid); } /*** PUBLIC ALIAS FUNCTIONS ***************************************************/ -b_uuid *b_uuid_create_from_uuid_bytes(const union b_uuid_bytes *bytes) +fx_uuid *fx_uuid_create_from_uuid_bytes(const union fx_uuid_bytes *bytes) { - return b_uuid_create_from_bytev(bytes->uuid_bytes); + return fx_uuid_create_from_bytev(bytes->uuid_bytes); } -b_uuid *b_uuid_create_from_string(const b_string *string) +fx_uuid *fx_uuid_create_from_string(const fx_string *string) { - return b_uuid_create_from_cstr(b_string_ptr(string)); + return fx_uuid_create_from_cstr(fx_string_ptr(string)); } /*** VIRTUAL FUNCTIONS ********************************************************/ -static void uuid_init(b_object *obj, void *priv) +static void uuid_init(fx_object *obj, void *priv) { - struct b_uuid_p *uuid = priv; + struct fx_uuid_p *uuid = priv; } -static void uuid_fini(b_object *obj, void *priv) +static void uuid_fini(fx_object *obj, void *priv) { - struct b_uuid_p *uuid = priv; + struct fx_uuid_p *uuid = priv; } -static void uuid_to_string(const b_object *uuid, b_stream *out) +static void uuid_to_string(const fx_object *uuid, fx_stream *out) { - char str[B_UUID_STRING_MAX]; - b_uuid_to_cstr(uuid, str); - b_stream_write_string(out, str, NULL); + char str[FX_UUID_STRING_MAX]; + fx_uuid_to_cstr(uuid, str); + fx_stream_write_string(out, str, NULL); } /*** CLASS DEFINITION *********************************************************/ -B_TYPE_CLASS_DEFINITION_BEGIN(b_uuid) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = uuid_to_string; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) -B_TYPE_CLASS_DEFINITION_END(b_uuid) +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_uuid) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = uuid_to_string; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_DEFINITION_END(fx_uuid) -B_TYPE_DEFINITION_BEGIN(b_uuid) - B_TYPE_ID(0x17037068, 0x92f7, 0x4582, 0xad1f, 0x0dea43b628de); - B_TYPE_CLASS(b_uuid_class); - B_TYPE_INSTANCE_PRIVATE(struct b_uuid_p); - B_TYPE_INSTANCE_INIT(uuid_init); - B_TYPE_INSTANCE_FINI(uuid_fini); -B_TYPE_DEFINITION_END(b_uuid) +FX_TYPE_DEFINITION_BEGIN(fx_uuid) + FX_TYPE_ID(0x17037068, 0x92f7, 0x4582, 0xad1f, 0x0dea43b628de); + FX_TYPE_CLASS(fx_uuid_class); + FX_TYPE_INSTANCE_PRIVATE(struct fx_uuid_p); + FX_TYPE_INSTANCE_INIT(uuid_init); + FX_TYPE_INSTANCE_FINI(uuid_fini); +FX_TYPE_DEFINITION_END(fx_uuid) diff --git a/io/CMakeLists.txt b/io/CMakeLists.txt index c79fe14..17b9abd 100644 --- a/io/CMakeLists.txt +++ b/io/CMakeLists.txt @@ -1,3 +1,3 @@ include(../cmake/Templates.cmake) -add_bluelib_module(NAME io DEPENDENCIES core ds) +add_fx_module(NAME io DEPENDENCIES core ds) diff --git a/io/include/blue/io/directory.h b/io/include/blue/io/directory.h deleted file mode 100644 index 89c5e92..0000000 --- a/io/include/blue/io/directory.h +++ /dev/null @@ -1,75 +0,0 @@ -#ifndef BLUE_IO_DIRECTORY_H_ -#define BLUE_IO_DIRECTORY_H_ - -#include -#include -#include -#include -#include -#include - -#define B_DIRECTORY_ROOT ((b_directory *)NULL) - -B_DECLS_BEGIN; - -struct b_directory_p; - -#define B_TYPE_DIRECTORY (b_directory_get_type()) -#define B_TYPE_DIRECTORY_ITERATOR (b_directory_iterator_get_type()) - -B_DECLARE_TYPE(b_directory); -B_DECLARE_TYPE(b_directory_iterator); - -B_TYPE_CLASS_DECLARATION_BEGIN(b_directory) -B_TYPE_CLASS_DECLARATION_END(b_directory) - -B_TYPE_CLASS_DECLARATION_BEGIN(b_directory_iterator) -B_TYPE_CLASS_DECLARATION_END(b_directory_iterator) - -struct z__b_directory_iterator; - -typedef enum b_directory_iterator_flags { - B_DIRECTORY_ITERATE_PARENT_FIRST = 0x01u, - B_DIRECTORY_ITERATE_PARENT_LAST = 0x02u, -} b_directory_iterator_flags; - -typedef enum b_directory_open_flags { - B_DIRECTORY_OPEN_CREATE = 0x01u, - B_DIRECTORY_OPEN_CREATE_INTERMEDIATE = 0x03u, - B_DIRECTORY_OPEN_DELETE_ON_CLOSE = 0x04u, -} b_directory_open_flags; - -typedef struct b_directory_entry { - const b_path *filepath; - char *filename; - b_file_info info; -} b_directory_entry; - -BLUE_API b_type b_directory_get_type(void); -BLUE_API b_type b_directory_iterator_get_type(void); - -BLUE_API b_result b_directory_open( - b_directory *root, const b_path *path, b_directory_open_flags flags, - b_directory **out); -BLUE_API b_result b_directory_open_temp(b_directory **out); -BLUE_API const b_path *b_directory_get_path(const b_directory *dir); -BLUE_API const b_path *b_directory_get_rel_path(const b_directory *dir); -BLUE_API const char *b_directory_get_path_cstr(const b_directory *dir); -BLUE_API const char *b_directory_get_rel_path_cstr(const b_directory *dir); -BLUE_API b_result b_directory_delete(b_directory *dir); - -BLUE_API bool b_directory_path_exists(const b_directory *root, const b_path *path); -BLUE_API bool b_directory_path_is_file(const b_directory *root, const b_path *path); -BLUE_API bool b_directory_path_is_directory( - const b_directory *root, const b_path *path); -BLUE_API b_result b_directory_path_stat( - const b_directory *root, const b_path *path, struct b_file_info *out); -BLUE_API b_result b_directory_path_unlink( - const b_directory *root, const b_path *path); - -BLUE_API b_iterator *b_directory_begin( - b_directory *dir, b_directory_iterator_flags flags); - -B_DECLS_END; - -#endif diff --git a/io/include/blue/io/file.h b/io/include/blue/io/file.h deleted file mode 100644 index bd55cda..0000000 --- a/io/include/blue/io/file.h +++ /dev/null @@ -1,80 +0,0 @@ -#ifndef BLUE_IO_FILE_H_ -#define BLUE_IO_FILE_H_ - -#include -#include -#include -#include - -B_DECLS_BEGIN; - -#define B_TYPE_FILE (b_file_get_type()) - -B_DECLARE_TYPE(b_file); - -B_TYPE_CLASS_DECLARATION_BEGIN(b_file) -B_TYPE_CLASS_DECLARATION_END(b_file) - -#define B_OFFSET_CURRENT ((size_t)-1) - -typedef enum b_seek_basis { - B_SEEK_BEGINNING, - B_SEEK_CURRENT, - B_SEEK_END -} b_seek_basis; - -typedef enum b_file_attribute { - B_FILE_ATTRIB_NORMAL = 0x01u, - B_FILE_ATTRIB_DIRECTORY = 0x02u, - B_FILE_ATTRIB_BLOCK_DEVICE = 0x04u, - B_FILE_ATTRIB_CHAR_DEVICE = 0x08u, - B_FILE_ATTRIB_SYMLINK = 0x80u, -} b_file_attribute; - -typedef enum b_file_mode { - B_FILE_READ_ONLY = 0x01u, - B_FILE_WRITE_ONLY = 0x02u, - B_FILE_READ_WRITE = B_FILE_READ_ONLY | B_FILE_WRITE_ONLY, - B_FILE_TRUNCATE = 0x04u, - B_FILE_CREATE = 0x08u, - B_FILE_CREATE_ONLY = 0x10u | B_FILE_CREATE, - B_FILE_APPEND = 0x20u, - B_FILE_BINARY = 0x40u, - B_FILE_DELETE_ON_CLOSE = 0x80u, - B_FILE_SHADOW = 0x100u, -} b_file_mode; - -typedef struct b_file_info { - b_file_attribute attrib; - b_file_mode mode; - - size_t length; -} b_file_info; - -BLUE_API b_type b_file_get_type(void); - -BLUE_API b_result b_file_open( - B_TYPE_FWDREF(b_directory) * root, const B_TYPE_FWDREF(b_path) * path, - b_file_mode mode, b_file **out); -BLUE_API b_result b_file_open_temp(b_file_mode mode, b_file **out); -BLUE_API b_result b_file_open_shadow( - b_file *original, b_file_mode mode, b_file **out); - -BLUE_API b_status b_file_stat(b_file *file, b_file_info *out); -BLUE_API b_status b_file_size(b_file *file, size_t *out_len); -BLUE_API b_status b_file_cursor(b_file *file, size_t *out_pos); -BLUE_API b_status b_file_resize(b_file *file, size_t len); -BLUE_API b_status b_file_seek(b_file *file, long long offset, b_seek_basis basis); -BLUE_API const B_TYPE_FWDREF(b_path) * b_file_path(const b_file *file); - -BLUE_API b_status b_file_swap_shadow(b_file *main_file, b_file *shadow_file); - -BLUE_API b_status b_file_read( - b_file *file, size_t offset, size_t len, void *buf, size_t *nr_read); -BLUE_API b_status b_file_write( - b_file *file, size_t offset, size_t len, const void *buf, - size_t *nr_written); - -B_DECLS_END; - -#endif diff --git a/io/include/blue/io/path.h b/io/include/blue/io/path.h deleted file mode 100644 index aa15651..0000000 --- a/io/include/blue/io/path.h +++ /dev/null @@ -1,54 +0,0 @@ -#ifndef BLUE_IO_PATH_H_ -#define BLUE_IO_PATH_H_ - -#include -#include -#include -#include -#include - -B_DECLS_BEGIN; - -#define B_TYPE_PATH (b_path_get_type()) - -B_DECLARE_TYPE(b_path); - -B_TYPE_CLASS_DECLARATION_BEGIN(b_path) -B_TYPE_CLASS_DECLARATION_END(b_path) - -#define B_RV_PATH(cstr) B_RV(b_path_create_from_cstr(cstr)) - -struct b_file_info; - -BLUE_API b_type b_path_get_type(void); - -B_TYPE_DEFAULT_CONSTRUCTOR(b_path, B_TYPE_PATH); - -BLUE_API b_path *b_path_create_root(); -BLUE_API b_path *b_path_create_cwd(); -BLUE_API b_path *b_path_create_from_cstr(const char *path); -BLUE_API b_path *b_path_duplicate(const b_path *path); - -BLUE_API b_path *b_path_join(const b_path *paths[], size_t nr_paths); - -BLUE_API b_path *b_path_make_absolute(const b_path *in); -BLUE_API b_path *b_path_make_relative(const b_path *in); -BLUE_API b_path *b_path_make_canonical(const b_path *in); - -BLUE_API bool b_path_is_absolute(const b_path *path); -BLUE_API bool b_path_exists(const b_path *path); -BLUE_API bool b_path_is_file(const b_path *path); -BLUE_API bool b_path_is_directory(const b_path *path); -BLUE_API b_status b_path_stat(const b_path *path, struct b_file_info *out); -BLUE_API b_status b_path_unlink(const b_path *path); - -BLUE_API enum b_status b_path_get_directory( - const b_path *path, b_path **out_dir_path); -BLUE_API enum b_status b_path_get_filename(const b_path *path, b_string *out_name); - -BLUE_API const char *b_path_ptr(const b_path *path); -BLUE_API size_t b_path_length(const b_path *path); - -B_DECLS_END; - -#endif diff --git a/io/include/blue/io.h b/io/include/fx/io.h similarity index 100% rename from io/include/blue/io.h rename to io/include/fx/io.h diff --git a/io/include/fx/io/directory.h b/io/include/fx/io/directory.h new file mode 100644 index 0000000..6f27760 --- /dev/null +++ b/io/include/fx/io/directory.h @@ -0,0 +1,75 @@ +#ifndef FX_IO_DIRECTORY_H_ +#define FX_IO_DIRECTORY_H_ + +#include +#include +#include +#include +#include +#include + +#define FX_DIRECTORY_ROOT ((fx_directory *)NULL) + +FX_DECLS_BEGIN; + +struct fx_directory_p; + +#define FX_TYPE_DIRECTORY (fx_directory_get_type()) +#define FX_TYPE_DIRECTORY_ITERATOR (fx_directory_iterator_get_type()) + +FX_DECLARE_TYPE(fx_directory); +FX_DECLARE_TYPE(fx_directory_iterator); + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_directory) +FX_TYPE_CLASS_DECLARATION_END(fx_directory) + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_directory_iterator) +FX_TYPE_CLASS_DECLARATION_END(fx_directory_iterator) + +struct z__fx_directory_iterator; + +typedef enum fx_directory_iterator_flags { + FX_DIRECTORY_ITERATE_PARENT_FIRST = 0x01u, + FX_DIRECTORY_ITERATE_PARENT_LAST = 0x02u, +} fx_directory_iterator_flags; + +typedef enum fx_directory_open_flags { + FX_DIRECTORY_OPEN_CREATE = 0x01u, + FX_DIRECTORY_OPEN_CREATE_INTERMEDIATE = 0x03u, + FX_DIRECTORY_OPEN_DELETE_ON_CLOSE = 0x04u, +} fx_directory_open_flags; + +typedef struct fx_directory_entry { + const fx_path *filepath; + char *filename; + fx_file_info info; +} fx_directory_entry; + +FX_API fx_type fx_directory_get_type(void); +FX_API fx_type fx_directory_iterator_get_type(void); + +FX_API fx_result fx_directory_open( + fx_directory *root, const fx_path *path, fx_directory_open_flags flags, + fx_directory **out); +FX_API fx_result fx_directory_open_temp(fx_directory **out); +FX_API const fx_path *fx_directory_get_path(const fx_directory *dir); +FX_API const fx_path *fx_directory_get_rel_path(const fx_directory *dir); +FX_API const char *fx_directory_get_path_cstr(const fx_directory *dir); +FX_API const char *fx_directory_get_rel_path_cstr(const fx_directory *dir); +FX_API fx_result fx_directory_delete(fx_directory *dir); + +FX_API bool fx_directory_path_exists(const fx_directory *root, const fx_path *path); +FX_API bool fx_directory_path_is_file(const fx_directory *root, const fx_path *path); +FX_API bool fx_directory_path_is_directory( + const fx_directory *root, const fx_path *path); +FX_API fx_result fx_directory_path_stat( + const fx_directory *root, const fx_path *path, struct fx_file_info *out); +FX_API fx_result fx_directory_path_unlink( + const fx_directory *root, const fx_path *path); + +FX_API fx_iterator *fx_directory_begin( + fx_directory *dir, fx_directory_iterator_flags flags); + +FX_DECLS_END; + +#endif diff --git a/io/include/fx/io/file.h b/io/include/fx/io/file.h new file mode 100644 index 0000000..b4224be --- /dev/null +++ b/io/include/fx/io/file.h @@ -0,0 +1,80 @@ +#ifndef FX_IO_FILE_H_ +#define FX_IO_FILE_H_ + +#include +#include +#include +#include + +FX_DECLS_BEGIN; + +#define FX_TYPE_FILE (fx_file_get_type()) + +FX_DECLARE_TYPE(fx_file); + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_file) +FX_TYPE_CLASS_DECLARATION_END(fx_file) + +#define FX_OFFSET_CURRENT ((size_t)-1) + +typedef enum fx_seek_basis { + FX_SEEK_BEGINNING, + FX_SEEK_CURRENT, + FX_SEEK_END +} fx_seek_basis; + +typedef enum fx_file_attribute { + FX_FILE_ATTRIB_NORMAL = 0x01u, + FX_FILE_ATTRIB_DIRECTORY = 0x02u, + FX_FILE_ATTRIB_BLOCK_DEVICE = 0x04u, + FX_FILE_ATTRIB_CHAR_DEVICE = 0x08u, + FX_FILE_ATTRIB_SYMLINK = 0x80u, +} fx_file_attribute; + +typedef enum fx_file_mode { + FX_FILE_READ_ONLY = 0x01u, + FX_FILE_WRITE_ONLY = 0x02u, + FX_FILE_READ_WRITE = FX_FILE_READ_ONLY | FX_FILE_WRITE_ONLY, + FX_FILE_TRUNCATE = 0x04u, + FX_FILE_CREATE = 0x08u, + FX_FILE_CREATE_ONLY = 0x10u | FX_FILE_CREATE, + FX_FILE_APPEND = 0x20u, + FX_FILE_BINARY = 0x40u, + FX_FILE_DELETE_ON_CLOSE = 0x80u, + FX_FILE_SHADOW = 0x100u, +} fx_file_mode; + +typedef struct fx_file_info { + fx_file_attribute attrib; + fx_file_mode mode; + + size_t length; +} fx_file_info; + +FX_API fx_type fx_file_get_type(void); + +FX_API fx_result fx_file_open( + FX_TYPE_FWDREF(fx_directory) * root, const FX_TYPE_FWDREF(fx_path) * path, + fx_file_mode mode, fx_file **out); +FX_API fx_result fx_file_open_temp(fx_file_mode mode, fx_file **out); +FX_API fx_result fx_file_open_shadow( + fx_file *original, fx_file_mode mode, fx_file **out); + +FX_API fx_status fx_file_stat(fx_file *file, fx_file_info *out); +FX_API fx_status fx_file_size(fx_file *file, size_t *out_len); +FX_API fx_status fx_file_cursor(fx_file *file, size_t *out_pos); +FX_API fx_status fx_file_resize(fx_file *file, size_t len); +FX_API fx_status fx_file_seek(fx_file *file, long long offset, fx_seek_basis basis); +FX_API const FX_TYPE_FWDREF(fx_path) * fx_file_path(const fx_file *file); + +FX_API fx_status fx_file_swap_shadow(fx_file *main_file, fx_file *shadow_file); + +FX_API fx_status fx_file_read( + fx_file *file, size_t offset, size_t len, void *buf, size_t *nr_read); +FX_API fx_status fx_file_write( + fx_file *file, size_t offset, size_t len, const void *buf, + size_t *nr_written); + +FX_DECLS_END; + +#endif diff --git a/io/include/fx/io/path.h b/io/include/fx/io/path.h new file mode 100644 index 0000000..6b7f092 --- /dev/null +++ b/io/include/fx/io/path.h @@ -0,0 +1,54 @@ +#ifndef FX_IO_PATH_H_ +#define FX_IO_PATH_H_ + +#include +#include +#include +#include +#include + +FX_DECLS_BEGIN; + +#define FX_TYPE_PATH (fx_path_get_type()) + +FX_DECLARE_TYPE(fx_path); + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_path) +FX_TYPE_CLASS_DECLARATION_END(fx_path) + +#define FX_RV_PATH(cstr) FX_RV(fx_path_create_from_cstr(cstr)) + +struct fx_file_info; + +FX_API fx_type fx_path_get_type(void); + +FX_TYPE_DEFAULT_CONSTRUCTOR(fx_path, FX_TYPE_PATH); + +FX_API fx_path *fx_path_create_root(); +FX_API fx_path *fx_path_create_cwd(); +FX_API fx_path *fx_path_create_from_cstr(const char *path); +FX_API fx_path *fx_path_duplicate(const fx_path *path); + +FX_API fx_path *fx_path_join(const fx_path *paths[], size_t nr_paths); + +FX_API fx_path *fx_path_make_absolute(const fx_path *in); +FX_API fx_path *fx_path_make_relative(const fx_path *in); +FX_API fx_path *fx_path_make_canonical(const fx_path *in); + +FX_API bool fx_path_is_absolute(const fx_path *path); +FX_API bool fx_path_exists(const fx_path *path); +FX_API bool fx_path_is_file(const fx_path *path); +FX_API bool fx_path_is_directory(const fx_path *path); +FX_API fx_status fx_path_stat(const fx_path *path, struct fx_file_info *out); +FX_API fx_status fx_path_unlink(const fx_path *path); + +FX_API enum fx_status fx_path_get_directory( + const fx_path *path, fx_path **out_dir_path); +FX_API enum fx_status fx_path_get_filename(const fx_path *path, fx_string *out_name); + +FX_API const char *fx_path_ptr(const fx_path *path); +FX_API size_t fx_path_length(const fx_path *path); + +FX_DECLS_END; + +#endif diff --git a/io/sys/darwin/directory.c b/io/sys/darwin/directory.c index db13f9d..007b15c 100644 --- a/io/sys/darwin/directory.c +++ b/io/sys/darwin/directory.c @@ -1,9 +1,9 @@ #include "misc.h" #include "posix.h" -#include -#include -#include +#include +#include +#include #include #include #include @@ -18,165 +18,165 @@ enum directory_flags { DIRECTORY_DELETE_ON_CLOSE = 0x01u, }; -struct b_directory_p { +struct fx_directory_p { enum directory_flags d_flags; int d_fd; - b_path *d_path_rel; - b_path *d_path_abs; + fx_path *d_path_rel; + fx_path *d_path_abs; }; -struct b_directory_iterator_p { - struct b_directory_p *_p; +struct fx_directory_iterator_p { + struct fx_directory_p *_p; FTS *fts; FTSENT *ent; - b_directory_iterator_flags flags; - b_directory *root; + fx_directory_iterator_flags flags; + fx_directory *root; - b_directory_entry entry; + fx_directory_entry entry; }; /*** PRIVATE FUNCTIONS ********************************************************/ -static const b_path *directory_get_path(const struct b_directory_p *dir) +static const fx_path *directory_get_path(const struct fx_directory_p *dir) { return dir->d_path_abs; } -static const b_path *directory_get_rel_path(const struct b_directory_p *dir) +static const fx_path *directory_get_rel_path(const struct fx_directory_p *dir) { return dir->d_path_rel; } -static const char *directory_get_path_cstr(const struct b_directory_p *dir) +static const char *directory_get_path_cstr(const struct fx_directory_p *dir) { - return b_path_ptr(dir->d_path_abs); + return fx_path_ptr(dir->d_path_abs); } -static const char *directory_get_rel_path_cstr(const struct b_directory_p *dir) +static const char *directory_get_rel_path_cstr(const struct fx_directory_p *dir) { - return b_path_ptr(dir->d_path_rel); + return fx_path_ptr(dir->d_path_rel); } -static b_result directory_delete(b_directory *dir, struct b_directory_p *dir_p) +static fx_result directory_delete(fx_directory *dir, struct fx_directory_p *dir_p) { - enum b_status status = B_SUCCESS; + enum fx_status status = FX_SUCCESS; - b_iterator *it = b_directory_begin(dir, B_DIRECTORY_ITERATE_PARENT_LAST); - while (B_OK(b_iterator_get_status(it))) { - b_iterator_erase(it); + fx_iterator *it = fx_directory_begin(dir, FX_DIRECTORY_ITERATE_PARENT_LAST); + while (FX_OK(fx_iterator_get_status(it))) { + fx_iterator_erase(it); } - status = b_iterator_get_status(it); - if (!B_OK(status) && status != B_ERR_NO_DATA) { - return B_RESULT_STATUS(status); + status = fx_iterator_get_status(it); + if (!FX_OK(status) && status != FX_ERR_NO_DATA) { + return FX_RESULT_STATUS(status); } - status = b_path_unlink(dir_p->d_path_abs); - if (!B_OK(status)) { - return B_RESULT_STATUS(status); + status = fx_path_unlink(dir_p->d_path_abs); + if (!FX_OK(status)) { + return FX_RESULT_STATUS(status); } - return B_RESULT_SUCCESS; + return FX_RESULT_SUCCESS; } static bool directory_path_exists( - const struct b_directory_p *root, const b_path *path) + const struct fx_directory_p *root, const fx_path *path) { - const b_path *parts[] = { + const fx_path *parts[] = { root ? root->d_path_abs : NULL, path, }; - b_path *abs_path = b_path_join(parts, sizeof parts / sizeof parts[0]); + fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]); if (!abs_path) { return false; } - bool result = b_path_exists(abs_path); - b_path_unref(abs_path); + bool result = fx_path_exists(abs_path); + fx_path_unref(abs_path); return result; } static bool directory_path_is_file( - const struct b_directory_p *root, const b_path *path) + const struct fx_directory_p *root, const fx_path *path) { - const b_path *parts[] = { + const fx_path *parts[] = { root ? root->d_path_abs : NULL, path, }; - b_path *abs_path = b_path_join(parts, sizeof parts / sizeof parts[0]); + fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]); if (!abs_path) { return false; } - bool result = b_path_is_file(abs_path); - b_path_unref(abs_path); + bool result = fx_path_is_file(abs_path); + fx_path_unref(abs_path); return result; } static bool directory_path_is_directory( - const struct b_directory_p *root, const b_path *path) + const struct fx_directory_p *root, const fx_path *path) { - const b_path *parts[] = { + const fx_path *parts[] = { root ? root->d_path_abs : NULL, path, }; - b_path *abs_path = b_path_join(parts, sizeof parts / sizeof parts[0]); + fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]); if (!abs_path) { return false; } - bool result = b_path_is_directory(abs_path); - b_path_unref(abs_path); + bool result = fx_path_is_directory(abs_path); + fx_path_unref(abs_path); return result; } -static b_result directory_path_stat( - const struct b_directory_p *root, const b_path *path, - struct b_file_info *out) +static fx_result directory_path_stat( + const struct fx_directory_p *root, const fx_path *path, + struct fx_file_info *out) { - const b_path *parts[] = { + const fx_path *parts[] = { root ? root->d_path_abs : NULL, path, }; - b_path *abs_path = b_path_join(parts, sizeof parts / sizeof parts[0]); + fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]); if (!abs_path) { - return B_RESULT_ERR(NO_MEMORY); + return FX_RESULT_ERR(NO_MEMORY); } - enum b_status status = b_path_stat(abs_path, out); - b_path_unref(abs_path); + enum fx_status status = fx_path_stat(abs_path, out); + fx_path_unref(abs_path); - return B_RESULT_STATUS(status); + return FX_RESULT_STATUS(status); } -static b_result directory_path_unlink( - const struct b_directory_p *root, const b_path *path) +static fx_result directory_path_unlink( + const struct fx_directory_p *root, const fx_path *path) { - const b_path *parts[] = { + const fx_path *parts[] = { root ? root->d_path_abs : NULL, path, }; - b_path *abs_path = b_path_join(parts, sizeof parts / sizeof parts[0]); + fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]); if (!abs_path) { - return B_RESULT_ERR(NO_MEMORY); + return FX_RESULT_ERR(NO_MEMORY); } - enum b_status status = b_path_unlink(abs_path); - b_path_unref(abs_path); + enum fx_status status = fx_path_unlink(abs_path); + fx_path_unref(abs_path); - return B_RESULT_STATUS(status); + return FX_RESULT_STATUS(status); } -static b_result create_directory(struct b_directory_p *root, const char *path) +static fx_result create_directory(struct fx_directory_p *root, const char *path) { int root_fd = root ? root->d_fd : -1; int err; @@ -188,24 +188,24 @@ static b_result create_directory(struct b_directory_p *root, const char *path) } if (err == 0 || errno == EEXIST) { - return B_RESULT_SUCCESS; + return FX_RESULT_SUCCESS; } - return b_result_from_errno_with_subfilepath( - errno, path, directory_get_rel_path_cstr(root), B_ERR_IO_FAILURE); + return fx_result_from_errno_with_subfilepath( + errno, path, directory_get_rel_path_cstr(root), FX_ERR_IO_FAILURE); } -static b_result create_directory_hierarchy( - struct b_directory_p *root, const char *path) +static fx_result create_directory_hierarchy( + struct fx_directory_p *root, const char *path) { int root_fd = root ? root->d_fd : AT_FDCWD; - char *path_buf = b_strdup(path); + char *path_buf = fx_strdup(path); if (!path_buf) { - return B_RESULT_ERR(NO_MEMORY); + return FX_RESULT_ERR(NO_MEMORY); } - b_result result = B_RESULT_SUCCESS; + fx_result result = FX_RESULT_SUCCESS; for (size_t i = 0; path_buf[i]; i++) { if (path_buf[i] != '/') { continue; @@ -215,9 +215,9 @@ static b_result create_directory_hierarchy( int err = mkdirat(root_fd, path_buf, 0755); if (err != 0 && errno != EEXIST) { - result = b_result_from_errno_with_subfilepath( + result = fx_result_from_errno_with_subfilepath( errno, path_buf, directory_get_rel_path_cstr(root), - B_ERR_IO_FAILURE); + FX_ERR_IO_FAILURE); break; } @@ -226,112 +226,112 @@ static b_result create_directory_hierarchy( int err = mkdirat(root_fd, path_buf, 0755); if (err != 0 && errno != EEXIST) { - result = b_result_from_errno_with_subfilepath( + result = fx_result_from_errno_with_subfilepath( errno, path_buf, directory_get_rel_path_cstr(root), - B_ERR_IO_FAILURE); + FX_ERR_IO_FAILURE); } free(path_buf); return result; } -static b_result directory_open( - struct b_directory_p *root, const b_path *path, - b_directory_open_flags flags, b_directory **out) +static fx_result directory_open( + struct fx_directory_p *root, const fx_path *path, + fx_directory_open_flags flags, fx_directory **out) { - enum b_status status = B_SUCCESS; + enum fx_status status = FX_SUCCESS; int root_fd = root ? root->d_fd : AT_FDCWD; - const char *path_cstr = b_path_ptr(path); + const char *path_cstr = fx_path_ptr(path); if (root) { while (*path_cstr == '/') { path_cstr++; } } - b_result result = B_RESULT_SUCCESS; - if ((flags & B_DIRECTORY_OPEN_CREATE_INTERMEDIATE) - == B_DIRECTORY_OPEN_CREATE_INTERMEDIATE) { + fx_result result = FX_RESULT_SUCCESS; + if ((flags & FX_DIRECTORY_OPEN_CREATE_INTERMEDIATE) + == FX_DIRECTORY_OPEN_CREATE_INTERMEDIATE) { result = create_directory_hierarchy(root, path_cstr); - } else if ((flags & B_DIRECTORY_OPEN_CREATE) == B_DIRECTORY_OPEN_CREATE) { + } else if ((flags & FX_DIRECTORY_OPEN_CREATE) == FX_DIRECTORY_OPEN_CREATE) { result = create_directory(root, path_cstr); } - if (b_result_is_error(result)) { - return b_result_propagate(result); + if (fx_result_is_error(result)) { + return fx_result_propagate(result); } int fd = openat(root_fd, path_cstr, O_DIRECTORY); if (fd == -1) { - status = b_status_from_errno(errno, B_ERR_IO_FAILURE); - return B_RESULT_STATUS(status); + status = fx_status_from_errno(errno, FX_ERR_IO_FAILURE); + return FX_RESULT_STATUS(status); } - b_directory *dir = b_object_create(B_TYPE_DIRECTORY); - b_path *cwd = NULL; - struct b_directory_p *p = b_object_get_private(dir, B_TYPE_DIRECTORY); + fx_directory *dir = fx_object_create(FX_TYPE_DIRECTORY); + fx_path *cwd = NULL; + struct fx_directory_p *p = fx_object_get_private(dir, FX_TYPE_DIRECTORY); if (!root) { - cwd = b_path_create_cwd(); + cwd = fx_path_create_cwd(); } - const b_path *parts[] = { + const fx_path *parts[] = { root ? root->d_path_abs : cwd, path, }; - b_path *new_path = b_path_join(parts, sizeof parts / sizeof parts[0]); + fx_path *new_path = fx_path_join(parts, sizeof parts / sizeof parts[0]); if (!new_path) { - return B_RESULT_ERR(NO_MEMORY); + return FX_RESULT_ERR(NO_MEMORY); } if (cwd) { - b_path_unref(cwd); + fx_path_unref(cwd); } p->d_path_abs = new_path; - p->d_path_rel = b_path_duplicate(path); + p->d_path_rel = fx_path_duplicate(path); p->d_fd = fd; - if (flags & B_DIRECTORY_OPEN_DELETE_ON_CLOSE) { + if (flags & FX_DIRECTORY_OPEN_DELETE_ON_CLOSE) { p->d_flags = DIRECTORY_DELETE_ON_CLOSE; } *out = dir; - return B_RESULT_SUCCESS; + return FX_RESULT_SUCCESS; } /*** PUBLIC FUNCTIONS *********************************************************/ -b_result b_directory_open( - b_directory *root, const b_path *path, b_directory_open_flags flags, - b_directory **out) +fx_result fx_directory_open( + fx_directory *root, const fx_path *path, fx_directory_open_flags flags, + fx_directory **out) { - B_CLASS_DISPATCH_STATIC( - B_TYPE_DIRECTORY, directory_open, root, path, flags, out); + FX_CLASS_DISPATCH_STATIC( + FX_TYPE_DIRECTORY, directory_open, root, path, flags, out); } -b_result b_directory_open_temp(b_directory **out) +fx_result fx_directory_open_temp(fx_directory **out) { char name[16]; char path[128]; while (1) { - z__b_io_generate_tmp_filename(name, sizeof name); + z__fx_io_generate_tmp_filename(name, sizeof name); snprintf(path, sizeof path, "/tmp/%s", name); - b_path *rpath = b_path_create_from_cstr(path); + fx_path *rpath = fx_path_create_from_cstr(path); - b_directory *dir = NULL; - b_result status = b_directory_open( - B_DIRECTORY_ROOT, rpath, B_DIRECTORY_OPEN_CREATE, &dir); - struct b_directory_p *p - = b_object_get_private(dir, B_TYPE_DIRECTORY); + fx_directory *dir = NULL; + fx_result status = fx_directory_open( + FX_DIRECTORY_ROOT, rpath, FX_DIRECTORY_OPEN_CREATE, &dir); + struct fx_directory_p *p + = fx_object_get_private(dir, FX_TYPE_DIRECTORY); - if (b_error_get_status_code(status) == B_ERR_NAME_EXISTS) { - b_path_unref(rpath); + if (fx_error_get_status_code(status) == FX_ERR_NAME_EXISTS) { + fx_path_unref(rpath); continue; } @@ -339,82 +339,82 @@ b_result b_directory_open_temp(b_directory **out) p->d_flags |= DIRECTORY_DELETE_ON_CLOSE; } - b_path_unlink(rpath); - b_path_unref(rpath); + fx_path_unlink(rpath); + fx_path_unref(rpath); return status; } } -const b_path *b_directory_get_path(const b_directory *dir) +const fx_path *fx_directory_get_path(const fx_directory *dir) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_DIRECTORY, directory_get_path, dir); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DIRECTORY, directory_get_path, dir); } -const b_path *b_directory_get_rel_path(const b_directory *dir) +const fx_path *fx_directory_get_rel_path(const fx_directory *dir) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_DIRECTORY, directory_get_rel_path, dir); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DIRECTORY, directory_get_rel_path, dir); } -const char *b_directory_get_path_cstr(const b_directory *dir) +const char *fx_directory_get_path_cstr(const fx_directory *dir) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_DIRECTORY, directory_get_path_cstr, dir); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DIRECTORY, directory_get_path_cstr, dir); } -const char *b_directory_get_rel_path_cstr(const b_directory *dir) +const char *fx_directory_get_rel_path_cstr(const fx_directory *dir) { - B_CLASS_DISPATCH_STATIC_0( - B_TYPE_DIRECTORY, directory_get_rel_path_cstr, dir); + FX_CLASS_DISPATCH_STATIC_0( + FX_TYPE_DIRECTORY, directory_get_rel_path_cstr, dir); } -b_result b_directory_delete(b_directory *dir) +fx_result fx_directory_delete(fx_directory *dir) { - struct b_directory_p *p = b_object_get_private(dir, B_TYPE_DIRECTORY); + struct fx_directory_p *p = fx_object_get_private(dir, FX_TYPE_DIRECTORY); p->d_flags |= DIRECTORY_DELETE_ON_CLOSE; - /* TODO allow object release functions to return a b_result value */ - b_directory_unref(dir); - return B_RESULT_SUCCESS; + /* TODO allow object release functions to return a fx_result value */ + fx_directory_unref(dir); + return FX_RESULT_SUCCESS; } -bool b_directory_path_exists(const b_directory *root, const b_path *path) +bool fx_directory_path_exists(const fx_directory *root, const fx_path *path) { - B_CLASS_DISPATCH_STATIC(B_TYPE_DIRECTORY, directory_path_exists, root, path); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_DIRECTORY, directory_path_exists, root, path); } -bool b_directory_path_is_file(const b_directory *root, const b_path *path) +bool fx_directory_path_is_file(const fx_directory *root, const fx_path *path) { - B_CLASS_DISPATCH_STATIC( - B_TYPE_DIRECTORY, directory_path_is_file, root, path); + FX_CLASS_DISPATCH_STATIC( + FX_TYPE_DIRECTORY, directory_path_is_file, root, path); } -bool b_directory_path_is_directory(const b_directory *root, const b_path *path) +bool fx_directory_path_is_directory(const fx_directory *root, const fx_path *path) { - B_CLASS_DISPATCH_STATIC( - B_TYPE_DIRECTORY, directory_path_is_directory, root, path); + FX_CLASS_DISPATCH_STATIC( + FX_TYPE_DIRECTORY, directory_path_is_directory, root, path); } -b_result b_directory_path_stat( - const b_directory *root, const b_path *path, struct b_file_info *out) +fx_result fx_directory_path_stat( + const fx_directory *root, const fx_path *path, struct fx_file_info *out) { - B_CLASS_DISPATCH_STATIC( - B_TYPE_DIRECTORY, directory_path_stat, root, path, out); + FX_CLASS_DISPATCH_STATIC( + FX_TYPE_DIRECTORY, directory_path_stat, root, path, out); } -b_result b_directory_path_unlink(const b_directory *root, const b_path *path) +fx_result fx_directory_path_unlink(const fx_directory *root, const fx_path *path) { - B_CLASS_DISPATCH_STATIC(B_TYPE_DIRECTORY, directory_path_unlink, root, path); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_DIRECTORY, directory_path_unlink, root, path); } /*** VIRTUAL FUNCTIONS ********************************************************/ -static void directory_init(b_object *obj, void *priv) +static void directory_init(fx_object *obj, void *priv) { - struct b_directory_p *dir = priv; + struct fx_directory_p *dir = priv; } -static void directory_fini(b_object *obj, void *priv) +static void directory_fini(fx_object *obj, void *priv) { - struct b_directory_p *dir = priv; + struct fx_directory_p *dir = priv; close(dir->d_fd); @@ -422,7 +422,7 @@ static void directory_fini(b_object *obj, void *priv) directory_delete(obj, dir); } - b_path_unref(dir->d_path_abs); + fx_path_unref(dir->d_path_abs); } /*** ITERATOR FUNCTIONS *******************************************************/ @@ -432,17 +432,17 @@ static int ftsent_compare(const FTSENT **one, const FTSENT **two) return (strcmp((*one)->fts_name, (*two)->fts_name)); } -static void update_iterator_data(struct b_directory_iterator_p *it) +static void update_iterator_data(struct fx_directory_iterator_p *it) { if (it->entry.filepath) { - b_path_unref((b_path *)it->entry.filepath); + fx_path_unref((fx_path *)it->entry.filepath); it->entry.filepath = NULL; } FTSENT *ent = it->ent; - b_path *path = b_path_create_from_cstr( - ent->fts_path + b_path_length(it->_p->d_path_abs) + 1); + fx_path *path = fx_path_create_from_cstr( + ent->fts_path + fx_path_length(it->_p->d_path_abs) + 1); it->entry.filename = ent->fts_name; it->entry.filepath = path; @@ -452,32 +452,32 @@ static void update_iterator_data(struct b_directory_iterator_p *it) it->entry.info.length = ent->fts_statp->st_size; if (S_ISREG(ent->fts_statp->st_mode)) { - it->entry.info.attrib |= B_FILE_ATTRIB_NORMAL; + it->entry.info.attrib |= FX_FILE_ATTRIB_NORMAL; } if (S_ISDIR(ent->fts_statp->st_mode)) { - it->entry.info.attrib |= B_FILE_ATTRIB_DIRECTORY; + it->entry.info.attrib |= FX_FILE_ATTRIB_DIRECTORY; } if (S_ISBLK(ent->fts_statp->st_mode)) { - it->entry.info.attrib |= B_FILE_ATTRIB_BLOCK_DEVICE; + it->entry.info.attrib |= FX_FILE_ATTRIB_BLOCK_DEVICE; } if (S_ISCHR(ent->fts_statp->st_mode)) { - it->entry.info.attrib |= B_FILE_ATTRIB_CHAR_DEVICE; + it->entry.info.attrib |= FX_FILE_ATTRIB_CHAR_DEVICE; } if (S_ISLNK(ent->fts_statp->st_mode)) { - it->entry.info.attrib |= B_FILE_ATTRIB_SYMLINK; + it->entry.info.attrib |= FX_FILE_ATTRIB_SYMLINK; } } -static void iterator_fini(b_object *obj, void *priv) +static void iterator_fini(fx_object *obj, void *priv) { - struct b_directory_iterator_p *it = priv; + struct fx_directory_iterator_p *it = priv; if (it->entry.filepath) { - b_path_unref((b_path *)it->entry.filepath); + fx_path_unref((fx_path *)it->entry.filepath); it->entry.filepath = NULL; } @@ -486,21 +486,21 @@ static void iterator_fini(b_object *obj, void *priv) } } -b_iterator *b_directory_begin( - b_directory *directory, enum b_directory_iterator_flags flags) +fx_iterator *fx_directory_begin( + fx_directory *directory, enum fx_directory_iterator_flags flags) { - b_iterator *it_obj = b_object_create(B_TYPE_DIRECTORY_ITERATOR); - struct b_directory_iterator_p *it - = b_object_get_private(it_obj, B_TYPE_DIRECTORY_ITERATOR); + fx_iterator *it_obj = fx_object_create(FX_TYPE_DIRECTORY_ITERATOR); + struct fx_directory_iterator_p *it + = fx_object_get_private(it_obj, FX_TYPE_DIRECTORY_ITERATOR); it->flags = flags; it->root = directory; - it->_p = b_object_get_private(directory, B_TYPE_DIRECTORY); + it->_p = fx_object_get_private(directory, FX_TYPE_DIRECTORY); int fts_flags = FTS_COMFOLLOW | FTS_NOCHDIR; const char *path_list[] = { - b_path_ptr(it->_p->d_path_abs), + fx_path_ptr(it->_p->d_path_abs), NULL, }; @@ -512,7 +512,7 @@ b_iterator *b_directory_begin( it->ent = fts_read(it->fts); if (!it->ent) { - b_iterator_set_status(it_obj, B_ERR_NO_DATA); + fx_iterator_set_status(it_obj, FX_ERR_NO_DATA); return it_obj; } @@ -527,14 +527,14 @@ b_iterator *b_directory_begin( done = true; break; case FTS_D: - if (it->flags & B_DIRECTORY_ITERATE_PARENT_LAST) { + if (it->flags & FX_DIRECTORY_ITERATE_PARENT_LAST) { continue; } done = true; break; case FTS_DP: - if (it->flags & B_DIRECTORY_ITERATE_PARENT_FIRST) { + if (it->flags & FX_DIRECTORY_ITERATE_PARENT_FIRST) { continue; } done = true; @@ -549,22 +549,22 @@ b_iterator *b_directory_begin( return it_obj; } -static b_iterator *iterator_begin(b_object *obj) +static fx_iterator *iterator_begin(fx_object *obj) { - return b_directory_begin(obj, B_DIRECTORY_ITERATE_PARENT_FIRST); + return fx_directory_begin(obj, FX_DIRECTORY_ITERATE_PARENT_FIRST); } -static const b_iterator *iterator_cbegin(const b_object *obj) +static const fx_iterator *iterator_cbegin(const fx_object *obj) { - return b_directory_begin((b_object *)obj, B_DIRECTORY_ITERATE_PARENT_FIRST); + return fx_directory_begin((fx_object *)obj, FX_DIRECTORY_ITERATE_PARENT_FIRST); } -static enum b_status iterator_move_next(const b_iterator *obj) +static enum fx_status iterator_move_next(const fx_iterator *obj) { - struct b_directory_iterator_p *it - = b_object_get_private(obj, B_TYPE_DIRECTORY_ITERATOR); + struct fx_directory_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR); if (!it || !it->fts) { - return B_ERR_NO_DATA; + return FX_ERR_NO_DATA; } bool done = false; @@ -573,7 +573,7 @@ static enum b_status iterator_move_next(const b_iterator *obj) it->ent = fts_read(it->fts); if (!it->ent) { - return B_ERR_NO_DATA; + return FX_ERR_NO_DATA; } if (it->ent->fts_level == 0) { @@ -587,13 +587,13 @@ static enum b_status iterator_move_next(const b_iterator *obj) done = true; break; case FTS_D: - if (it->flags & B_DIRECTORY_ITERATE_PARENT_LAST) { + if (it->flags & FX_DIRECTORY_ITERATE_PARENT_LAST) { continue; } done = true; break; case FTS_DP: - if (it->flags & B_DIRECTORY_ITERATE_PARENT_FIRST) { + if (it->flags & FX_DIRECTORY_ITERATE_PARENT_FIRST) { continue; } done = true; @@ -605,79 +605,79 @@ static enum b_status iterator_move_next(const b_iterator *obj) } update_iterator_data(it); - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status iterator_erase(b_iterator *obj) +static enum fx_status iterator_erase(fx_iterator *obj) { - struct b_directory_iterator_p *it - = b_object_get_private(obj, B_TYPE_DIRECTORY_ITERATOR); - b_result result = b_directory_path_unlink(it->root, it->entry.filepath); - if (b_result_is_error(result)) { - enum b_status status = b_error_get_status_code(result); - b_error_discard(result); + struct fx_directory_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR); + fx_result result = fx_directory_path_unlink(it->root, it->entry.filepath); + if (fx_result_is_error(result)) { + enum fx_status status = fx_error_get_status_code(result); + fx_error_discard(result); return status; } return iterator_move_next(obj); } -static b_iterator_value iterator_get_value(b_iterator *obj) +static fx_iterator_value iterator_get_value(fx_iterator *obj) { - struct b_directory_iterator_p *it - = b_object_get_private(obj, B_TYPE_DIRECTORY_ITERATOR); + struct fx_directory_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR); - return B_ITERATOR_VALUE_PTR(&it->entry); + return FX_ITERATOR_VALUE_PTR(&it->entry); } -static const b_iterator_value iterator_get_cvalue(const b_iterator *obj) +static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj) { - struct b_directory_iterator_p *it - = b_object_get_private(obj, B_TYPE_DIRECTORY_ITERATOR); + struct fx_directory_iterator_p *it + = fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR); - return B_ITERATOR_VALUE_CPTR(&it->entry); + return FX_ITERATOR_VALUE_CPTR(&it->entry); } /*** CLASS DEFINITION *********************************************************/ -// ---- b_directory DEFINITION -B_TYPE_CLASS_DEFINITION_BEGIN(b_directory) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) +// ---- fx_directory DEFINITION +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_directory) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) - B_TYPE_CLASS_INTERFACE_BEGIN(b_iterable, B_TYPE_ITERABLE) - B_INTERFACE_ENTRY(it_begin) = iterator_begin; - B_INTERFACE_ENTRY(it_cbegin) = iterator_cbegin; - B_TYPE_CLASS_INTERFACE_END(b_iterable, B_TYPE_ITERABLE) -B_TYPE_CLASS_DEFINITION_END(b_directory) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE) + FX_INTERFACE_ENTRY(it_begin) = iterator_begin; + FX_INTERFACE_ENTRY(it_cbegin) = iterator_cbegin; + FX_TYPE_CLASS_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE) +FX_TYPE_CLASS_DEFINITION_END(fx_directory) -B_TYPE_DEFINITION_BEGIN(b_directory) - B_TYPE_ID(0x10d36546, 0x7f96, 0x464b, 0xbc4d, 0xe504b283fa45); - B_TYPE_CLASS(b_directory_class); - B_TYPE_IMPLEMENTS(B_TYPE_ITERABLE); - B_TYPE_INSTANCE_PRIVATE(struct b_directory_p); - B_TYPE_INSTANCE_INIT(directory_init); - B_TYPE_INSTANCE_FINI(directory_fini); -B_TYPE_DEFINITION_END(b_directory) +FX_TYPE_DEFINITION_BEGIN(fx_directory) + FX_TYPE_ID(0x10d36546, 0x7f96, 0x464b, 0xbc4d, 0xe504b283fa45); + FX_TYPE_CLASS(fx_directory_class); + FX_TYPE_IMPLEMENTS(FX_TYPE_ITERABLE); + FX_TYPE_INSTANCE_PRIVATE(struct fx_directory_p); + FX_TYPE_INSTANCE_INIT(directory_init); + FX_TYPE_INSTANCE_FINI(directory_fini); +FX_TYPE_DEFINITION_END(fx_directory) -// ---- b_directory_iterator DEFINITION -B_TYPE_CLASS_DEFINITION_BEGIN(b_directory_iterator) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) +// ---- fx_directory_iterator DEFINITION +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_directory_iterator) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) - B_TYPE_CLASS_INTERFACE_BEGIN(b_iterator, B_TYPE_ITERATOR) - B_INTERFACE_ENTRY(it_move_next) = iterator_move_next; - B_INTERFACE_ENTRY(it_erase) = iterator_erase; - B_INTERFACE_ENTRY(it_get_value) = iterator_get_value; - B_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue; - B_TYPE_CLASS_INTERFACE_END(b_iterator, B_TYPE_ITERATOR) -B_TYPE_CLASS_DEFINITION_END(b_directory_iterator) + FX_TYPE_CLASS_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_CLASS_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR) +FX_TYPE_CLASS_DEFINITION_END(fx_directory_iterator) -B_TYPE_DEFINITION_BEGIN(b_directory_iterator) - B_TYPE_ID(0xc707fce6, 0xc895, 0x4925, 0x8700, 0xa60641dee0cc); - B_TYPE_EXTENDS(B_TYPE_ITERATOR); - B_TYPE_CLASS(b_directory_iterator_class); - B_TYPE_INSTANCE_PRIVATE(struct b_directory_iterator_p); -B_TYPE_DEFINITION_END(b_directory_iterator) +FX_TYPE_DEFINITION_BEGIN(fx_directory_iterator) + FX_TYPE_ID(0xc707fce6, 0xc895, 0x4925, 0x8700, 0xa60641dee0cc); + FX_TYPE_EXTENDS(FX_TYPE_ITERATOR); + FX_TYPE_CLASS(fx_directory_iterator_class); + FX_TYPE_INSTANCE_PRIVATE(struct fx_directory_iterator_p); +FX_TYPE_DEFINITION_END(fx_directory_iterator) diff --git a/io/sys/darwin/file.c b/io/sys/darwin/file.c index 6ae029d..7b0ff29 100644 --- a/io/sys/darwin/file.c +++ b/io/sys/darwin/file.c @@ -1,11 +1,11 @@ #include "misc.h" #include "posix.h" -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #include #include #include @@ -16,270 +16,270 @@ #define CHECK_FLAG(v, f) (((v) & (f)) == (f)) -static enum b_status stream_close(b_stream *); -static enum b_status stream_getc(b_stream *, int *); -static enum b_status stream_read(b_stream *, void *, size_t, size_t *); -static enum b_status stream_write(b_stream *, const void *, size_t, size_t *); -static enum b_status stream_seek(b_stream *, long long, b_stream_seek_origin); -static enum b_status stream_tell(const b_stream *, size_t *); +static enum fx_status stream_close(fx_stream *); +static enum fx_status stream_getc(fx_stream *, int *); +static enum fx_status stream_read(fx_stream *, void *, size_t, size_t *); +static enum fx_status stream_write(fx_stream *, const void *, size_t, size_t *); +static enum fx_status stream_seek(fx_stream *, long long, fx_stream_seek_origin); +static enum fx_status stream_tell(const fx_stream *, size_t *); /*** PRIVATE DATA *************************************************************/ -struct b_file_p { - enum b_file_mode mode; +struct fx_file_p { + enum fx_file_mode mode; int fd; - b_path *path; + fx_path *path; }; /*** PRIVATE FUNCTIONS ********************************************************/ -static unsigned int b_mode_to_unix_mode(enum b_file_mode mode) +static unsigned int fx_mode_to_unix_mode(enum fx_file_mode mode) { unsigned int result = 0; - if (CHECK_FLAG(mode, B_FILE_READ_WRITE)) { + if (CHECK_FLAG(mode, FX_FILE_READ_WRITE)) { result |= O_RDWR; - } else if (CHECK_FLAG(mode, B_FILE_READ_ONLY)) { + } else if (CHECK_FLAG(mode, FX_FILE_READ_ONLY)) { result |= O_RDONLY; - } else if (CHECK_FLAG(mode, B_FILE_WRITE_ONLY)) { + } else if (CHECK_FLAG(mode, FX_FILE_WRITE_ONLY)) { result |= O_WRONLY; } else { return (unsigned int)-1; } - if (CHECK_FLAG(mode, B_FILE_TRUNCATE)) { + if (CHECK_FLAG(mode, FX_FILE_TRUNCATE)) { result |= O_TRUNC; } - if (CHECK_FLAG(mode, B_FILE_CREATE)) { + if (CHECK_FLAG(mode, FX_FILE_CREATE)) { result |= O_CREAT; } - if (CHECK_FLAG(mode, B_FILE_CREATE_ONLY)) { + if (CHECK_FLAG(mode, FX_FILE_CREATE_ONLY)) { result |= O_EXCL; } return result; } -static b_result file_open_shadow( - struct b_file_p *original, enum b_file_mode mode, b_file **out) +static fx_result file_open_shadow( + struct fx_file_p *original, enum fx_file_mode mode, fx_file **out) { - mode |= B_FILE_SHADOW | B_FILE_DELETE_ON_CLOSE | B_FILE_CREATE; + mode |= FX_FILE_SHADOW | FX_FILE_DELETE_ON_CLOSE | FX_FILE_CREATE; - b_path *dir; - b_path_get_directory(original->path, &dir); + fx_path *dir; + fx_path_get_directory(original->path, &dir); - b_string *filename = b_string_create(); - b_path_get_filename(original->path, filename); + fx_string *filename = fx_string_create(); + fx_path_get_filename(original->path, filename); - b_string_prepend_cstr(filename, ".~"); + fx_string_prepend_cstr(filename, ".~"); - b_path *shadow_filename = b_path_create_from_cstr(b_string_ptr(filename)); - b_string_unref(filename); + fx_path *shadow_filename = fx_path_create_from_cstr(fx_string_ptr(filename)); + fx_string_unref(filename); - const b_path *parts[] = { + const fx_path *parts[] = { dir, shadow_filename, }; - b_path *shadow_filepath - = b_path_join(parts, sizeof parts / sizeof parts[0]); - b_path_unref(dir); - b_path_unref(shadow_filename); + fx_path *shadow_filepath + = fx_path_join(parts, sizeof parts / sizeof parts[0]); + fx_path_unref(dir); + fx_path_unref(shadow_filename); - if (b_path_exists(shadow_filepath)) { - b_path_unlink(shadow_filepath); + if (fx_path_exists(shadow_filepath)) { + fx_path_unlink(shadow_filepath); } - b_file *shadow_file; - b_result status = b_file_open( - B_DIRECTORY_ROOT, shadow_filepath, mode, &shadow_file); - b_path_unref(shadow_filepath); + fx_file *shadow_file; + fx_result status = fx_file_open( + FX_DIRECTORY_ROOT, shadow_filepath, mode, &shadow_file); + fx_path_unref(shadow_filepath); - if (b_result_is_error(status)) { + if (fx_result_is_error(status)) { return status; } *out = shadow_file; - return B_RESULT_SUCCESS; + return FX_RESULT_SUCCESS; } -static const b_path *file_path(const struct b_file_p *file) +static const fx_path *file_path(const struct fx_file_p *file) { return file->path; } -static enum b_status file_stat(struct b_file_p *file, struct b_file_info *out) +static enum fx_status file_stat(struct fx_file_p *file, struct fx_file_info *out) { struct stat st; int err = fstat(file->fd, &st); if (err != 0) { - return b_status_from_errno(errno, B_ERR_IO_FAILURE); + return fx_status_from_errno(errno, FX_ERR_IO_FAILURE); } memset(out, 0x0, sizeof *out); - return b_file_info_from_stat(&st, out); + return fx_file_info_from_stat(&st, out); } -static enum b_status file_size(struct b_file_p *file, size_t *out_len) +static enum fx_status file_size(struct fx_file_p *file, size_t *out_len) { off_t cur = lseek(file->fd, 0, SEEK_CUR); if (cur == (off_t)-1) { - return b_status_from_errno(errno, B_ERR_IO_FAILURE); + return fx_status_from_errno(errno, FX_ERR_IO_FAILURE); } off_t len = lseek(file->fd, 0, SEEK_END); if (len == (off_t)-1) { - return b_status_from_errno(errno, B_ERR_IO_FAILURE); + return fx_status_from_errno(errno, FX_ERR_IO_FAILURE); } cur = lseek(file->fd, cur, SEEK_SET); if (cur == (off_t)-1) { - return b_status_from_errno(errno, B_ERR_IO_FAILURE); + return fx_status_from_errno(errno, FX_ERR_IO_FAILURE); } *out_len = (size_t)len; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status file_cursor(struct b_file_p *file, size_t *out_pos) +static enum fx_status file_cursor(struct fx_file_p *file, size_t *out_pos) { off_t cur = lseek(file->fd, 0, SEEK_CUR); if (cur == (off_t)-1) { - return b_status_from_errno(errno, B_ERR_IO_FAILURE); + return fx_status_from_errno(errno, FX_ERR_IO_FAILURE); } *out_pos = (size_t)cur; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status file_resize(struct b_file_p *file, size_t len) +static enum fx_status file_resize(struct fx_file_p *file, size_t len) { int err = ftruncate(file->fd, len); if (err == 0) { - return B_SUCCESS; + return FX_SUCCESS; } - return b_status_from_errno(errno, B_ERR_IO_FAILURE); + return fx_status_from_errno(errno, FX_ERR_IO_FAILURE); } -static enum b_status file_seek( - struct b_file_p *file, long long offset, enum b_seek_basis basis) +static enum fx_status file_seek( + struct fx_file_p *file, long long offset, enum fx_seek_basis basis) { int whence; switch (basis) { - case B_SEEK_BEGINNING: + case FX_SEEK_BEGINNING: whence = SEEK_SET; break; - case B_SEEK_CURRENT: + case FX_SEEK_CURRENT: whence = SEEK_CUR; break; - case B_SEEK_END: + case FX_SEEK_END: whence = SEEK_END; break; default: - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } int err = lseek(file->fd, offset, whence); if (err == (off_t)-1) { - return b_status_from_errno(errno, B_ERR_IO_FAILURE); + return fx_status_from_errno(errno, FX_ERR_IO_FAILURE); } - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status file_swap_shadow( - struct b_file_p *main_file, struct b_file_p *shadow_file) +static enum fx_status file_swap_shadow( + struct fx_file_p *main_file, struct fx_file_p *shadow_file) { - if (main_file->mode & B_FILE_SHADOW) { - return B_ERR_NOT_SUPPORTED; + if (main_file->mode & FX_FILE_SHADOW) { + return FX_ERR_NOT_SUPPORTED; } - if (!(shadow_file->mode & B_FILE_SHADOW)) { - return B_ERR_NOT_SUPPORTED; + if (!(shadow_file->mode & FX_FILE_SHADOW)) { + return FX_ERR_NOT_SUPPORTED; } - b_path *dir_path; - b_path_get_directory(main_file->path, &dir_path); + fx_path *dir_path; + fx_path_get_directory(main_file->path, &dir_path); - b_path *tmp_path = NULL; + fx_path *tmp_path = NULL; while (1) { char tmp_name[16]; - z__b_io_generate_tmp_filename(tmp_name, sizeof tmp_name); - b_path *tmp_name_p = b_path_create_from_cstr(tmp_name); + z__fx_io_generate_tmp_filename(tmp_name, sizeof tmp_name); + fx_path *tmp_name_p = fx_path_create_from_cstr(tmp_name); - const b_path *parts[] = { + const fx_path *parts[] = { dir_path, tmp_name_p, }; - tmp_path = b_path_join(parts, sizeof parts / sizeof parts[0]); + tmp_path = fx_path_join(parts, sizeof parts / sizeof parts[0]); - b_path_unref(tmp_name_p); + fx_path_unref(tmp_name_p); - if (!b_path_exists(tmp_path)) { + if (!fx_path_exists(tmp_path)) { break; } - b_path_unref(tmp_path); + fx_path_unref(tmp_path); tmp_path = NULL; } - b_path_unref(dir_path); + fx_path_unref(dir_path); int err; - err = rename(b_path_ptr(main_file->path), b_path_ptr(tmp_path)); - err = rename(b_path_ptr(shadow_file->path), b_path_ptr(main_file->path)); - err = rename(b_path_ptr(tmp_path), b_path_ptr(shadow_file->path)); + err = rename(fx_path_ptr(main_file->path), fx_path_ptr(tmp_path)); + err = rename(fx_path_ptr(shadow_file->path), fx_path_ptr(main_file->path)); + err = rename(fx_path_ptr(tmp_path), fx_path_ptr(shadow_file->path)); - b_path_unref(tmp_path); + fx_path_unref(tmp_path); int fd = main_file->fd; main_file->fd = shadow_file->fd; shadow_file->fd = fd; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status file_read( - struct b_file_p *file, size_t offset, size_t len, void *buf, size_t *nr_read) +static enum fx_status file_read( + struct fx_file_p *file, size_t offset, size_t len, void *buf, size_t *nr_read) { - if (offset != B_OFFSET_CURRENT) { + if (offset != FX_OFFSET_CURRENT) { lseek(file->fd, offset, SEEK_SET); } long r = read(file->fd, buf, len); - enum b_status status = B_SUCCESS; + enum fx_status status = FX_SUCCESS; if (r < 0) { - status = b_status_from_errno(errno, B_ERR_IO_FAILURE); + status = fx_status_from_errno(errno, FX_ERR_IO_FAILURE); } *nr_read = r; return status; } -static enum b_status file_write( - struct b_file_p *file, size_t offset, size_t len, const void *buf, +static enum fx_status file_write( + struct fx_file_p *file, size_t offset, size_t len, const void *buf, size_t *nr_written) { - if (offset != B_OFFSET_CURRENT) { + if (offset != FX_OFFSET_CURRENT) { lseek(file->fd, offset, SEEK_SET); } long w = write(file->fd, buf, len); - enum b_status status = B_SUCCESS; + enum fx_status status = FX_SUCCESS; if (w < 0) { - status = b_status_from_errno(errno, B_ERR_IO_FAILURE); + status = fx_status_from_errno(errno, FX_ERR_IO_FAILURE); } *nr_written = w; @@ -288,151 +288,151 @@ static enum b_status file_write( /*** STREAM FUNCTIONS *********************************************************/ -static enum b_status stream_close(b_stream *stream) +static enum fx_status stream_close(fx_stream *stream) { - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status stream_getc(b_stream *stream, int *out) +static enum fx_status stream_getc(fx_stream *stream, int *out) { - struct b_file_p *file = b_object_get_private(stream, B_TYPE_FILE); + struct fx_file_p *file = fx_object_get_private(stream, FX_TYPE_FILE); char c; size_t nr_read = 0; - enum b_status status - = file_read(file, B_OFFSET_CURRENT, sizeof c, &c, &nr_read); - if (status != B_SUCCESS) { + enum fx_status status + = file_read(file, FX_OFFSET_CURRENT, sizeof c, &c, &nr_read); + if (status != FX_SUCCESS) { return status; } if (nr_read == 0) { - return B_ERR_NO_DATA; + return FX_ERR_NO_DATA; } *out = c; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status stream_read( - b_stream *stream, void *buf, size_t max, size_t *nr_read) +static enum fx_status stream_read( + fx_stream *stream, void *buf, size_t max, size_t *nr_read) { - struct b_file_p *file = b_object_get_private(stream, B_TYPE_FILE); + struct fx_file_p *file = fx_object_get_private(stream, FX_TYPE_FILE); - enum b_status status - = file_read(file, B_OFFSET_CURRENT, max, buf, nr_read); + enum fx_status status + = file_read(file, FX_OFFSET_CURRENT, max, buf, nr_read); return status; } -static enum b_status stream_write( - b_stream *stream, const void *buf, size_t count, size_t *nr_written) +static enum fx_status stream_write( + fx_stream *stream, const void *buf, size_t count, size_t *nr_written) { - struct b_file_p *file = b_object_get_private(stream, B_TYPE_FILE); + struct fx_file_p *file = fx_object_get_private(stream, FX_TYPE_FILE); - enum b_status status - = file_write(file, B_OFFSET_CURRENT, count, buf, nr_written); + enum fx_status status + = file_write(file, FX_OFFSET_CURRENT, count, buf, nr_written); return status; } -static enum b_status stream_seek( - b_stream *stream, long long offset, b_stream_seek_origin origin) +static enum fx_status stream_seek( + fx_stream *stream, long long offset, fx_stream_seek_origin origin) { - b_seek_basis basis; + fx_seek_basis basis; switch (origin) { - case B_STREAM_SEEK_START: - basis = B_SEEK_BEGINNING; + case FX_STREAM_SEEK_START: + basis = FX_SEEK_BEGINNING; break; - case B_STREAM_SEEK_CURRENT: - basis = B_SEEK_CURRENT; + case FX_STREAM_SEEK_CURRENT: + basis = FX_SEEK_CURRENT; break; - case B_STREAM_SEEK_END: - basis = B_SEEK_END; + case FX_STREAM_SEEK_END: + basis = FX_SEEK_END; break; default: - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } - struct b_file_p *file = b_object_get_private(stream, B_TYPE_FILE); + struct fx_file_p *file = fx_object_get_private(stream, FX_TYPE_FILE); return file_seek(file, offset, basis); } -static enum b_status stream_tell(const b_stream *stream, size_t *pos) +static enum fx_status stream_tell(const fx_stream *stream, size_t *pos) { - const struct b_file_p *file = b_object_get_private(stream, B_TYPE_FILE); + const struct fx_file_p *file = fx_object_get_private(stream, FX_TYPE_FILE); off_t v = lseek(file->fd, 0, SEEK_CUR); if (v == (off_t)-1) { - return b_status_from_errno(errno, B_ERR_IO_FAILURE); + return fx_status_from_errno(errno, FX_ERR_IO_FAILURE); } *pos = v; - return B_SUCCESS; + return FX_SUCCESS; } /*** PUBLIC FUNCTIONS *********************************************************/ -b_result b_file_open( - b_directory *root, const b_path *path, enum b_file_mode mode, b_file **out) +fx_result fx_file_open( + fx_directory *root, const fx_path *path, enum fx_file_mode mode, fx_file **out) { - const b_path *file_path = path; - unsigned int flags = b_mode_to_unix_mode(mode); + const fx_path *file_path = path; + unsigned int flags = fx_mode_to_unix_mode(mode); if (flags == (unsigned int)-1) { - return B_RESULT_ERR(INVALID_ARGUMENT); + return FX_RESULT_ERR(INVALID_ARGUMENT); } - const b_path *root_path = NULL; + const fx_path *root_path = NULL; bool free_root_path = false; if (root) { - root_path = b_directory_get_path(root); + root_path = fx_directory_get_path(root); } else { - root_path = b_path_create_cwd(); + root_path = fx_path_create_cwd(); free_root_path = true; } - const b_path *parts[] = { + const fx_path *parts[] = { root_path, file_path, }; - b_path *abs_path = b_path_join(parts, sizeof parts / sizeof parts[0]); + fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]); if (free_root_path) { - b_path_unref((b_path *)root_path); + fx_path_unref((fx_path *)root_path); } if (!abs_path) { - return B_RESULT_ERR(NO_MEMORY); + return FX_RESULT_ERR(NO_MEMORY); } - int fd = open(b_path_ptr(abs_path), flags, 0644); + int fd = open(fx_path_ptr(abs_path), flags, 0644); if (fd == -1) { - b_path_unref(abs_path); - return B_RESULT_STATUS( - b_status_from_errno(errno, B_ERR_IO_FAILURE)); + fx_path_unref(abs_path); + return FX_RESULT_STATUS( + fx_status_from_errno(errno, FX_ERR_IO_FAILURE)); } - b_file *file = b_object_create(B_TYPE_FILE); + fx_file *file = fx_object_create(FX_TYPE_FILE); if (!file) { close(fd); - b_path_unref(abs_path); - return B_RESULT_ERR(NO_MEMORY); + fx_path_unref(abs_path); + return FX_RESULT_ERR(NO_MEMORY); } - struct b_file_p *p = b_object_get_private(file, B_TYPE_FILE); - b_stream_cfg *cfg = b_object_get_protected(file, B_TYPE_STREAM); + struct fx_file_p *p = fx_object_get_private(file, FX_TYPE_FILE); + fx_stream_cfg *cfg = fx_object_get_protected(file, FX_TYPE_STREAM); - if (mode & B_FILE_READ_ONLY) { - cfg->s_mode |= B_STREAM_READ; + if (mode & FX_FILE_READ_ONLY) { + cfg->s_mode |= FX_STREAM_READ; } - if (mode & B_FILE_WRITE_ONLY) { - cfg->s_mode |= B_STREAM_WRITE; + if (mode & FX_FILE_WRITE_ONLY) { + cfg->s_mode |= FX_STREAM_WRITE; } - if (mode & B_FILE_BINARY) { - cfg->s_mode |= B_STREAM_BINARY; + if (mode & FX_FILE_BINARY) { + cfg->s_mode |= FX_STREAM_BINARY; } p->fd = fd; @@ -440,133 +440,133 @@ b_result b_file_open( p->mode = mode; *out = file; - return B_RESULT_SUCCESS; + return FX_RESULT_SUCCESS; } -b_result b_file_open_temp(enum b_file_mode mode, b_file **out) +fx_result fx_file_open_temp(enum fx_file_mode mode, fx_file **out) { - mode |= B_FILE_DELETE_ON_CLOSE; + mode |= FX_FILE_DELETE_ON_CLOSE; char name[16]; char path[128]; while (1) { - z__b_io_generate_tmp_filename(name, sizeof name); + z__fx_io_generate_tmp_filename(name, sizeof name); snprintf(path, sizeof path, "/tmp/%s", name); - b_path *rpath = b_path_create_from_cstr(path); + fx_path *rpath = fx_path_create_from_cstr(path); - b_result status = b_file_open( - B_DIRECTORY_ROOT, rpath, mode | B_FILE_CREATE_ONLY, out); + fx_result status = fx_file_open( + FX_DIRECTORY_ROOT, rpath, mode | FX_FILE_CREATE_ONLY, out); - if (b_error_get_status_code(status) == B_ERR_NAME_EXISTS) { - b_path_unref(rpath); + if (fx_error_get_status_code(status) == FX_ERR_NAME_EXISTS) { + fx_path_unref(rpath); continue; } - b_path_unlink(rpath); - b_path_unref(rpath); + fx_path_unlink(rpath); + fx_path_unref(rpath); return status; } } -b_result b_file_open_shadow(b_file *original, enum b_file_mode mode, b_file **out) +fx_result fx_file_open_shadow(fx_file *original, enum fx_file_mode mode, fx_file **out) { - B_CLASS_DISPATCH_STATIC(B_TYPE_FILE, file_open_shadow, original, mode, out); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_open_shadow, original, mode, out); } -const b_path *b_file_path(const b_file *file) +const fx_path *fx_file_path(const fx_file *file) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_FILE, file_path, file); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_FILE, file_path, file); } -enum b_status b_file_stat(b_file *file, struct b_file_info *out) +enum fx_status fx_file_stat(fx_file *file, struct fx_file_info *out) { - B_CLASS_DISPATCH_STATIC(B_TYPE_FILE, file_stat, file, out); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_stat, file, out); } -enum b_status b_file_size(b_file *file, size_t *out_len) +enum fx_status fx_file_size(fx_file *file, size_t *out_len) { - B_CLASS_DISPATCH_STATIC(B_TYPE_FILE, file_size, file, out_len); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_size, file, out_len); } -enum b_status b_file_cursor(b_file *file, size_t *out_pos) +enum fx_status fx_file_cursor(fx_file *file, size_t *out_pos) { - B_CLASS_DISPATCH_STATIC(B_TYPE_FILE, file_cursor, file, out_pos); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_cursor, file, out_pos); } -enum b_status b_file_resize(b_file *file, size_t len) +enum fx_status fx_file_resize(fx_file *file, size_t len) { - B_CLASS_DISPATCH_STATIC(B_TYPE_FILE, file_resize, file, len); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_resize, file, len); } -enum b_status b_file_seek(b_file *file, long long offset, enum b_seek_basis basis) +enum fx_status fx_file_seek(fx_file *file, long long offset, enum fx_seek_basis basis) { - B_CLASS_DISPATCH_STATIC(B_TYPE_FILE, file_seek, file, offset, basis); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_seek, file, offset, basis); } -enum b_status b_file_swap_shadow(b_file *main_file, b_file *shadow_file) +enum fx_status fx_file_swap_shadow(fx_file *main_file, fx_file *shadow_file) { - struct b_file_p *main_p = b_object_get_private(main_file, B_TYPE_FILE); - struct b_file_p *shadow_p = b_object_get_private(main_file, B_TYPE_FILE); + struct fx_file_p *main_p = fx_object_get_private(main_file, FX_TYPE_FILE); + struct fx_file_p *shadow_p = fx_object_get_private(main_file, FX_TYPE_FILE); return file_swap_shadow(main_p, shadow_p); } -enum b_status b_file_read( - b_file *file, size_t offset, size_t len, void *buf, size_t *nr_read) +enum fx_status fx_file_read( + fx_file *file, size_t offset, size_t len, void *buf, size_t *nr_read) { - B_CLASS_DISPATCH_STATIC( - B_TYPE_FILE, file_read, file, offset, len, buf, nr_read); + FX_CLASS_DISPATCH_STATIC( + FX_TYPE_FILE, file_read, file, offset, len, buf, nr_read); } -enum b_status b_file_write( - b_file *file, size_t offset, size_t len, const void *buf, size_t *nr_written) +enum fx_status fx_file_write( + fx_file *file, size_t offset, size_t len, const void *buf, size_t *nr_written) { - B_CLASS_DISPATCH_STATIC( - B_TYPE_FILE, file_write, file, offset, len, buf, nr_written); + FX_CLASS_DISPATCH_STATIC( + FX_TYPE_FILE, file_write, file, offset, len, buf, nr_written); } /*** VIRTUAL FUNCTIONS ********************************************************/ -static void file_init(b_object *obj, void *priv) +static void file_init(fx_object *obj, void *priv) { - struct b_file_p *file = priv; + struct fx_file_p *file = priv; } -static void file_fini(b_object *obj, void *priv) +static void file_fini(fx_object *obj, void *priv) { - struct b_file_p *file = priv; + struct fx_file_p *file = priv; close(file->fd); - if (file->mode & B_FILE_DELETE_ON_CLOSE) { - b_path_unlink(file->path); + if (file->mode & FX_FILE_DELETE_ON_CLOSE) { + fx_path_unlink(file->path); } - b_path_unref(file->path); + fx_path_unref(file->path); } /*** CLASS DEFINITION *********************************************************/ -B_TYPE_CLASS_DEFINITION_BEGIN(b_file) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_file) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) - B_TYPE_CLASS_INTERFACE_BEGIN(b_stream, B_TYPE_STREAM) - B_INTERFACE_ENTRY(s_close) = stream_close; - B_INTERFACE_ENTRY(s_getc) = stream_getc; - B_INTERFACE_ENTRY(s_read) = stream_read; - B_INTERFACE_ENTRY(s_write) = stream_write; - B_INTERFACE_ENTRY(s_seek) = stream_seek; - B_INTERFACE_ENTRY(s_tell) = stream_tell; - B_TYPE_CLASS_INTERFACE_END(b_stream, B_TYPE_STREAM) -B_TYPE_CLASS_DEFINITION_END(b_file) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_stream, FX_TYPE_STREAM) + FX_INTERFACE_ENTRY(s_close) = stream_close; + FX_INTERFACE_ENTRY(s_getc) = stream_getc; + FX_INTERFACE_ENTRY(s_read) = stream_read; + FX_INTERFACE_ENTRY(s_write) = stream_write; + FX_INTERFACE_ENTRY(s_seek) = stream_seek; + FX_INTERFACE_ENTRY(s_tell) = stream_tell; + FX_TYPE_CLASS_INTERFACE_END(fx_stream, FX_TYPE_STREAM) +FX_TYPE_CLASS_DEFINITION_END(fx_file) -B_TYPE_DEFINITION_BEGIN(b_file) - B_TYPE_ID(0x495a73f6, 0xb8c3, 0x4e17, 0xb5f4, 0x6fc321f67c7b); - B_TYPE_EXTENDS(B_TYPE_STREAM); - B_TYPE_CLASS(b_file_class); - B_TYPE_INSTANCE_PRIVATE(struct b_file_p); - B_TYPE_INSTANCE_INIT(file_init); - B_TYPE_INSTANCE_FINI(file_fini); -B_TYPE_DEFINITION_END(b_file) +FX_TYPE_DEFINITION_BEGIN(fx_file) + FX_TYPE_ID(0x495a73f6, 0xb8c3, 0x4e17, 0xb5f4, 0x6fc321f67c7b); + FX_TYPE_EXTENDS(FX_TYPE_STREAM); + FX_TYPE_CLASS(fx_file_class); + FX_TYPE_INSTANCE_PRIVATE(struct fx_file_p); + FX_TYPE_INSTANCE_INIT(file_init); + FX_TYPE_INSTANCE_FINI(file_fini); +FX_TYPE_DEFINITION_END(fx_file) diff --git a/io/sys/darwin/misc.c b/io/sys/darwin/misc.c index 005aa8d..b70ab49 100644 --- a/io/sys/darwin/misc.c +++ b/io/sys/darwin/misc.c @@ -1,8 +1,8 @@ #include "misc.h" -#include +#include -void z__b_io_generate_tmp_filename(char *out, size_t len) +void z__fx_io_generate_tmp_filename(char *out, size_t len) { static const char *alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" @@ -10,10 +10,10 @@ void z__b_io_generate_tmp_filename(char *out, size_t len) "89+=-_."; static const size_t alphabet_len = 67; - b_random_ctx *ctx = b_random_global_ctx(); + fx_random_ctx *ctx = fx_random_global_ctx(); for (size_t i = 0; i < len; i++) { - int v = b_random_next_int64(ctx) % alphabet_len; + int v = fx_random_next_int64(ctx) % alphabet_len; out[i] = alphabet[v]; } diff --git a/io/sys/darwin/misc.h b/io/sys/darwin/misc.h index 82106f5..303affe 100644 --- a/io/sys/darwin/misc.h +++ b/io/sys/darwin/misc.h @@ -3,6 +3,6 @@ #include -extern void z__b_io_generate_tmp_filename(char *out, size_t len); +extern void z__fx_io_generate_tmp_filename(char *out, size_t len); #endif diff --git a/io/sys/darwin/path.c b/io/sys/darwin/path.c index 600bb1d..42d3d35 100644 --- a/io/sys/darwin/path.c +++ b/io/sys/darwin/path.c @@ -1,8 +1,8 @@ #include "posix.h" -#include -#include -#include +#include +#include +#include #include #include #include @@ -13,142 +13,142 @@ /*** PRIVATE DATA *************************************************************/ -struct b_path_p { - b_string *p_pathstr; +struct fx_path_p { + fx_string *p_pathstr; }; /*** PRIVATE FUNCTIONS ********************************************************/ -static b_path *path_make_absolute(const struct b_path_p *in) +static fx_path *path_make_absolute(const struct fx_path_p *in) { return NULL; } -static b_path *path_make_relative(const struct b_path_p *in) +static fx_path *path_make_relative(const struct fx_path_p *in) { return NULL; } -static b_path *path_make_canonical(const struct b_path_p *in) +static fx_path *path_make_canonical(const struct fx_path_p *in) { return NULL; } -static bool path_is_absolute(const struct b_path_p *path) +static bool path_is_absolute(const struct fx_path_p *path) { - const char *s = b_string_ptr(path->p_pathstr); + const char *s = fx_string_ptr(path->p_pathstr); return s[0] == '/'; } -static const char *path_ptr(const struct b_path_p *path) +static const char *path_ptr(const struct fx_path_p *path) { - return b_string_ptr(path->p_pathstr); + return fx_string_ptr(path->p_pathstr); } -static enum b_status path_stat(const struct b_path_p *path, struct b_file_info *out) +static enum fx_status path_stat(const struct fx_path_p *path, struct fx_file_info *out) { struct stat st; int err = stat(path_ptr(path), &st); if (err != 0) { - return b_status_from_errno(errno, B_ERR_IO_FAILURE); + return fx_status_from_errno(errno, FX_ERR_IO_FAILURE); } memset(out, 0x0, sizeof *out); - return b_file_info_from_stat(&st, out); + return fx_file_info_from_stat(&st, out); } -static bool path_exists(const struct b_path_p *path) +static bool path_exists(const struct fx_path_p *path) { - b_file_info info; - if (!B_OK(path_stat(path, &info))) { + fx_file_info info; + if (!FX_OK(path_stat(path, &info))) { return false; } return true; } -static bool path_is_file(const struct b_path_p *path) +static bool path_is_file(const struct fx_path_p *path) { - b_file_info info; - if (!B_OK(path_stat(path, &info))) { + fx_file_info info; + if (!FX_OK(path_stat(path, &info))) { return false; } - return (info.attrib & B_FILE_ATTRIB_NORMAL) != 0; + return (info.attrib & FX_FILE_ATTRIB_NORMAL) != 0; } -static bool path_is_directory(const struct b_path_p *path) +static bool path_is_directory(const struct fx_path_p *path) { - b_file_info info; - if (!B_OK(path_stat(path, &info))) { + fx_file_info info; + if (!FX_OK(path_stat(path, &info))) { return false; } - return (info.attrib & B_FILE_ATTRIB_DIRECTORY) != 0; + return (info.attrib & FX_FILE_ATTRIB_DIRECTORY) != 0; } -static void append_path(struct b_path_p *dest, const struct b_path_p *src) +static void append_path(struct fx_path_p *dest, const struct fx_path_p *src) { if (path_is_absolute(src)) { - b_string_clear(dest->p_pathstr); - b_string_append_s(dest->p_pathstr, src->p_pathstr); + fx_string_clear(dest->p_pathstr); + fx_string_append_s(dest->p_pathstr, src->p_pathstr); return; } - if (b_string_get_size(dest->p_pathstr, B_STRLEN_NORMAL) > 0 - && b_string_back(dest->p_pathstr) != '/' - && b_string_front(src->p_pathstr) != '/') { + if (fx_string_get_size(dest->p_pathstr, FX_STRLEN_NORMAL) > 0 + && fx_string_back(dest->p_pathstr) != '/' + && fx_string_front(src->p_pathstr) != '/') { char s[] = {'/', 0}; - b_string_append_cstr(dest->p_pathstr, s); + fx_string_append_cstr(dest->p_pathstr, s); } - b_string_append_s(dest->p_pathstr, src->p_pathstr); + fx_string_append_s(dest->p_pathstr, src->p_pathstr); } -static enum b_status path_unlink(const struct b_path_p *path) +static enum fx_status path_unlink(const struct fx_path_p *path) { - int err = remove(b_string_ptr(path->p_pathstr)); - return err == 0 ? B_SUCCESS : b_status_from_errno(errno, B_ERR_IO_FAILURE); + int err = remove(fx_string_ptr(path->p_pathstr)); + return err == 0 ? FX_SUCCESS : fx_status_from_errno(errno, FX_ERR_IO_FAILURE); } -static enum b_status path_get_directory( - const struct b_path_p *path, b_path **out_dir_path) +static enum fx_status path_get_directory( + const struct fx_path_p *path, fx_path **out_dir_path) { - b_string *path_str = path->p_pathstr; - long len = b_string_get_size(path_str, B_STRLEN_NORMAL); + fx_string *path_str = path->p_pathstr; + long len = fx_string_get_size(path_str, FX_STRLEN_NORMAL); - const char *path_cstr = b_string_ptr(path_str); + const char *path_cstr = fx_string_ptr(path_str); char *sep = strrchr(path_cstr, '/'); if (!sep) { - *out_dir_path = b_path_create(); - return B_SUCCESS; + *out_dir_path = fx_path_create(); + return FX_SUCCESS; } size_t dir_path_len = (size_t)(sep - path_cstr); - b_string *dir_path_s = b_string_substr(path_str, 0, dir_path_len); + fx_string *dir_path_s = fx_string_substr(path_str, 0, dir_path_len); - b_path *dir_path = b_path_create_from_cstr(b_string_ptr(dir_path_s)); - b_string_unref(dir_path_s); + fx_path *dir_path = fx_path_create_from_cstr(fx_string_ptr(dir_path_s)); + fx_string_unref(dir_path_s); *out_dir_path = dir_path; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status path_get_filename( - const struct b_path_p *path, b_string *out_name) +static enum fx_status path_get_filename( + const struct fx_path_p *path, fx_string *out_name) { - b_string *path_str = path->p_pathstr; - long len = b_string_get_size(path_str, B_STRLEN_NORMAL); + fx_string *path_str = path->p_pathstr; + long len = fx_string_get_size(path_str, FX_STRLEN_NORMAL); - char *sep = strrchr(b_string_ptr(path_str), '/'); + char *sep = strrchr(fx_string_ptr(path_str), '/'); if (!sep) { - b_string_append_s(out_name, path_str); - return B_SUCCESS; + fx_string_append_s(out_name, path_str); + return FX_SUCCESS; } const char *filename = sep; @@ -157,43 +157,43 @@ static enum b_status path_get_filename( } if (*filename == '\0') { - b_string_clear(out_name); - return B_SUCCESS; + fx_string_clear(out_name); + return FX_SUCCESS; } - b_string_append_cstr(out_name, filename); + fx_string_append_cstr(out_name, filename); - return B_SUCCESS; + return FX_SUCCESS; } -static size_t path_length(const struct b_path_p *path) +static size_t path_length(const struct fx_path_p *path) { - return b_string_get_size(path->p_pathstr, B_STRLEN_NORMAL); + return fx_string_get_size(path->p_pathstr, FX_STRLEN_NORMAL); } /*** PUBLIC FUNCTIONS *********************************************************/ -b_path *b_path_create_root() +fx_path *fx_path_create_root() { const char *system_drive = "/"; size_t system_drive_len = strlen(system_drive); - b_path *path = b_path_create(); + fx_path *path = fx_path_create(); if (!path) { return NULL; } - struct b_path_p *p = b_object_get_private(path, B_TYPE_PATH); + struct fx_path_p *p = fx_object_get_private(path, FX_TYPE_PATH); - b_string_append_cstr(p->p_pathstr, system_drive); + fx_string_append_cstr(p->p_pathstr, system_drive); if (system_drive[system_drive_len - 1] != '\\') { - b_string_append_c(p->p_pathstr, '\\'); + fx_string_append_c(p->p_pathstr, '\\'); } return path; } -b_path *b_path_create_cwd() +fx_path *fx_path_create_cwd() { const long buf_len = 2048; char *buf = malloc(buf_len); @@ -206,28 +206,28 @@ b_path *b_path_create_cwd() return NULL; } - b_path *path = b_path_create(); + fx_path *path = fx_path_create(); if (!path) { free(buf); return NULL; } - struct b_path_p *p = b_object_get_private(path, B_TYPE_PATH); + struct fx_path_p *p = fx_object_get_private(path, FX_TYPE_PATH); - b_string_append_cstr(p->p_pathstr, buf); + fx_string_append_cstr(p->p_pathstr, buf); free(buf); return path; } -b_path *b_path_create_from_cstr(const char *cstr) +fx_path *fx_path_create_from_cstr(const char *cstr) { - b_path *path = b_path_create(); + fx_path *path = fx_path_create(); if (!path) { return NULL; } - struct b_path_p *p = b_object_get_private(path, B_TYPE_PATH); + struct fx_path_p *p = fx_object_get_private(path, FX_TYPE_PATH); char prev = 0; @@ -241,49 +241,49 @@ b_path *b_path_create_from_cstr(const char *cstr) continue; } - b_string_append_c(p->p_pathstr, c); + fx_string_append_c(p->p_pathstr, c); prev = c; } - while (b_string_back(p->p_pathstr) == '/') { - b_string_pop_back(p->p_pathstr); + while (fx_string_back(p->p_pathstr) == '/') { + fx_string_pop_back(p->p_pathstr); } return path; } -b_path *b_path_duplicate(const b_path *path) +fx_path *fx_path_duplicate(const fx_path *path) { - b_path *new_path = b_path_create(); + fx_path *new_path = fx_path_create(); if (!path) { return NULL; } - struct b_path_p *old_p = b_object_get_private(path, B_TYPE_PATH); - struct b_path_p *new_p = b_object_get_private(new_path, B_TYPE_PATH); + struct fx_path_p *old_p = fx_object_get_private(path, FX_TYPE_PATH); + struct fx_path_p *new_p = fx_object_get_private(new_path, FX_TYPE_PATH); - new_p->p_pathstr = b_string_duplicate(old_p->p_pathstr); + new_p->p_pathstr = fx_string_duplicate(old_p->p_pathstr); if (!new_p->p_pathstr) { - b_path_unref(new_path); + fx_path_unref(new_path); return NULL; } return new_path; } -b_path *b_path_join(const b_path *paths[], size_t nr_paths) +fx_path *fx_path_join(const fx_path *paths[], size_t nr_paths) { - b_path *result = b_path_create(); + fx_path *result = fx_path_create(); if (!result) { return NULL; } - struct b_path_p *result_p = b_object_get_private(result, B_TYPE_PATH); + struct fx_path_p *result_p = fx_object_get_private(result, FX_TYPE_PATH); for (size_t i = 0; i < nr_paths; i++) { if (paths[i]) { - struct b_path_p *path_p - = b_object_get_private(paths[i], B_TYPE_PATH); + struct fx_path_p *path_p + = fx_object_get_private(paths[i], FX_TYPE_PATH); append_path(result_p, path_p); } } @@ -291,109 +291,109 @@ b_path *b_path_join(const b_path *paths[], size_t nr_paths) return result; } -b_path *b_path_make_absolute(const b_path *in) +fx_path *fx_path_make_absolute(const fx_path *in) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_make_absolute, in); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_make_absolute, in); } -b_path *b_path_make_relative(const b_path *in) +fx_path *fx_path_make_relative(const fx_path *in) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_make_relative, in); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_make_relative, in); } -b_path *b_path_make_canonical(const b_path *in) +fx_path *fx_path_make_canonical(const fx_path *in) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_make_canonical, in); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_make_canonical, in); } -bool b_path_is_absolute(const b_path *path) +bool fx_path_is_absolute(const fx_path *path) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_is_absolute, path); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_is_absolute, path); } -bool b_path_exists(const b_path *path) +bool fx_path_exists(const fx_path *path) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_exists, path); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_exists, path); } -bool b_path_is_file(const b_path *path) +bool fx_path_is_file(const fx_path *path) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_is_file, path); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_is_file, path); } -bool b_path_is_directory(const b_path *path) +bool fx_path_is_directory(const fx_path *path) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_is_directory, path); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_is_directory, path); } -enum b_status b_path_stat(const b_path *path, struct b_file_info *out) +enum fx_status fx_path_stat(const fx_path *path, struct fx_file_info *out) { - B_CLASS_DISPATCH_STATIC(B_TYPE_PATH, path_stat, path, out); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_PATH, path_stat, path, out); } -enum b_status b_path_unlink(const b_path *path) +enum fx_status fx_path_unlink(const fx_path *path) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_unlink, path); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_unlink, path); } -enum b_status b_path_get_directory(const b_path *path, b_path **out_dir_path) +enum fx_status fx_path_get_directory(const fx_path *path, fx_path **out_dir_path) { - B_CLASS_DISPATCH_STATIC(B_TYPE_PATH, path_get_directory, path, out_dir_path); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_PATH, path_get_directory, path, out_dir_path); } -enum b_status b_path_get_filename(const b_path *path, b_string *out_name) +enum fx_status fx_path_get_filename(const fx_path *path, fx_string *out_name) { - B_CLASS_DISPATCH_STATIC(B_TYPE_PATH, path_get_filename, path, out_name); + FX_CLASS_DISPATCH_STATIC(FX_TYPE_PATH, path_get_filename, path, out_name); } -const char *b_path_ptr(const b_path *path) +const char *fx_path_ptr(const fx_path *path) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_ptr, path); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_ptr, path); } -size_t b_path_length(const b_path *path) +size_t fx_path_length(const fx_path *path) { - B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_length, path); + FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_length, path); } /*** VIRTUAL FUNCTIONS ********************************************************/ -static void path_init(b_object *obj, void *priv) +static void path_init(fx_object *obj, void *priv) { - struct b_path_p *path = priv; + struct fx_path_p *path = priv; - path->p_pathstr = b_string_create(); + path->p_pathstr = fx_string_create(); if (!path->p_pathstr) { /* TODO return error */ } } -void path_fini(b_object *obj, void *priv) +void path_fini(fx_object *obj, void *priv) { - struct b_path_p *path = priv; + struct fx_path_p *path = priv; - b_string_unref(path->p_pathstr); + fx_string_unref(path->p_pathstr); } -void path_to_string(const b_object *obj, b_stream *out) +void path_to_string(const fx_object *obj, fx_stream *out) { - struct b_path_p *path = b_object_get_private(obj, B_TYPE_PATH); + struct fx_path_p *path = fx_object_get_private(obj, FX_TYPE_PATH); - b_stream_write_string(out, b_string_ptr(path->p_pathstr), NULL); + fx_stream_write_string(out, fx_string_ptr(path->p_pathstr), NULL); } /*** CLASS DEFINITION *********************************************************/ -B_TYPE_CLASS_DEFINITION_BEGIN(b_path) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = path_to_string; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) -B_TYPE_CLASS_DEFINITION_END(b_path) +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_path) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = path_to_string; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_DEFINITION_END(fx_path) -B_TYPE_DEFINITION_BEGIN(b_path) - B_TYPE_ID(0x56dc32eb, 0xea96, 0x46ed, 0x85d3, 0x760fa4ad61f4); - B_TYPE_CLASS(b_path_class); - B_TYPE_INSTANCE_PRIVATE(struct b_path_p); - B_TYPE_INSTANCE_INIT(path_init); - B_TYPE_INSTANCE_FINI(path_fini); -B_TYPE_DEFINITION_END(b_path) +FX_TYPE_DEFINITION_BEGIN(fx_path) + FX_TYPE_ID(0x56dc32eb, 0xea96, 0x46ed, 0x85d3, 0x760fa4ad61f4); + FX_TYPE_CLASS(fx_path_class); + FX_TYPE_INSTANCE_PRIVATE(struct fx_path_p); + FX_TYPE_INSTANCE_INIT(path_init); + FX_TYPE_INSTANCE_FINI(path_fini); +FX_TYPE_DEFINITION_END(fx_path) diff --git a/io/sys/darwin/posix.c b/io/sys/darwin/posix.c index db10e8e..5a8805c 100644 --- a/io/sys/darwin/posix.c +++ b/io/sys/darwin/posix.c @@ -1,131 +1,131 @@ -#include -#include -#include +#include +#include +#include #include #include -enum b_status b_status_from_errno(int error, enum b_status default_value) +enum fx_status fx_status_from_errno(int error, enum fx_status default_value) { switch (error) { case 0: - return B_SUCCESS; + return FX_SUCCESS; case ENOENT: - return B_ERR_NO_ENTRY; + return FX_ERR_NO_ENTRY; case EEXIST: - return B_ERR_NAME_EXISTS; + return FX_ERR_NAME_EXISTS; case ENOMEM: - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; case EINVAL: - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; case EIO: - return B_ERR_IO_FAILURE; + return FX_ERR_IO_FAILURE; case EISDIR: - return B_ERR_IS_DIRECTORY; + return FX_ERR_IS_DIRECTORY; case ENOTDIR: - return B_ERR_NOT_DIRECTORY; + return FX_ERR_NOT_DIRECTORY; case EPERM: case EACCES: - return B_ERR_PERMISSION_DENIED; + return FX_ERR_PERMISSION_DENIED; case ENOTSUP: case ENOSYS: - return B_ERR_NOT_SUPPORTED; + return FX_ERR_NOT_SUPPORTED; default: return default_value; } } -b_result b_result_from_errno_with_filepath( - int error, const char *path, enum b_status default_value) +fx_result fx_result_from_errno_with_filepath( + int error, const char *path, enum fx_status default_value) { switch (error) { case 0: - return B_RESULT_SUCCESS; + return FX_RESULT_SUCCESS; case ENOENT: - return B_RESULT_STATUS_WITH_STRING( - B_ERR_NO_ENTRY, "Path @i{%s} does not exist", path); + return FX_RESULT_STATUS_WITH_STRING( + FX_ERR_NO_ENTRY, "Path @i{%s} does not exist", path); case ENOTDIR: - return B_RESULT_STATUS_WITH_STRING( - B_ERR_NOT_DIRECTORY, "Path @i{%s} is not a directory", + return FX_RESULT_STATUS_WITH_STRING( + FX_ERR_NOT_DIRECTORY, "Path @i{%s} is not a directory", path); case EISDIR: - return B_RESULT_STATUS_WITH_STRING( - B_ERR_IS_DIRECTORY, "Path @i{%s} is a directory", path); + return FX_RESULT_STATUS_WITH_STRING( + FX_ERR_IS_DIRECTORY, "Path @i{%s} is a directory", path); case EPERM: case EACCES: - return B_RESULT_STATUS_WITH_STRING( - B_ERR_PERMISSION_DENIED, + return FX_RESULT_STATUS_WITH_STRING( + FX_ERR_PERMISSION_DENIED, "Permission denied while accessing path @i{%s}", path); default: - return B_RESULT_STATUS(b_status_from_errno(error, default_value)); + return FX_RESULT_STATUS(fx_status_from_errno(error, default_value)); } - return B_RESULT_SUCCESS; + return FX_RESULT_SUCCESS; } -b_result b_result_from_errno_with_subfilepath( +fx_result fx_result_from_errno_with_subfilepath( int error, const char *path, const char *dir_path, - enum b_status default_value) + enum fx_status default_value) { if (!dir_path) { - return b_result_propagate(b_result_from_errno_with_filepath( + return fx_result_propagate(fx_result_from_errno_with_filepath( error, path, default_value)); } switch (error) { case 0: - return B_RESULT_SUCCESS; + return FX_RESULT_SUCCESS; case ENOENT: - return B_RESULT_STATUS_WITH_STRING( - B_ERR_NO_ENTRY, + return FX_RESULT_STATUS_WITH_STRING( + FX_ERR_NO_ENTRY, "Path @i{%s} in directory @i{%s} does not exist", path, dir_path); case ENOTDIR: - return B_RESULT_STATUS_WITH_STRING( - B_ERR_NOT_DIRECTORY, + return FX_RESULT_STATUS_WITH_STRING( + FX_ERR_NOT_DIRECTORY, "Path @i{%s} in directory @i{%s} is not a directory", path, dir_path); case EISDIR: - return B_RESULT_STATUS_WITH_STRING( - B_ERR_IS_DIRECTORY, + return FX_RESULT_STATUS_WITH_STRING( + FX_ERR_IS_DIRECTORY, "Path @i{%s} in directory @i{%s} is a directory", path, dir_path); case EPERM: case EACCES: - return B_RESULT_STATUS_WITH_STRING( - B_ERR_PERMISSION_DENIED, + return FX_RESULT_STATUS_WITH_STRING( + FX_ERR_PERMISSION_DENIED, "Permission denied while accessing path @i{%s} in " "directory @i{%s}", path, dir_path); default: - return B_RESULT_STATUS(b_status_from_errno(error, default_value)); + return FX_RESULT_STATUS(fx_status_from_errno(error, default_value)); } - return B_RESULT_SUCCESS; + return FX_RESULT_SUCCESS; } -enum b_status b_file_info_from_stat(const struct stat *st, struct b_file_info *out) +enum fx_status fx_file_info_from_stat(const struct stat *st, struct fx_file_info *out) { out->length = st->st_size; if (S_ISREG(st->st_mode)) { - out->attrib |= B_FILE_ATTRIB_NORMAL; + out->attrib |= FX_FILE_ATTRIB_NORMAL; } if (S_ISDIR(st->st_mode)) { - out->attrib |= B_FILE_ATTRIB_DIRECTORY; + out->attrib |= FX_FILE_ATTRIB_DIRECTORY; } if (S_ISBLK(st->st_mode)) { - out->attrib |= B_FILE_ATTRIB_BLOCK_DEVICE; + out->attrib |= FX_FILE_ATTRIB_BLOCK_DEVICE; } if (S_ISCHR(st->st_mode)) { - out->attrib |= B_FILE_ATTRIB_CHAR_DEVICE; + out->attrib |= FX_FILE_ATTRIB_CHAR_DEVICE; } if (S_ISLNK(st->st_mode)) { - out->attrib |= B_FILE_ATTRIB_SYMLINK; + out->attrib |= FX_FILE_ATTRIB_SYMLINK; } - return B_SUCCESS; + return FX_SUCCESS; } diff --git a/io/sys/darwin/posix.h b/io/sys/darwin/posix.h index 5ce72ba..620e651 100644 --- a/io/sys/darwin/posix.h +++ b/io/sys/darwin/posix.h @@ -1,19 +1,19 @@ #ifndef _IO_DARWIN_POSIX_H_ #define _IO_DARWIN_POSIX_H_ -#include -#include +#include +#include struct stat; -struct b_file_info; +struct fx_file_info; -extern enum b_status b_status_from_errno(int error, enum b_status default_value); -extern b_result b_result_from_errno_with_filepath( - int error, const char *path, enum b_status default_value); -extern b_result b_result_from_errno_with_subfilepath( +extern enum fx_status fx_status_from_errno(int error, enum fx_status default_value); +extern fx_result fx_result_from_errno_with_filepath( + int error, const char *path, enum fx_status default_value); +extern fx_result fx_result_from_errno_with_subfilepath( int error, const char *path, const char *dir_path, - enum b_status default_value); -extern enum b_status b_file_info_from_stat( - const struct stat *in, struct b_file_info *out); + enum fx_status default_value); +extern enum fx_status fx_file_info_from_stat( + const struct stat *in, struct fx_file_info *out); #endif diff --git a/io/sys/windows/directory.c b/io/sys/windows/directory.c index 047bb34..1be69cf 100644 --- a/io/sys/windows/directory.c +++ b/io/sys/windows/directory.c @@ -1,99 +1,99 @@ #define WIN32_LEAN_AND_MEAN #include -#include +#include #include #include -struct b_directory { - struct b_dsref base; +struct fx_directory { + struct fx_dsref base; HANDLE handle; - struct b_path *abs_path; + struct fx_path *abs_path; }; -struct z__b_directory_iterator { - b_queue state_stack; +struct z__fx_directory_iterator { + fx_queue state_stack; }; struct iteration_state { - const b_path *search_path; - b_queue_entry entry; + const fx_path *search_path; + fx_queue_entry entry; HANDLE search; WIN32_FIND_DATAA data; bool child_search_complete; }; -static void directory_release(struct b_dsref *obj); +static void directory_release(struct fx_dsref *obj); -static struct b_dsref_type directory_type = { +static struct fx_dsref_type directory_type = { .t_name = "corelib::directory", - .t_flags = B_DSREF_FUNDAMENTAL, - .t_id = B_DSREF_TYPE_PATH, - .t_instance_size = sizeof(struct b_directory), + .t_flags = FX_DSREF_FUNDAMENTAL, + .t_id = FX_DSREF_TYPE_PATH, + .t_instance_size = sizeof(struct fx_directory), .t_release = directory_release, }; -static enum b_status status_from_win32_error(int error, enum b_status default_value) +static enum fx_status status_from_win32_error(int error, enum fx_status default_value) { switch (error) { case ERROR_FILE_NOT_FOUND: - return B_ERR_NO_ENTRY; + return FX_ERR_NO_ENTRY; default: return default_value; } } -enum b_status b_directory_open( - struct b_directory *root, const struct b_path *path, - struct b_directory **out) +enum fx_status fx_directory_open( + struct fx_directory *root, const struct fx_path *path, + struct fx_directory **out) { - enum b_status status = B_SUCCESS; + enum fx_status status = FX_SUCCESS; - const b_path *parts[] = { + const fx_path *parts[] = { root ? root->abs_path : NULL, path, }; - b_path *new_path = b_path_join(parts, sizeof parts / sizeof *parts); + fx_path *new_path = fx_path_join(parts, sizeof parts / sizeof *parts); if (!new_path) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } HANDLE dir_handle = CreateFileA( - b_path_ptr(new_path), GENERIC_READ, + fx_path_ptr(new_path), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, INVALID_HANDLE_VALUE); if (dir_handle == INVALID_HANDLE_VALUE) { - status = status_from_win32_error(GetLastError(), B_ERR_IO_FAILURE); + status = status_from_win32_error(GetLastError(), FX_ERR_IO_FAILURE); - b_path_release(new_path); + fx_path_release(new_path); return status; } BY_HANDLE_FILE_INFORMATION dir_info; if (!GetFileInformationByHandle(dir_handle, &dir_info)) { - status = status_from_win32_error(GetLastError(), B_ERR_IO_FAILURE); + status = status_from_win32_error(GetLastError(), FX_ERR_IO_FAILURE); CloseHandle(dir_handle); - b_path_release(new_path); + fx_path_release(new_path); return status; } if (!(dir_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { CloseHandle(dir_handle); - b_path_release(new_path); - return B_ERR_NOT_DIRECTORY; + fx_path_release(new_path); + return FX_ERR_NOT_DIRECTORY; } - struct b_directory *dir - = (struct b_directory *)b_dsref_type_instantiate(&directory_type); + struct fx_directory *dir + = (struct fx_directory *)fx_dsref_type_instantiate(&directory_type); if (!path) { CloseHandle(dir_handle); - b_path_release(new_path); - return B_ERR_NO_MEMORY; + fx_path_release(new_path); + return FX_ERR_NO_MEMORY; } dir->abs_path = new_path; @@ -101,23 +101,23 @@ enum b_status b_directory_open( *out = dir; - return B_SUCCESS; + return FX_SUCCESS; } static struct iteration_state *get_iteration_state( - struct z__b_directory_iterator *it) + struct z__fx_directory_iterator *it) { - b_queue_entry *last = b_queue_last(&it->state_stack); + fx_queue_entry *last = fx_queue_last(&it->state_stack); if (!last) { return NULL; } - return b_unbox(struct iteration_state, last, entry); + return fx_unbox(struct iteration_state, last, entry); } static struct iteration_state *push_iteration_state( - struct z__b_directory_iterator *it) + struct z__fx_directory_iterator *it) { struct iteration_state *state = malloc(sizeof *state); if (!state) { @@ -126,11 +126,11 @@ static struct iteration_state *push_iteration_state( memset(state, 0x0, sizeof *state); - b_queue_push_back(&it->state_stack, &state->entry); + fx_queue_push_back(&it->state_stack, &state->entry); return state; } -static void pop_iteration_state(struct z__b_directory_iterator *it) +static void pop_iteration_state(struct z__fx_directory_iterator *it) { struct iteration_state *state = get_iteration_state(it); @@ -138,14 +138,14 @@ static void pop_iteration_state(struct z__b_directory_iterator *it) return; } - b_queue_pop_back(&it->state_stack); + fx_queue_pop_back(&it->state_stack); FindClose(state->search); free(state); } -static void cleanup_iterator(struct b_directory_iterator *it) +static void cleanup_iterator(struct fx_directory_iterator *it) { - while (!b_queue_empty(&it->_z->state_stack)) { + while (!fx_queue_empty(&it->_z->state_stack)) { pop_iteration_state(it->_z); } @@ -153,10 +153,10 @@ static void cleanup_iterator(struct b_directory_iterator *it) it->_z = NULL; } -static void update_iterator_data(struct b_directory_iterator *it) +static void update_iterator_data(struct fx_directory_iterator *it) { if (it->filepath) { - b_path_release(B_PATH(it->filepath)); + fx_path_release(FX_PATH(it->filepath)); it->filepath = NULL; } @@ -164,44 +164,44 @@ static void update_iterator_data(struct b_directory_iterator *it) if (state) { it->filename = state->data.cFileName; - struct b_path *filename = b_path_create_from_cstr(it->filename); + struct fx_path *filename = fx_path_create_from_cstr(it->filename); - const struct b_path *parts[] = { + const struct fx_path *parts[] = { state->search_path, filename, }; - it->filepath = b_path_join(parts, sizeof parts / sizeof parts[0]); + it->filepath = fx_path_join(parts, sizeof parts / sizeof parts[0]); } } -static bool move_into_directory(struct b_directory_iterator *it, const char *dir_name) +static bool move_into_directory(struct fx_directory_iterator *it, const char *dir_name) { struct iteration_state *state = get_iteration_state(it->_z); - struct b_path *dir_name_p = b_path_create_from_cstr(dir_name); - struct b_path *wildcard = b_path_create_from_cstr("*"); + struct fx_path *dir_name_p = fx_path_create_from_cstr(dir_name); + struct fx_path *wildcard = fx_path_create_from_cstr("*"); - const struct b_path *parts[] = { + const struct fx_path *parts[] = { state ? state->search_path : NULL, dir_name_p, }; - struct b_path *dir_path = b_path_join(parts, sizeof parts / sizeof *parts); + struct fx_path *dir_path = fx_path_join(parts, sizeof parts / sizeof *parts); parts[0] = dir_path; parts[1] = wildcard; - struct b_path *search_path - = b_path_join(parts, sizeof parts / sizeof *parts); + struct fx_path *search_path + = fx_path_join(parts, sizeof parts / sizeof *parts); state = push_iteration_state(it->_z); state->search_path = dir_path; - state->search = FindFirstFileA(b_path_ptr(search_path), &state->data); + state->search = FindFirstFileA(fx_path_ptr(search_path), &state->data); - b_path_release(search_path); - b_path_release(wildcard); - b_path_release(dir_name_p); + fx_path_release(search_path); + fx_path_release(wildcard); + fx_path_release(dir_name_p); if (state->search == INVALID_HANDLE_VALUE) { pop_iteration_state(it->_z); @@ -222,15 +222,15 @@ static bool move_into_directory(struct b_directory_iterator *it, const char *dir } static bool move_to_first_item( - struct b_directory_iterator *it, const struct b_path *root_dir) + struct fx_directory_iterator *it, const struct fx_path *root_dir) { - bool has_results = move_into_directory(it, b_path_ptr(root_dir)); + bool has_results = move_into_directory(it, fx_path_ptr(root_dir)); if (!has_results) { return false; } - if (it->flags & B_DIRECTORY_ITERATE_PARENT_FIRST) { + if (it->flags & FX_DIRECTORY_ITERATE_PARENT_FIRST) { return true; } @@ -252,14 +252,14 @@ static bool move_to_first_item( return true; } -static bool move_to_next_item(struct b_directory_iterator *it) +static bool move_to_next_item(struct fx_directory_iterator *it) { while (true) { struct iteration_state *state = get_iteration_state(it->_z); if (!state->child_search_complete && (state->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) - && (it->flags & B_DIRECTORY_ITERATE_PARENT_FIRST)) { + && (it->flags & FX_DIRECTORY_ITERATE_PARENT_FIRST)) { if (move_into_directory(it, state->data.cFileName)) { return true; } @@ -271,7 +271,7 @@ static bool move_to_next_item(struct b_directory_iterator *it) pop_iteration_state(it->_z); state = get_iteration_state(it->_z); - if (it->flags & B_DIRECTORY_ITERATE_PARENT_FIRST + if (it->flags & FX_DIRECTORY_ITERATE_PARENT_FIRST && state != NULL) { state->child_search_complete = true; continue; @@ -286,7 +286,7 @@ static bool move_to_next_item(struct b_directory_iterator *it) return true; } - if (it->flags & B_DIRECTORY_ITERATE_PARENT_FIRST) { + if (it->flags & FX_DIRECTORY_ITERATE_PARENT_FIRST) { return true; } @@ -311,17 +311,17 @@ static bool move_to_next_item(struct b_directory_iterator *it) return true; } -int b_directory_iterator_begin( - struct b_directory *directory, struct b_directory_iterator *it, - enum b_directory_iterator_flags flags) +int fx_directory_iterator_begin( + struct fx_directory *directory, struct fx_directory_iterator *it, + enum fx_directory_iterator_flags flags) { - if (b_directory_iterator_is_valid(it)) { + if (fx_directory_iterator_is_valid(it)) { cleanup_iterator(it); } it->flags = flags; - struct z__b_directory_iterator *it_data = malloc(sizeof *it_data); + struct z__fx_directory_iterator *it_data = malloc(sizeof *it_data); memset(it_data, 0x0, sizeof *it_data); it->_z = it_data; @@ -332,7 +332,7 @@ int b_directory_iterator_begin( return 0; } -bool b_directory_iterator_next(struct b_directory_iterator *it) +bool fx_directory_iterator_next(struct fx_directory_iterator *it) { if (!it->_z) { return false; @@ -343,12 +343,12 @@ bool b_directory_iterator_next(struct b_directory_iterator *it) return result; } -enum b_status b_directory_iterator_erase(struct b_directory_iterator *it) +enum fx_status fx_directory_iterator_erase(struct fx_directory_iterator *it) { - return B_SUCCESS; + return FX_SUCCESS; } -bool b_directory_iterator_is_valid(const struct b_directory_iterator *it) +bool fx_directory_iterator_is_valid(const struct fx_directory_iterator *it) { if (!it->_z) { return false; @@ -361,6 +361,6 @@ bool b_directory_iterator_is_valid(const struct b_directory_iterator *it) return true; } -static void directory_release(struct b_dsref *obj) +static void directory_release(struct fx_dsref *obj) { } diff --git a/io/sys/windows/path.c b/io/sys/windows/path.c index 33473c9..df9ce08 100644 --- a/io/sys/windows/path.c +++ b/io/sys/windows/path.c @@ -1,48 +1,48 @@ #define WIN32_LEAN_AND_MEAN -#include -#include -#include +#include +#include +#include #include #include #include #include -struct b_path { - struct b_dsref base; - struct b_string *pathstr; +struct fx_path { + struct fx_dsref base; + struct fx_string *pathstr; }; -static void path_release(struct b_dsref *obj); -static void path_to_string(struct b_dsref *obj, struct b_stringstream *out); +static void path_release(struct fx_dsref *obj); +static void path_to_string(struct fx_dsref *obj, struct fx_stringstream *out); -static struct b_dsref_type path_type = { +static struct fx_dsref_type path_type = { .t_name = "corelib::path", - .t_flags = B_DSREF_FUNDAMENTAL, - .t_id = B_DSREF_TYPE_PATH, - .t_instance_size = sizeof(struct b_path), + .t_flags = FX_DSREF_FUNDAMENTAL, + .t_id = FX_DSREF_TYPE_PATH, + .t_instance_size = sizeof(struct fx_path), .t_release = path_release, .t_to_string = path_to_string, }; -struct b_path *b_path_create() +struct fx_path *fx_path_create() { - struct b_path *path - = (struct b_path *)b_dsref_type_instantiate(&path_type); + struct fx_path *path + = (struct fx_path *)fx_dsref_type_instantiate(&path_type); if (!path) { return NULL; } - path->pathstr = b_string_create(); + path->pathstr = fx_string_create(); if (!path->pathstr) { - b_path_release(path); + fx_path_release(path); return NULL; } return path; } -struct b_path *b_path_create_root() +struct fx_path *fx_path_create_root() { const char *system_drive = getenv("SystemDrive"); if (!system_drive) { @@ -52,20 +52,20 @@ struct b_path *b_path_create_root() size_t system_drive_len = strlen(system_drive); - struct b_path *path = b_path_create(); + struct fx_path *path = fx_path_create(); if (!path) { return NULL; } - b_string_append_cstr(path->pathstr, system_drive); + fx_string_append_cstr(path->pathstr, system_drive); if (system_drive[system_drive_len - 1] != '\\') { - b_string_append_cstr(path->pathstr, "\\"); + fx_string_append_cstr(path->pathstr, "\\"); } return path; } -struct b_path *b_path_create_cwd() +struct fx_path *fx_path_create_cwd() { DWORD cwd_len = GetCurrentDirectory(0, NULL); if (cwd_len == 0) { @@ -83,7 +83,7 @@ struct b_path *b_path_create_cwd() return NULL; } - struct b_path *path = b_path_create(); + struct fx_path *path = fx_path_create(); if (!path) { return NULL; } @@ -91,16 +91,16 @@ struct b_path *b_path_create_cwd() for (DWORD i = 0; i < cwd_len; i++) { TCHAR c = cwd_buf[i]; char s[] = {(c >= 128 ? '?' : (char)c), 0}; - b_string_append_cstr(path->pathstr, s); + fx_string_append_cstr(path->pathstr, s); } free(cwd_buf); return path; } -struct b_path *b_path_create_from_cstr(const char *cstr) +struct fx_path *fx_path_create_from_cstr(const char *cstr) { - struct b_path *path = b_path_create(); + struct fx_path *path = fx_path_create(); if (!path) { return NULL; } @@ -119,38 +119,38 @@ struct b_path *b_path_create_from_cstr(const char *cstr) char s[] = {c, 0}; - b_string_append_cstr(path->pathstr, s); + fx_string_append_cstr(path->pathstr, s); prev = c; } - while (b_string_back(path->pathstr) == '/') { - b_string_pop_back(path->pathstr); + while (fx_string_back(path->pathstr) == '/') { + fx_string_pop_back(path->pathstr); } return path; } -static void append_path(struct b_path *dest, const struct b_path *src) +static void append_path(struct fx_path *dest, const struct fx_path *src) { - if (b_path_is_absolute(src)) { - b_string_clear(dest->pathstr); - b_string_append_s(dest->pathstr, src->pathstr); + if (fx_path_is_absolute(src)) { + fx_string_clear(dest->pathstr); + fx_string_append_s(dest->pathstr, src->pathstr); return; } - if (b_string_back(dest->pathstr) != '/' - && b_string_front(src->pathstr) != '/') { + if (fx_string_back(dest->pathstr) != '/' + && fx_string_front(src->pathstr) != '/') { char s[] = {'/', 0}; - b_string_append_cstr(dest->pathstr, s); + fx_string_append_cstr(dest->pathstr, s); } - b_string_append_s(dest->pathstr, src->pathstr); + fx_string_append_s(dest->pathstr, src->pathstr); } -struct b_path *b_path_join( - const b_path *paths[], size_t nr_paths) +struct fx_path *fx_path_join( + const fx_path *paths[], size_t nr_paths) { - struct b_path *result = b_path_create(); + struct fx_path *result = fx_path_create(); if (!result) { return NULL; } @@ -164,24 +164,24 @@ struct b_path *b_path_join( return result; } -struct b_path *b_path_make_absolute(const struct b_path *in) +struct fx_path *fx_path_make_absolute(const struct fx_path *in) { return NULL; } -struct b_path *b_path_make_relative(const struct b_path *in) +struct fx_path *fx_path_make_relative(const struct fx_path *in) { return NULL; } -struct b_path *b_path_make_canonical(const struct b_path *in) +struct fx_path *fx_path_make_canonical(const struct fx_path *in) { return NULL; } -bool b_path_is_absolute(const struct b_path *path) +bool fx_path_is_absolute(const struct fx_path *path) { - const char *s = b_string_ptr(path->pathstr); + const char *s = fx_string_ptr(path->pathstr); size_t i; for (i = 0; s[i]; i++) { @@ -203,15 +203,15 @@ bool b_path_is_absolute(const struct b_path *path) return true; } -bool b_path_exists(const struct b_path *path) +bool fx_path_exists(const struct fx_path *path) { - DWORD attrib = GetFileAttributesA(b_path_ptr(path)); + DWORD attrib = GetFileAttributesA(fx_path_ptr(path)); return attrib != INVALID_FILE_ATTRIBUTES; } -bool b_path_is_file(const struct b_path *path) +bool fx_path_is_file(const struct fx_path *path) { - DWORD attrib = GetFileAttributesA(b_path_ptr(path)); + DWORD attrib = GetFileAttributesA(fx_path_ptr(path)); if (attrib == INVALID_FILE_ATTRIBUTES) { return false; } @@ -219,9 +219,9 @@ bool b_path_is_file(const struct b_path *path) return (attrib & FILE_ATTRIBUTE_NORMAL) != 0; } -bool b_path_is_directory(const struct b_path *path) +bool fx_path_is_directory(const struct fx_path *path) { - DWORD attrib = GetFileAttributesA(b_path_ptr(path)); + DWORD attrib = GetFileAttributesA(fx_path_ptr(path)); if (attrib == INVALID_FILE_ATTRIBUTES) { return false; } @@ -229,20 +229,20 @@ bool b_path_is_directory(const struct b_path *path) return (attrib & FILE_ATTRIBUTE_DIRECTORY) != 0; } -const char *b_path_ptr(const struct b_path *path) +const char *fx_path_ptr(const struct fx_path *path) { - return b_string_ptr(path->pathstr); + return fx_string_ptr(path->pathstr); } -void path_release(struct b_dsref* obj) +void path_release(struct fx_dsref* obj) { - struct b_path *path = B_PATH(obj); - b_string_release(path->pathstr); + struct fx_path *path = FX_PATH(obj); + fx_string_release(path->pathstr); } -void path_to_string(struct b_dsref* obj, struct b_stringstream* out) +void path_to_string(struct fx_dsref* obj, struct fx_stringstream* out) { - struct b_path *path = (struct b_path *)obj; + struct fx_path *path = (struct fx_path *)obj; - b_stringstream_add(out, b_string_ptr(path->pathstr)); + fx_stringstream_add(out, fx_string_ptr(path->pathstr)); } \ No newline at end of file diff --git a/serial/CMakeLists.txt b/serial/CMakeLists.txt index 6fbd8a5..fc16ee5 100644 --- a/serial/CMakeLists.txt +++ b/serial/CMakeLists.txt @@ -1,3 +1,3 @@ include(../cmake/Templates.cmake) -add_bluelib_module(NAME serial DEPENDENCIES core ds) +add_fx_module(NAME serial DEPENDENCIES core ds) diff --git a/serial/bitcode.c b/serial/bitcode.c index 3ddc872..259a47b 100644 --- a/serial/bitcode.c +++ b/serial/bitcode.c @@ -1,46 +1,46 @@ -#include -#include +#include +#include /*** VIRTUAL FUNCTIONS ********************************************************/ -static enum b_status bitcode_serialise( - b_serial_ctx *serial, b_object *src, b_stream *dest, - enum b_serial_flags flags) +static enum fx_status bitcode_serialise( + fx_serial_ctx *serial, fx_object *src, fx_stream *dest, + enum fx_serial_flags flags) { - return B_ERR_NOT_SUPPORTED; + return FX_ERR_NOT_SUPPORTED; } -static enum b_status bitcode_deserialise( - b_serial_ctx *serial, b_stream *src, b_object **dest, - enum b_serial_flags flags) +static enum fx_status bitcode_deserialise( + fx_serial_ctx *serial, fx_stream *src, fx_object **dest, + enum fx_serial_flags flags) { - return B_ERR_NOT_SUPPORTED; + return FX_ERR_NOT_SUPPORTED; } -static void bitcode_serial_ctx_init(b_object *obj, void *priv) +static void bitcode_serial_ctx_init(fx_object *obj, void *priv) { } -static void bitcode_serial_ctx_fini(b_object *obj, void *priv) +static void bitcode_serial_ctx_fini(fx_object *obj, void *priv) { } /*** CLASS DEFINITION *********************************************************/ -B_TYPE_CLASS_DEFINITION_BEGIN(b_bitcode_serial_ctx) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_bitcode_serial_ctx) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) - B_TYPE_CLASS_INTERFACE_BEGIN(b_serial_ctx, B_TYPE_SERIAL_CTX) - B_INTERFACE_ENTRY(s_serialise) = bitcode_serialise; - B_INTERFACE_ENTRY(s_deserialise) = bitcode_deserialise; - B_TYPE_CLASS_INTERFACE_END(b_serial_ctx, B_TYPE_SERIAL_CTX) -B_TYPE_CLASS_DEFINITION_END(b_bitcode_serial_ctx) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_serial_ctx, FX_TYPE_SERIAL_CTX) + FX_INTERFACE_ENTRY(s_serialise) = bitcode_serialise; + FX_INTERFACE_ENTRY(s_deserialise) = bitcode_deserialise; + FX_TYPE_CLASS_INTERFACE_END(fx_serial_ctx, FX_TYPE_SERIAL_CTX) +FX_TYPE_CLASS_DEFINITION_END(fx_bitcode_serial_ctx) -B_TYPE_DEFINITION_BEGIN(b_bitcode_serial_ctx) - B_TYPE_ID(0xcdc8c462, 0xf2b3, 0x4193, 0x8cae, 0xc1e5ad9afcb8); - B_TYPE_CLASS(b_bitcode_serial_ctx_class); - B_TYPE_INSTANCE_INIT(bitcode_serial_ctx_init); - B_TYPE_INSTANCE_FINI(bitcode_serial_ctx_fini); -B_TYPE_DEFINITION_END(b_bitcode_serial_ctx) +FX_TYPE_DEFINITION_BEGIN(fx_bitcode_serial_ctx) + FX_TYPE_ID(0xcdc8c462, 0xf2b3, 0x4193, 0x8cae, 0xc1e5ad9afcb8); + FX_TYPE_CLASS(fx_bitcode_serial_ctx_class); + FX_TYPE_INSTANCE_INIT(bitcode_serial_ctx_init); + FX_TYPE_INSTANCE_FINI(bitcode_serial_ctx_fini); +FX_TYPE_DEFINITION_END(fx_bitcode_serial_ctx) diff --git a/serial/ctx.c b/serial/ctx.c index 64918b4..a284ed4 100644 --- a/serial/ctx.c +++ b/serial/ctx.c @@ -1,4 +1,4 @@ -#include +#include #include #include @@ -8,50 +8,50 @@ /*** PUBLIC ALIAS FUNCTIONS ***************************************************/ /*** VIRTUAL FUNCTIONS ********************************************************/ -static void serial_ctx_init(b_object *obj, void *priv) +static void serial_ctx_init(fx_object *obj, void *priv) { - b_serial_ctx_data *data = b_object_get_protected(obj, B_TYPE_SERIAL_CTX); + fx_serial_ctx_data *data = fx_object_get_protected(obj, FX_TYPE_SERIAL_CTX); - data->ctx_streambuf = b_stream_buffer_create_dynamic(2048); + data->ctx_streambuf = fx_stream_buffer_create_dynamic(2048); } -static void serial_ctx_fini(b_object *obj, void *priv) +static void serial_ctx_fini(fx_object *obj, void *priv) { - b_serial_ctx_data *data = b_object_get_protected(obj, B_TYPE_SERIAL_CTX); + fx_serial_ctx_data *data = fx_object_get_protected(obj, FX_TYPE_SERIAL_CTX); - b_stream_buffer_unref(data->ctx_streambuf); + fx_stream_buffer_unref(data->ctx_streambuf); } /*** CLASS DEFINITION *********************************************************/ -B_TYPE_CLASS_DEFINITION_BEGIN(b_serial_ctx) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) -B_TYPE_CLASS_DEFINITION_END(b_serial_ctx) +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_serial_ctx) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_DEFINITION_END(fx_serial_ctx) -B_TYPE_DEFINITION_BEGIN(b_serial_ctx) - B_TYPE_ID(0xc7c1039a, 0xf397, 0x4fda, 0xb473, 0x4d86fec85384); - B_TYPE_CLASS(b_serial_ctx_class); - B_TYPE_INSTANCE_PROTECTED(b_serial_ctx_data); - B_TYPE_INSTANCE_INIT(serial_ctx_init); - B_TYPE_INSTANCE_FINI(serial_ctx_fini); -B_TYPE_DEFINITION_END(b_serial_ctx) +FX_TYPE_DEFINITION_BEGIN(fx_serial_ctx) + FX_TYPE_ID(0xc7c1039a, 0xf397, 0x4fda, 0xb473, 0x4d86fec85384); + FX_TYPE_CLASS(fx_serial_ctx_class); + FX_TYPE_INSTANCE_PROTECTED(fx_serial_ctx_data); + FX_TYPE_INSTANCE_INIT(serial_ctx_init); + FX_TYPE_INSTANCE_FINI(serial_ctx_fini); +FX_TYPE_DEFINITION_END(fx_serial_ctx) /*** ITERATOR FUNCTIONS *******************************************************/ -enum b_status b_serial_ctx_serialise( - b_serial_ctx *ctx, b_object *src, b_stream *dest, enum b_serial_flags flags) +enum fx_status fx_serial_ctx_serialise( + fx_serial_ctx *ctx, fx_object *src, fx_stream *dest, enum fx_serial_flags flags) { - B_CLASS_DISPATCH_VIRTUAL( - b_serial_ctx, B_TYPE_SERIAL_CTX, B_ERR_NOT_SUPPORTED, + FX_CLASS_DISPATCH_VIRTUAL( + fx_serial_ctx, FX_TYPE_SERIAL_CTX, FX_ERR_NOT_SUPPORTED, s_serialise, ctx, src, dest, flags); } -enum b_status b_serial_ctx_deserialise( - b_serial_ctx *ctx, b_stream *src, b_object **dest, enum b_serial_flags flags) +enum fx_status fx_serial_ctx_deserialise( + fx_serial_ctx *ctx, fx_stream *src, fx_object **dest, enum fx_serial_flags flags) { - B_CLASS_DISPATCH_VIRTUAL( - b_serial_ctx, B_TYPE_SERIAL_CTX, B_ERR_NOT_SUPPORTED, + FX_CLASS_DISPATCH_VIRTUAL( + fx_serial_ctx, FX_TYPE_SERIAL_CTX, FX_ERR_NOT_SUPPORTED, s_deserialise, ctx, src, dest, flags); } diff --git a/serial/include/blue/serial.h b/serial/include/blue/serial.h deleted file mode 100644 index bddca05..0000000 --- a/serial/include/blue/serial.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef BLUE_SERIAL_H_ -#define BLUE_SERIAL_H_ - -#include -#include -#include - -#endif diff --git a/serial/include/blue/serial/bitcode.h b/serial/include/blue/serial/bitcode.h deleted file mode 100644 index 81d4349..0000000 --- a/serial/include/blue/serial/bitcode.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef BLUE_SERIAL_BITCODE_H_ -#define BLUE_SERIAL_BITCODE_H_ - -#include - -B_DECLS_BEGIN; - -#define B_TYPE_BITCODE_SERIAL_CTX (b_bitcode_serial_ctx_get_type()) - -B_DECLARE_TYPE(b_bitcode_serial_ctx); - -B_TYPE_CLASS_DECLARATION_BEGIN(b_bitcode_serial_ctx) -B_TYPE_CLASS_DECLARATION_END(b_bitcode_serial_ctx) - -BLUE_API b_type b_bitcode_serial_ctx_get_type(void); - -B_TYPE_DEFAULT_CONSTRUCTOR(b_bitcode_serial_ctx, B_TYPE_BITCODE_SERIAL_CTX); - -B_DECLS_END; - -#endif diff --git a/serial/include/blue/serial/ctx.h b/serial/include/blue/serial/ctx.h deleted file mode 100644 index 604dbb9..0000000 --- a/serial/include/blue/serial/ctx.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef BLUE_SERIAL_CTX_H_ -#define BLUE_SERIAL_CTX_H_ - -#include -#include -#include -#include -#include - -B_DECLS_BEGIN; - -#define B_TYPE_SERIAL_CTX (b_serial_ctx_get_type()) - -typedef enum b_serial_flags { - B_SERIAL_F_NONE = 0, - B_SERIAL_F_PRETTY = 0x01u, -} b_serial_flags; - -B_DECLARE_TYPE(b_serial_ctx); - -B_TYPE_CLASS_DECLARATION_BEGIN(b_serial_ctx) - b_status (*s_serialise)( - b_serial_ctx *, b_object *, b_stream *, b_serial_flags); - b_status (*s_deserialise)( - b_serial_ctx *, b_stream *, b_object **, b_serial_flags); -B_TYPE_CLASS_DECLARATION_END(b_serial_ctx) - -typedef struct b_serial_ctx_data { - b_stream_buffer *ctx_streambuf; -} b_serial_ctx_data; - -BLUE_API b_type b_serial_ctx_get_type(void); - -BLUE_API b_status b_serial_ctx_serialise( - b_serial_ctx *ctx, b_object *src, b_stream *dest, b_serial_flags flags); - -BLUE_API b_status b_serial_ctx_deserialise( - b_serial_ctx *ctx, b_stream *src, b_object **dest, b_serial_flags flags); - -B_DECLS_END; - -#endif diff --git a/serial/include/blue/serial/toml.h b/serial/include/blue/serial/toml.h deleted file mode 100644 index e66c676..0000000 --- a/serial/include/blue/serial/toml.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef BLUE_SERIAL_TOML_H_ -#define BLUE_SERIAL_TOML_H_ - -#include - -B_DECLS_BEGIN; - -#define B_TYPE_TOML_SERIAL_CTX (b_toml_serial_ctx_get_type()) - -B_DECLARE_TYPE(b_toml_serial_ctx); - -B_TYPE_CLASS_DECLARATION_BEGIN(b_toml_serial_ctx) -B_TYPE_CLASS_DECLARATION_END(b_toml_serial_ctx) - -BLUE_API b_type b_toml_serial_ctx_get_type(void); - -B_TYPE_DEFAULT_CONSTRUCTOR(b_toml_serial_ctx, B_TYPE_TOML_SERIAL_CTX); - -B_DECLS_END; - -#endif diff --git a/serial/include/fx/serial.h b/serial/include/fx/serial.h new file mode 100644 index 0000000..a3a5222 --- /dev/null +++ b/serial/include/fx/serial.h @@ -0,0 +1,8 @@ +#ifndef FX_SERIAL_H_ +#define FX_SERIAL_H_ + +#include +#include +#include + +#endif diff --git a/serial/include/fx/serial/bitcode.h b/serial/include/fx/serial/bitcode.h new file mode 100644 index 0000000..63eefa8 --- /dev/null +++ b/serial/include/fx/serial/bitcode.h @@ -0,0 +1,21 @@ +#ifndef FX_SERIAL_BITCODE_H_ +#define FX_SERIAL_BITCODE_H_ + +#include + +FX_DECLS_BEGIN; + +#define FX_TYPE_BITCODE_SERIAL_CTX (fx_bitcode_serial_ctx_get_type()) + +FX_DECLARE_TYPE(fx_bitcode_serial_ctx); + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_bitcode_serial_ctx) +FX_TYPE_CLASS_DECLARATION_END(fx_bitcode_serial_ctx) + +FX_API fx_type fx_bitcode_serial_ctx_get_type(void); + +FX_TYPE_DEFAULT_CONSTRUCTOR(fx_bitcode_serial_ctx, FX_TYPE_BITCODE_SERIAL_CTX); + +FX_DECLS_END; + +#endif diff --git a/serial/include/fx/serial/ctx.h b/serial/include/fx/serial/ctx.h new file mode 100644 index 0000000..dd08a25 --- /dev/null +++ b/serial/include/fx/serial/ctx.h @@ -0,0 +1,42 @@ +#ifndef FX_SERIAL_CTX_H_ +#define FX_SERIAL_CTX_H_ + +#include +#include +#include +#include +#include + +FX_DECLS_BEGIN; + +#define FX_TYPE_SERIAL_CTX (fx_serial_ctx_get_type()) + +typedef enum fx_serial_flags { + FX_SERIAL_F_NONE = 0, + FX_SERIAL_F_PRETTY = 0x01u, +} fx_serial_flags; + +FX_DECLARE_TYPE(fx_serial_ctx); + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_serial_ctx) + fx_status (*s_serialise)( + fx_serial_ctx *, fx_object *, fx_stream *, fx_serial_flags); + fx_status (*s_deserialise)( + fx_serial_ctx *, fx_stream *, fx_object **, fx_serial_flags); +FX_TYPE_CLASS_DECLARATION_END(fx_serial_ctx) + +typedef struct fx_serial_ctx_data { + fx_stream_buffer *ctx_streambuf; +} fx_serial_ctx_data; + +FX_API fx_type fx_serial_ctx_get_type(void); + +FX_API fx_status fx_serial_ctx_serialise( + fx_serial_ctx *ctx, fx_object *src, fx_stream *dest, fx_serial_flags flags); + +FX_API fx_status fx_serial_ctx_deserialise( + fx_serial_ctx *ctx, fx_stream *src, fx_object **dest, fx_serial_flags flags); + +FX_DECLS_END; + +#endif diff --git a/serial/include/fx/serial/toml.h b/serial/include/fx/serial/toml.h new file mode 100644 index 0000000..5c4ff4f --- /dev/null +++ b/serial/include/fx/serial/toml.h @@ -0,0 +1,21 @@ +#ifndef FX_SERIAL_TOML_H_ +#define FX_SERIAL_TOML_H_ + +#include + +FX_DECLS_BEGIN; + +#define FX_TYPE_TOML_SERIAL_CTX (fx_toml_serial_ctx_get_type()) + +FX_DECLARE_TYPE(fx_toml_serial_ctx); + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_toml_serial_ctx) +FX_TYPE_CLASS_DECLARATION_END(fx_toml_serial_ctx) + +FX_API fx_type fx_toml_serial_ctx_get_type(void); + +FX_TYPE_DEFAULT_CONSTRUCTOR(fx_toml_serial_ctx, FX_TYPE_TOML_SERIAL_CTX); + +FX_DECLS_END; + +#endif diff --git a/serial/toml.c b/serial/toml.c index 76e336a..56bb727 100644 --- a/serial/toml.c +++ b/serial/toml.c @@ -1,13 +1,13 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include #include #include @@ -86,8 +86,8 @@ struct timestamp { struct token { enum token_type tok_type; - struct b_queue_entry tok_entry; - b_string *tok_str; + struct fx_queue_entry tok_entry; + fx_string *tok_str; union { struct { @@ -102,37 +102,37 @@ struct token { bool b; // struct timestamp time; - b_datetime *time; + fx_datetime *time; } tok_value; }; struct ctx { enum ctx_flags ctx_flags; - b_stream *ctx_src; - b_string *ctx_wordbuf; - b_string *ctx_linebuf; - b_iterator *ctx_linebuf_ptr; - enum b_status ctx_status; - b_hashmap *ctx_objects_flags; + fx_stream *ctx_src; + fx_string *ctx_wordbuf; + fx_string *ctx_linebuf; + fx_iterator *ctx_linebuf_ptr; + enum fx_status ctx_status; + fx_hashmap *ctx_objects_flags; - b_queue ctx_tokens; + fx_queue ctx_tokens; }; static void ctx_set_object_flags( - struct ctx *ctx, b_object *obj, enum object_flags flags) + struct ctx *ctx, fx_object *obj, enum object_flags flags) { if (!obj) { return; } - b_hashmap_key key = { + fx_hashmap_key key = { .key_data = obj, - .key_size = sizeof(b_object *), - .key_flags = B_HASHMAP_KEY_F_INTVALUE, + .key_size = sizeof(fx_object *), + .key_flags = FX_HASHMAP_KEY_F_INTVALUE, }; - const b_hashmap_value *old_value - = b_hashmap_get(ctx->ctx_objects_flags, &key); + const fx_hashmap_value *old_value + = fx_hashmap_get(ctx->ctx_objects_flags, &key); enum object_flags new_flags = 0; if (old_value) { @@ -141,29 +141,29 @@ static void ctx_set_object_flags( new_flags |= flags; - b_hashmap_value value = { + fx_hashmap_value value = { .value_data = (void *)new_flags, .value_size = sizeof new_flags, }; - b_hashmap_put(ctx->ctx_objects_flags, &key, &value); + fx_hashmap_put(ctx->ctx_objects_flags, &key, &value); } static void ctx_clear_object_flags( - struct ctx *ctx, b_object *obj, enum object_flags mask) + struct ctx *ctx, fx_object *obj, enum object_flags mask) { if (!obj) { return; } - b_hashmap_key key = { + fx_hashmap_key key = { .key_data = obj, - .key_size = sizeof(b_object *), - .key_flags = B_HASHMAP_KEY_F_INTVALUE, + .key_size = sizeof(fx_object *), + .key_flags = FX_HASHMAP_KEY_F_INTVALUE, }; - const b_hashmap_value *old_value - = b_hashmap_get(ctx->ctx_objects_flags, &key); + const fx_hashmap_value *old_value + = fx_hashmap_get(ctx->ctx_objects_flags, &key); enum object_flags new_flags = 0; if (old_value) { @@ -172,27 +172,27 @@ static void ctx_clear_object_flags( new_flags &= ~mask; - b_hashmap_value value = { + fx_hashmap_value value = { .value_data = (void *)new_flags, .value_size = sizeof new_flags, }; - b_hashmap_put(ctx->ctx_objects_flags, &key, &value); + fx_hashmap_put(ctx->ctx_objects_flags, &key, &value); } -static enum object_flags ctx_get_object_flags(struct ctx *ctx, b_object *obj) +static enum object_flags ctx_get_object_flags(struct ctx *ctx, fx_object *obj) { if (!obj) { return 0; } - b_hashmap_key key = { + fx_hashmap_key key = { .key_data = obj, - .key_size = sizeof(b_object *), - .key_flags = B_HASHMAP_KEY_F_INTVALUE, + .key_size = sizeof(fx_object *), + .key_flags = FX_HASHMAP_KEY_F_INTVALUE, }; - const b_hashmap_value *value = b_hashmap_get(ctx->ctx_objects_flags, &key); + const fx_hashmap_value *value = fx_hashmap_get(ctx->ctx_objects_flags, &key); if (value) { return (enum object_flags)(uintptr_t)value->value_data; } @@ -200,51 +200,51 @@ static enum object_flags ctx_get_object_flags(struct ctx *ctx, b_object *obj) return 0; } -static enum b_status data_available(struct ctx *ctx) +static enum fx_status data_available(struct ctx *ctx) { - size_t len = b_string_get_size(ctx->ctx_linebuf, B_STRLEN_NORMAL); + size_t len = fx_string_get_size(ctx->ctx_linebuf, FX_STRLEN_NORMAL); if (len == 0) { - return B_ERR_NO_DATA; + return FX_ERR_NO_DATA; } - return b_iterator_get_status(ctx->ctx_linebuf_ptr); + return fx_iterator_get_status(ctx->ctx_linebuf_ptr); } -static enum b_status refill_linebuf(struct ctx *ctx) +static enum fx_status refill_linebuf(struct ctx *ctx) { if (ctx->ctx_linebuf_ptr) { - b_iterator_unref(ctx->ctx_linebuf_ptr); + fx_iterator_unref(ctx->ctx_linebuf_ptr); ctx->ctx_linebuf_ptr = NULL; } - b_string_clear(ctx->ctx_linebuf); + fx_string_clear(ctx->ctx_linebuf); - b_stringstream *buf = b_stringstream_create(); + fx_stringstream *buf = fx_stringstream_create(); - enum b_status status = b_stream_read_line_s(ctx->ctx_src, buf); - if (!B_OK(status)) { + enum fx_status status = fx_stream_read_line_s(ctx->ctx_src, buf); + if (!FX_OK(status)) { return status; } - status = b_string_replace_all_with_stringstream(ctx->ctx_linebuf, buf); - if (!B_OK(status)) { + status = fx_string_replace_all_with_stringstream(ctx->ctx_linebuf, buf); + if (!FX_OK(status)) { return status; } - b_stringstream_unref(buf); + fx_stringstream_unref(buf); - ctx->ctx_linebuf_ptr = b_iterator_begin(ctx->ctx_linebuf); + ctx->ctx_linebuf_ptr = fx_iterator_begin(ctx->ctx_linebuf); - return B_SUCCESS; + return FX_SUCCESS; } -static b_string *get_wordbuf(struct ctx *ctx) +static fx_string *get_wordbuf(struct ctx *ctx) { - b_string_clear(ctx->ctx_wordbuf); + fx_string_clear(ctx->ctx_wordbuf); return ctx->ctx_wordbuf; } -static bool is_valid_char(b_wchar c) +static bool is_valid_char(fx_wchar c) { if (c <= 0) { return false; @@ -263,62 +263,62 @@ static bool is_valid_char(b_wchar c) } } -static b_wchar advance_char(struct ctx *ctx) +static fx_wchar advance_char(struct ctx *ctx) { - enum b_status status = data_available(ctx); - if (status == B_ERR_NO_DATA) { + enum fx_status status = data_available(ctx); + if (status == FX_ERR_NO_DATA) { status = refill_linebuf(ctx); } - if (!B_OK(status)) { + if (!FX_OK(status)) { ctx->ctx_status = status; return -1; } status = data_available(ctx); - if (!B_OK(status)) { + if (!FX_OK(status)) { ctx->ctx_status = status; return -1; } - const char *s = b_string_ptr(ctx->ctx_linebuf); + const char *s = fx_string_ptr(ctx->ctx_linebuf); - b_wchar c = b_iterator_get_value(ctx->ctx_linebuf_ptr).v_int; + fx_wchar c = fx_iterator_get_value(ctx->ctx_linebuf_ptr).v_int; if (!is_valid_char(c)) { - ctx->ctx_status = B_ERR_BAD_FORMAT; + ctx->ctx_status = FX_ERR_BAD_FORMAT; return -1; } - b_iterator_move_next(ctx->ctx_linebuf_ptr); + fx_iterator_move_next(ctx->ctx_linebuf_ptr); return c; } -static b_wchar peek_char(struct ctx *ctx) +static fx_wchar peek_char(struct ctx *ctx) { - enum b_status status = data_available(ctx); - if (status == B_ERR_NO_DATA) { + enum fx_status status = data_available(ctx); + if (status == FX_ERR_NO_DATA) { status = refill_linebuf(ctx); } - if (!B_OK(status)) { + if (!FX_OK(status)) { ctx->ctx_status = status; return -1; } status = data_available(ctx); - if (!B_OK(status)) { + if (!FX_OK(status)) { ctx->ctx_status = status; return -1; } - const char *s = b_string_ptr(ctx->ctx_linebuf); + const char *s = fx_string_ptr(ctx->ctx_linebuf); - b_wchar c = b_iterator_get_value(ctx->ctx_linebuf_ptr).v_int; + fx_wchar c = fx_iterator_get_value(ctx->ctx_linebuf_ptr).v_int; if (!is_valid_char(c)) { - ctx->ctx_status = B_ERR_BAD_FORMAT; + ctx->ctx_status = FX_ERR_BAD_FORMAT; return -1; } @@ -328,7 +328,7 @@ static b_wchar peek_char(struct ctx *ctx) #if 0 static int peek_char(struct ctx *ctx) { - b_wchar c = __peek_char(ctx); + fx_wchar c = __peek_char(ctx); if (c != '#') { return c; @@ -345,7 +345,7 @@ static int peek_char(struct ctx *ctx) static int advance_char(struct ctx *ctx) { - b_wchar c = __advance_char(ctx); + fx_wchar c = __advance_char(ctx); if (c != '#') { return c; @@ -372,19 +372,19 @@ static struct token *enqueue_token(struct ctx *ctx, enum token_type type) tok->tok_type = type; - b_queue_push_back(&ctx->ctx_tokens, &tok->tok_entry); + fx_queue_push_back(&ctx->ctx_tokens, &tok->tok_entry); return tok; } static void discard_token(struct ctx *ctx) { - struct b_queue_entry *entry = b_queue_pop_front(&ctx->ctx_tokens); + struct fx_queue_entry *entry = fx_queue_pop_front(&ctx->ctx_tokens); if (!entry) { return; } - struct token *tok = b_unbox(struct token, entry, tok_entry); + struct token *tok = fx_unbox(struct token, entry, tok_entry); if (tok->tok_str) { free(tok->tok_str); @@ -393,26 +393,26 @@ static void discard_token(struct ctx *ctx) free(tok); } -static bool try_convert_word_to_timestamp(struct ctx *ctx, b_string *token_str) +static bool try_convert_word_to_timestamp(struct ctx *ctx, fx_string *token_str) { - b_datetime *dt = b_datetime_parse( - B_DATETIME_FORMAT_RFC3339, b_string_ptr(token_str)); + fx_datetime *dt = fx_datetime_parse( + FX_DATETIME_FORMAT_RFC3339, fx_string_ptr(token_str)); if (!dt) { return false; } struct token *tok = enqueue_token(ctx, TOK_TIMESTAMP); - tok->tok_str = b_string_duplicate(token_str); + tok->tok_str = fx_string_duplicate(token_str); tok->tok_value.time = dt; return true; } #if 0 -static bool try_convert_word_to_timestamp(struct ctx *ctx, b_string *token_str) +static bool try_convert_word_to_timestamp(struct ctx *ctx, fx_string *token_str) { - const char *s = b_string_ptr(token_str); - size_t len = b_string_get_size(token_str, B_STRLEN_NORMAL); + const char *s = fx_string_ptr(token_str); + size_t len = fx_string_get_size(token_str, FX_STRLEN_NORMAL); size_t i = 0, c = 0; struct timestamp ts = {0}; @@ -569,24 +569,24 @@ static bool try_convert_word_to_timestamp(struct ctx *ctx, b_string *token_str) } struct token *tok = enqueue_token(ctx, TOK_TIMESTAMP); - tok->tok_str = b_string_steal(token_str); + tok->tok_str = fx_string_steal(token_str); tok->tok_value.time = ts; return true; } #endif -static bool is_valid_digit(b_wchar c, int base) +static bool is_valid_digit(fx_wchar c, int base) { switch (base) { case 2: - return b_wchar_is_bin_digit(c); + return fx_wchar_is_bin_digit(c); case 8: - return b_wchar_is_oct_digit(c); + return fx_wchar_is_oct_digit(c); case 10: - return b_wchar_is_number(c); + return fx_wchar_is_number(c); case 16: - return b_wchar_is_hex_digit(c); + return fx_wchar_is_hex_digit(c); default: return false; } @@ -613,12 +613,12 @@ static bool has_trailing_zero(const char *s) return false; } -static bool try_convert_word_to_number(struct ctx *ctx, b_string *token_str) +static bool try_convert_word_to_number(struct ctx *ctx, fx_string *token_str) { - size_t len = b_string_get_size(token_str, B_STRLEN_NORMAL); - b_string *str = b_string_duplicate(token_str); + size_t len = fx_string_get_size(token_str, FX_STRLEN_NORMAL); + fx_string *str = fx_string_duplicate(token_str); struct token *tok = NULL; - const char *s = b_string_ptr(str); + const char *s = fx_string_ptr(str); if (len == 0) { return false; @@ -687,8 +687,8 @@ static bool try_convert_word_to_number(struct ctx *ctx, b_string *token_str) return false; } - size_t to_remove = (s - b_string_ptr(str)) + i; - b_string_remove(str, to_remove, 1); + size_t to_remove = (s - fx_string_ptr(str)) + i; + fx_string_remove(str, to_remove, 1); i--; previous = c; continue; @@ -816,18 +816,18 @@ static bool try_convert_word_to_number(struct ctx *ctx, b_string *token_str) return true; } -static bool try_convert_word_to_bool(struct ctx *ctx, b_string *token_str) +static bool try_convert_word_to_bool(struct ctx *ctx, fx_string *token_str) { - const char *s = b_string_ptr(token_str); + const char *s = fx_string_ptr(token_str); struct token *tok = NULL; if (!strcmp(s, "true")) { tok = enqueue_token(ctx, TOK_BOOL); - tok->tok_str = b_string_duplicate(token_str); + tok->tok_str = fx_string_duplicate(token_str); tok->tok_value.b = true; } else if (!strcmp(s, "false")) { tok = enqueue_token(ctx, TOK_BOOL); - tok->tok_str = b_string_duplicate(token_str); + tok->tok_str = fx_string_duplicate(token_str); tok->tok_value.b = false; } else { return false; @@ -836,29 +836,29 @@ static bool try_convert_word_to_bool(struct ctx *ctx, b_string *token_str) return true; } -static void split_word(struct ctx *ctx, b_string *wordbuf) +static void split_word(struct ctx *ctx, fx_string *wordbuf) { #if 0 - long len = b_string_get_size(wordbuf, B_STRLEN_NORMAL); + long len = fx_string_get_size(wordbuf, FX_STRLEN_NORMAL); if (!len) { return; } - char *s = b_string_steal(wordbuf); + char *s = fx_string_steal(wordbuf); int trailing_dots = 0; char prev = 0; for (long i = 0; i < len; i++) { if (prev == '.' && s[i] == '.') { - ctx->ctx_status = B_ERR_BAD_FORMAT; + ctx->ctx_status = FX_ERR_BAD_FORMAT; break; } prev = s[i]; } - if (!B_OK(ctx->ctx_status)) { + if (!FX_OK(ctx->ctx_status)) { free(s); return; } @@ -878,7 +878,7 @@ static void split_word(struct ctx *ctx, b_string *wordbuf) while (tok) { if (*tok == 0) { - ctx->ctx_status = B_ERR_BAD_FORMAT; + ctx->ctx_status = FX_ERR_BAD_FORMAT; break; } @@ -887,7 +887,7 @@ static void split_word(struct ctx *ctx, b_string *wordbuf) } struct token *word = enqueue_token(ctx, TOK_WORD); - word->tok_str = b_strdup(tok); + word->tok_str = fx_strdup(tok); i++; tok = strtok_r(NULL, ".", &ep); @@ -901,11 +901,11 @@ static void split_word(struct ctx *ctx, b_string *wordbuf) #endif const char *delims[] = {"."}; size_t nr_delims = sizeof delims / sizeof delims[0]; - b_iterator *it = b_string_tokenise( - wordbuf, delims, nr_delims, B_STRING_TOK_F_INCLUDE_EMPTY_TOKENS); + fx_iterator *it = fx_string_tokenise( + wordbuf, delims, nr_delims, FX_STRING_TOK_F_INCLUDE_EMPTY_TOKENS); size_t i = 0; - b_foreach_c(const char *, tok, it) + fx_foreach_c(const char *, tok, it) { if (i > 0) { enqueue_token(ctx, TOK_DOT); @@ -914,58 +914,58 @@ static void split_word(struct ctx *ctx, b_string *wordbuf) size_t len = strlen(tok); if (len > 0) { struct token *word = enqueue_token(ctx, TOK_WORD); - word->tok_str = b_string_create_from_cstr(tok); + word->tok_str = fx_string_create_from_cstr(tok); } i++; } - b_iterator_unref(it); + fx_iterator_unref(it); } static void read_number(struct ctx *ctx) { - b_wchar c = 0; - b_string *wordbuf = get_wordbuf(ctx); + fx_wchar c = 0; + fx_string *wordbuf = get_wordbuf(ctx); while (1) { c = peek_char(ctx); - if (c == -1 || !B_OK(ctx->ctx_status)) { + if (c == -1 || !FX_OK(ctx->ctx_status)) { break; } - bool ok = b_wchar_is_alnum(c) || c == '_' || c == '-' + bool ok = fx_wchar_is_alnum(c) || c == '_' || c == '-' || c == '.' || c == '+'; if (!ok) { break; } - b_string_append_wc(wordbuf, c); + fx_string_append_wc(wordbuf, c); advance_char(ctx); } bool is_number = try_convert_word_to_number(ctx, wordbuf); if (!is_number) { - ctx->ctx_status = B_ERR_BAD_FORMAT; + ctx->ctx_status = FX_ERR_BAD_FORMAT; } } static void read_word(struct ctx *ctx) { - b_wchar c = 0; - b_string *wordbuf = get_wordbuf(ctx); + fx_wchar c = 0; + fx_string *wordbuf = get_wordbuf(ctx); while (1) { c = peek_char(ctx); - if (c == -1 || !B_OK(ctx->ctx_status)) { + if (c == -1 || !FX_OK(ctx->ctx_status)) { break; } - bool ok = b_wchar_is_alnum(c) || c == '_' || c == '-' || c == '.'; + bool ok = fx_wchar_is_alnum(c) || c == '_' || c == '-' || c == '.'; if (ctx->ctx_flags & CTX_ENABLE_TIMESTAMPS) { ok = ok || c == ':' || c == ' ' || c == '+'; @@ -979,15 +979,15 @@ static void read_word(struct ctx *ctx) break; } - b_string_append_wc(wordbuf, c); + fx_string_append_wc(wordbuf, c); advance_char(ctx); } bool parsed = false; - b_string_trim(wordbuf); + fx_string_trim(wordbuf); - if (b_string_get_size(wordbuf, B_STRLEN_NORMAL) == 0) { - ctx->ctx_status = B_ERR_BAD_FORMAT; + if (fx_string_get_size(wordbuf, FX_STRLEN_NORMAL) == 0) { + ctx->ctx_status = FX_ERR_BAD_FORMAT; return; } @@ -1007,24 +1007,24 @@ static void read_word(struct ctx *ctx) return; } - b_iterator *it = b_iterator_begin(wordbuf); - b_foreach(b_wchar, c, it) + fx_iterator *it = fx_iterator_begin(wordbuf); + fx_foreach(fx_wchar, c, it) { /* only allow ASCII numbers/letters here */ bool ok = isalnum(c) || c == '_' || c == '-' || c == '.'; if (!ok) { - ctx->ctx_status = B_ERR_BAD_FORMAT; + ctx->ctx_status = FX_ERR_BAD_FORMAT; return; } } - b_iterator_unref(it); + fx_iterator_unref(it); split_word(ctx, wordbuf); } -static b_wchar read_unicode_sequence(struct ctx *ctx) +static fx_wchar read_unicode_sequence(struct ctx *ctx) { - b_wchar c = peek_char(ctx); + fx_wchar c = peek_char(ctx); int expected_len = 0; switch (c) { @@ -1035,7 +1035,7 @@ static b_wchar read_unicode_sequence(struct ctx *ctx) expected_len = 8; break; default: - return B_WCHAR_INVALID; + return FX_WCHAR_INVALID; } advance_char(ctx); @@ -1048,12 +1048,12 @@ static b_wchar read_unicode_sequence(struct ctx *ctx) break; } - b_wchar c = peek_char(ctx); - if (c == -1 || c == B_WCHAR_INVALID) { + fx_wchar c = peek_char(ctx); + if (c == -1 || c == FX_WCHAR_INVALID) { break; } - if (!b_wchar_is_hex_digit(c)) { + if (!fx_wchar_is_hex_digit(c)) { break; } @@ -1063,13 +1063,13 @@ static b_wchar read_unicode_sequence(struct ctx *ctx) } if (len != expected_len) { - return B_WCHAR_INVALID; + return FX_WCHAR_INVALID; } char *ep; c = strtoul(s, &ep, 16); if (*ep != 0) { - return B_WCHAR_INVALID; + return FX_WCHAR_INVALID; } return c; @@ -1086,9 +1086,9 @@ static void read_string(struct ctx *ctx, bool squote) bool multiline = false; struct token *tok = enqueue_token(ctx, TOK_STRING); - b_string *str = get_wordbuf(ctx); + fx_string *str = get_wordbuf(ctx); - b_wchar c = peek_char(ctx); + fx_wchar c = peek_char(ctx); if (c == term) { advance_char(ctx); c = peek_char(ctx); @@ -1098,7 +1098,7 @@ static void read_string(struct ctx *ctx, bool squote) c = peek_char(ctx); multiline = true; } else { - tok->tok_str = b_string_duplicate(str); + tok->tok_str = fx_string_duplicate(str); return; } @@ -1108,7 +1108,7 @@ static void read_string(struct ctx *ctx, bool squote) } if (multiline && !(ctx->ctx_flags & CTX_ENABLE_MULTILINE_STRING)) { - ctx->ctx_status = B_ERR_BAD_FORMAT; + ctx->ctx_status = FX_ERR_BAD_FORMAT; return; } @@ -1121,7 +1121,7 @@ static void read_string(struct ctx *ctx, bool squote) while (!fail) { c = peek_char(ctx); if (c == -1) { - ctx->ctx_status = B_ERR_BAD_FORMAT; + ctx->ctx_status = FX_ERR_BAD_FORMAT; fail = true; break; } @@ -1132,7 +1132,7 @@ static void read_string(struct ctx *ctx, bool squote) cr = true; continue; } else { - ctx->ctx_status = B_ERR_BAD_FORMAT; + ctx->ctx_status = FX_ERR_BAD_FORMAT; fail = true; break; } @@ -1157,7 +1157,7 @@ static void read_string(struct ctx *ctx, bool squote) } if (c != '\n') { - ctx->ctx_status = B_ERR_BAD_FORMAT; + ctx->ctx_status = FX_ERR_BAD_FORMAT; fail = true; break; } @@ -1175,45 +1175,45 @@ static void read_string(struct ctx *ctx, bool squote) switch (c) { case '"': case '\\': - b_string_append_wc(str, c); + fx_string_append_wc(str, c); advance_char(ctx); break; case 'b': - b_string_append_c(str, '\b'); + fx_string_append_c(str, '\b'); advance_char(ctx); break; case 't': - b_string_append_c(str, '\t'); + fx_string_append_c(str, '\t'); advance_char(ctx); break; case 'n': - b_string_append_c(str, '\n'); + fx_string_append_c(str, '\n'); advance_char(ctx); break; case 'r': - b_string_append_c(str, '\r'); + fx_string_append_c(str, '\r'); advance_char(ctx); break; case 'f': - b_string_append_c(str, '\f'); + fx_string_append_c(str, '\f'); advance_char(ctx); break; case 'u': case 'U': c = read_unicode_sequence(ctx); - if (c == B_WCHAR_INVALID) { - ctx->ctx_status = B_ERR_BAD_FORMAT; + if (c == FX_WCHAR_INVALID) { + ctx->ctx_status = FX_ERR_BAD_FORMAT; fail = true; break; } - ctx->ctx_status = B_OK(b_string_append_wc(str, c)) - ? B_SUCCESS - : B_ERR_BAD_FORMAT; - fail = !B_OK(ctx->ctx_status); + ctx->ctx_status = FX_OK(fx_string_append_wc(str, c)) + ? FX_SUCCESS + : FX_ERR_BAD_FORMAT; + fail = !FX_OK(ctx->ctx_status); break; default: - ctx->ctx_status = B_ERR_BAD_FORMAT; + ctx->ctx_status = FX_ERR_BAD_FORMAT; fail = true; break; } @@ -1229,16 +1229,16 @@ static void read_string(struct ctx *ctx, bool squote) else if (c == '\n') { if (!multiline) { fail = true; - ctx->ctx_status = B_ERR_BAD_FORMAT; + ctx->ctx_status = FX_ERR_BAD_FORMAT; break; } if (cr) { - b_string_append_wc(str, '\r'); + fx_string_append_wc(str, '\r'); cr = false; } - b_string_append_wc(str, c); + fx_string_append_wc(str, c); } else if (c == term) { @@ -1250,15 +1250,15 @@ static void read_string(struct ctx *ctx, bool squote) c = peek_char(ctx); if (c != term) { - b_string_append_wc(str, term); + fx_string_append_wc(str, term); continue; } advance_char(ctx); c = peek_char(ctx); if (c != term) { - b_string_append_wc(str, term); - b_string_append_wc(str, term); + fx_string_append_wc(str, term); + fx_string_append_wc(str, term); continue; } @@ -1268,7 +1268,7 @@ static void read_string(struct ctx *ctx, bool squote) } else { - b_string_append_wc(str, c); + fx_string_append_wc(str, c); } advance_char(ctx); @@ -1289,23 +1289,23 @@ static void read_string(struct ctx *ctx, bool squote) c = peek_char(ctx); if (c == term) { - b_string_append_wc(str, c); + fx_string_append_wc(str, c); advance_char(ctx); } c = peek_char(ctx); if (c == term) { - b_string_append_wc(str, c); + fx_string_append_wc(str, c); advance_char(ctx); } done: - tok->tok_str = b_string_duplicate(str); + tok->tok_str = fx_string_duplicate(str); } static void read_symbol(struct ctx *ctx) { - b_wchar c = peek_char(ctx); + fx_wchar c = peek_char(ctx); advance_char(ctx); struct token *tok = enqueue_token(ctx, TOK_NONE); @@ -1365,26 +1365,26 @@ static void read_symbol(struct ctx *ctx) break; default: discard_token(ctx); - ctx->ctx_status = B_ERR_BAD_FORMAT; + ctx->ctx_status = FX_ERR_BAD_FORMAT; break; } } static void read_newline(struct ctx *ctx) { - b_wchar c = peek_char(ctx); + fx_wchar c = peek_char(ctx); while (c == '\n') { advance_char(ctx); c = peek_char(ctx); } enqueue_token(ctx, TOK_NEWLINE); - ctx->ctx_status = B_SUCCESS; + ctx->ctx_status = FX_SUCCESS; } static void read_comment(struct ctx *ctx) { - b_wchar c = peek_char(ctx); + fx_wchar c = peek_char(ctx); bool cr = false; while (1) { @@ -1398,7 +1398,7 @@ static void read_comment(struct ctx *ctx) } if (cr) { - ctx->ctx_status = B_ERR_BAD_FORMAT; + ctx->ctx_status = FX_ERR_BAD_FORMAT; break; } @@ -1411,10 +1411,10 @@ static void read_comment(struct ctx *ctx) } if (cr) { - ctx->ctx_status = B_ERR_BAD_FORMAT; + ctx->ctx_status = FX_ERR_BAD_FORMAT; } - if (!B_OK(ctx->ctx_status)) { + if (!FX_OK(ctx->ctx_status)) { return; } @@ -1422,7 +1422,7 @@ static void read_comment(struct ctx *ctx) enqueue_token(ctx, TOK_NEWLINE); } -static bool is_symbol(b_wchar c) +static bool is_symbol(fx_wchar c) { switch (c) { case '=': @@ -1438,13 +1438,13 @@ static bool is_symbol(b_wchar c) } } -static enum b_status advance_token(struct ctx *ctx) +static enum fx_status advance_token(struct ctx *ctx) { - b_wchar c = B_WCHAR_INVALID; + fx_wchar c = FX_WCHAR_INVALID; discard_token(ctx); - if (!b_queue_empty(&ctx->ctx_tokens)) { - return B_SUCCESS; + if (!fx_queue_empty(&ctx->ctx_tokens)) { + return FX_SUCCESS; } start: @@ -1456,7 +1456,7 @@ start: if (c == -1) { ctx->ctx_flags |= CTX_EOF; - return B_ERR_NO_DATA; + return FX_ERR_NO_DATA; } #if 1 @@ -1466,7 +1466,7 @@ start: } #endif - if (!B_OK(ctx->ctx_status)) { + if (!FX_OK(ctx->ctx_status)) { return ctx->ctx_status; } @@ -1475,7 +1475,7 @@ start: c = peek_char(ctx); if (c != '\n') { - ctx->ctx_status = B_ERR_BAD_FORMAT; + ctx->ctx_status = FX_ERR_BAD_FORMAT; return ctx->ctx_status; } } @@ -1499,54 +1499,54 @@ start: static struct token *peek_token(struct ctx *ctx) { - struct b_queue_entry *entry = b_queue_first(&ctx->ctx_tokens); + struct fx_queue_entry *entry = fx_queue_first(&ctx->ctx_tokens); if (!entry) { return NULL; } - return b_unbox(struct token, entry, tok_entry); + return fx_unbox(struct token, entry, tok_entry); } static void ctx_cleanup(struct ctx *ctx) { if (ctx->ctx_linebuf_ptr) { - b_iterator_unref(ctx->ctx_linebuf_ptr); + fx_iterator_unref(ctx->ctx_linebuf_ptr); ctx->ctx_linebuf_ptr = NULL; } if (ctx->ctx_linebuf) { - b_string_unref(ctx->ctx_linebuf); + fx_string_unref(ctx->ctx_linebuf); ctx->ctx_linebuf = NULL; } if (ctx->ctx_wordbuf) { - b_string_unref(ctx->ctx_wordbuf); + fx_string_unref(ctx->ctx_wordbuf); ctx->ctx_wordbuf = NULL; } if (ctx->ctx_objects_flags) { - b_hashmap_unref(ctx->ctx_objects_flags); + fx_hashmap_unref(ctx->ctx_objects_flags); ctx->ctx_objects_flags = NULL; } } -static enum b_status ctx_init(struct ctx *ctx) +static enum fx_status ctx_init(struct ctx *ctx) { memset(ctx, 0x0, sizeof *ctx); - ctx->ctx_linebuf = b_string_create(); - ctx->ctx_wordbuf = b_string_create(); + ctx->ctx_linebuf = fx_string_create(); + ctx->ctx_wordbuf = fx_string_create(); - ctx->ctx_objects_flags = b_hashmap_create(NULL, NULL); + ctx->ctx_objects_flags = fx_hashmap_create(NULL, NULL); - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status toml_serialise( - b_serial_ctx *serial, b_object *src, b_stream *dest, - enum b_serial_flags flags) +static enum fx_status toml_serialise( + fx_serial_ctx *serial, fx_object *src, fx_stream *dest, + enum fx_serial_flags flags) { - return B_ERR_NOT_SUPPORTED; + return FX_ERR_NOT_SUPPORTED; } static void print_token(struct token *tok) @@ -1556,27 +1556,27 @@ static void print_token(struct token *tok) printf("TOK_NONE\n"); break; case TOK_WORD: - printf("TOK_WORD %s\n", b_string_ptr(tok->tok_str)); + printf("TOK_WORD %s\n", fx_string_ptr(tok->tok_str)); break; case TOK_STRING: - printf("TOK_STRING %s\n", b_string_ptr(tok->tok_str)); + printf("TOK_STRING %s\n", fx_string_ptr(tok->tok_str)); break; case TOK_TIMESTAMP: printf("TOK_TIMESTAMP %04ld-%02ld-%02ld " "%02ld:%02ld:%02ld.%04ld %c" "%02ld:%02ld\n", - b_datetime_year(tok->tok_value.time), - b_datetime_month(tok->tok_value.time), - b_datetime_day(tok->tok_value.time), - b_datetime_hour(tok->tok_value.time), - b_datetime_minute(tok->tok_value.time), - b_datetime_second(tok->tok_value.time), - b_datetime_subsecond(tok->tok_value.time), - b_datetime_zone_offset_is_negative(tok->tok_value.time) + fx_datetime_year(tok->tok_value.time), + fx_datetime_month(tok->tok_value.time), + fx_datetime_day(tok->tok_value.time), + fx_datetime_hour(tok->tok_value.time), + fx_datetime_minute(tok->tok_value.time), + fx_datetime_second(tok->tok_value.time), + fx_datetime_subsecond(tok->tok_value.time), + fx_datetime_zone_offset_is_negative(tok->tok_value.time) ? '-' : '+', - b_datetime_zone_offset_hour(tok->tok_value.time), - b_datetime_zone_offset_minute(tok->tok_value.time)); + fx_datetime_zone_offset_hour(tok->tok_value.time), + fx_datetime_zone_offset_minute(tok->tok_value.time)); break; case TOK_INT: printf("TOK_INT "); @@ -1636,126 +1636,126 @@ static void print_token(struct token *tok) } } -static enum b_status parse_value(struct ctx *ctx, b_object **result); -static enum b_status parse_key_value_pair(struct ctx *ctx, b_dict *container); +static enum fx_status parse_value(struct ctx *ctx, fx_object **result); +static enum fx_status parse_key_value_pair(struct ctx *ctx, fx_dict *container); -static enum b_status parse_timestamp(struct ctx *ctx, b_object **result) +static enum fx_status parse_timestamp(struct ctx *ctx, fx_object **result) { struct token *tok = peek_token(ctx); - b_datetime *dt = tok->tok_value.time; + fx_datetime *dt = tok->tok_value.time; tok->tok_value.time = NULL; *result = (dt); - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status parse_string(struct ctx *ctx, b_object **result) +static enum fx_status parse_string(struct ctx *ctx, fx_object **result) { struct token *tok = peek_token(ctx); - b_string *str = b_string_duplicate(tok->tok_str); + fx_string *str = fx_string_duplicate(tok->tok_str); if (!str) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } *result = (str); - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status parse_int(struct ctx *ctx, b_object **result) +static enum fx_status parse_int(struct ctx *ctx, fx_object **result) { struct token *tok = peek_token(ctx); - b_number *val = B_LONGLONG(tok->tok_value.i.v); + fx_number *val = FX_LONGLONG(tok->tok_value.i.v); if (!val) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } if (tok->tok_value.i.inf) { if (tok->tok_value.i.v >= 0) { - b_number_set_inf_positive(val, true); + fx_number_set_inf_positive(val, true); } else { - b_number_set_inf_negative(val, true); + fx_number_set_inf_negative(val, true); } } else if (tok->tok_value.i.nan) { if (tok->tok_value.i.v >= 0) { - b_number_set_nan_positive(val, true); + fx_number_set_nan_positive(val, true); } else { - b_number_set_nan_negative(val, true); + fx_number_set_nan_negative(val, true); } } *result = (val); - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status parse_float(struct ctx *ctx, b_object **result) +static enum fx_status parse_float(struct ctx *ctx, fx_object **result) { struct token *tok = peek_token(ctx); - b_number *val = B_DOUBLE(tok->tok_value.f.v); + fx_number *val = FX_DOUBLE(tok->tok_value.f.v); if (!val) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } if (tok->tok_value.f.inf) { if (tok->tok_value.f.v >= 0) { - b_number_set_inf_positive(val, true); + fx_number_set_inf_positive(val, true); } else { - b_number_set_inf_negative(val, true); + fx_number_set_inf_negative(val, true); } } else if (tok->tok_value.f.nan) { if (tok->tok_value.f.v >= 0) { - b_number_set_nan_positive(val, true); + fx_number_set_nan_positive(val, true); } else { - b_number_set_nan_negative(val, true); + fx_number_set_nan_negative(val, true); } } *result = (val); - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status parse_bool(struct ctx *ctx, b_object **result) +static enum fx_status parse_bool(struct ctx *ctx, fx_object **result) { struct token *tok = peek_token(ctx); - b_number *val = B_INT8(tok->tok_value.b); + fx_number *val = FX_INT8(tok->tok_value.b); if (!val) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } *result = (val); - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status parse_table_inline(struct ctx *ctx, b_object **result) +static enum fx_status parse_table_inline(struct ctx *ctx, fx_object **result) { DISABLE_EXTENDED_LEXING(ctx); advance_token(ctx); - b_dict *table = b_dict_create(); + fx_dict *table = fx_dict_create(); if (!table) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } struct token *tok = peek_token(ctx); if (tok && tok->tok_type == TOK_RIGHT_BRACE) { *result = (table); - return B_SUCCESS; + return FX_SUCCESS; } bool done = false; while (!done) { - b_object *value; - enum b_status status = parse_key_value_pair(ctx, table); - if (!B_OK(status)) { - b_dict_unref(table); + fx_object *value; + enum fx_status status = parse_key_value_pair(ctx, table); + if (!FX_OK(status)) { + fx_dict_unref(table); return status; } tok = peek_token(ctx); if (!tok) { - b_dict_unref(table); + fx_dict_unref(table); return status; } @@ -1767,13 +1767,13 @@ static enum b_status parse_table_inline(struct ctx *ctx, b_object **result) advance_token(ctx); break; default: - b_dict_unref(table); - return B_ERR_BAD_FORMAT; + fx_dict_unref(table); + return FX_ERR_BAD_FORMAT; } } *result = (table); - return B_SUCCESS; + return FX_SUCCESS; } static void skip_newlines(struct ctx *ctx) @@ -1786,22 +1786,22 @@ static void skip_newlines(struct ctx *ctx) } } -static enum b_status parse_array_inline(struct ctx *ctx, b_object **result) +static enum fx_status parse_array_inline(struct ctx *ctx, fx_object **result) { bool done = false; ENABLE_EXTENDED_LEXING(ctx); advance_token(ctx); - b_array *array = b_array_create(); + fx_array *array = fx_array_create(); if (!array) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } struct token *tok = peek_token(ctx); if (!tok) { - b_array_unref(array); - return B_ERR_BAD_FORMAT; + fx_array_unref(array); + return FX_ERR_BAD_FORMAT; } if (tok->tok_type == TOK_RIGHT_BRACKET) { @@ -1814,8 +1814,8 @@ static enum b_status parse_array_inline(struct ctx *ctx, b_object **result) tok = peek_token(ctx); if (!tok) { - b_array_unref(array); - return B_ERR_BAD_FORMAT; + fx_array_unref(array); + return FX_ERR_BAD_FORMAT; } if (tok->tok_type == TOK_RIGHT_BRACKET) { @@ -1823,14 +1823,14 @@ static enum b_status parse_array_inline(struct ctx *ctx, b_object **result) break; } - b_object *value; - enum b_status status = parse_value(ctx, &value); - if (!B_OK(status)) { - b_array_unref(array); + fx_object *value; + enum fx_status status = parse_value(ctx, &value); + if (!FX_OK(status)) { + fx_array_unref(array); return status; } - b_array_append(array, B_RV(value)); + fx_array_append(array, FX_RV(value)); ENABLE_EXTENDED_LEXING(ctx); advance_token(ctx); @@ -1844,8 +1844,8 @@ static enum b_status parse_array_inline(struct ctx *ctx, b_object **result) } if (!tok || tok->tok_type != TOK_COMMA) { - b_array_unref(array); - return B_ERR_BAD_FORMAT; + fx_array_unref(array); + return FX_ERR_BAD_FORMAT; } ENABLE_EXTENDED_LEXING(ctx); @@ -1854,15 +1854,15 @@ static enum b_status parse_array_inline(struct ctx *ctx, b_object **result) DISABLE_EXTENDED_LEXING(ctx); *result = (array); - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status parse_value(struct ctx *ctx, b_object **result) +static enum fx_status parse_value(struct ctx *ctx, fx_object **result) { struct token *tok = peek_token(ctx); if (!tok) { - return B_ERR_BAD_FORMAT; + return FX_ERR_BAD_FORMAT; } switch (tok->tok_type) { @@ -1881,43 +1881,43 @@ static enum b_status parse_value(struct ctx *ctx, b_object **result) case TOK_LEFT_BRACE: return parse_table_inline(ctx, result); default: - return B_ERR_BAD_FORMAT; + return FX_ERR_BAD_FORMAT; } } -static enum b_status parse_key_value_pair(struct ctx *ctx, b_dict *container) +static enum fx_status parse_key_value_pair(struct ctx *ctx, fx_dict *container) { struct token *tok = peek_token(ctx); if (!IS_VALID_KEY_COMPONENT(tok)) { - return B_ERR_BAD_FORMAT; + return FX_ERR_BAD_FORMAT; } - b_string *key = b_string_duplicate(tok->tok_str); + fx_string *key = fx_string_duplicate(tok->tok_str); if (!key) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } advance_token(ctx); tok = peek_token(ctx); if (!tok) { - return B_ERR_BAD_FORMAT; + return FX_ERR_BAD_FORMAT; } while (tok && tok->tok_type == TOK_DOT) { - b_object *sub_dict = b_dict_at_sk(container, key); + fx_object *sub_dict = fx_dict_at_sk(container, key); if (!sub_dict) { - sub_dict = (b_dict_create()); - b_dict_put_sk(container, key, B_RV(sub_dict)); - } else if (sub_dict && !b_object_is_type(sub_dict, B_TYPE_DICT)) { + sub_dict = (fx_dict_create()); + fx_dict_put_sk(container, key, FX_RV(sub_dict)); + } else if (sub_dict && !fx_object_is_type(sub_dict, FX_TYPE_DICT)) { free(key); - return B_ERR_BAD_FORMAT; + return FX_ERR_BAD_FORMAT; } #if 1 enum object_flags flags = ctx_get_object_flags(ctx, sub_dict); if (flags & (OBJECT_KV_END_DEFINED | OBJECT_HEADER_END_DEFINED)) { free(key); - return B_ERR_BAD_FORMAT; + return FX_ERR_BAD_FORMAT; } #endif @@ -1927,109 +1927,109 @@ static enum b_status parse_key_value_pair(struct ctx *ctx, b_dict *container) tok = peek_token(ctx); if (!IS_VALID_KEY_COMPONENT(tok)) { free(key); - return B_ERR_BAD_FORMAT; + return FX_ERR_BAD_FORMAT; } container = sub_dict; - b_string_unref(key); - key = b_string_duplicate(tok->tok_str); + fx_string_unref(key); + key = fx_string_duplicate(tok->tok_str); if (!key) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } advance_token(ctx); tok = peek_token(ctx); } - if (b_dict_has_skey(container, key)) { - return B_ERR_BAD_FORMAT; + if (fx_dict_has_skey(container, key)) { + return FX_ERR_BAD_FORMAT; } if (!tok) { - return B_ERR_BAD_FORMAT; + return FX_ERR_BAD_FORMAT; } if (tok->tok_type != TOK_EQUAL) { - return B_ERR_BAD_FORMAT; + return FX_ERR_BAD_FORMAT; } ENABLE_EXTENDED_LEXING(ctx); advance_token(ctx); - b_object *value = NULL; - enum b_status status = parse_value(ctx, &value); + fx_object *value = NULL; + enum fx_status status = parse_value(ctx, &value); DISABLE_EXTENDED_LEXING(ctx); - if (!B_OK(status)) { + if (!FX_OK(status)) { return status; } status = advance_token(ctx); - if (!B_OK(status) && status != B_ERR_NO_DATA) { + if (!FX_OK(status) && status != FX_ERR_NO_DATA) { return status; } - b_dict_put_sk(container, key, B_RV(value)); + fx_dict_put_sk(container, key, FX_RV(value)); - if (b_object_is_type(value, B_TYPE_DICT) - || b_object_is_type(value, B_TYPE_ARRAY)) { + if (fx_object_is_type(value, FX_TYPE_DICT) + || fx_object_is_type(value, FX_TYPE_ARRAY)) { ctx_set_object_flags(ctx, value, OBJECT_KV_END_DEFINED); } - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status parse_table_header( - struct ctx *ctx, b_dict *container, b_dict **new_container) +static enum fx_status parse_table_header( + struct ctx *ctx, fx_dict *container, fx_dict **new_container) { advance_token(ctx); struct token *tok = peek_token(ctx); if (!IS_VALID_KEY_COMPONENT(tok)) { - return B_ERR_BAD_FORMAT; + return FX_ERR_BAD_FORMAT; } - b_string *key = b_string_duplicate(tok->tok_str); + fx_string *key = fx_string_duplicate(tok->tok_str); if (!key) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } advance_token(ctx); tok = peek_token(ctx); if (!tok) { - return B_ERR_BAD_FORMAT; + return FX_ERR_BAD_FORMAT; } while (tok && tok->tok_type == TOK_DOT) { - b_object *sub_dict = b_dict_at_sk(container, key); + fx_object *sub_dict = fx_dict_at_sk(container, key); enum object_flags flags = ctx_get_object_flags(ctx, sub_dict); if (!sub_dict) { - sub_dict = (b_dict_create()); - b_dict_put_sk(container, key, B_RV(sub_dict)); - } else if (b_object_is_type(sub_dict, B_TYPE_ARRAY)) { + sub_dict = (fx_dict_create()); + fx_dict_put_sk(container, key, FX_RV(sub_dict)); + } else if (fx_object_is_type(sub_dict, FX_TYPE_ARRAY)) { - sub_dict = b_array_at(sub_dict, b_array_size(sub_dict) - 1); - } else if (!b_object_is_type(sub_dict, B_TYPE_DICT)) { - return B_ERR_BAD_FORMAT; + sub_dict = fx_array_at(sub_dict, fx_array_size(sub_dict) - 1); + } else if (!fx_object_is_type(sub_dict, FX_TYPE_DICT)) { + return FX_ERR_BAD_FORMAT; } if (flags & OBJECT_KV_END_DEFINED) { - return B_ERR_BAD_FORMAT; + return FX_ERR_BAD_FORMAT; } advance_token(ctx); tok = peek_token(ctx); if (!IS_VALID_KEY_COMPONENT(tok)) { - return B_ERR_BAD_FORMAT; + return FX_ERR_BAD_FORMAT; } ctx_set_object_flags(ctx, sub_dict, OBJECT_HEADER_MID_DEFINED); container = sub_dict; - b_string_unref(key); - key = b_string_duplicate(tok->tok_str); + fx_string_unref(key); + key = fx_string_duplicate(tok->tok_str); if (!key) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } advance_token(ctx); @@ -2037,83 +2037,83 @@ static enum b_status parse_table_header( } if (!tok || tok->tok_type != TOK_RIGHT_BRACKET) { - return B_ERR_BAD_FORMAT; + return FX_ERR_BAD_FORMAT; } - b_dict *new_table = b_dict_at_sk(container, key); + fx_dict *new_table = fx_dict_at_sk(container, key); if (!new_table) { - new_table = b_dict_create(); + new_table = fx_dict_create(); if (!new_table) { free(key); - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } - b_dict_put_sk(container, key, B_RV(new_table)); + fx_dict_put_sk(container, key, FX_RV(new_table)); } - if (!b_object_is_type((new_table), B_TYPE_DICT)) { - return B_ERR_BAD_FORMAT; + if (!fx_object_is_type((new_table), FX_TYPE_DICT)) { + return FX_ERR_BAD_FORMAT; } enum object_flags flags = ctx_get_object_flags(ctx, (new_table)); if (flags & (OBJECT_HEADER_END_DEFINED | OBJECT_KV_MID_DEFINED | OBJECT_KV_END_DEFINED)) { - return B_ERR_BAD_FORMAT; + return FX_ERR_BAD_FORMAT; } ctx_set_object_flags(ctx, (new_table), OBJECT_HEADER_END_DEFINED); - b_string_unref(key); + fx_string_unref(key); advance_token(ctx); *new_container = new_table; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status parse_array_header( - struct ctx *ctx, b_dict *container, b_dict **new_container) +static enum fx_status parse_array_header( + struct ctx *ctx, fx_dict *container, fx_dict **new_container) { advance_token(ctx); struct token *tok = peek_token(ctx); if (!IS_VALID_KEY_COMPONENT(tok)) { - return B_ERR_BAD_FORMAT; + return FX_ERR_BAD_FORMAT; } - b_string *key = b_string_duplicate(tok->tok_str); + fx_string *key = fx_string_duplicate(tok->tok_str); if (!key) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } advance_token(ctx); tok = peek_token(ctx); if (!tok) { - return B_ERR_BAD_FORMAT; + return FX_ERR_BAD_FORMAT; } while (tok && tok->tok_type == TOK_DOT) { - b_object *sub_dict = b_dict_at_sk(container, key); + fx_object *sub_dict = fx_dict_at_sk(container, key); if (!sub_dict) { - sub_dict = (b_dict_create()); - b_dict_put_sk(container, key, B_RV(sub_dict)); - } else if (b_object_is_type(sub_dict, B_TYPE_ARRAY)) { - sub_dict = b_array_at(sub_dict, b_array_size(sub_dict) - 1); - } else if (!b_object_is_type(sub_dict, B_TYPE_DICT)) { - return B_ERR_BAD_FORMAT; + sub_dict = (fx_dict_create()); + fx_dict_put_sk(container, key, FX_RV(sub_dict)); + } else if (fx_object_is_type(sub_dict, FX_TYPE_ARRAY)) { + sub_dict = fx_array_at(sub_dict, fx_array_size(sub_dict) - 1); + } else if (!fx_object_is_type(sub_dict, FX_TYPE_DICT)) { + return FX_ERR_BAD_FORMAT; } advance_token(ctx); tok = peek_token(ctx); if (!IS_VALID_KEY_COMPONENT(tok)) { - return B_ERR_BAD_FORMAT; + return FX_ERR_BAD_FORMAT; } container = sub_dict; - b_string_unref(key); - key = b_string_duplicate(tok->tok_str); + fx_string_unref(key); + key = fx_string_duplicate(tok->tok_str); if (!key) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } advance_token(ctx); @@ -2121,42 +2121,42 @@ static enum b_status parse_array_header( } if (!tok || tok->tok_type != TOK_DOUBLE_RIGHT_BRACKET) { - return B_ERR_BAD_FORMAT; + return FX_ERR_BAD_FORMAT; } - b_array *array = b_dict_get_sk(container, key); + fx_array *array = fx_dict_get_sk(container, key); if (!array) { - array = b_array_create(); - b_dict_put_sk(container, key, B_RV(array)); - } else if (!b_object_is_type(array, B_TYPE_ARRAY)) { - return B_ERR_BAD_FORMAT; + array = fx_array_create(); + fx_dict_put_sk(container, key, FX_RV(array)); + } else if (!fx_object_is_type(array, FX_TYPE_ARRAY)) { + return FX_ERR_BAD_FORMAT; } free(key); enum object_flags flags = ctx_get_object_flags(ctx, (array)); if (flags & OBJECT_KV_END_DEFINED) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } - b_dict *new_table = b_dict_create(); + fx_dict *new_table = fx_dict_create(); if (!new_table) { - return B_ERR_NO_MEMORY; + return FX_ERR_NO_MEMORY; } - b_array_append(array, B_RV(new_table)); + fx_array_append(array, FX_RV(new_table)); advance_token(ctx); *new_container = new_table; - return B_SUCCESS; + return FX_SUCCESS; } -static enum b_status parse_root(struct ctx *ctx, b_dict **result) +static enum fx_status parse_root(struct ctx *ctx, fx_dict **result) { - enum b_status status = B_SUCCESS; - b_dict *root = b_dict_create(); - b_dict *current = root; + enum fx_status status = FX_SUCCESS; + fx_dict *root = fx_dict_create(); + fx_dict *current = root; - while (!(ctx->ctx_flags & CTX_EOF) && B_OK(status)) { + while (!(ctx->ctx_flags & CTX_EOF) && FX_OK(status)) { struct token *tok = peek_token(ctx); if (!tok) { break; @@ -2165,36 +2165,36 @@ static enum b_status parse_root(struct ctx *ctx, b_dict **result) switch (tok->tok_type) { case TOK_LEFT_BRACKET: status = parse_table_header(ctx, root, ¤t); - if (!B_OK(status)) { + if (!FX_OK(status)) { break; } tok = peek_token(ctx); if (tok && tok->tok_type != TOK_NEWLINE) { - status = B_ERR_BAD_FORMAT; + status = FX_ERR_BAD_FORMAT; } break; case TOK_DOUBLE_LEFT_BRACKET: status = parse_array_header(ctx, root, ¤t); - if (!B_OK(status)) { + if (!FX_OK(status)) { break; } tok = peek_token(ctx); if (tok && tok->tok_type != TOK_NEWLINE) { - status = B_ERR_BAD_FORMAT; + status = FX_ERR_BAD_FORMAT; } break; case TOK_WORD: case TOK_STRING: status = parse_key_value_pair(ctx, current); - if (!B_OK(status)) { + if (!FX_OK(status)) { break; } tok = peek_token(ctx); if (tok && tok->tok_type != TOK_NEWLINE) { - status = B_ERR_BAD_FORMAT; + status = FX_ERR_BAD_FORMAT; } advance_token(ctx); break; @@ -2202,17 +2202,17 @@ static enum b_status parse_root(struct ctx *ctx, b_dict **result) advance_token(ctx); break; default: - status = B_ERR_BAD_FORMAT; + status = FX_ERR_BAD_FORMAT; break; } - if (!B_OK(ctx->ctx_status) && ctx->ctx_status != B_ERR_NO_DATA) { + if (!FX_OK(ctx->ctx_status) && ctx->ctx_status != FX_ERR_NO_DATA) { status = ctx->ctx_status; } } - if (!B_OK(status)) { - b_dict_unref(root); + if (!FX_OK(status)) { + fx_dict_unref(root); root = NULL; } @@ -2220,14 +2220,14 @@ static enum b_status parse_root(struct ctx *ctx, b_dict **result) return status; } -static enum b_status toml_deserialise( - b_serial_ctx *serial, b_stream *src, b_object **dest, - enum b_serial_flags flags) +static enum fx_status toml_deserialise( + fx_serial_ctx *serial, fx_stream *src, fx_object **dest, + enum fx_serial_flags flags) { struct ctx ctx = {0}; - enum b_status status = ctx_init(&ctx); + enum fx_status status = ctx_init(&ctx); - if (!B_OK(status)) { + if (!FX_OK(status)) { return status; } @@ -2236,18 +2236,18 @@ static enum b_status toml_deserialise( status = advance_token(&ctx); - if (!B_OK(ctx.ctx_status) && ctx.ctx_status != B_ERR_NO_DATA) { + if (!FX_OK(ctx.ctx_status) && ctx.ctx_status != FX_ERR_NO_DATA) { return ctx.ctx_status; } if (ctx.ctx_flags & CTX_EOF) { - *dest = (b_dict_create()); - return B_SUCCESS; + *dest = (fx_dict_create()); + return FX_SUCCESS; } - b_dict *result = NULL; + fx_dict *result = NULL; status = parse_root(&ctx, &result); - if (!B_OK(status)) { + if (!FX_OK(status)) { return status; } @@ -2256,43 +2256,43 @@ static enum b_status toml_deserialise( ctx.ctx_flags = CTX_ENABLE_NUMBERS | CTX_ENABLE_TIMESTAMPS | CTX_ENABLE_BOOLS; - while (!(ctx.ctx_flags & CTX_EOF) && B_OK(ctx.ctx_status)) { + while (!(ctx.ctx_flags & CTX_EOF) && FX_OK(ctx.ctx_status)) { struct token *tok = peek_token(&ctx); print_token(tok); status = advance_token(&ctx); } #endif - return B_SUCCESS; + return FX_SUCCESS; } /*** VIRTUAL FUNCTIONS ********************************************************/ -static void toml_serial_ctx_init(b_object *obj, void *priv) +static void toml_serial_ctx_init(fx_object *obj, void *priv) { } -static void toml_serial_ctx_fini(b_object *obj, void *priv) +static void toml_serial_ctx_fini(fx_object *obj, void *priv) { } /*** CLASS DEFINITION *********************************************************/ -B_TYPE_CLASS_DEFINITION_BEGIN(b_toml_serial_ctx) - B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) - B_INTERFACE_ENTRY(to_string) = NULL; - B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) +FX_TYPE_CLASS_DEFINITION_BEGIN(fx_toml_serial_ctx) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) - B_TYPE_CLASS_INTERFACE_BEGIN(b_serial_ctx, B_TYPE_SERIAL_CTX) - B_INTERFACE_ENTRY(s_serialise) = toml_serialise; - B_INTERFACE_ENTRY(s_deserialise) = toml_deserialise; - B_TYPE_CLASS_INTERFACE_END(b_serial_ctx, B_TYPE_SERIAL_CTX) -B_TYPE_CLASS_DEFINITION_END(b_toml_serial_ctx) + FX_TYPE_CLASS_INTERFACE_BEGIN(fx_serial_ctx, FX_TYPE_SERIAL_CTX) + FX_INTERFACE_ENTRY(s_serialise) = toml_serialise; + FX_INTERFACE_ENTRY(s_deserialise) = toml_deserialise; + FX_TYPE_CLASS_INTERFACE_END(fx_serial_ctx, FX_TYPE_SERIAL_CTX) +FX_TYPE_CLASS_DEFINITION_END(fx_toml_serial_ctx) -B_TYPE_DEFINITION_BEGIN(b_toml_serial_ctx) - B_TYPE_ID(0xaec8dca0, 0x131a, 0x4217, 0x916b, 0xaed15756601c); - B_TYPE_CLASS(b_toml_serial_ctx_class); - B_TYPE_EXTENDS(B_TYPE_SERIAL_CTX); - B_TYPE_INSTANCE_INIT(toml_serial_ctx_init); - B_TYPE_INSTANCE_FINI(toml_serial_ctx_fini); -B_TYPE_DEFINITION_END(b_toml_serial_ctx) +FX_TYPE_DEFINITION_BEGIN(fx_toml_serial_ctx) + FX_TYPE_ID(0xaec8dca0, 0x131a, 0x4217, 0x916b, 0xaed15756601c); + FX_TYPE_CLASS(fx_toml_serial_ctx_class); + FX_TYPE_EXTENDS(FX_TYPE_SERIAL_CTX); + FX_TYPE_INSTANCE_INIT(toml_serial_ctx_init); + FX_TYPE_INSTANCE_FINI(toml_serial_ctx_fini); +FX_TYPE_DEFINITION_END(fx_toml_serial_ctx) diff --git a/term/CMakeLists.txt b/term/CMakeLists.txt index 1581e77..3981a12 100644 --- a/term/CMakeLists.txt +++ b/term/CMakeLists.txt @@ -1,3 +1,3 @@ include(../cmake/Templates.cmake) -add_bluelib_module(NAME term DEPENDENCIES core ds) +add_fx_module(NAME term DEPENDENCIES core ds) diff --git a/term/error.c b/term/error.c index 31761d7..2b0bc49 100644 --- a/term/error.c +++ b/term/error.c @@ -1,47 +1,47 @@ -#include -#include -#include +#include +#include +#include #include static void get_error_id( - const struct b_error_vendor *vendor, const struct b_error *error, + const struct fx_error_vendor *vendor, const struct fx_error *error, char *out, size_t max) { const char *vendor_name = NULL; const char *error_name = NULL; - b_error_status_code code = b_error_get_status_code(error); + fx_error_status_code code = fx_error_get_status_code(error); if (vendor) { vendor_name = vendor->v_name; - error_name = b_error_vendor_get_status_code_name(vendor, code); + error_name = fx_error_vendor_get_status_code_name(vendor, code); } - b_stringstream *id = b_stringstream_create_with_buffer(out, max); + fx_stringstream *id = fx_stringstream_create_with_buffer(out, max); if (vendor_name) { - b_stream_write_string(id, vendor_name, NULL); + fx_stream_write_string(id, vendor_name, NULL); } if (error_name) { if (vendor_name) { - b_stream_write_string(id, ".", NULL); + fx_stream_write_string(id, ".", NULL); } - b_stream_write_string(id, error_name, NULL); + fx_stream_write_string(id, error_name, NULL); } else { if (vendor_name) { - b_stream_write_string(id, "#", NULL); + fx_stream_write_string(id, "#", NULL); } - b_stream_write_fmt(id, NULL, "%ld", code); + fx_stream_write_fmt(id, NULL, "%ld", code); } } static void print_template_parameter( - const struct b_error_template_parameter *params, size_t nr_params, + const struct fx_error_template_parameter *params, size_t nr_params, const char *param_name) { - const struct b_error_template_parameter *param = NULL; + const struct fx_error_template_parameter *param = NULL; for (size_t i = 0; i < nr_params; i++) { if (!params[i].param_name) { @@ -61,59 +61,59 @@ static void print_template_parameter( const char *format = param->__param_def->param_format; switch (param->__param_def->param_type) { - case B_ERROR_TEMPLATE_PARAM_STRING: { + case FX_ERROR_TEMPLATE_PARAM_STRING: { const char *s = (const char *)param->param_value; - b_tty_printf(b_stdtty_err, 0, format, s); + fx_tty_printf(fx_stdtty_err, 0, format, s); break; } - case B_ERROR_TEMPLATE_PARAM_CHAR: { + case FX_ERROR_TEMPLATE_PARAM_CHAR: { char v = (char)param->param_value; - b_tty_printf(b_stdtty_err, 0, format, v); + fx_tty_printf(fx_stdtty_err, 0, format, v); break; } - case B_ERROR_TEMPLATE_PARAM_INT: { + case FX_ERROR_TEMPLATE_PARAM_INT: { int v = (int)param->param_value; - b_tty_printf(b_stdtty_err, 0, format, v); + fx_tty_printf(fx_stdtty_err, 0, format, v); break; } - case B_ERROR_TEMPLATE_PARAM_UINT: { + case FX_ERROR_TEMPLATE_PARAM_UINT: { unsigned int v = (unsigned int)param->param_value; - b_tty_printf(b_stdtty_err, 0, format, v); + fx_tty_printf(fx_stdtty_err, 0, format, v); break; } - case B_ERROR_TEMPLATE_PARAM_LONG: { + case FX_ERROR_TEMPLATE_PARAM_LONG: { long v = (long)param->param_value; - b_tty_printf(b_stdtty_err, 0, format, v); + fx_tty_printf(fx_stdtty_err, 0, format, v); break; } - case B_ERROR_TEMPLATE_PARAM_ULONG: { + case FX_ERROR_TEMPLATE_PARAM_ULONG: { unsigned long v = (unsigned long)param->param_value; - b_tty_printf(b_stdtty_err, 0, format, v); + fx_tty_printf(fx_stdtty_err, 0, format, v); break; } - case B_ERROR_TEMPLATE_PARAM_LONGLONG: { + case FX_ERROR_TEMPLATE_PARAM_LONGLONG: { long long v = (long long)param->param_value; - b_tty_printf(b_stdtty_err, 0, format, v); + fx_tty_printf(fx_stdtty_err, 0, format, v); break; } - case B_ERROR_TEMPLATE_PARAM_ULONGLONG: { + case FX_ERROR_TEMPLATE_PARAM_ULONGLONG: { unsigned long long v = (unsigned long long)param->param_value; - b_tty_printf(b_stdtty_err, 0, format, v); + fx_tty_printf(fx_stdtty_err, 0, format, v); break; } - case B_ERROR_TEMPLATE_PARAM_PTR: { + case FX_ERROR_TEMPLATE_PARAM_PTR: { const void *p = (const void *)param->param_value; - b_tty_printf(b_stdtty_err, 0, format, p); + fx_tty_printf(fx_stdtty_err, 0, format, p); break; } - case B_ERROR_TEMPLATE_PARAM_INTPTR: { + case FX_ERROR_TEMPLATE_PARAM_INTPTR: { intptr_t v = (intptr_t)param->param_value; - b_tty_printf(b_stdtty_err, 0, format, v); + fx_tty_printf(fx_stdtty_err, 0, format, v); break; } - case B_ERROR_TEMPLATE_PARAM_UINTPTR: { + case FX_ERROR_TEMPLATE_PARAM_UINTPTR: { uintptr_t v = (uintptr_t)param->param_value; - b_tty_printf(b_stdtty_err, 0, format, v); + fx_tty_printf(fx_stdtty_err, 0, format, v); break; } default: @@ -122,7 +122,7 @@ static void print_template_parameter( } static void print_content( - const struct b_error_template_parameter *params, size_t nr_params, + const struct fx_error_template_parameter *params, size_t nr_params, const char *s) { char modifier = 0; @@ -134,7 +134,7 @@ static void print_content( char c = s[i]; if (c != '@') { - b_tty_putc(b_stdtty_err, 0, c); + fx_tty_putc(fx_stdtty_err, 0, c); i++; continue; } @@ -151,7 +151,7 @@ static void print_content( } if (s[i] == '@') { - b_tty_putc(b_stdtty_err, 0, c); + fx_tty_putc(fx_stdtty_err, 0, c); break; } @@ -173,13 +173,13 @@ static void print_content( switch (modifier) { case 'i': - b_tty_printf(b_stdtty_err, 0, "[cyan]"); + fx_tty_printf(fx_stdtty_err, 0, "[cyan]"); break; case 'w': - b_tty_printf(b_stdtty_err, 0, "[yellow]"); + fx_tty_printf(fx_stdtty_err, 0, "[yellow]"); break; case 'e': - b_tty_printf(b_stdtty_err, 0, "[red]"); + fx_tty_printf(fx_stdtty_err, 0, "[red]"); break; default: break; @@ -197,10 +197,10 @@ static void print_content( if (template_param) { print_template_parameter(params, nr_params, buf); } else { - b_tty_printf(b_stdtty_err, 0, "%s", buf); + fx_tty_printf(fx_stdtty_err, 0, "%s", buf); } - b_tty_printf(b_stdtty_err, 0, "[reset]"); + fx_tty_printf(fx_stdtty_err, 0, "[reset]"); if (s[i] != 0) { i++; @@ -208,33 +208,33 @@ static void print_content( } } -static void print_submsg(const struct b_error *error) +static void print_submsg(const struct fx_error *error) { - const struct b_error_definition *error_def = b_error_get_definition(error); - const struct b_error_submsg *submsg = b_error_get_first_submsg(error); + const struct fx_error_definition *error_def = fx_error_get_definition(error); + const struct fx_error_submsg *submsg = fx_error_get_first_submsg(error); while (submsg) { - enum b_error_submsg_type type = b_error_submsg_get_type(submsg); - const char *content = b_error_submsg_get_content(submsg); - const struct b_error_msg *msg = b_error_submsg_get_msg(submsg); - const struct b_error_template_parameter *params - = b_error_submsg_get_template_parameters(submsg); + enum fx_error_submsg_type type = fx_error_submsg_get_type(submsg); + const char *content = fx_error_submsg_get_content(submsg); + const struct fx_error_msg *msg = fx_error_submsg_get_msg(submsg); + const struct fx_error_template_parameter *params + = fx_error_submsg_get_template_parameters(submsg); - b_tty_printf(b_stdtty_err, 0, " "); + fx_tty_printf(fx_stdtty_err, 0, " "); switch (type) { - case B_ERROR_SUBMSG_ERROR: - b_tty_printf( - b_stdtty_err, 0, "[bright_red,bold]>[reset] "); + case FX_ERROR_SUBMSG_ERROR: + fx_tty_printf( + fx_stdtty_err, 0, "[bright_red,bold]>[reset] "); break; - case B_ERROR_SUBMSG_WARNING: - b_tty_printf( - b_stdtty_err, 0, + case FX_ERROR_SUBMSG_WARNING: + fx_tty_printf( + fx_stdtty_err, 0, "[bright_yellow,bold]>[reset] "); break; - case B_ERROR_SUBMSG_INFO: - b_tty_printf( - b_stdtty_err, 0, "[bright_cyan,bold]>[reset] "); + case FX_ERROR_SUBMSG_INFO: + fx_tty_printf( + fx_stdtty_err, 0, "[bright_cyan,bold]>[reset] "); break; default: break; @@ -242,16 +242,16 @@ static void print_submsg(const struct b_error *error) if (msg) { print_content( - params, B_ERROR_TEMPLATE_PARAMETER_MAX, - b_error_msg_get_content(msg)); + params, FX_ERROR_TEMPLATE_PARAMETER_MAX, + fx_error_msg_get_content(msg)); } else if (content) { print_content( - params, B_ERROR_TEMPLATE_PARAMETER_MAX, content); + params, FX_ERROR_TEMPLATE_PARAMETER_MAX, content); } - b_tty_printf(b_stdtty_err, 0, "\n"); + fx_tty_printf(fx_stdtty_err, 0, "\n"); - submsg = b_error_get_next_submsg(error, submsg); + submsg = fx_error_get_next_submsg(error, submsg); } } @@ -274,95 +274,95 @@ const char *get_short_filepath(const char *path) return path; } -static void print_stack_trace(const struct b_error *error) +static void print_stack_trace(const struct fx_error *error) { - const struct b_error_stack_frame *frame - = b_error_get_first_stack_frame(error); + const struct fx_error_stack_frame *frame + = fx_error_get_first_stack_frame(error); while (frame) { const char *file, *func; unsigned int line; - file = b_error_stack_frame_get_filepath(frame); - line = b_error_stack_frame_get_line_number(frame); - func = b_error_stack_frame_get_function_name(frame); + file = fx_error_stack_frame_get_filepath(frame); + line = fx_error_stack_frame_get_line_number(frame); + func = fx_error_stack_frame_get_function_name(frame); file = get_short_filepath(file); - b_tty_printf( - b_stdtty_err, 0, + fx_tty_printf( + fx_stdtty_err, 0, " [dark_grey]at %s() (%s:%u)[reset]\n", func, file, line); - frame = b_error_get_next_stack_frame(error, frame); + frame = fx_error_get_next_stack_frame(error, frame); } } static void report_error( - const b_error *error, b_error_report_flags flags, bool caused_by) + const fx_error *error, fx_error_report_flags flags, bool caused_by) { - const b_error_vendor *vendor = b_error_get_vendor(error); + const fx_error_vendor *vendor = fx_error_get_vendor(error); char error_id[128]; get_error_id(vendor, error, error_id, sizeof error_id); - const struct b_error_definition *error_def = b_error_get_definition(error); - b_error_status_code code = b_error_get_status_code(error); - const char *description = b_error_get_description(error); - const struct b_error_template_parameter *params - = b_error_get_template_parameters(error); + const struct fx_error_definition *error_def = fx_error_get_definition(error); + fx_error_status_code code = fx_error_get_status_code(error); + const char *description = fx_error_get_description(error); + const struct fx_error_template_parameter *params + = fx_error_get_template_parameters(error); if (!description && vendor) { - description = b_error_vendor_get_status_code_description( + description = fx_error_vendor_get_status_code_description( vendor, code); } if (caused_by) { - b_tty_printf( - b_stdtty_err, 0, + fx_tty_printf( + fx_stdtty_err, 0, " [green]->[reset] caused by [bright_red,bold]ERROR "); } else { - b_tty_printf(b_stdtty_err, 0, "[bright_red,bold]==> ERROR "); + fx_tty_printf(fx_stdtty_err, 0, "[bright_red,bold]==> ERROR "); } - if (flags & B_ERROR_REPORT_STATUS) { - b_tty_printf(b_stdtty_err, 0, "%s", error_id); + if (flags & FX_ERROR_REPORT_STATUS) { + fx_tty_printf(fx_stdtty_err, 0, "%s", error_id); } - b_tty_printf(b_stdtty_err, 0, "[reset]"); + fx_tty_printf(fx_stdtty_err, 0, "[reset]"); - if (flags & B_ERROR_REPORT_DESCRIPTION) { - const struct b_error_msg *msg = b_error_get_msg(error); + if (flags & FX_ERROR_REPORT_DESCRIPTION) { + const struct fx_error_msg *msg = fx_error_get_msg(error); if (msg) { - b_tty_printf(b_stdtty_err, 0, ": "); + fx_tty_printf(fx_stdtty_err, 0, ": "); print_content( - params, B_ERROR_TEMPLATE_PARAMETER_MAX, - b_error_msg_get_content(msg)); + params, FX_ERROR_TEMPLATE_PARAMETER_MAX, + fx_error_msg_get_content(msg)); } else if (description) { - b_tty_printf(b_stdtty_err, 0, ": "); + fx_tty_printf(fx_stdtty_err, 0, ": "); print_content( - params, B_ERROR_TEMPLATE_PARAMETER_MAX, + params, FX_ERROR_TEMPLATE_PARAMETER_MAX, description); } } - b_tty_printf(b_stdtty_err, 0, "\n"); + fx_tty_printf(fx_stdtty_err, 0, "\n"); - if (flags & B_ERROR_REPORT_SUBMSG) { + if (flags & FX_ERROR_REPORT_SUBMSG) { print_submsg(error); } - if (flags & B_ERROR_REPORT_STACK_TRACE) { + if (flags & FX_ERROR_REPORT_STACK_TRACE) { print_stack_trace(error); } - const struct b_error *cause = b_error_get_caused_by(error); - if (cause && (flags & B_ERROR_REPORT_CAUSE)) { + const struct fx_error *cause = fx_error_get_caused_by(error); + if (cause && (flags & FX_ERROR_REPORT_CAUSE)) { report_error(cause, flags, true); } } -void b_enhanced_error_reporter( - const struct b_error *error, b_error_report_flags flags) +void fx_enhanced_error_reporter( + const struct fx_error *error, fx_error_report_flags flags) { report_error(error, flags, false); } diff --git a/term/include/blue/term.h b/term/include/blue/term.h deleted file mode 100644 index 595452c..0000000 --- a/term/include/blue/term.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef BLUELIB_TERM_H_ -#define BLUELIB_TERM_H_ - -#include -#include - -#include -#include - -#endif diff --git a/term/include/blue/term/print.h b/term/include/blue/term/print.h deleted file mode 100644 index 8eda77c..0000000 --- a/term/include/blue/term/print.h +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef BLUELIB_TERM_PRINT_H_ -#define BLUELIB_TERM_PRINT_H_ - -#include -#include -#include -#include - -#define b_i(...) b_print(B_PRINT_I, __VA_ARGS__) -#define b_warn(...) b_print(B_PRINT_WARN, __VA_ARGS__) -#define b_err(...) b_print(B_PRINT_ERR, __VA_ARGS__) - -struct b_tty; - -struct b_error; -struct b_error_vendor; -enum b_error_report_flags; - -typedef enum b_paragraph_format_flags { - B_PARAGRAPH_DONT_INDENT_FIRST_LINE = 0x01u, - B_PARAGRAPH_HYPHENATE = 0x02u, - B_PARAGRAPH_DOUBLE_LINE_BREAK = 0x04u, - B_PARAGRAPH_MULTI_LINE_BREAK = 0x08u, -} b_paragraph_format_flags; - -typedef struct b_paragraph_format { - b_paragraph_format_flags p_flags; - - unsigned int p_left_margin; - unsigned int p_right_margin; - unsigned int p_line_length; -} b_paragraph_format; - -typedef enum b_print_format { - B_PRINT_NORMAL = 0, - B_PRINT_I, - B_PRINT_WARN, - B_PRINT_ERR, -} b_print_format; - -BLUE_API b_status b_print(b_print_format format, const char *str, ...); -BLUE_API b_status b_print_paragraph( - const char *str, struct b_tty *tty, b_paragraph_format *format); - -BLUE_API int b_putc(char c); -BLUE_API int b_puts(const char *s); -BLUE_API int b_printf(const char *format, ...); - -BLUE_API void b_enhanced_error_reporter( - const struct b_error *, enum b_error_report_flags flags); - -#endif diff --git a/term/include/blue/term/tty.h b/term/include/blue/term/tty.h deleted file mode 100644 index a07cac9..0000000 --- a/term/include/blue/term/tty.h +++ /dev/null @@ -1,180 +0,0 @@ -#ifndef BLUELIB_TERM_TTY_H_ -#define BLUELIB_TERM_TTY_H_ - -#include -#include -#include -#include -#include - -#define b_stdtty (z__b_tty_get_std()) -#define b_stdtty_err (z__b_tty_get_err()) - -#define B_TTY_CTRL_KEY(c) ((c) | B_MOD_CTRL) - -#define B_MAKE_VMODE(fg, bg, a) \ - { \ - .v_fg = fg, .v_bg = bg, .v_attrib = (a) \ - } - -#define B_MAKE_COLOUR_DEFAULT(v) \ - { \ - .c_mode = TTY_COLOUR_NONE, \ - } - -#define B_MAKE_COLOUR_16(v) \ - { \ - .c_mode = TTY_COLOUR_16, .c_16 = {.value = (v) } \ - } - -#define B_MAKE_COLOUR_256(v) \ - { \ - .c_mode = TTY_COLOUR_256, .c_256 = {.value = (v) } \ - } - -#define B_MAKE_COLOUR_TRUE(cr, cg, cb) \ - { \ - .c_mode = TTY_COLOUR_TRUE, .c_true \ - = {.r = (cr), \ - .g = (cg), \ - .b = (cb) } \ - } - -typedef struct b_tty b_tty; -typedef uint32_t b_keycode; - -/* codepoints U+F0000 to U+FFFFD are reserved areas in Unicode, free for - * application use. store special keycodes here */ -enum { - B_KEY_ARROW_LEFT = 0xF0000, - B_KEY_ARROW_RIGHT, - B_KEY_ARROW_UP, - B_KEY_ARROW_DOWN, - B_KEY_BACKSPACE, - B_KEY_RETURN, - - B_MOD_CTRL = 0x10000000, - - B_KEY_EOF = 0xFFFFFFFF, -}; - -typedef enum b_tty_colour16 { - B_TTY_COLOUR16_BLACK = 0, - B_TTY_COLOUR16_RED, - B_TTY_COLOUR16_GREEN, - B_TTY_COLOUR16_YELLOW, - B_TTY_COLOUR16_BLUE, - B_TTY_COLOUR16_MAGENTA, - B_TTY_COLOUR16_CYAN, - B_TTY_COLOUR16_WHITE, - B_TTY_COLOUR16_BRIGHT_BLACK, - B_TTY_COLOUR16_BRIGHT_RED, - B_TTY_COLOUR16_BRIGHT_GREEN, - B_TTY_COLOUR16_BRIGHT_YELLOW, - B_TTY_COLOUR16_BRIGHT_BLUE, - B_TTY_COLOUR16_BRIGHT_MAGENTA, - B_TTY_COLOUR16_BRIGHT_CYAN, - B_TTY_COLOUR16_BRIGHT_WHITE, -} b_tty_colour16; - -typedef enum b_tty_colour_mode { - B_TTY_COLOUR_NONE = 0, - B_TTY_COLOUR_16, - B_TTY_COLOUR_256, - B_TTY_COLOUR_TRUE, -} b_tty_colour_mode; - -typedef enum b_tty_position_base { - B_TTY_POS_START, - B_TTY_POS_CURSOR, -} b_tty_position_base; - -typedef enum b_tty_attrib { - B_TTY_ATTRIB_NORMAL = 0x00u, - B_TTY_ATTRIB_BOLD = 0x01u, - B_TTY_ATTRIB_UNDERLINE = 0x02u, - B_TTY_ATTRIB_ITALIC = 0x04u, - B_TTY_ATTRIB_INVERT = 0x08u, -} b_tty_attrib; - -typedef enum b_tty_clear_mode { - B_TTY_CLEAR_LINE = 0x01u, - B_TTY_CLEAR_SCREEN = 0x02, - - B_TTY_CLEAR_TO_CURSOR = 0x0100u, - B_TTY_CLEAR_FROM_CURSOR = 0x0200u, - B_TTY_CLEAR_ALL = 0x0400u, -} b_tty_clear_mode; - -typedef enum b_tty_mode { - B_TTY_CANONICAL = 0x00u, - B_TTY_RAW = 0x01u, -} b_tty_mode; - -typedef enum b_tty_print_flags { - B_TTY_DISABLE_FORMATTING = 0x01u, - B_TTY_DISABLE_INTERPOLATED_FORMATTING = 0x02u, -} b_tty_print_flags; - -typedef struct b_tty_colour { - enum b_tty_colour_mode c_mode; - - union { - struct { - uint8_t value; - } c_16; - - struct { - uint8_t value; - } c_256; - - struct { - uint8_t r; - uint8_t g; - uint8_t b; - } c_true; - }; -} b_tty_colour; - -typedef struct b_tty_vmode { - enum b_tty_attrib v_attrib; - struct b_tty_colour v_fg; - struct b_tty_colour v_bg; -} b_tty_vmode; - -BLUE_API b_tty *z__b_tty_get_std(void); -BLUE_API b_tty *z__b_tty_get_err(void); - -static inline unsigned int b_keycode_get_key(b_keycode v) -{ - return v & 0x0FFFFFFF; -} - -BLUE_API bool b_tty_is_interactive(const struct b_tty *tty); -BLUE_API void b_tty_set_mode(struct b_tty *tty, enum b_tty_mode mode); -BLUE_API void b_tty_set_vmode(struct b_tty *tty, const struct b_tty_vmode *vmode); -BLUE_API void b_tty_reset_vmode(struct b_tty *tty); - -BLUE_API enum b_status b_tty_get_dimensions( - struct b_tty *tty, unsigned int *w, unsigned int *h); -BLUE_API enum b_status b_tty_get_cursor_position( - struct b_tty *tty, unsigned int *x, unsigned int *y); - -BLUE_API b_keycode b_tty_read_key(struct b_tty *tty); - -BLUE_API void b_tty_move_cursor_x( - struct b_tty *tty, enum b_tty_position_base base, int pos); -BLUE_API void b_tty_move_cursor_y( - struct b_tty *tty, enum b_tty_position_base base, int pos); -BLUE_API void b_tty_clear(struct b_tty *tty, enum b_tty_clear_mode mode); - -BLUE_API int b_tty_putc(struct b_tty *tty, enum b_tty_print_flags flags, char c); -BLUE_API int b_tty_puts( - struct b_tty *tty, enum b_tty_print_flags flags, const char *s); -BLUE_API int b_tty_printf( - struct b_tty *tty, enum b_tty_print_flags flags, const char *s, ...); -BLUE_API int b_tty_vprintf( - struct b_tty *tty, enum b_tty_print_flags flags, const char *s, - va_list args); - -#endif diff --git a/term/include/fx/term.h b/term/include/fx/term.h new file mode 100644 index 0000000..bed1c86 --- /dev/null +++ b/term/include/fx/term.h @@ -0,0 +1,10 @@ +#ifndef FX_TERM_H_ +#define FX_TERM_H_ + +#include +#include + +#include +#include + +#endif diff --git a/term/include/fx/term/print.h b/term/include/fx/term/print.h new file mode 100644 index 0000000..07ad418 --- /dev/null +++ b/term/include/fx/term/print.h @@ -0,0 +1,52 @@ +#ifndef FX_TERM_PRINT_H_ +#define FX_TERM_PRINT_H_ + +#include +#include +#include +#include + +#define fx_i(...) fx_print(FX_PRINT_I, __VA_ARGS__) +#define fx_warn(...) fx_print(FX_PRINT_WARN, __VA_ARGS__) +#define fx_err(...) fx_print(FX_PRINT_ERR, __VA_ARGS__) + +struct fx_tty; + +struct fx_error; +struct fx_error_vendor; +enum fx_error_report_flags; + +typedef enum fx_paragraph_format_flags { + FX_PARAGRAPH_DONT_INDENT_FIRST_LINE = 0x01u, + FX_PARAGRAPH_HYPHENATE = 0x02u, + FX_PARAGRAPH_DOUBLE_LINE_BREAK = 0x04u, + FX_PARAGRAPH_MULTI_LINE_BREAK = 0x08u, +} fx_paragraph_format_flags; + +typedef struct fx_paragraph_format { + fx_paragraph_format_flags p_flags; + + unsigned int p_left_margin; + unsigned int p_right_margin; + unsigned int p_line_length; +} fx_paragraph_format; + +typedef enum fx_print_format { + FX_PRINT_NORMAL = 0, + FX_PRINT_I, + FX_PRINT_WARN, + FX_PRINT_ERR, +} fx_print_format; + +FX_API fx_status fx_print(fx_print_format format, const char *str, ...); +FX_API fx_status fx_print_paragraph( + const char *str, struct fx_tty *tty, fx_paragraph_format *format); + +FX_API int fx_putc(char c); +FX_API int fx_puts(const char *s); +FX_API int fx_printf(const char *format, ...); + +FX_API void fx_enhanced_error_reporter( + const struct fx_error *, enum fx_error_report_flags flags); + +#endif diff --git a/term/include/fx/term/tty.h b/term/include/fx/term/tty.h new file mode 100644 index 0000000..c382138 --- /dev/null +++ b/term/include/fx/term/tty.h @@ -0,0 +1,180 @@ +#ifndef FX_TERM_TTY_H_ +#define FX_TERM_TTY_H_ + +#include +#include +#include +#include +#include + +#define fx_stdtty (z__fx_tty_get_std()) +#define fx_stdtty_err (z__fx_tty_get_err()) + +#define FX_TTY_CTRL_KEY(c) ((c) | FX_MOD_CTRL) + +#define FX_MAKE_VMODE(fg, bg, a) \ + { \ + .v_fg = fg, .v_bg = bg, .v_attrib = (a) \ + } + +#define FX_MAKE_COLOUR_DEFAULT(v) \ + { \ + .c_mode = TTY_COLOUR_NONE, \ + } + +#define FX_MAKE_COLOUR_16(v) \ + { \ + .c_mode = TTY_COLOUR_16, .c_16 = {.value = (v) } \ + } + +#define FX_MAKE_COLOUR_256(v) \ + { \ + .c_mode = TTY_COLOUR_256, .c_256 = {.value = (v) } \ + } + +#define FX_MAKE_COLOUR_TRUE(cr, cg, cb) \ + { \ + .c_mode = TTY_COLOUR_TRUE, .c_true \ + = {.r = (cr), \ + .g = (cg), \ + .b = (cb) } \ + } + +typedef struct fx_tty fx_tty; +typedef uint32_t fx_keycode; + +/* codepoints U+F0000 to U+FFFFD are reserved areas in Unicode, free for + * application use. store special keycodes here */ +enum { + FX_KEY_ARROW_LEFT = 0xF0000, + FX_KEY_ARROW_RIGHT, + FX_KEY_ARROW_UP, + FX_KEY_ARROW_DOWN, + FX_KEY_BACKSPACE, + FX_KEY_RETURN, + + FX_MOD_CTRL = 0x10000000, + + FX_KEY_EOF = 0xFFFFFFFF, +}; + +typedef enum fx_tty_colour16 { + FX_TTY_COLOUR16_BLACK = 0, + FX_TTY_COLOUR16_RED, + FX_TTY_COLOUR16_GREEN, + FX_TTY_COLOUR16_YELLOW, + FX_TTY_COLOUR16_FX, + FX_TTY_COLOUR16_MAGENTA, + FX_TTY_COLOUR16_CYAN, + FX_TTY_COLOUR16_WHITE, + FX_TTY_COLOUR16_BRIGHT_BLACK, + FX_TTY_COLOUR16_BRIGHT_RED, + FX_TTY_COLOUR16_BRIGHT_GREEN, + FX_TTY_COLOUR16_BRIGHT_YELLOW, + FX_TTY_COLOUR16_BRIGHT_FX, + FX_TTY_COLOUR16_BRIGHT_MAGENTA, + FX_TTY_COLOUR16_BRIGHT_CYAN, + FX_TTY_COLOUR16_BRIGHT_WHITE, +} fx_tty_colour16; + +typedef enum fx_tty_colour_mode { + FX_TTY_COLOUR_NONE = 0, + FX_TTY_COLOUR_16, + FX_TTY_COLOUR_256, + FX_TTY_COLOUR_TRUE, +} fx_tty_colour_mode; + +typedef enum fx_tty_position_base { + FX_TTY_POS_START, + FX_TTY_POS_CURSOR, +} fx_tty_position_base; + +typedef enum fx_tty_attrib { + FX_TTY_ATTRIB_NORMAL = 0x00u, + FX_TTY_ATTRIB_BOLD = 0x01u, + FX_TTY_ATTRIB_UNDERLINE = 0x02u, + FX_TTY_ATTRIB_ITALIC = 0x04u, + FX_TTY_ATTRIB_INVERT = 0x08u, +} fx_tty_attrib; + +typedef enum fx_tty_clear_mode { + FX_TTY_CLEAR_LINE = 0x01u, + FX_TTY_CLEAR_SCREEN = 0x02, + + FX_TTY_CLEAR_TO_CURSOR = 0x0100u, + FX_TTY_CLEAR_FROM_CURSOR = 0x0200u, + FX_TTY_CLEAR_ALL = 0x0400u, +} fx_tty_clear_mode; + +typedef enum fx_tty_mode { + FX_TTY_CANONICAL = 0x00u, + FX_TTY_RAW = 0x01u, +} fx_tty_mode; + +typedef enum fx_tty_print_flags { + FX_TTY_DISABLE_FORMATTING = 0x01u, + FX_TTY_DISABLE_INTERPOLATED_FORMATTING = 0x02u, +} fx_tty_print_flags; + +typedef struct fx_tty_colour { + enum fx_tty_colour_mode c_mode; + + union { + struct { + uint8_t value; + } c_16; + + struct { + uint8_t value; + } c_256; + + struct { + uint8_t r; + uint8_t g; + uint8_t b; + } c_true; + }; +} fx_tty_colour; + +typedef struct fx_tty_vmode { + enum fx_tty_attrib v_attrib; + struct fx_tty_colour v_fg; + struct fx_tty_colour v_bg; +} fx_tty_vmode; + +FX_API fx_tty *z__fx_tty_get_std(void); +FX_API fx_tty *z__fx_tty_get_err(void); + +static inline unsigned int fx_keycode_get_key(fx_keycode v) +{ + return v & 0x0FFFFFFF; +} + +FX_API bool fx_tty_is_interactive(const struct fx_tty *tty); +FX_API void fx_tty_set_mode(struct fx_tty *tty, enum fx_tty_mode mode); +FX_API void fx_tty_set_vmode(struct fx_tty *tty, const struct fx_tty_vmode *vmode); +FX_API void fx_tty_reset_vmode(struct fx_tty *tty); + +FX_API enum fx_status fx_tty_get_dimensions( + struct fx_tty *tty, unsigned int *w, unsigned int *h); +FX_API enum fx_status fx_tty_get_cursor_position( + struct fx_tty *tty, unsigned int *x, unsigned int *y); + +FX_API fx_keycode fx_tty_read_key(struct fx_tty *tty); + +FX_API void fx_tty_move_cursor_x( + struct fx_tty *tty, enum fx_tty_position_base base, int pos); +FX_API void fx_tty_move_cursor_y( + struct fx_tty *tty, enum fx_tty_position_base base, int pos); +FX_API void fx_tty_clear(struct fx_tty *tty, enum fx_tty_clear_mode mode); + +FX_API int fx_tty_putc(struct fx_tty *tty, enum fx_tty_print_flags flags, char c); +FX_API int fx_tty_puts( + struct fx_tty *tty, enum fx_tty_print_flags flags, const char *s); +FX_API int fx_tty_printf( + struct fx_tty *tty, enum fx_tty_print_flags flags, const char *s, ...); +FX_API int fx_tty_vprintf( + struct fx_tty *tty, enum fx_tty_print_flags flags, const char *s, + va_list args); + +#endif diff --git a/term/paragraph.c b/term/paragraph.c index b83745c..bab62b2 100644 --- a/term/paragraph.c +++ b/term/paragraph.c @@ -1,5 +1,5 @@ -#include -#include +#include +#include #include #include #include @@ -9,18 +9,18 @@ #define DEFAULT_PARAGRAPH_WIDTH 160 static void indent( - struct b_paragraph_format *format, struct b_tty *tty, unsigned int margin) + struct fx_paragraph_format *format, struct fx_tty *tty, unsigned int margin) { unsigned int x = 0; while (x < margin) { - b_tty_puts(tty, 0, " "); + fx_tty_puts(tty, 0, " "); x++; } } static unsigned int extract_line( - const char **sp, unsigned int line_length, b_string *out, - struct b_paragraph_format *format) + const char **sp, unsigned int line_length, fx_string *out, + struct fx_paragraph_format *format) { const char *start = *sp; while (isspace(*start) || *start == '\n') { @@ -87,18 +87,18 @@ static unsigned int extract_line( len = strlen(start); } - b_string_insert_cstrn(out, start, len, B_NPOS); + fx_string_insert_cstrn(out, start, len, FX_NPOS); *sp = end; return len; } -static b_status print_paragraph_tty( - const char *str, struct b_tty *tty, struct b_paragraph_format *format) +static fx_status print_paragraph_tty( + const char *str, struct fx_tty *tty, struct fx_paragraph_format *format) { unsigned int w = 0, h = 0; - b_tty_get_dimensions(tty, &w, &h); + fx_tty_get_dimensions(tty, &w, &h); if (!w) { w = DEFAULT_PARAGRAPH_WIDTH; @@ -108,7 +108,7 @@ static b_status print_paragraph_tty( unsigned int line_length = format->p_line_length; if (format->p_left_margin + format->p_right_margin >= w) { - return B_SUCCESS; + return FX_SUCCESS; } unsigned int page_width @@ -117,32 +117,32 @@ static b_status print_paragraph_tty( line_length = page_width; } - if (!(format->p_flags & B_PARAGRAPH_DONT_INDENT_FIRST_LINE)) { + if (!(format->p_flags & FX_PARAGRAPH_DONT_INDENT_FIRST_LINE)) { indent(format, tty, left_margin); } - b_string *line = b_string_create(); + fx_string *line = fx_string_create(); bool need_indent = false; while (str) { if (*str == '\n') { - if (format->p_flags & B_PARAGRAPH_DOUBLE_LINE_BREAK) { - b_tty_putc(tty, 0, '\n'); + if (format->p_flags & FX_PARAGRAPH_DOUBLE_LINE_BREAK) { + fx_tty_putc(tty, 0, '\n'); } str++; need_indent = true; while (*str == '\n') { - if (format->p_flags & B_PARAGRAPH_MULTI_LINE_BREAK) { - b_tty_putc(tty, 0, '\n'); + if (format->p_flags & FX_PARAGRAPH_MULTI_LINE_BREAK) { + fx_tty_putc(tty, 0, '\n'); } str++; } } - b_string_clear(line); + fx_string_clear(line); unsigned int this_line = extract_line(&str, line_length, line, format); if (this_line == 0) { @@ -153,24 +153,24 @@ static b_status print_paragraph_tty( indent(format, tty, left_margin); } - b_tty_puts(tty, 0, b_string_ptr(line)); - b_tty_putc(tty, 0, '\n'); + fx_tty_puts(tty, 0, fx_string_ptr(line)); + fx_tty_putc(tty, 0, '\n'); need_indent = true; } - b_string_unref(line); + fx_string_unref(line); - return B_SUCCESS; + return FX_SUCCESS; } -static b_status print_paragraph_file( - const char *str, FILE *fp, struct b_paragraph_format *format) +static fx_status print_paragraph_file( + const char *str, FILE *fp, struct fx_paragraph_format *format) { - return B_SUCCESS; + return FX_SUCCESS; } -b_status b_print_paragraph( - const char *str, struct b_tty *fp, struct b_paragraph_format *format) +fx_status fx_print_paragraph( + const char *str, struct fx_tty *fp, struct fx_paragraph_format *format) { return print_paragraph_tty(str, fp, format); } diff --git a/term/print.c b/term/print.c index d9094f6..eb4a7b6 100644 --- a/term/print.c +++ b/term/print.c @@ -1,7 +1,7 @@ #include "print.h" -#include -#include +#include +#include #include #include #include @@ -25,7 +25,7 @@ #define F_RESET "[reset]" -typedef b_status (*print_function)(struct b_tty *tty, const char *s); +typedef fx_status (*print_function)(struct fx_tty *tty, const char *s); static int print_no_ansi(FILE *fp, const char *str) { @@ -46,66 +46,66 @@ static int print_no_ansi(FILE *fp, const char *str) return out; } -static b_status print_normal(struct b_tty *tty, const char *s) +static fx_status print_normal(struct fx_tty *tty, const char *s) { - return B_SUCCESS; + return FX_SUCCESS; } -static b_status print_i(struct b_tty *tty, const char *s) +static fx_status print_i(struct fx_tty *tty, const char *s) { - struct b_paragraph_format format = {0}; + struct fx_paragraph_format format = {0}; format.p_left_margin = 5; format.p_right_margin = 5; - format.p_flags = B_PARAGRAPH_DONT_INDENT_FIRST_LINE - | B_PARAGRAPH_MULTI_LINE_BREAK; + format.p_flags = FX_PARAGRAPH_DONT_INDENT_FIRST_LINE + | FX_PARAGRAPH_MULTI_LINE_BREAK; // printf(F_CYN_BG F_BLACK " i " F_RESET " "); - b_tty_puts(tty, 0, F_CYN_BOLD " i:" F_RESET " "); - b_print_paragraph(s, tty, &format); + fx_tty_puts(tty, 0, F_CYN_BOLD " i:" F_RESET " "); + fx_print_paragraph(s, tty, &format); - return B_SUCCESS; + return FX_SUCCESS; } -static b_status print_warn(struct b_tty *tty, const char *s) +static fx_status print_warn(struct fx_tty *tty, const char *s) { - struct b_paragraph_format format = {0}; + struct fx_paragraph_format format = {0}; format.p_left_margin = 5; format.p_right_margin = 5; - format.p_flags = B_PARAGRAPH_DONT_INDENT_FIRST_LINE - | B_PARAGRAPH_MULTI_LINE_BREAK; + format.p_flags = FX_PARAGRAPH_DONT_INDENT_FIRST_LINE + | FX_PARAGRAPH_MULTI_LINE_BREAK; // printf(F_YEL_BG F_BLACK " Warn " F_RESET " "); - b_tty_puts(tty, 0, F_YEL_BOLD " !!" F_RESET " "); - b_print_paragraph(s, tty, &format); + fx_tty_puts(tty, 0, F_YEL_BOLD " !!" F_RESET " "); + fx_print_paragraph(s, tty, &format); - return B_SUCCESS; + return FX_SUCCESS; } -static b_status print_err(struct b_tty *tty, const char *s) +static fx_status print_err(struct fx_tty *tty, const char *s) { - struct b_paragraph_format format = {0}; + struct fx_paragraph_format format = {0}; format.p_left_margin = 5; format.p_right_margin = 5; - format.p_flags = B_PARAGRAPH_DONT_INDENT_FIRST_LINE - | B_PARAGRAPH_MULTI_LINE_BREAK; + format.p_flags = FX_PARAGRAPH_DONT_INDENT_FIRST_LINE + | FX_PARAGRAPH_MULTI_LINE_BREAK; // printf(F_RED_BG F_BLACK " Err " F_RESET " "); - b_tty_puts(tty, 0, F_RED_BOLD "Err:" F_RESET " "); - b_print_paragraph(s, tty, &format); + fx_tty_puts(tty, 0, F_RED_BOLD "Err:" F_RESET " "); + fx_print_paragraph(s, tty, &format); - return B_SUCCESS; + return FX_SUCCESS; } static print_function format_printers[] = { - [B_PRINT_NORMAL] = print_normal, - [B_PRINT_I] = print_i, - [B_PRINT_WARN] = print_warn, - [B_PRINT_ERR] = print_err, + [FX_PRINT_NORMAL] = print_normal, + [FX_PRINT_I] = print_i, + [FX_PRINT_WARN] = print_warn, + [FX_PRINT_ERR] = print_err, }; static size_t nr_format_printers - = sizeof format_printers / sizeof format_printers[B_PRINT_NORMAL]; + = sizeof format_printers / sizeof format_printers[FX_PRINT_NORMAL]; -b_status b_print(b_print_format format, const char *str, ...) +fx_status fx_print(fx_print_format format, const char *str, ...) { char buf[1024]; @@ -115,33 +115,33 @@ b_status b_print(b_print_format format, const char *str, ...) va_end(arg); if (format >= nr_format_printers) { - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } print_function printer = format_printers[format]; if (!printer) { - return B_ERR_INVALID_ARGUMENT; + return FX_ERR_INVALID_ARGUMENT; } - return printer(b_stdtty, buf); + return printer(fx_stdtty, buf); } -int b_putc(char c) +int fx_putc(char c) { - return b_tty_putc(b_stdtty, 0, c); + return fx_tty_putc(fx_stdtty, 0, c); } -int b_puts(const char* s) +int fx_puts(const char* s) { - return b_tty_puts(b_stdtty, 0, s); + return fx_tty_puts(fx_stdtty, 0, s); } -int b_printf(const char* format, ...) +int fx_printf(const char* format, ...) { va_list arg; va_start(arg, format); - int x = b_tty_vprintf( - b_stdtty, B_TTY_DISABLE_INTERPOLATED_FORMATTING, format, arg); + int x = fx_tty_vprintf( + fx_stdtty, FX_TTY_DISABLE_INTERPOLATED_FORMATTING, format, arg); va_end(arg); return x; } \ No newline at end of file diff --git a/term/print.h b/term/print.h index a86d6b9..c02de62 100644 --- a/term/print.h +++ b/term/print.h @@ -1,53 +1,53 @@ -#ifndef _BLUELIB_PRINT_H_ -#define _BLUELIB_PRINT_H_ +#ifndef _FX_PRINT_H_ +#define _FX_PRINT_H_ -#include -#include +#include +#include #include -enum z__b_stream_modifier { - Z__B_STREAM_MOD_BLACK = 0x1, - Z__B_STREAM_MOD_RED = 0x2, - Z__B_STREAM_MOD_GREEN = 0x4, - Z__B_STREAM_MOD_BLUE = 0x8, - Z__B_STREAM_MOD_BRIGHT = 0x10, +enum z__fx_stream_modifier { + Z__FX_STREAM_MOD_BLACK = 0x1, + Z__FX_STREAM_MOD_RED = 0x2, + Z__FX_STREAM_MOD_GREEN = 0x4, + Z__FX_STREAM_MOD_FX = 0x8, + Z__FX_STREAM_MOD_BRIGHT = 0x10, - Z__B_STREAM_MOD_BG_BLACK = 0x20, - Z__B_STREAM_MOD_BG_RED = 0x40, - Z__B_STREAM_MOD_BG_GREEN = 0x80, - Z__B_STREAM_MOD_BG_BLUE = 0x100, - Z__B_STREAM_MOD_BG_BRIGHT = 0x200, + Z__FX_STREAM_MOD_BG_BLACK = 0x20, + Z__FX_STREAM_MOD_BG_RED = 0x40, + Z__FX_STREAM_MOD_BG_GREEN = 0x80, + Z__FX_STREAM_MOD_BG_FX = 0x100, + Z__FX_STREAM_MOD_BG_BRIGHT = 0x200, - Z__B_STREAM_MOD_BOLD = 0x400, - Z__B_STREAM_MOD_ITALIC = 0x800, - Z__B_STREAM_MOD_ULINE = 0x1000, - Z__B_STREAM_MOD_INVERT = 0x2000, + Z__FX_STREAM_MOD_BOLD = 0x400, + Z__FX_STREAM_MOD_ITALIC = 0x800, + Z__FX_STREAM_MOD_ULINE = 0x1000, + Z__FX_STREAM_MOD_INVERT = 0x2000, - Z__B_STREAM_MOD_RESET = 0x4000, + Z__FX_STREAM_MOD_RESET = 0x4000, }; -#define Z__B_STREAM_MOD_GET_FG_COLOUR(x) \ +#define Z__FX_STREAM_MOD_GET_FG_COLOUR(x) \ ((x) \ - & (Z__B_STREAM_MOD_BLACK | Z__B_STREAM_MOD_RED \ - | Z__B_STREAM_MOD_GREEN | Z__B_STREAM_MOD_BLUE)) -#define Z__B_STREAM_MOD_CLEAR_FG_COLOUR(x) \ + & (Z__FX_STREAM_MOD_BLACK | Z__FX_STREAM_MOD_RED \ + | Z__FX_STREAM_MOD_GREEN | Z__FX_STREAM_MOD_FX)) +#define Z__FX_STREAM_MOD_CLEAR_FG_COLOUR(x) \ ((x) \ - & ~(Z__B_STREAM_MOD_BLACK | Z__B_STREAM_MOD_RED \ - | Z__B_STREAM_MOD_GREEN | Z__B_STREAM_MOD_BLUE)) + & ~(Z__FX_STREAM_MOD_BLACK | Z__FX_STREAM_MOD_RED \ + | Z__FX_STREAM_MOD_GREEN | Z__FX_STREAM_MOD_FX)) -#define Z__B_STREAM_MOD_GET_BG_COLOUR(x) \ +#define Z__FX_STREAM_MOD_GET_BG_COLOUR(x) \ ((x) \ - & (Z__B_STREAM_MOD_BG_BLACK | Z__B_STREAM_MOD_BG_RED \ - | Z__B_STREAM_MOD_BG_GREEN | Z__B_STREAM_MOD_BG_BLUE)) -#define Z__B_STREAM_MOD_CLEAR_BG_COLOUR(x) \ + & (Z__FX_STREAM_MOD_BG_BLACK | Z__FX_STREAM_MOD_BG_RED \ + | Z__FX_STREAM_MOD_BG_GREEN | Z__FX_STREAM_MOD_BG_FX)) +#define Z__FX_STREAM_MOD_CLEAR_BG_COLOUR(x) \ ((x) \ - & ~(Z__B_STREAM_MOD_BG_BLACK | Z__B_STREAM_MOD_BG_RED \ - | Z__B_STREAM_MOD_BG_GREEN | Z__B_STREAM_MOD_BG_BLUE)) + & ~(Z__FX_STREAM_MOD_BG_BLACK | Z__FX_STREAM_MOD_BG_RED \ + | Z__FX_STREAM_MOD_BG_GREEN | Z__FX_STREAM_MOD_BG_FX)) -BLUE_API int z__b_stream_is_tty(FILE *fp); -BLUE_API int z__b_stream_dimensions(FILE *fp, unsigned int *w, unsigned int *h); -BLUE_API int z__b_stream_cursorpos( +FX_API int z__fx_stream_is_tty(FILE *fp); +FX_API int z__fx_stream_dimensions(FILE *fp, unsigned int *w, unsigned int *h); +FX_API int z__fx_stream_cursorpos( FILE *in, FILE *out, unsigned int *x, unsigned int *y); -BLUE_API int z__b_stream_set_modifier(FILE *fp, unsigned int mod); +FX_API int z__fx_stream_set_modifier(FILE *fp, unsigned int mod); #endif diff --git a/term/printf.c b/term/printf.c index dc18b40..786dc1e 100644 --- a/term/printf.c +++ b/term/printf.c @@ -32,7 +32,7 @@ #include "printf.h" -#include +#include #include #include @@ -116,8 +116,8 @@ #endif struct out_tty_args { - struct b_tty *tty; - enum b_tty_print_flags flags; + struct fx_tty *tty; + enum fx_tty_print_flags flags; bool format_ch; }; @@ -959,17 +959,17 @@ static void out_tty(char c, struct out_tty_args *args) return; } - enum b_tty_print_flags flags = args->flags; + enum fx_tty_print_flags flags = args->flags; - if (!args->format_ch && (flags & B_TTY_DISABLE_INTERPOLATED_FORMATTING)) { - flags |= B_TTY_DISABLE_FORMATTING; + if (!args->format_ch && (flags & FX_TTY_DISABLE_INTERPOLATED_FORMATTING)) { + flags |= FX_TTY_DISABLE_FORMATTING; } - b_tty_putc(args->tty, flags, c); + fx_tty_putc(args->tty, flags, c); } -int b_tty_vprintf( - struct b_tty *tty, enum b_tty_print_flags flags, const char *format, +int fx_tty_vprintf( + struct fx_tty *tty, enum fx_tty_print_flags flags, const char *format, va_list args) { struct out_tty_args tty_args = { diff --git a/term/sys/darwin/print.c b/term/sys/darwin/print.c index 21bd630..3362d4f 100644 --- a/term/sys/darwin/print.c +++ b/term/sys/darwin/print.c @@ -6,12 +6,12 @@ #include #include -int z__b_stream_is_tty(FILE *fp) +int z__fx_stream_is_tty(FILE *fp) { return isatty(fileno(fp)); } -int z__b_stream_dimensions(FILE *fp, unsigned int *w, unsigned int *h) +int z__fx_stream_dimensions(FILE *fp, unsigned int *w, unsigned int *h) { if (!isatty(fileno(fp))) { return -1; @@ -33,7 +33,7 @@ int z__b_stream_dimensions(FILE *fp, unsigned int *w, unsigned int *h) return 0; } -int z__b_stream_cursorpos(FILE *in, FILE *out, unsigned int *x, unsigned int *y) +int z__fx_stream_cursorpos(FILE *in, FILE *out, unsigned int *x, unsigned int *y) { if (!isatty(fileno(in)) || !isatty(fileno(out))) { return -1; @@ -66,7 +66,7 @@ int z__b_stream_cursorpos(FILE *in, FILE *out, unsigned int *x, unsigned int *y) return 0; } -int z__b_stream_set_modifier(FILE *fp, enum z__b_stream_modifier mod) +int z__fx_stream_set_modifier(FILE *fp, enum z__fx_stream_modifier mod) { char buf[128]; int buf_i = 0; @@ -75,7 +75,7 @@ int z__b_stream_set_modifier(FILE *fp, enum z__b_stream_modifier mod) buf[buf_i++] = '\033'; buf[buf_i++] = '['; - if (mod & Z__B_STREAM_MOD_RESET) { + if (mod & Z__FX_STREAM_MOD_RESET) { buf[buf_i++] = '0'; buf[buf_i++] = 'm'; buf[buf_i++] = '\0'; @@ -84,7 +84,7 @@ int z__b_stream_set_modifier(FILE *fp, enum z__b_stream_modifier mod) return 0; } - if (mod & Z__B_STREAM_MOD_BOLD) { + if (mod & Z__FX_STREAM_MOD_BOLD) { if (nr_codes > 0) { buf[buf_i++] = ';'; } @@ -93,7 +93,7 @@ int z__b_stream_set_modifier(FILE *fp, enum z__b_stream_modifier mod) nr_codes++; } - if (mod & Z__B_STREAM_MOD_ITALIC) { + if (mod & Z__FX_STREAM_MOD_ITALIC) { if (nr_codes > 0) { buf[buf_i++] = ';'; } @@ -102,7 +102,7 @@ int z__b_stream_set_modifier(FILE *fp, enum z__b_stream_modifier mod) nr_codes++; } - if (mod & Z__B_STREAM_MOD_ULINE) { + if (mod & Z__FX_STREAM_MOD_ULINE) { if (nr_codes > 0) { buf[buf_i++] = ';'; } @@ -111,7 +111,7 @@ int z__b_stream_set_modifier(FILE *fp, enum z__b_stream_modifier mod) nr_codes++; } - if (mod & Z__B_STREAM_MOD_INVERT) { + if (mod & Z__FX_STREAM_MOD_INVERT) { if (nr_codes > 0) { buf[buf_i++] = ';'; } @@ -120,44 +120,44 @@ int z__b_stream_set_modifier(FILE *fp, enum z__b_stream_modifier mod) nr_codes++; } - if (Z__B_STREAM_MOD_GET_FG_COLOUR(mod) != 0) { + if (Z__FX_STREAM_MOD_GET_FG_COLOUR(mod) != 0) { if (nr_codes > 0) { buf[buf_i++] = ';'; } - buf[buf_i++] = mod & Z__B_STREAM_MOD_BRIGHT ? '9' : '3'; + buf[buf_i++] = mod & Z__FX_STREAM_MOD_BRIGHT ? '9' : '3'; } - switch (Z__B_STREAM_MOD_GET_FG_COLOUR(mod)) { - case Z__B_STREAM_MOD_BLACK: + switch (Z__FX_STREAM_MOD_GET_FG_COLOUR(mod)) { + case Z__FX_STREAM_MOD_BLACK: buf[buf_i++] = '0'; nr_codes++; break; - case Z__B_STREAM_MOD_RED: + case Z__FX_STREAM_MOD_RED: buf[buf_i++] = '1'; nr_codes++; break; - case Z__B_STREAM_MOD_GREEN: + case Z__FX_STREAM_MOD_GREEN: buf[buf_i++] = '2'; nr_codes++; break; - case Z__B_STREAM_MOD_BLUE: + case Z__FX_STREAM_MOD_FX: buf[buf_i++] = '4'; nr_codes++; break; - case Z__B_STREAM_MOD_RED | Z__B_STREAM_MOD_GREEN: + case Z__FX_STREAM_MOD_RED | Z__FX_STREAM_MOD_GREEN: buf[buf_i++] = '3'; nr_codes++; break; - case Z__B_STREAM_MOD_RED | Z__B_STREAM_MOD_BLUE: + case Z__FX_STREAM_MOD_RED | Z__FX_STREAM_MOD_FX: buf[buf_i++] = '5'; nr_codes++; break; - case Z__B_STREAM_MOD_GREEN | Z__B_STREAM_MOD_BLUE: + case Z__FX_STREAM_MOD_GREEN | Z__FX_STREAM_MOD_FX: buf[buf_i++] = '6'; nr_codes++; break; - case Z__B_STREAM_MOD_RED | Z__B_STREAM_MOD_GREEN | Z__B_STREAM_MOD_BLUE: + case Z__FX_STREAM_MOD_RED | Z__FX_STREAM_MOD_GREEN | Z__FX_STREAM_MOD_FX: buf[buf_i++] = '7'; nr_codes++; break; @@ -165,12 +165,12 @@ int z__b_stream_set_modifier(FILE *fp, enum z__b_stream_modifier mod) break; } - if (Z__B_STREAM_MOD_GET_BG_COLOUR(mod) != 0) { + if (Z__FX_STREAM_MOD_GET_BG_COLOUR(mod) != 0) { if (nr_codes > 0) { buf[buf_i++] = ';'; } - if (mod & Z__B_STREAM_MOD_BG_BRIGHT) { + if (mod & Z__FX_STREAM_MOD_BG_BRIGHT) { buf[buf_i++] = '1'; buf[buf_i++] = '0'; } else { @@ -178,37 +178,37 @@ int z__b_stream_set_modifier(FILE *fp, enum z__b_stream_modifier mod) } } - switch (Z__B_STREAM_MOD_GET_BG_COLOUR(mod)) { - case Z__B_STREAM_MOD_BG_BLACK: + switch (Z__FX_STREAM_MOD_GET_BG_COLOUR(mod)) { + case Z__FX_STREAM_MOD_BG_BLACK: buf[buf_i++] = '0'; nr_codes++; break; - case Z__B_STREAM_MOD_BG_RED: + case Z__FX_STREAM_MOD_BG_RED: buf[buf_i++] = '1'; nr_codes++; break; - case Z__B_STREAM_MOD_BG_GREEN: + case Z__FX_STREAM_MOD_BG_GREEN: buf[buf_i++] = '2'; nr_codes++; break; - case Z__B_STREAM_MOD_BG_BLUE: + case Z__FX_STREAM_MOD_BG_FX: buf[buf_i++] = '4'; nr_codes++; break; - case Z__B_STREAM_MOD_BG_RED | Z__B_STREAM_MOD_BG_GREEN: + case Z__FX_STREAM_MOD_BG_RED | Z__FX_STREAM_MOD_BG_GREEN: buf[buf_i++] = '3'; nr_codes++; break; - case Z__B_STREAM_MOD_BG_RED | Z__B_STREAM_MOD_BG_BLUE: + case Z__FX_STREAM_MOD_BG_RED | Z__FX_STREAM_MOD_BG_FX: buf[buf_i++] = '5'; nr_codes++; break; - case Z__B_STREAM_MOD_BG_GREEN | Z__B_STREAM_MOD_BG_BLUE: + case Z__FX_STREAM_MOD_BG_GREEN | Z__FX_STREAM_MOD_BG_FX: buf[buf_i++] = '6'; nr_codes++; break; - case Z__B_STREAM_MOD_BG_RED | Z__B_STREAM_MOD_BG_GREEN - | Z__B_STREAM_MOD_BG_BLUE: + case Z__FX_STREAM_MOD_BG_RED | Z__FX_STREAM_MOD_BG_GREEN + | Z__FX_STREAM_MOD_BG_FX: buf[buf_i++] = '7'; nr_codes++; break; diff --git a/term/sys/darwin/tty.c b/term/sys/darwin/tty.c index b035f62..a9135a1 100644 --- a/term/sys/darwin/tty.c +++ b/term/sys/darwin/tty.c @@ -1,6 +1,6 @@ #include "../../tty.h" -#include +#include #include #include #include @@ -25,61 +25,61 @@ #define ANSI_TRUECOLOUR_BG "48;2" enum tty_flags { - B_TTY_INIT = 0x01u, - B_TTY_INTERACTIVE = 0x02u, + FX_TTY_INIT = 0x01u, + FX_TTY_INTERACTIVE = 0x02u, }; -struct b_tty { +struct fx_tty { FILE *t_in, *t_out; enum tty_flags t_flags; struct termios t_ios_raw, t_ios_default; - struct b_tty_vmode t_vmode; + struct fx_tty_vmode t_vmode; unsigned char t_mcount; struct tty_format_buf t_format_buf; }; -static struct b_tty std = {0}; -static struct b_tty err = {0}; +static struct fx_tty std = {0}; +static struct fx_tty err = {0}; static const char *ansi_colour16_fg[] = { - [B_TTY_COLOUR16_BLACK] = "30", - [B_TTY_COLOUR16_RED] = "31", - [B_TTY_COLOUR16_GREEN] = "32", - [B_TTY_COLOUR16_YELLOW] = "33", - [B_TTY_COLOUR16_BLUE] = "34", - [B_TTY_COLOUR16_MAGENTA] = "35", - [B_TTY_COLOUR16_CYAN] = "36", - [B_TTY_COLOUR16_WHITE] = "37", - [B_TTY_COLOUR16_BRIGHT_BLACK] = "90", - [B_TTY_COLOUR16_BRIGHT_RED] = "91", - [B_TTY_COLOUR16_BRIGHT_GREEN] = "92", - [B_TTY_COLOUR16_BRIGHT_YELLOW] = "93", - [B_TTY_COLOUR16_BRIGHT_BLUE] = "94", - [B_TTY_COLOUR16_BRIGHT_MAGENTA] = "95", - [B_TTY_COLOUR16_BRIGHT_CYAN] = "96", - [B_TTY_COLOUR16_BRIGHT_WHITE] = "97", + [FX_TTY_COLOUR16_BLACK] = "30", + [FX_TTY_COLOUR16_RED] = "31", + [FX_TTY_COLOUR16_GREEN] = "32", + [FX_TTY_COLOUR16_YELLOW] = "33", + [FX_TTY_COLOUR16_FX] = "34", + [FX_TTY_COLOUR16_MAGENTA] = "35", + [FX_TTY_COLOUR16_CYAN] = "36", + [FX_TTY_COLOUR16_WHITE] = "37", + [FX_TTY_COLOUR16_BRIGHT_BLACK] = "90", + [FX_TTY_COLOUR16_BRIGHT_RED] = "91", + [FX_TTY_COLOUR16_BRIGHT_GREEN] = "92", + [FX_TTY_COLOUR16_BRIGHT_YELLOW] = "93", + [FX_TTY_COLOUR16_BRIGHT_FX] = "94", + [FX_TTY_COLOUR16_BRIGHT_MAGENTA] = "95", + [FX_TTY_COLOUR16_BRIGHT_CYAN] = "96", + [FX_TTY_COLOUR16_BRIGHT_WHITE] = "97", }; static const char *ansi_colour16_bg[] = { - [B_TTY_COLOUR16_BLACK] = "40", - [B_TTY_COLOUR16_RED] = "41", - [B_TTY_COLOUR16_GREEN] = "42", - [B_TTY_COLOUR16_YELLOW] = "43", - [B_TTY_COLOUR16_BLUE] = "44", - [B_TTY_COLOUR16_MAGENTA] = "45", - [B_TTY_COLOUR16_CYAN] = "46", - [B_TTY_COLOUR16_WHITE] = "47", - [B_TTY_COLOUR16_BRIGHT_BLACK] = "100", - [B_TTY_COLOUR16_BRIGHT_RED] = "101", - [B_TTY_COLOUR16_BRIGHT_GREEN] = "102", - [B_TTY_COLOUR16_BRIGHT_YELLOW] = "103", - [B_TTY_COLOUR16_BRIGHT_BLUE] = "104", - [B_TTY_COLOUR16_BRIGHT_MAGENTA] = "105", - [B_TTY_COLOUR16_BRIGHT_CYAN] = "106", - [B_TTY_COLOUR16_BRIGHT_WHITE] = "107", + [FX_TTY_COLOUR16_BLACK] = "40", + [FX_TTY_COLOUR16_RED] = "41", + [FX_TTY_COLOUR16_GREEN] = "42", + [FX_TTY_COLOUR16_YELLOW] = "43", + [FX_TTY_COLOUR16_FX] = "44", + [FX_TTY_COLOUR16_MAGENTA] = "45", + [FX_TTY_COLOUR16_CYAN] = "46", + [FX_TTY_COLOUR16_WHITE] = "47", + [FX_TTY_COLOUR16_BRIGHT_BLACK] = "100", + [FX_TTY_COLOUR16_BRIGHT_RED] = "101", + [FX_TTY_COLOUR16_BRIGHT_GREEN] = "102", + [FX_TTY_COLOUR16_BRIGHT_YELLOW] = "103", + [FX_TTY_COLOUR16_BRIGHT_FX] = "104", + [FX_TTY_COLOUR16_BRIGHT_MAGENTA] = "105", + [FX_TTY_COLOUR16_BRIGHT_CYAN] = "106", + [FX_TTY_COLOUR16_BRIGHT_WHITE] = "107", }; -static void init_tty(struct b_tty *tty, FILE *in, FILE *out) +static void init_tty(struct fx_tty *tty, FILE *in, FILE *out) { tty->t_in = in; tty->t_out = out; @@ -96,7 +96,7 @@ static void init_tty(struct b_tty *tty, FILE *in, FILE *out) } if (isatty(fd)) { - tty->t_flags |= B_TTY_INTERACTIVE; + tty->t_flags |= FX_TTY_INTERACTIVE; } tcgetattr(fd, &tty->t_ios_default); @@ -106,43 +106,43 @@ static void init_tty(struct b_tty *tty, FILE *in, FILE *out) tty->t_ios_raw.c_oflag &= ~(OPOST); tty->t_ios_raw.c_lflag &= ~(ECHO | ICANON | IEXTEN); - tty->t_flags |= B_TTY_INIT; + tty->t_flags |= FX_TTY_INIT; } -struct b_tty *z__b_tty_get_std(void) +struct fx_tty *z__fx_tty_get_std(void) { - if (!(std.t_flags & B_TTY_INIT)) { + if (!(std.t_flags & FX_TTY_INIT)) { init_tty(&std, stdin, stdout); } return &std; } -struct b_tty *z__b_tty_get_err(void) +struct fx_tty *z__fx_tty_get_err(void) { - if (!(err.t_flags & B_TTY_INIT)) { + if (!(err.t_flags & FX_TTY_INIT)) { init_tty(&err, NULL, stderr); } return &err; } -struct tty_format_buf *z__b_tty_get_format_buf(struct b_tty *tty) +struct tty_format_buf *z__fx_tty_get_format_buf(struct fx_tty *tty) { return &tty->t_format_buf; } -void z__b_tty_putc(struct b_tty *tty, char c) +void z__fx_tty_putc(struct fx_tty *tty, char c) { fputc(c, tty->t_out); } -bool b_tty_is_interactive(const struct b_tty *tty) +bool fx_tty_is_interactive(const struct fx_tty *tty) { - return (tty->t_flags & B_TTY_INTERACTIVE) == B_TTY_INTERACTIVE; + return (tty->t_flags & FX_TTY_INTERACTIVE) == FX_TTY_INTERACTIVE; } -static void set_raw(struct b_tty *tty) +static void set_raw(struct fx_tty *tty) { int fd = -1; if (tty->t_in) { @@ -158,7 +158,7 @@ static void set_raw(struct b_tty *tty) tcsetattr(fd, TCSAFLUSH, &tty->t_ios_raw); } -static void set_canon(struct b_tty *tty) +static void set_canon(struct fx_tty *tty) { int fd = -1; if (tty->t_in) { @@ -174,13 +174,13 @@ static void set_canon(struct b_tty *tty) tcsetattr(fd, TCSAFLUSH, &tty->t_ios_default); } -void b_tty_set_mode(struct b_tty *tty, enum b_tty_mode mode) +void fx_tty_set_mode(struct fx_tty *tty, enum fx_tty_mode mode) { switch (mode) { - case B_TTY_CANONICAL: + case FX_TTY_CANONICAL: set_canon(tty); break; - case B_TTY_RAW: + case FX_TTY_RAW: set_raw(tty); break; default: @@ -188,11 +188,11 @@ void b_tty_set_mode(struct b_tty *tty, enum b_tty_mode mode) } } -void b_tty_reset_vmode(struct b_tty *tty) +void fx_tty_reset_vmode(struct fx_tty *tty) { - if (tty->t_vmode.v_fg.c_mode == B_TTY_COLOUR_NONE - && tty->t_vmode.v_bg.c_mode == B_TTY_COLOUR_NONE - && tty->t_vmode.v_attrib == B_TTY_ATTRIB_NORMAL) { + if (tty->t_vmode.v_fg.c_mode == FX_TTY_COLOUR_NONE + && tty->t_vmode.v_bg.c_mode == FX_TTY_COLOUR_NONE + && tty->t_vmode.v_attrib == FX_TTY_ATTRIB_NORMAL) { return; } @@ -200,30 +200,30 @@ void b_tty_reset_vmode(struct b_tty *tty) memset(&tty->t_vmode, 0x0, sizeof tty->t_vmode); - tty->t_vmode.v_fg.c_mode = B_TTY_COLOUR_NONE; - tty->t_vmode.v_bg.c_mode = B_TTY_COLOUR_NONE; - tty->t_vmode.v_attrib = B_TTY_ATTRIB_NORMAL; + tty->t_vmode.v_fg.c_mode = FX_TTY_COLOUR_NONE; + tty->t_vmode.v_bg.c_mode = FX_TTY_COLOUR_NONE; + tty->t_vmode.v_attrib = FX_TTY_ATTRIB_NORMAL; } static int compare_colour( - const struct b_tty_colour *a, const struct b_tty_colour *b) + const struct fx_tty_colour *a, const struct fx_tty_colour *b) { if (a->c_mode != b->c_mode) { return -1; } switch (a->c_mode) { - case B_TTY_COLOUR_16: + case FX_TTY_COLOUR_16: if (a->c_16.value != b->c_16.value) { return -1; } break; - case B_TTY_COLOUR_256: + case FX_TTY_COLOUR_256: if (a->c_256.value != b->c_256.value) { return -1; } break; - case B_TTY_COLOUR_TRUE: + case FX_TTY_COLOUR_TRUE: if (a->c_true.r != b->c_true.r) { return -1; } @@ -243,7 +243,7 @@ static int compare_colour( return 0; } -static int compare_vmode(const struct b_tty_vmode *a, const struct b_tty_vmode *b) +static int compare_vmode(const struct fx_tty_vmode *a, const struct fx_tty_vmode *b) { if (a->v_attrib != b->v_attrib) { return -1; @@ -260,7 +260,7 @@ static int compare_vmode(const struct b_tty_vmode *a, const struct b_tty_vmode * return 0; } -static void put_ansi_attrib(struct b_tty *tty, const char *s) +static void put_ansi_attrib(struct fx_tty *tty, const char *s) { if (tty->t_mcount > 0) { fputs(";", tty->t_out); @@ -271,46 +271,46 @@ static void put_ansi_attrib(struct b_tty *tty, const char *s) } static void set_attrib( - struct b_tty *tty, enum b_tty_attrib old, enum b_tty_attrib new) + struct fx_tty *tty, enum fx_tty_attrib old, enum fx_tty_attrib new) { if (old == new) { return; } /* Bold ON */ - if (!(old & B_TTY_ATTRIB_BOLD) && new &B_TTY_ATTRIB_BOLD) { + if (!(old & FX_TTY_ATTRIB_BOLD) && new &FX_TTY_ATTRIB_BOLD) { put_ansi_attrib(tty, ANSI_BOLD_ON); } /* Bold OFF */ - if (old & B_TTY_ATTRIB_BOLD && !(new &B_TTY_ATTRIB_BOLD)) { + if (old & FX_TTY_ATTRIB_BOLD && !(new &FX_TTY_ATTRIB_BOLD)) { put_ansi_attrib(tty, ANSI_BOLD_OFF); } /* Underline ON */ - if (!(old & B_TTY_ATTRIB_UNDERLINE) && new &B_TTY_ATTRIB_UNDERLINE) { + if (!(old & FX_TTY_ATTRIB_UNDERLINE) && new &FX_TTY_ATTRIB_UNDERLINE) { put_ansi_attrib(tty, ANSI_UNDERLINE_ON); } /* Underline OFF */ - if (old & B_TTY_ATTRIB_UNDERLINE && !(new &B_TTY_ATTRIB_UNDERLINE)) { + if (old & FX_TTY_ATTRIB_UNDERLINE && !(new &FX_TTY_ATTRIB_UNDERLINE)) { put_ansi_attrib(tty, ANSI_UNDERLINE_OFF); } /* Italic ON */ - if (!(old & B_TTY_ATTRIB_ITALIC) && new &B_TTY_ATTRIB_ITALIC) { + if (!(old & FX_TTY_ATTRIB_ITALIC) && new &FX_TTY_ATTRIB_ITALIC) { put_ansi_attrib(tty, ANSI_ITALIC_ON); } /* Italic OFF */ - if (old & B_TTY_ATTRIB_ITALIC && !(new &B_TTY_ATTRIB_ITALIC)) { + if (old & FX_TTY_ATTRIB_ITALIC && !(new &FX_TTY_ATTRIB_ITALIC)) { put_ansi_attrib(tty, ANSI_ITALIC_OFF); } } static void set_fg( - struct b_tty *tty, const struct b_tty_colour *old, - const struct b_tty_colour *new) + struct fx_tty *tty, const struct fx_tty_colour *old, + const struct fx_tty_colour *new) { if (compare_colour(old, new) == 0) { return; @@ -319,18 +319,18 @@ static void set_fg( char buf[8]; switch (new->c_mode) { - case B_TTY_COLOUR_NONE: + case FX_TTY_COLOUR_NONE: put_ansi_attrib(tty, ANSI_DEFAULTCOLOUR_FG); break; - case B_TTY_COLOUR_16: + case FX_TTY_COLOUR_16: put_ansi_attrib(tty, ansi_colour16_fg[new->c_16.value]); break; - case B_TTY_COLOUR_256: + case FX_TTY_COLOUR_256: put_ansi_attrib(tty, ANSI_256COLOUR_FG); snprintf(buf, sizeof buf, "%u", new->c_256.value); put_ansi_attrib(tty, buf); break; - case B_TTY_COLOUR_TRUE: + case FX_TTY_COLOUR_TRUE: put_ansi_attrib(tty, ANSI_TRUECOLOUR_FG); snprintf(buf, sizeof buf, "%u", new->c_true.r); put_ansi_attrib(tty, buf); @@ -345,8 +345,8 @@ static void set_fg( } static void set_bg( - struct b_tty *tty, const struct b_tty_colour *old, - const struct b_tty_colour *new) + struct fx_tty *tty, const struct fx_tty_colour *old, + const struct fx_tty_colour *new) { if (compare_colour(old, new) == 0) { return; @@ -355,18 +355,18 @@ static void set_bg( char buf[8]; switch (new->c_mode) { - case B_TTY_COLOUR_NONE: + case FX_TTY_COLOUR_NONE: put_ansi_attrib(tty, ANSI_DEFAULTCOLOUR_BG); break; - case B_TTY_COLOUR_16: + case FX_TTY_COLOUR_16: put_ansi_attrib(tty, ansi_colour16_bg[new->c_16.value]); break; - case B_TTY_COLOUR_256: + case FX_TTY_COLOUR_256: put_ansi_attrib(tty, ANSI_256COLOUR_BG); snprintf(buf, sizeof buf, "%u", new->c_256.value); put_ansi_attrib(tty, buf); break; - case B_TTY_COLOUR_TRUE: + case FX_TTY_COLOUR_TRUE: put_ansi_attrib(tty, ANSI_TRUECOLOUR_BG); snprintf(buf, sizeof buf, "%u", new->c_true.r); put_ansi_attrib(tty, buf); @@ -380,7 +380,7 @@ static void set_bg( } } -void b_tty_set_vmode(struct b_tty *tty, const struct b_tty_vmode *vmode) +void fx_tty_set_vmode(struct fx_tty *tty, const struct fx_tty_vmode *vmode) { if (compare_vmode(&tty->t_vmode, vmode) == 0) { return; @@ -399,7 +399,7 @@ void b_tty_set_vmode(struct b_tty *tty, const struct b_tty_vmode *vmode) memcpy(&tty->t_vmode, vmode, sizeof *vmode); } -b_keycode b_tty_read_key(struct b_tty *tty) +fx_keycode fx_tty_read_key(struct fx_tty *tty) { char c; int v; @@ -408,19 +408,19 @@ b_keycode b_tty_read_key(struct b_tty *tty) while (1) { v = read(fd, &c, 1); if (v < 1) { - return B_KEY_EOF; + return FX_KEY_EOF; } if (c == '\r' || c == '\n') { - return B_KEY_RETURN; + return FX_KEY_RETURN; } if (c == '\b' || c == 0x7F) { - return B_KEY_BACKSPACE; + return FX_KEY_BACKSPACE; } if (c >= 1 && c <= 26) { - return B_TTY_CTRL_KEY(c + 'a' - 1); + return FX_TTY_CTRL_KEY(c + 'a' - 1); } if (c != 0x1b) { @@ -429,7 +429,7 @@ b_keycode b_tty_read_key(struct b_tty *tty) v = read(fd, &c, 1); if (v < 1) { - return B_KEY_EOF; + return FX_KEY_EOF; } if (c != '[') { @@ -438,18 +438,18 @@ b_keycode b_tty_read_key(struct b_tty *tty) v = read(fd, &c, 1); if (v < 1) { - return B_KEY_EOF; + return FX_KEY_EOF; } switch (c) { case 'A': - return B_KEY_ARROW_UP; + return FX_KEY_ARROW_UP; case 'B': - return B_KEY_ARROW_DOWN; + return FX_KEY_ARROW_DOWN; case 'C': - return B_KEY_ARROW_RIGHT; + return FX_KEY_ARROW_RIGHT; case 'D': - return B_KEY_ARROW_LEFT; + return FX_KEY_ARROW_LEFT; default: continue; } @@ -458,9 +458,9 @@ b_keycode b_tty_read_key(struct b_tty *tty) return c; } -void b_tty_move_cursor_x(struct b_tty *tty, enum b_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 == B_TTY_POS_CURSOR && pos < 0 && pos >= -4) { + if (base == FX_TTY_POS_CURSOR && pos < 0 && pos >= -4) { for (int i = 0; i > pos; i--) { fputc('\b', tty->t_out); } @@ -468,7 +468,7 @@ void b_tty_move_cursor_x(struct b_tty *tty, enum b_tty_position_base base, int p return; } - if (base == B_TTY_POS_START) { + if (base == FX_TTY_POS_START) { if (pos == 0) { fputs("\033[G", tty->t_out); } else { @@ -487,9 +487,9 @@ void b_tty_move_cursor_x(struct b_tty *tty, enum b_tty_position_base base, int p } } -void b_tty_move_cursor_y(struct b_tty *tty, enum b_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 == B_TTY_POS_START) { + if (base == FX_TTY_POS_START) { /* we don't need this functionality right now */ abort(); } @@ -505,32 +505,32 @@ void b_tty_move_cursor_y(struct b_tty *tty, enum b_tty_position_base base, int p } } -void b_tty_clear(struct b_tty *tty, enum b_tty_clear_mode mode) +void fx_tty_clear(struct fx_tty *tty, enum fx_tty_clear_mode mode) { const char *arg; - if (mode & B_TTY_CLEAR_ALL) { + if (mode & FX_TTY_CLEAR_ALL) { arg = "2"; - } else if (mode & B_TTY_CLEAR_TO_CURSOR) { + } else if (mode & FX_TTY_CLEAR_TO_CURSOR) { arg = "1"; - } else if (mode & B_TTY_CLEAR_FROM_CURSOR) { + } else if (mode & FX_TTY_CLEAR_FROM_CURSOR) { arg = ""; } else { abort(); } - if (mode & B_TTY_CLEAR_SCREEN) { + if (mode & FX_TTY_CLEAR_SCREEN) { fprintf(tty->t_out, "\033[%sJ", arg); - } else if (mode & B_TTY_CLEAR_LINE) { + } else if (mode & FX_TTY_CLEAR_LINE) { fprintf(tty->t_out, "\033[%sK", arg); } else { abort(); } } -enum b_status b_tty_get_dimensions( - struct b_tty *tty, unsigned int *w, unsigned int *h) +enum fx_status fx_tty_get_dimensions( + struct fx_tty *tty, unsigned int *w, unsigned int *h) { - if (!(tty->t_flags & B_TTY_INTERACTIVE)) { + if (!(tty->t_flags & FX_TTY_INTERACTIVE)) { return -1; } diff --git a/term/sys/linux/print.c b/term/sys/linux/print.c index dc2a629..c4a909b 100644 --- a/term/sys/linux/print.c +++ b/term/sys/linux/print.c @@ -6,12 +6,12 @@ #include #include -int z__b_stream_is_tty(FILE *fp) +int z__fx_stream_is_tty(FILE *fp) { return isatty(fileno(fp)); } -int z__b_stream_dimensions(FILE *fp, unsigned int *w, unsigned int *h) +int z__fx_stream_dimensions(FILE *fp, unsigned int *w, unsigned int *h) { if (!isatty(fileno(fp))) { return -1; @@ -33,7 +33,7 @@ int z__b_stream_dimensions(FILE *fp, unsigned int *w, unsigned int *h) return 0; } -int z__b_stream_cursorpos(FILE *in, FILE *out, unsigned int *x, unsigned int *y) +int z__fx_stream_cursorpos(FILE *in, FILE *out, unsigned int *x, unsigned int *y) { if (!isatty(fileno(in)) || !isatty(fileno(out))) { return -1; @@ -66,7 +66,7 @@ int z__b_stream_cursorpos(FILE *in, FILE *out, unsigned int *x, unsigned int *y) return 0; } -int z__b_stream_set_modifier(FILE *fp, enum z__b_stream_modifier mod) +int z__fx_stream_set_modifier(FILE *fp, enum z__fx_stream_modifier mod) { char buf[128]; int buf_i = 0; @@ -75,7 +75,7 @@ int z__b_stream_set_modifier(FILE *fp, enum z__b_stream_modifier mod) buf[buf_i++] = '\033'; buf[buf_i++] = '['; - if (mod & Z__B_STREAM_MOD_RESET) { + if (mod & Z__FX_STREAM_MOD_RESET) { buf[buf_i++] = '0'; buf[buf_i++] = 'm'; buf[buf_i++] = '\0'; @@ -84,7 +84,7 @@ int z__b_stream_set_modifier(FILE *fp, enum z__b_stream_modifier mod) return 0; } - if (mod & Z__B_STREAM_MOD_BOLD) { + if (mod & Z__FX_STREAM_MOD_BOLD) { if (nr_codes > 0) { buf[buf_i++] = ';'; } @@ -93,7 +93,7 @@ int z__b_stream_set_modifier(FILE *fp, enum z__b_stream_modifier mod) nr_codes++; } - if (mod & Z__B_STREAM_MOD_ITALIC) { + if (mod & Z__FX_STREAM_MOD_ITALIC) { if (nr_codes > 0) { buf[buf_i++] = ';'; } @@ -102,7 +102,7 @@ int z__b_stream_set_modifier(FILE *fp, enum z__b_stream_modifier mod) nr_codes++; } - if (mod & Z__B_STREAM_MOD_ULINE) { + if (mod & Z__FX_STREAM_MOD_ULINE) { if (nr_codes > 0) { buf[buf_i++] = ';'; } @@ -111,7 +111,7 @@ int z__b_stream_set_modifier(FILE *fp, enum z__b_stream_modifier mod) nr_codes++; } - if (mod & Z__B_STREAM_MOD_INVERT) { + if (mod & Z__FX_STREAM_MOD_INVERT) { if (nr_codes > 0) { buf[buf_i++] = ';'; } @@ -120,43 +120,43 @@ int z__b_stream_set_modifier(FILE *fp, enum z__b_stream_modifier mod) nr_codes++; } - if (Z__B_STREAM_MOD_GET_FG_COLOUR(mod) != 0) { + if (Z__FX_STREAM_MOD_GET_FG_COLOUR(mod) != 0) { if (nr_codes > 0) { buf[buf_i++] = ';'; } - buf[buf_i++] = mod & Z__B_STREAM_MOD_BRIGHT ? '9' : '3'; + buf[buf_i++] = mod & Z__FX_STREAM_MOD_BRIGHT ? '9' : '3'; } - switch (Z__B_STREAM_MOD_GET_FG_COLOUR(mod)) { - case Z__B_STREAM_MOD_BLACK: + switch (Z__FX_STREAM_MOD_GET_FG_COLOUR(mod)) { + case Z__FX_STREAM_MOD_BLACK: buf[buf_i++] = '0'; nr_codes++; break; - case Z__B_STREAM_MOD_RED: + case Z__FX_STREAM_MOD_RED: buf[buf_i++] = '1'; nr_codes++; break; - case Z__B_STREAM_MOD_GREEN: + case Z__FX_STREAM_MOD_GREEN: buf[buf_i++] = '2'; nr_codes++; break; - case Z__B_STREAM_MOD_BLUE: + case Z__FX_STREAM_MOD_FX: nr_codes++; break; - case Z__B_STREAM_MOD_RED | Z__B_STREAM_MOD_GREEN: + case Z__FX_STREAM_MOD_RED | Z__FX_STREAM_MOD_GREEN: buf[buf_i++] = '3'; nr_codes++; break; - case Z__B_STREAM_MOD_RED | Z__B_STREAM_MOD_BLUE: + case Z__FX_STREAM_MOD_RED | Z__FX_STREAM_MOD_FX: buf[buf_i++] = '5'; nr_codes++; break; - case Z__B_STREAM_MOD_GREEN | Z__B_STREAM_MOD_BLUE: + case Z__FX_STREAM_MOD_GREEN | Z__FX_STREAM_MOD_FX: buf[buf_i++] = '6'; nr_codes++; break; - case Z__B_STREAM_MOD_RED | Z__B_STREAM_MOD_GREEN | Z__B_STREAM_MOD_BLUE: + case Z__FX_STREAM_MOD_RED | Z__FX_STREAM_MOD_GREEN | Z__FX_STREAM_MOD_FX: buf[buf_i++] = '7'; nr_codes++; break; @@ -164,12 +164,12 @@ int z__b_stream_set_modifier(FILE *fp, enum z__b_stream_modifier mod) break; } - if (Z__B_STREAM_MOD_GET_BG_COLOUR(mod) != 0) { + if (Z__FX_STREAM_MOD_GET_BG_COLOUR(mod) != 0) { if (nr_codes > 0) { buf[buf_i++] = ';'; } - if (mod & Z__B_STREAM_MOD_BG_BRIGHT) { + if (mod & Z__FX_STREAM_MOD_BG_BRIGHT) { buf[buf_i++] = '1'; buf[buf_i++] = '0'; } else { @@ -177,36 +177,36 @@ int z__b_stream_set_modifier(FILE *fp, enum z__b_stream_modifier mod) } } - switch (Z__B_STREAM_MOD_GET_BG_COLOUR(mod)) { - case Z__B_STREAM_MOD_BG_BLACK: + switch (Z__FX_STREAM_MOD_GET_BG_COLOUR(mod)) { + case Z__FX_STREAM_MOD_BG_BLACK: buf[buf_i++] = '0'; nr_codes++; break; - case Z__B_STREAM_MOD_BG_RED: + case Z__FX_STREAM_MOD_BG_RED: buf[buf_i++] = '1'; nr_codes++; break; - case Z__B_STREAM_MOD_BG_GREEN: + case Z__FX_STREAM_MOD_BG_GREEN: buf[buf_i++] = '2'; nr_codes++; break; - case Z__B_STREAM_MOD_BG_BLUE: + case Z__FX_STREAM_MOD_BG_FX: nr_codes++; break; - case Z__B_STREAM_MOD_BG_RED | Z__B_STREAM_MOD_BG_GREEN: + case Z__FX_STREAM_MOD_BG_RED | Z__FX_STREAM_MOD_BG_GREEN: buf[buf_i++] = '3'; nr_codes++; break; - case Z__B_STREAM_MOD_BG_RED | Z__B_STREAM_MOD_BG_BLUE: + case Z__FX_STREAM_MOD_BG_RED | Z__FX_STREAM_MOD_BG_FX: buf[buf_i++] = '5'; nr_codes++; break; - case Z__B_STREAM_MOD_BG_GREEN | Z__B_STREAM_MOD_BG_BLUE: + case Z__FX_STREAM_MOD_BG_GREEN | Z__FX_STREAM_MOD_BG_FX: buf[buf_i++] = '6'; nr_codes++; break; - case Z__B_STREAM_MOD_BG_RED | Z__B_STREAM_MOD_BG_GREEN - | Z__B_STREAM_MOD_BG_BLUE: + case Z__FX_STREAM_MOD_BG_RED | Z__FX_STREAM_MOD_BG_GREEN + | Z__FX_STREAM_MOD_BG_FX: buf[buf_i++] = '7'; nr_codes++; break; diff --git a/term/sys/linux/tty.c b/term/sys/linux/tty.c index b035f62..a9135a1 100644 --- a/term/sys/linux/tty.c +++ b/term/sys/linux/tty.c @@ -1,6 +1,6 @@ #include "../../tty.h" -#include +#include #include #include #include @@ -25,61 +25,61 @@ #define ANSI_TRUECOLOUR_BG "48;2" enum tty_flags { - B_TTY_INIT = 0x01u, - B_TTY_INTERACTIVE = 0x02u, + FX_TTY_INIT = 0x01u, + FX_TTY_INTERACTIVE = 0x02u, }; -struct b_tty { +struct fx_tty { FILE *t_in, *t_out; enum tty_flags t_flags; struct termios t_ios_raw, t_ios_default; - struct b_tty_vmode t_vmode; + struct fx_tty_vmode t_vmode; unsigned char t_mcount; struct tty_format_buf t_format_buf; }; -static struct b_tty std = {0}; -static struct b_tty err = {0}; +static struct fx_tty std = {0}; +static struct fx_tty err = {0}; static const char *ansi_colour16_fg[] = { - [B_TTY_COLOUR16_BLACK] = "30", - [B_TTY_COLOUR16_RED] = "31", - [B_TTY_COLOUR16_GREEN] = "32", - [B_TTY_COLOUR16_YELLOW] = "33", - [B_TTY_COLOUR16_BLUE] = "34", - [B_TTY_COLOUR16_MAGENTA] = "35", - [B_TTY_COLOUR16_CYAN] = "36", - [B_TTY_COLOUR16_WHITE] = "37", - [B_TTY_COLOUR16_BRIGHT_BLACK] = "90", - [B_TTY_COLOUR16_BRIGHT_RED] = "91", - [B_TTY_COLOUR16_BRIGHT_GREEN] = "92", - [B_TTY_COLOUR16_BRIGHT_YELLOW] = "93", - [B_TTY_COLOUR16_BRIGHT_BLUE] = "94", - [B_TTY_COLOUR16_BRIGHT_MAGENTA] = "95", - [B_TTY_COLOUR16_BRIGHT_CYAN] = "96", - [B_TTY_COLOUR16_BRIGHT_WHITE] = "97", + [FX_TTY_COLOUR16_BLACK] = "30", + [FX_TTY_COLOUR16_RED] = "31", + [FX_TTY_COLOUR16_GREEN] = "32", + [FX_TTY_COLOUR16_YELLOW] = "33", + [FX_TTY_COLOUR16_FX] = "34", + [FX_TTY_COLOUR16_MAGENTA] = "35", + [FX_TTY_COLOUR16_CYAN] = "36", + [FX_TTY_COLOUR16_WHITE] = "37", + [FX_TTY_COLOUR16_BRIGHT_BLACK] = "90", + [FX_TTY_COLOUR16_BRIGHT_RED] = "91", + [FX_TTY_COLOUR16_BRIGHT_GREEN] = "92", + [FX_TTY_COLOUR16_BRIGHT_YELLOW] = "93", + [FX_TTY_COLOUR16_BRIGHT_FX] = "94", + [FX_TTY_COLOUR16_BRIGHT_MAGENTA] = "95", + [FX_TTY_COLOUR16_BRIGHT_CYAN] = "96", + [FX_TTY_COLOUR16_BRIGHT_WHITE] = "97", }; static const char *ansi_colour16_bg[] = { - [B_TTY_COLOUR16_BLACK] = "40", - [B_TTY_COLOUR16_RED] = "41", - [B_TTY_COLOUR16_GREEN] = "42", - [B_TTY_COLOUR16_YELLOW] = "43", - [B_TTY_COLOUR16_BLUE] = "44", - [B_TTY_COLOUR16_MAGENTA] = "45", - [B_TTY_COLOUR16_CYAN] = "46", - [B_TTY_COLOUR16_WHITE] = "47", - [B_TTY_COLOUR16_BRIGHT_BLACK] = "100", - [B_TTY_COLOUR16_BRIGHT_RED] = "101", - [B_TTY_COLOUR16_BRIGHT_GREEN] = "102", - [B_TTY_COLOUR16_BRIGHT_YELLOW] = "103", - [B_TTY_COLOUR16_BRIGHT_BLUE] = "104", - [B_TTY_COLOUR16_BRIGHT_MAGENTA] = "105", - [B_TTY_COLOUR16_BRIGHT_CYAN] = "106", - [B_TTY_COLOUR16_BRIGHT_WHITE] = "107", + [FX_TTY_COLOUR16_BLACK] = "40", + [FX_TTY_COLOUR16_RED] = "41", + [FX_TTY_COLOUR16_GREEN] = "42", + [FX_TTY_COLOUR16_YELLOW] = "43", + [FX_TTY_COLOUR16_FX] = "44", + [FX_TTY_COLOUR16_MAGENTA] = "45", + [FX_TTY_COLOUR16_CYAN] = "46", + [FX_TTY_COLOUR16_WHITE] = "47", + [FX_TTY_COLOUR16_BRIGHT_BLACK] = "100", + [FX_TTY_COLOUR16_BRIGHT_RED] = "101", + [FX_TTY_COLOUR16_BRIGHT_GREEN] = "102", + [FX_TTY_COLOUR16_BRIGHT_YELLOW] = "103", + [FX_TTY_COLOUR16_BRIGHT_FX] = "104", + [FX_TTY_COLOUR16_BRIGHT_MAGENTA] = "105", + [FX_TTY_COLOUR16_BRIGHT_CYAN] = "106", + [FX_TTY_COLOUR16_BRIGHT_WHITE] = "107", }; -static void init_tty(struct b_tty *tty, FILE *in, FILE *out) +static void init_tty(struct fx_tty *tty, FILE *in, FILE *out) { tty->t_in = in; tty->t_out = out; @@ -96,7 +96,7 @@ static void init_tty(struct b_tty *tty, FILE *in, FILE *out) } if (isatty(fd)) { - tty->t_flags |= B_TTY_INTERACTIVE; + tty->t_flags |= FX_TTY_INTERACTIVE; } tcgetattr(fd, &tty->t_ios_default); @@ -106,43 +106,43 @@ static void init_tty(struct b_tty *tty, FILE *in, FILE *out) tty->t_ios_raw.c_oflag &= ~(OPOST); tty->t_ios_raw.c_lflag &= ~(ECHO | ICANON | IEXTEN); - tty->t_flags |= B_TTY_INIT; + tty->t_flags |= FX_TTY_INIT; } -struct b_tty *z__b_tty_get_std(void) +struct fx_tty *z__fx_tty_get_std(void) { - if (!(std.t_flags & B_TTY_INIT)) { + if (!(std.t_flags & FX_TTY_INIT)) { init_tty(&std, stdin, stdout); } return &std; } -struct b_tty *z__b_tty_get_err(void) +struct fx_tty *z__fx_tty_get_err(void) { - if (!(err.t_flags & B_TTY_INIT)) { + if (!(err.t_flags & FX_TTY_INIT)) { init_tty(&err, NULL, stderr); } return &err; } -struct tty_format_buf *z__b_tty_get_format_buf(struct b_tty *tty) +struct tty_format_buf *z__fx_tty_get_format_buf(struct fx_tty *tty) { return &tty->t_format_buf; } -void z__b_tty_putc(struct b_tty *tty, char c) +void z__fx_tty_putc(struct fx_tty *tty, char c) { fputc(c, tty->t_out); } -bool b_tty_is_interactive(const struct b_tty *tty) +bool fx_tty_is_interactive(const struct fx_tty *tty) { - return (tty->t_flags & B_TTY_INTERACTIVE) == B_TTY_INTERACTIVE; + return (tty->t_flags & FX_TTY_INTERACTIVE) == FX_TTY_INTERACTIVE; } -static void set_raw(struct b_tty *tty) +static void set_raw(struct fx_tty *tty) { int fd = -1; if (tty->t_in) { @@ -158,7 +158,7 @@ static void set_raw(struct b_tty *tty) tcsetattr(fd, TCSAFLUSH, &tty->t_ios_raw); } -static void set_canon(struct b_tty *tty) +static void set_canon(struct fx_tty *tty) { int fd = -1; if (tty->t_in) { @@ -174,13 +174,13 @@ static void set_canon(struct b_tty *tty) tcsetattr(fd, TCSAFLUSH, &tty->t_ios_default); } -void b_tty_set_mode(struct b_tty *tty, enum b_tty_mode mode) +void fx_tty_set_mode(struct fx_tty *tty, enum fx_tty_mode mode) { switch (mode) { - case B_TTY_CANONICAL: + case FX_TTY_CANONICAL: set_canon(tty); break; - case B_TTY_RAW: + case FX_TTY_RAW: set_raw(tty); break; default: @@ -188,11 +188,11 @@ void b_tty_set_mode(struct b_tty *tty, enum b_tty_mode mode) } } -void b_tty_reset_vmode(struct b_tty *tty) +void fx_tty_reset_vmode(struct fx_tty *tty) { - if (tty->t_vmode.v_fg.c_mode == B_TTY_COLOUR_NONE - && tty->t_vmode.v_bg.c_mode == B_TTY_COLOUR_NONE - && tty->t_vmode.v_attrib == B_TTY_ATTRIB_NORMAL) { + if (tty->t_vmode.v_fg.c_mode == FX_TTY_COLOUR_NONE + && tty->t_vmode.v_bg.c_mode == FX_TTY_COLOUR_NONE + && tty->t_vmode.v_attrib == FX_TTY_ATTRIB_NORMAL) { return; } @@ -200,30 +200,30 @@ void b_tty_reset_vmode(struct b_tty *tty) memset(&tty->t_vmode, 0x0, sizeof tty->t_vmode); - tty->t_vmode.v_fg.c_mode = B_TTY_COLOUR_NONE; - tty->t_vmode.v_bg.c_mode = B_TTY_COLOUR_NONE; - tty->t_vmode.v_attrib = B_TTY_ATTRIB_NORMAL; + tty->t_vmode.v_fg.c_mode = FX_TTY_COLOUR_NONE; + tty->t_vmode.v_bg.c_mode = FX_TTY_COLOUR_NONE; + tty->t_vmode.v_attrib = FX_TTY_ATTRIB_NORMAL; } static int compare_colour( - const struct b_tty_colour *a, const struct b_tty_colour *b) + const struct fx_tty_colour *a, const struct fx_tty_colour *b) { if (a->c_mode != b->c_mode) { return -1; } switch (a->c_mode) { - case B_TTY_COLOUR_16: + case FX_TTY_COLOUR_16: if (a->c_16.value != b->c_16.value) { return -1; } break; - case B_TTY_COLOUR_256: + case FX_TTY_COLOUR_256: if (a->c_256.value != b->c_256.value) { return -1; } break; - case B_TTY_COLOUR_TRUE: + case FX_TTY_COLOUR_TRUE: if (a->c_true.r != b->c_true.r) { return -1; } @@ -243,7 +243,7 @@ static int compare_colour( return 0; } -static int compare_vmode(const struct b_tty_vmode *a, const struct b_tty_vmode *b) +static int compare_vmode(const struct fx_tty_vmode *a, const struct fx_tty_vmode *b) { if (a->v_attrib != b->v_attrib) { return -1; @@ -260,7 +260,7 @@ static int compare_vmode(const struct b_tty_vmode *a, const struct b_tty_vmode * return 0; } -static void put_ansi_attrib(struct b_tty *tty, const char *s) +static void put_ansi_attrib(struct fx_tty *tty, const char *s) { if (tty->t_mcount > 0) { fputs(";", tty->t_out); @@ -271,46 +271,46 @@ static void put_ansi_attrib(struct b_tty *tty, const char *s) } static void set_attrib( - struct b_tty *tty, enum b_tty_attrib old, enum b_tty_attrib new) + struct fx_tty *tty, enum fx_tty_attrib old, enum fx_tty_attrib new) { if (old == new) { return; } /* Bold ON */ - if (!(old & B_TTY_ATTRIB_BOLD) && new &B_TTY_ATTRIB_BOLD) { + if (!(old & FX_TTY_ATTRIB_BOLD) && new &FX_TTY_ATTRIB_BOLD) { put_ansi_attrib(tty, ANSI_BOLD_ON); } /* Bold OFF */ - if (old & B_TTY_ATTRIB_BOLD && !(new &B_TTY_ATTRIB_BOLD)) { + if (old & FX_TTY_ATTRIB_BOLD && !(new &FX_TTY_ATTRIB_BOLD)) { put_ansi_attrib(tty, ANSI_BOLD_OFF); } /* Underline ON */ - if (!(old & B_TTY_ATTRIB_UNDERLINE) && new &B_TTY_ATTRIB_UNDERLINE) { + if (!(old & FX_TTY_ATTRIB_UNDERLINE) && new &FX_TTY_ATTRIB_UNDERLINE) { put_ansi_attrib(tty, ANSI_UNDERLINE_ON); } /* Underline OFF */ - if (old & B_TTY_ATTRIB_UNDERLINE && !(new &B_TTY_ATTRIB_UNDERLINE)) { + if (old & FX_TTY_ATTRIB_UNDERLINE && !(new &FX_TTY_ATTRIB_UNDERLINE)) { put_ansi_attrib(tty, ANSI_UNDERLINE_OFF); } /* Italic ON */ - if (!(old & B_TTY_ATTRIB_ITALIC) && new &B_TTY_ATTRIB_ITALIC) { + if (!(old & FX_TTY_ATTRIB_ITALIC) && new &FX_TTY_ATTRIB_ITALIC) { put_ansi_attrib(tty, ANSI_ITALIC_ON); } /* Italic OFF */ - if (old & B_TTY_ATTRIB_ITALIC && !(new &B_TTY_ATTRIB_ITALIC)) { + if (old & FX_TTY_ATTRIB_ITALIC && !(new &FX_TTY_ATTRIB_ITALIC)) { put_ansi_attrib(tty, ANSI_ITALIC_OFF); } } static void set_fg( - struct b_tty *tty, const struct b_tty_colour *old, - const struct b_tty_colour *new) + struct fx_tty *tty, const struct fx_tty_colour *old, + const struct fx_tty_colour *new) { if (compare_colour(old, new) == 0) { return; @@ -319,18 +319,18 @@ static void set_fg( char buf[8]; switch (new->c_mode) { - case B_TTY_COLOUR_NONE: + case FX_TTY_COLOUR_NONE: put_ansi_attrib(tty, ANSI_DEFAULTCOLOUR_FG); break; - case B_TTY_COLOUR_16: + case FX_TTY_COLOUR_16: put_ansi_attrib(tty, ansi_colour16_fg[new->c_16.value]); break; - case B_TTY_COLOUR_256: + case FX_TTY_COLOUR_256: put_ansi_attrib(tty, ANSI_256COLOUR_FG); snprintf(buf, sizeof buf, "%u", new->c_256.value); put_ansi_attrib(tty, buf); break; - case B_TTY_COLOUR_TRUE: + case FX_TTY_COLOUR_TRUE: put_ansi_attrib(tty, ANSI_TRUECOLOUR_FG); snprintf(buf, sizeof buf, "%u", new->c_true.r); put_ansi_attrib(tty, buf); @@ -345,8 +345,8 @@ static void set_fg( } static void set_bg( - struct b_tty *tty, const struct b_tty_colour *old, - const struct b_tty_colour *new) + struct fx_tty *tty, const struct fx_tty_colour *old, + const struct fx_tty_colour *new) { if (compare_colour(old, new) == 0) { return; @@ -355,18 +355,18 @@ static void set_bg( char buf[8]; switch (new->c_mode) { - case B_TTY_COLOUR_NONE: + case FX_TTY_COLOUR_NONE: put_ansi_attrib(tty, ANSI_DEFAULTCOLOUR_BG); break; - case B_TTY_COLOUR_16: + case FX_TTY_COLOUR_16: put_ansi_attrib(tty, ansi_colour16_bg[new->c_16.value]); break; - case B_TTY_COLOUR_256: + case FX_TTY_COLOUR_256: put_ansi_attrib(tty, ANSI_256COLOUR_BG); snprintf(buf, sizeof buf, "%u", new->c_256.value); put_ansi_attrib(tty, buf); break; - case B_TTY_COLOUR_TRUE: + case FX_TTY_COLOUR_TRUE: put_ansi_attrib(tty, ANSI_TRUECOLOUR_BG); snprintf(buf, sizeof buf, "%u", new->c_true.r); put_ansi_attrib(tty, buf); @@ -380,7 +380,7 @@ static void set_bg( } } -void b_tty_set_vmode(struct b_tty *tty, const struct b_tty_vmode *vmode) +void fx_tty_set_vmode(struct fx_tty *tty, const struct fx_tty_vmode *vmode) { if (compare_vmode(&tty->t_vmode, vmode) == 0) { return; @@ -399,7 +399,7 @@ void b_tty_set_vmode(struct b_tty *tty, const struct b_tty_vmode *vmode) memcpy(&tty->t_vmode, vmode, sizeof *vmode); } -b_keycode b_tty_read_key(struct b_tty *tty) +fx_keycode fx_tty_read_key(struct fx_tty *tty) { char c; int v; @@ -408,19 +408,19 @@ b_keycode b_tty_read_key(struct b_tty *tty) while (1) { v = read(fd, &c, 1); if (v < 1) { - return B_KEY_EOF; + return FX_KEY_EOF; } if (c == '\r' || c == '\n') { - return B_KEY_RETURN; + return FX_KEY_RETURN; } if (c == '\b' || c == 0x7F) { - return B_KEY_BACKSPACE; + return FX_KEY_BACKSPACE; } if (c >= 1 && c <= 26) { - return B_TTY_CTRL_KEY(c + 'a' - 1); + return FX_TTY_CTRL_KEY(c + 'a' - 1); } if (c != 0x1b) { @@ -429,7 +429,7 @@ b_keycode b_tty_read_key(struct b_tty *tty) v = read(fd, &c, 1); if (v < 1) { - return B_KEY_EOF; + return FX_KEY_EOF; } if (c != '[') { @@ -438,18 +438,18 @@ b_keycode b_tty_read_key(struct b_tty *tty) v = read(fd, &c, 1); if (v < 1) { - return B_KEY_EOF; + return FX_KEY_EOF; } switch (c) { case 'A': - return B_KEY_ARROW_UP; + return FX_KEY_ARROW_UP; case 'B': - return B_KEY_ARROW_DOWN; + return FX_KEY_ARROW_DOWN; case 'C': - return B_KEY_ARROW_RIGHT; + return FX_KEY_ARROW_RIGHT; case 'D': - return B_KEY_ARROW_LEFT; + return FX_KEY_ARROW_LEFT; default: continue; } @@ -458,9 +458,9 @@ b_keycode b_tty_read_key(struct b_tty *tty) return c; } -void b_tty_move_cursor_x(struct b_tty *tty, enum b_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 == B_TTY_POS_CURSOR && pos < 0 && pos >= -4) { + if (base == FX_TTY_POS_CURSOR && pos < 0 && pos >= -4) { for (int i = 0; i > pos; i--) { fputc('\b', tty->t_out); } @@ -468,7 +468,7 @@ void b_tty_move_cursor_x(struct b_tty *tty, enum b_tty_position_base base, int p return; } - if (base == B_TTY_POS_START) { + if (base == FX_TTY_POS_START) { if (pos == 0) { fputs("\033[G", tty->t_out); } else { @@ -487,9 +487,9 @@ void b_tty_move_cursor_x(struct b_tty *tty, enum b_tty_position_base base, int p } } -void b_tty_move_cursor_y(struct b_tty *tty, enum b_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 == B_TTY_POS_START) { + if (base == FX_TTY_POS_START) { /* we don't need this functionality right now */ abort(); } @@ -505,32 +505,32 @@ void b_tty_move_cursor_y(struct b_tty *tty, enum b_tty_position_base base, int p } } -void b_tty_clear(struct b_tty *tty, enum b_tty_clear_mode mode) +void fx_tty_clear(struct fx_tty *tty, enum fx_tty_clear_mode mode) { const char *arg; - if (mode & B_TTY_CLEAR_ALL) { + if (mode & FX_TTY_CLEAR_ALL) { arg = "2"; - } else if (mode & B_TTY_CLEAR_TO_CURSOR) { + } else if (mode & FX_TTY_CLEAR_TO_CURSOR) { arg = "1"; - } else if (mode & B_TTY_CLEAR_FROM_CURSOR) { + } else if (mode & FX_TTY_CLEAR_FROM_CURSOR) { arg = ""; } else { abort(); } - if (mode & B_TTY_CLEAR_SCREEN) { + if (mode & FX_TTY_CLEAR_SCREEN) { fprintf(tty->t_out, "\033[%sJ", arg); - } else if (mode & B_TTY_CLEAR_LINE) { + } else if (mode & FX_TTY_CLEAR_LINE) { fprintf(tty->t_out, "\033[%sK", arg); } else { abort(); } } -enum b_status b_tty_get_dimensions( - struct b_tty *tty, unsigned int *w, unsigned int *h) +enum fx_status fx_tty_get_dimensions( + struct fx_tty *tty, unsigned int *w, unsigned int *h) { - if (!(tty->t_flags & B_TTY_INTERACTIVE)) { + if (!(tty->t_flags & FX_TTY_INTERACTIVE)) { return -1; } diff --git a/term/sys/windows/print.c b/term/sys/windows/print.c index 4aa5544..ee578d4 100644 --- a/term/sys/windows/print.c +++ b/term/sys/windows/print.c @@ -4,7 +4,7 @@ #include #include -int z__b_stream_is_tty(FILE *fp) +int z__fx_stream_is_tty(FILE *fp) { CONSOLE_SCREEN_BUFFER_INFO csbi; HANDLE console = (HANDLE)(INT_PTR)_get_osfhandle(fileno(fp)); @@ -13,7 +13,7 @@ int z__b_stream_is_tty(FILE *fp) return status == TRUE ? 1 : 0; } -int z__b_stream_dimensions(FILE *fp, unsigned int *w, unsigned int *h) +int z__fx_stream_dimensions(FILE *fp, unsigned int *w, unsigned int *h) { CONSOLE_SCREEN_BUFFER_INFO csbi; HANDLE console = (HANDLE)(INT_PTR)_get_osfhandle(fileno(fp)); @@ -34,7 +34,7 @@ int z__b_stream_dimensions(FILE *fp, unsigned int *w, unsigned int *h) return 0; } -int z__b_stream_cursorpos(FILE *in, FILE *out, unsigned int *x, unsigned int *y) +int z__fx_stream_cursorpos(FILE *in, FILE *out, unsigned int *x, unsigned int *y) { CONSOLE_SCREEN_BUFFER_INFO csbi; HANDLE console = (HANDLE)(INT_PTR)_get_osfhandle(fileno(in)); @@ -67,7 +67,7 @@ int z__b_stream_cursorpos(FILE *in, FILE *out, unsigned int *x, unsigned int *y) (attrib) &= ~(y); \ } -int z__b_stream_set_modifier(FILE *fp, unsigned int mod) +int z__fx_stream_set_modifier(FILE *fp, unsigned int mod) { WORD attrib = 0; CONSOLE_SCREEN_BUFFER_INFO csbi; @@ -81,52 +81,52 @@ int z__b_stream_set_modifier(FILE *fp, unsigned int mod) attrib = csbi.wAttributes; - if (mod & Z__B_STREAM_MOD_RESET) { - attrib = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; + if (mod & Z__FX_STREAM_MOD_RESET) { + attrib = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_FX; SetConsoleTextAttribute(console, attrib); return 0; } - if (Z__B_STREAM_MOD_GET_FG_COLOUR(mod) != 0) { - if (mod & Z__B_STREAM_MOD_BLACK) { + if (Z__FX_STREAM_MOD_GET_FG_COLOUR(mod) != 0) { + if (mod & Z__FX_STREAM_MOD_BLACK) { attrib &= ~(FOREGROUND_RED | FOREGROUND_GREEN - | FOREGROUND_BLUE); + | FOREGROUND_FX); } else { APPLY_FLAG_X( - mod, attrib, Z__B_STREAM_MOD_RED, FOREGROUND_RED); + mod, attrib, Z__FX_STREAM_MOD_RED, FOREGROUND_RED); APPLY_FLAG_X( - mod, attrib, Z__B_STREAM_MOD_GREEN, + mod, attrib, Z__FX_STREAM_MOD_GREEN, FOREGROUND_GREEN); APPLY_FLAG_X( - mod, attrib, Z__B_STREAM_MOD_BLUE, FOREGROUND_BLUE); + mod, attrib, Z__FX_STREAM_MOD_FX, FOREGROUND_FX); } } - if (Z__B_STREAM_MOD_GET_BG_COLOUR(mod) != 0) { - if (mod & Z__B_STREAM_MOD_BG_BLACK) { + if (Z__FX_STREAM_MOD_GET_BG_COLOUR(mod) != 0) { + if (mod & Z__FX_STREAM_MOD_BG_BLACK) { attrib &= ~(BACKGROUND_RED | BACKGROUND_GREEN - | BACKGROUND_BLUE); + | BACKGROUND_FX); } else { APPLY_FLAG_X( - mod, attrib, Z__B_STREAM_MOD_BG_RED, + mod, attrib, Z__FX_STREAM_MOD_BG_RED, BACKGROUND_RED); APPLY_FLAG_X( - mod, attrib, Z__B_STREAM_MOD_BG_GREEN, + mod, attrib, Z__FX_STREAM_MOD_BG_GREEN, BACKGROUND_GREEN); APPLY_FLAG_X( - mod, attrib, Z__B_STREAM_MOD_BG_BLUE, - BACKGROUND_BLUE); + mod, attrib, Z__FX_STREAM_MOD_BG_FX, + BACKGROUND_FX); } } - APPLY_FLAG(mod, attrib, Z__B_STREAM_MOD_ULINE, COMMON_LVB_UNDERSCORE); - APPLY_FLAG(mod, attrib, Z__B_STREAM_MOD_INVERT, COMMON_LVB_REVERSE_VIDEO); + APPLY_FLAG(mod, attrib, Z__FX_STREAM_MOD_ULINE, COMMON_LVB_UNDERSCORE); + APPLY_FLAG(mod, attrib, Z__FX_STREAM_MOD_INVERT, COMMON_LVB_REVERSE_VIDEO); APPLY_FLAG( - mod, attrib, Z__B_STREAM_MOD_BRIGHT | Z__B_STREAM_MOD_BOLD, + mod, attrib, Z__FX_STREAM_MOD_BRIGHT | Z__FX_STREAM_MOD_BOLD, FOREGROUND_INTENSITY); - APPLY_FLAG(mod, attrib, Z__B_STREAM_MOD_BG_BRIGHT, BACKGROUND_INTENSITY); + APPLY_FLAG(mod, attrib, Z__FX_STREAM_MOD_BG_BRIGHT, BACKGROUND_INTENSITY); SetConsoleTextAttribute(console, attrib); return 0; diff --git a/term/sys/windows/tty.c b/term/sys/windows/tty.c index 396113b..3129078 100644 --- a/term/sys/windows/tty.c +++ b/term/sys/windows/tty.c @@ -1,4 +1,4 @@ -#include +#include #include "../../tty.h" #include #include @@ -23,62 +23,62 @@ #define ANSI_TRUECOLOUR_BG "48;2" enum tty_flags { - B_TTY_INIT = 0x01u, - B_TTY_INTERACTIVE = 0x02u, + FX_TTY_INIT = 0x01u, + FX_TTY_INTERACTIVE = 0x02u, }; -struct b_tty { +struct fx_tty { HANDLE t_in, t_out; DWORD t_canon_mode_in, t_canon_mode_out; enum s_key_code t_repeat_key; unsigned int t_repeat_count; enum tty_flags t_flags; - struct b_tty_vmode t_vmode; + struct fx_tty_vmode t_vmode; struct tty_format_buf t_format_buf; }; -static struct b_tty std = {0}; -static struct b_tty err = {0}; +static struct fx_tty std = {0}; +static struct fx_tty err = {0}; static WORD ansi_colour16_fg[] = { - [B_TTY_COLOUR16_BLACK] = 0, - [B_TTY_COLOUR16_RED] = FOREGROUND_RED, - [B_TTY_COLOUR16_GREEN] = FOREGROUND_GREEN, - [B_TTY_COLOUR16_YELLOW] = FOREGROUND_RED | FOREGROUND_GREEN, - [B_TTY_COLOUR16_BLUE] = FOREGROUND_BLUE, - [B_TTY_COLOUR16_MAGENTA] = FOREGROUND_RED | FOREGROUND_BLUE, - [B_TTY_COLOUR16_CYAN] = FOREGROUND_GREEN | FOREGROUND_BLUE, - [B_TTY_COLOUR16_WHITE] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, - [B_TTY_COLOUR16_BRIGHT_BLACK] = FOREGROUND_INTENSITY, - [B_TTY_COLOUR16_BRIGHT_RED] = FOREGROUND_RED | FOREGROUND_INTENSITY, - [B_TTY_COLOUR16_BRIGHT_GREEN] = FOREGROUND_GREEN | FOREGROUND_INTENSITY, - [B_TTY_COLOUR16_BRIGHT_YELLOW] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY, - [B_TTY_COLOUR16_BRIGHT_BLUE] = FOREGROUND_BLUE | FOREGROUND_INTENSITY, - [B_TTY_COLOUR16_BRIGHT_MAGENTA] = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY, - [B_TTY_COLOUR16_BRIGHT_CYAN] = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY, - [B_TTY_COLOUR16_BRIGHT_WHITE] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY, + [FX_TTY_COLOUR16_BLACK] = 0, + [FX_TTY_COLOUR16_RED] = FOREGROUND_RED, + [FX_TTY_COLOUR16_GREEN] = FOREGROUND_GREEN, + [FX_TTY_COLOUR16_YELLOW] = FOREGROUND_RED | FOREGROUND_GREEN, + [FX_TTY_COLOUR16_FX] = FOREGROUND_FX, + [FX_TTY_COLOUR16_MAGENTA] = FOREGROUND_RED | FOREGROUND_FX, + [FX_TTY_COLOUR16_CYAN] = FOREGROUND_GREEN | FOREGROUND_FX, + [FX_TTY_COLOUR16_WHITE] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_FX, + [FX_TTY_COLOUR16_BRIGHT_BLACK] = FOREGROUND_INTENSITY, + [FX_TTY_COLOUR16_BRIGHT_RED] = FOREGROUND_RED | FOREGROUND_INTENSITY, + [FX_TTY_COLOUR16_BRIGHT_GREEN] = FOREGROUND_GREEN | FOREGROUND_INTENSITY, + [FX_TTY_COLOUR16_BRIGHT_YELLOW] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY, + [FX_TTY_COLOUR16_BRIGHT_FX] = FOREGROUND_FX | FOREGROUND_INTENSITY, + [FX_TTY_COLOUR16_BRIGHT_MAGENTA] = FOREGROUND_RED | FOREGROUND_FX | FOREGROUND_INTENSITY, + [FX_TTY_COLOUR16_BRIGHT_CYAN] = FOREGROUND_GREEN | FOREGROUND_FX | FOREGROUND_INTENSITY, + [FX_TTY_COLOUR16_BRIGHT_WHITE] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_FX | FOREGROUND_INTENSITY, }; static WORD ansi_colour16_bg[] = { - [B_TTY_COLOUR16_BLACK] = 0, - [B_TTY_COLOUR16_RED] = BACKGROUND_RED, - [B_TTY_COLOUR16_GREEN] = BACKGROUND_GREEN, - [B_TTY_COLOUR16_YELLOW] = BACKGROUND_RED | BACKGROUND_GREEN, - [B_TTY_COLOUR16_BLUE] = BACKGROUND_BLUE, - [B_TTY_COLOUR16_MAGENTA] = BACKGROUND_RED | BACKGROUND_BLUE, - [B_TTY_COLOUR16_CYAN] = BACKGROUND_GREEN | BACKGROUND_BLUE, - [B_TTY_COLOUR16_WHITE] = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE, - [B_TTY_COLOUR16_BRIGHT_BLACK] = BACKGROUND_INTENSITY, - [B_TTY_COLOUR16_BRIGHT_RED] = BACKGROUND_RED | BACKGROUND_INTENSITY, - [B_TTY_COLOUR16_BRIGHT_GREEN] = BACKGROUND_GREEN | BACKGROUND_INTENSITY, - [B_TTY_COLOUR16_BRIGHT_YELLOW] = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_INTENSITY, - [B_TTY_COLOUR16_BRIGHT_BLUE] = BACKGROUND_BLUE | BACKGROUND_INTENSITY, - [B_TTY_COLOUR16_BRIGHT_MAGENTA] = BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_INTENSITY, - [B_TTY_COLOUR16_BRIGHT_CYAN] = BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY, - [B_TTY_COLOUR16_BRIGHT_WHITE] = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY, + [FX_TTY_COLOUR16_BLACK] = 0, + [FX_TTY_COLOUR16_RED] = BACKGROUND_RED, + [FX_TTY_COLOUR16_GREEN] = BACKGROUND_GREEN, + [FX_TTY_COLOUR16_YELLOW] = BACKGROUND_RED | BACKGROUND_GREEN, + [FX_TTY_COLOUR16_FX] = BACKGROUND_FX, + [FX_TTY_COLOUR16_MAGENTA] = BACKGROUND_RED | BACKGROUND_FX, + [FX_TTY_COLOUR16_CYAN] = BACKGROUND_GREEN | BACKGROUND_FX, + [FX_TTY_COLOUR16_WHITE] = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_FX, + [FX_TTY_COLOUR16_BRIGHT_BLACK] = BACKGROUND_INTENSITY, + [FX_TTY_COLOUR16_BRIGHT_RED] = BACKGROUND_RED | BACKGROUND_INTENSITY, + [FX_TTY_COLOUR16_BRIGHT_GREEN] = BACKGROUND_GREEN | BACKGROUND_INTENSITY, + [FX_TTY_COLOUR16_BRIGHT_YELLOW] = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_INTENSITY, + [FX_TTY_COLOUR16_BRIGHT_FX] = BACKGROUND_FX | BACKGROUND_INTENSITY, + [FX_TTY_COLOUR16_BRIGHT_MAGENTA] = BACKGROUND_RED | BACKGROUND_FX | BACKGROUND_INTENSITY, + [FX_TTY_COLOUR16_BRIGHT_CYAN] = BACKGROUND_GREEN | BACKGROUND_FX | BACKGROUND_INTENSITY, + [FX_TTY_COLOUR16_BRIGHT_WHITE] = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_FX | BACKGROUND_INTENSITY, }; -static void init_tty(struct b_tty *tty, FILE *in, FILE *out) +static void init_tty(struct fx_tty *tty, FILE *in, FILE *out) { HANDLE in_handle = INVALID_HANDLE_VALUE; HANDLE out_handle = INVALID_HANDLE_VALUE; @@ -93,7 +93,7 @@ static void init_tty(struct b_tty *tty, FILE *in, FILE *out) tty->t_in = in_handle; tty->t_out = out_handle; - tty->t_flags |= B_TTY_INIT; + tty->t_flags |= FX_TTY_INIT; CONSOLE_SCREEN_BUFFER_INFO csbi = {0}; DWORD mode = 0; @@ -101,51 +101,51 @@ static void init_tty(struct b_tty *tty, FILE *in, FILE *out) if (in && GetConsoleScreenBufferInfo(in_handle, &csbi)) { GetConsoleMode(in_handle, &mode); tty->t_canon_mode_in = mode; - tty->t_flags |= B_TTY_INTERACTIVE; + tty->t_flags |= FX_TTY_INTERACTIVE; } if (out && GetConsoleScreenBufferInfo(out_handle, &csbi)) { GetConsoleMode(out_handle, &mode); tty->t_canon_mode_out = mode; - tty->t_flags |= B_TTY_INTERACTIVE; + tty->t_flags |= FX_TTY_INTERACTIVE; } } -struct b_tty *z__b_tty_get_std(void) +struct fx_tty *z__fx_tty_get_std(void) { - if (!(std.t_flags & B_TTY_INIT)) { + if (!(std.t_flags & FX_TTY_INIT)) { init_tty(&std, stdin, stdout); } return &std; } -struct b_tty *z__b_tty_get_err(void) +struct fx_tty *z__fx_tty_get_err(void) { - if (!(err.t_flags & B_TTY_INIT)) { + if (!(err.t_flags & FX_TTY_INIT)) { init_tty(&err, NULL, stderr); } return &err; } -struct tty_format_buf *z__b_tty_get_format_buf(struct b_tty *tty) +struct tty_format_buf *z__fx_tty_get_format_buf(struct fx_tty *tty) { return &tty->t_format_buf; } -void z__b_tty_putc(struct b_tty* tty, char c) +void z__fx_tty_putc(struct fx_tty* tty, char c) { DWORD x; WriteConsoleA(tty->t_out, &c, 1, &x, NULL); } -bool b_tty_is_interactive(const struct b_tty *tty) +bool fx_tty_is_interactive(const struct fx_tty *tty) { - return (tty->t_flags & B_TTY_INTERACTIVE) == B_TTY_INTERACTIVE; + return (tty->t_flags & FX_TTY_INTERACTIVE) == FX_TTY_INTERACTIVE; } -static void tty_set_raw(struct b_tty *tty) +static void tty_set_raw(struct fx_tty *tty) { DWORD mode = tty->t_canon_mode_in; mode &= ~(ENABLE_ECHO_INPUT | ENABLE_INSERT_MODE | ENABLE_LINE_INPUT | ENABLE_QUICK_EDIT_MODE | ENABLE_WINDOW_INPUT); @@ -157,19 +157,19 @@ static void tty_set_raw(struct b_tty *tty) SetConsoleMode(tty->t_out, mode); } -static void tty_set_canon(struct b_tty *tty) +static void tty_set_canon(struct fx_tty *tty) { SetConsoleMode(tty->t_in, tty->t_canon_mode_in); SetConsoleMode(tty->t_out, tty->t_canon_mode_out); } -void b_tty_set_mode(struct b_tty* tty, enum b_tty_mode mode) +void fx_tty_set_mode(struct fx_tty* tty, enum fx_tty_mode mode) { switch (mode) { - case B_TTY_CANONICAL: + case FX_TTY_CANONICAL: tty_set_canon(tty); break; - case B_TTY_RAW: + case FX_TTY_RAW: tty_set_raw(tty); break; default: @@ -177,43 +177,43 @@ void b_tty_set_mode(struct b_tty* tty, enum b_tty_mode mode) } } -void b_tty_reset_vmode(struct b_tty *tty) +void fx_tty_reset_vmode(struct fx_tty *tty) { - if (tty->t_vmode.v_fg.c_mode == B_TTY_COLOUR_NONE - && tty->t_vmode.v_bg.c_mode == B_TTY_COLOUR_NONE - && tty->t_vmode.v_attrib == B_TTY_ATTRIB_NORMAL) { + if (tty->t_vmode.v_fg.c_mode == FX_TTY_COLOUR_NONE + && tty->t_vmode.v_bg.c_mode == FX_TTY_COLOUR_NONE + && tty->t_vmode.v_attrib == FX_TTY_ATTRIB_NORMAL) { return; } - WORD attrib = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; + WORD attrib = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_FX; SetConsoleTextAttribute(tty->t_out, attrib); memset(&tty->t_vmode, 0x0, sizeof tty->t_vmode); - tty->t_vmode.v_fg.c_mode = B_TTY_COLOUR_NONE; - tty->t_vmode.v_bg.c_mode = B_TTY_COLOUR_NONE; - tty->t_vmode.v_attrib = B_TTY_ATTRIB_NORMAL; + tty->t_vmode.v_fg.c_mode = FX_TTY_COLOUR_NONE; + tty->t_vmode.v_bg.c_mode = FX_TTY_COLOUR_NONE; + tty->t_vmode.v_attrib = FX_TTY_ATTRIB_NORMAL; } static int compare_colour( - const struct b_tty_colour *a, const struct b_tty_colour *b) + const struct fx_tty_colour *a, const struct fx_tty_colour *b) { if (a->c_mode != b->c_mode) { return -1; } switch (a->c_mode) { - case B_TTY_COLOUR_16: + case FX_TTY_COLOUR_16: if (a->c_16.value != b->c_16.value) { return -1; } break; - case B_TTY_COLOUR_256: + case FX_TTY_COLOUR_256: if (a->c_256.value != b->c_256.value) { return -1; } break; - case B_TTY_COLOUR_TRUE: + case FX_TTY_COLOUR_TRUE: if (a->c_true.r != b->c_true.r) { return -1; } @@ -233,7 +233,7 @@ static int compare_colour( return 0; } -static int compare_vmode(const struct b_tty_vmode *a, const struct b_tty_vmode *b) +static int compare_vmode(const struct fx_tty_vmode *a, const struct fx_tty_vmode *b) { if (a->v_attrib != b->v_attrib) { return -1; @@ -250,7 +250,7 @@ static int compare_vmode(const struct b_tty_vmode *a, const struct b_tty_vmode * return 0; } -static void set_attrib(WORD *attrp, enum b_tty_attrib old, enum b_tty_attrib new) +static void set_attrib(WORD *attrp, enum fx_tty_attrib old, enum fx_tty_attrib new) { if (old == new) { return; @@ -259,32 +259,32 @@ static void set_attrib(WORD *attrp, enum b_tty_attrib old, enum b_tty_attrib new WORD attrib = *attrp; /* Bold ON */ - if (!(old & B_TTY_ATTRIB_BOLD) && new & B_TTY_ATTRIB_BOLD) { + if (!(old & FX_TTY_ATTRIB_BOLD) && new & FX_TTY_ATTRIB_BOLD) { attrib |= FOREGROUND_INTENSITY; } /* Bold OFF */ - if (old & B_TTY_ATTRIB_BOLD && !(new & B_TTY_ATTRIB_BOLD)) { + if (old & FX_TTY_ATTRIB_BOLD && !(new & FX_TTY_ATTRIB_BOLD)) { attrib &= ~FOREGROUND_INTENSITY; } /* Underline ON */ - if (!(old & B_TTY_ATTRIB_UNDERLINE) && new & B_TTY_ATTRIB_UNDERLINE) { + if (!(old & FX_TTY_ATTRIB_UNDERLINE) && new & FX_TTY_ATTRIB_UNDERLINE) { attrib |= COMMON_LVB_UNDERSCORE; } /* Underline OFF */ - if (old & B_TTY_ATTRIB_UNDERLINE && !(new & B_TTY_ATTRIB_UNDERLINE)) { + if (old & FX_TTY_ATTRIB_UNDERLINE && !(new & FX_TTY_ATTRIB_UNDERLINE)) { attrib &= ~COMMON_LVB_UNDERSCORE; } /* Italic ON */ - if (!(old & B_TTY_ATTRIB_ITALIC) && new & B_TTY_ATTRIB_ITALIC) { + if (!(old & FX_TTY_ATTRIB_ITALIC) && new & FX_TTY_ATTRIB_ITALIC) { /* not supported */ } /* Italic OFF */ - if (old & B_TTY_ATTRIB_ITALIC && !(new & B_TTY_ATTRIB_ITALIC)) { + if (old & FX_TTY_ATTRIB_ITALIC && !(new & FX_TTY_ATTRIB_ITALIC)) { /* not supported */ } @@ -292,22 +292,22 @@ static void set_attrib(WORD *attrp, enum b_tty_attrib old, enum b_tty_attrib new } static void set_fg( - WORD *attrp, const struct b_tty_colour *old, - const struct b_tty_colour *new) + WORD *attrp, const struct fx_tty_colour *old, + const struct fx_tty_colour *new) { if (compare_colour(old, new) == 0) { return; } WORD attrib = *attrp; - attrib &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY); + attrib &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_FX | FOREGROUND_INTENSITY); switch (new->c_mode) { - case B_TTY_COLOUR_16: + case FX_TTY_COLOUR_16: attrib |= ansi_colour16_fg[new->c_16.value]; break; default: - attrib |= FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; + attrib |= FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_FX; break; } @@ -315,27 +315,27 @@ static void set_fg( } static void set_bg( - WORD *attrp, const struct b_tty_colour *old, - const struct b_tty_colour *new) + WORD *attrp, const struct fx_tty_colour *old, + const struct fx_tty_colour *new) { if (compare_colour(old, new) == 0) { return; } WORD attrib = *attrp; - attrib &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY); + attrib &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_FX | BACKGROUND_INTENSITY); switch (new->c_mode) { - case B_TTY_COLOUR_16: + case FX_TTY_COLOUR_16: attrib |= ansi_colour16_bg[new->c_16.value]; break; default: - attrib |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE; + attrib |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_FX; break; } } -void b_tty_set_vmode(struct b_tty *tty, const struct b_tty_vmode *vmode) +void fx_tty_set_vmode(struct fx_tty *tty, const struct fx_tty_vmode *vmode) { if (compare_vmode(&tty->t_vmode, vmode) == 0) { return; @@ -357,10 +357,10 @@ void b_tty_set_vmode(struct b_tty *tty, const struct b_tty_vmode *vmode) memcpy(&tty->t_vmode, vmode, sizeof *vmode); } -enum b_status b_tty_get_dimensions(struct b_tty* tty, unsigned int* w, unsigned int* h) +enum fx_status fx_tty_get_dimensions(struct fx_tty* tty, unsigned int* w, unsigned int* h) { - if (!(tty->t_flags & B_TTY_INTERACTIVE)) { - return B_ERR_NOT_SUPPORTED; + if (!(tty->t_flags & FX_TTY_INTERACTIVE)) { + return FX_ERR_NOT_SUPPORTED; } CONSOLE_SCREEN_BUFFER_INFO csbi = {0}; @@ -374,13 +374,13 @@ enum b_status b_tty_get_dimensions(struct b_tty* tty, unsigned int* w, unsigned *h = csbi.srWindow.Bottom - csbi.srWindow.Top; } - return B_SUCCESS; + return FX_SUCCESS; } -enum b_status b_tty_get_cursor_position(struct b_tty *tty, unsigned int *x, unsigned int *y) +enum fx_status fx_tty_get_cursor_position(struct fx_tty *tty, unsigned int *x, unsigned int *y) { - if (!(tty->t_flags & B_TTY_INTERACTIVE)) { - return B_ERR_NOT_SUPPORTED; + if (!(tty->t_flags & FX_TTY_INTERACTIVE)) { + return FX_ERR_NOT_SUPPORTED; } CONSOLE_SCREEN_BUFFER_INFO csbi = {0}; @@ -394,10 +394,10 @@ enum b_status b_tty_get_cursor_position(struct b_tty *tty, unsigned int *x, unsi *y = csbi.dwCursorPosition.Y; } - return B_SUCCESS; + return FX_SUCCESS; } -b_keycode b_tty_read_key(struct b_tty *tty) +fx_keycode fx_tty_read_key(struct fx_tty *tty) { if (tty->t_repeat_count > 0) { tty->t_repeat_count--; @@ -413,7 +413,7 @@ b_keycode b_tty_read_key(struct b_tty *tty) while (1) { status = ReadConsoleInputA(in, &d, 1, &nr_read); if (status == FALSE) { - return B_KEY_EOF; + return FX_KEY_EOF; } if (d.EventType != KEY_EVENT) { @@ -424,7 +424,7 @@ b_keycode b_tty_read_key(struct b_tty *tty) continue; } - b_keycode key = 0; + fx_keycode key = 0; switch (d.Event.KeyEvent.wVirtualKeyCode) { case VK_CONTROL: @@ -434,22 +434,22 @@ b_keycode b_tty_read_key(struct b_tty *tty) case VK_MENU: continue; case VK_UP: - key = B_KEY_ARROW_UP; + key = FX_KEY_ARROW_UP; break; case VK_DOWN: - key = B_KEY_ARROW_DOWN; + key = FX_KEY_ARROW_DOWN; break; case VK_LEFT: - key = B_KEY_ARROW_LEFT; + key = FX_KEY_ARROW_LEFT; break; case VK_RIGHT: - key = B_KEY_ARROW_RIGHT; + key = FX_KEY_ARROW_RIGHT; break; case VK_BACK: - key = B_KEY_BACKSPACE; + key = FX_KEY_BACKSPACE; break; case VK_RETURN: - key = B_KEY_RETURN; + key = FX_KEY_RETURN; break; default: if (d.Event.KeyEvent.uChar.UnicodeChar == 0) { @@ -461,7 +461,7 @@ b_keycode b_tty_read_key(struct b_tty *tty) } if (d.Event.KeyEvent.dwControlKeyState & LEFT_CTRL_PRESSED) { - key = B_TTY_CTRL_KEY('a' + key - 1); + key = FX_TTY_CTRL_KEY('a' + key - 1); } if (d.Event.KeyEvent.wRepeatCount > 1) { @@ -472,11 +472,11 @@ b_keycode b_tty_read_key(struct b_tty *tty) return key; } - return B_KEY_EOF; + return FX_KEY_EOF; } -void b_tty_move_cursor_x( - struct b_tty* tty, enum b_tty_position_base base, int pos) +void fx_tty_move_cursor_x( + struct fx_tty* tty, enum fx_tty_position_base base, int pos) { CONSOLE_SCREEN_BUFFER_INFO console = {0}; GetConsoleScreenBufferInfo(tty->t_out, &console); @@ -484,10 +484,10 @@ void b_tty_move_cursor_x( cursor_pos.Y = console.dwCursorPosition.Y; switch (base) { - case B_TTY_POS_CURSOR: + case FX_TTY_POS_CURSOR: cursor_pos.X = console.dwCursorPosition.X + pos; break; - case B_TTY_POS_START: + case FX_TTY_POS_START: cursor_pos.X = pos; break; default: @@ -497,7 +497,7 @@ void b_tty_move_cursor_x( SetConsoleCursorPosition(tty->t_out, cursor_pos); } -void b_tty_move_cursor_y(struct b_tty *tty, enum b_tty_position_base base, int pos) +void fx_tty_move_cursor_y(struct fx_tty *tty, enum fx_tty_position_base base, int pos) { CONSOLE_SCREEN_BUFFER_INFO console = {0}; GetConsoleScreenBufferInfo(tty->t_out, &console); @@ -505,10 +505,10 @@ void b_tty_move_cursor_y(struct b_tty *tty, enum b_tty_position_base base, int p cursor_pos.X = console.dwCursorPosition.X; switch (base) { - case B_TTY_POS_CURSOR: + case FX_TTY_POS_CURSOR: cursor_pos.Y = console.dwCursorPosition.Y + pos; break; - case B_TTY_POS_START: + case FX_TTY_POS_START: cursor_pos.Y = pos; break; default: @@ -518,7 +518,7 @@ void b_tty_move_cursor_y(struct b_tty *tty, enum b_tty_position_base base, int p SetConsoleCursorPosition(tty->t_out, cursor_pos); } -void b_tty_clear(struct b_tty *tty, enum b_tty_clear_mode mode) +void fx_tty_clear(struct fx_tty *tty, enum fx_tty_clear_mode mode) { CONSOLE_SCREEN_BUFFER_INFO console = {0}; GetConsoleScreenBufferInfo(tty->t_out, &console); @@ -529,13 +529,13 @@ void b_tty_clear(struct b_tty *tty, enum b_tty_clear_mode mode) DWORD all_length = 0, line_length = 0; - if (mode & B_TTY_CLEAR_ALL) { + if (mode & FX_TTY_CLEAR_ALL) { line_length = console.dwSize.X; all_length = line_length * console.dwSize.Y; - } else if (mode & B_TTY_CLEAR_FROM_CURSOR) { + } else if (mode & FX_TTY_CLEAR_FROM_CURSOR) { line_length = console.dwSize.X - console.dwCursorPosition.X + 1; all_length = line_length + ((console.dwSize.Y - console.dwCursorPosition.Y) * console.dwSize.X); - } else if (mode & B_TTY_CLEAR_TO_CURSOR) { + } else if (mode & FX_TTY_CLEAR_TO_CURSOR) { line_length = console.dwCursorPosition.X; all_length = line_length + ((console.dwCursorPosition.Y - 1) * console.dwSize.X); @@ -543,22 +543,22 @@ void b_tty_clear(struct b_tty *tty, enum b_tty_clear_mode mode) abort(); } - if (mode & B_TTY_CLEAR_SCREEN) { + if (mode & FX_TTY_CLEAR_SCREEN) { length = all_length; - if ((mode & B_TTY_CLEAR_ALL) || (mode & B_TTY_CLEAR_TO_CURSOR)) { + if ((mode & FX_TTY_CLEAR_ALL) || (mode & FX_TTY_CLEAR_TO_CURSOR)) { start.X = 0; start.Y = 0; - } else if (mode & B_TTY_CLEAR_FROM_CURSOR) { + } else if (mode & FX_TTY_CLEAR_FROM_CURSOR) { start = console.dwCursorPosition; } - } else if (mode & B_TTY_CLEAR_LINE) { + } else if (mode & FX_TTY_CLEAR_LINE) { length = line_length; - if ((mode & B_TTY_CLEAR_ALL) || (mode & B_TTY_CLEAR_TO_CURSOR)) { + if ((mode & FX_TTY_CLEAR_ALL) || (mode & FX_TTY_CLEAR_TO_CURSOR)) { start.X = 0; start.Y = console.dwCursorPosition.Y; - } else if (mode & B_TTY_CLEAR_FROM_CURSOR) { + } else if (mode & FX_TTY_CLEAR_FROM_CURSOR) { start = console.dwCursorPosition; } } else { diff --git a/term/tty.c b/term/tty.c index 05d1cb0..019a0d1 100644 --- a/term/tty.c +++ b/term/tty.c @@ -1,14 +1,14 @@ #include "tty.h" -#include -#include +#include +#include #include #define MOD_HASH_BLACK 0x4b5dd0abbc6fc1e4 #define MOD_HASH_RED 0x89e9be1960f4c21c #define MOD_HASH_GREEN 0x0f40f029637fecbc #define MOD_HASH_YELLOW 0x8346a574925e75a9 -#define MOD_HASH_BLUE 0xc5ccd29bc2dda64d +#define MOD_HASH_FX 0xc5ccd29bc2dda64d #define MOD_HASH_MAGENTA 0x6c90e772edbc8708 #define MOD_HASH_CYAN 0x70ae2e90c1bce27a #define MOD_HASH_WHITE 0xced973885856e206 @@ -17,7 +17,7 @@ #define MOD_HASH_BRIGHT_RED 0xbad8e3fe841b9385 #define MOD_HASH_BRIGHT_GREEN 0x11cc5e579bdd2fb9 #define MOD_HASH_BRIGHT_YELLOW 0xfd579007fe8579f6 -#define MOD_HASH_BRIGHT_BLUE 0x57c76bf18badb6d6 +#define MOD_HASH_BRIGHT_FX 0x57c76bf18badb6d6 #define MOD_HASH_BRIGHT_MAGENTA 0xf6ecc6d3fdfec129 #define MOD_HASH_BRIGHT_CYAN 0x03df73fd4e12ec6d #define MOD_HASH_BRIGHT_WHITE 0xb5ebc3323f57d7fb @@ -26,7 +26,7 @@ #define MOD_HASH_BG_RED 0x145b1e4366c7d7aa #define MOD_HASH_BG_GREEN 0xa00b8541d3b1e55a #define MOD_HASH_BG_YELLOW 0x98b030fd86e3b3cf -#define MOD_HASH_BG_BLUE 0xa15529109506b5df +#define MOD_HASH_BG_FX 0xa15529109506b5df #define MOD_HASH_BG_MAGENTA 0x86dbda99bcc86222 #define MOD_HASH_BG_CYAN 0xf16a3104cf61a098 #define MOD_HASH_BG_WHITE 0x3408c46ab5836674 @@ -35,7 +35,7 @@ #define MOD_HASH_BRIGHT_BG_RED 0x144f5dc138087701 #define MOD_HASH_BRIGHT_BG_GREEN 0xc4d88c6426ffe355 #define MOD_HASH_BRIGHT_BG_YELLOW 0xf7bb000a4a792602 -#define MOD_HASH_BRIGHT_BG_BLUE 0x9b5c16d6807a1002 +#define MOD_HASH_BRIGHT_BG_FX 0x9b5c16d6807a1002 #define MOD_HASH_BRIGHT_BG_MAGENTA 0xc59fb2196cdba3fd #define MOD_HASH_BRIGHT_BG_CYAN 0x46feb6dc999a6f09 #define MOD_HASH_BRIGHT_BG_WHITE 0xa3e7d1da08826f5f @@ -51,206 +51,206 @@ static void apply_code_to_vmode(struct tty_format_buf *fmt) { const char *modifier = fmt->buf; - uint64_t mod_hash = b_hash_cstr(modifier); + uint64_t mod_hash = fx_hash_cstr(modifier); if (COMPARE_MOD_NAME(modifier, "black", mod_hash, MOD_HASH_BLACK)) { - fmt->vmode.v_fg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_fg.c_16.value = B_TTY_COLOUR16_BLACK; + fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_BLACK; } if (COMPARE_MOD_NAME(modifier, "red", mod_hash, MOD_HASH_RED)) { - fmt->vmode.v_fg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_fg.c_16.value = B_TTY_COLOUR16_RED; + fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_RED; } if (COMPARE_MOD_NAME(modifier, "green", mod_hash, MOD_HASH_GREEN)) { - fmt->vmode.v_fg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_fg.c_16.value = B_TTY_COLOUR16_GREEN; + fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_GREEN; } if (COMPARE_MOD_NAME(modifier, "yellow", mod_hash, MOD_HASH_YELLOW)) { - fmt->vmode.v_fg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_fg.c_16.value = B_TTY_COLOUR16_YELLOW; + fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_YELLOW; } - if (COMPARE_MOD_NAME(modifier, "blue", mod_hash, MOD_HASH_BLUE)) { - fmt->vmode.v_fg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_fg.c_16.value = B_TTY_COLOUR16_BLUE; + if (COMPARE_MOD_NAME(modifier, "blue", mod_hash, MOD_HASH_FX)) { + fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_FX; } if (COMPARE_MOD_NAME(modifier, "magenta", mod_hash, MOD_HASH_MAGENTA)) { - fmt->vmode.v_fg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_fg.c_16.value = B_TTY_COLOUR16_MAGENTA; + fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_MAGENTA; } if (COMPARE_MOD_NAME(modifier, "cyan", mod_hash, MOD_HASH_CYAN)) { - fmt->vmode.v_fg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_fg.c_16.value = B_TTY_COLOUR16_CYAN; + fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_CYAN; } if (COMPARE_MOD_NAME(modifier, "white", mod_hash, MOD_HASH_WHITE)) { - fmt->vmode.v_fg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_fg.c_16.value = B_TTY_COLOUR16_WHITE; + fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_WHITE; } if (COMPARE_MOD_NAME(modifier, "dark_grey", mod_hash, MOD_HASH_DARK_GREY)) { - fmt->vmode.v_fg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_fg.c_16.value = B_TTY_COLOUR16_BRIGHT_BLACK; + fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_BRIGHT_BLACK; } if (COMPARE_MOD_NAME(modifier, "bright_red", mod_hash, MOD_HASH_BRIGHT_RED)) { - fmt->vmode.v_fg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_fg.c_16.value = B_TTY_COLOUR16_BRIGHT_RED; + fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_BRIGHT_RED; } if (COMPARE_MOD_NAME( modifier, "bright_green", mod_hash, MOD_HASH_BRIGHT_GREEN)) { - fmt->vmode.v_fg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_fg.c_16.value = B_TTY_COLOUR16_BRIGHT_GREEN; + fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_BRIGHT_GREEN; } if (COMPARE_MOD_NAME( modifier, "bright_yellow", mod_hash, MOD_HASH_BRIGHT_YELLOW)) { - fmt->vmode.v_fg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_fg.c_16.value = B_TTY_COLOUR16_BRIGHT_YELLOW; + fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_BRIGHT_YELLOW; } if (COMPARE_MOD_NAME( - modifier, "bright_blue", mod_hash, MOD_HASH_BRIGHT_BLUE)) { - fmt->vmode.v_fg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_fg.c_16.value = B_TTY_COLOUR16_BRIGHT_BLUE; + modifier, "bright_blue", mod_hash, MOD_HASH_BRIGHT_FX)) { + fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_BRIGHT_FX; } if (COMPARE_MOD_NAME( modifier, "bright_magenta", mod_hash, MOD_HASH_BRIGHT_MAGENTA)) { - fmt->vmode.v_fg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_fg.c_16.value = B_TTY_COLOUR16_BRIGHT_MAGENTA; + fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_BRIGHT_MAGENTA; } if (COMPARE_MOD_NAME( modifier, "bright_cyan", mod_hash, MOD_HASH_BRIGHT_CYAN)) { - fmt->vmode.v_fg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_fg.c_16.value = B_TTY_COLOUR16_BRIGHT_CYAN; + fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_BRIGHT_CYAN; } if (COMPARE_MOD_NAME( modifier, "bright_white", mod_hash, MOD_HASH_BRIGHT_WHITE)) { - fmt->vmode.v_fg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_fg.c_16.value = B_TTY_COLOUR16_BRIGHT_WHITE; + fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_BRIGHT_WHITE; } if (COMPARE_MOD_NAME(modifier, "bg_black", mod_hash, MOD_HASH_BG_BLACK)) { - fmt->vmode.v_bg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_bg.c_16.value = B_TTY_COLOUR16_BLACK; + fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_BLACK; } if (COMPARE_MOD_NAME(modifier, "bg_red", mod_hash, MOD_HASH_BG_RED)) { - fmt->vmode.v_bg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_bg.c_16.value = B_TTY_COLOUR16_RED; + fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_RED; } if (COMPARE_MOD_NAME(modifier, "bg_green", mod_hash, MOD_HASH_BG_GREEN)) { - fmt->vmode.v_bg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_bg.c_16.value = B_TTY_COLOUR16_GREEN; + fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_GREEN; } if (COMPARE_MOD_NAME(modifier, "bg_yellow", mod_hash, MOD_HASH_BG_YELLOW)) { - fmt->vmode.v_bg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_bg.c_16.value = B_TTY_COLOUR16_YELLOW; + fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_YELLOW; } - if (COMPARE_MOD_NAME(modifier, "bg_blue", mod_hash, MOD_HASH_BG_BLUE)) { - fmt->vmode.v_bg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_bg.c_16.value = B_TTY_COLOUR16_BLUE; + if (COMPARE_MOD_NAME(modifier, "bg_blue", mod_hash, MOD_HASH_BG_FX)) { + fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_FX; } if (COMPARE_MOD_NAME(modifier, "bg_magenta", mod_hash, MOD_HASH_BG_MAGENTA)) { - fmt->vmode.v_bg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_bg.c_16.value = B_TTY_COLOUR16_MAGENTA; + fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_MAGENTA; } if (COMPARE_MOD_NAME(modifier, "bg_cyan", mod_hash, MOD_HASH_BG_CYAN)) { - fmt->vmode.v_bg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_bg.c_16.value = B_TTY_COLOUR16_CYAN; + fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_CYAN; } if (COMPARE_MOD_NAME(modifier, "bg_white", mod_hash, MOD_HASH_BG_WHITE)) { - fmt->vmode.v_bg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_bg.c_16.value = B_TTY_COLOUR16_WHITE; + fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_WHITE; } if (COMPARE_MOD_NAME( modifier, "bg_dark_grey", mod_hash, MOD_HASH_BG_DARK_GREY)) { - fmt->vmode.v_bg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_bg.c_16.value = B_TTY_COLOUR16_BRIGHT_BLACK; + fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_BRIGHT_BLACK; } if (COMPARE_MOD_NAME( modifier, "bright_bg_red", mod_hash, MOD_HASH_BRIGHT_BG_RED)) { - fmt->vmode.v_bg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_bg.c_16.value = B_TTY_COLOUR16_BRIGHT_RED; + fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_BRIGHT_RED; } if (COMPARE_MOD_NAME( modifier, "bright_bg_green", mod_hash, MOD_HASH_BRIGHT_BG_GREEN)) { - fmt->vmode.v_bg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_bg.c_16.value = B_TTY_COLOUR16_BRIGHT_GREEN; + fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_BRIGHT_GREEN; } if (COMPARE_MOD_NAME( modifier, "bright_bg_yellow", mod_hash, MOD_HASH_BRIGHT_BG_YELLOW)) { - fmt->vmode.v_bg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_bg.c_16.value = B_TTY_COLOUR16_BRIGHT_YELLOW; + fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_BRIGHT_YELLOW; } if (COMPARE_MOD_NAME( - modifier, "bright_bg_blue", mod_hash, MOD_HASH_BRIGHT_BG_BLUE)) { - fmt->vmode.v_bg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_bg.c_16.value = B_TTY_COLOUR16_BRIGHT_BLUE; + modifier, "bright_bg_blue", mod_hash, MOD_HASH_BRIGHT_BG_FX)) { + fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_BRIGHT_FX; } if (COMPARE_MOD_NAME( modifier, "bright_bg_magenta", mod_hash, MOD_HASH_BRIGHT_BG_MAGENTA)) { - fmt->vmode.v_bg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_bg.c_16.value = B_TTY_COLOUR16_BRIGHT_MAGENTA; + fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_BRIGHT_MAGENTA; } if (COMPARE_MOD_NAME( modifier, "bright_bg_cyan", mod_hash, MOD_HASH_BRIGHT_BG_CYAN)) { - fmt->vmode.v_bg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_bg.c_16.value = B_TTY_COLOUR16_BRIGHT_CYAN; + fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_BRIGHT_CYAN; } if (COMPARE_MOD_NAME( modifier, "bright_bg_white", mod_hash, MOD_HASH_BRIGHT_BG_WHITE)) { - fmt->vmode.v_bg.c_mode = B_TTY_COLOUR_16; - fmt->vmode.v_bg.c_16.value = B_TTY_COLOUR16_BRIGHT_WHITE; + fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16; + fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_BRIGHT_WHITE; } if (COMPARE_MOD_NAME(modifier, "bold", mod_hash, MOD_HASH_BOLD)) { - fmt->vmode.v_attrib |= B_TTY_ATTRIB_BOLD; + fmt->vmode.v_attrib |= FX_TTY_ATTRIB_BOLD; } if (COMPARE_MOD_NAME(modifier, "uline", mod_hash, MOD_HASH_ULINE)) { - fmt->vmode.v_attrib |= B_TTY_ATTRIB_UNDERLINE; + fmt->vmode.v_attrib |= FX_TTY_ATTRIB_UNDERLINE; } if (COMPARE_MOD_NAME(modifier, "italic", mod_hash, MOD_HASH_ITALIC)) { - fmt->vmode.v_attrib |= B_TTY_ATTRIB_ITALIC; + fmt->vmode.v_attrib |= FX_TTY_ATTRIB_ITALIC; } if (COMPARE_MOD_NAME(modifier, "invert", mod_hash, MOD_HASH_INVERT)) { - fmt->vmode.v_attrib |= B_TTY_ATTRIB_INVERT; + fmt->vmode.v_attrib |= FX_TTY_ATTRIB_INVERT; } if (COMPARE_MOD_NAME(modifier, "reset", mod_hash, MOD_HASH_RESET)) { fmt->reset_vmode = true; - fmt->vmode.v_fg.c_mode = B_TTY_COLOUR_NONE; - fmt->vmode.v_bg.c_mode = B_TTY_COLOUR_NONE; + fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_NONE; + fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_NONE; fmt->vmode.v_attrib = 0; } } @@ -270,23 +270,23 @@ static void format_buffer_clear(struct tty_format_buf *fmt) fmt->reset_vmode = false; } -static void flush_vmode(struct b_tty *tty, struct tty_format_buf *fmt) +static void flush_vmode(struct fx_tty *tty, struct tty_format_buf *fmt) { if (fmt->reset_vmode) { - b_tty_reset_vmode(tty); + fx_tty_reset_vmode(tty); } else { - b_tty_set_vmode(tty, &fmt->vmode); + fx_tty_set_vmode(tty, &fmt->vmode); } } -int b_tty_putc(struct b_tty *tty, enum b_tty_print_flags flags, char c) +int fx_tty_putc(struct fx_tty *tty, enum fx_tty_print_flags flags, char c) { - if (flags & B_TTY_DISABLE_FORMATTING) { - z__b_tty_putc(tty, c); + if (flags & FX_TTY_DISABLE_FORMATTING) { + z__fx_tty_putc(tty, c); return c; } - struct tty_format_buf *fmt = z__b_tty_get_format_buf(tty); + struct tty_format_buf *fmt = z__fx_tty_get_format_buf(tty); if (c == '[') { if (!fmt->in_format) { @@ -296,7 +296,7 @@ int b_tty_putc(struct b_tty *tty, enum b_tty_print_flags flags, char c) if (fmt->ptr == 0) { fmt->in_format = false; - z__b_tty_putc(tty, '['); + z__fx_tty_putc(tty, '['); return c; } @@ -322,29 +322,29 @@ int b_tty_putc(struct b_tty *tty, enum b_tty_print_flags flags, char c) if (fmt->in_format) { format_buffer_putc(fmt, c); } else { - z__b_tty_putc(tty, c); + z__fx_tty_putc(tty, c); } return c; } -int b_tty_puts(struct b_tty *tty, enum b_tty_print_flags flags, const char *s) +int fx_tty_puts(struct fx_tty *tty, enum fx_tty_print_flags flags, const char *s) { int r = 0; while (s[r]) { - b_tty_putc(tty, flags, s[r]); + fx_tty_putc(tty, flags, s[r]); r++; } return r; } -int b_tty_printf(struct b_tty *tty, enum b_tty_print_flags flags, const char *s, ...) +int fx_tty_printf(struct fx_tty *tty, enum fx_tty_print_flags flags, const char *s, ...) { va_list arg; va_start(arg, s); - int r = b_tty_vprintf(tty, flags, s, arg); + int r = fx_tty_vprintf(tty, flags, s, arg); va_end(arg); return r; } diff --git a/term/tty.h b/term/tty.h index f84a067..c1ddcab 100644 --- a/term/tty.h +++ b/term/tty.h @@ -1,12 +1,12 @@ #ifndef _TTY_H_ #define _TTY_H_ -#include +#include #include #define TTY_TMPBUF_SIZE 128 -struct b_tty; +struct fx_tty; struct tty_format_buf { bool in_format; @@ -14,10 +14,10 @@ struct tty_format_buf { char buf[TTY_TMPBUF_SIZE]; unsigned int ptr; - struct b_tty_vmode vmode; + struct fx_tty_vmode vmode; }; -extern struct tty_format_buf *z__b_tty_get_format_buf(struct b_tty *tty); -extern void z__b_tty_putc(struct b_tty *tty, char c); +extern struct tty_format_buf *z__fx_tty_get_format_buf(struct fx_tty *tty); +extern void z__fx_tty_putc(struct fx_tty *tty, char c); #endif diff --git a/test/cat.c b/test/cat.c index 33d6fd0..943b561 100644 --- a/test/cat.c +++ b/test/cat.c @@ -1,8 +1,8 @@ -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #include int main(int argc, const char **argv) @@ -12,21 +12,21 @@ int main(int argc, const char **argv) } const char *path_cstr = argv[1]; - b_path *path = b_path_create_from_cstr(path_cstr); + fx_path *path = fx_path_create_from_cstr(path_cstr); - b_file *src = NULL; - b_result result = b_file_open(NULL, path, B_FILE_READ_ONLY, &src); - if (b_result_is_error(result)) { - b_throw(result); + fx_file *src = NULL; + fx_result result = fx_file_open(NULL, path, FX_FILE_READ_ONLY, &src); + if (fx_result_is_error(result)) { + fx_throw(result); return -1; } - b_stream_buffer *buf = b_stream_buffer_create_dynamic(1024); + fx_stream_buffer *buf = fx_stream_buffer_create_dynamic(1024); size_t nr_read; - b_stream_read_all_bytes_s(src, b_stdout, buf, &nr_read); + fx_stream_read_all_bytes_s(src, fx_stdout, buf, &nr_read); - b_stream_buffer_unref(buf); + fx_stream_buffer_unref(buf); return 0; } diff --git a/test/cat1.c b/test/cat1.c index f133a7e..5e62beb 100644 --- a/test/cat1.c +++ b/test/cat1.c @@ -1,8 +1,8 @@ -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #include int main(int argc, const char **argv) @@ -12,28 +12,28 @@ int main(int argc, const char **argv) } const char *path_cstr = argv[1]; - b_path *path = b_path_create_from_cstr(path_cstr); + fx_path *path = fx_path_create_from_cstr(path_cstr); - b_file *src = NULL; - b_result result = b_file_open(NULL, path, B_FILE_READ_ONLY, &src); - if (b_result_is_error(result)) { - b_throw(result); + fx_file *src = NULL; + fx_result result = fx_file_open(NULL, path, FX_FILE_READ_ONLY, &src); + if (fx_result_is_error(result)) { + fx_throw(result); return -1; } - b_wchar c; + fx_wchar c; size_t nr_read; - b_status status = b_stream_read_char(src, &c); - if (!B_OK(status)) { - printf("read error: %s\n", b_status_description(status)); - b_file_unref(src); + fx_status status = fx_stream_read_char(src, &c); + if (!FX_OK(status)) { + printf("read error: %s\n", fx_status_description(status)); + fx_file_unref(src); return -1; } - b_stream_write_char(b_stdout, c); - b_stream_write_char(b_stdout, '\n'); + fx_stream_write_char(fx_stdout, c); + fx_stream_write_char(fx_stdout, '\n'); - b_file_unref(src); + fx_file_unref(src); return 0; } diff --git a/test/cmd/example.c b/test/cmd/example.c index b6df3ae..6ea791f 100644 --- a/test/cmd/example.c +++ b/test/cmd/example.c @@ -1,4 +1,4 @@ -#include +#include #include enum { @@ -27,149 +27,149 @@ const char *text "great pleasure."; static int test_command( - const b_command *self, const b_arglist *opt, const b_array *args) + const fx_command *self, const fx_arglist *opt, const fx_array *args) { printf("Hello, world!\n"); - b_arglist_iterator it; - b_arglist_iterator_begin( - opt, B_COMMAND_INVALID_ID, B_COMMAND_INVALID_ID, &it); - while (b_arglist_iterator_is_valid(&it)) { + fx_arglist_iterator it; + fx_arglist_iterator_begin( + opt, FX_COMMAND_INVALID_ID, FX_COMMAND_INVALID_ID, &it); + while (fx_arglist_iterator_is_valid(&it)) { printf("opt:%u,arg:%u,i:%zu,value: %s\n", it.opt_id, it.value->val_id, it.i, it.value->val_str); - b_arglist_iterator_next(&it); + fx_arglist_iterator_next(&it); } return 0; } -B_COMMAND(CMD_TEST, B_COMMAND_INVALID_ID) +FX_COMMAND(CMD_TEST, FX_COMMAND_INVALID_ID) { - B_COMMAND_NAME("test"); - B_COMMAND_DESC("A test command."); - B_COMMAND_FLAGS(B_COMMAND_SHOW_HELP_BY_DEFAULT); - B_COMMAND_FUNCTION(test_command); + FX_COMMAND_NAME("test"); + FX_COMMAND_DESC("A test command."); + FX_COMMAND_FLAGS(FX_COMMAND_SHOW_HELP_BY_DEFAULT); + FX_COMMAND_FUNCTION(test_command); - B_COMMAND_OPTION(OPT_NAME) + FX_COMMAND_OPTION(OPT_NAME) { - B_OPTION_LONG_NAME("name"); - B_OPTION_SHORT_NAME('n'); - B_OPTION_DESC("The name of the data."); - B_OPTION_ARG(OPT_NAME_VALUE) + FX_OPTION_LONG_NAME("name"); + FX_OPTION_SHORT_NAME('n'); + FX_OPTION_DESC("The name of the data."); + FX_OPTION_ARG(OPT_NAME_VALUE) { - B_ARG_NAME("name"); - B_ARG_NR_VALUES(2); + FX_ARG_NAME("name"); + FX_ARG_NR_VALUES(2); } } - B_COMMAND_OPTION(OPT_OTHER) + FX_COMMAND_OPTION(OPT_OTHER) { - B_OPTION_LONG_NAME("other"); - B_OPTION_DESC(text); + FX_OPTION_LONG_NAME("other"); + FX_OPTION_DESC(text); - B_OPTION_ARG(OPT_OTHER_VALUE) + FX_OPTION_ARG(OPT_OTHER_VALUE) { - B_ARG_NAME("value"); - B_ARG_NR_VALUES(B_ARG_1_OR_MORE_VALUES); + FX_ARG_NAME("value"); + FX_ARG_NR_VALUES(FX_ARG_1_OR_MORE_VALUES); } } - B_COMMAND_OPTION(OPT_VERBOSE) + FX_COMMAND_OPTION(OPT_VERBOSE) { - B_OPTION_LONG_NAME("verbose"); - B_OPTION_SHORT_NAME('v'); - B_OPTION_DESC("Show detailed log output."); + FX_OPTION_LONG_NAME("verbose"); + FX_OPTION_SHORT_NAME('v'); + FX_OPTION_DESC("Show detailed log output."); } - B_COMMAND_OPTION(OPT_REALLY_VERBOSE) + FX_COMMAND_OPTION(OPT_REALLY_VERBOSE) { - B_OPTION_LONG_NAME("really-verbose"); - B_OPTION_SHORT_NAME('V'); - B_OPTION_DESC("Show REALLY detailed log output."); + FX_OPTION_LONG_NAME("really-verbose"); + FX_OPTION_SHORT_NAME('V'); + FX_OPTION_DESC("Show REALLY detailed log output."); } - B_COMMAND_OPTION(OPT_MODE) + FX_COMMAND_OPTION(OPT_MODE) { - B_OPTION_SHORT_NAME('m'); - B_OPTION_LONG_NAME("mode"); - B_OPTION_DESC("modes to operate in."); + FX_OPTION_SHORT_NAME('m'); + FX_OPTION_LONG_NAME("mode"); + FX_OPTION_DESC("modes to operate in."); - B_OPTION_ARG(OPT_MODE_VALUE) + FX_OPTION_ARG(OPT_MODE_VALUE) { - B_ARG_NAME("mode"); - B_ARG_NR_VALUES(1); - B_ARG_ALLOWED_VALUES("fast", "slow"); + FX_ARG_NAME("mode"); + FX_ARG_NR_VALUES(1); + FX_ARG_ALLOWED_VALUES("fast", "slow"); } - B_OPTION_ARG(OPT_MODE_VALUE2) + FX_OPTION_ARG(OPT_MODE_VALUE2) { - B_ARG_NAME("mode2"); - B_ARG_NR_VALUES(1); - B_ARG_ALLOWED_VALUES("very-fast", "very-slow"); + FX_ARG_NAME("mode2"); + FX_ARG_NR_VALUES(1); + FX_ARG_ALLOWED_VALUES("very-fast", "very-slow"); } } - B_COMMAND_ARG(ARG_FILE) + FX_COMMAND_ARG(ARG_FILE) { - B_ARG_NAME("file"); - B_ARG_DESC("The file(s) to use"); - B_ARG_NR_VALUES(2); + FX_ARG_NAME("file"); + FX_ARG_DESC("The file(s) to use"); + FX_ARG_NR_VALUES(2); } - B_COMMAND_ARG(ARG_MORE) + FX_COMMAND_ARG(ARG_MORE) { - B_ARG_NAME("more"); - B_ARG_DESC("More args to use"); - B_ARG_ALLOWED_VALUES("how", "wow"); - B_ARG_NR_VALUES(2); + FX_ARG_NAME("more"); + FX_ARG_DESC("More args to use"); + FX_ARG_ALLOWED_VALUES("how", "wow"); + FX_ARG_NR_VALUES(2); } - B_COMMAND_HELP_OPTION(); + FX_COMMAND_HELP_OPTION(); - B_COMMAND_USAGE() + FX_COMMAND_USAGE() { - B_COMMAND_USAGE_OPT(OPT_NAME); - B_COMMAND_USAGE_ARG(ARG_FILE); - B_COMMAND_USAGE_ARG(ARG_MORE); + FX_COMMAND_USAGE_OPT(OPT_NAME); + FX_COMMAND_USAGE_ARG(ARG_FILE); + FX_COMMAND_USAGE_ARG(ARG_MORE); } } -B_COMMAND(CMD_SUB, CMD_TEST) +FX_COMMAND(CMD_SUB, CMD_TEST) { - B_COMMAND_NAME("sub"); - B_COMMAND_LONG_NAME("sub"); - B_COMMAND_SHORT_NAME('S'); - B_COMMAND_DESC("A test subcommand"); - B_COMMAND_FLAGS(B_COMMAND_SHOW_HELP_BY_DEFAULT); - B_COMMAND_FUNCTION(test_command); + FX_COMMAND_NAME("sub"); + FX_COMMAND_LONG_NAME("sub"); + FX_COMMAND_SHORT_NAME('S'); + FX_COMMAND_DESC("A test subcommand"); + FX_COMMAND_FLAGS(FX_COMMAND_SHOW_HELP_BY_DEFAULT); + FX_COMMAND_FUNCTION(test_command); - B_COMMAND_OPTION(OPT_NAME) + FX_COMMAND_OPTION(OPT_NAME) { - B_OPTION_LONG_NAME("name"); - B_OPTION_SHORT_NAME('n'); - B_OPTION_DESC("The name of the data"); - B_OPTION_ARG(OPT_NAME_VALUE) + FX_OPTION_LONG_NAME("name"); + FX_OPTION_SHORT_NAME('n'); + FX_OPTION_DESC("The name of the data"); + FX_OPTION_ARG(OPT_NAME_VALUE) { - B_ARG_NAME("name"); + FX_ARG_NAME("name"); } } - B_COMMAND_OPTION(OPT_OTHER) + FX_COMMAND_OPTION(OPT_OTHER) { - B_OPTION_LONG_NAME("other"); - B_OPTION_SHORT_NAME('o'); - B_OPTION_DESC("The other argument"); - B_OPTION_ARG(OPT_OTHER_VALUE) + FX_OPTION_LONG_NAME("other"); + FX_OPTION_SHORT_NAME('o'); + FX_OPTION_DESC("The other argument"); + FX_OPTION_ARG(OPT_OTHER_VALUE) { - B_ARG_NAME("value"); + FX_ARG_NAME("value"); } } - B_COMMAND_HELP_OPTION(); + FX_COMMAND_HELP_OPTION(); } int main(int argc, const char **argv) { - return b_command_dispatch(CMD_TEST, argc, argv); + return fx_command_dispatch(CMD_TEST, argc, argv); } diff --git a/test/compress/mix-compress.c b/test/compress/mix-compress.c index d079d00..f08a385 100644 --- a/test/compress/mix-compress.c +++ b/test/compress/mix-compress.c @@ -1,9 +1,9 @@ #include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #include #include @@ -26,7 +26,7 @@ int main(int argc, const char **argv) return -1; } - b_compressor_mode mode = B_COMPRESSOR_MODE_COMPRESS; + fx_compressor_mode mode = FX_COMPRESSOR_MODE_COMPRESS; FILE *out_fp = fopen(argv[1], "wb"); if (!out_fp) { @@ -34,36 +34,36 @@ int main(int argc, const char **argv) return -1; } - b_stream *out_stream = b_stream_open_fp(out_fp); - b_cstream *cstream; - b_cstream_open(out_stream, B_TYPE_ZSTD_COMPRESSOR, mode, &cstream); + fx_stream *out_stream = fx_stream_open_fp(out_fp); + fx_cstream *cstream; + fx_cstream_open(out_stream, FX_TYPE_ZSTD_COMPRESSOR, mode, &cstream); const size_t source_len = strlen(source); bool compressed = false; for (int i = 0; i < NR_ITERATIONS; i++) { if (compressed) { - b_cstream_begin_compressed_section(cstream, NULL); + fx_cstream_begin_compressed_section(cstream, NULL); } size_t nr_written = 0; - b_status status = b_cstream_write( + fx_status status = fx_cstream_write( cstream, source, source_len, &nr_written); - if (!B_OK(status)) { + if (!FX_OK(status)) { fprintf(stderr, "write error: %s\n", - b_status_description(status)); + fx_status_description(status)); break; } size_t nr_written_compressed = 0; if (compressed) { - b_cstream_end_compressed_section( + fx_cstream_end_compressed_section( cstream, &nr_written_compressed, &nr_written); } size_t tx_total = 0; - b_cstream_tx_bytes(cstream, &tx_total); + fx_cstream_tx_bytes(cstream, &tx_total); printf("iteration %d: wrote %zu (compressed) / %zu " "(uncompressed) / %zu (total) bytes (%s)\n", i, nr_written_compressed, nr_written, tx_total, @@ -74,8 +74,8 @@ int main(int argc, const char **argv) printf("Done\n"); - b_cstream_unref(cstream); - b_stream_unref(out_stream); + fx_cstream_unref(cstream); + fx_stream_unref(out_stream); fclose(out_fp); return 0; diff --git a/test/compress/mix-decompress.c b/test/compress/mix-decompress.c index eaa0d6e..bcf2f64 100644 --- a/test/compress/mix-decompress.c +++ b/test/compress/mix-decompress.c @@ -1,9 +1,9 @@ #include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #include #include @@ -14,7 +14,7 @@ int main(int argc, const char **argv) return -1; } - b_compressor_mode mode = B_COMPRESSOR_MODE_DECOMPRESS; + fx_compressor_mode mode = FX_COMPRESSOR_MODE_DECOMPRESS; FILE *in_fp = fopen(argv[1], "rb"); if (!in_fp) { @@ -22,26 +22,26 @@ int main(int argc, const char **argv) return -1; } - b_stream *in_stream = b_stream_open_fp(in_fp); - b_cstream *cstream; - b_cstream_open(in_stream, B_TYPE_ZSTD_COMPRESSOR, mode, &cstream); + fx_stream *in_stream = fx_stream_open_fp(in_fp); + fx_cstream *cstream; + fx_cstream_open(in_stream, FX_TYPE_ZSTD_COMPRESSOR, mode, &cstream); bool compressed = false; char buf[513]; for (int i = 0;; i++) { if (compressed) { - b_cstream_begin_compressed_section(cstream, NULL); + fx_cstream_begin_compressed_section(cstream, NULL); } memset(buf, 0x0, sizeof buf); size_t nr_read = 0; - b_status status - = b_cstream_read(cstream, buf, sizeof buf - 1, &nr_read); - if (!B_OK(status)) { + fx_status status + = fx_cstream_read(cstream, buf, sizeof buf - 1, &nr_read); + if (!FX_OK(status)) { fprintf(stderr, "write error: %s\n", - b_status_description(status)); + fx_status_description(status)); break; } @@ -51,12 +51,12 @@ int main(int argc, const char **argv) size_t nr_read_compressed = 0; if (compressed) { - b_cstream_end_compressed_section( + fx_cstream_end_compressed_section( cstream, &nr_read_compressed, &nr_read); } size_t tx_total = 0; - b_cstream_tx_bytes(cstream, &tx_total); + fx_cstream_tx_bytes(cstream, &tx_total); printf(" * iteration %d: read %zu (compressed) / %zu " "(uncompressed) / %zu (total) bytes (%s)\n", i, nr_read_compressed, nr_read, tx_total, @@ -68,8 +68,8 @@ int main(int argc, const char **argv) printf("Done\n"); - b_cstream_unref(cstream); - b_stream_unref(in_stream); + fx_cstream_unref(cstream); + fx_stream_unref(in_stream); fclose(in_fp); return 0; diff --git a/test/compress/simple1.c b/test/compress/simple1.c index f571769..7ed1e67 100644 --- a/test/compress/simple1.c +++ b/test/compress/simple1.c @@ -1,30 +1,30 @@ #include -#include -#include -#include +#include +#include +#include #include #include #define BUF_SIZE 32 -int refill_input_buffer(FILE *fp, b_ringbuffer *dest) +int refill_input_buffer(FILE *fp, fx_ringbuffer *dest) { while (1) { void *buf; size_t capacity; - b_status status - = b_ringbuffer_open_write_buffer(dest, &buf, &capacity); - if (status == B_ERR_NO_SPACE) { + fx_status status + = fx_ringbuffer_open_write_buffer(dest, &buf, &capacity); + if (status == FX_ERR_NO_SPACE) { break; } - if (!B_OK(status)) { + if (!FX_OK(status)) { return -1; } size_t r = fread(buf, 1, capacity, fp); - b_ringbuffer_close_write_buffer(dest, &buf, r); + fx_ringbuffer_close_write_buffer(dest, &buf, r); if (r == 0) { return ferror(fp) ? -1 : 0; @@ -38,24 +38,24 @@ int refill_input_buffer(FILE *fp, b_ringbuffer *dest) return 0; } -int flush_output_buffer(FILE *fp, b_ringbuffer *src) +int flush_output_buffer(FILE *fp, fx_ringbuffer *src) { while (1) { const void *buf; size_t capacity; - b_status status - = b_ringbuffer_open_read_buffer(src, &buf, &capacity); - if (status == B_ERR_NO_DATA) { + fx_status status + = fx_ringbuffer_open_read_buffer(src, &buf, &capacity); + if (status == FX_ERR_NO_DATA) { break; } - if (!B_OK(status)) { + if (!FX_OK(status)) { return -1; } size_t w = fwrite(buf, 1, capacity, fp); - b_ringbuffer_close_read_buffer(src, &buf, w); + fx_ringbuffer_close_read_buffer(src, &buf, w); if (w < capacity) { return -1; @@ -72,11 +72,11 @@ int main(int argc, const char **argv) return -1; } - b_compressor_mode mode; + fx_compressor_mode mode; if (!strcmp(argv[1], "C")) { - mode = B_COMPRESSOR_MODE_COMPRESS; + mode = FX_COMPRESSOR_MODE_COMPRESS; } else if (!strcmp(argv[1], "D")) { - mode = B_COMPRESSOR_MODE_DECOMPRESS; + mode = FX_COMPRESSOR_MODE_DECOMPRESS; } else { fprintf(stderr, "invalid mode %s\n", argv[1]); return -1; @@ -95,24 +95,24 @@ int main(int argc, const char **argv) return -1; } - b_status status = B_SUCCESS; - b_type compressor_type = B_TYPE_ZSTD_COMPRESSOR; - b_compressor *compressor = b_object_create(compressor_type); + fx_status status = FX_SUCCESS; + fx_type compressor_type = FX_TYPE_ZSTD_COMPRESSOR; + fx_compressor *compressor = fx_object_create(compressor_type); size_t inbuf_size, outbuf_size; - b_compressor_get_buffer_size( + fx_compressor_get_buffer_size( compressor_type, mode, &inbuf_size, &outbuf_size); - b_ringbuffer *in = b_ringbuffer_create(inbuf_size); - b_ringbuffer *out = b_ringbuffer_create(outbuf_size); + fx_ringbuffer *in = fx_ringbuffer_create(inbuf_size); + fx_ringbuffer *out = fx_ringbuffer_create(outbuf_size); if (!in || !out) { fprintf(stderr, "memory allocation failure"); return -1; } - b_compressor_set_buffer(compressor, in, out); - b_compressor_set_mode(compressor, mode); + fx_compressor_set_buffer(compressor, in, out); + fx_compressor_set_mode(compressor, mode); int ret = 0; while (1) { @@ -122,21 +122,21 @@ int main(int argc, const char **argv) break; } - if (!b_ringbuffer_available_data_remaining(in)) { + if (!fx_ringbuffer_available_data_remaining(in)) { break; } - status = b_compressor_step(compressor); + status = fx_compressor_step(compressor); - if (status == B_ERR_NO_DATA) { + if (status == FX_ERR_NO_DATA) { break; - } else if (status == B_ERR_NO_SPACE) { + } else if (status == FX_ERR_NO_SPACE) { ret = flush_output_buffer(out_fp, out); if (ret != 0) { fprintf(stderr, "write error\n"); break; } - } else if (!B_OK(status)) { + } else if (!FX_OK(status)) { ret = -1; break; } @@ -148,16 +148,16 @@ int main(int argc, const char **argv) return -1; } - if (mode == B_COMPRESSOR_MODE_COMPRESS) { - while (!b_compressor_eof(compressor)) { - status = b_compressor_end(compressor); - if (!B_OK(status)) { + if (mode == FX_COMPRESSOR_MODE_COMPRESS) { + while (!fx_compressor_eof(compressor)) { + status = fx_compressor_end(compressor); + if (!FX_OK(status)) { fprintf(stderr, "compression finalisation error\n"); return -1; } - if (!b_ringbuffer_available_data_remaining(out)) { + if (!fx_ringbuffer_available_data_remaining(out)) { break; } @@ -171,13 +171,13 @@ int main(int argc, const char **argv) printf("Done\n"); - b_compressor_unref(compressor); + fx_compressor_unref(compressor); fclose(in_fp); fclose(out_fp); - b_ringbuffer_unref(in); - b_ringbuffer_unref(out); + fx_ringbuffer_unref(in); + fx_ringbuffer_unref(out); return ret; } diff --git a/test/compress/stream.c b/test/compress/stream.c index 8245c3b..b05c910 100644 --- a/test/compress/stream.c +++ b/test/compress/stream.c @@ -1,35 +1,35 @@ #include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #include #include #define BUF_SIZE 32 -static int compress(b_type compressor_type, FILE *in, FILE *out) +static int compress(fx_type compressor_type, FILE *in, FILE *out) { - b_stream *out_stream = b_stream_open_fp(out); + fx_stream *out_stream = fx_stream_open_fp(out); - b_cstream *cstream; - b_status status = b_cstream_open( - out_stream, compressor_type, B_COMPRESSOR_MODE_COMPRESS, &cstream); + fx_cstream *cstream; + fx_status status = fx_cstream_open( + out_stream, compressor_type, FX_COMPRESSOR_MODE_COMPRESS, &cstream); - if (!B_OK(status)) { + if (!FX_OK(status)) { fprintf(stderr, "cannot initialise compressor\n"); return -1; } - b_cstream_begin_compressed_section(cstream, NULL); + fx_cstream_begin_compressed_section(cstream, NULL); char buf[4096]; while (1) { size_t r = fread(buf, 1, sizeof buf, in); size_t w = 0; - b_cstream_write(cstream, buf, r, &w); + fx_cstream_write(cstream, buf, r, &w); if (r != w) { fprintf(stderr, "write error\n"); @@ -41,35 +41,35 @@ static int compress(b_type compressor_type, FILE *in, FILE *out) } } - b_cstream_end_compressed_section(cstream, NULL, NULL); - b_cstream_unref(cstream); - b_stream_unref(out_stream); + fx_cstream_end_compressed_section(cstream, NULL, NULL); + fx_cstream_unref(cstream); + fx_stream_unref(out_stream); return 0; } -static int decompress(b_type compressor_type, FILE *in, FILE *out) +static int decompress(fx_type compressor_type, FILE *in, FILE *out) { - b_stream *in_stream = b_stream_open_fp(in); + fx_stream *in_stream = fx_stream_open_fp(in); - b_cstream *cstream; - b_status status = b_cstream_open( - in_stream, compressor_type, B_COMPRESSOR_MODE_DECOMPRESS, &cstream); + fx_cstream *cstream; + fx_status status = fx_cstream_open( + in_stream, compressor_type, FX_COMPRESSOR_MODE_DECOMPRESS, &cstream); - if (!B_OK(status)) { + if (!FX_OK(status)) { fprintf(stderr, "cannot initialise compressor\n"); return -1; } - b_cstream_begin_compressed_section(cstream, NULL); + fx_cstream_begin_compressed_section(cstream, NULL); char buf[4096]; while (1) { size_t r = 0; - b_status status = b_cstream_read(cstream, buf, sizeof buf, &r); - if (!B_OK(status)) { + fx_status status = fx_cstream_read(cstream, buf, sizeof buf, &r); + if (!FX_OK(status)) { fprintf(stderr, "read error: %s\n", - b_status_description(status)); + fx_status_description(status)); return -1; } @@ -84,9 +84,9 @@ static int decompress(b_type compressor_type, FILE *in, FILE *out) } } - b_cstream_end_compressed_section(cstream, NULL, NULL); - b_cstream_unref(cstream); - b_stream_unref(in_stream); + fx_cstream_end_compressed_section(cstream, NULL, NULL); + fx_cstream_unref(cstream); + fx_stream_unref(in_stream); return 0; } @@ -98,11 +98,11 @@ int main(int argc, const char **argv) return -1; } - b_compressor_mode mode; + fx_compressor_mode mode; if (!strcmp(argv[1], "C")) { - mode = B_COMPRESSOR_MODE_COMPRESS; + mode = FX_COMPRESSOR_MODE_COMPRESS; } else if (!strcmp(argv[1], "D")) { - mode = B_COMPRESSOR_MODE_DECOMPRESS; + mode = FX_COMPRESSOR_MODE_DECOMPRESS; } else { fprintf(stderr, "invalid mode %s\n", argv[1]); return -1; @@ -123,11 +123,11 @@ int main(int argc, const char **argv) int ret = 0; switch (mode) { - case B_COMPRESSOR_MODE_COMPRESS: - ret = compress(B_TYPE_ZSTD_COMPRESSOR, in_fp, out_fp); + case FX_COMPRESSOR_MODE_COMPRESS: + ret = compress(FX_TYPE_ZSTD_COMPRESSOR, in_fp, out_fp); break; - case B_COMPRESSOR_MODE_DECOMPRESS: - ret = decompress(B_TYPE_ZSTD_COMPRESSOR, in_fp, out_fp); + case FX_COMPRESSOR_MODE_DECOMPRESS: + ret = decompress(FX_TYPE_ZSTD_COMPRESSOR, in_fp, out_fp); break; default: ret = -1; diff --git a/test/core/core-units.c b/test/core/core-units.c index 7fcb8d4..5e77812 100644 --- a/test/core/core-units.c +++ b/test/core/core-units.c @@ -1,27 +1,26 @@ -#include "blue/core/misc.h" - #include -#include -#include -#include +#include +#include +#include +#include #include #include struct test_tree_node { int value; - b_btree_node node; + fx_bst_node node; }; struct test_queue_entry { int value; - b_queue_entry entry; + fx_queue_entry entry; }; -B_BTREE_DEFINE_SIMPLE_INSERT(struct test_tree_node, node, value, test_tree_insert); +FX_BTREE_DEFINE_SIMPLE_INSERT(struct test_tree_node, node, value, test_tree_insert); void test_btree_insert(CuTest *tc) { - b_btree tree = {0}; + fx_bst tree = {0}; struct test_tree_node nodes[3] = {0}; for (int i = 0; i < sizeof nodes / sizeof *nodes; i++) { @@ -30,33 +29,33 @@ void test_btree_insert(CuTest *tc) test_tree_insert(&tree, &nodes[0]); - CuAssertPtrEquals(tc, NULL, nodes[0].node.b_left); - CuAssertPtrEquals(tc, NULL, nodes[0].node.b_right); - CuAssertIntEquals(tc, 1, nodes[0].node.b_height); + CuAssertPtrEquals(tc, NULL, nodes[0].node.n_left); + CuAssertPtrEquals(tc, NULL, nodes[0].node.n_right); + CuAssertIntEquals(tc, 1, nodes[0].node.n_height); test_tree_insert(&tree, &nodes[1]); - CuAssertPtrEquals(tc, NULL, nodes[0].node.b_left); - CuAssertPtrEquals(tc, &nodes[1].node, nodes[0].node.b_right); - CuAssertIntEquals(tc, 2, nodes[0].node.b_height); + CuAssertPtrEquals(tc, NULL, nodes[0].node.n_left); + CuAssertPtrEquals(tc, &nodes[1].node, nodes[0].node.n_right); + CuAssertIntEquals(tc, 2, nodes[0].node.n_height); - CuAssertPtrEquals(tc, NULL, nodes[1].node.b_left); - CuAssertPtrEquals(tc, NULL, nodes[1].node.b_right); - CuAssertIntEquals(tc, 1, nodes[1].node.b_height); + CuAssertPtrEquals(tc, NULL, nodes[1].node.n_left); + CuAssertPtrEquals(tc, NULL, nodes[1].node.n_right); + CuAssertIntEquals(tc, 1, nodes[1].node.n_height); test_tree_insert(&tree, &nodes[2]); - CuAssertPtrEquals(tc, &nodes[0].node, nodes[1].node.b_left); - CuAssertPtrEquals(tc, &nodes[2].node, nodes[1].node.b_right); - CuAssertIntEquals(tc, 2, nodes[1].node.b_height); + CuAssertPtrEquals(tc, &nodes[0].node, nodes[1].node.n_left); + CuAssertPtrEquals(tc, &nodes[2].node, nodes[1].node.n_right); + CuAssertIntEquals(tc, 2, nodes[1].node.n_height); - CuAssertPtrEquals(tc, NULL, nodes[0].node.b_left); - CuAssertPtrEquals(tc, NULL, nodes[0].node.b_right); - CuAssertIntEquals(tc, 1, nodes[0].node.b_height); + CuAssertPtrEquals(tc, NULL, nodes[0].node.n_left); + CuAssertPtrEquals(tc, NULL, nodes[0].node.n_right); + CuAssertIntEquals(tc, 1, nodes[0].node.n_height); - CuAssertPtrEquals(tc, NULL, nodes[2].node.b_left); - CuAssertPtrEquals(tc, NULL, nodes[2].node.b_right); - CuAssertIntEquals(tc, 1, nodes[2].node.b_height); + CuAssertPtrEquals(tc, NULL, nodes[2].node.n_left); + CuAssertPtrEquals(tc, NULL, nodes[2].node.n_right); + CuAssertIntEquals(tc, 1, nodes[2].node.n_height); } void test_btree_iterate(CuTest *tc) @@ -64,7 +63,7 @@ void test_btree_iterate(CuTest *tc) static const size_t nr_nodes = 256; srand(time(NULL)); - b_btree tree = {0}; + fx_bst tree = {0}; struct test_tree_node *nodes = calloc(nr_nodes, sizeof *nodes); CuAssertPtrNotNull(tc, nodes); @@ -74,21 +73,21 @@ void test_btree_iterate(CuTest *tc) } int prev = -1; - b_btree_node *bnode = b_btree_first(&tree); + fx_bst_node *bnode = fx_bst_first(&tree); while (bnode) { struct test_tree_node *node - = b_unbox(struct test_tree_node, bnode, node); + = fx_unbox(struct test_tree_node, bnode, node); CuAssertPtrNotNull(tc, node); if (prev == -1) { prev = node->value; - bnode = b_btree_next(bnode); + bnode = fx_bst_next(bnode); continue; } CuAssertTrue(tc, prev <= node->value); prev = node->value; - bnode = b_btree_next(bnode); + bnode = fx_bst_next(bnode); } free(nodes); @@ -101,13 +100,13 @@ void test_queue_insert(CuTest *tc) entries[i].value = i; } - b_queue q = B_QUEUE_INIT; + fx_queue q = FX_QUEUE_INIT; - b_queue_push_back(&q, &entries[0].entry); - b_queue_push_back(&q, &entries[2].entry); - b_queue_push_back(&q, &entries[4].entry); - b_queue_insert_after(&q, &entries[3].entry, &entries[2].entry); - b_queue_insert_before(&q, &entries[1].entry, &entries[2].entry); + fx_queue_push_back(&q, &entries[0].entry); + fx_queue_push_back(&q, &entries[2].entry); + fx_queue_push_back(&q, &entries[4].entry); + fx_queue_insert_after(&q, &entries[3].entry, &entries[2].entry); + fx_queue_insert_before(&q, &entries[1].entry, &entries[2].entry); CuAssertPtrEquals(tc, NULL, entries[0].entry.qe_prev); CuAssertPtrEquals(tc, &entries[1].entry, entries[0].entry.qe_next); @@ -127,19 +126,19 @@ void test_queue_insert(CuTest *tc) void test_queue_iterate(CuTest *tc) { - b_queue q = B_QUEUE_INIT; + fx_queue q = FX_QUEUE_INIT; struct test_queue_entry entries[32] = {0}; for (int i = 0; i < sizeof entries / sizeof *entries; i++) { entries[i].value = i; - b_queue_push_back(&q, &entries[i].entry); + fx_queue_push_back(&q, &entries[i].entry); } int prev = -1; - struct b_queue_entry *entry = b_queue_first(&q); + struct fx_queue_entry *entry = fx_queue_first(&q); while (entry) { struct test_queue_entry *e - = b_unbox(struct test_queue_entry, entry, entry); + = fx_unbox(struct test_queue_entry, entry, entry); CuAssertPtrNotNull(tc, e); if (prev == -1) { @@ -150,20 +149,20 @@ void test_queue_iterate(CuTest *tc) CuAssertTrue(tc, prev < e->value); prev = e->value; skip: - entry = b_queue_next(entry); + entry = fx_queue_next(entry); } } void test_stringstream_1(CuTest *tc) { char buf[1024]; - b_stringstream *s = b_stringstream_create_with_buffer(buf, sizeof buf); + fx_stringstream *s = fx_stringstream_create_with_buffer(buf, sizeof buf); - b_stream_write_string(s, "hello", NULL); - b_stream_write_fmt(s, NULL, "(%d + %.1f)", 32, 2.3); + fx_stream_write_string(s, "hello", NULL); + fx_stream_write_fmt(s, NULL, "(%d + %.1f)", 32, 2.3); - char *end = b_stringstream_steal(s); - b_stringstream_unref(s); + char *end = fx_stringstream_steal(s); + fx_stringstream_unref(s); CuAssertStrEquals(tc, "hello(32 + 2.3)", end); } @@ -171,19 +170,19 @@ void test_stringstream_1(CuTest *tc) void test_stringstream_2(CuTest *tc) { char buf[1024]; - b_stringstream *s = b_stringstream_create_with_buffer(buf, sizeof buf); + fx_stringstream *s = fx_stringstream_create_with_buffer(buf, sizeof buf); - b_stream_write_string(s, "{\n", NULL); - b_stream_push_indent(s, 1); + fx_stream_write_string(s, "{\n", NULL); + fx_stream_push_indent(s, 1); - b_stream_write_string(s, "a = 32,\n", NULL); - b_stream_write_string(s, "b = 64\n", NULL); + fx_stream_write_string(s, "a = 32,\n", NULL); + fx_stream_write_string(s, "b = 64\n", NULL); - b_stream_pop_indent(s); - b_stream_write_string(s, "}", NULL); + fx_stream_pop_indent(s); + fx_stream_write_string(s, "}", NULL); - char *str = b_stringstream_steal(s); - b_stringstream_unref(s); + char *str = fx_stringstream_steal(s); + fx_stringstream_unref(s); CuAssertStrEquals(tc, "{\n a = 32,\n b = 64\n}", str); } diff --git a/test/core/errors.c b/test/core/errors.c index 8c074ba..23fa08d 100644 --- a/test/core/errors.c +++ b/test/core/errors.c @@ -1,4 +1,4 @@ -#include +#include int main(void) { diff --git a/test/core/hash.c b/test/core/hash.c index 0e9cda0..7da40dd 100644 --- a/test/core/hash.c +++ b/test/core/hash.c @@ -1,26 +1,26 @@ -#include +#include #include #include static void print_digest( - const char *func_name, b_hash_function func, const char *msg, + const char *func_name, fx_hash_function func, const char *msg, size_t msg_len, size_t digest_len) { unsigned char digest[128]; - b_hash_ctx ctx; - if (!B_OK(b_hash_ctx_init(&ctx, func))) { - printf("b_hash_ctx_init failed\n"); + fx_hash_ctx ctx; + if (!FX_OK(fx_hash_ctx_init(&ctx, func))) { + printf("fx_hash_ctx_init failed\n"); return; } - if (!B_OK(b_hash_ctx_update(&ctx, msg, msg_len))) { - printf("b_hash_ctx_update failed\n"); + if (!FX_OK(fx_hash_ctx_update(&ctx, msg, msg_len))) { + printf("fx_hash_ctx_update failed\n"); return; } - if (!B_OK(b_hash_ctx_finish(&ctx, digest, sizeof digest))) { - printf("b_hash_ctx_finish failed\n"); + if (!FX_OK(fx_hash_ctx_finish(&ctx, digest, sizeof digest))) { + printf("fx_hash_ctx_finish failed\n"); return; } @@ -40,35 +40,35 @@ int main(void) const char *msg = "Hello, world!"; size_t msg_len = strlen(msg); - print_digest("MD4", B_HASH_MD4, msg, msg_len, B_DIGEST_LENGTH_MD4); - print_digest("MD5", B_HASH_MD5, msg, msg_len, B_DIGEST_LENGTH_MD5); - print_digest("SHA1", B_HASH_SHA1, msg, msg_len, B_DIGEST_LENGTH_SHA1); + print_digest("MD4", FX_HASH_MD4, msg, msg_len, FX_DIGEST_LENGTH_MD4); + print_digest("MD5", FX_HASH_MD5, msg, msg_len, FX_DIGEST_LENGTH_MD5); + print_digest("SHA1", FX_HASH_SHA1, msg, msg_len, FX_DIGEST_LENGTH_SHA1); print_digest( - "SHA224", B_HASH_SHA2_224, msg, msg_len, B_DIGEST_LENGTH_SHA2_224); + "SHA224", FX_HASH_SHA2_224, msg, msg_len, FX_DIGEST_LENGTH_SHA2_224); print_digest( - "SHA256", B_HASH_SHA2_256, msg, msg_len, B_DIGEST_LENGTH_SHA2_256); + "SHA256", FX_HASH_SHA2_256, msg, msg_len, FX_DIGEST_LENGTH_SHA2_256); print_digest( - "SHA384", B_HASH_SHA2_384, msg, msg_len, B_DIGEST_LENGTH_SHA2_384); + "SHA384", FX_HASH_SHA2_384, msg, msg_len, FX_DIGEST_LENGTH_SHA2_384); print_digest( - "SHA512", B_HASH_SHA2_512, msg, msg_len, B_DIGEST_LENGTH_SHA2_512); + "SHA512", FX_HASH_SHA2_512, msg, msg_len, FX_DIGEST_LENGTH_SHA2_512); print_digest( - "SHA3-224", B_HASH_SHA3_224, msg, msg_len, - B_DIGEST_LENGTH_SHA3_224); + "SHA3-224", FX_HASH_SHA3_224, msg, msg_len, + FX_DIGEST_LENGTH_SHA3_224); print_digest( - "SHA3-256", B_HASH_SHA3_256, msg, msg_len, - B_DIGEST_LENGTH_SHA3_256); + "SHA3-256", FX_HASH_SHA3_256, msg, msg_len, + FX_DIGEST_LENGTH_SHA3_256); print_digest( - "SHA3-384", B_HASH_SHA3_384, msg, msg_len, - B_DIGEST_LENGTH_SHA3_384); + "SHA3-384", FX_HASH_SHA3_384, msg, msg_len, + FX_DIGEST_LENGTH_SHA3_384); print_digest( - "SHA3-512", B_HASH_SHA3_512, msg, msg_len, - B_DIGEST_LENGTH_SHA3_512); + "SHA3-512", FX_HASH_SHA3_512, msg, msg_len, + FX_DIGEST_LENGTH_SHA3_512); print_digest( - "SHAKE128", B_HASH_SHAKE128, msg, msg_len, - B_DIGEST_LENGTH_SHAKE128); + "SHAKE128", FX_HASH_SHAKE128, msg, msg_len, + FX_DIGEST_LENGTH_SHAKE128); print_digest( - "SHAKE256", B_HASH_SHAKE256, msg, msg_len, - B_DIGEST_LENGTH_SHAKE256); + "SHAKE256", FX_HASH_SHAKE256, msg, msg_len, + FX_DIGEST_LENGTH_SHAKE256); return 0; } diff --git a/test/core/randomise.c b/test/core/randomise.c index 4905c49..2f59e68 100644 --- a/test/core/randomise.c +++ b/test/core/randomise.c @@ -1,4 +1,4 @@ -#include +#include #include #define NRAND_NUMBERS 12 @@ -7,12 +7,12 @@ int main(void) { - b_random_ctx random; - b_random_init(&random, B_RANDOM_SECURE | B_RANDOM_MT19937); + fx_random_ctx random; + fx_random_init(&random, FX_RANDOM_SECURE | FX_RANDOM_MT19937); printf("generating %d random numbers:\n", NRAND_NUMBERS); for (int i = 0; i < NRAND_NUMBERS; i++) { - unsigned long long v = b_random_next_int64(&random); + unsigned long long v = fx_random_next_int64(&random); printf(" %llu\n", v); } @@ -21,7 +21,7 @@ int main(void) for (int i = 0; i < NRAND_BYTES; i++) { if (i == 0 || (i % 16) == 0) { printf("\n "); - b_random_next_bytes(&random, bytes, sizeof bytes); + fx_random_next_bytes(&random, bytes, sizeof bytes); } else if ((i % 4) == 0) { printf(" "); } @@ -31,7 +31,7 @@ int main(void) printf("\n\ngenerating %d random doubles:\n", NRAND_DOUBLES); for (int i = 0; i < NRAND_DOUBLES; i++) { - double v = b_random_next_double(&random); + double v = fx_random_next_double(&random); printf(" %lf\n", v); } diff --git a/test/core/ringbuffers.c b/test/core/ringbuffers.c index ffef689..eb1ab5c 100644 --- a/test/core/ringbuffers.c +++ b/test/core/ringbuffers.c @@ -1,5 +1,5 @@ #include -#include +#include #include #include @@ -7,10 +7,10 @@ int main(void) { - b_ringbuffer *buf = b_ringbuffer_create(BUF_SIZE); + fx_ringbuffer *buf = fx_ringbuffer_create(BUF_SIZE); - size_t read_available = b_ringbuffer_available_data_remaining(buf); - size_t write_available = b_ringbuffer_write_capacity_remaining(buf); + size_t read_available = fx_ringbuffer_available_data_remaining(buf); + size_t write_available = fx_ringbuffer_write_capacity_remaining(buf); printf("read available: %zu\n", read_available); printf("write available: %zu\n", write_available); @@ -21,15 +21,15 @@ int main(void) const char ch = 'X'; printf("putc(%c)\n", ch); - b_ringbuffer_putc(buf, ch); - read_available = b_ringbuffer_available_data_remaining(buf); - write_available = b_ringbuffer_write_capacity_remaining(buf); + fx_ringbuffer_putc(buf, ch); + read_available = fx_ringbuffer_available_data_remaining(buf); + write_available = fx_ringbuffer_write_capacity_remaining(buf); printf("read available: %zu\n", read_available); printf("write available: %zu\n", write_available); assert(read_available == 1); assert(write_available == BUF_SIZE - 2); - int c = b_ringbuffer_getc(buf); + int c = fx_ringbuffer_getc(buf); printf("getc() = %c\n", c); assert(c == ch); @@ -40,9 +40,9 @@ int main(void) size_t nr_written = 0; printf("write(%s)\n", s); - b_ringbuffer_write(buf, s, s_len, &nr_written); - read_available = b_ringbuffer_available_data_remaining(buf); - write_available = b_ringbuffer_write_capacity_remaining(buf); + fx_ringbuffer_write(buf, s, s_len, &nr_written); + read_available = fx_ringbuffer_available_data_remaining(buf); + write_available = fx_ringbuffer_write_capacity_remaining(buf); printf("nr written: %zu\n", nr_written); printf("read available: %zu\n", read_available); printf("write available: %zu\n", write_available); @@ -51,17 +51,17 @@ int main(void) char data[BUF_SIZE + 32] = {0}; size_t nr_read = 0; - b_ringbuffer_read(buf, data, sizeof data, &nr_read); + fx_ringbuffer_read(buf, data, sizeof data, &nr_read); printf("read(%u) = %zu bytes\n", BUF_SIZE + 32, nr_read); printf(" = %s\n", data); - read_available = b_ringbuffer_available_data_remaining(buf); - write_available = b_ringbuffer_write_capacity_remaining(buf); + read_available = fx_ringbuffer_available_data_remaining(buf); + write_available = fx_ringbuffer_write_capacity_remaining(buf); printf("read available: %zu\n", read_available); printf("write available: %zu\n", write_available); assert(read_available == 0); assert(write_available == BUF_SIZE - 1); - b_ringbuffer_unref(buf); + fx_ringbuffer_unref(buf); return 0; } diff --git a/test/core/ropes.c b/test/core/ropes.c index e035e55..24dfc1a 100644 --- a/test/core/ropes.c +++ b/test/core/ropes.c @@ -1,8 +1,8 @@ -#include +#include #include #include -static void print_rope(const struct b_rope *rope, int depth) +static void print_rope(const struct fx_rope *rope, int depth) { for (int i = 0; i < depth; i++) { printf(" "); @@ -10,32 +10,32 @@ static void print_rope(const struct b_rope *rope, int depth) printf("[%x:", rope->r_flags); - (B_ROPE_TYPE(rope->r_flags) == B_ROPE_F_CHAR) && printf(" CHAR"); - (B_ROPE_TYPE(rope->r_flags) == B_ROPE_F_CSTR) && printf(" CSTR"); - (B_ROPE_TYPE(rope->r_flags) == B_ROPE_F_CSTR_BORROWED) + (FX_ROPE_TYPE(rope->r_flags) == FX_ROPE_F_CHAR) && printf(" CHAR"); + (FX_ROPE_TYPE(rope->r_flags) == FX_ROPE_F_CSTR) && printf(" CSTR"); + (FX_ROPE_TYPE(rope->r_flags) == FX_ROPE_F_CSTR_BORROWED) && printf(" CSTR_BORROWED"); - (B_ROPE_TYPE(rope->r_flags) == B_ROPE_F_CSTR_STATIC) + (FX_ROPE_TYPE(rope->r_flags) == FX_ROPE_F_CSTR_STATIC) && printf(" CSTR_STATIC"); - (B_ROPE_TYPE(rope->r_flags) == B_ROPE_F_INT) && printf(" INT"); - (B_ROPE_TYPE(rope->r_flags) == B_ROPE_F_UINT) && printf(" UINT"); - (B_ROPE_TYPE(rope->r_flags) == B_ROPE_F_COMPOSITE) + (FX_ROPE_TYPE(rope->r_flags) == FX_ROPE_F_INT) && printf(" INT"); + (FX_ROPE_TYPE(rope->r_flags) == FX_ROPE_F_UINT) && printf(" UINT"); + (FX_ROPE_TYPE(rope->r_flags) == FX_ROPE_F_COMPOSITE) && printf(" COMPOSITE"); - (rope->r_flags & B_ROPE_F_MALLOC) && printf(" MALLOC"); + (rope->r_flags & FX_ROPE_F_MALLOC) && printf(" MALLOC"); printf("] "); - switch (B_ROPE_TYPE(rope->r_flags)) { - case B_ROPE_F_CHAR: + switch (FX_ROPE_TYPE(rope->r_flags)) { + case FX_ROPE_F_CHAR: printf("%c", rope->r_v.v_char); break; - case B_ROPE_F_CSTR: - case B_ROPE_F_CSTR_BORROWED: - case B_ROPE_F_CSTR_STATIC: + case FX_ROPE_F_CSTR: + case FX_ROPE_F_CSTR_BORROWED: + case FX_ROPE_F_CSTR_STATIC: printf("%s", rope->r_v.v_cstr.s); break; - case B_ROPE_F_INT: + case FX_ROPE_F_INT: printf("%" PRIdPTR, rope->r_v.v_int); break; - case B_ROPE_F_UINT: + case FX_ROPE_F_UINT: printf("%" PRIuPTR, rope->r_v.v_uint); break; default: @@ -44,7 +44,7 @@ static void print_rope(const struct b_rope *rope, int depth) printf("\n"); - if (B_ROPE_TYPE(rope->r_flags) == B_ROPE_F_COMPOSITE) { + if (FX_ROPE_TYPE(rope->r_flags) == FX_ROPE_F_COMPOSITE) { if (rope->r_v.v_composite.r_left) { print_rope(rope->r_v.v_composite.r_left, depth + 1); } @@ -57,27 +57,27 @@ static void print_rope(const struct b_rope *rope, int depth) int main(void) { - b_rope a = B_ROPE_CHAR('a'); - b_rope b = B_ROPE_CSTR_STATIC("Hello, world!"); - b_rope c = B_ROPE_INT(-4096); - b_rope d = B_ROPE_UINT(4096); + fx_rope a = FX_ROPE_CHAR('a'); + fx_rope b = FX_ROPE_CSTR_STATIC("Hello, world!"); + fx_rope c = FX_ROPE_INT(-4096); + fx_rope d = FX_ROPE_UINT(4096); - b_rope str; + fx_rope str; - const b_rope *ropes[] = { + const fx_rope *ropes[] = { &a, &b, &c, &d, }; - b_rope_join(&str, ropes, sizeof ropes / sizeof ropes[0]); + fx_rope_join(&str, ropes, sizeof ropes / sizeof ropes[0]); print_rope(&str, 0); char cstr[1024]; - b_rope_to_cstr(&str, cstr, sizeof cstr); - b_rope_destroy(&str); + fx_rope_to_cstr(&str, cstr, sizeof cstr); + fx_rope_destroy(&str); printf("%s\n", cstr); return 0; diff --git a/test/core/streams.c b/test/core/streams.c index ba55355..ca6c393 100644 --- a/test/core/streams.c +++ b/test/core/streams.c @@ -1,20 +1,20 @@ -#include -#include +#include +#include #include int main(void) { - b_stream_read_line_s(b_stdin, b_stdout); - b_stream_write_char(b_stdout, '\n'); + fx_stream_read_line_s(fx_stdin, fx_stdout); + fx_stream_write_char(fx_stdout, '\n'); char s[16]; - b_bstr str; - b_bstr_begin(&str, s, sizeof s); + fx_bstr str; + fx_bstr_begin(&str, s, sizeof s); - b_stream_read_line_s(b_stdin, (b_stream *)&str); - b_stream_write_char((b_stream *)&str, '\n'); + fx_stream_read_line_s(fx_stdin, (fx_stream *)&str); + fx_stream_write_char((fx_stream *)&str, '\n'); - const char *e = b_bstr_end(&str); + const char *e = fx_bstr_end(&str); fputs(e, stdout); return 0; diff --git a/test/ds/arrays.c b/test/ds/arrays.c index b78b62a..a7facd0 100644 --- a/test/ds/arrays.c +++ b/test/ds/arrays.c @@ -1,21 +1,21 @@ -#include -#include +#include +#include #include int main(void) { - b_array *array = b_array_create(); - b_array_append(array, B_RV_INT(32)); - b_array_append(array, B_RV_INT(64)); - b_array_append(array, B_RV_INT(128)); + fx_array *array = fx_array_create(); + fx_array_append(array, FX_RV_INT(32)); + fx_array_append(array, FX_RV_INT(64)); + fx_array_append(array, FX_RV_INT(128)); - b_iterator *it = b_iterator_begin(array); - b_foreach_ptr(b_object, obj, it) + fx_iterator *it = fx_iterator_begin(array); + fx_foreach_ptr(fx_object, obj, it) { printf("object %p\n", obj); } - b_iterator_unref(it); + fx_iterator_unref(it); - b_array_unref(array); + fx_array_unref(array); return 0; } diff --git a/test/ds/ds-units.c b/test/ds/ds-units.c index d7b3020..c157158 100644 --- a/test/ds/ds-units.c +++ b/test/ds/ds-units.c @@ -1,42 +1,42 @@ #include -#include +#include static void test_string_create(CuTest *tc) { - b_string *str = b_string_create(); + fx_string *str = fx_string_create(); CuAssertPtrNotNull(tc, str); - CuAssertIntEquals(tc, 0, b_string_get_size(str, B_STRLEN_NORMAL)); - CuAssertStrEquals(tc, "", b_string_ptr(str)); + CuAssertIntEquals(tc, 0, fx_string_get_size(str, FX_STRLEN_NORMAL)); + CuAssertStrEquals(tc, "", fx_string_ptr(str)); - b_string_unref(str); + fx_string_unref(str); - str = b_string_create_from_c('A', 8); + str = fx_string_create_from_c('A', 8); CuAssertPtrNotNull(tc, str); - CuAssertIntEquals(tc, 8, b_string_get_size(str, B_STRLEN_NORMAL)); - CuAssertStrEquals(tc, "AAAAAAAA", b_string_ptr(str)); + CuAssertIntEquals(tc, 8, fx_string_get_size(str, FX_STRLEN_NORMAL)); + CuAssertStrEquals(tc, "AAAAAAAA", fx_string_ptr(str)); - b_string_unref(str); + fx_string_unref(str); - str = b_string_create_from_cstr("Hello, world!"); + str = fx_string_create_from_cstr("Hello, world!"); CuAssertPtrNotNull(tc, str); - CuAssertIntEquals(tc, 13, b_string_get_size(str, B_STRLEN_NORMAL)); - CuAssertStrEquals(tc, "Hello, world!", b_string_ptr(str)); + CuAssertIntEquals(tc, 13, fx_string_get_size(str, FX_STRLEN_NORMAL)); + CuAssertStrEquals(tc, "Hello, world!", fx_string_ptr(str)); - b_string_unref(str); + fx_string_unref(str); } static void test_string_length(CuTest *tc) { const char *cstr = "Hello, \033[91;1mworld!"; - b_string *s = b_string_create_from_cstr(cstr); + fx_string *s = fx_string_create_from_cstr(cstr); - CuAssertIntEquals(tc, 13, b_string_get_size(s, B_STRLEN_IGNORE_ESC)); - CuAssertIntEquals(tc, 20, b_string_get_size(s, B_STRLEN_NORMAL)); + CuAssertIntEquals(tc, 13, fx_string_get_size(s, FX_STRLEN_IGNORE_ESC)); + CuAssertIntEquals(tc, 20, fx_string_get_size(s, FX_STRLEN_NORMAL)); - b_string_unref(s); + fx_string_unref(s); } CuSuite *get_all_tests(void) diff --git a/test/ds/numbers.c b/test/ds/numbers.c index c777734..a904d8e 100644 --- a/test/ds/numbers.c +++ b/test/ds/numbers.c @@ -1,11 +1,11 @@ -#include +#include #include int main(void) { - b_number *number = b_number_create_float(6.8); + fx_number *number = fx_number_create_float(6.8); - printf("number=%zd\n", B_NUMBER_IVAL(number)); - b_number_unref(number); + printf("number=%zd\n", FX_NUMBER_IVAL(number)); + fx_number_unref(number); return 0; } diff --git a/test/ds/simple.c b/test/ds/simple.c index d9db6e1..46b260c 100644 --- a/test/ds/simple.c +++ b/test/ds/simple.c @@ -1,11 +1,11 @@ -#include +#include int main(void) { - b_string *string = B_CSTR("Hello, world!"); + fx_string *string = FX_CSTR("Hello, world!"); printf("string object = "); - b_object_to_string(string, b_stdout); + fx_object_to_string(string, fx_stdout); printf("\n"); - b_string_unref(string); + fx_string_unref(string); return 0; } diff --git a/test/ds/streams.c b/test/ds/streams.c index b606cfa..b89699c 100644 --- a/test/ds/streams.c +++ b/test/ds/streams.c @@ -1,19 +1,19 @@ -#include -#include -#include +#include +#include +#include #include int main(int argc, const char **argv) { size_t nr_read = 0; - b_stringstream *dest_stream = b_stringstream_create(); - b_stream_buffer *buf = b_stream_buffer_create_dynamic(1024); - b_stream_read_all_bytes_s(b_stdin, dest_stream, buf, &nr_read); + fx_stringstream *dest_stream = fx_stringstream_create(); + fx_stream_buffer *buf = fx_stream_buffer_create_dynamic(1024); + fx_stream_read_all_bytes_s(fx_stdin, dest_stream, buf, &nr_read); printf("done. read %zu bytes total.\n", nr_read); - printf("%s\n", b_stringstream_ptr(dest_stream)); + printf("%s\n", fx_stringstream_ptr(dest_stream)); - b_stringstream_unref(dest_stream); - b_stream_buffer_unref(buf); + fx_stringstream_unref(dest_stream); + fx_stream_buffer_unref(buf); return 0; } diff --git a/test/ds/strings.c b/test/ds/strings.c index e4c8f66..bcc6461 100644 --- a/test/ds/strings.c +++ b/test/ds/strings.c @@ -1,40 +1,40 @@ -#include -#include +#include +#include #include #include int main(void) { printf("-------------\n"); - b_string *str = b_string_create_from_cstr("Hello, world!\n"); - printf("%s\n", b_string_ptr(str)); - printf("len:%zu, max:%zu\n", b_string_get_size(str, B_STRLEN_NORMAL), - b_string_get_capacity(str)); + fx_string *str = fx_string_create_from_cstr("Hello, world!\n"); + printf("%s\n", fx_string_ptr(str)); + printf("len:%zu, max:%zu\n", fx_string_get_size(str, FX_STRLEN_NORMAL), + fx_string_get_capacity(str)); - b_string_insert_cstr(str, "WOW!", 4); + fx_string_insert_cstr(str, "WOW!", 4); printf("-------------\n"); - printf("%s\n", b_string_ptr(str)); - printf("len:%zu, max:%zu\n", b_string_get_size(str, B_STRLEN_NORMAL), - b_string_get_capacity(str)); + printf("%s\n", fx_string_ptr(str)); + printf("len:%zu, max:%zu\n", fx_string_get_size(str, FX_STRLEN_NORMAL), + fx_string_get_capacity(str)); - b_string_replace(str, 4, 4, "+"); + fx_string_replace(str, 4, 4, "+"); printf("-------------\n"); - printf("%s\n", b_string_ptr(str)); - printf("len:%zu, max:%zu\n", b_string_get_size(str, B_STRLEN_NORMAL), - b_string_get_capacity(str)); + printf("%s\n", fx_string_ptr(str)); + printf("len:%zu, max:%zu\n", fx_string_get_size(str, FX_STRLEN_NORMAL), + fx_string_get_capacity(str)); printf("-------------\n"); - b_string_unref(str); + fx_string_unref(str); - b_stringstream *strv = b_stringstream_create(); - b_stream_write_string(strv, "Hello", NULL); - b_stream_write_string(strv, ", world", NULL); - b_stream_write_string(strv, "!", NULL); + fx_stringstream *strv = fx_stringstream_create(); + fx_stream_write_string(strv, "Hello", NULL); + fx_stream_write_string(strv, ", world", NULL); + fx_stream_write_string(strv, "!", NULL); - char *s = b_stringstream_steal(strv); - b_stringstream_unref(strv); + char *s = fx_stringstream_steal(strv); + fx_stringstream_unref(strv); printf("%s\n", s); free(s); diff --git a/test/ds/trees.c b/test/ds/trees.c index c56c635..f0da4e2 100644 --- a/test/ds/trees.c +++ b/test/ds/trees.c @@ -1,68 +1,68 @@ -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #include #define NITEMS 16 struct tree_item { int value; - b_tree_node node; + fx_tree_node node; }; struct btree_item { int value; - b_btree_node node; + fx_bst_node node; }; -B_BTREE_DEFINE_SIMPLE_GET(struct btree_item, int, node, value, get_node) -B_BTREE_DEFINE_SIMPLE_INSERT(struct btree_item, node, value, put_node) +FX_BTREE_DEFINE_SIMPLE_GET(struct btree_item, int, node, value, get_node) +FX_BTREE_DEFINE_SIMPLE_INSERT(struct btree_item, node, value, put_node) int main(void) { - b_dict *dict = b_dict_create(); - b_dict_put(dict, "hello", B_RV_INT(32)); - b_dict_put(dict, "world", B_RV_INT(64)); - b_dict_put(dict, "more", B_RV_INT(128)); - b_dict_put(dict, "other", B_RV_INT(256)); + fx_dict *dict = fx_dict_create(); + fx_dict_put(dict, "hello", FX_RV_INT(32)); + fx_dict_put(dict, "world", FX_RV_INT(64)); + fx_dict_put(dict, "more", FX_RV_INT(128)); + fx_dict_put(dict, "other", FX_RV_INT(256)); - b_iterator *it = b_iterator_begin(dict); + fx_iterator *it = fx_iterator_begin(dict); size_t i = 0; - b_foreach(b_dict_item *, item, it) + fx_foreach(fx_dict_item *, item, it) { - printf("item %zu: %s=%d\n", i++, b_string_ptr(item->key), - b_number_get_int(item->value)); + printf("item %zu: %s=%d\n", i++, fx_string_ptr(item->key), + fx_number_get_int(item->value)); } - b_iterator_unref(it); + fx_iterator_unref(it); - b_tree *tree = b_tree_create(); + 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 = B_TREE_NODE_INIT; + items2[i].node = FX_TREE_NODE_INIT; } - b_tree_set_root(tree, &items2[0].node); + fx_tree_set_root(tree, &items2[0].node); - b_tree_node_add_child(&items2[0].node, &items2[1].node); - b_tree_node_add_child(&items2[0].node, &items2[2].node); - b_tree_node_add_child(&items2[0].node, &items2[3].node); - b_tree_node_add_child(&items2[0].node, &items2[7].node); - b_tree_node_add_child(&items2[1].node, &items2[4].node); - b_tree_node_add_child(&items2[1].node, &items2[5].node); - b_tree_node_add_child(&items2[4].node, &items2[6].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 = b_iterator_begin(tree); - b_tree_iterator it2; - b_tree_foreach(&it2, tree) + it = fx_iterator_begin(tree); + fx_tree_iterator it2; + fx_tree_foreach(&it2, tree) { - struct tree_item *item = b_unbox(struct tree_item, it2.node, node); + struct tree_item *item = fx_unbox(struct tree_item, it2.node, node); for (size_t i = 0; i < it2.depth; i++) { fputs(" ", stdout); @@ -71,7 +71,7 @@ int main(void) printf("%u\n", item->value); } - b_btree btree = {0}; + fx_bst btree = {0}; struct btree_item items3[NITEMS] = {0}; for (int i = 0; i < NITEMS; i++) { items3[i].value = i; @@ -80,10 +80,10 @@ int main(void) printf("\n\n"); - b_btree_iterator it3; - b_btree_foreach (&it3, &btree) { + fx_bst_iterator it3; + fx_bst_foreach (&it3, &btree) { struct btree_item *item - = b_unbox(struct btree_item, it3.node, node); + = fx_unbox(struct btree_item, it3.node, node); for (size_t i = 0; i < it3.depth; i++) { fputs(" ", stdout); @@ -92,23 +92,23 @@ int main(void) printf("%d\n", item->value); } - b_btree_iterator_begin(&btree, &it3); - while (b_btree_iterator_is_valid(&it3)) { + fx_bst_iterator_begin(&btree, &it3); + while (fx_bst_iterator_is_valid(&it3)) { struct btree_item *item - = b_unbox(struct btree_item, it3.node, node); + = fx_unbox(struct btree_item, it3.node, node); if (item->value == 9) { - b_btree_iterator_erase(&it3); + fx_bst_iterator_erase(&it3); } else { - b_btree_iterator_next(&it3); + fx_bst_iterator_next(&it3); } } printf("\n\n"); - b_btree_foreach (&it3, &btree) { + fx_bst_foreach (&it3, &btree) { struct btree_item *item - = b_unbox(struct btree_item, it3.node, node); + = fx_unbox(struct btree_item, it3.node, node); for (size_t i = 0; i < it3.depth; i++) { fputs(" ", stdout); @@ -117,9 +117,9 @@ int main(void) printf("%d\n", item->value); } - b_tree_unref(tree); + fx_tree_unref(tree); #endif - b_dict_unref(dict); + fx_dict_unref(dict); return 0; } diff --git a/test/ds/unicode-strings.c b/test/ds/unicode-strings.c index bc4b657..7976286 100644 --- a/test/ds/unicode-strings.c +++ b/test/ds/unicode-strings.c @@ -1,27 +1,27 @@ -#include -#include +#include +#include #include #include int main(void) { printf("здравс\u26A0твуите\n"); - b_string *str = b_string_create_from_cstr("здравствуите"); - const char *s = b_string_ptr(str); + fx_string *str = fx_string_create_from_cstr("здравствуите"); + const char *s = fx_string_ptr(str); printf("%s\n", s); - printf("len: %zu\n", b_string_get_size(str, B_STRLEN_NORMAL)); - printf("codepoints: %zu\n", b_string_get_size(str, B_STRLEN_CODEPOINTS)); + printf("len: %zu\n", fx_string_get_size(str, FX_STRLEN_NORMAL)); + printf("codepoints: %zu\n", fx_string_get_size(str, FX_STRLEN_CODEPOINTS)); const char *delims[] = {"в"}; size_t nr_delims = sizeof delims / sizeof delims[0]; - b_iterator *it = b_string_tokenise(str, delims, nr_delims, 0); - b_foreach(const char *, tok, it) + fx_iterator *it = fx_string_tokenise(str, delims, nr_delims, 0); + fx_foreach(const char *, tok, it) { printf("%s\n", tok); } - b_iterator_unref(it); - b_string_unref(str); + fx_iterator_unref(it); + fx_string_unref(str); return 0; } diff --git a/test/ds/uuids.c b/test/ds/uuids.c index f4977b6..885230f 100644 --- a/test/ds/uuids.c +++ b/test/ds/uuids.c @@ -1,14 +1,14 @@ -#include +#include #include int main(void) { - b_uuid *uuid = b_uuid_create_from_cstr( + fx_uuid *uuid = fx_uuid_create_from_cstr( "5b80ad1f-367f-4a1f-88f3-b3a6f8d1f63d"); - char str[B_UUID_STRING_MAX]; - b_uuid_to_cstr(uuid, str); + char str[FX_UUID_STRING_MAX]; + fx_uuid_to_cstr(uuid, str); printf("%s\n", str); - b_uuid_unref(uuid); + fx_uuid_unref(uuid); return 0; } diff --git a/test/io/io-units.c b/test/io/io-units.c index 7310ab4..2a058ac 100644 --- a/test/io/io-units.c +++ b/test/io/io-units.c @@ -1,27 +1,27 @@ #include -#include -#include +#include +#include #include void test_path_1(CuTest *tc) { - b_path *path = b_path_create_from_cstr("C:\\hello\\world\\"); + fx_path *path = fx_path_create_from_cstr("C:\\hello\\world\\"); char buf[512]; - b_stringstream *str = b_stringstream_create_with_buffer(buf, sizeof buf); + fx_stringstream *str = fx_stringstream_create_with_buffer(buf, sizeof buf); - b_object_to_string(path, str); + fx_object_to_string(path, str); printf("%s\n", buf); - b_path *path2 = b_path_create_from_cstr("path1\\path2\\"); - b_path *path3 = b_path_create_from_cstr("path3\\path4\\"); + fx_path *path2 = fx_path_create_from_cstr("path1\\path2\\"); + fx_path *path3 = fx_path_create_from_cstr("path3\\path4\\"); - const b_path *paths[] = {path, path2, path3}; + const fx_path *paths[] = {path, path2, path3}; - b_path *path4 = b_path_join(paths, sizeof paths / sizeof paths[0]); + fx_path *path4 = fx_path_join(paths, sizeof paths / sizeof paths[0]); - b_stringstream_reset_with_buffer(str, buf, sizeof buf); - b_object_to_string(path4, str); + fx_stringstream_reset_with_buffer(str, buf, sizeof buf); + fx_object_to_string(path4, str); printf("%s\n", buf); } diff --git a/test/io/mkdir.c b/test/io/mkdir.c index 3094bc6..703977f 100644 --- a/test/io/mkdir.c +++ b/test/io/mkdir.c @@ -1,5 +1,5 @@ -#include -#include +#include +#include int main(int argc, const char **argv) { @@ -9,14 +9,14 @@ int main(int argc, const char **argv) const char *path = argv[1]; - b_directory *dir; - b_result result = b_directory_open( - NULL, B_RV_PATH(path), B_DIRECTORY_OPEN_CREATE_INTERMEDIATE, &dir); - if (b_result_is_error(result)) { - b_throw(result); + fx_directory *dir; + fx_result result = fx_directory_open( + NULL, FX_RV_PATH(path), FX_DIRECTORY_OPEN_CREATE_INTERMEDIATE, &dir); + if (fx_result_is_error(result)) { + fx_throw(result); return -1; } - b_directory_unref(dir); + fx_directory_unref(dir); return 0; } diff --git a/test/io/rmdir.c b/test/io/rmdir.c index d2b8238..472af28 100644 --- a/test/io/rmdir.c +++ b/test/io/rmdir.c @@ -1,4 +1,4 @@ -#include +#include int main(int argc, const char **argv) { @@ -8,16 +8,16 @@ int main(int argc, const char **argv) const char *path = argv[1]; - b_directory *dir; - b_result result = b_directory_open(NULL, B_RV_PATH(path), 0, &dir); - if (b_result_is_error(result)) { - b_throw(result); + fx_directory *dir; + fx_result result = fx_directory_open(NULL, FX_RV_PATH(path), 0, &dir); + if (fx_result_is_error(result)) { + fx_throw(result); return -1; } - result = b_directory_delete(dir); - if (b_result_is_error(result)) { - b_throw(result); + result = fx_directory_delete(dir); + if (fx_result_is_error(result)) { + fx_throw(result); return -1; } diff --git a/test/io/streams.c b/test/io/streams.c index 29f0a71..8f49fe8 100644 --- a/test/io/streams.c +++ b/test/io/streams.c @@ -1,26 +1,26 @@ -#include -#include -#include +#include +#include +#include #include int main(int argc, const char **argv) { - b_file *dest; - b_path *path = b_path_create_from_cstr("data.txt"); - b_result result = b_file_open( - NULL, path, B_FILE_WRITE_ONLY | B_FILE_CREATE, &dest); - if (b_result_is_error(result)) { - b_throw(result); + fx_file *dest; + fx_path *path = fx_path_create_from_cstr("data.txt"); + fx_result result = fx_file_open( + NULL, path, FX_FILE_WRITE_ONLY | FX_FILE_CREATE, &dest); + if (fx_result_is_error(result)) { + fx_throw(result); return -1; } size_t nr_read = 0; - b_stream_buffer *buf = b_stream_buffer_create_dynamic(1024); - b_stream_read_all_bytes_s(b_stdin, dest, buf, &nr_read); + fx_stream_buffer *buf = fx_stream_buffer_create_dynamic(1024); + fx_stream_read_all_bytes_s(fx_stdin, dest, buf, &nr_read); printf("done. read %zu bytes total.\n", nr_read); - b_path_unref(path); - b_file_unref(dest); + fx_path_unref(path); + fx_file_unref(dest); return 0; } diff --git a/test/io/tree.c b/test/io/tree.c index c772bcc..1189f18 100644 --- a/test/io/tree.c +++ b/test/io/tree.c @@ -1,4 +1,4 @@ -#include +#include #include #define NRAND_NUMBERS 12 @@ -11,22 +11,22 @@ int main(int argc, const char **argv) return -1; } - b_directory *dir = NULL; - b_path *path = b_path_create_from_cstr(argv[1]); - b_result result = b_directory_open(B_DIRECTORY_ROOT, path, 0, &dir); + fx_directory *dir = NULL; + fx_path *path = fx_path_create_from_cstr(argv[1]); + fx_result result = fx_directory_open(FX_DIRECTORY_ROOT, path, 0, &dir); - if (b_result_is_error(result)) { - b_throw(result); + if (fx_result_is_error(result)) { + fx_throw(result); return -1; } - b_iterator *it = b_directory_begin(dir, B_DIRECTORY_ITERATE_PARENT_FIRST); - b_foreach(b_directory_entry *, entry, it) + fx_iterator *it = fx_directory_begin(dir, FX_DIRECTORY_ITERATE_PARENT_FIRST); + fx_foreach(fx_directory_entry *, entry, it) { - printf("%s\n", b_path_ptr(entry->filepath)); + printf("%s\n", fx_path_ptr(entry->filepath)); } - b_iterator_unref(it); + fx_iterator_unref(it); return 0; } diff --git a/test/json-read.c b/test/json-read.c index 066297f..ae46346 100644 --- a/test/json-read.c +++ b/test/json-read.c @@ -1,6 +1,6 @@ -#include -#include -#include +#include +#include +#include #include int main(int argc, const char **argv) @@ -10,31 +10,31 @@ int main(int argc, const char **argv) } const char *path_cstr = argv[1]; - b_path *path = b_path_create_from_cstr(path_cstr); + fx_path *path = fx_path_create_from_cstr(path_cstr); - b_file *src = NULL; - b_result result = b_file_open(NULL, path, B_FILE_READ_ONLY, &src); - if (b_result_is_error(result)) { - b_throw(result); + fx_file *src = NULL; + fx_result result = fx_file_open(NULL, path, FX_FILE_READ_ONLY, &src); + if (fx_result_is_error(result)) { + fx_throw(result); return -1; } /* TODO re-implement json support */ - b_serial_ctx *ctx = NULL; + fx_serial_ctx *ctx = NULL; - b_object *data; - b_status status = b_serial_ctx_deserialise(ctx, src, &data, 0); - if (!B_OK(status)) { + fx_object *data; + fx_status status = fx_serial_ctx_deserialise(ctx, src, &data, 0); + if (!FX_OK(status)) { fprintf(stderr, "cannot read data\n"); return -1; } - b_object_to_string(data, b_stdout); - b_stream_write_char(b_stdout, '\n'); + fx_object_to_string(data, fx_stdout); + fx_stream_write_char(fx_stdout, '\n'); - b_object_unref(data); - b_object_unref(src); - b_serial_ctx_unref(ctx); + fx_object_unref(data); + fx_object_unref(src); + fx_serial_ctx_unref(ctx); return 0; } diff --git a/test/serial/streams.c b/test/serial/streams.c index 5374d2d..f52b0b9 100644 --- a/test/serial/streams.c +++ b/test/serial/streams.c @@ -1,37 +1,37 @@ -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include #include int main(void) { - b_serial_ctx *ctx = b_toml_serial_ctx_create(); + fx_serial_ctx *ctx = fx_toml_serial_ctx_create(); - b_dict *dict = b_dict_create(); + fx_dict *dict = fx_dict_create(); - b_array *array = b_array_create(); - b_array_append(array, B_RV_INT(32)); - b_array_append(array, B_RV_INT(64)); - b_array_append(array, B_RV_INT(128)); + fx_array *array = fx_array_create(); + fx_array_append(array, FX_RV_INT(32)); + fx_array_append(array, FX_RV_INT(64)); + fx_array_append(array, FX_RV_INT(128)); - b_dict_put(dict, "numbers", B_RV(array)); + fx_dict_put(dict, "numbers", FX_RV(array)); - array = b_array_create(); - b_array_append(array, B_RV_CSTR("hello")); - b_array_append(array, B_RV_CSTR("world")); + array = fx_array_create(); + fx_array_append(array, FX_RV_CSTR("hello")); + fx_array_append(array, FX_RV_CSTR("world")); - b_dict_put(dict, "strings", B_RV(array)); + fx_dict_put(dict, "strings", FX_RV(array)); - b_object_to_string(dict, b_stdout); - b_stream_write_char(b_stdout, '\n'); + fx_object_to_string(dict, fx_stdout); + fx_stream_write_char(fx_stdout, '\n'); - b_serial_ctx_serialise(ctx, dict, b_stdout, 0); + fx_serial_ctx_serialise(ctx, dict, fx_stdout, 0); - b_dict_unref(dict); - b_serial_ctx_unref(ctx); + fx_dict_unref(dict); + fx_serial_ctx_unref(ctx); return 0; } diff --git a/test/serial/toml-decode.c b/test/serial/toml-decode.c index eb71483..70b8ee0 100644 --- a/test/serial/toml-decode.c +++ b/test/serial/toml-decode.c @@ -1,196 +1,196 @@ -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include #include -void write_tagged_value(b_object *data); +void write_tagged_value(fx_object *data); -void write_raw_string(const b_string *data) +void write_raw_string(const fx_string *data) { - b_stream_write_string(b_stdout, "\"", NULL); + fx_stream_write_string(fx_stdout, "\"", NULL); - const b_iterator *it = b_iterator_cbegin(data); - b_foreach_c(b_wchar, c, it) + const fx_iterator *it = fx_iterator_cbegin(data); + fx_foreach_c(fx_wchar, c, it) { if (c >= 0x10000) { c -= 0x10000; long hi = 0xD800 | ((c >> 10) & 0x3FF); long lo = 0xDC00 | (c & 0x3FF); - b_stream_write_fmt(b_stdout, NULL, "\\u%04x\\u%04x", hi, lo); + fx_stream_write_fmt(fx_stdout, NULL, "\\u%04x\\u%04x", hi, lo); } else if (c <= 0x1F || c >= 0x7F) { - b_stream_write_fmt(b_stdout, NULL, "\\u%04x", c); + fx_stream_write_fmt(fx_stdout, NULL, "\\u%04x", c); } else if (c == '\\' || c == '"') { - b_stream_write_fmt(b_stdout, NULL, "\\%c", c); + fx_stream_write_fmt(fx_stdout, NULL, "\\%c", c); } else { - b_stream_write_char(b_stdout, c); + fx_stream_write_char(fx_stdout, c); } } - b_iterator_unref(it); + fx_iterator_unref(it); - b_stream_write_string(b_stdout, "\"", NULL); + fx_stream_write_string(fx_stdout, "\"", NULL); } -void write_tagged_string(b_string *data) +void write_tagged_string(fx_string *data) { - b_stream_write_string(b_stdout, "{ \"type\": \"string\", \"value\": ", NULL); + fx_stream_write_string(fx_stdout, "{ \"type\": \"string\", \"value\": ", NULL); write_raw_string(data); - b_stream_write_string(b_stdout, " }", NULL); + fx_stream_write_string(fx_stdout, " }", NULL); } -void write_tagged_integer(b_number *data) +void write_tagged_integer(fx_number *data) { - b_stream_write_string( - b_stdout, "{ \"type\": \"integer\", \"value\": \"", NULL); + fx_stream_write_string( + fx_stdout, "{ \"type\": \"integer\", \"value\": \"", NULL); - if (b_number_is_inf_positive(data)) { - b_stream_write_string(b_stdout, "inf", NULL); - } else if (b_number_is_inf_negative(data)) { - b_stream_write_string(b_stdout, "-inf", NULL); - } else if (b_number_is_nan_positive(data)) { - b_stream_write_string(b_stdout, "nan", NULL); - } else if (b_number_is_nan_negative(data)) { - b_stream_write_string(b_stdout, "-nan", NULL); + if (fx_number_is_inf_positive(data)) { + fx_stream_write_string(fx_stdout, "inf", NULL); + } else if (fx_number_is_inf_negative(data)) { + fx_stream_write_string(fx_stdout, "-inf", NULL); + } else if (fx_number_is_nan_positive(data)) { + fx_stream_write_string(fx_stdout, "nan", NULL); + } else if (fx_number_is_nan_negative(data)) { + fx_stream_write_string(fx_stdout, "-nan", NULL); } else { - b_stream_write_fmt( - b_stdout, NULL, "%lld", b_number_get_longlong(data), NULL); + fx_stream_write_fmt( + fx_stdout, NULL, "%lld", fx_number_get_longlong(data), NULL); } - b_stream_write_string(b_stdout, "\" }", NULL); + fx_stream_write_string(fx_stdout, "\" }", NULL); } -void write_tagged_float(b_number *data) +void write_tagged_float(fx_number *data) { - b_stream_write_string( - b_stdout, "{ \"type\": \"float\", \"value\": \"", NULL); + fx_stream_write_string( + fx_stdout, "{ \"type\": \"float\", \"value\": \"", NULL); - if (b_number_is_inf_positive(data)) { - b_stream_write_string(b_stdout, "inf", NULL); - } else if (b_number_is_inf_negative(data)) { - b_stream_write_string(b_stdout, "-inf", NULL); - } else if (b_number_is_nan_positive(data)) { - b_stream_write_string(b_stdout, "nan", NULL); - } else if (b_number_is_nan_negative(data)) { - b_stream_write_string(b_stdout, "-nan", NULL); + if (fx_number_is_inf_positive(data)) { + fx_stream_write_string(fx_stdout, "inf", NULL); + } else if (fx_number_is_inf_negative(data)) { + fx_stream_write_string(fx_stdout, "-inf", NULL); + } else if (fx_number_is_nan_positive(data)) { + fx_stream_write_string(fx_stdout, "nan", NULL); + } else if (fx_number_is_nan_negative(data)) { + fx_stream_write_string(fx_stdout, "-nan", NULL); } else { - double v = b_number_get_double(data); + double v = fx_number_get_double(data); if ((v <= 0.00000001 && v > 0) || (v >= -0.00000001 && v < 0) || (v >= 1000000000) || (v <= -1000000000)) { - b_stream_write_fmt(b_stdout, NULL, "%.15e", v, NULL); + fx_stream_write_fmt(fx_stdout, NULL, "%.15e", v, NULL); } else { - b_stream_write_fmt(b_stdout, NULL, "%.15f", v, NULL); + fx_stream_write_fmt(fx_stdout, NULL, "%.15f", v, NULL); } } - b_stream_write_string(b_stdout, "\" }", NULL); + fx_stream_write_string(fx_stdout, "\" }", NULL); } -void write_tagged_bool(b_number *data) +void write_tagged_bool(fx_number *data) { - int v = b_number_get_int8(data); - b_stream_write_fmt( - b_stdout, NULL, "{ \"type\": \"bool\", \"value\": \"%s\" }", + int v = fx_number_get_int8(data); + fx_stream_write_fmt( + fx_stdout, NULL, "{ \"type\": \"bool\", \"value\": \"%s\" }", (v > 0) ? "true" : "false", NULL); } -void write_tagged_datetime(b_datetime *data) +void write_tagged_datetime(fx_datetime *data) { - bool has_date = b_datetime_has_date(data); - bool has_time = b_datetime_has_time(data); - bool localtime = b_datetime_is_localtime(data); + bool has_date = fx_datetime_has_date(data); + bool has_time = fx_datetime_has_time(data); + bool localtime = fx_datetime_is_localtime(data); - b_stream_write_string(b_stdout, "{ \"type\": \"", NULL); + fx_stream_write_string(fx_stdout, "{ \"type\": \"", NULL); if (has_date && has_time) { - b_stream_write_string( - b_stdout, localtime ? "datetime-local" : "datetime", NULL); + fx_stream_write_string( + fx_stdout, localtime ? "datetime-local" : "datetime", NULL); } else if (has_date) { - b_stream_write_string( - b_stdout, localtime ? "date-local" : "date", NULL); + fx_stream_write_string( + fx_stdout, localtime ? "date-local" : "date", NULL); } else if (has_time) { - b_stream_write_string( - b_stdout, localtime ? "time-local" : "time", NULL); + fx_stream_write_string( + fx_stdout, localtime ? "time-local" : "time", NULL); } - b_stream_write_string(b_stdout, "\", \"value\": \"", NULL); + fx_stream_write_string(fx_stdout, "\", \"value\": \"", NULL); - b_string *new_data = b_string_create(); - b_datetime_to_string(data, B_DATETIME_FORMAT_RFC3339, new_data); - b_stream_write_string(b_stdout, b_string_ptr(new_data), NULL); + fx_string *new_data = fx_string_create(); + fx_datetime_to_string(data, FX_DATETIME_FORMAT_RFC3339, new_data); + fx_stream_write_string(fx_stdout, fx_string_ptr(new_data), NULL); - b_stream_write_string(b_stdout, "\" }", NULL); + fx_stream_write_string(fx_stdout, "\" }", NULL); - b_string_unref(new_data); + fx_string_unref(new_data); } -void write_tagged_dict(b_dict *data) +void write_tagged_dict(fx_dict *data) { - b_stream_write_string(b_stdout, "{ ", NULL); + fx_stream_write_string(fx_stdout, "{ ", NULL); int i = 0; - b_iterator *it = b_iterator_begin(data); - b_foreach(b_dict_item *, item, it) + fx_iterator *it = fx_iterator_begin(data); + fx_foreach(fx_dict_item *, item, it) { if (i++ > 0) { - b_stream_write_string(b_stdout, ", ", NULL); + fx_stream_write_string(fx_stdout, ", ", NULL); } write_raw_string(item->key); - b_stream_write_string(b_stdout, ": ", NULL); + fx_stream_write_string(fx_stdout, ": ", NULL); write_tagged_value(item->value); } - b_iterator_unref(it); + fx_iterator_unref(it); - b_stream_write_string(b_stdout, " }", NULL); + fx_stream_write_string(fx_stdout, " }", NULL); } -void write_tagged_array(b_array *data) +void write_tagged_array(fx_array *data) { - b_stream_write_string(b_stdout, "[ ", NULL); + fx_stream_write_string(fx_stdout, "[ ", NULL); int i = 0; - b_iterator *it = b_iterator_begin(data); - b_foreach(b_object *, obj, it) + fx_iterator *it = fx_iterator_begin(data); + fx_foreach(fx_object *, obj, it) { if (i++ > 0) { - b_stream_write_string(b_stdout, ", ", NULL); + fx_stream_write_string(fx_stdout, ", ", NULL); } write_tagged_value(obj); } - b_iterator_unref(it); + fx_iterator_unref(it); - b_stream_write_string(b_stdout, " ]", NULL); + fx_stream_write_string(fx_stdout, " ]", NULL); } -void write_tagged_value(b_object *data) +void write_tagged_value(fx_object *data) { - if (b_object_is_type(data, B_TYPE_DICT)) { + if (fx_object_is_type(data, FX_TYPE_DICT)) { write_tagged_dict(data); - } else if (b_object_is_type(data, B_TYPE_ARRAY)) { + } else if (fx_object_is_type(data, FX_TYPE_ARRAY)) { write_tagged_array(data); - } else if (b_object_is_type(data, B_TYPE_STRING)) { + } else if (fx_object_is_type(data, FX_TYPE_STRING)) { write_tagged_string(data); - } else if (b_object_is_type(data, B_TYPE_DATETIME)) { + } else if (fx_object_is_type(data, FX_TYPE_DATETIME)) { write_tagged_datetime(data); - } else if (b_object_is_type(data, B_TYPE_NUMBER)) { - switch (b_number_get_number_type(data)) { - case B_NUMBER_LONGLONG: + } else if (fx_object_is_type(data, FX_TYPE_NUMBER)) { + switch (fx_number_get_number_type(data)) { + case FX_NUMBER_LONGLONG: write_tagged_integer(data); break; - case B_NUMBER_INT8: + case FX_NUMBER_INT8: write_tagged_bool(data); break; - case B_NUMBER_DOUBLE: + case FX_NUMBER_DOUBLE: write_tagged_float(data); break; default: @@ -201,23 +201,23 @@ void write_tagged_value(b_object *data) int main(void) { - b_stream *src = b_stdin; - b_stream *dest = b_stdout; + fx_stream *src = fx_stdin; + fx_stream *dest = fx_stdout; - b_serial_ctx *ctx = b_toml_serial_ctx_create(); + fx_serial_ctx *ctx = fx_toml_serial_ctx_create(); - b_object *data; - b_status status = b_serial_ctx_deserialise(ctx, src, &data, 0); - if (!B_OK(status)) { + fx_object *data; + fx_status status = fx_serial_ctx_deserialise(ctx, src, &data, 0); + if (!FX_OK(status)) { return 1; } write_tagged_value(data); - b_stream_write_char(b_stdout, '\n'); + fx_stream_write_char(fx_stdout, '\n'); - b_serial_ctx_unref(ctx); - b_object_unref(data); + fx_serial_ctx_unref(ctx); + fx_object_unref(data); return 0; } diff --git a/test/serial/toml-encode.c b/test/serial/toml-encode.c index dc25722..4f7903b 100644 --- a/test/serial/toml-encode.c +++ b/test/serial/toml-encode.c @@ -1,26 +1,26 @@ -#include +#include int main(void) { - b_stream *src = b_stdin; - b_stream *dest = b_stdout; + fx_stream *src = fx_stdin; + fx_stream *dest = fx_stdout; #if 0 - b_serial_ctx *ctx; - b_serial_ctx_create(&ctx); + fx_serial_ctx *ctx; + fx_serial_ctx_create(&ctx); - b_object *data; - b_status status = b_serial_ctx_deserialise( - ctx, B_SERIAL_FORMAT_JSON, src, &data, 0); - if (!B_OK(status)) { + fx_object *data; + fx_status status = fx_serial_ctx_deserialise( + ctx, FX_SERIAL_FORMAT_JSON, src, &data, 0); + if (!FX_OK(status)) { return -1; } - b_to_string(B_OBJECT(data), dest); - b_stream_write_char(b_stdout, '\n'); + fx_to_string(FX_OBJECT(data), dest); + fx_stream_write_char(fx_stdout, '\n'); - b_release(data); - b_serial_ctx_destroy(ctx); + fx_release(data); + fx_serial_ctx_destroy(ctx); #endif return 0; diff --git a/test/term/errors.c b/test/term/errors.c index efa35e2..dd9c4b6 100644 --- a/test/term/errors.c +++ b/test/term/errors.c @@ -1,5 +1,5 @@ -#include -#include +#include +#include #include enum sample_code { @@ -13,25 +13,25 @@ enum sample_msg { SAMPLE_MSG_A_TEMPLATED_MSG, }; -static const b_error_definition sample_errors[] = { - B_ERROR_DEFINITION(SAMPLE_OK, "OK", "Success"), - B_ERROR_DEFINITION(SAMPLE_ERR_IO_FAILURE, "IO_FAILURE", "I/O failure"), - B_ERROR_DEFINITION_TEMPLATE( +static const fx_error_definition sample_errors[] = { + FX_ERROR_DEFINITION(SAMPLE_OK, "OK", "Success"), + FX_ERROR_DEFINITION(SAMPLE_ERR_IO_FAILURE, "IO_FAILURE", "I/O failure"), + FX_ERROR_DEFINITION_TEMPLATE( SAMPLE_ERR_FILE_READ_FAILED, "FILE_READ_FAILED", "Failed to read file @i[filepath]", - B_ERROR_TEMPLATE_PARAM( - "filepath", B_ERROR_TEMPLATE_PARAM_STRING, "%s")), + FX_ERROR_TEMPLATE_PARAM( + "filepath", FX_ERROR_TEMPLATE_PARAM_STRING, "%s")), }; -static const b_error_msg sample_error_msg[] = { - B_ERROR_MSG_TEMPLATE( +static const fx_error_msg sample_error_msg[] = { + FX_ERROR_MSG_TEMPLATE( SAMPLE_MSG_A_TEMPLATED_MSG, "A templated message: @e[param1]", - B_ERROR_TEMPLATE_PARAM( - "param1", B_ERROR_TEMPLATE_PARAM_STRING, "%s")), + FX_ERROR_TEMPLATE_PARAM( + "param1", FX_ERROR_TEMPLATE_PARAM_STRING, "%s")), }; static const char *sample_code_to_string( - const struct b_error_vendor *vendor, b_error_status_code code) + const struct fx_error_vendor *vendor, fx_error_status_code code) { switch (code) { case SAMPLE_OK: @@ -45,7 +45,7 @@ static const char *sample_code_to_string( } } -static b_error_vendor sample_vendor = { +static fx_error_vendor sample_vendor = { .v_name = "Sample", .v_error_definitions = sample_errors, .v_error_definitions_length = sizeof sample_errors, @@ -53,31 +53,31 @@ static b_error_vendor sample_vendor = { .v_msg_length = sizeof sample_error_msg, }; -static b_result error_return_3(void) +static fx_result error_return_3(void) { - b_result err = b_error_with_string( + fx_result err = fx_error_with_string( &sample_vendor, SAMPLE_ERR_IO_FAILURE, "I/O failure while reading file"); - b_error_add_submsg_string( - err, B_ERROR_SUBMSG_ERROR, "An @e{error} message"); - b_error_add_submsg_string( - err, B_ERROR_SUBMSG_WARNING, "A @w{warning} message"); - b_error_add_submsg_template( - err, B_ERROR_SUBMSG_WARNING, SAMPLE_MSG_A_TEMPLATED_MSG, - B_ERROR_PARAM("param1", "Hello!")); + fx_error_add_submsg_string( + err, FX_ERROR_SUBMSG_ERROR, "An @e{error} message"); + fx_error_add_submsg_string( + err, FX_ERROR_SUBMSG_WARNING, "A @w{warning} message"); + fx_error_add_submsg_template( + err, FX_ERROR_SUBMSG_WARNING, SAMPLE_MSG_A_TEMPLATED_MSG, + FX_ERROR_PARAM("param1", "Hello!")); return err; } -static b_result error_return_2(void) +static fx_result error_return_2(void) { - return b_result_propagate(error_return_3()); + return fx_result_propagate(error_return_3()); } -static b_result error_return_1(void) +static fx_result error_return_1(void) { - return b_result_propagate(error_return_2()); + return fx_result_propagate(error_return_2()); } struct param { @@ -100,39 +100,39 @@ static void __test(struct param params[]) #define test(...) __test((struct param[]) {__VA_ARGS__, {}}) -static b_result some_operation(void) +static fx_result some_operation(void) { - b_result result = error_return_2(); - if (b_result_is_error(result)) { - b_result err = b_error_with_template( + fx_result result = error_return_2(); + if (fx_result_is_error(result)) { + fx_result err = fx_error_with_template( &sample_vendor, SAMPLE_ERR_FILE_READ_FAILED, - B_ERROR_PARAM("filepath", "src/Manifest.json")); - b_error_add_submsg_string( - err, B_ERROR_SUBMSG_INFO, + FX_ERROR_PARAM("filepath", "src/Manifest.json")); + fx_error_add_submsg_string( + err, FX_ERROR_SUBMSG_INFO, "An @i{informational} message"); - b_error_caused_by_b_status(result, B_ERR_IO_FAILURE); - b_result err2 = b_error_caused_by(err, result); + fx_error_caused_by_fx_status(result, FX_ERR_IO_FAILURE); + fx_result err2 = fx_error_caused_by(err, result); return err2; } - return B_RESULT_SUCCESS; + return FX_RESULT_SUCCESS; } int main(void) { - b_set_error_report_function(b_enhanced_error_reporter, B_ERROR_REPORT_ALL); + fx_set_error_report_function(fx_enhanced_error_reporter, FX_ERROR_REPORT_ALL); test(PARAM("Hello", 1), PARAM("Goodbye", 2)); - b_result err; - if (B_CATCH(err, some_operation())) { - b_throw(err); + fx_result err; + if (FX_CATCH(err, some_operation())) { + fx_throw(err); } - b_throw_status(B_ERR_INVALID_ARGUMENT); - b_throw_status_string(B_ERR_INVALID_ARGUMENT, "Hello!"); + fx_throw_status(FX_ERR_INVALID_ARGUMENT); + fx_throw_status_string(FX_ERR_INVALID_ARGUMENT, "Hello!"); return 0; } diff --git a/test/term/printing.c b/test/term/printing.c index 95cf086..c6492fb 100644 --- a/test/term/printing.c +++ b/test/term/printing.c @@ -1,6 +1,6 @@ -#include -#include -#include +#include +#include +#include #include #define F_GREEN "[green]" @@ -55,25 +55,25 @@ static const char *text2 int main(void) { const char *s = "[magenta,uline]Hello, [bright_magenta]world![reset]"; - b_puts(s); - b_putc('\n'); + fx_puts(s); + fx_putc('\n'); - b_string *str = b_string_create_from_cstr(s); - size_t len = b_string_get_size(str, B_STRLEN_IGNORE_MOD); + fx_string *str = fx_string_create_from_cstr(s); + size_t len = fx_string_get_size(str, FX_STRLEN_IGNORE_MOD); printf("length = %zu\n", len); - b_paragraph_format format = { 0 }; + fx_paragraph_format format = { 0 }; format.p_left_margin = 5; format.p_right_margin = 5; - format.p_flags = B_PARAGRAPH_DOUBLE_LINE_BREAK; + format.p_flags = FX_PARAGRAPH_DOUBLE_LINE_BREAK; - b_print_paragraph(text, b_stdtty, &format); + fx_print_paragraph(text, fx_stdtty, &format); - b_i("An informational message\n\nWith multiple lines"); - b_warn("A warning message\nWith multiple lines"); - b_err("An error message\nWith multiple lines"); + fx_i("An informational message\n\nWith multiple lines"); + fx_warn("A warning message\nWith multiple lines"); + fx_err("An error message\nWith multiple lines"); - b_printf("[red]formatting ignored: '%s'[reset]\n[dark_grey]dark text[reset]\n", "[blue]wow![reset]"); + fx_printf("[red]formatting ignored: '%s'[reset]\n[dark_grey]dark text[reset]\n", "[blue]wow![reset]"); return 0; } diff --git a/test/toml-read.c b/test/toml-read.c index 0234518..85a9b0e 100644 --- a/test/toml-read.c +++ b/test/toml-read.c @@ -1,8 +1,7 @@ -#include "blue/serial/toml.h" - -#include -#include -#include +#include +#include +#include +#include #include int main(int argc, const char **argv) @@ -12,20 +11,20 @@ int main(int argc, const char **argv) } const char *path_cstr = argv[1]; - b_path *path = b_path_create_from_cstr(path_cstr); + fx_path *path = fx_path_create_from_cstr(path_cstr); - b_file *src = NULL; - b_result result = b_file_open(NULL, path, B_FILE_READ_ONLY, &src); - if (b_result_is_error(result)) { - b_throw(result); + fx_file *src = NULL; + fx_result result = fx_file_open(NULL, path, FX_FILE_READ_ONLY, &src); + if (fx_result_is_error(result)) { + fx_throw(result); return -1; } - b_serial_ctx *ctx = b_toml_serial_ctx_create(); + fx_serial_ctx *ctx = fx_toml_serial_ctx_create(); - b_object *data = NULL; - b_status status = b_serial_ctx_deserialise(ctx, src, &data, 0); - if (!B_OK(status)) { + fx_object *data = NULL; + fx_status status = fx_serial_ctx_deserialise(ctx, src, &data, 0); + if (!FX_OK(status)) { fprintf(stderr, "cannot read data\n"); return -1; } @@ -34,12 +33,12 @@ int main(int argc, const char **argv) return 0; } - b_object_to_string(data, b_stdout); - b_stream_write_char(b_stdout, '\n'); + fx_object_to_string(data, fx_stdout); + fx_stream_write_char(fx_stdout, '\n'); - b_object_unref(data); - b_object_unref(src); - b_serial_ctx_unref(ctx); + fx_object_unref(data); + fx_object_unref(src); + fx_serial_ctx_unref(ctx); return 0; }